Revision: 18067
          http://sourceforge.net/p/edk2/code/18067
Author:   lzeng14
Date:     2015-07-27 03:03:25 +0000 (Mon, 27 Jul 2015)
Log Message:
-----------
MdeModulePkg BootScriptExecutorDxe: Reduce reserved memory consumption

We are going to reduce reserved memory consumption by page table buffer,
then OS can have more available memory to use.
Take PhysicalAddressBits = 48 and 2MB page granularity as example,
1:1 Virtual to Physical identity mapping page table buffer needs to be
((512 + 1) * 512 + 1) * 4096 = 1075843072 bytes = 0x40201000 bytes.

Current BootScriptExecutorDxe handles > 4G request by page fault because
S3ResumePeim only builds 4G page table when long mode waking vector is
not needed, but BootScriptExecutorDxe still assume the page table buffer for
page table is at 1:1 Virtual to Physical identity mapping.

To reduce reserved memory consumption, the code is updated to only use
8 extra pages to handles > 4G request by page fault.
Another, when both BIOS and OS wants long mode waking vector,
S3ResumePei should have established 1:1 Virtual to Physical identity mapping
page table for ACPI spec requirement, so no need to hook page fault handler.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Star Zeng <[email protected]>
Reviewed-by: Jiewen Yao <[email protected]>

Modified Paths:
--------------
    
trunk/edk2/MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/X64/SetIdtEntry.c

Modified: 
trunk/edk2/MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/X64/SetIdtEntry.c
===================================================================
--- 
trunk/edk2/MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/X64/SetIdtEntry.c  
    2015-07-27 02:45:43 UTC (rev 18066)
+++ 
trunk/edk2/MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/X64/SetIdtEntry.c  
    2015-07-27 03:03:25 UTC (rev 18067)
@@ -3,7 +3,7 @@
 
   Set a IDT entry for interrupt vector 3 for debug purpose for x64 platform
 
-Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
 
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD 
License
@@ -16,13 +16,23 @@
 **/
 #include "ScriptExecute.h"
 
+//
+// 8 extra pages for PF handler.
+//
+#define EXTRA_PAGE_TABLE_PAGES      8
+
 #define IA32_PG_P                   BIT0
 #define IA32_PG_RW                  BIT1
 #define IA32_PG_PS                  BIT7
 
-UINT64                             mPhyMask;
-VOID                               *mOriginalHandler;
-UINTN                              mS3NvsPageTableAddress;
+UINT64                              mPhyMask;
+VOID                                *mOriginalHandler;
+UINTN                               mPageFaultBuffer;
+UINTN                               mPageFaultIndex = 0;
+//
+// Store the uplink information for each page being used.
+//
+UINT64                              *mPageFaultUplink[EXTRA_PAGE_TABLE_PAGES];
 
 /**
   Page fault handler.
@@ -73,13 +83,49 @@
   IdtEntry->Bits.Reserved_1     = 0;
 
   if (mPage1GSupport) {
-    mS3NvsPageTableAddress = (UINTN)(AsmReadCr3 () & mPhyMask) + 
EFI_PAGES_TO_SIZE(2);
+    mPageFaultBuffer = (UINTN)(AsmReadCr3 () & mPhyMask) + 
EFI_PAGES_TO_SIZE(2);
   }else {
-    mS3NvsPageTableAddress = (UINTN)(AsmReadCr3 () & mPhyMask) + 
EFI_PAGES_TO_SIZE(6);
+    mPageFaultBuffer = (UINTN)(AsmReadCr3 () & mPhyMask) + 
EFI_PAGES_TO_SIZE(6);
   }
+  ZeroMem (mPageFaultUplink, sizeof (mPageFaultUplink));
 }
 
 /**
+  The function will check if current waking vector is long mode.
+
+  @param  AcpiS3Context                 a pointer to a structure of 
ACPI_S3_CONTEXT
+
+  @retval TRUE   Current context need long mode waking vector.
+  @retval FALSE  Current context need not long mode waking vector.
+**/
+BOOLEAN
+IsLongModeWakingVector (
+  IN ACPI_S3_CONTEXT                *AcpiS3Context
+  )
+{
+  EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE  *Facs;
+
+  Facs = (EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *) ((UINTN) 
(AcpiS3Context->AcpiFacsTable));
+  if ((Facs == NULL) ||
+      (Facs->Signature != 
EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE) ||
+      ((Facs->FirmwareWakingVector == 0) && (Facs->XFirmwareWakingVector == 
0)) ) {
+    // Something wrong with FACS
+    return FALSE;
+  }
+  if (Facs->XFirmwareWakingVector != 0) {
+    if ((Facs->Version == 
EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_VERSION) &&
+        ((Facs->Flags & EFI_ACPI_4_0_64BIT_WAKE_SUPPORTED_F) != 0) &&
+        ((Facs->Flags & EFI_ACPI_4_0_OSPM_64BIT_WAKE__F) != 0)) {
+      // Both BIOS and OS wants 64bit vector
+      if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {
+        return TRUE;
+      }
+    }
+  }
+  return FALSE;
+}
+
+/**
   Set a IDT entry for interrupt vector 3 for debug purpose.
 
   @param  AcpiS3Context  a pointer to a structure of ACPI_S3_CONTEXT
@@ -124,27 +170,47 @@
     }
   );
 
-  IdtEntry = (IA32_IDT_GATE_DESCRIPTOR *)(IdtDescriptor->Base + (14 * sizeof 
(IA32_IDT_GATE_DESCRIPTOR)));
-  HookPageFaultHandler (IdtEntry);
+  //
+  // If both BIOS and OS wants long mode waking vector,
+  // S3ResumePei should have established 1:1 Virtual to Physical identity 
mapping page table,
+  // no need to hook page fault handler.
+  //
+  if (!IsLongModeWakingVector (AcpiS3Context)) {
+    IdtEntry = (IA32_IDT_GATE_DESCRIPTOR *)(IdtDescriptor->Base + (14 * sizeof 
(IA32_IDT_GATE_DESCRIPTOR)));
+    HookPageFaultHandler (IdtEntry);
+  }
 }
 
 /**
-  Get new page address.
+  Acquire page for page fault.
 
-  @param  PageNum  new page number needed
+  @param[in, out] Uplink        Pointer to up page table entry.
 
-  @return new page address
 **/
-UINTN
-GetNewPage (
-  IN UINTN  PageNum
+VOID
+AcquirePage (
+  IN OUT UINT64                 *Uplink
   )
 {
-  UINTN  NewPage;
-  NewPage = mS3NvsPageTableAddress;
-  ZeroMem ((VOID *)NewPage, EFI_PAGES_TO_SIZE(PageNum));
-  mS3NvsPageTableAddress += EFI_PAGES_TO_SIZE(PageNum);
-  return NewPage;
+  UINTN             Address;
+
+  Address = mPageFaultBuffer + EFI_PAGES_TO_SIZE (mPageFaultIndex);
+  ZeroMem ((VOID *) Address, EFI_PAGES_TO_SIZE (1));
+
+  //
+  // Cut the previous uplink if it exists and wasn't overwritten.
+  //
+  if ((mPageFaultUplink[mPageFaultIndex] != NULL) && 
((*mPageFaultUplink[mPageFaultIndex] & mPhyMask) == Address)) {
+    *mPageFaultUplink[mPageFaultIndex] = 0;
+  }
+
+  //
+  // Link & Record the current uplink.
+  //
+  *Uplink = Address | IA32_PG_P | IA32_PG_RW;
+  mPageFaultUplink[mPageFaultIndex] = Uplink;
+
+  mPageFaultIndex = (mPageFaultIndex + 1) % EXTRA_PAGE_TABLE_PAGES;
 }
 
 /**
@@ -177,21 +243,21 @@
   PTIndex = BitFieldRead64 (PFAddress, 39, 47);
   // PML4E
   if ((PageTable[PTIndex] & IA32_PG_P) == 0) {
-    PageTable[PTIndex] = GetNewPage (1) | IA32_PG_P | IA32_PG_RW;
+    AcquirePage (&PageTable[PTIndex]);
   }
   PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & mPhyMask);
   PTIndex = BitFieldRead64 (PFAddress, 30, 38);
   // PDPTE
   if (mPage1GSupport) {
-    PageTable[PTIndex] = PFAddress | IA32_PG_P | IA32_PG_RW | IA32_PG_PS;
+    PageTable[PTIndex] = (PFAddress & ~((1ull << 30) - 1)) | IA32_PG_P | 
IA32_PG_RW | IA32_PG_PS;
   } else {
     if ((PageTable[PTIndex] & IA32_PG_P) == 0) {
-      PageTable[PTIndex] = GetNewPage (1) | IA32_PG_P | IA32_PG_RW;
+      AcquirePage (&PageTable[PTIndex]);
     }
     PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & mPhyMask);
     PTIndex = BitFieldRead64 (PFAddress, 21, 29);
     // PD
-    PageTable[PTIndex] = PFAddress | IA32_PG_P | IA32_PG_RW | IA32_PG_PS;
+    PageTable[PTIndex] = (PFAddress & ~((1ull << 21) - 1)) | IA32_PG_P | 
IA32_PG_RW | IA32_PG_PS;
   }
 
   return TRUE;


------------------------------------------------------------------------------
_______________________________________________
edk2-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/edk2-commits

Reply via email to