Skip to contentSkip to conversation

libxl TPM emulator support

PATCH In review Jason Andryuk 3 participants 10 messages Nov 6 – Nov 18

lore
Share & tools
lore
#0 lore raw

cover libxl TPM emulator support

This patch series adds support to libxl for QEMU to emulate a TPM with swtpm as a backend.

It reworks some ARM TPM support from Stefano to use the auto-started swtpm.

Since ARM uses the TIS interface, I expanded x86 to use either TIS or CRB for TPM 2.0.

On Ubuntu 22.04 (and probably others), swtpm has an AppArmor policy that restricts access to specific paths. Those don't include /var/run/xen. I disabled AppArmor with sudo aa-complain swtpm.

Jason Andryuk (3):
libacpi: Build TPM2 tables for the TIS interface
hvmloader: Support the TIS interface for TPM 2.0 tools/libxl: add emulated TPM support backed by swtpm

Stefano Stabellini (1):
libxl: Add emulated TPM support on ARM

 docs/man/xl.cfg.5.pod.in             |  42 +++++++
 docs/misc/xenstore-paths.pandoc      |  12 +-
 tools/firmware/hvmloader/util.c      |  12 +-
 tools/golang/xenlight/gengotypes.py  |   2 +-
 tools/golang/xenlight/helpers.gen.go |  48 ++++++++
 tools/golang/xenlight/types.gen.go   |  17 +++
 tools/include/libxl.h                |  11 ++
 tools/libacpi/Makefile               |   2 +-
 tools/libacpi/acpi2_0.h              |   1 +
 tools/libacpi/build.c                |  47 +++++--
 tools/libacpi/libacpi.h              |   4 +
 tools/libacpi/ssdt_tpm2_tis.asl      |  25 ++++
 tools/libs/light/libxl_arch.h        |   6 +
 tools/libs/light/libxl_arm.c         |  47 +++++++
 tools/libs/light/libxl_create.c      |  46 ++++++-
 tools/libs/light/libxl_dm.c          | 175 ++++++++++++++++++++++++++-
 tools/libs/light/libxl_domain.c      |   3 +
 tools/libs/light/libxl_internal.h    |   4 +
 tools/libs/light/libxl_types.idl     |  17 +++
 tools/libs/light/libxl_x86.c         |  24 ++++
 tools/xl/xl_parse.c                  |  20 +++
 xen/include/public/arch-arm.h        |   4 +
 22 files changed, 551 insertions(+), 18 deletions(-)
 create mode 100644 tools/libacpi/ssdt_tpm2_tis.asl
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/4 libacpi: Build TPM2 tables for the TIS interface

TPM 2.0 can be exposed over either the CRB or TIS (FIFO) interface. Add TIS support to libacpii's current CRB support.

An updated SSDT is needed for the 0x5000 MMIO region. Also the start method changes.

---
 tools/libacpi/Makefile          |  2 +-
 tools/libacpi/acpi2_0.h         |  1 +
 tools/libacpi/build.c           | 47 ++++++++++++++++++++++++---------
 tools/libacpi/libacpi.h         |  4 +++
 tools/libacpi/ssdt_tpm2_tis.asl | 25 ++++++++++++++++++
 5 files changed, 66 insertions(+), 13 deletions(-)
 create mode 100644 tools/libacpi/ssdt_tpm2_tis.asl
tools/libacpi/Makefile +1 -1
index 3b4c3d8c86..aa37d5662e 100644
@@ -16,7 +16,7 @@ C_SRC-$(CONFIG_ARM_64) = dsdt_anycpu_arm.c
 DSDT_FILES ?= $(C_SRC-y)
 C_SRC = $(addprefix $(ACPI_BUILD_DIR)/, $(DSDT_FILES))
 H_SRC = $(addprefix $(ACPI_BUILD_DIR)/, ssdt_s3.h ssdt_s4.h ssdt_pm.h)
-H_SRC += $(addprefix $(ACPI_BUILD_DIR)/, ssdt_tpm.h ssdt_tpm2.h ssdt_laptop_slate.h)
+H_SRC += $(addprefix $(ACPI_BUILD_DIR)/, ssdt_tpm.h ssdt_tpm2.h ssdt_tpm2_tis.h ssdt_laptop_slate.h)
 
 MKDSDT_CFLAGS-$(CONFIG_ARM_64) = -DCONFIG_ARM_64
 MKDSDT_CFLAGS-$(CONFIG_X86) = -DCONFIG_X86
tools/libacpi/acpi2_0.h +1 -0
index 51623e2a8a..759be9ba43 100644
@@ -126,6 +126,7 @@ struct acpi_20_tpm2 {
     uint64_t log_area_start_address;
 };
 #define TPM2_ACPI_CLASS_CLIENT      0
+#define TPM2_START_METHOD_MMIO      6
 #define TPM2_START_METHOD_CRB       7
 
 /*
tools/libacpi/build.c +35 -12

66 lines not on this page · raw message

tools/libacpi/libacpi.h +4 -0
index deda39e5db..0b40896127 100644
@@ -28,6 +28,9 @@
 #define ACPI_HAS_CMOS_RTC          (1<<14)
 #define ACPI_HAS_SSDT_LAPTOP_SLATE (1<<15)
 
+#define ACPI_TPM_INTERFACE_CRB     0
+#define ACPI_TPM_INTERFACE_TIS     1
+
 struct xen_vmemrange;
 struct acpi_numa {
     uint32_t nr_vmemranges;
@@ -58,6 +61,7 @@ struct acpi_config {
     uint32_t table_flags;
     uint8_t acpi_revision;
     uint8_t tpm_version;
+    uint8_t tpm_interface; /* ACPI_TPM_INTERFACE_*, only used for TPM 2.0 */
 
     uint64_t vm_gid[2];
     unsigned long vm_gid_addr; /* OUT parameter */
tools/libacpi/ssdt_tpm2_tis.asl new file +25 -0
new file mode 100644
index 0000000000..837e50f90e
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: LGPL-2.1-only */
+/*
+ * ssdt_tpm2_tis.asl
+ */
+
+/* SSDT for TPM 2.0 TIS Interface for Xen with Qemu device model. */
+
+DefinitionBlock ("SSDT_TPM2_TIS.aml", "SSDT", 2, "Xen", "HVM", 0)
+{
+    Device (TPM)
+    {
+        Name (_HID, "MSFT0101" /* TPM 2.0 Security Device */)  // _HID: Hardware ID
+        Name (_CRS, ResourceTemplate ()  // _CRS: Current Resource Settings
+        {
+            Memory32Fixed (ReadWrite,
+                0xFED40000,         // Address Base
+                0x00005000,         // Address Length
+                )
+        })
+        Method (_STA, 0, NotSerialized)  // _STA: Status
+        {
+            Return (0x0F)
+        }
+    }
+}
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/4] libacpi: Build TPM2 tables for the TIS interface

tools/libacpi/Makefile C_SRC-$(CONFIG_ARM_64) = dsdt_anycpu_arm.c · line 19

Quoted 8 lines · Jason Andryuk
tools/libacpi/Makefile +1 -1
@@ -16,7 +16,7 @@ C_SRC-$(CONFIG_ARM_64) = dsdt_anycpu_arm.c
 DSDT_FILES ?= $(C_SRC-y)
 C_SRC = $(addprefix $(ACPI_BUILD_DIR)/, $(DSDT_FILES))
 H_SRC = $(addprefix $(ACPI_BUILD_DIR)/, ssdt_s3.h ssdt_s4.h ssdt_pm.h)
-H_SRC += $(addprefix $(ACPI_BUILD_DIR)/, ssdt_tpm.h ssdt_tpm2.h ssdt_laptop_slate.h)
+H_SRC += $(addprefix $(ACPI_BUILD_DIR)/, ssdt_tpm.h ssdt_tpm2.h ssdt_tpm2_tis.h ssdt_laptop_slate.h)

I think it's time to wrap this line.

Quoted 12 lines
tools/libacpi/build.c +1 -0
@@ -11,6 +11,7 @@
 #include "ssdt_s4.h"
 #include "ssdt_tpm.h"
 #include "ssdt_tpm2.h"
+#include "ssdt_tpm2_tis.h"
 #include "ssdt_pm.h"
 #include "ssdt_laptop_slate.h"
 #include <xen/hvm/hvm_info_table.h>
@@ -444,17 +445,30 @@ static int construct_secondary_tables(struct acpi_ctxt *ctxt,
             break;

tools/libacpi/build.c static int construct_secondary_tables(struct acpi_ctxt *ctxt, · line 448

Quoted 10 lines
tools/libacpi/build.c +1 -7
 
         case 2:
-            /*
-             * Check VID stored in bits 37:32 (3rd 16 bit word) of CRB
-             * identifier register.  See table 16 of TCG PC client platform
-             * TPM profile specification for TPM 2.0.
-             */
-            if ( config->crb_id[2] == 0 || config->crb_id[2] == 0xffff )
-                break;
+            if ( config->tpm_interface == ACPI_TPM_INTERFACE_TIS )

Better use switch() right away?

tools/libacpi/build.c static int construct_secondary_tables(struct acpi_ctxt *ctxt, · line 455

Quoted 10 lines
tools/libacpi/build.c +6 -3
+            {
+                if ( config->tis_hdr[0] == 0 || config->tis_hdr[0] == 0xffff ||
+                     config->tis_hdr[1] == 0 || config->tis_hdr[1] == 0xffff )
+                    break;
 
-            ssdt = ctxt->mem_ops.alloc(ctxt, sizeof(ssdt_tpm2), 16);
-            if (!ssdt) return -1;
-            memcpy(ssdt, ssdt_tpm2, sizeof(ssdt_tpm2));
+                ssdt = ctxt->mem_ops.alloc(ctxt, sizeof(ssdt_tpm2_tis), 16);
+                if (!ssdt) return -1;

Please can we not gain further instances of this style-violating code? It would be nice if ...

Quoted 4 lines
tools/libacpi/build.c +4 -0
+                memcpy(ssdt, ssdt_tpm2_tis, sizeof(ssdt_tpm2_tis));
+            }
+            else
+            {

tools/libacpi/build.c static int construct_secondary_tables(struct acpi_ctxt *ctxt, · line 469

Quoted 10 lines
tools/libacpi/build.c +10 -0
+                /*
+                 * Check VID stored in bits 37:32 (3rd 16 bit word) of CRB
+                 * identifier register.  See table 16 of TCG PC client
+                 * platform TPM profile specification for TPM 2.0.
+                 */
+                if ( config->crb_id[2] == 0 || config->crb_id[2] == 0xffff )
+                    break;
+
+                ssdt = ctxt->mem_ops.alloc(ctxt, sizeof(ssdt_tpm2), 16);
+                if (!ssdt) return -1;

... the one you re-indent could be corrected as well. Actually, can't we consolidate both allocation and ...

tools/libacpi/build.c static int construct_secondary_tables(struct acpi_ctxt *ctxt, · line 470

Quoted 1 line
tools/libacpi/build.c +1 -0
+                memcpy(ssdt, ssdt_tpm2, sizeof(ssdt_tpm2));

... copying paths by way of having another pair of local variables (pointer and size)?

tools/libacpi/build.c static int construct_secondary_tables(struct acpi_ctxt *ctxt, · line 491

Quoted 10 lines
tools/libacpi/build.c +4 -2
@@ -471,8 +485,17 @@ static int construct_secondary_tables(struct acpi_ctxt *ctxt,
             tpm2->header.creator_id   = ACPI_CREATOR_ID;
             tpm2->header.creator_revision = ACPI_CREATOR_REVISION;
             tpm2->platform_class = TPM2_ACPI_CLASS_CLIENT;
-            tpm2->control_area_address = TPM_CRB_CTRL_REQ;
-            tpm2->start_method = TPM2_START_METHOD_CRB;
+            if ( config->tpm_interface == ACPI_TPM_INTERFACE_TIS )
+            {
+                /* The FIFO interface has no control area. */
+                tpm2->control_area_address = 0;

There is a memset() earlier on, so only the comment looks to be needed here.

Quoted 11 lines
tools/libacpi/ssdt_tpm2_tis.asl new file +8 -0
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: LGPL-2.1-only */
+/*
+ * ssdt_tpm2_tis.asl
+ */
+
+/* SSDT for TPM 2.0 TIS Interface for Xen with Qemu device model. */
+
+DefinitionBlock ("SSDT_TPM2_TIS.aml", "SSDT", 2, "Xen", "HVM", 0)

tools/libacpi/ssdt_tpm2_tis.asl line 18

Quoted 10 lines
tools/libacpi/ssdt_tpm2_tis.asl new file +10 -0
+{
+    Device (TPM)
+    {
+        Name (_HID, "MSFT0101" /* TPM 2.0 Security Device */)  // _HID: Hardware ID
+        Name (_CRS, ResourceTemplate ()  // _CRS: Current Resource Settings
+        {
+            Memory32Fixed (ReadWrite,
+                0xFED40000,         // Address Base
+                0x00005000,         // Address Length
+                )

I know you're only following what we do elsewhere (i.e. this isn't a request for you to change anything), but I wonder how we could make sure that such seemingly arbitrary address range allocations won't ever conflict. (I assume the base address is dictated by qemu?)

Jan

#3 lore raw

Re: [PATCH 1/4] libacpi: Build TPM2 tables for the TIS interface

Quoted 5 lines · Jan Beulich
Quoted 3 lines · Jason Andryuk
(quoted diff) +0 -0
@@ -444,17 +445,30 @@ static int construct_secondary_tables(struct acpi_ctxt *ctxt,
              break;
 
Quoted 11 lines

tools/libacpi/build.c static int construct_secondary_tables(struct acpi_ctxt *ctxt, · line 448

Quoted 9 lines
tools/libacpi/build.c +1 -7
          case 2:
-            /*
-             * Check VID stored in bits 37:32 (3rd 16 bit word) of CRB
-             * identifier register.  See table 16 of TCG PC client platform
-             * TPM profile specification for TPM 2.0.
-             */
-            if ( config->crb_id[2] == 0 || config->crb_id[2] == 0xffff )
-                break;
+            if ( config->tpm_interface == ACPI_TPM_INTERFACE_TIS )

Better use switch() right away?

Well, I don't expect there to be more than these two on x86.

Quoted 3 lines

tools/libacpi/build.c static int construct_secondary_tables(struct acpi_ctxt *ctxt, · line 451

Quoted 3 lines
tools/libacpi/build.c +3 -0
+            {
+                if ( config->tis_hdr[0] == 0 || config->tis_hdr[0] == 0xffff ||
+                     config->tis_hdr[1] == 0 || config->tis_hdr[1] == 0xffff )

I'm going to add a prerequisite patch that moves this probing into hvmloader.

Quoted 16 lines
Quoted 5 lines
tools/libacpi/build.c +4 -0
+                memcpy(ssdt, ssdt_tpm2_tis, sizeof(ssdt_tpm2_tis));
+            }
+            else
+            {

tools/libacpi/build.c static int construct_secondary_tables(struct acpi_ctxt *ctxt, · line 469

Quoted 10 lines
tools/libacpi/build.c +10 -0
+                /*
+                 * Check VID stored in bits 37:32 (3rd 16 bit word) of CRB
+                 * identifier register.  See table 16 of TCG PC client
+                 * platform TPM profile specification for TPM 2.0.
+                 */
+                if ( config->crb_id[2] == 0 || config->crb_id[2] == 0xffff )
+                    break;
+
+                ssdt = ctxt->mem_ops.alloc(ctxt, sizeof(ssdt_tpm2), 16);
+                if (!ssdt) return -1;
Quoted 7 lines

... the one you re-indent could be corrected as well. Actually, can't we consolidate both allocation and ...

tools/libacpi/build.c static int construct_secondary_tables(struct acpi_ctxt *ctxt, · line 470

Quoted 1 line
tools/libacpi/build.c +1 -0
+                memcpy(ssdt, ssdt_tpm2, sizeof(ssdt_tpm2));

... copying paths by way of having another pair of local variables (pointer and size)?

Yes, sounds good.

Quoted 5 lines
Quoted 5 lines
tools/libacpi/build.c +0 -1
@@ -471,8 +485,17 @@ static int construct_secondary_tables(struct acpi_ctxt *ctxt,
              tpm2->header.creator_id   = ACPI_CREATOR_ID;
              tpm2->header.creator_revision = ACPI_CREATOR_REVISION;
              tpm2->platform_class = TPM2_ACPI_CLASS_CLIENT;
-            tpm2->control_area_address = TPM_CRB_CTRL_REQ;
Quoted 8 lines

tools/libacpi/build.c static int construct_secondary_tables(struct acpi_ctxt *ctxt, · line 491

Quoted 5 lines
tools/libacpi/build.c +4 -1
-            tpm2->start_method = TPM2_START_METHOD_CRB;
+            if ( config->tpm_interface == ACPI_TPM_INTERFACE_TIS )
+            {
+                /* The FIFO interface has no control area. */
+                tpm2->control_area_address = 0;

There is a memset() earlier on, so only the comment looks to be needed here.

More local variables streamlines this away.

Quoted 18 lines
Quoted 18 lines
tools/libacpi/ssdt_tpm2_tis.asl new file +15 -0
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: LGPL-2.1-only */
+/*
+ * ssdt_tpm2_tis.asl
+ */
+
+/* SSDT for TPM 2.0 TIS Interface for Xen with Qemu device model. */
+
+DefinitionBlock ("SSDT_TPM2_TIS.aml", "SSDT", 2, "Xen", "HVM", 0)
+{
+    Device (TPM)
+    {
+        Name (_HID, "MSFT0101" /* TPM 2.0 Security Device */)  // _HID: Hardware ID
+        Name (_CRS, ResourceTemplate ()  // _CRS: Current Resource Settings
+        {
+            Memory32Fixed (ReadWrite,
Quoted 8 lines

tools/libacpi/ssdt_tpm2_tis.asl line 18

Quoted 3 lines
tools/libacpi/ssdt_tpm2_tis.asl new file +3 -0
+                0xFED40000,         // Address Base
+                0x00005000,         // Address Length
+                )

I know you're only following what we do elsewhere (i.e. this isn't a request for you to change anything), but I wonder how we could make sure that such seemingly arbitrary address range allocations won't ever conflict. (I assume the base address is dictated by qemu?)

Yes, it is a hardcoded address in QEMU for TPM-TIS. More generally, 0xfed4xxxx is standardized by the Trusted Computing Group for TPM TIS on x86.

ARM defines assorted GUEST_* values for ram and mmio. Something like that could be done for x86. Though using them in an .asl file isn't friendly. TPM_REGISTER_BASE is already a define. That could be placed in the comment at least?

Thanks,
Jason

#4 lore raw

Re: [PATCH 1/4] libacpi: Build TPM2 tables for the TIS interface

Quoted 18 lines · Jason Andryuk
Quoted 5 lines · Jan Beulich
Quoted 3 lines · Jason Andryuk
(quoted diff) +0 -0
@@ -444,17 +445,30 @@ static int construct_secondary_tables(struct acpi_ctxt *ctxt,
              break;
 
Quoted 11 lines

tools/libacpi/build.c static int construct_secondary_tables(struct acpi_ctxt *ctxt, · line 448

Quoted 9 lines
tools/libacpi/build.c +1 -7
          case 2:
-            /*
-             * Check VID stored in bits 37:32 (3rd 16 bit word) of CRB
-             * identifier register.  See table 16 of TCG PC client platform
-             * TPM profile specification for TPM 2.0.
-             */
-            if ( config->crb_id[2] == 0 || config->crb_id[2] == 0xffff )
-                break;
+            if ( config->tpm_interface == ACPI_TPM_INTERFACE_TIS )

Better use switch() right away?

Quoted 1 line

Well, I don't expect there to be more than these two on x86.

I see. Something assert()-like on the else path may then be nice, except that the way libacpi is used (and there being no abstraction of it) this may not be possible in practice.

Quoted 27 lines
Quoted 18 lines
Quoted 18 lines
tools/libacpi/ssdt_tpm2_tis.asl new file +15 -0
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: LGPL-2.1-only */
+/*
+ * ssdt_tpm2_tis.asl
+ */
+
+/* SSDT for TPM 2.0 TIS Interface for Xen with Qemu device model. */
+
+DefinitionBlock ("SSDT_TPM2_TIS.aml", "SSDT", 2, "Xen", "HVM", 0)
+{
+    Device (TPM)
+    {
+        Name (_HID, "MSFT0101" /* TPM 2.0 Security Device */)  // _HID: Hardware ID
+        Name (_CRS, ResourceTemplate ()  // _CRS: Current Resource Settings
+        {
+            Memory32Fixed (ReadWrite,
Quoted 8 lines

tools/libacpi/ssdt_tpm2_tis.asl line 18

Quoted 3 lines
tools/libacpi/ssdt_tpm2_tis.asl new file +3 -0
+                0xFED40000,         // Address Base
+                0x00005000,         // Address Length
+                )

I know you're only following what we do elsewhere (i.e. this isn't a request for you to change anything), but I wonder how we could make sure that such seemingly arbitrary address range allocations won't ever conflict. (I assume the base address is dictated by qemu?)

Quoted 8 lines

Yes, it is a hardcoded address in QEMU for TPM-TIS. More generally, 0xfed4xxxx is standardized by the Trusted Computing Group for TPM TIS on x86.

ARM defines assorted GUEST_* values for ram and mmio. Something like that could be done for x86. Though using them in an .asl file isn't friendly. TPM_REGISTER_BASE is already a define. That could be placed in the comment at least?

Yes, this would then also serve as grep fodder.

Jan

#5 lore raw

PATCH 2/4 hvmloader: Support the TIS interface for TPM 2.0

In reply to Jason Andryuk

Read platform/tpm-interface to determine whether a TPM 2.0 device uses the TIS or CRB interface, and pass it to libacpi so the matching SSDT and TPM2 table are built. The key defaults to "crb" when absent, preserving the existing behaviour. TPM 1.2 always uses TIS.

---
tpm-interface is inconsistent with tpm_version, but it seems like "-" is more common than "_".

 docs/misc/xenstore-paths.pandoc | 12 +++++++++++-
 tools/firmware/hvmloader/util.c | 12 +++++++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)
docs/misc/xenstore-paths.pandoc +11 -1
index b8b02e007a..c24ffe342e 100644
@@ -280,10 +280,20 @@ circumstances where the generation ID needs to be changed.
 The TPM version to be probed for.
 
 A value of 1 indicates to probe for TPM 1.2, whereas a value of 2
-indicates that a TPM 2.0 using CRB should be probed.
+indicates that a TPM 2.0 should be probed, using the interface given by
+~/platform/tpm-interface.
 A value of 0 or an invalid value will result in no TPM being probed.
 If unset, a default of 1 is assumed.
 
+#### ~/platform/tpm-interface = ("tis"|"crb") [HVM,INTERNAL]
+
+The TPM interface to be probed for when ~/platform/tpm_version is 2.
+
+A value of "tis" indicates the TPM Interface Specification (FIFO)
+interface, whereas "crb" indicates the Command Response Buffer
+interface.  If unset or set to any other value, "crb" is assumed.
+TPM 1.2 always uses the TIS interface and ignores this key.
+
 ### Frontend device paths
 
 Paravirtual device frontends are generally specified by their own
tools/firmware/hvmloader/util.c +11 -1
index e651342681..fe3e6ab1b9 100644
@@ -922,7 +922,17 @@ void hvmloader_acpi_build_tables(struct acpi_config *config,
 
         case 2:
             config->table_flags |= ACPI_HAS_TPM;
-            config->crb_id = (uint16_t *)TPM_CRB_INTF_ID;
+            if ( !strcmp(xenstore_read("platform/tpm-interface", "crb"),
+                         "tis") )
+            {
+                config->tis_hdr = (uint16_t *)ACPI_TIS_HDR_ADDRESS;
+                config->tpm_interface = ACPI_TPM_INTERFACE_TIS;
+            }
+            else
+            {
+                config->crb_id = (uint16_t *)TPM_CRB_INTF_ID;
+                config->tpm_interface = ACPI_TPM_INTERFACE_CRB;
+            }
 
             mem_hole_populate_ram(TPM_LOG_AREA_ADDRESS >> PAGE_SHIFT,
                                   TPM_LOG_SIZE >> PAGE_SHIFT);
Signature · 1 line
2.34.1
Acked-by on v1 2/4
#6 lore raw

Re: [PATCH 2/4] hvmloader: Support the TIS interface for TPM 2.0

tools/firmware/hvmloader/util.c void hvmloader_acpi_build_tables(struct acpi_config *config, · line 926

Quoted 9 lines · Jason Andryuk
tools/firmware/hvmloader/util.c +2 -1
@@ -922,7 +922,17 @@ void hvmloader_acpi_build_tables(struct acpi_config *config,
 
         case 2:
             config->table_flags |= ACPI_HAS_TPM;
-            config->crb_id = (uint16_t *)TPM_CRB_INTF_ID;
+            if ( !strcmp(xenstore_read("platform/tpm-interface", "crb"),
+                         "tis") )

If you only care about the result being (not) equal to "tis", why pass in "crb"? Preferably with "" passed in:

Jan

#7 lore raw

PATCH 3/4 tools/libxl: add emulated TPM support backed by swtpm

In reply to Jason Andryuk

Add support for an emulated TPM. QEMU emulates the device and uses swtpm for the backend. swtpm is started first and QEMU connects to it.

Add the xl.cfg options are:
tpm_version
tpm_statedir
tpm_statefile
tpm_interface
tpm_loglevel
tpm_swtpm_override

When tpm_version is absent, no TPM is configured. tpm_version must be 1 or 2, and exactly one of tpm_statedir or tpm_statefile must be specified. tpm_statefile is only supported for swtpm 0.7 and later, so tpm_statedir is provided for compatiblity. tpm_interface can be either tis or crb. tpm_loglevel is passed to swtpm when non-zero, with swtpm logging to /var/log/xen/swtpm-<domain>.log. tpm_swtpm_override specifies the swtpm executable path; otherwise swtpm is found via PATH.

On x86, TPM 1.2 only supports TIS, while TPM 2.0 may use either and defaults to CRB.

libxl typically tracks spawned daemons through xenstore keys - like QEMU writing its "state" key as "running". swtpm does not write to xenstores, so that is not used. Instead the daemon is launched first and then QEMU launches and connects to it. If there is a failure, the QEMU log messages will list tpm in the error message.

interface is a Go keyword, so the binding generator needs to specify that.

---
 docs/man/xl.cfg.5.pod.in             |  36 ++++++
 tools/golang/xenlight/gengotypes.py  |   2 +-
 tools/golang/xenlight/helpers.gen.go |  48 ++++++++
 tools/golang/xenlight/types.gen.go   |  17 +++
 tools/include/libxl.h                |  10 ++
 tools/libs/light/libxl_create.c      |  46 +++++++-
 tools/libs/light/libxl_dm.c          | 161 +++++++++++++++++++++++++++
 tools/libs/light/libxl_domain.c      |   3 +
 tools/libs/light/libxl_internal.h    |   4 +
 tools/libs/light/libxl_types.idl     |  17 +++
 tools/libs/light/libxl_x86.c         |  18 +++
 tools/xl/xl_parse.c                  |  20 ++++
 12 files changed, 380 insertions(+), 2 deletions(-)
docs/man/xl.cfg.5.pod.in +36 -0
index b19c25c8d1..c12d2dc4db 100644
@@ -821,6 +821,42 @@ value is optional if this is a guest domain.
 
 =back
 
+=item B<tpm_version=NUMBER>
+
+Provides an emulated TPM to a QEMU-based HVM guest, backed by B<swtpm(8)>.
+The TPM version to emulate is either B<1> for TPM 1.2 or B<2> for TPM 2.0.
+When this option is not specified, no TPM is provided and the other
+B<tpm_*> options are ignored.
+
+One of B<tpm_statefile> or B<tpm_statedir> is required, but not both.
+
+=item B<tpm_statefile="PATH">
+
+Path to a single file in which B<swtpm> stores the persistent TPM state.
+This uses the C<--tpmstate backend-uri=file://PATH> option added in swtpm
+version 0.7.0.
+
+=item B<tpm_statedir="PATH">
+
+Path to a directory in which B<swtpm> stores the persistent TPM state.
+
+=item B<tpm_interface="tis"|"crb">
+
+The TPM interface presented to the guest.  B<tis> is the TPM Interface
+Specification (FIFO) interface and B<crb> is the Command Response Buffer
+interface.  B<tpm_version=1> only supports B<tis>, while B<tpm_version=2>
+supports either and defaults to B<crb>.
+
+=item B<tpm_swtpm_override="PATH">
+
+Path to the B<swtpm> executable.  By default, B<swtpm> is searched for in
+B<PATH>.
+
+=item B<tpm_loglevel=NUMBER>
+
+The B<swtpm> log level. When non-zero, B<swtpm> logs to
+F</var/log/xen/swtpm-DOMAIN.log>. Defaults to B<0> (no log).
+
 =item B<p9=[ "9PFS_SPEC_STRING", "9PFS_SPEC_STRING", ...]>
 
 Creates a Xen 9pfs connection to share a filesystem from the backend to the
tools/golang/xenlight/gengotypes.py +1 -1
index e4eb7ca1c1..628772d45d 100644
@@ -28,7 +28,7 @@ builtin_type_names = {
 }
 
 # Some go keywords that conflict with field names in libxl structs.
-go_keywords = ['type', 'func']
+go_keywords = ['type', 'func', 'interface']
 
 go_builtin_types = ['bool', 'string', 'int', 'byte',
                     'uint16', 'uint32', 'uint64']
tools/golang/xenlight/helpers.gen.go +48 -0

66 lines not on this page · raw message

tools/golang/xenlight/types.gen.go +17 -0
index 39bf9962d1..55b02ffcd7 100644
@@ -553,6 +553,22 @@ type XsQuotaList struct {
 Quota []XsQuotaItem
 }
 
+type TpmInterface int
+const(
+TpmInterfaceDefault TpmInterface = 0
+TpmInterfaceTis TpmInterface = 1
+TpmInterfaceCrb TpmInterface = 2
+)
+
+type Tpm struct {
+Statefile string
+Statedir string
+SwtpmOverride string
+Version int
+Loglevel int
+Interface TpmInterface
+}
+
 type DomainBuildInfo struct {
 MaxVcpus int
 AvailVcpus Bitmap
@@ -612,6 +628,7 @@ NestedHvm Defbool
 Apic Defbool
 DmRestrict Defbool
 Tee TeeType
+Tpm Tpm
 Type DomainType
 TypeUnion DomainBuildInfoTypeUnion
 ArchArm struct {
tools/include/libxl.h +10 -0
index d294fb36bd..8b431b708d 100644
@@ -1563,6 +1563,16 @@ void libxl_mac_copy(libxl_ctx *ctx, libxl_mac *dst, const libxl_mac *src);
  */
 #define LIBXL_HAVE_XENSTORE_QUOTA
 
+/*
+ * LIBXL_HAVE_TPM
+ *
+ * If this is defined the tpm member of struct domain_build_info is
+ * available.  When its version is non-zero, libxl starts swtpm to
+ * provide an emulated TPM to a QEMU-based HVM guest.  Its interface
+ * member selects the TPM interface (TIS or CRB) presented to the guest.
+ */
+#define LIBXL_HAVE_TPM
+
 typedef char **libxl_string_list;
 void libxl_string_list_dispose(libxl_string_list *sl);
 int libxl_string_list_length(const libxl_string_list *sl);
tools/libs/light/libxl_create.c +45 -1

64 lines not on this page · raw message

tools/libs/light/libxl_dm.c +161 -0

197 lines not on this page · raw message

tools/libs/light/libxl_domain.c +3 -0
index 37fcd92871..5f2ca8da7d 100644
@@ -1275,6 +1275,9 @@ static void dm_destroy_cb(libxl__egc *egc,
         libxl__qmp_cleanup(gc, target_domid);
     }
 
+    /* Terminate the emulated TPM backend, if any.  No-op otherwise. */
+    libxl__destroy_swtpm(gc, domid);
+
     dis->drs.ao = ao;
     dis->drs.domid = domid;
     dis->drs.callback = devices_destroy_cb;
tools/libs/light/libxl_internal.h +4 -0
index b65e0064b9..c561d3bc33 100644
@@ -4106,6 +4106,7 @@ struct libxl__dm_spawn_state {
     libxl__spawn_state spawn;
     libxl__ev_qmp qmp;
     libxl__ev_time timeout;
+    libxl__ev_child swtpm_child;
     libxl__dm_resume_state dmrs;
     libxl__qemu_available_opts qemu_opts;
     const char *dm;
@@ -4123,6 +4124,9 @@ _hidden void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state*);
  */
 _hidden int libxl__local_dm_preexec_restrict(libxl__gc *gc);
 
+/* swtpm (emulated TPM) backend for a QEMU-based HVM guest. */
+_hidden int libxl__destroy_swtpm(libxl__gc *gc, uint32_t domid);
+
 /* Stubdom device models. */
 
 typedef struct {
tools/libs/light/libxl_types.idl +17 -0
index ce697303b6..dc6790b821 100644
@@ -584,6 +584,22 @@ libxl_xs_quota_list = Struct("xs_quota_list", [
     ("quota", Array(libxl_xs_quota_item, "num_quota"))
     ])
 
+libxl_tpm_interface = Enumeration("tpm_interface", [
+    (0, "default"),
+    (1, "tis"),
+    (2, "crb"),
+    ])
+
+# Emulated TPM backed by swtpm.  A version of 0 means no TPM.
+libxl_tpm = Struct("tpm", [
+    ("statefile", string),
+    ("statedir",  string),
+    ("swtpm_override", string),
+    ("version",   integer, {'init_val': '0'}),
+    ("loglevel",  integer, {'init_val': '0'}),
+    ("interface", libxl_tpm_interface),
+    ])
+
 libxl_domain_build_info = Struct("domain_build_info",[
     ("max_vcpus",       integer),
     ("avail_vcpus",     libxl_bitmap),
@@ -658,6 +674,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
     ("apic",             libxl_defbool),
     ("dm_restrict",      libxl_defbool),
     ("tee",              libxl_tee_type),
+    ("tpm",              libxl_tpm),
     ("u", KeyedUnion(None, libxl_domain_type, "type",
                 [("hvm", Struct(None, [("firmware",         string),
                                        ("bios",             libxl_bios_type),
tools/libs/light/libxl_x86.c +18 -0
index 9e8b48372d..3182cfc9df 100644
@@ -825,6 +825,24 @@ int libxl__arch_domain_build_info_setdefault(libxl__gc *gc,
     libxl_defbool_setdefault(&b_info->arch_x86.msr_relaxed, false);
     libxl_defbool_setdefault(&b_info->trap_unmapped_accesses, false);
 
+    if (b_info->tpm.version && b_info->type == LIBXL_DOMAIN_TYPE_PVH) {
+        LOG(ERROR, "TPM is not supported for PVH guests on x86.");
+        return ERROR_INVAL;
+    }
+
+    /* TPM 1.2 only supports TIS.  TPM 2.0 supports TIS or CRB. */
+    if (b_info->tpm.version) {
+        if (b_info->tpm.interface == LIBXL_TPM_INTERFACE_DEFAULT)
+            b_info->tpm.interface = b_info->tpm.version == 2 ?
+                                    LIBXL_TPM_INTERFACE_CRB :
+                                    LIBXL_TPM_INTERFACE_TIS;
+        if (b_info->tpm.version == 1 &&
+            b_info->tpm.interface != LIBXL_TPM_INTERFACE_TIS) {
+            LOG(ERROR, "TPM version 1 only supports the tis interface.");
+            return ERROR_INVAL;
+        }
+    }
+
     if (b_info->type == LIBXL_DOMAIN_TYPE_HVM) {
         /*
          * The config parameter "altp2m" replaces the parameter "altp2mhvm".
tools/xl/xl_parse.c +20 -0
index 0ccece3e8a..1fed322380 100644
@@ -3060,6 +3060,26 @@ skip_usbdev:
         }
     }
 
+    if (!xlu_cfg_get_long(config, "tpm_version", &l, 0)) {
+        b_info->tpm.version = l;
+
+        xlu_cfg_replace_string(config, "tpm_statefile",
+                               &b_info->tpm.statefile, 0);
+        xlu_cfg_replace_string(config, "tpm_statedir",
+                               &b_info->tpm.statedir, 0);
+        xlu_cfg_replace_string(config, "tpm_swtpm_override",
+                               &b_info->tpm.swtpm_override, 0);
+        if (!xlu_cfg_get_long(config, "tpm_loglevel", &l, 0))
+            b_info->tpm.loglevel = l;
+
+        if (!xlu_cfg_get_string(config, "tpm_interface", &buf, 0) &&
+            (libxl_tpm_interface_from_string(buf, &b_info->tpm.interface) ||
+             b_info->tpm.interface == LIBXL_TPM_INTERFACE_DEFAULT)) {
+            fprintf(stderr, "xl: tpm_interface must be tis or crb\n");
+            exit(EXIT_FAILURE);
+        }
+    }
+
     if (!xlu_cfg_get_string (config, "sve", &buf, 1)) {
         e = libxl_sve_type_from_string(buf, &b_info->arch_arm.sve_vl);
         if (e) {
Signature · 1 line
2.34.1
Acked-by on v1 3/4
#8 lore raw

Re: [PATCH 3/4] tools/libxl: add emulated TPM support backed by swtpm

Quoted 23 lines · Jason Andryuk

Add support for an emulated TPM. QEMU emulates the device and uses swtpm for the backend. swtpm is started first and QEMU connects to it.

Add the xl.cfg options are:
tpm_version
tpm_statedir
tpm_statefile
tpm_interface
tpm_loglevel
tpm_swtpm_override

When tpm_version is absent, no TPM is configured. tpm_version must be 1 or 2, and exactly one of tpm_statedir or tpm_statefile must be specified. tpm_statefile is only supported for swtpm 0.7 and later, so tpm_statedir is provided for compatiblity. tpm_interface can be either tis or crb. tpm_loglevel is passed to swtpm when non-zero, with swtpm logging to /var/log/xen/swtpm-<domain>.log. tpm_swtpm_override specifies the swtpm executable path; otherwise swtpm is found via PATH.

On x86, TPM 1.2 only supports TIS, while TPM 2.0 may use either and defaults to CRB.

Quoted 9 lines

libxl typically tracks spawned daemons through xenstore keys - like QEMU writing its "state" key as "running". swtpm does not write to xenstores, so that is not used. Instead the daemon is launched first and then QEMU launches and connects to it. If there is a failure, the QEMU log messages will list tpm in the error message.

interface is a Go keyword, so the binding generator needs to specify that.

For the tools/golang changes:

#9 lore raw

PATCH 4/4 libxl: Add emulated TPM support on ARM

In reply to Jason Andryuk

From: Stefano Stabellini <[email protected]>

QEMU's ARM "xenpvh" machine runs as an IOREQ server and, when given a "tpm-base-addr" machine property, instantiates a TPM TIS sysbus device at that address connected to the "tpm0" TPM backend. Hook the existing tpm_* configuration up to it so ARM guests can use an swtpm-backed TPM.

Reserve GUEST_TPM_BASE/GUEST_TPM_SIZE in the ARM guest memory map and add a "tcg,tpm-tis-mmio" node to the guest device tree when a TPM version is configured. The xenpvh machine has no CRB device, so TIS is the default and only interface allowed for both TPM 1.2 and 2.0.

Add libxl__arch_pv_pvh_machinearg() returning the QEMU -machine argument for a PV/PVH guest. x86 returns plain "xenpv", while ARM uses "xenpvh" and appends tpm-base-addr when a TPM is configured. uint64_t tpm_base in ARM's libxl__arch_pvh_machinearg() is to allow PRIx64 to print the value. Otherwise the ull argument mismatches PRIx64's %lx format.

libxl__arch_pv_pvh_machinearg() also handles PV guests. This keeps the extra_pv handling identical between PV and PVH guests.

[ Merge ARM parts with new libxl_dm implementation. ]
---
 docs/man/xl.cfg.5.pod.in      | 14 ++++++++---
 tools/include/libxl.h         |  5 ++--
 tools/libs/light/libxl_arch.h |  6 +++++
 tools/libs/light/libxl_arm.c  | 47 +++++++++++++++++++++++++++++++++++
 tools/libs/light/libxl_dm.c   | 14 ++++++++++-
 tools/libs/light/libxl_x86.c  |  6 +++++
 xen/include/public/arch-arm.h |  4 +++
 7 files changed, 89 insertions(+), 7 deletions(-)
docs/man/xl.cfg.5.pod.in +10 -4
index c12d2dc4db..e5952d7e27 100644
@@ -823,11 +823,16 @@ value is optional if this is a guest domain.
 
 =item B<tpm_version=NUMBER>
 
-Provides an emulated TPM to a QEMU-based HVM guest, backed by B<swtpm(8)>.
-The TPM version to emulate is either B<1> for TPM 1.2 or B<2> for TPM 2.0.
+Provides an emulated TPM to a QEMU-based HVM guest, or to an Arm guest,
+backed by B<swtpm(8)>.  The TPM version to emulate is either B<1> for
+TPM 1.2 or B<2> for TPM 2.0.
 When this option is not specified, no TPM is provided and the other
 B<tpm_*> options are ignored.
 
+On Arm, QEMU's B<xenpvh> machine is started as an IOREQ server to emulate
+a TPM TIS interface, and the guest device tree gains a
+C<tcg,tpm-tis-mmio> node describing it.
+
 One of B<tpm_statefile> or B<tpm_statedir> is required, but not both.
 
 =item B<tpm_statefile="PATH">
@@ -844,8 +849,9 @@ Path to a directory in which B<swtpm> stores the persistent TPM state.
 
 The TPM interface presented to the guest.  B<tis> is the TPM Interface
 Specification (FIFO) interface and B<crb> is the Command Response Buffer
-interface.  B<tpm_version=1> only supports B<tis>, while B<tpm_version=2>
-supports either and defaults to B<crb>.
+interface.  On x86, B<tpm_version=1> only supports B<tis>, while
+B<tpm_version=2> supports either and defaults to B<crb>.  On Arm, only
+B<tis> is supported, for both TPM versions.
 
 =item B<tpm_swtpm_override="PATH">
 
tools/include/libxl.h +3 -2
index 8b431b708d..8d19cf41bc 100644
@@ -1568,8 +1568,9 @@ void libxl_mac_copy(libxl_ctx *ctx, libxl_mac *dst, const libxl_mac *src);
  *
  * If this is defined the tpm member of struct domain_build_info is
  * available.  When its version is non-zero, libxl starts swtpm to
- * provide an emulated TPM to a QEMU-based HVM guest.  Its interface
- * member selects the TPM interface (TIS or CRB) presented to the guest.
+ * provide an emulated TPM to a QEMU-based HVM guest, or to an Arm guest
+ * via QEMU's xenpvh machine.  Its interface member selects the TPM
+ * interface (TIS or CRB) presented to the guest.
  */
 #define LIBXL_HAVE_TPM
 
tools/libs/light/libxl_arch.h +6 -0
index c8ef52ddbe..b3355d1e82 100644
@@ -97,6 +97,12 @@ _hidden
 int libxl__arch_hvm_unmap_gsi(libxl__gc *gc, uint32_t sbdf, uint32_t domid);
 _hidden
 bool libxl__arch_local_domain_has_pirq_notion(libxl__gc *gc);
+
+/* QEMU -machine argument for a PVH guest. */
+_hidden
+char *libxl__arch_pv_pvh_machinearg(libxl__gc *gc,
+                                    const libxl_domain_build_info *b_info);
+
 #if defined(__i386__) || defined(__x86_64__)
 
 #define LAPIC_BASE_ADDRESS  0xfee00000
tools/libs/light/libxl_arm.c +47 -0

71 lines not on this page · raw message

tools/libs/light/libxl_dm.c +13 -1
index 843b8e29eb..ebdd05586e 100644
@@ -18,6 +18,7 @@
 #include "libxl_osdeps.h" /* must come before any other headers */
 
 #include "libxl_internal.h"
+#include "libxl_arch.h"
 
 #include <xen/hvm/e820.h>
 #include <sys/types.h>
@@ -1573,7 +1574,12 @@ static int libxl__build_device_model_args_new(libxl__gc *gc,
     switch (b_info->type) {
     case LIBXL_DOMAIN_TYPE_PVH:
     case LIBXL_DOMAIN_TYPE_PV:
-        flexarray_append(dm_args, "xenpv");
+        machinearg = libxl__arch_pv_pvh_machinearg(gc, b_info);
+        flexarray_append(dm_args, machinearg);
+
+        if (b_info->tpm.version)
+            libxl__dm_add_tpm_args(gc, dm_args, guest_domid);
+
         for (i = 0; b_info->extra_pv && b_info->extra_pv[i] != NULL; i++)
             flexarray_append(dm_args, b_info->extra_pv[i]);
         break;
@@ -3892,6 +3898,12 @@ int libxl__need_xenpv_qemu(libxl__gc *gc, libxl_domain_config *d_config)
         goto out;
     }
 
+    /* QEMU provides the emulated TPM, e.g. via the ARM xenpvh machine. */
+    if (d_config->b_info.tpm.version) {
+        ret = 1;
+        goto out;
+    }
+
     for (idx = 0;; idx++) {
         dt = device_type_tbl[idx];
         if (!dt)
tools/libs/light/libxl_x86.c +6 -0
index 3182cfc9df..a6e9ad777e 100644
@@ -937,6 +937,12 @@ bool libxl__arch_local_domain_has_pirq_notion(libxl__gc *gc)
     return false;
 }
 
+char *libxl__arch_pv_pvh_machinearg(libxl__gc *gc,
+                                    const libxl_domain_build_info *b_info)
+{
+    return libxl__strdup(gc, "xenpv");
+}
+
 int libxl__arch_hvm_map_gsi(libxl__gc *gc, uint32_t sbdf, uint32_t domid)
 {
     int pirq = -1, gsi, r;
xen/include/public/arch-arm.h +4 -0
index 5d15f572c7..efb45eb33c 100644
@@ -452,6 +452,10 @@ typedef uint64_t xen_callback_t;
 #define GUEST_VPCI_ECAM_BASE    xen_mk_ullong(0x10000000)
 #define GUEST_VPCI_ECAM_SIZE    xen_mk_ullong(0x10000000)
 
+/* Emulated TPM (TIS over MMIO) */
+#define GUEST_TPM_BASE    xen_mk_ullong(0x0c000000)
+#define GUEST_TPM_SIZE    xen_mk_ullong(0x00005000)
+
 /* ACPI tables physical address */
 #define GUEST_ACPI_BASE xen_mk_ullong(0x20000000)
 #define GUEST_ACPI_SIZE xen_mk_ullong(0x02000000)
Signature · 1 line
2.34.1