Re: [edk2] [PATCH v2] MdeModulePkg PeiCore: Add PCD to specify PEIM Shadow

2015-07-30 Thread Zeng, Star
Reviewed-by: Star Zeng 

-Original Message-
From: edk2-devel [mailto:edk2-devel-boun...@lists.01.org] On Behalf Of Liming 
Gao
Sent: Thursday, July 30, 2015 5:40 PM
To: edk2-devel@lists.01.org
Subject: [edk2] [PATCH v2] MdeModulePkg PeiCore: Add PCD to specify PEIM Shadow

v2 changelog:
Check CurrentPeimHandle to check the matched PeimHandle.
Add check point to ShadowPeiCore based on PCD.

v1 changelog:
PeiCore LoadImage always shadow itself and PEIM on normal boot after the 
physical memory is installed. On the emulator platform, the shadow may be not 
necessary. To support such usage, new PCD PcdShadowPeimOnBoot is introduced to 
specify whether loads PEIM in memory by default.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Liming Gao 
---
 MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c |  21 +---
 MdeModulePkg/Core/Pei/Image/Image.c   |  33 ++
 MdeModulePkg/Core/Pei/PeiMain.inf |   1 +
 MdeModulePkg/Core/Pei/PeiMain/PeiMain.c   |   6 -
 MdeModulePkg/MdeModulePkg.dec |   7 ++
 MdeModulePkg/MdeModulePkg.uni | Bin 166792 -> 168226 bytes
 6 files changed, 59 insertions(+), 9 deletions(-)

diff --git a/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c 
b/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
index 3a85502..46e990d 100644
--- a/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
+++ b/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
@@ -695,10 +695,13 @@ PeiDispatcher (
 
 for (Index1 = 0; Index1 <= SaveCurrentFvCount; Index1++) {
   for (Index2 = 0; (Index2 < PcdGet32 (PcdPeiCoreMaxPeimPerFv)) && 
(Private->Fv[Index1].FvFileHandles[Index2] != NULL); Index2++) {
 if (Private->Fv[Index1].PeimState[Index2] == 
PEIM_STATE_REGISITER_FOR_SHADOW) {
   PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2];
+  Private->CurrentFileHandle   = PeimFileHandle;
+  Private->CurrentPeimFvCount  = Index1;
+  Private->CurrentPeimCount= Index2;
   Status = PeiLoadImage (
 (CONST EFI_PEI_SERVICES **) &Private->Ps,
 PeimFileHandle,
 PEIM_STATE_REGISITER_FOR_SHADOW,
 &EntryPoint,
@@ -707,13 +710,10 @@ PeiDispatcher (
   if (Status == EFI_SUCCESS) {
 //
 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE
 //
 Private->Fv[Index1].PeimState[Index2]++;
-Private->CurrentFileHandle   = PeimFileHandle;
-Private->CurrentPeimFvCount  = Index1;
-Private->CurrentPeimCount= Index2;
 //
 // Call the PEIM entry point
 //
 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;
 
@@ -1106,10 +1106,25 @@ PeiDispatcher (
   //
   // If memory is availble we shadow images by default for 
performance reasons.
   // We call the entry point a 2nd time so the module knows it's 
shadowed.
   //
   //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);
+  if ((Private->HobList.HandoffInformationTable->BootMode != 
BOOT_ON_S3_RESUME) && !PcdGetBool (PcdShadowPeimOnBoot)) {
+//
+// Load PEIM into Memory for Register for shadow PEIM.
+//
+Status = PeiLoadImage (
+   PeiServices,
+   PeimFileHandle,
+   PEIM_STATE_REGISITER_FOR_SHADOW,
+   &EntryPoint,
+   &AuthenticationState
+   );
+if (Status == EFI_SUCCESS) {
+  PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;
+}
+  }
   ASSERT (PeimEntryPoint != NULL);
   PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) 
PeiServices);
   //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);
 
   //
diff --git a/MdeModulePkg/Core/Pei/Image/Image.c 
b/MdeModulePkg/Core/Pei/Image/Image.c
index cab08fe..9c54192 100644
--- a/MdeModulePkg/Core/Pei/Image/Image.c
+++ b/MdeModulePkg/Core/Pei/Image/Image.c
@@ -1,9 +1,9 @@
 /** @file
   Pei Core Load Image Support
 
-Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.
 This program and the accompanying materials  are licensed and made available 
under the terms and conditions of the BSD License  which accompanies this 
distribution.  The full text of the license may be found at  
http://opensource.org/licenses/bsd-license.php
 
@@ -115,11 +115,12 @@ GetImageReadFunction (
   PEI_CORE_INSTANCE  *Private;
   VOID*  MemoryBuffer;
 
   Private = PEI_CORE_INS

[edk2] [PATCH v2] MdeModulePkg PeiCore: Add PCD to specify PEIM Shadow

2015-07-30 Thread Liming Gao
v2 changelog:
Check CurrentPeimHandle to check the matched PeimHandle.
Add check point to ShadowPeiCore based on PCD.

v1 changelog:
PeiCore LoadImage always shadow itself and PEIM on normal boot after
the physical memory is installed. On the emulator platform, the shadow
may be not necessary. To support such usage, new PCD PcdShadowPeimOnBoot
is introduced to specify whether loads PEIM in memory by default.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Liming Gao 
---
 MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c |  21 +---
 MdeModulePkg/Core/Pei/Image/Image.c   |  33 ++
 MdeModulePkg/Core/Pei/PeiMain.inf |   1 +
 MdeModulePkg/Core/Pei/PeiMain/PeiMain.c   |   6 -
 MdeModulePkg/MdeModulePkg.dec |   7 ++
 MdeModulePkg/MdeModulePkg.uni | Bin 166792 -> 168226 bytes
 6 files changed, 59 insertions(+), 9 deletions(-)

diff --git a/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c 
b/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
index 3a85502..46e990d 100644
--- a/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
+++ b/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
@@ -695,10 +695,13 @@ PeiDispatcher (
 
 for (Index1 = 0; Index1 <= SaveCurrentFvCount; Index1++) {
   for (Index2 = 0; (Index2 < PcdGet32 (PcdPeiCoreMaxPeimPerFv)) && 
(Private->Fv[Index1].FvFileHandles[Index2] != NULL); Index2++) {
 if (Private->Fv[Index1].PeimState[Index2] == 
PEIM_STATE_REGISITER_FOR_SHADOW) {
   PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2];
+  Private->CurrentFileHandle   = PeimFileHandle;
+  Private->CurrentPeimFvCount  = Index1;
+  Private->CurrentPeimCount= Index2;
   Status = PeiLoadImage (
 (CONST EFI_PEI_SERVICES **) &Private->Ps,
 PeimFileHandle,
 PEIM_STATE_REGISITER_FOR_SHADOW,
 &EntryPoint,
@@ -707,13 +710,10 @@ PeiDispatcher (
   if (Status == EFI_SUCCESS) {
 //
 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE
 //
 Private->Fv[Index1].PeimState[Index2]++;
-Private->CurrentFileHandle   = PeimFileHandle;
-Private->CurrentPeimFvCount  = Index1;
-Private->CurrentPeimCount= Index2;
 //
 // Call the PEIM entry point
 //
 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;
 
@@ -1106,10 +1106,25 @@ PeiDispatcher (
   //
   // If memory is availble we shadow images by default for 
performance reasons.
   // We call the entry point a 2nd time so the module knows it's 
shadowed.
   //
   //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);
+  if ((Private->HobList.HandoffInformationTable->BootMode != 
BOOT_ON_S3_RESUME) && !PcdGetBool (PcdShadowPeimOnBoot)) {
+//
+// Load PEIM into Memory for Register for shadow PEIM.
+//
+Status = PeiLoadImage (
+   PeiServices,
+   PeimFileHandle,
+   PEIM_STATE_REGISITER_FOR_SHADOW,
+   &EntryPoint,
+   &AuthenticationState
+   );
+if (Status == EFI_SUCCESS) {
+  PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;
+}
+  }
   ASSERT (PeimEntryPoint != NULL);
   PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) 
PeiServices);
   //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);
 
   //
diff --git a/MdeModulePkg/Core/Pei/Image/Image.c 
b/MdeModulePkg/Core/Pei/Image/Image.c
index cab08fe..9c54192 100644
--- a/MdeModulePkg/Core/Pei/Image/Image.c
+++ b/MdeModulePkg/Core/Pei/Image/Image.c
@@ -1,9 +1,9 @@
 /** @file
   Pei Core Load Image Support
 
-Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD 
License
 which accompanies this distribution.  The full text of the license may be 
found at
 http://opensource.org/licenses/bsd-license.php
 
@@ -115,11 +115,12 @@ GetImageReadFunction (
   PEI_CORE_INSTANCE  *Private;
   VOID*  MemoryBuffer;
 
   Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
   
-  if (Private->PeiMemoryInstalled  && 
((Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME) || 
PcdGetBool (PcdShadowPeimOnS3Boot))  &&
+  if (Private->PeiMemoryInstalled  && 
(((Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME) && 
PcdGetBool (PcdShadowPeimOnBoot)) || 
+  ((Private->HobList.HandoffInform