Skip to contentSkip to conversation

tpm: introduce TPM CRB SysBus device

PATCH In review Joelle van Dyne 4 participants 35 messages Nov 14, 2023 – Apr 30, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v5 cover tpm: introduce TPM CRB SysBus device

The impetus for this patch set is to get TPM 2.0 working on Windows 11 ARM64. Windows' tpm.sys does not seem to work on a TPM TIS device (as verified with VMWare's implementation). However, the current TPM CRB device uses a fixed system bus address that is reserved for RAM in ARM64 Virt machines.

In the process of adding the TPM CRB SysBus device, we also went ahead and cleaned up some of the existing TPM hardware code and fixed some bugs. We used the TPM TIS devices as a template for the TPM CRB devices and refactored out common code. We moved the ACPI DSDT generation to the device in order to handle dynamic base address requirements as well as reduce redundent code in different machine ACPI generation. We also changed the tpm_crb device to use the ISA bus instead of depending on the default system bus as the device only was built for the PC configuration.

Another change is that the TPM CRB registers are now mapped in the same way that the pflash ROM devices are mapped. It is a memory region whose writes are trapped as MMIO accesses. This was needed because Apple Silicon does not decode LDP (AARCH64 load pair of registers) caused page faults. @agraf suggested that we do this to avoid having to do AARCH64 decoding in the HVF backend's fault handler.

Unfortunately, it seems like the LDP fault still happens on HVF but the issue seems to be in the HVF backend which needs to be fixed in a separate patch.

One last thing that's needed to get Windows 11 to recognize the TPM 2.0 device is for the OVMF firmware to setup the TPM device. Currently, OVMF for ARM64 Virt only recognizes the TPM TIS device through a FDT entry. A workaround is to falsely identify the TPM CRB device as a TPM TIS device in the FDT node but this causes issues for Linux. A proper fix would involve adding an ACPI device driver in OVMF.

This has been tested on ARM64 with tpm-crb-device and on x86_64 with tpm-crb. Additional testing should be performed on other architectures (RISCV and Loongarch for example) as well as migration cases.

v5:
- Fixed a typo in "tpm_crb: use a single read-as-mem/write-as-mmio mapping"
- Fixed ACPI tables not being created for pc CRB device

v4:
- Fixed broken test blobs

v3:
- Support backwards and forwards migration of existing tpm-crb device
- Dropped patch which moved tpm-crb to ISA bus due to migration concerns
- Unified tpm_sysbus_plug handler for ARM and Loongarch
- Added ACPI table tests for tpm-crb-device
- Refactored TPM CRB tests to run on tpm-crb-device for ARM Virt

v2:
- Fixed an issue where VMstate restore from an older version failed due to name collision of the memory block.
- In the ACPI table generation for CRB devices, the check for TPM 2.0 backend is moved to the device realize as CRB does not support TPM 1.0. It will error in that case.
- Dropped the patch to fix crash when PPI is enabled on TIS SysBus device since a separate patch submitted by Stefan Berger disables such an option.
- Fixed an issue where we default tpmEstablished=0 when it should be 1.
- In TPM CRB SysBus's ACPI entry, we accidently changed _UID from 0 to 1. This shouldn't be an issue but we changed it back just in case.
- Added a patch to migrate saved VMstate from an older version with the regs saved separately instead of as a RAM block.

Joelle van Dyne (14):
tpm_crb: refactor common code
tpm_crb: CTRL_RSP_ADDR is 64-bits wide
tpm_ppi: refactor memory space initialization
tpm_crb: use a single read-as-mem/write-as-mmio mapping
tpm_crb: move ACPI table building to device interface
tpm-sysbus: add plug handler for TPM on SysBus hw/arm/virt: connect TPM to platform bus hw/loongarch/virt: connect TPM to platform bus
tpm_tis_sysbus: move DSDT AML generation to device
tests: acpi: prepare for TPM CRB tests
tpm_crb_sysbus: introduce TPM CRB SysBus device
tests: acpi: implement TPM CRB tests for ARM virt
tests: acpi: updated expected blobs for TPM CRB
tests: add TPM-CRB sysbus tests for aarch64

 docs/specs/tpm.rst                        |   2 +
 hw/tpm/tpm_crb.h                          |  79 ++++++
 hw/tpm/tpm_ppi.h                          |  10 +-
 include/hw/acpi/tpm.h                     |   3 +-
 include/sysemu/tpm.h                      |   7 +
 tests/qtest/tpm-tests.h                   |   2 +
 tests/qtest/tpm-util.h                    |   4 +-
 hw/acpi/aml-build.c                       |   7 +-
 hw/arm/virt-acpi-build.c                  |  38 +--
 hw/arm/virt.c                             |   8 +
 hw/core/sysbus-fdt.c                      |   1 +
 hw/i386/acpi-build.c                      |  16 +-
 hw/loongarch/acpi-build.c                 |  38 +--
 hw/loongarch/virt.c                       |   8 +
 hw/riscv/virt.c                           |   1 +
 hw/tpm/tpm-sysbus.c                       |  47 ++++
 hw/tpm/tpm_crb.c                          | 302 ++++++----------------
 hw/tpm/tpm_crb_common.c                   | 262 +++++++++++++++++++
 hw/tpm/tpm_crb_sysbus.c                   | 162 ++++++++++++
 hw/tpm/tpm_ppi.c                          |   5 +-
 hw/tpm/tpm_tis_isa.c                      |   5 +-
 hw/tpm/tpm_tis_sysbus.c                   |  37 +++
 tests/qtest/bios-tables-test.c            |  47 +++-
 tests/qtest/tpm-crb-device-swtpm-test.c   |  72 ++++++
 tests/qtest/tpm-crb-device-test.c         |  71 +++++
 tests/qtest/tpm-crb-swtpm-test.c          |   2 +
 tests/qtest/tpm-crb-test.c                | 121 +--------
 tests/qtest/tpm-tests.c                   | 121 +++++++++
 tests/qtest/tpm-tis-device-swtpm-test.c   |   2 +-
 tests/qtest/tpm-tis-device-test.c         |   2 +-
 tests/qtest/tpm-tis-i2c-test.c            |   3 +
 tests/qtest/tpm-tis-swtpm-test.c          |   2 +-
 tests/qtest/tpm-tis-test.c                |   2 +-
 tests/qtest/tpm-util.c                    |  16 +-
 hw/arm/Kconfig                            |   1 +
 hw/loongarch/Kconfig                      |   2 +
 hw/riscv/Kconfig                          |   1 +
 hw/tpm/Kconfig                            |   5 +
 hw/tpm/meson.build                        |   5 +
 hw/tpm/trace-events                       |   2 +-
 tests/data/acpi/q35/DSDT.crb.tpm2         | Bin 0 -> 8355 bytes
 tests/data/acpi/q35/TPM2.crb.tpm2         | Bin 0 -> 76 bytes
 tests/data/acpi/virt/DSDT.crb-device.tpm2 | Bin 0 -> 5276 bytes
 tests/data/acpi/virt/TPM2.crb-device.tpm2 | Bin 0 -> 76 bytes
 tests/qtest/meson.build                   |   4 +
 45 files changed, 1057 insertions(+), 468 deletions(-)
 create mode 100644 hw/tpm/tpm_crb.h
 create mode 100644 hw/tpm/tpm-sysbus.c
 create mode 100644 hw/tpm/tpm_crb_common.c
 create mode 100644 hw/tpm/tpm_crb_sysbus.c
 create mode 100644 tests/qtest/tpm-crb-device-swtpm-test.c
 create mode 100644 tests/qtest/tpm-crb-device-test.c
 create mode 100644 tests/data/acpi/q35/DSDT.crb.tpm2
 create mode 100644 tests/data/acpi/q35/TPM2.crb.tpm2
 create mode 100644 tests/data/acpi/virt/DSDT.crb-device.tpm2
 create mode 100644 tests/data/acpi/virt/TPM2.crb-device.tpm2
Signature · 1 line
2.41.0
#1 lore raw

PATCH v5 1/14 tpm_crb: refactor common code

In preparation for the SysBus variant, we move common code styled after the TPM TIS devices.

To maintain compatibility, we do not rename the existing tpm-crb device.

---
 docs/specs/tpm.rst      |   1 +
 hw/tpm/tpm_crb.h        |  76 +++++++++++
 hw/tpm/tpm_crb.c        | 270 ++++++----------------------------------
 hw/tpm/tpm_crb_common.c | 216 ++++++++++++++++++++++++++++++++
 hw/tpm/meson.build      |   1 +
 hw/tpm/trace-events     |   2 +-
 6 files changed, 331 insertions(+), 235 deletions(-)
 create mode 100644 hw/tpm/tpm_crb.h
 create mode 100644 hw/tpm/tpm_crb_common.c
docs/specs/tpm.rst +1 -0
index efe124a148..2bc29c9804 100644
@@ -45,6 +45,7 @@ operating system.
 
 QEMU files related to TPM CRB interface:
  - ``hw/tpm/tpm_crb.c``
+ - ``hw/tpm/tpm_crb_common.c``
 
 SPAPR interface
 ---------------
hw/tpm/tpm_crb.h new file +76 -0

76 lines not on this page · raw message

hw/tpm/tpm_crb.c +36 -234

357 lines not on this page · raw message

hw/tpm/tpm_crb_common.c new file +216 -0

216 lines not on this page · raw message

hw/tpm/meson.build +1 -0
index 6968e60b3f..cb8204d5bc 100644
@@ -3,6 +3,7 @@ system_ss.add(when: 'CONFIG_TPM_TIS_ISA', if_true: files('tpm_tis_isa.c'))
 system_ss.add(when: 'CONFIG_TPM_TIS_SYSBUS', if_true: files('tpm_tis_sysbus.c'))
 system_ss.add(when: 'CONFIG_TPM_TIS_I2C', if_true: files('tpm_tis_i2c.c'))
 system_ss.add(when: 'CONFIG_TPM_CRB', if_true: files('tpm_crb.c'))
+system_ss.add(when: 'CONFIG_TPM_CRB', if_true: files('tpm_crb_common.c'))
 system_ss.add(when: 'CONFIG_TPM_TIS', if_true: files('tpm_ppi.c'))
 system_ss.add(when: 'CONFIG_TPM_CRB', if_true: files('tpm_ppi.c'))
 
hw/tpm/trace-events +1 -1
index fa882dfefe..3ab1bdb97b 100644
@@ -1,6 +1,6 @@
 # See docs/devel/tracing.rst for syntax documentation.
 
-# tpm_crb.c
+# tpm_crb_common.c
 tpm_crb_mmio_read(uint64_t addr, unsigned size, uint32_t val) "CRB read 0x%016" PRIx64 " len:%u val: 0x%" PRIx32
 tpm_crb_mmio_write(uint64_t addr, unsigned size, uint32_t val) "CRB write 0x%016" PRIx64 " len:%u val: 0x%" PRIx32
 
Signature · 1 line
2.41.0
#2 lore raw

PATCH v5 2/14 tpm_crb: CTRL_RSP_ADDR is 64-bits wide

In reply to Joelle van Dyne

The register is actually 64-bits but in order to make this more clear than the specification, we define two 32-bit registers:
CTRL_RSP_LADDR and CTRL_RSP_HADDR to match the CTRL_CMD_* naming. This deviates from the specs but is way more clear.

Previously, the only CRB device uses a fixed system address so this was not an issue. However, once we support SysBus CRB device, the address can be anywhere in 64-bit space.

---
 include/hw/acpi/tpm.h      | 3 ++-
 hw/tpm/tpm_crb_common.c    | 3 ++-
 tests/qtest/tpm-crb-test.c | 2 +-
 tests/qtest/tpm-util.c     | 2 +-
 4 files changed, 6 insertions(+), 4 deletions(-)
include/hw/acpi/tpm.h +2 -1
index 579c45f5ba..f60bfe2789 100644
@@ -174,7 +174,8 @@ REG32(CRB_CTRL_CMD_SIZE, 0x58)
 REG32(CRB_CTRL_CMD_LADDR, 0x5C)
 REG32(CRB_CTRL_CMD_HADDR, 0x60)
 REG32(CRB_CTRL_RSP_SIZE, 0x64)
-REG32(CRB_CTRL_RSP_ADDR, 0x68)
+REG32(CRB_CTRL_RSP_LADDR, 0x68)
+REG32(CRB_CTRL_RSP_HADDR, 0x6C)
 REG32(CRB_DATA_BUFFER, 0x80)
 
 #define TPM_CRB_ADDR_BASE           0xFED40000
hw/tpm/tpm_crb_common.c +2 -1
index fa463f295f..01b35808f6 100644
@@ -197,7 +197,8 @@ void tpm_crb_reset(TPMCRBState *s, uint64_t baseaddr)
     s->regs[R_CRB_CTRL_CMD_LADDR] = (uint32_t)baseaddr;
     s->regs[R_CRB_CTRL_CMD_HADDR] = (uint32_t)(baseaddr >> 32);
     s->regs[R_CRB_CTRL_RSP_SIZE] = CRB_CTRL_CMD_SIZE;
-    s->regs[R_CRB_CTRL_RSP_ADDR] = (uint32_t)baseaddr;
+    s->regs[R_CRB_CTRL_RSP_LADDR] = (uint32_t)baseaddr;
+    s->regs[R_CRB_CTRL_RSP_HADDR] = (uint32_t)(baseaddr >> 32);
 
     s->be_buffer_size = MIN(tpm_backend_get_buffer_size(s->tpmbe),
                             CRB_CTRL_CMD_SIZE);
tests/qtest/tpm-crb-test.c +1 -1
index 396ae3f91c..9d30fe8293 100644
@@ -28,7 +28,7 @@ static void tpm_crb_test(const void *data)
     uint32_t csize = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_SIZE);
     uint64_t caddr = readq(TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_LADDR);
     uint32_t rsize = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_SIZE);
-    uint64_t raddr = readq(TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_ADDR);
+    uint64_t raddr = readq(TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_LADDR);
     uint8_t locstate = readb(TPM_CRB_ADDR_BASE + A_CRB_LOC_STATE);
     uint32_t locctrl = readl(TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL);
     uint32_t locsts = readl(TPM_CRB_ADDR_BASE + A_CRB_LOC_STS);
tests/qtest/tpm-util.c +1 -1
index 1c0319e6e7..dd02057fc0 100644
@@ -25,7 +25,7 @@ void tpm_util_crb_transfer(QTestState *s,
                            unsigned char *rsp, size_t rsp_size)
 {
     uint64_t caddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_LADDR);
-    uint64_t raddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_ADDR);
+    uint64_t raddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_LADDR);
 
     qtest_writeb(s, TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL, 1);
 
Signature · 1 line
2.41.0
#3 lore raw

PATCH v5 3/14 tpm_ppi: refactor memory space initialization

In reply to Joelle van Dyne

Instead of calling memory_region_add_subregion directly, we defer to the caller to do it. This allows us to re-use the code for a SysBus device.

---
 hw/tpm/tpm_ppi.h        | 10 +++-------
 hw/tpm/tpm_crb.c        |  4 ++--
 hw/tpm/tpm_crb_common.c |  3 +++
 hw/tpm/tpm_ppi.c        |  5 +----
 hw/tpm/tpm_tis_isa.c    |  5 +++--
 5 files changed, 12 insertions(+), 15 deletions(-)
hw/tpm/tpm_ppi.h +3 -7
index bf5d4a300f..30863c6438 100644
@@ -20,17 +20,13 @@ typedef struct TPMPPI {
 } TPMPPI;
 
 /**
- * tpm_ppi_init:
+ * tpm_ppi_init_memory:
  * @tpmppi: a TPMPPI
- * @m: the address-space / MemoryRegion to use
- * @addr: the address of the PPI region
  * @obj: the owner object
  *
- * Register the TPM PPI memory region at @addr on the given address
- * space for the object @obj.
+ * Creates the TPM PPI memory region.
  **/
-void tpm_ppi_init(TPMPPI *tpmppi, MemoryRegion *m,
-                  hwaddr addr, Object *obj);
+void tpm_ppi_init_memory(TPMPPI *tpmppi, Object *obj);
 
 /**
  * tpm_ppi_reset:
hw/tpm/tpm_crb.c +2 -2
index 3ef4977fb5..598c3e0161 100644
@@ -107,8 +107,8 @@ static void tpm_crb_none_realize(DeviceState *dev, Error **errp)
         TPM_CRB_ADDR_BASE + sizeof(s->state.regs), &s->state.cmdmem);
 
     if (s->state.ppi_enabled) {
-        tpm_ppi_init(&s->state.ppi, get_system_memory(),
-                     TPM_PPI_ADDR_BASE, OBJECT(s));
+        memory_region_add_subregion(get_system_memory(),
+            TPM_PPI_ADDR_BASE, &s->state.ppi.ram);
     }
 
     if (xen_enabled()) {
hw/tpm/tpm_crb_common.c +3 -0
index 01b35808f6..bee0b71fee 100644
@@ -214,4 +214,7 @@ void tpm_crb_init_memory(Object *obj, TPMCRBState *s, Error **errp)
         "tpm-crb-mmio", sizeof(s->regs));
     memory_region_init_ram(&s->cmdmem, obj,
         "tpm-crb-cmd", CRB_CTRL_CMD_SIZE, errp);
+    if (s->ppi_enabled) {
+        tpm_ppi_init_memory(&s->ppi, obj);
+    }
 }
hw/tpm/tpm_ppi.c +1 -4
index 7f74e26ec6..40cab59afa 100644
@@ -44,14 +44,11 @@ void tpm_ppi_reset(TPMPPI *tpmppi)
     }
 }
 
-void tpm_ppi_init(TPMPPI *tpmppi, MemoryRegion *m,
-                  hwaddr addr, Object *obj)
+void tpm_ppi_init_memory(TPMPPI *tpmppi, Object *obj)
 {
     tpmppi->buf = qemu_memalign(qemu_real_host_page_size(),
                                 HOST_PAGE_ALIGN(TPM_PPI_ADDR_SIZE));
     memory_region_init_ram_device_ptr(&tpmppi->ram, obj, "tpm-ppi",
                                       TPM_PPI_ADDR_SIZE, tpmppi->buf);
     vmstate_register_ram(&tpmppi->ram, DEVICE(obj));
-
-    memory_region_add_subregion(m, addr, &tpmppi->ram);
 }
hw/tpm/tpm_tis_isa.c +3 -2
index 0367401586..d596f38c0f 100644
@@ -134,8 +134,9 @@ static void tpm_tis_isa_realizefn(DeviceState *dev, Error **errp)
                                 TPM_TIS_ADDR_BASE, &s->mmio);
 
     if (s->ppi_enabled) {
-        tpm_ppi_init(&s->ppi, isa_address_space(ISA_DEVICE(dev)),
-                     TPM_PPI_ADDR_BASE, OBJECT(dev));
+        tpm_ppi_init_memory(&s->ppi, OBJECT(dev));
+        memory_region_add_subregion(isa_address_space(ISA_DEVICE(dev)),
+                                    TPM_PPI_ADDR_BASE, &s->ppi.ram);
     }
 }
 
Signature · 1 line
2.41.0
#4 lore raw

PATCH v5 4/14 tpm_crb: use a single read-as-mem/write-as-mmio mapping

In reply to Joelle van Dyne

On Apple Silicon, when Windows performs a LDP on the CRB MMIO space, the exception is not decoded by hardware and we cannot trap the MMIO read. This led to the idea from @agraf to use the same mapping type as ROM devices: namely that reads should be seen as memory type and writes should trap as MMIO.

Once that was done, the second memory mapping of the command buffer region was redundent and was removed.

A note about the removal of the read trap for CRB_LOC_STATE:
The only usage was to return the most up-to-date value for tpmEstablished. However, tpmEstablished is only cleared when a TPM2_HashStart operation is called which only exists for locality 4. We do not handle locality 4. Indeed, the comment for the write handler of CRB_LOC_CTRL makes the same argument for why it is not calling the backend to reset the tpmEstablished bit (to 1).
As this bit is unused, we do not need to worry about updating it for reads.

In order to maintain migration compatibility with older versions of QEMU, we store a copy of the register data and command data which is used only during save/restore.

---
 hw/tpm/tpm_crb.h        |   5 +-
 hw/tpm/tpm_crb.c        |  30 ++++++++-
 hw/tpm/tpm_crb_common.c | 145 +++++++++++++++++++++++-----------------
 3 files changed, 114 insertions(+), 66 deletions(-)
hw/tpm/tpm_crb.h +3 -2
index da3a0cf256..36863e1664 100644
@@ -26,9 +26,7 @@
 typedef struct TPMCRBState {
     TPMBackend *tpmbe;
     TPMBackendCmd cmd;
-    uint32_t regs[TPM_CRB_R_MAX];
     MemoryRegion mmio;
-    MemoryRegion cmdmem;
 
     size_t be_buffer_size;
 
@@ -72,5 +70,8 @@ enum TPMVersion tpm_crb_get_version(TPMCRBState *s);
 int tpm_crb_pre_save(TPMCRBState *s);
 void tpm_crb_reset(TPMCRBState *s, uint64_t baseaddr);
 void tpm_crb_init_memory(Object *obj, TPMCRBState *s, Error **errp);
+void tpm_crb_mem_save(TPMCRBState *s, uint32_t *saved_regs, void *saved_cmdmem);
+void tpm_crb_mem_load(TPMCRBState *s, const uint32_t *saved_regs,
+                      const void *saved_cmdmem);
 
 #endif /* TPM_TPM_CRB_H */
hw/tpm/tpm_crb.c +27 -3
index 598c3e0161..99c64dd72a 100644
@@ -37,6 +37,10 @@ struct CRBState {
     DeviceState parent_obj;
 
     TPMCRBState state;
+
+    /* These states are only for migration */
+    uint32_t saved_regs[TPM_CRB_R_MAX];
+    MemoryRegion saved_cmdmem;
 };
 typedef struct CRBState CRBState;
 
@@ -57,18 +61,36 @@ static enum TPMVersion tpm_crb_none_get_version(TPMIf *ti)
     return tpm_crb_get_version(&s->state);
 }
 
+/**
+ * For migrating to an older version of QEMU
+ */
 static int tpm_crb_none_pre_save(void *opaque)
 {
     CRBState *s = opaque;
+    void *saved_cmdmem = memory_region_get_ram_ptr(&s->saved_cmdmem);
 
+    tpm_crb_mem_save(&s->state, s->saved_regs, saved_cmdmem);
     return tpm_crb_pre_save(&s->state);
 }
 
+/**
+ * For migrating from an older version of QEMU
+ */
+static int tpm_crb_none_post_load(void *opaque, int version_id)
+{
+    CRBState *s = opaque;
+    void *saved_cmdmem = memory_region_get_ram_ptr(&s->saved_cmdmem);
+
+    tpm_crb_mem_load(&s->state, s->saved_regs, saved_cmdmem);
+    return 0;
+}
+
 static const VMStateDescription vmstate_tpm_crb_none = {
     .name = "tpm-crb",
     .pre_save = tpm_crb_none_pre_save,
+    .post_load = tpm_crb_none_post_load,
     .fields = (VMStateField[]) {
-        VMSTATE_UINT32_ARRAY(state.regs, CRBState, TPM_CRB_R_MAX),
+        VMSTATE_UINT32_ARRAY(saved_regs, CRBState, TPM_CRB_R_MAX),
         VMSTATE_END_OF_LIST(),
     }
 };
@@ -101,10 +123,12 @@ static void tpm_crb_none_realize(DeviceState *dev, Error **errp)
 
     tpm_crb_init_memory(OBJECT(s), &s->state, errp);
 
+    /* only used for migration */
+    memory_region_init_ram(&s->saved_cmdmem, OBJECT(s),
+        "tpm-crb-cmd", CRB_CTRL_CMD_SIZE, errp);
+
     memory_region_add_subregion(get_system_memory(),
         TPM_CRB_ADDR_BASE, &s->state.mmio);
-    memory_region_add_subregion(get_system_memory(),
-        TPM_CRB_ADDR_BASE + sizeof(s->state.regs), &s->state.cmdmem);
 
     if (s->state.ppi_enabled) {
         memory_region_add_subregion(get_system_memory(),
hw/tpm/tpm_crb_common.c +84 -61

248 lines not on this page · raw message

Signature · 1 line
2.41.0
Reviewed-by on v5 4/14
#5 lore raw

Re: [PATCH v5 04/14] tpm_crb: use a single read-as-mem/write-as-mmio mapping

Quoted 19 lines · Joelle van Dyne

On Apple Silicon, when Windows performs a LDP on the CRB MMIO space, the exception is not decoded by hardware and we cannot trap the MMIO read. This led to the idea from @agraf to use the same mapping type as ROM devices: namely that reads should be seen as memory type and writes should trap as MMIO.

Once that was done, the second memory mapping of the command buffer region was redundent and was removed.

A note about the removal of the read trap for CRB_LOC_STATE:
The only usage was to return the most up-to-date value for tpmEstablished. However, tpmEstablished is only cleared when a TPM2_HashStart operation is called which only exists for locality 4. We do not handle locality 4. Indeed, the comment for the write handler of CRB_LOC_CTRL makes the same argument for why it is not calling the backend to reset the tpmEstablished bit (to 1).
As this bit is unused, we do not need to worry about updating it for reads.

Quoted 5 lines

In order to maintain migration compatibility with older versions of QEMU, we store a copy of the register data and command data which is used only during save/restore.

Quoted 355 lines
---
  hw/tpm/tpm_crb.h        |   5 +-
  hw/tpm/tpm_crb.c        |  30 ++++++++-
  hw/tpm/tpm_crb_common.c | 145 +++++++++++++++++++++++-----------------
  3 files changed, 114 insertions(+), 66 deletions(-)
hw/tpm/tpm_crb.h +3 -2
index da3a0cf256..36863e1664 100644
@@ -26,9 +26,7 @@
  typedef struct TPMCRBState {
      TPMBackend *tpmbe;
      TPMBackendCmd cmd;
-    uint32_t regs[TPM_CRB_R_MAX];
      MemoryRegion mmio;
-    MemoryRegion cmdmem;
 
      size_t be_buffer_size;
 
@@ -72,5 +70,8 @@ enum TPMVersion tpm_crb_get_version(TPMCRBState *s);
  int tpm_crb_pre_save(TPMCRBState *s);
  void tpm_crb_reset(TPMCRBState *s, uint64_t baseaddr);
  void tpm_crb_init_memory(Object *obj, TPMCRBState *s, Error **errp);
+void tpm_crb_mem_save(TPMCRBState *s, uint32_t *saved_regs, void *saved_cmdmem);
+void tpm_crb_mem_load(TPMCRBState *s, const uint32_t *saved_regs,
+                      const void *saved_cmdmem);
 
  #endif /* TPM_TPM_CRB_H */
hw/tpm/tpm_crb.c +27 -3
index 598c3e0161..99c64dd72a 100644
@@ -37,6 +37,10 @@ struct CRBState {
      DeviceState parent_obj;
 
      TPMCRBState state;
+
+    /* These states are only for migration */
+    uint32_t saved_regs[TPM_CRB_R_MAX];
+    MemoryRegion saved_cmdmem;
  };
  typedef struct CRBState CRBState;
 
@@ -57,18 +61,36 @@ static enum TPMVersion tpm_crb_none_get_version(TPMIf *ti)
      return tpm_crb_get_version(&s->state);
  }
 
+/**
+ * For migrating to an older version of QEMU
+ */
  static int tpm_crb_none_pre_save(void *opaque)
  {
      CRBState *s = opaque;
+    void *saved_cmdmem = memory_region_get_ram_ptr(&s->saved_cmdmem);
 
+    tpm_crb_mem_save(&s->state, s->saved_regs, saved_cmdmem);
      return tpm_crb_pre_save(&s->state);
  }
 
+/**
+ * For migrating from an older version of QEMU
+ */
+static int tpm_crb_none_post_load(void *opaque, int version_id)
+{
+    CRBState *s = opaque;
+    void *saved_cmdmem = memory_region_get_ram_ptr(&s->saved_cmdmem);
+
+    tpm_crb_mem_load(&s->state, s->saved_regs, saved_cmdmem);
+    return 0;
+}
+
  static const VMStateDescription vmstate_tpm_crb_none = {
      .name = "tpm-crb",
      .pre_save = tpm_crb_none_pre_save,
+    .post_load = tpm_crb_none_post_load,
      .fields = (VMStateField[]) {
-        VMSTATE_UINT32_ARRAY(state.regs, CRBState, TPM_CRB_R_MAX),
+        VMSTATE_UINT32_ARRAY(saved_regs, CRBState, TPM_CRB_R_MAX),
          VMSTATE_END_OF_LIST(),
      }
  };
@@ -101,10 +123,12 @@ static void tpm_crb_none_realize(DeviceState *dev, Error **errp)
 
      tpm_crb_init_memory(OBJECT(s), &s->state, errp);
 
+    /* only used for migration */
+    memory_region_init_ram(&s->saved_cmdmem, OBJECT(s),
+        "tpm-crb-cmd", CRB_CTRL_CMD_SIZE, errp);
+
      memory_region_add_subregion(get_system_memory(),
          TPM_CRB_ADDR_BASE, &s->state.mmio);
-    memory_region_add_subregion(get_system_memory(),
-        TPM_CRB_ADDR_BASE + sizeof(s->state.regs), &s->state.cmdmem);
 
      if (s->state.ppi_enabled) {
          memory_region_add_subregion(get_system_memory(),
hw/tpm/tpm_crb_common.c +84 -61
index bee0b71fee..f96a8cf299 100644
@@ -31,31 +31,12 @@
  #include "qom/object.h"
  #include "tpm_crb.h"
 
-static uint64_t tpm_crb_mmio_read(void *opaque, hwaddr addr,
-                                  unsigned size)
+static uint8_t tpm_crb_get_active_locty(TPMCRBState *s, uint32_t *regs)
  {
-    TPMCRBState *s = opaque;
-    void *regs = (void *)&s->regs + (addr & ~3);
-    unsigned offset = addr & 3;
-    uint32_t val = *(uint32_t *)regs >> (8 * offset);
-
-    switch (addr) {
-    case A_CRB_LOC_STATE:
-        val |= !tpm_backend_get_tpm_established_flag(s->tpmbe);
-        break;
-    }
-
-    trace_tpm_crb_mmio_read(addr, size, val);
-
-    return val;
-}
-
-static uint8_t tpm_crb_get_active_locty(TPMCRBState *s)
-{
-    if (!ARRAY_FIELD_EX32(s->regs, CRB_LOC_STATE, locAssigned)) {
+    if (!ARRAY_FIELD_EX32(regs, CRB_LOC_STATE, locAssigned)) {
          return TPM_CRB_NO_LOCALITY;
      }
-    return ARRAY_FIELD_EX32(s->regs, CRB_LOC_STATE, activeLocality);
+    return ARRAY_FIELD_EX32(regs, CRB_LOC_STATE, activeLocality);
  }
 
  static void tpm_crb_mmio_write(void *opaque, hwaddr addr,
@@ -63,35 +44,49 @@ static void tpm_crb_mmio_write(void *opaque, hwaddr addr,
  {
      TPMCRBState *s = opaque;
      uint8_t locty =  addr >> 12;
+    uint32_t *regs;
+    void *mem;
 
      trace_tpm_crb_mmio_write(addr, size, val);
+    regs = memory_region_get_ram_ptr(&s->mmio);
+    mem = &regs[R_CRB_DATA_BUFFER];
+    assert(regs);
+
+    /* receive TPM command bytes in DATA_BUFFER */
+    if (addr >= A_CRB_DATA_BUFFER) {
+        assert(addr + size <= TPM_CRB_ADDR_SIZE);
+        assert(size <= sizeof(val));
+        memcpy(mem + addr - A_CRB_DATA_BUFFER, &val, size);
+        memory_region_set_dirty(&s->mmio, addr, size);
+        return;
+    }
 
+    /* otherwise we are doing MMIO writes */
      switch (addr) {
      case A_CRB_CTRL_REQ:
          switch (val) {
          case CRB_CTRL_REQ_CMD_READY:
-            ARRAY_FIELD_DP32(s->regs, CRB_CTRL_STS,
+            ARRAY_FIELD_DP32(regs, CRB_CTRL_STS,
                               tpmIdle, 0);
              break;
          case CRB_CTRL_REQ_GO_IDLE:
-            ARRAY_FIELD_DP32(s->regs, CRB_CTRL_STS,
+            ARRAY_FIELD_DP32(regs, CRB_CTRL_STS,
                               tpmIdle, 1);
              break;
          }
          break;
      case A_CRB_CTRL_CANCEL:
          if (val == CRB_CANCEL_INVOKE &&
-            s->regs[R_CRB_CTRL_START] & CRB_START_INVOKE) {
+            regs[R_CRB_CTRL_START] & CRB_START_INVOKE) {
              tpm_backend_cancel_cmd(s->tpmbe);
          }
          break;
      case A_CRB_CTRL_START:
          if (val == CRB_START_INVOKE &&
-            !(s->regs[R_CRB_CTRL_START] & CRB_START_INVOKE) &&
-            tpm_crb_get_active_locty(s) == locty) {
-            void *mem = memory_region_get_ram_ptr(&s->cmdmem);
+            !(regs[R_CRB_CTRL_START] & CRB_START_INVOKE) &&
+            tpm_crb_get_active_locty(s, regs) == locty) {
 
-            s->regs[R_CRB_CTRL_START] |= CRB_START_INVOKE;
+            regs[R_CRB_CTRL_START] |= CRB_START_INVOKE;
              s->cmd = (TPMBackendCmd) {
                  .in = mem,
                  .in_len = MIN(tpm_cmd_get_size(mem), s->be_buffer_size),
@@ -108,26 +103,27 @@ static void tpm_crb_mmio_write(void *opaque, hwaddr addr,
              /* not loc 3 or 4 */
              break;
          case CRB_LOC_CTRL_RELINQUISH:
-            ARRAY_FIELD_DP32(s->regs, CRB_LOC_STATE,
+            ARRAY_FIELD_DP32(regs, CRB_LOC_STATE,
                               locAssigned, 0);
-            ARRAY_FIELD_DP32(s->regs, CRB_LOC_STS,
+            ARRAY_FIELD_DP32(regs, CRB_LOC_STS,
                               Granted, 0);
              break;
          case CRB_LOC_CTRL_REQUEST_ACCESS:
-            ARRAY_FIELD_DP32(s->regs, CRB_LOC_STS,
+            ARRAY_FIELD_DP32(regs, CRB_LOC_STS,
                               Granted, 1);
-            ARRAY_FIELD_DP32(s->regs, CRB_LOC_STS,
+            ARRAY_FIELD_DP32(regs, CRB_LOC_STS,
                               beenSeized, 0);
-            ARRAY_FIELD_DP32(s->regs, CRB_LOC_STATE,
+            ARRAY_FIELD_DP32(regs, CRB_LOC_STATE,
                               locAssigned, 1);
              break;
          }
          break;
      }
+
+    memory_region_set_dirty(&s->mmio, 0, A_CRB_DATA_BUFFER);
  }
 
  const MemoryRegionOps tpm_crb_memory_ops = {
-    .read = tpm_crb_mmio_read,
      .write = tpm_crb_mmio_write,
      .endianness = DEVICE_LITTLE_ENDIAN,
      .valid = {
@@ -138,12 +134,16 @@ const MemoryRegionOps tpm_crb_memory_ops = {
 
  void tpm_crb_request_completed(TPMCRBState *s, int ret)
  {
-    s->regs[R_CRB_CTRL_START] &= ~CRB_START_INVOKE;
+    uint32_t *regs = memory_region_get_ram_ptr(&s->mmio);
+
+    assert(regs);
+    regs[R_CRB_CTRL_START] &= ~CRB_START_INVOKE;
      if (ret != 0) {
-        ARRAY_FIELD_DP32(s->regs, CRB_CTRL_STS,
+        ARRAY_FIELD_DP32(regs, CRB_CTRL_STS,
                           tpmSts, 1); /* fatal error */
      }
-    memory_region_set_dirty(&s->cmdmem, 0, CRB_CTRL_CMD_SIZE);
+
+    memory_region_set_dirty(&s->mmio, 0, TPM_CRB_ADDR_SIZE);
  }
 
  enum TPMVersion tpm_crb_get_version(TPMCRBState *s)
@@ -160,45 +160,50 @@ int tpm_crb_pre_save(TPMCRBState *s)
 
  void tpm_crb_reset(TPMCRBState *s, uint64_t baseaddr)
  {
+    uint32_t *regs = memory_region_get_ram_ptr(&s->mmio);
+
+    assert(regs);
      if (s->ppi_enabled) {
          tpm_ppi_reset(&s->ppi);
      }
      tpm_backend_reset(s->tpmbe);
 
-    memset(s->regs, 0, sizeof(s->regs));
+    memset(regs, 0, TPM_CRB_ADDR_SIZE);
 
-    ARRAY_FIELD_DP32(s->regs, CRB_LOC_STATE,
+    ARRAY_FIELD_DP32(regs, CRB_LOC_STATE,
                       tpmRegValidSts, 1);
-    ARRAY_FIELD_DP32(s->regs, CRB_CTRL_STS,
+    ARRAY_FIELD_DP32(regs, CRB_LOC_STATE,
+                     tpmEstablished, 1);
+    ARRAY_FIELD_DP32(regs, CRB_CTRL_STS,
                       tpmIdle, 1);
-    ARRAY_FIELD_DP32(s->regs, CRB_INTF_ID,
+    ARRAY_FIELD_DP32(regs, CRB_INTF_ID,
                       InterfaceType, CRB_INTF_TYPE_CRB_ACTIVE);
-    ARRAY_FIELD_DP32(s->regs, CRB_INTF_ID,
+    ARRAY_FIELD_DP32(regs, CRB_INTF_ID,
                       InterfaceVersion, CRB_INTF_VERSION_CRB);
-    ARRAY_FIELD_DP32(s->regs, CRB_INTF_ID,
+    ARRAY_FIELD_DP32(regs, CRB_INTF_ID,
                       CapLocality, CRB_INTF_CAP_LOCALITY_0_ONLY);
-    ARRAY_FIELD_DP32(s->regs, CRB_INTF_ID,
+    ARRAY_FIELD_DP32(regs, CRB_INTF_ID,
                       CapCRBIdleBypass, CRB_INTF_CAP_IDLE_FAST);
-    ARRAY_FIELD_DP32(s->regs, CRB_INTF_ID,
+    ARRAY_FIELD_DP32(regs, CRB_INTF_ID,
                       CapDataXferSizeSupport, CRB_INTF_CAP_XFER_SIZE_64);
-    ARRAY_FIELD_DP32(s->regs, CRB_INTF_ID,
+    ARRAY_FIELD_DP32(regs, CRB_INTF_ID,
                       CapFIFO, CRB_INTF_CAP_FIFO_NOT_SUPPORTED);
-    ARRAY_FIELD_DP32(s->regs, CRB_INTF_ID,
+    ARRAY_FIELD_DP32(regs, CRB_INTF_ID,
                       CapCRB, CRB_INTF_CAP_CRB_SUPPORTED);
-    ARRAY_FIELD_DP32(s->regs, CRB_INTF_ID,
+    ARRAY_FIELD_DP32(regs, CRB_INTF_ID,
                       InterfaceSelector, CRB_INTF_IF_SELECTOR_CRB);
-    ARRAY_FIELD_DP32(s->regs, CRB_INTF_ID,
+    ARRAY_FIELD_DP32(regs, CRB_INTF_ID,
                       RID, 0b0000);
-    ARRAY_FIELD_DP32(s->regs, CRB_INTF_ID2,
+    ARRAY_FIELD_DP32(regs, CRB_INTF_ID2,
                       VID, PCI_VENDOR_ID_IBM);
 
      baseaddr += A_CRB_DATA_BUFFER;
-    s->regs[R_CRB_CTRL_CMD_SIZE] = CRB_CTRL_CMD_SIZE;
-    s->regs[R_CRB_CTRL_CMD_LADDR] = (uint32_t)baseaddr;
-    s->regs[R_CRB_CTRL_CMD_HADDR] = (uint32_t)(baseaddr >> 32);
-    s->regs[R_CRB_CTRL_RSP_SIZE] = CRB_CTRL_CMD_SIZE;
-    s->regs[R_CRB_CTRL_RSP_LADDR] = (uint32_t)baseaddr;
-    s->regs[R_CRB_CTRL_RSP_HADDR] = (uint32_t)(baseaddr >> 32);
+    regs[R_CRB_CTRL_CMD_SIZE] = CRB_CTRL_CMD_SIZE;
+    regs[R_CRB_CTRL_CMD_LADDR] = (uint32_t)baseaddr;
+    regs[R_CRB_CTRL_CMD_HADDR] = (uint32_t)(baseaddr >> 32);
+    regs[R_CRB_CTRL_RSP_SIZE] = CRB_CTRL_CMD_SIZE;
+    regs[R_CRB_CTRL_RSP_LADDR] = (uint32_t)baseaddr;
+    regs[R_CRB_CTRL_RSP_HADDR] = (uint32_t)(baseaddr >> 32);
 
      s->be_buffer_size = MIN(tpm_backend_get_buffer_size(s->tpmbe),
                              CRB_CTRL_CMD_SIZE);
@@ -206,15 +211,33 @@ void tpm_crb_reset(TPMCRBState *s, uint64_t baseaddr)
      if (tpm_backend_startup_tpm(s->tpmbe, s->be_buffer_size) < 0) {
          exit(1);
      }
+
+    memory_region_rom_device_set_romd(&s->mmio, true);
+    memory_region_set_dirty(&s->mmio, 0, TPM_CRB_ADDR_SIZE);
  }
 
  void tpm_crb_init_memory(Object *obj, TPMCRBState *s, Error **errp)
  {
-    memory_region_init_io(&s->mmio, obj, &tpm_crb_memory_ops, s,
-        "tpm-crb-mmio", sizeof(s->regs));
-    memory_region_init_ram(&s->cmdmem, obj,
-        "tpm-crb-cmd", CRB_CTRL_CMD_SIZE, errp);
+    memory_region_init_rom_device_nomigrate(&s->mmio, obj, &tpm_crb_memory_ops,
+        s, "tpm-crb-mem", TPM_CRB_ADDR_SIZE, errp);
      if (s->ppi_enabled) {
          tpm_ppi_init_memory(&s->ppi, obj);
      }
  }
+
+void tpm_crb_mem_save(TPMCRBState *s, uint32_t *saved_regs, void *saved_cmdmem)
+{
+    uint32_t *regs = memory_region_get_ram_ptr(&s->mmio);
+
+    memcpy(saved_regs, regs, A_CRB_DATA_BUFFER);
+    memcpy(saved_cmdmem, &regs[R_CRB_DATA_BUFFER], CRB_CTRL_CMD_SIZE);
+}
+
+void tpm_crb_mem_load(TPMCRBState *s, const uint32_t *saved_regs,
+                      const void *saved_cmdmem)
+{
+    uint32_t *regs = memory_region_get_ram_ptr(&s->mmio);
+
+    memcpy(regs, saved_regs, A_CRB_DATA_BUFFER);
+    memcpy(&regs[R_CRB_DATA_BUFFER], saved_cmdmem, CRB_CTRL_CMD_SIZE);
+}
#6 lore raw

PATCH v5 5/14 tpm_crb: move ACPI table building to device interface

In reply to Joelle van Dyne

This logic is similar to TPM TIS ISA device. Since TPM CRB can only support TPM 2.0 backends, we check for this in realize.

---
 hw/tpm/tpm_crb.h        |  2 ++
 hw/i386/acpi-build.c    | 16 +---------------
 hw/tpm/tpm_crb.c        | 16 ++++++++++++++++
 hw/tpm/tpm_crb_common.c | 19 +++++++++++++++++++
 4 files changed, 38 insertions(+), 15 deletions(-)
hw/tpm/tpm_crb.h +2 -0
index 36863e1664..e6a86e3fd1 100644
@@ -73,5 +73,7 @@ void tpm_crb_init_memory(Object *obj, TPMCRBState *s, Error **errp);
 void tpm_crb_mem_save(TPMCRBState *s, uint32_t *saved_regs, void *saved_cmdmem);
 void tpm_crb_mem_load(TPMCRBState *s, const uint32_t *saved_regs,
                       const void *saved_cmdmem);
+void tpm_crb_build_aml(TPMIf *ti, Aml *scope, uint32_t baseaddr, uint32_t size,
+                       bool build_ppi);
 
 #endif /* TPM_TPM_CRB_H */
hw/i386/acpi-build.c +1 -15
index 80db183b78..7491cee2af 100644
@@ -1792,21 +1792,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
 
 #ifdef CONFIG_TPM
     if (TPM_IS_CRB(tpm)) {
-        dev = aml_device("TPM");
-        aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
-        aml_append(dev, aml_name_decl("_STR",
-                                      aml_string("TPM 2.0 Device")));
-        crs = aml_resource_template();
-        aml_append(crs, aml_memory32_fixed(TPM_CRB_ADDR_BASE,
-                                           TPM_CRB_ADDR_SIZE, AML_READ_WRITE));
-        aml_append(dev, aml_name_decl("_CRS", crs));
-
-        aml_append(dev, aml_name_decl("_STA", aml_int(0xf)));
-        aml_append(dev, aml_name_decl("_UID", aml_int(1)));
-
-        tpm_build_ppi_acpi(tpm, dev);
-
-        aml_append(sb_scope, dev);
+        call_dev_aml_func(DEVICE(tpm), scope);
     }
 #endif
 
hw/tpm/tpm_crb.c +16 -0
index 99c64dd72a..8d57295b15 100644
@@ -19,6 +19,8 @@
 #include "qemu/module.h"
 #include "qapi/error.h"
 #include "exec/address-spaces.h"
+#include "hw/acpi/acpi_aml_interface.h"
+#include "hw/acpi/tpm.h"
 #include "hw/qdev-properties.h"
 #include "hw/pci/pci_ids.h"
 #include "hw/acpi/tpm.h"
@@ -121,6 +123,11 @@ static void tpm_crb_none_realize(DeviceState *dev, Error **errp)
         return;
     }
 
+    if (tpm_crb_none_get_version(TPM_IF(s)) != TPM_VERSION_2_0) {
+        error_setg(errp, "TPM CRB only supports TPM 2.0 backends");
+        return;
+    }
+
     tpm_crb_init_memory(OBJECT(s), &s->state, errp);
 
     /* only used for migration */
@@ -142,10 +149,17 @@ static void tpm_crb_none_realize(DeviceState *dev, Error **errp)
     }
 }
 
+static void build_tpm_crb_none_aml(AcpiDevAmlIf *adev, Aml *scope)
+{
+    tpm_crb_build_aml(TPM_IF(adev), scope, TPM_CRB_ADDR_BASE, TPM_CRB_ADDR_SIZE,
+                      true);
+}
+
 static void tpm_crb_none_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
     TPMIfClass *tc = TPM_IF_CLASS(klass);
+    AcpiDevAmlIfClass *adevc = ACPI_DEV_AML_IF_CLASS(klass);
 
     dc->realize = tpm_crb_none_realize;
     device_class_set_props(dc, tpm_crb_none_properties);
@@ -154,6 +168,7 @@ static void tpm_crb_none_class_init(ObjectClass *klass, void *data)
     tc->model = TPM_MODEL_TPM_CRB;
     tc->get_version = tpm_crb_none_get_version;
     tc->request_completed = tpm_crb_none_request_completed;
+    adevc->build_dev_aml = build_tpm_crb_none_aml;
 
     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
 }
@@ -166,6 +181,7 @@ static const TypeInfo tpm_crb_none_info = {
     .class_init  = tpm_crb_none_class_init,
     .interfaces = (InterfaceInfo[]) {
         { TYPE_TPM_IF },
+        { TYPE_ACPI_DEV_AML_IF },
         { }
     }
 };
hw/tpm/tpm_crb_common.c +19 -0
index f96a8cf299..09ca55eece 100644
@@ -241,3 +241,22 @@ void tpm_crb_mem_load(TPMCRBState *s, const uint32_t *saved_regs,
     memcpy(regs, saved_regs, A_CRB_DATA_BUFFER);
     memcpy(&regs[R_CRB_DATA_BUFFER], saved_cmdmem, CRB_CTRL_CMD_SIZE);
 }
+
+void tpm_crb_build_aml(TPMIf *ti, Aml *scope, uint32_t baseaddr, uint32_t size,
+                       bool build_ppi)
+{
+    Aml *dev, *crs;
+
+    dev = aml_device("TPM");
+    aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
+    aml_append(dev, aml_name_decl("_STR", aml_string("TPM 2.0 Device")));
+    aml_append(dev, aml_name_decl("_UID", aml_int(1)));
+    aml_append(dev, aml_name_decl("_STA", aml_int(0xF)));
+    crs = aml_resource_template();
+    aml_append(crs, aml_memory32_fixed(baseaddr, size, AML_READ_WRITE));
+    aml_append(dev, aml_name_decl("_CRS", crs));
+    if (build_ppi) {
+        tpm_build_ppi_acpi(ti, dev);
+    }
+    aml_append(scope, dev);
+}
Signature · 1 line
2.41.0
#7 lore raw

Re: [PATCH v5 05/14] tpm_crb: move ACPI table building to device interface

Quoted 37 lines · Joelle van Dyne

This logic is similar to TPM TIS ISA device. Since TPM CRB can only support TPM 2.0 backends, we check for this in realize.

---
  hw/tpm/tpm_crb.h        |  2 ++
  hw/i386/acpi-build.c    | 16 +---------------
  hw/tpm/tpm_crb.c        | 16 ++++++++++++++++
  hw/tpm/tpm_crb_common.c | 19 +++++++++++++++++++
  4 files changed, 38 insertions(+), 15 deletions(-)
hw/tpm/tpm_crb.h +2 -0
index 36863e1664..e6a86e3fd1 100644
@@ -73,5 +73,7 @@ void tpm_crb_init_memory(Object *obj, TPMCRBState *s, Error **errp);
  void tpm_crb_mem_save(TPMCRBState *s, uint32_t *saved_regs, void *saved_cmdmem);
  void tpm_crb_mem_load(TPMCRBState *s, const uint32_t *saved_regs,
                        const void *saved_cmdmem);
+void tpm_crb_build_aml(TPMIf *ti, Aml *scope, uint32_t baseaddr, uint32_t size,
+                       bool build_ppi);
 
  #endif /* TPM_TPM_CRB_H */
hw/i386/acpi-build.c +0 -6
index 80db183b78..7491cee2af 100644
@@ -1792,21 +1792,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
 
  #ifdef CONFIG_TPM
      if (TPM_IS_CRB(tpm)) {
-        dev = aml_device("TPM");
-        aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
-        aml_append(dev, aml_name_decl("_STR",
-                                      aml_string("TPM 2.0 Device")));
-        crs = aml_resource_template();
-        aml_append(crs, aml_memory32_fixed(TPM_CRB_ADDR_BASE,

hw/i386/acpi-build.c build_dsdt(GArray *table_data, BIOSLinker *linker, · line 1795

Quoted 10 lines
hw/i386/acpi-build.c +1 -9
-                                           TPM_CRB_ADDR_SIZE, AML_READ_WRITE));
-        aml_append(dev, aml_name_decl("_CRS", crs));
-
-        aml_append(dev, aml_name_decl("_STA", aml_int(0xf)));
-        aml_append(dev, aml_name_decl("_UID", aml_int(1)));
-
-        tpm_build_ppi_acpi(tpm, dev);
-
-        aml_append(sb_scope, dev);
+        call_dev_aml_func(DEVICE(tpm), scope);

For an x86_64 VM we have to call it directly otherwise the ACPI table won't be there.

tpm_crb_build_aml(tpm, sb_scope, TPM_CRB_ADDR_BASE,
TPM_CRB_ADDR_SIZE, true);

#8 lore raw

Re: [PATCH v5 05/14] tpm_crb: move ACPI table building to device interface

Quoted 56 lines · Stefan Berger
Quoted 40 lines · Joelle van Dyne

This logic is similar to TPM TIS ISA device. Since TPM CRB can only support TPM 2.0 backends, we check for this in realize.

---
  hw/tpm/tpm_crb.h        |  2 ++ hw/i386/acpi-build.c    | 16 +--------------- hw/tpm/tpm_crb.c        | 16 ++++++++++++++++ hw/tpm/tpm_crb_common.c | 19 +++++++++++++++++++

  4 files changed, 38 insertions(+), 15 deletions(-)
hw/tpm/tpm_crb.h +2 -0
index 36863e1664..e6a86e3fd1 100644
@@ -73,5 +73,7 @@ void tpm_crb_init_memory(Object *obj, TPMCRBState 
 *s, Error **errp);
   void tpm_crb_mem_save(TPMCRBState *s, uint32_t *saved_regs, void *saved_cmdmem);
   void tpm_crb_mem_load(TPMCRBState *s, const uint32_t *saved_regs,
                         const void *saved_cmdmem);
+void tpm_crb_build_aml(TPMIf *ti, Aml *scope, uint32_t baseaddr, 
 uint32_t size,
+                       bool build_ppi);

  #endif /* TPM_TPM_CRB_H */

hw/i386/acpi-build.c +0 -7
index 80db183b78..7491cee2af 100644
@@ -1792,21 +1792,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
 
   #ifdef CONFIG_TPM
       if (TPM_IS_CRB(tpm)) {
-        dev = aml_device("TPM");
-        aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
-        aml_append(dev, aml_name_decl("_STR",
-                                      aml_string("TPM 2.0 Device")));
-        crs = aml_resource_template();
-        aml_append(crs, aml_memory32_fixed(TPM_CRB_ADDR_BASE,
-                                           TPM_CRB_ADDR_SIZE, 

hw/i386/acpi-build.c build_dsdt(GArray *table_data, BIOSLinker *linker, · line 1796

Quoted 10 lines
hw/i386/acpi-build.c +1 -8
 AML_READ_WRITE));
-        aml_append(dev, aml_name_decl("_CRS", crs));
-
-        aml_append(dev, aml_name_decl("_STA", aml_int(0xf)));
-        aml_append(dev, aml_name_decl("_UID", aml_int(1)));
-
-        tpm_build_ppi_acpi(tpm, dev);
-
-        aml_append(sb_scope, dev);
+        call_dev_aml_func(DEVICE(tpm), scope);

For an x86_64 VM we have to call it directly otherwise the ACPI table won't be there.

Quoted 4 lines

        tpm_crb_build_aml(tpm, sb_scope, TPM_CRB_ADDR_BASE,
TPM_CRB_ADDR_SIZE, true);

I looks like a good place for the moved code would be in hw/acpi/tpm.c

#9 lore raw

Re: [PATCH v5 05/14] tpm_crb: move ACPI table building to device interface

Quoted 62 lines · Stefan Berger
Quoted 58 lines · Stefan Berger
Quoted 41 lines · Joelle van Dyne

This logic is similar to TPM TIS ISA device. Since TPM CRB can only support TPM 2.0 backends, we check for this in realize.

---
  hw/tpm/tpm_crb.h        |  2 ++
  hw/i386/acpi-build.c    | 16 +---------------
  hw/tpm/tpm_crb.c        | 16 ++++++++++++++++
  hw/tpm/tpm_crb_common.c | 19 +++++++++++++++++++
  4 files changed, 38 insertions(+), 15 deletions(-)
hw/tpm/tpm_crb.h +1 -0
index 36863e1664..e6a86e3fd1 100644
@@ -73,5 +73,7 @@ void tpm_crb_init_memory(Object *obj, TPMCRBState
 *s, Error **errp);
  void tpm_crb_mem_save(TPMCRBState *s, uint32_t *saved_regs, void
 *saved_cmdmem);
  void tpm_crb_mem_load(TPMCRBState *s, const uint32_t *saved_regs,
                        const void *saved_cmdmem);
+void tpm_crb_build_aml(TPMIf *ti, Aml *scope, uint32_t baseaddr,

uint32_t size,

hw/tpm/tpm_crb.h +1 -0
+                       bool build_ppi);
 
  #endif /* TPM_TPM_CRB_H */
hw/i386/acpi-build.c +0 -7
index 80db183b78..7491cee2af 100644
@@ -1792,21 +1792,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
 
  #ifdef CONFIG_TPM
      if (TPM_IS_CRB(tpm)) {
-        dev = aml_device("TPM");
-        aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
-        aml_append(dev, aml_name_decl("_STR",
-                                      aml_string("TPM 2.0 Device")));
-        crs = aml_resource_template();
-        aml_append(crs, aml_memory32_fixed(TPM_CRB_ADDR_BASE,
-                                           TPM_CRB_ADDR_SIZE,

hw/i386/acpi-build.c build_dsdt(GArray *table_data, BIOSLinker *linker, · line 1796

Quoted 10 lines
hw/i386/acpi-build.c +1 -8
 AML_READ_WRITE));
-        aml_append(dev, aml_name_decl("_CRS", crs));
-
-        aml_append(dev, aml_name_decl("_STA", aml_int(0xf)));
-        aml_append(dev, aml_name_decl("_UID", aml_int(1)));
-
-        tpm_build_ppi_acpi(tpm, dev);
-
-        aml_append(sb_scope, dev);
+        call_dev_aml_func(DEVICE(tpm), scope);

For an x86_64 VM we have to call it directly otherwise the ACPI table won't be there.

Quoted 5 lines
Quoted 4 lines

tpm_crb_build_aml(tpm, sb_scope, TPM_CRB_ADDR_BASE,
TPM_CRB_ADDR_SIZE, true);

I looks like a good place for the moved code would be in hw/acpi/tpm.c

The change in v5 is that we call "call_dev_aml_func" which calls "tpm_crb_build_aml" from tpm_crb.c (later moved to tpm_crb_common.c). I think it's a better place for it because TPM TIS has a different ACPI table and both can be handled correctly by call_dev_aml_func. In the previous versions when we moved TPM CRB to ISA there was no need to manually call "call_dev_aml_func" because the function for building the ISA bus called it. However, I think this is still better in case it is decided in the future to move the CRB device on ISA or another bus and it makes the code more manageable with more TPM types.

#10 lore raw

PATCH v5 6/14 tpm-sysbus: add plug handler for TPM on SysBus

In reply to Joelle van Dyne

TPM needs to know its own base address in order to generate its DSDT device entry.

---
 include/sysemu/tpm.h |  4 ++++
 hw/tpm/tpm-sysbus.c  | 47 ++++++++++++++++++++++++++++++++++++++++++++
 hw/tpm/meson.build   |  1 +
 3 files changed, 52 insertions(+)
 create mode 100644 hw/tpm/tpm-sysbus.c
include/sysemu/tpm.h +4 -0
index 1ee568b3b6..ffd300e607 100644
@@ -12,6 +12,8 @@
 #ifndef QEMU_TPM_H
 #define QEMU_TPM_H
 
+#include "qemu/osdep.h"
+#include "exec/hwaddr.h"
 #include "qapi/qapi-types-tpm.h"
 #include "qom/object.h"
 
@@ -78,6 +80,8 @@ static inline TPMVersion tpm_get_version(TPMIf *ti)
     return TPM_IF_GET_CLASS(ti)->get_version(ti);
 }
 
+void tpm_sysbus_plug(TPMIf *tpmif, Object *pbus, hwaddr pbus_base);
+
 #else /* CONFIG_TPM */
 
 #define tpm_init()  (0)
hw/tpm/tpm-sysbus.c new file +47 -0

47 lines not on this page · raw message

hw/tpm/meson.build +1 -0
index cb8204d5bc..3060ac05e8 100644
@@ -1,6 +1,7 @@
 system_ss.add(when: 'CONFIG_TPM_TIS', if_true: files('tpm_tis_common.c'))
 system_ss.add(when: 'CONFIG_TPM_TIS_ISA', if_true: files('tpm_tis_isa.c'))
 system_ss.add(when: 'CONFIG_TPM_TIS_SYSBUS', if_true: files('tpm_tis_sysbus.c'))
+system_ss.add(when: 'CONFIG_TPM_TIS_SYSBUS', if_true: files('tpm-sysbus.c'))
 system_ss.add(when: 'CONFIG_TPM_TIS_I2C', if_true: files('tpm_tis_i2c.c'))
 system_ss.add(when: 'CONFIG_TPM_CRB', if_true: files('tpm_crb.c'))
 system_ss.add(when: 'CONFIG_TPM_CRB', if_true: files('tpm_crb_common.c'))
Signature · 1 line
2.41.0
#11 lore raw

PATCH v5 7/14 hw/arm/virt: connect TPM to platform bus

In reply to Joelle van Dyne
---
 hw/arm/virt.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/arm/virt.c +7 -0
index 85e3c5ba9d..36e2506420 100644
@@ -2811,6 +2811,13 @@ static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev,
         vms->virtio_iommu_bdf = pci_get_bdf(pdev);
         create_virtio_iommu_dt_bindings(vms);
     }
+
+#ifdef CONFIG_TPM
+    if (object_dynamic_cast(OBJECT(dev), TYPE_TPM_IF)) {
+        tpm_sysbus_plug(TPM_IF(dev), OBJECT(vms->platform_bus_dev),
+                        vms->memmap[VIRT_PLATFORM_BUS].base);
+    }
+#endif
 }
 
 static void virt_dimm_unplug_request(HotplugHandler *hotplug_dev,
Signature · 1 line
2.41.0
#12 lore raw

PATCH v5 8/14 hw/loongarch/virt: connect TPM to platform bus

In reply to Joelle van Dyne
---
 hw/loongarch/virt.c  | 7 +++++++
 hw/loongarch/Kconfig | 1 +
 2 files changed, 8 insertions(+)
hw/loongarch/virt.c +7 -0
index 4b7dc67a2d..feed0f8bbf 100644
@@ -1004,6 +1004,13 @@ static void loongarch_machine_device_plug_cb(HotplugHandler *hotplug_dev,
     } else if (memhp_type_supported(dev)) {
         virt_mem_plug(hotplug_dev, dev, errp);
     }
+
+#ifdef CONFIG_TPM
+    if (object_dynamic_cast(OBJECT(dev), TYPE_TPM_IF)) {
+        tpm_sysbus_plug(TPM_IF(dev), OBJECT(lams->platform_bus_dev),
+                        VIRT_PLATFORM_BUS_BASEADDRESS);
+    }
+#endif
 }
 
 static HotplugHandler *virt_machine_get_hotplug_handler(MachineState *machine,
hw/loongarch/Kconfig +1 -0
index 5727efed6d..25da190ffc 100644
@@ -5,6 +5,7 @@ config LOONGARCH_VIRT
     imply VIRTIO_VGA
     imply PCI_DEVICES
     imply NVDIMM
+    imply TPM_TIS_SYSBUS
     select SERIAL
     select VIRTIO_PCI
     select PLATFORM_BUS
Signature · 1 line
2.41.0
#13 lore raw

PATCH v5 9/14 tpm_tis_sysbus: move DSDT AML generation to device

In reply to Joelle van Dyne

This reduces redundant code in different machine types with ACPI table generation. Additionally, this will allow us to support different TPM interfaces with the same AML logic. Finally, this matches up with the TPM TIS ISA implementation.

Ideally, we would be able to call qbus_build_aml and avoid any TPM specific code in the ACPI table generation. However, currently we still have to call build_tpm2 anyways and it does not look like most other ACPI devices support the ACPI_DEV_AML_IF interface.

---
 hw/arm/virt-acpi-build.c  | 38 ++------------------------------------
 hw/loongarch/acpi-build.c | 38 ++------------------------------------
 hw/tpm/tpm_tis_sysbus.c   | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 41 insertions(+), 72 deletions(-)
hw/arm/virt-acpi-build.c +2 -36
index 8bc35a483c..499d30eb5d 100644
@@ -35,6 +35,7 @@
 #include "target/arm/cpu.h"
 #include "hw/acpi/acpi-defs.h"
 #include "hw/acpi/acpi.h"
+#include "hw/acpi/acpi_aml_interface.h"
 #include "hw/nvram/fw_cfg.h"
 #include "hw/acpi/bios-linker-loader.h"
 #include "hw/acpi/aml-build.h"
@@ -208,41 +209,6 @@ static void acpi_dsdt_add_gpio(Aml *scope, const MemMapEntry *gpio_memmap,
     aml_append(scope, dev);
 }
 
-#ifdef CONFIG_TPM
-static void acpi_dsdt_add_tpm(Aml *scope, VirtMachineState *vms)
-{
-    PlatformBusDevice *pbus = PLATFORM_BUS_DEVICE(vms->platform_bus_dev);
-    hwaddr pbus_base = vms->memmap[VIRT_PLATFORM_BUS].base;
-    SysBusDevice *sbdev = SYS_BUS_DEVICE(tpm_find());
-    MemoryRegion *sbdev_mr;
-    hwaddr tpm_base;
-
-    if (!sbdev) {
-        return;
-    }
-
-    tpm_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
-    assert(tpm_base != -1);
-
-    tpm_base += pbus_base;
-
-    sbdev_mr = sysbus_mmio_get_region(sbdev, 0);
-
-    Aml *dev = aml_device("TPM0");
-    aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
-    aml_append(dev, aml_name_decl("_STR", aml_string("TPM 2.0 Device")));
-    aml_append(dev, aml_name_decl("_UID", aml_int(0)));
-
-    Aml *crs = aml_resource_template();
-    aml_append(crs,
-               aml_memory32_fixed(tpm_base,
-                                  (uint32_t)memory_region_size(sbdev_mr),
-                                  AML_READ_WRITE));
-    aml_append(dev, aml_name_decl("_CRS", crs));
-    aml_append(scope, dev);
-}
-#endif
-
 #define ID_MAPPING_ENTRY_SIZE 20
 #define SMMU_V3_ENTRY_SIZE 68
 #define ROOT_COMPLEX_ENTRY_SIZE 36
@@ -891,7 +857,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms)
 
     acpi_dsdt_add_power_button(scope);
 #ifdef CONFIG_TPM
-    acpi_dsdt_add_tpm(scope, vms);
+    call_dev_aml_func(DEVICE(tpm_find()), scope);
 #endif
 
     aml_append(dsdt, scope);
hw/loongarch/acpi-build.c +2 -36
index ae292fc543..1969bfc8f9 100644
@@ -14,6 +14,7 @@
 #include "target/loongarch/cpu.h"
 #include "hw/acpi/acpi-defs.h"
 #include "hw/acpi/acpi.h"
+#include "hw/acpi/acpi_aml_interface.h"
 #include "hw/nvram/fw_cfg.h"
 #include "hw/acpi/bios-linker-loader.h"
 #include "migration/vmstate.h"
@@ -328,41 +329,6 @@ static void build_flash_aml(Aml *scope, LoongArchMachineState *lams)
     aml_append(scope, dev);
 }
 
-#ifdef CONFIG_TPM
-static void acpi_dsdt_add_tpm(Aml *scope, LoongArchMachineState *vms)
-{
-    PlatformBusDevice *pbus = PLATFORM_BUS_DEVICE(vms->platform_bus_dev);
-    hwaddr pbus_base = VIRT_PLATFORM_BUS_BASEADDRESS;
-    SysBusDevice *sbdev = SYS_BUS_DEVICE(tpm_find());
-    MemoryRegion *sbdev_mr;
-    hwaddr tpm_base;
-
-    if (!sbdev) {
-        return;
-    }
-
-    tpm_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
-    assert(tpm_base != -1);
-
-    tpm_base += pbus_base;
-
-    sbdev_mr = sysbus_mmio_get_region(sbdev, 0);
-
-    Aml *dev = aml_device("TPM0");
-    aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
-    aml_append(dev, aml_name_decl("_STR", aml_string("TPM 2.0 Device")));
-    aml_append(dev, aml_name_decl("_UID", aml_int(0)));
-
-    Aml *crs = aml_resource_template();
-    aml_append(crs,
-               aml_memory32_fixed(tpm_base,
-                                  (uint32_t)memory_region_size(sbdev_mr),
-                                  AML_READ_WRITE));
-    aml_append(dev, aml_name_decl("_CRS", crs));
-    aml_append(scope, dev);
-}
-#endif
-
 /* build DSDT */
 static void
 build_dsdt(GArray *table_data, BIOSLinker *linker, MachineState *machine)
@@ -379,7 +345,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker, MachineState *machine)
     build_la_ged_aml(dsdt, machine);
     build_flash_aml(dsdt, lams);
 #ifdef CONFIG_TPM
-    acpi_dsdt_add_tpm(dsdt, lams);
+    call_dev_aml_func(DEVICE(tpm_find()), dsdt);
 #endif
     /* System State Package */
     scope = aml_scope("\\");
hw/tpm/tpm_tis_sysbus.c +37 -0
index 2fc550f119..462b0e1571 100644
@@ -30,6 +30,7 @@
 #include "hw/sysbus.h"
 #include "tpm_tis.h"
 #include "qom/object.h"
+#include "hw/acpi/acpi_aml_interface.h"
 
 struct TPMStateSysBus {
     /*< private >*/
@@ -37,6 +38,8 @@ struct TPMStateSysBus {
 
     /*< public >*/
     TPMState state; /* not a QOM object */
+    uint64_t baseaddr;
+    uint64_t size;
 };
 
 OBJECT_DECLARE_SIMPLE_TYPE(TPMStateSysBus, TPM_TIS_SYSBUS)
@@ -93,6 +96,10 @@ static void tpm_tis_sysbus_reset(DeviceState *dev)
 static Property tpm_tis_sysbus_properties[] = {
     DEFINE_PROP_UINT32("irq", TPMStateSysBus, state.irq_num, TPM_TIS_IRQ),
     DEFINE_PROP_TPMBE("tpmdev", TPMStateSysBus, state.be_driver),
+    DEFINE_PROP_UINT64("x-baseaddr", TPMStateSysBus, baseaddr,
+                       TPM_TIS_ADDR_BASE),
+    DEFINE_PROP_UINT64("x-size", TPMStateSysBus, size,
+                       TPM_TIS_ADDR_SIZE),
     DEFINE_PROP_END_OF_LIST(),
 };
 
@@ -125,10 +132,38 @@ static void tpm_tis_sysbus_realizefn(DeviceState *dev, Error **errp)
     }
 }
 
+static void build_tpm_tis_sysbus_aml(AcpiDevAmlIf *adev, Aml *scope)
+{
+    Aml *dev, *crs;
+    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(adev);
+    TPMIf *ti = TPM_IF(sbdev);
+
+    dev = aml_device("TPM");
+    if (tpm_tis_sysbus_get_tpm_version(ti) == TPM_VERSION_2_0) {
+        aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
+        aml_append(dev, aml_name_decl("_STR", aml_string("TPM 2.0 Device")));
+    } else {
+        aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C31")));
+    }
+    aml_append(dev, aml_name_decl("_UID", aml_int(0)));
+    aml_append(dev, aml_name_decl("_STA", aml_int(0xF)));
+    crs = aml_resource_template();
+    aml_append(crs, aml_memory32_fixed(sbdev->baseaddr, sbdev->size,
+                                      AML_READ_WRITE));
+    /*
+     * FIXME: TPM_TIS_IRQ=5 conflicts with PNP0C0F irqs,
+     * fix default TPM_TIS_IRQ value there to use some unused IRQ
+     */
+    /* aml_append(crs, aml_irq_no_flags(sbdev->state.irq_num)); */
+    aml_append(dev, aml_name_decl("_CRS", crs));
+    aml_append(scope, dev);
+}
+
 static void tpm_tis_sysbus_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
     TPMIfClass *tc = TPM_IF_CLASS(klass);
+    AcpiDevAmlIfClass *adevc = ACPI_DEV_AML_IF_CLASS(klass);
 
     device_class_set_props(dc, tpm_tis_sysbus_properties);
     dc->vmsd  = &vmstate_tpm_tis_sysbus;
@@ -139,6 +174,7 @@ static void tpm_tis_sysbus_class_init(ObjectClass *klass, void *data)
     tc->request_completed = tpm_tis_sysbus_request_completed;
     tc->get_version = tpm_tis_sysbus_get_tpm_version;
     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
+    adevc->build_dev_aml = build_tpm_tis_sysbus_aml;
 }
 
 static const TypeInfo tpm_tis_sysbus_info = {
@@ -149,6 +185,7 @@ static const TypeInfo tpm_tis_sysbus_info = {
     .class_init  = tpm_tis_sysbus_class_init,
     .interfaces = (InterfaceInfo[]) {
         { TYPE_TPM_IF },
+        { TYPE_ACPI_DEV_AML_IF },
         { }
     }
 };
Signature · 1 line
2.41.0
#14 lore raw

PATCH v5 10/14 tests: acpi: prepare for TPM CRB tests

In reply to Joelle van Dyne
---
 tests/qtest/bios-tables-test-allowed-diff.h | 4 ++++
 tests/data/acpi/q35/DSDT.crb.tpm2           | 0
 tests/data/acpi/q35/TPM2.crb.tpm2           | 0
 tests/data/acpi/virt/DSDT.crb-device.tpm2   | 0
 tests/data/acpi/virt/TPM2.crb-device.tpm2   | 0
 5 files changed, 4 insertions(+)
 create mode 100644 tests/data/acpi/q35/DSDT.crb.tpm2
 create mode 100644 tests/data/acpi/q35/TPM2.crb.tpm2
 create mode 100644 tests/data/acpi/virt/DSDT.crb-device.tpm2
 create mode 100644 tests/data/acpi/virt/TPM2.crb-device.tpm2
tests/qtest/bios-tables-test-allowed-diff.h +4 -0
index dfb8523c8b..c2d1924c2f 100644
@@ -1 +1,5 @@
 /* List of comma-separated changed AML files to ignore */
+"tests/data/acpi/q35/DSDT.crb.tpm2",
+"tests/data/acpi/q35/TPM2.crb.tpm2",
+"tests/data/acpi/virt/DSDT.crb.tpm2",
+"tests/data/acpi/virt/TPM2.crb.tpm2",
tests/data/acpi/q35/DSDT.crb.tpm2 new file +0 -0
new file mode 100644
index 0000000000..e69de29bb2
tests/data/acpi/q35/TPM2.crb.tpm2 new file +0 -0
new file mode 100644
index 0000000000..e69de29bb2
tests/data/acpi/virt/DSDT.crb-device.tpm2 new file +0 -0
new file mode 100644
index 0000000000..e69de29bb2
tests/data/acpi/virt/TPM2.crb-device.tpm2 new file +0 -0
new file mode 100644
index 0000000000..e69de29bb2
Signature · 1 line
2.41.0
#15 lore raw

PATCH v5 11/14 tpm_crb_sysbus: introduce TPM CRB SysBus device

In reply to Joelle van Dyne

This SysBus variant of the CRB interface supports dynamically locating the MMIO interface so that Virt machines can use it. This interface is currently the only one supported by QEMU that works on Windows 11 ARM64 as 'tpm-tis-device' does not work with current Windows drivers. We largely follow that device as a template.

To try out this device with Windows 11 before OVMF is updated, you will need to modify sysbus-fdt.c and change the added line from:

```c
    TYPE_BINDING(TYPE_TPM_CRB_SYSBUS, no_fdt_node),
```

to

```c
    TYPE_BINDING(TYPE_TPM_CRB_SYSBUS, add_tpm_tis_fdt_node),
```

This change was not included because it can confuse Linux (although from testing, it seems like Linux is able to properly ignore the device from the TPM TIS driver and recognize it from the ACPI device in the TPM CRB driver). A proper fix would require OVMF to recognize the ACPI device and not depend on the FDT node for recognizing TPM.

The command line to try out this device with SWTPM is:

```
$ qemu-system-aarch64 \
    -chardev socket,id=chrtpm0,path=tpm.sock \
    -tpmdev emulator,id=tpm0,chardev=chrtpm0 \
    -device tpm-crb-device,tpmdev=tpm0
```

along with SWTPM:

```
$ swtpm \
    --ctrl type=unixio,path=tpm.sock,terminate \
    --tpmstate backend-uri=file://tpm.data \
    --tpm2
```
---
 docs/specs/tpm.rst      |   1 +
 include/sysemu/tpm.h    |   3 +
 hw/acpi/aml-build.c     |   7 +-
 hw/arm/virt.c           |   1 +
 hw/core/sysbus-fdt.c    |   1 +
 hw/loongarch/virt.c     |   1 +
 hw/riscv/virt.c         |   1 +
 hw/tpm/tpm_crb_sysbus.c | 162 ++++++++++++++++++++++++++++++++++++++++
 hw/arm/Kconfig          |   1 +
 hw/loongarch/Kconfig    |   1 +
 hw/riscv/Kconfig        |   1 +
 hw/tpm/Kconfig          |   5 ++
 hw/tpm/meson.build      |   3 +
 13 files changed, 187 insertions(+), 1 deletion(-)
 create mode 100644 hw/tpm/tpm_crb_sysbus.c
docs/specs/tpm.rst +1 -0
index 2bc29c9804..95aeb49220 100644
@@ -46,6 +46,7 @@ operating system.
 QEMU files related to TPM CRB interface:
  - ``hw/tpm/tpm_crb.c``
  - ``hw/tpm/tpm_crb_common.c``
+ - ``hw/tpm/tpm_crb_sysbus.c``
 
 SPAPR interface
 ---------------
include/sysemu/tpm.h +3 -0
index ffd300e607..bab30fa546 100644
@@ -49,6 +49,7 @@ struct TPMIfClass {
 #define TYPE_TPM_TIS_ISA            "tpm-tis"
 #define TYPE_TPM_TIS_SYSBUS         "tpm-tis-device"
 #define TYPE_TPM_CRB                "tpm-crb"
+#define TYPE_TPM_CRB_SYSBUS         "tpm-crb-device"
 #define TYPE_TPM_SPAPR              "tpm-spapr"
 #define TYPE_TPM_TIS_I2C            "tpm-tis-i2c"
 
@@ -58,6 +59,8 @@ struct TPMIfClass {
     object_dynamic_cast(OBJECT(chr), TYPE_TPM_TIS_SYSBUS)
 #define TPM_IS_CRB(chr)                             \
     object_dynamic_cast(OBJECT(chr), TYPE_TPM_CRB)
+#define TPM_IS_CRB_SYSBUS(chr)                      \
+    object_dynamic_cast(OBJECT(chr), TYPE_TPM_CRB_SYSBUS)
 #define TPM_IS_SPAPR(chr)                           \
     object_dynamic_cast(OBJECT(chr), TYPE_TPM_SPAPR)
 #define TPM_IS_TIS_I2C(chr)                      \
hw/acpi/aml-build.c +6 -1
index af66bde0f5..acc654382e 100644
@@ -31,6 +31,7 @@
 #include "hw/pci/pci_bus.h"
 #include "hw/pci/pci_bridge.h"
 #include "qemu/cutils.h"
+#include "qom/object.h"
 
 static GArray *build_alloc_array(void)
 {
@@ -2218,7 +2219,7 @@ void build_tpm2(GArray *table_data, BIOSLinker *linker, GArray *tcpalog,
 {
     uint8_t start_method_params[12] = {};
     unsigned log_addr_offset;
-    uint64_t control_area_start_address;
+    uint64_t baseaddr, control_area_start_address;
     TPMIf *tpmif = tpm_find();
     uint32_t start_method;
     AcpiTable table = { .sig = "TPM2", .rev = 4,
@@ -2236,6 +2237,10 @@ void build_tpm2(GArray *table_data, BIOSLinker *linker, GArray *tcpalog,
     } else if (TPM_IS_CRB(tpmif)) {
         control_area_start_address = TPM_CRB_ADDR_CTRL;
         start_method = TPM2_START_METHOD_CRB;
+    } else if (TPM_IS_CRB_SYSBUS(tpmif)) {
+        baseaddr = object_property_get_uint(OBJECT(tpmif), "x-baseaddr", NULL);
+        control_area_start_address = baseaddr + A_CRB_CTRL_REQ;
+        start_method = TPM2_START_METHOD_CRB;
     } else {
         g_assert_not_reached();
     }
hw/arm/virt.c +1 -0
index 36e2506420..e6152a2f51 100644
@@ -2955,6 +2955,7 @@ static void virt_machine_class_init(ObjectClass *oc, void *data)
     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_PLATFORM);
 #ifdef CONFIG_TPM
     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
+    machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_CRB_SYSBUS);
 #endif
     mc->block_default_type = IF_VIRTIO;
     mc->no_cdrom = 1;
hw/core/sysbus-fdt.c +1 -0
index eebcd28f9a..9c783f88eb 100644
@@ -493,6 +493,7 @@ static const BindingEntry bindings[] = {
 #endif
 #ifdef CONFIG_TPM
     TYPE_BINDING(TYPE_TPM_TIS_SYSBUS, add_tpm_tis_fdt_node),
+    TYPE_BINDING(TYPE_TPM_CRB_SYSBUS, no_fdt_node),
 #endif
     TYPE_BINDING(TYPE_RAMFB_DEVICE, no_fdt_node),
     TYPE_BINDING("", NULL), /* last element */
hw/loongarch/virt.c +1 -0
index feed0f8bbf..9d5ad2bc8e 100644
@@ -1114,6 +1114,7 @@ static void loongarch_class_init(ObjectClass *oc, void *data)
     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
 #ifdef CONFIG_TPM
     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
+    machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_CRB_SYSBUS);
 #endif
 }
 
hw/riscv/virt.c +1 -0
index c7fc97e273..4cce0b14ab 100644
@@ -1702,6 +1702,7 @@ static void virt_machine_class_init(ObjectClass *oc, void *data)
     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
 #ifdef CONFIG_TPM
     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
+    machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_CRB_SYSBUS);
 #endif
 
 
hw/tpm/tpm_crb_sysbus.c new file +162 -0

162 lines not on this page · raw message

hw/arm/Kconfig +1 -0
index 3ada335a24..f524994fd3 100644
@@ -5,6 +5,7 @@ config ARM_VIRT
     imply VFIO_AMD_XGBE
     imply VFIO_PLATFORM
     imply VFIO_XGMAC
+    imply TPM_CRB_SYSBUS
     imply TPM_TIS_SYSBUS
     imply TPM_TIS_I2C
     imply NVDIMM
hw/loongarch/Kconfig +1 -0
index 25da190ffc..70544f3ce5 100644
@@ -6,6 +6,7 @@ config LOONGARCH_VIRT
     imply PCI_DEVICES
     imply NVDIMM
     imply TPM_TIS_SYSBUS
+    imply TPM_CRB_SYSBUS
     select SERIAL
     select VIRTIO_PCI
     select PLATFORM_BUS
hw/riscv/Kconfig +1 -0
index b6a5eb4452..d824cb58f9 100644
@@ -29,6 +29,7 @@ config RISCV_VIRT
     imply PCI_DEVICES
     imply VIRTIO_VGA
     imply TEST_DEVICES
+    imply TPM_CRB_SYSBUS
     imply TPM_TIS_SYSBUS
     select RISCV_NUMA
     select GOLDFISH_RTC
hw/tpm/Kconfig +5 -0
index a46663288c..28139c456a 100644
@@ -25,6 +25,11 @@ config TPM_CRB
     depends on TPM && PC
     select TPM_BACKEND
 
+config TPM_CRB_SYSBUS
+    bool
+    depends on TPM
+    select TPM_BACKEND
+
 config TPM_SPAPR
     bool
     default y
hw/tpm/meson.build +3 -0
index 3060ac05e8..f2536c99e3 100644
@@ -5,6 +5,9 @@ system_ss.add(when: 'CONFIG_TPM_TIS_SYSBUS', if_true: files('tpm-sysbus.c'))
 system_ss.add(when: 'CONFIG_TPM_TIS_I2C', if_true: files('tpm_tis_i2c.c'))
 system_ss.add(when: 'CONFIG_TPM_CRB', if_true: files('tpm_crb.c'))
 system_ss.add(when: 'CONFIG_TPM_CRB', if_true: files('tpm_crb_common.c'))
+system_ss.add(when: 'CONFIG_TPM_CRB_SYSBUS', if_true: files('tpm_crb_sysbus.c'))
+system_ss.add(when: 'CONFIG_TPM_CRB_SYSBUS', if_true: files('tpm_crb_common.c'))
+system_ss.add(when: 'CONFIG_TPM_CRB_SYSBUS', if_true: files('tpm-sysbus.c'))
 system_ss.add(when: 'CONFIG_TPM_TIS', if_true: files('tpm_ppi.c'))
 system_ss.add(when: 'CONFIG_TPM_CRB', if_true: files('tpm_ppi.c'))
 
Signature · 1 line
2.41.0
#16 lore raw

PATCH v5 12/14 tests: acpi: implement TPM CRB tests for ARM virt

In reply to Joelle van Dyne
---
 tests/qtest/bios-tables-test.c | 43 ++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)
tests/qtest/bios-tables-test.c +41 -2
index 71af5cf69f..bb4ebf00c1 100644
@@ -1447,6 +1447,28 @@ static void test_acpi_piix4_tcg_numamem(void)
 
 uint64_t tpm_tis_base_addr;
 
+static test_data tcg_tpm_test_data(const char *machine)
+{
+    if (g_strcmp0(machine, "virt") == 0) {
+        test_data data = {
+            .machine = "virt",
+            .tcg_only = true,
+            .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
+            .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
+            .cd =
+               "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
+            .ram_start = 0x40000000ULL,
+            .scan_len = 128ULL * 1024 * 1024,
+        };
+        return data;
+    } else {
+        test_data data = {
+            .machine = machine,
+        };
+        return data;
+    }
+}
+
 static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
                               uint64_t base, enum TPMVersion tpm_version)
 {
@@ -1454,7 +1476,7 @@ static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
                                           machine, tpm_if);
     char *tmp_path = g_dir_make_tmp(tmp_dir_name, NULL);
     TPMTestState test;
-    test_data data = {};
+    test_data data = tcg_tpm_test_data(machine);
     GThread *thread;
     const char *suffix = tpm_version == TPM_VERSION_2_0 ? "tpm2" : "tpm12";
     char *args, *variant = g_strdup_printf(".%s.%s", tpm_if, suffix);
@@ -1474,13 +1496,14 @@ static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
     thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
     tpm_emu_test_wait_cond(&test);
 
-    data.machine = machine;
     data.variant = variant;
 
     args = g_strdup_printf(
+        " %s"
         " -chardev socket,id=chr,path=%s"
         " -tpmdev emulator,id=dev,chardev=chr"
         " -device tpm-%s,tpmdev=dev",
+        g_strcmp0(machine, "virt") == 0 ? "-cpu cortex-a57" : "",
         test.addr->u.q_unix.path, tpm_if);
 
     test_acpi_one(args, &data);
@@ -1506,6 +1529,16 @@ static void test_acpi_q35_tcg_tpm12_tis(void)
     test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_1_2);
 }
 
+static void test_acpi_q35_tcg_tpm2_crb(void)
+{
+    test_acpi_tcg_tpm("q35", "crb", 0xFED40000, TPM_VERSION_2_0);
+}
+
+static void test_acpi_virt_tcg_tpm2_crb(void)
+{
+    test_acpi_tcg_tpm("virt", "crb-device", 0xFED40000, TPM_VERSION_2_0);
+}
+
 static void test_acpi_tcg_dimm_pxm(const char *machine)
 {
     test_data data = {};
@@ -2212,6 +2245,9 @@ int main(int argc, char *argv[])
                 qtest_add_func("acpi/q35/tpm12-tis",
                                test_acpi_q35_tcg_tpm12_tis);
             }
+            if (tpm_model_is_available("-machine q35", "tpm-crb")) {
+                qtest_add_func("acpi/q35/tpm2-crb", test_acpi_q35_tcg_tpm2_crb);
+            }
             qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge);
             qtest_add_func("acpi/q35/no-acpi-hotplug",
                            test_acpi_q35_tcg_no_acpi_hotplug);
@@ -2301,6 +2337,9 @@ int main(int argc, char *argv[])
                 qtest_add_func("acpi/virt/viot", test_acpi_virt_viot);
             }
         }
+        if (tpm_model_is_available("-machine virt", "tpm-crb")) {
+            qtest_add_func("acpi/virt/tpm2-crb", test_acpi_virt_tcg_tpm2_crb);
+        }
     }
     ret = g_test_run();
     boot_sector_cleanup(disk);
Signature · 1 line
2.41.0
#17 lore raw

Re: [PATCH v5 12/14] tests: acpi: implement TPM CRB tests for ARM virt

Hi

Quoted 3 lines · Joelle van Dyne

nit: you also added tests for x86, could be a different patch?

For arm, the test fails until next patch with:

# starting QEMU: exec ./qemu-system-aarch64 -qtest
unix:/tmp/qtest-991279.sock -qtest-log /dev/null -chardev
socket,path=/tmp/qtest-991279.qmp,id=char0 -mon
chardev=char0,mode=control -display none -audio none -machine virt
-accel tcg -nodefaults -nographic -drive
if=pflash,format=raw,file=pc-bios/edk2-aarch64-code.fd,readonly=on
-drive if=pflash,format=raw,file=pc-bios/edk2-arm-vars.fd,snapshot=on
-cdrom tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2
-cpu cortex-a57 -chardev
socket,id=chr,path=/tmp/qemu-test_acpi_virt_tcg_crb-device.KZ3GE2/sock
-tpmdev emulator,id=dev,chardev=chr -device tpm-crb-device,tpmdev=dev
-accel qtest
Warning! zero length expected file 'tests/data/acpi/virt/TPM2.crb-device.tpm2'
Warning! zero length expected file 'tests/data/acpi/virt/DSDT.crb-device.tpm2'
acpi-test: Warning!  binary file mismatch. Actual
[aml:/tmp/aml-GO4ME2], Expected
[aml:tests/data/acpi/virt/TPM2.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how
to update expected files.
acpi-test: Warning!  binary file mismatch. Actual
[aml:/tmp/aml-6N4ME2], Expected
[aml:tests/data/acpi/virt/DSDT.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how
to update expected files.
to see ASL diff between mismatched files install IASL, rebuild QEMU
from scratch and re-run tests with V=1 environment variable set**
ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
not ok /aarch64/acpi/virt/tpm2-crb -
ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
Bail out!
qemu-system-aarch64: tpm-emulator: Could not cleanly shutdown the TPM:
Resource temporarily unavailable
Unexpected error in qio_channel_socket_writev() at ../io/channel-socket.c:622:
/home/elmarco/src/qemu/buildall/tests/qtest/bios-tables-test: Unable
to write to socket: Bad file descriptor
Quoted 103 lines
---
 tests/qtest/bios-tables-test.c | 43 ++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)
tests/qtest/bios-tables-test.c +41 -3
index 71af5cf69f..bb4ebf00c1 100644
@@ -1447,6 +1447,28 @@ static void test_acpi_piix4_tcg_numamem(void)
 
 uint64_t tpm_tis_base_addr;
 
+static test_data tcg_tpm_test_data(const char *machine)
+{
+    if (g_strcmp0(machine, "virt") == 0) {
+        test_data data = {
+            .machine = "virt",
+            .tcg_only = true,
+            .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
+            .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
+            .cd =
+               "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
+            .ram_start = 0x40000000ULL,
+            .scan_len = 128ULL * 1024 * 1024,
+        };
+        return data;
+    } else {
+        test_data data = {
+            .machine = machine,
+        };
+        return data;
+    }
+}
+
 static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
                               uint64_t base, enum TPMVersion tpm_version)
 {
@@ -1454,7 +1476,7 @@ static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
                                           machine, tpm_if);
     char *tmp_path = g_dir_make_tmp(tmp_dir_name, NULL);
     TPMTestState test;
-    test_data data = {};
+    test_data data = tcg_tpm_test_data(machine);
     GThread *thread;
     const char *suffix = tpm_version == TPM_VERSION_2_0 ? "tpm2" : "tpm12";
     char *args, *variant = g_strdup_printf(".%s.%s", tpm_if, suffix);
@@ -1474,13 +1496,14 @@ static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
     thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
     tpm_emu_test_wait_cond(&test);
 
-    data.machine = machine;
     data.variant = variant;
 
     args = g_strdup_printf(
+        " %s"
         " -chardev socket,id=chr,path=%s"
         " -tpmdev emulator,id=dev,chardev=chr"
         " -device tpm-%s,tpmdev=dev",
+        g_strcmp0(machine, "virt") == 0 ? "-cpu cortex-a57" : "",
         test.addr->u.q_unix.path, tpm_if);
 
     test_acpi_one(args, &data);
@@ -1506,6 +1529,16 @@ static void test_acpi_q35_tcg_tpm12_tis(void)
     test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_1_2);
 }
 
+static void test_acpi_q35_tcg_tpm2_crb(void)
+{
+    test_acpi_tcg_tpm("q35", "crb", 0xFED40000, TPM_VERSION_2_0);
+}
+
+static void test_acpi_virt_tcg_tpm2_crb(void)
+{
+    test_acpi_tcg_tpm("virt", "crb-device", 0xFED40000, TPM_VERSION_2_0);
+}
+
 static void test_acpi_tcg_dimm_pxm(const char *machine)
 {
     test_data data = {};
@@ -2212,6 +2245,9 @@ int main(int argc, char *argv[])
                 qtest_add_func("acpi/q35/tpm12-tis",
                                test_acpi_q35_tcg_tpm12_tis);
             }
+            if (tpm_model_is_available("-machine q35", "tpm-crb")) {
+                qtest_add_func("acpi/q35/tpm2-crb", test_acpi_q35_tcg_tpm2_crb);
+            }
             qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge);
             qtest_add_func("acpi/q35/no-acpi-hotplug",
                            test_acpi_q35_tcg_no_acpi_hotplug);
@@ -2301,6 +2337,9 @@ int main(int argc, char *argv[])
                 qtest_add_func("acpi/virt/viot", test_acpi_virt_viot);
             }
         }
+        if (tpm_model_is_available("-machine virt", "tpm-crb")) {
+            qtest_add_func("acpi/virt/tpm2-crb", test_acpi_virt_tcg_tpm2_crb);
+        }
     }
     ret = g_test_run();
     boot_sector_cleanup(disk);
--

2.41.0

Signature · 1 line
Marc-André Lureau
#18 lore raw

Re: [PATCH v5 12/14] tests: acpi: implement TPM CRB tests for ARM virt

Quoted 40 lines · Marc-André Lureau

Hi

Quoted 3 lines · Joelle van Dyne

nit: you also added tests for x86, could be a different patch?

For arm, the test fails until next patch with:

# starting QEMU: exec ./qemu-system-aarch64 -qtest
unix:/tmp/qtest-991279.sock -qtest-log /dev/null -chardev
socket,path=/tmp/qtest-991279.qmp,id=char0 -mon
chardev=char0,mode=control -display none -audio none -machine virt
-accel tcg -nodefaults -nographic -drive
if=pflash,format=raw,file=pc-bios/edk2-aarch64-code.fd,readonly=on
(quoted diff) +0 -3
-drive if=pflash,format=raw,file=pc-bios/edk2-arm-vars.fd,snapshot=on
-cdrom tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2
-cpu cortex-a57 -chardev

socket,id=chr,path=/tmp/qemu-test_acpi_virt_tcg_crb-device.KZ3GE2/sock

(quoted diff) +0 -2
-tpmdev emulator,id=dev,chardev=chr -device tpm-crb-device,tpmdev=dev
-accel qtest

Warning! zero length expected file 'tests/data/acpi/virt/TPM2.crb-device.tpm2' Warning! zero length expected file 'tests/data/acpi/virt/DSDT.crb-device.tpm2'
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-GO4ME2], Expected [aml:tests/data/acpi/virt/TPM2.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-6N4ME2], Expected [aml:tests/data/acpi/virt/DSDT.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
to see ASL diff between mismatched files install IASL, rebuild QEMU from scratch and re-run tests with V=1 environment variable set** ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
not ok /aarch64/acpi/virt/tpm2-crb -

Quoted 8 lines

ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
Bail out!
qemu-system-aarch64: tpm-emulator: Could not cleanly shutdown the TPM:
Resource temporarily unavailable
Unexpected error in qio_channel_socket_writev() at ../io/channel-socket.c:622:
/home/elmarco/src/qemu/buildall/tests/qtest/bios-tables-test: Unable to write to socket: Bad file descriptor

I had seen errors like these as well. However, the first error appears when applying the series after getting it using 'b4 am' -- it complains about patch 13. I always take patch 13 & 14 from my email client.

Both of these here pass:

QTEST_QEMU_BINARY=./qemu-system-aarch64 ./tests/qtest/bios-tables-test QTEST_QEMU_BINARY=./qemu-system-x86_64 ./tests/qtest/bios-tables-test

#19 lore raw

Re: [PATCH v5 12/14] tests: acpi: implement TPM CRB tests for ARM virt

In reply to Marc-André Lureau
Quoted 40 lines · Marc-André Lureau

Hi

Quoted 3 lines · Joelle van Dyne

nit: you also added tests for x86, could be a different patch?

For arm, the test fails until next patch with:

# starting QEMU: exec ./qemu-system-aarch64 -qtest
unix:/tmp/qtest-991279.sock -qtest-log /dev/null -chardev
socket,path=/tmp/qtest-991279.qmp,id=char0 -mon
chardev=char0,mode=control -display none -audio none -machine virt
-accel tcg -nodefaults -nographic -drive
if=pflash,format=raw,file=pc-bios/edk2-aarch64-code.fd,readonly=on
(quoted diff) +0 -3
-drive if=pflash,format=raw,file=pc-bios/edk2-arm-vars.fd,snapshot=on
-cdrom tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2
-cpu cortex-a57 -chardev

socket,id=chr,path=/tmp/qemu-test_acpi_virt_tcg_crb-device.KZ3GE2/sock

(quoted diff) +0 -2
-tpmdev emulator,id=dev,chardev=chr -device tpm-crb-device,tpmdev=dev
-accel qtest

Warning! zero length expected file 'tests/data/acpi/virt/TPM2.crb-device.tpm2' Warning! zero length expected file 'tests/data/acpi/virt/DSDT.crb-device.tpm2'
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-GO4ME2], Expected [aml:tests/data/acpi/virt/TPM2.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-6N4ME2], Expected [aml:tests/data/acpi/virt/DSDT.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
to see ASL diff between mismatched files install IASL, rebuild QEMU from scratch and re-run tests with V=1 environment variable set** ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
not ok /aarch64/acpi/virt/tpm2-crb -

Quoted 9 lines

ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
Bail out!
qemu-system-aarch64: tpm-emulator: Could not cleanly shutdown the TPM:
Resource temporarily unavailable
Unexpected error in qio_channel_socket_writev() at ../io/channel-socket.c:622:
/home/elmarco/src/qemu/buildall/tests/qtest/bios-tables-test: Unable to write to socket: Bad file descriptor

Travis testing on s390x I see the following failures for this patchset (search for 'ERROR'):

https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267230363

Summary of Failures:

134/320 qemu:qtest+qtest-aarch64 / qtest-aarch64/tpm-crb-device-test
ERROR 0.70s killed by signal 6 SIGABRT

219/320 qemu:qtest+qtest-x86_64 / qtest-x86_64/tpm-crb-test
ERROR 0.88s killed by signal 6 SIGABRT

Summary of Failures:

271/537 qemu:qtest+qtest-i386 / qtest-i386/tpm-crb-test
ERROR 0.59s killed by signal 6 SIGABRT

My guess is it's an endianess issue on big endian machines due to reading from the ROM device where we lost the .endianess:

+const MemoryRegionOps tpm_crb_memory_ops = {

+    .read = tpm_crb_mmio_read,
+    .write = tpm_crb_mmio_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {

+ .min_access_size = 1,
+ .max_access_size = 4,
+ },
+};

#20 lore raw

Re: [PATCH v5 12/14] tests: acpi: implement TPM CRB tests for ARM virt

Quoted 72 lines · Stefan Berger
Quoted 40 lines · Marc-André Lureau

Hi

Quoted 3 lines · Joelle van Dyne

nit: you also added tests for x86, could be a different patch?

For arm, the test fails until next patch with:

# starting QEMU: exec ./qemu-system-aarch64 -qtest
unix:/tmp/qtest-991279.sock -qtest-log /dev/null -chardev
socket,path=/tmp/qtest-991279.qmp,id=char0 -mon
chardev=char0,mode=control -display none -audio none -machine virt
-accel tcg -nodefaults -nographic -drive
if=pflash,format=raw,file=pc-bios/edk2-aarch64-code.fd,readonly=on
(quoted diff) +0 -3
-drive if=pflash,format=raw,file=pc-bios/edk2-arm-vars.fd,snapshot=on
-cdrom tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2
-cpu cortex-a57 -chardev

socket,id=chr,path=/tmp/qemu-test_acpi_virt_tcg_crb-device.KZ3GE2/sock

(quoted diff) +0 -2
-tpmdev emulator,id=dev,chardev=chr -device tpm-crb-device,tpmdev=dev
-accel qtest

Warning! zero length expected file 'tests/data/acpi/virt/TPM2.crb-device.tpm2' Warning! zero length expected file 'tests/data/acpi/virt/DSDT.crb-device.tpm2'
acpi-test: Warning!  binary file mismatch. Actual [aml:/tmp/aml-GO4ME2], Expected [aml:tests/data/acpi/virt/TPM2.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
acpi-test: Warning!  binary file mismatch. Actual [aml:/tmp/aml-6N4ME2], Expected [aml:tests/data/acpi/virt/DSDT.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
to see ASL diff between mismatched files install IASL, rebuild QEMU from scratch and re-run tests with V=1 environment variable set** ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
not ok /aarch64/acpi/virt/tpm2-crb -

Quoted 9 lines

ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
Bail out!
qemu-system-aarch64: tpm-emulator: Could not cleanly shutdown the TPM:
Resource temporarily unavailable
Unexpected error in qio_channel_socket_writev() at ../io/channel-socket.c:622:
/home/elmarco/src/qemu/buildall/tests/qtest/bios-tables-test: Unable to write to socket: Bad file descriptor

Travis testing on s390x I see the following failures for this patchset (search for 'ERROR'):

https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267230363

Summary of Failures:

134/320 qemu:qtest+qtest-aarch64 / qtest-aarch64/tpm-crb-device-test ERROR           0.70s   killed by signal 6 SIGABRT

219/320 qemu:qtest+qtest-x86_64 / qtest-x86_64/tpm-crb-test ERROR           0.88s   killed by signal 6 SIGABRT

Summary of Failures:

271/537 qemu:qtest+qtest-i386 / qtest-i386/tpm-crb-test ERROR           0.59s   killed by signal 6 SIGABRT

My guess is it's an endianess issue on big endian machines due to reading from the ROM device where we lost the .endianess:

Review

Quoted 9 lines
(quoted diff) +9 -0
+const MemoryRegionOps tpm_crb_memory_ops = {
+    .read = tpm_crb_mmio_read,
+    .write = tpm_crb_mmio_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+};

I think we need a 2nd set of registers to support the endianess conversion. It's not exactly nice, though. Basically the saved_regs could be used for this directly, even though I did not do that but introduced n_regs: https://github.com/stefanberger/qemu-tpm/commit/90f6b21c0dd93dbb13d9e80a628f5b631fd07d91

This patch allows the tests on s390x to run farther but the execution of the command doesn't seem to work maybe due to command data that were also written in wrong endianess. I don't know. I would have to get access to a big endian / s390 machine to be able to fix it.

#21 lore raw

Re: [PATCH v5 12/14] tests: acpi: implement TPM CRB tests for ARM virt

Quoted 87 lines · Stefan Berger
Quoted 72 lines · Stefan Berger
Quoted 40 lines · Marc-André Lureau

Hi

Quoted 3 lines · Joelle van Dyne

nit: you also added tests for x86, could be a different patch?

For arm, the test fails until next patch with:

# starting QEMU: exec ./qemu-system-aarch64 -qtest
unix:/tmp/qtest-991279.sock -qtest-log /dev/null -chardev
socket,path=/tmp/qtest-991279.qmp,id=char0 -mon
chardev=char0,mode=control -display none -audio none -machine virt
-accel tcg -nodefaults -nographic -drive
if=pflash,format=raw,file=pc-bios/edk2-aarch64-code.fd,readonly=on
(quoted diff) +0 -3
-drive if=pflash,format=raw,file=pc-bios/edk2-arm-vars.fd,snapshot=on
-cdrom tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2
-cpu cortex-a57 -chardev

socket,id=chr,path=/tmp/qemu-test_acpi_virt_tcg_crb-device.KZ3GE2/sock

(quoted diff) +0 -2
-tpmdev emulator,id=dev,chardev=chr -device tpm-crb-device,tpmdev=dev
-accel qtest

Warning! zero length expected file 'tests/data/acpi/virt/TPM2.crb-device.tpm2' Warning! zero length expected file 'tests/data/acpi/virt/DSDT.crb-device.tpm2'
acpi-test: Warning!  binary file mismatch. Actual [aml:/tmp/aml-GO4ME2], Expected [aml:tests/data/acpi/virt/TPM2.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
acpi-test: Warning!  binary file mismatch. Actual [aml:/tmp/aml-6N4ME2], Expected [aml:tests/data/acpi/virt/DSDT.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
to see ASL diff between mismatched files install IASL, rebuild QEMU from scratch and re-run tests with V=1 environment variable set** ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
not ok /aarch64/acpi/virt/tpm2-crb -

Quoted 9 lines

ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
Bail out!
qemu-system-aarch64: tpm-emulator: Could not cleanly shutdown the TPM:
Resource temporarily unavailable
Unexpected error in qio_channel_socket_writev() at ../io/channel-socket.c:622:
/home/elmarco/src/qemu/buildall/tests/qtest/bios-tables-test: Unable to write to socket: Bad file descriptor

Travis testing on s390x I see the following failures for this patchset (search for 'ERROR'):

https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267230363

Summary of Failures:

134/320 qemu:qtest+qtest-aarch64 / qtest-aarch64/tpm-crb-device-test ERROR           0.70s   killed by signal 6 SIGABRT

219/320 qemu:qtest+qtest-x86_64 / qtest-x86_64/tpm-crb-test ERROR           0.88s   killed by signal 6 SIGABRT

Summary of Failures:

271/537 qemu:qtest+qtest-i386 / qtest-i386/tpm-crb-test ERROR           0.59s   killed by signal 6 SIGABRT

My guess is it's an endianess issue on big endian machines due to reading from the ROM device where we lost the .endianess:

Review

Quoted 9 lines
(quoted diff) +9 -0
+const MemoryRegionOps tpm_crb_memory_ops = {
+    .read = tpm_crb_mmio_read,
+    .write = tpm_crb_mmio_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+};

I think we need a 2nd set of registers to support the endianess conversion. It's not exactly nice, though. Basically the saved_regs could be used for this directly, even though I did not do that but introduced n_regs: https://github.com/stefanberger/qemu-tpm/commit/90f6b21c0dd93dbb13d9e80a628f5b631fd07d91

Quoted 3 lines

This patch allows the tests on s390x to run farther but the execution of the command doesn't seem to work maybe due to command data that were also written in wrong endianess. I don't know. I would have to get access to a big endian / s390 machine to be able to fix it.

The latest version now passes on Travis s390x: https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267245220

#22 lore raw

Re: [PATCH v5 12/14] tests: acpi: implement TPM CRB tests for ARM virt

Quoted 110 lines · Stefan Berger
Quoted 99 lines · Stefan Berger
Quoted 80 lines · Stefan Berger
Quoted 43 lines · Marc-André Lureau

Hi

Quoted 3 lines · Joelle van Dyne

nit: you also added tests for x86, could be a different patch?

For arm, the test fails until next patch with:

# starting QEMU: exec ./qemu-system-aarch64 -qtest
unix:/tmp/qtest-991279.sock -qtest-log /dev/null -chardev
socket,path=/tmp/qtest-991279.qmp,id=char0 -mon
chardev=char0,mode=control -display none -audio none -machine virt
-accel tcg -nodefaults -nographic -drive
if=pflash,format=raw,file=pc-bios/edk2-aarch64-code.fd,readonly=on
(quoted diff) +0 -3
-drive if=pflash,format=raw,file=pc-bios/edk2-arm-vars.fd,snapshot=on
-cdrom tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2
-cpu cortex-a57 -chardev

socket,id=chr,path=/tmp/qemu-test_acpi_virt_tcg_crb-device.KZ3GE2/sock

(quoted diff) +0 -2
-tpmdev emulator,id=dev,chardev=chr -device tpm-crb-device,tpmdev=dev
-accel qtest

Warning! zero length expected file 'tests/data/acpi/virt/TPM2.crb-device.tpm2'
Warning! zero length expected file 'tests/data/acpi/virt/DSDT.crb-device.tpm2'
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-GO4ME2], Expected [aml:tests/data/acpi/virt/TPM2.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-6N4ME2], Expected [aml:tests/data/acpi/virt/DSDT.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
to see ASL diff between mismatched files install IASL, rebuild QEMU from scratch and re-run tests with V=1 environment variable set** ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
not ok /aarch64/acpi/virt/tpm2-crb - ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion

Quoted 9 lines

failed: (all_tables_match)
Bail out!
qemu-system-aarch64: tpm-emulator: Could not cleanly shutdown the TPM:
Resource temporarily unavailable
Unexpected error in qio_channel_socket_writev() at ../io/channel-socket.c:622:
/home/elmarco/src/qemu/buildall/tests/qtest/bios-tables-test: Unable to write to socket: Bad file descriptor

Travis testing on s390x I see the following failures for this patchset (search for 'ERROR'):

https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267230363

Summary of Failures:

134/320 qemu:qtest+qtest-aarch64 / qtest-aarch64/tpm-crb-device-test
ERROR 0.70s killed by signal 6 SIGABRT

219/320 qemu:qtest+qtest-x86_64 / qtest-x86_64/tpm-crb-test
ERROR 0.88s killed by signal 6 SIGABRT

Summary of Failures:

271/537 qemu:qtest+qtest-i386 / qtest-i386/tpm-crb-test
ERROR 0.59s killed by signal 6 SIGABRT

My guess is it's an endianess issue on big endian machines due to reading from the ROM device where we lost the .endianess:

Review

Quoted 9 lines
(quoted diff) +9 -0
+const MemoryRegionOps tpm_crb_memory_ops = {
+    .read = tpm_crb_mmio_read,
+    .write = tpm_crb_mmio_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+};

I think we need a 2nd set of registers to support the endianess conversion. It's not exactly nice, though. Basically the saved_regs could be used for this directly, even though I did not do that but introduced n_regs:
https://github.com/stefanberger/qemu-tpm/commit/90f6b21c0dd93dbb13d9e80a628f5b631fd07d91

Quoted 6 lines

This patch allows the tests on s390x to run farther but the execution of the command doesn't seem to work maybe due to command data that were also written in wrong endianess. I don't know. I would have to get access to a big endian / s390 machine to be able to fix it.

Quoted 3 lines

The latest version now passes on Travis s390x:
https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267245220

Are the tests failing on S390X due to the added code or are they failing because previously it was untested? I don't think the original code took account of endianness and that should be fixed, but feels like it should be in a separate patch set?

#23 lore raw

Re: [PATCH v5 12/14] tests: acpi: implement TPM CRB tests for ARM virt

Quoted 111 lines · Joelle van Dyne
Quoted 110 lines · Stefan Berger
Quoted 105 lines · Stefan Berger
Quoted 80 lines · Stefan Berger
Quoted 43 lines · Marc-André Lureau

Hi

Quoted 3 lines · Joelle van Dyne

nit: you also added tests for x86, could be a different patch?

For arm, the test fails until next patch with:

# starting QEMU: exec ./qemu-system-aarch64 -qtest
unix:/tmp/qtest-991279.sock -qtest-log /dev/null -chardev
socket,path=/tmp/qtest-991279.qmp,id=char0 -mon
chardev=char0,mode=control -display none -audio none -machine virt
-accel tcg -nodefaults -nographic -drive
if=pflash,format=raw,file=pc-bios/edk2-aarch64-code.fd,readonly=on
(quoted diff) +0 -3
-drive if=pflash,format=raw,file=pc-bios/edk2-arm-vars.fd,snapshot=on
-cdrom tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2
-cpu cortex-a57 -chardev

socket,id=chr,path=/tmp/qemu-test_acpi_virt_tcg_crb-device.KZ3GE2/sock

(quoted diff) +0 -2
-tpmdev emulator,id=dev,chardev=chr -device tpm-crb-device,tpmdev=dev
-accel qtest

Warning! zero length expected file 'tests/data/acpi/virt/TPM2.crb-device.tpm2'
Warning! zero length expected file 'tests/data/acpi/virt/DSDT.crb-device.tpm2'
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-GO4ME2], Expected [aml:tests/data/acpi/virt/TPM2.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-6N4ME2], Expected [aml:tests/data/acpi/virt/DSDT.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
to see ASL diff between mismatched files install IASL, rebuild QEMU from scratch and re-run tests with V=1 environment variable set** ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
not ok /aarch64/acpi/virt/tpm2-crb - ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion

Quoted 9 lines

failed: (all_tables_match)
Bail out!
qemu-system-aarch64: tpm-emulator: Could not cleanly shutdown the TPM:
Resource temporarily unavailable
Unexpected error in qio_channel_socket_writev() at ../io/channel-socket.c:622:
/home/elmarco/src/qemu/buildall/tests/qtest/bios-tables-test: Unable to write to socket: Bad file descriptor

Travis testing on s390x I see the following failures for this patchset (search for 'ERROR'):

https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267230363

Summary of Failures:

134/320 qemu:qtest+qtest-aarch64 / qtest-aarch64/tpm-crb-device-test
ERROR 0.70s killed by signal 6 SIGABRT

219/320 qemu:qtest+qtest-x86_64 / qtest-x86_64/tpm-crb-test
ERROR 0.88s killed by signal 6 SIGABRT

Summary of Failures:

271/537 qemu:qtest+qtest-i386 / qtest-i386/tpm-crb-test
ERROR 0.59s killed by signal 6 SIGABRT

My guess is it's an endianess issue on big endian machines due to reading from the ROM device where we lost the .endianess:

Review

Quoted 9 lines
(quoted diff) +9 -0
+const MemoryRegionOps tpm_crb_memory_ops = {
+    .read = tpm_crb_mmio_read,
+    .write = tpm_crb_mmio_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+};

I think we need a 2nd set of registers to support the endianess conversion. It's not exactly nice, though. Basically the saved_regs could be used for this directly, even though I did not do that but introduced n_regs:
https://github.com/stefanberger/qemu-tpm/commit/90f6b21c0dd93dbb13d9e80a628f5b631fd07d91

This patch allows the tests on s390x to run farther but the execution of the command doesn't seem to work maybe due to command data that were also written in wrong endianess. I don't know. I would have to get access to a big endian / s390 machine to be able to fix it.

Quoted 8 lines
Quoted 3 lines

The latest version now passes on Travis s390x:
https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267245220

Are the tests failing on S390X due to the added code or are they failing because previously it was untested? I don't think the original code took account of endianness and that should be fixed, but feels like it should be in a separate patch set?

They are failing because something like the topmost one or two patches as in this branch here are missing for a big endian host:

https://github.com/stefanberger/qemu-tpm/tree/joelle.v5%2B2nd_registers

#24 lore raw

Re: [PATCH v5 12/14] tests: acpi: implement TPM CRB tests for ARM virt

Quoted 124 lines · Stefan Berger
Quoted 111 lines · Joelle van Dyne
Quoted 110 lines · Stefan Berger
Quoted 105 lines · Stefan Berger
Quoted 80 lines · Stefan Berger
Quoted 43 lines · Marc-André Lureau

Hi

Quoted 3 lines · Joelle van Dyne

nit: you also added tests for x86, could be a different patch?

For arm, the test fails until next patch with:

# starting QEMU: exec ./qemu-system-aarch64 -qtest
unix:/tmp/qtest-991279.sock -qtest-log /dev/null -chardev
socket,path=/tmp/qtest-991279.qmp,id=char0 -mon
chardev=char0,mode=control -display none -audio none -machine virt
-accel tcg -nodefaults -nographic -drive
if=pflash,format=raw,file=pc-bios/edk2-aarch64-code.fd,readonly=on
(quoted diff) +0 -3
-drive if=pflash,format=raw,file=pc-bios/edk2-arm-vars.fd,snapshot=on
-cdrom tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2
-cpu cortex-a57 -chardev

socket,id=chr,path=/tmp/qemu-test_acpi_virt_tcg_crb-device.KZ3GE2/sock

(quoted diff) +0 -2
-tpmdev emulator,id=dev,chardev=chr -device tpm-crb-device,tpmdev=dev
-accel qtest

Warning! zero length expected file 'tests/data/acpi/virt/TPM2.crb-device.tpm2'
Warning! zero length expected file 'tests/data/acpi/virt/DSDT.crb-device.tpm2'
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-GO4ME2], Expected [aml:tests/data/acpi/virt/TPM2.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-6N4ME2], Expected [aml:tests/data/acpi/virt/DSDT.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
to see ASL diff between mismatched files install IASL, rebuild QEMU from scratch and re-run tests with V=1 environment variable set** ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
not ok /aarch64/acpi/virt/tpm2-crb - ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion

Quoted 9 lines

failed: (all_tables_match)
Bail out!
qemu-system-aarch64: tpm-emulator: Could not cleanly shutdown the TPM:
Resource temporarily unavailable
Unexpected error in qio_channel_socket_writev() at ../io/channel-socket.c:622:
/home/elmarco/src/qemu/buildall/tests/qtest/bios-tables-test: Unable to write to socket: Bad file descriptor

Travis testing on s390x I see the following failures for this patchset (search for 'ERROR'):

https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267230363

Summary of Failures:

134/320 qemu:qtest+qtest-aarch64 / qtest-aarch64/tpm-crb-device-test
ERROR 0.70s killed by signal 6 SIGABRT

219/320 qemu:qtest+qtest-x86_64 / qtest-x86_64/tpm-crb-test
ERROR 0.88s killed by signal 6 SIGABRT

Summary of Failures:

271/537 qemu:qtest+qtest-i386 / qtest-i386/tpm-crb-test
ERROR 0.59s killed by signal 6 SIGABRT

My guess is it's an endianess issue on big endian machines due to reading from the ROM device where we lost the .endianess:

Review

Quoted 9 lines
(quoted diff) +9 -0
+const MemoryRegionOps tpm_crb_memory_ops = {
+    .read = tpm_crb_mmio_read,
+    .write = tpm_crb_mmio_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+};

I think we need a 2nd set of registers to support the endianess conversion. It's not exactly nice, though. Basically the saved_regs could be used for this directly, even though I did not do that but introduced n_regs:
https://github.com/stefanberger/qemu-tpm/commit/90f6b21c0dd93dbb13d9e80a628f5b631fd07d91

This patch allows the tests on s390x to run farther but the execution of the command doesn't seem to work maybe due to command data that were also written in wrong endianess. I don't know. I would have to get access to a big endian / s390 machine to be able to fix it.

Quoted 8 lines
Quoted 3 lines

The latest version now passes on Travis s390x:
https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267245220

Are the tests failing on S390X due to the added code or are they failing because previously it was untested? I don't think the original code took account of endianness and that should be fixed, but feels like it should be in a separate patch set?

Quoted 4 lines

They are failing because something like the topmost one or two patches as in this branch here are missing for a big endian host:

https://github.com/stefanberger/qemu-tpm/tree/joelle.v5%2B2nd_registers

Right, but is this issue new due to the patchset? i.e. if just the tests were added without the other patches, would they still fail? If so, I think the fixes should be in another patchset while we disable them in this patchset.

#25 lore raw

Re: [PATCH v5 12/14] tests: acpi: implement TPM CRB tests for ARM virt

Quoted 125 lines · Joelle van Dyne
Quoted 124 lines · Stefan Berger
Quoted 119 lines · Joelle van Dyne
Quoted 110 lines · Stefan Berger
Quoted 99 lines · Stefan Berger
Quoted 80 lines · Stefan Berger
Quoted 43 lines · Marc-André Lureau

Hi

Quoted 3 lines · Joelle van Dyne

nit: you also added tests for x86, could be a different patch?

For arm, the test fails until next patch with:

# starting QEMU: exec ./qemu-system-aarch64 -qtest
unix:/tmp/qtest-991279.sock -qtest-log /dev/null -chardev
socket,path=/tmp/qtest-991279.qmp,id=char0 -mon
chardev=char0,mode=control -display none -audio none -machine virt
-accel tcg -nodefaults -nographic -drive
if=pflash,format=raw,file=pc-bios/edk2-aarch64-code.fd,readonly=on
(quoted diff) +0 -3
-drive if=pflash,format=raw,file=pc-bios/edk2-arm-vars.fd,snapshot=on
-cdrom tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2
-cpu cortex-a57 -chardev

socket,id=chr,path=/tmp/qemu-test_acpi_virt_tcg_crb-device.KZ3GE2/sock

(quoted diff) +0 -2
-tpmdev emulator,id=dev,chardev=chr -device tpm-crb-device,tpmdev=dev
-accel qtest

Warning! zero length expected file 'tests/data/acpi/virt/TPM2.crb-device.tpm2'
Warning! zero length expected file 'tests/data/acpi/virt/DSDT.crb-device.tpm2'
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-GO4ME2], Expected [aml:tests/data/acpi/virt/TPM2.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-6N4ME2], Expected [aml:tests/data/acpi/virt/DSDT.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
to see ASL diff between mismatched files install IASL, rebuild QEMU from scratch and re-run tests with V=1 environment variable set** ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
not ok /aarch64/acpi/virt/tpm2-crb - ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion

Quoted 9 lines

failed: (all_tables_match)
Bail out!
qemu-system-aarch64: tpm-emulator: Could not cleanly shutdown the TPM:
Resource temporarily unavailable
Unexpected error in qio_channel_socket_writev() at ../io/channel-socket.c:622:
/home/elmarco/src/qemu/buildall/tests/qtest/bios-tables-test: Unable to write to socket: Bad file descriptor

Travis testing on s390x I see the following failures for this patchset (search for 'ERROR'):

https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267230363

Summary of Failures:

134/320 qemu:qtest+qtest-aarch64 / qtest-aarch64/tpm-crb-device-test
ERROR 0.70s killed by signal 6 SIGABRT

219/320 qemu:qtest+qtest-x86_64 / qtest-x86_64/tpm-crb-test
ERROR 0.88s killed by signal 6 SIGABRT

Summary of Failures:

271/537 qemu:qtest+qtest-i386 / qtest-i386/tpm-crb-test
ERROR 0.59s killed by signal 6 SIGABRT

My guess is it's an endianess issue on big endian machines due to reading from the ROM device where we lost the .endianess:

Review

Quoted 9 lines
(quoted diff) +9 -0
+const MemoryRegionOps tpm_crb_memory_ops = {
+    .read = tpm_crb_mmio_read,
+    .write = tpm_crb_mmio_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+};

I think we need a 2nd set of registers to support the endianess conversion. It's not exactly nice, though. Basically the saved_regs could be used for this directly, even though I did not do that but introduced n_regs:
https://github.com/stefanberger/qemu-tpm/commit/90f6b21c0dd93dbb13d9e80a628f5b631fd07d91

Quoted 6 lines

This patch allows the tests on s390x to run farther but the execution of the command doesn't seem to work maybe due to command data that were also written in wrong endianess. I don't know. I would have to get access to a big endian / s390 machine to be able to fix it.

Quoted 3 lines

The latest version now passes on Travis s390x:
https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267245220

Are the tests failing on S390X due to the added code or are they failing because previously it was untested? I don't think the original code took account of endianness and that should be fixed, but feels like it should be in a separate patch set?

Quoted 6 lines
Quoted 4 lines

They are failing because something like the topmost one or two patches as in this branch here are missing for a big endian host:

https://github.com/stefanberger/qemu-tpm/tree/joelle.v5%2B2nd_registers

Right, but is this issue new due to the patchset? i.e. if just the

Yes, it is due to this patchset. The reason is that CRB switched to a ROMD interface where the fact that the MMIO registers are little endian got lost for existing x86_64 support.

Quoted 3 lines

tests were added without the other patches, would they still fail? If so, I think the fixes should be in another patchset while we disable them in this patchset.

#26 lore raw

Re: [PATCH v5 12/14] tests: acpi: implement TPM CRB tests for ARM virt

Quoted 132 lines · Stefan Berger
Quoted 128 lines · Joelle van Dyne
Quoted 127 lines · Stefan Berger
Quoted 111 lines · Joelle van Dyne
Quoted 110 lines · Stefan Berger
Quoted 105 lines · Stefan Berger
Quoted 80 lines · Stefan Berger
Quoted 43 lines · Marc-André Lureau

Hi

Quoted 3 lines · Joelle van Dyne

nit: you also added tests for x86, could be a different patch?

For arm, the test fails until next patch with:

# starting QEMU: exec ./qemu-system-aarch64 -qtest
unix:/tmp/qtest-991279.sock -qtest-log /dev/null -chardev
socket,path=/tmp/qtest-991279.qmp,id=char0 -mon
chardev=char0,mode=control -display none -audio none -machine virt
-accel tcg -nodefaults -nographic -drive
if=pflash,format=raw,file=pc-bios/edk2-aarch64-code.fd,readonly=on
(quoted diff) +0 -3
-drive if=pflash,format=raw,file=pc-bios/edk2-arm-vars.fd,snapshot=on
-cdrom tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2
-cpu cortex-a57 -chardev

socket,id=chr,path=/tmp/qemu-test_acpi_virt_tcg_crb-device.KZ3GE2/sock

(quoted diff) +0 -2
-tpmdev emulator,id=dev,chardev=chr -device tpm-crb-device,tpmdev=dev
-accel qtest

Warning! zero length expected file 'tests/data/acpi/virt/TPM2.crb-device.tpm2'
Warning! zero length expected file 'tests/data/acpi/virt/DSDT.crb-device.tpm2'
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-GO4ME2], Expected [aml:tests/data/acpi/virt/TPM2.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-6N4ME2], Expected [aml:tests/data/acpi/virt/DSDT.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
to see ASL diff between mismatched files install IASL, rebuild QEMU from scratch and re-run tests with V=1 environment variable set** ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
not ok /aarch64/acpi/virt/tpm2-crb - ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion

Quoted 9 lines

failed: (all_tables_match)
Bail out!
qemu-system-aarch64: tpm-emulator: Could not cleanly shutdown the TPM:
Resource temporarily unavailable
Unexpected error in qio_channel_socket_writev() at ../io/channel-socket.c:622:
/home/elmarco/src/qemu/buildall/tests/qtest/bios-tables-test: Unable to write to socket: Bad file descriptor

Travis testing on s390x I see the following failures for this patchset (search for 'ERROR'):

https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267230363

Summary of Failures:

134/320 qemu:qtest+qtest-aarch64 / qtest-aarch64/tpm-crb-device-test
ERROR 0.70s killed by signal 6 SIGABRT

219/320 qemu:qtest+qtest-x86_64 / qtest-x86_64/tpm-crb-test
ERROR 0.88s killed by signal 6 SIGABRT

Summary of Failures:

271/537 qemu:qtest+qtest-i386 / qtest-i386/tpm-crb-test
ERROR 0.59s killed by signal 6 SIGABRT

My guess is it's an endianess issue on big endian machines due to reading from the ROM device where we lost the .endianess:

Review

Quoted 9 lines
(quoted diff) +9 -0
+const MemoryRegionOps tpm_crb_memory_ops = {
+    .read = tpm_crb_mmio_read,
+    .write = tpm_crb_mmio_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+};

I think we need a 2nd set of registers to support the endianess conversion. It's not exactly nice, though. Basically the saved_regs could be used for this directly, even though I did not do that but introduced n_regs:
https://github.com/stefanberger/qemu-tpm/commit/90f6b21c0dd93dbb13d9e80a628f5b631fd07d91

This patch allows the tests on s390x to run farther but the execution of the command doesn't seem to work maybe due to command data that were also written in wrong endianess. I don't know. I would have to get access to a big endian / s390 machine to be able to fix it.

Quoted 8 lines
Quoted 3 lines

The latest version now passes on Travis s390x:
https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267245220

Are the tests failing on S390X due to the added code or are they failing because previously it was untested? I don't think the original code took account of endianness and that should be fixed, but feels like it should be in a separate patch set?

They are failing because something like the topmost one or two patches as in this branch here are missing for a big endian host:

Quoted 11 lines
Quoted 3 lines
Quoted 1 line

https://github.com/stefanberger/qemu-tpm/tree/joelle.v5%2B2nd_registers

Right, but is this issue new due to the patchset? i.e. if just the

Yes, it is due to this patchset. The reason is that CRB switched to a ROMD interface where the fact that the MMIO registers are little endian got lost for existing x86_64 support.

Quoted 3 lines

tests were added without the other patches, would they still fail? If so, I think the fixes should be in another patchset while we disable them in this patchset.

I see, how do you want to best integrate your changes? Do you want me to squash your changes into the patch that introduces the code? Or do you want them to be separate commits?

#27 lore raw

Re: [PATCH v5 12/14] tests: acpi: implement TPM CRB tests for ARM virt

Quoted 141 lines · Joelle van Dyne
Quoted 140 lines · Stefan Berger
Quoted 125 lines · Joelle van Dyne
Quoted 124 lines · Stefan Berger
Quoted 119 lines · Joelle van Dyne
Quoted 110 lines · Stefan Berger
Quoted 99 lines · Stefan Berger
Quoted 80 lines · Stefan Berger
Quoted 43 lines · Marc-André Lureau

Hi

Quoted 3 lines · Joelle van Dyne

nit: you also added tests for x86, could be a different patch?

For arm, the test fails until next patch with:

# starting QEMU: exec ./qemu-system-aarch64 -qtest
unix:/tmp/qtest-991279.sock -qtest-log /dev/null -chardev
socket,path=/tmp/qtest-991279.qmp,id=char0 -mon
chardev=char0,mode=control -display none -audio none -machine virt
-accel tcg -nodefaults -nographic -drive
if=pflash,format=raw,file=pc-bios/edk2-aarch64-code.fd,readonly=on
(quoted diff) +0 -3
-drive if=pflash,format=raw,file=pc-bios/edk2-arm-vars.fd,snapshot=on
-cdrom tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2
-cpu cortex-a57 -chardev

socket,id=chr,path=/tmp/qemu-test_acpi_virt_tcg_crb-device.KZ3GE2/sock

(quoted diff) +0 -2
-tpmdev emulator,id=dev,chardev=chr -device tpm-crb-device,tpmdev=dev
-accel qtest

Warning! zero length expected file 'tests/data/acpi/virt/TPM2.crb-device.tpm2'
Warning! zero length expected file 'tests/data/acpi/virt/DSDT.crb-device.tpm2'
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-GO4ME2], Expected [aml:tests/data/acpi/virt/TPM2.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
acpi-test: Warning! binary file mismatch. Actual [aml:/tmp/aml-6N4ME2], Expected [aml:tests/data/acpi/virt/DSDT.crb-device.tpm2].
See source file tests/qtest/bios-tables-test.c for instructions on how to update expected files.
to see ASL diff between mismatched files install IASL, rebuild QEMU from scratch and re-run tests with V=1 environment variable set** ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion
failed: (all_tables_match)
not ok /aarch64/acpi/virt/tpm2-crb - ERROR:../tests/qtest/bios-tables-test.c:538:test_acpi_asl: assertion

Quoted 9 lines

failed: (all_tables_match)
Bail out!
qemu-system-aarch64: tpm-emulator: Could not cleanly shutdown the TPM:
Resource temporarily unavailable
Unexpected error in qio_channel_socket_writev() at ../io/channel-socket.c:622:
/home/elmarco/src/qemu/buildall/tests/qtest/bios-tables-test: Unable to write to socket: Bad file descriptor

Travis testing on s390x I see the following failures for this patchset (search for 'ERROR'):

https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267230363

Summary of Failures:

134/320 qemu:qtest+qtest-aarch64 / qtest-aarch64/tpm-crb-device-test
ERROR 0.70s killed by signal 6 SIGABRT

219/320 qemu:qtest+qtest-x86_64 / qtest-x86_64/tpm-crb-test
ERROR 0.88s killed by signal 6 SIGABRT

Summary of Failures:

271/537 qemu:qtest+qtest-i386 / qtest-i386/tpm-crb-test
ERROR 0.59s killed by signal 6 SIGABRT

My guess is it's an endianess issue on big endian machines due to reading from the ROM device where we lost the .endianess:

Review

Quoted 9 lines
(quoted diff) +9 -0
+const MemoryRegionOps tpm_crb_memory_ops = {
+    .read = tpm_crb_mmio_read,
+    .write = tpm_crb_mmio_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+};

I think we need a 2nd set of registers to support the endianess conversion. It's not exactly nice, though. Basically the saved_regs could be used for this directly, even though I did not do that but introduced n_regs:
https://github.com/stefanberger/qemu-tpm/commit/90f6b21c0dd93dbb13d9e80a628f5b631fd07d91

Quoted 6 lines

This patch allows the tests on s390x to run farther but the execution of the command doesn't seem to work maybe due to command data that were also written in wrong endianess. I don't know. I would have to get access to a big endian / s390 machine to be able to fix it.

Quoted 3 lines

The latest version now passes on Travis s390x:
https://app.travis-ci.com/github/stefanberger/qemu-tpm/builds/267245220

Are the tests failing on S390X due to the added code or are they failing because previously it was untested? I don't think the original code took account of endianness and that should be fixed, but feels like it should be in a separate patch set?

Quoted 6 lines
Quoted 4 lines

They are failing because something like the topmost one or two patches as in this branch here are missing for a big endian host:

https://github.com/stefanberger/qemu-tpm/tree/joelle.v5%2B2nd_registers

Right, but is this issue new due to the patchset? i.e. if just the

Yes, it is due to this patchset. The reason is that CRB switched to a ROMD interface where the fact that the MMIO registers are little endian got lost for existing x86_64 support.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

tests were added without the other patches, would they still fail? If so, I think the fixes should be in another patchset while we disable them in this patchset.

I see, how do you want to best integrate your changes? Do you want me to squash your changes into the patch that introduces the code? Or do you want them to be separate commits?

I think squashing them in would be the right thing to do. You may want to append _LE to the macros in the 2nd patch if you take them.

Stefan

#28 lore raw

PATCH v5 13/14 tests: acpi: updated expected blobs for TPM CRB

In reply to Joelle van Dyne
---
 tests/qtest/bios-tables-test-allowed-diff.h |   4 ----
 tests/data/acpi/q35/DSDT.crb.tpm2           | Bin 0 -> 8355 bytes
 tests/data/acpi/q35/TPM2.crb.tpm2           | Bin 0 -> 76 bytes
 tests/data/acpi/virt/DSDT.crb-device.tpm2   | Bin 0 -> 5276 bytes
 tests/data/acpi/virt/TPM2.crb-device.tpm2   | Bin 0 -> 76 bytes
 5 files changed, 4 deletions(-)
tests/qtest/bios-tables-test-allowed-diff.h +0 -4
index c2d1924c2f..dfb8523c8b 100644
@@ -1,5 +1 @@
 /* List of comma-separated changed AML files to ignore */
-"tests/data/acpi/q35/DSDT.crb.tpm2",
-"tests/data/acpi/q35/TPM2.crb.tpm2",
-"tests/data/acpi/virt/DSDT.crb.tpm2",
-"tests/data/acpi/virt/TPM2.crb.tpm2",
tests/data/acpi/q35/DSDT.crb.tpm2 binary +0 -0
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..fb89ae0ac6d4346e33156e9e4d3718698a0a1a8e 100644
GIT binary patch
tests/data/acpi/q35/TPM2.crb.tpm2 binary +0 -0
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..9d89c5becc0fc4558a0556daf46e152181f2eb51 100644
GIT binary patch
tests/data/acpi/virt/DSDT.crb-device.tpm2 binary +0 -0
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..1b3a8ef4cb3ab093402f4bdb2a349b3d0d015ad5 100644
GIT binary patch
tests/data/acpi/virt/TPM2.crb-device.tpm2 binary +0 -0
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..b2d9268351fcfdc634131e326bfafdd306dc639c 100644
GIT binary patch
Signature · 1 line
2.41.0
#29 lore raw

PATCH v5 14/14 tests: add TPM-CRB sysbus tests for aarch64

In reply to Joelle van Dyne

- Factor out common test code from tpm-crb-test.c -> tpm-tests.c
- Store device addr in tpm_device_base_addr (unify with TIS tests)
- Add new tests for aarch64

---
 tests/qtest/tpm-tests.h                 |   2 +
 tests/qtest/tpm-util.h                  |   4 +-
 tests/qtest/bios-tables-test.c          |   4 +-
 tests/qtest/tpm-crb-device-swtpm-test.c |  72 ++++++++++++++
 tests/qtest/tpm-crb-device-test.c       |  71 ++++++++++++++
 tests/qtest/tpm-crb-swtpm-test.c        |   2 +
 tests/qtest/tpm-crb-test.c              | 121 +-----------------------
 tests/qtest/tpm-tests.c                 | 121 ++++++++++++++++++++++++
 tests/qtest/tpm-tis-device-swtpm-test.c |   2 +-
 tests/qtest/tpm-tis-device-test.c       |   2 +-
 tests/qtest/tpm-tis-i2c-test.c          |   3 +
 tests/qtest/tpm-tis-swtpm-test.c        |   2 +-
 tests/qtest/tpm-tis-test.c              |   2 +-
 tests/qtest/tpm-util.c                  |  16 ++--
 tests/qtest/meson.build                 |   4 +
 15 files changed, 295 insertions(+), 133 deletions(-)
 create mode 100644 tests/qtest/tpm-crb-device-swtpm-test.c
 create mode 100644 tests/qtest/tpm-crb-device-test.c
tests/qtest/tpm-tests.h +2 -0
index 07ba60d26e..c1bfb2f914 100644
@@ -24,4 +24,6 @@ void tpm_test_swtpm_migration_test(const char *src_tpm_path,
                                    const char *ifmodel,
                                    const char *machine_options);
 
+void tpm_test_crb(const void *data);
+
 #endif /* TESTS_TPM_TESTS_H */
tests/qtest/tpm-util.h +2 -2
index 0cb28dd6e5..c99380684e 100644
@@ -15,10 +15,10 @@
 
 #include "io/channel-socket.h"
 
-extern uint64_t tpm_tis_base_addr;
+extern uint64_t tpm_device_base_addr;
 
 #define TIS_REG(LOCTY, REG) \
-    (tpm_tis_base_addr + ((LOCTY) << 12) + REG)
+    (tpm_device_base_addr + ((LOCTY) << 12) + REG)
 
 typedef void (tx_func)(QTestState *s,
                        const unsigned char *req, size_t req_size,
tests/qtest/bios-tables-test.c +2 -2
index bb4ebf00c1..01e0a4aa00 100644
@@ -1445,7 +1445,7 @@ static void test_acpi_piix4_tcg_numamem(void)
     free_test_data(&data);
 }
 
-uint64_t tpm_tis_base_addr;
+uint64_t tpm_device_base_addr;
 
 static test_data tcg_tpm_test_data(const char *machine)
 {
@@ -1481,7 +1481,7 @@ static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
     const char *suffix = tpm_version == TPM_VERSION_2_0 ? "tpm2" : "tpm12";
     char *args, *variant = g_strdup_printf(".%s.%s", tpm_if, suffix);
 
-    tpm_tis_base_addr = base;
+    tpm_device_base_addr = base;
 
     module_call_init(MODULE_INIT_QOM);
 
tests/qtest/tpm-crb-device-swtpm-test.c new file +72 -0

72 lines not on this page · raw message

tests/qtest/tpm-crb-device-test.c new file +71 -0

71 lines not on this page · raw message

tests/qtest/tpm-crb-swtpm-test.c +2 -0
index ffeb1c396b..08862210a4 100644
@@ -19,6 +19,8 @@
 #include "tpm-tests.h"
 #include "hw/acpi/tpm.h"
 
+uint64_t tpm_device_base_addr = TPM_CRB_ADDR_BASE;
+
 typedef struct TestState {
     char *src_tpm_path;
     char *dst_tpm_path;
tests/qtest/tpm-crb-test.c +3 -118

134 lines not on this page · raw message

tests/qtest/tpm-tests.c +121 -0

131 lines not on this page · raw message

tests/qtest/tpm-tis-device-swtpm-test.c +1 -1
index 517a077005..54d1cedc6f 100644
@@ -21,7 +21,7 @@
 #include "tpm-tis-util.h"
 #include "hw/acpi/tpm.h"
 
-uint64_t tpm_tis_base_addr = 0xc000000;
+uint64_t tpm_device_base_addr = 0xc000000;
 #define MACHINE_OPTIONS "-machine virt,gic-version=max -accel tcg"
 
 typedef struct TestState {
tests/qtest/tpm-tis-device-test.c +1 -1
index 3ddefb51ec..4f6609ae98 100644
@@ -27,7 +27,7 @@
  * platform bus and it is the only sysbus device dynamically
  * instantiated, it gets plugged at its base address
  */
-uint64_t tpm_tis_base_addr = 0xc000000;
+uint64_t tpm_device_base_addr = 0xc000000;
 
 int main(int argc, char **argv)
 {
tests/qtest/tpm-tis-i2c-test.c +3 -0
index 3a1af026f2..9495cc1739 100644
@@ -39,6 +39,9 @@
 #define I2C_SLAVE_ADDR   0x2e
 #define I2C_DEV_BUS_NUM  10
 
+/* unused but needed for tpm-util.c to link */
+uint64_t tpm_device_base_addr;
+
 static const uint8_t TPM_CMD[12] =
     "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00";
 
tests/qtest/tpm-tis-swtpm-test.c +1 -1
index 105e42e21d..5bfbbc0a67 100644
@@ -20,7 +20,7 @@
 #include "tpm-tis-util.h"
 #include "hw/acpi/tpm.h"
 
-uint64_t tpm_tis_base_addr = TPM_TIS_ADDR_BASE;
+uint64_t tpm_device_base_addr = TPM_TIS_ADDR_BASE;
 
 typedef struct TestState {
     char *src_tpm_path;
tests/qtest/tpm-tis-test.c +1 -1
index a4a25ba745..7661568aa8 100644
@@ -22,7 +22,7 @@
 #include "tpm-emu.h"
 #include "tpm-tis-util.h"
 
-uint64_t tpm_tis_base_addr = TPM_TIS_ADDR_BASE;
+uint64_t tpm_device_base_addr = TPM_TIS_ADDR_BASE;
 
 int main(int argc, char **argv)
 {
tests/qtest/tpm-util.c +9 -7
index dd02057fc0..1608901a76 100644
@@ -24,18 +24,20 @@ void tpm_util_crb_transfer(QTestState *s,
                            const unsigned char *req, size_t req_size,
                            unsigned char *rsp, size_t rsp_size)
 {
-    uint64_t caddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_LADDR);
-    uint64_t raddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_LADDR);
+    uint64_t caddr = qtest_readq(s, tpm_device_base_addr +
+                                    A_CRB_CTRL_CMD_LADDR);
+    uint64_t raddr = qtest_readq(s, tpm_device_base_addr +
+                                    A_CRB_CTRL_RSP_LADDR);
 
-    qtest_writeb(s, TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL, 1);
+    qtest_writeb(s, tpm_device_base_addr + A_CRB_LOC_CTRL, 1);
 
     qtest_memwrite(s, caddr, req, req_size);
 
     uint32_t sts, start = 1;
     uint64_t end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
-    qtest_writel(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START, start);
+    qtest_writel(s, tpm_device_base_addr + A_CRB_CTRL_START, start);
     while (true) {
-        start = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START);
+        start = qtest_readl(s, tpm_device_base_addr + A_CRB_CTRL_START);
         if ((start & 1) == 0) {
             break;
         }
@@ -43,9 +45,9 @@ void tpm_util_crb_transfer(QTestState *s,
             break;
         }
     };
-    start = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START);
+    start = qtest_readl(s, tpm_device_base_addr + A_CRB_CTRL_START);
     g_assert_cmpint(start & 1, ==, 0);
-    sts = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS);
+    sts = qtest_readl(s, tpm_device_base_addr + A_CRB_CTRL_STS);
     g_assert_cmpint(sts & 1, ==, 0);
 
     qtest_memread(s, raddr, rsp, rsp_size);
tests/qtest/meson.build +4 -0
index 47dabf91d0..386f71df4d 100644
@@ -215,6 +215,8 @@ qtests_aarch64 = \
   (cpu != 'arm' and unpack_edk2_blobs ? ['bios-tables-test'] : []) +                            \
   (config_all.has_key('CONFIG_TCG') and config_all_devices.has_key('CONFIG_TPM_TIS_SYSBUS') ?            \
     ['tpm-tis-device-test', 'tpm-tis-device-swtpm-test'] : []) +                                         \
+  (config_all.has_key('CONFIG_TCG') and config_all_devices.has_key('CONFIG_TPM_CRB_SYSBUS') ?            \
+    ['tpm-crb-device-test', 'tpm-crb-device-swtpm-test'] : []) +                                         \
   (config_all_devices.has_key('CONFIG_XLNX_ZYNQMP_ARM') ? ['xlnx-can-test', 'fuzz-xlnx-dp-test'] : []) + \
   (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test', 'xlnx-versal-trng-test'] : []) + \
   (config_all_devices.has_key('CONFIG_RASPI') ? ['bcm2835-dma-test'] : []) +  \
@@ -313,6 +315,8 @@ qtests = {
   'qos-test': [chardev, io, qos_test_ss.apply(config_targetos, strict: false).sources()],
   'tpm-crb-swtpm-test': [io, tpmemu_files],
   'tpm-crb-test': [io, tpmemu_files],
+  'tpm-crb-device-swtpm-test': [io, tpmemu_files],
+  'tpm-crb-device-test': [io, tpmemu_files],
   'tpm-tis-swtpm-test': [io, tpmemu_files, 'tpm-tis-util.c'],
   'tpm-tis-test': [io, tpmemu_files, 'tpm-tis-util.c'],
   'tpm-tis-i2c-test': [io, tpmemu_files, 'qtest_aspeed.c'],
Signature · 1 line
2.41.0
#30 lore raw
In reply to Joelle van Dyne

Hi

Quoted 64 lines · Joelle van Dyne

The impetus for this patch set is to get TPM 2.0 working on Windows 11 ARM64. Windows' tpm.sys does not seem to work on a TPM TIS device (as verified with VMWare's implementation). However, the current TPM CRB device uses a fixed system bus address that is reserved for RAM in ARM64 Virt machines.

In the process of adding the TPM CRB SysBus device, we also went ahead and cleaned up some of the existing TPM hardware code and fixed some bugs. We used the TPM TIS devices as a template for the TPM CRB devices and refactored out common code. We moved the ACPI DSDT generation to the device in order to handle dynamic base address requirements as well as reduce redundent code in different machine ACPI generation. We also changed the tpm_crb device to use the ISA bus instead of depending on the default system bus as the device only was built for the PC configuration.

Another change is that the TPM CRB registers are now mapped in the same way that the pflash ROM devices are mapped. It is a memory region whose writes are trapped as MMIO accesses. This was needed because Apple Silicon does not decode LDP (AARCH64 load pair of registers) caused page faults. @agraf suggested that we do this to avoid having to do AARCH64 decoding in the HVF backend's fault handler.

Unfortunately, it seems like the LDP fault still happens on HVF but the issue seems to be in the HVF backend which needs to be fixed in a separate patch.

One last thing that's needed to get Windows 11 to recognize the TPM 2.0 device is for the OVMF firmware to setup the TPM device. Currently, OVMF for ARM64 Virt only recognizes the TPM TIS device through a FDT entry. A workaround is to falsely identify the TPM CRB device as a TPM TIS device in the FDT node but this causes issues for Linux. A proper fix would involve adding an ACPI device driver in OVMF.

This has been tested on ARM64 with tpm-crb-device and on x86_64 with tpm-crb. Additional testing should be performed on other architectures (RISCV and Loongarch for example) as well as migration cases.

v5:
- Fixed a typo in "tpm_crb: use a single read-as-mem/write-as-mmio mapping"
- Fixed ACPI tables not being created for pc CRB device

v4:
- Fixed broken test blobs

v3:
- Support backwards and forwards migration of existing tpm-crb device
- Dropped patch which moved tpm-crb to ISA bus due to migration concerns
- Unified tpm_sysbus_plug handler for ARM and Loongarch
- Added ACPI table tests for tpm-crb-device
- Refactored TPM CRB tests to run on tpm-crb-device for ARM Virt

v2:
- Fixed an issue where VMstate restore from an older version failed due to name collision of the memory block.
- In the ACPI table generation for CRB devices, the check for TPM 2.0 backend is moved to the device realize as CRB does not support TPM 1.0. It will error in that case.
- Dropped the patch to fix crash when PPI is enabled on TIS SysBus device since a separate patch submitted by Stefan Berger disables such an option.
- Fixed an issue where we default tpmEstablished=0 when it should be 1.
- In TPM CRB SysBus's ACPI entry, we accidently changed _UID from 0 to 1. This shouldn't be an issue but we changed it back just in case.
- Added a patch to migrate saved VMstate from an older version with the regs saved separately instead of as a RAM block.

Quoted 15 lines

Joelle van Dyne (14):
tpm_crb: refactor common code
tpm_crb: CTRL_RSP_ADDR is 64-bits wide
tpm_ppi: refactor memory space initialization
tpm_crb: use a single read-as-mem/write-as-mmio mapping
tpm_crb: move ACPI table building to device interface
tpm-sysbus: add plug handler for TPM on SysBus hw/arm/virt: connect TPM to platform bus hw/loongarch/virt: connect TPM to platform bus
tpm_tis_sysbus: move DSDT AML generation to device
tests: acpi: prepare for TPM CRB tests
tpm_crb_sysbus: introduce TPM CRB SysBus device
tests: acpi: implement TPM CRB tests for ARM virt
tests: acpi: updated expected blobs for TPM CRB
tests: add TPM-CRB sysbus tests for aarch64

The series looks good to me.
Have you checked there are no regressions with Windows HLK?

thanks

Quoted 62 lines
 docs/specs/tpm.rst                        |   2 +
 hw/tpm/tpm_crb.h                          |  79 ++++++
 hw/tpm/tpm_ppi.h                          |  10 +-
 include/hw/acpi/tpm.h                     |   3 +-
 include/sysemu/tpm.h                      |   7 +
 tests/qtest/tpm-tests.h                   |   2 +
 tests/qtest/tpm-util.h                    |   4 +-
 hw/acpi/aml-build.c                       |   7 +-
 hw/arm/virt-acpi-build.c                  |  38 +--
 hw/arm/virt.c                             |   8 +
 hw/core/sysbus-fdt.c                      |   1 +
 hw/i386/acpi-build.c                      |  16 +-
 hw/loongarch/acpi-build.c                 |  38 +--
 hw/loongarch/virt.c                       |   8 +
 hw/riscv/virt.c                           |   1 +
 hw/tpm/tpm-sysbus.c                       |  47 ++++
 hw/tpm/tpm_crb.c                          | 302 ++++++----------------
 hw/tpm/tpm_crb_common.c                   | 262 +++++++++++++++++++
 hw/tpm/tpm_crb_sysbus.c                   | 162 ++++++++++++
 hw/tpm/tpm_ppi.c                          |   5 +-
 hw/tpm/tpm_tis_isa.c                      |   5 +-
 hw/tpm/tpm_tis_sysbus.c                   |  37 +++
 tests/qtest/bios-tables-test.c            |  47 +++-
 tests/qtest/tpm-crb-device-swtpm-test.c   |  72 ++++++
 tests/qtest/tpm-crb-device-test.c         |  71 +++++
 tests/qtest/tpm-crb-swtpm-test.c          |   2 +
 tests/qtest/tpm-crb-test.c                | 121 +--------
 tests/qtest/tpm-tests.c                   | 121 +++++++++
 tests/qtest/tpm-tis-device-swtpm-test.c   |   2 +-
 tests/qtest/tpm-tis-device-test.c         |   2 +-
 tests/qtest/tpm-tis-i2c-test.c            |   3 +
 tests/qtest/tpm-tis-swtpm-test.c          |   2 +-
 tests/qtest/tpm-tis-test.c                |   2 +-
 tests/qtest/tpm-util.c                    |  16 +-
 hw/arm/Kconfig                            |   1 +
 hw/loongarch/Kconfig                      |   2 +
 hw/riscv/Kconfig                          |   1 +
 hw/tpm/Kconfig                            |   5 +
 hw/tpm/meson.build                        |   5 +
 hw/tpm/trace-events                       |   2 +-
 tests/data/acpi/q35/DSDT.crb.tpm2         | Bin 0 -> 8355 bytes
 tests/data/acpi/q35/TPM2.crb.tpm2         | Bin 0 -> 76 bytes
 tests/data/acpi/virt/DSDT.crb-device.tpm2 | Bin 0 -> 5276 bytes
 tests/data/acpi/virt/TPM2.crb-device.tpm2 | Bin 0 -> 76 bytes
 tests/qtest/meson.build                   |   4 +
 45 files changed, 1057 insertions(+), 468 deletions(-)
 create mode 100644 hw/tpm/tpm_crb.h
 create mode 100644 hw/tpm/tpm-sysbus.c
 create mode 100644 hw/tpm/tpm_crb_common.c
 create mode 100644 hw/tpm/tpm_crb_sysbus.c
 create mode 100644 tests/qtest/tpm-crb-device-swtpm-test.c
 create mode 100644 tests/qtest/tpm-crb-device-test.c
 create mode 100644 tests/data/acpi/q35/DSDT.crb.tpm2
 create mode 100644 tests/data/acpi/q35/TPM2.crb.tpm2
 create mode 100644 tests/data/acpi/virt/DSDT.crb-device.tpm2
 create mode 100644 tests/data/acpi/virt/TPM2.crb-device.tpm2

--
2.41.0

Signature · 1 line
Marc-André Lureau
#31 lore raw
Quoted 84 lines · Marc-André Lureau

Hi

Quoted 64 lines · Joelle van Dyne

The impetus for this patch set is to get TPM 2.0 working on Windows 11 ARM64. Windows' tpm.sys does not seem to work on a TPM TIS device (as verified with VMWare's implementation). However, the current TPM CRB device uses a fixed system bus address that is reserved for RAM in ARM64 Virt machines.

In the process of adding the TPM CRB SysBus device, we also went ahead and cleaned up some of the existing TPM hardware code and fixed some bugs. We used the TPM TIS devices as a template for the TPM CRB devices and refactored out common code. We moved the ACPI DSDT generation to the device in order to handle dynamic base address requirements as well as reduce redundent code in different machine ACPI generation. We also changed the tpm_crb device to use the ISA bus instead of depending on the default system bus as the device only was built for the PC configuration.

Another change is that the TPM CRB registers are now mapped in the same way that the pflash ROM devices are mapped. It is a memory region whose writes are trapped as MMIO accesses. This was needed because Apple Silicon does not decode LDP (AARCH64 load pair of registers) caused page faults. @agraf suggested that we do this to avoid having to do AARCH64 decoding in the HVF backend's fault handler.

Unfortunately, it seems like the LDP fault still happens on HVF but the issue seems to be in the HVF backend which needs to be fixed in a separate patch.

One last thing that's needed to get Windows 11 to recognize the TPM 2.0 device is for the OVMF firmware to setup the TPM device. Currently, OVMF for ARM64 Virt only recognizes the TPM TIS device through a FDT entry. A workaround is to falsely identify the TPM CRB device as a TPM TIS device in the FDT node but this causes issues for Linux. A proper fix would involve adding an ACPI device driver in OVMF.

This has been tested on ARM64 with tpm-crb-device and on x86_64 with tpm-crb. Additional testing should be performed on other architectures (RISCV and Loongarch for example) as well as migration cases.

v5:
- Fixed a typo in "tpm_crb: use a single read-as-mem/write-as-mmio mapping"
- Fixed ACPI tables not being created for pc CRB device

v4:
- Fixed broken test blobs

v3:
- Support backwards and forwards migration of existing tpm-crb device
- Dropped patch which moved tpm-crb to ISA bus due to migration concerns
- Unified tpm_sysbus_plug handler for ARM and Loongarch
- Added ACPI table tests for tpm-crb-device
- Refactored TPM CRB tests to run on tpm-crb-device for ARM Virt

v2:
- Fixed an issue where VMstate restore from an older version failed due to name collision of the memory block.
- In the ACPI table generation for CRB devices, the check for TPM 2.0 backend is moved to the device realize as CRB does not support TPM 1.0. It will error in that case.
- Dropped the patch to fix crash when PPI is enabled on TIS SysBus device since a separate patch submitted by Stefan Berger disables such an option.
- Fixed an issue where we default tpmEstablished=0 when it should be 1.
- In TPM CRB SysBus's ACPI entry, we accidently changed _UID from 0 to 1. This shouldn't be an issue but we changed it back just in case.
- Added a patch to migrate saved VMstate from an older version with the regs saved separately instead of as a RAM block.

Quoted 15 lines

Joelle van Dyne (14):
tpm_crb: refactor common code
tpm_crb: CTRL_RSP_ADDR is 64-bits wide
tpm_ppi: refactor memory space initialization
tpm_crb: use a single read-as-mem/write-as-mmio mapping
tpm_crb: move ACPI table building to device interface
tpm-sysbus: add plug handler for TPM on SysBus hw/arm/virt: connect TPM to platform bus hw/loongarch/virt: connect TPM to platform bus
tpm_tis_sysbus: move DSDT AML generation to device
tests: acpi: prepare for TPM CRB tests
tpm_crb_sysbus: introduce TPM CRB SysBus device
tests: acpi: implement TPM CRB tests for ARM virt
tests: acpi: updated expected blobs for TPM CRB
tests: add TPM-CRB sysbus tests for aarch64

Quoted 2 lines

The series looks good to me.
Have you checked there are no regressions with Windows HLK?

I don't have any experience with Windows HLK. Is there any guide on running it with QEMU? Preferably with a prebuilt image? Or maybe someone in the mailing list can run it?

Quoted 69 lines

thanks

Quoted 58 lines
 docs/specs/tpm.rst                        |   2 +
 hw/tpm/tpm_crb.h                          |  79 ++++++
 hw/tpm/tpm_ppi.h                          |  10 +-
 include/hw/acpi/tpm.h                     |   3 +-
 include/sysemu/tpm.h                      |   7 +
 tests/qtest/tpm-tests.h                   |   2 +
 tests/qtest/tpm-util.h                    |   4 +-
 hw/acpi/aml-build.c                       |   7 +-
 hw/arm/virt-acpi-build.c                  |  38 +--
 hw/arm/virt.c                             |   8 +
 hw/core/sysbus-fdt.c                      |   1 +
 hw/i386/acpi-build.c                      |  16 +-
 hw/loongarch/acpi-build.c                 |  38 +--
 hw/loongarch/virt.c                       |   8 +
 hw/riscv/virt.c                           |   1 +
 hw/tpm/tpm-sysbus.c                       |  47 ++++
 hw/tpm/tpm_crb.c                          | 302 ++++++----------------
 hw/tpm/tpm_crb_common.c                   | 262 +++++++++++++++++++
 hw/tpm/tpm_crb_sysbus.c                   | 162 ++++++++++++
 hw/tpm/tpm_ppi.c                          |   5 +-
 hw/tpm/tpm_tis_isa.c                      |   5 +-
 hw/tpm/tpm_tis_sysbus.c                   |  37 +++
 tests/qtest/bios-tables-test.c            |  47 +++-
 tests/qtest/tpm-crb-device-swtpm-test.c   |  72 ++++++
 tests/qtest/tpm-crb-device-test.c         |  71 +++++
 tests/qtest/tpm-crb-swtpm-test.c          |   2 +
 tests/qtest/tpm-crb-test.c                | 121 +--------
 tests/qtest/tpm-tests.c                   | 121 +++++++++
 tests/qtest/tpm-tis-device-swtpm-test.c   |   2 +-
 tests/qtest/tpm-tis-device-test.c         |   2 +-
 tests/qtest/tpm-tis-i2c-test.c            |   3 +
 tests/qtest/tpm-tis-swtpm-test.c          |   2 +-
 tests/qtest/tpm-tis-test.c                |   2 +-
 tests/qtest/tpm-util.c                    |  16 +-
 hw/arm/Kconfig                            |   1 +
 hw/loongarch/Kconfig                      |   2 +
 hw/riscv/Kconfig                          |   1 +
 hw/tpm/Kconfig                            |   5 +
 hw/tpm/meson.build                        |   5 +
 hw/tpm/trace-events                       |   2 +-
 tests/data/acpi/q35/DSDT.crb.tpm2         | Bin 0 -> 8355 bytes
 tests/data/acpi/q35/TPM2.crb.tpm2         | Bin 0 -> 76 bytes
 tests/data/acpi/virt/DSDT.crb-device.tpm2 | Bin 0 -> 5276 bytes
 tests/data/acpi/virt/TPM2.crb-device.tpm2 | Bin 0 -> 76 bytes
 tests/qtest/meson.build                   |   4 +
 45 files changed, 1057 insertions(+), 468 deletions(-)
 create mode 100644 hw/tpm/tpm_crb.h
 create mode 100644 hw/tpm/tpm-sysbus.c
 create mode 100644 hw/tpm/tpm_crb_common.c
 create mode 100644 hw/tpm/tpm_crb_sysbus.c
 create mode 100644 tests/qtest/tpm-crb-device-swtpm-test.c
 create mode 100644 tests/qtest/tpm-crb-device-test.c
 create mode 100644 tests/data/acpi/q35/DSDT.crb.tpm2
 create mode 100644 tests/data/acpi/q35/TPM2.crb.tpm2
 create mode 100644 tests/data/acpi/virt/DSDT.crb-device.tpm2
 create mode 100644 tests/data/acpi/virt/TPM2.crb-device.tpm2
Quoted 4 lines

--
2.41.0

--
Marc-André Lureau

#32 lore raw

Hi

Quoted 87 lines · Joelle van Dyne
Quoted 84 lines · Marc-André Lureau

Hi

Quoted 79 lines · Joelle van Dyne

The impetus for this patch set is to get TPM 2.0 working on Windows 11 ARM64. Windows' tpm.sys does not seem to work on a TPM TIS device (as verified with VMWare's implementation). However, the current TPM CRB device uses a fixed system bus address that is reserved for RAM in ARM64 Virt machines.

In the process of adding the TPM CRB SysBus device, we also went ahead and cleaned up some of the existing TPM hardware code and fixed some bugs. We used the TPM TIS devices as a template for the TPM CRB devices and refactored out common code. We moved the ACPI DSDT generation to the device in order to handle dynamic base address requirements as well as reduce redundent code in different machine ACPI generation. We also changed the tpm_crb device to use the ISA bus instead of depending on the default system bus as the device only was built for the PC configuration.

Another change is that the TPM CRB registers are now mapped in the same way that the pflash ROM devices are mapped. It is a memory region whose writes are trapped as MMIO accesses. This was needed because Apple Silicon does not decode LDP (AARCH64 load pair of registers) caused page faults. @agraf suggested that we do this to avoid having to do AARCH64 decoding in the HVF backend's fault handler.

Unfortunately, it seems like the LDP fault still happens on HVF but the issue seems to be in the HVF backend which needs to be fixed in a separate patch.

One last thing that's needed to get Windows 11 to recognize the TPM 2.0 device is for the OVMF firmware to setup the TPM device. Currently, OVMF for ARM64 Virt only recognizes the TPM TIS device through a FDT entry. A workaround is to falsely identify the TPM CRB device as a TPM TIS device in the FDT node but this causes issues for Linux. A proper fix would involve adding an ACPI device driver in OVMF.

This has been tested on ARM64 with tpm-crb-device and on x86_64 with tpm-crb. Additional testing should be performed on other architectures (RISCV and Loongarch for example) as well as migration cases.

v5:
- Fixed a typo in "tpm_crb: use a single read-as-mem/write-as-mmio mapping"
- Fixed ACPI tables not being created for pc CRB device

v4:
- Fixed broken test blobs

v3:
- Support backwards and forwards migration of existing tpm-crb device
- Dropped patch which moved tpm-crb to ISA bus due to migration concerns
- Unified tpm_sysbus_plug handler for ARM and Loongarch
- Added ACPI table tests for tpm-crb-device
- Refactored TPM CRB tests to run on tpm-crb-device for ARM Virt

v2:
- Fixed an issue where VMstate restore from an older version failed due to name collision of the memory block.
- In the ACPI table generation for CRB devices, the check for TPM 2.0 backend is moved to the device realize as CRB does not support TPM 1.0. It will error in that case.
- Dropped the patch to fix crash when PPI is enabled on TIS SysBus device since a separate patch submitted by Stefan Berger disables such an option.
- Fixed an issue where we default tpmEstablished=0 when it should be 1.
- In TPM CRB SysBus's ACPI entry, we accidently changed _UID from 0 to 1. This shouldn't be an issue but we changed it back just in case.
- Added a patch to migrate saved VMstate from an older version with the regs saved separately instead of as a RAM block.

Joelle van Dyne (14):
tpm_crb: refactor common code
tpm_crb: CTRL_RSP_ADDR is 64-bits wide
tpm_ppi: refactor memory space initialization
tpm_crb: use a single read-as-mem/write-as-mmio mapping
tpm_crb: move ACPI table building to device interface
tpm-sysbus: add plug handler for TPM on SysBus hw/arm/virt: connect TPM to platform bus hw/loongarch/virt: connect TPM to platform bus
tpm_tis_sysbus: move DSDT AML generation to device
tests: acpi: prepare for TPM CRB tests
tpm_crb_sysbus: introduce TPM CRB SysBus device
tests: acpi: implement TPM CRB tests for ARM virt
tests: acpi: updated expected blobs for TPM CRB
tests: add TPM-CRB sysbus tests for aarch64

Quoted 6 lines
Quoted 2 lines

The series looks good to me.
Have you checked there are no regressions with Windows HLK?

I don't have any experience with Windows HLK. Is there any guide on running it with QEMU? Preferably with a prebuilt image? Or maybe someone in the mailing list can run it?

I haven't done it for a while. (it wasn't that hard, just a bit
tedious: install a Controller on a Windows Server VM, and set up a target VM)

There is also AutoHCK, but I never managed to use it.
(https://github.com/HCK-CI/AutoHCK)

For the current results, they are summarized here:
https://github.com/stefanberger/libtpms/wiki/Testing-of-libtpms-Functionality

Stefan, do you have a working setup? I'll try to make one if it can help.

Quoted 61 lines
Quoted 61 lines

thanks

Quoted 58 lines
 docs/specs/tpm.rst                        |   2 +
 hw/tpm/tpm_crb.h                          |  79 ++++++
 hw/tpm/tpm_ppi.h                          |  10 +-
 include/hw/acpi/tpm.h                     |   3 +-
 include/sysemu/tpm.h                      |   7 +
 tests/qtest/tpm-tests.h                   |   2 +
 tests/qtest/tpm-util.h                    |   4 +-
 hw/acpi/aml-build.c                       |   7 +-
 hw/arm/virt-acpi-build.c                  |  38 +--
 hw/arm/virt.c                             |   8 +
 hw/core/sysbus-fdt.c                      |   1 +
 hw/i386/acpi-build.c                      |  16 +-
 hw/loongarch/acpi-build.c                 |  38 +--
 hw/loongarch/virt.c                       |   8 +
 hw/riscv/virt.c                           |   1 +
 hw/tpm/tpm-sysbus.c                       |  47 ++++
 hw/tpm/tpm_crb.c                          | 302 ++++++----------------
 hw/tpm/tpm_crb_common.c                   | 262 +++++++++++++++++++
 hw/tpm/tpm_crb_sysbus.c                   | 162 ++++++++++++
 hw/tpm/tpm_ppi.c                          |   5 +-
 hw/tpm/tpm_tis_isa.c                      |   5 +-
 hw/tpm/tpm_tis_sysbus.c                   |  37 +++
 tests/qtest/bios-tables-test.c            |  47 +++-
 tests/qtest/tpm-crb-device-swtpm-test.c   |  72 ++++++
 tests/qtest/tpm-crb-device-test.c         |  71 +++++
 tests/qtest/tpm-crb-swtpm-test.c          |   2 +
 tests/qtest/tpm-crb-test.c                | 121 +--------
 tests/qtest/tpm-tests.c                   | 121 +++++++++
 tests/qtest/tpm-tis-device-swtpm-test.c   |   2 +-
 tests/qtest/tpm-tis-device-test.c         |   2 +-
 tests/qtest/tpm-tis-i2c-test.c            |   3 +
 tests/qtest/tpm-tis-swtpm-test.c          |   2 +-
 tests/qtest/tpm-tis-test.c                |   2 +-
 tests/qtest/tpm-util.c                    |  16 +-
 hw/arm/Kconfig                            |   1 +
 hw/loongarch/Kconfig                      |   2 +
 hw/riscv/Kconfig                          |   1 +
 hw/tpm/Kconfig                            |   5 +
 hw/tpm/meson.build                        |   5 +
 hw/tpm/trace-events                       |   2 +-
 tests/data/acpi/q35/DSDT.crb.tpm2         | Bin 0 -> 8355 bytes
 tests/data/acpi/q35/TPM2.crb.tpm2         | Bin 0 -> 76 bytes
 tests/data/acpi/virt/DSDT.crb-device.tpm2 | Bin 0 -> 5276 bytes
 tests/data/acpi/virt/TPM2.crb-device.tpm2 | Bin 0 -> 76 bytes
 tests/qtest/meson.build                   |   4 +
 45 files changed, 1057 insertions(+), 468 deletions(-)
 create mode 100644 hw/tpm/tpm_crb.h
 create mode 100644 hw/tpm/tpm-sysbus.c
 create mode 100644 hw/tpm/tpm_crb_common.c
 create mode 100644 hw/tpm/tpm_crb_sysbus.c
 create mode 100644 tests/qtest/tpm-crb-device-swtpm-test.c
 create mode 100644 tests/qtest/tpm-crb-device-test.c
 create mode 100644 tests/data/acpi/q35/DSDT.crb.tpm2
 create mode 100644 tests/data/acpi/q35/TPM2.crb.tpm2
 create mode 100644 tests/data/acpi/virt/DSDT.crb-device.tpm2
 create mode 100644 tests/data/acpi/virt/TPM2.crb-device.tpm2
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

--
2.41.0

--
Marc-André Lureau

--
Marc-André Lureau

#33 lore raw
Quoted 101 lines · Marc-André Lureau

Hi

Quoted 87 lines · Joelle van Dyne
Quoted 84 lines · Marc-André Lureau

Hi

Quoted 79 lines · Joelle van Dyne

The impetus for this patch set is to get TPM 2.0 working on Windows 11 ARM64. Windows' tpm.sys does not seem to work on a TPM TIS device (as verified with VMWare's implementation). However, the current TPM CRB device uses a fixed system bus address that is reserved for RAM in ARM64 Virt machines.

In the process of adding the TPM CRB SysBus device, we also went ahead and cleaned up some of the existing TPM hardware code and fixed some bugs. We used the TPM TIS devices as a template for the TPM CRB devices and refactored out common code. We moved the ACPI DSDT generation to the device in order to handle dynamic base address requirements as well as reduce redundent code in different machine ACPI generation. We also changed the tpm_crb device to use the ISA bus instead of depending on the default system bus as the device only was built for the PC configuration.

Another change is that the TPM CRB registers are now mapped in the same way that the pflash ROM devices are mapped. It is a memory region whose writes are trapped as MMIO accesses. This was needed because Apple Silicon does not decode LDP (AARCH64 load pair of registers) caused page faults. @agraf suggested that we do this to avoid having to do AARCH64 decoding in the HVF backend's fault handler.

Unfortunately, it seems like the LDP fault still happens on HVF but the issue seems to be in the HVF backend which needs to be fixed in a separate patch.

One last thing that's needed to get Windows 11 to recognize the TPM 2.0 device is for the OVMF firmware to setup the TPM device. Currently, OVMF for ARM64 Virt only recognizes the TPM TIS device through a FDT entry. A workaround is to falsely identify the TPM CRB device as a TPM TIS device in the FDT node but this causes issues for Linux. A proper fix would involve adding an ACPI device driver in OVMF.

This has been tested on ARM64 with tpm-crb-device and on x86_64 with tpm-crb. Additional testing should be performed on other architectures (RISCV and Loongarch for example) as well as migration cases.

v5:
- Fixed a typo in "tpm_crb: use a single read-as-mem/write-as-mmio mapping"
- Fixed ACPI tables not being created for pc CRB device

v4:
- Fixed broken test blobs

v3:
- Support backwards and forwards migration of existing tpm-crb device
- Dropped patch which moved tpm-crb to ISA bus due to migration concerns
- Unified tpm_sysbus_plug handler for ARM and Loongarch
- Added ACPI table tests for tpm-crb-device
- Refactored TPM CRB tests to run on tpm-crb-device for ARM Virt

v2:
- Fixed an issue where VMstate restore from an older version failed due to name collision of the memory block.
- In the ACPI table generation for CRB devices, the check for TPM 2.0 backend is moved to the device realize as CRB does not support TPM 1.0. It will error in that case.
- Dropped the patch to fix crash when PPI is enabled on TIS SysBus device since a separate patch submitted by Stefan Berger disables such an option.
- Fixed an issue where we default tpmEstablished=0 when it should be 1.
- In TPM CRB SysBus's ACPI entry, we accidently changed _UID from 0 to 1. This shouldn't be an issue but we changed it back just in case.
- Added a patch to migrate saved VMstate from an older version with the regs saved separately instead of as a RAM block.

Joelle van Dyne (14):
tpm_crb: refactor common code
tpm_crb: CTRL_RSP_ADDR is 64-bits wide
tpm_ppi: refactor memory space initialization
tpm_crb: use a single read-as-mem/write-as-mmio mapping
tpm_crb: move ACPI table building to device interface
tpm-sysbus: add plug handler for TPM on SysBus hw/arm/virt: connect TPM to platform bus hw/loongarch/virt: connect TPM to platform bus
tpm_tis_sysbus: move DSDT AML generation to device
tests: acpi: prepare for TPM CRB tests
tpm_crb_sysbus: introduce TPM CRB SysBus device
tests: acpi: implement TPM CRB tests for ARM virt
tests: acpi: updated expected blobs for TPM CRB
tests: add TPM-CRB sysbus tests for aarch64

Quoted 6 lines
Quoted 2 lines

The series looks good to me.
Have you checked there are no regressions with Windows HLK?

I don't have any experience with Windows HLK. Is there any guide on running it with QEMU? Preferably with a prebuilt image? Or maybe someone in the mailing list can run it?

I haven't done it for a while. (it wasn't that hard, just a bit
tedious: install a Controller on a Windows Server VM, and set up a target VM)

Quoted 7 lines

There is also AutoHCK, but I never managed to use it.
(https://github.com/HCK-CI/AutoHCK)

For the current results, they are summarized here:
https://github.com/stefanberger/libtpms/wiki/Testing-of-libtpms-Functionality

Stefan, do you have a working setup? I'll try to make one if it can help.

Once we have the fixes for big endian support I can run some tests. It should be rather quick to verify that the CRB interface still works, even with Linux. What I would be more concerned are suspend/upgrade or downgrade/resume tests.

Stefan

Quoted 76 lines
Quoted 63 lines
Quoted 63 lines

thanks

Quoted 58 lines
  docs/specs/tpm.rst                        |   2 +
  hw/tpm/tpm_crb.h                          |  79 ++++++
  hw/tpm/tpm_ppi.h                          |  10 +-
  include/hw/acpi/tpm.h                     |   3 +-
  include/sysemu/tpm.h                      |   7 +
  tests/qtest/tpm-tests.h                   |   2 +
  tests/qtest/tpm-util.h                    |   4 +-
  hw/acpi/aml-build.c                       |   7 +-
  hw/arm/virt-acpi-build.c                  |  38 +--
  hw/arm/virt.c                             |   8 +
  hw/core/sysbus-fdt.c                      |   1 +
  hw/i386/acpi-build.c                      |  16 +-
  hw/loongarch/acpi-build.c                 |  38 +--
  hw/loongarch/virt.c                       |   8 +
  hw/riscv/virt.c                           |   1 +
  hw/tpm/tpm-sysbus.c                       |  47 ++++
  hw/tpm/tpm_crb.c                          | 302 ++++++----------------
  hw/tpm/tpm_crb_common.c                   | 262 +++++++++++++++++++
  hw/tpm/tpm_crb_sysbus.c                   | 162 ++++++++++++
  hw/tpm/tpm_ppi.c                          |   5 +-
  hw/tpm/tpm_tis_isa.c                      |   5 +-
  hw/tpm/tpm_tis_sysbus.c                   |  37 +++
  tests/qtest/bios-tables-test.c            |  47 +++-
  tests/qtest/tpm-crb-device-swtpm-test.c   |  72 ++++++
  tests/qtest/tpm-crb-device-test.c         |  71 +++++
  tests/qtest/tpm-crb-swtpm-test.c          |   2 +
  tests/qtest/tpm-crb-test.c                | 121 +--------
  tests/qtest/tpm-tests.c                   | 121 +++++++++
  tests/qtest/tpm-tis-device-swtpm-test.c   |   2 +-
  tests/qtest/tpm-tis-device-test.c         |   2 +-
  tests/qtest/tpm-tis-i2c-test.c            |   3 +
  tests/qtest/tpm-tis-swtpm-test.c          |   2 +-
  tests/qtest/tpm-tis-test.c                |   2 +-
  tests/qtest/tpm-util.c                    |  16 +-
  hw/arm/Kconfig                            |   1 +
  hw/loongarch/Kconfig                      |   2 +
  hw/riscv/Kconfig                          |   1 +
  hw/tpm/Kconfig                            |   5 +
  hw/tpm/meson.build                        |   5 +
  hw/tpm/trace-events                       |   2 +-
  tests/data/acpi/q35/DSDT.crb.tpm2         | Bin 0 -> 8355 bytes
  tests/data/acpi/q35/TPM2.crb.tpm2         | Bin 0 -> 76 bytes
  tests/data/acpi/virt/DSDT.crb-device.tpm2 | Bin 0 -> 5276 bytes
  tests/data/acpi/virt/TPM2.crb-device.tpm2 | Bin 0 -> 76 bytes
  tests/qtest/meson.build                   |   4 +
  45 files changed, 1057 insertions(+), 468 deletions(-)

create mode 100644 hw/tpm/tpm_crb.h
create mode 100644 hw/tpm/tpm-sysbus.c
create mode 100644 hw/tpm/tpm_crb_common.c
create mode 100644 hw/tpm/tpm_crb_sysbus.c
create mode 100644 tests/qtest/tpm-crb-device-swtpm-test.c create mode 100644 tests/qtest/tpm-crb-device-test.c create mode 100644 tests/data/acpi/q35/DSDT.crb.tpm2 create mode 100644 tests/data/acpi/q35/TPM2.crb.tpm2 create mode 100644 tests/data/acpi/virt/DSDT.crb-device.tpm2 create mode 100644 tests/data/acpi/virt/TPM2.crb-device.tpm2

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

--
2.41.0

--
Marc-André Lureau

--
Marc-André Lureau

#34 lore raw
In reply to Joelle van Dyne
Quoted 14 lines · Joelle van Dyne

The impetus for this patch set is to get TPM 2.0 working on Windows 11 ARM64. Windows' tpm.sys does not seem to work on a TPM TIS device (as verified with VMWare's implementation). However, the current TPM CRB device uses a fixed system bus address that is reserved for RAM in ARM64 Virt machines.

In the process of adding the TPM CRB SysBus device, we also went ahead and cleaned up some of the existing TPM hardware code and fixed some bugs. We used the TPM TIS devices as a template for the TPM CRB devices and refactored out common code. We moved the ACPI DSDT generation to the device in order to handle dynamic base address requirements as well as reduce redundent code in different machine ACPI generation. We also changed the tpm_crb device to use the ISA bus instead of depending on the default system bus as the device only was built for the PC configuration.

Quoted 6 lines

Another change is that the TPM CRB registers are now mapped in the same way that the pflash ROM devices are mapped. It is a memory region whose writes are trapped as MMIO accesses. This was needed because Apple Silicon does not decode LDP (AARCH64 load pair of registers) caused page faults. @agraf suggested that we do this to avoid having to do AARCH64 decoding in the HVF backend's fault handler.

I had a conversation about this on IRC a week or so back (though I forget who with, sorry) that made me realise there's a problem with this approach, and I wanted to write that up for the mailing list.

The problem with turning this into a memory-backed device rather than an MMIO backed device is that it breaks KVM on Arm CPUs which don't have FEAT_S2FWB (i.e. anything older than Armv8.4). This is because without FEAT_S2FWB the guest and host will disagree about the memory attributes of the region:
* the host knows this is RAM backed and it's normal-cacheable (certainly as far as the mapping that QEMU itself has)
* the guest thinks it's real hardware device registers and maps it as Device
The resulting mismatch in cacheability attributes can cause unexpected behaviour where the guest and QEMU views of the memory contents don't necessarily match up. (This is the same underlying issue that means that you can't use QEMU devices that emulate VGA framebuffers on AArch64 KVM without FEAT_S2FWB.)

With FEAT_S2FWB the problem goes away because the hypervisor can override the guest's specified memory attributes to get rid of the attribute mismatch.

So given that this would cause a regression for KVM, my preference is to stick with the current "the device is backed by MMIO read and write functions in the normal way". If a particular guest is trying to access it with LDP/STP that is best fixed in the guest.

Potentially we could emulate (interpret) some subset of complex load/store insns in QEMU at the point where we get the "took a data abort but no syndrome information". This ought to be doable in a way that's shareable between hvf and KVM, and we can write a decodetree file for the insns we want to interpret. (I would not try to share with TCG decodetree, though the patterns can probably be copied across.)

thanks
-- PMM