diff mbox series

[33/53] acpi: build_tpm_tcpa: use acpi_init_table()/acpi_table_composed() instead of build_header()

Message ID 20210625091818.1047980-35-imammedo@redhat.com
State New
Headers show
Series acpi: refactor error prone build_header() and packed structures usage in ACPI tables | expand

Commit Message

Igor Mammedov June 25, 2021, 9:17 a.m. UTC
it replaces error-prone pointer arithmetic for build_header() API,
with 2 calls to start and finish table creation,
which hides offsets magic from API user.

While at it switch to build_append_int_noprefix() to build
table entries (which also removes some manual offset
calculations).

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
CC: marcel.apfelbaum@gmail.com
CC: stefanb@linux.vnet.ibm.com
---
 include/hw/acpi/acpi-defs.h | 14 -------------
 hw/i386/acpi-build.c        | 39 +++++++++++++++++++++----------------
 2 files changed, 22 insertions(+), 31 deletions(-)

Comments

Stefan Berger June 29, 2021, 3:19 p.m. UTC | #1
On 6/25/21 5:17 AM, Igor Mammedov wrote:

> it replaces error-prone pointer arithmetic for build_header() API,
> with 2 calls to start and finish table creation,
> which hides offsets magic from API user.
>
> While at it switch to build_append_int_noprefix() to build
> table entries (which also removes some manual offset
> calculations).
>
> Signed-off-by: Igor Mammedov <imammedo@redhat.com>


Patch 19, 24 and this one applied causes this error here when using a 
TPM 1.2:

qemu-system-x86_64: ../hw/acpi/bios-linker-loader.c:286: 
bios_linker_loader_add_pointer: Assertion `source_file' failed.


    Stefan


> ---
> CC: marcel.apfelbaum@gmail.com
> CC: stefanb@linux.vnet.ibm.com
> ---
>   include/hw/acpi/acpi-defs.h | 14 -------------
>   hw/i386/acpi-build.c        | 39 +++++++++++++++++++++----------------
>   2 files changed, 22 insertions(+), 31 deletions(-)
>
> diff --git a/include/hw/acpi/acpi-defs.h b/include/hw/acpi/acpi-defs.h
> index 4d8f8b34b0..3b42b138f0 100644
> --- a/include/hw/acpi/acpi-defs.h
> +++ b/include/hw/acpi/acpi-defs.h
> @@ -418,20 +418,6 @@ struct AcpiSratProcessorGiccAffinity {
>
>   typedef struct AcpiSratProcessorGiccAffinity AcpiSratProcessorGiccAffinity;
>
> -/*
> - * TCPA Description Table
> - *
> - * Following Level 00, Rev 00.37 of specs:
> - * http://www.trustedcomputinggroup.org/resources/tcg_acpi_specification
> - */
> -struct Acpi20Tcpa {
> -    ACPI_TABLE_HEADER_DEF                    /* ACPI common table header */
> -    uint16_t platform_class;
> -    uint32_t log_area_minimum_length;
> -    uint64_t log_area_start_address;
> -} QEMU_PACKED;
> -typedef struct Acpi20Tcpa Acpi20Tcpa;
> -
>   /* DMAR - DMA Remapping table r2.2 */
>   struct AcpiTableDmar {
>       ACPI_TABLE_HEADER_DEF
> diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
> index 96ad3e1b9d..bc45a666f6 100644
> --- a/hw/i386/acpi-build.c
> +++ b/hw/i386/acpi-build.c
> @@ -1849,31 +1849,36 @@ build_hpet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
>   }
>
>   #ifdef CONFIG_TPM
> +/*
> + * TCPA Description Table
> + *
> + * Following Level 00, Rev 00.37 of specs:
> + * http://www.trustedcomputinggroup.org/resources/tcg_acpi_specification
> + * 7.1.2 ACPI Table Layout
> + */
>   static void
>   build_tpm_tcpa(GArray *table_data, BIOSLinker *linker, GArray *tcpalog,
>                  const char *oem_id, const char *oem_table_id)
>   {
> -    int tcpa_start = table_data->len;
> -    Acpi20Tcpa *tcpa = acpi_data_push(table_data, sizeof *tcpa);
> -    unsigned log_addr_size = sizeof(tcpa->log_area_start_address);
> -    unsigned log_addr_offset =
> -        (char *)&tcpa->log_area_start_address - table_data->data;
> +    AcpiTable table = { .sig = "TCPA", .rev = 2,
> +                        .oem_id = oem_id, .oem_table_id = oem_table_id };
>
> -    tcpa->platform_class = cpu_to_le16(TPM_TCPA_ACPI_CLASS_CLIENT);
> -    tcpa->log_area_minimum_length = cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE);
> -    acpi_data_push(tcpalog, le32_to_cpu(tcpa->log_area_minimum_length));
> +    acpi_init_table(&table, table_data);
> +    /* Platform Class */
> +    build_append_int_noprefix(table_data, TPM_TCPA_ACPI_CLASS_CLIENT, 2);
> +    /* Log Area Minimum Length (LAML) */
> +    build_append_int_noprefix(table_data, TPM_LOG_AREA_MINIMUM_SIZE, 4);
> +    /* Log Area Start Address (LASA) */
> +    /* log area start address to be filled by Guest linker */
> +    bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
> +        table_data->len, 8, ACPI_BUILD_TPMLOG_FILE, 0);
> +    build_append_int_noprefix(table_data, 0, 8);
> +    acpi_table_composed(linker, &table);
>
> +    /* allocate/reserve space for TPM log area */
> +    acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE);
>       bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, tcpalog, 1,
>                                false /* high memory */);
> -
> -    /* log area start address to be filled by Guest linker */
> -    bios_linker_loader_add_pointer(linker,
> -        ACPI_BUILD_TABLE_FILE, log_addr_offset, log_addr_size,
> -        ACPI_BUILD_TPMLOG_FILE, 0);
> -
> -    build_header(linker, table_data,
> -                 (void *)(table_data->data + tcpa_start),
> -                 "TCPA", sizeof(*tcpa), 2, oem_id, oem_table_id);
>   }
>   #endif
>
Igor Mammedov June 30, 2021, 7:22 a.m. UTC | #2
On Tue, 29 Jun 2021 11:19:17 -0400
Stefan Berger <stefanb@linux.ibm.com> wrote:

> On 6/25/21 5:17 AM, Igor Mammedov wrote:
> 
> > it replaces error-prone pointer arithmetic for build_header() API,
> > with 2 calls to start and finish table creation,
> > which hides offsets magic from API user.
> >
> > While at it switch to build_append_int_noprefix() to build
> > table entries (which also removes some manual offset
> > calculations).
> >
> > Signed-off-by: Igor Mammedov <imammedo@redhat.com>  
> 
> 
> Patch 19, 24 and this one applied causes this error here when using a 
> TPM 1.2:
> 
> qemu-system-x86_64: ../hw/acpi/bios-linker-loader.c:286: 
> bios_linker_loader_add_pointer: Assertion `source_file' failed.

I missed this one since it's not covered by qtest,
it seems that test_acpi_tcg_tpm() tests only TPM2.0,
can you look into adding a test-case for TPM1.2
so it won't get missed/broken in the future?

I'll post fixed patch shortly as a reply this this thread.

> 
> 
>     Stefan
> 
> 
> > ---
> > CC: marcel.apfelbaum@gmail.com
> > CC: stefanb@linux.vnet.ibm.com
> > ---
> >   include/hw/acpi/acpi-defs.h | 14 -------------
> >   hw/i386/acpi-build.c        | 39 +++++++++++++++++++++----------------
> >   2 files changed, 22 insertions(+), 31 deletions(-)
> >
> > diff --git a/include/hw/acpi/acpi-defs.h b/include/hw/acpi/acpi-defs.h
> > index 4d8f8b34b0..3b42b138f0 100644
> > --- a/include/hw/acpi/acpi-defs.h
> > +++ b/include/hw/acpi/acpi-defs.h
> > @@ -418,20 +418,6 @@ struct AcpiSratProcessorGiccAffinity {
> >
> >   typedef struct AcpiSratProcessorGiccAffinity AcpiSratProcessorGiccAffinity;
> >
> > -/*
> > - * TCPA Description Table
> > - *
> > - * Following Level 00, Rev 00.37 of specs:
> > - * http://www.trustedcomputinggroup.org/resources/tcg_acpi_specification
> > - */
> > -struct Acpi20Tcpa {
> > -    ACPI_TABLE_HEADER_DEF                    /* ACPI common table header */
> > -    uint16_t platform_class;
> > -    uint32_t log_area_minimum_length;
> > -    uint64_t log_area_start_address;
> > -} QEMU_PACKED;
> > -typedef struct Acpi20Tcpa Acpi20Tcpa;
> > -
> >   /* DMAR - DMA Remapping table r2.2 */
> >   struct AcpiTableDmar {
> >       ACPI_TABLE_HEADER_DEF
> > diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
> > index 96ad3e1b9d..bc45a666f6 100644
> > --- a/hw/i386/acpi-build.c
> > +++ b/hw/i386/acpi-build.c
> > @@ -1849,31 +1849,36 @@ build_hpet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
> >   }
> >
> >   #ifdef CONFIG_TPM
> > +/*
> > + * TCPA Description Table
> > + *
> > + * Following Level 00, Rev 00.37 of specs:
> > + * http://www.trustedcomputinggroup.org/resources/tcg_acpi_specification
> > + * 7.1.2 ACPI Table Layout
> > + */
> >   static void
> >   build_tpm_tcpa(GArray *table_data, BIOSLinker *linker, GArray *tcpalog,
> >                  const char *oem_id, const char *oem_table_id)
> >   {
> > -    int tcpa_start = table_data->len;
> > -    Acpi20Tcpa *tcpa = acpi_data_push(table_data, sizeof *tcpa);
> > -    unsigned log_addr_size = sizeof(tcpa->log_area_start_address);
> > -    unsigned log_addr_offset =
> > -        (char *)&tcpa->log_area_start_address - table_data->data;
> > +    AcpiTable table = { .sig = "TCPA", .rev = 2,
> > +                        .oem_id = oem_id, .oem_table_id = oem_table_id };
> >
> > -    tcpa->platform_class = cpu_to_le16(TPM_TCPA_ACPI_CLASS_CLIENT);
> > -    tcpa->log_area_minimum_length = cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE);
> > -    acpi_data_push(tcpalog, le32_to_cpu(tcpa->log_area_minimum_length));
> > +    acpi_init_table(&table, table_data);
> > +    /* Platform Class */
> > +    build_append_int_noprefix(table_data, TPM_TCPA_ACPI_CLASS_CLIENT, 2);
> > +    /* Log Area Minimum Length (LAML) */
> > +    build_append_int_noprefix(table_data, TPM_LOG_AREA_MINIMUM_SIZE, 4);
> > +    /* Log Area Start Address (LASA) */
> > +    /* log area start address to be filled by Guest linker */
> > +    bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
> > +        table_data->len, 8, ACPI_BUILD_TPMLOG_FILE, 0);
> > +    build_append_int_noprefix(table_data, 0, 8);
> > +    acpi_table_composed(linker, &table);
> >
> > +    /* allocate/reserve space for TPM log area */
> > +    acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE);
> >       bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, tcpalog, 1,
> >                                false /* high memory */);
> > -
> > -    /* log area start address to be filled by Guest linker */
> > -    bios_linker_loader_add_pointer(linker,
> > -        ACPI_BUILD_TABLE_FILE, log_addr_offset, log_addr_size,
> > -        ACPI_BUILD_TPMLOG_FILE, 0);
> > -
> > -    build_header(linker, table_data,
> > -                 (void *)(table_data->data + tcpa_start),
> > -                 "TCPA", sizeof(*tcpa), 2, oem_id, oem_table_id);
> >   }
> >   #endif
> >  
>
diff mbox series

Patch

diff --git a/include/hw/acpi/acpi-defs.h b/include/hw/acpi/acpi-defs.h
index 4d8f8b34b0..3b42b138f0 100644
--- a/include/hw/acpi/acpi-defs.h
+++ b/include/hw/acpi/acpi-defs.h
@@ -418,20 +418,6 @@  struct AcpiSratProcessorGiccAffinity {
 
 typedef struct AcpiSratProcessorGiccAffinity AcpiSratProcessorGiccAffinity;
 
-/*
- * TCPA Description Table
- *
- * Following Level 00, Rev 00.37 of specs:
- * http://www.trustedcomputinggroup.org/resources/tcg_acpi_specification
- */
-struct Acpi20Tcpa {
-    ACPI_TABLE_HEADER_DEF                    /* ACPI common table header */
-    uint16_t platform_class;
-    uint32_t log_area_minimum_length;
-    uint64_t log_area_start_address;
-} QEMU_PACKED;
-typedef struct Acpi20Tcpa Acpi20Tcpa;
-
 /* DMAR - DMA Remapping table r2.2 */
 struct AcpiTableDmar {
     ACPI_TABLE_HEADER_DEF
diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
index 96ad3e1b9d..bc45a666f6 100644
--- a/hw/i386/acpi-build.c
+++ b/hw/i386/acpi-build.c
@@ -1849,31 +1849,36 @@  build_hpet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
 }
 
 #ifdef CONFIG_TPM
+/*
+ * TCPA Description Table
+ *
+ * Following Level 00, Rev 00.37 of specs:
+ * http://www.trustedcomputinggroup.org/resources/tcg_acpi_specification
+ * 7.1.2 ACPI Table Layout
+ */
 static void
 build_tpm_tcpa(GArray *table_data, BIOSLinker *linker, GArray *tcpalog,
                const char *oem_id, const char *oem_table_id)
 {
-    int tcpa_start = table_data->len;
-    Acpi20Tcpa *tcpa = acpi_data_push(table_data, sizeof *tcpa);
-    unsigned log_addr_size = sizeof(tcpa->log_area_start_address);
-    unsigned log_addr_offset =
-        (char *)&tcpa->log_area_start_address - table_data->data;
+    AcpiTable table = { .sig = "TCPA", .rev = 2,
+                        .oem_id = oem_id, .oem_table_id = oem_table_id };
 
-    tcpa->platform_class = cpu_to_le16(TPM_TCPA_ACPI_CLASS_CLIENT);
-    tcpa->log_area_minimum_length = cpu_to_le32(TPM_LOG_AREA_MINIMUM_SIZE);
-    acpi_data_push(tcpalog, le32_to_cpu(tcpa->log_area_minimum_length));
+    acpi_init_table(&table, table_data);
+    /* Platform Class */
+    build_append_int_noprefix(table_data, TPM_TCPA_ACPI_CLASS_CLIENT, 2);
+    /* Log Area Minimum Length (LAML) */
+    build_append_int_noprefix(table_data, TPM_LOG_AREA_MINIMUM_SIZE, 4);
+    /* Log Area Start Address (LASA) */
+    /* log area start address to be filled by Guest linker */
+    bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
+        table_data->len, 8, ACPI_BUILD_TPMLOG_FILE, 0);
+    build_append_int_noprefix(table_data, 0, 8);
+    acpi_table_composed(linker, &table);
 
+    /* allocate/reserve space for TPM log area */
+    acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE);
     bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, tcpalog, 1,
                              false /* high memory */);
-
-    /* log area start address to be filled by Guest linker */
-    bios_linker_loader_add_pointer(linker,
-        ACPI_BUILD_TABLE_FILE, log_addr_offset, log_addr_size,
-        ACPI_BUILD_TPMLOG_FILE, 0);
-
-    build_header(linker, table_data,
-                 (void *)(table_data->data + tcpa_start),
-                 "TCPA", sizeof(*tcpa), 2, oem_id, oem_table_id);
 }
 #endif