Skip to contentSkip to conversation

Implement Pysical Presence Interface for TPM 1.2 and 2

RFC PATCH Superseded Stefan Berger 4 participants 16 messages Jan 10, 2018 – Jan 15, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Implement Pysical Presence Interface for TPM 1.2 and 2

The following patches implement the TPM Physical Presence Interface that allows a user to set a command via ACPI (sysfs entry in Linux) that, upon the next reboot, the firmware (BIOS, UEFI) looks for an acts upon by sending sequences of commands to the TPM 1.2 or 2.

My first goal is to get the virtual memory device accepted since it extends the TPM TIS (and TPM CRB) interface's internal data structure with another 256 bytes that would have to be written out when suspending the VM. So this patch would have to come before we support VM with TPM suspend and resume. The device supports 256 bytes at address 0xffff0000. This is the address that EDK2 uses and that I have patches for for SeaBIOS support.

Regards,
Stefan

Stefan Berger (3):
tpm: Implement virtual memory device for TPM PPI
acpi: implement aml_lless_equal
acpi: Build TPM Physical Presence interface

 hw/acpi/aml-build.c         |  10 ++
 hw/i386/acpi-build.c        | 227 ++++++++++++++++++++++++++++++++++++++++++++
 hw/tpm/Makefile.objs        |   2 +-
 hw/tpm/tpm_ppi.c            |  79 +++++++++++++++
 hw/tpm/tpm_ppi.h            |  25 +++++
 hw/tpm/tpm_tis.c            |   5 +
 include/hw/acpi/aml-build.h |   1 +
 include/hw/acpi/tpm.h       |  21 ++++
 8 files changed, 369 insertions(+), 1 deletion(-)
 create mode 100644 hw/tpm/tpm_ppi.c
 create mode 100644 hw/tpm/tpm_ppi.h
Signature · 1 line
2.5.5
#1 lore raw

RFC PATCH 1/3 tpm: Implement virtual memory device for TPM PPI

Implement a virtual memory device for the TPM physical presence interface. The memory is located at 0xffff0000 and used by ACPI to send messages to the firmware (BIOS).

This device should be used by all TPM interfaces on x86 and can be added through by calling tpm_ppi_init_io().

---
 hw/tpm/Makefile.objs  |  2 +-
 hw/tpm/tpm_ppi.c      | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/tpm/tpm_ppi.h      | 25 ++++++++++++++++
 hw/tpm/tpm_tis.c      |  5 ++++
 include/hw/acpi/tpm.h |  6 ++++
 5 files changed, 116 insertions(+), 1 deletion(-)
 create mode 100644 hw/tpm/tpm_ppi.c
 create mode 100644 hw/tpm/tpm_ppi.h
hw/tpm/Makefile.objs +1 -1
index 7a93b24..3eb0558 100644
@@ -1,4 +1,4 @@
-common-obj-y += tpm_util.o
+common-obj-y += tpm_util.o tpm_ppi.o
 common-obj-$(CONFIG_TPM_TIS) += tpm_tis.o
 common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
 common-obj-$(CONFIG_TPM_EMULATOR) += tpm_emulator.o
hw/tpm/tpm_ppi.c new file +79 -0

79 lines not on this page · raw message

hw/tpm/tpm_ppi.h new file +25 -0
new file mode 100644
index 0000000..8959912
@@ -0,0 +1,25 @@
+/*
+ * TPM Physical Presence Interface
+ *
+ * Copyright (C) 2018 IBM Corporation
+ *
+ * Authors:
+ *  Stefan Berger    <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#ifndef TPM_TPM_PPI_H
+#define TPM_TPM_PPI_H
+
+#include "hw/acpi/tpm.h"
+
+typedef struct TPMPPI {
+    MemoryRegion mmio;
+
+    uint8_t ram[TPM_PPI_ADDR_SIZE];
+} TPMPPI;
+
+void tpm_ppi_init_io(TPMPPI *tpmppi, Object *obj);
+
+#endif /* TPM_TPM_PPI_H */
hw/tpm/tpm_tis.c +5 -0
index 561384c..f980972 100644
@@ -31,6 +31,7 @@
 #include "sysemu/tpm_backend.h"
 #include "tpm_int.h"
 #include "tpm_util.h"
+#include "tpm_ppi.h"
 
 #define TPM_TIS_NUM_LOCALITIES      5     /* per spec */
 #define TPM_TIS_LOCALITY_SHIFT      12
@@ -80,6 +81,8 @@ typedef struct TPMState {
     TPMVersion be_tpm_version;
 
     size_t be_buffer_size;
+
+    TPMPPI ppi;
 } TPMState;
 
 #define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)
@@ -1050,6 +1053,8 @@ static void tpm_tis_initfn(Object *obj)
     memory_region_init_io(&s->mmio, OBJECT(s), &tpm_tis_memory_ops,
                           s, "tpm-tis-mmio",
                           TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
+
+    tpm_ppi_init_io(&s->ppi, obj);
 }
 
 static void tpm_tis_class_init(ObjectClass *klass, void *data)
include/hw/acpi/tpm.h +6 -0
index 6d516c6..d9b7452 100644
@@ -31,4 +31,10 @@
 
 #define TPM2_START_METHOD_MMIO      6
 
+/*
+ * Physical Presence Interface
+ */
+#define TPM_PPI_ADDR_SIZE           0x100
+#define TPM_PPI_ADDR_BASE           0xffff0000
+
 #endif /* HW_ACPI_TPM_H */
Signature · 1 line
2.5.5
#7 lore raw

RFC PATCH 2/3 acpi: implement aml_lless_equal

In reply to Stefan Berger

LLessEqualOp = LNotOp LGreaterOp

---
 hw/acpi/aml-build.c         | 10 ++++++++++
 include/hw/acpi/aml-build.h |  1 +
 2 files changed, 11 insertions(+)
hw/acpi/aml-build.c +10 -0
index 36a6cc4..597a58d 100644
@@ -568,6 +568,16 @@ Aml *aml_lless(Aml *arg1, Aml *arg2)
     return var;
 }
 
+Aml *aml_lless_equal(Aml *arg1, Aml *arg2)
+{
+    /* LLessEqualOp := LNotOp LGreaterOp */
+    Aml *var = aml_opcode(0x92 /* LNotOp */);
+    build_append_byte(var->buf, 0x94 /* LGreaterOp */);
+    aml_append(var, arg1);
+    aml_append(var, arg2);
+    return var;
+}
+
 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAdd */
 Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst)
 {
include/hw/acpi/aml-build.h +1 -0
index 88d0738..c4398cc 100644
@@ -267,6 +267,7 @@ Aml *aml_lor(Aml *arg1, Aml *arg2);
 Aml *aml_shiftleft(Aml *arg1, Aml *count);
 Aml *aml_shiftright(Aml *arg1, Aml *count, Aml *dst);
 Aml *aml_lless(Aml *arg1, Aml *arg2);
+Aml *aml_lless_equal(Aml *arg1, Aml *arg2);
 Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst);
 Aml *aml_subtract(Aml *arg1, Aml *arg2, Aml *dst);
 Aml *aml_increment(Aml *arg);
Signature · 1 line
2.5.5
#11 lore raw

RFC PATCH 3/3 acpi: Build TPM Physical Presence interface

In reply to Stefan Berger

The TPM Physical Presence interface consists of an ACPI part, a shared memory part, and code in the firmware. Users can send messages to the firmware by writing a code into the shared memory through invoking the ACPI code. When a reboot happens, the firmware looks for the code and acts on it by sending sequences of commands to the TPM.

This patch adds the ACPI code. It is similar to the one in EDK2 but doesn't assume that SMIs are necessary to use. Besides that it tests the code entered by the user and checks whether it is supported. The range of codes supported matches the range of codes supported in SeaBIOS. It uses the same datastructure for the shared memory as EDK2 does so that EDK2 and SeaBIOS could both make use of the shared memory.

The underlying TCG specification is accessible from the following page.

https://trustedcomputinggroup.org/tcg-physical-presence-interface-specification/

This patch implements version 1.20.

---
 hw/i386/acpi-build.c  | 227 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/acpi/tpm.h |  15 ++++
 2 files changed, 242 insertions(+)
hw/i386/acpi-build.c +227 -0
index 18b939e..e3905a3 100644
@@ -42,6 +42,7 @@
 #include "hw/acpi/memory_hotplug.h"
 #include "sysemu/tpm.h"
 #include "hw/acpi/tpm.h"
+#include "hw/tpm/tpm_ppi.h"
 #include "hw/acpi/vmgenid.h"
 #include "sysemu/tpm_backend.h"
 #include "hw/timer/mc146818rtc_regs.h"
@@ -1860,6 +1861,231 @@ static Aml *build_q35_osc_method(void)
 }
 
 static void
+build_tpm_ppi(Aml *dev, TPMVersion tpm_version)
+{
+    Aml *method, *field, *ifctx, *ifctx2, *ifctx3, *pak;
+
+    aml_append(dev,
+               aml_operation_region("TPPI", AML_SYSTEM_MEMORY,
+                                    aml_int(TPM_PPI_ADDR_BASE),
+                                    TPM_PPI_STRUCT_SIZE));
+
+    field = aml_field("TPPI", AML_ANY_ACC, AML_NOLOCK, AML_PRESERVE);
+    aml_append(field, aml_named_field("PPIN",
+               sizeof(uint8_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPIP",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPRP",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPRQ",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPRM",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("LPPR",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("FRET",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("RES1",
+               sizeof(uint8_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("RES2",
+               sizeof(uint32_t) * BITS_PER_BYTE * 4));
+    aml_append(field, aml_named_field("FAIL",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(dev, field);
+
+    /*
+     * Write the given operations code into 'PPRQ'.
+     */
+    method = aml_method("WRAM", 2, AML_SERIALIZED);
+    {
+        aml_append(method, aml_store(aml_arg(0), aml_name("PPRQ")));
+        aml_append(method, aml_store(aml_arg(1), aml_name("PPRM")));
+        /* 0 = Success */
+        aml_append(method, aml_return(aml_int(0)));
+    }
+    aml_append(dev, method);
+
+    /*
+     * CKOP: Check whether the opcode is valid
+     */
+    if (tpm_version == TPM_VERSION_1_2) {
+        method = aml_method("CKOP", 1, AML_NOTSERIALIZED);
+        {
+            ifctx = aml_if(
+                      aml_or(
+                        aml_or(
+                          aml_and(
+                            aml_lgreater_equal(aml_arg(0), aml_int(0)),
+                            aml_lless_equal(aml_arg(0), aml_int(11)),
+                            NULL
+                          ),
+                          aml_equal(aml_arg(0), aml_int(14)),
+                          NULL
+                        ),
+                        aml_and(
+                          aml_lgreater_equal(aml_arg(0), aml_int(21)),
+                          aml_lless_equal(aml_arg(0), aml_int(22)),
+                          NULL
+                       ),
+                       NULL
+                      )
+                    );
+            {
+                aml_append(ifctx, aml_return(aml_int(1)));
+            }
+            aml_append(method, ifctx);
+
+            aml_append(method, aml_return(aml_int(0)));
+        }
+    } else {
+        method = aml_method("CKOP", 1, AML_NOTSERIALIZED);
+        {
+            ifctx = aml_if(
+                      aml_equal(aml_arg(0), aml_int(0))
+                    );
+            {
+                aml_append(ifctx, aml_return(aml_int(1)));
+            }
+            aml_append(method, ifctx);
+
+            aml_append(method, aml_return(aml_int(0)));
+        }
+    }
+    aml_append(dev, method);
+
+    method = aml_method("_DSM", 4, AML_SERIALIZED);
+    {
+        uint8_t zerobyte[1] = { 0 };
+
+        ifctx = aml_if(
+                  aml_equal(aml_arg(0),
+                            aml_touuid("3DDDFAA6-361B-4EB4-A424-8D10089D1653"))
+                );
+        {
+            aml_append(ifctx,
+                       aml_store(aml_to_integer(aml_arg(2)), aml_local(0)));
+
+            /* standard DSM query function */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(0)));
+            {
+                uint8_t byte_list[2] = { 0xff, 0x01 };
+                aml_append(ifctx2, aml_return(aml_buffer(2, byte_list)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* interface version: 1.2 */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(1)));
+            {
+                aml_append(ifctx2, aml_return(aml_string("1.2")));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* submit TPM operation */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(2)));
+            {
+                aml_append(ifctx2,
+                           aml_store(aml_derefof(aml_index(aml_arg(3),
+                                                           aml_int(0))),
+                                     aml_local(0)));
+                ifctx3 = aml_if(aml_call1("CKOP", aml_local(0)));
+                {
+                    aml_append(ifctx3,
+                               aml_return(aml_call2("WRAM",
+                                                    aml_local(0),
+                                                    aml_int(0))));
+                }
+                aml_append(ifctx2, ifctx3);
+                aml_append(ifctx2, aml_return(aml_int(1)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get pending TPM operation */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(3)));
+            {
+                pak = aml_package(2);
+                aml_append(pak, aml_int(0));
+                aml_append(pak, aml_name("PPRQ"));
+                aml_append(ifctx2, aml_return(pak));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get platform-specific action to transition to pre-OS env. */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(4)));
+            {
+                /* 2 = reboot */
+                aml_append(ifctx2, aml_return(aml_int(2)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get TPM operation response */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(5)));
+            {
+                pak = aml_package(3);
+
+                aml_append(pak, aml_name("FAIL"));
+                aml_append(pak, aml_name("LPPR"));
+                aml_append(pak, aml_name("PPRP"));
+
+                aml_append(ifctx2, aml_return(pak));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* submit preferred user language */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(6)));
+            {
+                /* 3 = not implemented */
+                aml_append(ifctx2, aml_return(aml_int(3)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* submit TPM operation v2 */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(7)));
+            {
+                aml_append(ifctx2,
+                           aml_store(aml_derefof(aml_index(aml_arg(3),
+                                                           aml_int(0))),
+                                     aml_local(0)));
+                ifctx3 = aml_if(aml_call1("CKOP", aml_local(0)));
+                {
+                    aml_append(ifctx3,
+                               aml_store(aml_call2("WRAM",
+                                                   aml_local(0),
+                                                   aml_int(0)),
+                                         aml_local(1)));
+                    aml_append(ifctx3, aml_return(aml_local(1)));
+                }
+                aml_append(ifctx2, ifctx3);
+                /* 1 = requested operation not implemented */
+                aml_append(ifctx2, aml_return(aml_int(1)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get user confirmation status for operation */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(8)));
+            {
+                aml_append(ifctx2,
+                           aml_store(aml_derefof(aml_index(aml_arg(3),
+                                                           aml_int(0))),
+                                     aml_local(0)));
+                ifctx3 = aml_if(aml_call1("CKOP", aml_local(0)));
+                {
+                    /* 4 = Allowed and physically present user not required */
+                    aml_append(ifctx3, aml_return(aml_int(4)));
+                }
+                aml_append(ifctx2, ifctx3);
+                /* 0 = requested operation not implemented */
+                aml_append(ifctx2, aml_return(aml_int(0)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            aml_append(ifctx, aml_return(aml_buffer(1, zerobyte)));
+        }
+        aml_append(method, ifctx);
+    }
+    aml_append(dev, method);
+}
+
+static void
 build_dsdt(GArray *table_data, BIOSLinker *linker,
            AcpiPmInfo *pm, AcpiMiscInfo *misc,
            Range *pci_hole, Range *pci_hole64, MachineState *machine)
@@ -2218,6 +2444,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
                  */
                 /* aml_append(crs, aml_irq_no_flags(TPM_TIS_IRQ)); */
                 aml_append(dev, aml_name_decl("_CRS", crs));
+                build_tpm_ppi(dev, misc->tpm_version);
                 aml_append(scope, dev);
             }
 
include/hw/acpi/tpm.h +15 -0
index d9b7452..23f5da1 100644
@@ -37,4 +37,19 @@
 #define TPM_PPI_ADDR_SIZE           0x100
 #define TPM_PPI_ADDR_BASE           0xffff0000
 
+struct tpm_ppi {
+    uint8_t ppin;            /* set by BIOS; currently initialization flag */
+    uint32_t ppip;           /* set by ACPI; not used */
+    uint32_t pprp;           /* response from TPM; set by BIOS */
+    uint32_t pprq;           /* opcode; set by ACPI */
+    uint32_t pprm;           /* parameter for opcode; set by ACPI */
+    uint32_t lppr;           /* last opcode; set by BIOS */
+    uint32_t fret;           /* set by ACPI; not used*/
+    uint32_t res1;           /* reserved */
+    uint32_t res2[4];        /* reserved */
+    uint32_t fail;           /* set by BIOS (0 = success) */
+} QEMU_PACKED;
+
+#define TPM_PPI_STRUCT_SIZE  sizeof(struct tpm_ppi)
+
 #endif /* HW_ACPI_TPM_H */
Signature · 1 line
2.5.5
#12 lore raw

Re: [Qemu-devel] [RFC PATCH 3/3] acpi: Build TPM Physical Presence interface

Quoted 13 lines · Stefan Berger

The TPM Physical Presence interface consists of an ACPI part, a shared memory part, and code in the firmware. Users can send messages to the firmware by writing a code into the shared memory through invoking the ACPI code. When a reboot happens, the firmware looks for the code and acts on it by sending sequences of commands to the TPM.

This patch adds the ACPI code. It is similar to the one in EDK2 but doesn't assume that SMIs are necessary to use. Besides that it tests the code entered by the user and checks whether it is supported. The range of codes supported matches the range of codes supported in SeaBIOS. It uses the same datastructure for the shared memory as EDK2 does so that EDK2 and SeaBIOS could both make use of the shared memory.

Quoted 5 lines

The underlying TCG specification is accessible from the following page.

https://trustedcomputinggroup.org/tcg-physical-presence-interface-specification/

This patch implements version 1.20.

The below ACPI code is partly a translation to C from a previous posted patch:

https://lists.gnu.org/archive/html/qemu-devel/2015-05/msg05353.html

Stefan

Quoted 283 lines
---
  hw/i386/acpi-build.c  | 227 ++++++++++++++++++++++++++++++++++++++++++++++++++
  include/hw/acpi/tpm.h |  15 ++++
  2 files changed, 242 insertions(+)
hw/i386/acpi-build.c +227 -0
index 18b939e..e3905a3 100644
@@ -42,6 +42,7 @@
  #include "hw/acpi/memory_hotplug.h"
  #include "sysemu/tpm.h"
  #include "hw/acpi/tpm.h"
+#include "hw/tpm/tpm_ppi.h"
  #include "hw/acpi/vmgenid.h"
  #include "sysemu/tpm_backend.h"
  #include "hw/timer/mc146818rtc_regs.h"
@@ -1860,6 +1861,231 @@ static Aml *build_q35_osc_method(void)
  }
 
  static void
+build_tpm_ppi(Aml *dev, TPMVersion tpm_version)
+{
+    Aml *method, *field, *ifctx, *ifctx2, *ifctx3, *pak;
+
+    aml_append(dev,
+               aml_operation_region("TPPI", AML_SYSTEM_MEMORY,
+                                    aml_int(TPM_PPI_ADDR_BASE),
+                                    TPM_PPI_STRUCT_SIZE));
+
+    field = aml_field("TPPI", AML_ANY_ACC, AML_NOLOCK, AML_PRESERVE);
+    aml_append(field, aml_named_field("PPIN",
+               sizeof(uint8_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPIP",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPRP",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPRQ",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPRM",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("LPPR",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("FRET",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("RES1",
+               sizeof(uint8_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("RES2",
+               sizeof(uint32_t) * BITS_PER_BYTE * 4));
+    aml_append(field, aml_named_field("FAIL",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(dev, field);
+
+    /*
+     * Write the given operations code into 'PPRQ'.
+     */
+    method = aml_method("WRAM", 2, AML_SERIALIZED);
+    {
+        aml_append(method, aml_store(aml_arg(0), aml_name("PPRQ")));
+        aml_append(method, aml_store(aml_arg(1), aml_name("PPRM")));
+        /* 0 = Success */
+        aml_append(method, aml_return(aml_int(0)));
+    }
+    aml_append(dev, method);
+
+    /*
+     * CKOP: Check whether the opcode is valid
+     */
+    if (tpm_version == TPM_VERSION_1_2) {
+        method = aml_method("CKOP", 1, AML_NOTSERIALIZED);
+        {
+            ifctx = aml_if(
+                      aml_or(
+                        aml_or(
+                          aml_and(
+                            aml_lgreater_equal(aml_arg(0), aml_int(0)),
+                            aml_lless_equal(aml_arg(0), aml_int(11)),
+                            NULL
+                          ),
+                          aml_equal(aml_arg(0), aml_int(14)),
+                          NULL
+                        ),
+                        aml_and(
+                          aml_lgreater_equal(aml_arg(0), aml_int(21)),
+                          aml_lless_equal(aml_arg(0), aml_int(22)),
+                          NULL
+                       ),
+                       NULL
+                      )
+                    );
+            {
+                aml_append(ifctx, aml_return(aml_int(1)));
+            }
+            aml_append(method, ifctx);
+
+            aml_append(method, aml_return(aml_int(0)));
+        }
+    } else {
+        method = aml_method("CKOP", 1, AML_NOTSERIALIZED);
+        {
+            ifctx = aml_if(
+                      aml_equal(aml_arg(0), aml_int(0))
+                    );
+            {
+                aml_append(ifctx, aml_return(aml_int(1)));
+            }
+            aml_append(method, ifctx);
+
+            aml_append(method, aml_return(aml_int(0)));
+        }
+    }
+    aml_append(dev, method);
+
+    method = aml_method("_DSM", 4, AML_SERIALIZED);
+    {
+        uint8_t zerobyte[1] = { 0 };
+
+        ifctx = aml_if(
+                  aml_equal(aml_arg(0),
+                            aml_touuid("3DDDFAA6-361B-4EB4-A424-8D10089D1653"))
+                );
+        {
+            aml_append(ifctx,
+                       aml_store(aml_to_integer(aml_arg(2)), aml_local(0)));
+
+            /* standard DSM query function */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(0)));
+            {
+                uint8_t byte_list[2] = { 0xff, 0x01 };
+                aml_append(ifctx2, aml_return(aml_buffer(2, byte_list)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* interface version: 1.2 */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(1)));
+            {
+                aml_append(ifctx2, aml_return(aml_string("1.2")));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* submit TPM operation */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(2)));
+            {
+                aml_append(ifctx2,
+                           aml_store(aml_derefof(aml_index(aml_arg(3),
+                                                           aml_int(0))),
+                                     aml_local(0)));
+                ifctx3 = aml_if(aml_call1("CKOP", aml_local(0)));
+                {
+                    aml_append(ifctx3,
+                               aml_return(aml_call2("WRAM",
+                                                    aml_local(0),
+                                                    aml_int(0))));
+                }
+                aml_append(ifctx2, ifctx3);
+                aml_append(ifctx2, aml_return(aml_int(1)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get pending TPM operation */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(3)));
+            {
+                pak = aml_package(2);
+                aml_append(pak, aml_int(0));
+                aml_append(pak, aml_name("PPRQ"));
+                aml_append(ifctx2, aml_return(pak));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get platform-specific action to transition to pre-OS env. */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(4)));
+            {
+                /* 2 = reboot */
+                aml_append(ifctx2, aml_return(aml_int(2)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get TPM operation response */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(5)));
+            {
+                pak = aml_package(3);
+
+                aml_append(pak, aml_name("FAIL"));
+                aml_append(pak, aml_name("LPPR"));
+                aml_append(pak, aml_name("PPRP"));
+
+                aml_append(ifctx2, aml_return(pak));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* submit preferred user language */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(6)));
+            {
+                /* 3 = not implemented */
+                aml_append(ifctx2, aml_return(aml_int(3)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* submit TPM operation v2 */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(7)));
+            {
+                aml_append(ifctx2,
+                           aml_store(aml_derefof(aml_index(aml_arg(3),
+                                                           aml_int(0))),
+                                     aml_local(0)));
+                ifctx3 = aml_if(aml_call1("CKOP", aml_local(0)));
+                {
+                    aml_append(ifctx3,
+                               aml_store(aml_call2("WRAM",
+                                                   aml_local(0),
+                                                   aml_int(0)),
+                                         aml_local(1)));
+                    aml_append(ifctx3, aml_return(aml_local(1)));
+                }
+                aml_append(ifctx2, ifctx3);
+                /* 1 = requested operation not implemented */
+                aml_append(ifctx2, aml_return(aml_int(1)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get user confirmation status for operation */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(8)));
+            {
+                aml_append(ifctx2,
+                           aml_store(aml_derefof(aml_index(aml_arg(3),
+                                                           aml_int(0))),
+                                     aml_local(0)));
+                ifctx3 = aml_if(aml_call1("CKOP", aml_local(0)));
+                {
+                    /* 4 = Allowed and physically present user not required */
+                    aml_append(ifctx3, aml_return(aml_int(4)));
+                }
+                aml_append(ifctx2, ifctx3);
+                /* 0 = requested operation not implemented */
+                aml_append(ifctx2, aml_return(aml_int(0)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            aml_append(ifctx, aml_return(aml_buffer(1, zerobyte)));
+        }
+        aml_append(method, ifctx);
+    }
+    aml_append(dev, method);
+}
+
+static void
  build_dsdt(GArray *table_data, BIOSLinker *linker,
             AcpiPmInfo *pm, AcpiMiscInfo *misc,
             Range *pci_hole, Range *pci_hole64, MachineState *machine)
@@ -2218,6 +2444,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
                   */
                  /* aml_append(crs, aml_irq_no_flags(TPM_TIS_IRQ)); */
                  aml_append(dev, aml_name_decl("_CRS", crs));
+                build_tpm_ppi(dev, misc->tpm_version);
                  aml_append(scope, dev);
              }
 
include/hw/acpi/tpm.h +15 -0
index d9b7452..23f5da1 100644
@@ -37,4 +37,19 @@
  #define TPM_PPI_ADDR_SIZE           0x100
  #define TPM_PPI_ADDR_BASE           0xffff0000
 
+struct tpm_ppi {
+    uint8_t ppin;            /* set by BIOS; currently initialization flag */
+    uint32_t ppip;           /* set by ACPI; not used */
+    uint32_t pprp;           /* response from TPM; set by BIOS */
+    uint32_t pprq;           /* opcode; set by ACPI */
+    uint32_t pprm;           /* parameter for opcode; set by ACPI */
+    uint32_t lppr;           /* last opcode; set by BIOS */
+    uint32_t fret;           /* set by ACPI; not used*/
+    uint32_t res1;           /* reserved */
+    uint32_t res2[4];        /* reserved */
+    uint32_t fail;           /* set by BIOS (0 = success) */
+} QEMU_PACKED;
+
+#define TPM_PPI_STRUCT_SIZE  sizeof(struct tpm_ppi)
+
  #endif /* HW_ACPI_TPM_H */
#2 lore raw

Re: [Qemu-devel] [RFC PATCH 1/3] tpm: Implement virtual memory device for TPM PPI

In reply to Stefan Berger

Hi

Quoted 7 lines · Stefan Berger

Implement a virtual memory device for the TPM physical presence interface. The memory is located at 0xffff0000 and used by ACPI to send messages to the firmware (BIOS).

This device should be used by all TPM interfaces on x86 and can be added through by calling tpm_ppi_init_io().

I haven't followed closely the current design discussion on seabios ML, so this is just a quick review:

Quoted 37 lines
---
 hw/tpm/Makefile.objs  |  2 +-
 hw/tpm/tpm_ppi.c      | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/tpm/tpm_ppi.h      | 25 ++++++++++++++++
 hw/tpm/tpm_tis.c      |  5 ++++
 include/hw/acpi/tpm.h |  6 ++++
 5 files changed, 116 insertions(+), 1 deletion(-)
 create mode 100644 hw/tpm/tpm_ppi.c
 create mode 100644 hw/tpm/tpm_ppi.h
hw/tpm/Makefile.objs +1 -1
index 7a93b24..3eb0558 100644
@@ -1,4 +1,4 @@
-common-obj-y += tpm_util.o
+common-obj-y += tpm_util.o tpm_ppi.o
 common-obj-$(CONFIG_TPM_TIS) += tpm_tis.o
 common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
 common-obj-$(CONFIG_TPM_EMULATOR) += tpm_emulator.o
hw/tpm/tpm_ppi.c new file +10 -0
new file mode 100644
index 0000000..fa8c3c3
@@ -0,0 +1,79 @@
+/*
+ * tpm_ppi.c - TPM Physical Presence Interface
+ *
+ * Copyright (C) 2018 IBM Corporation
+ *
+ * Authors:
+ *  Stefan Berger <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.

hw/tpm/tpm_ppi.c line 20

Quoted 10 lines
hw/tpm/tpm_ppi.c new file +10 -0
+ *
+ */
+
+#include "qemu/osdep.h"
+#include "exec/memory.h"
+#include "exec/address-spaces.h"
+
+#include "tpm_ppi.h"
+
+#define DEBUG_PPI 1

to be switched to 0

Quoted 5 lines
hw/tpm/tpm_ppi.c new file +5 -0
+
+#define DPRINTF(fmt, ...) do { \
+    if (DEBUG_PPI) { \
+        printf(fmt, ## __VA_ARGS__); \
+    } \

hw/tpm/tpm_ppi.c line 35

Quoted 10 lines
hw/tpm/tpm_ppi.c new file +10 -0
+} while (0);
+
+static uint64_t tpm_ppi_mmio_read(void *opaque, hwaddr addr,
+                                  unsigned size)
+{
+    TPMPPI *s = opaque;
+    uint32_t val = 0;
+    int c;
+
+    for (c = size - 1; c >= 0; c--) {

I would rather put the -1,

hw/tpm/tpm_ppi.c line 37

Quoted 2 lines
hw/tpm/tpm_ppi.c new file +2 -0
+        val <<= 8;
+        val |= s->ram[addr + c];

^ here, ymmv

Quoted 8 lines
hw/tpm/tpm_ppi.c new file +8 -0
+    }
+
+    DPRINTF("tpm_ppi: read.%u(%08x) = %08x\n", size,
+            (unsigned int)addr, (unsigned int)val);
+
+    return val;
+}
+

hw/tpm/tpm_ppi.c line 55

Quoted 10 lines
hw/tpm/tpm_ppi.c new file +10 -0
+static void tpm_ppi_mmio_write(void *opaque, hwaddr addr,
+                               uint64_t val, unsigned size)
+{
+    TPMPPI *s = opaque;
+    int c;
+
+    DPRINTF("tpm_ppi: write.%u(%08x) = %08x\n", size,
+            (unsigned int)addr, (unsigned int)val);
+
+    for (c = 0; c <= size - 1; c++) {

same

(alternatively, why not cast the addr pointer?)

hw/tpm/tpm_ppi.c line 64

Quoted 9 lines
hw/tpm/tpm_ppi.c new file +9 -0
+        s->ram[addr + c] = val;
+        val >>= 8;
+    }
+}
+
+static const MemoryRegionOps tpm_ppi_memory_ops = {
+    .read = tpm_ppi_mmio_read,
+    .write = tpm_ppi_mmio_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,

Shouldn't it be native endian?

Quoted 68 lines
hw/tpm/tpm_ppi.c new file +15 -0
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+};
+
+void tpm_ppi_init_io(TPMPPI *tpmppi, Object *obj)
+{
+    memory_region_init_io(&tpmppi->mmio, obj, &tpm_ppi_memory_ops,
+                          tpmppi, "tpm-ppi-mmio",
+                          TPM_PPI_ADDR_SIZE);
+
+    memory_region_add_subregion(get_system_memory(),
+                                TPM_PPI_ADDR_BASE, &tpmppi->mmio);
+}
hw/tpm/tpm_ppi.h new file +25 -0
new file mode 100644
index 0000000..8959912
@@ -0,0 +1,25 @@
+/*
+ * TPM Physical Presence Interface
+ *
+ * Copyright (C) 2018 IBM Corporation
+ *
+ * Authors:
+ *  Stefan Berger    <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#ifndef TPM_TPM_PPI_H
+#define TPM_TPM_PPI_H
+
+#include "hw/acpi/tpm.h"
+
+typedef struct TPMPPI {
+    MemoryRegion mmio;
+
+    uint8_t ram[TPM_PPI_ADDR_SIZE];
+} TPMPPI;
+
+void tpm_ppi_init_io(TPMPPI *tpmppi, Object *obj);
+
+#endif /* TPM_TPM_PPI_H */
hw/tpm/tpm_tis.c +3 -0
index 561384c..f980972 100644
@@ -31,6 +31,7 @@
 #include "sysemu/tpm_backend.h"
 #include "tpm_int.h"
 #include "tpm_util.h"
+#include "tpm_ppi.h"
 
 #define TPM_TIS_NUM_LOCALITIES      5     /* per spec */
 #define TPM_TIS_LOCALITY_SHIFT      12
@@ -80,6 +81,8 @@ typedef struct TPMState {
     TPMVersion be_tpm_version;
 
     size_t be_buffer_size;
+
+    TPMPPI ppi;
 } TPMState;
 
 #define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)

hw/tpm/tpm_tis.c static void tpm_tis_initfn(Object *obj) · line 1057

Quoted 5 lines
hw/tpm/tpm_tis.c +2 -0
@@ -1050,6 +1053,8 @@ static void tpm_tis_initfn(Object *obj)
     memory_region_init_io(&s->mmio, OBJECT(s), &tpm_tis_memory_ops,
                           s, "tpm-tis-mmio",
                           TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
+
+    tpm_ppi_init_io(&s->ppi, obj);

I suggest to pass the device isa address space there (instead of hooking the PPI only on system_memory).

Quoted 22 lines
hw/tpm/tpm_tis.c +0 -0
 }
 
 static void tpm_tis_class_init(ObjectClass *klass, void *data)
include/hw/acpi/tpm.h +6 -1
index 6d516c6..d9b7452 100644
@@ -31,4 +31,10 @@
 
 #define TPM2_START_METHOD_MMIO      6
 
+/*
+ * Physical Presence Interface
+ */
+#define TPM_PPI_ADDR_SIZE           0x100
+#define TPM_PPI_ADDR_BASE           0xffff0000
+
 #endif /* HW_ACPI_TPM_H */
--

2.5.5

Signature · 1 line
Marc-André Lureau
Reviewed-by on v1 2/3
#8 lore raw

Re: [Qemu-devel] [RFC PATCH 2/3] acpi: implement aml_lless_equal

In reply to Stefan Berger

2018-01-10 19:35 GMT+01:00 Stefan Berger <[email protected]>:

Quoted 3 lines

LLessEqualOp = LNotOp LGreaterOp

Quoted 42 lines
---
 hw/acpi/aml-build.c         | 10 ++++++++++
 include/hw/acpi/aml-build.h |  1 +
 2 files changed, 11 insertions(+)
hw/acpi/aml-build.c +10 -0
index 36a6cc4..597a58d 100644
@@ -568,6 +568,16 @@ Aml *aml_lless(Aml *arg1, Aml *arg2)
     return var;
 }
 
+Aml *aml_lless_equal(Aml *arg1, Aml *arg2)
+{
+    /* LLessEqualOp := LNotOp LGreaterOp */
+    Aml *var = aml_opcode(0x92 /* LNotOp */);
+    build_append_byte(var->buf, 0x94 /* LGreaterOp */);
+    aml_append(var, arg1);
+    aml_append(var, arg2);
+    return var;
+}
+
 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAdd */
 Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst)
 {
include/hw/acpi/aml-build.h +1 -1
index 88d0738..c4398cc 100644
@@ -267,6 +267,7 @@ Aml *aml_lor(Aml *arg1, Aml *arg2);
 Aml *aml_shiftleft(Aml *arg1, Aml *count);
 Aml *aml_shiftright(Aml *arg1, Aml *count, Aml *dst);
 Aml *aml_lless(Aml *arg1, Aml *arg2);
+Aml *aml_lless_equal(Aml *arg1, Aml *arg2);
 Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst);
 Aml *aml_subtract(Aml *arg1, Aml *arg2, Aml *dst);
 Aml *aml_increment(Aml *arg);
--

2.5.5

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

Re: [Qemu-devel] [RFC PATCH 3/3] acpi: Build TPM Physical Presence interface

In reply to Stefan Berger

Hi

Quoted 22 lines · Stefan Berger
Quoted 21 lines · Stefan Berger

The TPM Physical Presence interface consists of an ACPI part, a shared memory part, and code in the firmware. Users can send messages to the firmware by writing a code into the shared memory through invoking the ACPI code. When a reboot happens, the firmware looks for the code and acts on it by sending sequences of commands to the TPM.

This patch adds the ACPI code. It is similar to the one in EDK2 but doesn't
assume that SMIs are necessary to use. Besides that it tests the code entered by the user and checks whether it is supported. The range of codes supported matches the range of codes supported in SeaBIOS. It uses the same
datastructure for the shared memory as EDK2 does so that EDK2 and SeaBIOS could both make use of the shared memory.

The underlying TCG specification is accessible from the following page.

https://trustedcomputinggroup.org/tcg-physical-presence-interface-specification/

Quoted 9 lines
Quoted 1 line

This patch implements version 1.20.

The below ACPI code is partly a translation to C from a previous posted patch:

https://lists.gnu.org/archive/html/qemu-devel/2015-05/msg05353.html

And fwiw, this is the acpi dump disassembled:

            Device (ISA.TPM)
            {
                Name (_HID, EisaId ("PNP0C31"))  // _HID: Hardware ID
                Name (_STA, 0x0F)  // _STA: Status
                Name (_CRS, ResourceTemplate ()  // _CRS: Current
Resource Settings
                {
                    Memory32Fixed (ReadWrite,
                        0xFED40000,         // Address Base
                        0x00005000,         // Address Length
                        )
                })
                OperationRegion (TPPI, SystemMemory, 0xFFFF0000, 0x31)
                Field (TPPI, AnyAcc, NoLock, Preserve)
                {
                    PPIN,   8,
                    PPIP,   32,
                    PPRP,   32,
                    PPRQ,   32,
                    PPRM,   32,
                    LPPR,   32,
                    FRET,   32,
                    RES1,   8,
                    RES2,   128,
                    FAIL,   32
                }

                Method (WRAM, 2, Serialized)
                {
                    PPRQ = Arg0
                    PPRM = Arg1
                    Return (Zero)
                }

                Method (CKOP, 1, NotSerialized)
                {
                    If ((Arg0 == Zero))
                    {
                        Return (One)
                    }

                    Return (Zero)
                }

                Method (_DSM, 4, Serialized)  // _DSM: Device-Specific Method
                {
                    If ((Arg0 == ToUUID
("3dddfaa6-361b-4eb4-a424-8d10089d1653") /* Physical Presence
Interface */))
                    {
                        Local0 = ToInteger (Arg2)
                        If ((Local0 == Zero))
                        {
                            Return (Buffer (0x02)
                            {
                                 0xFF, 0x01
           // ..
                            })
                        }

                        If ((Local0 == One))
                        {
                            Return ("1.2")
                        }

                        If ((Local0 == 0x02))
                        {
                            Local0 = DerefOf (Arg3 [Zero])
                            If (CKOP (Local0))
                            {
                                Return (WRAM (Local0, Zero))
                            }

                            Return (One)
                        }

                        If ((Local0 == 0x03))
                        {
                            Return (Package (0x02)
                            {
                                Zero,
                                PPRQ
                            })
                        }

                        If ((Local0 == 0x04))
                        {
                            Return (0x02)
                        }

                        If ((Local0 == 0x05))
                        {
                            Return (Package (0x03)
                            {
                                FAIL,
                                LPPR,
                                PPRP
                            })
                        }

                        If ((Local0 == 0x06))
                        {
                            Return (0x03)
                        }

                        If ((Local0 == 0x07))
                        {
                            Local0 = DerefOf (Arg3 [Zero])
                            If (CKOP (Local0))
                            {
                                Local1 = WRAM (Local0, Zero)
                                Return (Local1)
                            }

                            Return (One)
                        }

                        If ((Local0 == 0x08))
                        {
                            Local0 = DerefOf (Arg3 [Zero])
                            If (CKOP (Local0))
                            {
                                Return (0x04)
                            }

                            Return (Zero)
                        }

                        Return (Buffer (One)
                        {
                             0x00
       // .
                        })
                    }
                }

Linux PPI driver seems to be happy about it, looking at /sys/devices/pnp0/00\:06/tpm/tpm0/ppi/.

I haven't done further testing. Is there anything using this on Linux? tpm2-tools doesn't seem to use it. I suppose I should be able to write something to /sys/devices/pnp0/00\:06/tpm/tpm0/ppi/request.

Any help appreciated :)

Quoted 283 lines

Stefan

Quoted 281 lines
---
  hw/i386/acpi-build.c  | 227
++++++++++++++++++++++++++++++++++++++++++++++++++
  include/hw/acpi/tpm.h |  15 ++++
  2 files changed, 242 insertions(+)
hw/i386/acpi-build.c +210 -0
index 18b939e..e3905a3 100644
@@ -42,6 +42,7 @@
  #include "hw/acpi/memory_hotplug.h"
  #include "sysemu/tpm.h"
  #include "hw/acpi/tpm.h"
+#include "hw/tpm/tpm_ppi.h"
  #include "hw/acpi/vmgenid.h"
  #include "sysemu/tpm_backend.h"
  #include "hw/timer/mc146818rtc_regs.h"
@@ -1860,6 +1861,231 @@ static Aml *build_q35_osc_method(void)
  }
 
  static void
+build_tpm_ppi(Aml *dev, TPMVersion tpm_version)
+{
+    Aml *method, *field, *ifctx, *ifctx2, *ifctx3, *pak;
+
+    aml_append(dev,
+               aml_operation_region("TPPI", AML_SYSTEM_MEMORY,
+                                    aml_int(TPM_PPI_ADDR_BASE),
+                                    TPM_PPI_STRUCT_SIZE));
+
+    field = aml_field("TPPI", AML_ANY_ACC, AML_NOLOCK, AML_PRESERVE);
+    aml_append(field, aml_named_field("PPIN",
+               sizeof(uint8_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPIP",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPRP",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPRQ",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPRM",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("LPPR",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("FRET",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("RES1",
+               sizeof(uint8_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("RES2",
+               sizeof(uint32_t) * BITS_PER_BYTE * 4));
+    aml_append(field, aml_named_field("FAIL",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(dev, field);
+
+    /*
+     * Write the given operations code into 'PPRQ'.
+     */
+    method = aml_method("WRAM", 2, AML_SERIALIZED);
+    {
+        aml_append(method, aml_store(aml_arg(0), aml_name("PPRQ")));
+        aml_append(method, aml_store(aml_arg(1), aml_name("PPRM")));
+        /* 0 = Success */
+        aml_append(method, aml_return(aml_int(0)));
+    }
+    aml_append(dev, method);
+
+    /*
+     * CKOP: Check whether the opcode is valid
+     */
+    if (tpm_version == TPM_VERSION_1_2) {
+        method = aml_method("CKOP", 1, AML_NOTSERIALIZED);
+        {
+            ifctx = aml_if(
+                      aml_or(
+                        aml_or(
+                          aml_and(
+                            aml_lgreater_equal(aml_arg(0), aml_int(0)),
+                            aml_lless_equal(aml_arg(0), aml_int(11)),
+                            NULL
+                          ),
+                          aml_equal(aml_arg(0), aml_int(14)),
+                          NULL
+                        ),
+                        aml_and(
+                          aml_lgreater_equal(aml_arg(0), aml_int(21)),
+                          aml_lless_equal(aml_arg(0), aml_int(22)),
+                          NULL
+                       ),
+                       NULL
+                      )
+                    );
+            {
+                aml_append(ifctx, aml_return(aml_int(1)));
+            }
+            aml_append(method, ifctx);
+
+            aml_append(method, aml_return(aml_int(0)));
+        }
+    } else {
+        method = aml_method("CKOP", 1, AML_NOTSERIALIZED);
+        {
+            ifctx = aml_if(
+                      aml_equal(aml_arg(0), aml_int(0))
+                    );
+            {
+                aml_append(ifctx, aml_return(aml_int(1)));
+            }
+            aml_append(method, ifctx);
+
+            aml_append(method, aml_return(aml_int(0)));
+        }
+    }
+    aml_append(dev, method);
+
+    method = aml_method("_DSM", 4, AML_SERIALIZED);
+    {
+        uint8_t zerobyte[1] = { 0 };
+
+        ifctx = aml_if(
+                  aml_equal(aml_arg(0),
+
 aml_touuid("3DDDFAA6-361B-4EB4-A424-8D10089D1653"))
+                );
+        {
+            aml_append(ifctx,
+                       aml_store(aml_to_integer(aml_arg(2)),
 aml_local(0)));
+
+            /* standard DSM query function */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(0)));
+            {
+                uint8_t byte_list[2] = { 0xff, 0x01 };
+                aml_append(ifctx2, aml_return(aml_buffer(2, byte_list)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* interface version: 1.2 */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(1)));
+            {
+                aml_append(ifctx2, aml_return(aml_string("1.2")));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* submit TPM operation */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(2)));
+            {
+                aml_append(ifctx2,
+                           aml_store(aml_derefof(aml_index(aml_arg(3),
+                                                           aml_int(0))),
+                                     aml_local(0)));
+                ifctx3 = aml_if(aml_call1("CKOP", aml_local(0)));
+                {
+                    aml_append(ifctx3,
+                               aml_return(aml_call2("WRAM",
+                                                    aml_local(0),
+                                                    aml_int(0))));
+                }
+                aml_append(ifctx2, ifctx3);
+                aml_append(ifctx2, aml_return(aml_int(1)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get pending TPM operation */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(3)));
+            {
+                pak = aml_package(2);
+                aml_append(pak, aml_int(0));
+                aml_append(pak, aml_name("PPRQ"));
+                aml_append(ifctx2, aml_return(pak));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get platform-specific action to transition to pre-OS env.
 */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(4)));
+            {
+                /* 2 = reboot */
+                aml_append(ifctx2, aml_return(aml_int(2)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get TPM operation response */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(5)));
+            {
+                pak = aml_package(3);
+
+                aml_append(pak, aml_name("FAIL"));
+                aml_append(pak, aml_name("LPPR"));
+                aml_append(pak, aml_name("PPRP"));
+
+                aml_append(ifctx2, aml_return(pak));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* submit preferred user language */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(6)));
+            {
+                /* 3 = not implemented */
+                aml_append(ifctx2, aml_return(aml_int(3)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* submit TPM operation v2 */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(7)));
+            {
+                aml_append(ifctx2,
+                           aml_store(aml_derefof(aml_index(aml_arg(3),
+                                                           aml_int(0))),
+                                     aml_local(0)));
+                ifctx3 = aml_if(aml_call1("CKOP", aml_local(0)));
+                {
+                    aml_append(ifctx3,
+                               aml_store(aml_call2("WRAM",
+                                                   aml_local(0),
+                                                   aml_int(0)),
+                                         aml_local(1)));
+                    aml_append(ifctx3, aml_return(aml_local(1)));
+                }
+                aml_append(ifctx2, ifctx3);
+                /* 1 = requested operation not implemented */
+                aml_append(ifctx2, aml_return(aml_int(1)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get user confirmation status for operation */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(8)));
+            {
+                aml_append(ifctx2,
+                           aml_store(aml_derefof(aml_index(aml_arg(3),
+                                                           aml_int(0))),
+                                     aml_local(0)));
+                ifctx3 = aml_if(aml_call1("CKOP", aml_local(0)));
+                {
+                    /* 4 = Allowed and physically present user not

required */

hw/i386/acpi-build.c +17 -0
+                    aml_append(ifctx3, aml_return(aml_int(4)));
+                }
+                aml_append(ifctx2, ifctx3);
+                /* 0 = requested operation not implemented */
+                aml_append(ifctx2, aml_return(aml_int(0)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            aml_append(ifctx, aml_return(aml_buffer(1, zerobyte)));
+        }
+        aml_append(method, ifctx);
+    }
+    aml_append(dev, method);
+}
+
+static void
  build_dsdt(GArray *table_data, BIOSLinker *linker,
             AcpiPmInfo *pm, AcpiMiscInfo *misc,
             Range *pci_hole, Range *pci_hole64, MachineState *machine)
@@ -2218,6 +2444,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
                   */
                  /* aml_append(crs, aml_irq_no_flags(TPM_TIS_IRQ)); */
                  aml_append(dev, aml_name_decl("_CRS", crs));
+                build_tpm_ppi(dev, misc->tpm_version);
                  aml_append(scope, dev);
              }
 
include/hw/acpi/tpm.h +2 -0
index d9b7452..23f5da1 100644
@@ -37,4 +37,19 @@
  #define TPM_PPI_ADDR_SIZE           0x100
  #define TPM_PPI_ADDR_BASE           0xffff0000
 
+struct tpm_ppi {
+    uint8_t ppin;            /* set by BIOS; currently initialization

flag */

include/hw/acpi/tpm.h +6 -0
+    uint32_t ppip;           /* set by ACPI; not used */
+    uint32_t pprp;           /* response from TPM; set by BIOS */
+    uint32_t pprq;           /* opcode; set by ACPI */
+    uint32_t pprm;           /* parameter for opcode; set by ACPI */
+    uint32_t lppr;           /* last opcode; set by BIOS */
+    uint32_t fret;           /* set by ACPI; not used*/
Quoted 12 lines

include/hw/acpi/tpm.h line 55

Quoted 8 lines
include/hw/acpi/tpm.h +7 -0
+    uint32_t res1;           /* reserved */
+    uint32_t res2[4];        /* reserved */
+    uint32_t fail;           /* set by BIOS (0 = success) */
+} QEMU_PACKED;
+
+#define TPM_PPI_STRUCT_SIZE  sizeof(struct tpm_ppi)
+
  #endif /* HW_ACPI_TPM_H */

otherwise, patch looks good.

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

Re: [Qemu-devel] [RFC PATCH 1/3] tpm: Implement virtual memory device for TPM PPI

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

Hi

Quoted 9 lines · Stefan Berger

Implement a virtual memory device for the TPM physical presence interface. The memory is located at 0xffff0000 and used by ACPI to send messages to the firmware (BIOS).

This device should be used by all TPM interfaces on x86 and can be added through by calling tpm_ppi_init_io().

+#define DEBUG_PPI 1

Quoted 8 lines

to be switched to 0

hw/tpm/tpm_ppi.c line 26

Quoted 6 lines
hw/tpm/tpm_ppi.c new file +6 -0
+
+#define DPRINTF(fmt, ...) do { \
+    if (DEBUG_PPI) { \
+        printf(fmt, ## __VA_ARGS__); \
+    } \
+} while (0);

Also, this falls foul of my pending patch to forbid trailing semicolons in do/while(0) macros:
https://lists.gnu.org/archive/html/qemu-devel/2017-12/msg00246.html

But even though it is not a trace point, at least you made sure -Wformat will avoid bit-rotting debug statements.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#14 lore raw

Re: [Qemu-devel] [RFC PATCH 3/3] acpi: Build TPM Physical Presence interface

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

Hi

Quoted 19 lines · Stefan Berger
Quoted 18 lines · Stefan Berger

The TPM Physical Presence interface consists of an ACPI part, a shared memory part, and code in the firmware. Users can send messages to the firmware by writing a code into the shared memory through invoking the ACPI code. When a reboot happens, the firmware looks for the code and acts on it by sending sequences of commands to the TPM.

This patch adds the ACPI code. It is similar to the one in EDK2 but doesn't
assume that SMIs are necessary to use. Besides that it tests the code entered by the user and checks whether it is supported. The range of codes supported matches the range of codes supported in SeaBIOS. It uses the same
datastructure for the shared memory as EDK2 does so that EDK2 and SeaBIOS could both make use of the shared memory.

The underlying TCG specification is accessible from the following page.

Quoted 10 lines
Quoted 3 lines

https://trustedcomputinggroup.org/tcg-physical-presence-interface-specification/

This patch implements version 1.20.

The below ACPI code is partly a translation to C from a previous posted patch:

https://lists.gnu.org/archive/html/qemu-devel/2015-05/msg05353.html

And fwiw, this is the acpi dump disassembled:

             Device (ISA.TPM)
             {
                 Name (_HID, EisaId ("PNP0C31"))  // _HID: Hardware ID
                 Name (_STA, 0x0F)  // _STA: Status
                 Name (_CRS, ResourceTemplate ()  // _CRS: Current
Resource Settings
                 {
                     Memory32Fixed (ReadWrite,
                         0xFED40000,         // Address Base
                         0x00005000,         // Address Length
                         )
                 })
                 OperationRegion (TPPI, SystemMemory, 0xFFFF0000, 0x31)
                 Field (TPPI, AnyAcc, NoLock, Preserve)
                 {
                     PPIN,   8,
                     PPIP,   32,
                     PPRP,   32,
                     PPRQ,   32,
                     PPRM,   32,
                     LPPR,   32,
                     FRET,   32,
                     RES1,   8,
                     RES2,   128,
                     FAIL,   32
                 }

                 Method (WRAM, 2, Serialized)
                 {
                     PPRQ = Arg0
                     PPRM = Arg1
                     Return (Zero)
                 }

                 Method (CKOP, 1, NotSerialized)
                 {
                     If ((Arg0 == Zero))
                     {
                         Return (One)
                     }

                     Return (Zero)
                 }

                 Method (_DSM, 4, Serialized)  // _DSM: Device-Specific Method
                 {
                     If ((Arg0 == ToUUID
("3dddfaa6-361b-4eb4-a424-8d10089d1653") /* Physical Presence
Interface */))
                     {
                         Local0 = ToInteger (Arg2)
                         If ((Local0 == Zero))
                         {
                             Return (Buffer (0x02)
                             {
                                  0xFF, 0x01
            // ..
                             })
                         }

                         If ((Local0 == One))
                         {
                             Return ("1.2")
                         }

                         If ((Local0 == 0x02))
                         {
                             Local0 = DerefOf (Arg3 [Zero])
                             If (CKOP (Local0))
                             {
                                 Return (WRAM (Local0, Zero))
                             }

                             Return (One)
                         }

                         If ((Local0 == 0x03))
                         {
                             Return (Package (0x02)
                             {
                                 Zero,
                                 PPRQ
                             })
                         }

                         If ((Local0 == 0x04))
                         {
                             Return (0x02)
                         }

                         If ((Local0 == 0x05))
                         {
                             Return (Package (0x03)
                             {
                                 FAIL,
                                 LPPR,
                                 PPRP
                             })
                         }

                         If ((Local0 == 0x06))
                         {
                             Return (0x03)
                         }

                         If ((Local0 == 0x07))
                         {
                             Local0 = DerefOf (Arg3 [Zero])
                             If (CKOP (Local0))
                             {
                                 Local1 = WRAM (Local0, Zero)
                                 Return (Local1)
                             }

                             Return (One)
                         }

                         If ((Local0 == 0x08))
                         {
                             Local0 = DerefOf (Arg3 [Zero])
                             If (CKOP (Local0))
                             {
                                 Return (0x04)
                             }

                             Return (Zero)
                         }

                         Return (Buffer (One)
                         {
                              0x00
        // .
                         })
                     }
                 }
Quoted 8 lines

Linux PPI driver seems to be happy about it, looking at /sys/devices/pnp0/00\:06/tpm/tpm0/ppi/.

I haven't done further testing. Is there anything using this on Linux? tpm2-tools doesn't seem to use it. I suppose I should be able to write something to /sys/devices/pnp0/00\:06/tpm/tpm0/ppi/request.

Any help appreciated :)

To deacativate and disable the TPM 1.2 (!) write a '7' into the request.

echo 7 > request
cat ../enabled ../active # should show two 1s indicating that it's currently enabled and active cat request # should show 7 now

Do a reboot. Linux may complain that it cannot extend a PCR. This is expected when the TPM is deactivated and disabled.

To enable and activate it after reboot do:
echo 6 > request
cat request # should show 6 now
cat response # should show that 7 succeeded
cat ../enable ../active # should show two zeros

reboot

Now the TPM 1.2 is enabled and active again, no more error message from Linux should occur.

cat ../enabled ../active # should show two 1s
cat response # should show that 6 succeeded

PPI isn't used much probably. I mainly want the device for suspending the state of it along with the TIS state. I suppose that once we do that it 'nails the coffin' on the device state we write out and future extensions of fields to write out only cause issues later. So better have it now.

Stefan

Quoted 296 lines
Quoted 283 lines

Stefan

Quoted 280 lines
---
   hw/i386/acpi-build.c  | 227
++++++++++++++++++++++++++++++++++++++++++++++++++
   include/hw/acpi/tpm.h |  15 ++++
   2 files changed, 242 insertions(+)
hw/i386/acpi-build.c +210 -0
index 18b939e..e3905a3 100644
@@ -42,6 +42,7 @@
   #include "hw/acpi/memory_hotplug.h"
   #include "sysemu/tpm.h"
   #include "hw/acpi/tpm.h"
+#include "hw/tpm/tpm_ppi.h"
   #include "hw/acpi/vmgenid.h"
   #include "sysemu/tpm_backend.h"
   #include "hw/timer/mc146818rtc_regs.h"
@@ -1860,6 +1861,231 @@ static Aml *build_q35_osc_method(void)
   }
 
   static void
+build_tpm_ppi(Aml *dev, TPMVersion tpm_version)
+{
+    Aml *method, *field, *ifctx, *ifctx2, *ifctx3, *pak;
+
+    aml_append(dev,
+               aml_operation_region("TPPI", AML_SYSTEM_MEMORY,
+                                    aml_int(TPM_PPI_ADDR_BASE),
+                                    TPM_PPI_STRUCT_SIZE));
+
+    field = aml_field("TPPI", AML_ANY_ACC, AML_NOLOCK, AML_PRESERVE);
+    aml_append(field, aml_named_field("PPIN",
+               sizeof(uint8_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPIP",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPRP",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPRQ",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPRM",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("LPPR",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("FRET",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("RES1",
+               sizeof(uint8_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("RES2",
+               sizeof(uint32_t) * BITS_PER_BYTE * 4));
+    aml_append(field, aml_named_field("FAIL",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(dev, field);
+
+    /*
+     * Write the given operations code into 'PPRQ'.
+     */
+    method = aml_method("WRAM", 2, AML_SERIALIZED);
+    {
+        aml_append(method, aml_store(aml_arg(0), aml_name("PPRQ")));
+        aml_append(method, aml_store(aml_arg(1), aml_name("PPRM")));
+        /* 0 = Success */
+        aml_append(method, aml_return(aml_int(0)));
+    }
+    aml_append(dev, method);
+
+    /*
+     * CKOP: Check whether the opcode is valid
+     */
+    if (tpm_version == TPM_VERSION_1_2) {
+        method = aml_method("CKOP", 1, AML_NOTSERIALIZED);
+        {
+            ifctx = aml_if(
+                      aml_or(
+                        aml_or(
+                          aml_and(
+                            aml_lgreater_equal(aml_arg(0), aml_int(0)),
+                            aml_lless_equal(aml_arg(0), aml_int(11)),
+                            NULL
+                          ),
+                          aml_equal(aml_arg(0), aml_int(14)),
+                          NULL
+                        ),
+                        aml_and(
+                          aml_lgreater_equal(aml_arg(0), aml_int(21)),
+                          aml_lless_equal(aml_arg(0), aml_int(22)),
+                          NULL
+                       ),
+                       NULL
+                      )
+                    );
+            {
+                aml_append(ifctx, aml_return(aml_int(1)));
+            }
+            aml_append(method, ifctx);
+
+            aml_append(method, aml_return(aml_int(0)));
+        }
+    } else {
+        method = aml_method("CKOP", 1, AML_NOTSERIALIZED);
+        {
+            ifctx = aml_if(
+                      aml_equal(aml_arg(0), aml_int(0))
+                    );
+            {
+                aml_append(ifctx, aml_return(aml_int(1)));
+            }
+            aml_append(method, ifctx);
+
+            aml_append(method, aml_return(aml_int(0)));
+        }
+    }
+    aml_append(dev, method);
+
+    method = aml_method("_DSM", 4, AML_SERIALIZED);
+    {
+        uint8_t zerobyte[1] = { 0 };
+
+        ifctx = aml_if(
+                  aml_equal(aml_arg(0),
+
 aml_touuid("3DDDFAA6-361B-4EB4-A424-8D10089D1653"))
+                );
+        {
+            aml_append(ifctx,
+                       aml_store(aml_to_integer(aml_arg(2)),
 aml_local(0)));
+
+            /* standard DSM query function */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(0)));
+            {
+                uint8_t byte_list[2] = { 0xff, 0x01 };
+                aml_append(ifctx2, aml_return(aml_buffer(2, byte_list)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* interface version: 1.2 */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(1)));
+            {
+                aml_append(ifctx2, aml_return(aml_string("1.2")));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* submit TPM operation */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(2)));
+            {
+                aml_append(ifctx2,
+                           aml_store(aml_derefof(aml_index(aml_arg(3),
+                                                           aml_int(0))),
+                                     aml_local(0)));
+                ifctx3 = aml_if(aml_call1("CKOP", aml_local(0)));
+                {
+                    aml_append(ifctx3,
+                               aml_return(aml_call2("WRAM",
+                                                    aml_local(0),
+                                                    aml_int(0))));
+                }
+                aml_append(ifctx2, ifctx3);
+                aml_append(ifctx2, aml_return(aml_int(1)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get pending TPM operation */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(3)));
+            {
+                pak = aml_package(2);
+                aml_append(pak, aml_int(0));
+                aml_append(pak, aml_name("PPRQ"));
+                aml_append(ifctx2, aml_return(pak));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get platform-specific action to transition to pre-OS env.
 */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(4)));
+            {
+                /* 2 = reboot */
+                aml_append(ifctx2, aml_return(aml_int(2)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get TPM operation response */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(5)));
+            {
+                pak = aml_package(3);
+
+                aml_append(pak, aml_name("FAIL"));
+                aml_append(pak, aml_name("LPPR"));
+                aml_append(pak, aml_name("PPRP"));
+
+                aml_append(ifctx2, aml_return(pak));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* submit preferred user language */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(6)));
+            {
+                /* 3 = not implemented */
+                aml_append(ifctx2, aml_return(aml_int(3)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* submit TPM operation v2 */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(7)));
+            {
+                aml_append(ifctx2,
+                           aml_store(aml_derefof(aml_index(aml_arg(3),
+                                                           aml_int(0))),
+                                     aml_local(0)));
+                ifctx3 = aml_if(aml_call1("CKOP", aml_local(0)));
+                {
+                    aml_append(ifctx3,
+                               aml_store(aml_call2("WRAM",
+                                                   aml_local(0),
+                                                   aml_int(0)),
+                                         aml_local(1)));
+                    aml_append(ifctx3, aml_return(aml_local(1)));
+                }
+                aml_append(ifctx2, ifctx3);
+                /* 1 = requested operation not implemented */
+                aml_append(ifctx2, aml_return(aml_int(1)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get user confirmation status for operation */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(8)));
+            {
+                aml_append(ifctx2,
+                           aml_store(aml_derefof(aml_index(aml_arg(3),
+                                                           aml_int(0))),
+                                     aml_local(0)));
+                ifctx3 = aml_if(aml_call1("CKOP", aml_local(0)));
+                {
+                    /* 4 = Allowed and physically present user not

required */

hw/i386/acpi-build.c +17 -0
+                    aml_append(ifctx3, aml_return(aml_int(4)));
+                }
+                aml_append(ifctx2, ifctx3);
+                /* 0 = requested operation not implemented */
+                aml_append(ifctx2, aml_return(aml_int(0)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            aml_append(ifctx, aml_return(aml_buffer(1, zerobyte)));
+        }
+        aml_append(method, ifctx);
+    }
+    aml_append(dev, method);
+}
+
+static void
   build_dsdt(GArray *table_data, BIOSLinker *linker,
              AcpiPmInfo *pm, AcpiMiscInfo *misc,
              Range *pci_hole, Range *pci_hole64, MachineState *machine)
@@ -2218,6 +2444,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
                    */
                   /* aml_append(crs, aml_irq_no_flags(TPM_TIS_IRQ)); */
                   aml_append(dev, aml_name_decl("_CRS", crs));
+                build_tpm_ppi(dev, misc->tpm_version);
                   aml_append(scope, dev);
               }
 
include/hw/acpi/tpm.h +2 -0
index d9b7452..23f5da1 100644
@@ -37,4 +37,19 @@
   #define TPM_PPI_ADDR_SIZE           0x100
   #define TPM_PPI_ADDR_BASE           0xffff0000
 
+struct tpm_ppi {
+    uint8_t ppin;            /* set by BIOS; currently initialization

flag */

include/hw/acpi/tpm.h +6 -0
+    uint32_t ppip;           /* set by ACPI; not used */
+    uint32_t pprp;           /* response from TPM; set by BIOS */
+    uint32_t pprq;           /* opcode; set by ACPI */
+    uint32_t pprm;           /* parameter for opcode; set by ACPI */
+    uint32_t lppr;           /* last opcode; set by BIOS */
+    uint32_t fret;           /* set by ACPI; not used*/
Quoted 11 lines

include/hw/acpi/tpm.h line 55

Quoted 8 lines
include/hw/acpi/tpm.h +7 -0
+    uint32_t res1;           /* reserved */
+    uint32_t res2[4];        /* reserved */
+    uint32_t fail;           /* set by BIOS (0 = success) */
+} QEMU_PACKED;
+
+#define TPM_PPI_STRUCT_SIZE  sizeof(struct tpm_ppi)
+
   #endif /* HW_ACPI_TPM_H */

otherwise, patch looks good.

#4 lore raw

Re: [Qemu-devel] [RFC PATCH 1/3] tpm: Implement virtual memory device for TPM PPI

In reply to Eric Blake
Quoted 25 lines · Eric Blake
Quoted 12 lines · Marc-André Lureau

Hi

Quoted 8 lines · Stefan Berger

Implement a virtual memory device for the TPM physical presence interface. The memory is located at 0xffff0000 and used by ACPI to send messages to the firmware (BIOS).

This device should be used by all TPM interfaces on x86 and can be added through by calling tpm_ppi_init_io().

+#define DEBUG_PPI 1

Quoted 8 lines

to be switched to 0

hw/tpm/tpm_ppi.c line 26

Quoted 6 lines
hw/tpm/tpm_ppi.c new file +6 -0
+
+#define DPRINTF(fmt, ...) do { \
+    if (DEBUG_PPI) { \
+        printf(fmt, ## __VA_ARGS__); \
+    } \
+} while (0);

Also, this falls foul of my pending patch to forbid trailing semicolons in do/while(0) macros:
https://lists.gnu.org/archive/html/qemu-devel/2017-12/msg00246.html

Quoted 3 lines

But even though it is not a trace point, at least you made sure -Wformat will avoid bit-rotting debug statements.

Fixing it.

#5 lore raw

Re: [Qemu-devel] [RFC PATCH 1/3] tpm: Implement virtual memory device for TPM PPI

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

Hi

Quoted 4 lines · Stefan Berger

Implement a virtual memory device for the TPM physical presence interface. The memory is located at 0xffff0000 and used by ACPI to send messages to the firmware (BIOS).

Quoted 5 lines
Quoted 3 lines

This device should be used by all TPM interfaces on x86 and can be added through by calling tpm_ppi_init_io().

I haven't followed closely the current design discussion on seabios ML, so this is just a quick review:

I think what I should add is an ACPI table called QEMU that holds the address of this device so that we can move this device later on. SeaBIOS would find this table and use the address it finds there rather than the #define.

Quoted 41 lines
Quoted 41 lines
---
  hw/tpm/Makefile.objs  |  2 +-
  hw/tpm/tpm_ppi.c      | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++
  hw/tpm/tpm_ppi.h      | 25 ++++++++++++++++
  hw/tpm/tpm_tis.c      |  5 ++++
  include/hw/acpi/tpm.h |  6 ++++
  5 files changed, 116 insertions(+), 1 deletion(-)

create mode 100644 hw/tpm/tpm_ppi.c
create mode 100644 hw/tpm/tpm_ppi.h

hw/tpm/Makefile.objs +1 -1
index 7a93b24..3eb0558 100644
@@ -1,4 +1,4 @@
-common-obj-y += tpm_util.o
+common-obj-y += tpm_util.o tpm_ppi.o
  common-obj-$(CONFIG_TPM_TIS) += tpm_tis.o
  common-obj-$(CONFIG_TPM_PASSTHROUGH) += tpm_passthrough.o
  common-obj-$(CONFIG_TPM_EMULATOR) += tpm_emulator.o
hw/tpm/tpm_ppi.c new file +13 -0
new file mode 100644
index 0000000..fa8c3c3
@@ -0,0 +1,79 @@
+/*
+ * tpm_ppi.c - TPM Physical Presence Interface
+ *
+ * Copyright (C) 2018 IBM Corporation
+ *
+ * Authors:
+ *  Stefan Berger <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
Quoted 8 lines

hw/tpm/tpm_ppi.c line 20

Quoted 7 lines
hw/tpm/tpm_ppi.c new file +7 -0
+#include "qemu/osdep.h"
+#include "exec/memory.h"
+#include "exec/address-spaces.h"
+
+#include "tpm_ppi.h"
+
+#define DEBUG_PPI 1

to be switched to 0

Done.

Quoted 18 lines
Quoted 6 lines
hw/tpm/tpm_ppi.c new file +5 -0
+
+#define DPRINTF(fmt, ...) do { \
+    if (DEBUG_PPI) { \
+        printf(fmt, ## __VA_ARGS__); \
+    } \

hw/tpm/tpm_ppi.c line 35

Quoted 10 lines
hw/tpm/tpm_ppi.c new file +10 -0
+} while (0);
+
+static uint64_t tpm_ppi_mmio_read(void *opaque, hwaddr addr,
+                                  unsigned size)
+{
+    TPMPPI *s = opaque;
+    uint32_t val = 0;
+    int c;
+
+    for (c = size - 1; c >= 0; c--) {

I would rather put the -1,

Quoted 3 lines

hw/tpm/tpm_ppi.c line 37

Quoted 2 lines
hw/tpm/tpm_ppi.c new file +2 -0
+        val <<= 8;
+        val |= s->ram[addr + c];

^ here, ymmv

you mean val |= s->ram[addr + c - 1]?

Quoted 21 lines
Quoted 9 lines
hw/tpm/tpm_ppi.c new file +8 -0
+    }
+
+    DPRINTF("tpm_ppi: read.%u(%08x) = %08x\n", size,
+            (unsigned int)addr, (unsigned int)val);
+
+    return val;
+}
+

hw/tpm/tpm_ppi.c line 55

Quoted 10 lines
hw/tpm/tpm_ppi.c new file +10 -0
+static void tpm_ppi_mmio_write(void *opaque, hwaddr addr,
+                               uint64_t val, unsigned size)
+{
+    TPMPPI *s = opaque;
+    int c;
+
+    DPRINTF("tpm_ppi: write.%u(%08x) = %08x\n", size,
+            (unsigned int)addr, (unsigned int)val);
+
+    for (c = 0; c <= size - 1; c++) {

same

Quoted 1 line

(alternatively, why not cast the addr pointer?)

You mean write 16 bits and 32bits directly into the array? I have to see what happens when one writes beyond the end of the array...

Quoted 11 lines

hw/tpm/tpm_ppi.c line 64

Quoted 10 lines
hw/tpm/tpm_ppi.c new file +9 -0
+        s->ram[addr + c] = val;
+        val >>= 8;
+    }
+}
+
+static const MemoryRegionOps tpm_ppi_memory_ops = {
+    .read = tpm_ppi_mmio_read,
+    .write = tpm_ppi_mmio_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,

Shouldn't it be native endian?

Have to figure out what that means...

Quoted 66 lines
Quoted 66 lines
hw/tpm/tpm_ppi.c new file +15 -0
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+};
+
+void tpm_ppi_init_io(TPMPPI *tpmppi, Object *obj)
+{
+    memory_region_init_io(&tpmppi->mmio, obj, &tpm_ppi_memory_ops,
+                          tpmppi, "tpm-ppi-mmio",
+                          TPM_PPI_ADDR_SIZE);
+
+    memory_region_add_subregion(get_system_memory(),
+                                TPM_PPI_ADDR_BASE, &tpmppi->mmio);
+}
hw/tpm/tpm_ppi.h new file +25 -0
new file mode 100644
index 0000000..8959912
@@ -0,0 +1,25 @@
+/*
+ * TPM Physical Presence Interface
+ *
+ * Copyright (C) 2018 IBM Corporation
+ *
+ * Authors:
+ *  Stefan Berger    <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#ifndef TPM_TPM_PPI_H
+#define TPM_TPM_PPI_H
+
+#include "hw/acpi/tpm.h"
+
+typedef struct TPMPPI {
+    MemoryRegion mmio;
+
+    uint8_t ram[TPM_PPI_ADDR_SIZE];
+} TPMPPI;
+
+void tpm_ppi_init_io(TPMPPI *tpmppi, Object *obj);
+
+#endif /* TPM_TPM_PPI_H */
hw/tpm/tpm_tis.c +3 -0
index 561384c..f980972 100644
@@ -31,6 +31,7 @@
  #include "sysemu/tpm_backend.h"
  #include "tpm_int.h"
  #include "tpm_util.h"
+#include "tpm_ppi.h"
 
  #define TPM_TIS_NUM_LOCALITIES      5     /* per spec */
  #define TPM_TIS_LOCALITY_SHIFT      12
@@ -80,6 +81,8 @@ typedef struct TPMState {
      TPMVersion be_tpm_version;
 
      size_t be_buffer_size;
+
+    TPMPPI ppi;
  } TPMState;
 
Quoted 9 lines

hw/tpm/tpm_tis.c static void tpm_tis_initfn(Object *obj) · line 1057

Quoted 7 lines
hw/tpm/tpm_tis.c +2 -0
  #define TPM(obj) OBJECT_CHECK(TPMState, (obj), TYPE_TPM_TIS)
@@ -1050,6 +1053,8 @@ static void tpm_tis_initfn(Object *obj)
      memory_region_init_io(&s->mmio, OBJECT(s), &tpm_tis_memory_ops,
                            s, "tpm-tis-mmio",
                            TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT);
+
+    tpm_ppi_init_io(&s->ppi, obj);

I suggest to pass the device isa address space there (instead of hooking the PPI only on system_memory).

I can do that. For my education, I wonder why, though.

Quoted 25 lines
Quoted 23 lines
hw/tpm/tpm_tis.c +0 -0
  }
 
  static void tpm_tis_class_init(ObjectClass *klass, void *data)
include/hw/acpi/tpm.h +6 -1
index 6d516c6..d9b7452 100644
@@ -31,4 +31,10 @@
 
  #define TPM2_START_METHOD_MMIO      6
 
+/*
+ * Physical Presence Interface
+ */
+#define TPM_PPI_ADDR_SIZE           0x100
+#define TPM_PPI_ADDR_BASE           0xffff0000
+
  #endif /* HW_ACPI_TPM_H */
--

2.5.5

#9 lore raw

Re: [Qemu-devel] [RFC PATCH 2/3] acpi: implement aml_lless_equal

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

2018-01-10 19:35 GMT+01:00 Stefan Berger <[email protected]>:

Quoted 3 lines

LLessEqualOp = LNotOp LGreaterOp

Thanks. I added a comment line above the function now:

/* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLLessEqual */

Quoted 46 lines
Quoted 44 lines

---
  hw/acpi/aml-build.c         | 10 ++++++++++
  include/hw/acpi/aml-build.h |  1 +
  2 files changed, 11 insertions(+)
hw/acpi/aml-build.c +10 -0
index 36a6cc4..597a58d 100644
@@ -568,6 +568,16 @@ Aml *aml_lless(Aml *arg1, Aml *arg2)
      return var;
  }
 
+Aml *aml_lless_equal(Aml *arg1, Aml *arg2)
+{
+    /* LLessEqualOp := LNotOp LGreaterOp */
+    Aml *var = aml_opcode(0x92 /* LNotOp */);
+    build_append_byte(var->buf, 0x94 /* LGreaterOp */);
+    aml_append(var, arg1);
+    aml_append(var, arg2);
+    return var;
+}
+
  /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAdd */
  Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst)
  {
include/hw/acpi/aml-build.h +1 -1
index 88d0738..c4398cc 100644
@@ -267,6 +267,7 @@ Aml *aml_lor(Aml *arg1, Aml *arg2);
  Aml *aml_shiftleft(Aml *arg1, Aml *count);
  Aml *aml_shiftright(Aml *arg1, Aml *count, Aml *dst);
  Aml *aml_lless(Aml *arg1, Aml *arg2);
+Aml *aml_lless_equal(Aml *arg1, Aml *arg2);
  Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst);
  Aml *aml_subtract(Aml *arg1, Aml *arg2, Aml *dst);
  Aml *aml_increment(Aml *arg);
--

2.5.5

#15 lore raw

Re: [Qemu-devel] [RFC PATCH 3/3] acpi: Build TPM Physical Presence interface

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

Hi

Quoted 19 lines · Stefan Berger
Quoted 18 lines · Stefan Berger

The TPM Physical Presence interface consists of an ACPI part, a shared memory part, and code in the firmware. Users can send messages to the firmware by writing a code into the shared memory through invoking the ACPI code. When a reboot happens, the firmware looks for the code and acts on it by sending sequences of commands to the TPM.

This patch adds the ACPI code. It is similar to the one in EDK2 but doesn't
assume that SMIs are necessary to use. Besides that it tests the code entered by the user and checks whether it is supported. The range of codes supported matches the range of codes supported in SeaBIOS. It uses the same
datastructure for the shared memory as EDK2 does so that EDK2 and SeaBIOS could both make use of the shared memory.

The underlying TCG specification is accessible from the following page.

Quoted 10 lines
Quoted 3 lines

https://trustedcomputinggroup.org/tcg-physical-presence-interface-specification/

This patch implements version 1.20.

The below ACPI code is partly a translation to C from a previous posted patch:

https://lists.gnu.org/archive/html/qemu-devel/2015-05/msg05353.html

And fwiw, this is the acpi dump disassembled:

             Device (ISA.TPM)
             {
                 Name (_HID, EisaId ("PNP0C31"))  // _HID: Hardware ID
                 Name (_STA, 0x0F)  // _STA: Status
                 Name (_CRS, ResourceTemplate ()  // _CRS: Current
Resource Settings
                 {
                     Memory32Fixed (ReadWrite,
                         0xFED40000,         // Address Base
                         0x00005000,         // Address Length
                         )
                 })
                 OperationRegion (TPPI, SystemMemory, 0xFFFF0000, 0x31)
                 Field (TPPI, AnyAcc, NoLock, Preserve)
                 {
                     PPIN,   8,
                     PPIP,   32,
                     PPRP,   32,
                     PPRQ,   32,
                     PPRM,   32,
                     LPPR,   32,
                     FRET,   32,
                     RES1,   8,
                     RES2,   128,
                     FAIL,   32
                 }

                 Method (WRAM, 2, Serialized)
                 {
                     PPRQ = Arg0
                     PPRM = Arg1
                     Return (Zero)
                 }

                 Method (CKOP, 1, NotSerialized)
                 {
                     If ((Arg0 == Zero))
                     {
                         Return (One)
                     }

                     Return (Zero)
                 }

                 Method (_DSM, 4, Serialized)  // _DSM: Device-Specific Method
                 {
                     If ((Arg0 == ToUUID
("3dddfaa6-361b-4eb4-a424-8d10089d1653") /* Physical Presence
Interface */))
                     {
                         Local0 = ToInteger (Arg2)
                         If ((Local0 == Zero))
                         {
                             Return (Buffer (0x02)
                             {
                                  0xFF, 0x01
            // ..
                             })
                         }

                         If ((Local0 == One))
                         {
                             Return ("1.2")
                         }

                         If ((Local0 == 0x02))
                         {
                             Local0 = DerefOf (Arg3 [Zero])
                             If (CKOP (Local0))
                             {
                                 Return (WRAM (Local0, Zero))
                             }

                             Return (One)
                         }

                         If ((Local0 == 0x03))
                         {
                             Return (Package (0x02)
                             {
                                 Zero,
                                 PPRQ
                             })
                         }

                         If ((Local0 == 0x04))
                         {
                             Return (0x02)
                         }

                         If ((Local0 == 0x05))
                         {
                             Return (Package (0x03)
                             {
                                 FAIL,
                                 LPPR,
                                 PPRP
                             })
                         }

                         If ((Local0 == 0x06))
                         {
                             Return (0x03)
                         }

                         If ((Local0 == 0x07))
                         {
                             Local0 = DerefOf (Arg3 [Zero])
                             If (CKOP (Local0))
                             {
                                 Local1 = WRAM (Local0, Zero)
                                 Return (Local1)
                             }

                             Return (One)
                         }

                         If ((Local0 == 0x08))
                         {
                             Local0 = DerefOf (Arg3 [Zero])
                             If (CKOP (Local0))
                             {
                                 Return (0x04)
                             }

                             Return (Zero)
                         }

                         Return (Buffer (One)
                         {
                              0x00
        // .
                         })
                     }
                 }

Linux PPI driver seems to be happy about it, looking at /sys/devices/pnp0/00\:06/tpm/tpm0/ppi/.

I haven't done further testing. Is there anything using this on Linux? tpm2-tools doesn't seem to use it. I suppose I should be able to write something to /sys/devices/pnp0/00\:06/tpm/tpm0/ppi/request.

Any help appreciated :)

Quoted 109 lines

Stefan

Quoted 107 lines
---
   hw/i386/acpi-build.c  | 227
++++++++++++++++++++++++++++++++++++++++++++++++++
   include/hw/acpi/tpm.h |  15 ++++
   2 files changed, 242 insertions(+)
hw/i386/acpi-build.c +85 -0
index 18b939e..e3905a3 100644
@@ -42,6 +42,7 @@
   #include "hw/acpi/memory_hotplug.h"
   #include "sysemu/tpm.h"
   #include "hw/acpi/tpm.h"
+#include "hw/tpm/tpm_ppi.h"
   #include "hw/acpi/vmgenid.h"
   #include "sysemu/tpm_backend.h"
   #include "hw/timer/mc146818rtc_regs.h"
@@ -1860,6 +1861,231 @@ static Aml *build_q35_osc_method(void)
   }
 
   static void
+build_tpm_ppi(Aml *dev, TPMVersion tpm_version)
+{
+    Aml *method, *field, *ifctx, *ifctx2, *ifctx3, *pak;
+
+    aml_append(dev,
+               aml_operation_region("TPPI", AML_SYSTEM_MEMORY,
+                                    aml_int(TPM_PPI_ADDR_BASE),
+                                    TPM_PPI_STRUCT_SIZE));
+
+    field = aml_field("TPPI", AML_ANY_ACC, AML_NOLOCK, AML_PRESERVE);
+    aml_append(field, aml_named_field("PPIN",
+               sizeof(uint8_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPIP",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPRP",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPRQ",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("PPRM",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("LPPR",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("FRET",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("RES1",
+               sizeof(uint8_t) * BITS_PER_BYTE));
+    aml_append(field, aml_named_field("RES2",
+               sizeof(uint32_t) * BITS_PER_BYTE * 4));
+    aml_append(field, aml_named_field("FAIL",
+               sizeof(uint32_t) * BITS_PER_BYTE));
+    aml_append(dev, field);
+
+    /*
+     * Write the given operations code into 'PPRQ'.
+     */
+    method = aml_method("WRAM", 2, AML_SERIALIZED);
+    {
+        aml_append(method, aml_store(aml_arg(0), aml_name("PPRQ")));
+        aml_append(method, aml_store(aml_arg(1), aml_name("PPRM")));
+        /* 0 = Success */
+        aml_append(method, aml_return(aml_int(0)));
+    }
+    aml_append(dev, method);
+
+    /*
+     * CKOP: Check whether the opcode is valid
+     */
+    if (tpm_version == TPM_VERSION_1_2) {
+        method = aml_method("CKOP", 1, AML_NOTSERIALIZED);
+        {
+            ifctx = aml_if(
+                      aml_or(
+                        aml_or(
+                          aml_and(
+                            aml_lgreater_equal(aml_arg(0), aml_int(0)),
+                            aml_lless_equal(aml_arg(0), aml_int(11)),
+                            NULL
+                          ),
+                          aml_equal(aml_arg(0), aml_int(14)),
+                          NULL
+                        ),
+                        aml_and(
+                          aml_lgreater_equal(aml_arg(0), aml_int(21)),
+                          aml_lless_equal(aml_arg(0), aml_int(22)),
+                          NULL
+                       ),
+                       NULL
+                      )
+                    );
+            {
+                aml_append(ifctx, aml_return(aml_int(1)));
+            }
+            aml_append(method, ifctx);
+
+            aml_append(method, aml_return(aml_int(0)));
+        }
+    } else {
+        method = aml_method("CKOP", 1, AML_NOTSERIALIZED);
+        {
+            ifctx = aml_if(
+                      aml_equal(aml_arg(0), aml_int(0))
+                    );
+            {
+                aml_append(ifctx, aml_return(aml_int(1)));
Quoted 8 lines
Quoted 8 lines

hw/i386/acpi-build.c static Aml *build_q35_osc_method(void) · line 1955

Quoted 8 lines
hw/i386/acpi-build.c +8 -0
+            }
+            aml_append(method, ifctx);
+
+            aml_append(method, aml_return(aml_int(0)));
+        }
+    }
+    aml_append(dev, method);
+

CKOP checks whether the firmware implements a certain code. Function 8 returns a number 0 - 4 for a certain code, 0 meaning 'Not implemented'. Maybe the firmware should fill an array and put the numbers in that function 8 expects and CKOP can use that also to see whether functions are supported. That may 'loosen' the ACPI a bit from the firmware.

Comments? Is that better than hard coding in the ACPI what the firmware supports?

specs: https://trustedcomputinggroup.org/wp-content/uploads/Physical_Presence_Interface_1-20_0-100.pdf

Quoted 178 lines
Quoted 165 lines
Quoted 165 lines
hw/i386/acpi-build.c +117 -0
+    method = aml_method("_DSM", 4, AML_SERIALIZED);
+    {
+        uint8_t zerobyte[1] = { 0 };
+
+        ifctx = aml_if(
+                  aml_equal(aml_arg(0),
+
 aml_touuid("3DDDFAA6-361B-4EB4-A424-8D10089D1653"))
+                );
+        {
+            aml_append(ifctx,
+                       aml_store(aml_to_integer(aml_arg(2)),
 aml_local(0)));
+
+            /* standard DSM query function */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(0)));
+            {
+                uint8_t byte_list[2] = { 0xff, 0x01 };
+                aml_append(ifctx2, aml_return(aml_buffer(2, byte_list)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* interface version: 1.2 */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(1)));
+            {
+                aml_append(ifctx2, aml_return(aml_string("1.2")));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* submit TPM operation */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(2)));
+            {
+                aml_append(ifctx2,
+                           aml_store(aml_derefof(aml_index(aml_arg(3),
+                                                           aml_int(0))),
+                                     aml_local(0)));
+                ifctx3 = aml_if(aml_call1("CKOP", aml_local(0)));
+                {
+                    aml_append(ifctx3,
+                               aml_return(aml_call2("WRAM",
+                                                    aml_local(0),
+                                                    aml_int(0))));
+                }
+                aml_append(ifctx2, ifctx3);
+                aml_append(ifctx2, aml_return(aml_int(1)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get pending TPM operation */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(3)));
+            {
+                pak = aml_package(2);
+                aml_append(pak, aml_int(0));
+                aml_append(pak, aml_name("PPRQ"));
+                aml_append(ifctx2, aml_return(pak));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get platform-specific action to transition to pre-OS env.
 */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(4)));
+            {
+                /* 2 = reboot */
+                aml_append(ifctx2, aml_return(aml_int(2)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get TPM operation response */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(5)));
+            {
+                pak = aml_package(3);
+
+                aml_append(pak, aml_name("FAIL"));
+                aml_append(pak, aml_name("LPPR"));
+                aml_append(pak, aml_name("PPRP"));
+
+                aml_append(ifctx2, aml_return(pak));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* submit preferred user language */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(6)));
+            {
+                /* 3 = not implemented */
+                aml_append(ifctx2, aml_return(aml_int(3)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* submit TPM operation v2 */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(7)));
+            {
+                aml_append(ifctx2,
+                           aml_store(aml_derefof(aml_index(aml_arg(3),
+                                                           aml_int(0))),
+                                     aml_local(0)));
+                ifctx3 = aml_if(aml_call1("CKOP", aml_local(0)));
+                {
+                    aml_append(ifctx3,
+                               aml_store(aml_call2("WRAM",
+                                                   aml_local(0),
+                                                   aml_int(0)),
+                                         aml_local(1)));
+                    aml_append(ifctx3, aml_return(aml_local(1)));
+                }
+                aml_append(ifctx2, ifctx3);
+                /* 1 = requested operation not implemented */
+                aml_append(ifctx2, aml_return(aml_int(1)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            /* get user confirmation status for operation */
+            ifctx2 = aml_if(aml_equal(aml_local(0), aml_int(8)));
+            {
+                aml_append(ifctx2,
+                           aml_store(aml_derefof(aml_index(aml_arg(3),
+                                                           aml_int(0))),
+                                     aml_local(0)));
+                ifctx3 = aml_if(aml_call1("CKOP", aml_local(0)));
+                {
+                    /* 4 = Allowed and physically present user not

required */

hw/i386/acpi-build.c +17 -0
+                    aml_append(ifctx3, aml_return(aml_int(4)));
+                }
+                aml_append(ifctx2, ifctx3);
+                /* 0 = requested operation not implemented */
+                aml_append(ifctx2, aml_return(aml_int(0)));
+            }
+            aml_append(ifctx, ifctx2);
+
+            aml_append(ifctx, aml_return(aml_buffer(1, zerobyte)));
+        }
+        aml_append(method, ifctx);
+    }
+    aml_append(dev, method);
+}
+
+static void
   build_dsdt(GArray *table_data, BIOSLinker *linker,
              AcpiPmInfo *pm, AcpiMiscInfo *misc,
              Range *pci_hole, Range *pci_hole64, MachineState *machine)
@@ -2218,6 +2444,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
                    */
                   /* aml_append(crs, aml_irq_no_flags(TPM_TIS_IRQ)); */
                   aml_append(dev, aml_name_decl("_CRS", crs));
+                build_tpm_ppi(dev, misc->tpm_version);
                   aml_append(scope, dev);
               }
 
include/hw/acpi/tpm.h +2 -0
index d9b7452..23f5da1 100644
@@ -37,4 +37,19 @@
   #define TPM_PPI_ADDR_SIZE           0x100
   #define TPM_PPI_ADDR_BASE           0xffff0000
 
+struct tpm_ppi {
+    uint8_t ppin;            /* set by BIOS; currently initialization

flag */

include/hw/acpi/tpm.h +6 -0
+    uint32_t ppip;           /* set by ACPI; not used */
+    uint32_t pprp;           /* response from TPM; set by BIOS */
+    uint32_t pprq;           /* opcode; set by ACPI */
+    uint32_t pprm;           /* parameter for opcode; set by ACPI */
+    uint32_t lppr;           /* last opcode; set by BIOS */
+    uint32_t fret;           /* set by ACPI; not used*/
Quoted 11 lines

include/hw/acpi/tpm.h line 55

Quoted 8 lines
include/hw/acpi/tpm.h +7 -0
+    uint32_t res1;           /* reserved */
+    uint32_t res2[4];        /* reserved */
+    uint32_t fail;           /* set by BIOS (0 = success) */
+} QEMU_PACKED;
+
+#define TPM_PPI_STRUCT_SIZE  sizeof(struct tpm_ppi)
+
   #endif /* HW_ACPI_TPM_H */

otherwise, patch looks good.

Reviewed-by
#10 lore raw

Re: [Qemu-devel] [RFC PATCH 2/3] acpi: implement aml_lless_equal

In reply to Stefan Berger
Quoted 10 lines · On Fri, 12 Jan 2018 14:42:14 -0500 Stefan Berger <[email protected]>…
Quoted 5 lines · Marc-André Lureau

2018-01-10 19:35 GMT+01:00 Stefan Berger <[email protected]>:

Quoted 3 lines

LLessEqualOp = LNotOp LGreaterOp

Thanks. I added a comment line above the function now:

/* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLLessEqual */

Quoted 48 lines
Quoted 46 lines
Quoted 44 lines
 
---
  hw/acpi/aml-build.c         | 10 ++++++++++
  include/hw/acpi/aml-build.h |  1 +
  2 files changed, 11 insertions(+)
hw/acpi/aml-build.c +10 -0
index 36a6cc4..597a58d 100644
@@ -568,6 +568,16 @@ Aml *aml_lless(Aml *arg1, Aml *arg2)
      return var;
  }
 
+Aml *aml_lless_equal(Aml *arg1, Aml *arg2)
+{
+    /* LLessEqualOp := LNotOp LGreaterOp */
+    Aml *var = aml_opcode(0x92 /* LNotOp */);
+    build_append_byte(var->buf, 0x94 /* LGreaterOp */);
+    aml_append(var, arg1);
+    aml_append(var, arg2);
+    return var;
+}
+
  /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAdd */
  Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst)
  {
include/hw/acpi/aml-build.h +1 -1
index 88d0738..c4398cc 100644
@@ -267,6 +267,7 @@ Aml *aml_lor(Aml *arg1, Aml *arg2);
  Aml *aml_shiftleft(Aml *arg1, Aml *count);
  Aml *aml_shiftright(Aml *arg1, Aml *count, Aml *dst);
  Aml *aml_lless(Aml *arg1, Aml *arg2);
+Aml *aml_lless_equal(Aml *arg1, Aml *arg2);
  Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst);
  Aml *aml_subtract(Aml *arg1, Aml *arg2, Aml *dst);
  Aml *aml_increment(Aml *arg);
--

2.5.5

#6 lore raw

Re: [Qemu-devel] [RFC PATCH 1/3] tpm: Implement virtual memory device for TPM PPI

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

Hi

Quoted 9 lines · Stefan Berger
  }

static void tpm_tis_class_init(ObjectClass *klass, void *data)

include/hw/acpi/tpm.h +0 -0
index 6d516c6..d9b7452 100644
@@ -31,4 +31,10 @@
 
Quoted 7 lines

include/hw/acpi/tpm.h line 38

Quoted 7 lines
include/hw/acpi/tpm.h +5 -0
  #define TPM2_START_METHOD_MMIO      6
 
+/*
+ * Physical Presence Interface
+ */
+#define TPM_PPI_ADDR_SIZE           0x100
+#define TPM_PPI_ADDR_BASE           0xffff0000

I tried to extend the memory size to be able to store per opcode flags into this area to de-couple the ACPI code from the firmware code as far as possible. I ended up seeing crashes. The following comment is in SeaBIOS:

./src/fw/shadow.c:// On the emulators, the bios at 0xf0000 is also at 0xffff0000

Following this, I am moving this base address to 0xfffe_f000 and extending it to 0x400 bytes. Besides that there will be a QEMU ACPI table with OEM 'QEMU' and oem_table_id 'CONF' for which the firmware has to look for. It holds the base address and the TPM version.

Stefan