OvmfPkg: Allow multiple add-pointer linker commands to same ACPI table
authorPhil Dennis-Jordan <phil@philjordan.eu>
Thu, 30 Mar 2017 10:40:57 +0000 (23:40 +1300)
committerLaszlo Ersek <lersek@redhat.com>
Thu, 30 Mar 2017 19:53:13 +0000 (21:53 +0200)
ACPI tables may contain multiple fields which point to the same
destination table. For example, in some revisions, the FADT contains
both DSDT and X_DSDT fields, and they may both point to the DSDT.

Previously, if Qemu created QEMU_LOADER_ADD_POINTER linker commands for
such instances, the linking process would attempt to install the same
pointed-to table repeatedly. For tables of which there must only be one
instance, the call to AcpiProtocol->InstallAcpiTable() would fail during
the second linker command pointing to the same table, thus entirely
aborting the ACPI table linking process. In the case of tables of which
there may be multiple instances, the table would end up duplicated.

This change adds a memoisation data structure which tracks the table
pointers that have already been processed; even if the same pointer is
encountered multiple times, it is only processed once.

Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
[lersek@redhat.com: DSDT<->XSDT typo, debug msg, and coding style fixups]
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=368
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c

index 7bb2e3f..1bc5fe2 100644 (file)
@@ -99,6 +99,39 @@ BlobCompare (
 }\r
 \r
 \r
+/**\r
+  Comparator function for two opaque pointers, ordering on (unsigned) pointer\r
+  value itself.\r
+  Can be used as both Key and UserStruct comparator.\r
+\r
+  @param[in] Pointer1  First pointer.\r
+\r
+  @param[in] Pointer2  Second pointer.\r
+\r
+  @retval <0  If Pointer1 compares less than Pointer2.\r
+\r
+  @retval  0  If Pointer1 compares equal to Pointer2.\r
+\r
+  @retval >0  If Pointer1 compares greater than Pointer2.\r
+**/\r
+STATIC\r
+INTN\r
+EFIAPI\r
+PointerCompare (\r
+  IN CONST VOID *Pointer1,\r
+  IN CONST VOID *Pointer2\r
+  )\r
+{\r
+  if (Pointer1 == Pointer2) {\r
+    return 0;\r
+  }\r
+  if ((UINTN)Pointer1 < (UINTN)Pointer2) {\r
+    return -1;\r
+  }\r
+  return 1;\r
+}\r
+\r
+\r
 /**\r
   Process a QEMU_LOADER_ALLOCATE command.\r
 \r
@@ -557,6 +590,16 @@ UndoCmdWritePointer (
                                command identified an ACPI table that is\r
                                different from RSDT and XSDT.\r
 \r
+  @param[in,out] SeenPointers  The ORDERED_COLLECTION tracking the absolute\r
+                               target addresses that have been pointed-to by\r
+                               QEMU_LOADER_ADD_POINTER commands thus far. If a\r
+                               target address is encountered for the first\r
+                               time, and it identifies an ACPI table that is\r
+                               different from RDST and XSDT, the table is\r
+                               installed. If a target address is seen for the\r
+                               second or later times, it is skipped without\r
+                               taking any action.\r
+\r
   @retval EFI_INVALID_PARAMETER  NumInstalled was outside the allowed range on\r
                                  input.\r
 \r
@@ -564,14 +607,15 @@ UndoCmdWritePointer (
                                  table different from RSDT and XSDT, but there\r
                                  was no more room in InstalledKey.\r
 \r
-  @retval EFI_SUCCESS            AddPointer has been processed. Either an ACPI\r
-                                 table different from RSDT and XSDT has been\r
-                                 installed (reflected by InstalledKey and\r
-                                 NumInstalled), or RSDT or XSDT has been\r
-                                 identified but not installed, or the fw_cfg\r
-                                 blob pointed-into by AddPointer has been\r
-                                 marked as hosting something else than just\r
-                                 direct ACPI table contents.\r
+  @retval EFI_SUCCESS            AddPointer has been processed. Either its\r
+                                 absolute target address has been encountered\r
+                                 before, or an ACPI table different from RSDT\r
+                                 and XSDT has been installed (reflected by\r
+                                 InstalledKey and NumInstalled), or RSDT or\r
+                                 XSDT has been identified but not installed, or\r
+                                 the fw_cfg blob pointed-into by AddPointer has\r
+                                 been marked as hosting something else than\r
+                                 just direct ACPI table contents.\r
 \r
   @return                        Error codes returned by\r
                                  AcpiProtocol->InstallAcpiTable().\r
@@ -584,11 +628,13 @@ Process2ndPassCmdAddPointer (
   IN     CONST ORDERED_COLLECTION      *Tracker,\r
   IN     EFI_ACPI_TABLE_PROTOCOL       *AcpiProtocol,\r
   IN OUT UINTN                         InstalledKey[INSTALLED_TABLES_MAX],\r
-  IN OUT INT32                         *NumInstalled\r
+  IN OUT INT32                         *NumInstalled,\r
+  IN OUT ORDERED_COLLECTION            *SeenPointers\r
   )\r
 {\r
   CONST ORDERED_COLLECTION_ENTRY                     *TrackerEntry;\r
   CONST ORDERED_COLLECTION_ENTRY                     *TrackerEntry2;\r
+  ORDERED_COLLECTION_ENTRY                           *SeenPointerEntry;\r
   CONST BLOB                                         *Blob;\r
   BLOB                                               *Blob2;\r
   CONST UINT8                                        *PointerField;\r
@@ -620,6 +666,27 @@ Process2ndPassCmdAddPointer (
   Blob2Remaining += Blob2->Size;\r
   ASSERT (PointerValue < Blob2Remaining);\r
 \r
+  Status = OrderedCollectionInsert (\r
+             SeenPointers,\r
+             &SeenPointerEntry, // for reverting insertion in error case\r
+             (VOID *)(UINTN)PointerValue\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    if (Status == RETURN_ALREADY_STARTED) {\r
+      //\r
+      // Already seen this pointer, don't try to process it again.\r
+      //\r
+      DEBUG ((\r
+        DEBUG_VERBOSE,\r
+        "%a: PointerValue=0x%Lx already processed, skipping.\n",\r
+        __FUNCTION__,\r
+        PointerValue\r
+        ));\r
+      Status = EFI_SUCCESS;\r
+    }\r
+    return Status;\r
+  }\r
+\r
   Blob2Remaining -= (UINTN) PointerValue;\r
   DEBUG ((EFI_D_VERBOSE, "%a: checking for ACPI header in \"%a\" at 0x%Lx "\r
     "(remaining: 0x%Lx): ", __FUNCTION__, AddPointer->PointeeFile,\r
@@ -682,7 +749,8 @@ Process2ndPassCmdAddPointer (
   if (*NumInstalled == INSTALLED_TABLES_MAX) {\r
     DEBUG ((EFI_D_ERROR, "%a: can't install more than %d tables\n",\r
       __FUNCTION__, INSTALLED_TABLES_MAX));\r
-    return EFI_OUT_OF_RESOURCES;\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto RollbackSeenPointer;\r
   }\r
 \r
   Status = AcpiProtocol->InstallAcpiTable (AcpiProtocol,\r
@@ -691,10 +759,14 @@ Process2ndPassCmdAddPointer (
   if (EFI_ERROR (Status)) {\r
     DEBUG ((EFI_D_ERROR, "%a: InstallAcpiTable(): %r\n", __FUNCTION__,\r
       Status));\r
-    return Status;\r
+    goto RollbackSeenPointer;\r
   }\r
   ++*NumInstalled;\r
   return EFI_SUCCESS;\r
+\r
+RollbackSeenPointer:\r
+  OrderedCollectionDelete (SeenPointers, SeenPointerEntry, NULL);\r
+  return Status;\r
 }\r
 \r
 \r
@@ -739,6 +811,8 @@ InstallQemuFwCfgTables (
   UINTN                    *InstalledKey;\r
   INT32                    Installed;\r
   ORDERED_COLLECTION_ENTRY *TrackerEntry, *TrackerEntry2;\r
+  ORDERED_COLLECTION       *SeenPointers;\r
+  ORDERED_COLLECTION_ENTRY *SeenPointerEntry, *SeenPointerEntry2;\r
 \r
   Status = QemuFwCfgFindFile ("etc/table-loader", &FwCfgItem, &FwCfgSize);\r
   if (EFI_ERROR (Status)) {\r
@@ -827,14 +901,26 @@ InstallQemuFwCfgTables (
     goto RollbackWritePointersAndFreeTracker;\r
   }\r
 \r
+  SeenPointers = OrderedCollectionInit (PointerCompare, PointerCompare);\r
+  if (SeenPointers == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto FreeKeys;\r
+  }\r
+\r
   //\r
   // second pass: identify and install ACPI tables\r
   //\r
   Installed = 0;\r
   for (LoaderEntry = LoaderStart; LoaderEntry < LoaderEnd; ++LoaderEntry) {\r
     if (LoaderEntry->Type == QemuLoaderCmdAddPointer) {\r
-      Status = Process2ndPassCmdAddPointer (&LoaderEntry->Command.AddPointer,\r
-                 Tracker, AcpiProtocol, InstalledKey, &Installed);\r
+      Status = Process2ndPassCmdAddPointer (\r
+                 &LoaderEntry->Command.AddPointer,\r
+                 Tracker,\r
+                 AcpiProtocol,\r
+                 InstalledKey,\r
+                 &Installed,\r
+                 SeenPointers\r
+                 );\r
       if (EFI_ERROR (Status)) {\r
         goto UninstallAcpiTables;\r
       }\r
@@ -870,6 +956,15 @@ UninstallAcpiTables:
     DEBUG ((EFI_D_INFO, "%a: installed %d tables\n", __FUNCTION__, Installed));\r
   }\r
 \r
+  for (SeenPointerEntry = OrderedCollectionMin (SeenPointers);\r
+       SeenPointerEntry != NULL;\r
+       SeenPointerEntry = SeenPointerEntry2) {\r
+    SeenPointerEntry2 = OrderedCollectionNext (SeenPointerEntry);\r
+    OrderedCollectionDelete (SeenPointers, SeenPointerEntry, NULL);\r
+  }\r
+  OrderedCollectionUninit (SeenPointers);\r
+\r
+FreeKeys:\r
   FreePool (InstalledKey);\r
 \r
 RollbackWritePointersAndFreeTracker:\r