]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/XenPlatformPei: Rework memory detection
authorAnthony PERARD <anthony.perard@citrix.com>
Tue, 13 Aug 2019 11:31:07 +0000 (12:31 +0100)
committerLaszlo Ersek <lersek@redhat.com>
Wed, 21 Aug 2019 16:03:49 +0000 (18:03 +0200)
When running as a Xen PVH guest, there is no CMOS to read the memory
size from.  Rework GetSystemMemorySize(Below|Above)4gb() so they can
work without CMOS by reading the e820 table.

Rework XenPublishRamRegions to also care for the reserved and ACPI
entry in the e820 table. The region that was added by InitializeXen()
isn't needed as that same entry is in the e820 table provided by
hvmloader.

MTRR settings aren't modified anymore, on HVM it's already done by
hvmloader, on PVH it is supposed to have sane default. MTRR will need
to be done properly but keeping what's already been done by programs
that have run before OVMF will do for now.

Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=1689
Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20190813113119.14804-24-anthony.perard@citrix.com>

OvmfPkg/XenPlatformPei/MemDetect.c
OvmfPkg/XenPlatformPei/Platform.c
OvmfPkg/XenPlatformPei/Platform.h
OvmfPkg/XenPlatformPei/Xen.c

index cf95f9c4745ba298054a5eab1d86a3241b2183e2..1f81eee4074c207c2b32da91e783a17d73969ef3 100644 (file)
@@ -96,6 +96,45 @@ Q35TsegMbytesInitialization (
   mQ35TsegMbytes = ExtendedTsegMbytes;\r
 }\r
 \r
+STATIC\r
+UINT64\r
+GetHighestSystemMemoryAddress (\r
+  BOOLEAN       Below4gb\r
+  )\r
+{\r
+  EFI_E820_ENTRY64    *E820Map;\r
+  UINT32              E820EntriesCount;\r
+  EFI_E820_ENTRY64    *Entry;\r
+  EFI_STATUS          Status;\r
+  UINT32              Loop;\r
+  UINT64              HighestAddress;\r
+  UINT64              EntryEnd;\r
+\r
+  HighestAddress = 0;\r
+\r
+  Status = XenGetE820Map (&E820Map, &E820EntriesCount);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  for (Loop = 0; Loop < E820EntriesCount; Loop++) {\r
+    Entry = E820Map + Loop;\r
+    EntryEnd = Entry->BaseAddr + Entry->Length;\r
+\r
+    if (Entry->Type == EfiAcpiAddressRangeMemory &&\r
+        EntryEnd > HighestAddress) {\r
+\r
+      if (Below4gb && (EntryEnd <= BASE_4GB)) {\r
+        HighestAddress = EntryEnd;\r
+      } else if (!Below4gb && (EntryEnd >= BASE_4GB)) {\r
+        HighestAddress = EntryEnd;\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // Round down the end address.\r
+  //\r
+  return HighestAddress & ~(UINT64)EFI_PAGE_MASK;\r
+}\r
 \r
 UINT32\r
 GetSystemMemorySizeBelow4gb (\r
@@ -105,6 +144,19 @@ GetSystemMemorySizeBelow4gb (
   UINT8 Cmos0x34;\r
   UINT8 Cmos0x35;\r
 \r
+  //\r
+  // In PVH case, there is no CMOS, we have to calculate the memory size\r
+  // from parsing the E820\r
+  //\r
+  if (XenPvhDetected ()) {\r
+    UINT64  HighestAddress;\r
+\r
+    HighestAddress = GetHighestSystemMemoryAddress (TRUE);\r
+    ASSERT (HighestAddress > 0 && HighestAddress <= BASE_4GB);\r
+\r
+    return HighestAddress;\r
+  }\r
+\r
   //\r
   // CMOS 0x34/0x35 specifies the system memory above 16 MB.\r
   // * CMOS(0x35) is the high byte\r
@@ -129,6 +181,23 @@ GetSystemMemorySizeAbove4gb (
   UINT32 Size;\r
   UINTN  CmosIndex;\r
 \r
+  //\r
+  // In PVH case, there is no CMOS, we have to calculate the memory size\r
+  // from parsing the E820\r
+  //\r
+  if (XenPvhDetected ()) {\r
+    UINT64  HighestAddress;\r
+\r
+    HighestAddress = GetHighestSystemMemoryAddress (FALSE);\r
+    ASSERT (HighestAddress == 0 || HighestAddress >= BASE_4GB);\r
+\r
+    if (HighestAddress >= BASE_4GB) {\r
+      HighestAddress -= BASE_4GB;\r
+    }\r
+\r
+    return HighestAddress;\r
+  }\r
+\r
   //\r
   // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.\r
   // * CMOS(0x5d) is the most significant size byte\r
index 6aaafc3ee966096663aa22b7174294ebb5e058f2..2f42ca6ccd94b694117bb60ec909a0f5448f9029 100644 (file)
@@ -102,6 +102,17 @@ AddReservedMemoryBaseSizeHob (
     );\r
 }\r
 \r
+VOID\r
+AddReservedMemoryRangeHob (\r
+  EFI_PHYSICAL_ADDRESS        MemoryBase,\r
+  EFI_PHYSICAL_ADDRESS        MemoryLimit,\r
+  BOOLEAN                     Cacheable\r
+  )\r
+{\r
+  AddReservedMemoryBaseSizeHob (MemoryBase,\r
+    (UINT64)(MemoryLimit - MemoryBase), Cacheable);\r
+}\r
+\r
 VOID\r
 AddIoMemoryRangeHob (\r
   EFI_PHYSICAL_ADDRESS        MemoryBase,\r
index db9a62572f89bdaac49fbd97d41c5760a61aacf9..7661f4a8de0a4b76487d2472c226f5c6d7694d00 100644 (file)
@@ -44,6 +44,13 @@ AddReservedMemoryBaseSizeHob (
   BOOLEAN                     Cacheable\r
   );\r
 \r
+VOID\r
+AddReservedMemoryRangeHob (\r
+  EFI_PHYSICAL_ADDRESS        MemoryBase,\r
+  EFI_PHYSICAL_ADDRESS        MemoryLimit,\r
+  BOOLEAN                     Cacheable\r
+  );\r
+\r
 VOID\r
 AddressWidthInitialization (\r
   VOID\r
@@ -114,6 +121,12 @@ XenPublishRamRegions (
   VOID\r
   );\r
 \r
+EFI_STATUS\r
+XenGetE820Map (\r
+  EFI_E820_ENTRY64 **Entries,\r
+  UINT32 *Count\r
+  );\r
+\r
 extern EFI_BOOT_MODE mBootMode;\r
 \r
 extern UINT8 mPhysMemAddressWidth;\r
index 72f6f37b46c814b5a6e004b5a6a2b2406249b71a..c4506def9a2b120670bcc6f825a545bdfd2607ce 100644 (file)
@@ -276,9 +276,14 @@ XenPublishRamRegions (
   VOID\r
   )\r
 {\r
-  EFI_E820_ENTRY64  *E820Map;\r
-  UINT32            E820EntriesCount;\r
-  EFI_STATUS        Status;\r
+  EFI_E820_ENTRY64      *E820Map;\r
+  UINT32                E820EntriesCount;\r
+  EFI_STATUS            Status;\r
+  EFI_E820_ENTRY64      *Entry;\r
+  UINTN                 Index;\r
+  UINT64                LapicBase;\r
+  UINT64                LapicEnd;\r
+\r
 \r
   DEBUG ((DEBUG_INFO, "Using memory map provided by Xen\n"));\r
 \r
@@ -287,26 +292,60 @@ XenPublishRamRegions (
   //\r
   E820EntriesCount = 0;\r
   Status = XenGetE820Map (&E820Map, &E820EntriesCount);\r
-\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-  if (E820EntriesCount > 0) {\r
-    EFI_E820_ENTRY64 *Entry;\r
-    UINT32 Loop;\r
-\r
-    for (Loop = 0; Loop < E820EntriesCount; Loop++) {\r
-      Entry = E820Map + Loop;\r
+  LapicBase = PcdGet32 (PcdCpuLocalApicBaseAddress);\r
+  LapicEnd = LapicBase + SIZE_1MB;\r
+  AddIoMemoryRangeHob (LapicBase, LapicEnd);\r
+\r
+  for (Index = 0; Index < E820EntriesCount; Index++) {\r
+    UINT64 Base;\r
+    UINT64 End;\r
+    UINT64 ReservedBase;\r
+    UINT64 ReservedEnd;\r
+\r
+    Entry = &E820Map[Index];\r
+\r
+    //\r
+    // Round up the start address, and round down the end address.\r
+    //\r
+    Base = ALIGN_VALUE (Entry->BaseAddr, (UINT64)EFI_PAGE_SIZE);\r
+    End = (Entry->BaseAddr + Entry->Length) & ~(UINT64)EFI_PAGE_MASK;\r
+\r
+    switch (Entry->Type) {\r
+    case EfiAcpiAddressRangeMemory:\r
+      AddMemoryRangeHob (Base, End);\r
+      break;\r
+    case EfiAcpiAddressRangeACPI:\r
+      AddReservedMemoryRangeHob (Base, End, FALSE);\r
+      break;\r
+    case EfiAcpiAddressRangeReserved:\r
+      //\r
+      // hvmloader marks a range that overlaps with the local APIC memory\r
+      // mapped region as reserved, but CpuDxe wants it as mapped IO. We\r
+      // have already added it as mapped IO, so skip it here.\r
+      //\r
 \r
       //\r
-      // Only care about RAM\r
+      // add LAPIC predecessor range, if any\r
       //\r
-      if (Entry->Type != EfiAcpiAddressRangeMemory) {\r
-        continue;\r
+      ReservedBase = Base;\r
+      ReservedEnd = MIN (End, LapicBase);\r
+      if (ReservedBase < ReservedEnd) {\r
+        AddReservedMemoryRangeHob (ReservedBase, ReservedEnd, FALSE);\r
       }\r
 \r
-      AddMemoryBaseSizeHob (Entry->BaseAddr, Entry->Length);\r
-\r
-      MtrrSetMemoryAttribute (Entry->BaseAddr, Entry->Length, CacheWriteBack);\r
+      //\r
+      // add LAPIC successor range, if any\r
+      //\r
+      ReservedBase = MAX (Base, LapicEnd);\r
+      ReservedEnd = End;\r
+      if (ReservedBase < ReservedEnd) {\r
+        AddReservedMemoryRangeHob (ReservedBase, ReservedEnd, FALSE);\r
+      }\r
+      break;\r
+    default:\r
+      break;\r
     }\r
   }\r
 }\r
@@ -326,12 +365,6 @@ InitializeXen (
 {\r
   RETURN_STATUS PcdStatus;\r
 \r
-  //\r
-  // Reserve away HVMLOADER reserved memory [0xFC000000,0xFD000000).\r
-  // This needs to match HVMLOADER RESERVED_MEMBASE/RESERVED_MEMSIZE.\r
-  //\r
-  AddReservedMemoryBaseSizeHob (0xFC000000, 0x1000000, FALSE);\r
-\r
   PcdStatus = PcdSetBoolS (PcdPciDisableBusEnumeration, TRUE);\r
   ASSERT_RETURN_ERROR (PcdStatus);\r
 \r