* [PATCH 1/3] EmulatorPkg/WinHost: pre-allocate "physical" RAM
2022-11-12 4:00 [PATCH 0/3] Add reset support in Emulator/WinHost PEI Ni, Ray
@ 2022-11-12 4:00 ` Ni, Ray
2022-11-12 4:00 ` [PATCH 2/3] EmulatorPkg/WinHost: XIP for SEC and PEI_CORE Ni, Ray
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Ni, Ray @ 2022-11-12 4:00 UTC (permalink / raw)
To: devel; +Cc: Andrew Fish, Zhiguang Liu
Move the "physical" RAM allocation from WinPeiAutoScan
to main() entrypoint.
This is to prepare the changes for "reset" support.
Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Andrew Fish <afish@apple.com>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
---
EmulatorPkg/Win/Host/WinHost.c | 60 ++++++++++++++--------------------
1 file changed, 25 insertions(+), 35 deletions(-)
diff --git a/EmulatorPkg/Win/Host/WinHost.c b/EmulatorPkg/Win/Host/WinHost.c
index 93247c5043..5b780ca8af 100644
--- a/EmulatorPkg/Win/Host/WinHost.c
+++ b/EmulatorPkg/Win/Host/WinHost.c
@@ -8,7 +8,7 @@
This code produces 128 K of temporary memory for the SEC stack by directly
allocate memory space with ReadWrite and Execute attribute.
-Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2022, Intel Corporation. All rights reserved.<BR>
(C) Copyright 2016-2020 Hewlett Packard Enterprise Development LP<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
@@ -87,14 +87,6 @@ WinPeiAutoScan (
return EFI_UNSUPPORTED;
}
- //
- // Allocate enough memory space for emulator
- //
- gSystemMemory[Index].Memory = (EFI_PHYSICAL_ADDRESS)(UINTN)VirtualAlloc (NULL, (SIZE_T)(gSystemMemory[Index].Size), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
- if (gSystemMemory[Index].Memory == 0) {
- return EFI_OUT_OF_RESOURCES;
- }
-
*MemoryBase = gSystemMemory[Index].Memory;
*MemorySize = gSystemMemory[Index].Size;
@@ -457,6 +449,30 @@ Returns:
exit (1);
}
+ //
+ // Allocate "physical" memory space for emulator. It will be reported out later throuth MemoryAutoScan()
+ //
+ for (Index = 0, Done = FALSE; !Done; Index++) {
+ ASSERT (Index < gSystemMemoryCount);
+ gSystemMemory[Index].Size = ((UINT64)_wtoi (MemorySizeStr)) * ((UINT64)SIZE_1MB);
+ gSystemMemory[Index].Memory = (EFI_PHYSICAL_ADDRESS)(UINTN)VirtualAlloc (NULL, (SIZE_T)(gSystemMemory[Index].Size), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
+ if (gSystemMemory[Index].Memory == 0) {
+ return EFI_OUT_OF_RESOURCES;
+ }
+
+ //
+ // Find the next region
+ //
+ for (Index1 = 0; MemorySizeStr[Index1] != '!' && MemorySizeStr[Index1] != 0; Index1++) {
+ }
+
+ if (MemorySizeStr[Index1] == 0) {
+ Done = TRUE;
+ }
+
+ MemorySizeStr = MemorySizeStr + Index1 + 1;
+ }
+
//
// Allocate space for gSystemMemory Array
//
@@ -575,32 +591,6 @@ Returns:
SecPrint ("\n\r");
}
- //
- // Calculate memory regions and store the information in the gSystemMemory
- // global for later use. The autosizing code will use this data to
- // map this memory into the SEC process memory space.
- //
- for (Index = 0, Done = FALSE; !Done; Index++) {
- //
- // Save the size of the memory and make a Unicode filename SystemMemory00, ...
- //
- gSystemMemory[Index].Size = ((UINT64)_wtoi (MemorySizeStr)) * ((UINT64)SIZE_1MB);
-
- //
- // Find the next region
- //
- for (Index1 = 0; MemorySizeStr[Index1] != '!' && MemorySizeStr[Index1] != 0; Index1++) {
- }
-
- if (MemorySizeStr[Index1] == 0) {
- Done = TRUE;
- }
-
- MemorySizeStr = MemorySizeStr + Index1 + 1;
- }
-
- SecPrint ("\n\r");
-
//
// Hand off to SEC Core
//
--
2.37.2.windows.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 2/3] EmulatorPkg/WinHost: XIP for SEC and PEI_CORE
2022-11-12 4:00 [PATCH 0/3] Add reset support in Emulator/WinHost PEI Ni, Ray
2022-11-12 4:00 ` [PATCH 1/3] EmulatorPkg/WinHost: pre-allocate "physical" RAM Ni, Ray
@ 2022-11-12 4:00 ` Ni, Ray
2022-11-12 4:00 ` [PATCH 3/3] EmulatorPkg/WinHost: Add Reset2 PPI Ni, Ray
2022-11-18 6:31 ` [edk2-devel] [PATCH 0/3] Add reset support in Emulator/WinHost PEI Zhiguang Liu
3 siblings, 0 replies; 6+ messages in thread
From: Ni, Ray @ 2022-11-12 4:00 UTC (permalink / raw)
To: devel; +Cc: Zhiguang Liu
In EmulatorPkg/Win, SEC and PEI_CORE are loaded to memory allocated
through VirtualAlloc. Though the corresponding DLL files are loaded
and the entry points in DLL files are executed. The loading to memory
allocated through VirtualAlloc is for the case when the DLL files can
not be loaded.
Actually some PEIMs like PcdPeim which are loaded before
"physical" RAM is discovered, they are executing in the original
location (FV) like XIP module in real platform.
The SEC and PEI_CORE can follow the same mechanism.
So, the VirtualAlloc call is removed.
This is to prepare the "reset" support to avoid additional OS memory
consumption when reset happens.
Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
Cc: Andrew Fish <afish@apple.com
---
EmulatorPkg/Win/Host/WinHost.c | 14 ++------------
1 file changed, 2 insertions(+), 12 deletions(-)
diff --git a/EmulatorPkg/Win/Host/WinHost.c b/EmulatorPkg/Win/Host/WinHost.c
index 5b780ca8af..9b10290ff3 100644
--- a/EmulatorPkg/Win/Host/WinHost.c
+++ b/EmulatorPkg/Win/Host/WinHost.c
@@ -718,19 +718,9 @@ SecPeCoffGetEntryPoint (
}
//
- // Allocate space in NT (not emulator) memory with ReadWrite and Execute attribute.
- // Extra space is for alignment
+ // XIP for SEC and PEI_CORE
//
- ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)VirtualAlloc (NULL, (SIZE_T)(ImageContext.ImageSize + (ImageContext.SectionAlignment * 2)), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
- if (ImageContext.ImageAddress == 0) {
- return EFI_OUT_OF_RESOURCES;
- }
-
- //
- // Align buffer on section boundary
- //
- ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
- ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);
+ ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Pe32Data;
Status = PeCoffLoaderLoadImage (&ImageContext);
if (EFI_ERROR (Status)) {
--
2.37.2.windows.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 3/3] EmulatorPkg/WinHost: Add Reset2 PPI
2022-11-12 4:00 [PATCH 0/3] Add reset support in Emulator/WinHost PEI Ni, Ray
2022-11-12 4:00 ` [PATCH 1/3] EmulatorPkg/WinHost: pre-allocate "physical" RAM Ni, Ray
2022-11-12 4:00 ` [PATCH 2/3] EmulatorPkg/WinHost: XIP for SEC and PEI_CORE Ni, Ray
@ 2022-11-12 4:00 ` Ni, Ray
2022-11-18 6:31 ` [edk2-devel] [PATCH 0/3] Add reset support in Emulator/WinHost PEI Zhiguang Liu
3 siblings, 0 replies; 6+ messages in thread
From: Ni, Ray @ 2022-11-12 4:00 UTC (permalink / raw)
To: devel; +Cc: Andrew Fish, Zhiguang Liu
When shutdown is requested, WinHost exits.
Otherwise, WinHost re-runs from SEC.
Tested no extra memory consumption with multiple resets in PEI.
Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Andrew Fish <afish@apple.com>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
---
EmulatorPkg/Win/Host/WinHost.c | 75 ++++++++++++++++++++++++++++----
EmulatorPkg/Win/Host/WinHost.h | 3 +-
EmulatorPkg/Win/Host/WinHost.inf | 3 +-
3 files changed, 71 insertions(+), 10 deletions(-)
diff --git a/EmulatorPkg/Win/Host/WinHost.c b/EmulatorPkg/Win/Host/WinHost.c
index 9b10290ff3..32b6922307 100644
--- a/EmulatorPkg/Win/Host/WinHost.c
+++ b/EmulatorPkg/Win/Host/WinHost.c
@@ -56,6 +56,14 @@ NT_FD_INFO *gFdInfo;
UINTN gSystemMemoryCount = 0;
NT_SYSTEM_MEMORY *gSystemMemory;
+BASE_LIBRARY_JUMP_BUFFER mResetJumpBuffer;
+CHAR8 *mResetTypeStr[] = {
+ "EfiResetCold",
+ "EfiResetWarm",
+ "EfiResetShutdown",
+ "EfiResetPlatformSpecific"
+};
+
/*++
Routine Description:
@@ -196,6 +204,45 @@ SecPrint (
);
}
+/**
+ Resets the entire platform.
+
+ @param[in] ResetType The type of reset to perform.
+ @param[in] ResetStatus The status code for the reset.
+ @param[in] DataSize The size, in bytes, of ResetData.
+ @param[in] ResetData For a ResetType of EfiResetCold, EfiResetWarm, or EfiResetShutdown
+ the data buffer starts with a Null-terminated string, optionally
+ followed by additional binary data. The string is a description
+ that the caller may use to further indicate the reason for the
+ system reset.
+
+**/
+VOID
+EFIAPI
+WinReset (
+ IN EFI_RESET_TYPE ResetType,
+ IN EFI_STATUS ResetStatus,
+ IN UINTN DataSize,
+ IN VOID *ResetData OPTIONAL
+ )
+{
+ ASSERT (ResetType <= EfiResetPlatformSpecific);
+ SecPrint (" Emu ResetSystem is called: ResetType = %s\n", mResetTypeStr[ResetType]);
+
+ if (ResetType == EfiResetShutdown) {
+ exit (0);
+ } else {
+ //
+ // Jump back to SetJump with jump code = ResetType + 1
+ //
+ LongJump (&mResetJumpBuffer, ResetType + 1);
+ }
+}
+
+EFI_PEI_RESET2_PPI mEmuReset2Ppi = {
+ WinReset
+};
+
/*++
Routine Description:
@@ -388,6 +435,7 @@ Returns:
UINTN ProcessAffinityMask;
UINTN SystemAffinityMask;
INT32 LowBit;
+ UINTN ResetJumpCode;
//
// Enable the privilege so that RTC driver can successfully run SetTime()
@@ -430,6 +478,7 @@ Returns:
// PPIs pased into PEI_CORE
//
AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEmuThunkPpiGuid, &mSecEmuThunkPpi);
+ AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEfiPeiReset2PpiGuid, &mEmuReset2Ppi);
//
// Emulator Bus Driver Thunks
@@ -500,14 +549,6 @@ Returns:
exit (1);
}
- SetMem32 (TemporaryRam, TemporaryRamSize, PcdGet32 (PcdInitValueInTempStack));
-
- SecPrint (
- " OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n\r",
- TemporaryRamSize / SIZE_1KB,
- TemporaryRam
- );
-
//
// If enabled use the magic page to communicate between modules
// This replaces the PI PeiServicesTable pointer mechanism that
@@ -591,6 +632,24 @@ Returns:
SecPrint ("\n\r");
}
+ ResetJumpCode = SetJump (&mResetJumpBuffer);
+
+ //
+ // Do not clear memory content for warm reset.
+ //
+ if (ResetJumpCode != EfiResetWarm + 1) {
+ SecPrint (" OS Emulator clearing temp RAM and physical RAM (to be discovered later)......\n\r");
+ SetMem32 (TemporaryRam, TemporaryRamSize, PcdGet32 (PcdInitValueInTempStack));
+ for (Index = 0; Index < gSystemMemoryCount; Index++) {
+ SetMem32 ((VOID *)(UINTN)gSystemMemory[Index].Memory, gSystemMemory[Index].Size, PcdGet32 (PcdInitValueInTempStack));
+ }
+ }
+
+ SecPrint (
+ " OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n\r",
+ TemporaryRamSize / SIZE_1KB,
+ TemporaryRam
+ );
//
// Hand off to SEC Core
//
diff --git a/EmulatorPkg/Win/Host/WinHost.h b/EmulatorPkg/Win/Host/WinHost.h
index 49d42d1ad8..a9a21007e3 100644
--- a/EmulatorPkg/Win/Host/WinHost.h
+++ b/EmulatorPkg/Win/Host/WinHost.h
@@ -1,6 +1,6 @@
/**@file
-Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2022, Intel Corporation. All rights reserved.<BR>
(C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -26,6 +26,7 @@ Abstract:
#include <Guid/FileSystemInfo.h>
#include <Guid/FileSystemVolumeLabelInfo.h>
#include <Ppi/EmuThunk.h>
+#include <Ppi/Reset2.h>
#include <Protocol/EmuThunk.h>
#include <Protocol/SimpleFileSystem.h>
diff --git a/EmulatorPkg/Win/Host/WinHost.inf b/EmulatorPkg/Win/Host/WinHost.inf
index 2030ac0847..b61901fae2 100644
--- a/EmulatorPkg/Win/Host/WinHost.inf
+++ b/EmulatorPkg/Win/Host/WinHost.inf
@@ -2,7 +2,7 @@
# Entry Point of Win Emulator
#
# Main executable file of Win Emulator that loads Sec core after initialization finished.
-# Copyright (c) 2008 - 2019, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2008 - 2022, Intel Corporation. All rights reserved.<BR>
# Portions copyright (c) 2008 - 2011, Apple Inc. All rights reserved.<BR>
# (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
#
@@ -58,6 +58,7 @@
[Ppis]
gEmuThunkPpiGuid
+ gEfiPeiReset2PpiGuid
[Protocols]
gEmuIoThunkProtocolGuid
--
2.37.2.windows.2
^ permalink raw reply related [flat|nested] 6+ messages in thread