public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms ***
@ 2018-04-06 14:42 Supreeth Venkatesh
  2018-04-06 14:42 ` [PATCH v1 01/18] ArmPkg: Add PCDs needed for MM communication driver Supreeth Venkatesh
                   ` (18 more replies)
  0 siblings, 19 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

***
PI Specification v1.5  "Volume 4: Management Mode Core Interface" 
introduces the concept of MM Standalone Mode. Initialization of this mode
can be done during the SEC phase (Section 1.5.2). 
On ARMv8-A systems, ARM Trusted Firmware is responsible for launching 
the normal world firmware e.g. UEFI.

The Standalone MM environment is instantiated in Secure EL0 as a separate
firmware volume. It executes as BL32 Image under control of ARM TF 
which is instantiated in EL3. Both components execute in the AArch64 execution state.
This patchset will build upon the StandaloneSmmPkg module originally 
contributed by Intel.

This package can be used in conjunction with ARM Trusted Firmware
to recreate a simple MM secure environment that demonstrates communication 
between two UEFI images where one is executing in the normal world and the other is
executing in the secure world.

The normal world image includes:
MM Communication DXE runtime driver that implements the protocol for communication
with the MM environment in the secure world.

The secure world image includes:
The MM Standalone framework.

This patchset includes the proposed organization/structure.
In order to be able to review the changes more effectively, the changes are present here:
https://github.com/supven01/edk2.git (Branch: master)

Steps to build MM Standalone images
In user preferred "work" directory, execute the following shell commands

git clone https://github.com/tianocore/edk2.git
git checkout master

git clone https://github.com/tianocore/edk2-platforms.git
git checkout master

mkdir arm-tf
cd arm-tf
git clone https://github.com/ARM-software/arm-trusted-firmware.git .
git checkout master
cd ..

git clone https://git.linaro.org/uefi/uefi-tools.git .
git checkout master

The following will build the MM Standalone image which runs in secure world.
./uefi-tools/edk2-build.sh -b DEBUG fvp_mm_standalone

The follwing will build the normal world UEFI image, ARM Trusted Firmware and 
a Firmware Image Package (FIP) that includes both the UEFI images.
./uefi-tools/edk2-build.sh -a ./arm-tf -b DEBUG fvp_mm_normal

Boot Loader Stage 1 (BL1) binary and combined arm-tf/uefi firmware image package (fip) binary will be generated at:

Build Output
Build/ArmVExpress-FVP-AArch64-MM-Normal/DEBUG_GCC5/FV/bl1.bin
Build/ArmVExpress-FVP-AArch64-MM-Normal/DEBUG_GCC5/FV/fip.bin

Steps to run MM Standalone image
1. Download the ARMv8 Architecture FVP from
     https://silver.arm.com/download/download.tm?pv=3744408&p=1424570
     For more information, please refer
     https://developer.arm.com/products/system-design/fixed-virtual-platforms
2.  Install FVP into preferred "work" directory.
3.  Create a shell script "run_mm.sh" in the same folder where "FVP_Base_AEMv8A-AEMv8A" is present. 
     Sample Shell script below: 
    ./FVP_Base_AEMv8A-AEMv8A 
    -C cache_state_modelled=0 
    -C bp.secure_memory=1 
    -C bp.tzc_400.diagnostics=1 
    -C bp.pl011_uart0.untimed_fifos=0 
    -C cluster1.NUM_CORES=4 
    -C cluster0.NUM_CORES=4 
    -C bp.pl011_uart0.out_file=uart0.output 
    -C bp.pl011_uart1.out_file=uart1.output 
    -C bp.pl011_uart2.out_file=uart2.output 
    -C bp.pl011_uart3.out_file=uart3.output 
    -C bp.secureflashloader.fname="" 
    -C bp.flashloader0.fname="" 
    -S -R
4. ./run_mm.sh
5. Output can be seen on FVP console.
6. The normal world will boot to the UEFI shell.

Sample Output

MM Standalone Output (FVP UART2)
SPM Version: Major=0x0, Minor=0x1
NumSpMemRegions - 0x6
SpMemBase       - 0xFF200000
SpMemLimit      - 0x100000000
SpImageBase     - 0xFF200000
SpStackBase     - 0xFF610000
SpHeapBase      - 0xFF620000
SpNsCommBufBase - 0xFF600000
SpSharedBufBase - 0xFF500000
SpImageSize     - 0x300000
SpPcpuStackSize - 0x2000
SpHeapSize      - 0x9E0000
SpNsCommBufSize - 0x10000
SpPcpuSharedBufSize - 0x20000
NumCpus         - 0x8
CpuInfo         - 0xFF500680
Mpidr           - 0x80000000
LinearId        - 0x0
Flags           - 0x1
Mpidr           - 0x80000001
LinearId        - 0x1
Flags           - 0x0
Mpidr           - 0x80000002
LinearId        - 0x2
Flags           - 0x0
Mpidr           - 0x80000003
LinearId        - 0x3
Flags           - 0x0
Mpidr           - 0x80000100
LinearId        - 0x4
Flags           - 0x0
Mpidr           - 0x80000101
LinearId        - 0x5
Flags           - 0x0
Mpidr           - 0x80000102
LinearId        - 0x6
Flags           - 0x0
Mpidr           - 0x80000103
LinearId        - 0x7
Flags           - 0x0
Found Standalone MM PE data - 0xFF201000
Found Standalone MM PE data - 0xFF201000
Standalone MM Core PE-COFF SectionHeaderOffset - 0xF60, NumberOfSections - 3
UpdateMmFoundationPeCoffPermissions: Section 0 of image at 0xFF201000 has 0x60000020 permissions
UpdateMmFoundationPeCoffPermissions: Section 0 of image at 0xFF201000 has .et name
UpdateMmFoundationPeCoffPermissions: Section 0 of image at 0xFF201000 has 0xFF202000 address
UpdateMmFoundationPeCoffPermissions: Section 0 of image at 0xFF201000 has 0x1000 data
UpdateMmFoundationPeCoffPermissions: Ignoring section 0 of image at 0xFF201000 with 0x60000020 permissions
UpdateMmFoundationPeCoffPermissions: Section 1 of image at 0xFF201000 has 0xC0000040 permissions
UpdateMmFoundationPeCoffPermissions: Section 1 of image at 0xFF201000 has .aa name
UpdateMmFoundationPeCoffPermissions: Section 1 of image at 0xFF201000 has 0xFF217000 address
UpdateMmFoundationPeCoffPermissions: Section 1 of image at 0xFF201000 has 0x16000 data
UpdateMmFoundationPeCoffPermissions: Mapping section 1 of image at 0xFF201000 with RW-XN permissions
UpdateMmFoundationPeCoffPermissions: Section 2 of image at 0xFF201000 has 0x42000040 permissions
UpdateMmFoundationPeCoffPermissions: Section 2 of image at 0xFF201000 has .eo name
UpdateMmFoundationPeCoffPermissions: Section 2 of image at 0xFF201000 has 0xFF218000 address
UpdateMmFoundationPeCoffPermissions: Section 2 of image at 0xFF201000 has 0x17000 data
UpdateMmFoundationPeCoffPermissions: Mapping section 2 of image at 0xFF201000 with RO-XN permissions
StandaloneMmCoreMemoryAllocationLibConstructor - 0xFF620000
MmramRangeCount - 0x6
MmramRanges[0]: 0x00000000FF200000 - 0x0000000000300000
MmramRanges[1]: 0x00000000FF500000 - 0x0000000000100000
MmramRanges[2]: 0x00000000FF600000 - 0x0000000000010000
MmramRanges[3]: 0x00000000FF610000 - 0x0000000000010000
MmramRanges[4]: 0x00000000FF620000 - 0x00000000000002C8
MmramRanges[5]: 0x00000000FF6202C8 - 0x00000000009DFD38
MmInitializeMemoryServices
MmAddMemoryRegion 0 : 0x00000000FF200000 - 0x0000000000300000
MmAddMemoryRegion 1 : 0x00000000FF500000 - 0x0000000000100000
MmAddMemoryRegion 2 : 0x00000000FF600000 - 0x0000000000010000
MmAddMemoryRegion 3 : 0x00000000FF610000 - 0x0000000000010000
MmAddMemoryRegion 4 : 0x00000000FF620000 - 0x00000000000002C8
MmAddMemoryRegion 5 : 0x00000000FF6202C8 - 0x00000000009DFD38
mMmMemLibInternalMaximumSupportAddress = 0xFFFFFFFFF
MmMain - 0xFF620000
MmramRangeCount - 0x6
MmramRanges[0]: 0x00000000FF200000 - 0x300000
MmramRanges[1]: 0x00000000FF500000 - 0x100000
MmramRanges[2]: 0x00000000FF600000 - 0x10000
MmramRanges[3]: 0x00000000FF610000 - 0x10000
MmramRanges[4]: 0x00000000FF620000 - 0x2C8
MmramRanges[5]: 0x00000000FF6202C8 - 0x9DFD38
mMmramRangeCount - 0x6
mMmramRanges - 0xFFFFEE10
BFV address - 0xFF200000
BFV size    - 0x300000
MmInstallConfigurationTable For HobList
HobSize - 0x2C8
MmHobStart - 0xFFFFE810
MmRegisterProtocolNotify - MmConfigurationMmProtocol
Mm Dispatch StandaloneBfvAddress - 0xFF200000
MmCoreFfsFindMmDriver - 0xFF200000
FvIsBeingProcesssed - 0xFF200000
Check MmFileTypes - 0xA
Check MmFileTypes - 0xE
Find PE data - 0xFF219024
MmAddToDriverList - 58F7A62B-6280-42A7-BC38-10535A64A92C (0xFF219024)
MmDispatcher
  Drain the Scheduled Queue
  Search DriverList for items to place on Scheduled Queue
  DriverEntry (Discovered) - 58F7A62B-6280-42A7-BC38-10535A64A92C
Evaluate MM DEPEX for FFS(58F7A62B-6280-42A7-BC38-10535A64A92C)
  TRUE
  END
  RESULT = TRUE
  Drain the Scheduled Queue
  DriverEntry (Scheduled) - 58F7A62B-6280-42A7-BC38-10535A64A92C
MmLoadImage - 58F7A62B-6280-42A7-BC38-10535A64A92C
UpdatePeCoffPermissions: Mapping section 0 of image at 0xFFFE7000 with RO-XN permissions and size 0x7000
UpdatePeCoffPermissions: Mapping section 0 of image at 0xFFFE7000 with RO-X permissions and size 0x7000
UpdatePeCoffPermissions: Mapping section 1 of image at 0xFFFEE000 with RW-XN permissions and size 0x1000
UpdatePeCoffPermissions: Mapping section 2 of image at 0xFFFEF000 with RO-XN permissions and size 0x1000
add-symbol-file /home/supven01/work/mm_upstream/Build/StandaloneMmPkg/DEBUG_GCC5/AARCH64/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver/DEBUG/PiMmStandloneArmTfCpuDriver.dll 0xFFFE7000
Loading MM driver at 0x000FFFE6000 EntryPoint=0x000FFFE7000 PiMmStandloneArmTfCpuDriver.efi
StartImage - 0xFFFE7000 (Standalone Mode)
MmInstallProtocolInterface: 26EEB3DE-B689-492E-80F0-BE8BD7DA4BA7 FFFEE008
MmConfigurationMmNotify(26EEB3DE-B689-492E-80F0-BE8BD7DA4BA7) - FFFEE008
MM Core registered MM Entry Point address FF2041C0
MmInstallProtocolInterface: EB346B97-975F-4A9F-8B22-F8E92BB3D569 FFFEE018
Sharing Cpu Driver EP *0xFF217280 = 0xFFFE798C
mNsCommBuffer.PhysicalStart - 0xFF600000
mNsCommBuffer.PhysicalSize - 0x10000
mNsCommBuffer: 0x00000000FF600000 - 0x10000
mMpInformationHobData: 0x0000000000000008 - 0x8
mMpInformationHobData[0x80000000]: 0, 0, 0
mMpInformationHobData[0x80000001]: 0, 1, 1
mMpInformationHobData[0x80000002]: 0, 2, 2
mMpInformationHobData[0x80000003]: 0, 3, 3
mMpInformationHobData[0x80000100]: 1, 0, 0
mMpInformationHobData[0x80000101]: 1, 1, 1
mMpInformationHobData[0x80000102]: 1, 2, 2
mMpInformationHobData[0x80000103]: 1, 3, 3
MmiHandlerRegister - GUID B65694CC-09E3-4C3B-B5CD-05F44D3CDBFF - Status 0
MmiHandlerRegister - GUID 7081E22F-CAC6-4053-9468-675782CF88E5 - Status 0
MmiHandlerRegister - GUID 60FF8964-E906-41D0-AFED-F241E974E08E - Status 0
MmiHandlerRegister - GUID 02CE967A-DD7E-4FFC-9EE7-810CF0470880 - Status 0
MmiHandlerRegister - GUID 2A571201-4966-47F6-8B86-F31E41F32F10 - Status 0
MmiHandlerRegister - GUID 27ABF055-B1B8-4C26-8048-748F37BAA2DF - Status 0
MmiHandlerRegister - GUID 7CE88FB3-4BD7-4679-87A8-A8D8DEE50D2B - Status 0
MmMain Done!
Shared Cpu Driver EP 0xFFFE798C

ARM TF (FVP UART0)
NOTICE:  Booting Trusted Firmware
NOTICE:  BL1: v1.4(debug):v1.4-96-gae48aad7-dirty
NOTICE:  BL1: Built : 16:14:26, Nov  1 2017
INFO:    BL1: RAM 0x4036000 - 0x403c000
INFO:    BL1: Loading BL2
INFO:    Loading image id=1 at address 0x4027000
INFO:    Image id=1 loaded: 0x4027000 - 0x402c2a8
NOTICE:  BL1: Booting BL2
INFO:    Entry point address = 0x4027000
INFO:    SPSR = 0x3c5
NOTICE:  BL2: v1.4(debug):v1.4-96-gae48aad7-dirty
NOTICE:  BL2: Built : 16:14:26, Nov  1 2017
INFO:    BL2: Doing platform setup
INFO:    Configuring TrustZone Controller
INFO:    BL2: Loading image id 3
INFO:    Loading image id=3 at address 0xff000000
INFO:    Image id=3 loaded: 0xff000000 - 0xff00c230
INFO:    BL2: Loading image id 4
INFO:    Loading image id=4 at address 0xff200000
INFO:    Image id=4 loaded: 0xff200000 - 0xff480000
INFO:    BL2: Loading image id 5
INFO:    Loading image id=5 at address 0x88000000
INFO:    Image id=5 loaded: 0x88000000 - 0x88280000
NOTICE:  BL1: Booting BL31
INFO:    Entry point address = 0xff000000
INFO:    SPSR = 0x3cd
NOTICE:  BL31: v1.4(debug):v1.4-96-gae48aad7-dirty
NOTICE:  BL31: Built : 16:14:26, Nov  1 2017
INFO:    GICv3 with legacy support detected. ARM GICV3 driver initialized in EL3
INFO:    BL31: Initializing runtime services
INFO:    BL31: Initializing BL32
NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
NOTICE:    Start address  : 0xff217000
NOTICE:    Number of pages: 1 (4096 bytes)
NOTICE:    Attributes     : 0x7
NOTICE:    (Equivalent TF attributes: 0x22)
NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
NOTICE:    Start address  : 0xff217000
NOTICE:    Number of pages: 1 (4096 bytes)
NOTICE:    Attributes     : 0x5
NOTICE:    (Equivalent TF attributes: 0x2a)
NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
NOTICE:    Start address  : 0xff218000
NOTICE:    Number of pages: 1 (4096 bytes)
NOTICE:    Attributes     : 0x7
NOTICE:    (Equivalent TF attributes: 0x22)
NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
NOTICE:    Start address  : 0xfffe7000
NOTICE:    Number of pages: 7 (28672 bytes)
NOTICE:    Attributes     : 0x7
NOTICE:    (Equivalent TF attributes: 0x22)
NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
NOTICE:    Start address  : 0xfffe7000
NOTICE:    Number of pages: 7 (28672 bytes)
NOTICE:    Attributes     : 0x3
NOTICE:    (Equivalent TF attributes: 0x2)
NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
NOTICE:    Start address  : 0xfffef000
NOTICE:    Number of pages: 1 (4096 bytes)
NOTICE:    Attributes     : 0x7
NOTICE:    (Equivalent TF attributes: 0x22)
INFO:    BL31: Preparing for EL3 exit to normal world
INFO:    Entry point address = 0x88000000
INFO:    SPSR = 0x3c9
UEFI firmware (version  built at 16:14:14 on Nov  1 2017)
***

Supreeth Venkatesh (18):
  ArmPkg: Add PCDs needed for MM communication driver.
  ArmPkg/Drivers: Add EFI_MM_COMMUNICATION_PROTOCOL DXE driver.
  ArmPkg/Include: Add MM interface SVC return codes.
  ArmPkg/ArmMmuLib: Add MMU Library suitable for use in S-EL0.
  ArmPkg/ArmMmuLib: Add MMU library inf file suitable for use in S-EL0.
  StandaloneMmPkg: Add an AArch64 specific entry point library.
  StandaloneMmPkg/FvLib: Add a common FV Library for management mode.
  StandaloneMmPkg/MemLib: AARCH64 Specific instance of memory check
    library.
  StandaloneMmPkg/MemoryAllocationLib: Add MM memory allocation library.
  StandaloneMmPkg/HobLib: Add AARCH64 Specific HOB Library for
    management mode.
  StandaloneMmPkg: MM driver entry point library.
  StandaloneMmPkg/CpuMm: Add CPU driver suitable for ARM Platforms.
  StandaloneMmPkg/Core: Implementation of Standalone MM Core Module.
  StandaloneMmPkg: Describe the declaration, definition and fdf files.
  ArmPkg: Extra action to update permissions for S-ELO MM Image.
  BaseTools/AutoGen: Update header file for MM modules.
  StandaloneMmPkg: Add application to test MM communication protocol.
  StandaloneMmPkg: Add handler to handle event received from Normal
    World.

 ArmPkg/ArmPkg.dec                                  |    3 +
 .../Drivers/MmCommunicationDxe/MmCommunication.c   |  339 +++++++
 .../Drivers/MmCommunicationDxe/MmCommunication.inf |   50 +
 ArmPkg/Include/IndustryStandard/ArmMmSvc.h         |    9 +-
 ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c    |  146 +++
 ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf          |   37 +
 .../DebugPeCoffExtraActionLib.c                    |  185 +++-
 .../DebugPeCoffExtraActionLib.inf                  |    7 +
 BaseTools/Source/Python/AutoGen/GenC.py            |   16 +-
 .../Application/MmCommTestApp/MmCommTest.c         |   81 ++
 .../Application/MmCommTestApp/MmCommTest.h         |   37 +
 .../Application/MmCommTestApp/MmCommTest.inf       |   57 ++
 StandaloneMmPkg/Core/Dependency.c                  |  389 +++++++
 StandaloneMmPkg/Core/Dispatcher.c                  | 1071 ++++++++++++++++++++
 StandaloneMmPkg/Core/FwVol.c                       |  104 ++
 StandaloneMmPkg/Core/Handle.c                      |  533 ++++++++++
 StandaloneMmPkg/Core/InstallConfigurationTable.c   |  178 ++++
 StandaloneMmPkg/Core/Locate.c                      |  496 +++++++++
 StandaloneMmPkg/Core/Mmi.c                         |  337 ++++++
 StandaloneMmPkg/Core/Notify.c                      |  203 ++++
 StandaloneMmPkg/Core/Page.c                        |  384 +++++++
 StandaloneMmPkg/Core/Pool.c                        |  287 ++++++
 StandaloneMmPkg/Core/StandaloneMmCore.c            |  747 ++++++++++++++
 StandaloneMmPkg/Core/StandaloneMmCore.h            |  903 +++++++++++++++++
 StandaloneMmPkg/Core/StandaloneMmCore.inf          |   82 ++
 StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h |   66 ++
 StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S          |   33 +
 StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c    |  231 +++++
 StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c           |  229 +++++
 .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h        |   89 ++
 .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf      |   60 ++
 StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c      |   51 +
 StandaloneMmPkg/Include/Guid/MmCoreData.h          |  132 +++
 StandaloneMmPkg/Include/Guid/MmFvDispatch.h        |   38 +
 StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h  |   62 ++
 StandaloneMmPkg/Include/Guid/MpInformation.h       |   41 +
 .../Library/Arm/StandaloneMmCoreEntryPoint.h       |  232 +++++
 StandaloneMmPkg/Include/Library/FvLib.h            |  109 ++
 StandaloneMmPkg/Include/Library/MemLib.h           |  140 +++
 .../Include/Library/MmCoreStandaloneEntryPoint.h   |  101 ++
 .../Include/Library/MmDriverStandaloneEntryPoint.h |  148 +++
 StandaloneMmPkg/Include/StandaloneMm.h             |   36 +
 StandaloneMmPkg/Library/FvLib/FvLib.c              |  366 +++++++
 StandaloneMmPkg/Library/FvLib/FvLib.inf            |   57 ++
 StandaloneMmPkg/Library/HobLib/Arm/HobLib.c        |  697 +++++++++++++
 StandaloneMmPkg/Library/HobLib/HobLib.inf          |   45 +
 StandaloneMmPkg/Library/MemLib/Arm/MemLib.c        |  276 +++++
 StandaloneMmPkg/Library/MemLib/MemLib.inf          |   47 +
 .../MemoryAllocationLib/MemoryAllocationLib.c      |  907 +++++++++++++++++
 .../MemoryAllocationLib/MemoryAllocationLib.inf    |   49 +
 .../MemoryAllocationLib/MemoryAllocationServices.h |   38 +
 .../StandaloneMmCoreEntryPoint/Arm/CreateHobList.c |  200 ++++
 .../Arm/SetPermissions.c                           |  278 +++++
 .../Arm/StandaloneMmCoreEntryPoint.c               |  264 +++++
 .../StandaloneMmCoreEntryPoint.inf                 |   53 +
 .../StandaloneMmDriverEntryPoint.c                 |  102 ++
 .../StandaloneMmDriverEntryPoint.inf               |   41 +
 StandaloneMmPkg/StandaloneMmPkg.dec                |   49 +
 StandaloneMmPkg/StandaloneMmPkg.dsc                |  132 +++
 StandaloneMmPkg/StandaloneMmPkg.fdf                |  184 ++++
 StandaloneMmPkg => StandaloneMmPkg~HEAD            |    0
 61 files changed, 12244 insertions(+), 20 deletions(-)
 create mode 100644 ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.c
 create mode 100644 ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.inf
 create mode 100644 ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c
 create mode 100644 ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
 create mode 100644 StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.c
 create mode 100644 StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.h
 create mode 100644 StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.inf
 create mode 100644 StandaloneMmPkg/Core/Dependency.c
 create mode 100644 StandaloneMmPkg/Core/Dispatcher.c
 create mode 100644 StandaloneMmPkg/Core/FwVol.c
 create mode 100644 StandaloneMmPkg/Core/Handle.c
 create mode 100644 StandaloneMmPkg/Core/InstallConfigurationTable.c
 create mode 100644 StandaloneMmPkg/Core/Locate.c
 create mode 100644 StandaloneMmPkg/Core/Mmi.c
 create mode 100644 StandaloneMmPkg/Core/Notify.c
 create mode 100644 StandaloneMmPkg/Core/Page.c
 create mode 100644 StandaloneMmPkg/Core/Pool.c
 create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.c
 create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.h
 create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.inf
 create mode 100644 StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
 create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
 create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
 create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
 create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
 create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
 create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
 create mode 100644 StandaloneMmPkg/Include/Guid/MmCoreData.h
 create mode 100644 StandaloneMmPkg/Include/Guid/MmFvDispatch.h
 create mode 100644 StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
 create mode 100644 StandaloneMmPkg/Include/Guid/MpInformation.h
 create mode 100644 StandaloneMmPkg/Include/Library/Arm/StandaloneMmCoreEntryPoint.h
 create mode 100644 StandaloneMmPkg/Include/Library/FvLib.h
 create mode 100644 StandaloneMmPkg/Include/Library/MemLib.h
 create mode 100644 StandaloneMmPkg/Include/Library/MmCoreStandaloneEntryPoint.h
 create mode 100644 StandaloneMmPkg/Include/Library/MmDriverStandaloneEntryPoint.h
 create mode 100644 StandaloneMmPkg/Include/StandaloneMm.h
 create mode 100644 StandaloneMmPkg/Library/FvLib/FvLib.c
 create mode 100644 StandaloneMmPkg/Library/FvLib/FvLib.inf
 create mode 100644 StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
 create mode 100644 StandaloneMmPkg/Library/HobLib/HobLib.inf
 create mode 100644 StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
 create mode 100644 StandaloneMmPkg/Library/MemLib/MemLib.inf
 create mode 100644 StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
 create mode 100644 StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
 create mode 100644 StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.h
 create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/CreateHobList.c
 create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/SetPermissions.c
 create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c
 create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
 create mode 100644 StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.c
 create mode 100644 StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf
 create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dec
 create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dsc
 create mode 100644 StandaloneMmPkg/StandaloneMmPkg.fdf
 rename StandaloneMmPkg => StandaloneMmPkg~HEAD (100%)

-- 
2.16.2



^ permalink raw reply	[flat|nested] 70+ messages in thread

* [PATCH v1 01/18] ArmPkg: Add PCDs needed for MM communication driver.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-11 14:43   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 02/18] ArmPkg/Drivers: Add EFI_MM_COMMUNICATION_PROTOCOL DXE driver Supreeth Venkatesh
                   ` (17 subsequent siblings)
  18 siblings, 1 reply; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

This patch defines PCDs to describe the base address and size of
communication buffer between normal world (uefi) and standalone MM
environment in the secure world.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 ArmPkg/ArmPkg.dec | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/ArmPkg/ArmPkg.dec b/ArmPkg/ArmPkg.dec
index a55b6268ff..b64942220b 100644
--- a/ArmPkg/ArmPkg.dec
+++ b/ArmPkg/ArmPkg.dec
@@ -223,6 +223,9 @@
   gArmTokenSpaceGuid.PcdSystemMemoryBase|0|UINT64|0x00000029
   gArmTokenSpaceGuid.PcdSystemMemorySize|0|UINT64|0x0000002A
 
+  gArmTokenSpaceGuid.PcdMmBufferBase|0|UINT64|0x00000045
+  gArmTokenSpaceGuid.PcdMmBufferSize|0|UINT64|0x00000046
+
 [PcdsFixedAtBuild.common, PcdsDynamic.common]
   #
   # ARM Architectural Timer
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 02/18] ArmPkg/Drivers: Add EFI_MM_COMMUNICATION_PROTOCOL DXE driver.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
  2018-04-06 14:42 ` [PATCH v1 01/18] ArmPkg: Add PCDs needed for MM communication driver Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-11 14:00   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 03/18] ArmPkg/Include: Add MM interface SVC return codes Supreeth Venkatesh
                   ` (16 subsequent siblings)
  18 siblings, 1 reply; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

PI v1.5 Specification Volume 4 defines Management Mode Core Interface
and defines EFI_MM_COMMUNICATION_PROTOCOL. This protocol provides a
means of communicating between drivers outside of MM and MMI
handlers inside of MM.

This patch implements the EFI_MM_COMMUNICATION_PROTOCOL DXE runtime
driver for AARCH64 platforms. It uses SMCs allocated from the standard
SMC range defined in DEN0060A_ARM_MM_Interface_Specification.pdf
to communicate with the standalone MM environment in the secure world.

This patch also adds the MM Communication driver (.inf) file to
define entry point for this driver and other compile
related information the driver needs.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 .../Drivers/MmCommunicationDxe/MmCommunication.c   | 339 +++++++++++++++++++++
 .../Drivers/MmCommunicationDxe/MmCommunication.inf |  50 +++
 2 files changed, 389 insertions(+)
 create mode 100644 ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.c
 create mode 100644 ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.inf

diff --git a/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.c b/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.c
new file mode 100644
index 0000000000..e801c1c601
--- /dev/null
+++ b/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.c
@@ -0,0 +1,339 @@
+/** @file
+
+  Copyright (c) 2016-2017, ARM Limited. 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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <Library/ArmLib.h>
+#include <Library/ArmSmcLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/DxeServicesTableLib.h>
+#include <Library/HobLib.h>
+#include <Library/PcdLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/UefiRuntimeServicesTableLib.h>
+
+#include <Protocol/MmCommunication.h>
+
+#include <IndustryStandard/ArmStdSmc.h>
+
+//
+// Address, Length of the pre-allocated buffer for communication with the secure
+// world.
+//
+STATIC ARM_MEMORY_REGION_DESCRIPTOR  mNsCommBuffMemRegion;
+
+// Notification event when virtual address map is set.
+STATIC EFI_EVENT  mSetVirtualAddressMapEvent;
+
+//
+// Handle to install the MM Communication Protocol
+//
+STATIC EFI_HANDLE  mMmCommunicateHandle;
+
+/**
+  Communicates with a registered handler.
+
+  This function provides an interface to send and receive messages to the
+  Standalone MM environment on behalf of UEFI services.  This function is part
+  of the MM Communication Protocol that may be called in physical mode prior to
+  SetVirtualAddressMap() and in virtual mode after SetVirtualAddressMap().
+
+  @param[in]      This                The EFI_MM_COMMUNICATION_PROTOCOL
+                                      instance.
+  @param[in, out] CommBuffer          A pointer to the buffer to convey
+                                      into MMRAM.
+  @param[in, out] CommSize            The size of the data buffer being
+                                      passed in. This is optional.
+
+  @retval EFI_SUCCESS                 The message was successfully posted.
+  @retval EFI_INVALID_PARAMETER       The CommBuffer was NULL.
+  @retval EFI_BAD_BUFFER_SIZE         The buffer size is incorrect for the MM
+                                      implementation. If this error is
+                                      returned, the MessageLength field in
+                                      the CommBuffer header or the integer
+                                      pointed by CommSize are updated to reflect
+                                      the maximum payload size the
+                                      implementation can accommodate.
+  @retval EFI_ACCESS_DENIED           The CommunicateBuffer parameter
+                                      or CommSize parameter, if not omitted,
+                                      are in address range that cannot be
+                                      accessed by the MM environment
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+MmCommunicationCommunicate (
+  IN CONST EFI_MM_COMMUNICATION_PROTOCOL  *This,
+  IN OUT VOID                             *CommBuffer,
+  IN OUT UINTN                            *CommSize OPTIONAL
+  )
+{
+  EFI_MM_COMMUNICATE_HEADER   *CommunicateHeader;
+  ARM_SMC_ARGS                CommunicateSmcArgs;
+  EFI_STATUS                  Status;
+  UINTN                       BufferSize;
+
+  CommunicateHeader = CommBuffer;
+  Status = EFI_ACCESS_DENIED;
+  BufferSize = 0;
+
+  ZeroMem (&CommunicateSmcArgs, sizeof (ARM_SMC_ARGS));
+
+  //
+  // Check parameters
+  //
+  if (CommBuffer == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  // If the length of the CommBuffer is 0 then return the expected length.
+  if (CommSize) {
+    if (*CommSize == 0) {
+      *CommSize = mNsCommBuffMemRegion.Length;
+      return EFI_BAD_BUFFER_SIZE;
+    }
+    //
+    // CommSize must hold HeaderGuid and MessageLength
+    //
+    if (*CommSize < sizeof (EFI_MM_COMMUNICATE_HEADER)) {
+        return EFI_INVALID_PARAMETER;
+    }
+    BufferSize = *CommSize;
+  } else {
+    BufferSize = CommunicateHeader->MessageLength +
+                 sizeof (CommunicateHeader->HeaderGuid) +
+                 sizeof (CommunicateHeader->MessageLength);
+  }
+
+  //
+  // If the buffer size is 0 or greater than what can be tolerated by the MM
+  // environment then return the expected size.
+  //
+  if ((BufferSize == 0) ||
+      (BufferSize > mNsCommBuffMemRegion.Length)) {
+    CommunicateHeader->MessageLength = mNsCommBuffMemRegion.Length -
+                                       sizeof (CommunicateHeader->HeaderGuid) -
+                                       sizeof (CommunicateHeader->MessageLength);
+    return EFI_BAD_BUFFER_SIZE;
+  }
+
+  // SMC Function ID
+  CommunicateSmcArgs.Arg0 = ARM_SMC_ID_MM_COMMUNICATE_AARCH64;
+
+  // Reserved for Future. Must be Zero.
+  CommunicateSmcArgs.Arg1 = 0;
+
+  if (mNsCommBuffMemRegion.VirtualBase) {
+    CopyMem ((VOID *)mNsCommBuffMemRegion.VirtualBase, CommBuffer, BufferSize);
+  } else {
+    return EFI_ACCESS_DENIED;
+  }
+
+  // For the SMC64 version, this parameter is a 64-bit Physical Address (PA)
+  // or Intermediate Physical Address (IPA).
+  // For the SMC32 version, this parameter is a 32-bit PA or IPA.
+  CommunicateSmcArgs.Arg2 = (UINTN)mNsCommBuffMemRegion.PhysicalBase;
+
+  // comm_size_address is a PA or an IPA that holds the size of the
+  // communication buffer being passed in. This parameter is optional
+  // and can be omitted by passing a zero.
+  // ARM does not recommend using it since this might require the
+  // implementation to create a separate memory mapping for the parameter.
+  // ARM recommends storing the buffer size in the buffer itself.
+  CommunicateSmcArgs.Arg3 = 0;
+
+  // Call the Standalone MM environment.
+  ArmCallSmc (&CommunicateSmcArgs);
+
+  switch (CommunicateSmcArgs.Arg0) {
+  case ARM_SMC_MM_RET_SUCCESS:
+    // On exit, the size of data being returned is inferred from
+    // CommSize or MessageLength + Header.
+    CopyMem (CommBuffer,
+             (const VOID *)mNsCommBuffMemRegion.VirtualBase,
+             BufferSize);
+    Status = EFI_SUCCESS;
+    break;
+
+  case ARM_SMC_MM_RET_NOT_SUPPORTED:
+  case ARM_SMC_MM_RET_INVALID_PARAMS:
+    Status = EFI_INVALID_PARAMETER;
+    break;
+
+  case ARM_SMC_MM_RET_DENIED:
+    Status = EFI_ACCESS_DENIED;
+    break;
+
+  case ARM_SMC_MM_RET_NO_MEMORY:
+    // Unexpected error since the CommSize was checked for zero length
+    // prior to issuing the SMC
+  default:
+    Status = EFI_ACCESS_DENIED;
+    ASSERT (0);
+  }
+
+  return Status;
+}
+
+//
+// MM Communication Protocol instance
+//
+EFI_MM_COMMUNICATION_PROTOCOL  mMmCommunication = {
+  MmCommunicationCommunicate
+};
+
+/**
+  Notification callback on SetVirtualAddressMap event.
+
+  This function notifies the MM communication protocol interface on
+  SetVirtualAddressMap event and converts pointers used in this driver
+  from physical to virtual address.
+
+  @param  Event          SetVirtualAddressMap event.
+  @param  Context        A context when the SetVirtualAddressMap triggered.
+
+  @retval EFI_SUCCESS    The function executed successfully.
+  @retval Other          Some error occurred when executing this function.
+
+**/
+STATIC
+VOID
+EFIAPI
+NotifySetVirtualAddressMap (
+  IN EFI_EVENT  Event,
+  IN VOID      *Context
+  )
+{
+  EFI_STATUS  Status;
+
+  Status = gRT->ConvertPointer (EFI_OPTIONAL_PTR,
+                                (VOID **)&mNsCommBuffMemRegion.VirtualBase
+                               );
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "NotifySetVirtualAddressMap():"
+            " Unable to convert MM runtime pointer. Status:0x%r\n", Status));
+  }
+
+}
+
+/**
+  The Entry Point for MM Communication
+
+  This function installs the MM communication protocol interface and finds out
+  what type of buffer management will be required prior to invoking the
+  communication SMC.
+
+  @param  ImageHandle    The firmware allocated handle for the EFI image.
+  @param  SystemTable    A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS    The entry point is executed successfully.
+  @retval Other          Some error occurred when executing this entry point.
+
+**/
+EFI_STATUS
+EFIAPI
+MmCommunicationInitialize (
+  IN EFI_HANDLE         ImageHandle,
+  IN EFI_SYSTEM_TABLE  *SystemTable
+  )
+{
+  EFI_STATUS                 Status;
+
+  mNsCommBuffMemRegion.PhysicalBase = PcdGet64 (PcdMmBufferBase);
+  // During boot , Virtual and Physical are same
+  mNsCommBuffMemRegion.VirtualBase = mNsCommBuffMemRegion.PhysicalBase;
+  mNsCommBuffMemRegion.Length = PcdGet64 (PcdMmBufferSize);
+
+  if (mNsCommBuffMemRegion.PhysicalBase == 0) {
+    DEBUG ((DEBUG_ERROR, "MmCommunicateInitialize: "
+            "Invalid MM Buffer Base Address.\n"));
+    goto ReturnErrorStatus;
+  }
+
+  if (mNsCommBuffMemRegion.Length == 0) {
+    DEBUG ((DEBUG_ERROR, "MmCommunicateInitialize: "
+            "Maximum Buffer Size is zero.\n"));
+    goto ReturnErrorStatus;
+  }
+
+  Status = gDS->AddMemorySpace (EfiGcdMemoryTypeSystemMemory,
+                                mNsCommBuffMemRegion.PhysicalBase,
+                                mNsCommBuffMemRegion.Length,
+                                EFI_MEMORY_WB |
+                                EFI_MEMORY_XP |
+                                EFI_MEMORY_RUNTIME);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "MmCommunicateInitialize: "
+            "Failed to add MM-NS Buffer Memory Space\n"));
+    goto ReturnErrorStatus;
+  }
+
+  Status = gDS->SetMemorySpaceAttributes(mNsCommBuffMemRegion.PhysicalBase,
+                                         mNsCommBuffMemRegion.Length,
+                                         EFI_MEMORY_WB | EFI_MEMORY_XP);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "MmCommunicateInitialize: "
+            "Failed to set MM-NS Buffer Memory attributes\n"));
+    goto CleanAddedMemorySpace;
+  }
+
+  Status = gBS->AllocatePages (AllocateAddress,
+                               EfiRuntimeServicesData,
+                               EFI_SIZE_TO_PAGES (mNsCommBuffMemRegion.Length),
+                               &mNsCommBuffMemRegion.PhysicalBase);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "MmCommunicateInitialize: "
+            "Failed to allocate MM-NS Buffer Memory Space\n"));
+    goto CleanAddedMemorySpace;
+  }
+
+  // Install the communication protocol
+  Status = gBS->InstallProtocolInterface (&mMmCommunicateHandle,
+                                          &gEfiMmCommunicationProtocolGuid,
+                                          EFI_NATIVE_INTERFACE,
+                                          &mMmCommunication);
+  if (EFI_ERROR(Status)) {
+    DEBUG ((DEBUG_ERROR, "MmCommunicationInitialize: "
+            "Failed to install MM communication protocol\n"));
+    goto CleanAllocatedPages;
+  }
+
+  // Register notification callback when  virtual address is associated
+  // with the physical address.
+  // Create a Set Virtual Address Map event.
+  //
+  Status = gBS->CreateEvent (EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,  // Type
+                             TPL_NOTIFY,                         // NotifyTpl
+                             NotifySetVirtualAddressMap,         // NotifyFunction
+                             NULL,                               // NotifyContext
+                             &mSetVirtualAddressMapEvent         // Event
+                            );
+  if (Status == EFI_SUCCESS) {
+    return Status;
+  }
+
+  gBS->UninstallProtocolInterface(mMmCommunicateHandle,
+                                  &gEfiMmCommunicationProtocolGuid,
+                                  &mMmCommunication);
+
+CleanAllocatedPages:
+  gBS->FreePages (mNsCommBuffMemRegion.PhysicalBase,
+                  EFI_SIZE_TO_PAGES (mNsCommBuffMemRegion.Length));
+
+CleanAddedMemorySpace:
+  gDS->RemoveMemorySpace (mNsCommBuffMemRegion.PhysicalBase,
+                          mNsCommBuffMemRegion.Length);
+
+ReturnErrorStatus:
+  return EFI_INVALID_PARAMETER;
+}
diff --git a/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.inf b/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.inf
new file mode 100644
index 0000000000..344d55f333
--- /dev/null
+++ b/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.inf
@@ -0,0 +1,50 @@
+#/** @file
+#
+#  DXE MM Communicate driver
+#
+#  Copyright (c) 2016 - 2017, ARM Limited. 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
+#
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+#**/
+
+[Defines]
+  INF_VERSION                    = 0x0001001A
+  BASE_NAME                      = ArmMmCommunication
+  FILE_GUID                      = 09EE81D3-F15E-43F4-85B4-CB9873DA5D6B
+  MODULE_TYPE                    = DXE_RUNTIME_DRIVER
+  VERSION_STRING                 = 1.0
+
+  ENTRY_POINT                    = MmCommunicationInitialize
+
+[Sources.Common]
+  MmCommunication.c
+
+[Packages]
+  ArmPkg/ArmPkg.dec
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  ArmLib
+  ArmSmcLib
+  BaseMemoryLib
+  DebugLib
+  DxeServicesTableLib
+  HobLib
+  UefiDriverEntryPoint
+
+[Protocols]
+  gEfiMmCommunicationProtocolGuid              ## PRODUCES
+
+[Pcd.common]
+  gArmTokenSpaceGuid.PcdMmBufferBase
+  gArmTokenSpaceGuid.PcdMmBufferSize
+
+[Depex]
+  AFTER gArmGicDxeFileGuid
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 03/18] ArmPkg/Include: Add MM interface SVC return codes.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
  2018-04-06 14:42 ` [PATCH v1 01/18] ArmPkg: Add PCDs needed for MM communication driver Supreeth Venkatesh
  2018-04-06 14:42 ` [PATCH v1 02/18] ArmPkg/Drivers: Add EFI_MM_COMMUNICATION_PROTOCOL DXE driver Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-11 14:38   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 04/18] ArmPkg/ArmMmuLib: Add MMU Library suitable for use in S-EL0 Supreeth Venkatesh
                   ` (15 subsequent siblings)
  18 siblings, 1 reply; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

This patch adds the Management Mode(MM) SVC return codes as specified in
http://infocenter.arm.com/help/topic/com.arm.doc.den0060a/DEN0060A_ARM_MM_Interface_Specification.pdf.
Also, corrects SVC ID for retrieving SPM version information.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 ArmPkg/Include/IndustryStandard/ArmMmSvc.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/ArmPkg/Include/IndustryStandard/ArmMmSvc.h b/ArmPkg/Include/IndustryStandard/ArmMmSvc.h
index 4c7b6c3386..a64b9ec23c 100644
--- a/ArmPkg/Include/IndustryStandard/ArmMmSvc.h
+++ b/ArmPkg/Include/IndustryStandard/ArmMmSvc.h
@@ -20,7 +20,7 @@
  * delegated events and request the Secure partition manager to perform
  * privileged operations on its behalf.
  */
-#define ARM_SVC_ID_SPM_VERSION_AARCH64             0xC4000060
+#define ARM_SVC_ID_SPM_VERSION_AARCH32             0x84000060
 #define ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64       0xC4000061
 #define ARM_SVC_ID_SP_GET_MEM_ATTRIBUTES_AARCH64   0xC4000064
 #define ARM_SVC_ID_SP_SET_MEM_ATTRIBUTES_AARCH64   0xC4000065
@@ -40,4 +40,11 @@
     ((((c_perm) & SET_MEM_ATTR_CODE_PERM_MASK) << SET_MEM_ATTR_CODE_PERM_SHIFT) | \
     (( (d_perm) & SET_MEM_ATTR_DATA_PERM_MASK) << SET_MEM_ATTR_DATA_PERM_SHIFT))
 
+/* MM SVC Return error codes */
+#define ARM_SVC_SPM_RET_SUCCESS               0
+#define ARM_SVC_SPM_RET_NOT_SUPPORTED        -1
+#define ARM_SVC_SPM_RET_INVALID_PARAMS       -2
+#define ARM_SVC_SPM_RET_DENIED               -3
+#define ARM_SVC_SPM_RET_NO_MEMORY            -5
+
 #endif
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 04/18] ArmPkg/ArmMmuLib: Add MMU Library suitable for use in S-EL0.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (2 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 03/18] ArmPkg/Include: Add MM interface SVC return codes Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-11 19:21   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 05/18] ArmPkg/ArmMmuLib: Add MMU library inf file " Supreeth Venkatesh
                   ` (14 subsequent siblings)
  18 siblings, 1 reply; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

The Standalone MM environment runs in S-EL0 in AArch64 on ARM Standard
Platforms. Privileged firmware e.g. ARM Trusted Firmware sets up its
architectural context including the initial translation tables for the
S-EL1/EL0 translation regime. The MM environment could still request ARM
TF to change the memory attributes of memory regions during
initialization.

This patch adds a simple MMU library suitable for execution in S-EL0 and
requesting operations from higher exception levels.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c | 146 ++++++++++++++++++++++++
 1 file changed, 146 insertions(+)
 create mode 100644 ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c

diff --git a/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c b/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c
new file mode 100644
index 0000000000..56969e31d1
--- /dev/null
+++ b/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c
@@ -0,0 +1,146 @@
+/** @file
+*  File managing the MMU for ARMv8 architecture in S-EL0
+*
+*  Copyright (c) 2017, ARM Limited. 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
+*
+*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+*
+**/
+
+#include <Uefi.h>
+#include <Chipset/AArch64.h>
+#include <IndustryStandard/ArmMmSvc.h>
+
+#include <Library/ArmLib.h>
+#include <Library/ArmMmuLib.h>
+#include <Library/ArmSvcLib.h>
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+
+EFI_STATUS
+RequestMemoryPermissionChange(
+  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
+  IN  UINT64                    Length,
+  IN  UINTN                     Permissions
+  )
+{
+  EFI_STATUS    Status;
+  ARM_SVC_ARGS  ChangeMemoryPermissionsSvcArgs = {0};
+
+  ChangeMemoryPermissionsSvcArgs.Arg0 = ARM_SVC_ID_SP_SET_MEM_ATTRIBUTES_AARCH64;
+  ChangeMemoryPermissionsSvcArgs.Arg1 = BaseAddress;
+  ChangeMemoryPermissionsSvcArgs.Arg2 = (Length >= EFI_PAGE_SIZE) ? \
+                                         Length >> EFI_PAGE_SHIFT : 1;
+  ChangeMemoryPermissionsSvcArgs.Arg3 = Permissions;
+
+  ArmCallSvc(&ChangeMemoryPermissionsSvcArgs);
+
+  Status = ChangeMemoryPermissionsSvcArgs.Arg0;
+
+  switch (Status) {
+  case ARM_SVC_SPM_RET_SUCCESS:
+    Status = EFI_SUCCESS;
+    break;
+
+  case ARM_SVC_SPM_RET_NOT_SUPPORTED:
+    Status = EFI_UNSUPPORTED;
+    break;
+
+  case ARM_SVC_SPM_RET_INVALID_PARAMS:
+    Status = EFI_INVALID_PARAMETER;
+    break;
+
+  case ARM_SVC_SPM_RET_DENIED:
+    Status = EFI_ACCESS_DENIED;
+    break;
+
+  case ARM_SVC_SPM_RET_NO_MEMORY:
+    Status = EFI_BAD_BUFFER_SIZE;
+    break;
+
+  default:
+    Status = EFI_ACCESS_DENIED;
+    ASSERT (0);
+  }
+
+  return Status;
+}
+
+EFI_STATUS
+ArmSetMemoryRegionNoExec (
+  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
+  IN  UINT64                    Length
+  )
+{
+  return RequestMemoryPermissionChange(BaseAddress,
+                                       Length,
+                                       SET_MEM_ATTR_MAKE_PERM_REQUEST( \
+                                         SET_MEM_ATTR_DATA_PERM_RO, \
+                                         SET_MEM_ATTR_CODE_PERM_XN));
+}
+
+EFI_STATUS
+ArmClearMemoryRegionNoExec (
+  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
+  IN  UINT64                    Length
+  )
+{
+  return RequestMemoryPermissionChange(BaseAddress,
+                                       Length,
+                                       SET_MEM_ATTR_MAKE_PERM_REQUEST( \
+                                         SET_MEM_ATTR_DATA_PERM_RO, \
+                                         SET_MEM_ATTR_CODE_PERM_X));
+}
+
+EFI_STATUS
+ArmSetMemoryRegionReadOnly (
+  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
+  IN  UINT64                    Length
+  )
+{
+  return RequestMemoryPermissionChange(BaseAddress,
+                                       Length,
+                                       SET_MEM_ATTR_MAKE_PERM_REQUEST( \
+                                         SET_MEM_ATTR_DATA_PERM_RO, \
+                                         SET_MEM_ATTR_CODE_PERM_XN));
+}
+
+EFI_STATUS
+ArmClearMemoryRegionReadOnly (
+  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
+  IN  UINT64                    Length
+  )
+{
+  return RequestMemoryPermissionChange(BaseAddress,
+                                       Length,
+                                       SET_MEM_ATTR_MAKE_PERM_REQUEST( \
+                                         SET_MEM_ATTR_DATA_PERM_RW, \
+                                         SET_MEM_ATTR_CODE_PERM_XN));
+}
+
+EFI_STATUS
+EFIAPI
+ArmConfigureMmu (
+  IN  ARM_MEMORY_REGION_DESCRIPTOR  *MemoryTable,
+  OUT VOID                          **TranslationTableBase OPTIONAL,
+  OUT UINTN                         *TranslationTableSize OPTIONAL
+  )
+{
+  return EFI_UNSUPPORTED;
+}
+
+EFI_STATUS
+EFIAPI
+ArmMmuSecLibConstructor (
+  IN EFI_HANDLE            ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
+  )
+{
+  return EFI_SUCCESS;
+}
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 05/18] ArmPkg/ArmMmuLib: Add MMU library inf file suitable for use in S-EL0.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (3 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 04/18] ArmPkg/ArmMmuLib: Add MMU Library suitable for use in S-EL0 Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-11 19:24   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 06/18] StandaloneMmPkg: Add an AArch64 specific entry point library Supreeth Venkatesh
                   ` (13 subsequent siblings)
  18 siblings, 1 reply; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

This patch adds the definitions, sources, packages and library classes
needed to compile and link MMU Library suitable for use in S-EL0.

Currently, this is used only during the Standalone MM Core
initialization and hence defined as MM_CORE_STANDALONE Module.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf | 37 +++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100644 ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf

diff --git a/ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf b/ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
new file mode 100644
index 0000000000..5c802923da
--- /dev/null
+++ b/ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
@@ -0,0 +1,37 @@
+#/** @file
+#
+#  Copyright (c) 2017, ARM Limited. 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
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+#
+#**/
+
+[Defines]
+  INF_VERSION                    = 0x0001001A
+  BASE_NAME                      = ArmMmuSecLib
+  FILE_GUID                      = da8f0232-fb14-42f0-922c-63104d2c70bd
+  MODULE_TYPE                    = MM_CORE_STANDALONE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = ArmMmuSecLib|MM_CORE_STANDALONE
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  CONSTRUCTOR                    = ArmMmuSecLibConstructor
+
+[Sources.AARCH64]
+  AArch64/ArmMmuSecLib.c
+
+[Packages]
+  ArmPkg/ArmPkg.dec
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  ArmLib
+  CacheMaintenanceLib
+  MemoryAllocationLib
+
+
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 06/18] StandaloneMmPkg: Add an AArch64 specific entry point library.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (4 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 05/18] ArmPkg/ArmMmuLib: Add MMU library inf file " Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-16 14:04   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 07/18] StandaloneMmPkg/FvLib: Add a common FV Library for management mode Supreeth Venkatesh
                   ` (12 subsequent siblings)
  18 siblings, 1 reply; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

The Standalone MM environment runs in S-EL0 in AArch64 on ARM Standard
Platforms and is initialised during the SEC phase. ARM Trusted firmware
in EL3 is responsible for initialising the architectural context for
S-EL0 and loading the Standalone MM image. The memory allocated to this
image is marked as RO+X. Heap memory is marked as RW+XN.

Certain actions have to be completed prior to executing the generic code
in the Standalone MM Core module. These are:

1. Memory permission attributes for each section of the Standalone MM
   Core module need to be changed prior to accessing any RW data.

2. A Hob list has to be created with information that allows the MM
   environment to initialise and dispatch drivers.

Furthermore, this module is responsible for handing over runtime MM
events to the Standalone MM CPU driver and returning control to ARM
Trusted Firmware upon event completion. Hence it needs to know the CPU
driver entry point.

This patch implements an entry point module that ARM Trusted Firmware
jumps to in S-EL0. It then performs the above actions before calling the
Standalone MM Foundation entry point and handling subsequent MM events.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 .../Library/Arm/StandaloneMmCoreEntryPoint.h       | 232 +++++++++++++++++
 .../Include/Library/MmCoreStandaloneEntryPoint.h   | 101 ++++++++
 .../StandaloneMmCoreEntryPoint/Arm/CreateHobList.c | 200 +++++++++++++++
 .../Arm/SetPermissions.c                           | 278 +++++++++++++++++++++
 .../Arm/StandaloneMmCoreEntryPoint.c               | 264 +++++++++++++++++++
 .../StandaloneMmCoreEntryPoint.inf                 |  53 ++++
 StandaloneMmPkg => StandaloneMmPkg~HEAD            |   0
 7 files changed, 1128 insertions(+)
 create mode 100644 StandaloneMmPkg/Include/Library/Arm/StandaloneMmCoreEntryPoint.h
 create mode 100644 StandaloneMmPkg/Include/Library/MmCoreStandaloneEntryPoint.h
 create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/CreateHobList.c
 create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/SetPermissions.c
 create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c
 create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
 rename StandaloneMmPkg => StandaloneMmPkg~HEAD (100%)

diff --git a/StandaloneMmPkg/Include/Library/Arm/StandaloneMmCoreEntryPoint.h b/StandaloneMmPkg/Include/Library/Arm/StandaloneMmCoreEntryPoint.h
new file mode 100644
index 0000000000..029c6c476c
--- /dev/null
+++ b/StandaloneMmPkg/Include/Library/Arm/StandaloneMmCoreEntryPoint.h
@@ -0,0 +1,232 @@
+/** @file
+  Entry point to the Standalone MM Foundation when initialised during the SEC
+  phase on ARM platforms
+
+Copyright (c) 2017, ARM Ltd. All rights reserved.<BR>
+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
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef __MODULE_ENTRY_POINT_H__
+#define __MODULE_ENTRY_POINT_H__
+
+#include <Library/PeCoffLib.h>
+#include <Library/FvLib.h>
+
+#define CPU_INFO_FLAG_PRIMARY_CPU  0x00000001
+
+typedef
+EFI_STATUS
+(*PI_MM_CPU_TP_FW_ENTRYPOINT) (
+  IN UINTN EventId,
+  IN UINTN CpuNumber,
+  IN UINTN NsCommBufferAddr
+  );
+
+typedef struct {
+  UINT8  Type;       /* type of the structure */
+  UINT8  Version;    /* version of this structure */
+  UINT16 Size;      /* size of this structure in bytes */
+  UINT32 Attr;      /* attributes: unused bits SBZ */
+} EFI_PARAM_HEADER;
+
+typedef struct {
+  UINT64 Mpidr;
+  UINT32 LinearId;
+  UINT32 Flags;
+} EFI_SECURE_PARTITION_CPU_INFO;
+
+typedef struct {
+  EFI_PARAM_HEADER              Header;
+  UINT64                        SpMemBase;
+  UINT64                        SpMemLimit;
+  UINT64                        SpImageBase;
+  UINT64                        SpStackBase;
+  UINT64                        SpHeapBase;
+  UINT64                        SpNsCommBufBase;
+  UINT64                        SpSharedBufBase;
+  UINT64                        SpImageSize;
+  UINT64                        SpPcpuStackSize;
+  UINT64                        SpHeapSize;
+  UINT64                        SpNsCommBufSize;
+  UINT64                        SpPcpuSharedBufSize;
+  UINT32                        NumSpMemRegions;
+  UINT32                        NumCpus;
+  EFI_SECURE_PARTITION_CPU_INFO *CpuInfo;
+} EFI_SECURE_PARTITION_BOOT_INFO;
+
+typedef struct {
+  EFI_PARAM_HEADER               h;
+  UINT64                         SpStackBase;
+  UINT64                         SpSharedBufBase;
+  UINT32                         SpPcpuStackSize;
+  UINT32                         SpPcpuSharedBufSize;
+  EFI_SECURE_PARTITION_CPU_INFO  CpuInfo;
+} EFI_SECURE_PARTITION_WARM_BOOT_INFO;
+
+
+typedef
+EFI_STATUS
+(*PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT) (
+  IN UINTN EventId,
+  IN UINTN CpuNumber,
+  IN UINTN NsCommBufferAddr
+  );
+
+typedef struct {
+  PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT *ArmTfCpuDriverEpPtr;
+} ARM_TF_CPU_DRIVER_EP_DESCRIPTOR;
+
+typedef RETURN_STATUS (*REGION_PERMISSION_UPDATE_FUNC) (
+  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
+  IN  UINT64                    Length
+  );
+
+/**
+  Privileged firmware assigns RO & Executable attributes to all memory occupied
+  by the Boot Firmware Volume. This function sets the correct permissions of
+  sections in the Standalone MM Core module to be able to access RO and RW data
+  and make further progress in the boot process.
+
+  @param  ImageContext           Pointer to PE/COFF image context
+  @param  SectionHeaderOffset    Offset of PE/COFF image section header
+  @param  NumberOfSections       Number of Sections
+  @param  TextUpdater            Function to change code permissions
+  @param  ReadOnlyUpdater        Function to change RO permissions
+  @param  ReadWriteUpdater       Function to change RW permissions
+
+**/
+EFI_STATUS
+EFIAPI
+UpdateMmFoundationPeCoffPermissions (
+  IN  CONST PE_COFF_LOADER_IMAGE_CONTEXT      *ImageContext,
+  IN  UINT32                                  SectionHeaderOffset,
+  IN  CONST  UINTN                            NumberOfSections,
+  IN  REGION_PERMISSION_UPDATE_FUNC           TextUpdater,
+  IN  REGION_PERMISSION_UPDATE_FUNC           ReadOnlyUpdater,
+  IN  REGION_PERMISSION_UPDATE_FUNC           ReadWriteUpdater
+  );
+
+
+/**
+  Privileged firmware assigns RO & Executable attributes to all memory occupied
+  by the Boot Firmware Volume. This function locates the section information of
+  the Standalone MM Core module to be able to change permissions of the
+  individual sections later in the boot process.
+
+  @param  TeData                 Pointer to PE/COFF image data
+  @param  ImageContext           Pointer to PE/COFF image context
+  @param  SectionHeaderOffset    Offset of PE/COFF image section header
+  @param  NumberOfSections       Number of Sections
+
+**/
+EFI_STATUS
+EFIAPI
+GetStandaloneMmCorePeCoffSections (
+  IN        VOID                            *TeData,
+  IN  OUT   PE_COFF_LOADER_IMAGE_CONTEXT    *ImageContext,
+  IN  OUT   UINT32                          *SectionHeaderOffset;
+  IN  OUT   UINTN                           *NumberOfSections;
+  );
+
+
+/**
+  Privileged firmware assigns RO & Executable attributes to all memory occupied
+  by the Boot Firmware Volume. This function locates the Standalone MM Core
+  module PE/COFF image in the BFV and returns this information.
+
+  @param  BfvAddress             Base Address of Boot Firmware Volume
+  @param  TeData                 Pointer to address for allocating memory for
+                                 PE/COFF image data
+  @param  TeDataSize             Pointer to size of PE/COFF image data
+
+**/
+EFI_STATUS
+EFIAPI
+LocateStandaloneMmCorePeCoffData (
+  IN        EFI_FIRMWARE_VOLUME_HEADER      *BfvAddress,
+  IN  OUT   VOID                            **TeData;
+  IN  OUT   UINTN                           *TeDataSize;
+  );
+
+
+/**
+  Use the boot information passed by privileged firmware to populate a HOB list
+  suitable for consumption by the MM Core and drivers.
+
+  @param  CpuDriverEntryPoint    Address of MM CPU driver entrypoint
+  @param  PayloadBootInfo        Boot information passed by privileged firmware
+
+**/
+VOID *
+EFIAPI
+CreateHobListFromBootInfo (
+  IN  OUT  PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT *CpuDriverEntryPoint,
+  IN       EFI_SECURE_PARTITION_BOOT_INFO     *PayloadBootInfo
+  );
+
+
+/**
+  The entry point of Standalone MM Foundation.
+
+  @param  HobStart  Pointer to the beginning of the HOB List.
+
+**/
+VOID
+EFIAPI
+_ModuleEntryPoint (
+  IN VOID    *SharedBufAddress,
+  IN UINT64  SharedBufSize,
+  IN UINT64  cookie1,
+  IN UINT64  cookie2
+  );
+
+
+/**
+  Autogenerated function that calls the library constructors for all of the module's dependent libraries.
+
+  This function must be called by _ModuleEntryPoint().
+  This function calls the set of library constructors for the set of library instances
+  that a module depends on.  This includes library instances that a module depends on
+  directly and library instances that a module depends on indirectly through other
+  libraries. This function is autogenerated by build tools and those build tools are
+  responsible for collecting the set of library instances, determine which ones have
+  constructors, and calling the library constructors in the proper order based upon
+  each of the library instances own dependencies.
+
+  @param  ImageHandle  The image handle of the DXE Core.
+  @param  SystemTable  A pointer to the EFI System Table.
+
+**/
+VOID
+EFIAPI
+ProcessLibraryConstructorList (
+  IN EFI_HANDLE             ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE  *MmSystemTable
+  );
+
+
+/**
+  Autogenerated function that calls a set of module entry points.
+
+  This function must be called by _ModuleEntryPoint().
+  This function calls the set of module entry points.
+  This function is autogenerated by build tools and those build tools are responsible
+  for collecting the module entry points and calling them in a specified order.
+
+  @param  HobStart  Pointer to the beginning of the HOB List passed in from the PEI Phase.
+
+**/
+VOID
+EFIAPI
+ProcessModuleEntryPointList (
+  IN VOID  *HobStart
+  );
+
+#endif
diff --git a/StandaloneMmPkg/Include/Library/MmCoreStandaloneEntryPoint.h b/StandaloneMmPkg/Include/Library/MmCoreStandaloneEntryPoint.h
new file mode 100644
index 0000000000..d6105d4935
--- /dev/null
+++ b/StandaloneMmPkg/Include/Library/MmCoreStandaloneEntryPoint.h
@@ -0,0 +1,101 @@
+/** @file
+  Module entry point library for DXE core.
+
+Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+
+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
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef __MODULE_ENTRY_POINT_H__
+#define __MODULE_ENTRY_POINT_H__
+
+///
+/// Global variable that contains a pointer to the Hob List passed into the DXE Core entry point.
+///
+extern VOID  *gHobList;
+
+
+/**
+  The entry point of PE/COFF Image for the DXE Core.
+
+  This function is the entry point for the DXE Core. This function is required to call
+  ProcessModuleEntryPointList() and ProcessModuleEntryPointList() is never expected to return.
+  The DXE Core is responsible for calling ProcessLibraryConstructorList() as soon as the EFI
+  System Table and the image handle for the DXE Core itself have been established.
+  If ProcessModuleEntryPointList() returns, then ASSERT() and halt the system.
+
+  @param  HobStart  Pointer to the beginning of the HOB List passed in from the PEI Phase.
+
+**/
+VOID
+EFIAPI
+_ModuleEntryPoint (
+  IN VOID  *HobStart
+  );
+
+
+/**
+  Required by the EBC compiler and identical in functionality to _ModuleEntryPoint().
+
+  This function is required to call _ModuleEntryPoint() passing in HobStart.
+
+  @param  HobStart  Pointer to the beginning of the HOB List passed in from the PEI Phase.
+
+**/
+VOID
+EFIAPI
+EfiMain (
+  IN VOID  *HobStart
+  );
+
+
+/**
+  Autogenerated function that calls the library constructors for all of the module's dependent libraries.
+
+  This function must be called by _ModuleEntryPoint().
+  This function calls the set of library constructors for the set of library instances
+  that a module depends on.  This includes library instances that a module depends on
+  directly and library instances that a module depends on indirectly through other
+  libraries. This function is autogenerated by build tools and those build tools are
+  responsible for collecting the set of library instances, determine which ones have
+  constructors, and calling the library constructors in the proper order based upon
+  each of the library instances own dependencies.
+
+  @param  ImageHandle  The image handle of the DXE Core.
+  @param  SystemTable  A pointer to the EFI System Table.
+
+**/
+VOID
+EFIAPI
+ProcessLibraryConstructorList (
+  IN EFI_HANDLE             ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
+  );
+
+
+/**
+  Autogenerated function that calls a set of module entry points.
+
+  This function must be called by _ModuleEntryPoint().
+  This function calls the set of module entry points.
+  This function is auto generated by build tools and those build tools are responsible
+  for collecting the module entry points and calling them in a specified order.
+
+  @param  HobStart  Pointer to the beginning of the HOB List passed in from the PEI Phase.
+
+**/
+VOID
+EFIAPI
+ProcessModuleEntryPointList (
+  IN VOID  *HobStart
+  );
+
+#endif
diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/CreateHobList.c b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/CreateHobList.c
new file mode 100644
index 0000000000..f9b3faea8f
--- /dev/null
+++ b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/CreateHobList.c
@@ -0,0 +1,200 @@
+/** @file
+  Entry point to the Standalone MM Foundation when initialized during the SEC
+  phase on ARM platforms
+
+Copyright (c) 2017, ARM Ltd. All rights reserved.<BR>
+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.
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+
+#include <PiMm.h>
+
+#include <PiPei.h>
+#include <Guid/MmramMemoryReserve.h>
+#include <Guid/MpInformation.h>
+
+#include <Library/Arm/StandaloneMmCoreEntryPoint.h>
+#include <Library/ArmMmuLib.h>
+#include <Library/ArmSvcLib.h>
+#include <Library/DebugLib.h>
+#include <Library/HobLib.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/SerialPortLib.h>
+
+#include <IndustryStandard/ArmStdSmc.h>
+
+extern EFI_HOB_HANDOFF_INFO_TABLE*
+HobConstructor (
+  IN VOID   *EfiMemoryBegin,
+  IN UINTN  EfiMemoryLength,
+  IN VOID   *EfiFreeMemoryBottom,
+  IN VOID   *EfiFreeMemoryTop
+  );
+
+// GUID to identify HOB with whereabouts of communication buffer with Normal
+// World
+extern EFI_GUID gEfiStandaloneMmNonSecureBufferGuid;
+
+// GUID to identify HOB where the entry point of the CPU driver will be
+// populated to allow this entry point driver to invoke it upon receipt of an
+// event
+extern EFI_GUID gEfiArmTfCpuDriverEpDescriptorGuid;
+
+/**
+  Use the boot information passed by privileged firmware to populate a HOB list
+  suitable for consumption by the MM Core and drivers.
+
+  @param  PayloadBootInfo    Boot information passed by privileged firmware
+
+**/
+VOID *
+CreateHobListFromBootInfo (
+  IN  OUT  PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT *CpuDriverEntryPoint,
+  IN       EFI_SECURE_PARTITION_BOOT_INFO     *PayloadBootInfo
+)
+{
+  EFI_HOB_HANDOFF_INFO_TABLE      *HobStart;
+  EFI_RESOURCE_ATTRIBUTE_TYPE     Attributes;
+  UINT32                          Index;
+  UINT32                          BufferSize;
+  UINT32                          Flags;
+  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHob;
+  EFI_MMRAM_DESCRIPTOR            *MmramRanges;
+  EFI_MMRAM_DESCRIPTOR            *NsCommBufMmramRange;
+  MP_INFORMATION_HOB_DATA         *MpInformationHobData;
+  EFI_PROCESSOR_INFORMATION       *ProcInfoBuffer;
+  EFI_SECURE_PARTITION_CPU_INFO   *CpuInfo;
+  ARM_TF_CPU_DRIVER_EP_DESCRIPTOR *CpuDriverEntryPointDesc;
+
+  // Create a hoblist with a PHIT and EOH
+  HobStart = HobConstructor ((VOID *) PayloadBootInfo->SpMemBase,
+    (UINTN)  PayloadBootInfo->SpMemLimit - PayloadBootInfo->SpMemBase,
+    (VOID *) PayloadBootInfo->SpHeapBase,
+    (VOID *) (PayloadBootInfo->SpHeapBase + PayloadBootInfo->SpHeapSize));
+
+  // Check that the Hoblist starts at the bottom of the Heap
+  ASSERT (HobStart == (VOID *) PayloadBootInfo->SpHeapBase);
+
+  // Build a Boot Firmware Volume HOB
+  BuildFvHob(PayloadBootInfo->SpImageBase, PayloadBootInfo->SpImageSize);
+
+  // Build a resource descriptor Hob that describes the available physical
+  // memory range
+  Attributes =(
+    EFI_RESOURCE_ATTRIBUTE_PRESENT |
+    EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
+    EFI_RESOURCE_ATTRIBUTE_TESTED |
+    EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
+    EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
+    EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
+    EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
+  );
+
+  BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,
+    Attributes,
+    (UINTN) PayloadBootInfo->SpMemBase,
+    PayloadBootInfo->SpMemLimit - PayloadBootInfo->SpMemBase);
+
+  // Find the size of the GUIDed HOB with MP information
+  BufferSize = sizeof (MP_INFORMATION_HOB_DATA);
+  BufferSize += sizeof (EFI_PROCESSOR_INFORMATION) * PayloadBootInfo->NumCpus;
+
+  // Create a Guided MP information HOB to enable the ARM TF CPU driver to
+  // perform per-cpu allocations.
+  MpInformationHobData = BuildGuidHob(&gMpInformationHobGuid, BufferSize);
+
+  // Populate the MP information HOB with the topology information passed by
+  // privileged firmware
+  MpInformationHobData->NumberOfProcessors = PayloadBootInfo->NumCpus;
+  MpInformationHobData->NumberOfEnabledProcessors = PayloadBootInfo->NumCpus;
+  ProcInfoBuffer = MpInformationHobData->ProcessorInfoBuffer;
+  CpuInfo = PayloadBootInfo->CpuInfo;
+
+  for (Index = 0; Index < PayloadBootInfo->NumCpus; Index++) {
+    ProcInfoBuffer[Index].ProcessorId      = CpuInfo[Index].Mpidr;
+    ProcInfoBuffer[Index].Location.Package = GET_CLUSTER_ID(CpuInfo[Index].Mpidr);
+    ProcInfoBuffer[Index].Location.Core    = GET_CORE_ID(CpuInfo[Index].Mpidr);
+    ProcInfoBuffer[Index].Location.Thread  = GET_CORE_ID(CpuInfo[Index].Mpidr);
+
+    Flags = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;
+    if (CpuInfo[Index].Flags & CPU_INFO_FLAG_PRIMARY_CPU) {
+      Flags |= PROCESSOR_AS_BSP_BIT;
+    }
+    ProcInfoBuffer[Index].StatusFlag = Flags;
+  }
+
+  // Create a Guided HOB to tell the ARM TF CPU driver the location and length
+  // of the communication buffer shared with the Normal world.
+  NsCommBufMmramRange = (EFI_MMRAM_DESCRIPTOR *) BuildGuidHob (&gEfiStandaloneMmNonSecureBufferGuid, sizeof(EFI_MMRAM_DESCRIPTOR));
+  NsCommBufMmramRange->PhysicalStart = PayloadBootInfo->SpNsCommBufBase;
+  NsCommBufMmramRange->CpuStart      = PayloadBootInfo->SpNsCommBufBase;
+  NsCommBufMmramRange->PhysicalSize  = PayloadBootInfo->SpNsCommBufSize;
+  NsCommBufMmramRange->RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
+
+  // Create a Guided HOB to enable the ARM TF CPU driver to share its entry
+  // point and populate it with the address of the shared buffer
+  CpuDriverEntryPointDesc = (ARM_TF_CPU_DRIVER_EP_DESCRIPTOR *) BuildGuidHob (&gEfiArmTfCpuDriverEpDescriptorGuid, sizeof(ARM_TF_CPU_DRIVER_EP_DESCRIPTOR));
+
+  *CpuDriverEntryPoint = NULL;
+  CpuDriverEntryPointDesc->ArmTfCpuDriverEpPtr = CpuDriverEntryPoint;
+
+  // Find the size of the GUIDed HOB with SRAM ranges
+  BufferSize = sizeof (EFI_MMRAM_HOB_DESCRIPTOR_BLOCK);
+  BufferSize += PayloadBootInfo->NumSpMemRegions *
+    sizeof(EFI_MMRAM_DESCRIPTOR);
+
+  // Create a GUIDed HOB with SRAM ranges
+  MmramRangesHob = BuildGuidHob (&gEfiMmPeiMmramMemoryReserveGuid, BufferSize);
+
+  // Fill up the number of MMRAM memory regions
+  MmramRangesHob->NumberOfMmReservedRegions = PayloadBootInfo->NumSpMemRegions;
+  // Fill up the MMRAM ranges
+  MmramRanges = &MmramRangesHob->Descriptor[0];
+
+  // Base and size of memory occupied by the Standalone MM image
+  MmramRanges[0].PhysicalStart = PayloadBootInfo->SpImageBase;
+  MmramRanges[0].CpuStart      = PayloadBootInfo->SpImageBase;
+  MmramRanges[0].PhysicalSize  = PayloadBootInfo->SpImageSize;
+  MmramRanges[0].RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
+
+  // Base and size of buffer shared with privileged Secure world software
+  MmramRanges[1].PhysicalStart = PayloadBootInfo->SpSharedBufBase;
+  MmramRanges[1].CpuStart      = PayloadBootInfo->SpSharedBufBase;
+  MmramRanges[1].PhysicalSize  = PayloadBootInfo->SpPcpuSharedBufSize * PayloadBootInfo->NumCpus;
+  MmramRanges[1].RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
+
+  // Base and size of buffer used for synchronous communication with Normal
+  // world software
+  MmramRanges[2].PhysicalStart = PayloadBootInfo->SpNsCommBufBase;
+  MmramRanges[2].CpuStart      = PayloadBootInfo->SpNsCommBufBase;
+  MmramRanges[2].PhysicalSize  = PayloadBootInfo->SpNsCommBufSize;
+  MmramRanges[2].RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
+
+  // Base and size of memory allocated for stacks for all cpus
+  MmramRanges[3].PhysicalStart = PayloadBootInfo->SpStackBase;
+  MmramRanges[3].CpuStart      = PayloadBootInfo->SpStackBase;
+  MmramRanges[3].PhysicalSize  = PayloadBootInfo->SpPcpuStackSize * PayloadBootInfo->NumCpus;
+  MmramRanges[3].RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
+
+  // Base and size of heap memory shared by all cpus
+  MmramRanges[4].PhysicalStart = (EFI_PHYSICAL_ADDRESS) HobStart;
+  MmramRanges[4].CpuStart      = (EFI_PHYSICAL_ADDRESS) HobStart;
+  MmramRanges[4].PhysicalSize  = HobStart->EfiFreeMemoryBottom - (EFI_PHYSICAL_ADDRESS) HobStart;
+  MmramRanges[4].RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
+
+  // Base and size of heap memory shared by all cpus
+  MmramRanges[5].PhysicalStart = HobStart->EfiFreeMemoryBottom;
+  MmramRanges[5].CpuStart      = HobStart->EfiFreeMemoryBottom;
+  MmramRanges[5].PhysicalSize  = HobStart->EfiFreeMemoryTop - HobStart->EfiFreeMemoryBottom;
+  MmramRanges[5].RegionState   = EFI_CACHEABLE;
+
+  return HobStart;
+}
diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/SetPermissions.c b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/SetPermissions.c
new file mode 100644
index 0000000000..e96b81cdc0
--- /dev/null
+++ b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/SetPermissions.c
@@ -0,0 +1,278 @@
+/** @file
+  Entry point to the Standalone MM Foundation when initialised during the SEC
+  phase on ARM platforms
+
+Copyright (c) 2017, ARM Ltd. All rights reserved.<BR>
+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.
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+
+#include <PiMm.h>
+
+
+
+#include <PiPei.h>
+#include <Guid/MmramMemoryReserve.h>
+#include <Guid/MpInformation.h>
+
+#include <Library/Arm/StandaloneMmCoreEntryPoint.h>
+#include <Library/ArmMmuLib.h>
+#include <Library/ArmSvcLib.h>
+#include <Library/DebugLib.h>
+#include <Library/HobLib.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/SerialPortLib.h>
+
+
+#include <IndustryStandard/ArmStdSmc.h>
+
+EFI_STATUS
+EFIAPI
+UpdateMmFoundationPeCoffPermissions (
+  IN  CONST PE_COFF_LOADER_IMAGE_CONTEXT      *ImageContext,
+  IN  UINT32                                  SectionHeaderOffset,
+  IN  CONST  UINTN                            NumberOfSections,
+  IN  REGION_PERMISSION_UPDATE_FUNC           TextUpdater,
+  IN  REGION_PERMISSION_UPDATE_FUNC           ReadOnlyUpdater,
+  IN  REGION_PERMISSION_UPDATE_FUNC           ReadWriteUpdater
+  )
+{
+  EFI_IMAGE_SECTION_HEADER         SectionHeader;
+  RETURN_STATUS                    Status;
+  EFI_PHYSICAL_ADDRESS             Base;
+  UINTN                            Size;
+  UINTN                            ReadSize;
+  UINTN                            Index;
+
+  ASSERT (ImageContext != NULL);
+
+  //
+  // Iterate over the sections
+  //
+  for (Index = 0; Index < NumberOfSections; Index++) {
+    //
+    // Read section header from file
+    //
+    Size = sizeof (EFI_IMAGE_SECTION_HEADER);
+    ReadSize = Size;
+    Status = ImageContext->ImageRead (ImageContext->Handle, SectionHeaderOffset,
+                                   &Size, &SectionHeader);
+    if (RETURN_ERROR (Status) || (Size != ReadSize)) {
+      DEBUG ((DEBUG_ERROR,
+        "%a: ImageContext->ImageRead () failed (Status = %r)\n",
+        __FUNCTION__, Status));
+      return Status;
+    }
+
+    DEBUG ((DEBUG_INFO,
+            "%a: Section %d of image at 0x%lx has 0x%x permissions\n",
+            __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.Characteristics));
+    DEBUG ((DEBUG_INFO,
+            "%a: Section %d of image at 0x%lx has %s name\n",
+            __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.Name));
+    DEBUG ((DEBUG_INFO,
+            "%a: Section %d of image at 0x%lx has 0x%x address\n",
+            __FUNCTION__, Index, ImageContext->ImageAddress, ImageContext->ImageAddress + SectionHeader.VirtualAddress));
+    DEBUG ((DEBUG_INFO,
+            "%a: Section %d of image at 0x%lx has 0x%x data\n",
+            __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.PointerToRawData));
+
+    //
+    // If the section is marked as XN then remove the X attribute. Furthermore,
+    // if it is a writeable section then mark it appropriately as well.
+    //
+    if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_MEM_EXECUTE) == 0) {
+      Base = ImageContext->ImageAddress + SectionHeader.VirtualAddress;
+
+      TextUpdater (Base, SectionHeader.Misc.VirtualSize);
+
+      if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_MEM_WRITE) != 0) {
+        ReadWriteUpdater (Base, SectionHeader.Misc.VirtualSize);
+        DEBUG ((DEBUG_INFO,
+                "%a: Mapping section %d of image at 0x%lx with RW-XN permissions\n",
+                __FUNCTION__, Index, ImageContext->ImageAddress));
+      } else {
+        DEBUG ((DEBUG_INFO,
+                "%a: Mapping section %d of image at 0x%lx with RO-XN permissions\n",
+                __FUNCTION__, Index, ImageContext->ImageAddress));
+      }
+    } else {
+        DEBUG ((DEBUG_INFO,
+                "%a: Ignoring section %d of image at 0x%lx with 0x%x permissions\n",
+                __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.Characteristics));
+    }
+    SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
+  }
+
+  return RETURN_SUCCESS;
+}
+
+EFI_STATUS
+EFIAPI
+LocateStandaloneMmCorePeCoffData (
+  IN        EFI_FIRMWARE_VOLUME_HEADER      *BfvAddress,
+  IN  OUT   VOID                            **TeData,
+  IN  OUT   UINTN                           *TeDataSize
+  )
+{
+  EFI_FFS_FILE_HEADER             *FileHeader = NULL;
+  EFI_STATUS                      Status;
+
+  Status = FfsFindNextFile (
+                  EFI_FV_FILETYPE_SECURITY_CORE,
+                  BfvAddress,
+                  &FileHeader);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "Unable to locate Standalone MM FFS file - 0x%x\n",
+      Status));
+    return Status;
+  }
+
+  Status = FfsFindSectionData (EFI_SECTION_PE32, FileHeader, TeData, TeDataSize);
+  if (EFI_ERROR (Status)) {
+      DEBUG ((DEBUG_ERROR, "Unable to locate Standalone MM Section data - 0x%x\n",
+              Status));
+    return Status;
+  }
+
+  DEBUG ((DEBUG_INFO, "Found Standalone MM PE data - 0x%x\n", *TeData));
+  return Status;
+}
+
+static
+EFI_STATUS
+GetPeCoffSectionInformation (
+  IN  CONST PE_COFF_LOADER_IMAGE_CONTEXT      *ImageContext,
+  IN  OUT   PE_COFF_LOADER_IMAGE_CONTEXT      *TmpContext,
+  IN  OUT   UINT32                            *SectionHeaderOffset,
+  IN  OUT   UINTN                             *NumberOfSections
+  )
+{
+  RETURN_STATUS                         Status;
+  EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION   Hdr;
+  EFI_IMAGE_OPTIONAL_HEADER_UNION       HdrData;
+  UINTN                                 Size;
+  UINTN                                 ReadSize;
+
+  ASSERT (ImageContext != NULL);
+  ASSERT (TmpContext != NULL);
+  ASSERT (SectionHeaderOffset != NULL);
+  ASSERT (NumberOfSections != NULL);
+
+  //
+  // We need to copy ImageContext since PeCoffLoaderGetImageInfo ()
+  // will mangle the ImageAddress field
+  //
+  CopyMem (TmpContext, ImageContext, sizeof (*TmpContext));
+
+  if (TmpContext->PeCoffHeaderOffset == 0) {
+    Status = PeCoffLoaderGetImageInfo (TmpContext);
+    if (RETURN_ERROR (Status)) {
+      DEBUG ((DEBUG_ERROR,
+        "%a: PeCoffLoaderGetImageInfo () failed (Status = %r)\n",
+        __FUNCTION__, Status));
+      return Status;
+    }
+  }
+
+  if (TmpContext->IsTeImage &&
+      TmpContext->ImageAddress == ImageContext->ImageAddress) {
+    DEBUG ((DEBUG_INFO, "%a: ignoring XIP TE image at 0x%lx\n", __FUNCTION__,
+      ImageContext->ImageAddress));
+    return RETURN_UNSUPPORTED;
+  }
+
+  if (TmpContext->SectionAlignment < EFI_PAGE_SIZE) {
+    //
+    // The sections need to be at least 4 KB aligned, since that is the
+    // granularity at which we can tighten permissions.
+    //
+    if (!TmpContext->IsTeImage) {
+      DEBUG ((DEBUG_WARN,
+        "%a: non-TE Image at 0x%lx has SectionAlignment < 4 KB (%lu)\n",
+        __FUNCTION__, ImageContext->ImageAddress, TmpContext->SectionAlignment));
+    }
+    return RETURN_UNSUPPORTED;
+  }
+
+  //
+  // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
+  // data, but that should not hurt anything. Hdr.Pe32->OptionalHeader.Magic
+  // determines if this is a PE32 or PE32+ image. The magic is in the same
+  // location in both images.
+  //
+  Hdr.Union = &HdrData;
+  Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);
+  ReadSize = Size;
+  Status = TmpContext->ImageRead (TmpContext->Handle,
+                         TmpContext->PeCoffHeaderOffset, &Size, Hdr.Pe32);
+  if (RETURN_ERROR (Status) || (Size != ReadSize)) {
+    DEBUG ((DEBUG_ERROR,
+      "%a: TmpContext->ImageRead () failed (Status = %r)\n",
+      __FUNCTION__, Status));
+    return Status;
+  }
+
+  ASSERT (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE);
+
+  *SectionHeaderOffset = TmpContext->PeCoffHeaderOffset + sizeof (UINT32) +
+                        sizeof (EFI_IMAGE_FILE_HEADER);
+  *NumberOfSections    = (UINTN)(Hdr.Pe32->FileHeader.NumberOfSections);
+
+  switch (Hdr.Pe32->OptionalHeader.Magic) {
+    case EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC:
+      *SectionHeaderOffset += Hdr.Pe32->FileHeader.SizeOfOptionalHeader;
+      break;
+    case EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC:
+      *SectionHeaderOffset += Hdr.Pe32Plus->FileHeader.SizeOfOptionalHeader;
+      break;
+    default:
+      ASSERT (FALSE);
+  }
+
+  return RETURN_SUCCESS;
+}
+
+EFI_STATUS
+EFIAPI
+GetStandaloneMmCorePeCoffSections (
+  IN        VOID                            *TeData,
+  IN  OUT   PE_COFF_LOADER_IMAGE_CONTEXT    *ImageContext,
+  IN  OUT   UINT32                          *SectionHeaderOffset,
+  IN  OUT   UINTN                           *NumberOfSections
+  )
+{
+  EFI_STATUS                   Status;
+  PE_COFF_LOADER_IMAGE_CONTEXT TmpContext;
+
+  // Initialize the Image Context
+  ZeroMem (ImageContext, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT));
+  ImageContext->Handle    = TeData;
+  ImageContext->ImageRead = PeCoffLoaderImageReadFromMemory;
+
+  DEBUG((DEBUG_INFO, "Found Standalone MM PE data - 0x%x\n", TeData));
+
+  Status = PeCoffLoaderGetImageInfo (ImageContext);
+  if (EFI_ERROR(Status)) {
+    DEBUG((DEBUG_ERROR, "Unable to locate Standalone MM Core PE-COFF Image information - 0x%x\n", Status));
+    return Status;
+  }
+
+  Status = GetPeCoffSectionInformation(ImageContext, &TmpContext, SectionHeaderOffset, NumberOfSections);
+  if (EFI_ERROR(Status)) {
+    DEBUG((DEBUG_ERROR, "Unable to locate Standalone MM Core PE-COFF Section information - 0x%x\n", Status));
+    return Status;
+  }
+
+  DEBUG((DEBUG_INFO, "Standalone MM Core PE-COFF SectionHeaderOffset - 0x%x, NumberOfSections - %d\n", *SectionHeaderOffset, *NumberOfSections));
+
+  return Status;
+}
diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c
new file mode 100644
index 0000000000..72e3b834d4
--- /dev/null
+++ b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c
@@ -0,0 +1,264 @@
+/** @file
+  Entry point to the Standalone MM Foundation when initialized during the SEC
+  phase on ARM platforms
+
+Copyright (c) 2017, ARM Ltd. All rights reserved.<BR>
+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.
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+
+#include <PiMm.h>
+
+#include <Library/Arm/StandaloneMmCoreEntryPoint.h>
+
+#include <PiPei.h>
+#include <Guid/MmramMemoryReserve.h>
+#include <Guid/MpInformation.h>
+
+#include <Library/ArmMmuLib.h>
+#include <Library/ArmSvcLib.h>
+#include <Library/DebugLib.h>
+#include <Library/HobLib.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/SerialPortLib.h>
+
+#include <IndustryStandard/ArmStdSmc.h>
+#include <IndustryStandard/ArmMmSvc.h>
+
+#define SPM_MAJOR_VER_MASK        0xFFFF0000
+#define SPM_MINOR_VER_MASK        0x0000FFFF
+#define SPM_MAJOR_VER_SHIFT       16
+
+const UINT32 SPM_MAJOR_VER = 0;
+const UINT32 SPM_MINOR_VER = 1;
+
+const UINT8 BOOT_PAYLOAD_VERSION = 1;
+
+PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT      CpuDriverEntryPoint = NULL;
+
+/**
+  Retrieve a pointer to and print the boot information passed by privileged
+  secure firmware
+
+  @param  SharedBufAddress The pointer memory shared with privileged firmware
+
+**/
+EFI_SECURE_PARTITION_BOOT_INFO *
+GetAndPrintBootinformation (
+  IN VOID                      *SharedBufAddress
+)
+{
+  EFI_SECURE_PARTITION_BOOT_INFO *PayloadBootInfo;
+  EFI_SECURE_PARTITION_CPU_INFO  *PayloadCpuInfo;
+  UINTN                          Index;
+
+  PayloadBootInfo = (EFI_SECURE_PARTITION_BOOT_INFO *) SharedBufAddress;
+
+  if (PayloadBootInfo->Header.Version != BOOT_PAYLOAD_VERSION)
+  {
+    DEBUG((DEBUG_ERROR, "Boot Information Version Mismatch. Current=0x%x, Expected=0x%x.\n", PayloadBootInfo->Header.Version, BOOT_PAYLOAD_VERSION));
+    return NULL;
+  }
+  DEBUG((DEBUG_INFO, "NumSpMemRegions - 0x%x\n", PayloadBootInfo->NumSpMemRegions));
+  DEBUG((DEBUG_INFO, "SpMemBase       - 0x%lx\n", PayloadBootInfo->SpMemBase));
+  DEBUG((DEBUG_INFO, "SpMemLimit      - 0x%lx\n", PayloadBootInfo->SpMemLimit));
+  DEBUG((DEBUG_INFO, "SpImageBase     - 0x%lx\n", PayloadBootInfo->SpImageBase));
+  DEBUG((DEBUG_INFO, "SpStackBase     - 0x%lx\n", PayloadBootInfo->SpStackBase));
+  DEBUG((DEBUG_INFO, "SpHeapBase      - 0x%lx\n", PayloadBootInfo->SpHeapBase));
+  DEBUG((DEBUG_INFO, "SpNsCommBufBase - 0x%lx\n", PayloadBootInfo->SpNsCommBufBase));
+  DEBUG((DEBUG_INFO, "SpSharedBufBase - 0x%lx\n", PayloadBootInfo->SpSharedBufBase));
+
+  DEBUG((DEBUG_INFO, "SpImageSize     - 0x%x\n", PayloadBootInfo->SpImageSize));
+  DEBUG((DEBUG_INFO, "SpPcpuStackSize - 0x%x\n", PayloadBootInfo->SpPcpuStackSize));
+  DEBUG((DEBUG_INFO, "SpHeapSize      - 0x%x\n", PayloadBootInfo->SpHeapSize));
+  DEBUG((DEBUG_INFO, "SpNsCommBufSize - 0x%x\n", PayloadBootInfo->SpNsCommBufSize));
+  DEBUG((DEBUG_INFO, "SpPcpuSharedBufSize - 0x%x\n", PayloadBootInfo->SpPcpuSharedBufSize));
+
+  DEBUG((DEBUG_INFO, "NumCpus         - 0x%x\n", PayloadBootInfo->NumCpus));
+  DEBUG((DEBUG_INFO, "CpuInfo         - 0x%p\n", PayloadBootInfo->CpuInfo));
+
+  PayloadCpuInfo = (EFI_SECURE_PARTITION_CPU_INFO *) PayloadBootInfo->CpuInfo;
+
+  for (Index = 0; Index < PayloadBootInfo->NumCpus; Index++) {
+    DEBUG((DEBUG_INFO, "Mpidr           - 0x%lx\n", PayloadCpuInfo[Index].Mpidr));
+    DEBUG((DEBUG_INFO, "LinearId        - 0x%x\n", PayloadCpuInfo[Index].LinearId));
+    DEBUG((DEBUG_INFO, "Flags           - 0x%x\n", PayloadCpuInfo[Index].Flags));
+  }
+
+  return PayloadBootInfo;
+}
+
+VOID
+EFIAPI
+DelegatedEventLoop (
+  IN ARM_SVC_ARGS *EventCompleteSvcArgs
+  )
+{
+  EFI_STATUS Status;
+
+  while(TRUE) {
+    ArmCallSvc(EventCompleteSvcArgs);
+
+    DEBUG ((DEBUG_INFO, "Received delegated event\n"));
+    DEBUG ((DEBUG_INFO, "X0 :  0x%x\n", (UINT32) EventCompleteSvcArgs->Arg0));
+    DEBUG ((DEBUG_INFO, "X1 :  0x%x\n", (UINT32) EventCompleteSvcArgs->Arg1));
+    DEBUG ((DEBUG_INFO, "X2 :  0x%x\n", (UINT32) EventCompleteSvcArgs->Arg2));
+    DEBUG ((DEBUG_INFO, "X3 :  0x%x\n", (UINT32) EventCompleteSvcArgs->Arg3));
+
+    Status = CpuDriverEntryPoint(
+      EventCompleteSvcArgs->Arg0,
+      EventCompleteSvcArgs->Arg2,
+      EventCompleteSvcArgs->Arg1);
+    if (EFI_ERROR(Status)) {
+      DEBUG((DEBUG_ERROR, "Failed delegated event 0x%x, Status 0x%x\n",
+        EventCompleteSvcArgs->Arg0, Status));
+    }
+
+    EventCompleteSvcArgs->Arg0 = ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64;
+    EventCompleteSvcArgs->Arg1 = Status;
+  }
+}
+
+STATIC
+EFI_STATUS
+GetSpmVersion ()
+{
+  EFI_STATUS   Status;
+  UINT16       SpmMajorVersion;
+  UINT16       SpmMinorVersion;
+  UINT32       SpmVersion;
+  ARM_SVC_ARGS SpmVersionArgs;
+
+  SpmVersionArgs.Arg0 = ARM_SVC_ID_SPM_VERSION_AARCH32;
+
+  ArmCallSvc (&SpmVersionArgs);
+
+  SpmVersion = SpmVersionArgs.Arg0;
+
+  SpmMajorVersion = ((SpmVersion & SPM_MAJOR_VER_MASK) >> SPM_MAJOR_VER_SHIFT);
+  SpmMinorVersion = ((SpmVersion & SPM_MINOR_VER_MASK) >> 0);
+
+  // Different major revision values indicate possibly incompatible functions.
+  // For two revisions, A and B, for which the major revision values are
+  // identical, if the minor revision value of revision B is greater than
+  // the minor revision value of revision A, then every function in
+  // revision A must work in a compatible way with revision B.
+  // However, it is possible for revision B to have a higher
+  // function count than revision A.
+  if ((SpmMajorVersion == SPM_MAJOR_VER) &&
+      (SpmMinorVersion >= SPM_MINOR_VER))
+  {
+    DEBUG ((DEBUG_INFO, "SPM Version: Major=0x%x, Minor=0x%x\n",
+           SpmMajorVersion, SpmMinorVersion));
+    Status = EFI_SUCCESS;
+  }
+  else
+  {
+    DEBUG ((DEBUG_INFO, "Incompatible SPM Versions.\n Current Version: Major=0x%x, Minor=0x%x.\n Expected: Major=0x%x, Minor>=0x%x.\n",
+            SpmMajorVersion, SpmMinorVersion, SPM_MAJOR_VER, SPM_MINOR_VER));
+    Status = EFI_UNSUPPORTED;
+  }
+
+  return Status;
+}
+
+/**
+  The entry point of Standalone MM Foundation.
+
+  @param  HobStart  The pointer to the beginning of the HOB List.
+
+**/
+VOID
+EFIAPI
+_ModuleEntryPoint (
+  IN VOID    *SharedBufAddress,
+  IN UINT64  SharedBufSize,
+  IN UINT64  cookie1,
+  IN UINT64  cookie2
+  )
+{
+  PE_COFF_LOADER_IMAGE_CONTEXT            ImageContext;
+  EFI_SECURE_PARTITION_BOOT_INFO          *PayloadBootInfo;
+  ARM_SVC_ARGS                            InitMmFoundationSvcArgs = {0};
+  EFI_STATUS                              Status;
+  UINT32                                  SectionHeaderOffset;
+  UINTN                                   NumberOfSections;
+  VOID                                    *HobStart;
+  VOID                                    *TeData;
+  UINTN                                   TeDataSize;
+
+  Status = SerialPortInitialize ();
+  ASSERT_EFI_ERROR (Status);
+
+  // Get Secure Partition Manager Version Information
+  Status = GetSpmVersion();
+  if (EFI_ERROR(Status)) {
+    goto finish;
+  }
+
+  PayloadBootInfo = GetAndPrintBootinformation(SharedBufAddress);
+  if (PayloadBootInfo == NULL) {
+    Status = EFI_UNSUPPORTED;
+    goto finish;
+  }
+
+  // Locate PE/COFF File information for the Standalone MM core module
+  Status = LocateStandaloneMmCorePeCoffData (
+                 (EFI_FIRMWARE_VOLUME_HEADER *) PayloadBootInfo->SpImageBase,
+                 &TeData,
+                 &TeDataSize);
+  if (EFI_ERROR(Status)) {
+    goto finish;
+  }
+
+  // Obtain the PE/COFF Section information for the Standalone MM core module
+  Status = GetStandaloneMmCorePeCoffSections (
+            TeData,
+            &ImageContext,
+            &SectionHeaderOffset,
+            &NumberOfSections);
+  if (EFI_ERROR(Status)) {
+    goto finish;
+  }
+
+  // Update the memory access permissions of individual sections in the
+  // Standalone MM core module
+  Status = UpdateMmFoundationPeCoffPermissions(
+            &ImageContext,
+            SectionHeaderOffset,
+            NumberOfSections,
+            ArmSetMemoryRegionNoExec,
+            ArmSetMemoryRegionReadOnly,
+            ArmClearMemoryRegionReadOnly);
+  if (EFI_ERROR(Status)) {
+    goto finish;
+  }
+
+  //
+  // Create Hoblist based upon boot information passed by privileged software
+  //
+  HobStart = CreateHobListFromBootInfo(&CpuDriverEntryPoint, PayloadBootInfo);
+
+  //
+  // Call the MM Core entry point
+  //
+  ProcessModuleEntryPointList (HobStart);
+
+  ASSERT_EFI_ERROR (CpuDriverEntryPoint);
+  DEBUG ((DEBUG_INFO, "Shared Cpu Driver EP 0x%lx\n", (UINT64) CpuDriverEntryPoint));
+
+finish:
+  InitMmFoundationSvcArgs.Arg0 = ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64;
+  InitMmFoundationSvcArgs.Arg1 = Status;
+  DelegatedEventLoop(&InitMmFoundationSvcArgs);
+  ASSERT_EFI_ERROR(0);
+
+}
diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
new file mode 100644
index 0000000000..9edc85d406
--- /dev/null
+++ b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
@@ -0,0 +1,53 @@
+## @file
+# Module entry point library for DXE core.
+#
+# Copyright (c) 2017, ARM Ltd. All rights reserved.<BR>
+#
+#  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.
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001A
+  BASE_NAME                      = StandaloneMmCoreEntryPoint
+  FILE_GUID                      = C97AC593-109A-4C63-905C-675FDE2689E8
+  MODULE_TYPE                    = MM_CORE_STANDALONE
+  VERSION_STRING                 = 1.0
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  LIBRARY_CLASS                  = StandaloneMmCoreEntryPoint|MM_CORE_STANDALONE
+
+#
+#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC (EBC is for build only)
+#
+
+[Sources.AARCH64]
+  Arm/StandaloneMmCoreEntryPoint.c
+  Arm/SetPermissions.c
+  Arm/CreateHobList.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  StandaloneMmPkg/StandaloneMmPkg.dec
+
+[Packages.AARCH64]
+  ArmPkg/ArmPkg.dec
+  ArmPlatformPkg/ArmPlatformPkg.dec
+
+[LibraryClasses]
+  ArmMmuLib
+  ArmSvcLib
+  BaseLib
+  DebugLib
+
+[Guids]
+  gMpInformationHobGuid
+  gEfiMmPeiMmramMemoryReserveGuid
+  gEfiStandaloneMmNonSecureBufferGuid
+  gEfiArmTfCpuDriverEpDescriptorGuid
diff --git a/StandaloneMmPkg b/StandaloneMmPkg~HEAD
similarity index 100%
rename from StandaloneMmPkg
rename to StandaloneMmPkg~HEAD
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 07/18] StandaloneMmPkg/FvLib: Add a common FV Library for management mode.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (5 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 06/18] StandaloneMmPkg: Add an AArch64 specific entry point library Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-16 14:44   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 08/18] StandaloneMmPkg/MemLib: AARCH64 Specific instance of memory check library Supreeth Venkatesh
                   ` (11 subsequent siblings)
  18 siblings, 1 reply; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

This patch implements a firmware volume library that can be used by the
Standalone management mode core module to parse the firmware volume.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 StandaloneMmPkg/Include/Library/FvLib.h | 109 ++++++++++
 StandaloneMmPkg/Library/FvLib/FvLib.c   | 366 ++++++++++++++++++++++++++++++++
 StandaloneMmPkg/Library/FvLib/FvLib.inf |  57 +++++
 3 files changed, 532 insertions(+)
 create mode 100644 StandaloneMmPkg/Include/Library/FvLib.h
 create mode 100644 StandaloneMmPkg/Library/FvLib/FvLib.c
 create mode 100644 StandaloneMmPkg/Library/FvLib/FvLib.inf

diff --git a/StandaloneMmPkg/Include/Library/FvLib.h b/StandaloneMmPkg/Include/Library/FvLib.h
new file mode 100644
index 0000000000..13e1ae2b01
--- /dev/null
+++ b/StandaloneMmPkg/Include/Library/FvLib.h
@@ -0,0 +1,109 @@
+/** @file
+
+Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+
+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
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef _FV_LIB_H_
+#define _FV_LIB_H_
+
+#include <Uefi.h>
+#include <Pi/PiFirmwareVolume.h>
+#include <Pi/PiFirmwareFile.h>
+
+/**
+  Given the input file pointer, search for the next matching file in the
+  FFS volume as defined by SearchType. The search starts from FileHeader inside
+  the Firmware Volume defined by FwVolHeader.
+
+  @param  SearchType  Filter to find only files of this type.
+                      Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
+  @param  FwVolHeader Pointer to the FV header of the volume to search.
+                      This parameter must point to a valid FFS volume.
+  @param  FileHeader  Pointer to the current file from which to begin searching.
+                      This pointer will be updated upon return to reflect the file found.
+
+  @retval EFI_NOT_FOUND  No files matching the search criteria were found
+  @retval EFI_SUCCESS
+**/
+EFI_STATUS
+EFIAPI
+FfsFindNextFile (
+  IN EFI_FV_FILETYPE             SearchType,
+  IN EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader,
+  IN OUT EFI_FFS_FILE_HEADER     **FileHeader
+  );
+
+/**
+  Given the input file pointer, search for the next matching section in the
+  FFS volume.
+
+  @param  SearchType    Filter to find only sections of this type.
+  @param  FfsFileHeader Pointer to the current file to search.
+  @param  SectionHeader Pointer to the Section matching SectionType in FfsFileHeader.
+                        NULL if section not found
+
+  @retval  EFI_NOT_FOUND  No files matching the search criteria were found
+  @retval  EFI_SUCCESS
+**/
+EFI_STATUS
+FfsFindSection (
+  IN EFI_SECTION_TYPE              SectionType,
+  IN EFI_FFS_FILE_HEADER           *FfsFileHeader,
+  IN OUT EFI_COMMON_SECTION_HEADER **SectionHeader
+  );
+
+/**
+  Locates a section within a series of sections
+  with the specified section type.
+
+  @param[in]   Sections        The sections to search
+  @param[in]   SizeOfSections  Total size of all sections
+  @param[in]   SectionType     The section type to locate
+  @param[out]  FoundSection    The FFS section if found
+
+  @retval EFI_SUCCESS           The file and section was found
+  @retval EFI_NOT_FOUND         The file and section was not found
+  @retval EFI_VOLUME_CORRUPTED  The firmware volume was corrupted
+**/
+EFI_STATUS
+EFIAPI
+FindFfsSectionInSections (
+  IN  VOID                             *Sections,
+  IN  UINTN                            SizeOfSections,
+  IN  EFI_SECTION_TYPE                 SectionType,
+  OUT EFI_COMMON_SECTION_HEADER        **FoundSection
+  );
+
+/**
+  Given the input file pointer, search for the next matching section in the
+  FFS volume.
+
+  @param  SearchType      Filter to find only sections of this type.
+  @param  FfsFileHeader   Pointer to the current file to search.
+  @param  SectionData     Pointer to the Section matching SectionType in FfsFileHeader.
+                          NULL if section not found
+  @param  SectionDataSize The size of SectionData
+
+  @retval  EFI_NOT_FOUND  No files matching the search criteria were found
+  @retval  EFI_SUCCESS
+**/
+EFI_STATUS
+EFIAPI
+FfsFindSectionData (
+  IN EFI_SECTION_TYPE              SectionType,
+  IN EFI_FFS_FILE_HEADER           *FfsFileHeader,
+  OUT VOID                         **SectionData,
+  OUT UINTN                        *SectionDataSize
+  );
+
+#endif
diff --git a/StandaloneMmPkg/Library/FvLib/FvLib.c b/StandaloneMmPkg/Library/FvLib/FvLib.c
new file mode 100644
index 0000000000..cacfc81338
--- /dev/null
+++ b/StandaloneMmPkg/Library/FvLib/FvLib.c
@@ -0,0 +1,366 @@
+/** @file
+
+Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+
+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
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <Library/FvLib.h>
+
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+
+#define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
+  (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))
+
+/**
+  Returns the highest bit set of the State field
+
+  @param ErasePolarity   Erase Polarity  as defined by EFI_FVB_ERASE_POLARITY
+                         in the Attributes field.
+  @param FfsHeader       Pointer to FFS File Header.
+
+  @return the highest bit in the State field
+**/
+EFI_FFS_FILE_STATE
+GetFileState (
+  IN UINT8                ErasePolarity,
+  IN EFI_FFS_FILE_HEADER  *FfsHeader
+  )
+{
+  EFI_FFS_FILE_STATE  FileState;
+  EFI_FFS_FILE_STATE  HighestBit;
+
+  FileState = FfsHeader->State;
+
+  if (ErasePolarity != 0) {
+    FileState = (EFI_FFS_FILE_STATE)~FileState;
+  }
+
+  HighestBit = 0x80;
+  while (HighestBit != 0 && (HighestBit & FileState) == 0) {
+    HighestBit >>= 1;
+  }
+
+  return HighestBit;
+}
+
+/**
+  Calculates the checksum of the header of a file.
+
+  @param FileHeader       Pointer to FFS File Header.
+
+  @return Checksum of the header.
+**/
+UINT8
+CalculateHeaderChecksum (
+  IN EFI_FFS_FILE_HEADER  *FileHeader
+  )
+{
+  UINT8 *ptr;
+  UINTN Index;
+  UINT8 Sum;
+
+  Sum = 0;
+  ptr = (UINT8 *) FileHeader;
+
+  for (Index = 0; Index < sizeof (EFI_FFS_FILE_HEADER) - 3; Index += 4) {
+    Sum = (UINT8) (Sum + ptr[Index]);
+    Sum = (UINT8) (Sum + ptr[Index + 1]);
+    Sum = (UINT8) (Sum + ptr[Index + 2]);
+    Sum = (UINT8) (Sum + ptr[Index + 3]);
+  }
+
+  for (; Index < sizeof (EFI_FFS_FILE_HEADER); Index++) {
+    Sum = (UINT8) (Sum + ptr[Index]);
+  }
+  //
+  // State field (since this indicates the different state of file).
+  //
+  Sum = (UINT8) (Sum - FileHeader->State);
+  //
+  // Checksum field of the file is not part of the header checksum.
+  //
+  Sum = (UINT8) (Sum - FileHeader->IntegrityCheck.Checksum.File);
+
+  return Sum;
+}
+
+/**
+  Given the input file pointer, search for the next matching file in the
+  FFS volume as defined by SearchType. The search starts from FileHeader inside
+  the Firmware Volume defined by FwVolHeader.
+
+  @param  SearchType  Filter to find only files of this type.
+                      Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
+  @param  FwVolHeader Pointer to the FV header of the volume to search.
+                      This parameter must point to a valid FFS volume.
+  @param  FileHeader  Pointer to the current file from which to begin searching.
+                      This pointer will be updated upon return to reflect the file found.
+
+  @retval EFI_NOT_FOUND  No files matching the search criteria were found
+  @retval EFI_SUCCESS
+**/
+EFI_STATUS
+EFIAPI
+FfsFindNextFile (
+  IN EFI_FV_FILETYPE             SearchType,
+  IN EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader,
+  IN OUT EFI_FFS_FILE_HEADER     **FileHeader
+  )
+{
+  EFI_FFS_FILE_HEADER *FfsFileHeader;
+  UINT32              FileLength;
+  UINT32              FileOccupiedSize;
+  UINT32              FileOffset;
+  UINT64              FvLength;
+  UINT8               ErasePolarity;
+  UINT8               FileState;
+
+  FvLength = FwVolHeader->FvLength;
+  if (FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {
+    ErasePolarity = 1;
+  } else {
+    ErasePolarity = 0;
+  }
+  //
+  // If FileHeader is not specified (NULL) start with the first file in the
+  // firmware volume.  Otherwise, start from the FileHeader.
+  //
+  if (*FileHeader == NULL) {
+    FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FwVolHeader + FwVolHeader->HeaderLength);
+  } else {
+    //
+    // Length is 24 bits wide so mask upper 8 bits
+    // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
+    //
+    FileLength        = FFS_FILE_SIZE(*FileHeader);
+    FileOccupiedSize  = GET_OCCUPIED_SIZE (FileLength, 8);
+    FfsFileHeader     = (EFI_FFS_FILE_HEADER *) ((UINT8 *) *FileHeader + FileOccupiedSize);
+  }
+
+  FileOffset = (UINT32) ((UINT8 *) FfsFileHeader - (UINT8 *) FwVolHeader);
+
+  while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {
+    //
+    // Get FileState which is the highest bit of the State
+    //
+    FileState = GetFileState (ErasePolarity, FfsFileHeader);
+
+    switch (FileState) {
+
+    case EFI_FILE_HEADER_INVALID:
+      FileOffset += sizeof (EFI_FFS_FILE_HEADER);
+      FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
+      break;
+
+    case EFI_FILE_DATA_VALID:
+    case EFI_FILE_MARKED_FOR_UPDATE:
+      if (CalculateHeaderChecksum (FfsFileHeader) == 0) {
+        FileLength        = FFS_FILE_SIZE(FfsFileHeader);
+        FileOccupiedSize  = GET_OCCUPIED_SIZE (FileLength, 8);
+
+        if ((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) {
+
+          *FileHeader = FfsFileHeader;
+
+          return EFI_SUCCESS;
+        }
+
+        FileOffset += FileOccupiedSize;
+        FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + FileOccupiedSize);
+      } else {
+        return EFI_NOT_FOUND;
+      }
+      break;
+
+    case EFI_FILE_DELETED:
+      FileLength        = FFS_FILE_SIZE(FfsFileHeader);
+      FileOccupiedSize  = GET_OCCUPIED_SIZE (FileLength, 8);
+      FileOffset += FileOccupiedSize;
+      FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + FileOccupiedSize);
+      break;
+
+    default:
+      return EFI_NOT_FOUND;
+
+    }
+  }
+
+  return EFI_NOT_FOUND;
+}
+
+/**
+  Locates a section within a series of sections
+  with the specified section type.
+
+  @param[in]   Sections        The sections to search
+  @param[in]   SizeOfSections  Total size of all sections
+  @param[in]   SectionType     The section type to locate
+  @param[out]  FoundSection    The FFS section if found
+
+  @retval EFI_SUCCESS           The file and section was found
+  @retval EFI_NOT_FOUND         The file and section was not found
+  @retval EFI_VOLUME_CORRUPTED  The firmware volume was corrupted
+**/
+EFI_STATUS
+EFIAPI
+FindFfsSectionInSections (
+  IN  VOID                             *Sections,
+  IN  UINTN                            SizeOfSections,
+  IN  EFI_SECTION_TYPE                 SectionType,
+  OUT EFI_COMMON_SECTION_HEADER        **FoundSection
+  )
+{
+  EFI_PHYSICAL_ADDRESS        CurrentAddress;
+  UINT32                      Size;
+  EFI_PHYSICAL_ADDRESS        EndOfSections;
+  EFI_COMMON_SECTION_HEADER   *Section;
+  EFI_PHYSICAL_ADDRESS        EndOfSection;
+
+  //
+  // Loop through the FFS file sections
+  //
+  EndOfSection = (EFI_PHYSICAL_ADDRESS)(UINTN) Sections;
+  EndOfSections = EndOfSection + SizeOfSections;
+  for (;;) {
+    if (EndOfSection == EndOfSections) {
+      break;
+    }
+    CurrentAddress = EndOfSection;
+
+    Section = (EFI_COMMON_SECTION_HEADER*)(UINTN) CurrentAddress;
+//    printf ("Section->Type: 0x%08x\n", Section->Type);
+
+    Size = SECTION_SIZE (Section);
+    if (Size < sizeof (*Section)) {
+      return EFI_VOLUME_CORRUPTED;
+    }
+
+    EndOfSection = CurrentAddress + Size;
+    if (EndOfSection > EndOfSections) {
+      return EFI_VOLUME_CORRUPTED;
+    }
+    Size = GET_OCCUPIED_SIZE (Size, 4);
+
+    //
+    // Look for the requested section type
+    //
+    if (Section->Type == SectionType) {
+      *FoundSection = Section;
+      return EFI_SUCCESS;
+    }
+  }
+
+  return EFI_NOT_FOUND;
+}
+
+/**
+  Given the input file pointer, search for the next matching section in the
+  FFS volume.
+
+  @param  SearchType    Filter to find only sections of this type.
+  @param  FfsFileHeader Pointer to the current file to search.
+  @param  SectionHeader Pointer to the Section matching SectionType in FfsFileHeader.
+                        NULL if section not found
+
+  @retval  EFI_NOT_FOUND  No files matching the search criteria were found
+  @retval  EFI_SUCCESS
+**/
+EFI_STATUS
+EFIAPI
+FfsFindSection (
+  IN EFI_SECTION_TYPE              SectionType,
+  IN EFI_FFS_FILE_HEADER           *FfsFileHeader,
+  IN OUT EFI_COMMON_SECTION_HEADER **SectionHeader
+  )
+{
+  UINT32                    FileSize;
+  EFI_COMMON_SECTION_HEADER *Section;
+  EFI_STATUS                Status;
+
+  //
+  // Size is 24 bits wide so mask upper 8 bits.
+  //    Does not include FfsFileHeader header size
+  // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
+  //
+  Section   = (EFI_COMMON_SECTION_HEADER *) (FfsFileHeader + 1);
+  FileSize  = FFS_FILE_SIZE(FfsFileHeader);
+  FileSize -= sizeof (EFI_FFS_FILE_HEADER);
+
+  Status = FindFfsSectionInSections (
+             Section,
+             FileSize,
+             SectionType,
+             SectionHeader
+             );
+  return Status;
+}
+
+/**
+  Given the input file pointer, search for the next matching section in the
+  FFS volume.
+
+  @param  SearchType      Filter to find only sections of this type.
+  @param  FfsFileHeader   Pointer to the current file to search.
+  @param  SectionData     Pointer to the Section matching SectionType in FfsFileHeader.
+                          NULL if section not found
+  @param  SectionDataSize The size of SectionData
+
+  @retval  EFI_NOT_FOUND  No files matching the search criteria were found
+  @retval  EFI_SUCCESS
+**/
+EFI_STATUS
+EFIAPI
+FfsFindSectionData (
+  IN EFI_SECTION_TYPE      SectionType,
+  IN EFI_FFS_FILE_HEADER   *FfsFileHeader,
+  IN OUT VOID              **SectionData,
+  IN OUT UINTN             *SectionDataSize
+  )
+{
+  UINT32                    FileSize;
+  EFI_COMMON_SECTION_HEADER *Section;
+  UINT32                    SectionLength;
+  UINT32                    ParsedLength;
+
+  //
+  // Size is 24 bits wide so mask upper 8 bits.
+  //    Does not include FfsFileHeader header size
+  // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
+  //
+  Section   = (EFI_COMMON_SECTION_HEADER *) (FfsFileHeader + 1);
+  FileSize  = FFS_FILE_SIZE(FfsFileHeader);
+  FileSize -= sizeof (EFI_FFS_FILE_HEADER);
+
+  *SectionData  = NULL;
+  ParsedLength  = 0;
+  while (ParsedLength < FileSize) {
+    if (Section->Type == SectionType) {
+      *SectionData = (VOID *) (Section + 1);
+      *SectionDataSize = SECTION_SIZE(Section);
+      return EFI_SUCCESS;
+    }
+    //
+    // Size is 24 bits wide so mask upper 8 bits.
+    // SectionLength is adjusted it is 4 byte aligned.
+    // Go to the next section
+    //
+    SectionLength = SECTION_SIZE(Section);
+    SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
+
+    ParsedLength += SectionLength;
+    Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + SectionLength);
+  }
+
+  return EFI_NOT_FOUND;
+}
diff --git a/StandaloneMmPkg/Library/FvLib/FvLib.inf b/StandaloneMmPkg/Library/FvLib/FvLib.inf
new file mode 100644
index 0000000000..6df50ab364
--- /dev/null
+++ b/StandaloneMmPkg/Library/FvLib/FvLib.inf
@@ -0,0 +1,57 @@
+## @file
+#
+#  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+#
+#  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
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+##
+
+################################################################################
+#
+# Defines Section - statements that will be processed to create a Makefile.
+#
+################################################################################
+[Defines]
+  INF_VERSION                    = 0x0001001A
+  BASE_NAME                      = FvLib
+  FILE_GUID                      = C20085E9-E3AB-4938-A727-C10935FEEE2B
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = FvLib
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+#  VALID_ARCHITECTURES           = IA32
+#
+
+################################################################################
+#
+# Sources Section - list of files that are required for the build to succeed.
+#
+################################################################################
+
+[Sources]
+  FvLib.c
+
+################################################################################
+#
+# Package Dependency Section - list of Package files that are required for
+#                              this module.
+#
+################################################################################
+
+[Packages]
+  MdePkg/MdePkg.dec
+  StandaloneMmPkg/StandaloneMmPkg.dec
+
+[LibraryClasses]
+  BaseLib
+  BaseMemoryLib
+  DebugLib
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 08/18] StandaloneMmPkg/MemLib: AARCH64 Specific instance of memory check library.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (6 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 07/18] StandaloneMmPkg/FvLib: Add a common FV Library for management mode Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-16 15:12   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 09/18] StandaloneMmPkg/MemoryAllocationLib: Add MM memory allocation library Supreeth Venkatesh
                   ` (10 subsequent siblings)
  18 siblings, 1 reply; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

MM memory check library library implementation. This library consumes
MM_ACCESS_PROTOCOL to get MMRAM information. In order to use this
library instance, the platform should produce all MMRAM range via
MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core
and MM driver) and/or specific dedicated hardware.

This patch provides services for MM Memory Operation.
The management mode Mem Library provides function for checking if buffer
is outside MMRAM and valid. It also provides functions for copy data
from MMRAM to non-MMRAM, from non-MMRAM to MMRAM,
from non-MMRAM to non-MMRAM, or set data in non-MMRAM.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 StandaloneMmPkg/Include/Library/MemLib.h    | 140 ++++++++++++++
 StandaloneMmPkg/Library/MemLib/Arm/MemLib.c | 276 ++++++++++++++++++++++++++++
 StandaloneMmPkg/Library/MemLib/MemLib.inf   |  47 +++++
 3 files changed, 463 insertions(+)
 create mode 100644 StandaloneMmPkg/Include/Library/MemLib.h
 create mode 100644 StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
 create mode 100644 StandaloneMmPkg/Library/MemLib/MemLib.inf

diff --git a/StandaloneMmPkg/Include/Library/MemLib.h b/StandaloneMmPkg/Include/Library/MemLib.h
new file mode 100644
index 0000000000..3264f10010
--- /dev/null
+++ b/StandaloneMmPkg/Include/Library/MemLib.h
@@ -0,0 +1,140 @@
+/** @file
+  Provides services for MM Memory Operation.
+
+  The MM Mem Library provides function for checking if buffer is outside MMRAM and valid.
+  It also provides functions for copy data from MMRAM to non-MMRAM, from non-MMRAM to MMRAM,
+  from non-MMRAM to non-MMRAM, or set data in non-MMRAM.
+
+  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef _MM_MEM_LIB_H_
+#define _MM_MEM_LIB_H_
+
+/**
+  This function check if the buffer is valid per processor architecture and not overlap with MMRAM.
+
+  @param Buffer  The buffer start address to be checked.
+  @param Length  The buffer length to be checked.
+
+  @retval TRUE  This buffer is valid per processor architecture and not overlap with MMRAM.
+  @retval FALSE This buffer is not valid per processor architecture or overlap with MMRAM.
+**/
+BOOLEAN
+EFIAPI
+MmIsBufferOutsideMmValid (
+  IN EFI_PHYSICAL_ADDRESS  Buffer,
+  IN UINT64                Length
+  );
+
+/**
+  Copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
+
+  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
+  It checks if source buffer is valid per processor architecture and not overlap with MMRAM.
+  If the check passes, it copies memory and returns EFI_SUCCESS.
+  If the check fails, it return EFI_SECURITY_VIOLATION.
+  The implementation must be reentrant.
+
+  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
+  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
+  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
+
+  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with MMRAM.
+  @retval EFI_SUCCESS            Memory is copied.
+
+**/
+EFI_STATUS
+EFIAPI
+MmCopyMemToSmram (
+  OUT VOID       *DestinationBuffer,
+  IN CONST VOID  *SourceBuffer,
+  IN UINTN       Length
+  );
+
+/**
+  Copies a source buffer (MMRAM) to a destination buffer (NON-MMRAM).
+
+  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
+  It checks if destination buffer is valid per processor architecture and not overlap with MMRAM.
+  If the check passes, it copies memory and returns EFI_SUCCESS.
+  If the check fails, it returns EFI_SECURITY_VIOLATION.
+  The implementation must be reentrant.
+
+  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
+  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
+  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
+
+  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.
+  @retval EFI_SUCCESS            Memory is copied.
+
+**/
+EFI_STATUS
+EFIAPI
+MmCopyMemFromSmram (
+  OUT VOID       *DestinationBuffer,
+  IN CONST VOID  *SourceBuffer,
+  IN UINTN       Length
+  );
+
+/**
+  Copies a source buffer (NON-MMRAM) to a destination buffer (NON-MMRAM).
+
+  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
+  It checks if source buffer and destination buffer are valid per processor architecture and not overlap with MMRAM.
+  If the check passes, it copies memory and returns EFI_SUCCESS.
+  If the check fails, it returns EFI_SECURITY_VIOLATION.
+  The implementation must be reentrant, and it must handle the case where source buffer overlaps destination buffer.
+
+  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
+  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
+  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
+
+  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.
+  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with MMRAM.
+  @retval EFI_SUCCESS            Memory is copied.
+
+**/
+EFI_STATUS
+EFIAPI
+MmCopyMem (
+  OUT VOID       *DestinationBuffer,
+  IN CONST VOID  *SourceBuffer,
+  IN UINTN       Length
+  );
+
+/**
+  Fills a target buffer (NON-MMRAM) with a byte value.
+
+  This function fills a target buffer (non-MMRAM) with a byte value.
+  It checks if target buffer is valid per processor architecture and not overlap with MMRAM.
+  If the check passes, it fills memory and returns EFI_SUCCESS.
+  If the check fails, it returns EFI_SECURITY_VIOLATION.
+
+  @param  Buffer    The memory to set.
+  @param  Length    The number of bytes to set.
+  @param  Value     The value with which to fill Length bytes of Buffer.
+
+  @retval EFI_SECURITY_VIOLATION The Buffer is invalid per processor architecture or overlap with MMRAM.
+  @retval EFI_SUCCESS            Memory is set.
+
+**/
+EFI_STATUS
+EFIAPI
+MmSetMem (
+  OUT VOID  *Buffer,
+  IN UINTN  Length,
+  IN UINT8  Value
+  );
+
+#endif
diff --git a/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c b/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
new file mode 100644
index 0000000000..432a45698b
--- /dev/null
+++ b/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
@@ -0,0 +1,276 @@
+/** @file
+  Instance of MM memory check library.
+
+  MM memory check library library implementation. This library consumes MM_ACCESS_PROTOCOL
+  to get MMRAM information. In order to use this library instance, the platform should produce
+  all MMRAM range via MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core
+  and MM driver) and/or specific dedicated hardware.
+
+  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+
+#include <PiMm.h>
+
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+
+EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges;
+UINTN                mMmMemLibInternalMmramCount;
+
+//
+// Maximum support address used to check input buffer
+//
+EFI_PHYSICAL_ADDRESS  mMmMemLibInternalMaximumSupportAddress = 0;
+
+/**
+  Calculate and save the maximum support address.
+
+**/
+VOID
+MmMemLibInternalCalculateMaximumSupportAddress (
+  VOID
+  )
+{
+  UINT8        PhysicalAddressBits;
+
+  PhysicalAddressBits = 36;
+
+  //
+  // Save the maximum support address in one global variable
+  //
+  mMmMemLibInternalMaximumSupportAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, PhysicalAddressBits) - 1);
+  DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress = 0x%lx\n", mMmMemLibInternalMaximumSupportAddress));
+}
+
+/**
+  This function check if the buffer is valid per processor architecture and not overlap with MMRAM.
+
+  @param Buffer  The buffer start address to be checked.
+  @param Length  The buffer length to be checked.
+
+  @retval TRUE  This buffer is valid per processor architecture and not overlap with MMRAM.
+  @retval FALSE This buffer is not valid per processor architecture or overlap with MMRAM.
+**/
+BOOLEAN
+EFIAPI
+MmIsBufferOutsideMmValid (
+  IN EFI_PHYSICAL_ADDRESS  Buffer,
+  IN UINT64                Length
+  )
+{
+  UINTN  Index;
+
+  //
+  // Check override.
+  // NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1) is valid.
+  //
+  if ((Length > mMmMemLibInternalMaximumSupportAddress) ||
+      (Buffer > mMmMemLibInternalMaximumSupportAddress) ||
+      ((Length != 0) && (Buffer > (mMmMemLibInternalMaximumSupportAddress - (Length - 1)))) ) {
+    //
+    // Overflow happen
+    //
+    DEBUG ((
+      DEBUG_ERROR,
+      "MmIsBufferOutsideMmValid: Overflow: Buffer (0x%lx) - Length (0x%lx), MaximumSupportAddress (0x%lx)\n",
+      Buffer,
+      Length,
+      mMmMemLibInternalMaximumSupportAddress
+      ));
+    return FALSE;
+  }
+
+  for (Index = 0; Index < mMmMemLibInternalMmramCount; Index ++) {
+    if (((Buffer >= mMmMemLibInternalMmramRanges[Index].CpuStart) && (Buffer < mMmMemLibInternalMmramRanges[Index].CpuStart + mMmMemLibInternalMmramRanges[Index].PhysicalSize)) ||
+        ((mMmMemLibInternalMmramRanges[Index].CpuStart >= Buffer) && (mMmMemLibInternalMmramRanges[Index].CpuStart < Buffer + Length))) {
+      DEBUG ((
+        DEBUG_ERROR,
+        "MmIsBufferOutsideMmValid: Overlap: Buffer (0x%lx) - Length (0x%lx), ",
+        Buffer,
+        Length
+        ));
+      DEBUG ((
+        DEBUG_ERROR,
+        "CpuStart (0x%lx) - PhysicalSize (0x%lx)\n",
+        mMmMemLibInternalMmramRanges[Index].CpuStart,
+        mMmMemLibInternalMmramRanges[Index].PhysicalSize
+        ));
+      return FALSE;
+    }
+  }
+
+  return TRUE;
+}
+
+/**
+  Copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
+
+  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
+  It checks if source buffer is valid per processor architecture and not overlap with MMRAM.
+  If the check passes, it copies memory and returns EFI_SUCCESS.
+  If the check fails, it return EFI_SECURITY_VIOLATION.
+  The implementation must be reentrant.
+
+  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
+  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
+  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
+
+  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with MMRAM.
+  @retval EFI_SUCCESS            Memory is copied.
+
+**/
+EFI_STATUS
+EFIAPI
+MmCopyMemToMmram (
+  OUT VOID       *DestinationBuffer,
+  IN CONST VOID  *SourceBuffer,
+  IN UINTN       Length
+  )
+{
+  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {
+    DEBUG ((DEBUG_ERROR, "MmCopyMemToMmram: Security Violation: Source (0x%x), Length (0x%x)\n", SourceBuffer, Length));
+    return EFI_SECURITY_VIOLATION;
+  }
+  CopyMem (DestinationBuffer, SourceBuffer, Length);
+  return EFI_SUCCESS;
+}
+
+/**
+  Copies a source buffer (MMRAM) to a destination buffer (NON-MMRAM).
+
+  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
+  It checks if destination buffer is valid per processor architecture and not overlap with MMRAM.
+  If the check passes, it copies memory and returns EFI_SUCCESS.
+  If the check fails, it returns EFI_SECURITY_VIOLATION.
+  The implementation must be reentrant.
+
+  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
+  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
+  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
+
+  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.
+  @retval EFI_SUCCESS            Memory is copied.
+
+**/
+EFI_STATUS
+EFIAPI
+MmCopyMemFromMmram (
+  OUT VOID       *DestinationBuffer,
+  IN CONST VOID  *SourceBuffer,
+  IN UINTN       Length
+  )
+{
+  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {
+    DEBUG ((DEBUG_ERROR, "MmCopyMemFromMmram: Security Violation: Destination (0x%x), Length (0x%x)\n", DestinationBuffer, Length));
+    return EFI_SECURITY_VIOLATION;
+  }
+  CopyMem (DestinationBuffer, SourceBuffer, Length);
+  return EFI_SUCCESS;
+}
+
+/**
+  Copies a source buffer (NON-MMRAM) to a destination buffer (NON-MMRAM).
+
+  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
+  It checks if source buffer and destination buffer are valid per processor architecture and not overlap with MMRAM.
+  If the check passes, it copies memory and returns EFI_SUCCESS.
+  If the check fails, it returns EFI_SECURITY_VIOLATION.
+  The implementation must be reentrant, and it must handle the case where source buffer overlaps destination buffer.
+
+  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
+  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
+  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
+
+  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.
+  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with MMRAM.
+  @retval EFI_SUCCESS            Memory is copied.
+
+**/
+EFI_STATUS
+EFIAPI
+MmCopyMem (
+  OUT VOID       *DestinationBuffer,
+  IN CONST VOID  *SourceBuffer,
+  IN UINTN       Length
+  )
+{
+  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {
+    DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation: Destination (0x%x), Length (0x%x)\n", DestinationBuffer, Length));
+    return EFI_SECURITY_VIOLATION;
+  }
+  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {
+    DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation: Source (0x%x), Length (0x%x)\n", SourceBuffer, Length));
+    return EFI_SECURITY_VIOLATION;
+  }
+  CopyMem (DestinationBuffer, SourceBuffer, Length);
+  return EFI_SUCCESS;
+}
+
+/**
+  Fills a target buffer (NON-MMRAM) with a byte value.
+
+  This function fills a target buffer (non-MMRAM) with a byte value.
+  It checks if target buffer is valid per processor architecture and not overlap with MMRAM.
+  If the check passes, it fills memory and returns EFI_SUCCESS.
+  If the check fails, it returns EFI_SECURITY_VIOLATION.
+
+  @param  Buffer    The memory to set.
+  @param  Length    The number of bytes to set.
+  @param  Value     The value with which to fill Length bytes of Buffer.
+
+  @retval EFI_SECURITY_VIOLATION The Buffer is invalid per processor architecture or overlap with MMRAM.
+  @retval EFI_SUCCESS            Memory is set.
+
+**/
+EFI_STATUS
+EFIAPI
+MmSetMem (
+  OUT VOID  *Buffer,
+  IN UINTN  Length,
+  IN UINT8  Value
+  )
+{
+  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Length)) {
+    DEBUG ((DEBUG_ERROR, "MmSetMem: Security Violation: Source (0x%x), Length (0x%x)\n", Buffer, Length));
+    return EFI_SECURITY_VIOLATION;
+  }
+  SetMem (Buffer, Length, Value);
+  return EFI_SUCCESS;
+}
+
+/**
+  The constructor function initializes the Mm Mem library
+
+  @param  ImageHandle   The firmware allocated handle for the EFI image.
+  @param  SystemTable   A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
+
+**/
+EFI_STATUS
+EFIAPI
+MemLibConstructor (
+  IN EFI_HANDLE             ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
+  )
+{
+
+  //
+  // Calculate and save maximum support address
+  //
+  MmMemLibInternalCalculateMaximumSupportAddress ();
+
+  return EFI_SUCCESS;
+}
diff --git a/StandaloneMmPkg/Library/MemLib/MemLib.inf b/StandaloneMmPkg/Library/MemLib/MemLib.inf
new file mode 100644
index 0000000000..52b7c06397
--- /dev/null
+++ b/StandaloneMmPkg/Library/MemLib/MemLib.inf
@@ -0,0 +1,47 @@
+## @file
+#  Instance of MM memory check library.
+#
+#  MM memory check library library implementation. This library consumes MM_ACCESS_PROTOCOL
+#  to get MMRAM information. In order to use this library instance, the platform should produce
+#  all MMRAM range via MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core
+#  and MM driver) and/or specific dedicated hardware.
+#
+#  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+#
+#  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
+#
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001A
+  BASE_NAME                      = MemLib
+  FILE_GUID                      = EA355F14-6409-4716-829F-37B3BC7C7F26
+  MODULE_TYPE                    = MM_STANDALONE
+  VERSION_STRING                 = 1.0
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  LIBRARY_CLASS                  = MemLib|MM_STANDALONE MM_CORE_STANDALONE
+  CONSTRUCTOR                    = MemLibConstructor
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+#  VALID_ARCHITECTURES           = AARCH64
+#
+
+[Sources.AARCH64]
+  Arm/MemLib.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  StandaloneMmPkg/StandaloneMmPkg.dec
+
+[LibraryClasses]
+  BaseMemoryLib
+  DebugLib
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 09/18] StandaloneMmPkg/MemoryAllocationLib: Add MM memory allocation library.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (7 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 08/18] StandaloneMmPkg/MemLib: AARCH64 Specific instance of memory check library Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-25 14:33   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 10/18] StandaloneMmPkg/HobLib: Add AARCH64 Specific HOB Library for management mode Supreeth Venkatesh
                   ` (9 subsequent siblings)
  18 siblings, 1 reply; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

This patch implements management mode memory allocation services.The
implementation borrows the MM Core Memory Allocation services as the
primitive for memory allocation instead of using MM System Table
services.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 StandaloneMmPkg/Include/Guid/MmCoreData.h          | 132 +++
 StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h  |  62 ++
 .../MemoryAllocationLib/MemoryAllocationLib.c      | 907 +++++++++++++++++++++
 .../MemoryAllocationLib/MemoryAllocationLib.inf    |  49 ++
 .../MemoryAllocationLib/MemoryAllocationServices.h |  38 +
 5 files changed, 1188 insertions(+)
 create mode 100644 StandaloneMmPkg/Include/Guid/MmCoreData.h
 create mode 100644 StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
 create mode 100644 StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
 create mode 100644 StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
 create mode 100644 StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.h

diff --git a/StandaloneMmPkg/Include/Guid/MmCoreData.h b/StandaloneMmPkg/Include/Guid/MmCoreData.h
new file mode 100644
index 0000000000..c0ac772014
--- /dev/null
+++ b/StandaloneMmPkg/Include/Guid/MmCoreData.h
@@ -0,0 +1,132 @@
+/** @file
+  MM Core data.
+
+Copyright (c) 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 that accompanies this distribution.
+The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php.
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef __MM_CORE_DATA_H__
+#define __MM_CORE_DATA_H__
+
+#define MM_CORE_DATA_HOB_GUID \
+  { 0xa160bf99, 0x2aa4, 0x4d7d, { 0x99, 0x93, 0x89, 0x9c, 0xb1, 0x2d, 0xf3, 0x76 }}
+
+extern EFI_GUID gMmCoreDataHobGuid;
+
+typedef struct {
+  //
+  // Address pointer to MM_CORE_PRIVATE_DATA
+  //
+  EFI_PHYSICAL_ADDRESS   Address;
+} MM_CORE_DATA_HOB_DATA;
+
+
+///
+/// Define values for the communications buffer used when gEfiEventDxeDispatchGuid is
+/// event signaled.  This event is signaled by the DXE Core each time the DXE Core
+/// dispatcher has completed its work.  When this event is signaled, the MM Core
+/// if notified, so the MM Core can dispatch MM drivers.  If COMM_BUFFER_MM_DISPATCH_ERROR
+/// is returned in the communication buffer, then an error occurred dispatching MM
+/// Drivers.  If COMM_BUFFER_MM_DISPATCH_SUCCESS is returned, then the MM Core
+/// dispatched all the drivers it could.  If COMM_BUFFER_MM_DISPATCH_RESTART is
+/// returned, then the MM Core just dispatched the MM Driver that registered
+/// the MM Entry Point enabling the use of MM Mode.  In this case, the MM Core
+/// should be notified again to dispatch more MM Drivers using MM Mode.
+///
+#define COMM_BUFFER_MM_DISPATCH_ERROR    0x00
+#define COMM_BUFFER_MM_DISPATCH_SUCCESS  0x01
+#define COMM_BUFFER_MM_DISPATCH_RESTART  0x02
+
+///
+/// Signature for the private structure shared between the MM IPL and the MM Core
+///
+#define MM_CORE_PRIVATE_DATA_SIGNATURE  SIGNATURE_32 ('m', 'm', 'i', 'c')
+
+///
+/// Private structure that is used to share information between the MM IPL and
+/// the MM Core.  This structure is allocated from memory of type EfiRuntimeServicesData.
+/// Since runtime memory types are converted to available memory when a legacy boot
+/// is performed, the MM Core must not access any fields of this structure if a legacy
+/// boot is performed.  As a result, the MM IPL must create an event notification
+/// for the Legacy Boot event and notify the MM Core that a legacy boot is being
+/// performed.  The MM Core can then use this information to filter accesses to
+/// thos structure.
+///
+typedef struct {
+  UINT64                          Signature;
+
+  ///
+  /// The number of MMRAM ranges passed from the MM IPL to the MM Core.  The MM
+  /// Core uses these ranges of MMRAM to initialize the MM Core memory manager.
+  ///
+  UINT64                          MmramRangeCount;
+
+  ///
+  /// A table of MMRAM ranges passed from the MM IPL to the MM Core.  The MM
+  /// Core uses these ranges of MMRAM to initialize the MM Core memory manager.
+  ///
+  EFI_PHYSICAL_ADDRESS            MmramRanges;
+
+  ///
+  /// The MM Foundation Entry Point.  The MM Core fills in this field when the
+  /// MM Core is initialized.  The MM IPL is responsbile for registering this entry
+  /// point with the MM Configuration Protocol.  The MM Configuration Protocol may
+  /// not be available at the time the MM IPL and MM Core are started, so the MM IPL
+  /// sets up a protocol notification on the MM Configuration Protocol and registers
+  /// the MM Foundation Entry Point as soon as the MM Configuration Protocol is
+  /// available.
+  ///
+  EFI_PHYSICAL_ADDRESS            MmEntryPoint;
+
+  ///
+  /// Boolean flag set to TRUE while an MMI is being processed by the MM Core.
+  ///
+  BOOLEAN                         MmEntryPointRegistered;
+
+  ///
+  /// Boolean flag set to TRUE while an MMI is being processed by the MM Core.
+  ///
+  BOOLEAN                         InMm;
+
+  ///
+  /// This field is set by the MM Core then the MM Core is initialized.  This field is
+  /// used by the MM Base 2 Protocol and MM Communication Protocol implementations in
+  /// the MM IPL.
+  ///
+  EFI_PHYSICAL_ADDRESS            Mmst;
+
+  ///
+  /// This field is used by the MM Communicatioon Protocol to pass a buffer into
+  /// a software MMI handler and for the software MMI handler to pass a buffer back to
+  /// the caller of the MM Communication Protocol.
+  ///
+  EFI_PHYSICAL_ADDRESS            CommunicationBuffer;
+
+  ///
+  /// This field is used by the MM Communicatioon Protocol to pass the size of a buffer,
+  /// in bytes, into a software MMI handler and for the software MMI handler to pass the
+  /// size, in bytes, of a buffer back to the caller of the MM Communication Protocol.
+  ///
+  UINT64                          BufferSize;
+
+  ///
+  /// This field is used by the MM Communication Protocol to pass the return status from
+  /// a software MMI handler back to the caller of the MM Communication Protocol.
+  ///
+  UINT64                          ReturnStatus;
+
+  EFI_PHYSICAL_ADDRESS            MmCoreImageBase;
+  UINT64                          MmCoreImageSize;
+  EFI_PHYSICAL_ADDRESS            MmCoreEntryPoint;
+
+  EFI_PHYSICAL_ADDRESS            StandaloneBfvAddress;
+} MM_CORE_PRIVATE_DATA;
+
+#endif
diff --git a/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h b/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
new file mode 100644
index 0000000000..c4104b755d
--- /dev/null
+++ b/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
@@ -0,0 +1,62 @@
+/** @file
+  Definition of GUIDed HOB for reserving MMRAM regions.
+
+  This file defines:
+  * the GUID used to identify the GUID HOB for reserving MMRAM regions.
+  * the data structure of MMRAM descriptor to describe MMRAM candidate regions
+  * values of state of MMRAM candidate regions
+  * the GUID specific data structure of HOB for reserving MMRAM regions.
+  This GUIDed HOB can be used to convey the existence of the T-SEG reservation and H-SEG usage
+
+Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+
+This program and the accompanying materials are licensed and made available under
+the terms and conditions of the BSD License that accompanies this distribution.
+The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php.
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+  @par Revision Reference:
+  GUIDs defined in MmCis spec version 0.9.
+
+**/
+
+#ifndef _EFI_MM_PEI_MMRAM_MEMORY_RESERVE_H_
+#define _EFI_MM_PEI_MMRAM_MEMORY_RESERVE_H_
+
+#define EFI_MM_PEI_MMRAM_MEMORY_RESERVE \
+  { \
+    0x0703f912, 0xbf8d, 0x4e2a, {0xbe, 0x07, 0xab, 0x27, 0x25, 0x25, 0xc5, 0x92 } \
+  }
+
+/**
+* GUID specific data structure of HOB for reserving MMRAM regions.
+*
+* Inconsistent with specification here:
+* EFI_HOB_MMRAM_DESCRIPTOR_BLOCK has been changed to EFI_MMRAM_HOB_DESCRIPTOR_BLOCK.
+* This inconsistency is kept in code in order for backward compatibility.
+**/
+typedef struct {
+  ///
+  /// Designates the number of possible regions in the system
+  /// that can be usable for MMRAM.
+  ///
+  /// Inconsistent with specification here:
+  /// In Framework MM CIS 0.91 specification, it defines the field type as UINTN.
+  /// However, HOBs are supposed to be CPU neutral, so UINT32 should be used instead.
+  ///
+  UINT32                NumberOfMmReservedRegions;
+  ///
+  /// Used throughout this protocol to describe the candidate
+  /// regions for MMRAM that are supported by this platform.
+  ///
+  EFI_MMRAM_DESCRIPTOR  Descriptor[1];
+} EFI_MMRAM_HOB_DESCRIPTOR_BLOCK;
+
+extern EFI_GUID gEfiMmPeiSmramMemoryReserveGuid;
+
+#endif
+
diff --git a/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
new file mode 100644
index 0000000000..c177a8f538
--- /dev/null
+++ b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
@@ -0,0 +1,907 @@
+/** @file
+  Support routines for memory allocation routines based on Standalone MM Core internal functions.
+
+  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <PiMm.h>
+
+#include <Guid/MmramMemoryReserve.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/HobLib.h>
+#include "MemoryAllocationServices.h"
+
+EFI_MM_SYSTEM_TABLE   *gMmst = NULL;
+
+/**
+  Allocates one or more 4KB pages of a certain memory type.
+
+  Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
+  buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0, then NULL is returned.
+  If there is not enough memory remaining to satisfy the request, then NULL is returned.
+
+  @param  MemoryType            The type of memory to allocate.
+  @param  Pages                 The number of 4 KB pages to allocate.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+InternalAllocatePages (
+  IN EFI_MEMORY_TYPE  MemoryType,
+  IN UINTN            Pages
+  )
+{
+  EFI_STATUS            Status;
+  EFI_PHYSICAL_ADDRESS  Memory;
+
+  if (Pages == 0) {
+    return NULL;
+  }
+
+  Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);
+  if (EFI_ERROR (Status)) {
+    return NULL;
+  }
+  return (VOID *) (UINTN) Memory;
+}
+
+/**
+  Allocates one or more 4KB pages of type EfiBootServicesData.
+
+  Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
+  allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0, then NULL
+  is returned.  If there is not enough memory remaining to satisfy the request, then NULL is
+  returned.
+
+  @param  Pages                 The number of 4 KB pages to allocate.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+AllocatePages (
+  IN UINTN  Pages
+  )
+{
+  return InternalAllocatePages (EfiRuntimeServicesData, Pages);
+}
+
+/**
+  Allocates one or more 4KB pages of type EfiRuntimeServicesData.
+
+  Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
+  allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0, then NULL
+  is returned.  If there is not enough memory remaining to satisfy the request, then NULL is
+  returned.
+
+  @param  Pages                 The number of 4 KB pages to allocate.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+AllocateRuntimePages (
+  IN UINTN  Pages
+  )
+{
+  return InternalAllocatePages (EfiRuntimeServicesData, Pages);
+}
+
+/**
+  Allocates one or more 4KB pages of type EfiReservedMemoryType.
+
+  Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
+  allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0, then NULL
+  is returned.  If there is not enough memory remaining to satisfy the request, then NULL is
+  returned.
+
+  @param  Pages                 The number of 4 KB pages to allocate.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+AllocateReservedPages (
+  IN UINTN  Pages
+  )
+{
+  return NULL;
+}
+
+/**
+  Frees one or more 4KB pages that were previously allocated with one of the page allocation
+  functions in the Memory Allocation Library.
+
+  Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer.  Buffer
+  must have been allocated on a previous call to the page allocation services of the Memory
+  Allocation Library.  If it is not possible to free allocated pages, then this function will
+  perform no actions.
+
+  If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
+  then ASSERT().
+  If Pages is zero, then ASSERT().
+
+  @param  Buffer                Pointer to the buffer of pages to free.
+  @param  Pages                 The number of 4 KB pages to free.
+
+**/
+VOID
+EFIAPI
+FreePages (
+  IN VOID   *Buffer,
+  IN UINTN  Pages
+  )
+{
+  EFI_STATUS  Status;
+
+  ASSERT (Pages != 0);
+  Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);
+  ASSERT_EFI_ERROR (Status);
+}
+
+/**
+  Allocates one or more 4KB pages of a certain memory type at a specified alignment.
+
+  Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
+  specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is returned.
+  If there is not enough memory at the specified alignment remaining to satisfy the request, then
+  NULL is returned.
+  If Alignment is not a power of two and Alignment is not zero, then ASSERT().
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
+
+  @param  MemoryType            The type of memory to allocate.
+  @param  Pages                 The number of 4 KB pages to allocate.
+  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.
+                                If Alignment is zero, then byte alignment is used.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+InternalAllocateAlignedPages (
+  IN EFI_MEMORY_TYPE  MemoryType,
+  IN UINTN            Pages,
+  IN UINTN            Alignment
+  )
+{
+  EFI_STATUS            Status;
+  EFI_PHYSICAL_ADDRESS  Memory;
+  UINTN                 AlignedMemory;
+  UINTN                 AlignmentMask;
+  UINTN                 UnalignedPages;
+  UINTN                 RealPages;
+
+  //
+  // Alignment must be a power of two or zero.
+  //
+  ASSERT ((Alignment & (Alignment - 1)) == 0);
+
+  if (Pages == 0) {
+    return NULL;
+  }
+  if (Alignment > EFI_PAGE_SIZE) {
+    //
+    // Calculate the total number of pages since alignment is larger than page size.
+    //
+    AlignmentMask  = Alignment - 1;
+    RealPages      = Pages + EFI_SIZE_TO_PAGES (Alignment);
+    //
+    // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
+    //
+    ASSERT (RealPages > Pages);
+
+    Status         = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);
+    if (EFI_ERROR (Status)) {
+      return NULL;
+    }
+    AlignedMemory  = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;
+    UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);
+    if (UnalignedPages > 0) {
+      //
+      // Free first unaligned page(s).
+      //
+      Status = gMmst->MmFreePages (Memory, UnalignedPages);
+      ASSERT_EFI_ERROR (Status);
+    }
+    Memory         = (EFI_PHYSICAL_ADDRESS) (AlignedMemory + EFI_PAGES_TO_SIZE (Pages));
+    UnalignedPages = RealPages - Pages - UnalignedPages;
+    if (UnalignedPages > 0) {
+      //
+      // Free last unaligned page(s).
+      //
+      Status = gMmst->MmFreePages (Memory, UnalignedPages);
+      ASSERT_EFI_ERROR (Status);
+    }
+  } else {
+    //
+    // Do not over-allocate pages in this case.
+    //
+    Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);
+    if (EFI_ERROR (Status)) {
+      return NULL;
+    }
+    AlignedMemory  = (UINTN) Memory;
+  }
+  return (VOID *) AlignedMemory;
+}
+
+/**
+  Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
+
+  Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
+  alignment specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is
+  returned.  If there is not enough memory at the specified alignment remaining to satisfy the
+  request, then NULL is returned.
+
+  If Alignment is not a power of two and Alignment is not zero, then ASSERT().
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
+
+  @param  Pages                 The number of 4 KB pages to allocate.
+  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.
+                                If Alignment is zero, then byte alignment is used.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+AllocateAlignedPages (
+  IN UINTN  Pages,
+  IN UINTN  Alignment
+  )
+{
+  return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);
+}
+
+/**
+  Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
+
+  Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
+  alignment specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is
+  returned.  If there is not enough memory at the specified alignment remaining to satisfy the
+  request, then NULL is returned.
+
+  If Alignment is not a power of two and Alignment is not zero, then ASSERT().
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
+
+  @param  Pages                 The number of 4 KB pages to allocate.
+  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.
+                                If Alignment is zero, then byte alignment is used.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+AllocateAlignedRuntimePages (
+  IN UINTN  Pages,
+  IN UINTN  Alignment
+  )
+{
+  return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);
+}
+
+/**
+  Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
+
+  Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
+  alignment specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is
+  returned.  If there is not enough memory at the specified alignment remaining to satisfy the
+  request, then NULL is returned.
+
+  If Alignment is not a power of two and Alignment is not zero, then ASSERT().
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
+
+  @param  Pages                 The number of 4 KB pages to allocate.
+  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.
+                                If Alignment is zero, then byte alignment is used.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+AllocateAlignedReservedPages (
+  IN UINTN  Pages,
+  IN UINTN  Alignment
+  )
+{
+  return NULL;
+}
+
+/**
+  Frees one or more 4KB pages that were previously allocated with one of the aligned page
+  allocation functions in the Memory Allocation Library.
+
+  Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer.  Buffer
+  must have been allocated on a previous call to the aligned page allocation services of the Memory
+  Allocation Library.  If it is not possible to free allocated pages, then this function will
+  perform no actions.
+
+  If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
+  Library, then ASSERT().
+  If Pages is zero, then ASSERT().
+
+  @param  Buffer                Pointer to the buffer of pages to free.
+  @param  Pages                 The number of 4 KB pages to free.
+
+**/
+VOID
+EFIAPI
+FreeAlignedPages (
+  IN VOID   *Buffer,
+  IN UINTN  Pages
+  )
+{
+  EFI_STATUS  Status;
+
+  ASSERT (Pages != 0);
+  Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);
+  ASSERT_EFI_ERROR (Status);
+}
+
+/**
+  Allocates a buffer of a certain pool type.
+
+  Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
+  pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
+  returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.
+
+  @param  MemoryType            The type of memory to allocate.
+  @param  AllocationSize        The number of bytes to allocate.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+InternalAllocatePool (
+  IN EFI_MEMORY_TYPE  MemoryType,
+  IN UINTN            AllocationSize
+  )
+{
+  EFI_STATUS  Status;
+  VOID        *Memory;
+
+  Memory = NULL;
+
+  Status = gMmst->MmAllocatePool (MemoryType, AllocationSize, &Memory);
+  if (EFI_ERROR (Status)) {
+    Memory = NULL;
+  }
+  return Memory;
+}
+
+/**
+  Allocates a buffer of type EfiBootServicesData.
+
+  Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
+  pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
+  returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.
+
+  @param  AllocationSize        The number of bytes to allocate.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+AllocatePool (
+  IN UINTN  AllocationSize
+  )
+{
+  return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);
+}
+
+/**
+  Allocates a buffer of type EfiRuntimeServicesData.
+
+  Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
+  a pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
+  returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.
+
+  @param  AllocationSize        The number of bytes to allocate.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+AllocateRuntimePool (
+  IN UINTN  AllocationSize
+  )
+{
+  return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);
+}
+
+/**
+  Allocates a buffer of type EfiReservedMemoryType.
+
+  Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns
+  a pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
+  returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.
+
+  @param  AllocationSize        The number of bytes to allocate.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+AllocateReservedPool (
+  IN UINTN  AllocationSize
+  )
+{
+  return NULL;
+}
+
+/**
+  Allocates and zeros a buffer of a certain pool type.
+
+  Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer
+  with zeros, and returns a pointer to the allocated buffer.  If AllocationSize is 0, then a valid
+  buffer of 0 size is returned.  If there is not enough memory remaining to satisfy the request,
+  then NULL is returned.
+
+  @param  PoolType              The type of memory to allocate.
+  @param  AllocationSize        The number of bytes to allocate and zero.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+InternalAllocateZeroPool (
+  IN EFI_MEMORY_TYPE  PoolType,
+  IN UINTN            AllocationSize
+  )
+{
+  VOID  *Memory;
+
+  Memory = InternalAllocatePool (PoolType, AllocationSize);
+  if (Memory != NULL) {
+    Memory = ZeroMem (Memory, AllocationSize);
+  }
+  return Memory;
+}
+
+/**
+  Allocates and zeros a buffer of type EfiBootServicesData.
+
+  Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
+  buffer with zeros, and returns a pointer to the allocated buffer.  If AllocationSize is 0, then a
+  valid buffer of 0 size is returned.  If there is not enough memory remaining to satisfy the
+  request, then NULL is returned.
+
+  @param  AllocationSize        The number of bytes to allocate and zero.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+AllocateZeroPool (
+  IN UINTN  AllocationSize
+  )
+{
+  return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);
+}
+
+/**
+  Allocates and zeros a buffer of type EfiRuntimeServicesData.
+
+  Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
+  buffer with zeros, and returns a pointer to the allocated buffer.  If AllocationSize is 0, then a
+  valid buffer of 0 size is returned.  If there is not enough memory remaining to satisfy the
+  request, then NULL is returned.
+
+  @param  AllocationSize        The number of bytes to allocate and zero.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+AllocateRuntimeZeroPool (
+  IN UINTN  AllocationSize
+  )
+{
+  return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);
+}
+
+/**
+  Allocates and zeros a buffer of type EfiReservedMemoryType.
+
+  Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
+  buffer with zeros, and returns a pointer to the allocated buffer.  If AllocationSize is 0, then a
+  valid buffer of 0 size is returned.  If there is not enough memory remaining to satisfy the
+  request, then NULL is returned.
+
+  @param  AllocationSize        The number of bytes to allocate and zero.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+AllocateReservedZeroPool (
+  IN UINTN  AllocationSize
+  )
+{
+  return NULL;
+}
+
+/**
+  Copies a buffer to an allocated buffer of a certain pool type.
+
+  Allocates the number bytes specified by AllocationSize of a certain pool type, copies
+  AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
+  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there
+  is not enough memory remaining to satisfy the request, then NULL is returned.
+  If Buffer is NULL, then ASSERT().
+  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
+
+  @param  PoolType              The type of pool to allocate.
+  @param  AllocationSize        The number of bytes to allocate and zero.
+  @param  Buffer                The buffer to copy to the allocated buffer.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+InternalAllocateCopyPool (
+  IN EFI_MEMORY_TYPE  PoolType,
+  IN UINTN            AllocationSize,
+  IN CONST VOID       *Buffer
+  )
+{
+  VOID  *Memory;
+
+  ASSERT (Buffer != NULL);
+  ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));
+
+  Memory = InternalAllocatePool (PoolType, AllocationSize);
+  if (Memory != NULL) {
+     Memory = CopyMem (Memory, Buffer, AllocationSize);
+  }
+  return Memory;
+}
+
+/**
+  Copies a buffer to an allocated buffer of type EfiBootServicesData.
+
+  Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
+  AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
+  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there
+  is not enough memory remaining to satisfy the request, then NULL is returned.
+
+  If Buffer is NULL, then ASSERT().
+  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
+
+  @param  AllocationSize        The number of bytes to allocate and zero.
+  @param  Buffer                The buffer to copy to the allocated buffer.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+AllocateCopyPool (
+  IN UINTN       AllocationSize,
+  IN CONST VOID  *Buffer
+  )
+{
+  return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);
+}
+
+/**
+  Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
+
+  Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
+  AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
+  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there
+  is not enough memory remaining to satisfy the request, then NULL is returned.
+
+  If Buffer is NULL, then ASSERT().
+  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
+
+  @param  AllocationSize        The number of bytes to allocate and zero.
+  @param  Buffer                The buffer to copy to the allocated buffer.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+AllocateRuntimeCopyPool (
+  IN UINTN       AllocationSize,
+  IN CONST VOID  *Buffer
+  )
+{
+  return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);
+}
+
+/**
+  Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
+
+  Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
+  AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
+  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there
+  is not enough memory remaining to satisfy the request, then NULL is returned.
+
+  If Buffer is NULL, then ASSERT().
+  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
+
+  @param  AllocationSize        The number of bytes to allocate and zero.
+  @param  Buffer                The buffer to copy to the allocated buffer.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+AllocateReservedCopyPool (
+  IN UINTN       AllocationSize,
+  IN CONST VOID  *Buffer
+  )
+{
+  return NULL;
+}
+
+/**
+  Reallocates a buffer of a specified memory type.
+
+  Allocates and zeros the number bytes specified by NewSize from memory of the type
+  specified by PoolType.  If OldBuffer is not NULL, then the smaller of OldSize and
+  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
+  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
+  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
+  enough memory remaining to satisfy the request, then NULL is returned.
+
+  If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
+  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
+
+  @param  PoolType       The type of pool to allocate.
+  @param  OldSize        The size, in bytes, of OldBuffer.
+  @param  NewSize        The size, in bytes, of the buffer to reallocate.
+  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional
+                         parameter that may be NULL.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+InternalReallocatePool (
+  IN EFI_MEMORY_TYPE  PoolType,
+  IN UINTN            OldSize,
+  IN UINTN            NewSize,
+  IN VOID             *OldBuffer  OPTIONAL
+  )
+{
+  VOID  *NewBuffer;
+
+  NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);
+  if (NewBuffer != NULL && OldBuffer != NULL) {
+    CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));
+    FreePool (OldBuffer);
+  }
+  return NewBuffer;
+}
+
+/**
+  Reallocates a buffer of type EfiBootServicesData.
+
+  Allocates and zeros the number bytes specified by NewSize from memory of type
+  EfiBootServicesData.  If OldBuffer is not NULL, then the smaller of OldSize and
+  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
+  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
+  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
+  enough memory remaining to satisfy the request, then NULL is returned.
+
+  If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
+  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
+
+  @param  OldSize        The size, in bytes, of OldBuffer.
+  @param  NewSize        The size, in bytes, of the buffer to reallocate.
+  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional
+                         parameter that may be NULL.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+ReallocatePool (
+  IN UINTN  OldSize,
+  IN UINTN  NewSize,
+  IN VOID   *OldBuffer  OPTIONAL
+  )
+{
+  return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);
+}
+
+/**
+  Reallocates a buffer of type EfiRuntimeServicesData.
+
+  Allocates and zeros the number bytes specified by NewSize from memory of type
+  EfiRuntimeServicesData.  If OldBuffer is not NULL, then the smaller of OldSize and
+  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
+  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
+  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
+  enough memory remaining to satisfy the request, then NULL is returned.
+
+  If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
+  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
+
+  @param  OldSize        The size, in bytes, of OldBuffer.
+  @param  NewSize        The size, in bytes, of the buffer to reallocate.
+  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional
+                         parameter that may be NULL.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+ReallocateRuntimePool (
+  IN UINTN  OldSize,
+  IN UINTN  NewSize,
+  IN VOID   *OldBuffer  OPTIONAL
+  )
+{
+  return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);
+}
+
+/**
+  Reallocates a buffer of type EfiReservedMemoryType.
+
+  Allocates and zeros the number bytes specified by NewSize from memory of type
+  EfiReservedMemoryType.  If OldBuffer is not NULL, then the smaller of OldSize and
+  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
+  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
+  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
+  enough memory remaining to satisfy the request, then NULL is returned.
+
+  If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
+  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
+
+  @param  OldSize        The size, in bytes, of OldBuffer.
+  @param  NewSize        The size, in bytes, of the buffer to reallocate.
+  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional
+                         parameter that may be NULL.
+
+  @return A pointer to the allocated buffer or NULL if allocation fails.
+
+**/
+VOID *
+EFIAPI
+ReallocateReservedPool (
+  IN UINTN  OldSize,
+  IN UINTN  NewSize,
+  IN VOID   *OldBuffer  OPTIONAL
+  )
+{
+  return NULL;
+}
+
+/**
+  Frees a buffer that was previously allocated with one of the pool allocation functions in the
+  Memory Allocation Library.
+
+  Frees the buffer specified by Buffer.  Buffer must have been allocated on a previous call to the
+  pool allocation services of the Memory Allocation Library.  If it is not possible to free pool
+  resources, then this function will perform no actions.
+
+  If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
+  then ASSERT().
+
+  @param  Buffer                Pointer to the buffer to free.
+
+**/
+VOID
+EFIAPI
+FreePool (
+  IN VOID   *Buffer
+  )
+{
+  EFI_STATUS    Status;
+
+  Status = gMmst->MmFreePool (Buffer);
+  ASSERT_EFI_ERROR (Status);
+}
+
+/**
+  The constructor function calls MmInitializeMemoryServices to initialize
+  memory in MMRAM and caches EFI_MM_SYSTEM_TABLE pointer.
+
+  @param  ImageHandle   The firmware allocated handle for the EFI image.
+  @param  SystemTable   A pointer to the Management mode System Table.
+
+  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
+
+**/
+EFI_STATUS
+EFIAPI
+MemoryAllocationLibConstructor (
+  IN EFI_HANDLE             ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
+  )
+{
+  MM_CORE_PRIVATE_DATA           *MmCorePrivate;
+  EFI_HOB_GUID_TYPE               *GuidHob;
+  MM_CORE_DATA_HOB_DATA          *DataInHob;
+  VOID                            *HobStart;
+  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
+  EFI_MMRAM_DESCRIPTOR            *MmramRanges;
+  UINT32                           MmramRangeCount;
+  EFI_HOB_GUID_TYPE               *MmramRangesHob;
+
+  HobStart = GetHobList ();
+  DEBUG ((DEBUG_INFO, "StandaloneMmCoreMemoryAllocationLibConstructor - 0x%x\n", HobStart));
+
+  //
+  // Extract MM Core Private context from the Hob. If absent search for
+  // a Hob containing the MMRAM ranges
+  //
+  GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);
+  if (GuidHob == NULL) {
+    MmramRangesHob = GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid, HobStart);
+    if (MmramRangesHob == NULL) {
+      return EFI_UNSUPPORTED;
+    }
+
+    MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
+    if (MmramRangesHobData == NULL) {
+      return EFI_UNSUPPORTED;
+    }
+
+    MmramRanges = MmramRangesHobData->Descriptor;
+    if (MmramRanges == NULL) {
+      return EFI_UNSUPPORTED;
+    }
+
+    MmramRangeCount = MmramRangesHobData->NumberOfMmReservedRegions;
+    if (MmramRanges == NULL) {
+      return EFI_UNSUPPORTED;
+    }
+
+  } else {
+    DataInHob      = GET_GUID_HOB_DATA (GuidHob);
+    MmCorePrivate = (MM_CORE_PRIVATE_DATA *)(UINTN)DataInHob->Address;
+    MmramRanges     = (EFI_MMRAM_DESCRIPTOR *)(UINTN)MmCorePrivate->MmramRanges;
+    MmramRangeCount = MmCorePrivate->MmramRangeCount;
+  }
+
+  {
+    UINTN                Index;
+
+    DEBUG ((DEBUG_INFO, "MmramRangeCount - 0x%x\n", MmramRangeCount));
+    for (Index = 0; Index < MmramRangeCount; Index++) {
+      DEBUG ((DEBUG_INFO, "MmramRanges[%d]: 0x%016lx - 0x%016lx\n", Index, MmramRanges[Index].CpuStart, MmramRanges[Index].PhysicalSize));
+    }
+  }
+
+  //
+  // Initialize memory service using free MMRAM
+  //
+  DEBUG ((DEBUG_INFO, "MmInitializeMemoryServices\n"));
+  MmInitializeMemoryServices ((UINTN)MmramRangeCount, (VOID *)(UINTN)MmramRanges);
+
+  // Initialize MM Services Table
+  gMmst = MmSystemTable;
+  return EFI_SUCCESS;
+}
diff --git a/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
new file mode 100644
index 0000000000..068607f90e
--- /dev/null
+++ b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
@@ -0,0 +1,49 @@
+## @file
+# Memory Allocation Library instance dedicated to MM Core.
+# The implementation borrows the MM Core Memory Allocation services as the primitive
+# for memory allocation instead of using MM System Table servces in an indirect way.
+# It is assumed that this library instance must be linked with MM Core in this package.
+#
+# Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+#
+#  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
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001A
+  BASE_NAME                      = MemoryAllocationLib
+  FILE_GUID                      = DCDCBE1D-E760-4E1D-85B4-96E3F0439C41
+  MODULE_TYPE                    = MM_CORE_STANDALONE
+  VERSION_STRING                 = 1.0
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  LIBRARY_CLASS                  = MemoryAllocationLib|MM_CORE_STANDALONE
+  CONSTRUCTOR                    = MemoryAllocationLibConstructor
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+#  VALID_ARCHITECTURES           = IA32 X64
+#
+
+[Sources]
+  MemoryAllocationLib.c
+  MemoryAllocationServices.h
+
+[Packages]
+  MdePkg/MdePkg.dec
+  StandaloneMmPkg/StandaloneMmPkg.dec
+
+[LibraryClasses]
+  BaseMemoryLib
+  DebugLib
+  HobLib
+
+[Guids]
+  gEfiMmPeiMmramMemoryReserveGuid
diff --git a/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.h b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.h
new file mode 100644
index 0000000000..eb4f4c3984
--- /dev/null
+++ b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.h
@@ -0,0 +1,38 @@
+/** @file
+  Contains function prototypes for Memory Services in the MM Core.
+
+  This header file borrows the StandaloneMmCore Memory Allocation services as the primitive
+  for memory allocation.
+
+  Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef _PI_MM_CORE_MEMORY_ALLOCATION_SERVICES_H_
+#define _PI_MM_CORE_MEMORY_ALLOCATION_SERVICES_H_
+
+#include <Guid/MmCoreData.h>
+
+/**
+  Called to initialize the memory service.
+
+  @param   MmramRangeCount       Number of MMRAM Regions
+  @param   MmramRanges           Pointer to MMRAM Descriptors
+
+**/
+VOID
+MmInitializeMemoryServices (
+  IN UINTN                 MmramRangeCount,
+  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
+  );
+
+#endif
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 10/18] StandaloneMmPkg/HobLib: Add AARCH64 Specific HOB Library for management mode.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (8 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 09/18] StandaloneMmPkg/MemoryAllocationLib: Add MM memory allocation library Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-25 14:50   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 11/18] StandaloneMmPkg: MM driver entry point library Supreeth Venkatesh
                   ` (8 subsequent siblings)
  18 siblings, 1 reply; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

The Standalone MM environment is initialized during the SEC phase on ARM
Standard Platforms. The MM Core driver implements an entry point module
which is architecture specific and runs prior to the generic core driver
code. The former creates a Hob list that the latter consumes. This
happens in the same phase.

This patch implements a Hob library that can be used by the entry point
module to produce a Hob list and by the core driver code to consume it.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 StandaloneMmPkg/Library/HobLib/Arm/HobLib.c | 697 ++++++++++++++++++++++++++++
 StandaloneMmPkg/Library/HobLib/HobLib.inf   |  45 ++
 2 files changed, 742 insertions(+)
 create mode 100644 StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
 create mode 100644 StandaloneMmPkg/Library/HobLib/HobLib.inf

diff --git a/StandaloneMmPkg/Library/HobLib/Arm/HobLib.c b/StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
new file mode 100644
index 0000000000..62abf47f95
--- /dev/null
+++ b/StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
@@ -0,0 +1,697 @@
+/** @file
+  HOB Library implementation for DxeCore driver.
+
+Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2017, ARM Limited. All rights reserved.<BR>
+
+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.
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <PiMm.h>
+
+#include <Library/HobLib.h>
+#include <Library/DebugLib.h>
+#include <Library/BaseMemoryLib.h>
+
+#include <Guid/MemoryAllocationHob.h>
+
+//
+// Cache copy of HobList pointer.
+//
+VOID *gHobList = NULL;
+
+/**
+  Returns the pointer to the HOB list.
+
+  This function returns the pointer to first HOB in the list.
+  For PEI phase, the PEI service GetHobList() can be used to retrieve the pointer
+  to the HOB list.  For the DXE phase, the HOB list pointer can be retrieved through
+  the EFI System Table by looking up theHOB list GUID in the System Configuration Table.
+  Since the System Configuration Table does not exist that the time the DXE Core is
+  launched, the DXE Core uses a global variable from the DXE Core Entry Point Library
+  to manage the pointer to the HOB list.
+
+  If the pointer to the HOB list is NULL, then ASSERT().
+
+  @return The pointer to the HOB list.
+
+**/
+VOID *
+EFIAPI
+GetHobList (
+  VOID
+  )
+{
+  ASSERT (gHobList != NULL);
+  return gHobList;
+}
+
+/**
+  Returns the next instance of a HOB type from the starting HOB.
+
+  This function searches the first instance of a HOB type from the starting HOB pointer.
+  If there does not exist such HOB type from the starting HOB pointer, it will return NULL.
+  In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
+  unconditionally: it returns HobStart back if HobStart itself meets the requirement;
+  caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
+
+  If HobStart is NULL, then ASSERT().
+
+  @param  Type          The HOB type to return.
+  @param  HobStart      The starting HOB pointer to search from.
+
+  @return The next instance of a HOB type from the starting HOB.
+
+**/
+VOID *
+EFIAPI
+GetNextHob (
+  IN UINT16                 Type,
+  IN CONST VOID             *HobStart
+  )
+{
+  EFI_PEI_HOB_POINTERS  Hob;
+
+  ASSERT (HobStart != NULL);
+
+  Hob.Raw = (UINT8 *) HobStart;
+  //
+  // Parse the HOB list until end of list or matching type is found.
+  //
+  while (!END_OF_HOB_LIST (Hob)) {
+    if (Hob.Header->HobType == Type) {
+      return Hob.Raw;
+    }
+    Hob.Raw = GET_NEXT_HOB (Hob);
+  }
+  return NULL;
+}
+
+/**
+  Returns the first instance of a HOB type among the whole HOB list.
+
+  This function searches the first instance of a HOB type among the whole HOB list.
+  If there does not exist such HOB type in the HOB list, it will return NULL.
+
+  If the pointer to the HOB list is NULL, then ASSERT().
+
+  @param  Type          The HOB type to return.
+
+  @return The next instance of a HOB type from the starting HOB.
+
+**/
+VOID *
+EFIAPI
+GetFirstHob (
+  IN UINT16                 Type
+  )
+{
+  VOID      *HobList;
+
+  HobList = GetHobList ();
+  return GetNextHob (Type, HobList);
+}
+
+/**
+  Returns the next instance of the matched GUID HOB from the starting HOB.
+
+  This function searches the first instance of a HOB from the starting HOB pointer.
+  Such HOB should satisfy two conditions:
+  its HOB type is EFI_HOB_TYPE_GUID_EXTENSION, and its GUID Name equals to the input Guid.
+  If such a HOB from the starting HOB pointer does not exist, it will return NULL.
+  Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
+  to extract the data section and its size information, respectively.
+  In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
+  unconditionally: it returns HobStart back if HobStart itself meets the requirement;
+  caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
+
+  If Guid is NULL, then ASSERT().
+  If HobStart is NULL, then ASSERT().
+
+  @param  Guid          The GUID to match with in the HOB list.
+  @param  HobStart      A pointer to a Guid.
+
+  @return The next instance of the matched GUID HOB from the starting HOB.
+
+**/
+VOID *
+EFIAPI
+GetNextGuidHob (
+  IN CONST EFI_GUID         *Guid,
+  IN CONST VOID             *HobStart
+  )
+{
+  EFI_PEI_HOB_POINTERS  GuidHob;
+
+  GuidHob.Raw = (UINT8 *) HobStart;
+  while ((GuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, GuidHob.Raw)) != NULL) {
+    if (CompareGuid (Guid, &GuidHob.Guid->Name)) {
+      break;
+    }
+    GuidHob.Raw = GET_NEXT_HOB (GuidHob);
+  }
+  return GuidHob.Raw;
+}
+
+/**
+  Returns the first instance of the matched GUID HOB among the whole HOB list.
+
+  This function searches the first instance of a HOB among the whole HOB list.
+  Such HOB should satisfy two conditions:
+  its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
+  If such a HOB from the starting HOB pointer does not exist, it will return NULL.
+  Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
+  to extract the data section and its size information, respectively.
+
+  If the pointer to the HOB list is NULL, then ASSERT().
+  If Guid is NULL, then ASSERT().
+
+  @param  Guid          The GUID to match with in the HOB list.
+
+  @return The first instance of the matched GUID HOB among the whole HOB list.
+
+**/
+VOID *
+EFIAPI
+GetFirstGuidHob (
+  IN CONST EFI_GUID         *Guid
+  )
+{
+  VOID      *HobList;
+
+  HobList = GetHobList ();
+  return GetNextGuidHob (Guid, HobList);
+}
+
+/**
+  Get the system boot mode from the HOB list.
+
+  This function returns the system boot mode information from the
+  PHIT HOB in HOB list.
+
+  If the pointer to the HOB list is NULL, then ASSERT().
+
+  @param  VOID
+
+  @return The Boot Mode.
+
+**/
+EFI_BOOT_MODE
+EFIAPI
+GetBootModeHob (
+  VOID
+  )
+{
+  EFI_HOB_HANDOFF_INFO_TABLE    *HandOffHob;
+
+  HandOffHob = (EFI_HOB_HANDOFF_INFO_TABLE *) GetHobList ();
+
+  return  HandOffHob->BootMode;
+}
+
+
+/**
+
+
+**/
+EFI_HOB_HANDOFF_INFO_TABLE*
+HobConstructor (
+  IN VOID   *EfiMemoryBegin,
+  IN UINTN  EfiMemoryLength,
+  IN VOID   *EfiFreeMemoryBottom,
+  IN VOID   *EfiFreeMemoryTop
+  )
+{
+  EFI_HOB_HANDOFF_INFO_TABLE  *Hob;
+  EFI_HOB_GENERIC_HEADER      *HobEnd;
+
+  Hob    = EfiFreeMemoryBottom;
+  HobEnd = (EFI_HOB_GENERIC_HEADER *)(Hob+1);
+
+  Hob->Header.HobType     = EFI_HOB_TYPE_HANDOFF;
+  Hob->Header.HobLength   = sizeof(EFI_HOB_HANDOFF_INFO_TABLE);
+  Hob->Header.Reserved    = 0;
+
+  HobEnd->HobType     = EFI_HOB_TYPE_END_OF_HOB_LIST;
+  HobEnd->HobLength   = sizeof(EFI_HOB_GENERIC_HEADER);
+  HobEnd->Reserved    = 0;
+
+  Hob->Version             = EFI_HOB_HANDOFF_TABLE_VERSION;
+  Hob->BootMode            = BOOT_WITH_FULL_CONFIGURATION;
+
+  Hob->EfiMemoryTop        = (UINTN)EfiMemoryBegin + EfiMemoryLength;
+  Hob->EfiMemoryBottom     = (UINTN)EfiMemoryBegin;
+  Hob->EfiFreeMemoryTop    = (UINTN)EfiFreeMemoryTop;
+  Hob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS)(UINTN)(HobEnd+1);
+  Hob->EfiEndOfHobList     = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd;
+
+  gHobList = Hob;
+
+  return Hob;
+}
+
+VOID *
+CreateHob (
+  IN  UINT16    HobType,
+  IN  UINT16    HobLength
+  )
+{
+  EFI_HOB_HANDOFF_INFO_TABLE  *HandOffHob;
+  EFI_HOB_GENERIC_HEADER      *HobEnd;
+  EFI_PHYSICAL_ADDRESS        FreeMemory;
+  VOID                        *Hob;
+
+  HandOffHob = GetHobList ();
+
+  HobLength = (UINT16)((HobLength + 0x7) & (~0x7));
+
+  FreeMemory = HandOffHob->EfiFreeMemoryTop - HandOffHob->EfiFreeMemoryBottom;
+
+  if (FreeMemory < HobLength) {
+      return NULL;
+  }
+
+  Hob = (VOID*) (UINTN) HandOffHob->EfiEndOfHobList;
+  ((EFI_HOB_GENERIC_HEADER*) Hob)->HobType = HobType;
+  ((EFI_HOB_GENERIC_HEADER*) Hob)->HobLength = HobLength;
+  ((EFI_HOB_GENERIC_HEADER*) Hob)->Reserved = 0;
+
+  HobEnd = (EFI_HOB_GENERIC_HEADER*) ((UINTN)Hob + HobLength);
+  HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
+
+  HobEnd->HobType   = EFI_HOB_TYPE_END_OF_HOB_LIST;
+  HobEnd->HobLength = sizeof(EFI_HOB_GENERIC_HEADER);
+  HobEnd->Reserved  = 0;
+  HobEnd++;
+  HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
+
+  return Hob;
+}
+
+/**
+  Builds a HOB for a loaded PE32 module.
+
+  This function builds a HOB for a loaded PE32 module.
+  It can only be invoked during PEI phase;
+  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+  If ModuleName is NULL, then ASSERT().
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  ModuleName              The GUID File Name of the module.
+  @param  MemoryAllocationModule  The 64 bit physical address of the module.
+  @param  ModuleLength            The length of the module in bytes.
+  @param  EntryPoint              The 64 bit physical address of the module entry point.
+
+**/
+VOID
+EFIAPI
+BuildModuleHob (
+  IN CONST EFI_GUID         *ModuleName,
+  IN EFI_PHYSICAL_ADDRESS   MemoryAllocationModule,
+  IN UINT64                 ModuleLength,
+  IN EFI_PHYSICAL_ADDRESS   EntryPoint
+  )
+{
+  EFI_HOB_MEMORY_ALLOCATION_MODULE  *Hob;
+
+  ASSERT (((MemoryAllocationModule & (EFI_PAGE_SIZE - 1)) == 0) &&
+          ((ModuleLength & (EFI_PAGE_SIZE - 1)) == 0));
+
+  Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE));
+
+  CopyGuid (&(Hob->MemoryAllocationHeader.Name), &gEfiHobMemoryAllocModuleGuid);
+  Hob->MemoryAllocationHeader.MemoryBaseAddress = MemoryAllocationModule;
+  Hob->MemoryAllocationHeader.MemoryLength      = ModuleLength;
+  Hob->MemoryAllocationHeader.MemoryType        = EfiBootServicesCode;
+
+  //
+  // Zero the reserved space to match HOB spec
+  //
+  ZeroMem (Hob->MemoryAllocationHeader.Reserved, sizeof (Hob->MemoryAllocationHeader.Reserved));
+
+  CopyGuid (&Hob->ModuleName, ModuleName);
+  Hob->EntryPoint = EntryPoint;
+}
+
+/**
+  Builds a HOB that describes a chunk of system memory.
+
+  This function builds a HOB that describes a chunk of system memory.
+  It can only be invoked during PEI phase;
+  for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.
+
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  ResourceType        The type of resource described by this HOB.
+  @param  ResourceAttribute   The resource attributes of the memory described by this HOB.
+  @param  PhysicalStart       The 64 bit physical address of memory described by this HOB.
+  @param  NumberOfBytes       The length of the memory described by this HOB in bytes.
+
+**/
+VOID
+EFIAPI
+BuildResourceDescriptorHob (
+  IN EFI_RESOURCE_TYPE            ResourceType,
+  IN EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttribute,
+  IN EFI_PHYSICAL_ADDRESS         PhysicalStart,
+  IN UINT64                       NumberOfBytes
+  )
+{
+  EFI_HOB_RESOURCE_DESCRIPTOR  *Hob;
+
+  Hob = CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR));
+  ASSERT(Hob != NULL);
+
+  Hob->ResourceType      = ResourceType;
+  Hob->ResourceAttribute = ResourceAttribute;
+  Hob->PhysicalStart     = PhysicalStart;
+  Hob->ResourceLength    = NumberOfBytes;
+}
+
+/**
+  Builds a GUID HOB with a certain data length.
+
+  This function builds a customized HOB tagged with a GUID for identification
+  and returns the start address of GUID HOB data so that caller can fill the customized data.
+  The HOB Header and Name field is already stripped.
+  It can only be invoked during PEI phase;
+  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+  If Guid is NULL, then ASSERT().
+  If there is no additional space for HOB creation, then ASSERT().
+  If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
+
+  @param  Guid          The GUID to tag the customized HOB.
+  @param  DataLength    The size of the data payload for the GUID HOB.
+
+  @return The start address of GUID HOB data.
+
+**/
+VOID *
+EFIAPI
+BuildGuidHob (
+  IN CONST EFI_GUID              *Guid,
+  IN UINTN                       DataLength
+  )
+{
+  EFI_HOB_GUID_TYPE *Hob;
+
+  //
+  // Make sure that data length is not too long.
+  //
+  ASSERT (DataLength <= (0xffff - sizeof (EFI_HOB_GUID_TYPE)));
+
+  Hob = CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16) (sizeof (EFI_HOB_GUID_TYPE) + DataLength));
+  CopyGuid (&Hob->Name, Guid);
+  return Hob + 1;
+}
+
+
+/**
+  Copies a data buffer to a newly-built HOB.
+
+  This function builds a customized HOB tagged with a GUID for identification,
+  copies the input data to the HOB data field and returns the start address of the GUID HOB data.
+  The HOB Header and Name field is already stripped.
+  It can only be invoked during PEI phase;
+  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+  If Guid is NULL, then ASSERT().
+  If Data is NULL and DataLength > 0, then ASSERT().
+  If there is no additional space for HOB creation, then ASSERT().
+  If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
+
+  @param  Guid          The GUID to tag the customized HOB.
+  @param  Data          The data to be copied into the data field of the GUID HOB.
+  @param  DataLength    The size of the data payload for the GUID HOB.
+
+  @return The start address of GUID HOB data.
+
+**/
+VOID *
+EFIAPI
+BuildGuidDataHob (
+  IN CONST EFI_GUID              *Guid,
+  IN VOID                        *Data,
+  IN UINTN                       DataLength
+  )
+{
+  VOID  *HobData;
+
+  ASSERT (Data != NULL || DataLength == 0);
+
+  HobData = BuildGuidHob (Guid, DataLength);
+
+  return CopyMem (HobData, Data, DataLength);
+}
+
+/**
+  Builds a Firmware Volume HOB.
+
+  This function builds a Firmware Volume HOB.
+  It can only be invoked during PEI phase;
+  for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.
+
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  BaseAddress   The base address of the Firmware Volume.
+  @param  Length        The size of the Firmware Volume in bytes.
+
+**/
+VOID
+EFIAPI
+BuildFvHob (
+  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
+  IN UINT64                      Length
+  )
+{
+  EFI_HOB_FIRMWARE_VOLUME  *Hob;
+
+  Hob = CreateHob (EFI_HOB_TYPE_FV, sizeof (EFI_HOB_FIRMWARE_VOLUME));
+
+  Hob->BaseAddress = BaseAddress;
+  Hob->Length      = Length;
+}
+
+
+/**
+  Builds a EFI_HOB_TYPE_FV2 HOB.
+
+  This function builds a EFI_HOB_TYPE_FV2 HOB.
+  It can only be invoked during PEI phase;
+  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  BaseAddress   The base address of the Firmware Volume.
+  @param  Length        The size of the Firmware Volume in bytes.
+  @param  FvName       The name of the Firmware Volume.
+  @param  FileName      The name of the file.
+
+**/
+VOID
+EFIAPI
+BuildFv2Hob (
+  IN          EFI_PHYSICAL_ADDRESS        BaseAddress,
+  IN          UINT64                      Length,
+  IN CONST    EFI_GUID                    *FvName,
+  IN CONST    EFI_GUID                    *FileName
+  )
+{
+  EFI_HOB_FIRMWARE_VOLUME2  *Hob;
+
+  Hob = CreateHob (EFI_HOB_TYPE_FV2, sizeof (EFI_HOB_FIRMWARE_VOLUME2));
+
+  Hob->BaseAddress = BaseAddress;
+  Hob->Length      = Length;
+  CopyGuid (&Hob->FvName, FvName);
+  CopyGuid (&Hob->FileName, FileName);
+}
+
+
+/**
+  Builds a HOB for the CPU.
+
+  This function builds a HOB for the CPU.
+  It can only be invoked during PEI phase;
+  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  SizeOfMemorySpace   The maximum physical memory addressability of the processor.
+  @param  SizeOfIoSpace       The maximum physical I/O addressability of the processor.
+
+**/
+VOID
+EFIAPI
+BuildCpuHob (
+  IN UINT8                       SizeOfMemorySpace,
+  IN UINT8                       SizeOfIoSpace
+  )
+{
+  EFI_HOB_CPU  *Hob;
+
+  Hob = CreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));
+
+  Hob->SizeOfMemorySpace = SizeOfMemorySpace;
+  Hob->SizeOfIoSpace     = SizeOfIoSpace;
+
+  //
+  // Zero the reserved space to match HOB spec
+  //
+  ZeroMem (Hob->Reserved, sizeof (Hob->Reserved));
+}
+
+/**
+  Builds a HOB for the memory allocation.
+
+  This function builds a HOB for the memory allocation.
+  It can only be invoked during PEI phase;
+  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  BaseAddress   The 64 bit physical address of the memory.
+  @param  Length        The length of the memory allocation in bytes.
+  @param  MemoryType    Type of memory allocated by this HOB.
+
+**/
+VOID
+EFIAPI
+BuildMemoryAllocationHob (
+  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
+  IN UINT64                      Length,
+  IN EFI_MEMORY_TYPE             MemoryType
+  )
+{
+  EFI_HOB_MEMORY_ALLOCATION  *Hob;
+
+  ASSERT (((BaseAddress & (EFI_PAGE_SIZE - 1)) == 0) &&
+          ((Length & (EFI_PAGE_SIZE - 1)) == 0));
+
+  Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION));
+
+  ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));
+  Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
+  Hob->AllocDescriptor.MemoryLength      = Length;
+  Hob->AllocDescriptor.MemoryType        = MemoryType;
+  //
+  // Zero the reserved space to match HOB spec
+  //
+  ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));
+}
+
+/**
+  Builds a HOB that describes a chunk of system memory with Owner GUID.
+
+  This function builds a HOB that describes a chunk of system memory.
+  It can only be invoked during PEI phase;
+  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  ResourceType        The type of resource described by this HOB.
+  @param  ResourceAttribute   The resource attributes of the memory described by this HOB.
+  @param  PhysicalStart       The 64 bit physical address of memory described by this HOB.
+  @param  NumberOfBytes       The length of the memory described by this HOB in bytes.
+  @param  OwnerGUID           GUID for the owner of this resource.
+
+**/
+VOID
+EFIAPI
+BuildResourceDescriptorWithOwnerHob (
+  IN EFI_RESOURCE_TYPE            ResourceType,
+  IN EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttribute,
+  IN EFI_PHYSICAL_ADDRESS         PhysicalStart,
+  IN UINT64                       NumberOfBytes,
+  IN EFI_GUID                     *OwnerGUID
+  )
+{
+  //
+  // PEI HOB is read only for DXE phase
+  //
+  ASSERT (FALSE);
+}
+
+/**
+  Builds a Capsule Volume HOB.
+
+  This function builds a Capsule Volume HOB.
+  It can only be invoked during PEI phase;
+  for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.
+
+  If the platform does not support Capsule Volume HOBs, then ASSERT().
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  BaseAddress   The base address of the Capsule Volume.
+  @param  Length        The size of the Capsule Volume in bytes.
+
+**/
+VOID
+EFIAPI
+BuildCvHob (
+  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
+  IN UINT64                      Length
+  )
+{
+  //
+  // PEI HOB is read only for DXE phase
+  //
+  ASSERT (FALSE);
+}
+
+
+/**
+  Builds a HOB for the BSP store.
+
+  This function builds a HOB for BSP store.
+  It can only be invoked during PEI phase;
+  for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.
+
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  BaseAddress   The 64 bit physical address of the BSP.
+  @param  Length        The length of the BSP store in bytes.
+  @param  MemoryType    Type of memory allocated by this HOB.
+
+**/
+VOID
+EFIAPI
+BuildBspStoreHob (
+  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
+  IN UINT64                      Length,
+  IN EFI_MEMORY_TYPE             MemoryType
+  )
+{
+  //
+  // PEI HOB is read only for DXE phase
+  //
+  ASSERT (FALSE);
+}
+
+/**
+  Builds a HOB for the Stack.
+
+  This function builds a HOB for the stack.
+  It can only be invoked during PEI phase;
+  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  BaseAddress   The 64 bit physical address of the Stack.
+  @param  Length        The length of the stack in bytes.
+
+**/
+VOID
+EFIAPI
+BuildStackHob (
+  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
+  IN UINT64                      Length
+  )
+{
+  //
+  // PEI HOB is read only for DXE phase
+  //
+  ASSERT (FALSE);
+}
diff --git a/StandaloneMmPkg/Library/HobLib/HobLib.inf b/StandaloneMmPkg/Library/HobLib/HobLib.inf
new file mode 100644
index 0000000000..42273b6d66
--- /dev/null
+++ b/StandaloneMmPkg/Library/HobLib/HobLib.inf
@@ -0,0 +1,45 @@
+## @file
+# Instance of HOB Library for DXE Core.
+#
+# HOB Library implementation for the DXE Core. Does not have a constructor.
+#  Uses gHobList defined in the DXE Core Entry Point Library.
+#
+# Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+#
+#  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.
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001A
+  BASE_NAME                      = HobLib
+  FILE_GUID                      = CF56EF2C-68D8-4BD5-9A8B-8A7BFCFF751C
+  MODULE_TYPE                    = MM_CORE_STANDALONE
+  VERSION_STRING                 = 1.0
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  LIBRARY_CLASS                  = HobLib|MM_CORE_STANDALONE MM_STANDALONE
+
+#
+#  VALID_ARCHITECTURES           = AARCH64
+#
+
+[Sources.AARCH64]
+  Arm/HobLib.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+
+
+[LibraryClasses]
+  BaseMemoryLib
+  DebugLib
+
+[Guids]
+  gEfiHobListGuid                               ## CONSUMES  ## SystemTable
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 11/18] StandaloneMmPkg: MM driver entry point library.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (9 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 10/18] StandaloneMmPkg/HobLib: Add AARCH64 Specific HOB Library for management mode Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-30 14:29   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 12/18] StandaloneMmPkg/CpuMm: Add CPU driver suitable for ARM Platforms Supreeth Venkatesh
                   ` (7 subsequent siblings)
  18 siblings, 1 reply; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

This patch implements module entry point library for Standalone
management mode (MM) Drivers.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 .../Include/Library/MmDriverStandaloneEntryPoint.h | 148 +++++++++++++++++++++
 .../StandaloneMmDriverEntryPoint.c                 | 102 ++++++++++++++
 .../StandaloneMmDriverEntryPoint.inf               |  41 ++++++
 3 files changed, 291 insertions(+)
 create mode 100644 StandaloneMmPkg/Include/Library/MmDriverStandaloneEntryPoint.h
 create mode 100644 StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.c
 create mode 100644 StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf

diff --git a/StandaloneMmPkg/Include/Library/MmDriverStandaloneEntryPoint.h b/StandaloneMmPkg/Include/Library/MmDriverStandaloneEntryPoint.h
new file mode 100644
index 0000000000..6fb9224e2e
--- /dev/null
+++ b/StandaloneMmPkg/Include/Library/MmDriverStandaloneEntryPoint.h
@@ -0,0 +1,148 @@
+/** @file
+  Module entry point library for UEFI drivers, DXE Drivers, DXE Runtime Drivers,
+  and DXE SMM Drivers.
+
+Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+
+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
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef __MODULE_ENTRY_POINT_H__
+#define __MODULE_ENTRY_POINT_H__
+
+///
+///Declare the PI Specification Revision that this driver requires to execute correctly.
+///
+extern CONST UINT32                   _gMmRevision;
+
+/**
+  The entry point of PE/COFF Image for a DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
+
+  This function is the entry point for a DXE Driver, DXE Runtime Driver, DXE SMM Driver,
+  or UEFI Driver.  This function must call ProcessLibraryConstructorList() and
+  ProcessModuleEntryPointList(). If the return status from ProcessModuleEntryPointList()
+  is an error status, then ProcessLibraryDestructorList() must be called. The return value
+  from ProcessModuleEntryPointList() is returned. If _gDriverUnloadImageCount is greater
+  than zero, then an unload handler must be registered for this image and the unload handler
+  must invoke ProcessModuleUnloadList().
+  If _gUefiDriverRevision is not zero and SystemTable->Hdr.Revision is less than _gUefiDriverRevison,
+  then return EFI_INCOMPATIBLE_VERSION.
+
+
+  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
+  @param  SystemTable  A pointer to the EFI System Table.
+
+  @retval  EFI_SUCCESS               The DXE Driver, DXE Runtime Driver, DXE SMM Driver,
+                                     or UEFI Driver exited normally.
+  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than SystemTable->Hdr.Revision.
+  @retval  Other                     Return value from ProcessModuleEntryPointList().
+
+**/
+EFI_STATUS
+EFIAPI
+_ModuleEntryPoint (
+  IN EFI_HANDLE             ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
+  );
+
+
+/**
+  Required by the EBC compiler and identical in functionality to _ModuleEntryPoint().
+
+  This function is required to call _ModuleEntryPoint() passing in ImageHandle, and SystemTable.
+
+  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
+  @param  SystemTable  A pointer to the EFI System Table.
+
+  @retval  EFI_SUCCESS               The DXE Driver, DXE Runtime Driver, DXE SMM Driver,
+                                     or UEFI Driver exited normally.
+  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than SystemTable->Hdr.Revision.
+  @retval  Other                     Return value from ProcessModuleEntryPointList().
+**/
+EFI_STATUS
+EFIAPI
+EfiMain (
+  IN EFI_HANDLE             ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
+  );
+
+
+/**
+  Autogenerated function that calls the library constructors for all of the module's
+  dependent libraries.
+
+  This function must be called by _ModuleEntryPoint().
+  This function calls the set of library constructors for the set of library instances
+  that a module depends on.  This includes library instances that a module depends on
+  directly and library instances that a module depends on indirectly through other libraries.
+  This function is autogenerated by build tools and those build tools are responsible
+  for collecting the set of library instances, determine which ones have constructors,
+  and calling the library constructors in the proper order based upon each of the library
+  instances own dependencies.
+
+  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
+  @param  SystemTable  A pointer to the EFI System Table.
+
+**/
+VOID
+EFIAPI
+ProcessLibraryConstructorList (
+  IN EFI_HANDLE             ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
+  );
+
+
+/**
+  Autogenerated function that calls the library descructors for all of the module's
+  dependent libraries.
+
+  This function may be called by _ModuleEntryPoint() or ExitDriver().
+  This function calls the set of library destructors for the set of library instances
+  that a module depends on. This includes library instances that a module depends on
+  directly and library instances that a module depends on indirectly through other libraries.
+  This function is autogenerated by build tools and those build tools are responsible for
+  collecting the set of library instances, determine which ones have destructors, and calling
+  the library destructors in the proper order based upon each of the library instances own dependencies.
+
+  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
+  @param  SystemTable  A pointer to the EFI System Table.
+
+**/
+VOID
+EFIAPI
+ProcessLibraryDestructorList (
+  IN EFI_HANDLE             ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
+  );
+
+
+/**
+  Autogenerated function that calls a set of module entry points.
+
+  This function must be called by _ModuleEntryPoint().
+  This function calls the set of module entry points.
+  This function is autogenerated by build tools and those build tools are responsible
+  for collecting the module entry points and calling them in a specified order.
+
+  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
+  @param  SystemTable  A pointer to the EFI System Table.
+
+  @retval  EFI_SUCCESS   The DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver executed normally.
+  @retval  !EFI_SUCCESS  The DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver failed to execute normally.
+**/
+EFI_STATUS
+EFIAPI
+ProcessModuleEntryPointList (
+  IN EFI_HANDLE             ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
+  );
+
+#endif
diff --git a/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.c b/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.c
new file mode 100644
index 0000000000..84b3d9cd08
--- /dev/null
+++ b/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.c
@@ -0,0 +1,102 @@
+/** @file
+  Entry point to a Standalone SMM driver.
+
+Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2016-2017, ARM Ltd. All rights reserved.<BR>
+
+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
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+
+
+#include <PiMm.h>
+
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+
+VOID
+EFIAPI
+ProcessLibraryConstructorList (
+  IN EFI_HANDLE               ImageHandle,
+  IN IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
+  );
+
+EFI_STATUS
+EFIAPI
+ProcessModuleEntryPointList (
+  IN EFI_HANDLE               ImageHandle,
+  IN IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
+  );
+
+VOID
+EFIAPI
+ProcessLibraryDestructorList (
+  IN EFI_HANDLE               ImageHandle,
+  IN IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
+  );
+
+/**
+  The entry point of PE/COFF Image for a DXE Driver, DXE Runtime Driver, DXE SMM
+  Driver, or UEFI Driver.
+
+  This function is the entry point for a DXE Driver, DXE Runtime Driver, DXE SMM Driver,
+  or UEFI Driver.  This function must call ProcessLibraryConstructorList() and
+  ProcessModuleEntryPointList(). If the return status from ProcessModuleEntryPointList()
+  is an error status, then ProcessLibraryDestructorList() must be called. The return
+  value from ProcessModuleEntryPointList() is returned. If _gDriverUnloadImageCount
+  is greater than zero, then an unload handler must be registered for this image
+  and the unload handler must invoke ProcessModuleUnloadList().
+  If _gUefiDriverRevision is not zero and SystemTable->Hdr.Revision is less than
+  _gUefiDriverRevison, then return EFI_INCOMPATIBLE_VERSION.
+
+
+  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver,
+                       DXE SMM Driver, or UEFI Driver.
+  @param  SystemTable  A pointer to the EFI System Table.
+
+  @retval  EFI_SUCCESS               The DXE Driver, DXE Runtime Driver, DXE SMM
+                                     Driver, or UEFI Driver exited normally.
+  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than
+                                    SystemTable->Hdr.Revision.
+  @retval  Other                     Return value from ProcessModuleEntryPointList().
+
+**/
+EFI_STATUS
+EFIAPI
+_ModuleEntryPoint (
+  IN EFI_HANDLE               ImageHandle,
+  IN IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
+  )
+{
+  EFI_STATUS                 Status;
+
+  //
+  // Call constructor for all libraries
+  //
+  ProcessLibraryConstructorList (ImageHandle, MmSystemTable);
+
+  //
+  // Call the driver entry point
+  //
+  Status = ProcessModuleEntryPointList (ImageHandle, MmSystemTable);
+
+  //
+  // If all of the drivers returned errors, then invoke all of the library destructors
+  //
+  if (EFI_ERROR (Status)) {
+    ProcessLibraryDestructorList (ImageHandle, MmSystemTable);
+  }
+
+  //
+  // Return the cumulative return status code from all of the driver entry points
+  //
+  return Status;
+}
+
diff --git a/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf b/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf
new file mode 100644
index 0000000000..564a3f14c8
--- /dev/null
+++ b/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf
@@ -0,0 +1,41 @@
+## @file
+# Module entry point library for Standalone SMM driver.
+#
+# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2016-2017, ARM Ltd. All rights reserved.<BR>
+#
+#  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.
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001A
+  BASE_NAME                      = StandaloneMmDriverEntryPoint
+  FILE_GUID                      = BBC33478-98F8-4B78-B29D-574D681B7E43
+  MODULE_TYPE                    = MM_STANDALONE
+  VERSION_STRING                 = 1.0
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  LIBRARY_CLASS                  = StandaloneMmDriverEntryPoint|MM_STANDALONE
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC
+#
+
+[Sources]
+  StandaloneMmDriverEntryPoint.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  BaseLib
+  DebugLib
+
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 12/18] StandaloneMmPkg/CpuMm: Add CPU driver suitable for ARM Platforms.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (10 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 11/18] StandaloneMmPkg: MM driver entry point library Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-18 22:09   ` Daniil Egranov
  2018-04-30 15:50   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 13/18] StandaloneMmPkg/Core: Implementation of Standalone MM Core Module Supreeth Venkatesh
                   ` (6 subsequent siblings)
  18 siblings, 2 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

This patch adds a simple CPU driver that exports the
EFI_MM_CONFIGURATION_PROTOCOL to allow registration of the Standalone
MM Foundation entry point. It preserves the existing notification
mechanism for the configuration protocol.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S          |  33 +++
 StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c    | 231 +++++++++++++++++++++
 StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c           | 229 ++++++++++++++++++++
 .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h        |  89 ++++++++
 .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf      |  60 ++++++
 StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c      |  51 +++++
 StandaloneMmPkg/Include/Guid/MpInformation.h       |  41 ++++
 7 files changed, 734 insertions(+)
 create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
 create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
 create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
 create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
 create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
 create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
 create mode 100644 StandaloneMmPkg/Include/Guid/MpInformation.h

diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S b/StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
new file mode 100644
index 0000000000..0b6e1c330d
--- /dev/null
+++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
@@ -0,0 +1,33 @@
+//
+//  Copyright (c) 2017, ARM Limited. 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
+//
+//  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+//
+//
+
+#include <Base.h>
+#include <AutoGen.h>
+#include <IndustryStandard/ArmStdSmc.h>
+
+.text
+.align 3
+
+GCC_ASM_IMPORT(PiMmStandloneArmTfCpuDriverEntry)
+GCC_ASM_EXPORT(_PiMmStandloneArmTfCpuDriverEntry)
+
+// Stub entry point to ensure that the stacks are completely unwound before
+// signalling completion of event handling
+ASM_PFX(_PiMmStandloneArmTfCpuDriverEntry):
+  bl    PiMmStandloneArmTfCpuDriverEntry
+  mov   x1, x0
+  mov   x0, #(ARM_SMC_ID_MM_EVENT_COMPLETE_AARCH64 & 0xffff)
+  movk  x0, #((ARM_SMC_ID_MM_EVENT_COMPLETE_AARCH64 >> 16) & 0xffff), lsl #16
+  svc   #0
+LoopForever:
+  b     LoopForever
diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c b/StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
new file mode 100644
index 0000000000..7b19f53ecb
--- /dev/null
+++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
@@ -0,0 +1,231 @@
+/** @file
+
+  Copyright (c) 2016 HP Development Company, L.P.
+  Copyright (c) 2016 - 2017, ARM Limited. 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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <Base.h>
+#include <Pi/PiMmCis.h>
+
+
+#include <Library/ArmSvcLib.h>
+#include <Library/ArmLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/HobLib.h>
+
+#include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT
+
+#include <Guid/ZeroGuid.h>
+#include <Guid/MmramMemoryReserve.h>
+
+#include <IndustryStandard/ArmStdSmc.h>
+
+#include "PiMmStandloneArmTfCpuDriver.h"
+
+EFI_STATUS
+EFIAPI
+MmFoundationEntryRegister(
+  IN CONST EFI_MM_CONFIGURATION_PROTOCOL  *This,
+  IN EFI_MM_ENTRY_POINT                    MmEntryPoint
+  );
+
+//
+// On ARM platforms every event is expected to have a GUID associated with
+// it. It will be used by the MM Entry point to find the handler for the
+// event. It will either be populated in a EFI_MM_COMMUNICATE_HEADER by the
+// caller of the event (e.g. MM_COMMUNICATE SMC) or by the CPU driver
+// (e.g. during an asynchronous event). In either case, this context is
+// maintained in an array which has an entry for each CPU. The pointer to this
+// array is held in PerCpuGuidedEventContext. Memory is allocated once the
+// number of CPUs in the system are made known through the
+// MP_INFORMATION_HOB_DATA.
+//
+EFI_MM_COMMUNICATE_HEADER **PerCpuGuidedEventContext = NULL;
+
+//
+// When an event is received by the CPU driver, it could correspond to a unique
+// GUID (e.g. interrupt events) or to multiple GUIDs (e.g. MM_COMMUNICATE
+// SMC). A table is used by the CPU driver to find the GUID corresponding to the
+// event id in case there is a 1:1 mapping between the two. If an event id has
+// multiple GUIDs associated with it then such an entry will not be found in
+// this table.
+//
+// TODO: Currently NULL since there are no asynchronous events
+static EFI_GUID *EventIdToGuidLookupTable = NULL;
+
+// Descriptor with whereabouts of memory used for communication with the normal world
+EFI_MMRAM_DESCRIPTOR  mNsCommBuffer;
+
+MP_INFORMATION_HOB_DATA *mMpInformationHobData;
+
+EFI_MM_CONFIGURATION_PROTOCOL mMmConfig = {
+  0,
+  MmFoundationEntryRegister
+};
+
+static EFI_MM_ENTRY_POINT     mMmEntryPoint = NULL;
+
+EFI_STATUS
+PiMmStandloneArmTfCpuDriverEntry (
+  IN UINTN EventId,
+  IN UINTN CpuNumber,
+  IN UINTN NsCommBufferAddr
+  )
+{
+  EFI_MM_COMMUNICATE_HEADER *GuidedEventContext = NULL;
+  EFI_MM_ENTRY_CONTEXT        MmEntryPointContext = {0};
+  EFI_STATUS                  Status;
+  UINTN                       NsCommBufferSize;
+
+  DEBUG ((DEBUG_INFO, "Received event - 0x%x on cpu %d\n", EventId, CpuNumber));
+
+  //
+  // ARM TF passes SMC FID of the MM_COMMUNICATE interface as the Event ID upon
+  // receipt of a synchronous MM request. Use the Event ID to distinguish
+  // between synchronous and asynchronous events.
+  //
+  if (ARM_SMC_ID_MM_COMMUNICATE_AARCH64 != EventId) {
+    // Found a GUID, allocate memory to populate a communication buffer
+    // with the GUID in it
+    Status = mMmst->MmAllocatePool(EfiRuntimeServicesData, sizeof(EFI_MM_COMMUNICATE_HEADER), (VOID **) &GuidedEventContext);
+    if (Status != EFI_SUCCESS) {
+      DEBUG ((DEBUG_INFO, "Mem alloc failed - 0x%x\n", EventId));
+      return Status;
+    }
+
+    // Copy the GUID
+    CopyGuid(&GuidedEventContext->HeaderGuid, &EventIdToGuidLookupTable[EventId]);
+
+    // Message Length is 0 'cause of the assumption mentioned above
+    GuidedEventContext->MessageLength = 0;
+  } else {
+    // TODO: Perform parameter validation of NsCommBufferAddr
+
+    // This event id is the parent of multiple GUIDed handlers. Retrieve
+    // the specific GUID from the communication buffer passed by the
+    // caller.
+
+    if (NsCommBufferAddr && (NsCommBufferAddr < mNsCommBuffer.PhysicalStart))
+      return EFI_INVALID_PARAMETER;
+
+    // Find out the size of the buffer passed
+    NsCommBufferSize = ((EFI_MM_COMMUNICATE_HEADER *) NsCommBufferAddr)->MessageLength +
+                        sizeof(((EFI_MM_COMMUNICATE_HEADER *)NsCommBufferAddr)->MessageLength) +
+                        sizeof(((EFI_MM_COMMUNICATE_HEADER *)NsCommBufferAddr)->HeaderGuid);
+
+    // Alternative approach in case EL3 has preallocated the non-secure
+    // buffer. MM Foundation is told about the buffer through the Hoblist
+    // and is responsible for performing the bounds check.
+    if (NsCommBufferAddr + NsCommBufferSize >=
+      mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize)
+        return EFI_INVALID_PARAMETER;
+
+
+    // Now that the secure world can see the normal world buffer, allocate
+    // memory to copy the communication buffer to the secure world.
+    Status = mMmst->MmAllocatePool(EfiRuntimeServicesData, NsCommBufferSize, (VOID **) &GuidedEventContext);
+    if (Status != EFI_SUCCESS) {
+      DEBUG ((DEBUG_INFO, "Mem alloc failed - 0x%x\n", EventId));
+      // TODO: Unmap secure memory before exiting to the normal world
+      return Status;
+    }
+
+    // X1 contains the VA of the normal world memory accessible from
+    // S-EL0
+    CopyMem(GuidedEventContext, (CONST VOID *) NsCommBufferAddr, NsCommBufferSize);
+  }
+
+  // Stash the pointer to the allocated Event Context for this CPU
+  PerCpuGuidedEventContext[CpuNumber] = GuidedEventContext;
+
+  // TODO: Populate entire entry point context with valid information
+  MmEntryPointContext.CurrentlyExecutingCpu = CpuNumber;
+  MmEntryPointContext.NumberOfCpus = mMpInformationHobData->NumberOfProcessors;
+
+  // Populate the MM system table with MP and state information
+  mMmst->CurrentlyExecutingCpu = CpuNumber;
+  mMmst->NumberOfCpus = mMpInformationHobData->NumberOfProcessors;
+  mMmst->CpuSaveStateSize = 0;
+  mMmst->CpuSaveState = NULL;
+
+  mMmEntryPoint(&MmEntryPointContext);
+
+  // Free the memory allocation done earlier and reset the per-cpu context
+  // TODO: Check for the return status of the FreePool API
+  ASSERT (GuidedEventContext);
+  CopyMem ((VOID *)NsCommBufferAddr, (CONST VOID *) GuidedEventContext, NsCommBufferSize);
+  mMmst->MmFreePool((VOID *) GuidedEventContext);
+  PerCpuGuidedEventContext[CpuNumber] = NULL;
+
+  return Status;
+}
+
+EFI_STATUS
+EFIAPI
+MmFoundationEntryRegister(
+  IN CONST EFI_MM_CONFIGURATION_PROTOCOL  *This,
+  IN EFI_MM_ENTRY_POINT                    MmEntryPoint
+  ) {
+  // store the entry point in a global
+  mMmEntryPoint = MmEntryPoint;
+  return EFI_SUCCESS;
+}
+
+/**
+  This function is the main entry point for an MM handler dispatch
+  or communicate-based callback.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
+
+  @return Status Code
+
+**/
+EFI_STATUS
+EFIAPI
+PiMmCpuTpFwRootMmiHandler (
+  IN     EFI_HANDLE               DispatchHandle,
+  IN     CONST VOID               *Context,        OPTIONAL
+  IN OUT VOID                     *CommBuffer,     OPTIONAL
+  IN OUT UINTN                    *CommBufferSize  OPTIONAL
+  )
+{
+  EFI_STATUS Status;
+  UINTN      CpuNumber;
+
+  ASSERT (Context == NULL);
+  ASSERT (CommBuffer == NULL);
+  ASSERT (CommBufferSize == NULL);
+
+  CpuNumber = mMmst->CurrentlyExecutingCpu;
+  if (!PerCpuGuidedEventContext[CpuNumber])
+    return EFI_NOT_FOUND;
+
+  DEBUG ((DEBUG_INFO, "CommBuffer - 0x%x, CommBufferSize - 0x%x\n",
+          PerCpuGuidedEventContext[CpuNumber],
+          PerCpuGuidedEventContext[CpuNumber]->MessageLength));
+
+  Status = mMmst->MmiManage(&PerCpuGuidedEventContext[CpuNumber]->HeaderGuid,
+                     NULL,
+                     PerCpuGuidedEventContext[CpuNumber]->Data,
+                     &PerCpuGuidedEventContext[CpuNumber]->MessageLength);
+
+  if (Status != EFI_SUCCESS) {
+    DEBUG ((DEBUG_WARN, "Unable to manage Guided Event - %d\n", Status));
+  }
+
+  return Status;
+}
diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c b/StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
new file mode 100644
index 0000000000..9b48ea15c1
--- /dev/null
+++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
@@ -0,0 +1,229 @@
+/** @file
+
+  Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
+  Copyright (c) 2011, ARM Limited. All rights reserved.
+  Copyright (c) 2016 HP Development Company, L.P.
+  Copyright (c) 2016-2017, ARM Limited. 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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <Base.h>
+#include <Pi/PiMmCis.h>
+#include <Library/Arm/StandaloneMmCoreEntryPoint.h>
+#include <Library/DebugLib.h>
+#include <Library/ArmSvcLib.h>
+#include <Library/ArmLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/HobLib.h>
+
+#include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT
+
+#include <Guid/ZeroGuid.h>
+#include <Guid/MmramMemoryReserve.h>
+
+
+#include "PiMmStandloneArmTfCpuDriver.h"
+
+// GUID to identify HOB with whereabouts of communication buffer with Normal
+// World
+extern EFI_GUID gEfiStandaloneMmNonSecureBufferGuid;
+
+// GUID to identify HOB where the entry point of this CPU driver will be
+// populated to allow the entry point driver to invoke it upon receipt of an
+// event
+extern EFI_GUID gEfiArmTfCpuDriverEpDescriptorGuid;
+
+//
+// Private copy of the MM system table for future use
+//
+EFI_MM_SYSTEM_TABLE *mMmst = NULL;
+
+//
+// Globals used to initialize the protocol
+//
+static EFI_HANDLE            mMmCpuHandle = NULL;
+
+EFI_STATUS
+GetGuidedHobData (
+  IN  VOID *HobList,
+  IN  CONST EFI_GUID *HobGuid,
+  OUT VOID **HobData)
+{
+  EFI_HOB_GUID_TYPE *Hob;
+
+  if (!HobList || !HobGuid || !HobData)
+    return EFI_INVALID_PARAMETER;
+
+  Hob = GetNextGuidHob (HobGuid, HobList);
+  if (!Hob)
+    return EFI_NOT_FOUND;
+
+  *HobData = GET_GUID_HOB_DATA (Hob);
+  if (!HobData)
+    return EFI_NOT_FOUND;
+
+  return EFI_SUCCESS;
+}
+
+EFI_STATUS
+PiMmStandloneArmTfCpuDriverInitialize (
+  IN EFI_HANDLE         ImageHandle,  // not actual imagehandle
+  IN EFI_MM_SYSTEM_TABLE   *SystemTable  // not actual systemtable
+  )
+{
+  ARM_TF_CPU_DRIVER_EP_DESCRIPTOR *CpuDriverEntryPointDesc;
+  EFI_CONFIGURATION_TABLE         *ConfigurationTable;
+  MP_INFORMATION_HOB_DATA         *MpInformationHobData;
+  EFI_MMRAM_DESCRIPTOR            *NsCommBufMmramRange;
+  EFI_STATUS                       Status;
+  EFI_HANDLE                       DispatchHandle;
+  UINT32                           MpInfoSize;
+  UINTN                            Index;
+  UINTN                            ArraySize;
+  VOID                            *HobStart;
+
+  ASSERT (SystemTable != NULL);
+  mMmst = SystemTable;
+
+  // publish the MM config protocol so the MM core can register its entry point
+  Status = mMmst->MmInstallProtocolInterface (&mMmCpuHandle,
+                                              &gEfiMmConfigurationProtocolGuid,
+                                              EFI_NATIVE_INTERFACE,
+                                              &mMmConfig);
+  if (EFI_ERROR(Status)) {
+    return Status;
+  }
+
+  // publish the MM CPU save state protocol
+  Status = mMmst->MmInstallProtocolInterface (&mMmCpuHandle,
+    &gEfiMmCpuProtocolGuid, EFI_NATIVE_INTERFACE, &mMmCpuState);
+  if (EFI_ERROR(Status)) {
+    return Status;
+  }
+
+  // register the root MMI handler
+  Status = mMmst->MmiHandlerRegister (PiMmCpuTpFwRootMmiHandler,
+                                      NULL,
+                                      &DispatchHandle);
+  if (EFI_ERROR(Status)) {
+    return Status;
+  }
+
+  // Retrieve the Hoblist from the MMST to extract the details of the NS
+  // communication buffer that has been reserved by S-EL1/EL3
+  ConfigurationTable = mMmst->MmConfigurationTable;
+  for (Index = 0; Index < mMmst->NumberOfTableEntries; Index++) {
+    if (CompareGuid (&gEfiHobListGuid, &(ConfigurationTable[Index].VendorGuid))) {
+      break;
+    }
+  }
+
+  // Bail out if the Hoblist could not be found
+  // TODO: This could also mean that
+  // the normal world will never interact synchronously with the MM environment
+  if (Index >= mMmst->NumberOfTableEntries) {
+    DEBUG ((DEBUG_INFO, "Hoblist not found - 0x%x\n", Index));
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  HobStart = ConfigurationTable[Index].VendorTable;
+
+  //
+  // Locate the HOB with the buffer to populate the entry point of this driver
+  //
+  Status = GetGuidedHobData (
+            HobStart,
+            &gEfiArmTfCpuDriverEpDescriptorGuid,
+            (VOID **) &CpuDriverEntryPointDesc);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_INFO, "ArmTfCpuDriverEpDesc HOB data extraction failed - 0x%x\n", Status));
+    return Status;
+  }
+
+  // Share the entry point of the CPU driver
+  DEBUG ((DEBUG_INFO, "Sharing Cpu Driver EP *0x%lx = 0x%lx\n",
+    (UINT64) CpuDriverEntryPointDesc->ArmTfCpuDriverEpPtr,
+    (UINT64) PiMmStandloneArmTfCpuDriverEntry));
+  *(CpuDriverEntryPointDesc->ArmTfCpuDriverEpPtr) = PiMmStandloneArmTfCpuDriverEntry;
+
+  // Find the descriptor that contains the whereabouts of the buffer for
+  // communication with the Normal world.
+  Status = GetGuidedHobData (
+            HobStart,
+            &gEfiStandaloneMmNonSecureBufferGuid,
+            (VOID **) &NsCommBufMmramRange);
+  if (EFI_ERROR(Status)) {
+    DEBUG ((DEBUG_INFO, "NsCommBufMmramRange HOB data extraction failed - 0x%x\n", Status));
+    return Status;
+  }
+
+  DEBUG ((DEBUG_INFO, "mNsCommBuffer.PhysicalStart - 0x%lx\n", (UINT64) NsCommBufMmramRange->PhysicalStart));
+  DEBUG ((DEBUG_INFO, "mNsCommBuffer.PhysicalSize - 0x%lx\n", (UINT64) NsCommBufMmramRange->PhysicalSize));
+
+  CopyMem (&mNsCommBuffer, NsCommBufMmramRange, sizeof(EFI_MMRAM_DESCRIPTOR));
+  DEBUG ((DEBUG_INFO, "mNsCommBuffer: 0x%016lx - 0x%lx\n", mNsCommBuffer.CpuStart, mNsCommBuffer.PhysicalSize));
+
+  //
+  // Extract the MP information from the Hoblist
+  //
+  Status = GetGuidedHobData (HobStart,
+                             &gMpInformationHobGuid,
+                             (VOID **) &MpInformationHobData);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_INFO, "MpInformationHob extraction failed - 0x%x\n", Status));
+    return Status;
+  }
+
+  //
+  // Allocate memory for the MP information and copy over the MP information
+  // passed by Trusted Firmware. Use the number of processors passed in the HOB
+  // to copy the processor information
+  //
+  MpInfoSize = sizeof (MP_INFORMATION_HOB_DATA) +
+               (sizeof (EFI_PROCESSOR_INFORMATION) *
+               MpInformationHobData->NumberOfProcessors);
+  Status = mMmst->MmAllocatePool (EfiRuntimeServicesData,
+                                  MpInfoSize,
+                                  (void **) &mMpInformationHobData);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_INFO, "mMpInformationHobData mem alloc failed - 0x%x\n", Status));
+    return Status;
+  }
+
+  CopyMem (mMpInformationHobData, MpInformationHobData, MpInfoSize);
+
+  // Print MP information
+  DEBUG ((DEBUG_INFO, "mMpInformationHobData: 0x%016lx - 0x%lx\n",
+          mMpInformationHobData->NumberOfProcessors,
+          mMpInformationHobData->NumberOfEnabledProcessors));
+  for (Index = 0; Index < mMpInformationHobData->NumberOfProcessors; Index++) {
+    DEBUG ((DEBUG_INFO, "mMpInformationHobData[0x%lx]: %d, %d, %d\n",
+            mMpInformationHobData->ProcessorInfoBuffer[Index].ProcessorId,
+            mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Package,
+            mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Core,
+            mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Thread));
+  }
+
+  //
+  // Allocate memory for a table to hold pointers to a
+  // EFI_MM_COMMUNICATE_HEADER for each CPU
+  //
+  ArraySize = sizeof (EFI_MM_COMMUNICATE_HEADER *) *
+              mMpInformationHobData->NumberOfEnabledProcessors;
+  Status = mMmst->MmAllocatePool (EfiRuntimeServicesData,
+                                  ArraySize,
+                                  (VOID **) &PerCpuGuidedEventContext);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_INFO, "PerCpuGuidedEventContext mem alloc failed - 0x%x\n", Status));
+    return Status;
+  }
+  return Status;
+}
diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
new file mode 100644
index 0000000000..17cefd171c
--- /dev/null
+++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
@@ -0,0 +1,89 @@
+/** @file
+  Private header with declarations and definitions specific to the MM Standalone
+  CPU driver
+
+  Copyright (c) 2017, ARM Limited. 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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef _ARM_TF_CPU_DRIVER_H_
+#define _ARM_TF_CPU_DRIVER_H_
+
+#include <Protocol/MmCommunication.h>
+#include <Protocol/MmConfiguration.h>
+#include <Protocol/MmCpu.h>
+#include <Guid/MpInformation.h>
+
+//
+// Common declarations and definitions
+//
+#define EVENT_ID_MM_COMMUNICATE_SMC     0x10
+
+//
+// CPU driver initialization specific declarations
+//
+extern EFI_MM_SYSTEM_TABLE *mMmst;
+
+//
+// CPU State Save protocol specific declarations
+//
+extern EFI_MM_CPU_PROTOCOL mMmCpuState;
+
+EFI_STATUS
+EFIAPI
+MmReadSaveState (
+  IN CONST EFI_MM_CPU_PROTOCOL   *This,
+  IN UINTN                        Width,
+  IN EFI_MM_SAVE_STATE_REGISTER  Register,
+  IN UINTN                        CpuIndex,
+  OUT VOID                        *Buffer
+  );
+
+EFI_STATUS
+EFIAPI
+MmWriteSaveState (
+  IN CONST EFI_MM_CPU_PROTOCOL   *This,
+  IN UINTN                        Width,
+  IN EFI_MM_SAVE_STATE_REGISTER  Register,
+  IN UINTN                        CpuIndex,
+  IN CONST VOID                   *Buffer
+  );
+
+//
+// MM event handling specific declarations
+//
+extern EFI_MM_COMMUNICATE_HEADER    **PerCpuGuidedEventContext;
+extern EFI_MMRAM_DESCRIPTOR          mNsCommBuffer;
+extern MP_INFORMATION_HOB_DATA       *mMpInformationHobData;
+extern EFI_MM_CONFIGURATION_PROTOCOL mMmConfig;
+
+EFI_STATUS
+PiMmStandloneArmTfCpuDriverEntry (
+  IN UINTN EventId,
+  IN UINTN CpuNumber,
+  IN UINTN NsCommBufferAddr
+  );
+
+EFI_STATUS
+EFIAPI
+PiMmCpuTpFwRootMmiHandler (
+  IN     EFI_HANDLE               DispatchHandle,
+  IN     CONST VOID               *Context,        OPTIONAL
+  IN OUT VOID                     *CommBuffer,     OPTIONAL
+  IN OUT UINTN                    *CommBufferSize  OPTIONAL
+  );
+
+EFI_STATUS _PiMmStandloneArmTfCpuDriverEntry (
+  IN UINTN EventId,
+  IN UINTN CpuNumber,
+  IN UINTN NsCommBufferAddr
+  );
+
+#endif
diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
new file mode 100644
index 0000000000..baf6d957bb
--- /dev/null
+++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
@@ -0,0 +1,60 @@
+#/** @file
+#
+#  Standalone MM CPU driver for ARM Standard Platforms
+#
+#  Copyright (c) 2009, Apple Inc. All rights reserved.<BR>
+#  Copyright (c) 2016 HP Development Company, L.P.
+#  Copyright (c) 2017, ARM Limited. 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
+#
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+#**/
+
+[Defines]
+  INF_VERSION                    = 0x0001001A
+  BASE_NAME                      = PiMmStandloneArmTfCpuDriver
+  FILE_GUID                      = 58F7A62B-6280-42A7-BC38-10535A64A92C
+  MODULE_TYPE                    = MM_STANDALONE
+  VERSION_STRING                 = 1.0
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  ENTRY_POINT                    = PiMmStandloneArmTfCpuDriverInitialize
+
+[Sources]
+  Init.c
+  EventHandle.c
+  StateSave.c
+
+[Packages]
+  ArmPkg/ArmPkg.dec
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  StandaloneMmPkg/StandaloneMmPkg.dec
+
+[LibraryClasses]
+  ArmLib
+  ArmSvcLib
+  BaseMemoryLib
+  DebugLib
+  HobLib
+  StandaloneMmDriverEntryPoint
+
+[Protocols]
+  gEfiMmConfigurationProtocolGuid                        # PROTOCOL ALWAYS_PRODUCED
+  gEfiMmCpuProtocolGuid                                  # PROTOCOL ALWAYS_PRODUCED
+
+[Guids]
+  gEfiHobListGuid
+  gEfiMmPeiMmramMemoryReserveGuid
+  gZeroGuid
+  gMpInformationHobGuid
+  gEfiStandaloneMmNonSecureBufferGuid
+  gEfiArmTfCpuDriverEpDescriptorGuid
+
+[Depex]
+  TRUE
diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c b/StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
new file mode 100644
index 0000000000..c5155e1b31
--- /dev/null
+++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
@@ -0,0 +1,51 @@
+/** @file
+
+  Copyright (c) 2016 HP Development Company, L.P.
+  Copyright (c) 2016-2017, ARM Limited. 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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <Base.h>
+#include <Pi/PiSmmCis.h>
+#include <Library/DebugLib.h>
+
+#include "PiMmStandloneArmTfCpuDriver.h"
+
+EFI_MM_CPU_PROTOCOL mMmCpuState = {
+  MmReadSaveState,
+  MmWriteSaveState
+};
+
+EFI_STATUS
+EFIAPI
+MmReadSaveState(
+  IN CONST EFI_MM_CPU_PROTOCOL   *This,
+  IN UINTN                        Width,
+  IN EFI_MM_SAVE_STATE_REGISTER  Register,
+  IN UINTN                        CpuIndex,
+  OUT VOID                        *Buffer
+  ) {
+  // todo: implement
+  return EFI_UNSUPPORTED;
+}
+
+EFI_STATUS
+EFIAPI
+MmWriteSaveState(
+  IN CONST EFI_MM_CPU_PROTOCOL   *This,
+  IN UINTN                        Width,
+  IN EFI_MM_SAVE_STATE_REGISTER  Register,
+  IN UINTN                        CpuIndex,
+  IN CONST VOID                   *Buffer
+  ) {
+  // todo: implement
+  return EFI_UNSUPPORTED;
+}
diff --git a/StandaloneMmPkg/Include/Guid/MpInformation.h b/StandaloneMmPkg/Include/Guid/MpInformation.h
new file mode 100644
index 0000000000..4e9a3c04ec
--- /dev/null
+++ b/StandaloneMmPkg/Include/Guid/MpInformation.h
@@ -0,0 +1,41 @@
+/** @file
+  EFI MP information protocol provides a lightweight MP_SERVICES_PROTOCOL.
+
+  MP information protocol only provides static information of MP processor.
+
+  Copyright (c) 2009, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef _MP_INFORMATION_H_
+#define _MP_INFORMATION_H_
+
+#include <Protocol/MpService.h>
+#include <PiPei.h>
+#include <Ppi/SecPlatformInformation.h>
+
+#define MP_INFORMATION_GUID \
+  { \
+    0xba33f15d, 0x4000, 0x45c1, {0x8e, 0x88, 0xf9, 0x16, 0x92, 0xd4, 0x57, 0xe3}  \
+  }
+
+#pragma pack(1)
+typedef struct {
+  UINT64                     NumberOfProcessors;
+  UINT64                     NumberOfEnabledProcessors;
+  EFI_PROCESSOR_INFORMATION  ProcessorInfoBuffer[];
+} MP_INFORMATION_HOB_DATA;
+#pragma pack()
+
+extern EFI_GUID gMpInformationHobGuid;
+
+#endif
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 13/18] StandaloneMmPkg/Core: Implementation of Standalone MM Core Module.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (11 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 12/18] StandaloneMmPkg/CpuMm: Add CPU driver suitable for ARM Platforms Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-30 19:19   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 14/18] StandaloneMmPkg: Describe the declaration, definition and fdf files Supreeth Venkatesh
                   ` (5 subsequent siblings)
  18 siblings, 1 reply; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

Management Mode (MM) is a generic term used to describe a secure
execution environment provided by the CPU and related silicon that is
entered when the CPU detects a MMI. For x86 systems, this can be
implemented with System Management Mode (SMM). For ARM systems, this can
be implemented with TrustZone (TZ).
A MMI can be a CPU instruction or interrupt. Upon detection of a MMI, a
CPU will jump to the MM Entry Point and save some portion of its state
(the "save state") such that execution can be resumed.
The MMI can be generated synchronously by software or asynchronously by
a hardware event. Each MMI source can be detected, cleared and disabled.
Some systems provide for special memory (Management Mode RAM or MMRAM)
which is set aside for software running in MM. Usually the MMRAM is
hidden during normal CPU execution, but this is not required. Usually,
after MMRAM is hidden it cannot be exposed until the next system reset.

The MM Core Interface Specification describes three pieces of the PI
Management Mode architecture:
1. MM Dispatch
   During DXE, the DXE Foundation works with the MM Foundation to
   schedule MM drivers for execution in the discovered firmware volumes.
2. MM Initialization
   MM related code opens MMRAM, creates the MMRAM memory map, and
   launches the MM Foundation, which provides the necessary services to
   launch MM-related drivers. Then, sometime before boot, MMRAM is
   closed and locked. This piece may be completed during the
   SEC, PEI or DXE phases.
3. MMI Management
   When an MMI generated, the MM environment is created and then the MMI

   sources are detected and MMI handlers called.

This patch implements the MM Core.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 StandaloneMmPkg/Core/Dependency.c                  |  389 +++++++
 StandaloneMmPkg/Core/Dispatcher.c                  | 1071 ++++++++++++++++++++
 StandaloneMmPkg/Core/FwVol.c                       |  104 ++
 StandaloneMmPkg/Core/Handle.c                      |  533 ++++++++++
 StandaloneMmPkg/Core/InstallConfigurationTable.c   |  178 ++++
 StandaloneMmPkg/Core/Locate.c                      |  496 +++++++++
 StandaloneMmPkg/Core/Mmi.c                         |  337 ++++++
 StandaloneMmPkg/Core/Notify.c                      |  203 ++++
 StandaloneMmPkg/Core/Page.c                        |  384 +++++++
 StandaloneMmPkg/Core/Pool.c                        |  287 ++++++
 StandaloneMmPkg/Core/StandaloneMmCore.c            |  708 +++++++++++++
 StandaloneMmPkg/Core/StandaloneMmCore.h            |  903 +++++++++++++++++
 StandaloneMmPkg/Core/StandaloneMmCore.inf          |   80 ++
 StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h |   66 ++
 StandaloneMmPkg/Include/Guid/MmFvDispatch.h        |   38 +
 StandaloneMmPkg/Include/StandaloneMm.h             |   36 +
 16 files changed, 5813 insertions(+)
 create mode 100644 StandaloneMmPkg/Core/Dependency.c
 create mode 100644 StandaloneMmPkg/Core/Dispatcher.c
 create mode 100644 StandaloneMmPkg/Core/FwVol.c
 create mode 100644 StandaloneMmPkg/Core/Handle.c
 create mode 100644 StandaloneMmPkg/Core/InstallConfigurationTable.c
 create mode 100644 StandaloneMmPkg/Core/Locate.c
 create mode 100644 StandaloneMmPkg/Core/Mmi.c
 create mode 100644 StandaloneMmPkg/Core/Notify.c
 create mode 100644 StandaloneMmPkg/Core/Page.c
 create mode 100644 StandaloneMmPkg/Core/Pool.c
 create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.c
 create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.h
 create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.inf
 create mode 100644 StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
 create mode 100644 StandaloneMmPkg/Include/Guid/MmFvDispatch.h
 create mode 100644 StandaloneMmPkg/Include/StandaloneMm.h

diff --git a/StandaloneMmPkg/Core/Dependency.c b/StandaloneMmPkg/Core/Dependency.c
new file mode 100644
index 0000000000..e501369130
--- /dev/null
+++ b/StandaloneMmPkg/Core/Dependency.c
@@ -0,0 +1,389 @@
+/** @file
+  MM Driver Dispatcher Dependency Evaluator
+
+  This routine evaluates a dependency expression (DEPENDENCY_EXPRESSION) to determine
+  if a driver can be scheduled for execution.  The criteria for
+  schedulability is that the dependency expression is satisfied.
+
+  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include "StandaloneMmCore.h"
+
+///
+/// EFI_DEP_REPLACE_TRUE - Used to dynamically patch the dependency expression
+///                        to save time.  A EFI_DEP_PUSH is evaluated one an
+///                        replaced with EFI_DEP_REPLACE_TRUE. If PI spec's Vol 2
+///                        Driver Execution Environment Core Interface use 0xff
+///                        as new DEPEX opcode. EFI_DEP_REPLACE_TRUE should be
+///                        defined to a new value that is not conflicting with PI spec.
+///
+#define EFI_DEP_REPLACE_TRUE  0xff
+
+///
+/// Define the initial size of the dependency expression evaluation stack
+///
+#define DEPEX_STACK_SIZE_INCREMENT  0x1000
+
+//
+// Global stack used to evaluate dependency expressions
+//
+BOOLEAN  *mDepexEvaluationStack        = NULL;
+BOOLEAN  *mDepexEvaluationStackEnd     = NULL;
+BOOLEAN  *mDepexEvaluationStackPointer = NULL;
+
+/**
+  Grow size of the Depex stack
+
+  @retval EFI_SUCCESS           Stack successfully growed.
+  @retval EFI_OUT_OF_RESOURCES  There is not enough system memory to grow the stack.
+
+**/
+EFI_STATUS
+GrowDepexStack (
+  VOID
+  )
+{
+  BOOLEAN     *NewStack;
+  UINTN       Size;
+
+  Size = DEPEX_STACK_SIZE_INCREMENT;
+  if (mDepexEvaluationStack != NULL) {
+    Size = Size + (mDepexEvaluationStackEnd - mDepexEvaluationStack);
+  }
+
+  NewStack = AllocatePool (Size * sizeof (BOOLEAN));
+  if (NewStack == NULL) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  if (mDepexEvaluationStack != NULL) {
+    //
+    // Copy to Old Stack to the New Stack
+    //
+    CopyMem (
+      NewStack,
+      mDepexEvaluationStack,
+      (mDepexEvaluationStackEnd - mDepexEvaluationStack) * sizeof (BOOLEAN)
+      );
+
+    //
+    // Free The Old Stack
+    //
+    FreePool (mDepexEvaluationStack);
+  }
+
+  //
+  // Make the Stack pointer point to the old data in the new stack
+  //
+  mDepexEvaluationStackPointer = NewStack + (mDepexEvaluationStackPointer - mDepexEvaluationStack);
+  mDepexEvaluationStack        = NewStack;
+  mDepexEvaluationStackEnd     = NewStack + Size;
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Push an element onto the Boolean Stack.
+
+  @param  Value                 BOOLEAN to push.
+
+  @retval EFI_SUCCESS           The value was pushed onto the stack.
+  @retval EFI_OUT_OF_RESOURCES  There is not enough system memory to grow the stack.
+
+**/
+EFI_STATUS
+PushBool (
+  IN BOOLEAN  Value
+  )
+{
+  EFI_STATUS  Status;
+
+  //
+  // Check for a stack overflow condition
+  //
+  if (mDepexEvaluationStackPointer == mDepexEvaluationStackEnd) {
+    //
+    // Grow the stack
+    //
+    Status = GrowDepexStack ();
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
+  }
+
+  //
+  // Push the item onto the stack
+  //
+  *mDepexEvaluationStackPointer = Value;
+  mDepexEvaluationStackPointer++;
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Pop an element from the Boolean stack.
+
+  @param  Value                 BOOLEAN to pop.
+
+  @retval EFI_SUCCESS           The value was popped onto the stack.
+  @retval EFI_ACCESS_DENIED     The pop operation underflowed the stack.
+
+**/
+EFI_STATUS
+PopBool (
+  OUT BOOLEAN  *Value
+  )
+{
+  //
+  // Check for a stack underflow condition
+  //
+  if (mDepexEvaluationStackPointer == mDepexEvaluationStack) {
+    return EFI_ACCESS_DENIED;
+  }
+
+  //
+  // Pop the item off the stack
+  //
+  mDepexEvaluationStackPointer--;
+  *Value = *mDepexEvaluationStackPointer;
+  return EFI_SUCCESS;
+}
+
+/**
+  This is the POSTFIX version of the dependency evaluator.  This code does
+  not need to handle Before or After, as it is not valid to call this
+  routine in this case. POSTFIX means all the math is done on top of the stack.
+
+  @param  DriverEntry           DriverEntry element to update.
+
+  @retval TRUE                  If driver is ready to run.
+  @retval FALSE                 If driver is not ready to run or some fatal error
+                                was found.
+
+**/
+BOOLEAN
+MmIsSchedulable (
+  IN  EFI_MM_DRIVER_ENTRY   *DriverEntry
+  )
+{
+  EFI_STATUS  Status;
+  UINT8       *Iterator;
+  BOOLEAN     Operator;
+  BOOLEAN     Operator2;
+  EFI_GUID    DriverGuid;
+  VOID        *Interface;
+
+  Operator = FALSE;
+  Operator2 = FALSE;
+
+  if (DriverEntry->After || DriverEntry->Before) {
+    //
+    // If Before or After Depex skip as MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
+    // processes them.
+    //
+    return FALSE;
+  }
+
+  DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
+
+  if (DriverEntry->Depex == NULL) {
+    //
+    // A NULL Depex means that the MM driver is not built correctly.
+    // All MM drivers must have a valid depex expressiion.
+    //
+    DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Depex is empty)\n"));
+    ASSERT (FALSE);
+    return FALSE;
+  }
+
+  //
+  // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
+  // incorrectly formed DEPEX expressions
+  //
+  mDepexEvaluationStackPointer = mDepexEvaluationStack;
+
+
+  Iterator = DriverEntry->Depex;
+
+  while (TRUE) {
+    //
+    // Check to see if we are attempting to fetch dependency expression instructions
+    // past the end of the dependency expression.
+    //
+    if (((UINTN)Iterator - (UINTN)DriverEntry->Depex) >= DriverEntry->DepexSize) {
+      DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Attempt to fetch past end of depex)\n"));
+      return FALSE;
+    }
+
+    //
+    // Look at the opcode of the dependency expression instruction.
+    //
+    switch (*Iterator) {
+    case EFI_DEP_BEFORE:
+    case EFI_DEP_AFTER:
+      //
+      // For a well-formed Dependency Expression, the code should never get here.
+      // The BEFORE and AFTER are processed prior to this routine's invocation.
+      // If the code flow arrives at this point, there was a BEFORE or AFTER
+      // that were not the first opcodes.
+      //
+      DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));
+      ASSERT (FALSE);
+
+    case EFI_DEP_PUSH:
+      //
+      // Push operator is followed by a GUID. Test to see if the GUID protocol
+      // is installed and push the boolean result on the stack.
+      //
+      CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));
+
+      Status = MmLocateProtocol (&DriverGuid, NULL, &Interface);
+      if (EFI_ERROR (Status) && (mEfiSystemTable != NULL)) {
+        //
+        // For MM Driver, it may depend on uefi protocols
+        //
+        Status = mEfiSystemTable->BootServices->LocateProtocol (&DriverGuid, NULL, &Interface);
+      }
+
+      if (EFI_ERROR (Status)) {
+        DEBUG ((DEBUG_DISPATCH, "  PUSH GUID(%g) = FALSE\n", &DriverGuid));
+        Status = PushBool (FALSE);
+      } else {
+        DEBUG ((DEBUG_DISPATCH, "  PUSH GUID(%g) = TRUE\n", &DriverGuid));
+        *Iterator = EFI_DEP_REPLACE_TRUE;
+        Status = PushBool (TRUE);
+      }
+      if (EFI_ERROR (Status)) {
+        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
+        return FALSE;
+      }
+
+      Iterator += sizeof (EFI_GUID);
+      break;
+
+    case EFI_DEP_AND:
+      DEBUG ((DEBUG_DISPATCH, "  AND\n"));
+      Status = PopBool (&Operator);
+      if (EFI_ERROR (Status)) {
+        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
+        return FALSE;
+      }
+
+      Status = PopBool (&Operator2);
+      if (EFI_ERROR (Status)) {
+        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
+        return FALSE;
+      }
+
+      Status = PushBool ((BOOLEAN)(Operator && Operator2));
+      if (EFI_ERROR (Status)) {
+        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
+        return FALSE;
+      }
+      break;
+
+    case EFI_DEP_OR:
+      DEBUG ((DEBUG_DISPATCH, "  OR\n"));
+      Status = PopBool (&Operator);
+      if (EFI_ERROR (Status)) {
+        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
+        return FALSE;
+      }
+
+      Status = PopBool (&Operator2);
+      if (EFI_ERROR (Status)) {
+        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
+        return FALSE;
+      }
+
+      Status = PushBool ((BOOLEAN)(Operator || Operator2));
+      if (EFI_ERROR (Status)) {
+        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
+        return FALSE;
+      }
+      break;
+
+    case EFI_DEP_NOT:
+      DEBUG ((DEBUG_DISPATCH, "  NOT\n"));
+      Status = PopBool (&Operator);
+      if (EFI_ERROR (Status)) {
+        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
+        return FALSE;
+      }
+
+      Status = PushBool ((BOOLEAN)(!Operator));
+      if (EFI_ERROR (Status)) {
+        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
+        return FALSE;
+      }
+      break;
+
+    case EFI_DEP_TRUE:
+      DEBUG ((DEBUG_DISPATCH, "  TRUE\n"));
+      Status = PushBool (TRUE);
+      if (EFI_ERROR (Status)) {
+        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
+        return FALSE;
+      }
+      break;
+
+    case EFI_DEP_FALSE:
+      DEBUG ((DEBUG_DISPATCH, "  FALSE\n"));
+      Status = PushBool (FALSE);
+      if (EFI_ERROR (Status)) {
+        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
+        return FALSE;
+      }
+      break;
+
+    case EFI_DEP_END:
+      DEBUG ((DEBUG_DISPATCH, "  END\n"));
+      Status = PopBool (&Operator);
+      if (EFI_ERROR (Status)) {
+        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
+        return FALSE;
+      }
+      DEBUG ((DEBUG_DISPATCH, "  RESULT = %a\n", Operator ? "TRUE" : "FALSE"));
+      return Operator;
+
+    case EFI_DEP_REPLACE_TRUE:
+      CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));
+      DEBUG ((DEBUG_DISPATCH, "  PUSH GUID(%g) = TRUE\n", &DriverGuid));
+      Status = PushBool (TRUE);
+      if (EFI_ERROR (Status)) {
+        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
+        return FALSE;
+      }
+
+      Iterator += sizeof (EFI_GUID);
+      break;
+
+    default:
+      DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unknown opcode)\n"));
+      goto Done;
+    }
+
+    //
+    // Skip over the Dependency Op Code we just processed in the switch.
+    // The math is done out of order, but it should not matter. That is
+    // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
+    // This is not an issue, since we just need the correct end result. You
+    // need to be careful using Iterator in the loop as it's intermediate value
+    // may be strange.
+    //
+    Iterator++;
+  }
+
+Done:
+  return FALSE;
+}
diff --git a/StandaloneMmPkg/Core/Dispatcher.c b/StandaloneMmPkg/Core/Dispatcher.c
new file mode 100644
index 0000000000..af18fa7eaa
--- /dev/null
+++ b/StandaloneMmPkg/Core/Dispatcher.c
@@ -0,0 +1,1071 @@
+/** @file
+  MM Driver Dispatcher.
+
+  Step #1 - When a FV protocol is added to the system every driver in the FV
+            is added to the mDiscoveredList. The Before, and After Depex are
+            pre-processed as drivers are added to the mDiscoveredList. If an Apriori
+            file exists in the FV those drivers are addeded to the
+            mScheduledQueue. The mFvHandleList is used to make sure a
+            FV is only processed once.
+
+  Step #2 - Dispatch. Remove driver from the mScheduledQueue and load and
+            start it. After mScheduledQueue is drained check the
+            mDiscoveredList to see if any item has a Depex that is ready to
+            be placed on the mScheduledQueue.
+
+  Step #3 - Adding to the mScheduledQueue requires that you process Before
+            and After dependencies. This is done recursively as the call to add
+            to the mScheduledQueue checks for Before and recursively adds
+            all Befores. It then addes the item that was passed in and then
+            processess the After dependecies by recursively calling the routine.
+
+  Dispatcher Rules:
+  The rules for the dispatcher are similar to the DXE dispatcher.
+
+  The rules for DXE dispatcher are in chapter 10 of the DXE CIS. Figure 10-3
+  is the state diagram for the DXE dispatcher
+
+  Depex - Dependency Expresion.
+
+  Copyright (c) 2014, Hewlett-Packard Development Company, L.P.
+  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include "StandaloneMmCore.h"
+
+//
+// MM Dispatcher Data structures
+//
+#define KNOWN_HANDLE_SIGNATURE  SIGNATURE_32('k','n','o','w')
+typedef struct {
+  UINTN           Signature;
+  LIST_ENTRY      Link;         // mFvHandleList
+  EFI_HANDLE      Handle;
+} KNOWN_HANDLE;
+
+//
+// Function Prototypes
+//
+
+EFI_STATUS
+MmCoreFfsFindMmDriver (
+  IN  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader
+  );
+
+/**
+  Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
+  must add any driver with a before dependency on InsertedDriverEntry first.
+  You do this by recursively calling this routine. After all the Befores are
+  processed you can add InsertedDriverEntry to the mScheduledQueue.
+  Then you can add any driver with an After dependency on InsertedDriverEntry
+  by recursively calling this routine.
+
+  @param  InsertedDriverEntry   The driver to insert on the ScheduledLink Queue
+
+**/
+VOID
+MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
+  IN  EFI_MM_DRIVER_ENTRY   *InsertedDriverEntry
+  );
+
+//
+// The Driver List contains one copy of every driver that has been discovered.
+// Items are never removed from the driver list. List of EFI_MM_DRIVER_ENTRY
+//
+LIST_ENTRY  mDiscoveredList = INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList);
+
+//
+// Queue of drivers that are ready to dispatch. This queue is a subset of the
+// mDiscoveredList.list of EFI_MM_DRIVER_ENTRY.
+//
+LIST_ENTRY  mScheduledQueue = INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue);
+
+//
+// List of handles who's Fv's have been parsed and added to the mFwDriverList.
+//
+LIST_ENTRY  mFvHandleList = INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList);
+
+//
+// Flag for the MM Dispacher.  TRUE if dispatcher is execuing.
+//
+BOOLEAN  gDispatcherRunning = FALSE;
+
+//
+// Flag for the MM Dispacher.  TRUE if there is one or more MM drivers ready to be dispatched
+//
+BOOLEAN  gRequestDispatch = FALSE;
+
+//
+// The global variable is defined for Loading modules at fixed address feature to track the MM code
+// memory range usage. It is a bit mapped array in which every bit indicates the correspoding
+// memory page available or not.
+//
+GLOBAL_REMOVE_IF_UNREFERENCED    UINT64                *mMmCodeMemoryRangeUsageBitMap=NULL;
+
+/**
+  To check memory usage bit map array to figure out if the memory range in which the image will be loaded is available or not. If
+  memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.
+  The function is only invoked when load modules at fixed address feature is enabled.
+
+  @param  ImageBase                The base addres the image will be loaded at.
+  @param  ImageSize                The size of the image
+
+  @retval EFI_SUCCESS              The memory range the image will be loaded in is available
+  @retval EFI_NOT_FOUND            The memory range the image will be loaded in is not available
+**/
+EFI_STATUS
+CheckAndMarkFixLoadingMemoryUsageBitMap (
+  IN  EFI_PHYSICAL_ADDRESS          ImageBase,
+  IN  UINTN                         ImageSize
+  )
+{
+   UINT32                             MmCodePageNumber;
+   UINT64                             MmCodeSize;
+   EFI_PHYSICAL_ADDRESS               MmCodeBase;
+   UINTN                              BaseOffsetPageNumber;
+   UINTN                              TopOffsetPageNumber;
+   UINTN                              Index;
+   //
+   // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressMmCodePageNumber
+   //
+   MmCodePageNumber = 0;
+   MmCodeSize = EFI_PAGES_TO_SIZE (MmCodePageNumber);
+   MmCodeBase = gLoadModuleAtFixAddressMmramBase;
+
+   //
+   // If the memory usage bit map is not initialized,  do it. Every bit in the array
+   // indicate the status of the corresponding memory page, available or not
+   //
+   if (mMmCodeMemoryRangeUsageBitMap == NULL) {
+     mMmCodeMemoryRangeUsageBitMap = AllocateZeroPool(((MmCodePageNumber / 64) + 1)*sizeof(UINT64));
+   }
+   //
+   // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
+   //
+   if (mMmCodeMemoryRangeUsageBitMap == NULL) {
+     return EFI_NOT_FOUND;
+   }
+   //
+   // see if the memory range for loading the image is in the MM code range.
+   //
+   if (MmCodeBase + MmCodeSize <  ImageBase + ImageSize || MmCodeBase >  ImageBase) {
+     return EFI_NOT_FOUND;
+   }
+   //
+   // Test if the memory is avalaible or not.
+   //
+   BaseOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase - MmCodeBase));
+   TopOffsetPageNumber  = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - MmCodeBase));
+   for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
+     if ((mMmCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {
+       //
+       // This page is already used.
+       //
+       return EFI_NOT_FOUND;
+     }
+   }
+
+   //
+   // Being here means the memory range is available.  So mark the bits for the memory range
+   //
+   for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
+     mMmCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));
+   }
+   return  EFI_SUCCESS;
+}
+/**
+  Get the fixed loading address from image header assigned by build tool. This function only be called
+  when Loading module at Fixed address feature enabled.
+
+  @param  ImageContext              Pointer to the image context structure that describes the PE/COFF
+                                    image that needs to be examined by this function.
+  @retval EFI_SUCCESS               An fixed loading address is assigned to this image by build tools .
+  @retval EFI_NOT_FOUND             The image has no assigned fixed loadding address.
+
+**/
+EFI_STATUS
+GetPeCoffImageFixLoadingAssignedAddress(
+  IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext
+  )
+{
+  UINTN                              SectionHeaderOffset;
+  EFI_STATUS                         Status;
+  EFI_IMAGE_SECTION_HEADER           SectionHeader;
+  EFI_IMAGE_OPTIONAL_HEADER_UNION    *ImgHdr;
+  EFI_PHYSICAL_ADDRESS               FixLoadingAddress;
+  UINT16                             Index;
+  UINTN                              Size;
+  UINT16                             NumberOfSections;
+  UINT64                             ValueInSectionHeader;
+
+  FixLoadingAddress = 0;
+  Status = EFI_NOT_FOUND;
+
+  //
+  // Get PeHeader pointer
+  //
+  ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
+  SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +
+                        sizeof (UINT32) +
+                        sizeof (EFI_IMAGE_FILE_HEADER) +
+                        ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;
+  NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
+
+  //
+  // Get base address from the first section header that doesn't point to code section.
+  //
+  for (Index = 0; Index < NumberOfSections; Index++) {
+    //
+    // Read section header from file
+    //
+    Size = sizeof (EFI_IMAGE_SECTION_HEADER);
+    Status = ImageContext->ImageRead (
+                              ImageContext->Handle,
+                              SectionHeaderOffset,
+                              &Size,
+                              &SectionHeader
+                              );
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
+
+    Status = EFI_NOT_FOUND;
+
+    if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
+      //
+      // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
+      // that doesn't point to code section in image header.So there is an assumption that when the feature is enabled,
+      // if a module with a loading address assigned by tools, the PointerToRelocations & PointerToLineNumbers fields
+      // should not be Zero, or else, these 2 fields should be set to Zero
+      //
+      ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);
+      if (ValueInSectionHeader != 0) {
+        //
+        // Found first section header that doesn't point to code section in which build tool saves the
+        // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
+        //
+        FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressMmramBase + (INT64)ValueInSectionHeader);
+        //
+        // Check if the memory range is available.
+        //
+        Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));
+        if (!EFI_ERROR(Status)) {
+          //
+          // The assigned address is valid. Return the specified loading address
+          //
+          ImageContext->ImageAddress = FixLoadingAddress;
+        }
+      }
+      break;
+    }
+    SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
+  }
+  DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n", FixLoadingAddress, Status));
+  return Status;
+}
+/**
+  Loads an EFI image into SMRAM.
+
+  @param  DriverEntry             EFI_MM_DRIVER_ENTRY instance
+
+  @return EFI_STATUS
+
+**/
+EFI_STATUS
+EFIAPI
+MmLoadImage (
+  IN OUT EFI_MM_DRIVER_ENTRY  *DriverEntry
+  )
+{
+  VOID                           *Buffer;
+  UINTN                          PageCount;
+  EFI_STATUS                     Status;
+  EFI_PHYSICAL_ADDRESS           DstBuffer;
+  PE_COFF_LOADER_IMAGE_CONTEXT   ImageContext;
+
+  DEBUG ((DEBUG_INFO, "MmLoadImage - %g\n", &DriverEntry->FileName));
+
+  Buffer = AllocateCopyPool (DriverEntry->Pe32DataSize, DriverEntry->Pe32Data);
+  if (Buffer == NULL) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  Status               = EFI_SUCCESS;
+
+  //
+  // Initialize ImageContext
+  //
+  ImageContext.Handle = Buffer;
+  ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
+
+  //
+  // Get information about the image being loaded
+  //
+  Status = PeCoffLoaderGetImageInfo (&ImageContext);
+  if (EFI_ERROR (Status)) {
+    if (Buffer != NULL) {
+      MmFreePool (Buffer);
+    }
+    return Status;
+  }
+
+  PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
+  DstBuffer = (UINTN)(-1);
+
+  Status = MmAllocatePages (
+              AllocateMaxAddress,
+              EfiRuntimeServicesCode,
+              PageCount,
+              &DstBuffer
+              );
+  if (EFI_ERROR (Status)) {
+    if (Buffer != NULL) {
+      MmFreePool (Buffer);
+    }
+    return Status;
+  }
+
+  ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;
+
+  //
+  // Align buffer on section boundry
+  //
+  ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
+  ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)(ImageContext.SectionAlignment - 1));
+
+  //
+  // Load the image to our new buffer
+  //
+  Status = PeCoffLoaderLoadImage (&ImageContext);
+  if (EFI_ERROR (Status)) {
+    if (Buffer != NULL) {
+      MmFreePool (Buffer);
+    }
+    MmFreePages (DstBuffer, PageCount);
+    return Status;
+  }
+
+  //
+  // Relocate the image in our new buffer
+  //
+  Status = PeCoffLoaderRelocateImage (&ImageContext);
+  if (EFI_ERROR (Status)) {
+    if (Buffer != NULL) {
+      MmFreePool (Buffer);
+    }
+    MmFreePages (DstBuffer, PageCount);
+    return Status;
+  }
+
+  //
+  // Flush the instruction cache so the image data are written before we execute it
+  //
+  InvalidateInstructionCacheRange ((VOID *)(UINTN) ImageContext.ImageAddress, (UINTN) ImageContext.ImageSize);
+
+  //
+  // Save Image EntryPoint in DriverEntry
+  //
+  DriverEntry->ImageEntryPoint  = ImageContext.EntryPoint;
+  DriverEntry->ImageBuffer      = DstBuffer;
+  DriverEntry->NumberOfPage     = PageCount;
+
+  if (mEfiSystemTable != NULL) {
+    Status = mEfiSystemTable->BootServices->AllocatePool (EfiBootServicesData, sizeof (EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&DriverEntry->LoadedImage);
+    if (EFI_ERROR (Status)) {
+      if (Buffer != NULL) {
+        MmFreePool (Buffer);
+      }
+      MmFreePages (DstBuffer, PageCount);
+      return Status;
+    }
+
+    ZeroMem (DriverEntry->LoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));
+    //
+    // Fill in the remaining fields of the Loaded Image Protocol instance.
+    // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
+    //
+    DriverEntry->LoadedImage->Revision      = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
+    DriverEntry->LoadedImage->ParentHandle  = NULL;
+    DriverEntry->LoadedImage->SystemTable   = mEfiSystemTable;
+    DriverEntry->LoadedImage->DeviceHandle  = NULL;
+    DriverEntry->LoadedImage->FilePath      = NULL;
+
+    DriverEntry->LoadedImage->ImageBase     = (VOID *)(UINTN)DriverEntry->ImageBuffer;
+    DriverEntry->LoadedImage->ImageSize     = ImageContext.ImageSize;
+    DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;
+    DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;
+
+    //
+    // Create a new image handle in the UEFI handle database for the MM Driver
+    //
+    DriverEntry->ImageHandle = NULL;
+    Status = mEfiSystemTable->BootServices->InstallMultipleProtocolInterfaces (
+                    &DriverEntry->ImageHandle,
+                    &gEfiLoadedImageProtocolGuid, DriverEntry->LoadedImage,
+                    NULL
+                    );
+  }
+
+  //
+  // Print the load address and the PDB file name if it is available
+  //
+
+  DEBUG_CODE_BEGIN ();
+
+    UINTN Index;
+    UINTN StartIndex;
+    CHAR8 EfiFileName[256];
+
+
+    DEBUG ((DEBUG_INFO | DEBUG_LOAD,
+           "Loading MM driver at 0x%11p EntryPoint=0x%11p ",
+           (VOID *)(UINTN) ImageContext.ImageAddress,
+           FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));
+
+
+    //
+    // Print Module Name by Pdb file path.
+    // Windows and Unix style file path are all trimmed correctly.
+    //
+    if (ImageContext.PdbPointer != NULL) {
+      StartIndex = 0;
+      for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {
+        if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {
+          StartIndex = Index + 1;
+        }
+      }
+      //
+      // Copy the PDB file name to our temporary string, and replace .pdb with .efi
+      // The PDB file name is limited in the range of 0~255.
+      // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
+      //
+      for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {
+        EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];
+        if (EfiFileName[Index] == 0) {
+          EfiFileName[Index] = '.';
+        }
+        if (EfiFileName[Index] == '.') {
+          EfiFileName[Index + 1] = 'e';
+          EfiFileName[Index + 2] = 'f';
+          EfiFileName[Index + 3] = 'i';
+          EfiFileName[Index + 4] = 0;
+          break;
+        }
+      }
+
+      if (Index == sizeof (EfiFileName) - 4) {
+        EfiFileName[Index] = 0;
+      }
+      DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));
+    }
+    DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));
+
+  DEBUG_CODE_END ();
+
+  //
+  // Free buffer allocated by Fv->ReadSection.
+  //
+  // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
+  // used the UEFI Boot Services AllocatePool() function
+  //
+  MmFreePool(Buffer);
+  return Status;
+}
+
+/**
+  Preprocess dependency expression and update DriverEntry to reflect the
+  state of  Before and After dependencies. If DriverEntry->Before
+  or DriverEntry->After is set it will never be cleared.
+
+  @param  DriverEntry           DriverEntry element to update .
+
+  @retval EFI_SUCCESS           It always works.
+
+**/
+EFI_STATUS
+MmPreProcessDepex (
+  IN EFI_MM_DRIVER_ENTRY  *DriverEntry
+  )
+{
+  UINT8  *Iterator;
+
+  Iterator = DriverEntry->Depex;
+  DriverEntry->Dependent = TRUE;
+
+  if (*Iterator == EFI_DEP_BEFORE) {
+    DriverEntry->Before = TRUE;
+  } else if (*Iterator == EFI_DEP_AFTER) {
+    DriverEntry->After = TRUE;
+  }
+
+  if (DriverEntry->Before || DriverEntry->After) {
+    CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));
+  }
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Read Depex and pre-process the Depex for Before and After. If Section Extraction
+  protocol returns an error via ReadSection defer the reading of the Depex.
+
+  @param  DriverEntry           Driver to work on.
+
+  @retval EFI_SUCCESS           Depex read and preprossesed
+  @retval EFI_PROTOCOL_ERROR    The section extraction protocol returned an error
+                                and  Depex reading needs to be retried.
+  @retval Error                 DEPEX not found.
+
+**/
+EFI_STATUS
+MmGetDepexSectionAndPreProccess (
+  IN EFI_MM_DRIVER_ENTRY  *DriverEntry
+  )
+{
+  EFI_STATUS                     Status;
+
+  //
+  // Data already read
+  //
+  if (DriverEntry->Depex == NULL) {
+    Status = EFI_NOT_FOUND;
+  } else {
+    Status = EFI_SUCCESS;
+  }
+  if (EFI_ERROR (Status)) {
+    if (Status == EFI_PROTOCOL_ERROR) {
+      //
+      // The section extraction protocol failed so set protocol error flag
+      //
+      DriverEntry->DepexProtocolError = TRUE;
+    } else {
+      //
+      // If no Depex assume depend on all architectural protocols
+      //
+      DriverEntry->Depex = NULL;
+      DriverEntry->Dependent = TRUE;
+      DriverEntry->DepexProtocolError = FALSE;
+    }
+  } else {
+    //
+    // Set Before and After state information based on Depex
+    // Driver will be put in Dependent state
+    //
+    MmPreProcessDepex (DriverEntry);
+    DriverEntry->DepexProtocolError = FALSE;
+  }
+
+  return Status;
+}
+
+/**
+  This is the main Dispatcher for MM and it exits when there are no more
+  drivers to run. Drain the mScheduledQueue and load and start a PE
+  image for each driver. Search the mDiscoveredList to see if any driver can
+  be placed on the mScheduledQueue. If no drivers are placed on the
+  mScheduledQueue exit the function.
+
+  @retval EFI_SUCCESS           All of the MM Drivers that could be dispatched
+                                have been run and the MM Entry Point has been
+                                registered.
+  @retval EFI_NOT_READY         The MM Driver that registered the MM Entry Point
+                                was just dispatched.
+  @retval EFI_NOT_FOUND         There are no MM Drivers available to be dispatched.
+  @retval EFI_ALREADY_STARTED   The MM Dispatcher is already running
+
+**/
+EFI_STATUS
+MmDispatcher (
+  VOID
+  )
+{
+  EFI_STATUS            Status;
+  LIST_ENTRY            *Link;
+  EFI_MM_DRIVER_ENTRY  *DriverEntry;
+  BOOLEAN               ReadyToRun;
+  BOOLEAN               PreviousMmEntryPointRegistered;
+
+  DEBUG ((DEBUG_INFO, "MmDispatcher\n"));
+
+  if (!gRequestDispatch) {
+    DEBUG ((DEBUG_INFO, "  !gRequestDispatch\n"));
+    return EFI_NOT_FOUND;
+  }
+
+  if (gDispatcherRunning) {
+    DEBUG ((DEBUG_INFO, "  gDispatcherRunning\n"));
+    //
+    // If the dispatcher is running don't let it be restarted.
+    //
+    return EFI_ALREADY_STARTED;
+  }
+
+  gDispatcherRunning = TRUE;
+
+  do {
+    //
+    // Drain the Scheduled Queue
+    //
+    DEBUG ((DEBUG_INFO, "  Drain the Scheduled Queue\n"));
+    while (!IsListEmpty (&mScheduledQueue)) {
+      DriverEntry = CR (
+                      mScheduledQueue.ForwardLink,
+                      EFI_MM_DRIVER_ENTRY,
+                      ScheduledLink,
+                      EFI_MM_DRIVER_ENTRY_SIGNATURE
+                      );
+      DEBUG ((DEBUG_INFO, "  DriverEntry (Scheduled) - %g\n", &DriverEntry->FileName));
+
+      //
+      // Load the MM Driver image into memory. If the Driver was transitioned from
+      // Untrused to Scheduled it would have already been loaded so we may need to
+      // skip the LoadImage
+      //
+      if (DriverEntry->ImageHandle == NULL) {
+        Status = MmLoadImage (DriverEntry);
+
+        //
+        // Update the driver state to reflect that it's been loaded
+        //
+        if (EFI_ERROR (Status)) {
+          //
+          // The MM Driver could not be loaded, and do not attempt to load or start it again.
+          // Take driver from Scheduled to Initialized.
+          //
+          DriverEntry->Initialized  = TRUE;
+          DriverEntry->Scheduled = FALSE;
+          RemoveEntryList (&DriverEntry->ScheduledLink);
+
+          //
+          // If it's an error don't try the StartImage
+          //
+          continue;
+        }
+      }
+
+      DriverEntry->Scheduled    = FALSE;
+      DriverEntry->Initialized  = TRUE;
+      RemoveEntryList (&DriverEntry->ScheduledLink);
+
+      /*REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
+        EFI_PROGRESS_CODE,
+        EFI_SOFTWARE_MM_DRIVER | EFI_SW_PC_INIT_BEGIN,
+        &DriverEntry->ImageHandle,
+        sizeof (DriverEntry->ImageHandle)
+        );*/
+
+      //
+      // Cache state of MmEntryPointRegistered before calling entry point
+      //
+      PreviousMmEntryPointRegistered = gMmCorePrivate->MmEntryPointRegistered;
+
+      //
+      // For each MM driver, pass NULL as ImageHandle
+      //
+      if (mEfiSystemTable == NULL) {
+        DEBUG ((DEBUG_INFO, "StartImage - 0x%x (Standalone Mode)\n", DriverEntry->ImageEntryPoint));
+        Status = ((MM_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, &gMmCoreMmst);
+      } else {
+        DEBUG ((DEBUG_INFO, "StartImage - 0x%x (Tradition Mode)\n", DriverEntry->ImageEntryPoint));
+        Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, mEfiSystemTable);
+      }
+      if (EFI_ERROR(Status)){
+        DEBUG ((DEBUG_INFO, "StartImage Status - %r\n", Status));
+        MmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);
+      }
+
+      /*REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
+        EFI_PROGRESS_CODE,
+        EFI_SOFTWARE_MM_DRIVER | EFI_SW_PC_INIT_END,
+        &DriverEntry->ImageHandle,
+        sizeof (DriverEntry->ImageHandle)
+        );*/
+
+      if (!PreviousMmEntryPointRegistered && gMmCorePrivate->MmEntryPointRegistered) {
+        //
+        // Return immediately if the MM Entry Point was registered by the MM
+        // Driver that was just dispatched.  The MM IPL will reinvoke the MM
+        // Core Dispatcher.  This is required so MM Mode may be enabled as soon
+        // as all the dependent MM Drivers for MM Mode have been dispatched.
+        // Once the MM Entry Point has been registered, then MM Mode will be
+        // used.
+        //
+        gRequestDispatch = TRUE;
+        gDispatcherRunning = FALSE;
+        return EFI_NOT_READY;
+      }
+    }
+
+    //
+    // Search DriverList for items to place on Scheduled Queue
+    //
+    DEBUG ((DEBUG_INFO, "  Search DriverList for items to place on Scheduled Queue\n"));
+    ReadyToRun = FALSE;
+    for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
+      DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
+      DEBUG ((DEBUG_INFO, "  DriverEntry (Discovered) - %g\n", &DriverEntry->FileName));
+
+      if (DriverEntry->DepexProtocolError){
+        //
+        // If Section Extraction Protocol did not let the Depex be read before retry the read
+        //
+        Status = MmGetDepexSectionAndPreProccess (DriverEntry);
+      }
+
+      if (DriverEntry->Dependent) {
+        if (MmIsSchedulable (DriverEntry)) {
+          MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
+          ReadyToRun = TRUE;
+        }
+      }
+    }
+  } while (ReadyToRun);
+
+  //
+  // If there is no more MM driver to dispatch, stop the dispatch request
+  //
+  DEBUG ((DEBUG_INFO, "  no more MM driver to dispatch, stop the dispatch request\n"));
+  gRequestDispatch = FALSE;
+  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
+    DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
+    DEBUG ((DEBUG_INFO, "  DriverEntry (Discovered) - %g\n", &DriverEntry->FileName));
+
+    if (!DriverEntry->Initialized){
+      //
+      // We have MM driver pending to dispatch
+      //
+      gRequestDispatch = TRUE;
+      break;
+    }
+  }
+
+  gDispatcherRunning = FALSE;
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
+  must add any driver with a before dependency on InsertedDriverEntry first.
+  You do this by recursively calling this routine. After all the Befores are
+  processed you can add InsertedDriverEntry to the mScheduledQueue.
+  Then you can add any driver with an After dependency on InsertedDriverEntry
+  by recursively calling this routine.
+
+  @param  InsertedDriverEntry   The driver to insert on the ScheduledLink Queue
+
+**/
+VOID
+MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
+  IN  EFI_MM_DRIVER_ENTRY   *InsertedDriverEntry
+  )
+{
+  LIST_ENTRY            *Link;
+  EFI_MM_DRIVER_ENTRY *DriverEntry;
+
+  //
+  // Process Before Dependency
+  //
+  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
+    DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
+    if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
+      DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
+      DEBUG ((DEBUG_DISPATCH, "  BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
+      if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
+        //
+        // Recursively process BEFORE
+        //
+        DEBUG ((DEBUG_DISPATCH, "TRUE\n  END\n  RESULT = TRUE\n"));
+        MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
+      } else {
+        DEBUG ((DEBUG_DISPATCH, "FALSE\n  END\n  RESULT = FALSE\n"));
+      }
+    }
+  }
+
+  //
+  // Convert driver from Dependent to Scheduled state
+  //
+
+  InsertedDriverEntry->Dependent = FALSE;
+  InsertedDriverEntry->Scheduled = TRUE;
+  InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);
+
+
+  //
+  // Process After Dependency
+  //
+  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
+    DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
+    if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
+      DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
+      DEBUG ((DEBUG_DISPATCH, "  AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
+      if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
+        //
+        // Recursively process AFTER
+        //
+        DEBUG ((DEBUG_DISPATCH, "TRUE\n  END\n  RESULT = TRUE\n"));
+        MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
+      } else {
+        DEBUG ((DEBUG_DISPATCH, "FALSE\n  END\n  RESULT = FALSE\n"));
+      }
+    }
+  }
+}
+
+/**
+  Return TRUE if the Fv has been processed, FALSE if not.
+
+  @param  FvHandle              The handle of a FV that's being tested
+
+  @retval TRUE                  Fv protocol on FvHandle has been processed
+  @retval FALSE                 Fv protocol on FvHandle has not yet been
+                                processed
+
+**/
+BOOLEAN
+FvHasBeenProcessed (
+  IN EFI_HANDLE  FvHandle
+  )
+{
+  LIST_ENTRY    *Link;
+  KNOWN_HANDLE  *KnownHandle;
+
+  for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {
+    KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);
+    if (KnownHandle->Handle == FvHandle) {
+      return TRUE;
+    }
+  }
+  return FALSE;
+}
+
+/**
+  Remember that Fv protocol on FvHandle has had it's drivers placed on the
+  mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are
+  never removed/freed from the mFvHandleList.
+
+  @param  FvHandle              The handle of a FV that has been processed
+
+**/
+VOID
+FvIsBeingProcesssed (
+  IN EFI_HANDLE  FvHandle
+  )
+{
+  KNOWN_HANDLE  *KnownHandle;
+
+  DEBUG ((DEBUG_INFO, "FvIsBeingProcesssed - 0x%08x\n", FvHandle));
+
+  KnownHandle = AllocatePool (sizeof (KNOWN_HANDLE));
+  ASSERT (KnownHandle != NULL);
+
+  KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;
+  KnownHandle->Handle = FvHandle;
+  InsertTailList (&mFvHandleList, &KnownHandle->Link);
+}
+
+/**
+  Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
+  and initilize any state variables. Read the Depex from the FV and store it
+  in DriverEntry. Pre-process the Depex to set the Before and After state.
+  The Discovered list is never free'ed and contains booleans that represent the
+  other possible MM driver states.
+
+  @param  Fv                    Fv protocol, needed to read Depex info out of
+                                FLASH.
+  @param  FvHandle              Handle for Fv, needed in the
+                                EFI_MM_DRIVER_ENTRY so that the PE image can be
+                                read out of the FV at a later time.
+  @param  DriverName            Name of driver to add to mDiscoveredList.
+
+  @retval EFI_SUCCESS           If driver was added to the mDiscoveredList.
+  @retval EFI_ALREADY_STARTED   The driver has already been started. Only one
+                                DriverName may be active in the system at any one
+                                time.
+
+**/
+EFI_STATUS
+MmAddToDriverList (
+  IN EFI_HANDLE   FvHandle,
+  IN VOID         *Pe32Data,
+  IN UINTN        Pe32DataSize,
+  IN VOID         *Depex,
+  IN UINTN        DepexSize,
+  IN EFI_GUID     *DriverName
+  )
+{
+  EFI_MM_DRIVER_ENTRY  *DriverEntry;
+
+  DEBUG ((DEBUG_INFO, "MmAddToDriverList - %g (0x%08x)\n", DriverName, Pe32Data));
+
+  //
+  // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
+  // NULL or FALSE.
+  //
+  DriverEntry = AllocateZeroPool (sizeof (EFI_MM_DRIVER_ENTRY));
+  ASSERT (DriverEntry != NULL);
+
+  DriverEntry->Signature        = EFI_MM_DRIVER_ENTRY_SIGNATURE;
+  CopyGuid (&DriverEntry->FileName, DriverName);
+  DriverEntry->FvHandle         = FvHandle;
+  DriverEntry->Pe32Data         = Pe32Data;
+  DriverEntry->Pe32DataSize     = Pe32DataSize;
+  DriverEntry->Depex            = Depex;
+  DriverEntry->DepexSize        = DepexSize;
+
+  MmGetDepexSectionAndPreProccess (DriverEntry);
+
+  InsertTailList (&mDiscoveredList, &DriverEntry->Link);
+  gRequestDispatch = TRUE;
+
+  return EFI_SUCCESS;
+}
+
+/**
+  This function is the main entry point for an MM handler dispatch
+  or communicate-based callback.
+
+  Event notification that is fired every time a FV dispatch protocol is added.
+  More than one protocol may have been added when this event is fired, so you
+  must loop on MmLocateHandle () to see how many protocols were added and
+  do the following to each FV:
+  If the Fv has already been processed, skip it. If the Fv has not been
+  processed then mark it as being processed, as we are about to process it.
+  Read the Fv and add any driver in the Fv to the mDiscoveredList.The
+  mDiscoveredList is never free'ed and contains variables that define
+  the other states the MM driver transitions to..
+  While you are at it read the A Priori file into memory.
+  Place drivers in the A Priori list onto the mScheduledQueue.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
+
+  @return Status Code
+
+**/
+EFI_STATUS
+EFIAPI
+MmDriverDispatchHandler (
+  IN     EFI_HANDLE  DispatchHandle,
+  IN     CONST VOID  *Context,        OPTIONAL
+  IN OUT VOID        *CommBuffer,     OPTIONAL
+  IN OUT UINTN       *CommBufferSize  OPTIONAL
+  )
+{
+  EFI_STATUS                            Status;
+
+  DEBUG ((DEBUG_INFO, "MmDriverDispatchHandler\n"));
+
+  //
+  // Execute the MM Dispatcher on any newly discovered FVs and previously
+  // discovered MM drivers that have been discovered but not dispatched.
+  //
+  Status = MmDispatcher ();
+
+  //
+  // Check to see if CommBuffer and CommBufferSize are valid
+  //
+  if (CommBuffer != NULL && CommBufferSize != NULL) {
+    if (*CommBufferSize > 0) {
+      if (Status == EFI_NOT_READY) {
+        //
+        // If a the MM Core Entry Point was just registered, then set flag to
+        // request the MM Dispatcher to be restarted.
+        //
+        *(UINT8 *)CommBuffer = COMM_BUFFER_MM_DISPATCH_RESTART;
+      } else if (!EFI_ERROR (Status)) {
+        //
+        // Set the flag to show that the MM Dispatcher executed without errors
+        //
+        *(UINT8 *)CommBuffer = COMM_BUFFER_MM_DISPATCH_SUCCESS;
+      } else {
+        //
+        // Set the flag to show that the MM Dispatcher encountered an error
+        //
+        *(UINT8 *)CommBuffer = COMM_BUFFER_MM_DISPATCH_ERROR;
+      }
+    }
+  }
+
+  return EFI_SUCCESS;
+}
+
+/**
+  This function is the main entry point for an MM handler dispatch
+  or communicate-based callback.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
+
+  @return Status Code
+
+**/
+EFI_STATUS
+EFIAPI
+MmFvDispatchHandler (
+  IN     EFI_HANDLE               DispatchHandle,
+  IN     CONST VOID               *Context,        OPTIONAL
+  IN OUT VOID                     *CommBuffer,     OPTIONAL
+  IN OUT UINTN                    *CommBufferSize  OPTIONAL
+  )
+{
+  EFI_STATUS                            Status;
+  EFI_MM_COMMUNICATE_FV_DISPATCH_DATA  *CommunicationFvDispatchData;
+  EFI_FIRMWARE_VOLUME_HEADER            *FwVolHeader;
+
+  DEBUG ((DEBUG_INFO, "MmFvDispatchHandler\n"));
+
+  CommunicationFvDispatchData = CommBuffer;
+
+  DEBUG ((DEBUG_INFO, "  Dispatch - 0x%016lx - 0x%016lx\n", CommunicationFvDispatchData->Address, CommunicationFvDispatchData->Size));
+
+  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)CommunicationFvDispatchData->Address;
+
+  MmCoreFfsFindMmDriver (FwVolHeader);
+
+  //
+  // Execute the MM Dispatcher on any newly discovered FVs and previously
+  // discovered MM drivers that have been discovered but not dispatched.
+  //
+  Status = MmDispatcher ();
+
+  return Status;
+}
+
+/**
+  Traverse the discovered list for any drivers that were discovered but not loaded
+  because the dependency experessions evaluated to false.
+
+**/
+VOID
+MmDisplayDiscoveredNotDispatched (
+  VOID
+  )
+{
+  LIST_ENTRY                   *Link;
+  EFI_MM_DRIVER_ENTRY         *DriverEntry;
+
+  for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {
+    DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
+    if (DriverEntry->Dependent) {
+      DEBUG ((DEBUG_LOAD, "MM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));
+    }
+  }
+}
diff --git a/StandaloneMmPkg/Core/FwVol.c b/StandaloneMmPkg/Core/FwVol.c
new file mode 100644
index 0000000000..901c58bc53
--- /dev/null
+++ b/StandaloneMmPkg/Core/FwVol.c
@@ -0,0 +1,104 @@
+/**@file
+
+Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+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
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include "StandaloneMmCore.h"
+#include <Library/FvLib.h>
+
+//
+// List of file types supported by dispatcher
+//
+EFI_FV_FILETYPE mMmFileTypes[] = {
+  EFI_FV_FILETYPE_MM,
+  0xE, //EFI_FV_FILETYPE_MM_STANDALONE,
+       //
+       // Note: DXE core will process the FV image file, so skip it in MM core
+       // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
+       //
+};
+
+EFI_STATUS
+MmAddToDriverList (
+  IN EFI_HANDLE   FvHandle,
+  IN VOID         *Pe32Data,
+  IN UINTN        Pe32DataSize,
+  IN VOID         *Depex,
+  IN UINTN        DepexSize,
+  IN EFI_GUID     *DriverName
+  );
+
+BOOLEAN
+FvHasBeenProcessed (
+  IN EFI_HANDLE  FvHandle
+  );
+
+VOID
+FvIsBeingProcesssed (
+  IN EFI_HANDLE  FvHandle
+  );
+
+EFI_STATUS
+MmCoreFfsFindMmDriver (
+  IN  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader
+  )
+/*++
+
+Routine Description:
+  Given the pointer to the Firmware Volume Header find the
+  MM driver and return it's PE32 image.
+
+Arguments:
+  FwVolHeader - Pointer to memory mapped FV
+
+Returns:
+  other       - Failure
+
+--*/
+{
+  EFI_STATUS          Status;
+  EFI_STATUS          DepexStatus;
+  EFI_FFS_FILE_HEADER *FileHeader;
+  EFI_FV_FILETYPE     FileType;
+  VOID                *Pe32Data;
+  UINTN               Pe32DataSize;
+  VOID                *Depex;
+  UINTN               DepexSize;
+  UINTN               Index;
+
+  DEBUG((DEBUG_INFO, "MmCoreFfsFindMmDriver - 0x%x\n", FwVolHeader));
+
+  if (FvHasBeenProcessed (FwVolHeader)) {
+    return EFI_SUCCESS;
+  }
+
+  FvIsBeingProcesssed (FwVolHeader);
+
+  for (Index = 0; Index < sizeof(mMmFileTypes) / sizeof(mMmFileTypes[0]); Index++) {
+    DEBUG((DEBUG_INFO, "Check MmFileTypes - 0x%x\n", mMmFileTypes[Index]));
+    FileType = mMmFileTypes[Index];
+    FileHeader = NULL;
+    do {
+      Status = FfsFindNextFile(FileType, FwVolHeader, &FileHeader);
+      if (!EFI_ERROR(Status)) {
+        Status = FfsFindSectionData(EFI_SECTION_PE32, FileHeader, &Pe32Data, &Pe32DataSize);
+        DEBUG((DEBUG_INFO, "Find PE data - 0x%x\n", Pe32Data));
+        DepexStatus = FfsFindSectionData(EFI_SECTION_MM_DEPEX, FileHeader, &Depex, &DepexSize);
+        if (!EFI_ERROR(DepexStatus)) {
+          MmAddToDriverList(FwVolHeader, Pe32Data, Pe32DataSize, Depex, DepexSize, &FileHeader->Name);
+        }
+      }
+    } while (!EFI_ERROR(Status));
+  }
+
+  return Status;
+}
diff --git a/StandaloneMmPkg/Core/Handle.c b/StandaloneMmPkg/Core/Handle.c
new file mode 100644
index 0000000000..01832f4bbe
--- /dev/null
+++ b/StandaloneMmPkg/Core/Handle.c
@@ -0,0 +1,533 @@
+/** @file
+  SMM handle & protocol handling.
+
+  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include "StandaloneMmCore.h"
+
+//
+// mProtocolDatabase     - A list of all protocols in the system.  (simple list for now)
+// gHandleList           - A list of all the handles in the system
+//
+LIST_ENTRY  mProtocolDatabase  = INITIALIZE_LIST_HEAD_VARIABLE (mProtocolDatabase);
+LIST_ENTRY  gHandleList        = INITIALIZE_LIST_HEAD_VARIABLE (gHandleList);
+
+/**
+  Check whether a handle is a valid EFI_HANDLE
+
+  @param  UserHandle             The handle to check
+
+  @retval EFI_INVALID_PARAMETER  The handle is NULL or not a valid EFI_HANDLE.
+  @retval EFI_SUCCESS            The handle is valid EFI_HANDLE.
+
+**/
+EFI_STATUS
+MmValidateHandle (
+  IN EFI_HANDLE  UserHandle
+  )
+{
+  IHANDLE  *Handle;
+
+  Handle = (IHANDLE *)UserHandle;
+  if (Handle == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+  if (Handle->Signature != EFI_HANDLE_SIGNATURE) {
+    return EFI_INVALID_PARAMETER;
+  }
+  return EFI_SUCCESS;
+}
+
+/**
+  Finds the protocol entry for the requested protocol.
+
+  @param  Protocol               The ID of the protocol
+  @param  Create                 Create a new entry if not found
+
+  @return Protocol entry
+
+**/
+PROTOCOL_ENTRY  *
+MmFindProtocolEntry (
+  IN EFI_GUID   *Protocol,
+  IN BOOLEAN    Create
+  )
+{
+  LIST_ENTRY          *Link;
+  PROTOCOL_ENTRY      *Item;
+  PROTOCOL_ENTRY      *ProtEntry;
+
+  //
+  // Search the database for the matching GUID
+  //
+
+  ProtEntry = NULL;
+  for (Link = mProtocolDatabase.ForwardLink;
+       Link != &mProtocolDatabase;
+       Link = Link->ForwardLink) {
+
+    Item = CR(Link, PROTOCOL_ENTRY, AllEntries, PROTOCOL_ENTRY_SIGNATURE);
+    if (CompareGuid (&Item->ProtocolID, Protocol)) {
+      //
+      // This is the protocol entry
+      //
+      ProtEntry = Item;
+      break;
+    }
+  }
+
+  //
+  // If the protocol entry was not found and Create is TRUE, then
+  // allocate a new entry
+  //
+  if ((ProtEntry == NULL) && Create) {
+    ProtEntry = AllocatePool (sizeof(PROTOCOL_ENTRY));
+    if (ProtEntry != NULL) {
+      //
+      // Initialize new protocol entry structure
+      //
+      ProtEntry->Signature = PROTOCOL_ENTRY_SIGNATURE;
+      CopyGuid ((VOID *)&ProtEntry->ProtocolID, Protocol);
+      InitializeListHead (&ProtEntry->Protocols);
+      InitializeListHead (&ProtEntry->Notify);
+
+      //
+      // Add it to protocol database
+      //
+      InsertTailList (&mProtocolDatabase, &ProtEntry->AllEntries);
+    }
+  }
+  return ProtEntry;
+}
+
+/**
+  Finds the protocol instance for the requested handle and protocol.
+  Note: This function doesn't do parameters checking, it's caller's responsibility
+  to pass in valid parameters.
+
+  @param  Handle                 The handle to search the protocol on
+  @param  Protocol               GUID of the protocol
+  @param  Interface              The interface for the protocol being searched
+
+  @return Protocol instance (NULL: Not found)
+
+**/
+PROTOCOL_INTERFACE *
+MmFindProtocolInterface (
+  IN IHANDLE   *Handle,
+  IN EFI_GUID  *Protocol,
+  IN VOID      *Interface
+  )
+{
+  PROTOCOL_INTERFACE  *Prot;
+  PROTOCOL_ENTRY      *ProtEntry;
+  LIST_ENTRY          *Link;
+
+  Prot = NULL;
+
+  //
+  // Lookup the protocol entry for this protocol ID
+  //
+  ProtEntry = MmFindProtocolEntry (Protocol, FALSE);
+  if (ProtEntry != NULL) {
+    //
+    // Look at each protocol interface for any matches
+    //
+    for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link=Link->ForwardLink) {
+      //
+      // If this protocol interface matches, remove it
+      //
+      Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);
+      if (Prot->Interface == Interface && Prot->Protocol == ProtEntry) {
+        break;
+      }
+      Prot = NULL;
+    }
+  }
+  return Prot;
+}
+
+/**
+  Wrapper function to MmInstallProtocolInterfaceNotify.  This is the public API which
+  Calls the private one which contains a BOOLEAN parameter for notifications
+
+  @param  UserHandle             The handle to install the protocol handler on,
+                                 or NULL if a new handle is to be allocated
+  @param  Protocol               The protocol to add to the handle
+  @param  InterfaceType          Indicates whether Interface is supplied in
+                                 native form.
+  @param  Interface              The interface for the protocol being added
+
+  @return Status code
+
+**/
+EFI_STATUS
+EFIAPI
+MmInstallProtocolInterface (
+  IN OUT EFI_HANDLE      *UserHandle,
+  IN EFI_GUID            *Protocol,
+  IN EFI_INTERFACE_TYPE  InterfaceType,
+  IN VOID                *Interface
+  )
+{
+  return MmInstallProtocolInterfaceNotify (
+           UserHandle,
+           Protocol,
+           InterfaceType,
+           Interface,
+           TRUE
+           );
+}
+
+/**
+  Installs a protocol interface into the boot services environment.
+
+  @param  UserHandle             The handle to install the protocol handler on,
+                                 or NULL if a new handle is to be allocated
+  @param  Protocol               The protocol to add to the handle
+  @param  InterfaceType          Indicates whether Interface is supplied in
+                                 native form.
+  @param  Interface              The interface for the protocol being added
+  @param  Notify                 indicates whether notify the notification list
+                                 for this protocol
+
+  @retval EFI_INVALID_PARAMETER  Invalid parameter
+  @retval EFI_OUT_OF_RESOURCES   No enough buffer to allocate
+  @retval EFI_SUCCESS            Protocol interface successfully installed
+
+**/
+EFI_STATUS
+MmInstallProtocolInterfaceNotify (
+  IN OUT EFI_HANDLE          *UserHandle,
+  IN     EFI_GUID            *Protocol,
+  IN     EFI_INTERFACE_TYPE  InterfaceType,
+  IN     VOID                *Interface,
+  IN     BOOLEAN             Notify
+  )
+{
+  PROTOCOL_INTERFACE  *Prot;
+  PROTOCOL_ENTRY      *ProtEntry;
+  IHANDLE             *Handle;
+  EFI_STATUS          Status;
+  VOID                *ExistingInterface;
+
+  //
+  // returns EFI_INVALID_PARAMETER if InterfaceType is invalid.
+  // Also added check for invalid UserHandle and Protocol pointers.
+  //
+  if (UserHandle == NULL || Protocol == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if (InterfaceType != EFI_NATIVE_INTERFACE) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  //
+  // Print debug message
+  //
+  DEBUG((DEBUG_LOAD | DEBUG_INFO, "MmInstallProtocolInterface: %g %p\n", Protocol, Interface));
+
+  Status = EFI_OUT_OF_RESOURCES;
+  Prot = NULL;
+  Handle = NULL;
+
+  if (*UserHandle != NULL) {
+    Status = MmHandleProtocol (*UserHandle, Protocol, (VOID **)&ExistingInterface);
+    if (!EFI_ERROR (Status)) {
+      return EFI_INVALID_PARAMETER;
+    }
+  }
+
+  //
+  // Lookup the Protocol Entry for the requested protocol
+  //
+  ProtEntry = MmFindProtocolEntry (Protocol, TRUE);
+  if (ProtEntry == NULL) {
+    goto Done;
+  }
+
+  //
+  // Allocate a new protocol interface structure
+  //
+  Prot = AllocateZeroPool (sizeof(PROTOCOL_INTERFACE));
+  if (Prot == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto Done;
+  }
+
+  //
+  // If caller didn't supply a handle, allocate a new one
+  //
+  Handle = (IHANDLE *)*UserHandle;
+  if (Handle == NULL) {
+    Handle = AllocateZeroPool (sizeof(IHANDLE));
+    if (Handle == NULL) {
+      Status = EFI_OUT_OF_RESOURCES;
+      goto Done;
+    }
+
+    //
+    // Initialize new handler structure
+    //
+    Handle->Signature = EFI_HANDLE_SIGNATURE;
+    InitializeListHead (&Handle->Protocols);
+
+    //
+    // Add this handle to the list global list of all handles
+    // in the system
+    //
+    InsertTailList (&gHandleList, &Handle->AllHandles);
+  }
+
+  Status = MmValidateHandle (Handle);
+  if (EFI_ERROR (Status)) {
+    goto Done;
+  }
+
+  //
+  // Each interface that is added must be unique
+  //
+  ASSERT (MmFindProtocolInterface (Handle, Protocol, Interface) == NULL);
+
+  //
+  // Initialize the protocol interface structure
+  //
+  Prot->Signature = PROTOCOL_INTERFACE_SIGNATURE;
+  Prot->Handle = Handle;
+  Prot->Protocol = ProtEntry;
+  Prot->Interface = Interface;
+
+  //
+  // Add this protocol interface to the head of the supported
+  // protocol list for this handle
+  //
+  InsertHeadList (&Handle->Protocols, &Prot->Link);
+
+  //
+  // Add this protocol interface to the tail of the
+  // protocol entry
+  //
+  InsertTailList (&ProtEntry->Protocols, &Prot->ByProtocol);
+
+  //
+  // Notify the notification list for this protocol
+  //
+  if (Notify) {
+    MmNotifyProtocol (Prot);
+  }
+  Status = EFI_SUCCESS;
+
+Done:
+  if (!EFI_ERROR (Status)) {
+    //
+    // Return the new handle back to the caller
+    //
+    *UserHandle = Handle;
+  } else {
+    //
+    // There was an error, clean up
+    //
+    if (Prot != NULL) {
+      FreePool (Prot);
+    }
+  }
+  return Status;
+}
+
+/**
+  Uninstalls all instances of a protocol:interfacer from a handle.
+  If the last protocol interface is remove from the handle, the
+  handle is freed.
+
+  @param  UserHandle             The handle to remove the protocol handler from
+  @param  Protocol               The protocol, of protocol:interface, to remove
+  @param  Interface              The interface, of protocol:interface, to remove
+
+  @retval EFI_INVALID_PARAMETER  Protocol is NULL.
+  @retval EFI_SUCCESS            Protocol interface successfully uninstalled.
+
+**/
+EFI_STATUS
+EFIAPI
+MmUninstallProtocolInterface (
+  IN EFI_HANDLE  UserHandle,
+  IN EFI_GUID    *Protocol,
+  IN VOID        *Interface
+  )
+{
+  EFI_STATUS          Status;
+  IHANDLE             *Handle;
+  PROTOCOL_INTERFACE  *Prot;
+
+  //
+  // Check that Protocol is valid
+  //
+  if (Protocol == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  //
+  // Check that UserHandle is a valid handle
+  //
+  Status = MmValidateHandle (UserHandle);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  //
+  // Check that Protocol exists on UserHandle, and Interface matches the interface in the database
+  //
+  Prot = MmFindProtocolInterface (UserHandle, Protocol, Interface);
+  if (Prot == NULL) {
+    return EFI_NOT_FOUND;
+  }
+
+  //
+  // Remove the protocol interface from the protocol
+  //
+  Status = EFI_NOT_FOUND;
+  Handle = (IHANDLE *)UserHandle;
+  Prot   = MmRemoveInterfaceFromProtocol (Handle, Protocol, Interface);
+
+  if (Prot != NULL) {
+    //
+    // Remove the protocol interface from the handle
+    //
+    RemoveEntryList (&Prot->Link);
+
+    //
+    // Free the memory
+    //
+    Prot->Signature = 0;
+    FreePool (Prot);
+    Status = EFI_SUCCESS;
+  }
+
+  //
+  // If there are no more handlers for the handle, free the handle
+  //
+  if (IsListEmpty (&Handle->Protocols)) {
+    Handle->Signature = 0;
+    RemoveEntryList (&Handle->AllHandles);
+    FreePool (Handle);
+  }
+  return Status;
+}
+
+/**
+  Locate a certain GUID protocol interface in a Handle's protocols.
+
+  @param  UserHandle             The handle to obtain the protocol interface on
+  @param  Protocol               The GUID of the protocol
+
+  @return The requested protocol interface for the handle
+
+**/
+PROTOCOL_INTERFACE  *
+MmGetProtocolInterface (
+  IN EFI_HANDLE  UserHandle,
+  IN EFI_GUID    *Protocol
+  )
+{
+  EFI_STATUS          Status;
+  PROTOCOL_ENTRY      *ProtEntry;
+  PROTOCOL_INTERFACE  *Prot;
+  IHANDLE             *Handle;
+  LIST_ENTRY          *Link;
+
+  Status = MmValidateHandle (UserHandle);
+  if (EFI_ERROR (Status)) {
+    return NULL;
+  }
+
+  Handle = (IHANDLE *)UserHandle;
+
+  //
+  // Look at each protocol interface for a match
+  //
+  for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link = Link->ForwardLink) {
+    Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);
+    ProtEntry = Prot->Protocol;
+    if (CompareGuid (&ProtEntry->ProtocolID, Protocol)) {
+      return Prot;
+    }
+  }
+  return NULL;
+}
+
+/**
+  Queries a handle to determine if it supports a specified protocol.
+
+  @param  UserHandle             The handle being queried.
+  @param  Protocol               The published unique identifier of the protocol.
+  @param  Interface              Supplies the address where a pointer to the
+                                 corresponding Protocol Interface is returned.
+
+  @retval EFI_SUCCESS            The interface information for the specified protocol was returned.
+  @retval EFI_UNSUPPORTED        The device does not support the specified protocol.
+  @retval EFI_INVALID_PARAMETER  Handle is not a valid EFI_HANDLE..
+  @retval EFI_INVALID_PARAMETER  Protocol is NULL.
+  @retval EFI_INVALID_PARAMETER  Interface is NULL.
+
+**/
+EFI_STATUS
+EFIAPI
+MmHandleProtocol (
+  IN  EFI_HANDLE  UserHandle,
+  IN  EFI_GUID    *Protocol,
+  OUT VOID        **Interface
+  )
+{
+  EFI_STATUS          Status;
+  PROTOCOL_INTERFACE  *Prot;
+
+  //
+  // Check for invalid Protocol
+  //
+  if (Protocol == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  //
+  // Check for invalid Interface
+  //
+  if (Interface == NULL) {
+    return EFI_INVALID_PARAMETER;
+  } else {
+    *Interface = NULL;
+  }
+
+  //
+  // Check for invalid UserHandle
+  //
+  Status = MmValidateHandle (UserHandle);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  //
+  // Look at each protocol interface for a match
+  //
+  Prot = MmGetProtocolInterface (UserHandle, Protocol);
+  if (Prot == NULL) {
+    return EFI_UNSUPPORTED;
+  }
+
+  //
+  // This is the protocol interface entry for this protocol
+  //
+  *Interface = Prot->Interface;
+
+  return EFI_SUCCESS;
+}
diff --git a/StandaloneMmPkg/Core/InstallConfigurationTable.c b/StandaloneMmPkg/Core/InstallConfigurationTable.c
new file mode 100644
index 0000000000..3a31c63f94
--- /dev/null
+++ b/StandaloneMmPkg/Core/InstallConfigurationTable.c
@@ -0,0 +1,178 @@
+/** @file
+  System Management System Table Services MmInstallConfigurationTable service
+
+  Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include "StandaloneMmCore.h"
+
+#define CONFIG_TABLE_SIZE_INCREASED 0x10
+
+UINTN  mMmSystemTableAllocateSize = 0;
+
+/**
+  The MmInstallConfigurationTable() function is used to maintain the list
+  of configuration tables that are stored in the System Management System
+  Table.  The list is stored as an array of (GUID, Pointer) pairs.  The list
+  must be allocated from pool memory with PoolType set to EfiRuntimeServicesData.
+
+  @param  SystemTable      A pointer to the SMM System Table (SMST).
+  @param  Guid             A pointer to the GUID for the entry to add, update, or remove.
+  @param  Table            A pointer to the buffer of the table to add.
+  @param  TableSize        The size of the table to install.
+
+  @retval EFI_SUCCESS           The (Guid, Table) pair was added, updated, or removed.
+  @retval EFI_INVALID_PARAMETER Guid is not valid.
+  @retval EFI_NOT_FOUND         An attempt was made to delete a non-existent entry.
+  @retval EFI_OUT_OF_RESOURCES  There is not enough memory available to complete the operation.
+
+**/
+EFI_STATUS
+EFIAPI
+MmInstallConfigurationTable (
+  IN  CONST EFI_MM_SYSTEM_TABLE    *SystemTable,
+  IN  CONST EFI_GUID               *Guid,
+  IN  VOID                         *Table,
+  IN  UINTN                        TableSize
+  )
+{
+  UINTN                    Index;
+  EFI_CONFIGURATION_TABLE  *ConfigurationTable;
+  EFI_CONFIGURATION_TABLE  *OldTable;
+
+  //
+  // If Guid is NULL, then this operation cannot be performed
+  //
+  if (Guid == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  ConfigurationTable = gMmCoreMmst.MmConfigurationTable;
+
+  //
+  // Search all the table for an entry that matches Guid
+  //
+  for (Index = 0; Index < gMmCoreMmst.NumberOfTableEntries; Index++) {
+    if (CompareGuid (Guid, &(ConfigurationTable[Index].VendorGuid))) {
+      break;
+    }
+  }
+
+  if (Index < gMmCoreMmst.NumberOfTableEntries) {
+    //
+    // A match was found, so this is either a modify or a delete operation
+    //
+    if (Table != NULL) {
+      //
+      // If Table is not NULL, then this is a modify operation.
+      // Modify the table entry and return.
+      //
+      ConfigurationTable[Index].VendorTable = Table;
+      return EFI_SUCCESS;
+    }
+
+    //
+    // A match was found and Table is NULL, so this is a delete operation.
+    //
+    gMmCoreMmst.NumberOfTableEntries--;
+
+    //
+    // Copy over deleted entry
+    //
+    CopyMem (
+      &(ConfigurationTable[Index]),
+      &(ConfigurationTable[Index + 1]),
+      (gMmCoreMmst.NumberOfTableEntries - Index) * sizeof (EFI_CONFIGURATION_TABLE)
+      );
+
+  } else {
+    //
+    // No matching GUIDs were found, so this is an add operation.
+    //
+    if (Table == NULL) {
+      //
+      // If Table is NULL on an add operation, then return an error.
+      //
+      return EFI_NOT_FOUND;
+    }
+
+    //
+    // Assume that Index == gMmCoreMmst.NumberOfTableEntries
+    //
+    if ((Index * sizeof (EFI_CONFIGURATION_TABLE)) >= mMmSystemTableAllocateSize) {
+      //
+      // Allocate a table with one additional entry.
+      //
+      mMmSystemTableAllocateSize += (CONFIG_TABLE_SIZE_INCREASED * sizeof (EFI_CONFIGURATION_TABLE));
+      ConfigurationTable = AllocatePool (mMmSystemTableAllocateSize);
+      if (ConfigurationTable == NULL) {
+        //
+        // If a new table could not be allocated, then return an error.
+        //
+        return EFI_OUT_OF_RESOURCES;
+      }
+
+      if (gMmCoreMmst.MmConfigurationTable != NULL) {
+        //
+        // Copy the old table to the new table.
+        //
+        CopyMem (
+          ConfigurationTable,
+          gMmCoreMmst.MmConfigurationTable,
+          Index * sizeof (EFI_CONFIGURATION_TABLE)
+          );
+
+        //
+        // Record the old table pointer.
+        //
+        OldTable = gMmCoreMmst.MmConfigurationTable;
+
+        //
+        // As the MmInstallConfigurationTable() may be re-entered by FreePool() in
+        // its calling stack, updating System table to the new table pointer must
+        // be done before calling FreePool() to free the old table.
+        // It can make sure the gMmCoreMmst.MmConfigurationTable point to the new
+        // table and avoid the errors of use-after-free to the old table by the
+        // reenter of MmInstallConfigurationTable() in FreePool()'s calling stack.
+        //
+        gMmCoreMmst.MmConfigurationTable = ConfigurationTable;
+
+        //
+        // Free the old table after updating System Table to the new table pointer.
+        //
+        FreePool (OldTable);
+      } else {
+        //
+        // Update System Table
+        //
+        gMmCoreMmst.MmConfigurationTable = ConfigurationTable;
+      }
+    }
+
+    //
+    // Fill in the new entry
+    //
+    CopyGuid ((VOID *)&ConfigurationTable[Index].VendorGuid, Guid);
+    ConfigurationTable[Index].VendorTable = Table;
+
+    //
+    // This is an add operation, so increment the number of table entries
+    //
+    gMmCoreMmst.NumberOfTableEntries++;
+  }
+
+  //
+  // CRC-32 field is ignorable for SMM System Table and should be set to zero
+  //
+
+  return EFI_SUCCESS;
+}
diff --git a/StandaloneMmPkg/Core/Locate.c b/StandaloneMmPkg/Core/Locate.c
new file mode 100644
index 0000000000..6a90575f99
--- /dev/null
+++ b/StandaloneMmPkg/Core/Locate.c
@@ -0,0 +1,496 @@
+/** @file
+  Locate handle functions
+
+  Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include "StandaloneMmCore.h"
+
+//
+// ProtocolRequest - Last LocateHandle request ID
+//
+UINTN mEfiLocateHandleRequest = 0;
+
+//
+// Internal prototypes
+//
+
+typedef struct {
+  EFI_GUID        *Protocol;
+  VOID            *SearchKey;
+  LIST_ENTRY      *Position;
+  PROTOCOL_ENTRY  *ProtEntry;
+} LOCATE_POSITION;
+
+typedef
+IHANDLE *
+(* CORE_GET_NEXT) (
+  IN OUT LOCATE_POSITION    *Position,
+  OUT VOID                  **Interface
+  );
+
+/**
+  Routine to get the next Handle, when you are searching for all handles.
+
+  @param  Position               Information about which Handle to seach for.
+  @param  Interface              Return the interface structure for the matching
+                                 protocol.
+
+  @return An pointer to IHANDLE if the next Position is not the end of the list.
+          Otherwise,NULL is returned.
+
+**/
+IHANDLE *
+MmGetNextLocateAllHandles (
+  IN OUT LOCATE_POSITION  *Position,
+  OUT    VOID             **Interface
+  )
+{
+  IHANDLE     *Handle;
+
+  //
+  // Next handle
+  //
+  Position->Position = Position->Position->ForwardLink;
+
+  //
+  // If not at the end of the list, get the handle
+  //
+  Handle      = NULL;
+  *Interface  = NULL;
+  if (Position->Position != &gHandleList) {
+    Handle = CR (Position->Position, IHANDLE, AllHandles, EFI_HANDLE_SIGNATURE);
+  }
+  return Handle;
+}
+
+/**
+  Routine to get the next Handle, when you are searching for register protocol
+  notifies.
+
+  @param  Position               Information about which Handle to seach for.
+  @param  Interface              Return the interface structure for the matching
+                                 protocol.
+
+  @return An pointer to IHANDLE if the next Position is not the end of the list.
+          Otherwise,NULL is returned.
+
+**/
+IHANDLE *
+MmGetNextLocateByRegisterNotify (
+  IN OUT LOCATE_POSITION  *Position,
+  OUT    VOID             **Interface
+  )
+{
+  IHANDLE             *Handle;
+  PROTOCOL_NOTIFY     *ProtNotify;
+  PROTOCOL_INTERFACE  *Prot;
+  LIST_ENTRY          *Link;
+
+  Handle      = NULL;
+  *Interface  = NULL;
+  ProtNotify = Position->SearchKey;
+
+  //
+  // If this is the first request, get the next handle
+  //
+  if (ProtNotify != NULL) {
+    ASSERT(ProtNotify->Signature == PROTOCOL_NOTIFY_SIGNATURE);
+    Position->SearchKey = NULL;
+
+    //
+    // If not at the end of the list, get the next handle
+    //
+    Link = ProtNotify->Position->ForwardLink;
+    if (Link != &ProtNotify->Protocol->Protocols) {
+      Prot = CR (Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_INTERFACE_SIGNATURE);
+      Handle = Prot->Handle;
+      *Interface = Prot->Interface;
+    }
+  }
+  return Handle;
+}
+
+/**
+  Routine to get the next Handle, when you are searching for a given protocol.
+
+  @param  Position               Information about which Handle to seach for.
+  @param  Interface              Return the interface structure for the matching
+                                 protocol.
+
+  @return An pointer to IHANDLE if the next Position is not the end of the list.
+          Otherwise,NULL is returned.
+
+**/
+IHANDLE *
+MmGetNextLocateByProtocol (
+  IN OUT LOCATE_POSITION  *Position,
+  OUT    VOID             **Interface
+  )
+{
+  IHANDLE             *Handle;
+  LIST_ENTRY          *Link;
+  PROTOCOL_INTERFACE  *Prot;
+
+  Handle      = NULL;
+  *Interface  = NULL;
+  for (; ;) {
+    //
+    // Next entry
+    //
+    Link = Position->Position->ForwardLink;
+    Position->Position = Link;
+
+    //
+    // If not at the end, return the handle
+    //
+    if (Link == &Position->ProtEntry->Protocols) {
+      Handle = NULL;
+      break;
+    }
+
+    //
+    // Get the handle
+    //
+    Prot = CR(Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_INTERFACE_SIGNATURE);
+    Handle = Prot->Handle;
+    *Interface = Prot->Interface;
+
+    //
+    // If this handle has not been returned this request, then
+    // return it now
+    //
+    if (Handle->LocateRequest != mEfiLocateHandleRequest) {
+      Handle->LocateRequest = mEfiLocateHandleRequest;
+      break;
+    }
+  }
+  return Handle;
+}
+
+/**
+  Return the first Protocol Interface that matches the Protocol GUID. If
+  Registration is pasased in return a Protocol Instance that was just add
+  to the system. If Retistration is NULL return the first Protocol Interface
+  you find.
+
+  @param  Protocol               The protocol to search for
+  @param  Registration           Optional Registration Key returned from
+                                 RegisterProtocolNotify()
+  @param  Interface              Return the Protocol interface (instance).
+
+  @retval EFI_SUCCESS            If a valid Interface is returned
+  @retval EFI_INVALID_PARAMETER  Invalid parameter
+  @retval EFI_NOT_FOUND          Protocol interface not found
+
+**/
+EFI_STATUS
+EFIAPI
+MmLocateProtocol (
+  IN  EFI_GUID  *Protocol,
+  IN  VOID      *Registration OPTIONAL,
+  OUT VOID      **Interface
+  )
+{
+  EFI_STATUS              Status;
+  LOCATE_POSITION         Position;
+  PROTOCOL_NOTIFY         *ProtNotify;
+  IHANDLE                 *Handle;
+
+  if ((Interface == NULL) || (Protocol == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  *Interface = NULL;
+  Status = EFI_SUCCESS;
+
+  //
+  // Set initial position
+  //
+  Position.Protocol  = Protocol;
+  Position.SearchKey = Registration;
+  Position.Position  = &gHandleList;
+
+  mEfiLocateHandleRequest += 1;
+
+  if (Registration == NULL) {
+    //
+    // Look up the protocol entry and set the head pointer
+    //
+    Position.ProtEntry = MmFindProtocolEntry (Protocol, FALSE);
+    if (Position.ProtEntry == NULL) {
+      return EFI_NOT_FOUND;
+    }
+    Position.Position = &Position.ProtEntry->Protocols;
+
+    Handle = MmGetNextLocateByProtocol (&Position, Interface);
+  } else {
+    Handle = MmGetNextLocateByRegisterNotify (&Position, Interface);
+  }
+
+  if (Handle == NULL) {
+    Status = EFI_NOT_FOUND;
+  } else if (Registration != NULL) {
+    //
+    // If this is a search by register notify and a handle was
+    // returned, update the register notification position
+    //
+    ProtNotify = Registration;
+    ProtNotify->Position = ProtNotify->Position->ForwardLink;
+  }
+
+  return Status;
+}
+
+/**
+  Locates the requested handle(s) and returns them in Buffer.
+
+  @param  SearchType             The type of search to perform to locate the
+                                 handles
+  @param  Protocol               The protocol to search for
+  @param  SearchKey              Dependant on SearchType
+  @param  BufferSize             On input the size of Buffer.  On output the
+                                 size of data returned.
+  @param  Buffer                 The buffer to return the results in
+
+  @retval EFI_BUFFER_TOO_SMALL   Buffer too small, required buffer size is
+                                 returned in BufferSize.
+  @retval EFI_INVALID_PARAMETER  Invalid parameter
+  @retval EFI_SUCCESS            Successfully found the requested handle(s) and
+                                 returns them in Buffer.
+
+**/
+EFI_STATUS
+EFIAPI
+MmLocateHandle (
+  IN     EFI_LOCATE_SEARCH_TYPE  SearchType,
+  IN     EFI_GUID                *Protocol   OPTIONAL,
+  IN     VOID                    *SearchKey  OPTIONAL,
+  IN OUT UINTN                   *BufferSize,
+  OUT    EFI_HANDLE              *Buffer
+  )
+{
+  EFI_STATUS       Status;
+  LOCATE_POSITION  Position;
+  PROTOCOL_NOTIFY  *ProtNotify;
+  CORE_GET_NEXT    GetNext;
+  UINTN            ResultSize;
+  IHANDLE          *Handle;
+  IHANDLE          **ResultBuffer;
+  VOID             *Interface;
+
+  if (BufferSize == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if ((*BufferSize > 0) && (Buffer == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  GetNext = NULL;
+
+  //
+  // Set initial position
+  //
+  Position.Protocol  = Protocol;
+  Position.SearchKey = SearchKey;
+  Position.Position  = &gHandleList;
+
+  ResultSize = 0;
+  ResultBuffer = (IHANDLE **) Buffer;
+  Status = EFI_SUCCESS;
+
+  //
+  // Get the search function based on type
+  //
+  switch (SearchType) {
+  case AllHandles:
+    GetNext = MmGetNextLocateAllHandles;
+    break;
+
+  case ByRegisterNotify:
+    GetNext = MmGetNextLocateByRegisterNotify;
+    //
+    // Must have SearchKey for locate ByRegisterNotify
+    //
+    if (SearchKey == NULL) {
+      Status = EFI_INVALID_PARAMETER;
+    }
+    break;
+
+  case ByProtocol:
+    GetNext = MmGetNextLocateByProtocol;
+    if (Protocol == NULL) {
+      Status = EFI_INVALID_PARAMETER;
+      break;
+    }
+    //
+    // Look up the protocol entry and set the head pointer
+    //
+    Position.ProtEntry = MmFindProtocolEntry (Protocol, FALSE);
+    if (Position.ProtEntry == NULL) {
+      Status = EFI_NOT_FOUND;
+      break;
+    }
+    Position.Position = &Position.ProtEntry->Protocols;
+    break;
+
+  default:
+    Status = EFI_INVALID_PARAMETER;
+    break;
+  }
+
+  if (EFI_ERROR(Status)) {
+    return Status;
+  }
+
+  //
+  // Enumerate out the matching handles
+  //
+  mEfiLocateHandleRequest += 1;
+  for (; ;) {
+    //
+    // Get the next handle.  If no more handles, stop
+    //
+    Handle = GetNext (&Position, &Interface);
+    if (NULL == Handle) {
+      break;
+    }
+
+    //
+    // Increase the resulting buffer size, and if this handle
+    // fits return it
+    //
+    ResultSize += sizeof(Handle);
+    if (ResultSize <= *BufferSize) {
+        *ResultBuffer = Handle;
+        ResultBuffer += 1;
+    }
+  }
+
+  //
+  // If the result is a zero length buffer, then there were no
+  // matching handles
+  //
+  if (ResultSize == 0) {
+    Status = EFI_NOT_FOUND;
+  } else {
+    //
+    // Return the resulting buffer size.  If it's larger than what
+    // was passed, then set the error code
+    //
+    if (ResultSize > *BufferSize) {
+      Status = EFI_BUFFER_TOO_SMALL;
+    }
+
+    *BufferSize = ResultSize;
+
+    if (SearchType == ByRegisterNotify && !EFI_ERROR(Status)) {
+      ASSERT (SearchKey != NULL);
+      //
+      // If this is a search by register notify and a handle was
+      // returned, update the register notification position
+      //
+      ProtNotify = SearchKey;
+      ProtNotify->Position = ProtNotify->Position->ForwardLink;
+    }
+  }
+
+  return Status;
+}
+
+/**
+  Function returns an array of handles that support the requested protocol
+  in a buffer allocated from pool. This is a version of MmLocateHandle()
+  that allocates a buffer for the caller.
+
+  @param  SearchType             Specifies which handle(s) are to be returned.
+  @param  Protocol               Provides the protocol to search by.    This
+                                 parameter is only valid for SearchType
+                                 ByProtocol.
+  @param  SearchKey              Supplies the search key depending on the
+                                 SearchType.
+  @param  NumberHandles          The number of handles returned in Buffer.
+  @param  Buffer                 A pointer to the buffer to return the requested
+                                 array of  handles that support Protocol.
+
+  @retval EFI_SUCCESS            The result array of handles was returned.
+  @retval EFI_NOT_FOUND          No handles match the search.
+  @retval EFI_OUT_OF_RESOURCES   There is not enough pool memory to store the
+                                 matching results.
+  @retval EFI_INVALID_PARAMETER  One or more parameters are not valid.
+
+**/
+EFI_STATUS
+EFIAPI
+MmLocateHandleBuffer (
+  IN     EFI_LOCATE_SEARCH_TYPE  SearchType,
+  IN     EFI_GUID                *Protocol OPTIONAL,
+  IN     VOID                    *SearchKey OPTIONAL,
+  IN OUT UINTN                   *NumberHandles,
+  OUT    EFI_HANDLE              **Buffer
+  )
+{
+  EFI_STATUS  Status;
+  UINTN       BufferSize;
+
+  if (NumberHandles == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if (Buffer == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  BufferSize = 0;
+  *NumberHandles = 0;
+  *Buffer = NULL;
+  Status = MmLocateHandle (
+             SearchType,
+             Protocol,
+             SearchKey,
+             &BufferSize,
+             *Buffer
+             );
+  //
+  // LocateHandleBuffer() returns incorrect status code if SearchType is
+  // invalid.
+  //
+  // Add code to correctly handle expected errors from MmLocateHandle().
+  //
+  if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {
+    if (Status != EFI_INVALID_PARAMETER) {
+      Status = EFI_NOT_FOUND;
+    }
+    return Status;
+  }
+
+  *Buffer = AllocatePool (BufferSize);
+  if (*Buffer == NULL) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  Status = MmLocateHandle (
+             SearchType,
+             Protocol,
+             SearchKey,
+             &BufferSize,
+             *Buffer
+             );
+
+  *NumberHandles = BufferSize / sizeof(EFI_HANDLE);
+  if (EFI_ERROR(Status)) {
+    *NumberHandles = 0;
+  }
+
+  return Status;
+}
diff --git a/StandaloneMmPkg/Core/Mmi.c b/StandaloneMmPkg/Core/Mmi.c
new file mode 100644
index 0000000000..29aba7b53d
--- /dev/null
+++ b/StandaloneMmPkg/Core/Mmi.c
@@ -0,0 +1,337 @@
+/** @file
+  MMI management.
+
+  Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include "StandaloneMmCore.h"
+
+//
+// MM_HANDLER_STATE_NOTIFIER
+//
+
+//
+// MM_HANDLER - used for each MM handler
+//
+
+#define MMI_ENTRY_SIGNATURE  SIGNATURE_32('m','m','i','e')
+
+ typedef struct {
+  UINTN       Signature;
+  LIST_ENTRY  AllEntries;  // All entries
+
+  EFI_GUID    HandlerType; // Type of interrupt
+  LIST_ENTRY  MmiHandlers; // All handlers
+} MMI_ENTRY;
+
+#define MMI_HANDLER_SIGNATURE  SIGNATURE_32('m','m','i','h')
+
+ typedef struct {
+  UINTN                         Signature;
+  LIST_ENTRY                    Link;        // Link on MMI_ENTRY.MmiHandlers
+  EFI_MM_HANDLER_ENTRY_POINT    Handler;     // The mm handler's entry point
+  MMI_ENTRY                     *MmiEntry;
+} MMI_HANDLER;
+
+LIST_ENTRY  mRootMmiHandlerList = INITIALIZE_LIST_HEAD_VARIABLE (mRootMmiHandlerList);
+LIST_ENTRY  mMmiEntryList       = INITIALIZE_LIST_HEAD_VARIABLE (mMmiEntryList);
+
+/**
+  Finds the MMI entry for the requested handler type.
+
+  @param  HandlerType            The type of the interrupt
+  @param  Create                 Create a new entry if not found
+
+  @return MMI entry
+
+**/
+MMI_ENTRY  *
+EFIAPI
+MmCoreFindMmiEntry (
+  IN EFI_GUID  *HandlerType,
+  IN BOOLEAN   Create
+  )
+{
+  LIST_ENTRY  *Link;
+  MMI_ENTRY   *Item;
+  MMI_ENTRY   *MmiEntry;
+
+  //
+  // Search the MMI entry list for the matching GUID
+  //
+  MmiEntry = NULL;
+  for (Link = mMmiEntryList.ForwardLink;
+       Link != &mMmiEntryList;
+       Link = Link->ForwardLink) {
+
+    Item = CR (Link, MMI_ENTRY, AllEntries, MMI_ENTRY_SIGNATURE);
+    if (CompareGuid (&Item->HandlerType, HandlerType)) {
+      //
+      // This is the MMI entry
+      //
+      MmiEntry = Item;
+      break;
+    }
+  }
+
+  //
+  // If the protocol entry was not found and Create is TRUE, then
+  // allocate a new entry
+  //
+  if ((MmiEntry == NULL) && Create) {
+    MmiEntry = AllocatePool (sizeof(MMI_ENTRY));
+    if (MmiEntry != NULL) {
+      //
+      // Initialize new MMI entry structure
+      //
+      MmiEntry->Signature = MMI_ENTRY_SIGNATURE;
+      CopyGuid ((VOID *)&MmiEntry->HandlerType, HandlerType);
+      InitializeListHead (&MmiEntry->MmiHandlers);
+
+      //
+      // Add it to MMI entry list
+      //
+      InsertTailList (&mMmiEntryList, &MmiEntry->AllEntries);
+    }
+  }
+  return MmiEntry;
+}
+
+/**
+  Manage MMI of a particular type.
+
+  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
+  @param  Context        Points to an optional context buffer.
+  @param  CommBuffer     Points to the optional communication buffer.
+  @param  CommBufferSize Points to the size of the optional communication buffer.
+
+  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING  Interrupt source was processed successfully but not quiesced.
+  @retval EFI_INTERRUPT_PENDING              One or more MMI sources could not be quiesced.
+  @retval EFI_NOT_FOUND                      Interrupt source was not handled or quiesced.
+  @retval EFI_SUCCESS                        Interrupt source was handled and quiesced.
+
+**/
+EFI_STATUS
+EFIAPI
+MmiManage (
+  IN     CONST EFI_GUID  *HandlerType,
+  IN     CONST VOID      *Context         OPTIONAL,
+  IN OUT VOID            *CommBuffer      OPTIONAL,
+  IN OUT UINTN           *CommBufferSize  OPTIONAL
+  )
+{
+  LIST_ENTRY   *Link;
+  LIST_ENTRY   *Head;
+  MMI_ENTRY    *MmiEntry;
+  MMI_HANDLER  *MmiHandler;
+  BOOLEAN      SuccessReturn;
+  EFI_STATUS   Status;
+
+  Status = EFI_NOT_FOUND;
+  SuccessReturn = FALSE;
+  if (HandlerType == NULL) {
+    //
+    // Root MMI handler
+    //
+
+    Head = &mRootMmiHandlerList;
+  } else {
+    //
+    // Non-root MMI handler
+    //
+    MmiEntry = MmCoreFindMmiEntry ((EFI_GUID *) HandlerType, FALSE);
+    if (MmiEntry == NULL) {
+      //
+      // There is no handler registered for this interrupt source
+      //
+      return Status;
+    }
+
+    Head = &MmiEntry->MmiHandlers;
+  }
+
+  for (Link = Head->ForwardLink; Link != Head; Link = Link->ForwardLink) {
+    MmiHandler = CR (Link, MMI_HANDLER, Link, MMI_HANDLER_SIGNATURE);
+
+    Status = MmiHandler->Handler (
+               (EFI_HANDLE) MmiHandler,
+               Context,
+               CommBuffer,
+               CommBufferSize
+               );
+
+    switch (Status) {
+    case EFI_INTERRUPT_PENDING:
+      //
+      // If a handler returns EFI_INTERRUPT_PENDING and HandlerType is not NULL then
+      // no additional handlers will be processed and EFI_INTERRUPT_PENDING will be returned.
+      //
+      if (HandlerType != NULL) {
+        return EFI_INTERRUPT_PENDING;
+      }
+      break;
+
+    case EFI_SUCCESS:
+      //
+      // If at least one of the handlers returns EFI_SUCCESS then the function will return
+      // EFI_SUCCESS. If a handler returns EFI_SUCCESS and HandlerType is not NULL then no
+      // additional handlers will be processed.
+      //
+      if (HandlerType != NULL) {
+        return EFI_SUCCESS;
+      }
+      SuccessReturn = TRUE;
+      break;
+
+    case EFI_WARN_INTERRUPT_SOURCE_QUIESCED:
+      //
+      // If at least one of the handlers returns EFI_WARN_INTERRUPT_SOURCE_QUIESCED
+      // then the function will return EFI_SUCCESS.
+      //
+      SuccessReturn = TRUE;
+      break;
+
+    case EFI_WARN_INTERRUPT_SOURCE_PENDING:
+      //
+      // If all the handlers returned EFI_WARN_INTERRUPT_SOURCE_PENDING
+      // then EFI_WARN_INTERRUPT_SOURCE_PENDING will be returned.
+      //
+      break;
+
+    default:
+      //
+      // Unexpected status code returned.
+      //
+      ASSERT (FALSE);
+      break;
+    }
+  }
+
+  if (SuccessReturn) {
+    Status = EFI_SUCCESS;
+  }
+
+  return Status;
+}
+
+/**
+  Registers a handler to execute within MM.
+
+  @param  Handler        Handler service funtion pointer.
+  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
+  @param  DispatchHandle On return, contains a unique handle which can be used to later unregister the handler function.
+
+  @retval EFI_SUCCESS           Handler register success.
+  @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
+
+**/
+EFI_STATUS
+EFIAPI
+MmiHandlerRegister (
+  IN  EFI_MM_HANDLER_ENTRY_POINT    Handler,
+  IN  CONST EFI_GUID                *HandlerType  OPTIONAL,
+  OUT EFI_HANDLE                    *DispatchHandle
+  )
+{
+  MMI_HANDLER  *MmiHandler;
+  MMI_ENTRY    *MmiEntry;
+  LIST_ENTRY   *List;
+
+  if (Handler == NULL || DispatchHandle == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  MmiHandler = AllocateZeroPool (sizeof (MMI_HANDLER));
+  if (MmiHandler == NULL) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  MmiHandler->Signature = MMI_HANDLER_SIGNATURE;
+  MmiHandler->Handler = Handler;
+
+  if (HandlerType == NULL) {
+    //
+    // This is root MMI handler
+    //
+    MmiEntry = NULL;
+    List = &mRootMmiHandlerList;
+  } else {
+    //
+    // None root MMI handler
+    //
+    MmiEntry = MmCoreFindMmiEntry ((EFI_GUID *) HandlerType, TRUE);
+    if (MmiEntry == NULL) {
+      return EFI_OUT_OF_RESOURCES;
+    }
+
+    List = &MmiEntry->MmiHandlers;
+  }
+
+  MmiHandler->MmiEntry = MmiEntry;
+  InsertTailList (List, &MmiHandler->Link);
+
+  *DispatchHandle = (EFI_HANDLE) MmiHandler;
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Unregister a handler in MM.
+
+  @param  DispatchHandle  The handle that was specified when the handler was registered.
+
+  @retval EFI_SUCCESS           Handler function was successfully unregistered.
+  @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid handle.
+
+**/
+EFI_STATUS
+EFIAPI
+MmiHandlerUnRegister (
+  IN EFI_HANDLE  DispatchHandle
+  )
+{
+  MMI_HANDLER  *MmiHandler;
+  MMI_ENTRY    *MmiEntry;
+
+  MmiHandler = (MMI_HANDLER *) DispatchHandle;
+
+  if (MmiHandler == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if (MmiHandler->Signature != MMI_HANDLER_SIGNATURE) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  MmiEntry = MmiHandler->MmiEntry;
+
+  RemoveEntryList (&MmiHandler->Link);
+  FreePool (MmiHandler);
+
+  if (MmiEntry == NULL) {
+    //
+    // This is root MMI handler
+    //
+    return EFI_SUCCESS;
+  }
+
+  if (IsListEmpty (&MmiEntry->MmiHandlers)) {
+    //
+    // No handler registered for this interrupt now, remove the MMI_ENTRY
+    //
+    RemoveEntryList (&MmiEntry->AllEntries);
+
+    FreePool (MmiEntry);
+  }
+
+  return EFI_SUCCESS;
+}
diff --git a/StandaloneMmPkg/Core/Notify.c b/StandaloneMmPkg/Core/Notify.c
new file mode 100644
index 0000000000..d5fc8f50d1
--- /dev/null
+++ b/StandaloneMmPkg/Core/Notify.c
@@ -0,0 +1,203 @@
+/** @file
+  Support functions for UEFI protocol notification infrastructure.
+
+  Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include "StandaloneMmCore.h"
+
+/**
+  Signal event for every protocol in protocol entry.
+
+  @param  Prot                   Protocol interface
+
+**/
+VOID
+MmNotifyProtocol (
+  IN PROTOCOL_INTERFACE  *Prot
+  )
+{
+  PROTOCOL_ENTRY   *ProtEntry;
+  PROTOCOL_NOTIFY  *ProtNotify;
+  LIST_ENTRY       *Link;
+
+  ProtEntry = Prot->Protocol;
+  for (Link=ProtEntry->Notify.ForwardLink; Link != &ProtEntry->Notify; Link=Link->ForwardLink) {
+    ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
+    ProtNotify->Function (&ProtEntry->ProtocolID, Prot->Interface, Prot->Handle);
+  }
+}
+
+/**
+  Removes Protocol from the protocol list (but not the handle list).
+
+  @param  Handle                 The handle to remove protocol on.
+  @param  Protocol               GUID of the protocol to be moved
+  @param  Interface              The interface of the protocol
+
+  @return Protocol Entry
+
+**/
+PROTOCOL_INTERFACE *
+MmRemoveInterfaceFromProtocol (
+  IN IHANDLE   *Handle,
+  IN EFI_GUID  *Protocol,
+  IN VOID      *Interface
+  )
+{
+  PROTOCOL_INTERFACE  *Prot;
+  PROTOCOL_NOTIFY     *ProtNotify;
+  PROTOCOL_ENTRY      *ProtEntry;
+  LIST_ENTRY          *Link;
+
+  Prot = MmFindProtocolInterface (Handle, Protocol, Interface);
+  if (Prot != NULL) {
+
+    ProtEntry = Prot->Protocol;
+
+    //
+    // If there's a protocol notify location pointing to this entry, back it up one
+    //
+    for(Link = ProtEntry->Notify.ForwardLink; Link != &ProtEntry->Notify; Link=Link->ForwardLink) {
+      ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
+
+      if (ProtNotify->Position == &Prot->ByProtocol) {
+        ProtNotify->Position = Prot->ByProtocol.BackLink;
+      }
+    }
+
+    //
+    // Remove the protocol interface entry
+    //
+    RemoveEntryList (&Prot->ByProtocol);
+  }
+
+  return Prot;
+}
+
+/**
+  Add a new protocol notification record for the request protocol.
+
+  @param  Protocol               The requested protocol to add the notify
+                                 registration
+  @param  Function               Points to the notification function
+  @param  Registration           Returns the registration record
+
+  @retval EFI_SUCCESS            Successfully returned the registration record
+                                 that has been added or unhooked
+  @retval EFI_INVALID_PARAMETER  Protocol is NULL or Registration is NULL
+  @retval EFI_OUT_OF_RESOURCES   Not enough memory resource to finish the request
+  @retval EFI_NOT_FOUND          If the registration is not found when Function == NULL
+
+**/
+EFI_STATUS
+EFIAPI
+MmRegisterProtocolNotify (
+  IN  CONST EFI_GUID     *Protocol,
+  IN  EFI_MM_NOTIFY_FN  Function,
+  OUT VOID               **Registration
+  )
+{
+  PROTOCOL_ENTRY   *ProtEntry;
+  PROTOCOL_NOTIFY  *ProtNotify;
+  LIST_ENTRY       *Link;
+  EFI_STATUS       Status;
+
+  if (Protocol == NULL || Registration == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if (Function == NULL) {
+    //
+    // Get the protocol entry per Protocol
+    //
+    ProtEntry = MmFindProtocolEntry ((EFI_GUID *) Protocol, FALSE);
+    if (ProtEntry != NULL) {
+      ProtNotify = (PROTOCOL_NOTIFY * )*Registration;
+      for (Link = ProtEntry->Notify.ForwardLink;
+           Link != &ProtEntry->Notify;
+           Link = Link->ForwardLink) {
+        //
+        // Compare the notification record
+        //
+        if (ProtNotify == (CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE))){
+          //
+          // If Registration is an existing registration, then unhook it
+          //
+          ProtNotify->Signature = 0;
+          RemoveEntryList (&ProtNotify->Link);
+          FreePool (ProtNotify);
+          return EFI_SUCCESS;
+        }
+      }
+    }
+    //
+    // If the registration is not found
+    //
+    return EFI_NOT_FOUND;
+  }
+
+  ProtNotify = NULL;
+
+  //
+  // Get the protocol entry to add the notification too
+  //
+  ProtEntry = MmFindProtocolEntry ((EFI_GUID *) Protocol, TRUE);
+  if (ProtEntry != NULL) {
+    //
+    // Find whether notification already exist
+    //
+    for (Link = ProtEntry->Notify.ForwardLink;
+         Link != &ProtEntry->Notify;
+         Link = Link->ForwardLink) {
+
+      ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
+      if (CompareGuid (&ProtNotify->Protocol->ProtocolID, Protocol) &&
+          (ProtNotify->Function == Function)) {
+
+        //
+        // Notification already exist
+        //
+        *Registration = ProtNotify;
+
+        return EFI_SUCCESS;
+      }
+    }
+
+    //
+    // Allocate a new notification record
+    //
+    ProtNotify = AllocatePool (sizeof(PROTOCOL_NOTIFY));
+    if (ProtNotify != NULL) {
+      ProtNotify->Signature = PROTOCOL_NOTIFY_SIGNATURE;
+      ProtNotify->Protocol = ProtEntry;
+      ProtNotify->Function = Function;
+      //
+      // Start at the ending
+      //
+      ProtNotify->Position = ProtEntry->Protocols.BackLink;
+
+      InsertTailList (&ProtEntry->Notify, &ProtNotify->Link);
+    }
+  }
+
+  //
+  // Done.  If we have a protocol notify entry, then return it.
+  // Otherwise, we must have run out of resources trying to add one
+  //
+  Status = EFI_OUT_OF_RESOURCES;
+  if (ProtNotify != NULL) {
+    *Registration = ProtNotify;
+    Status = EFI_SUCCESS;
+  }
+  return Status;
+}
diff --git a/StandaloneMmPkg/Core/Page.c b/StandaloneMmPkg/Core/Page.c
new file mode 100644
index 0000000000..ba3e7cea74
--- /dev/null
+++ b/StandaloneMmPkg/Core/Page.c
@@ -0,0 +1,384 @@
+/** @file
+  MM Memory page management functions.
+
+  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include "StandaloneMmCore.h"
+
+#define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \
+  ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size)))
+
+#define TRUNCATE_TO_PAGES(a)  ((a) >> EFI_PAGE_SHIFT)
+
+LIST_ENTRY  mMmMemoryMap = INITIALIZE_LIST_HEAD_VARIABLE (mMmMemoryMap);
+
+UINTN mMapKey;
+
+/**
+  Internal Function. Allocate n pages from given free page node.
+
+  @param  Pages                  The free page node.
+  @param  NumberOfPages          Number of pages to be allocated.
+  @param  MaxAddress             Request to allocate memory below this address.
+
+  @return Memory address of allocated pages.
+
+**/
+UINTN
+InternalAllocPagesOnOneNode (
+  IN OUT FREE_PAGE_LIST  *Pages,
+  IN     UINTN           NumberOfPages,
+  IN     UINTN           MaxAddress
+  )
+{
+  UINTN           Top;
+  UINTN           Bottom;
+  FREE_PAGE_LIST  *Node;
+
+  Top = TRUNCATE_TO_PAGES (MaxAddress + 1 - (UINTN)Pages);
+  if (Top > Pages->NumberOfPages) {
+    Top = Pages->NumberOfPages;
+  }
+  Bottom = Top - NumberOfPages;
+
+  if (Top < Pages->NumberOfPages) {
+    Node = (FREE_PAGE_LIST*)((UINTN)Pages + EFI_PAGES_TO_SIZE (Top));
+    Node->NumberOfPages = Pages->NumberOfPages - Top;
+    InsertHeadList (&Pages->Link, &Node->Link);
+  }
+
+  if (Bottom > 0) {
+    Pages->NumberOfPages = Bottom;
+  } else {
+    RemoveEntryList (&Pages->Link);
+  }
+
+  return (UINTN)Pages + EFI_PAGES_TO_SIZE (Bottom);
+}
+
+/**
+  Internal Function. Allocate n pages from free page list below MaxAddress.
+
+  @param  FreePageList           The free page node.
+  @param  NumberOfPages          Number of pages to be allocated.
+  @param  MaxAddress             Request to allocate memory below this address.
+
+  @return Memory address of allocated pages.
+
+**/
+UINTN
+InternalAllocMaxAddress (
+  IN OUT LIST_ENTRY  *FreePageList,
+  IN     UINTN       NumberOfPages,
+  IN     UINTN       MaxAddress
+  )
+{
+  LIST_ENTRY      *Node;
+  FREE_PAGE_LIST  *Pages;
+
+  for (Node = FreePageList->BackLink; Node != FreePageList; Node = Node->BackLink) {
+    Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
+    if (Pages->NumberOfPages >= NumberOfPages &&
+        (UINTN)Pages + EFI_PAGES_TO_SIZE (NumberOfPages) - 1 <= MaxAddress) {
+      return InternalAllocPagesOnOneNode (Pages, NumberOfPages, MaxAddress);
+    }
+  }
+  return (UINTN)(-1);
+}
+
+/**
+  Internal Function. Allocate n pages from free page list at given address.
+
+  @param  FreePageList           The free page node.
+  @param  NumberOfPages          Number of pages to be allocated.
+  @param  MaxAddress             Request to allocate memory below this address.
+
+  @return Memory address of allocated pages.
+
+**/
+UINTN
+InternalAllocAddress (
+  IN OUT LIST_ENTRY  *FreePageList,
+  IN     UINTN       NumberOfPages,
+  IN     UINTN       Address
+  )
+{
+  UINTN           EndAddress;
+  LIST_ENTRY      *Node;
+  FREE_PAGE_LIST  *Pages;
+
+  if ((Address & EFI_PAGE_MASK) != 0) {
+    return ~Address;
+  }
+
+  EndAddress = Address + EFI_PAGES_TO_SIZE (NumberOfPages);
+  for (Node = FreePageList->BackLink; Node!= FreePageList; Node = Node->BackLink) {
+    Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
+    if ((UINTN)Pages <= Address) {
+      if ((UINTN)Pages + EFI_PAGES_TO_SIZE (Pages->NumberOfPages) < EndAddress) {
+        break;
+      }
+      return InternalAllocPagesOnOneNode (Pages, NumberOfPages, EndAddress);
+    }
+  }
+  return ~Address;
+}
+
+/**
+  Allocates pages from the memory map.
+
+  @param  Type                   The type of allocation to perform.
+  @param  MemoryType             The type of memory to turn the allocated pages
+                                 into.
+  @param  NumberOfPages          The number of pages to allocate.
+  @param  Memory                 A pointer to receive the base allocated memory
+                                 address.
+
+  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
+  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
+  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
+  @retval EFI_SUCCESS            Pages successfully allocated.
+
+**/
+EFI_STATUS
+EFIAPI
+MmInternalAllocatePages (
+  IN  EFI_ALLOCATE_TYPE     Type,
+  IN  EFI_MEMORY_TYPE       MemoryType,
+  IN  UINTN                 NumberOfPages,
+  OUT EFI_PHYSICAL_ADDRESS  *Memory
+  )
+{
+  UINTN  RequestedAddress;
+
+  if (MemoryType != EfiRuntimeServicesCode &&
+      MemoryType != EfiRuntimeServicesData) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if (NumberOfPages > TRUNCATE_TO_PAGES ((UINTN)-1) + 1) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  //
+  // We don't track memory type in MM
+  //
+  RequestedAddress = (UINTN)*Memory;
+  switch (Type) {
+    case AllocateAnyPages:
+      RequestedAddress = (UINTN)(-1);
+    case AllocateMaxAddress:
+      *Memory = InternalAllocMaxAddress (
+                  &mMmMemoryMap,
+                  NumberOfPages,
+                  RequestedAddress
+                  );
+      if (*Memory == (UINTN)-1) {
+        return EFI_OUT_OF_RESOURCES;
+      }
+      break;
+    case AllocateAddress:
+      *Memory = InternalAllocAddress (
+                  &mMmMemoryMap,
+                  NumberOfPages,
+                  RequestedAddress
+                  );
+      if (*Memory != RequestedAddress) {
+        return EFI_NOT_FOUND;
+      }
+      break;
+    default:
+      return EFI_INVALID_PARAMETER;
+  }
+  return EFI_SUCCESS;
+}
+
+/**
+  Allocates pages from the memory map.
+
+  @param  Type                   The type of allocation to perform.
+  @param  MemoryType             The type of memory to turn the allocated pages
+                                 into.
+  @param  NumberOfPages          The number of pages to allocate.
+  @param  Memory                 A pointer to receive the base allocated memory
+                                 address.
+
+  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
+  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
+  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
+  @retval EFI_SUCCESS            Pages successfully allocated.
+
+**/
+EFI_STATUS
+EFIAPI
+MmAllocatePages (
+  IN  EFI_ALLOCATE_TYPE     Type,
+  IN  EFI_MEMORY_TYPE       MemoryType,
+  IN  UINTN                 NumberOfPages,
+  OUT EFI_PHYSICAL_ADDRESS  *Memory
+  )
+{
+  EFI_STATUS  Status;
+
+  Status = MmInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory);
+  return Status;
+}
+
+/**
+  Internal Function. Merge two adjacent nodes.
+
+  @param  First             The first of two nodes to merge.
+
+  @return Pointer to node after merge (if success) or pointer to next node (if fail).
+
+**/
+FREE_PAGE_LIST *
+InternalMergeNodes (
+  IN FREE_PAGE_LIST  *First
+  )
+{
+  FREE_PAGE_LIST  *Next;
+
+  Next = BASE_CR (First->Link.ForwardLink, FREE_PAGE_LIST, Link);
+  ASSERT (
+    TRUNCATE_TO_PAGES ((UINTN)Next - (UINTN)First) >= First->NumberOfPages);
+
+  if (TRUNCATE_TO_PAGES ((UINTN)Next - (UINTN)First) == First->NumberOfPages) {
+    First->NumberOfPages += Next->NumberOfPages;
+    RemoveEntryList (&Next->Link);
+    Next = First;
+  }
+  return Next;
+}
+
+/**
+  Frees previous allocated pages.
+
+  @param  Memory                 Base address of memory being freed.
+  @param  NumberOfPages          The number of pages to free.
+
+  @retval EFI_NOT_FOUND          Could not find the entry that covers the range.
+  @retval EFI_INVALID_PARAMETER  Address not aligned.
+  @return EFI_SUCCESS            Pages successfully freed.
+
+**/
+EFI_STATUS
+EFIAPI
+MmInternalFreePages (
+  IN EFI_PHYSICAL_ADDRESS  Memory,
+  IN UINTN                 NumberOfPages
+  )
+{
+  LIST_ENTRY      *Node;
+  FREE_PAGE_LIST  *Pages;
+
+  if ((Memory & EFI_PAGE_MASK) != 0) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  Pages = NULL;
+  Node = mMmMemoryMap.ForwardLink;
+  while (Node != &mMmMemoryMap) {
+    Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
+    if (Memory < (UINTN)Pages) {
+      break;
+    }
+    Node = Node->ForwardLink;
+  }
+
+  if (Node != &mMmMemoryMap &&
+      Memory + EFI_PAGES_TO_SIZE (NumberOfPages) > (UINTN)Pages) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if (Node->BackLink != &mMmMemoryMap) {
+    Pages = BASE_CR (Node->BackLink, FREE_PAGE_LIST, Link);
+    if ((UINTN)Pages + EFI_PAGES_TO_SIZE (Pages->NumberOfPages) > Memory) {
+      return EFI_INVALID_PARAMETER;
+    }
+  }
+
+  Pages = (FREE_PAGE_LIST*)(UINTN)Memory;
+  Pages->NumberOfPages = NumberOfPages;
+  InsertTailList (Node, &Pages->Link);
+
+  if (Pages->Link.BackLink != &mMmMemoryMap) {
+    Pages = InternalMergeNodes (
+              BASE_CR (Pages->Link.BackLink, FREE_PAGE_LIST, Link)
+              );
+  }
+
+  if (Node != &mMmMemoryMap) {
+    InternalMergeNodes (Pages);
+  }
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Frees previous allocated pages.
+
+  @param  Memory                 Base address of memory being freed.
+  @param  NumberOfPages          The number of pages to free.
+
+  @retval EFI_NOT_FOUND          Could not find the entry that covers the range.
+  @retval EFI_INVALID_PARAMETER  Address not aligned.
+  @return EFI_SUCCESS            Pages successfully freed.
+
+**/
+EFI_STATUS
+EFIAPI
+MmFreePages (
+  IN EFI_PHYSICAL_ADDRESS  Memory,
+  IN UINTN                 NumberOfPages
+  )
+{
+  EFI_STATUS  Status;
+
+  Status = MmInternalFreePages (Memory, NumberOfPages);
+  return Status;
+}
+
+/**
+  Add free MMRAM region for use by memory service.
+
+  @param  MemBase                Base address of memory region.
+  @param  MemLength              Length of the memory region.
+  @param  Type                   Memory type.
+  @param  Attributes             Memory region state.
+
+**/
+VOID
+MmAddMemoryRegion (
+  IN  EFI_PHYSICAL_ADDRESS  MemBase,
+  IN  UINT64                MemLength,
+  IN  EFI_MEMORY_TYPE       Type,
+  IN  UINT64                Attributes
+  )
+{
+  UINTN  AlignedMemBase;
+
+  //
+  // Do not add memory regions that is already allocated, needs testing, or needs ECC initialization
+  //
+  if ((Attributes & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {
+    return;
+  }
+
+  //
+  // Align range on an EFI_PAGE_SIZE boundary
+  //
+  AlignedMemBase = (UINTN)(MemBase + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
+  MemLength -= AlignedMemBase - MemBase;
+  MmFreePages (AlignedMemBase, TRUNCATE_TO_PAGES ((UINTN)MemLength));
+}
diff --git a/StandaloneMmPkg/Core/Pool.c b/StandaloneMmPkg/Core/Pool.c
new file mode 100644
index 0000000000..bdf1258381
--- /dev/null
+++ b/StandaloneMmPkg/Core/Pool.c
@@ -0,0 +1,287 @@
+/** @file
+  SMM Memory pool management functions.
+
+  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include "StandaloneMmCore.h"
+
+LIST_ENTRY  mMmPoolLists[MAX_POOL_INDEX];
+//
+// To cache the MMRAM base since when Loading modules At fixed address feature is enabled,
+// all module is assigned an offset relative the MMRAM base in build time.
+//
+GLOBAL_REMOVE_IF_UNREFERENCED  EFI_PHYSICAL_ADDRESS       gLoadModuleAtFixAddressMmramBase = 0;
+
+/**
+  Called to initialize the memory service.
+
+  @param   MmramRangeCount       Number of MMRAM Regions
+  @param   MmramRanges           Pointer to MMRAM Descriptors
+
+**/
+VOID
+MmInitializeMemoryServices (
+  IN UINTN                 MmramRangeCount,
+  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
+  )
+{
+  UINTN                  Index;
+
+  //
+  // Initialize Pool list
+  //
+  for (Index = sizeof (mMmPoolLists) / sizeof (*mMmPoolLists); Index > 0;) {
+    InitializeListHead (&mMmPoolLists[--Index]);
+  }
+
+
+  //
+  // Initialize free MMRAM regions
+  //
+  for (Index = 0; Index < MmramRangeCount; Index++) {
+    //
+    // BUGBUG: Add legacy MMRAM region is buggy.
+    //
+    if (MmramRanges[Index].CpuStart < BASE_1MB) {
+      continue;
+    }
+    DEBUG ((DEBUG_INFO, "MmAddMemoryRegion %d : 0x%016lx - 0x%016lx\n", Index, MmramRanges[Index].CpuStart, MmramRanges[Index].PhysicalSize));
+    MmAddMemoryRegion (
+      MmramRanges[Index].CpuStart,
+      MmramRanges[Index].PhysicalSize,
+      EfiConventionalMemory,
+      MmramRanges[Index].RegionState
+      );
+  }
+
+}
+
+/**
+  Internal Function. Allocate a pool by specified PoolIndex.
+
+  @param  PoolIndex             Index which indicate the Pool size.
+  @param  FreePoolHdr           The returned Free pool.
+
+  @retval EFI_OUT_OF_RESOURCES   Allocation failed.
+  @retval EFI_SUCCESS            Pool successfully allocated.
+
+**/
+EFI_STATUS
+InternalAllocPoolByIndex (
+  IN  UINTN             PoolIndex,
+  OUT FREE_POOL_HEADER  **FreePoolHdr
+  )
+{
+  EFI_STATUS            Status;
+  FREE_POOL_HEADER      *Hdr;
+  EFI_PHYSICAL_ADDRESS  Address;
+
+  ASSERT (PoolIndex <= MAX_POOL_INDEX);
+  Status = EFI_SUCCESS;
+  Hdr = NULL;
+  if (PoolIndex == MAX_POOL_INDEX) {
+    Status = MmInternalAllocatePages (AllocateAnyPages, EfiRuntimeServicesData, EFI_SIZE_TO_PAGES (MAX_POOL_SIZE << 1), &Address);
+    if (EFI_ERROR (Status)) {
+      return EFI_OUT_OF_RESOURCES;
+    }
+    Hdr = (FREE_POOL_HEADER *) (UINTN) Address;
+  } else if (!IsListEmpty (&mMmPoolLists[PoolIndex])) {
+    Hdr = BASE_CR (GetFirstNode (&mMmPoolLists[PoolIndex]), FREE_POOL_HEADER, Link);
+    RemoveEntryList (&Hdr->Link);
+  } else {
+    Status = InternalAllocPoolByIndex (PoolIndex + 1, &Hdr);
+    if (!EFI_ERROR (Status)) {
+      Hdr->Header.Size >>= 1;
+      Hdr->Header.Available = TRUE;
+      InsertHeadList (&mMmPoolLists[PoolIndex], &Hdr->Link);
+      Hdr = (FREE_POOL_HEADER*)((UINT8*)Hdr + Hdr->Header.Size);
+    }
+  }
+
+  if (!EFI_ERROR (Status)) {
+    Hdr->Header.Size = MIN_POOL_SIZE << PoolIndex;
+    Hdr->Header.Available = FALSE;
+  }
+
+  *FreePoolHdr = Hdr;
+  return Status;
+}
+
+/**
+  Internal Function. Free a pool by specified PoolIndex.
+
+  @param  FreePoolHdr           The pool to free.
+
+  @retval EFI_SUCCESS           Pool successfully freed.
+
+**/
+EFI_STATUS
+InternalFreePoolByIndex (
+  IN FREE_POOL_HEADER  *FreePoolHdr
+  )
+{
+  UINTN  PoolIndex;
+
+  ASSERT ((FreePoolHdr->Header.Size & (FreePoolHdr->Header.Size - 1)) == 0);
+  ASSERT (((UINTN)FreePoolHdr & (FreePoolHdr->Header.Size - 1)) == 0);
+  ASSERT (FreePoolHdr->Header.Size >= MIN_POOL_SIZE);
+
+  PoolIndex = (UINTN) (HighBitSet32 ((UINT32)FreePoolHdr->Header.Size) - MIN_POOL_SHIFT);
+  FreePoolHdr->Header.Available = TRUE;
+  ASSERT (PoolIndex < MAX_POOL_INDEX);
+  InsertHeadList (&mMmPoolLists[PoolIndex], &FreePoolHdr->Link);
+  return EFI_SUCCESS;
+}
+
+/**
+  Allocate pool of a particular type.
+
+  @param  PoolType               Type of pool to allocate.
+  @param  Size                   The amount of pool to allocate.
+  @param  Buffer                 The address to return a pointer to the allocated
+                                 pool.
+
+  @retval EFI_INVALID_PARAMETER  PoolType not valid.
+  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
+  @retval EFI_SUCCESS            Pool successfully allocated.
+
+**/
+EFI_STATUS
+EFIAPI
+MmInternalAllocatePool (
+  IN   EFI_MEMORY_TYPE  PoolType,
+  IN   UINTN            Size,
+  OUT  VOID             **Buffer
+  )
+{
+  POOL_HEADER           *PoolHdr;
+  FREE_POOL_HEADER      *FreePoolHdr;
+  EFI_STATUS            Status;
+  EFI_PHYSICAL_ADDRESS  Address;
+  UINTN                 PoolIndex;
+
+  if (PoolType != EfiRuntimeServicesCode &&
+      PoolType != EfiRuntimeServicesData) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  Size += sizeof (*PoolHdr);
+  if (Size > MAX_POOL_SIZE) {
+    Size = EFI_SIZE_TO_PAGES (Size);
+    Status = MmInternalAllocatePages (AllocateAnyPages, PoolType, Size, &Address);
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
+
+    PoolHdr = (POOL_HEADER*)(UINTN)Address;
+    PoolHdr->Size = EFI_PAGES_TO_SIZE (Size);
+    PoolHdr->Available = FALSE;
+    *Buffer = PoolHdr + 1;
+    return Status;
+  }
+
+  Size = (Size + MIN_POOL_SIZE - 1) >> MIN_POOL_SHIFT;
+  PoolIndex = (UINTN) HighBitSet32 ((UINT32)Size);
+  if ((Size & (Size - 1)) != 0) {
+    PoolIndex++;
+  }
+
+  Status = InternalAllocPoolByIndex (PoolIndex, &FreePoolHdr);
+  if (!EFI_ERROR(Status)) {
+    *Buffer = &FreePoolHdr->Header + 1;
+  }
+  return Status;
+}
+
+/**
+  Allocate pool of a particular type.
+
+  @param  PoolType               Type of pool to allocate.
+  @param  Size                   The amount of pool to allocate.
+  @param  Buffer                 The address to return a pointer to the allocated
+                                 pool.
+
+  @retval EFI_INVALID_PARAMETER  PoolType not valid.
+  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
+  @retval EFI_SUCCESS            Pool successfully allocated.
+
+**/
+EFI_STATUS
+EFIAPI
+MmAllocatePool (
+  IN   EFI_MEMORY_TYPE  PoolType,
+  IN   UINTN            Size,
+  OUT  VOID             **Buffer
+  )
+{
+  EFI_STATUS  Status;
+
+  Status = MmInternalAllocatePool (PoolType, Size, Buffer);
+  return Status;
+}
+
+/**
+  Frees pool.
+
+  @param  Buffer                 The allocated pool entry to free.
+
+  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
+  @retval EFI_SUCCESS            Pool successfully freed.
+
+**/
+EFI_STATUS
+EFIAPI
+MmInternalFreePool (
+  IN VOID  *Buffer
+  )
+{
+  FREE_POOL_HEADER  *FreePoolHdr;
+
+  if (Buffer == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  FreePoolHdr = (FREE_POOL_HEADER*)((POOL_HEADER*)Buffer - 1);
+  ASSERT (!FreePoolHdr->Header.Available);
+
+  if (FreePoolHdr->Header.Size > MAX_POOL_SIZE) {
+    ASSERT (((UINTN)FreePoolHdr & EFI_PAGE_MASK) == 0);
+    ASSERT ((FreePoolHdr->Header.Size & EFI_PAGE_MASK) == 0);
+    return MmInternalFreePages (
+             (EFI_PHYSICAL_ADDRESS)(UINTN)FreePoolHdr,
+             EFI_SIZE_TO_PAGES (FreePoolHdr->Header.Size)
+             );
+  }
+  return InternalFreePoolByIndex (FreePoolHdr);
+}
+
+/**
+  Frees pool.
+
+  @param  Buffer                 The allocated pool entry to free.
+
+  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
+  @retval EFI_SUCCESS            Pool successfully freed.
+
+**/
+EFI_STATUS
+EFIAPI
+MmFreePool (
+  IN VOID  *Buffer
+  )
+{
+  EFI_STATUS  Status;
+
+  Status = MmInternalFreePool (Buffer);
+  return Status;
+}
diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.c b/StandaloneMmPkg/Core/StandaloneMmCore.c
new file mode 100644
index 0000000000..0bb99b9710
--- /dev/null
+++ b/StandaloneMmPkg/Core/StandaloneMmCore.c
@@ -0,0 +1,708 @@
+/** @file
+  MM Core Main Entry Point
+
+  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include "StandaloneMmCore.h"
+
+EFI_STATUS
+MmCoreFfsFindMmDriver (
+  IN  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader
+  );
+
+EFI_STATUS
+MmDispatcher (
+  VOID
+  );
+
+//
+// Globals used to initialize the protocol
+//
+EFI_HANDLE            mMmCpuHandle = NULL;
+
+//
+// Physical pointer to private structure shared between MM IPL and the MM Core
+//
+MM_CORE_PRIVATE_DATA  *gMmCorePrivate;
+
+//
+// MM Core global variable for MM System Table.  Only accessed as a physical structure in MMRAM.
+//
+EFI_MM_SYSTEM_TABLE  gMmCoreMmst = {
+
+  // The table header for the MMST.
+  {
+    MM_MMST_SIGNATURE,
+    EFI_MM_SYSTEM_TABLE_REVISION,
+    sizeof (gMmCoreMmst.Hdr)
+  },
+  // MmFirmwareVendor
+  NULL,
+  // MmFirmwareRevision
+  0,
+  // MmInstallConfigurationTable
+  MmInstallConfigurationTable,
+  // I/O Service
+  {
+    {
+      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5,       // MmMemRead
+      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5        // MmMemWrite
+    },
+    {
+      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5,       // MmIoRead
+      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5        // MmIoWrite
+    }
+  },
+  // Runtime memory services
+  MmAllocatePool,
+  MmFreePool,
+  MmAllocatePages,
+  MmFreePages,
+  // MP service
+  NULL,                          // MmStartupThisAp
+  0,                             // CurrentlyExecutingCpu
+  0,                             // NumberOfCpus
+  NULL,                          // CpuSaveStateSize
+  NULL,                          // CpuSaveState
+  0,                             // NumberOfTableEntries
+  NULL,                          // MmConfigurationTable
+  MmInstallProtocolInterface,
+  MmUninstallProtocolInterface,
+  MmHandleProtocol,
+  MmRegisterProtocolNotify,
+  MmLocateHandle,
+  MmLocateProtocol,
+  MmiManage,
+  MmiHandlerRegister,
+  MmiHandlerUnRegister
+};
+
+//
+// Flag to determine if the platform has performed a legacy boot.
+// If this flag is TRUE, then the runtime code and runtime data associated with the
+// MM IPL are converted to free memory, so the MM Core must guarantee that is
+// does not touch of the code/data associated with the MM IPL if this flag is TRUE.
+//
+BOOLEAN  mInLegacyBoot = FALSE;
+
+//
+// Table of MMI Handlers that are registered by the MM Core when it is initialized
+//
+MM_CORE_MMI_HANDLERS  mMmCoreMmiHandlers[] = {
+  { MmFvDispatchHandler,     &gMmFvDispatchGuid,                 NULL, TRUE  },
+  { MmDriverDispatchHandler, &gEfiEventDxeDispatchGuid,          NULL, TRUE  },
+  { MmReadyToLockHandler,    &gEfiDxeMmReadyToLockProtocolGuid,  NULL, TRUE  },
+  { MmEndOfDxeHandler,       &gEfiEndOfDxeEventGroupGuid,        NULL, FALSE },
+  { MmLegacyBootHandler,     &gEfiEventLegacyBootGuid,           NULL, FALSE },
+  { MmExitBootServiceHandler,&gEfiEventExitBootServicesGuid,     NULL, FALSE },
+  { MmReadyToBootHandler,    &gEfiEventReadyToBootGuid,          NULL, FALSE },
+  { NULL,                    NULL,                               NULL, FALSE },
+};
+
+EFI_SYSTEM_TABLE                *mEfiSystemTable;
+UINTN                           mMmramRangeCount;
+EFI_MMRAM_DESCRIPTOR            *mMmramRanges;
+
+/**
+  Place holder function until all the MM System Table Service are available.
+
+  Note: This function is only used by MMRAM invocation.  It is never used by DXE invocation.
+
+  @param  Arg1                   Undefined
+  @param  Arg2                   Undefined
+  @param  Arg3                   Undefined
+  @param  Arg4                   Undefined
+  @param  Arg5                   Undefined
+
+  @return EFI_NOT_AVAILABLE_YET
+
+**/
+EFI_STATUS
+EFIAPI
+MmEfiNotAvailableYetArg5 (
+  UINTN Arg1,
+  UINTN Arg2,
+  UINTN Arg3,
+  UINTN Arg4,
+  UINTN Arg5
+  )
+{
+  //
+  // This function should never be executed.  If it does, then the architectural protocols
+  // have not been designed correctly.
+  //
+  return EFI_NOT_AVAILABLE_YET;
+}
+
+/**
+  Software MMI handler that is called when a Legacy Boot event is signaled.  The MM
+  Core uses this signal to know that a Legacy Boot has been performed and that
+  gMmCorePrivate that is shared between the UEFI and MM execution environments can
+  not be accessed from MM anymore since that structure is considered free memory by
+  a legacy OS.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
+
+  @return Status Code
+
+**/
+EFI_STATUS
+EFIAPI
+MmLegacyBootHandler (
+  IN     EFI_HANDLE  DispatchHandle,
+  IN     CONST VOID  *Context,        OPTIONAL
+  IN OUT VOID        *CommBuffer,     OPTIONAL
+  IN OUT UINTN       *CommBufferSize  OPTIONAL
+  )
+{
+  EFI_HANDLE  MmHandle;
+  EFI_STATUS  Status = EFI_SUCCESS;
+
+  if (!mInLegacyBoot) {
+    MmHandle = NULL;
+    Status = MmInstallProtocolInterface (
+               &MmHandle,
+               &gEfiEventLegacyBootGuid,
+               EFI_NATIVE_INTERFACE,
+               NULL
+               );
+  }
+  mInLegacyBoot = TRUE;
+  return Status;
+}
+
+/**
+  Software MMI handler that is called when a ExitBoot Service event is signaled.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
+
+  @return Status Code
+
+**/
+EFI_STATUS
+EFIAPI
+MmExitBootServiceHandler (
+  IN     EFI_HANDLE  DispatchHandle,
+  IN     CONST VOID  *Context,        OPTIONAL
+  IN OUT VOID        *CommBuffer,     OPTIONAL
+  IN OUT UINTN       *CommBufferSize  OPTIONAL
+  )
+{
+  EFI_HANDLE  MmHandle;
+  EFI_STATUS  Status = EFI_SUCCESS;
+  STATIC BOOLEAN mInExitBootServices = FALSE;
+
+  if (!mInExitBootServices) {
+    MmHandle = NULL;
+    Status = MmInstallProtocolInterface (
+               &MmHandle,
+               &gEfiEventExitBootServicesGuid,
+               EFI_NATIVE_INTERFACE,
+               NULL
+               );
+  }
+  mInExitBootServices = TRUE;
+  return Status;
+}
+
+/**
+  Software MMI handler that is called when a ExitBoot Service event is signaled.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
+
+  @return Status Code
+
+**/
+EFI_STATUS
+EFIAPI
+MmReadyToBootHandler (
+  IN     EFI_HANDLE  DispatchHandle,
+  IN     CONST VOID  *Context,        OPTIONAL
+  IN OUT VOID        *CommBuffer,     OPTIONAL
+  IN OUT UINTN       *CommBufferSize  OPTIONAL
+  )
+{
+  EFI_HANDLE  MmHandle;
+  EFI_STATUS  Status = EFI_SUCCESS;
+  STATIC BOOLEAN mInReadyToBoot = FALSE;
+
+  if (!mInReadyToBoot) {
+    MmHandle = NULL;
+    Status = MmInstallProtocolInterface (
+               &MmHandle,
+               &gEfiEventReadyToBootGuid,
+               EFI_NATIVE_INTERFACE,
+               NULL
+               );
+  }
+  mInReadyToBoot = TRUE;
+  return Status;
+}
+
+/**
+  Software MMI handler that is called when the DxeMmReadyToLock protocol is added
+  or if gEfiEventReadyToBootGuid is signaled.  This function unregisters the
+  Software SMIs that are nor required after MMRAM is locked and installs the
+  MM Ready To Lock Protocol so MM Drivers are informed that MMRAM is about
+  to be locked.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
+
+  @return Status Code
+
+**/
+EFI_STATUS
+EFIAPI
+MmReadyToLockHandler (
+  IN     EFI_HANDLE  DispatchHandle,
+  IN     CONST VOID  *Context,        OPTIONAL
+  IN OUT VOID        *CommBuffer,     OPTIONAL
+  IN OUT UINTN       *CommBufferSize  OPTIONAL
+  )
+{
+  EFI_STATUS  Status;
+  UINTN       Index;
+  EFI_HANDLE  MmHandle;
+
+  DEBUG ((DEBUG_INFO, "MmReadyToLockHandler\n"));
+
+  //
+  // Unregister MMI Handlers that are no longer required after the MM driver dispatch is stopped
+  //
+  for (Index = 0; mMmCoreMmiHandlers[Index].HandlerType != NULL; Index++) {
+    if (mMmCoreMmiHandlers[Index].UnRegister) {
+      MmiHandlerUnRegister (mMmCoreMmiHandlers[Index].DispatchHandle);
+    }
+  }
+
+  //
+  // Install MM Ready to lock protocol
+  //
+  MmHandle = NULL;
+  Status = MmInstallProtocolInterface (
+             &MmHandle,
+             &gEfiMmReadyToLockProtocolGuid,
+             EFI_NATIVE_INTERFACE,
+             NULL
+             );
+
+  //
+  // Make sure MM CPU I/O 2 Protocol has been installed into the handle database
+  //
+  //Status = MmLocateProtocol (&EFI_MM_CPU_IO_PROTOCOL_GUID, NULL, &Interface);
+
+  //
+  // Print a message on a debug build if the MM CPU I/O 2 Protocol is not installed
+  //
+  //if (EFI_ERROR (Status)) {
+      //DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));
+  //}
+
+
+  //
+  // Assert if the CPU I/O 2 Protocol is not installed
+  //
+  //ASSERT_EFI_ERROR (Status);
+
+  //
+  // Display any drivers that were not dispatched because dependency expression
+  // evaluated to false if this is a debug build
+  //
+  //MmDisplayDiscoveredNotDispatched ();
+
+  return Status;
+}
+
+/**
+  Software MMI handler that is called when the EndOfDxe event is signaled.
+  This function installs the MM EndOfDxe Protocol so MM Drivers are informed that
+  platform code will invoke 3rd part code.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
+
+  @return Status Code
+
+**/
+EFI_STATUS
+EFIAPI
+MmEndOfDxeHandler (
+  IN     EFI_HANDLE  DispatchHandle,
+  IN     CONST VOID  *Context,        OPTIONAL
+  IN OUT VOID        *CommBuffer,     OPTIONAL
+  IN OUT UINTN       *CommBufferSize  OPTIONAL
+  )
+{
+  EFI_STATUS  Status;
+  EFI_HANDLE  MmHandle;
+
+  DEBUG ((DEBUG_INFO, "MmEndOfDxeHandler\n"));
+  //
+  // Install MM EndOfDxe protocol
+  //
+  MmHandle = NULL;
+  Status = MmInstallProtocolInterface (
+             &MmHandle,
+             &gEfiMmEndOfDxeProtocolGuid,
+             EFI_NATIVE_INTERFACE,
+             NULL
+             );
+  return Status;
+}
+
+
+
+/**
+  The main entry point to MM Foundation.
+
+  Note: This function is only used by MMRAM invocation.  It is never used by DXE invocation.
+
+  @param  MmEntryContext           Processor information and functionality
+                                    needed by MM Foundation.
+
+**/
+VOID
+EFIAPI
+MmEntryPoint (
+  IN CONST EFI_MM_ENTRY_CONTEXT  *MmEntryContext
+)
+{
+  EFI_STATUS                  Status;
+  EFI_MM_COMMUNICATE_HEADER  *CommunicateHeader;
+  BOOLEAN                     InLegacyBoot;
+
+  DEBUG ((DEBUG_INFO, "MmEntryPoint ...\n"));
+
+  //
+  // Update MMST using the context
+  //
+  CopyMem (&gMmCoreMmst.MmStartupThisAp, MmEntryContext, sizeof (EFI_MM_ENTRY_CONTEXT));
+
+  //
+  // Call platform hook before Mm Dispatch
+  //
+  //PlatformHookBeforeMmDispatch ();
+
+  //
+  // If a legacy boot has occured, then make sure gMmCorePrivate is not accessed
+  //
+  InLegacyBoot = mInLegacyBoot;
+  if (!InLegacyBoot) {
+    //
+    // TBD: Mark the InMm flag as TRUE
+    //
+    gMmCorePrivate->InMm = TRUE;
+
+    //
+    // Check to see if this is a Synchronous MMI sent through the MM Communication
+    // Protocol or an Asynchronous MMI
+    //
+    if (gMmCorePrivate->CommunicationBuffer != 0) {
+      //
+      // Synchronous MMI for MM Core or request from Communicate protocol
+      //
+      if (!MmIsBufferOutsideMmValid ((UINTN)gMmCorePrivate->CommunicationBuffer, gMmCorePrivate->BufferSize)) {
+        //
+        // If CommunicationBuffer is not in valid address scope, return EFI_INVALID_PARAMETER
+        //
+        gMmCorePrivate->CommunicationBuffer = 0;
+        gMmCorePrivate->ReturnStatus = EFI_INVALID_PARAMETER;
+      } else {
+        CommunicateHeader = (EFI_MM_COMMUNICATE_HEADER *)(UINTN)gMmCorePrivate->CommunicationBuffer;
+        gMmCorePrivate->BufferSize -= OFFSET_OF (EFI_MM_COMMUNICATE_HEADER, Data);
+        //DEBUG ((DEBUG_INFO, "CommunicateHeader->HeaderGuid - %g\n", &CommunicateHeader->HeaderGuid));
+        Status = MmiManage (
+                   &CommunicateHeader->HeaderGuid,
+                   NULL,
+                   CommunicateHeader->Data,
+                   (UINTN *)&gMmCorePrivate->BufferSize
+                   );
+        //
+        // Update CommunicationBuffer, BufferSize and ReturnStatus
+        // Communicate service finished, reset the pointer to CommBuffer to NULL
+        //
+        gMmCorePrivate->BufferSize += OFFSET_OF (EFI_MM_COMMUNICATE_HEADER, Data);
+        gMmCorePrivate->CommunicationBuffer = 0;
+        gMmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
+      }
+    }
+  }
+
+  //
+  // Process Asynchronous MMI sources
+  //
+  MmiManage (NULL, NULL, NULL, NULL);
+
+  //
+  // TBD: Do not use private data structure ?
+  //
+
+  //
+  // If a legacy boot has occured, then make sure gMmCorePrivate is not accessed
+  //
+  if (!InLegacyBoot) {
+    //
+    // Clear the InMm flag as we are going to leave MM
+    //
+    gMmCorePrivate->InMm = FALSE;
+  }
+
+  DEBUG ((DEBUG_INFO, "MmEntryPoint Done\n"));
+}
+
+EFI_STATUS
+EFIAPI
+MmConfigurationMmNotify (
+  IN CONST EFI_GUID *Protocol,
+  IN VOID           *Interface,
+  IN EFI_HANDLE      Handle
+  )
+{
+  EFI_STATUS                      Status;
+  EFI_MM_CONFIGURATION_PROTOCOL  *MmConfiguration;
+
+  DEBUG ((DEBUG_INFO, "MmConfigurationMmNotify(%g) - %x\n", Protocol, Interface));
+
+  MmConfiguration = Interface;
+
+  //
+  // Register the MM Entry Point provided by the MM Core with the MM COnfiguration protocol
+  //
+  Status = MmConfiguration->RegisterMmEntry (MmConfiguration, (EFI_MM_ENTRY_POINT)(UINTN)gMmCorePrivate->MmEntryPoint);
+  ASSERT_EFI_ERROR (Status);
+
+  //
+  // Set flag to indicate that the MM Entry Point has been registered which
+  // means that MMIs are now fully operational.
+  //
+  gMmCorePrivate->MmEntryPointRegistered = TRUE;
+
+  //
+  // Print debug message showing MM Core entry point address.
+  //
+  DEBUG ((DEBUG_INFO, "MM Core registered MM Entry Point address %p\n", (VOID *)(UINTN)gMmCorePrivate->MmEntryPoint));
+  return EFI_SUCCESS;
+}
+
+UINTN
+GetHobListSize (
+  IN VOID *HobStart
+  )
+{
+  EFI_PEI_HOB_POINTERS  Hob;
+
+  ASSERT (HobStart != NULL);
+
+  Hob.Raw = (UINT8 *) HobStart;
+  while (!END_OF_HOB_LIST (Hob)) {
+    Hob.Raw = GET_NEXT_HOB (Hob);
+  }
+  //
+  // Need plus END_OF_HOB_LIST
+  //
+  return (UINTN)Hob.Raw - (UINTN)HobStart + sizeof(EFI_HOB_GENERIC_HEADER);
+}
+
+/**
+  The Entry Point for MM Core
+
+  Install DXE Protocols and reload MM Core into MMRAM and register MM Core
+  EntryPoint on the MMI vector.
+
+  Note: This function is called for both DXE invocation and MMRAM invocation.
+
+  @param  ImageHandle    The firmware allocated handle for the EFI image.
+  @param  SystemTable    A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS    The entry point is executed successfully.
+  @retval Other          Some error occurred when executing this entry point.
+
+**/
+EFI_STATUS
+EFIAPI
+StandaloneMmMain (
+  IN VOID  *HobStart
+  )
+{
+  EFI_STATUS                      Status;
+  UINTN                           Index;
+  VOID                            *MmHobStart;
+  UINTN                           HobSize;
+  VOID                            *Registration;
+  EFI_HOB_GUID_TYPE               *GuidHob;
+  MM_CORE_DATA_HOB_DATA           *DataInHob;
+  EFI_HOB_GUID_TYPE               *MmramRangesHob;
+  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
+  EFI_MMRAM_DESCRIPTOR            *MmramRanges;
+  UINT32                          MmramRangeCount;
+  EFI_HOB_FIRMWARE_VOLUME         *BfvHob;
+
+  ProcessLibraryConstructorList (HobStart, &gMmCoreMmst);
+
+  DEBUG ((DEBUG_INFO, "MmMain - 0x%x\n", HobStart));
+
+  //
+  // Determine if the caller has passed a reference to a MM_CORE_PRIVATE_DATA
+  // structure in the Hoblist. This choice will govern how boot information is
+  // extracted later.
+  //
+  GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);
+  if (GuidHob == NULL) {
+    //
+    // Allocate and zero memory for a MM_CORE_PRIVATE_DATA table and then
+    // initialise it
+    //
+    gMmCorePrivate = (MM_CORE_PRIVATE_DATA *) AllocateRuntimePages(EFI_SIZE_TO_PAGES(sizeof(MM_CORE_PRIVATE_DATA)));
+    SetMem ((VOID *)(UINTN)gMmCorePrivate, sizeof(MM_CORE_PRIVATE_DATA), 0);
+    gMmCorePrivate->Signature = MM_CORE_PRIVATE_DATA_SIGNATURE;
+    gMmCorePrivate->MmEntryPointRegistered = FALSE;
+    gMmCorePrivate->InMm = FALSE;
+    gMmCorePrivate->ReturnStatus = EFI_SUCCESS;
+
+    //
+    // Extract the MMRAM ranges from the MMRAM descriptor HOB
+    //
+    MmramRangesHob = GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid, HobStart);
+    if (MmramRangesHob == NULL)
+      return EFI_UNSUPPORTED;
+
+    MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
+    ASSERT (MmramRangesHobData != NULL);
+    MmramRanges = MmramRangesHobData->Descriptor;
+    MmramRangeCount = MmramRangesHobData->NumberOfMmReservedRegions;
+    ASSERT (MmramRanges);
+    ASSERT (MmramRangeCount);
+
+    //
+    // Copy the MMRAM ranges into MM_CORE_PRIVATE_DATA table just in case any
+    // code relies on them being present there
+    //
+    gMmCorePrivate->MmramRangeCount = MmramRangeCount;
+    gMmCorePrivate->MmramRanges = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (MmramRangeCount * sizeof(EFI_MMRAM_DESCRIPTOR));
+    ASSERT (gMmCorePrivate->MmramRanges != 0);
+    CopyMem ((VOID *)(UINTN)gMmCorePrivate->MmramRanges,
+             MmramRanges,
+             MmramRangeCount * sizeof(EFI_MMRAM_DESCRIPTOR));
+  } else {
+    DataInHob       = GET_GUID_HOB_DATA (GuidHob);
+    gMmCorePrivate = (MM_CORE_PRIVATE_DATA *)(UINTN)DataInHob->Address;
+    MmramRanges     = (EFI_MMRAM_DESCRIPTOR *)(UINTN)gMmCorePrivate->MmramRanges;
+    MmramRangeCount = gMmCorePrivate->MmramRangeCount;
+  }
+
+  //
+  // Print the MMRAM ranges passed by the caller
+  //
+  DEBUG ((DEBUG_INFO, "MmramRangeCount - 0x%x\n", MmramRangeCount));
+  for (Index = 0; Index < MmramRangeCount; Index++) {
+          DEBUG ((DEBUG_INFO, "MmramRanges[%d]: 0x%016lx - 0x%lx\n", Index,
+                  MmramRanges[Index].CpuStart,
+                  MmramRanges[Index].PhysicalSize));
+  }
+
+  //
+  // Copy the MMRAM ranges into private MMRAM
+  //
+  mMmramRangeCount = MmramRangeCount;
+  DEBUG ((DEBUG_INFO, "mMmramRangeCount - 0x%x\n", mMmramRangeCount));
+  mMmramRanges = AllocatePool (mMmramRangeCount * sizeof (EFI_MMRAM_DESCRIPTOR));
+  DEBUG ((DEBUG_INFO, "mMmramRanges - 0x%x\n", mMmramRanges));
+  ASSERT (mMmramRanges != NULL);
+  CopyMem (mMmramRanges, (VOID *)(UINTN)MmramRanges, mMmramRangeCount * sizeof (EFI_MMRAM_DESCRIPTOR));
+
+  //
+  // Get Boot Firmware Volume address from the BFV Hob
+  //
+  BfvHob = GetFirstHob (EFI_HOB_TYPE_FV);
+  if (BfvHob != NULL) {
+    DEBUG ((DEBUG_INFO, "BFV address - 0x%x\n", BfvHob->BaseAddress));
+    DEBUG ((DEBUG_INFO, "BFV size    - 0x%x\n", BfvHob->Length));
+    gMmCorePrivate->StandaloneBfvAddress = BfvHob->BaseAddress;
+  }
+
+  gMmCorePrivate->Mmst          = (EFI_PHYSICAL_ADDRESS)(UINTN)&gMmCoreMmst;
+  gMmCorePrivate->MmEntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)MmEntryPoint;
+
+  //
+  // No need to initialize memory service.
+  // It is done in constructor of StandaloneMmCoreMemoryAllocationLib(),
+  // so that the library linked with StandaloneMmCore can use AllocatePool() in constuctor.
+  //
+
+  DEBUG ((DEBUG_INFO, "MmInstallConfigurationTable For HobList\n"));
+  //
+  // Install HobList
+  //
+  HobSize = GetHobListSize (HobStart);
+  DEBUG ((DEBUG_INFO, "HobSize - 0x%x\n", HobSize));
+  MmHobStart = AllocatePool (HobSize);
+  DEBUG ((DEBUG_INFO, "MmHobStart - 0x%x\n", MmHobStart));
+  ASSERT (MmHobStart != NULL);
+  CopyMem (MmHobStart, HobStart, HobSize);
+  Status = MmInstallConfigurationTable (&gMmCoreMmst, &gEfiHobListGuid, MmHobStart, HobSize);
+  ASSERT_EFI_ERROR (Status);
+
+  //
+  // Register notification for EFI_MM_CONFIGURATION_PROTOCOL registration and
+  // use it to register the MM Foundation entrypoint
+  //
+  DEBUG ((DEBUG_INFO, "MmRegisterProtocolNotify - MmConfigurationMmProtocol\n"));
+  Status = MmRegisterProtocolNotify (
+             &gEfiMmConfigurationProtocolGuid,
+             MmConfigurationMmNotify,
+             &Registration
+             );
+  ASSERT_EFI_ERROR (Status);
+
+  //
+  // Dispatch standalone BFV
+  //
+  DEBUG ((DEBUG_INFO, "Mm Dispatch StandaloneBfvAddress - 0x%08x\n", gMmCorePrivate->StandaloneBfvAddress));
+  if (gMmCorePrivate->StandaloneBfvAddress != 0) {
+    MmCoreFfsFindMmDriver ((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)gMmCorePrivate->StandaloneBfvAddress);
+    MmDispatcher ();
+  }
+
+  //
+  // Register all handlers in the core table
+  //
+  for (Index = 0; mMmCoreMmiHandlers[Index].HandlerType != NULL; Index++) {
+    Status = MmiHandlerRegister(mMmCoreMmiHandlers[Index].Handler,
+                                mMmCoreMmiHandlers[Index].HandlerType,
+                                &mMmCoreMmiHandlers[Index].DispatchHandle);
+    DEBUG ((DEBUG_INFO, "MmiHandlerRegister - GUID %g - Status %d\n", mMmCoreMmiHandlers[Index].HandlerType, Status));
+  }
+
+  DEBUG ((DEBUG_INFO, "MmMain Done!\n"));
+
+  return EFI_SUCCESS;
+}
diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.h b/StandaloneMmPkg/Core/StandaloneMmCore.h
new file mode 100644
index 0000000000..53921b7844
--- /dev/null
+++ b/StandaloneMmPkg/Core/StandaloneMmCore.h
@@ -0,0 +1,903 @@
+/** @file
+  The internal header file includes the common header files, defines
+  internal structure and functions used by MmCore module.
+
+  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef _MM_CORE_H_
+#define _MM_CORE_H_
+
+#include <PiMm.h>
+#include <StandaloneMm.h>
+
+#include <Protocol/DxeMmReadyToLock.h>
+#include <Protocol/MmReadyToLock.h>
+#include <Protocol/MmEndOfDxe.h>
+#include <Protocol/MmCommunication.h>
+#include <Protocol/LoadedImage.h>
+#include <Protocol/MmConfiguration.h>
+
+#include <Guid/Apriori.h>
+#include <Guid/EventGroup.h>
+#include <Guid/EventLegacyBios.h>
+#include <Guid/ZeroGuid.h>
+#include <Guid/MemoryProfile.h>
+#include <Guid/HobList.h>
+#include <Guid/MmFvDispatch.h>
+#include <Guid/MmramMemoryReserve.h>
+
+#include <Library/MmCoreStandaloneEntryPoint.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/PeCoffLib.h>
+#include <Library/CacheMaintenanceLib.h>
+#include <Library/DebugLib.h>
+#include <Library/ReportStatusCodeLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/PcdLib.h>
+//#include <Library/MmCorePlatformHookLib.h>
+#include <Library/MemLib.h>
+#include <Library/HobLib.h>
+
+#include "StandaloneMmCorePrivateData.h"
+
+//
+// Used to build a table of MMI Handlers that the MM Core registers
+//
+typedef struct {
+  EFI_MM_HANDLER_ENTRY_POINT    Handler;
+  EFI_GUID                      *HandlerType;
+  EFI_HANDLE                    DispatchHandle;
+  BOOLEAN                       UnRegister;
+} MM_CORE_MMI_HANDLERS;
+
+//
+// Structure for recording the state of an MM Driver
+//
+#define EFI_MM_DRIVER_ENTRY_SIGNATURE SIGNATURE_32('s', 'd','r','v')
+
+typedef struct {
+  UINTN                           Signature;
+  LIST_ENTRY                      Link;             // mDriverList
+
+  LIST_ENTRY                      ScheduledLink;    // mScheduledQueue
+
+  EFI_HANDLE                      FvHandle;
+  EFI_GUID                        FileName;
+  VOID                            *Pe32Data;
+  UINTN                           Pe32DataSize;
+
+  VOID                            *Depex;
+  UINTN                           DepexSize;
+
+  BOOLEAN                         Before;
+  BOOLEAN                         After;
+  EFI_GUID                        BeforeAfterGuid;
+
+  BOOLEAN                         Dependent;
+  BOOLEAN                         Scheduled;
+  BOOLEAN                         Initialized;
+  BOOLEAN                         DepexProtocolError;
+
+  EFI_HANDLE                      ImageHandle;
+  EFI_LOADED_IMAGE_PROTOCOL       *LoadedImage;
+  //
+  // Image EntryPoint in MMRAM
+  //
+  PHYSICAL_ADDRESS                ImageEntryPoint;
+  //
+  // Image Buffer in MMRAM
+  //
+  PHYSICAL_ADDRESS                ImageBuffer;
+  //
+  // Image Page Number
+  //
+  UINTN                           NumberOfPage;
+} EFI_MM_DRIVER_ENTRY;
+
+#define EFI_HANDLE_SIGNATURE            SIGNATURE_32('h','n','d','l')
+
+///
+/// IHANDLE - contains a list of protocol handles
+///
+typedef struct {
+  UINTN               Signature;
+  /// All handles list of IHANDLE
+  LIST_ENTRY          AllHandles;
+  /// List of PROTOCOL_INTERFACE's for this handle
+  LIST_ENTRY          Protocols;
+  UINTN               LocateRequest;
+} IHANDLE;
+
+#define ASSERT_IS_HANDLE(a)  ASSERT((a)->Signature == EFI_HANDLE_SIGNATURE)
+
+#define PROTOCOL_ENTRY_SIGNATURE        SIGNATURE_32('p','r','t','e')
+
+///
+/// PROTOCOL_ENTRY - each different protocol has 1 entry in the protocol
+/// database.  Each handler that supports this protocol is listed, along
+/// with a list of registered notifies.
+///
+typedef struct {
+  UINTN               Signature;
+  /// Link Entry inserted to mProtocolDatabase
+  LIST_ENTRY          AllEntries;
+  /// ID of the protocol
+  EFI_GUID            ProtocolID;
+  /// All protocol interfaces
+  LIST_ENTRY          Protocols;
+  /// Registerd notification handlers
+  LIST_ENTRY          Notify;
+} PROTOCOL_ENTRY;
+
+#define PROTOCOL_INTERFACE_SIGNATURE  SIGNATURE_32('p','i','f','c')
+
+///
+/// PROTOCOL_INTERFACE - each protocol installed on a handle is tracked
+/// with a protocol interface structure
+///
+typedef struct {
+  UINTN                       Signature;
+  /// Link on IHANDLE.Protocols
+  LIST_ENTRY                  Link;
+  /// Back pointer
+  IHANDLE                     *Handle;
+  /// Link on PROTOCOL_ENTRY.Protocols
+  LIST_ENTRY                  ByProtocol;
+  /// The protocol ID
+  PROTOCOL_ENTRY              *Protocol;
+  /// The interface value
+  VOID                        *Interface;
+} PROTOCOL_INTERFACE;
+
+#define PROTOCOL_NOTIFY_SIGNATURE       SIGNATURE_32('p','r','t','n')
+
+///
+/// PROTOCOL_NOTIFY - used for each register notification for a protocol
+///
+typedef struct {
+  UINTN               Signature;
+  PROTOCOL_ENTRY      *Protocol;
+  /// All notifications for this protocol
+  LIST_ENTRY          Link;
+  /// Notification function
+  EFI_MM_NOTIFY_FN   Function;
+  /// Last position notified
+  LIST_ENTRY          *Position;
+} PROTOCOL_NOTIFY;
+
+//
+// MM Core Global Variables
+//
+extern MM_CORE_PRIVATE_DATA  *gMmCorePrivate;
+extern EFI_MM_SYSTEM_TABLE   gMmCoreMmst;
+extern LIST_ENTRY            gHandleList;
+extern EFI_PHYSICAL_ADDRESS  gLoadModuleAtFixAddressMmramBase;
+
+/**
+  Called to initialize the memory service.
+
+  @param   MmramRangeCount       Number of MMRAM Regions
+  @param   MmramRanges           Pointer to MMRAM Descriptors
+
+**/
+VOID
+MmInitializeMemoryServices (
+  IN UINTN                 MmramRangeCount,
+  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
+  );
+
+/**
+  The MmInstallConfigurationTable() function is used to maintain the list
+  of configuration tables that are stored in the System Management System
+  Table.  The list is stored as an array of (GUID, Pointer) pairs.  The list
+  must be allocated from pool memory with PoolType set to EfiRuntimeServicesData.
+
+  @param  SystemTable      A pointer to the MM System Table (SMST).
+  @param  Guid             A pointer to the GUID for the entry to add, update, or remove.
+  @param  Table            A pointer to the buffer of the table to add.
+  @param  TableSize        The size of the table to install.
+
+  @retval EFI_SUCCESS           The (Guid, Table) pair was added, updated, or removed.
+  @retval EFI_INVALID_PARAMETER Guid is not valid.
+  @retval EFI_NOT_FOUND         An attempt was made to delete a non-existent entry.
+  @retval EFI_OUT_OF_RESOURCES  There is not enough memory available to complete the operation.
+
+**/
+EFI_STATUS
+EFIAPI
+MmInstallConfigurationTable (
+  IN  CONST EFI_MM_SYSTEM_TABLE  *SystemTable,
+  IN  CONST EFI_GUID              *Guid,
+  IN  VOID                        *Table,
+  IN  UINTN                       TableSize
+  );
+
+/**
+  Wrapper function to MmInstallProtocolInterfaceNotify.  This is the public API which
+  Calls the private one which contains a BOOLEAN parameter for notifications
+
+  @param  UserHandle             The handle to install the protocol handler on,
+                                 or NULL if a new handle is to be allocated
+  @param  Protocol               The protocol to add to the handle
+  @param  InterfaceType          Indicates whether Interface is supplied in
+                                 native form.
+  @param  Interface              The interface for the protocol being added
+
+  @return Status code
+
+**/
+EFI_STATUS
+EFIAPI
+MmInstallProtocolInterface (
+  IN OUT EFI_HANDLE     *UserHandle,
+  IN EFI_GUID           *Protocol,
+  IN EFI_INTERFACE_TYPE InterfaceType,
+  IN VOID               *Interface
+  );
+
+/**
+  Allocates pages from the memory map.
+
+  @param  Type                   The type of allocation to perform
+  @param  MemoryType             The type of memory to turn the allocated pages
+                                 into
+  @param  NumberOfPages          The number of pages to allocate
+  @param  Memory                 A pointer to receive the base allocated memory
+                                 address
+
+  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
+  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
+  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
+  @retval EFI_SUCCESS            Pages successfully allocated.
+
+**/
+EFI_STATUS
+EFIAPI
+MmAllocatePages (
+  IN      EFI_ALLOCATE_TYPE         Type,
+  IN      EFI_MEMORY_TYPE           MemoryType,
+  IN      UINTN                     NumberOfPages,
+  OUT     EFI_PHYSICAL_ADDRESS      *Memory
+  );
+
+/**
+  Allocates pages from the memory map.
+
+  @param  Type                   The type of allocation to perform
+  @param  MemoryType             The type of memory to turn the allocated pages
+                                 into
+  @param  NumberOfPages          The number of pages to allocate
+  @param  Memory                 A pointer to receive the base allocated memory
+                                 address
+
+  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
+  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
+  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
+  @retval EFI_SUCCESS            Pages successfully allocated.
+
+**/
+EFI_STATUS
+EFIAPI
+MmInternalAllocatePages (
+  IN      EFI_ALLOCATE_TYPE         Type,
+  IN      EFI_MEMORY_TYPE           MemoryType,
+  IN      UINTN                     NumberOfPages,
+  OUT     EFI_PHYSICAL_ADDRESS      *Memory
+  );
+
+/**
+  Frees previous allocated pages.
+
+  @param  Memory                 Base address of memory being freed
+  @param  NumberOfPages          The number of pages to free
+
+  @retval EFI_NOT_FOUND          Could not find the entry that covers the range
+  @retval EFI_INVALID_PARAMETER  Address not aligned, Address is zero or NumberOfPages is zero.
+  @return EFI_SUCCESS            Pages successfully freed.
+
+**/
+EFI_STATUS
+EFIAPI
+MmFreePages (
+  IN      EFI_PHYSICAL_ADDRESS      Memory,
+  IN      UINTN                     NumberOfPages
+  );
+
+/**
+  Frees previous allocated pages.
+
+  @param  Memory                 Base address of memory being freed
+  @param  NumberOfPages          The number of pages to free
+
+  @retval EFI_NOT_FOUND          Could not find the entry that covers the range
+  @retval EFI_INVALID_PARAMETER  Address not aligned, Address is zero or NumberOfPages is zero.
+  @return EFI_SUCCESS            Pages successfully freed.
+
+**/
+EFI_STATUS
+EFIAPI
+MmInternalFreePages (
+  IN      EFI_PHYSICAL_ADDRESS      Memory,
+  IN      UINTN                     NumberOfPages
+  );
+
+/**
+  Allocate pool of a particular type.
+
+  @param  PoolType               Type of pool to allocate
+  @param  Size                   The amount of pool to allocate
+  @param  Buffer                 The address to return a pointer to the allocated
+                                 pool
+
+  @retval EFI_INVALID_PARAMETER  PoolType not valid
+  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
+  @retval EFI_SUCCESS            Pool successfully allocated.
+
+**/
+EFI_STATUS
+EFIAPI
+MmAllocatePool (
+  IN      EFI_MEMORY_TYPE           PoolType,
+  IN      UINTN                     Size,
+  OUT     VOID                      **Buffer
+  );
+
+/**
+  Allocate pool of a particular type.
+
+  @param  PoolType               Type of pool to allocate
+  @param  Size                   The amount of pool to allocate
+  @param  Buffer                 The address to return a pointer to the allocated
+                                 pool
+
+  @retval EFI_INVALID_PARAMETER  PoolType not valid
+  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
+  @retval EFI_SUCCESS            Pool successfully allocated.
+
+**/
+EFI_STATUS
+EFIAPI
+MmInternalAllocatePool (
+  IN      EFI_MEMORY_TYPE           PoolType,
+  IN      UINTN                     Size,
+  OUT     VOID                      **Buffer
+  );
+
+/**
+  Frees pool.
+
+  @param  Buffer                 The allocated pool entry to free
+
+  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
+  @retval EFI_SUCCESS            Pool successfully freed.
+
+**/
+EFI_STATUS
+EFIAPI
+MmFreePool (
+  IN      VOID                      *Buffer
+  );
+
+/**
+  Frees pool.
+
+  @param  Buffer                 The allocated pool entry to free
+
+  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
+  @retval EFI_SUCCESS            Pool successfully freed.
+
+**/
+EFI_STATUS
+EFIAPI
+MmInternalFreePool (
+  IN      VOID                      *Buffer
+  );
+
+/**
+  Installs a protocol interface into the boot services environment.
+
+  @param  UserHandle             The handle to install the protocol handler on,
+                                 or NULL if a new handle is to be allocated
+  @param  Protocol               The protocol to add to the handle
+  @param  InterfaceType          Indicates whether Interface is supplied in
+                                 native form.
+  @param  Interface              The interface for the protocol being added
+  @param  Notify                 indicates whether notify the notification list
+                                 for this protocol
+
+  @retval EFI_INVALID_PARAMETER  Invalid parameter
+  @retval EFI_OUT_OF_RESOURCES   No enough buffer to allocate
+  @retval EFI_SUCCESS            Protocol interface successfully installed
+
+**/
+EFI_STATUS
+MmInstallProtocolInterfaceNotify (
+  IN OUT EFI_HANDLE     *UserHandle,
+  IN EFI_GUID           *Protocol,
+  IN EFI_INTERFACE_TYPE InterfaceType,
+  IN VOID               *Interface,
+  IN BOOLEAN            Notify
+  );
+
+/**
+  Uninstalls all instances of a protocol:interfacer from a handle.
+  If the last protocol interface is remove from the handle, the
+  handle is freed.
+
+  @param  UserHandle             The handle to remove the protocol handler from
+  @param  Protocol               The protocol, of protocol:interface, to remove
+  @param  Interface              The interface, of protocol:interface, to remove
+
+  @retval EFI_INVALID_PARAMETER  Protocol is NULL.
+  @retval EFI_SUCCESS            Protocol interface successfully uninstalled.
+
+**/
+EFI_STATUS
+EFIAPI
+MmUninstallProtocolInterface (
+  IN EFI_HANDLE       UserHandle,
+  IN EFI_GUID         *Protocol,
+  IN VOID             *Interface
+  );
+
+/**
+  Queries a handle to determine if it supports a specified protocol.
+
+  @param  UserHandle             The handle being queried.
+  @param  Protocol               The published unique identifier of the protocol.
+  @param  Interface              Supplies the address where a pointer to the
+                                 corresponding Protocol Interface is returned.
+
+  @return The requested protocol interface for the handle
+
+**/
+EFI_STATUS
+EFIAPI
+MmHandleProtocol (
+  IN EFI_HANDLE       UserHandle,
+  IN EFI_GUID         *Protocol,
+  OUT VOID            **Interface
+  );
+
+/**
+  Add a new protocol notification record for the request protocol.
+
+  @param  Protocol               The requested protocol to add the notify
+                                 registration
+  @param  Function               Points to the notification function
+  @param  Registration           Returns the registration record
+
+  @retval EFI_INVALID_PARAMETER  Invalid parameter
+  @retval EFI_SUCCESS            Successfully returned the registration record
+                                 that has been added
+
+**/
+EFI_STATUS
+EFIAPI
+MmRegisterProtocolNotify (
+  IN  CONST EFI_GUID              *Protocol,
+  IN  EFI_MM_NOTIFY_FN           Function,
+  OUT VOID                        **Registration
+  );
+
+/**
+  Locates the requested handle(s) and returns them in Buffer.
+
+  @param  SearchType             The type of search to perform to locate the
+                                 handles
+  @param  Protocol               The protocol to search for
+  @param  SearchKey              Dependant on SearchType
+  @param  BufferSize             On input the size of Buffer.  On output the
+                                 size of data returned.
+  @param  Buffer                 The buffer to return the results in
+
+  @retval EFI_BUFFER_TOO_SMALL   Buffer too small, required buffer size is
+                                 returned in BufferSize.
+  @retval EFI_INVALID_PARAMETER  Invalid parameter
+  @retval EFI_SUCCESS            Successfully found the requested handle(s) and
+                                 returns them in Buffer.
+
+**/
+EFI_STATUS
+EFIAPI
+MmLocateHandle (
+  IN EFI_LOCATE_SEARCH_TYPE   SearchType,
+  IN EFI_GUID                 *Protocol   OPTIONAL,
+  IN VOID                     *SearchKey  OPTIONAL,
+  IN OUT UINTN                *BufferSize,
+  OUT EFI_HANDLE              *Buffer
+  );
+
+/**
+  Return the first Protocol Interface that matches the Protocol GUID. If
+  Registration is pasased in return a Protocol Instance that was just add
+  to the system. If Retistration is NULL return the first Protocol Interface
+  you find.
+
+  @param  Protocol               The protocol to search for
+  @param  Registration           Optional Registration Key returned from
+                                 RegisterProtocolNotify()
+  @param  Interface              Return the Protocol interface (instance).
+
+  @retval EFI_SUCCESS            If a valid Interface is returned
+  @retval EFI_INVALID_PARAMETER  Invalid parameter
+  @retval EFI_NOT_FOUND          Protocol interface not found
+
+**/
+EFI_STATUS
+EFIAPI
+MmLocateProtocol (
+  IN  EFI_GUID  *Protocol,
+  IN  VOID      *Registration OPTIONAL,
+  OUT VOID      **Interface
+  );
+
+/**
+  Manage MMI of a particular type.
+
+  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
+  @param  Context        Points to an optional context buffer.
+  @param  CommBuffer     Points to the optional communication buffer.
+  @param  CommBufferSize Points to the size of the optional communication buffer.
+
+  @retval EFI_SUCCESS                        Interrupt source was processed successfully but not quiesced.
+  @retval EFI_INTERRUPT_PENDING              One or more MMI sources could not be quiesced.
+  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING  Interrupt source was not handled or quiesced.
+  @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED Interrupt source was handled and quiesced.
+
+**/
+EFI_STATUS
+EFIAPI
+MmiManage (
+  IN     CONST EFI_GUID           *HandlerType,
+  IN     CONST VOID               *Context         OPTIONAL,
+  IN OUT VOID                     *CommBuffer      OPTIONAL,
+  IN OUT UINTN                    *CommBufferSize  OPTIONAL
+  );
+
+/**
+  Registers a handler to execute within MM.
+
+  @param  Handler        Handler service funtion pointer.
+  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
+  @param  DispatchHandle On return, contains a unique handle which can be used to later unregister the handler function.
+
+  @retval EFI_SUCCESS           Handler register success.
+  @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
+
+**/
+EFI_STATUS
+EFIAPI
+MmiHandlerRegister (
+  IN   EFI_MM_HANDLER_ENTRY_POINT     Handler,
+  IN   CONST EFI_GUID                 *HandlerType  OPTIONAL,
+  OUT  EFI_HANDLE                     *DispatchHandle
+  );
+
+/**
+  Unregister a handler in MM.
+
+  @param  DispatchHandle  The handle that was specified when the handler was registered.
+
+  @retval EFI_SUCCESS           Handler function was successfully unregistered.
+  @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid handle.
+
+**/
+EFI_STATUS
+EFIAPI
+MmiHandlerUnRegister (
+  IN  EFI_HANDLE                      DispatchHandle
+  );
+
+/**
+  This function is the main entry point for an MM handler dispatch
+  or communicate-based callback.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
+
+  @return Status Code
+
+**/
+EFI_STATUS
+EFIAPI
+MmDriverDispatchHandler (
+  IN     EFI_HANDLE               DispatchHandle,
+  IN     CONST VOID               *Context,        OPTIONAL
+  IN OUT VOID                     *CommBuffer,     OPTIONAL
+  IN OUT UINTN                    *CommBufferSize  OPTIONAL
+  );
+
+/**
+  This function is the main entry point for an MM handler dispatch
+  or communicate-based callback.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
+
+  @return Status Code
+
+**/
+EFI_STATUS
+EFIAPI
+MmFvDispatchHandler (
+  IN     EFI_HANDLE               DispatchHandle,
+  IN     CONST VOID               *Context,        OPTIONAL
+  IN OUT VOID                     *CommBuffer,     OPTIONAL
+  IN OUT UINTN                    *CommBufferSize  OPTIONAL
+  );
+
+/**
+  This function is the main entry point for an MM handler dispatch
+  or communicate-based callback.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
+
+  @return Status Code
+
+**/
+EFI_STATUS
+EFIAPI
+MmLegacyBootHandler (
+  IN     EFI_HANDLE               DispatchHandle,
+  IN     CONST VOID               *Context,        OPTIONAL
+  IN OUT VOID                     *CommBuffer,     OPTIONAL
+  IN OUT UINTN                    *CommBufferSize  OPTIONAL
+  );
+
+/**
+  This function is the main entry point for an MM handler dispatch
+  or communicate-based callback.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
+
+  @return Status Code
+
+**/
+EFI_STATUS
+EFIAPI
+MmExitBootServiceHandler (
+  IN     EFI_HANDLE               DispatchHandle,
+  IN     CONST VOID               *Context,        OPTIONAL
+  IN OUT VOID                     *CommBuffer,     OPTIONAL
+  IN OUT UINTN                    *CommBufferSize  OPTIONAL
+  );
+
+/**
+  This function is the main entry point for an MM handler dispatch
+  or communicate-based callback.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
+
+  @return Status Code
+
+**/
+EFI_STATUS
+EFIAPI
+MmReadyToBootHandler (
+  IN     EFI_HANDLE               DispatchHandle,
+  IN     CONST VOID               *Context,        OPTIONAL
+  IN OUT VOID                     *CommBuffer,     OPTIONAL
+  IN OUT UINTN                    *CommBufferSize  OPTIONAL
+  );
+
+/**
+  This function is the main entry point for an MM handler dispatch
+  or communicate-based callback.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
+
+  @return Status Code
+
+**/
+EFI_STATUS
+EFIAPI
+MmReadyToLockHandler (
+  IN     EFI_HANDLE               DispatchHandle,
+  IN     CONST VOID               *Context,        OPTIONAL
+  IN OUT VOID                     *CommBuffer,     OPTIONAL
+  IN OUT UINTN                    *CommBufferSize  OPTIONAL
+  );
+
+/**
+  This function is the main entry point for an MM handler dispatch
+  or communicate-based callback.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
+
+  @return Status Code
+
+**/
+EFI_STATUS
+EFIAPI
+MmEndOfDxeHandler (
+  IN     EFI_HANDLE               DispatchHandle,
+  IN     CONST VOID               *Context,        OPTIONAL
+  IN OUT VOID                     *CommBuffer,     OPTIONAL
+  IN OUT UINTN                    *CommBufferSize  OPTIONAL
+  );
+
+/**
+  Place holder function until all the MM System Table Service are available.
+
+  @param  Arg1                   Undefined
+  @param  Arg2                   Undefined
+  @param  Arg3                   Undefined
+  @param  Arg4                   Undefined
+  @param  Arg5                   Undefined
+
+  @return EFI_NOT_AVAILABLE_YET
+
+**/
+EFI_STATUS
+EFIAPI
+MmEfiNotAvailableYetArg5 (
+  UINTN Arg1,
+  UINTN Arg2,
+  UINTN Arg3,
+  UINTN Arg4,
+  UINTN Arg5
+  );
+
+//
+//Functions used during debug buils
+//
+
+/**
+  Traverse the discovered list for any drivers that were discovered but not loaded
+  because the dependency expressions evaluated to false.
+
+**/
+VOID
+MmDisplayDiscoveredNotDispatched (
+  VOID
+  );
+
+/**
+  Add free MMRAM region for use by memory service.
+
+  @param  MemBase                Base address of memory region.
+  @param  MemLength              Length of the memory region.
+  @param  Type                   Memory type.
+  @param  Attributes             Memory region state.
+
+**/
+VOID
+MmAddMemoryRegion (
+  IN      EFI_PHYSICAL_ADDRESS      MemBase,
+  IN      UINT64                    MemLength,
+  IN      EFI_MEMORY_TYPE           Type,
+  IN      UINT64                    Attributes
+  );
+
+/**
+  Finds the protocol entry for the requested protocol.
+
+  @param  Protocol               The ID of the protocol
+  @param  Create                 Create a new entry if not found
+
+  @return Protocol entry
+
+**/
+PROTOCOL_ENTRY  *
+MmFindProtocolEntry (
+  IN EFI_GUID   *Protocol,
+  IN BOOLEAN    Create
+  );
+
+/**
+  Signal event for every protocol in protocol entry.
+
+  @param  Prot                   Protocol interface
+
+**/
+VOID
+MmNotifyProtocol (
+  IN PROTOCOL_INTERFACE   *Prot
+  );
+
+/**
+  Finds the protocol instance for the requested handle and protocol.
+  Note: This function doesn't do parameters checking, it's caller's responsibility
+  to pass in valid parameters.
+
+  @param  Handle                 The handle to search the protocol on
+  @param  Protocol               GUID of the protocol
+  @param  Interface              The interface for the protocol being searched
+
+  @return Protocol instance (NULL: Not found)
+
+**/
+PROTOCOL_INTERFACE *
+MmFindProtocolInterface (
+  IN IHANDLE        *Handle,
+  IN EFI_GUID       *Protocol,
+  IN VOID           *Interface
+  );
+
+/**
+  Removes Protocol from the protocol list (but not the handle list).
+
+  @param  Handle                 The handle to remove protocol on.
+  @param  Protocol               GUID of the protocol to be moved
+  @param  Interface              The interface of the protocol
+
+  @return Protocol Entry
+
+**/
+PROTOCOL_INTERFACE *
+MmRemoveInterfaceFromProtocol (
+  IN IHANDLE        *Handle,
+  IN EFI_GUID       *Protocol,
+  IN VOID           *Interface
+  );
+
+/**
+  This is the POSTFIX version of the dependency evaluator.  This code does
+  not need to handle Before or After, as it is not valid to call this
+  routine in this case. POSTFIX means all the math is done on top of the stack.
+
+  @param  DriverEntry           DriverEntry element to update.
+
+  @retval TRUE                  If driver is ready to run.
+  @retval FALSE                 If driver is not ready to run or some fatal error
+                                was found.
+
+**/
+BOOLEAN
+MmIsSchedulable (
+  IN  EFI_MM_DRIVER_ENTRY   *DriverEntry
+  );
+
+/**
+  Dump MMRAM information.
+
+**/
+VOID
+DumpMmramInfo (
+  VOID
+  );
+
+extern UINTN                    mMmramRangeCount;
+extern EFI_MMRAM_DESCRIPTOR     *mMmramRanges;
+extern EFI_SYSTEM_TABLE         *mEfiSystemTable;
+
+#endif
diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.inf b/StandaloneMmPkg/Core/StandaloneMmCore.inf
new file mode 100644
index 0000000000..c5eaa14ba3
--- /dev/null
+++ b/StandaloneMmPkg/Core/StandaloneMmCore.inf
@@ -0,0 +1,80 @@
+## @file
+# This module provide an SMM CIS compliant implementation of SMM Core.
+#
+# Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+#
+# 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
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001A
+  BASE_NAME                      = StandaloneMmCore
+  FILE_GUID                      = 6E14B6FD-3600-4DD6-A17A-206B3B6DCE16
+  MODULE_TYPE                    = MM_CORE_STANDALONE
+  VERSION_STRING                 = 1.0
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  ENTRY_POINT                    = StandaloneMmMain
+
+#  VALID_ARCHITECTURES           = IA32 X64 AARCH64
+
+[Sources]
+  StandaloneMmCore.c
+  StandaloneMmCore.h
+  StandaloneMmCorePrivateData.h
+  Page.c
+  Pool.c
+  Handle.c
+  Locate.c
+  Notify.c
+  Dependency.c
+  Dispatcher.c
+  Mmi.c
+  InstallConfigurationTable.c
+  FwVol.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  StandaloneMmPkg/StandaloneMmPkg.dec
+
+[LibraryClasses]
+  BaseLib
+  BaseMemoryLib
+  CacheMaintenanceLib
+  DebugLib
+  FvLib
+  HobLib
+  MemoryAllocationLib
+  MemLib
+  PeCoffLib
+  ReportStatusCodeLib
+  StandaloneMmCoreEntryPoint
+
+[Protocols]
+  gEfiDxeMmReadyToLockProtocolGuid             ## UNDEFINED # SmiHandlerRegister
+  gEfiMmReadyToLockProtocolGuid                ## PRODUCES
+  gEfiMmEndOfDxeProtocolGuid                   ## PRODUCES
+  gEfiLoadedImageProtocolGuid                   ## PRODUCES
+  gEfiMmConfigurationProtocolGuid               ## CONSUMES
+
+[Guids]
+  gAprioriGuid                                  ## SOMETIMES_CONSUMES   ## File
+  gEfiEventDxeDispatchGuid                      ## PRODUCES             ## GUID # SmiHandlerRegister
+  gEfiEndOfDxeEventGroupGuid                    ## PRODUCES             ## GUID # SmiHandlerRegister
+  ## SOMETIMES_CONSUMES   ## GUID # Locate protocol
+  ## SOMETIMES_PRODUCES   ## GUID # SmiHandlerRegister
+  gEdkiiMemoryProfileGuid
+  gZeroGuid                                     ## SOMETIMES_CONSUMES   ## GUID
+  gEfiHobListGuid
+  gMmCoreDataHobGuid
+  gMmFvDispatchGuid
+  gEfiEventLegacyBootGuid
+  gEfiEventExitBootServicesGuid
+  gEfiEventReadyToBootGuid
diff --git a/StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h b/StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
new file mode 100644
index 0000000000..faedf3ff2d
--- /dev/null
+++ b/StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
@@ -0,0 +1,66 @@
+/** @file
+  The internal header file that declared a data structure that is shared
+  between the MM IPL and the MM Core.
+
+  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef _STANDALONE_MM_CORE_PRIVATE_DATA_H_
+#define _STANDALONE_MM_CORE_PRIVATE_DATA_H_
+
+#include <Guid/MmCoreData.h>
+
+//
+// Page management
+//
+
+typedef struct {
+  LIST_ENTRY  Link;
+  UINTN       NumberOfPages;
+} FREE_PAGE_LIST;
+
+extern LIST_ENTRY  mMmMemoryMap;
+
+//
+// Pool management
+//
+
+//
+// MIN_POOL_SHIFT must not be less than 5
+//
+#define MIN_POOL_SHIFT  6
+#define MIN_POOL_SIZE   (1 << MIN_POOL_SHIFT)
+
+//
+// MAX_POOL_SHIFT must not be less than EFI_PAGE_SHIFT - 1
+//
+#define MAX_POOL_SHIFT  (EFI_PAGE_SHIFT - 1)
+#define MAX_POOL_SIZE   (1 << MAX_POOL_SHIFT)
+
+//
+// MAX_POOL_INDEX are calculated by maximum and minimum pool sizes
+//
+#define MAX_POOL_INDEX  (MAX_POOL_SHIFT - MIN_POOL_SHIFT + 1)
+
+typedef struct {
+  UINTN        Size;
+  BOOLEAN      Available;
+} POOL_HEADER;
+
+typedef struct {
+  POOL_HEADER  Header;
+  LIST_ENTRY   Link;
+} FREE_POOL_HEADER;
+
+extern LIST_ENTRY  mMmPoolLists[MAX_POOL_INDEX];
+
+#endif
diff --git a/StandaloneMmPkg/Include/Guid/MmFvDispatch.h b/StandaloneMmPkg/Include/Guid/MmFvDispatch.h
new file mode 100644
index 0000000000..fb194d3474
--- /dev/null
+++ b/StandaloneMmPkg/Include/Guid/MmFvDispatch.h
@@ -0,0 +1,38 @@
+/** @file
+  GUIDs for MM Event.
+
+Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+
+This program and the accompanying materials are licensed and made available under
+the terms and conditions of the BSD License that accompanies this distribution.
+The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php.
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef __MM_FV_DISPATCH_H__
+#define __MM_FV_DISPATCH_H__
+
+#define MM_FV_DISPATCH_GUID \
+  { 0xb65694cc, 0x9e3, 0x4c3b, { 0xb5, 0xcd, 0x5, 0xf4, 0x4d, 0x3c, 0xdb, 0xff }}
+
+extern EFI_GUID gMmFvDispatchGuid;
+
+#pragma pack(1)
+typedef struct {
+  EFI_PHYSICAL_ADDRESS  Address;
+  UINT64                Size;
+} EFI_MM_COMMUNICATE_FV_DISPATCH_DATA;
+
+typedef struct {
+  EFI_GUID                              HeaderGuid;
+  UINTN                                 MessageLength;
+  EFI_MM_COMMUNICATE_FV_DISPATCH_DATA   Data;
+} EFI_MM_COMMUNICATE_FV_DISPATCH;
+#pragma pack()
+
+#endif
diff --git a/StandaloneMmPkg/Include/StandaloneMm.h b/StandaloneMmPkg/Include/StandaloneMm.h
new file mode 100644
index 0000000000..0e420315bb
--- /dev/null
+++ b/StandaloneMmPkg/Include/StandaloneMm.h
@@ -0,0 +1,36 @@
+/** @file
+  Standalone MM.
+
+Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+
+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
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef _STANDALONE_MM_H_
+#define _STANDALONE_MM_H_
+
+#include <PiMm.h>
+
+typedef
+EFI_STATUS
+(EFIAPI *MM_IMAGE_ENTRY_POINT) (
+  IN EFI_HANDLE            ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
+  );
+
+typedef
+EFI_STATUS
+(EFIAPI *STANDALONE_MM_FOUNDATION_ENTRY_POINT) (
+  IN VOID  *HobStart
+  );
+
+#endif
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 14/18] StandaloneMmPkg: Describe the declaration, definition and fdf files.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (12 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 13/18] StandaloneMmPkg/Core: Implementation of Standalone MM Core Module Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-18 19:50   ` Daniil Egranov
  2018-04-30 19:32   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 15/18] ArmPkg: Extra action to update permissions for S-ELO MM Image Supreeth Venkatesh
                   ` (4 subsequent siblings)
  18 siblings, 2 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

This patch describes the package declarations, definitions and firmware
device files for creating standalone management mode image with
core/foundation and drivers.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 StandaloneMmPkg/StandaloneMmPkg.dec |  47 +++++++++
 StandaloneMmPkg/StandaloneMmPkg.dsc | 132 ++++++++++++++++++++++++++
 StandaloneMmPkg/StandaloneMmPkg.fdf | 184 ++++++++++++++++++++++++++++++++++++
 3 files changed, 363 insertions(+)
 create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dec
 create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dsc
 create mode 100644 StandaloneMmPkg/StandaloneMmPkg.fdf

diff --git a/StandaloneMmPkg/StandaloneMmPkg.dec b/StandaloneMmPkg/StandaloneMmPkg.dec
new file mode 100644
index 0000000000..36521bb039
--- /dev/null
+++ b/StandaloneMmPkg/StandaloneMmPkg.dec
@@ -0,0 +1,47 @@
+## @file
+# This package is a platform package that provide platform module/library
+# required by Standalone MM platform.
+#
+# Copyright (c) 2016-2017, ARM Ltd. All rights reserved.<BR>
+#
+# 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
+#
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+#
+
+[Defines]
+  DEC_SPECIFICATION              = 0x0001001A
+  PACKAGE_NAME                   = StandaloneMmPkg
+  PACKAGE_GUID                   = 2AE82968-7769-4A85-A5BC-A0954CE54A5C
+  PACKAGE_VERSION                = 1.0
+
+[Includes]
+  Include
+
+[LibraryClasses]
+
+[Guids]
+  gStandaloneMmPkgTokenSpaceGuid           = { 0x18fe7632, 0xf5c8, 0x4e63, { 0x8d, 0xe8, 0x17, 0xa5, 0x5c, 0x59, 0x13, 0xbd }}
+  gMpInformationHobGuid                    = { 0xba33f15d, 0x4000, 0x45c1, { 0x8e, 0x88, 0xf9, 0x16, 0x92, 0xd4, 0x57, 0xe3 }}
+  gMmFvDispatchGuid                        = { 0xb65694cc, 0x09e3, 0x4c3b, { 0xb5, 0xcd, 0x05, 0xf4, 0x4d, 0x3c, 0xdb, 0xff }}
+
+  ## Include/Guid/MmCoreData.h
+  gMmCoreDataHobGuid                       = { 0xa160bf99, 0x2aa4, 0x4d7d, { 0x99, 0x93, 0x89, 0x9c, 0xb1, 0x2d, 0xf3, 0x76 }}
+
+  ## Include/Guid/MmramMemoryReserve.h
+  gEfiMmPeiMmramMemoryReserveGuid          = { 0x0703f912, 0xbf8d, 0x4e2a, { 0xbe, 0x07, 0xab, 0x27, 0x25, 0x25, 0xc5, 0x92 }}
+
+  gEfiStandaloneMmNonSecureBufferGuid      = { 0xf00497e3, 0xbfa2, 0x41a1, { 0x9d, 0x29, 0x54, 0xc2, 0xe9, 0x37, 0x21, 0xc5 }}
+  gEfiArmTfCpuDriverEpDescriptorGuid       = { 0x6ecbd5a1, 0xc0f8, 0x4702, { 0x83, 0x01, 0x4f, 0xc2, 0xc5, 0x47, 0x0a, 0x51 }}
+
+[PcdsFeatureFlag]
+  gStandaloneMmPkgTokenSpaceGuid.PcdStandaloneMmEnable|FALSE|BOOLEAN|0x00000001
+
+[Protocols]
+  gEfiMmConfigurationProtocolGuid          = { 0xc109319, 0xc149, 0x450e,  { 0xa3, 0xe3, 0xb9, 0xba, 0xdd, 0x9d, 0xc3, 0xa4 }}
+
diff --git a/StandaloneMmPkg/StandaloneMmPkg.dsc b/StandaloneMmPkg/StandaloneMmPkg.dsc
new file mode 100644
index 0000000000..8cc996f6b0
--- /dev/null
+++ b/StandaloneMmPkg/StandaloneMmPkg.dsc
@@ -0,0 +1,132 @@
+## @file
+# Standalone MM Platform.
+#
+# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
+#
+#    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
+#
+#    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+##
+
+################################################################################
+#
+# Defines Section - statements that will be processed to create a Makefile.
+#
+################################################################################
+[Defines]
+  PLATFORM_NAME                  = StandaloneMm
+  PLATFORM_GUID                  = 9A4BBA60-B4F9-47C7-9258-3BD77CAE9322
+  PLATFORM_VERSION               = 1.0
+  DSC_SPECIFICATION              = 0x00010011
+  OUTPUT_DIRECTORY               = Build/StandaloneMmPkg
+  SUPPORTED_ARCHITECTURES        = IA32|X64|AARCH64
+  BUILD_TARGETS                  = DEBUG|RELEASE
+  SKUID_IDENTIFIER               = DEFAULT
+  FLASH_DEFINITION               = StandaloneMmPkg/StandaloneMmPkg.fdf
+  DEFINE DEBUG_MESSAGE           = TRUE
+
+  # LzmaF86
+  DEFINE COMPRESSION_TOOL_GUID   = D42AE6BD-1352-4bfb-909A-CA72A6EAE889
+
+################################################################################
+#
+# Library Class section - list of all Library Classes needed by this Platform.
+#
+################################################################################
+[LibraryClasses]
+  #
+  # Basic
+  #
+  BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
+  BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
+  DebugLib|MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort.inf
+  DebugPrintErrorLevelLib|MdePkg/Library/BaseDebugPrintErrorLevelLib/BaseDebugPrintErrorLevelLib.inf
+  FvLib|StandaloneMmPkg/Library/FvLib/FvLib.inf
+  IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
+  MemoryAllocationLib|StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
+  PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
+  PeCoffLib|MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf
+  PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf
+  ReportStatusCodeLib|MdePkg/Library/BaseReportStatusCodeLibNull/BaseReportStatusCodeLibNull.inf
+
+  #
+  # Entry point
+  #
+  StandaloneMmDriverEntryPoint|StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf
+
+[LibraryClasses.AARCH64]
+  ArmLib|ArmPkg/Library/ArmLib/ArmBaseLib.inf
+  ArmMmuLib|ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
+  ArmSvcLib|ArmPkg/Library/ArmSvcLib/ArmSvcLib.inf
+  CacheMaintenanceLib|ArmPkg/Library/ArmCacheMaintenanceLib/ArmCacheMaintenanceLib.inf
+  HobLib|StandaloneMmPkg/Library/HobLib/HobLib.inf
+  MemLib|StandaloneMmPkg/Library/MemLib/MemLib.inf
+  PeCoffExtraActionLib|ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.inf
+  PL011UartLib|ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.inf
+  # ARM PL011 UART Driver
+  SerialPortLib|ArmPlatformPkg/Library/PL011SerialPortLib/PL011SerialPortLib.inf
+
+  StandaloneMmCoreEntryPoint|StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
+
+################################################################################
+#
+# Pcd Section - list of all EDK II PCD Entries defined by this Platform
+#
+################################################################################
+[PcdsFeatureFlag]
+  gStandaloneMmPkgTokenSpaceGuid.PcdStandaloneMmEnable|TRUE
+
+[PcdsFixedAtBuild]
+  gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x800000CF
+  gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0xff
+  gEfiMdePkgTokenSpaceGuid.PcdReportStatusCodePropertyMask|0x0f
+
+[PcdsFixedAtBuild.AARCH64]
+  ## PL011 - Serial Terminal
+  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialRegisterBase|0x1c0b0000
+  gEfiMdePkgTokenSpaceGuid.PcdUartDefaultBaudRate|115200
+
+###################################################################################################
+#
+# Components Section - list of the modules and components that will be processed by compilation
+#                      tools and the EDK II tools to generate PE32/PE32+/Coff image files.
+#
+# Note: The EDK II DSC file is not used to specify how compiled binary images get placed
+#       into firmware volume images. This section is just a list of modules to compile from
+#       source into UEFI-compliant binaries.
+#       It is the FDF file that contains information on combining binary files into firmware
+#       volume images, whose concept is beyond UEFI and is described in PI specification.
+#       Binary modules do not need to be listed in this section, as they should be
+#       specified in the FDF file. For example: Shell binary (Shell_Full.efi), FAT binary (Fat.efi),
+#       Logo (Logo.bmp), and etc.
+#       There may also be modules listed in this section that are not required in the FDF file,
+#       When a module listed here is excluded from FDF file, then UEFI-compliant binary will be
+#       generated for it, but the binary will not be put into any firmware volume.
+#
+###################################################################################################
+[Components.common]
+  #
+  # MM Core
+  #
+  StandaloneMmPkg/Core/StandaloneMmCore.inf
+
+[Components.AARCH64]
+  StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
+
+###################################################################################################
+#
+# BuildOptions Section - Define the module specific tool chain flags that should be used as
+#                        the default flags for a module. These flags are appended to any
+#                        standard flags that are defined by the build process. They can be
+#                        applied for any modules or only those modules with the specific
+#                        module style (EDK or EDKII) specified in [Components] section.
+#
+###################################################################################################
+[BuildOptions.Common]
+GCC:*_*_*_DLINK_FLAGS = -z common-page-size=0x1000
diff --git a/StandaloneMmPkg/StandaloneMmPkg.fdf b/StandaloneMmPkg/StandaloneMmPkg.fdf
new file mode 100644
index 0000000000..7a22a51b4c
--- /dev/null
+++ b/StandaloneMmPkg/StandaloneMmPkg.fdf
@@ -0,0 +1,184 @@
+#
+#  Copyright (c) 2011 - 2017, ARM Limited. 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
+#
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+
+################################################################################
+#
+# FD Section
+# The [FD] Section is made up of the definition statements and a
+# description of what goes into  the Flash Device Image.  Each FD section
+# defines one flash "device" image.  A flash device image may be one of
+# the following: Removable media bootable image (like a boot floppy
+# image,) an Option ROM image (that would be "flashed" into an add-in
+# card,) a System "Flash"  image (that would be burned into a system's
+# flash) or an Update ("Capsule") image that will be used to update and
+# existing system flash.
+#
+################################################################################
+
+[FD.]
+!ifdef ARM_FVP_RUN_NORFLASH
+BaseAddress   = 0x08000000|gArmTokenSpaceGuid.PcdFdBaseAddress  # The base address of the Firmware in Flash0.
+!else
+BaseAddress   = 0xff200000|gArmTokenSpaceGuid.PcdFdBaseAddress  # UEFI in DRAM + 128MB.
+!endif
+Size          = 0x00e00000|gArmTokenSpaceGuid.PcdFdSize         # The size in bytes of the device (64MiB).
+ErasePolarity = 1
+
+# This one is tricky, it must be: BlockSize * NumBlocks = Size
+BlockSize     = 0x00001000
+NumBlocks     = 0x0e00
+
+0x00000000|0x00280000
+gArmTokenSpaceGuid.PcdFvBaseAddress|gArmTokenSpaceGuid.PcdFvSize
+FV = FVMAIN_COMPACT
+
+[FV.FVMAIN_COMPACT]
+FvAlignment        = 16
+ERASE_POLARITY     = 1
+MEMORY_MAPPED      = TRUE
+STICKY_WRITE       = TRUE
+LOCK_CAP           = TRUE
+LOCK_STATUS        = TRUE
+WRITE_DISABLED_CAP = TRUE
+WRITE_ENABLED_CAP  = TRUE
+WRITE_STATUS       = TRUE
+WRITE_LOCK_CAP     = TRUE
+WRITE_LOCK_STATUS  = TRUE
+READ_DISABLED_CAP  = TRUE
+READ_ENABLED_CAP   = TRUE
+READ_STATUS        = TRUE
+READ_LOCK_CAP      = TRUE
+READ_LOCK_STATUS   = TRUE
+
+INF StandaloneMmPkg/Core/StandaloneMmCore.inf
+INF StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
+
+################################################################################
+#
+# Rules are use with the [FV] section's module INF type to define
+# how an FFS file is created for a given INF file. The following Rule are the default
+# rules for the different module type. User can add the customized rules to define the
+# content of the FFS file.
+#
+################################################################################
+
+
+############################################################################
+# Example of a DXE_DRIVER FFS file with a Checksum encapsulation section   #
+############################################################################
+#
+#[Rule.Common.DXE_DRIVER]
+#  FILE DRIVER = $(NAMED_GUID) {
+#    DXE_DEPEX    DXE_DEPEX               Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
+#    COMPRESS PI_STD {
+#      GUIDED {
+#        PE32     PE32                    $(INF_OUTPUT)/$(MODULE_NAME).efi
+#        UI       STRING="$(MODULE_NAME)" Optional
+#        VERSION  STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
+#      }
+#    }
+#  }
+#
+############################################################################
+
+[Rule.Common.SEC]
+  FILE SEC = $(NAMED_GUID) RELOCS_STRIPPED FIXED {
+    TE  TE Align = Auto                 $(INF_OUTPUT)/$(MODULE_NAME).efi
+  }
+
+[Rule.Common.MM_CORE_STANDALONE]
+  FILE SEC = $(NAMED_GUID) RELOCS_STRIPPED FIXED {
+    PE32  PE32 Align = Auto             $(INF_OUTPUT)/$(MODULE_NAME).efi
+  }
+
+[Rule.Common.MM_STANDALONE]
+  FILE MM_STANDALONE = $(NAMED_GUID) {
+    SMM_DEPEX SMM_DEPEX Optional       $(INF_OUTPUT)/$(MODULE_NAME).depex
+    PE32      PE32                     $(INF_OUTPUT)/$(MODULE_NAME).efi
+    UI        STRING="$(MODULE_NAME)" Optional
+    VERSION   STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
+  }
+
+[Rule.Common.PEI_CORE]
+  FILE PEI_CORE = $(NAMED_GUID) FIXED {
+    TE     TE Align = Auto              $(INF_OUTPUT)/$(MODULE_NAME).efi
+    UI     STRING ="$(MODULE_NAME)" Optional
+  }
+
+[Rule.Common.PEIM]
+  FILE PEIM = $(NAMED_GUID) FIXED {
+     PEI_DEPEX PEI_DEPEX Optional       $(INF_OUTPUT)/$(MODULE_NAME).depex
+     TE       TE Align = Auto           $(INF_OUTPUT)/$(MODULE_NAME).efi
+     UI       STRING="$(MODULE_NAME)" Optional
+  }
+
+[Rule.Common.PEIM.TIANOCOMPRESSED]
+  FILE PEIM = $(NAMED_GUID) DEBUG_MYTOOLS_IA32 {
+    PEI_DEPEX PEI_DEPEX Optional        $(INF_OUTPUT)/$(MODULE_NAME).depex
+    GUIDED A31280AD-481E-41B6-95E8-127F4C984779 PROCESSING_REQUIRED = TRUE {
+      PE32      PE32                    $(INF_OUTPUT)/$(MODULE_NAME).efi
+      UI        STRING="$(MODULE_NAME)" Optional
+    }
+  }
+
+[Rule.Common.DXE_CORE]
+  FILE DXE_CORE = $(NAMED_GUID) {
+    PE32     PE32                       $(INF_OUTPUT)/$(MODULE_NAME).efi
+    UI       STRING="$(MODULE_NAME)" Optional
+  }
+
+[Rule.Common.UEFI_DRIVER]
+  FILE DRIVER = $(NAMED_GUID) {
+    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
+    PE32         PE32                   $(INF_OUTPUT)/$(MODULE_NAME).efi
+    UI           STRING="$(MODULE_NAME)" Optional
+  }
+
+[Rule.Common.DXE_DRIVER]
+  FILE DRIVER = $(NAMED_GUID) {
+    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
+    PE32         PE32                   $(INF_OUTPUT)/$(MODULE_NAME).efi
+    UI           STRING="$(MODULE_NAME)" Optional
+  }
+
+[Rule.Common.DXE_RUNTIME_DRIVER]
+  FILE DRIVER = $(NAMED_GUID) {
+    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
+    PE32         PE32                   $(INF_OUTPUT)/$(MODULE_NAME).efi
+    UI           STRING="$(MODULE_NAME)" Optional
+  }
+
+[Rule.Common.UEFI_APPLICATION]
+  FILE APPLICATION = $(NAMED_GUID) {
+    UI     STRING ="$(MODULE_NAME)"     Optional
+    PE32   PE32                         $(INF_OUTPUT)/$(MODULE_NAME).efi
+  }
+
+[Rule.Common.UEFI_DRIVER.BINARY]
+  FILE DRIVER = $(NAMED_GUID) {
+    DXE_DEPEX DXE_DEPEX Optional      |.depex
+    PE32      PE32                    |.efi
+    UI        STRING="$(MODULE_NAME)" Optional
+    VERSION   STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
+  }
+
+[Rule.Common.UEFI_APPLICATION.BINARY]
+  FILE APPLICATION = $(NAMED_GUID) {
+    PE32      PE32                    |.efi
+    UI        STRING="$(MODULE_NAME)" Optional
+    VERSION   STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
+  }
+
+[Rule.Common.USER_DEFINED.ACPITABLE]
+  FILE FREEFORM = $(NAMED_GUID) {
+    RAW ASL                |.aml
+  }
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 15/18] ArmPkg: Extra action to update permissions for S-ELO MM Image.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (13 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 14/18] StandaloneMmPkg: Describe the declaration, definition and fdf files Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-30 19:49   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 16/18] BaseTools/AutoGen: Update header file for MM modules Supreeth Venkatesh
                   ` (3 subsequent siblings)
  18 siblings, 1 reply; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

The Standalone MM drivers runs in S-EL0 in AArch64 on ARM Standard
Platforms and is deployed during SEC phase. The memory allocated to the
Standalone MM drivers should be marked as RO+X.

During PE/COFF Image section parsing, this patch implements extra action
"UpdatePeCoffPermissions" to request the privileged firmware in EL3 to
update the permissions.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 .../DebugPeCoffExtraActionLib.c                    | 185 +++++++++++++++++++--
 .../DebugPeCoffExtraActionLib.inf                  |   7 +
 2 files changed, 181 insertions(+), 11 deletions(-)

diff --git a/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.c b/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.c
index f298e58cdf..c87aaf05c7 100644
--- a/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.c
+++ b/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.c
@@ -15,14 +15,165 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 **/
 
 #include <PiDxe.h>
-#include <Library/PeCoffLib.h>
 
+#include <Library/ArmMmuLib.h>
 #include <Library/BaseLib.h>
-#include <Library/DebugLib.h>
 #include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/PcdLib.h>
+#include <Library/PeCoffLib.h>
 #include <Library/PeCoffExtraActionLib.h>
 #include <Library/PrintLib.h>
 
+typedef RETURN_STATUS (*REGION_PERMISSION_UPDATE_FUNC) (
+  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
+  IN  UINT64                    Length
+  );
+
+STATIC
+RETURN_STATUS
+UpdatePeCoffPermissions (
+  IN  CONST PE_COFF_LOADER_IMAGE_CONTEXT      *ImageContext,
+  IN  REGION_PERMISSION_UPDATE_FUNC           NoExecUpdater,
+  IN  REGION_PERMISSION_UPDATE_FUNC           ReadOnlyUpdater
+  )
+{
+  RETURN_STATUS                         Status;
+  EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION   Hdr;
+  EFI_IMAGE_OPTIONAL_HEADER_UNION       HdrData;
+  UINTN                                 Size;
+  UINTN                                 ReadSize;
+  UINT32                                SectionHeaderOffset;
+  UINTN                                 NumberOfSections;
+  UINTN                                 Index;
+  EFI_IMAGE_SECTION_HEADER              SectionHeader;
+  PE_COFF_LOADER_IMAGE_CONTEXT          TmpContext;
+  EFI_PHYSICAL_ADDRESS                  Base;
+
+  //
+  // We need to copy ImageContext since PeCoffLoaderGetImageInfo ()
+  // will mangle the ImageAddress field
+  //
+  CopyMem (&TmpContext, ImageContext, sizeof (TmpContext));
+
+  if (TmpContext.PeCoffHeaderOffset == 0) {
+    Status = PeCoffLoaderGetImageInfo (&TmpContext);
+    if (RETURN_ERROR (Status)) {
+      DEBUG ((DEBUG_ERROR,
+        "%a: PeCoffLoaderGetImageInfo () failed (Status = %r)\n",
+        __FUNCTION__, Status));
+      return Status;
+    }
+  }
+
+  if (TmpContext.IsTeImage &&
+      TmpContext.ImageAddress == ImageContext->ImageAddress) {
+    DEBUG ((DEBUG_INFO, "%a: ignoring XIP TE image at 0x%lx\n", __FUNCTION__,
+      ImageContext->ImageAddress));
+    return RETURN_SUCCESS;
+  }
+
+  if (TmpContext.SectionAlignment < EFI_PAGE_SIZE) {
+    //
+    // The sections need to be at least 4 KB aligned, since that is the
+    // granularity at which we can tighten permissions. So just clear the
+    // noexec permissions on the entire region.
+    //
+    if (!TmpContext.IsTeImage) {
+      DEBUG ((DEBUG_WARN,
+        "%a: non-TE Image at 0x%lx has SectionAlignment < 4 KB (%lu)\n",
+        __FUNCTION__, ImageContext->ImageAddress, TmpContext.SectionAlignment));
+    }
+    Base = ImageContext->ImageAddress & ~(EFI_PAGE_SIZE - 1);
+    Size = ImageContext->ImageAddress - Base + ImageContext->ImageSize;
+    return NoExecUpdater (Base, ALIGN_VALUE (Size, EFI_PAGE_SIZE));
+  }
+
+  //
+  // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
+  // data, but that should not hurt anything. Hdr.Pe32->OptionalHeader.Magic
+  // determines if this is a PE32 or PE32+ image. The magic is in the same
+  // location in both images.
+  //
+  Hdr.Union = &HdrData;
+  Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);
+  ReadSize = Size;
+  Status = TmpContext.ImageRead (TmpContext.Handle,
+                         TmpContext.PeCoffHeaderOffset, &Size, Hdr.Pe32);
+  if (RETURN_ERROR (Status) || (Size != ReadSize)) {
+    DEBUG ((DEBUG_ERROR,
+      "%a: TmpContext.ImageRead () failed (Status = %r)\n",
+      __FUNCTION__, Status));
+    return Status;
+  }
+
+  ASSERT (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE);
+
+  SectionHeaderOffset = TmpContext.PeCoffHeaderOffset + sizeof (UINT32) +
+                        sizeof (EFI_IMAGE_FILE_HEADER);
+  NumberOfSections    = (UINTN)(Hdr.Pe32->FileHeader.NumberOfSections);
+
+  switch (Hdr.Pe32->OptionalHeader.Magic) {
+    case EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC:
+      SectionHeaderOffset += Hdr.Pe32->FileHeader.SizeOfOptionalHeader;
+      break;
+    case EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC:
+      SectionHeaderOffset += Hdr.Pe32Plus->FileHeader.SizeOfOptionalHeader;
+      break;
+    default:
+      ASSERT (FALSE);
+  }
+
+  //
+  // Iterate over the sections
+  //
+  for (Index = 0; Index < NumberOfSections; Index++) {
+    //
+    // Read section header from file
+    //
+    Size = sizeof (EFI_IMAGE_SECTION_HEADER);
+    ReadSize = Size;
+    Status = TmpContext.ImageRead (TmpContext.Handle, SectionHeaderOffset,
+                                   &Size, &SectionHeader);
+    if (RETURN_ERROR (Status) || (Size != ReadSize)) {
+      DEBUG ((DEBUG_ERROR,
+        "%a: TmpContext.ImageRead () failed (Status = %r)\n",
+        __FUNCTION__, Status));
+      return Status;
+    }
+
+    Base = TmpContext.ImageAddress + SectionHeader.VirtualAddress;
+
+    if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_MEM_EXECUTE) == 0) {
+
+      if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_MEM_WRITE) == 0 &&
+          TmpContext.ImageType != EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
+
+        DEBUG ((DEBUG_INFO,
+          "%a: Mapping section %d of image at 0x%lx with RO-XN permissions and size 0x%x\n",
+          __FUNCTION__, Index, Base, SectionHeader.Misc.VirtualSize));
+        ReadOnlyUpdater (Base, SectionHeader.Misc.VirtualSize);
+      } else {
+        DEBUG ((DEBUG_WARN,
+          "%a: Mapping section %d of image at 0x%lx with RW-XN permissions and size 0x%x\n",
+          __FUNCTION__, Index, Base, SectionHeader.Misc.VirtualSize));
+      }
+    } else {
+        DEBUG ((DEBUG_INFO,
+          "%a: Mapping section %d of image at 0x%lx with RO-XN permissions and size 0x%x\n",
+           __FUNCTION__, Index, Base, SectionHeader.Misc.VirtualSize));
+        ReadOnlyUpdater (Base, SectionHeader.Misc.VirtualSize);
+
+        DEBUG ((DEBUG_INFO,
+          "%a: Mapping section %d of image at 0x%lx with RO-X permissions and size 0x%x\n",
+          __FUNCTION__, Index, Base, SectionHeader.Misc.VirtualSize));
+        NoExecUpdater (Base, SectionHeader.Misc.VirtualSize);
+    }
+
+    SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
+  }
+  return RETURN_SUCCESS;
+}
 
 /**
   If the build is done on cygwin the paths are cygpaths.
@@ -83,23 +234,29 @@ PeCoffLoaderRelocateImageExtraAction (
   CHAR8 Temp[512];
 #endif
 
+  if (PcdGetBool(PcdStandaloneMmEnable) == TRUE)
+  {
+     UpdatePeCoffPermissions (ImageContext, ArmClearMemoryRegionNoExec,
+                              ArmSetMemoryRegionReadOnly);
+  }
+
   if (ImageContext->PdbPointer) {
 #ifdef __CC_ARM
 #if (__ARMCC_VERSION < 500000)
     // Print out the command for the RVD debugger to load symbols for this image
-    DEBUG ((EFI_D_LOAD | EFI_D_INFO, "load /a /ni /np %a &0x%p\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
+    DEBUG ((DEBUG_LOAD | DEBUG_INFO, "load /a /ni /np %a &0x%p\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
 #else
     // Print out the command for the DS-5 to load symbols for this image
-    DEBUG ((EFI_D_LOAD | EFI_D_INFO, "add-symbol-file %a 0x%p\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
+    DEBUG ((DEBUG_LOAD | DEBUG_INFO, "add-symbol-file %a 0x%p\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
 #endif
 #elif __GNUC__
     // This may not work correctly if you generate PE/COFF directlyas then the Offset would not be required
-    DEBUG ((EFI_D_LOAD | EFI_D_INFO, "add-symbol-file %a 0x%p\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
+    DEBUG ((DEBUG_LOAD | DEBUG_INFO, "add-symbol-file %a 0x%p\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
 #else
-    DEBUG ((EFI_D_LOAD | EFI_D_INFO, "Loading driver at 0x%11p EntryPoint=0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress, FUNCTION_ENTRY_POINT (ImageContext->EntryPoint)));
+    DEBUG ((DEBUG_LOAD | DEBUG_INFO, "Loading driver at 0x%11p EntryPoint=0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress, FUNCTION_ENTRY_POINT (ImageContext->EntryPoint)));
 #endif
   } else {
-    DEBUG ((EFI_D_LOAD | EFI_D_INFO, "Loading driver at 0x%11p EntryPoint=0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress, FUNCTION_ENTRY_POINT (ImageContext->EntryPoint)));
+    DEBUG ((DEBUG_LOAD | DEBUG_INFO, "Loading driver at 0x%11p EntryPoint=0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress, FUNCTION_ENTRY_POINT (ImageContext->EntryPoint)));
   }
 }
 
@@ -125,17 +282,23 @@ PeCoffLoaderUnloadImageExtraAction (
   CHAR8 Temp[512];
 #endif
 
+  if (PcdGetBool(PcdStandaloneMmEnable) == TRUE)
+  {
+     UpdatePeCoffPermissions (ImageContext, ArmSetMemoryRegionNoExec,
+                              ArmClearMemoryRegionReadOnly);
+  }
+
   if (ImageContext->PdbPointer) {
 #ifdef __CC_ARM
     // Print out the command for the RVD debugger to load symbols for this image
-    DEBUG ((EFI_D_ERROR, "unload symbols_only %a\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp))));
+    DEBUG ((DEBUG_ERROR, "unload symbols_only %a\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp))));
 #elif __GNUC__
     // This may not work correctly if you generate PE/COFF directlyas then the Offset would not be required
-    DEBUG ((EFI_D_ERROR, "remove-symbol-file %a 0x%08x\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
+    DEBUG ((DEBUG_ERROR, "remove-symbol-file %a 0x%08x\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
 #else
-    DEBUG ((EFI_D_ERROR, "Unloading %a\n", ImageContext->PdbPointer));
+    DEBUG ((DEBUG_ERROR, "Unloading %a\n", ImageContext->PdbPointer));
 #endif
   } else {
-    DEBUG ((EFI_D_ERROR, "Unloading driver at 0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress));
+    DEBUG ((DEBUG_ERROR, "Unloading driver at 0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress));
   }
 }
diff --git a/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.inf b/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.inf
index c1f717e5bd..38bf3993ae 100644
--- a/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.inf
+++ b/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.inf
@@ -33,7 +33,14 @@
   DebugPeCoffExtraActionLib.c
 
 [Packages]
+  ArmPkg/ArmPkg.dec
   MdePkg/MdePkg.dec
+  StandaloneMmPkg/StandaloneMmPkg.dec
+
+[FeaturePcd]
+  gStandaloneMmPkgTokenSpaceGuid.PcdStandaloneMmEnable
 
 [LibraryClasses]
+  ArmMmuLib
   DebugLib
+  PcdLib
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 16/18] BaseTools/AutoGen: Update header file for MM modules.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (14 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 15/18] ArmPkg: Extra action to update permissions for S-ELO MM Image Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-30 19:52   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 17/18] StandaloneMmPkg: Add application to test MM communication protocol Supreeth Venkatesh
                   ` (2 subsequent siblings)
  18 siblings, 1 reply; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

This patch corrects the Module Type Header file for Management Mode(MM)
as specified in PI v1.6 Specification. Also, it updates parameter for
auto generated template functions from EFI_SMM_SYSTEM_TABLE2 to
EFI_MM_SYSTEM_TABLE.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 BaseTools/Source/Python/AutoGen/GenC.py | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/BaseTools/Source/Python/AutoGen/GenC.py b/BaseTools/Source/Python/AutoGen/GenC.py
index 4d9ea1b2a8..8601e4ee70 100644
--- a/BaseTools/Source/Python/AutoGen/GenC.py
+++ b/BaseTools/Source/Python/AutoGen/GenC.py
@@ -270,7 +270,7 @@ EFI_STATUS
 EFIAPI
 ${Function} (
   IN EFI_HANDLE            ImageHandle,
-  IN EFI_SMM_SYSTEM_TABLE2 *MmSystemTable
+  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
   );
 ${END}
 """)
@@ -283,7 +283,7 @@ EFI_STATUS
 EFIAPI
 ProcessModuleEntryPointList (
   IN EFI_HANDLE            ImageHandle,
-  IN EFI_SMM_SYSTEM_TABLE2 *MmSystemTable
+  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
   )
 
 {
@@ -297,7 +297,7 @@ EFI_STATUS
 EFIAPI
 ProcessModuleEntryPointList (
   IN EFI_HANDLE            ImageHandle,
-  IN EFI_SMM_SYSTEM_TABLE2 *MmSystemTable
+  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
   )
 
 {
@@ -312,7 +312,7 @@ EFI_STATUS
 EFIAPI
 ProcessModuleEntryPointList (
   IN EFI_HANDLE            ImageHandle,
-  IN EFI_SMM_SYSTEM_TABLE2 *MmSystemTable
+  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
   )
 
 {
@@ -680,7 +680,7 @@ EFI_STATUS
 EFIAPI
 ${Function} (
   IN EFI_HANDLE            ImageHandle,
-  IN EFI_SMM_SYSTEM_TABLE2  *MmSystemTable
+  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
   );${END}
 """),
 }
@@ -760,7 +760,7 @@ VOID
 EFIAPI
 ProcessLibrary${Type}List (
   IN EFI_HANDLE            ImageHandle,
-  IN EFI_SMM_SYSTEM_TABLE2  *MmSystemTable
+  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
   )
 {
 ${BEGIN}  EFI_STATUS  Status;
@@ -784,8 +784,8 @@ gModuleTypeHeaderFile = {
     "UEFI_DRIVER"       :   ["Uefi.h",  "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiDriverEntryPoint.h"],
     "UEFI_APPLICATION"  :   ["Uefi.h",  "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiApplicationEntryPoint.h"],
     "SMM_CORE"          :   ["PiDxe.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiDriverEntryPoint.h"],
-    "MM_STANDALONE"     :   ["PiSmm.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/SmmDriverStandaloneEntryPoint.h"],
-    "MM_CORE_STANDALONE" :  ["PiSmm.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/SmmCoreStandaloneEntryPoint.h"],
+    "MM_STANDALONE"     :   ["PiMm.h",  "Library/BaseLib.h", "Library/DebugLib.h", "Library/MmDriverStandaloneEntryPoint.h"],
+    "MM_CORE_STANDALONE":   ["PiMm.h",  "Library/BaseLib.h", "Library/DebugLib.h", "Library/MmCoreStandaloneEntryPoint.h"],
     "USER_DEFINED"      :   [gBasicHeaderFile]
 }
 
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 17/18] StandaloneMmPkg: Add application to test MM communication protocol.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (15 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 16/18] BaseTools/AutoGen: Update header file for MM modules Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-30 20:02   ` Achin Gupta
  2018-04-06 14:42 ` [PATCH v1 18/18] StandaloneMmPkg: Add handler to handle event received from Normal World Supreeth Venkatesh
  2018-04-08  6:01 ` [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Yao, Jiewen
  18 siblings, 1 reply; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

This patch adds a simple application that uses the MM
communication protocol to pass a copy of the UEFI system table to
the MM environment in the secure world.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 .../Application/MmCommTestApp/MmCommTest.c         | 81 ++++++++++++++++++++++
 .../Application/MmCommTestApp/MmCommTest.h         | 37 ++++++++++
 .../Application/MmCommTestApp/MmCommTest.inf       | 57 +++++++++++++++
 3 files changed, 175 insertions(+)
 create mode 100644 StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.c
 create mode 100644 StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.h
 create mode 100644 StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.inf

diff --git a/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.c b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.c
new file mode 100644
index 0000000000..efbafdde62
--- /dev/null
+++ b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.c
@@ -0,0 +1,81 @@
+/** @file
+  This sample application demos how to communicate
+  with secure partition using MM communication protocol
+
+  Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
+
+  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
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <Uefi.h>
+#include <Library/PcdLib.h>
+#include <Library/UefiLib.h>
+#include <Library/UefiApplicationEntryPoint.h>
+#include <Library/DebugLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+
+#include "MmCommTest.h"
+
+#include <Library/ArmSmcLib.h>
+
+#include <Protocol/MmCommunication.h>
+
+EFI_MM_COMMUNICATION_PROTOCOL  *mMmCommunication = NULL;
+
+EFI_STATUS
+MmIplNotifyCommTest (
+  VOID
+  )
+{
+  EFI_MM_COMMUNICATE_TEST    MmCommTest;
+  UINTN                      Size;
+
+  DEBUG ((DEBUG_INFO, "MmIplNotifyCommTest\n"));
+
+  CopyGuid (&MmCommTest.HeaderGuid, &gMmCommTestGuid);
+  CopyMem (&MmCommTest.Data.EfiSystemTable, gST, sizeof (EFI_SYSTEM_TABLE));
+  MmCommTest.MessageLength = sizeof (EFI_MM_COMMUNICATE_TEST_DATA);
+
+  //
+  // Generate the MM_COMMUNICATE SMC and return the result
+  //
+  Size = sizeof (MmCommTest);
+  return mMmCommunication->Communicate (NULL, &MmCommTest, &Size);
+}
+
+/**
+  The user Entry Point for Application. The user code starts with this function
+  as the real entry point for the application.
+
+  @param[in] ImageHandle    The firmware allocated handle for the EFI image.
+  @param[in] SystemTable    A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS       The entry point is executed successfully.
+  @retval other             Some error occurs when executing this entry point.
+
+**/
+EFI_STATUS
+EFIAPI
+MmCommTestEntryPoint (
+  IN EFI_HANDLE        ImageHandle,
+  IN EFI_SYSTEM_TABLE  *SystemTable
+  )
+{
+  EFI_STATUS Status;
+
+  Status = gBS->LocateProtocol (&gEfiMmCommunicationProtocolGuid, NULL, (VOID **) &mMmCommunication);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  return MmIplNotifyCommTest ();
+}
diff --git a/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.h b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.h
new file mode 100644
index 0000000000..8e8305a060
--- /dev/null
+++ b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.h
@@ -0,0 +1,37 @@
+/** @file
+  GUIDs for MM Event.
+
+Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
+
+This program and the accompanying materials are licensed and made available under
+the terms and conditions of the BSD License that accompanies this distribution.
+The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php.
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef __MM_COMM_TEST_H__
+#define __MM_COMM_TEST_H__
+
+#define MM_COMM_TEST_GUID \
+  { 0xa37721e4, 0x8c0b, 0x4bca, { 0xb5, 0xe8, 0xe9, 0x2, 0xa0, 0x25, 0x51, 0x4e }}
+
+extern EFI_GUID gMmCommTestGuid;
+
+#pragma pack(1)
+typedef struct {
+  EFI_SYSTEM_TABLE      EfiSystemTable;
+} EFI_MM_COMMUNICATE_TEST_DATA;
+
+typedef struct {
+  EFI_GUID                         HeaderGuid;
+  UINTN                            MessageLength;
+  EFI_MM_COMMUNICATE_TEST_DATA     Data;
+} EFI_MM_COMMUNICATE_TEST;
+#pragma pack()
+
+#endif
diff --git a/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.inf b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.inf
new file mode 100644
index 0000000000..1828cd7e13
--- /dev/null
+++ b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.inf
@@ -0,0 +1,57 @@
+## @file
+#  Sample UEFI Application Reference EDKII Module.
+#
+#  This is a sample shell application that will print "UEFI firmware version Info!" to the
+#  UEFI Console.
+#
+#  It demos how to communicate with secure partition using MM
+#  communication protocol.
+#
+#  Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2016 - 2018, ARM Limited. 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
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001A
+  BASE_NAME                      = MmCommTest
+  FILE_GUID                      = 6987936E-ED34-44db-AE97-1FA5E4ED2116
+  MODULE_TYPE                    = UEFI_APPLICATION
+  VERSION_STRING                 = 1.0
+  ENTRY_POINT                    = MmCommTestEntryPoint
+
+[Sources]
+  MmCommTest.c
+
+[Packages]
+  ArmPkg/ArmPkg.dec
+  ArmPlatformPkg/ArmPlatformPkg.dec
+  MdePkg/MdePkg.dec
+  StandaloneMmPkg/StandaloneMmPkg.dec
+
+[LibraryClasses]
+  ArmSmcLib
+  BaseMemoryLib
+  DebugLib
+  PrintLib
+  UefiApplicationEntryPoint
+  UefiBootServicesTableLib
+  UefiLib
+
+[FeaturePcd]
+
+[Pcd]
+
+[Guids]
+  gMmCommTestGuid
+
+[Protocols]
+  gEfiMmCommunicationProtocolGuid
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v1 18/18] StandaloneMmPkg: Add handler to handle event received from Normal World.
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (16 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 17/18] StandaloneMmPkg: Add application to test MM communication protocol Supreeth Venkatesh
@ 2018-04-06 14:42 ` Supreeth Venkatesh
  2018-04-08  6:01 ` [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Yao, Jiewen
  18 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-04-06 14:42 UTC (permalink / raw)
  To: edk2-devel
  Cc: michael.d.kinney, liming.gao, jiewen.yao, achin.gupta,
	leif.lindholm, ard.biesheuvel, Supreeth Venkatesh

This patch adds a simple handler to handle event received from
"mmcommtest" shell application. It prints the version information from
the copy of the UEFI system table received from normal world.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Achin Gupta <achin.gupta@arm.com>
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
---
 StandaloneMmPkg/Core/StandaloneMmCore.c   | 39 +++++++++++++++++++++++++++++++
 StandaloneMmPkg/Core/StandaloneMmCore.inf |  2 ++
 StandaloneMmPkg/StandaloneMmPkg.dec       |  2 ++
 3 files changed, 43 insertions(+)

diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.c b/StandaloneMmPkg/Core/StandaloneMmCore.c
index 0bb99b9710..4aaecdb2ea 100644
--- a/StandaloneMmPkg/Core/StandaloneMmCore.c
+++ b/StandaloneMmPkg/Core/StandaloneMmCore.c
@@ -95,6 +95,15 @@ EFI_MM_SYSTEM_TABLE  gMmCoreMmst = {
 //
 BOOLEAN  mInLegacyBoot = FALSE;
 
+EFI_STATUS
+EFIAPI
+MmCommTestHandler (
+  IN     EFI_HANDLE               DispatchHandle,
+  IN     CONST VOID               *Context,        OPTIONAL
+  IN OUT VOID                     *CommBuffer,     OPTIONAL
+  IN OUT UINTN                    *CommBufferSize  OPTIONAL
+  );
+
 //
 // Table of MMI Handlers that are registered by the MM Core when it is initialized
 //
@@ -106,6 +115,7 @@ MM_CORE_MMI_HANDLERS  mMmCoreMmiHandlers[] = {
   { MmLegacyBootHandler,     &gEfiEventLegacyBootGuid,           NULL, FALSE },
   { MmExitBootServiceHandler,&gEfiEventExitBootServicesGuid,     NULL, FALSE },
   { MmReadyToBootHandler,    &gEfiEventReadyToBootGuid,          NULL, FALSE },
+  { MmCommTestHandler,       &gMmCommTestGuid,                   NULL, TRUE  },
   { NULL,                    NULL,                               NULL, FALSE },
 };
 
@@ -379,7 +389,36 @@ MmEndOfDxeHandler (
   return Status;
 }
 
+/**
+  This function is the main entry point for communicate-based callback.
+
+  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.
+  @param  CommBuffer      A pointer to a collection of data in memory that will
+                          be conveyed from a non-MM environment into an MM environment.
+  @param  CommBufferSize  The size of the CommBuffer.
 
+  @return Status Code
+**/
+EFI_STATUS
+EFIAPI
+MmCommTestHandler (
+  IN     EFI_HANDLE               DispatchHandle,
+  IN     CONST VOID               *Context,        OPTIONAL
+  IN OUT VOID                     *CommBuffer,     OPTIONAL
+  IN OUT UINTN                    *CommBufferSize  OPTIONAL
+  )
+{
+  EFI_SYSTEM_TABLE                      SystemTable;
+
+  DEBUG ((DEBUG_INFO, "MmUefiInfoHandler\n"));
+
+  CopyMem (&SystemTable, CommBuffer, sizeof(EFI_SYSTEM_TABLE));
+
+  DEBUG ((DEBUG_INFO, "  UEFI Firmware Version - 0x%x\n", SystemTable.FirmwareRevision));
+
+  return EFI_SUCCESS;
+}
 
 /**
   The main entry point to MM Foundation.
diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.inf b/StandaloneMmPkg/Core/StandaloneMmCore.inf
index c5eaa14ba3..b35870732c 100644
--- a/StandaloneMmPkg/Core/StandaloneMmCore.inf
+++ b/StandaloneMmPkg/Core/StandaloneMmCore.inf
@@ -78,3 +78,5 @@
   gEfiEventLegacyBootGuid
   gEfiEventExitBootServicesGuid
   gEfiEventReadyToBootGuid
+  gMmCommTestGuid
+
diff --git a/StandaloneMmPkg/StandaloneMmPkg.dec b/StandaloneMmPkg/StandaloneMmPkg.dec
index 36521bb039..d88be3ce5e 100644
--- a/StandaloneMmPkg/StandaloneMmPkg.dec
+++ b/StandaloneMmPkg/StandaloneMmPkg.dec
@@ -39,6 +39,8 @@
   gEfiStandaloneMmNonSecureBufferGuid      = { 0xf00497e3, 0xbfa2, 0x41a1, { 0x9d, 0x29, 0x54, 0xc2, 0xe9, 0x37, 0x21, 0xc5 }}
   gEfiArmTfCpuDriverEpDescriptorGuid       = { 0x6ecbd5a1, 0xc0f8, 0x4702, { 0x83, 0x01, 0x4f, 0xc2, 0xc5, 0x47, 0x0a, 0x51 }}
 
+  gMmCommTestGuid                          = { 0xa37721e4, 0x8c0b, 0x4bca, { 0xb5, 0xe8, 0xe9, 0x2, 0xa0, 0x25, 0x51, 0x4e }}
+
 [PcdsFeatureFlag]
   gStandaloneMmPkgTokenSpaceGuid.PcdStandaloneMmEnable|FALSE|BOOLEAN|0x00000001
 
-- 
2.16.2



^ permalink raw reply related	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms ***
  2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
                   ` (17 preceding siblings ...)
  2018-04-06 14:42 ` [PATCH v1 18/18] StandaloneMmPkg: Add handler to handle event received from Normal World Supreeth Venkatesh
@ 2018-04-08  6:01 ` Yao, Jiewen
  2018-05-04 23:15   ` Supreeth Venkatesh
  18 siblings, 1 reply; 70+ messages in thread
From: Yao, Jiewen @ 2018-04-08  6:01 UTC (permalink / raw)
  To: Supreeth Venkatesh, edk2-devel@lists.01.org
  Cc: Kinney, Michael D, Gao, Liming, achin.gupta@arm.com,
	leif.lindholm@linaro.org, ard.biesheuvel@linaro.org

Thank you for this great work.

Comment below:

1) I don't have comment for ArmPkg update (0001~0005, 0015) - I expect Arm expert will help on that. :-)

2) BaseTool (0016), reviewed-by: Jiewen.yao@intel.com

3) StandaloneMmPkg
3.1) 0007 - I think we need add ExHeader check and alignment enforcement for FfsFileHeader.

    FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FwVolHeader + FwVolHeader->HeaderLength);

You may refer to https://github.com/tianocore/edk2/blob/master/SignedCapsulePkg/Library/EdkiiSystemCapsuleLib/EdkiiSystemCapsuleLib.c GetFfsByName()

    if (FvHeader->ExtHeaderOffset != 0) {
      FvExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *)((UINT8 *)FvHeader + FvHeader->ExtHeaderOffset);
      FfsHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FvExtHeader + FvExtHeader->ExtHeaderSize);
    } else {
      FfsHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FvHeader + FvHeader->HeaderLength);
    }
    FfsHeader = (EFI_FFS_FILE_HEADER *)((UINTN)FvHeader + ALIGN_VALUE((UINTN)FfsHeader - (UINTN)FvHeader, 8));

3.2) 0008 - I think we need rename MmCopyMemToSmram to MmCopyMemToMmram, and MmCopyMemFromSmram to MmCopyMemFromMmram.
Also if this MemLib is only for MM_STANDALONE, we should name it StandaloneMmMemLib.

3.3) 0009 - If this MemoryAllocationLib is only for MM_CORE_STANDALONE, we should name it StandaloneMmCoreMemoryAllocationLib.
Also do we need StandaloneMmMemoryAllocationLib (non-Core version) for MmDriver ?
(You may find that my POC zip has both StandaloneSmmCoreMemoryAllocationLib and StandaloneSmmMemoryAllocationLib.)

3.4) 0010 - If this HobLib is only for MM_STANDALONE, we should name it StandaloneMmHobLib.

3.5) 0014 - I do not believe we should add FDF file here. StandaloneMmPkg should only provide the component. (such as MdeModulePkg). The FDF file should be in a real project.

3.6) 0017/0018 - they are only for unit test. I do not think we should check in them. Please discard them when you check in the series.

3.x) 0006/0011/0012/0013, they are good so far. We may do some enhancement to move common logic out of Arm directory. But that can be done later, when we add X86 content there. reviewed-by: Jiewen.yao@intel.com

Thank you
Yao Jiewen



> -----Original Message-----
> From: Supreeth Venkatesh [mailto:supreeth.venkatesh@arm.com]
> Sent: Friday, April 6, 2018 10:42 PM
> To: edk2-devel@lists.01.org
> Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming
> <liming.gao@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>;
> achin.gupta@arm.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org;
> Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> Subject: [PATCH v1 00/18] *** Standalone Management Mode Core Interface
> for AARCH64 Platforms ***
> 
> ***
> PI Specification v1.5  "Volume 4: Management Mode Core Interface"
> introduces the concept of MM Standalone Mode. Initialization of this mode
> can be done during the SEC phase (Section 1.5.2).
> On ARMv8-A systems, ARM Trusted Firmware is responsible for launching
> the normal world firmware e.g. UEFI.
> 
> The Standalone MM environment is instantiated in Secure EL0 as a separate
> firmware volume. It executes as BL32 Image under control of ARM TF
> which is instantiated in EL3. Both components execute in the AArch64 execution
> state.
> This patchset will build upon the StandaloneSmmPkg module originally
> contributed by Intel.
> 
> This package can be used in conjunction with ARM Trusted Firmware
> to recreate a simple MM secure environment that demonstrates communication
> between two UEFI images where one is executing in the normal world and the
> other is
> executing in the secure world.
> 
> The normal world image includes:
> MM Communication DXE runtime driver that implements the protocol for
> communication
> with the MM environment in the secure world.
> 
> The secure world image includes:
> The MM Standalone framework.
> 
> This patchset includes the proposed organization/structure.
> In order to be able to review the changes more effectively, the changes are
> present here:
> https://github.com/supven01/edk2.git (Branch: master)
> 
> Steps to build MM Standalone images
> In user preferred "work" directory, execute the following shell commands
> 
> git clone https://github.com/tianocore/edk2.git
> git checkout master
> 
> git clone https://github.com/tianocore/edk2-platforms.git
> git checkout master
> 
> mkdir arm-tf
> cd arm-tf
> git clone https://github.com/ARM-software/arm-trusted-firmware.git .
> git checkout master
> cd ..
> 
> git clone https://git.linaro.org/uefi/uefi-tools.git .
> git checkout master
> 
> The following will build the MM Standalone image which runs in secure world.
> ./uefi-tools/edk2-build.sh -b DEBUG fvp_mm_standalone
> 
> The follwing will build the normal world UEFI image, ARM Trusted Firmware and
> a Firmware Image Package (FIP) that includes both the UEFI images.
> ./uefi-tools/edk2-build.sh -a ./arm-tf -b DEBUG fvp_mm_normal
> 
> Boot Loader Stage 1 (BL1) binary and combined arm-tf/uefi firmware image
> package (fip) binary will be generated at:
> 
> Build Output
> Build/ArmVExpress-FVP-AArch64-MM-Normal/DEBUG_GCC5/FV/bl1.bin
> Build/ArmVExpress-FVP-AArch64-MM-Normal/DEBUG_GCC5/FV/fip.bin
> 
> Steps to run MM Standalone image
> 1. Download the ARMv8 Architecture FVP from
> 
> https://silver.arm.com/download/download.tm?pv=3744408&p=1424570
>      For more information, please refer
> 
> https://developer.arm.com/products/system-design/fixed-virtual-platforms
> 2.  Install FVP into preferred "work" directory.
> 3.  Create a shell script "run_mm.sh" in the same folder where
> "FVP_Base_AEMv8A-AEMv8A" is present.
>      Sample Shell script below:
>     ./FVP_Base_AEMv8A-AEMv8A
>     -C cache_state_modelled=0
>     -C bp.secure_memory=1
>     -C bp.tzc_400.diagnostics=1
>     -C bp.pl011_uart0.untimed_fifos=0
>     -C cluster1.NUM_CORES=4
>     -C cluster0.NUM_CORES=4
>     -C bp.pl011_uart0.out_file=uart0.output
>     -C bp.pl011_uart1.out_file=uart1.output
>     -C bp.pl011_uart2.out_file=uart2.output
>     -C bp.pl011_uart3.out_file=uart3.output
>     -C bp.secureflashloader.fname=""
>     -C bp.flashloader0.fname=""
>     -S -R
> 4. ./run_mm.sh
> 5. Output can be seen on FVP console.
> 6. The normal world will boot to the UEFI shell.
> 
> Sample Output
> 
> MM Standalone Output (FVP UART2)
> SPM Version: Major=0x0, Minor=0x1
> NumSpMemRegions - 0x6
> SpMemBase       - 0xFF200000
> SpMemLimit      - 0x100000000
> SpImageBase     - 0xFF200000
> SpStackBase     - 0xFF610000
> SpHeapBase      - 0xFF620000
> SpNsCommBufBase - 0xFF600000
> SpSharedBufBase - 0xFF500000
> SpImageSize     - 0x300000
> SpPcpuStackSize - 0x2000
> SpHeapSize      - 0x9E0000
> SpNsCommBufSize - 0x10000
> SpPcpuSharedBufSize - 0x20000
> NumCpus         - 0x8
> CpuInfo         - 0xFF500680
> Mpidr           - 0x80000000
> LinearId        - 0x0
> Flags           - 0x1
> Mpidr           - 0x80000001
> LinearId        - 0x1
> Flags           - 0x0
> Mpidr           - 0x80000002
> LinearId        - 0x2
> Flags           - 0x0
> Mpidr           - 0x80000003
> LinearId        - 0x3
> Flags           - 0x0
> Mpidr           - 0x80000100
> LinearId        - 0x4
> Flags           - 0x0
> Mpidr           - 0x80000101
> LinearId        - 0x5
> Flags           - 0x0
> Mpidr           - 0x80000102
> LinearId        - 0x6
> Flags           - 0x0
> Mpidr           - 0x80000103
> LinearId        - 0x7
> Flags           - 0x0
> Found Standalone MM PE data - 0xFF201000
> Found Standalone MM PE data - 0xFF201000
> Standalone MM Core PE-COFF SectionHeaderOffset - 0xF60, NumberOfSections
> - 3
> UpdateMmFoundationPeCoffPermissions: Section 0 of image at 0xFF201000 has
> 0x60000020 permissions
> UpdateMmFoundationPeCoffPermissions: Section 0 of image at 0xFF201000
> has .et name
> UpdateMmFoundationPeCoffPermissions: Section 0 of image at 0xFF201000 has
> 0xFF202000 address
> UpdateMmFoundationPeCoffPermissions: Section 0 of image at 0xFF201000 has
> 0x1000 data
> UpdateMmFoundationPeCoffPermissions: Ignoring section 0 of image at
> 0xFF201000 with 0x60000020 permissions
> UpdateMmFoundationPeCoffPermissions: Section 1 of image at 0xFF201000 has
> 0xC0000040 permissions
> UpdateMmFoundationPeCoffPermissions: Section 1 of image at 0xFF201000
> has .aa name
> UpdateMmFoundationPeCoffPermissions: Section 1 of image at 0xFF201000 has
> 0xFF217000 address
> UpdateMmFoundationPeCoffPermissions: Section 1 of image at 0xFF201000 has
> 0x16000 data
> UpdateMmFoundationPeCoffPermissions: Mapping section 1 of image at
> 0xFF201000 with RW-XN permissions
> UpdateMmFoundationPeCoffPermissions: Section 2 of image at 0xFF201000 has
> 0x42000040 permissions
> UpdateMmFoundationPeCoffPermissions: Section 2 of image at 0xFF201000
> has .eo name
> UpdateMmFoundationPeCoffPermissions: Section 2 of image at 0xFF201000 has
> 0xFF218000 address
> UpdateMmFoundationPeCoffPermissions: Section 2 of image at 0xFF201000 has
> 0x17000 data
> UpdateMmFoundationPeCoffPermissions: Mapping section 2 of image at
> 0xFF201000 with RO-XN permissions
> StandaloneMmCoreMemoryAllocationLibConstructor - 0xFF620000
> MmramRangeCount - 0x6
> MmramRanges[0]: 0x00000000FF200000 - 0x0000000000300000
> MmramRanges[1]: 0x00000000FF500000 - 0x0000000000100000
> MmramRanges[2]: 0x00000000FF600000 - 0x0000000000010000
> MmramRanges[3]: 0x00000000FF610000 - 0x0000000000010000
> MmramRanges[4]: 0x00000000FF620000 - 0x00000000000002C8
> MmramRanges[5]: 0x00000000FF6202C8 - 0x00000000009DFD38
> MmInitializeMemoryServices
> MmAddMemoryRegion 0 : 0x00000000FF200000 - 0x0000000000300000
> MmAddMemoryRegion 1 : 0x00000000FF500000 - 0x0000000000100000
> MmAddMemoryRegion 2 : 0x00000000FF600000 - 0x0000000000010000
> MmAddMemoryRegion 3 : 0x00000000FF610000 - 0x0000000000010000
> MmAddMemoryRegion 4 : 0x00000000FF620000 - 0x00000000000002C8
> MmAddMemoryRegion 5 : 0x00000000FF6202C8 - 0x00000000009DFD38
> mMmMemLibInternalMaximumSupportAddress = 0xFFFFFFFFF
> MmMain - 0xFF620000
> MmramRangeCount - 0x6
> MmramRanges[0]: 0x00000000FF200000 - 0x300000
> MmramRanges[1]: 0x00000000FF500000 - 0x100000
> MmramRanges[2]: 0x00000000FF600000 - 0x10000
> MmramRanges[3]: 0x00000000FF610000 - 0x10000
> MmramRanges[4]: 0x00000000FF620000 - 0x2C8
> MmramRanges[5]: 0x00000000FF6202C8 - 0x9DFD38
> mMmramRangeCount - 0x6
> mMmramRanges - 0xFFFFEE10
> BFV address - 0xFF200000
> BFV size    - 0x300000
> MmInstallConfigurationTable For HobList
> HobSize - 0x2C8
> MmHobStart - 0xFFFFE810
> MmRegisterProtocolNotify - MmConfigurationMmProtocol
> Mm Dispatch StandaloneBfvAddress - 0xFF200000
> MmCoreFfsFindMmDriver - 0xFF200000
> FvIsBeingProcesssed - 0xFF200000
> Check MmFileTypes - 0xA
> Check MmFileTypes - 0xE
> Find PE data - 0xFF219024
> MmAddToDriverList - 58F7A62B-6280-42A7-BC38-10535A64A92C
> (0xFF219024)
> MmDispatcher
>   Drain the Scheduled Queue
>   Search DriverList for items to place on Scheduled Queue
>   DriverEntry (Discovered) - 58F7A62B-6280-42A7-BC38-10535A64A92C
> Evaluate MM DEPEX for FFS(58F7A62B-6280-42A7-BC38-10535A64A92C)
>   TRUE
>   END
>   RESULT = TRUE
>   Drain the Scheduled Queue
>   DriverEntry (Scheduled) - 58F7A62B-6280-42A7-BC38-10535A64A92C
> MmLoadImage - 58F7A62B-6280-42A7-BC38-10535A64A92C
> UpdatePeCoffPermissions: Mapping section 0 of image at 0xFFFE7000 with
> RO-XN permissions and size 0x7000
> UpdatePeCoffPermissions: Mapping section 0 of image at 0xFFFE7000 with RO-X
> permissions and size 0x7000
> UpdatePeCoffPermissions: Mapping section 1 of image at 0xFFFEE000 with
> RW-XN permissions and size 0x1000
> UpdatePeCoffPermissions: Mapping section 2 of image at 0xFFFEF000 with
> RO-XN permissions and size 0x1000
> add-symbol-file
> /home/supven01/work/mm_upstream/Build/StandaloneMmPkg/DEBUG_GCC5
> /AARCH64/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpu
> Driver/DEBUG/PiMmStandloneArmTfCpuDriver.dll 0xFFFE7000
> Loading MM driver at 0x000FFFE6000 EntryPoint=0x000FFFE7000
> PiMmStandloneArmTfCpuDriver.efi
> StartImage - 0xFFFE7000 (Standalone Mode)
> MmInstallProtocolInterface: 26EEB3DE-B689-492E-80F0-BE8BD7DA4BA7
> FFFEE008
> MmConfigurationMmNotify(26EEB3DE-B689-492E-80F0-BE8BD7DA4BA7) -
> FFFEE008
> MM Core registered MM Entry Point address FF2041C0
> MmInstallProtocolInterface: EB346B97-975F-4A9F-8B22-F8E92BB3D569
> FFFEE018
> Sharing Cpu Driver EP *0xFF217280 = 0xFFFE798C
> mNsCommBuffer.PhysicalStart - 0xFF600000
> mNsCommBuffer.PhysicalSize - 0x10000
> mNsCommBuffer: 0x00000000FF600000 - 0x10000
> mMpInformationHobData: 0x0000000000000008 - 0x8
> mMpInformationHobData[0x80000000]: 0, 0, 0
> mMpInformationHobData[0x80000001]: 0, 1, 1
> mMpInformationHobData[0x80000002]: 0, 2, 2
> mMpInformationHobData[0x80000003]: 0, 3, 3
> mMpInformationHobData[0x80000100]: 1, 0, 0
> mMpInformationHobData[0x80000101]: 1, 1, 1
> mMpInformationHobData[0x80000102]: 1, 2, 2
> mMpInformationHobData[0x80000103]: 1, 3, 3
> MmiHandlerRegister - GUID B65694CC-09E3-4C3B-B5CD-05F44D3CDBFF -
> Status 0
> MmiHandlerRegister - GUID 7081E22F-CAC6-4053-9468-675782CF88E5 -
> Status 0
> MmiHandlerRegister - GUID 60FF8964-E906-41D0-AFED-F241E974E08E - Status
> 0
> MmiHandlerRegister - GUID 02CE967A-DD7E-4FFC-9EE7-810CF0470880 -
> Status 0
> MmiHandlerRegister - GUID 2A571201-4966-47F6-8B86-F31E41F32F10 - Status
> 0
> MmiHandlerRegister - GUID 27ABF055-B1B8-4C26-8048-748F37BAA2DF -
> Status 0
> MmiHandlerRegister - GUID 7CE88FB3-4BD7-4679-87A8-A8D8DEE50D2B -
> Status 0
> MmMain Done!
> Shared Cpu Driver EP 0xFFFE798C
> 
> ARM TF (FVP UART0)
> NOTICE:  Booting Trusted Firmware
> NOTICE:  BL1: v1.4(debug):v1.4-96-gae48aad7-dirty
> NOTICE:  BL1: Built : 16:14:26, Nov  1 2017
> INFO:    BL1: RAM 0x4036000 - 0x403c000
> INFO:    BL1: Loading BL2
> INFO:    Loading image id=1 at address 0x4027000
> INFO:    Image id=1 loaded: 0x4027000 - 0x402c2a8
> NOTICE:  BL1: Booting BL2
> INFO:    Entry point address = 0x4027000
> INFO:    SPSR = 0x3c5
> NOTICE:  BL2: v1.4(debug):v1.4-96-gae48aad7-dirty
> NOTICE:  BL2: Built : 16:14:26, Nov  1 2017
> INFO:    BL2: Doing platform setup
> INFO:    Configuring TrustZone Controller
> INFO:    BL2: Loading image id 3
> INFO:    Loading image id=3 at address 0xff000000
> INFO:    Image id=3 loaded: 0xff000000 - 0xff00c230
> INFO:    BL2: Loading image id 4
> INFO:    Loading image id=4 at address 0xff200000
> INFO:    Image id=4 loaded: 0xff200000 - 0xff480000
> INFO:    BL2: Loading image id 5
> INFO:    Loading image id=5 at address 0x88000000
> INFO:    Image id=5 loaded: 0x88000000 - 0x88280000
> NOTICE:  BL1: Booting BL31
> INFO:    Entry point address = 0xff000000
> INFO:    SPSR = 0x3cd
> NOTICE:  BL31: v1.4(debug):v1.4-96-gae48aad7-dirty
> NOTICE:  BL31: Built : 16:14:26, Nov  1 2017
> INFO:    GICv3 with legacy support detected. ARM GICV3 driver initialized in
> EL3
> INFO:    BL31: Initializing runtime services
> INFO:    BL31: Initializing BL32
> NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
> NOTICE:    Start address  : 0xff217000
> NOTICE:    Number of pages: 1 (4096 bytes)
> NOTICE:    Attributes     : 0x7
> NOTICE:    (Equivalent TF attributes: 0x22)
> NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
> NOTICE:    Start address  : 0xff217000
> NOTICE:    Number of pages: 1 (4096 bytes)
> NOTICE:    Attributes     : 0x5
> NOTICE:    (Equivalent TF attributes: 0x2a)
> NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
> NOTICE:    Start address  : 0xff218000
> NOTICE:    Number of pages: 1 (4096 bytes)
> NOTICE:    Attributes     : 0x7
> NOTICE:    (Equivalent TF attributes: 0x22)
> NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
> NOTICE:    Start address  : 0xfffe7000
> NOTICE:    Number of pages: 7 (28672 bytes)
> NOTICE:    Attributes     : 0x7
> NOTICE:    (Equivalent TF attributes: 0x22)
> NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
> NOTICE:    Start address  : 0xfffe7000
> NOTICE:    Number of pages: 7 (28672 bytes)
> NOTICE:    Attributes     : 0x3
> NOTICE:    (Equivalent TF attributes: 0x2)
> NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
> NOTICE:    Start address  : 0xfffef000
> NOTICE:    Number of pages: 1 (4096 bytes)
> NOTICE:    Attributes     : 0x7
> NOTICE:    (Equivalent TF attributes: 0x22)
> INFO:    BL31: Preparing for EL3 exit to normal world
> INFO:    Entry point address = 0x88000000
> INFO:    SPSR = 0x3c9
> UEFI firmware (version  built at 16:14:14 on Nov  1 2017)
> ***
> 
> Supreeth Venkatesh (18):
>   ArmPkg: Add PCDs needed for MM communication driver.
>   ArmPkg/Drivers: Add EFI_MM_COMMUNICATION_PROTOCOL DXE driver.
>   ArmPkg/Include: Add MM interface SVC return codes.
>   ArmPkg/ArmMmuLib: Add MMU Library suitable for use in S-EL0.
>   ArmPkg/ArmMmuLib: Add MMU library inf file suitable for use in S-EL0.
>   StandaloneMmPkg: Add an AArch64 specific entry point library.
>   StandaloneMmPkg/FvLib: Add a common FV Library for management mode.
>   StandaloneMmPkg/MemLib: AARCH64 Specific instance of memory check
>     library.
>   StandaloneMmPkg/MemoryAllocationLib: Add MM memory allocation library.
>   StandaloneMmPkg/HobLib: Add AARCH64 Specific HOB Library for
>     management mode.
>   StandaloneMmPkg: MM driver entry point library.
>   StandaloneMmPkg/CpuMm: Add CPU driver suitable for ARM Platforms.
>   StandaloneMmPkg/Core: Implementation of Standalone MM Core Module.
>   StandaloneMmPkg: Describe the declaration, definition and fdf files.
>   ArmPkg: Extra action to update permissions for S-ELO MM Image.
>   BaseTools/AutoGen: Update header file for MM modules.
>   StandaloneMmPkg: Add application to test MM communication protocol.
>   StandaloneMmPkg: Add handler to handle event received from Normal
>     World.
> 
>  ArmPkg/ArmPkg.dec                                  |    3 +
>  .../Drivers/MmCommunicationDxe/MmCommunication.c   |  339 +++++++
>  .../Drivers/MmCommunicationDxe/MmCommunication.inf |   50 +
>  ArmPkg/Include/IndustryStandard/ArmMmSvc.h         |    9 +-
>  ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c    |  146 +++
>  ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf          |   37 +
>  .../DebugPeCoffExtraActionLib.c                    |  185 +++-
>  .../DebugPeCoffExtraActionLib.inf                  |    7 +
>  BaseTools/Source/Python/AutoGen/GenC.py            |   16 +-
>  .../Application/MmCommTestApp/MmCommTest.c         |   81 ++
>  .../Application/MmCommTestApp/MmCommTest.h         |   37 +
>  .../Application/MmCommTestApp/MmCommTest.inf       |   57 ++
>  StandaloneMmPkg/Core/Dependency.c                  |  389 +++++++
>  StandaloneMmPkg/Core/Dispatcher.c                  | 1071
> ++++++++++++++++++++
>  StandaloneMmPkg/Core/FwVol.c                       |  104 ++
>  StandaloneMmPkg/Core/Handle.c                      |  533
> ++++++++++
>  StandaloneMmPkg/Core/InstallConfigurationTable.c   |  178 ++++
>  StandaloneMmPkg/Core/Locate.c                      |  496 +++++++++
>  StandaloneMmPkg/Core/Mmi.c                         |  337 ++++++
>  StandaloneMmPkg/Core/Notify.c                      |  203 ++++
>  StandaloneMmPkg/Core/Page.c                        |  384 +++++++
>  StandaloneMmPkg/Core/Pool.c                        |  287 ++++++
>  StandaloneMmPkg/Core/StandaloneMmCore.c            |  747
> ++++++++++++++
>  StandaloneMmPkg/Core/StandaloneMmCore.h            |  903
> +++++++++++++++++
>  StandaloneMmPkg/Core/StandaloneMmCore.inf          |   82 ++
>  StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h |   66 ++
>  StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S          |   33 +
>  StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c    |  231 +++++
>  StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c           |  229 +++++
>  .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h        |   89 ++
>  .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf      |   60 ++
>  StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c      |   51 +
>  StandaloneMmPkg/Include/Guid/MmCoreData.h          |  132 +++
>  StandaloneMmPkg/Include/Guid/MmFvDispatch.h        |   38 +
>  StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h  |   62 ++
>  StandaloneMmPkg/Include/Guid/MpInformation.h       |   41 +
>  .../Library/Arm/StandaloneMmCoreEntryPoint.h       |  232 +++++
>  StandaloneMmPkg/Include/Library/FvLib.h            |  109 ++
>  StandaloneMmPkg/Include/Library/MemLib.h           |  140 +++
>  .../Include/Library/MmCoreStandaloneEntryPoint.h   |  101 ++
>  .../Include/Library/MmDriverStandaloneEntryPoint.h |  148 +++
>  StandaloneMmPkg/Include/StandaloneMm.h             |   36 +
>  StandaloneMmPkg/Library/FvLib/FvLib.c              |  366 +++++++
>  StandaloneMmPkg/Library/FvLib/FvLib.inf            |   57 ++
>  StandaloneMmPkg/Library/HobLib/Arm/HobLib.c        |  697
> +++++++++++++
>  StandaloneMmPkg/Library/HobLib/HobLib.inf          |   45 +
>  StandaloneMmPkg/Library/MemLib/Arm/MemLib.c        |  276 +++++
>  StandaloneMmPkg/Library/MemLib/MemLib.inf          |   47 +
>  .../MemoryAllocationLib/MemoryAllocationLib.c      |  907
> +++++++++++++++++
>  .../MemoryAllocationLib/MemoryAllocationLib.inf    |   49 +
>  .../MemoryAllocationLib/MemoryAllocationServices.h |   38 +
>  .../StandaloneMmCoreEntryPoint/Arm/CreateHobList.c |  200 ++++
>  .../Arm/SetPermissions.c                           |  278 +++++
>  .../Arm/StandaloneMmCoreEntryPoint.c               |  264 +++++
>  .../StandaloneMmCoreEntryPoint.inf                 |   53 +
>  .../StandaloneMmDriverEntryPoint.c                 |  102 ++
>  .../StandaloneMmDriverEntryPoint.inf               |   41 +
>  StandaloneMmPkg/StandaloneMmPkg.dec                |   49 +
>  StandaloneMmPkg/StandaloneMmPkg.dsc                |  132 +++
>  StandaloneMmPkg/StandaloneMmPkg.fdf                |  184 ++++
>  StandaloneMmPkg => StandaloneMmPkg~HEAD            |    0
>  61 files changed, 12244 insertions(+), 20 deletions(-)
>  create mode 100644
> ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.c
>  create mode 100644
> ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.inf
>  create mode 100644 ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c
>  create mode 100644 ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
>  create mode 100644
> StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.c
>  create mode 100644
> StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.h
>  create mode 100644
> StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.inf
>  create mode 100644 StandaloneMmPkg/Core/Dependency.c
>  create mode 100644 StandaloneMmPkg/Core/Dispatcher.c
>  create mode 100644 StandaloneMmPkg/Core/FwVol.c
>  create mode 100644 StandaloneMmPkg/Core/Handle.c
>  create mode 100644 StandaloneMmPkg/Core/InstallConfigurationTable.c
>  create mode 100644 StandaloneMmPkg/Core/Locate.c
>  create mode 100644 StandaloneMmPkg/Core/Mmi.c
>  create mode 100644 StandaloneMmPkg/Core/Notify.c
>  create mode 100644 StandaloneMmPkg/Core/Page.c
>  create mode 100644 StandaloneMmPkg/Core/Pool.c
>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.c
>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.h
>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.inf
>  create mode 100644
> StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
>  create mode 100644
> StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
>  create mode 100644
> StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
>  create mode 100644 StandaloneMmPkg/Include/Guid/MmCoreData.h
>  create mode 100644 StandaloneMmPkg/Include/Guid/MmFvDispatch.h
>  create mode 100644
> StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
>  create mode 100644 StandaloneMmPkg/Include/Guid/MpInformation.h
>  create mode 100644
> StandaloneMmPkg/Include/Library/Arm/StandaloneMmCoreEntryPoint.h
>  create mode 100644 StandaloneMmPkg/Include/Library/FvLib.h
>  create mode 100644 StandaloneMmPkg/Include/Library/MemLib.h
>  create mode 100644
> StandaloneMmPkg/Include/Library/MmCoreStandaloneEntryPoint.h
>  create mode 100644
> StandaloneMmPkg/Include/Library/MmDriverStandaloneEntryPoint.h
>  create mode 100644 StandaloneMmPkg/Include/StandaloneMm.h
>  create mode 100644 StandaloneMmPkg/Library/FvLib/FvLib.c
>  create mode 100644 StandaloneMmPkg/Library/FvLib/FvLib.inf
>  create mode 100644 StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
>  create mode 100644 StandaloneMmPkg/Library/HobLib/HobLib.inf
>  create mode 100644 StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
>  create mode 100644 StandaloneMmPkg/Library/MemLib/MemLib.inf
>  create mode 100644
> StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
>  create mode 100644
> StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
>  create mode 100644
> StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.h
>  create mode 100644
> StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/CreateHobList.c
>  create mode 100644
> StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/SetPermissions.
> c
>  create mode 100644
> StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMm
> CoreEntryPoint.c
>  create mode 100644
> StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreE
> ntryPoint.inf
>  create mode 100644
> StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriv
> erEntryPoint.c
>  create mode 100644
> StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriv
> erEntryPoint.inf
>  create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dec
>  create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dsc
>  create mode 100644 StandaloneMmPkg/StandaloneMmPkg.fdf
>  rename StandaloneMmPkg => StandaloneMmPkg~HEAD (100%)
> 
> --
> 2.16.2



^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 02/18] ArmPkg/Drivers: Add EFI_MM_COMMUNICATION_PROTOCOL DXE driver.
  2018-04-06 14:42 ` [PATCH v1 02/18] ArmPkg/Drivers: Add EFI_MM_COMMUNICATION_PROTOCOL DXE driver Supreeth Venkatesh
@ 2018-04-11 14:00   ` Achin Gupta
  2018-05-04 23:18     ` Supreeth Venkatesh
  0 siblings, 1 reply; 70+ messages in thread
From: Achin Gupta @ 2018-04-11 14:00 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

Hi Supreeth,

CIL.

On Fri, Apr 06, 2018 at 03:42:07PM +0100, Supreeth Venkatesh wrote:
> PI v1.5 Specification Volume 4 defines Management Mode Core Interface
> and defines EFI_MM_COMMUNICATION_PROTOCOL. This protocol provides a
> means of communicating between drivers outside of MM and MMI
> handlers inside of MM.
> 
> This patch implements the EFI_MM_COMMUNICATION_PROTOCOL DXE runtime
> driver for AARCH64 platforms. It uses SMCs allocated from the standard
> SMC range defined in DEN0060A_ARM_MM_Interface_Specification.pdf
> to communicate with the standalone MM environment in the secure world.
> 
> This patch also adds the MM Communication driver (.inf) file to
> define entry point for this driver and other compile
> related information the driver needs.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  .../Drivers/MmCommunicationDxe/MmCommunication.c   | 339 +++++++++++++++++++++
>  .../Drivers/MmCommunicationDxe/MmCommunication.inf |  50 +++
>  2 files changed, 389 insertions(+)
>  create mode 100644 ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.c
>  create mode 100644 ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.inf
> 
> diff --git a/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.c b/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.c
> new file mode 100644
> index 0000000000..e801c1c601
> --- /dev/null
> +++ b/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.c
> @@ -0,0 +1,339 @@
> +/** @file
> +
> +  Copyright (c) 2016-2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Library/ArmLib.h>
> +#include <Library/ArmSmcLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/DxeServicesTableLib.h>
> +#include <Library/HobLib.h>
> +#include <Library/PcdLib.h>
> +#include <Library/UefiBootServicesTableLib.h>
> +#include <Library/UefiRuntimeServicesTableLib.h>
> +
> +#include <Protocol/MmCommunication.h>
> +
> +#include <IndustryStandard/ArmStdSmc.h>
> +
> +//
> +// Address, Length of the pre-allocated buffer for communication with the secure
> +// world.
> +//
> +STATIC ARM_MEMORY_REGION_DESCRIPTOR  mNsCommBuffMemRegion;
> +
> +// Notification event when virtual address map is set.
> +STATIC EFI_EVENT  mSetVirtualAddressMapEvent;
> +
> +//
> +// Handle to install the MM Communication Protocol
> +//
> +STATIC EFI_HANDLE  mMmCommunicateHandle;
> +
> +/**
> +  Communicates with a registered handler.
> +
> +  This function provides an interface to send and receive messages to the
> +  Standalone MM environment on behalf of UEFI services.  This function is part
> +  of the MM Communication Protocol that may be called in physical mode prior to
> +  SetVirtualAddressMap() and in virtual mode after SetVirtualAddressMap().
> +
> +  @param[in]      This                The EFI_MM_COMMUNICATION_PROTOCOL
> +                                      instance.
> +  @param[in, out] CommBuffer          A pointer to the buffer to convey
> +                                      into MMRAM.
> +  @param[in, out] CommSize            The size of the data buffer being
> +                                      passed in. This is optional.
> +
> +  @retval EFI_SUCCESS                 The message was successfully posted.
> +  @retval EFI_INVALID_PARAMETER       The CommBuffer was NULL.
> +  @retval EFI_BAD_BUFFER_SIZE         The buffer size is incorrect for the MM
> +                                      implementation. If this error is
> +                                      returned, the MessageLength field in
> +                                      the CommBuffer header or the integer
> +                                      pointed by CommSize are updated to reflect
> +                                      the maximum payload size the
> +                                      implementation can accommodate.
> +  @retval EFI_ACCESS_DENIED           The CommunicateBuffer parameter
> +                                      or CommSize parameter, if not omitted,
> +                                      are in address range that cannot be
> +                                      accessed by the MM environment
> +**/
> +STATIC
> +EFI_STATUS
> +EFIAPI
> +MmCommunicationCommunicate (
> +  IN CONST EFI_MM_COMMUNICATION_PROTOCOL  *This,
> +  IN OUT VOID                             *CommBuffer,
> +  IN OUT UINTN                            *CommSize OPTIONAL
> +  )
> +{
> +  EFI_MM_COMMUNICATE_HEADER   *CommunicateHeader;
> +  ARM_SMC_ARGS                CommunicateSmcArgs;
> +  EFI_STATUS                  Status;
> +  UINTN                       BufferSize;
> +
> +  CommunicateHeader = CommBuffer;
> +  Status = EFI_ACCESS_DENIED;
> +  BufferSize = 0;
> +
> +  ZeroMem (&CommunicateSmcArgs, sizeof (ARM_SMC_ARGS));
> +
> +  //
> +  // Check parameters
> +  //
> +  if (CommBuffer == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  // If the length of the CommBuffer is 0 then return the expected length.
> +  if (CommSize) {
> +    if (*CommSize == 0) {
> +      *CommSize = mNsCommBuffMemRegion.Length;
> +      return EFI_BAD_BUFFER_SIZE;
> +    }
> +    //
> +    // CommSize must hold HeaderGuid and MessageLength
> +    //
> +    if (*CommSize < sizeof (EFI_MM_COMMUNICATE_HEADER)) {
> +        return EFI_INVALID_PARAMETER;
> +    }

The check looks inadequate as it does not cater for the MessageLength specified
in the EFI_MM_COMMUNICATE_HEADER i.e it will ensure there is at least a
EFI_MM_COMMUNICATE_HEADER but will not ensure that the message is included as
well.

Nit: It would be better if the parameters are checked before Status and
CommunicateSmcArgs are updated above.

> +    BufferSize = *CommSize;
> +  } else {
> +    BufferSize = CommunicateHeader->MessageLength +
> +                 sizeof (CommunicateHeader->HeaderGuid) +
> +                 sizeof (CommunicateHeader->MessageLength);
> +  }
> +
> +  //
> +  // If the buffer size is 0 or greater than what can be tolerated by the MM
> +  // environment then return the expected size.
> +  //
> +  if ((BufferSize == 0) ||
> +      (BufferSize > mNsCommBuffMemRegion.Length)) {
> +    CommunicateHeader->MessageLength = mNsCommBuffMemRegion.Length -
> +                                       sizeof (CommunicateHeader->HeaderGuid) -
> +                                       sizeof (CommunicateHeader->MessageLength);
> +    return EFI_BAD_BUFFER_SIZE;
> +  }

The 'if' condition works as expected to return the maximum buffer size if
'CommSize' is NULL. When 'CommSize' is used and the 'BufferSize' (derived from
'CommSize') is greater than what can be tolerated, then it is 'CommSize' that
must be updated as per the PI spec. but the 'if'condition will still update the
MessageLength.

> +
> +  // SMC Function ID
> +  CommunicateSmcArgs.Arg0 = ARM_SMC_ID_MM_COMMUNICATE_AARCH64;
> +
> +  // Reserved for Future. Must be Zero.
> +  CommunicateSmcArgs.Arg1 = 0;
> +
> +  if (mNsCommBuffMemRegion.VirtualBase) {
> +    CopyMem ((VOID *)mNsCommBuffMemRegion.VirtualBase, CommBuffer, BufferSize);
> +  } else {
> +    return EFI_ACCESS_DENIED;
> +  }

VirtualBase was set to PhysicalBase in MmCommunicationInitialize(). Is there any
reason why it would be unset here apart from a corruption of
mNsCommBuffMemRegion structure? If not, then this is a panic condition. It is
not the caller's fault and returning EFI_ACCESS_DENIED does not really help.

> +
> +  // For the SMC64 version, this parameter is a 64-bit Physical Address (PA)
> +  // or Intermediate Physical Address (IPA).
> +  // For the SMC32 version, this parameter is a 32-bit PA or IPA.
> +  CommunicateSmcArgs.Arg2 = (UINTN)mNsCommBuffMemRegion.PhysicalBase;
> +
> +  // comm_size_address is a PA or an IPA that holds the size of the
> +  // communication buffer being passed in. This parameter is optional
> +  // and can be omitted by passing a zero.
> +  // ARM does not recommend using it since this might require the
> +  // implementation to create a separate memory mapping for the parameter.
> +  // ARM recommends storing the buffer size in the buffer itself.
> +  CommunicateSmcArgs.Arg3 = 0;
> +
> +  // Call the Standalone MM environment.
> +  ArmCallSmc (&CommunicateSmcArgs);
> +
> +  switch (CommunicateSmcArgs.Arg0) {
> +  case ARM_SMC_MM_RET_SUCCESS:
> +    // On exit, the size of data being returned is inferred from
> +    // CommSize or MessageLength + Header.
> +    CopyMem (CommBuffer,
> +             (const VOID *)mNsCommBuffMemRegion.VirtualBase,
> +             BufferSize);

Umm! I vaguely remember we had a chat about this but this does not seem
right. The PI spec. does not help by being vague about how data should be copied
out and its length reported. However, here is my take.

If CommSize is used, then it must be updated by the handler with the size of the
data returned, zero otherwise. This is as far as the PI spec. goes in this
regard.

Even though the spec. does not say this, if CommSize is not used then,
MessageLength in the EFI_MM_COMMUNICATE_HEADER must be updated. This also means
that the handler has to preserve the EFI_MM_COMMUNICATE_HEADER in the CommBuffer
in the return path (again something the PI spec. does not say).

In the SMC ABI, CommSize maps to comm_size_address. We do not use this. So we
must rely on the MessageLength on the way back.

Using BufferSize does not seem right to me. We are screwed if the input size is
not equal to the output size. It is better to retrieve this information from the
MessageLength as the comment says.

> +    Status = EFI_SUCCESS;
> +    break;
> +
> +  case ARM_SMC_MM_RET_NOT_SUPPORTED:

NOT_SUPPORTED means that MM_COMMUNICATE is not implemented which must not be the
case if we are here. MmCommunicationInitialize() must call MM_VERSION to check
this. In fact, the MM interface specification should be updated to remove
NOT_SUPPORTED for MM_COMMUNICATE if MM_VERSION is implemented.

Can you panic() if this is returned?

> +  case ARM_SMC_MM_RET_INVALID_PARAMS:
> +    Status = EFI_INVALID_PARAMETER;
> +    break;
> +
> +  case ARM_SMC_MM_RET_DENIED:
> +    Status = EFI_ACCESS_DENIED;
> +    break;
> +
> +  case ARM_SMC_MM_RET_NO_MEMORY:
> +    // Unexpected error since the CommSize was checked for zero length
> +    // prior to issuing the SMC
> +  default:
> +    Status = EFI_ACCESS_DENIED;
> +    ASSERT (0);
> +  }
> +
> +  return Status;
> +}
> +
> +//
> +// MM Communication Protocol instance
> +//
> +EFI_MM_COMMUNICATION_PROTOCOL  mMmCommunication = {
> +  MmCommunicationCommunicate
> +};
> +
> +/**
> +  Notification callback on SetVirtualAddressMap event.
> +
> +  This function notifies the MM communication protocol interface on
> +  SetVirtualAddressMap event and converts pointers used in this driver
> +  from physical to virtual address.
> +
> +  @param  Event          SetVirtualAddressMap event.
> +  @param  Context        A context when the SetVirtualAddressMap triggered.
> +
> +  @retval EFI_SUCCESS    The function executed successfully.
> +  @retval Other          Some error occurred when executing this function.
> +
> +**/
> +STATIC
> +VOID
> +EFIAPI
> +NotifySetVirtualAddressMap (
> +  IN EFI_EVENT  Event,
> +  IN VOID      *Context
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  Status = gRT->ConvertPointer (EFI_OPTIONAL_PTR,
> +                                (VOID **)&mNsCommBuffMemRegion.VirtualBase
> +                               );
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_ERROR, "NotifySetVirtualAddressMap():"
> +            " Unable to convert MM runtime pointer. Status:0x%r\n", Status));
> +  }
> +
> +}
> +
> +/**
> +  The Entry Point for MM Communication
> +
> +  This function installs the MM communication protocol interface and finds out
> +  what type of buffer management will be required prior to invoking the
> +  communication SMC.
> +
> +  @param  ImageHandle    The firmware allocated handle for the EFI image.
> +  @param  SystemTable    A pointer to the EFI System Table.
> +
> +  @retval EFI_SUCCESS    The entry point is executed successfully.
> +  @retval Other          Some error occurred when executing this entry point.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCommunicationInitialize (
> +  IN EFI_HANDLE         ImageHandle,
> +  IN EFI_SYSTEM_TABLE  *SystemTable
> +  )
> +{
> +  EFI_STATUS                 Status;
> +
> +  mNsCommBuffMemRegion.PhysicalBase = PcdGet64 (PcdMmBufferBase);
> +  // During boot , Virtual and Physical are same
> +  mNsCommBuffMemRegion.VirtualBase = mNsCommBuffMemRegion.PhysicalBase;
> +  mNsCommBuffMemRegion.Length = PcdGet64 (PcdMmBufferSize);
> +
> +  if (mNsCommBuffMemRegion.PhysicalBase == 0) {
> +    DEBUG ((DEBUG_ERROR, "MmCommunicateInitialize: "
> +            "Invalid MM Buffer Base Address.\n"));
> +    goto ReturnErrorStatus;
> +  }
> +

We need to call MM_VERSION to check whether there is a StandaloneMm imp. in the
Secure world or not. 

> +  if (mNsCommBuffMemRegion.Length == 0) {
> +    DEBUG ((DEBUG_ERROR, "MmCommunicateInitialize: "
> +            "Maximum Buffer Size is zero.\n"));
> +    goto ReturnErrorStatus;
> +  }
> +
> +  Status = gDS->AddMemorySpace (EfiGcdMemoryTypeSystemMemory,
> +                                mNsCommBuffMemRegion.PhysicalBase,
> +                                mNsCommBuffMemRegion.Length,
> +                                EFI_MEMORY_WB |
> +                                EFI_MEMORY_XP |
> +                                EFI_MEMORY_RUNTIME);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_ERROR, "MmCommunicateInitialize: "
> +            "Failed to add MM-NS Buffer Memory Space\n"));
> +    goto ReturnErrorStatus;
> +  }
> +
> +  Status = gDS->SetMemorySpaceAttributes(mNsCommBuffMemRegion.PhysicalBase,
> +                                         mNsCommBuffMemRegion.Length,
> +                                         EFI_MEMORY_WB | EFI_MEMORY_XP);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_ERROR, "MmCommunicateInitialize: "
> +            "Failed to set MM-NS Buffer Memory attributes\n"));
> +    goto CleanAddedMemorySpace;
> +  }
> +
> +  Status = gBS->AllocatePages (AllocateAddress,
> +                               EfiRuntimeServicesData,
> +                               EFI_SIZE_TO_PAGES (mNsCommBuffMemRegion.Length),
> +                               &mNsCommBuffMemRegion.PhysicalBase);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_ERROR, "MmCommunicateInitialize: "
> +            "Failed to allocate MM-NS Buffer Memory Space\n"));
> +    goto CleanAddedMemorySpace;
> +  }
> +
> +  // Install the communication protocol
> +  Status = gBS->InstallProtocolInterface (&mMmCommunicateHandle,
> +                                          &gEfiMmCommunicationProtocolGuid,
> +                                          EFI_NATIVE_INTERFACE,
> +                                          &mMmCommunication);
> +  if (EFI_ERROR(Status)) {
> +    DEBUG ((DEBUG_ERROR, "MmCommunicationInitialize: "
> +            "Failed to install MM communication protocol\n"));
> +    goto CleanAllocatedPages;
> +  }
> +
> +  // Register notification callback when  virtual address is associated
> +  // with the physical address.
> +  // Create a Set Virtual Address Map event.
> +  //
> +  Status = gBS->CreateEvent (EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,  // Type
> +                             TPL_NOTIFY,                         // NotifyTpl
> +                             NotifySetVirtualAddressMap,         // NotifyFunction
> +                             NULL,                               // NotifyContext
> +                             &mSetVirtualAddressMapEvent         // Event
> +                            );
> +  if (Status == EFI_SUCCESS) {
> +    return Status;
> +  }
> +
> +  gBS->UninstallProtocolInterface(mMmCommunicateHandle,
> +                                  &gEfiMmCommunicationProtocolGuid,
> +                                  &mMmCommunication);
> +
> +CleanAllocatedPages:
> +  gBS->FreePages (mNsCommBuffMemRegion.PhysicalBase,
> +                  EFI_SIZE_TO_PAGES (mNsCommBuffMemRegion.Length));
> +
> +CleanAddedMemorySpace:
> +  gDS->RemoveMemorySpace (mNsCommBuffMemRegion.PhysicalBase,
> +                          mNsCommBuffMemRegion.Length);
> +
> +ReturnErrorStatus:
> +  return EFI_INVALID_PARAMETER;
> +}
> diff --git a/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.inf b/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.inf
> new file mode 100644
> index 0000000000..344d55f333
> --- /dev/null
> +++ b/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.inf
> @@ -0,0 +1,50 @@
> +#/** @file
> +#
> +#  DXE MM Communicate driver
> +#
> +#  Copyright (c) 2016 - 2017, ARM Limited. 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
> +#
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#**/
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = ArmMmCommunication
> +  FILE_GUID                      = 09EE81D3-F15E-43F4-85B4-CB9873DA5D6B
> +  MODULE_TYPE                    = DXE_RUNTIME_DRIVER
> +  VERSION_STRING                 = 1.0
> +
> +  ENTRY_POINT                    = MmCommunicationInitialize
> +
> +[Sources.Common]
> +  MmCommunication.c

Since this is a AArch64 only driver. Should it not be under
Sources.Common.AArch64?

Cheers,
Achin

> +
> +[Packages]
> +  ArmPkg/ArmPkg.dec
> +  MdePkg/MdePkg.dec
> +
> +[LibraryClasses]
> +  ArmLib
> +  ArmSmcLib
> +  BaseMemoryLib
> +  DebugLib
> +  DxeServicesTableLib
> +  HobLib
> +  UefiDriverEntryPoint
> +
> +[Protocols]
> +  gEfiMmCommunicationProtocolGuid              ## PRODUCES
> +
> +[Pcd.common]
> +  gArmTokenSpaceGuid.PcdMmBufferBase
> +  gArmTokenSpaceGuid.PcdMmBufferSize
> +
> +[Depex]
> +  AFTER gArmGicDxeFileGuid
> -- 
> 2.16.2
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 03/18] ArmPkg/Include: Add MM interface SVC return codes.
  2018-04-06 14:42 ` [PATCH v1 03/18] ArmPkg/Include: Add MM interface SVC return codes Supreeth Venkatesh
@ 2018-04-11 14:38   ` Achin Gupta
  2018-05-04 23:19     ` Supreeth Venkatesh
  0 siblings, 1 reply; 70+ messages in thread
From: Achin Gupta @ 2018-04-11 14:38 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

Hi Supreeth,

On Fri, Apr 06, 2018 at 03:42:08PM +0100, Supreeth Venkatesh wrote:
> This patch adds the Management Mode(MM) SVC return codes as specified in
> http://infocenter.arm.com/help/topic/com.arm.doc.den0060a/DEN0060A_ARM_MM_Interface_Specification.pdf.
> Also, corrects SVC ID for retrieving SPM version information.

The MM interface specification says nothing about these SVCs. At the moment,
this interface is exported by Arm TF. So lets say that instead. Also, it would
make sense to rename this file to ArmTfSpmSvc.h or similar 'cause this SVC
interface is usable by non-MM partitions too.

cheers,
Achin

> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  ArmPkg/Include/IndustryStandard/ArmMmSvc.h | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/ArmPkg/Include/IndustryStandard/ArmMmSvc.h b/ArmPkg/Include/IndustryStandard/ArmMmSvc.h
> index 4c7b6c3386..a64b9ec23c 100644
> --- a/ArmPkg/Include/IndustryStandard/ArmMmSvc.h
> +++ b/ArmPkg/Include/IndustryStandard/ArmMmSvc.h
> @@ -20,7 +20,7 @@
>   * delegated events and request the Secure partition manager to perform
>   * privileged operations on its behalf.
>   */
> -#define ARM_SVC_ID_SPM_VERSION_AARCH64             0xC4000060
> +#define ARM_SVC_ID_SPM_VERSION_AARCH32             0x84000060
>  #define ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64       0xC4000061
>  #define ARM_SVC_ID_SP_GET_MEM_ATTRIBUTES_AARCH64   0xC4000064
>  #define ARM_SVC_ID_SP_SET_MEM_ATTRIBUTES_AARCH64   0xC4000065
> @@ -40,4 +40,11 @@
>      ((((c_perm) & SET_MEM_ATTR_CODE_PERM_MASK) << SET_MEM_ATTR_CODE_PERM_SHIFT) | \
>      (( (d_perm) & SET_MEM_ATTR_DATA_PERM_MASK) << SET_MEM_ATTR_DATA_PERM_SHIFT))
>  
> +/* MM SVC Return error codes */
> +#define ARM_SVC_SPM_RET_SUCCESS               0
> +#define ARM_SVC_SPM_RET_NOT_SUPPORTED        -1
> +#define ARM_SVC_SPM_RET_INVALID_PARAMS       -2
> +#define ARM_SVC_SPM_RET_DENIED               -3
> +#define ARM_SVC_SPM_RET_NO_MEMORY            -5
> +
>  #endif
> -- 
> 2.16.2
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 01/18] ArmPkg: Add PCDs needed for MM communication driver.
  2018-04-06 14:42 ` [PATCH v1 01/18] ArmPkg: Add PCDs needed for MM communication driver Supreeth Venkatesh
@ 2018-04-11 14:43   ` Achin Gupta
       [not found]     ` <AM4PR0802MB23063743A3B2F5A552BE320580870@AM4PR0802MB2306.eurprd08.prod.outlook.com>
  2018-05-04 23:17     ` Supreeth Venkatesh
  0 siblings, 2 replies; 70+ messages in thread
From: Achin Gupta @ 2018-04-11 14:43 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

On Fri, Apr 06, 2018 at 03:42:06PM +0100, Supreeth Venkatesh wrote:
> This patch defines PCDs to describe the base address and size of
> communication buffer between normal world (uefi) and standalone MM
> environment in the secure world.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  ArmPkg/ArmPkg.dec | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/ArmPkg/ArmPkg.dec b/ArmPkg/ArmPkg.dec
> index a55b6268ff..b64942220b 100644
> --- a/ArmPkg/ArmPkg.dec
> +++ b/ArmPkg/ArmPkg.dec
> @@ -223,6 +223,9 @@
>    gArmTokenSpaceGuid.PcdSystemMemoryBase|0|UINT64|0x00000029
>    gArmTokenSpaceGuid.PcdSystemMemorySize|0|UINT64|0x0000002A
>  
> +  gArmTokenSpaceGuid.PcdMmBufferBase|0|UINT64|0x00000045
> +  gArmTokenSpaceGuid.PcdMmBufferSize|0|UINT64|0x00000046
> +
>  [PcdsFixedAtBuild.common, PcdsDynamic.common]
>    #
>    # ARM Architectural Timer
> -- 
> 2.16.2
> 

Acked-by: Achin Gupta <achin.gupta@arm.com>


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 04/18] ArmPkg/ArmMmuLib: Add MMU Library suitable for use in S-EL0.
  2018-04-06 14:42 ` [PATCH v1 04/18] ArmPkg/ArmMmuLib: Add MMU Library suitable for use in S-EL0 Supreeth Venkatesh
@ 2018-04-11 19:21   ` Achin Gupta
  2018-05-04 23:19     ` Supreeth Venkatesh
  0 siblings, 1 reply; 70+ messages in thread
From: Achin Gupta @ 2018-04-11 19:21 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

Hi Supreeth,

On Fri, Apr 06, 2018 at 03:42:09PM +0100, Supreeth Venkatesh wrote:
> The Standalone MM environment runs in S-EL0 in AArch64 on ARM Standard
> Platforms. Privileged firmware e.g. ARM Trusted Firmware sets up its
> architectural context including the initial translation tables for the
> S-EL1/EL0 translation regime. The MM environment could still request ARM
> TF to change the memory attributes of memory regions during
> initialization.

The commit message needs more detail to better flesh out why we are doing what
we are doing here i.e. the StandaloneMm image is a FV that encapsulates the MM
foundation and drivers. These are PE-COFF images with data and text
segments. Arm TF does not have visibility of the contents of the FV. Moreover,
the driver images are relocated upon dispatch. However, to initialise the MM
environment, Arm TF has to create translation tables with sane default
attributes for the memory occupied by the FV............

I am hoping you can extrapolate from here and clearly describe what problem this
library solves.

> 
> This patch adds a simple MMU library suitable for execution in S-EL0 and
> requesting operations from higher exception levels.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c | 146 ++++++++++++++++++++++++
>  1 file changed, 146 insertions(+)
>  create mode 100644 ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c
> 
> diff --git a/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c b/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c

I am not sure about the name of the library. ArmMmuSecLib sounds like an MMU
library for the SEC phase in the Normal world. Can we call it
ArmMmuSecStandaloneMmLib or similar.

> new file mode 100644
> index 0000000000..56969e31d1
> --- /dev/null
> +++ b/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c
> @@ -0,0 +1,146 @@
> +/** @file
> +*  File managing the MMU for ARMv8 architecture in S-EL0
> +*
> +*  Copyright (c) 2017, ARM Limited. All rights reserved.

Nit: Copyright 2018? For this and other files?

> +*
> +*  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
> +*
> +*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +*
> +**/
> +
> +#include <Uefi.h>
> +#include <Chipset/AArch64.h>
> +#include <IndustryStandard/ArmMmSvc.h>
> +
> +#include <Library/ArmLib.h>
> +#include <Library/ArmMmuLib.h>
> +#include <Library/ArmSvcLib.h>
> +#include <Library/BaseLib.h>
> +#include <Library/DebugLib.h>
> +
> +EFI_STATUS
> +RequestMemoryPermissionChange(
> +  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
> +  IN  UINT64                    Length,
> +  IN  UINTN                     Permissions
> +  )
> +{
> +  EFI_STATUS    Status;
> +  ARM_SVC_ARGS  ChangeMemoryPermissionsSvcArgs = {0};
> +
> +  ChangeMemoryPermissionsSvcArgs.Arg0 = ARM_SVC_ID_SP_SET_MEM_ATTRIBUTES_AARCH64;
> +  ChangeMemoryPermissionsSvcArgs.Arg1 = BaseAddress;
> +  ChangeMemoryPermissionsSvcArgs.Arg2 = (Length >= EFI_PAGE_SIZE) ? \
> +                                         Length >> EFI_PAGE_SHIFT : 1;
> +  ChangeMemoryPermissionsSvcArgs.Arg3 = Permissions;
> +
> +  ArmCallSvc(&ChangeMemoryPermissionsSvcArgs);
> +
> +  Status = ChangeMemoryPermissionsSvcArgs.Arg0;
> +
> +  switch (Status) {
> +  case ARM_SVC_SPM_RET_SUCCESS:
> +    Status = EFI_SUCCESS;
> +    break;
> +
> +  case ARM_SVC_SPM_RET_NOT_SUPPORTED:
> +    Status = EFI_UNSUPPORTED;
> +    break;
> +
> +  case ARM_SVC_SPM_RET_INVALID_PARAMS:
> +    Status = EFI_INVALID_PARAMETER;
> +    break;
> +
> +  case ARM_SVC_SPM_RET_DENIED:
> +    Status = EFI_ACCESS_DENIED;
> +    break;
> +
> +  case ARM_SVC_SPM_RET_NO_MEMORY:
> +    Status = EFI_BAD_BUFFER_SIZE;
> +    break;
> +
> +  default:
> +    Status = EFI_ACCESS_DENIED;
> +    ASSERT (0);
> +  }
> +
> +  return Status;
> +}
> +
> +EFI_STATUS
> +ArmSetMemoryRegionNoExec (
> +  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
> +  IN  UINT64                    Length
> +  )
> +{
> +  return RequestMemoryPermissionChange(BaseAddress,
> +                                       Length,
> +                                       SET_MEM_ATTR_MAKE_PERM_REQUEST( \
> +                                         SET_MEM_ATTR_DATA_PERM_RO, \
> +                                         SET_MEM_ATTR_CODE_PERM_XN));
> +}
> +
> +EFI_STATUS
> +ArmClearMemoryRegionNoExec (
> +  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
> +  IN  UINT64                    Length
> +  )
> +{
> +  return RequestMemoryPermissionChange(BaseAddress,
> +                                       Length,
> +                                       SET_MEM_ATTR_MAKE_PERM_REQUEST( \
> +                                         SET_MEM_ATTR_DATA_PERM_RO, \
> +                                         SET_MEM_ATTR_CODE_PERM_X));
> +}
> +
> +EFI_STATUS
> +ArmSetMemoryRegionReadOnly (
> +  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
> +  IN  UINT64                    Length
> +  )
> +{
> +  return RequestMemoryPermissionChange(BaseAddress,
> +                                       Length,
> +                                       SET_MEM_ATTR_MAKE_PERM_REQUEST( \
> +                                         SET_MEM_ATTR_DATA_PERM_RO, \
> +                                         SET_MEM_ATTR_CODE_PERM_XN));
> +}
> +
> +EFI_STATUS
> +ArmClearMemoryRegionReadOnly (
> +  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
> +  IN  UINT64                    Length
> +  )
> +{
> +  return RequestMemoryPermissionChange(BaseAddress,
> +                                       Length,
> +                                       SET_MEM_ATTR_MAKE_PERM_REQUEST( \
> +                                         SET_MEM_ATTR_DATA_PERM_RW, \
> +                                         SET_MEM_ATTR_CODE_PERM_XN));
> +}

The above four functions were written as prototypes in the edk2-staging
branch. I do not think they are adequate for upstreaming since each function
makes assumptions about the current data or instruction access permission of the
input memory region instead of only doing what the function's name suggests.

For example, ArmSetMemoryRegionNoExec() is supposed to only set the XN
bit. However, it also sets the data access permission to RO. If the region was
RW then this will lead to incorrect behaviour. Ditto for the other functions.

We need a GetMemoryPermission() function that first uses the
ARM_SVC_ID_SP_GET_MEM_ATTRIBUTES_AARCH64 call to obtain the memory attributes of
the input region. Each of the above functions must use this new function and
only change the data or instruction permission attribute as appropriate.

cheers,
Achin

> +
> +EFI_STATUS
> +EFIAPI
> +ArmConfigureMmu (
> +  IN  ARM_MEMORY_REGION_DESCRIPTOR  *MemoryTable,
> +  OUT VOID                          **TranslationTableBase OPTIONAL,
> +  OUT UINTN                         *TranslationTableSize OPTIONAL
> +  )
> +{
> +  return EFI_UNSUPPORTED;
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +ArmMmuSecLibConstructor (
> +  IN EFI_HANDLE            ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
> +  )
> +{
> +  return EFI_SUCCESS;
> +}
> -- 
> 2.16.2
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 05/18] ArmPkg/ArmMmuLib: Add MMU library inf file suitable for use in S-EL0.
  2018-04-06 14:42 ` [PATCH v1 05/18] ArmPkg/ArmMmuLib: Add MMU library inf file " Supreeth Venkatesh
@ 2018-04-11 19:24   ` Achin Gupta
  2018-05-04 23:19     ` Supreeth Venkatesh
  0 siblings, 1 reply; 70+ messages in thread
From: Achin Gupta @ 2018-04-11 19:24 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

On Fri, Apr 06, 2018 at 03:42:10PM +0100, Supreeth Venkatesh wrote:
> This patch adds the definitions, sources, packages and library classes
> needed to compile and link MMU Library suitable for use in S-EL0.
> 
> Currently, this is used only during the Standalone MM Core
> initialization and hence defined as MM_CORE_STANDALONE Module.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf | 37 +++++++++++++++++++++++++++++++
>  1 file changed, 37 insertions(+)
>  create mode 100644 ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
> 
> diff --git a/ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf b/ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
> new file mode 100644
> index 0000000000..5c802923da
> --- /dev/null
> +++ b/ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
> @@ -0,0 +1,37 @@
> +#/** @file
> +#
> +#  Copyright (c) 2017, ARM Limited. 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
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#
> +#**/
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = ArmMmuSecLib
> +  FILE_GUID                      = da8f0232-fb14-42f0-922c-63104d2c70bd
> +  MODULE_TYPE                    = MM_CORE_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  LIBRARY_CLASS                  = ArmMmuSecLib|MM_CORE_STANDALONE
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  CONSTRUCTOR                    = ArmMmuSecLibConstructor
> +
> +[Sources.AARCH64]
> +  AArch64/ArmMmuSecLib.c
> +
> +[Packages]
> +  ArmPkg/ArmPkg.dec
> +  MdePkg/MdePkg.dec
> +
> +[LibraryClasses]
> +  ArmLib
> +  CacheMaintenanceLib
> +  MemoryAllocationLib
> +
> +
> -- 
> 2.16.2
> 

Acked-by: Achin Gupta <achin.gupta@arm.com>


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 06/18] StandaloneMmPkg: Add an AArch64 specific entry point library.
  2018-04-06 14:42 ` [PATCH v1 06/18] StandaloneMmPkg: Add an AArch64 specific entry point library Supreeth Venkatesh
@ 2018-04-16 14:04   ` Achin Gupta
  2018-05-04 23:20     ` Supreeth Venkatesh
  0 siblings, 1 reply; 70+ messages in thread
From: Achin Gupta @ 2018-04-16 14:04 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

Hi Supreeth,

On Fri, Apr 06, 2018 at 03:42:11PM +0100, Supreeth Venkatesh wrote:
> The Standalone MM environment runs in S-EL0 in AArch64 on ARM Standard
> Platforms and is initialised during the SEC phase. ARM Trusted firmware
> in EL3 is responsible for initialising the architectural context for
> S-EL0 and loading the Standalone MM image. The memory allocated to this
> image is marked as RO+X. Heap memory is marked as RW+XN.
> 
> Certain actions have to be completed prior to executing the generic code
> in the Standalone MM Core module. These are:
> 
> 1. Memory permission attributes for each section of the Standalone MM
>    Core module need to be changed prior to accessing any RW data.
> 
> 2. A Hob list has to be created with information that allows the MM
>    environment to initialise and dispatch drivers.
> 
> Furthermore, this module is responsible for handing over runtime MM
> events to the Standalone MM CPU driver and returning control to ARM
> Trusted Firmware upon event completion. Hence it needs to know the CPU
> driver entry point.
> 
> This patch implements an entry point module that ARM Trusted Firmware
> jumps to in S-EL0. It then performs the above actions before calling the
> Standalone MM Foundation entry point and handling subsequent MM events.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  .../Library/Arm/StandaloneMmCoreEntryPoint.h       | 232 +++++++++++++++++
>  .../Include/Library/MmCoreStandaloneEntryPoint.h   | 101 ++++++++
>  .../StandaloneMmCoreEntryPoint/Arm/CreateHobList.c | 200 +++++++++++++++
>  .../Arm/SetPermissions.c                           | 278 +++++++++++++++++++++
>  .../Arm/StandaloneMmCoreEntryPoint.c               | 264 +++++++++++++++++++
>  .../StandaloneMmCoreEntryPoint.inf                 |  53 ++++
>  StandaloneMmPkg => StandaloneMmPkg~HEAD            |   0
>  7 files changed, 1128 insertions(+)
>  create mode 100644 StandaloneMmPkg/Include/Library/Arm/StandaloneMmCoreEntryPoint.h

The names of this file and the one below are re-arrangements of the same
words. This is confusing. It would be better to stick to one convention. Since
the name of the package starts with StandaloneMm, everything else should follow
suit.

So can this file be renamed to ArmStandaloneMmCoreEntryPoint.h and the one below
to StandaloneMmCoreEntryPoint.h unless you think this can be done differently?

>  create mode 100644 StandaloneMmPkg/Include/Library/MmCoreStandaloneEntryPoint.h

<tag>

>  create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/CreateHobList.c
>  create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/SetPermissions.c

PeCoffExtraActionLib and the HobLib are pre-requisites for these two
files. Should they not appear first in the patch stack?

>  create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c
>  create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf

Can this and the tagged file above go in a separate commit? Cannot see how they
are related to the Arm specific entry point.

>  rename StandaloneMmPkg => StandaloneMmPkg~HEAD (100%)
> 
> diff --git a/StandaloneMmPkg/Include/Library/Arm/StandaloneMmCoreEntryPoint.h b/StandaloneMmPkg/Include/Library/Arm/StandaloneMmCoreEntryPoint.h
> new file mode 100644
> index 0000000000..029c6c476c
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Library/Arm/StandaloneMmCoreEntryPoint.h
> @@ -0,0 +1,232 @@
> +/** @file
> +  Entry point to the Standalone MM Foundation when initialised during the SEC
> +  phase on ARM platforms
> +
> +Copyright (c) 2017, ARM Ltd. All rights reserved.<BR>

Copyright year needs to be updated.

> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef __MODULE_ENTRY_POINT_H__
> +#define __MODULE_ENTRY_POINT_H__

Name of this define needs to be changed

> +
> +#include <Library/PeCoffLib.h>
> +#include <Library/FvLib.h>
> +
> +#define CPU_INFO_FLAG_PRIMARY_CPU  0x00000001
> +
> +typedef
> +EFI_STATUS
> +(*PI_MM_CPU_TP_FW_ENTRYPOINT) (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  );

This typedef is not being used.

> +
> +typedef struct {
> +  UINT8  Type;       /* type of the structure */
> +  UINT8  Version;    /* version of this structure */
> +  UINT16 Size;      /* size of this structure in bytes */
> +  UINT32 Attr;      /* attributes: unused bits SBZ */
> +} EFI_PARAM_HEADER;
> +
> +typedef struct {
> +  UINT64 Mpidr;
> +  UINT32 LinearId;
> +  UINT32 Flags;
> +} EFI_SECURE_PARTITION_CPU_INFO;
> +
> +typedef struct {
> +  EFI_PARAM_HEADER              Header;
> +  UINT64                        SpMemBase;
> +  UINT64                        SpMemLimit;
> +  UINT64                        SpImageBase;
> +  UINT64                        SpStackBase;
> +  UINT64                        SpHeapBase;
> +  UINT64                        SpNsCommBufBase;
> +  UINT64                        SpSharedBufBase;
> +  UINT64                        SpImageSize;
> +  UINT64                        SpPcpuStackSize;
> +  UINT64                        SpHeapSize;
> +  UINT64                        SpNsCommBufSize;
> +  UINT64                        SpPcpuSharedBufSize;
> +  UINT32                        NumSpMemRegions;
> +  UINT32                        NumCpus;
> +  EFI_SECURE_PARTITION_CPU_INFO *CpuInfo;
> +} EFI_SECURE_PARTITION_BOOT_INFO;
> +
> +typedef struct {
> +  EFI_PARAM_HEADER               h;
> +  UINT64                         SpStackBase;
> +  UINT64                         SpSharedBufBase;
> +  UINT32                         SpPcpuStackSize;
> +  UINT32                         SpPcpuSharedBufSize;
> +  EFI_SECURE_PARTITION_CPU_INFO  CpuInfo;
> +} EFI_SECURE_PARTITION_WARM_BOOT_INFO;

This data structure is not being used.

> +
> +
> +typedef
> +EFI_STATUS
> +(*PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT) (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  );

This prototype interface is not adequate for upstreaming. I am thinking of the
following:

typedef
EFI_STATUS
(*PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT) (
  IN UINT64 EventInfoAddr,
  IN UINT32 EventInfoSize,
  IN UINT32 CpuNumber
  );

When the MM_COMMUNICATE SMC is invoked, EventInfoAddr -> MM communication
buffer. Ditto for EventInfoSize.

I don't think there is a need to pass EventId any longer. The original thinking
was that for asynchronous events e.g. interrupts handled in an MM partition, Arm
TF will not use UUIDs. Instead EventIDs will be used. Hence there was a need to
convert an EventID into a UUID in the MM CPU Driver. It would then use the UUID
to call SmiManage(). This restriction is no longer true. So, the SPM can
delegate an asynchronous event using the GUID of a StandaloneMM driver just like
the normal world populates the communication buffer.

This will result in changes to the MM CPU driver. I will comment on those
separately. 

We also need to tweak the ABI during an ERET from SP_EVENT_COMPLETE to the
following.

x0 -> Address of versioned data structure populated in RO memory shared between
      SPM and SP + Paylod
x1 -> MBZ. Reserved.
x2 -> MBZ. Reserved.
x3 -> MBZ. Reserved.

The versioned data structure could look like this:

typedef struct {^M
  EFI_PARAM_HEADER              Header;^M   
  UINT32                        CpuNumber;^M
  UINT64                        EventBufferAddress;^M // MM Communication buffer
  UINT32                        EventBufferSize;^M    // MM Comm. buffer size
} EFI_SECURE_PARTITION_EVENT_INFO;^M

The point being that just like the StandaloneMmCoreEntryPoint and SPM share a
data structure to pass information during initialisation, the above data
structure will be used to pass information when an event has to be delegated
from the SPM.

> +
> +typedef struct {
> +  PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT *ArmTfCpuDriverEpPtr;
> +} ARM_TF_CPU_DRIVER_EP_DESCRIPTOR;
> +
> +typedef RETURN_STATUS (*REGION_PERMISSION_UPDATE_FUNC) (
> +  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
> +  IN  UINT64                    Length
> +  );
> +
> +/**
> +  Privileged firmware assigns RO & Executable attributes to all memory occupied
> +  by the Boot Firmware Volume. This function sets the correct permissions of
> +  sections in the Standalone MM Core module to be able to access RO and RW data
> +  and make further progress in the boot process.
> +
> +  @param  ImageContext           Pointer to PE/COFF image context
> +  @param  SectionHeaderOffset    Offset of PE/COFF image section header
> +  @param  NumberOfSections       Number of Sections
> +  @param  TextUpdater            Function to change code permissions
> +  @param  ReadOnlyUpdater        Function to change RO permissions
> +  @param  ReadWriteUpdater       Function to change RW permissions
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +UpdateMmFoundationPeCoffPermissions (
> +  IN  CONST PE_COFF_LOADER_IMAGE_CONTEXT      *ImageContext,
> +  IN  UINT32                                  SectionHeaderOffset,
> +  IN  CONST  UINTN                            NumberOfSections,

UINT32?

> +  IN  REGION_PERMISSION_UPDATE_FUNC           TextUpdater,
> +  IN  REGION_PERMISSION_UPDATE_FUNC           ReadOnlyUpdater,
> +  IN  REGION_PERMISSION_UPDATE_FUNC           ReadWriteUpdater
> +  );
> +
> +
> +/**
> +  Privileged firmware assigns RO & Executable attributes to all memory occupied
> +  by the Boot Firmware Volume. This function locates the section information of
> +  the Standalone MM Core module to be able to change permissions of the
> +  individual sections later in the boot process.
> +
> +  @param  TeData                 Pointer to PE/COFF image data
> +  @param  ImageContext           Pointer to PE/COFF image context
> +  @param  SectionHeaderOffset    Offset of PE/COFF image section header
> +  @param  NumberOfSections       Number of Sections
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +GetStandaloneMmCorePeCoffSections (
> +  IN        VOID                            *TeData,
> +  IN  OUT   PE_COFF_LOADER_IMAGE_CONTEXT    *ImageContext,
> +  IN  OUT   UINT32                          *SectionHeaderOffset;
> +  IN  OUT   UINTN                           *NumberOfSections;
> +  );
> +
> +
> +/**
> +  Privileged firmware assigns RO & Executable attributes to all memory occupied
> +  by the Boot Firmware Volume. This function locates the Standalone MM Core
> +  module PE/COFF image in the BFV and returns this information.
> +
> +  @param  BfvAddress             Base Address of Boot Firmware Volume
> +  @param  TeData                 Pointer to address for allocating memory for
> +                                 PE/COFF image data
> +  @param  TeDataSize             Pointer to size of PE/COFF image data
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +LocateStandaloneMmCorePeCoffData (
> +  IN        EFI_FIRMWARE_VOLUME_HEADER      *BfvAddress,
> +  IN  OUT   VOID                            **TeData;

UINT64**?

> +  IN  OUT   UINTN                           *TeDataSize;

UINT32?

> +  );
> +
> +
> +/**
> +  Use the boot information passed by privileged firmware to populate a HOB list
> +  suitable for consumption by the MM Core and drivers.
> +
> +  @param  CpuDriverEntryPoint    Address of MM CPU driver entrypoint
> +  @param  PayloadBootInfo        Boot information passed by privileged firmware
> +
> +**/
> +VOID *
> +EFIAPI
> +CreateHobListFromBootInfo (
> +  IN  OUT  PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT *CpuDriverEntryPoint,
> +  IN       EFI_SECURE_PARTITION_BOOT_INFO     *PayloadBootInfo
> +  );
> +
> +
> +/**
> +  The entry point of Standalone MM Foundation.
> +
> +  @param  HobStart  Pointer to the beginning of the HOB List.

Comment needs to be updated.

> +
> +**/
> +VOID
> +EFIAPI
> +_ModuleEntryPoint (
> +  IN VOID    *SharedBufAddress,
> +  IN UINT64  SharedBufSize,
> +  IN UINT64  cookie1,
> +  IN UINT64  cookie2
> +  );

The ABI for the first ERET into the partition needs to be tweaked. I don't think
we need SharedBufSize as this is discoverable from Header.Size in
EFI_SECURE_PARTITION_BOOT_INFO.

> +
> +
> +/**
> +  Autogenerated function that calls the library constructors for all of the module's dependent libraries.
> +
> +  This function must be called by _ModuleEntryPoint().
> +  This function calls the set of library constructors for the set of library instances
> +  that a module depends on.  This includes library instances that a module depends on
> +  directly and library instances that a module depends on indirectly through other
> +  libraries. This function is autogenerated by build tools and those build tools are
> +  responsible for collecting the set of library instances, determine which ones have
> +  constructors, and calling the library constructors in the proper order based upon
> +  each of the library instances own dependencies.
> +
> +  @param  ImageHandle  The image handle of the DXE Core.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +**/
> +VOID
> +EFIAPI
> +ProcessLibraryConstructorList (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE  *MmSystemTable
> +  );
> +
> +
> +/**
> +  Autogenerated function that calls a set of module entry points.
> +
> +  This function must be called by _ModuleEntryPoint().
> +  This function calls the set of module entry points.
> +  This function is autogenerated by build tools and those build tools are responsible
> +  for collecting the module entry points and calling them in a specified order.
> +
> +  @param  HobStart  Pointer to the beginning of the HOB List passed in from the PEI Phase.
> +
> +**/
> +VOID
> +EFIAPI
> +ProcessModuleEntryPointList (
> +  IN VOID  *HobStart
> +  );
> +
> +#endif
> diff --git a/StandaloneMmPkg/Include/Library/MmCoreStandaloneEntryPoint.h b/StandaloneMmPkg/Include/Library/MmCoreStandaloneEntryPoint.h
> new file mode 100644
> index 0000000000..d6105d4935
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Library/MmCoreStandaloneEntryPoint.h
> @@ -0,0 +1,101 @@
> +/** @file
> +  Module entry point library for DXE core.

This comment needs to be changed.

> +
> +Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>

Copyright year here and everywhere else.

> +
> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef __MODULE_ENTRY_POINT_H__
> +#define __MODULE_ENTRY_POINT_H__
> +
> +///
> +/// Global variable that contains a pointer to the Hob List passed into the DXE Core entry point.
> +///
> +extern VOID  *gHobList;
> +
> +
> +/**
> +  The entry point of PE/COFF Image for the DXE Core.
> +
> +  This function is the entry point for the DXE Core. This function is required to call
> +  ProcessModuleEntryPointList() and ProcessModuleEntryPointList() is never expected to return.
> +  The DXE Core is responsible for calling ProcessLibraryConstructorList() as soon as the EFI
> +  System Table and the image handle for the DXE Core itself have been established.
> +  If ProcessModuleEntryPointList() returns, then ASSERT() and halt the system.

This comment needs to be changed as this is not the DXE core.

> +
> +  @param  HobStart  Pointer to the beginning of the HOB List passed in from the PEI Phase.
> +
> +**/
> +VOID
> +EFIAPI
> +_ModuleEntryPoint (
> +  IN VOID  *HobStart
> +  );
> +
> +
> +/**
> +  Required by the EBC compiler and identical in functionality to _ModuleEntryPoint().
> +
> +  This function is required to call _ModuleEntryPoint() passing in HobStart.
> +
> +  @param  HobStart  Pointer to the beginning of the HOB List passed in from the PEI Phase.
> +
> +**/
> +VOID
> +EFIAPI
> +EfiMain (
> +  IN VOID  *HobStart
> +  );
> +
> +
> +/**
> +  Autogenerated function that calls the library constructors for all of the module's dependent libraries.
> +
> +  This function must be called by _ModuleEntryPoint().
> +  This function calls the set of library constructors for the set of library instances
> +  that a module depends on.  This includes library instances that a module depends on
> +  directly and library instances that a module depends on indirectly through other
> +  libraries. This function is autogenerated by build tools and those build tools are
> +  responsible for collecting the set of library instances, determine which ones have
> +  constructors, and calling the library constructors in the proper order based upon
> +  each of the library instances own dependencies.
> +
> +  @param  ImageHandle  The image handle of the DXE Core.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +**/
> +VOID
> +EFIAPI
> +ProcessLibraryConstructorList (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  );
> +
> +
> +/**
> +  Autogenerated function that calls a set of module entry points.
> +
> +  This function must be called by _ModuleEntryPoint().
> +  This function calls the set of module entry points.
> +  This function is auto generated by build tools and those build tools are responsible
> +  for collecting the module entry points and calling them in a specified order.
> +
> +  @param  HobStart  Pointer to the beginning of the HOB List passed in from the PEI Phase.
> +
> +**/
> +VOID
> +EFIAPI
> +ProcessModuleEntryPointList (
> +  IN VOID  *HobStart
> +  );
> +
> +#endif
> diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/CreateHobList.c b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/CreateHobList.c
> new file mode 100644
> index 0000000000..f9b3faea8f
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/CreateHobList.c
> @@ -0,0 +1,200 @@
> +/** @file
> +  Entry point to the Standalone MM Foundation when initialized during the SEC
> +  phase on ARM platforms

The comment needs to be updated.

> +
> +Copyright (c) 2017, ARM Ltd. All rights reserved.<BR>
> +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.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +
> +#include <PiMm.h>
> +
> +#include <PiPei.h>
> +#include <Guid/MmramMemoryReserve.h>
> +#include <Guid/MpInformation.h>
> +
> +#include <Library/Arm/StandaloneMmCoreEntryPoint.h>
> +#include <Library/ArmMmuLib.h>
> +#include <Library/ArmSvcLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/HobLib.h>
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/SerialPortLib.h>
> +
> +#include <IndustryStandard/ArmStdSmc.h>
> +
> +extern EFI_HOB_HANDOFF_INFO_TABLE*
> +HobConstructor (
> +  IN VOID   *EfiMemoryBegin,
> +  IN UINTN  EfiMemoryLength,

UINT32 or UINT64?

> +  IN VOID   *EfiFreeMemoryBottom,
> +  IN VOID   *EfiFreeMemoryTop
> +  );
> +
> +// GUID to identify HOB with whereabouts of communication buffer with Normal
> +// World
> +extern EFI_GUID gEfiStandaloneMmNonSecureBufferGuid;
> +
> +// GUID to identify HOB where the entry point of the CPU driver will be
> +// populated to allow this entry point driver to invoke it upon receipt of an
> +// event
> +extern EFI_GUID gEfiArmTfCpuDriverEpDescriptorGuid;
> +
> +/**
> +  Use the boot information passed by privileged firmware to populate a HOB list
> +  suitable for consumption by the MM Core and drivers.
> +
> +  @param  PayloadBootInfo    Boot information passed by privileged firmware
> +
> +**/
> +VOID *
> +CreateHobListFromBootInfo (
> +  IN  OUT  PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT *CpuDriverEntryPoint,
> +  IN       EFI_SECURE_PARTITION_BOOT_INFO     *PayloadBootInfo
> +)
> +{
> +  EFI_HOB_HANDOFF_INFO_TABLE      *HobStart;
> +  EFI_RESOURCE_ATTRIBUTE_TYPE     Attributes;
> +  UINT32                          Index;
> +  UINT32                          BufferSize;
> +  UINT32                          Flags;
> +  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHob;
> +  EFI_MMRAM_DESCRIPTOR            *MmramRanges;
> +  EFI_MMRAM_DESCRIPTOR            *NsCommBufMmramRange;
> +  MP_INFORMATION_HOB_DATA         *MpInformationHobData;
> +  EFI_PROCESSOR_INFORMATION       *ProcInfoBuffer;
> +  EFI_SECURE_PARTITION_CPU_INFO   *CpuInfo;
> +  ARM_TF_CPU_DRIVER_EP_DESCRIPTOR *CpuDriverEntryPointDesc;
> +
> +  // Create a hoblist with a PHIT and EOH
> +  HobStart = HobConstructor ((VOID *) PayloadBootInfo->SpMemBase,
> +    (UINTN)  PayloadBootInfo->SpMemLimit - PayloadBootInfo->SpMemBase,
> +    (VOID *) PayloadBootInfo->SpHeapBase,
> +    (VOID *) (PayloadBootInfo->SpHeapBase + PayloadBootInfo->SpHeapSize));
> +
> +  // Check that the Hoblist starts at the bottom of the Heap
> +  ASSERT (HobStart == (VOID *) PayloadBootInfo->SpHeapBase);
> +
> +  // Build a Boot Firmware Volume HOB
> +  BuildFvHob(PayloadBootInfo->SpImageBase, PayloadBootInfo->SpImageSize);
> +
> +  // Build a resource descriptor Hob that describes the available physical
> +  // memory range
> +  Attributes =(
> +    EFI_RESOURCE_ATTRIBUTE_PRESENT |
> +    EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
> +    EFI_RESOURCE_ATTRIBUTE_TESTED |
> +    EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
> +    EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
> +    EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
> +    EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
> +  );
> +
> +  BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,
> +    Attributes,
> +    (UINTN) PayloadBootInfo->SpMemBase,
> +    PayloadBootInfo->SpMemLimit - PayloadBootInfo->SpMemBase);
> +
> +  // Find the size of the GUIDed HOB with MP information
> +  BufferSize = sizeof (MP_INFORMATION_HOB_DATA);
> +  BufferSize += sizeof (EFI_PROCESSOR_INFORMATION) * PayloadBootInfo->NumCpus;
> +
> +  // Create a Guided MP information HOB to enable the ARM TF CPU driver to
> +  // perform per-cpu allocations.
> +  MpInformationHobData = BuildGuidHob(&gMpInformationHobGuid, BufferSize);
> +
> +  // Populate the MP information HOB with the topology information passed by
> +  // privileged firmware
> +  MpInformationHobData->NumberOfProcessors = PayloadBootInfo->NumCpus;
> +  MpInformationHobData->NumberOfEnabledProcessors = PayloadBootInfo->NumCpus;
> +  ProcInfoBuffer = MpInformationHobData->ProcessorInfoBuffer;
> +  CpuInfo = PayloadBootInfo->CpuInfo;
> +
> +  for (Index = 0; Index < PayloadBootInfo->NumCpus; Index++) {
> +    ProcInfoBuffer[Index].ProcessorId      = CpuInfo[Index].Mpidr;
> +    ProcInfoBuffer[Index].Location.Package = GET_CLUSTER_ID(CpuInfo[Index].Mpidr);
> +    ProcInfoBuffer[Index].Location.Core    = GET_CORE_ID(CpuInfo[Index].Mpidr);
> +    ProcInfoBuffer[Index].Location.Thread  = GET_CORE_ID(CpuInfo[Index].Mpidr);
> +
> +    Flags = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;
> +    if (CpuInfo[Index].Flags & CPU_INFO_FLAG_PRIMARY_CPU) {
> +      Flags |= PROCESSOR_AS_BSP_BIT;
> +    }
> +    ProcInfoBuffer[Index].StatusFlag = Flags;
> +  }
> +
> +  // Create a Guided HOB to tell the ARM TF CPU driver the location and length
> +  // of the communication buffer shared with the Normal world.
> +  NsCommBufMmramRange = (EFI_MMRAM_DESCRIPTOR *) BuildGuidHob (&gEfiStandaloneMmNonSecureBufferGuid, sizeof(EFI_MMRAM_DESCRIPTOR));
> +  NsCommBufMmramRange->PhysicalStart = PayloadBootInfo->SpNsCommBufBase;
> +  NsCommBufMmramRange->CpuStart      = PayloadBootInfo->SpNsCommBufBase;
> +  NsCommBufMmramRange->PhysicalSize  = PayloadBootInfo->SpNsCommBufSize;
> +  NsCommBufMmramRange->RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
> +
> +  // Create a Guided HOB to enable the ARM TF CPU driver to share its entry
> +  // point and populate it with the address of the shared buffer
> +  CpuDriverEntryPointDesc = (ARM_TF_CPU_DRIVER_EP_DESCRIPTOR *) BuildGuidHob (&gEfiArmTfCpuDriverEpDescriptorGuid, sizeof(ARM_TF_CPU_DRIVER_EP_DESCRIPTOR));
> +
> +  *CpuDriverEntryPoint = NULL;
> +  CpuDriverEntryPointDesc->ArmTfCpuDriverEpPtr = CpuDriverEntryPoint;
> +
> +  // Find the size of the GUIDed HOB with SRAM ranges
> +  BufferSize = sizeof (EFI_MMRAM_HOB_DESCRIPTOR_BLOCK);
> +  BufferSize += PayloadBootInfo->NumSpMemRegions *
> +    sizeof(EFI_MMRAM_DESCRIPTOR);
> +
> +  // Create a GUIDed HOB with SRAM ranges
> +  MmramRangesHob = BuildGuidHob (&gEfiMmPeiMmramMemoryReserveGuid, BufferSize);
> +
> +  // Fill up the number of MMRAM memory regions
> +  MmramRangesHob->NumberOfMmReservedRegions = PayloadBootInfo->NumSpMemRegions;
> +  // Fill up the MMRAM ranges
> +  MmramRanges = &MmramRangesHob->Descriptor[0];
> +
> +  // Base and size of memory occupied by the Standalone MM image
> +  MmramRanges[0].PhysicalStart = PayloadBootInfo->SpImageBase;
> +  MmramRanges[0].CpuStart      = PayloadBootInfo->SpImageBase;
> +  MmramRanges[0].PhysicalSize  = PayloadBootInfo->SpImageSize;
> +  MmramRanges[0].RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
> +
> +  // Base and size of buffer shared with privileged Secure world software
> +  MmramRanges[1].PhysicalStart = PayloadBootInfo->SpSharedBufBase;
> +  MmramRanges[1].CpuStart      = PayloadBootInfo->SpSharedBufBase;
> +  MmramRanges[1].PhysicalSize  = PayloadBootInfo->SpPcpuSharedBufSize * PayloadBootInfo->NumCpus;
> +  MmramRanges[1].RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
> +
> +  // Base and size of buffer used for synchronous communication with Normal
> +  // world software
> +  MmramRanges[2].PhysicalStart = PayloadBootInfo->SpNsCommBufBase;
> +  MmramRanges[2].CpuStart      = PayloadBootInfo->SpNsCommBufBase;
> +  MmramRanges[2].PhysicalSize  = PayloadBootInfo->SpNsCommBufSize;
> +  MmramRanges[2].RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
> +
> +  // Base and size of memory allocated for stacks for all cpus
> +  MmramRanges[3].PhysicalStart = PayloadBootInfo->SpStackBase;
> +  MmramRanges[3].CpuStart      = PayloadBootInfo->SpStackBase;
> +  MmramRanges[3].PhysicalSize  = PayloadBootInfo->SpPcpuStackSize * PayloadBootInfo->NumCpus;
> +  MmramRanges[3].RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
> +
> +  // Base and size of heap memory shared by all cpus
> +  MmramRanges[4].PhysicalStart = (EFI_PHYSICAL_ADDRESS) HobStart;
> +  MmramRanges[4].CpuStart      = (EFI_PHYSICAL_ADDRESS) HobStart;
> +  MmramRanges[4].PhysicalSize  = HobStart->EfiFreeMemoryBottom - (EFI_PHYSICAL_ADDRESS) HobStart;
> +  MmramRanges[4].RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
> +
> +  // Base and size of heap memory shared by all cpus
> +  MmramRanges[5].PhysicalStart = HobStart->EfiFreeMemoryBottom;
> +  MmramRanges[5].CpuStart      = HobStart->EfiFreeMemoryBottom;
> +  MmramRanges[5].PhysicalSize  = HobStart->EfiFreeMemoryTop - HobStart->EfiFreeMemoryBottom;
> +  MmramRanges[5].RegionState   = EFI_CACHEABLE;
> +
> +  return HobStart;
> +}
> diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/SetPermissions.c b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/SetPermissions.c
> new file mode 100644
> index 0000000000..e96b81cdc0
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/SetPermissions.c

This file needs to be reviewed by Ard as it is heavily based upon his original
work and has not changed much since I wrote it.

> @@ -0,0 +1,278 @@
> +/** @file
> +  Entry point to the Standalone MM Foundation when initialised during the SEC
> +  phase on ARM platforms

Comment needs to be updated.

> +
> +Copyright (c) 2017, ARM Ltd. All rights reserved.<BR>
> +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.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +
> +#include <PiMm.h>
> +
> +
> +
> +#include <PiPei.h>
> +#include <Guid/MmramMemoryReserve.h>
> +#include <Guid/MpInformation.h>
> +
> +#include <Library/Arm/StandaloneMmCoreEntryPoint.h>
> +#include <Library/ArmMmuLib.h>
> +#include <Library/ArmSvcLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/HobLib.h>
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/SerialPortLib.h>
> +
> +
> +#include <IndustryStandard/ArmStdSmc.h>
> +
> +EFI_STATUS
> +EFIAPI
> +UpdateMmFoundationPeCoffPermissions (
> +  IN  CONST PE_COFF_LOADER_IMAGE_CONTEXT      *ImageContext,
> +  IN  UINT32                                  SectionHeaderOffset,
> +  IN  CONST  UINTN                            NumberOfSections,

UINT32?

> +  IN  REGION_PERMISSION_UPDATE_FUNC           TextUpdater,
> +  IN  REGION_PERMISSION_UPDATE_FUNC           ReadOnlyUpdater,
> +  IN  REGION_PERMISSION_UPDATE_FUNC           ReadWriteUpdater
> +  )
> +{
> +  EFI_IMAGE_SECTION_HEADER         SectionHeader;
> +  RETURN_STATUS                    Status;
> +  EFI_PHYSICAL_ADDRESS             Base;
> +  UINTN                            Size;
> +  UINTN                            ReadSize;
> +  UINTN                            Index;
> +
> +  ASSERT (ImageContext != NULL);
> +
> +  //
> +  // Iterate over the sections
> +  //
> +  for (Index = 0; Index < NumberOfSections; Index++) {
> +    //
> +    // Read section header from file
> +    //
> +    Size = sizeof (EFI_IMAGE_SECTION_HEADER);
> +    ReadSize = Size;
> +    Status = ImageContext->ImageRead (ImageContext->Handle, SectionHeaderOffset,
> +                                   &Size, &SectionHeader);
> +    if (RETURN_ERROR (Status) || (Size != ReadSize)) {
> +      DEBUG ((DEBUG_ERROR,
> +        "%a: ImageContext->ImageRead () failed (Status = %r)\n",
> +        __FUNCTION__, Status));
> +      return Status;
> +    }
> +
> +    DEBUG ((DEBUG_INFO,
> +            "%a: Section %d of image at 0x%lx has 0x%x permissions\n",
> +            __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.Characteristics));
> +    DEBUG ((DEBUG_INFO,
> +            "%a: Section %d of image at 0x%lx has %s name\n",
> +            __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.Name));
> +    DEBUG ((DEBUG_INFO,
> +            "%a: Section %d of image at 0x%lx has 0x%x address\n",
> +            __FUNCTION__, Index, ImageContext->ImageAddress, ImageContext->ImageAddress + SectionHeader.VirtualAddress));
> +    DEBUG ((DEBUG_INFO,
> +            "%a: Section %d of image at 0x%lx has 0x%x data\n",
> +            __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.PointerToRawData));
> +
> +    //
> +    // If the section is marked as XN then remove the X attribute. Furthermore,
> +    // if it is a writeable section then mark it appropriately as well.
> +    //
> +    if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_MEM_EXECUTE) == 0) {
> +      Base = ImageContext->ImageAddress + SectionHeader.VirtualAddress;
> +
> +      TextUpdater (Base, SectionHeader.Misc.VirtualSize);
> +
> +      if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_MEM_WRITE) != 0) {
> +        ReadWriteUpdater (Base, SectionHeader.Misc.VirtualSize);
> +        DEBUG ((DEBUG_INFO,
> +                "%a: Mapping section %d of image at 0x%lx with RW-XN permissions\n",
> +                __FUNCTION__, Index, ImageContext->ImageAddress));
> +      } else {
> +        DEBUG ((DEBUG_INFO,
> +                "%a: Mapping section %d of image at 0x%lx with RO-XN permissions\n",
> +                __FUNCTION__, Index, ImageContext->ImageAddress));
> +      }
> +    } else {
> +        DEBUG ((DEBUG_INFO,
> +                "%a: Ignoring section %d of image at 0x%lx with 0x%x permissions\n",
> +                __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.Characteristics));
> +    }
> +    SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
> +  }
> +
> +  return RETURN_SUCCESS;
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +LocateStandaloneMmCorePeCoffData (
> +  IN        EFI_FIRMWARE_VOLUME_HEADER      *BfvAddress,
> +  IN  OUT   VOID                            **TeData,
> +  IN  OUT   UINTN                           *TeDataSize
> +  )
> +{
> +  EFI_FFS_FILE_HEADER             *FileHeader = NULL;
> +  EFI_STATUS                      Status;
> +
> +  Status = FfsFindNextFile (
> +                  EFI_FV_FILETYPE_SECURITY_CORE,
> +                  BfvAddress,
> +                  &FileHeader);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_ERROR, "Unable to locate Standalone MM FFS file - 0x%x\n",
> +      Status));
> +    return Status;
> +  }
> +
> +  Status = FfsFindSectionData (EFI_SECTION_PE32, FileHeader, TeData, TeDataSize);
> +  if (EFI_ERROR (Status)) {
> +      DEBUG ((DEBUG_ERROR, "Unable to locate Standalone MM Section data - 0x%x\n",
> +              Status));
> +    return Status;
> +  }
> +
> +  DEBUG ((DEBUG_INFO, "Found Standalone MM PE data - 0x%x\n", *TeData));
> +  return Status;
> +}
> +
> +static
> +EFI_STATUS
> +GetPeCoffSectionInformation (
> +  IN  CONST PE_COFF_LOADER_IMAGE_CONTEXT      *ImageContext,
> +  IN  OUT   PE_COFF_LOADER_IMAGE_CONTEXT      *TmpContext,
> +  IN  OUT   UINT32                            *SectionHeaderOffset,
> +  IN  OUT   UINTN                             *NumberOfSections
> +  )
> +{
> +  RETURN_STATUS                         Status;
> +  EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION   Hdr;
> +  EFI_IMAGE_OPTIONAL_HEADER_UNION       HdrData;
> +  UINTN                                 Size;
> +  UINTN                                 ReadSize;
> +
> +  ASSERT (ImageContext != NULL);
> +  ASSERT (TmpContext != NULL);
> +  ASSERT (SectionHeaderOffset != NULL);
> +  ASSERT (NumberOfSections != NULL);
> +
> +  //
> +  // We need to copy ImageContext since PeCoffLoaderGetImageInfo ()
> +  // will mangle the ImageAddress field
> +  //
> +  CopyMem (TmpContext, ImageContext, sizeof (*TmpContext));
> +
> +  if (TmpContext->PeCoffHeaderOffset == 0) {
> +    Status = PeCoffLoaderGetImageInfo (TmpContext);
> +    if (RETURN_ERROR (Status)) {
> +      DEBUG ((DEBUG_ERROR,
> +        "%a: PeCoffLoaderGetImageInfo () failed (Status = %r)\n",
> +        __FUNCTION__, Status));
> +      return Status;
> +    }
> +  }
> +
> +  if (TmpContext->IsTeImage &&
> +      TmpContext->ImageAddress == ImageContext->ImageAddress) {
> +    DEBUG ((DEBUG_INFO, "%a: ignoring XIP TE image at 0x%lx\n", __FUNCTION__,
> +      ImageContext->ImageAddress));
> +    return RETURN_UNSUPPORTED;
> +  }
> +
> +  if (TmpContext->SectionAlignment < EFI_PAGE_SIZE) {
> +    //
> +    // The sections need to be at least 4 KB aligned, since that is the
> +    // granularity at which we can tighten permissions.
> +    //
> +    if (!TmpContext->IsTeImage) {
> +      DEBUG ((DEBUG_WARN,
> +        "%a: non-TE Image at 0x%lx has SectionAlignment < 4 KB (%lu)\n",
> +        __FUNCTION__, ImageContext->ImageAddress, TmpContext->SectionAlignment));
> +    }
> +    return RETURN_UNSUPPORTED;
> +  }
> +
> +  //
> +  // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
> +  // data, but that should not hurt anything. Hdr.Pe32->OptionalHeader.Magic
> +  // determines if this is a PE32 or PE32+ image. The magic is in the same
> +  // location in both images.
> +  //
> +  Hdr.Union = &HdrData;
> +  Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);
> +  ReadSize = Size;
> +  Status = TmpContext->ImageRead (TmpContext->Handle,
> +                         TmpContext->PeCoffHeaderOffset, &Size, Hdr.Pe32);
> +  if (RETURN_ERROR (Status) || (Size != ReadSize)) {
> +    DEBUG ((DEBUG_ERROR,
> +      "%a: TmpContext->ImageRead () failed (Status = %r)\n",
> +      __FUNCTION__, Status));
> +    return Status;
> +  }
> +
> +  ASSERT (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE);
> +
> +  *SectionHeaderOffset = TmpContext->PeCoffHeaderOffset + sizeof (UINT32) +
> +                        sizeof (EFI_IMAGE_FILE_HEADER);
> +  *NumberOfSections    = (UINTN)(Hdr.Pe32->FileHeader.NumberOfSections);
> +
> +  switch (Hdr.Pe32->OptionalHeader.Magic) {
> +    case EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC:
> +      *SectionHeaderOffset += Hdr.Pe32->FileHeader.SizeOfOptionalHeader;
> +      break;
> +    case EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC:
> +      *SectionHeaderOffset += Hdr.Pe32Plus->FileHeader.SizeOfOptionalHeader;
> +      break;
> +    default:
> +      ASSERT (FALSE);
> +  }
> +
> +  return RETURN_SUCCESS;
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +GetStandaloneMmCorePeCoffSections (
> +  IN        VOID                            *TeData,
> +  IN  OUT   PE_COFF_LOADER_IMAGE_CONTEXT    *ImageContext,
> +  IN  OUT   UINT32                          *SectionHeaderOffset,
> +  IN  OUT   UINTN                           *NumberOfSections
> +  )
> +{
> +  EFI_STATUS                   Status;
> +  PE_COFF_LOADER_IMAGE_CONTEXT TmpContext;
> +
> +  // Initialize the Image Context
> +  ZeroMem (ImageContext, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT));
> +  ImageContext->Handle    = TeData;
> +  ImageContext->ImageRead = PeCoffLoaderImageReadFromMemory;
> +
> +  DEBUG((DEBUG_INFO, "Found Standalone MM PE data - 0x%x\n", TeData));
> +
> +  Status = PeCoffLoaderGetImageInfo (ImageContext);
> +  if (EFI_ERROR(Status)) {
> +    DEBUG((DEBUG_ERROR, "Unable to locate Standalone MM Core PE-COFF Image information - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  Status = GetPeCoffSectionInformation(ImageContext, &TmpContext, SectionHeaderOffset, NumberOfSections);
> +  if (EFI_ERROR(Status)) {
> +    DEBUG((DEBUG_ERROR, "Unable to locate Standalone MM Core PE-COFF Section information - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  DEBUG((DEBUG_INFO, "Standalone MM Core PE-COFF SectionHeaderOffset - 0x%x, NumberOfSections - %d\n", *SectionHeaderOffset, *NumberOfSections));
> +
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c
> new file mode 100644
> index 0000000000..72e3b834d4
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c
> @@ -0,0 +1,264 @@
> +/** @file
> +  Entry point to the Standalone MM Foundation when initialized during the SEC
> +  phase on ARM platforms
> +
> +Copyright (c) 2017, ARM Ltd. All rights reserved.<BR>
> +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.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +
> +#include <PiMm.h>
> +
> +#include <Library/Arm/StandaloneMmCoreEntryPoint.h>
> +
> +#include <PiPei.h>
> +#include <Guid/MmramMemoryReserve.h>
> +#include <Guid/MpInformation.h>
> +
> +#include <Library/ArmMmuLib.h>
> +#include <Library/ArmSvcLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/HobLib.h>
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/SerialPortLib.h>
> +
> +#include <IndustryStandard/ArmStdSmc.h>
> +#include <IndustryStandard/ArmMmSvc.h>
> +
> +#define SPM_MAJOR_VER_MASK        0xFFFF0000
> +#define SPM_MINOR_VER_MASK        0x0000FFFF
> +#define SPM_MAJOR_VER_SHIFT       16
> +
> +const UINT32 SPM_MAJOR_VER = 0;
> +const UINT32 SPM_MINOR_VER = 1;
> +
> +const UINT8 BOOT_PAYLOAD_VERSION = 1;
> +
> +PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT      CpuDriverEntryPoint = NULL;
> +
> +/**
> +  Retrieve a pointer to and print the boot information passed by privileged
> +  secure firmware
> +
> +  @param  SharedBufAddress The pointer memory shared with privileged firmware
> +
> +**/
> +EFI_SECURE_PARTITION_BOOT_INFO *
> +GetAndPrintBootinformation (
> +  IN VOID                      *SharedBufAddress
> +)
> +{
> +  EFI_SECURE_PARTITION_BOOT_INFO *PayloadBootInfo;
> +  EFI_SECURE_PARTITION_CPU_INFO  *PayloadCpuInfo;
> +  UINTN                          Index;
> +
> +  PayloadBootInfo = (EFI_SECURE_PARTITION_BOOT_INFO *) SharedBufAddress;
> +
> +  if (PayloadBootInfo->Header.Version != BOOT_PAYLOAD_VERSION)
> +  {
> +    DEBUG((DEBUG_ERROR, "Boot Information Version Mismatch. Current=0x%x, Expected=0x%x.\n", PayloadBootInfo->Header.Version, BOOT_PAYLOAD_VERSION));
> +    return NULL;
> +  }
> +  DEBUG((DEBUG_INFO, "NumSpMemRegions - 0x%x\n", PayloadBootInfo->NumSpMemRegions));
> +  DEBUG((DEBUG_INFO, "SpMemBase       - 0x%lx\n", PayloadBootInfo->SpMemBase));
> +  DEBUG((DEBUG_INFO, "SpMemLimit      - 0x%lx\n", PayloadBootInfo->SpMemLimit));
> +  DEBUG((DEBUG_INFO, "SpImageBase     - 0x%lx\n", PayloadBootInfo->SpImageBase));
> +  DEBUG((DEBUG_INFO, "SpStackBase     - 0x%lx\n", PayloadBootInfo->SpStackBase));
> +  DEBUG((DEBUG_INFO, "SpHeapBase      - 0x%lx\n", PayloadBootInfo->SpHeapBase));
> +  DEBUG((DEBUG_INFO, "SpNsCommBufBase - 0x%lx\n", PayloadBootInfo->SpNsCommBufBase));
> +  DEBUG((DEBUG_INFO, "SpSharedBufBase - 0x%lx\n", PayloadBootInfo->SpSharedBufBase));
> +
> +  DEBUG((DEBUG_INFO, "SpImageSize     - 0x%x\n", PayloadBootInfo->SpImageSize));
> +  DEBUG((DEBUG_INFO, "SpPcpuStackSize - 0x%x\n", PayloadBootInfo->SpPcpuStackSize));
> +  DEBUG((DEBUG_INFO, "SpHeapSize      - 0x%x\n", PayloadBootInfo->SpHeapSize));
> +  DEBUG((DEBUG_INFO, "SpNsCommBufSize - 0x%x\n", PayloadBootInfo->SpNsCommBufSize));
> +  DEBUG((DEBUG_INFO, "SpPcpuSharedBufSize - 0x%x\n", PayloadBootInfo->SpPcpuSharedBufSize));
> +
> +  DEBUG((DEBUG_INFO, "NumCpus         - 0x%x\n", PayloadBootInfo->NumCpus));
> +  DEBUG((DEBUG_INFO, "CpuInfo         - 0x%p\n", PayloadBootInfo->CpuInfo));
> +
> +  PayloadCpuInfo = (EFI_SECURE_PARTITION_CPU_INFO *) PayloadBootInfo->CpuInfo;
> +
> +  for (Index = 0; Index < PayloadBootInfo->NumCpus; Index++) {
> +    DEBUG((DEBUG_INFO, "Mpidr           - 0x%lx\n", PayloadCpuInfo[Index].Mpidr));
> +    DEBUG((DEBUG_INFO, "LinearId        - 0x%x\n", PayloadCpuInfo[Index].LinearId));
> +    DEBUG((DEBUG_INFO, "Flags           - 0x%x\n", PayloadCpuInfo[Index].Flags));
> +  }
> +
> +  return PayloadBootInfo;
> +}
> +
> +VOID
> +EFIAPI
> +DelegatedEventLoop (
> +  IN ARM_SVC_ARGS *EventCompleteSvcArgs
> +  )
> +{
> +  EFI_STATUS Status;
> +
> +  while(TRUE) {
> +    ArmCallSvc(EventCompleteSvcArgs);
> +
> +    DEBUG ((DEBUG_INFO, "Received delegated event\n"));
> +    DEBUG ((DEBUG_INFO, "X0 :  0x%x\n", (UINT32) EventCompleteSvcArgs->Arg0));
> +    DEBUG ((DEBUG_INFO, "X1 :  0x%x\n", (UINT32) EventCompleteSvcArgs->Arg1));
> +    DEBUG ((DEBUG_INFO, "X2 :  0x%x\n", (UINT32) EventCompleteSvcArgs->Arg2));
> +    DEBUG ((DEBUG_INFO, "X3 :  0x%x\n", (UINT32) EventCompleteSvcArgs->Arg3));
> +
> +    Status = CpuDriverEntryPoint(
> +      EventCompleteSvcArgs->Arg0,
> +      EventCompleteSvcArgs->Arg2,
> +      EventCompleteSvcArgs->Arg1);
> +    if (EFI_ERROR(Status)) {
> +      DEBUG((DEBUG_ERROR, "Failed delegated event 0x%x, Status 0x%x\n",
> +        EventCompleteSvcArgs->Arg0, Status));
> +    }
> +
> +    EventCompleteSvcArgs->Arg0 = ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64;
> +    EventCompleteSvcArgs->Arg1 = Status;
> +  }
> +}
> +
> +STATIC
> +EFI_STATUS
> +GetSpmVersion ()
> +{
> +  EFI_STATUS   Status;
> +  UINT16       SpmMajorVersion;
> +  UINT16       SpmMinorVersion;
> +  UINT32       SpmVersion;
> +  ARM_SVC_ARGS SpmVersionArgs;
> +
> +  SpmVersionArgs.Arg0 = ARM_SVC_ID_SPM_VERSION_AARCH32;
> +
> +  ArmCallSvc (&SpmVersionArgs);
> +
> +  SpmVersion = SpmVersionArgs.Arg0;
> +
> +  SpmMajorVersion = ((SpmVersion & SPM_MAJOR_VER_MASK) >> SPM_MAJOR_VER_SHIFT);
> +  SpmMinorVersion = ((SpmVersion & SPM_MINOR_VER_MASK) >> 0);
> +
> +  // Different major revision values indicate possibly incompatible functions.
> +  // For two revisions, A and B, for which the major revision values are
> +  // identical, if the minor revision value of revision B is greater than
> +  // the minor revision value of revision A, then every function in
> +  // revision A must work in a compatible way with revision B.
> +  // However, it is possible for revision B to have a higher
> +  // function count than revision A.
> +  if ((SpmMajorVersion == SPM_MAJOR_VER) &&
> +      (SpmMinorVersion >= SPM_MINOR_VER))
> +  {
> +    DEBUG ((DEBUG_INFO, "SPM Version: Major=0x%x, Minor=0x%x\n",
> +           SpmMajorVersion, SpmMinorVersion));
> +    Status = EFI_SUCCESS;
> +  }
> +  else
> +  {
> +    DEBUG ((DEBUG_INFO, "Incompatible SPM Versions.\n Current Version: Major=0x%x, Minor=0x%x.\n Expected: Major=0x%x, Minor>=0x%x.\n",
> +            SpmMajorVersion, SpmMinorVersion, SPM_MAJOR_VER, SPM_MINOR_VER));
> +    Status = EFI_UNSUPPORTED;
> +  }
> +
> +  return Status;
> +}
> +
> +/**
> +  The entry point of Standalone MM Foundation.
> +
> +  @param  HobStart  The pointer to the beginning of the HOB List.
> +
> +**/
> +VOID
> +EFIAPI
> +_ModuleEntryPoint (
> +  IN VOID    *SharedBufAddress,
> +  IN UINT64  SharedBufSize,
> +  IN UINT64  cookie1,
> +  IN UINT64  cookie2

As described earlier, SharedBufSize is not required. The format of the header in
SharedBufAddress should provide enough information to discover the size.

> +  )
> +{
> +  PE_COFF_LOADER_IMAGE_CONTEXT            ImageContext;
> +  EFI_SECURE_PARTITION_BOOT_INFO          *PayloadBootInfo;
> +  ARM_SVC_ARGS                            InitMmFoundationSvcArgs = {0};
> +  EFI_STATUS                              Status;
> +  UINT32                                  SectionHeaderOffset;
> +  UINTN                                   NumberOfSections;
> +  VOID                                    *HobStart;
> +  VOID                                    *TeData;
> +  UINTN                                   TeDataSize;
> +
> +  Status = SerialPortInitialize ();

It is worth commenting here that this function will succeed as the Arm TF
platform port has granted access to the UART. It is also worth checking in
edk2-platforms that the UART allocated is different from the one used by Arm TF.

cheers,
Achin

> +  ASSERT_EFI_ERROR (Status);
> +
> +  // Get Secure Partition Manager Version Information
> +  Status = GetSpmVersion();
> +  if (EFI_ERROR(Status)) {
> +    goto finish;
> +  }
> +
> +  PayloadBootInfo = GetAndPrintBootinformation(SharedBufAddress);
> +  if (PayloadBootInfo == NULL) {
> +    Status = EFI_UNSUPPORTED;
> +    goto finish;
> +  }
> +
> +  // Locate PE/COFF File information for the Standalone MM core module
> +  Status = LocateStandaloneMmCorePeCoffData (
> +                 (EFI_FIRMWARE_VOLUME_HEADER *) PayloadBootInfo->SpImageBase,
> +                 &TeData,
> +                 &TeDataSize);
> +  if (EFI_ERROR(Status)) {
> +    goto finish;
> +  }
> +
> +  // Obtain the PE/COFF Section information for the Standalone MM core module
> +  Status = GetStandaloneMmCorePeCoffSections (
> +            TeData,
> +            &ImageContext,
> +            &SectionHeaderOffset,
> +            &NumberOfSections);
> +  if (EFI_ERROR(Status)) {
> +    goto finish;
> +  }
> +
> +  // Update the memory access permissions of individual sections in the
> +  // Standalone MM core module
> +  Status = UpdateMmFoundationPeCoffPermissions(
> +            &ImageContext,
> +            SectionHeaderOffset,
> +            NumberOfSections,
> +            ArmSetMemoryRegionNoExec,
> +            ArmSetMemoryRegionReadOnly,
> +            ArmClearMemoryRegionReadOnly);
> +  if (EFI_ERROR(Status)) {
> +    goto finish;
> +  }
> +
> +  //
> +  // Create Hoblist based upon boot information passed by privileged software
> +  //
> +  HobStart = CreateHobListFromBootInfo(&CpuDriverEntryPoint, PayloadBootInfo);
> +
> +  //
> +  // Call the MM Core entry point
> +  //
> +  ProcessModuleEntryPointList (HobStart);
> +
> +  ASSERT_EFI_ERROR (CpuDriverEntryPoint);
> +  DEBUG ((DEBUG_INFO, "Shared Cpu Driver EP 0x%lx\n", (UINT64) CpuDriverEntryPoint));
> +
> +finish:
> +  InitMmFoundationSvcArgs.Arg0 = ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64;
> +  InitMmFoundationSvcArgs.Arg1 = Status;
> +  DelegatedEventLoop(&InitMmFoundationSvcArgs);
> +  ASSERT_EFI_ERROR(0);
> +
> +}
> diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
> new file mode 100644
> index 0000000000..9edc85d406
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
> @@ -0,0 +1,53 @@
> +## @file
> +# Module entry point library for DXE core.
> +#
> +# Copyright (c) 2017, ARM Ltd. All rights reserved.<BR>
> +#
> +#  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.
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = StandaloneMmCoreEntryPoint
> +  FILE_GUID                      = C97AC593-109A-4C63-905C-675FDE2689E8
> +  MODULE_TYPE                    = MM_CORE_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  LIBRARY_CLASS                  = StandaloneMmCoreEntryPoint|MM_CORE_STANDALONE
> +
> +#
> +#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC (EBC is for build only)
> +#
> +
> +[Sources.AARCH64]
> +  Arm/StandaloneMmCoreEntryPoint.c
> +  Arm/SetPermissions.c
> +  Arm/CreateHobList.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +  MdeModulePkg/MdeModulePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[Packages.AARCH64]
> +  ArmPkg/ArmPkg.dec
> +  ArmPlatformPkg/ArmPlatformPkg.dec
> +
> +[LibraryClasses]
> +  ArmMmuLib
> +  ArmSvcLib
> +  BaseLib
> +  DebugLib
> +
> +[Guids]
> +  gMpInformationHobGuid
> +  gEfiMmPeiMmramMemoryReserveGuid
> +  gEfiStandaloneMmNonSecureBufferGuid
> +  gEfiArmTfCpuDriverEpDescriptorGuid
> diff --git a/StandaloneMmPkg b/StandaloneMmPkg~HEAD
> similarity index 100%
> rename from StandaloneMmPkg
> rename to StandaloneMmPkg~HEAD
> -- 
> 2.16.2
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 07/18] StandaloneMmPkg/FvLib: Add a common FV Library for management mode.
  2018-04-06 14:42 ` [PATCH v1 07/18] StandaloneMmPkg/FvLib: Add a common FV Library for management mode Supreeth Venkatesh
@ 2018-04-16 14:44   ` Achin Gupta
  2018-05-04 23:21     ` Supreeth Venkatesh
  0 siblings, 1 reply; 70+ messages in thread
From: Achin Gupta @ 2018-04-16 14:44 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

Hi Supreeth,

On Fri, Apr 06, 2018 at 03:42:12PM +0100, Supreeth Venkatesh wrote:
> This patch implements a firmware volume library that can be used by the
> Standalone management mode core module to parse the firmware volume.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>

I have not contributed to this patch at all. Could you please remove me? 

Not being an expert, I will wait for Jiewen's feedback cycle to complete.

cheers,
Achin

> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  StandaloneMmPkg/Include/Library/FvLib.h | 109 ++++++++++
>  StandaloneMmPkg/Library/FvLib/FvLib.c   | 366 ++++++++++++++++++++++++++++++++
>  StandaloneMmPkg/Library/FvLib/FvLib.inf |  57 +++++
>  3 files changed, 532 insertions(+)
>  create mode 100644 StandaloneMmPkg/Include/Library/FvLib.h
>  create mode 100644 StandaloneMmPkg/Library/FvLib/FvLib.c
>  create mode 100644 StandaloneMmPkg/Library/FvLib/FvLib.inf
> 
> diff --git a/StandaloneMmPkg/Include/Library/FvLib.h b/StandaloneMmPkg/Include/Library/FvLib.h
> new file mode 100644
> index 0000000000..13e1ae2b01
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Library/FvLib.h
> @@ -0,0 +1,109 @@
> +/** @file
> +
> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _FV_LIB_H_
> +#define _FV_LIB_H_
> +
> +#include <Uefi.h>
> +#include <Pi/PiFirmwareVolume.h>
> +#include <Pi/PiFirmwareFile.h>
> +
> +/**
> +  Given the input file pointer, search for the next matching file in the
> +  FFS volume as defined by SearchType. The search starts from FileHeader inside
> +  the Firmware Volume defined by FwVolHeader.
> +
> +  @param  SearchType  Filter to find only files of this type.
> +                      Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
> +  @param  FwVolHeader Pointer to the FV header of the volume to search.
> +                      This parameter must point to a valid FFS volume.
> +  @param  FileHeader  Pointer to the current file from which to begin searching.
> +                      This pointer will be updated upon return to reflect the file found.
> +
> +  @retval EFI_NOT_FOUND  No files matching the search criteria were found
> +  @retval EFI_SUCCESS
> +**/
> +EFI_STATUS
> +EFIAPI
> +FfsFindNextFile (
> +  IN EFI_FV_FILETYPE             SearchType,
> +  IN EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader,
> +  IN OUT EFI_FFS_FILE_HEADER     **FileHeader
> +  );
> +
> +/**
> +  Given the input file pointer, search for the next matching section in the
> +  FFS volume.
> +
> +  @param  SearchType    Filter to find only sections of this type.
> +  @param  FfsFileHeader Pointer to the current file to search.
> +  @param  SectionHeader Pointer to the Section matching SectionType in FfsFileHeader.
> +                        NULL if section not found
> +
> +  @retval  EFI_NOT_FOUND  No files matching the search criteria were found
> +  @retval  EFI_SUCCESS
> +**/
> +EFI_STATUS
> +FfsFindSection (
> +  IN EFI_SECTION_TYPE              SectionType,
> +  IN EFI_FFS_FILE_HEADER           *FfsFileHeader,
> +  IN OUT EFI_COMMON_SECTION_HEADER **SectionHeader
> +  );
> +
> +/**
> +  Locates a section within a series of sections
> +  with the specified section type.
> +
> +  @param[in]   Sections        The sections to search
> +  @param[in]   SizeOfSections  Total size of all sections
> +  @param[in]   SectionType     The section type to locate
> +  @param[out]  FoundSection    The FFS section if found
> +
> +  @retval EFI_SUCCESS           The file and section was found
> +  @retval EFI_NOT_FOUND         The file and section was not found
> +  @retval EFI_VOLUME_CORRUPTED  The firmware volume was corrupted
> +**/
> +EFI_STATUS
> +EFIAPI
> +FindFfsSectionInSections (
> +  IN  VOID                             *Sections,
> +  IN  UINTN                            SizeOfSections,
> +  IN  EFI_SECTION_TYPE                 SectionType,
> +  OUT EFI_COMMON_SECTION_HEADER        **FoundSection
> +  );
> +
> +/**
> +  Given the input file pointer, search for the next matching section in the
> +  FFS volume.
> +
> +  @param  SearchType      Filter to find only sections of this type.
> +  @param  FfsFileHeader   Pointer to the current file to search.
> +  @param  SectionData     Pointer to the Section matching SectionType in FfsFileHeader.
> +                          NULL if section not found
> +  @param  SectionDataSize The size of SectionData
> +
> +  @retval  EFI_NOT_FOUND  No files matching the search criteria were found
> +  @retval  EFI_SUCCESS
> +**/
> +EFI_STATUS
> +EFIAPI
> +FfsFindSectionData (
> +  IN EFI_SECTION_TYPE              SectionType,
> +  IN EFI_FFS_FILE_HEADER           *FfsFileHeader,
> +  OUT VOID                         **SectionData,
> +  OUT UINTN                        *SectionDataSize
> +  );
> +
> +#endif
> diff --git a/StandaloneMmPkg/Library/FvLib/FvLib.c b/StandaloneMmPkg/Library/FvLib/FvLib.c
> new file mode 100644
> index 0000000000..cacfc81338
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/FvLib/FvLib.c
> @@ -0,0 +1,366 @@
> +/** @file
> +
> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Library/FvLib.h>
> +
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +
> +#define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
> +  (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))
> +
> +/**
> +  Returns the highest bit set of the State field
> +
> +  @param ErasePolarity   Erase Polarity  as defined by EFI_FVB_ERASE_POLARITY
> +                         in the Attributes field.
> +  @param FfsHeader       Pointer to FFS File Header.
> +
> +  @return the highest bit in the State field
> +**/
> +EFI_FFS_FILE_STATE
> +GetFileState (
> +  IN UINT8                ErasePolarity,
> +  IN EFI_FFS_FILE_HEADER  *FfsHeader
> +  )
> +{
> +  EFI_FFS_FILE_STATE  FileState;
> +  EFI_FFS_FILE_STATE  HighestBit;
> +
> +  FileState = FfsHeader->State;
> +
> +  if (ErasePolarity != 0) {
> +    FileState = (EFI_FFS_FILE_STATE)~FileState;
> +  }
> +
> +  HighestBit = 0x80;
> +  while (HighestBit != 0 && (HighestBit & FileState) == 0) {
> +    HighestBit >>= 1;
> +  }
> +
> +  return HighestBit;
> +}
> +
> +/**
> +  Calculates the checksum of the header of a file.
> +
> +  @param FileHeader       Pointer to FFS File Header.
> +
> +  @return Checksum of the header.
> +**/
> +UINT8
> +CalculateHeaderChecksum (
> +  IN EFI_FFS_FILE_HEADER  *FileHeader
> +  )
> +{
> +  UINT8 *ptr;
> +  UINTN Index;
> +  UINT8 Sum;
> +
> +  Sum = 0;
> +  ptr = (UINT8 *) FileHeader;
> +
> +  for (Index = 0; Index < sizeof (EFI_FFS_FILE_HEADER) - 3; Index += 4) {
> +    Sum = (UINT8) (Sum + ptr[Index]);
> +    Sum = (UINT8) (Sum + ptr[Index + 1]);
> +    Sum = (UINT8) (Sum + ptr[Index + 2]);
> +    Sum = (UINT8) (Sum + ptr[Index + 3]);
> +  }
> +
> +  for (; Index < sizeof (EFI_FFS_FILE_HEADER); Index++) {
> +    Sum = (UINT8) (Sum + ptr[Index]);
> +  }
> +  //
> +  // State field (since this indicates the different state of file).
> +  //
> +  Sum = (UINT8) (Sum - FileHeader->State);
> +  //
> +  // Checksum field of the file is not part of the header checksum.
> +  //
> +  Sum = (UINT8) (Sum - FileHeader->IntegrityCheck.Checksum.File);
> +
> +  return Sum;
> +}
> +
> +/**
> +  Given the input file pointer, search for the next matching file in the
> +  FFS volume as defined by SearchType. The search starts from FileHeader inside
> +  the Firmware Volume defined by FwVolHeader.
> +
> +  @param  SearchType  Filter to find only files of this type.
> +                      Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
> +  @param  FwVolHeader Pointer to the FV header of the volume to search.
> +                      This parameter must point to a valid FFS volume.
> +  @param  FileHeader  Pointer to the current file from which to begin searching.
> +                      This pointer will be updated upon return to reflect the file found.
> +
> +  @retval EFI_NOT_FOUND  No files matching the search criteria were found
> +  @retval EFI_SUCCESS
> +**/
> +EFI_STATUS
> +EFIAPI
> +FfsFindNextFile (
> +  IN EFI_FV_FILETYPE             SearchType,
> +  IN EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader,
> +  IN OUT EFI_FFS_FILE_HEADER     **FileHeader
> +  )
> +{
> +  EFI_FFS_FILE_HEADER *FfsFileHeader;
> +  UINT32              FileLength;
> +  UINT32              FileOccupiedSize;
> +  UINT32              FileOffset;
> +  UINT64              FvLength;
> +  UINT8               ErasePolarity;
> +  UINT8               FileState;
> +
> +  FvLength = FwVolHeader->FvLength;
> +  if (FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {
> +    ErasePolarity = 1;
> +  } else {
> +    ErasePolarity = 0;
> +  }
> +  //
> +  // If FileHeader is not specified (NULL) start with the first file in the
> +  // firmware volume.  Otherwise, start from the FileHeader.
> +  //
> +  if (*FileHeader == NULL) {
> +    FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FwVolHeader + FwVolHeader->HeaderLength);
> +  } else {
> +    //
> +    // Length is 24 bits wide so mask upper 8 bits
> +    // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
> +    //
> +    FileLength        = FFS_FILE_SIZE(*FileHeader);
> +    FileOccupiedSize  = GET_OCCUPIED_SIZE (FileLength, 8);
> +    FfsFileHeader     = (EFI_FFS_FILE_HEADER *) ((UINT8 *) *FileHeader + FileOccupiedSize);
> +  }
> +
> +  FileOffset = (UINT32) ((UINT8 *) FfsFileHeader - (UINT8 *) FwVolHeader);
> +
> +  while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {
> +    //
> +    // Get FileState which is the highest bit of the State
> +    //
> +    FileState = GetFileState (ErasePolarity, FfsFileHeader);
> +
> +    switch (FileState) {
> +
> +    case EFI_FILE_HEADER_INVALID:
> +      FileOffset += sizeof (EFI_FFS_FILE_HEADER);
> +      FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
> +      break;
> +
> +    case EFI_FILE_DATA_VALID:
> +    case EFI_FILE_MARKED_FOR_UPDATE:
> +      if (CalculateHeaderChecksum (FfsFileHeader) == 0) {
> +        FileLength        = FFS_FILE_SIZE(FfsFileHeader);
> +        FileOccupiedSize  = GET_OCCUPIED_SIZE (FileLength, 8);
> +
> +        if ((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) {
> +
> +          *FileHeader = FfsFileHeader;
> +
> +          return EFI_SUCCESS;
> +        }
> +
> +        FileOffset += FileOccupiedSize;
> +        FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + FileOccupiedSize);
> +      } else {
> +        return EFI_NOT_FOUND;
> +      }
> +      break;
> +
> +    case EFI_FILE_DELETED:
> +      FileLength        = FFS_FILE_SIZE(FfsFileHeader);
> +      FileOccupiedSize  = GET_OCCUPIED_SIZE (FileLength, 8);
> +      FileOffset += FileOccupiedSize;
> +      FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + FileOccupiedSize);
> +      break;
> +
> +    default:
> +      return EFI_NOT_FOUND;
> +
> +    }
> +  }
> +
> +  return EFI_NOT_FOUND;
> +}
> +
> +/**
> +  Locates a section within a series of sections
> +  with the specified section type.
> +
> +  @param[in]   Sections        The sections to search
> +  @param[in]   SizeOfSections  Total size of all sections
> +  @param[in]   SectionType     The section type to locate
> +  @param[out]  FoundSection    The FFS section if found
> +
> +  @retval EFI_SUCCESS           The file and section was found
> +  @retval EFI_NOT_FOUND         The file and section was not found
> +  @retval EFI_VOLUME_CORRUPTED  The firmware volume was corrupted
> +**/
> +EFI_STATUS
> +EFIAPI
> +FindFfsSectionInSections (
> +  IN  VOID                             *Sections,
> +  IN  UINTN                            SizeOfSections,
> +  IN  EFI_SECTION_TYPE                 SectionType,
> +  OUT EFI_COMMON_SECTION_HEADER        **FoundSection
> +  )
> +{
> +  EFI_PHYSICAL_ADDRESS        CurrentAddress;
> +  UINT32                      Size;
> +  EFI_PHYSICAL_ADDRESS        EndOfSections;
> +  EFI_COMMON_SECTION_HEADER   *Section;
> +  EFI_PHYSICAL_ADDRESS        EndOfSection;
> +
> +  //
> +  // Loop through the FFS file sections
> +  //
> +  EndOfSection = (EFI_PHYSICAL_ADDRESS)(UINTN) Sections;
> +  EndOfSections = EndOfSection + SizeOfSections;
> +  for (;;) {
> +    if (EndOfSection == EndOfSections) {
> +      break;
> +    }
> +    CurrentAddress = EndOfSection;
> +
> +    Section = (EFI_COMMON_SECTION_HEADER*)(UINTN) CurrentAddress;
> +//    printf ("Section->Type: 0x%08x\n", Section->Type);
> +
> +    Size = SECTION_SIZE (Section);
> +    if (Size < sizeof (*Section)) {
> +      return EFI_VOLUME_CORRUPTED;
> +    }
> +
> +    EndOfSection = CurrentAddress + Size;
> +    if (EndOfSection > EndOfSections) {
> +      return EFI_VOLUME_CORRUPTED;
> +    }
> +    Size = GET_OCCUPIED_SIZE (Size, 4);
> +
> +    //
> +    // Look for the requested section type
> +    //
> +    if (Section->Type == SectionType) {
> +      *FoundSection = Section;
> +      return EFI_SUCCESS;
> +    }
> +  }
> +
> +  return EFI_NOT_FOUND;
> +}
> +
> +/**
> +  Given the input file pointer, search for the next matching section in the
> +  FFS volume.
> +
> +  @param  SearchType    Filter to find only sections of this type.
> +  @param  FfsFileHeader Pointer to the current file to search.
> +  @param  SectionHeader Pointer to the Section matching SectionType in FfsFileHeader.
> +                        NULL if section not found
> +
> +  @retval  EFI_NOT_FOUND  No files matching the search criteria were found
> +  @retval  EFI_SUCCESS
> +**/
> +EFI_STATUS
> +EFIAPI
> +FfsFindSection (
> +  IN EFI_SECTION_TYPE              SectionType,
> +  IN EFI_FFS_FILE_HEADER           *FfsFileHeader,
> +  IN OUT EFI_COMMON_SECTION_HEADER **SectionHeader
> +  )
> +{
> +  UINT32                    FileSize;
> +  EFI_COMMON_SECTION_HEADER *Section;
> +  EFI_STATUS                Status;
> +
> +  //
> +  // Size is 24 bits wide so mask upper 8 bits.
> +  //    Does not include FfsFileHeader header size
> +  // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
> +  //
> +  Section   = (EFI_COMMON_SECTION_HEADER *) (FfsFileHeader + 1);
> +  FileSize  = FFS_FILE_SIZE(FfsFileHeader);
> +  FileSize -= sizeof (EFI_FFS_FILE_HEADER);
> +
> +  Status = FindFfsSectionInSections (
> +             Section,
> +             FileSize,
> +             SectionType,
> +             SectionHeader
> +             );
> +  return Status;
> +}
> +
> +/**
> +  Given the input file pointer, search for the next matching section in the
> +  FFS volume.
> +
> +  @param  SearchType      Filter to find only sections of this type.
> +  @param  FfsFileHeader   Pointer to the current file to search.
> +  @param  SectionData     Pointer to the Section matching SectionType in FfsFileHeader.
> +                          NULL if section not found
> +  @param  SectionDataSize The size of SectionData
> +
> +  @retval  EFI_NOT_FOUND  No files matching the search criteria were found
> +  @retval  EFI_SUCCESS
> +**/
> +EFI_STATUS
> +EFIAPI
> +FfsFindSectionData (
> +  IN EFI_SECTION_TYPE      SectionType,
> +  IN EFI_FFS_FILE_HEADER   *FfsFileHeader,
> +  IN OUT VOID              **SectionData,
> +  IN OUT UINTN             *SectionDataSize
> +  )
> +{
> +  UINT32                    FileSize;
> +  EFI_COMMON_SECTION_HEADER *Section;
> +  UINT32                    SectionLength;
> +  UINT32                    ParsedLength;
> +
> +  //
> +  // Size is 24 bits wide so mask upper 8 bits.
> +  //    Does not include FfsFileHeader header size
> +  // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
> +  //
> +  Section   = (EFI_COMMON_SECTION_HEADER *) (FfsFileHeader + 1);
> +  FileSize  = FFS_FILE_SIZE(FfsFileHeader);
> +  FileSize -= sizeof (EFI_FFS_FILE_HEADER);
> +
> +  *SectionData  = NULL;
> +  ParsedLength  = 0;
> +  while (ParsedLength < FileSize) {
> +    if (Section->Type == SectionType) {
> +      *SectionData = (VOID *) (Section + 1);
> +      *SectionDataSize = SECTION_SIZE(Section);
> +      return EFI_SUCCESS;
> +    }
> +    //
> +    // Size is 24 bits wide so mask upper 8 bits.
> +    // SectionLength is adjusted it is 4 byte aligned.
> +    // Go to the next section
> +    //
> +    SectionLength = SECTION_SIZE(Section);
> +    SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
> +
> +    ParsedLength += SectionLength;
> +    Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + SectionLength);
> +  }
> +
> +  return EFI_NOT_FOUND;
> +}
> diff --git a/StandaloneMmPkg/Library/FvLib/FvLib.inf b/StandaloneMmPkg/Library/FvLib/FvLib.inf
> new file mode 100644
> index 0000000000..6df50ab364
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/FvLib/FvLib.inf
> @@ -0,0 +1,57 @@
> +## @file
> +#
> +#  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +#  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +#
> +#  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
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +##
> +
> +################################################################################
> +#
> +# Defines Section - statements that will be processed to create a Makefile.
> +#
> +################################################################################
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = FvLib
> +  FILE_GUID                      = C20085E9-E3AB-4938-A727-C10935FEEE2B
> +  MODULE_TYPE                    = BASE
> +  VERSION_STRING                 = 1.0
> +  LIBRARY_CLASS                  = FvLib
> +
> +#
> +# The following information is for reference only and not required by the build tools.
> +#
> +#  VALID_ARCHITECTURES           = IA32
> +#
> +
> +################################################################################
> +#
> +# Sources Section - list of files that are required for the build to succeed.
> +#
> +################################################################################
> +
> +[Sources]
> +  FvLib.c
> +
> +################################################################################
> +#
> +# Package Dependency Section - list of Package files that are required for
> +#                              this module.
> +#
> +################################################################################
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[LibraryClasses]
> +  BaseLib
> +  BaseMemoryLib
> +  DebugLib
> -- 
> 2.16.2
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 08/18] StandaloneMmPkg/MemLib: AARCH64 Specific instance of memory check library.
  2018-04-06 14:42 ` [PATCH v1 08/18] StandaloneMmPkg/MemLib: AARCH64 Specific instance of memory check library Supreeth Venkatesh
@ 2018-04-16 15:12   ` Achin Gupta
  2018-04-16 22:30     ` Yao, Jiewen
  2018-05-04 23:21     ` Supreeth Venkatesh
  0 siblings, 2 replies; 70+ messages in thread
From: Achin Gupta @ 2018-04-16 15:12 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

Hi Supreeth,

On Fri, Apr 06, 2018 at 03:42:13PM +0100, Supreeth Venkatesh wrote:
> MM memory check library library implementation. This library consumes
> MM_ACCESS_PROTOCOL to get MMRAM information. In order to use this
> library instance, the platform should produce all MMRAM range via
> MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core
> and MM driver) and/or specific dedicated hardware.
> 
> This patch provides services for MM Memory Operation.
> The management mode Mem Library provides function for checking if buffer
> is outside MMRAM and valid. It also provides functions for copy data
> from MMRAM to non-MMRAM, from non-MMRAM to MMRAM,
> from non-MMRAM to non-MMRAM, or set data in non-MMRAM.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  StandaloneMmPkg/Include/Library/MemLib.h    | 140 ++++++++++++++
>  StandaloneMmPkg/Library/MemLib/Arm/MemLib.c | 276 ++++++++++++++++++++++++++++

Why is this Library Arm specific. Apart from cosmetics tweaks, it has not
changed since it was originally contributed?

cheers,
Achin

>  StandaloneMmPkg/Library/MemLib/MemLib.inf   |  47 +++++
>  3 files changed, 463 insertions(+)
>  create mode 100644 StandaloneMmPkg/Include/Library/MemLib.h
>  create mode 100644 StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
>  create mode 100644 StandaloneMmPkg/Library/MemLib/MemLib.inf
> 
> diff --git a/StandaloneMmPkg/Include/Library/MemLib.h b/StandaloneMmPkg/Include/Library/MemLib.h
> new file mode 100644
> index 0000000000..3264f10010
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Library/MemLib.h
> @@ -0,0 +1,140 @@
> +/** @file
> +  Provides services for MM Memory Operation.
> +
> +  The MM Mem Library provides function for checking if buffer is outside MMRAM and valid.
> +  It also provides functions for copy data from MMRAM to non-MMRAM, from non-MMRAM to MMRAM,
> +  from non-MMRAM to non-MMRAM, or set data in non-MMRAM.
> +
> +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _MM_MEM_LIB_H_
> +#define _MM_MEM_LIB_H_
> +
> +/**
> +  This function check if the buffer is valid per processor architecture and not overlap with MMRAM.
> +
> +  @param Buffer  The buffer start address to be checked.
> +  @param Length  The buffer length to be checked.
> +
> +  @retval TRUE  This buffer is valid per processor architecture and not overlap with MMRAM.
> +  @retval FALSE This buffer is not valid per processor architecture or overlap with MMRAM.
> +**/
> +BOOLEAN
> +EFIAPI
> +MmIsBufferOutsideMmValid (
> +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> +  IN UINT64                Length
> +  );
> +
> +/**
> +  Copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +
> +  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +  It checks if source buffer is valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it copies memory and returns EFI_SUCCESS.
> +  If the check fails, it return EFI_SECURITY_VIOLATION.
> +  The implementation must be reentrant.
> +
> +  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
> +  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
> +  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is copied.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCopyMemToSmram (
> +  OUT VOID       *DestinationBuffer,
> +  IN CONST VOID  *SourceBuffer,
> +  IN UINTN       Length
> +  );
> +
> +/**
> +  Copies a source buffer (MMRAM) to a destination buffer (NON-MMRAM).
> +
> +  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +  It checks if destination buffer is valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it copies memory and returns EFI_SUCCESS.
> +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> +  The implementation must be reentrant.
> +
> +  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
> +  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
> +  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is copied.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCopyMemFromSmram (
> +  OUT VOID       *DestinationBuffer,
> +  IN CONST VOID  *SourceBuffer,
> +  IN UINTN       Length
> +  );
> +
> +/**
> +  Copies a source buffer (NON-MMRAM) to a destination buffer (NON-MMRAM).
> +
> +  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +  It checks if source buffer and destination buffer are valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it copies memory and returns EFI_SUCCESS.
> +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> +  The implementation must be reentrant, and it must handle the case where source buffer overlaps destination buffer.
> +
> +  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
> +  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
> +  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is copied.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCopyMem (
> +  OUT VOID       *DestinationBuffer,
> +  IN CONST VOID  *SourceBuffer,
> +  IN UINTN       Length
> +  );
> +
> +/**
> +  Fills a target buffer (NON-MMRAM) with a byte value.
> +
> +  This function fills a target buffer (non-MMRAM) with a byte value.
> +  It checks if target buffer is valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it fills memory and returns EFI_SUCCESS.
> +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> +
> +  @param  Buffer    The memory to set.
> +  @param  Length    The number of bytes to set.
> +  @param  Value     The value with which to fill Length bytes of Buffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The Buffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is set.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmSetMem (
> +  OUT VOID  *Buffer,
> +  IN UINTN  Length,
> +  IN UINT8  Value
> +  );
> +
> +#endif
> diff --git a/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c b/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> new file mode 100644
> index 0000000000..432a45698b
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> @@ -0,0 +1,276 @@
> +/** @file
> +  Instance of MM memory check library.
> +
> +  MM memory check library library implementation. This library consumes MM_ACCESS_PROTOCOL
> +  to get MMRAM information. In order to use this library instance, the platform should produce
> +  all MMRAM range via MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core
> +  and MM driver) and/or specific dedicated hardware.
> +
> +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +
> +#include <PiMm.h>
> +
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +
> +EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges;
> +UINTN                mMmMemLibInternalMmramCount;
> +
> +//
> +// Maximum support address used to check input buffer
> +//
> +EFI_PHYSICAL_ADDRESS  mMmMemLibInternalMaximumSupportAddress = 0;
> +
> +/**
> +  Calculate and save the maximum support address.
> +
> +**/
> +VOID
> +MmMemLibInternalCalculateMaximumSupportAddress (
> +  VOID
> +  )
> +{
> +  UINT8        PhysicalAddressBits;
> +
> +  PhysicalAddressBits = 36;
> +
> +  //
> +  // Save the maximum support address in one global variable
> +  //
> +  mMmMemLibInternalMaximumSupportAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, PhysicalAddressBits) - 1);
> +  DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress = 0x%lx\n", mMmMemLibInternalMaximumSupportAddress));
> +}
> +
> +/**
> +  This function check if the buffer is valid per processor architecture and not overlap with MMRAM.
> +
> +  @param Buffer  The buffer start address to be checked.
> +  @param Length  The buffer length to be checked.
> +
> +  @retval TRUE  This buffer is valid per processor architecture and not overlap with MMRAM.
> +  @retval FALSE This buffer is not valid per processor architecture or overlap with MMRAM.
> +**/
> +BOOLEAN
> +EFIAPI
> +MmIsBufferOutsideMmValid (
> +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> +  IN UINT64                Length
> +  )
> +{
> +  UINTN  Index;
> +
> +  //
> +  // Check override.
> +  // NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1) is valid.
> +  //
> +  if ((Length > mMmMemLibInternalMaximumSupportAddress) ||
> +      (Buffer > mMmMemLibInternalMaximumSupportAddress) ||
> +      ((Length != 0) && (Buffer > (mMmMemLibInternalMaximumSupportAddress - (Length - 1)))) ) {
> +    //
> +    // Overflow happen
> +    //
> +    DEBUG ((
> +      DEBUG_ERROR,
> +      "MmIsBufferOutsideMmValid: Overflow: Buffer (0x%lx) - Length (0x%lx), MaximumSupportAddress (0x%lx)\n",
> +      Buffer,
> +      Length,
> +      mMmMemLibInternalMaximumSupportAddress
> +      ));
> +    return FALSE;
> +  }
> +
> +  for (Index = 0; Index < mMmMemLibInternalMmramCount; Index ++) {
> +    if (((Buffer >= mMmMemLibInternalMmramRanges[Index].CpuStart) && (Buffer < mMmMemLibInternalMmramRanges[Index].CpuStart + mMmMemLibInternalMmramRanges[Index].PhysicalSize)) ||
> +        ((mMmMemLibInternalMmramRanges[Index].CpuStart >= Buffer) && (mMmMemLibInternalMmramRanges[Index].CpuStart < Buffer + Length))) {
> +      DEBUG ((
> +        DEBUG_ERROR,
> +        "MmIsBufferOutsideMmValid: Overlap: Buffer (0x%lx) - Length (0x%lx), ",
> +        Buffer,
> +        Length
> +        ));
> +      DEBUG ((
> +        DEBUG_ERROR,
> +        "CpuStart (0x%lx) - PhysicalSize (0x%lx)\n",
> +        mMmMemLibInternalMmramRanges[Index].CpuStart,
> +        mMmMemLibInternalMmramRanges[Index].PhysicalSize
> +        ));
> +      return FALSE;
> +    }
> +  }
> +
> +  return TRUE;
> +}
> +
> +/**
> +  Copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +
> +  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +  It checks if source buffer is valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it copies memory and returns EFI_SUCCESS.
> +  If the check fails, it return EFI_SECURITY_VIOLATION.
> +  The implementation must be reentrant.
> +
> +  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
> +  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
> +  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is copied.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCopyMemToMmram (
> +  OUT VOID       *DestinationBuffer,
> +  IN CONST VOID  *SourceBuffer,
> +  IN UINTN       Length
> +  )
> +{
> +  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {
> +    DEBUG ((DEBUG_ERROR, "MmCopyMemToMmram: Security Violation: Source (0x%x), Length (0x%x)\n", SourceBuffer, Length));
> +    return EFI_SECURITY_VIOLATION;
> +  }
> +  CopyMem (DestinationBuffer, SourceBuffer, Length);
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Copies a source buffer (MMRAM) to a destination buffer (NON-MMRAM).
> +
> +  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +  It checks if destination buffer is valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it copies memory and returns EFI_SUCCESS.
> +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> +  The implementation must be reentrant.
> +
> +  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
> +  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
> +  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is copied.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCopyMemFromMmram (
> +  OUT VOID       *DestinationBuffer,
> +  IN CONST VOID  *SourceBuffer,
> +  IN UINTN       Length
> +  )
> +{
> +  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {
> +    DEBUG ((DEBUG_ERROR, "MmCopyMemFromMmram: Security Violation: Destination (0x%x), Length (0x%x)\n", DestinationBuffer, Length));
> +    return EFI_SECURITY_VIOLATION;
> +  }
> +  CopyMem (DestinationBuffer, SourceBuffer, Length);
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Copies a source buffer (NON-MMRAM) to a destination buffer (NON-MMRAM).
> +
> +  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +  It checks if source buffer and destination buffer are valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it copies memory and returns EFI_SUCCESS.
> +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> +  The implementation must be reentrant, and it must handle the case where source buffer overlaps destination buffer.
> +
> +  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
> +  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
> +  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is copied.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCopyMem (
> +  OUT VOID       *DestinationBuffer,
> +  IN CONST VOID  *SourceBuffer,
> +  IN UINTN       Length
> +  )
> +{
> +  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {
> +    DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation: Destination (0x%x), Length (0x%x)\n", DestinationBuffer, Length));
> +    return EFI_SECURITY_VIOLATION;
> +  }
> +  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {
> +    DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation: Source (0x%x), Length (0x%x)\n", SourceBuffer, Length));
> +    return EFI_SECURITY_VIOLATION;
> +  }
> +  CopyMem (DestinationBuffer, SourceBuffer, Length);
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Fills a target buffer (NON-MMRAM) with a byte value.
> +
> +  This function fills a target buffer (non-MMRAM) with a byte value.
> +  It checks if target buffer is valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it fills memory and returns EFI_SUCCESS.
> +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> +
> +  @param  Buffer    The memory to set.
> +  @param  Length    The number of bytes to set.
> +  @param  Value     The value with which to fill Length bytes of Buffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The Buffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is set.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmSetMem (
> +  OUT VOID  *Buffer,
> +  IN UINTN  Length,
> +  IN UINT8  Value
> +  )
> +{
> +  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Length)) {
> +    DEBUG ((DEBUG_ERROR, "MmSetMem: Security Violation: Source (0x%x), Length (0x%x)\n", Buffer, Length));
> +    return EFI_SECURITY_VIOLATION;
> +  }
> +  SetMem (Buffer, Length, Value);
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  The constructor function initializes the Mm Mem library
> +
> +  @param  ImageHandle   The firmware allocated handle for the EFI image.
> +  @param  SystemTable   A pointer to the EFI System Table.
> +
> +  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MemLibConstructor (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  )
> +{
> +
> +  //
> +  // Calculate and save maximum support address
> +  //
> +  MmMemLibInternalCalculateMaximumSupportAddress ();
> +
> +  return EFI_SUCCESS;
> +}
> diff --git a/StandaloneMmPkg/Library/MemLib/MemLib.inf b/StandaloneMmPkg/Library/MemLib/MemLib.inf
> new file mode 100644
> index 0000000000..52b7c06397
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/MemLib/MemLib.inf
> @@ -0,0 +1,47 @@
> +## @file
> +#  Instance of MM memory check library.
> +#
> +#  MM memory check library library implementation. This library consumes MM_ACCESS_PROTOCOL
> +#  to get MMRAM information. In order to use this library instance, the platform should produce
> +#  all MMRAM range via MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core
> +#  and MM driver) and/or specific dedicated hardware.
> +#
> +#  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +#  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +#
> +#  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
> +#
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = MemLib
> +  FILE_GUID                      = EA355F14-6409-4716-829F-37B3BC7C7F26
> +  MODULE_TYPE                    = MM_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  LIBRARY_CLASS                  = MemLib|MM_STANDALONE MM_CORE_STANDALONE
> +  CONSTRUCTOR                    = MemLibConstructor
> +
> +#
> +# The following information is for reference only and not required by the build tools.
> +#
> +#  VALID_ARCHITECTURES           = AARCH64
> +#
> +
> +[Sources.AARCH64]
> +  Arm/MemLib.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[LibraryClasses]
> +  BaseMemoryLib
> +  DebugLib
> -- 
> 2.16.2
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 08/18] StandaloneMmPkg/MemLib: AARCH64 Specific instance of memory check library.
  2018-04-16 15:12   ` Achin Gupta
@ 2018-04-16 22:30     ` Yao, Jiewen
  2018-04-25 10:35       ` Achin Gupta
  2018-05-04 23:21     ` Supreeth Venkatesh
  1 sibling, 1 reply; 70+ messages in thread
From: Yao, Jiewen @ 2018-04-16 22:30 UTC (permalink / raw)
  To: Achin Gupta, Supreeth Venkatesh
  Cc: ard.biesheuvel@linaro.org, edk2-devel@lists.01.org,
	leif.lindholm@linaro.org, Gao, Liming, Kinney, Michael D,
	nd@arm.com

Hi
I don't think this lib is generic, because it hardcode the physical address bits.

PhysicalAddressBits = 36;

For X86 CPU, we get it from CPUID. :-)

As enhancement, we may put most common C-code logic (such as CopyMem, or memmap calculation) to StandaloneMmPkg/MemLib, and only include the PhysicalAddresBit calculation under StandaloneMmPkg/MemLib/Arm folder.

As such, we know clearly on which one is ARM specific.

Thank you
Yao Jiewen

> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Achin
> Gupta
> Sent: Monday, April 16, 2018 11:13 PM
> To: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> Cc: ard.biesheuvel@linaro.org; edk2-devel@lists.01.org;
> leif.lindholm@linaro.org; Yao, Jiewen <jiewen.yao@intel.com>; Gao, Liming
> <liming.gao@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>;
> nd@arm.com
> Subject: Re: [edk2] [PATCH v1 08/18] StandaloneMmPkg/MemLib: AARCH64
> Specific instance of memory check library.
> 
> Hi Supreeth,
> 
> On Fri, Apr 06, 2018 at 03:42:13PM +0100, Supreeth Venkatesh wrote:
> > MM memory check library library implementation. This library consumes
> > MM_ACCESS_PROTOCOL to get MMRAM information. In order to use this
> > library instance, the platform should produce all MMRAM range via
> > MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core
> > and MM driver) and/or specific dedicated hardware.
> >
> > This patch provides services for MM Memory Operation.
> > The management mode Mem Library provides function for checking if buffer
> > is outside MMRAM and valid. It also provides functions for copy data
> > from MMRAM to non-MMRAM, from non-MMRAM to MMRAM,
> > from non-MMRAM to non-MMRAM, or set data in non-MMRAM.
> >
> > Contributed-under: TianoCore Contribution Agreement 1.1
> > Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> > Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> > ---
> >  StandaloneMmPkg/Include/Library/MemLib.h    | 140 ++++++++++++++
> >  StandaloneMmPkg/Library/MemLib/Arm/MemLib.c | 276
> ++++++++++++++++++++++++++++
> 
> Why is this Library Arm specific. Apart from cosmetics tweaks, it has not
> changed since it was originally contributed?
> 
> cheers,
> Achin
> 
> >  StandaloneMmPkg/Library/MemLib/MemLib.inf   |  47 +++++
> >  3 files changed, 463 insertions(+)
> >  create mode 100644 StandaloneMmPkg/Include/Library/MemLib.h
> >  create mode 100644 StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> >  create mode 100644 StandaloneMmPkg/Library/MemLib/MemLib.inf
> >
> > diff --git a/StandaloneMmPkg/Include/Library/MemLib.h
> b/StandaloneMmPkg/Include/Library/MemLib.h
> > new file mode 100644
> > index 0000000000..3264f10010
> > --- /dev/null
> > +++ b/StandaloneMmPkg/Include/Library/MemLib.h
> > @@ -0,0 +1,140 @@
> > +/** @file
> > +  Provides services for MM Memory Operation.
> > +
> > +  The MM Mem Library provides function for checking if buffer is outside
> MMRAM and valid.
> > +  It also provides functions for copy data from MMRAM to non-MMRAM,
> from non-MMRAM to MMRAM,
> > +  from non-MMRAM to non-MMRAM, or set data in non-MMRAM.
> > +
> > +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > +
> > +  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
> > +
> > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +
> > +**/
> > +
> > +#ifndef _MM_MEM_LIB_H_
> > +#define _MM_MEM_LIB_H_
> > +
> > +/**
> > +  This function check if the buffer is valid per processor architecture and not
> overlap with MMRAM.
> > +
> > +  @param Buffer  The buffer start address to be checked.
> > +  @param Length  The buffer length to be checked.
> > +
> > +  @retval TRUE  This buffer is valid per processor architecture and not
> overlap with MMRAM.
> > +  @retval FALSE This buffer is not valid per processor architecture or overlap
> with MMRAM.
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +MmIsBufferOutsideMmValid (
> > +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> > +  IN UINT64                Length
> > +  );
> > +
> > +/**
> > +  Copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> > +
> > +  This function copies a source buffer (non-MMRAM) to a destination buffer
> (MMRAM).
> > +  It checks if source buffer is valid per processor architecture and not overlap
> with MMRAM.
> > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > +  If the check fails, it return EFI_SECURITY_VIOLATION.
> > +  The implementation must be reentrant.
> > +
> > +  @param  DestinationBuffer   The pointer to the destination buffer of the
> memory copy.
> > +  @param  SourceBuffer        The pointer to the source buffer of the
> memory copy.
> > +  @param  Length              The number of bytes to copy from
> SourceBuffer to DestinationBuffer.
> > +
> > +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per
> processor architecture or overlap with MMRAM.
> > +  @retval EFI_SUCCESS            Memory is copied.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +MmCopyMemToSmram (
> > +  OUT VOID       *DestinationBuffer,
> > +  IN CONST VOID  *SourceBuffer,
> > +  IN UINTN       Length
> > +  );
> > +
> > +/**
> > +  Copies a source buffer (MMRAM) to a destination buffer (NON-MMRAM).
> > +
> > +  This function copies a source buffer (non-MMRAM) to a destination buffer
> (MMRAM).
> > +  It checks if destination buffer is valid per processor architecture and not
> overlap with MMRAM.
> > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > +  The implementation must be reentrant.
> > +
> > +  @param  DestinationBuffer   The pointer to the destination buffer of the
> memory copy.
> > +  @param  SourceBuffer        The pointer to the source buffer of the
> memory copy.
> > +  @param  Length              The number of bytes to copy from
> SourceBuffer to DestinationBuffer.
> > +
> > +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per
> processor architecture or overlap with MMRAM.
> > +  @retval EFI_SUCCESS            Memory is copied.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +MmCopyMemFromSmram (
> > +  OUT VOID       *DestinationBuffer,
> > +  IN CONST VOID  *SourceBuffer,
> > +  IN UINTN       Length
> > +  );
> > +
> > +/**
> > +  Copies a source buffer (NON-MMRAM) to a destination buffer
> (NON-MMRAM).
> > +
> > +  This function copies a source buffer (non-MMRAM) to a destination buffer
> (MMRAM).
> > +  It checks if source buffer and destination buffer are valid per processor
> architecture and not overlap with MMRAM.
> > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > +  The implementation must be reentrant, and it must handle the case where
> source buffer overlaps destination buffer.
> > +
> > +  @param  DestinationBuffer   The pointer to the destination buffer of the
> memory copy.
> > +  @param  SourceBuffer        The pointer to the source buffer of the
> memory copy.
> > +  @param  Length              The number of bytes to copy from
> SourceBuffer to DestinationBuffer.
> > +
> > +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per
> processor architecture or overlap with MMRAM.
> > +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per
> processor architecture or overlap with MMRAM.
> > +  @retval EFI_SUCCESS            Memory is copied.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +MmCopyMem (
> > +  OUT VOID       *DestinationBuffer,
> > +  IN CONST VOID  *SourceBuffer,
> > +  IN UINTN       Length
> > +  );
> > +
> > +/**
> > +  Fills a target buffer (NON-MMRAM) with a byte value.
> > +
> > +  This function fills a target buffer (non-MMRAM) with a byte value.
> > +  It checks if target buffer is valid per processor architecture and not overlap
> with MMRAM.
> > +  If the check passes, it fills memory and returns EFI_SUCCESS.
> > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > +
> > +  @param  Buffer    The memory to set.
> > +  @param  Length    The number of bytes to set.
> > +  @param  Value     The value with which to fill Length bytes of Buffer.
> > +
> > +  @retval EFI_SECURITY_VIOLATION The Buffer is invalid per processor
> architecture or overlap with MMRAM.
> > +  @retval EFI_SUCCESS            Memory is set.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +MmSetMem (
> > +  OUT VOID  *Buffer,
> > +  IN UINTN  Length,
> > +  IN UINT8  Value
> > +  );
> > +
> > +#endif
> > diff --git a/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> b/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> > new file mode 100644
> > index 0000000000..432a45698b
> > --- /dev/null
> > +++ b/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> > @@ -0,0 +1,276 @@
> > +/** @file
> > +  Instance of MM memory check library.
> > +
> > +  MM memory check library library implementation. This library consumes
> MM_ACCESS_PROTOCOL
> > +  to get MMRAM information. In order to use this library instance, the
> platform should produce
> > +  all MMRAM range via MM_ACCESS_PROTOCOL, including the range for
> firmware (like MM Core
> > +  and MM driver) and/or specific dedicated hardware.
> > +
> > +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > +
> > +  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
> > +
> > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +
> > +**/
> > +
> > +
> > +#include <PiMm.h>
> > +
> > +#include <Library/BaseLib.h>
> > +#include <Library/BaseMemoryLib.h>
> > +#include <Library/DebugLib.h>
> > +
> > +EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges;
> > +UINTN                mMmMemLibInternalMmramCount;
> > +
> > +//
> > +// Maximum support address used to check input buffer
> > +//
> > +EFI_PHYSICAL_ADDRESS  mMmMemLibInternalMaximumSupportAddress =
> 0;
> > +
> > +/**
> > +  Calculate and save the maximum support address.
> > +
> > +**/
> > +VOID
> > +MmMemLibInternalCalculateMaximumSupportAddress (
> > +  VOID
> > +  )
> > +{
> > +  UINT8        PhysicalAddressBits;
> > +
> > +  PhysicalAddressBits = 36;
> > +
> > +  //
> > +  // Save the maximum support address in one global variable
> > +  //
> > +  mMmMemLibInternalMaximumSupportAddress =
> (EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, PhysicalAddressBits) - 1);
> > +  DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress =
> 0x%lx\n", mMmMemLibInternalMaximumSupportAddress));
> > +}
> > +
> > +/**
> > +  This function check if the buffer is valid per processor architecture and not
> overlap with MMRAM.
> > +
> > +  @param Buffer  The buffer start address to be checked.
> > +  @param Length  The buffer length to be checked.
> > +
> > +  @retval TRUE  This buffer is valid per processor architecture and not
> overlap with MMRAM.
> > +  @retval FALSE This buffer is not valid per processor architecture or overlap
> with MMRAM.
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +MmIsBufferOutsideMmValid (
> > +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> > +  IN UINT64                Length
> > +  )
> > +{
> > +  UINTN  Index;
> > +
> > +  //
> > +  // Check override.
> > +  // NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1) is
> valid.
> > +  //
> > +  if ((Length > mMmMemLibInternalMaximumSupportAddress) ||
> > +      (Buffer > mMmMemLibInternalMaximumSupportAddress) ||
> > +      ((Length != 0) && (Buffer >
> (mMmMemLibInternalMaximumSupportAddress - (Length - 1)))) ) {
> > +    //
> > +    // Overflow happen
> > +    //
> > +    DEBUG ((
> > +      DEBUG_ERROR,
> > +      "MmIsBufferOutsideMmValid: Overflow: Buffer (0x%lx) - Length
> (0x%lx), MaximumSupportAddress (0x%lx)\n",
> > +      Buffer,
> > +      Length,
> > +      mMmMemLibInternalMaximumSupportAddress
> > +      ));
> > +    return FALSE;
> > +  }
> > +
> > +  for (Index = 0; Index < mMmMemLibInternalMmramCount; Index ++) {
> > +    if (((Buffer >= mMmMemLibInternalMmramRanges[Index].CpuStart) &&
> (Buffer < mMmMemLibInternalMmramRanges[Index].CpuStart +
> mMmMemLibInternalMmramRanges[Index].PhysicalSize)) ||
> > +        ((mMmMemLibInternalMmramRanges[Index].CpuStart >= Buffer)
> && (mMmMemLibInternalMmramRanges[Index].CpuStart < Buffer + Length))) {
> > +      DEBUG ((
> > +        DEBUG_ERROR,
> > +        "MmIsBufferOutsideMmValid: Overlap: Buffer (0x%lx) - Length
> (0x%lx), ",
> > +        Buffer,
> > +        Length
> > +        ));
> > +      DEBUG ((
> > +        DEBUG_ERROR,
> > +        "CpuStart (0x%lx) - PhysicalSize (0x%lx)\n",
> > +        mMmMemLibInternalMmramRanges[Index].CpuStart,
> > +        mMmMemLibInternalMmramRanges[Index].PhysicalSize
> > +        ));
> > +      return FALSE;
> > +    }
> > +  }
> > +
> > +  return TRUE;
> > +}
> > +
> > +/**
> > +  Copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> > +
> > +  This function copies a source buffer (non-MMRAM) to a destination buffer
> (MMRAM).
> > +  It checks if source buffer is valid per processor architecture and not overlap
> with MMRAM.
> > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > +  If the check fails, it return EFI_SECURITY_VIOLATION.
> > +  The implementation must be reentrant.
> > +
> > +  @param  DestinationBuffer   The pointer to the destination buffer of the
> memory copy.
> > +  @param  SourceBuffer        The pointer to the source buffer of the
> memory copy.
> > +  @param  Length              The number of bytes to copy from
> SourceBuffer to DestinationBuffer.
> > +
> > +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per
> processor architecture or overlap with MMRAM.
> > +  @retval EFI_SUCCESS            Memory is copied.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +MmCopyMemToMmram (
> > +  OUT VOID       *DestinationBuffer,
> > +  IN CONST VOID  *SourceBuffer,
> > +  IN UINTN       Length
> > +  )
> > +{
> > +  if (!MmIsBufferOutsideMmValid
> ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {
> > +    DEBUG ((DEBUG_ERROR, "MmCopyMemToMmram: Security Violation:
> Source (0x%x), Length (0x%x)\n", SourceBuffer, Length));
> > +    return EFI_SECURITY_VIOLATION;
> > +  }
> > +  CopyMem (DestinationBuffer, SourceBuffer, Length);
> > +  return EFI_SUCCESS;
> > +}
> > +
> > +/**
> > +  Copies a source buffer (MMRAM) to a destination buffer (NON-MMRAM).
> > +
> > +  This function copies a source buffer (non-MMRAM) to a destination buffer
> (MMRAM).
> > +  It checks if destination buffer is valid per processor architecture and not
> overlap with MMRAM.
> > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > +  The implementation must be reentrant.
> > +
> > +  @param  DestinationBuffer   The pointer to the destination buffer of the
> memory copy.
> > +  @param  SourceBuffer        The pointer to the source buffer of the
> memory copy.
> > +  @param  Length              The number of bytes to copy from
> SourceBuffer to DestinationBuffer.
> > +
> > +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per
> processor architecture or overlap with MMRAM.
> > +  @retval EFI_SUCCESS            Memory is copied.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +MmCopyMemFromMmram (
> > +  OUT VOID       *DestinationBuffer,
> > +  IN CONST VOID  *SourceBuffer,
> > +  IN UINTN       Length
> > +  )
> > +{
> > +  if (!MmIsBufferOutsideMmValid
> ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {
> > +    DEBUG ((DEBUG_ERROR, "MmCopyMemFromMmram: Security
> Violation: Destination (0x%x), Length (0x%x)\n", DestinationBuffer, Length));
> > +    return EFI_SECURITY_VIOLATION;
> > +  }
> > +  CopyMem (DestinationBuffer, SourceBuffer, Length);
> > +  return EFI_SUCCESS;
> > +}
> > +
> > +/**
> > +  Copies a source buffer (NON-MMRAM) to a destination buffer
> (NON-MMRAM).
> > +
> > +  This function copies a source buffer (non-MMRAM) to a destination buffer
> (MMRAM).
> > +  It checks if source buffer and destination buffer are valid per processor
> architecture and not overlap with MMRAM.
> > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > +  The implementation must be reentrant, and it must handle the case where
> source buffer overlaps destination buffer.
> > +
> > +  @param  DestinationBuffer   The pointer to the destination buffer of the
> memory copy.
> > +  @param  SourceBuffer        The pointer to the source buffer of the
> memory copy.
> > +  @param  Length              The number of bytes to copy from
> SourceBuffer to DestinationBuffer.
> > +
> > +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per
> processor architecture or overlap with MMRAM.
> > +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per
> processor architecture or overlap with MMRAM.
> > +  @retval EFI_SUCCESS            Memory is copied.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +MmCopyMem (
> > +  OUT VOID       *DestinationBuffer,
> > +  IN CONST VOID  *SourceBuffer,
> > +  IN UINTN       Length
> > +  )
> > +{
> > +  if (!MmIsBufferOutsideMmValid
> ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {
> > +    DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation: Destination
> (0x%x), Length (0x%x)\n", DestinationBuffer, Length));
> > +    return EFI_SECURITY_VIOLATION;
> > +  }
> > +  if (!MmIsBufferOutsideMmValid
> ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {
> > +    DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation: Source
> (0x%x), Length (0x%x)\n", SourceBuffer, Length));
> > +    return EFI_SECURITY_VIOLATION;
> > +  }
> > +  CopyMem (DestinationBuffer, SourceBuffer, Length);
> > +  return EFI_SUCCESS;
> > +}
> > +
> > +/**
> > +  Fills a target buffer (NON-MMRAM) with a byte value.
> > +
> > +  This function fills a target buffer (non-MMRAM) with a byte value.
> > +  It checks if target buffer is valid per processor architecture and not overlap
> with MMRAM.
> > +  If the check passes, it fills memory and returns EFI_SUCCESS.
> > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > +
> > +  @param  Buffer    The memory to set.
> > +  @param  Length    The number of bytes to set.
> > +  @param  Value     The value with which to fill Length bytes of Buffer.
> > +
> > +  @retval EFI_SECURITY_VIOLATION The Buffer is invalid per processor
> architecture or overlap with MMRAM.
> > +  @retval EFI_SUCCESS            Memory is set.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +MmSetMem (
> > +  OUT VOID  *Buffer,
> > +  IN UINTN  Length,
> > +  IN UINT8  Value
> > +  )
> > +{
> > +  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer,
> Length)) {
> > +    DEBUG ((DEBUG_ERROR, "MmSetMem: Security Violation: Source
> (0x%x), Length (0x%x)\n", Buffer, Length));
> > +    return EFI_SECURITY_VIOLATION;
> > +  }
> > +  SetMem (Buffer, Length, Value);
> > +  return EFI_SUCCESS;
> > +}
> > +
> > +/**
> > +  The constructor function initializes the Mm Mem library
> > +
> > +  @param  ImageHandle   The firmware allocated handle for the EFI
> image.
> > +  @param  SystemTable   A pointer to the EFI System Table.
> > +
> > +  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +MemLibConstructor (
> > +  IN EFI_HANDLE             ImageHandle,
> > +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> > +  )
> > +{
> > +
> > +  //
> > +  // Calculate and save maximum support address
> > +  //
> > +  MmMemLibInternalCalculateMaximumSupportAddress ();
> > +
> > +  return EFI_SUCCESS;
> > +}
> > diff --git a/StandaloneMmPkg/Library/MemLib/MemLib.inf
> b/StandaloneMmPkg/Library/MemLib/MemLib.inf
> > new file mode 100644
> > index 0000000000..52b7c06397
> > --- /dev/null
> > +++ b/StandaloneMmPkg/Library/MemLib/MemLib.inf
> > @@ -0,0 +1,47 @@
> > +## @file
> > +#  Instance of MM memory check library.
> > +#
> > +#  MM memory check library library implementation. This library consumes
> MM_ACCESS_PROTOCOL
> > +#  to get MMRAM information. In order to use this library instance, the
> platform should produce
> > +#  all MMRAM range via MM_ACCESS_PROTOCOL, including the range for
> firmware (like MM Core
> > +#  and MM driver) and/or specific dedicated hardware.
> > +#
> > +#  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > +#  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > +#
> > +#  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
> > +#
> > +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +#
> > +##
> > +
> > +[Defines]
> > +  INF_VERSION                    = 0x0001001A
> > +  BASE_NAME                      = MemLib
> > +  FILE_GUID                      =
> EA355F14-6409-4716-829F-37B3BC7C7F26
> > +  MODULE_TYPE                    = MM_STANDALONE
> > +  VERSION_STRING                 = 1.0
> > +  PI_SPECIFICATION_VERSION       = 0x00010032
> > +  LIBRARY_CLASS                  = MemLib|MM_STANDALONE
> MM_CORE_STANDALONE
> > +  CONSTRUCTOR                    = MemLibConstructor
> > +
> > +#
> > +# The following information is for reference only and not required by the
> build tools.
> > +#
> > +#  VALID_ARCHITECTURES           = AARCH64
> > +#
> > +
> > +[Sources.AARCH64]
> > +  Arm/MemLib.c
> > +
> > +[Packages]
> > +  MdePkg/MdePkg.dec
> > +  StandaloneMmPkg/StandaloneMmPkg.dec
> > +
> > +[LibraryClasses]
> > +  BaseMemoryLib
> > +  DebugLib
> > --
> > 2.16.2
> >
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 14/18] StandaloneMmPkg: Describe the declaration, definition and fdf files.
  2018-04-06 14:42 ` [PATCH v1 14/18] StandaloneMmPkg: Describe the declaration, definition and fdf files Supreeth Venkatesh
@ 2018-04-18 19:50   ` Daniil Egranov
  2018-05-04 23:29     ` Supreeth Venkatesh
  2018-04-30 19:32   ` Achin Gupta
  1 sibling, 1 reply; 70+ messages in thread
From: Daniil Egranov @ 2018-04-18 19:50 UTC (permalink / raw)
  To: Supreeth Venkatesh, edk2-devel
  Cc: ard.biesheuvel, leif.lindholm, jiewen.yao, liming.gao,
	michael.d.kinney

Hi Supreeth,

Having .inc with the common configuration and data can be useful for 
including it to platform specific configurations. It will help to avoid 
patching platform specific builds in case of any changes in StandaloneMM 
structure or configuration.

Thanks,
Daniil

On 04/06/2018 09:42 AM, Supreeth Venkatesh wrote:
> This patch describes the package declarations, definitions and firmware
> device files for creating standalone management mode image with
> core/foundation and drivers.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>   StandaloneMmPkg/StandaloneMmPkg.dec |  47 +++++++++
>   StandaloneMmPkg/StandaloneMmPkg.dsc | 132 ++++++++++++++++++++++++++
>   StandaloneMmPkg/StandaloneMmPkg.fdf | 184 ++++++++++++++++++++++++++++++++++++
>   3 files changed, 363 insertions(+)
>   create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dec
>   create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dsc
>   create mode 100644 StandaloneMmPkg/StandaloneMmPkg.fdf
> 
> diff --git a/StandaloneMmPkg/StandaloneMmPkg.dec b/StandaloneMmPkg/StandaloneMmPkg.dec
> new file mode 100644
> index 0000000000..36521bb039
> --- /dev/null
> +++ b/StandaloneMmPkg/StandaloneMmPkg.dec
> @@ -0,0 +1,47 @@
> +## @file
> +# This package is a platform package that provide platform module/library
> +# required by Standalone MM platform.
> +#
> +# Copyright (c) 2016-2017, ARM Ltd. All rights reserved.<BR>
> +#
> +# 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
> +#
> +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#
> +
> +[Defines]
> +  DEC_SPECIFICATION              = 0x0001001A
> +  PACKAGE_NAME                   = StandaloneMmPkg
> +  PACKAGE_GUID                   = 2AE82968-7769-4A85-A5BC-A0954CE54A5C
> +  PACKAGE_VERSION                = 1.0
> +
> +[Includes]
> +  Include
> +
> +[LibraryClasses]
> +
> +[Guids]
> +  gStandaloneMmPkgTokenSpaceGuid           = { 0x18fe7632, 0xf5c8, 0x4e63, { 0x8d, 0xe8, 0x17, 0xa5, 0x5c, 0x59, 0x13, 0xbd }}
> +  gMpInformationHobGuid                    = { 0xba33f15d, 0x4000, 0x45c1, { 0x8e, 0x88, 0xf9, 0x16, 0x92, 0xd4, 0x57, 0xe3 }}
> +  gMmFvDispatchGuid                        = { 0xb65694cc, 0x09e3, 0x4c3b, { 0xb5, 0xcd, 0x05, 0xf4, 0x4d, 0x3c, 0xdb, 0xff }}
> +
> +  ## Include/Guid/MmCoreData.h
> +  gMmCoreDataHobGuid                       = { 0xa160bf99, 0x2aa4, 0x4d7d, { 0x99, 0x93, 0x89, 0x9c, 0xb1, 0x2d, 0xf3, 0x76 }}
> +
> +  ## Include/Guid/MmramMemoryReserve.h
> +  gEfiMmPeiMmramMemoryReserveGuid          = { 0x0703f912, 0xbf8d, 0x4e2a, { 0xbe, 0x07, 0xab, 0x27, 0x25, 0x25, 0xc5, 0x92 }}
> +
> +  gEfiStandaloneMmNonSecureBufferGuid      = { 0xf00497e3, 0xbfa2, 0x41a1, { 0x9d, 0x29, 0x54, 0xc2, 0xe9, 0x37, 0x21, 0xc5 }}
> +  gEfiArmTfCpuDriverEpDescriptorGuid       = { 0x6ecbd5a1, 0xc0f8, 0x4702, { 0x83, 0x01, 0x4f, 0xc2, 0xc5, 0x47, 0x0a, 0x51 }}
> +
> +[PcdsFeatureFlag]
> +  gStandaloneMmPkgTokenSpaceGuid.PcdStandaloneMmEnable|FALSE|BOOLEAN|0x00000001
> +
> +[Protocols]
> +  gEfiMmConfigurationProtocolGuid          = { 0xc109319, 0xc149, 0x450e,  { 0xa3, 0xe3, 0xb9, 0xba, 0xdd, 0x9d, 0xc3, 0xa4 }}
> +
> diff --git a/StandaloneMmPkg/StandaloneMmPkg.dsc b/StandaloneMmPkg/StandaloneMmPkg.dsc
> new file mode 100644
> index 0000000000..8cc996f6b0
> --- /dev/null
> +++ b/StandaloneMmPkg/StandaloneMmPkg.dsc
> @@ -0,0 +1,132 @@
> +## @file
> +# Standalone MM Platform.
> +#
> +# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +# Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +#
> +#    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
> +#
> +#    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +##
> +
> +################################################################################
> +#
> +# Defines Section - statements that will be processed to create a Makefile.
> +#
> +################################################################################
> +[Defines]
> +  PLATFORM_NAME                  = StandaloneMm
> +  PLATFORM_GUID                  = 9A4BBA60-B4F9-47C7-9258-3BD77CAE9322
> +  PLATFORM_VERSION               = 1.0
> +  DSC_SPECIFICATION              = 0x00010011
> +  OUTPUT_DIRECTORY               = Build/StandaloneMmPkg
> +  SUPPORTED_ARCHITECTURES        = IA32|X64|AARCH64
> +  BUILD_TARGETS                  = DEBUG|RELEASE
> +  SKUID_IDENTIFIER               = DEFAULT
> +  FLASH_DEFINITION               = StandaloneMmPkg/StandaloneMmPkg.fdf
> +  DEFINE DEBUG_MESSAGE           = TRUE
> +
> +  # LzmaF86
> +  DEFINE COMPRESSION_TOOL_GUID   = D42AE6BD-1352-4bfb-909A-CA72A6EAE889
> +
> +################################################################################
> +#
> +# Library Class section - list of all Library Classes needed by this Platform.
> +#
> +################################################################################
> +[LibraryClasses]
> +  #
> +  # Basic
> +  #
> +  BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
> +  BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
> +  DebugLib|MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort.inf
> +  DebugPrintErrorLevelLib|MdePkg/Library/BaseDebugPrintErrorLevelLib/BaseDebugPrintErrorLevelLib.inf
> +  FvLib|StandaloneMmPkg/Library/FvLib/FvLib.inf
> +  IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
> +  MemoryAllocationLib|StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
> +  PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
> +  PeCoffLib|MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf
> +  PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf
> +  ReportStatusCodeLib|MdePkg/Library/BaseReportStatusCodeLibNull/BaseReportStatusCodeLibNull.inf
> +
> +  #
> +  # Entry point
> +  #
> +  StandaloneMmDriverEntryPoint|StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf
> +
> +[LibraryClasses.AARCH64]
> +  ArmLib|ArmPkg/Library/ArmLib/ArmBaseLib.inf
> +  ArmMmuLib|ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
> +  ArmSvcLib|ArmPkg/Library/ArmSvcLib/ArmSvcLib.inf
> +  CacheMaintenanceLib|ArmPkg/Library/ArmCacheMaintenanceLib/ArmCacheMaintenanceLib.inf
> +  HobLib|StandaloneMmPkg/Library/HobLib/HobLib.inf
> +  MemLib|StandaloneMmPkg/Library/MemLib/MemLib.inf
> +  PeCoffExtraActionLib|ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.inf
> +  PL011UartLib|ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.inf
> +  # ARM PL011 UART Driver
> +  SerialPortLib|ArmPlatformPkg/Library/PL011SerialPortLib/PL011SerialPortLib.inf
> +
> +  StandaloneMmCoreEntryPoint|StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
> +
> +################################################################################
> +#
> +# Pcd Section - list of all EDK II PCD Entries defined by this Platform
> +#
> +################################################################################
> +[PcdsFeatureFlag]
> +  gStandaloneMmPkgTokenSpaceGuid.PcdStandaloneMmEnable|TRUE
> +
> +[PcdsFixedAtBuild]
> +  gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x800000CF
> +  gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0xff
> +  gEfiMdePkgTokenSpaceGuid.PcdReportStatusCodePropertyMask|0x0f
> +
> +[PcdsFixedAtBuild.AARCH64]
> +  ## PL011 - Serial Terminal
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialRegisterBase|0x1c0b0000
> +  gEfiMdePkgTokenSpaceGuid.PcdUartDefaultBaudRate|115200
> +
> +###################################################################################################
> +#
> +# Components Section - list of the modules and components that will be processed by compilation
> +#                      tools and the EDK II tools to generate PE32/PE32+/Coff image files.
> +#
> +# Note: The EDK II DSC file is not used to specify how compiled binary images get placed
> +#       into firmware volume images. This section is just a list of modules to compile from
> +#       source into UEFI-compliant binaries.
> +#       It is the FDF file that contains information on combining binary files into firmware
> +#       volume images, whose concept is beyond UEFI and is described in PI specification.
> +#       Binary modules do not need to be listed in this section, as they should be
> +#       specified in the FDF file. For example: Shell binary (Shell_Full.efi), FAT binary (Fat.efi),
> +#       Logo (Logo.bmp), and etc.
> +#       There may also be modules listed in this section that are not required in the FDF file,
> +#       When a module listed here is excluded from FDF file, then UEFI-compliant binary will be
> +#       generated for it, but the binary will not be put into any firmware volume.
> +#
> +###################################################################################################
> +[Components.common]
> +  #
> +  # MM Core
> +  #
> +  StandaloneMmPkg/Core/StandaloneMmCore.inf
> +
> +[Components.AARCH64]
> +  StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> +
> +###################################################################################################
> +#
> +# BuildOptions Section - Define the module specific tool chain flags that should be used as
> +#                        the default flags for a module. These flags are appended to any
> +#                        standard flags that are defined by the build process. They can be
> +#                        applied for any modules or only those modules with the specific
> +#                        module style (EDK or EDKII) specified in [Components] section.
> +#
> +###################################################################################################
> +[BuildOptions.Common]
> +GCC:*_*_*_DLINK_FLAGS = -z common-page-size=0x1000
> diff --git a/StandaloneMmPkg/StandaloneMmPkg.fdf b/StandaloneMmPkg/StandaloneMmPkg.fdf
> new file mode 100644
> index 0000000000..7a22a51b4c
> --- /dev/null
> +++ b/StandaloneMmPkg/StandaloneMmPkg.fdf
> @@ -0,0 +1,184 @@
> +#
> +#  Copyright (c) 2011 - 2017, ARM Limited. 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
> +#
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +
> +################################################################################
> +#
> +# FD Section
> +# The [FD] Section is made up of the definition statements and a
> +# description of what goes into  the Flash Device Image.  Each FD section
> +# defines one flash "device" image.  A flash device image may be one of
> +# the following: Removable media bootable image (like a boot floppy
> +# image,) an Option ROM image (that would be "flashed" into an add-in
> +# card,) a System "Flash"  image (that would be burned into a system's
> +# flash) or an Update ("Capsule") image that will be used to update and
> +# existing system flash.
> +#
> +################################################################################
> +
> +[FD.]
> +!ifdef ARM_FVP_RUN_NORFLASH
> +BaseAddress   = 0x08000000|gArmTokenSpaceGuid.PcdFdBaseAddress  # The base address of the Firmware in Flash0.
> +!else
> +BaseAddress   = 0xff200000|gArmTokenSpaceGuid.PcdFdBaseAddress  # UEFI in DRAM + 128MB.
> +!endif
> +Size          = 0x00e00000|gArmTokenSpaceGuid.PcdFdSize         # The size in bytes of the device (64MiB).
> +ErasePolarity = 1
> +
> +# This one is tricky, it must be: BlockSize * NumBlocks = Size
> +BlockSize     = 0x00001000
> +NumBlocks     = 0x0e00
> +
> +0x00000000|0x00280000
> +gArmTokenSpaceGuid.PcdFvBaseAddress|gArmTokenSpaceGuid.PcdFvSize
> +FV = FVMAIN_COMPACT
> +
> +[FV.FVMAIN_COMPACT]
> +FvAlignment        = 16
> +ERASE_POLARITY     = 1
> +MEMORY_MAPPED      = TRUE
> +STICKY_WRITE       = TRUE
> +LOCK_CAP           = TRUE
> +LOCK_STATUS        = TRUE
> +WRITE_DISABLED_CAP = TRUE
> +WRITE_ENABLED_CAP  = TRUE
> +WRITE_STATUS       = TRUE
> +WRITE_LOCK_CAP     = TRUE
> +WRITE_LOCK_STATUS  = TRUE
> +READ_DISABLED_CAP  = TRUE
> +READ_ENABLED_CAP   = TRUE
> +READ_STATUS        = TRUE
> +READ_LOCK_CAP      = TRUE
> +READ_LOCK_STATUS   = TRUE
> +
> +INF StandaloneMmPkg/Core/StandaloneMmCore.inf
> +INF StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> +
> +################################################################################
> +#
> +# Rules are use with the [FV] section's module INF type to define
> +# how an FFS file is created for a given INF file. The following Rule are the default
> +# rules for the different module type. User can add the customized rules to define the
> +# content of the FFS file.
> +#
> +################################################################################
> +
> +
> +############################################################################
> +# Example of a DXE_DRIVER FFS file with a Checksum encapsulation section   #
> +############################################################################
> +#
> +#[Rule.Common.DXE_DRIVER]
> +#  FILE DRIVER = $(NAMED_GUID) {
> +#    DXE_DEPEX    DXE_DEPEX               Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +#    COMPRESS PI_STD {
> +#      GUIDED {
> +#        PE32     PE32                    $(INF_OUTPUT)/$(MODULE_NAME).efi
> +#        UI       STRING="$(MODULE_NAME)" Optional
> +#        VERSION  STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +#      }
> +#    }
> +#  }
> +#
> +############################################################################
> +
> +[Rule.Common.SEC]
> +  FILE SEC = $(NAMED_GUID) RELOCS_STRIPPED FIXED {
> +    TE  TE Align = Auto                 $(INF_OUTPUT)/$(MODULE_NAME).efi
> +  }
> +
> +[Rule.Common.MM_CORE_STANDALONE]
> +  FILE SEC = $(NAMED_GUID) RELOCS_STRIPPED FIXED {
> +    PE32  PE32 Align = Auto             $(INF_OUTPUT)/$(MODULE_NAME).efi
> +  }
> +
> +[Rule.Common.MM_STANDALONE]
> +  FILE MM_STANDALONE = $(NAMED_GUID) {
> +    SMM_DEPEX SMM_DEPEX Optional       $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32      PE32                     $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI        STRING="$(MODULE_NAME)" Optional
> +    VERSION   STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +  }
> +
> +[Rule.Common.PEI_CORE]
> +  FILE PEI_CORE = $(NAMED_GUID) FIXED {
> +    TE     TE Align = Auto              $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI     STRING ="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.PEIM]
> +  FILE PEIM = $(NAMED_GUID) FIXED {
> +     PEI_DEPEX PEI_DEPEX Optional       $(INF_OUTPUT)/$(MODULE_NAME).depex
> +     TE       TE Align = Auto           $(INF_OUTPUT)/$(MODULE_NAME).efi
> +     UI       STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.PEIM.TIANOCOMPRESSED]
> +  FILE PEIM = $(NAMED_GUID) DEBUG_MYTOOLS_IA32 {
> +    PEI_DEPEX PEI_DEPEX Optional        $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    GUIDED A31280AD-481E-41B6-95E8-127F4C984779 PROCESSING_REQUIRED = TRUE {
> +      PE32      PE32                    $(INF_OUTPUT)/$(MODULE_NAME).efi
> +      UI        STRING="$(MODULE_NAME)" Optional
> +    }
> +  }
> +
> +[Rule.Common.DXE_CORE]
> +  FILE DXE_CORE = $(NAMED_GUID) {
> +    PE32     PE32                       $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI       STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.UEFI_DRIVER]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32         PE32                   $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI           STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.DXE_DRIVER]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32         PE32                   $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI           STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.DXE_RUNTIME_DRIVER]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32         PE32                   $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI           STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.UEFI_APPLICATION]
> +  FILE APPLICATION = $(NAMED_GUID) {
> +    UI     STRING ="$(MODULE_NAME)"     Optional
> +    PE32   PE32                         $(INF_OUTPUT)/$(MODULE_NAME).efi
> +  }
> +
> +[Rule.Common.UEFI_DRIVER.BINARY]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX DXE_DEPEX Optional      |.depex
> +    PE32      PE32                    |.efi
> +    UI        STRING="$(MODULE_NAME)" Optional
> +    VERSION   STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +  }
> +
> +[Rule.Common.UEFI_APPLICATION.BINARY]
> +  FILE APPLICATION = $(NAMED_GUID) {
> +    PE32      PE32                    |.efi
> +    UI        STRING="$(MODULE_NAME)" Optional
> +    VERSION   STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +  }
> +
> +[Rule.Common.USER_DEFINED.ACPITABLE]
> +  FILE FREEFORM = $(NAMED_GUID) {
> +    RAW ASL                |.aml
> +  }
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 12/18] StandaloneMmPkg/CpuMm: Add CPU driver suitable for ARM Platforms.
  2018-04-06 14:42 ` [PATCH v1 12/18] StandaloneMmPkg/CpuMm: Add CPU driver suitable for ARM Platforms Supreeth Venkatesh
@ 2018-04-18 22:09   ` Daniil Egranov
  2018-05-04 23:25     ` Supreeth Venkatesh
  2018-04-30 15:50   ` Achin Gupta
  1 sibling, 1 reply; 70+ messages in thread
From: Daniil Egranov @ 2018-04-18 22:09 UTC (permalink / raw)
  To: Supreeth Venkatesh, edk2-devel
  Cc: ard.biesheuvel, leif.lindholm, jiewen.yao, liming.gao,
	michael.d.kinney

Hi Supreeth,

I am not sure how much it enforced in edk2 but in most of the cases, the 
driver's name matches its directory name.

Thanks,
Daniil

On 04/06/2018 09:42 AM, Supreeth Venkatesh wrote:
> This patch adds a simple CPU driver that exports the
> EFI_MM_CONFIGURATION_PROTOCOL to allow registration of the Standalone
> MM Foundation entry point. It preserves the existing notification
> mechanism for the configuration protocol.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>   StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S          |  33 +++
>   StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c    | 231 +++++++++++++++++++++
>   StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c           | 229 ++++++++++++++++++++
>   .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h        |  89 ++++++++
>   .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf      |  60 ++++++
>   StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c      |  51 +++++
>   StandaloneMmPkg/Include/Guid/MpInformation.h       |  41 ++++
>   7 files changed, 734 insertions(+)
>   create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
>   create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
>   create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
>   create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
>   create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
>   create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
>   create mode 100644 StandaloneMmPkg/Include/Guid/MpInformation.h
> 
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S b/StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
> new file mode 100644
> index 0000000000..0b6e1c330d
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
> @@ -0,0 +1,33 @@
> +//
> +//  Copyright (c) 2017, ARM Limited. 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
> +//
> +//  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +//  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +//
> +//
> +
> +#include <Base.h>
> +#include <AutoGen.h>
> +#include <IndustryStandard/ArmStdSmc.h>
> +
> +.text
> +.align 3
> +
> +GCC_ASM_IMPORT(PiMmStandloneArmTfCpuDriverEntry)
> +GCC_ASM_EXPORT(_PiMmStandloneArmTfCpuDriverEntry)
> +
> +// Stub entry point to ensure that the stacks are completely unwound before
> +// signalling completion of event handling
> +ASM_PFX(_PiMmStandloneArmTfCpuDriverEntry):
> +  bl    PiMmStandloneArmTfCpuDriverEntry
> +  mov   x1, x0
> +  mov   x0, #(ARM_SMC_ID_MM_EVENT_COMPLETE_AARCH64 & 0xffff)
> +  movk  x0, #((ARM_SMC_ID_MM_EVENT_COMPLETE_AARCH64 >> 16) & 0xffff), lsl #16
> +  svc   #0
> +LoopForever:
> +  b     LoopForever
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c b/StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
> new file mode 100644
> index 0000000000..7b19f53ecb
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
> @@ -0,0 +1,231 @@
> +/** @file
> +
> +  Copyright (c) 2016 HP Development Company, L.P.
> +  Copyright (c) 2016 - 2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Base.h>
> +#include <Pi/PiMmCis.h>
> +
> +
> +#include <Library/ArmSvcLib.h>
> +#include <Library/ArmLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/HobLib.h>
> +
> +#include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT
> +
> +#include <Guid/ZeroGuid.h>
> +#include <Guid/MmramMemoryReserve.h>
> +
> +#include <IndustryStandard/ArmStdSmc.h>
> +
> +#include "PiMmStandloneArmTfCpuDriver.h"
> +
> +EFI_STATUS
> +EFIAPI
> +MmFoundationEntryRegister(
> +  IN CONST EFI_MM_CONFIGURATION_PROTOCOL  *This,
> +  IN EFI_MM_ENTRY_POINT                    MmEntryPoint
> +  );
> +
> +//
> +// On ARM platforms every event is expected to have a GUID associated with
> +// it. It will be used by the MM Entry point to find the handler for the
> +// event. It will either be populated in a EFI_MM_COMMUNICATE_HEADER by the
> +// caller of the event (e.g. MM_COMMUNICATE SMC) or by the CPU driver
> +// (e.g. during an asynchronous event). In either case, this context is
> +// maintained in an array which has an entry for each CPU. The pointer to this
> +// array is held in PerCpuGuidedEventContext. Memory is allocated once the
> +// number of CPUs in the system are made known through the
> +// MP_INFORMATION_HOB_DATA.
> +//
> +EFI_MM_COMMUNICATE_HEADER **PerCpuGuidedEventContext = NULL;
> +
> +//
> +// When an event is received by the CPU driver, it could correspond to a unique
> +// GUID (e.g. interrupt events) or to multiple GUIDs (e.g. MM_COMMUNICATE
> +// SMC). A table is used by the CPU driver to find the GUID corresponding to the
> +// event id in case there is a 1:1 mapping between the two. If an event id has
> +// multiple GUIDs associated with it then such an entry will not be found in
> +// this table.
> +//
> +// TODO: Currently NULL since there are no asynchronous events
> +static EFI_GUID *EventIdToGuidLookupTable = NULL;
> +
> +// Descriptor with whereabouts of memory used for communication with the normal world
> +EFI_MMRAM_DESCRIPTOR  mNsCommBuffer;
> +
> +MP_INFORMATION_HOB_DATA *mMpInformationHobData;
> +
> +EFI_MM_CONFIGURATION_PROTOCOL mMmConfig = {
> +  0,
> +  MmFoundationEntryRegister
> +};
> +
> +static EFI_MM_ENTRY_POINT     mMmEntryPoint = NULL;
> +
> +EFI_STATUS
> +PiMmStandloneArmTfCpuDriverEntry (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  )
> +{
> +  EFI_MM_COMMUNICATE_HEADER *GuidedEventContext = NULL;
> +  EFI_MM_ENTRY_CONTEXT        MmEntryPointContext = {0};
> +  EFI_STATUS                  Status;
> +  UINTN                       NsCommBufferSize;
> +
> +  DEBUG ((DEBUG_INFO, "Received event - 0x%x on cpu %d\n", EventId, CpuNumber));
> +
> +  //
> +  // ARM TF passes SMC FID of the MM_COMMUNICATE interface as the Event ID upon
> +  // receipt of a synchronous MM request. Use the Event ID to distinguish
> +  // between synchronous and asynchronous events.
> +  //
> +  if (ARM_SMC_ID_MM_COMMUNICATE_AARCH64 != EventId) {
> +    // Found a GUID, allocate memory to populate a communication buffer
> +    // with the GUID in it
> +    Status = mMmst->MmAllocatePool(EfiRuntimeServicesData, sizeof(EFI_MM_COMMUNICATE_HEADER), (VOID **) &GuidedEventContext);
> +    if (Status != EFI_SUCCESS) {
> +      DEBUG ((DEBUG_INFO, "Mem alloc failed - 0x%x\n", EventId));
> +      return Status;
> +    }
> +
> +    // Copy the GUID
> +    CopyGuid(&GuidedEventContext->HeaderGuid, &EventIdToGuidLookupTable[EventId]);
> +
> +    // Message Length is 0 'cause of the assumption mentioned above
> +    GuidedEventContext->MessageLength = 0;
> +  } else {
> +    // TODO: Perform parameter validation of NsCommBufferAddr
> +
> +    // This event id is the parent of multiple GUIDed handlers. Retrieve
> +    // the specific GUID from the communication buffer passed by the
> +    // caller.
> +
> +    if (NsCommBufferAddr && (NsCommBufferAddr < mNsCommBuffer.PhysicalStart))
> +      return EFI_INVALID_PARAMETER;
> +
> +    // Find out the size of the buffer passed
> +    NsCommBufferSize = ((EFI_MM_COMMUNICATE_HEADER *) NsCommBufferAddr)->MessageLength +
> +                        sizeof(((EFI_MM_COMMUNICATE_HEADER *)NsCommBufferAddr)->MessageLength) +
> +                        sizeof(((EFI_MM_COMMUNICATE_HEADER *)NsCommBufferAddr)->HeaderGuid);
> +
> +    // Alternative approach in case EL3 has preallocated the non-secure
> +    // buffer. MM Foundation is told about the buffer through the Hoblist
> +    // and is responsible for performing the bounds check.
> +    if (NsCommBufferAddr + NsCommBufferSize >=
> +      mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize)
> +        return EFI_INVALID_PARAMETER;
> +
> +
> +    // Now that the secure world can see the normal world buffer, allocate
> +    // memory to copy the communication buffer to the secure world.
> +    Status = mMmst->MmAllocatePool(EfiRuntimeServicesData, NsCommBufferSize, (VOID **) &GuidedEventContext);
> +    if (Status != EFI_SUCCESS) {
> +      DEBUG ((DEBUG_INFO, "Mem alloc failed - 0x%x\n", EventId));
> +      // TODO: Unmap secure memory before exiting to the normal world
> +      return Status;
> +    }
> +
> +    // X1 contains the VA of the normal world memory accessible from
> +    // S-EL0
> +    CopyMem(GuidedEventContext, (CONST VOID *) NsCommBufferAddr, NsCommBufferSize);
> +  }
> +
> +  // Stash the pointer to the allocated Event Context for this CPU
> +  PerCpuGuidedEventContext[CpuNumber] = GuidedEventContext;
> +
> +  // TODO: Populate entire entry point context with valid information
> +  MmEntryPointContext.CurrentlyExecutingCpu = CpuNumber;
> +  MmEntryPointContext.NumberOfCpus = mMpInformationHobData->NumberOfProcessors;
> +
> +  // Populate the MM system table with MP and state information
> +  mMmst->CurrentlyExecutingCpu = CpuNumber;
> +  mMmst->NumberOfCpus = mMpInformationHobData->NumberOfProcessors;
> +  mMmst->CpuSaveStateSize = 0;
> +  mMmst->CpuSaveState = NULL;
> +
> +  mMmEntryPoint(&MmEntryPointContext);
> +
> +  // Free the memory allocation done earlier and reset the per-cpu context
> +  // TODO: Check for the return status of the FreePool API
> +  ASSERT (GuidedEventContext);
> +  CopyMem ((VOID *)NsCommBufferAddr, (CONST VOID *) GuidedEventContext, NsCommBufferSize);
> +  mMmst->MmFreePool((VOID *) GuidedEventContext);
> +  PerCpuGuidedEventContext[CpuNumber] = NULL;
> +
> +  return Status;
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +MmFoundationEntryRegister(
> +  IN CONST EFI_MM_CONFIGURATION_PROTOCOL  *This,
> +  IN EFI_MM_ENTRY_POINT                    MmEntryPoint
> +  ) {
> +  // store the entry point in a global
> +  mMmEntryPoint = MmEntryPoint;
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +PiMmCpuTpFwRootMmiHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_STATUS Status;
> +  UINTN      CpuNumber;
> +
> +  ASSERT (Context == NULL);
> +  ASSERT (CommBuffer == NULL);
> +  ASSERT (CommBufferSize == NULL);
> +
> +  CpuNumber = mMmst->CurrentlyExecutingCpu;
> +  if (!PerCpuGuidedEventContext[CpuNumber])
> +    return EFI_NOT_FOUND;
> +
> +  DEBUG ((DEBUG_INFO, "CommBuffer - 0x%x, CommBufferSize - 0x%x\n",
> +          PerCpuGuidedEventContext[CpuNumber],
> +          PerCpuGuidedEventContext[CpuNumber]->MessageLength));
> +
> +  Status = mMmst->MmiManage(&PerCpuGuidedEventContext[CpuNumber]->HeaderGuid,
> +                     NULL,
> +                     PerCpuGuidedEventContext[CpuNumber]->Data,
> +                     &PerCpuGuidedEventContext[CpuNumber]->MessageLength);
> +
> +  if (Status != EFI_SUCCESS) {
> +    DEBUG ((DEBUG_WARN, "Unable to manage Guided Event - %d\n", Status));
> +  }
> +
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c b/StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
> new file mode 100644
> index 0000000000..9b48ea15c1
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
> @@ -0,0 +1,229 @@
> +/** @file
> +
> +  Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
> +  Copyright (c) 2011, ARM Limited. All rights reserved.
> +  Copyright (c) 2016 HP Development Company, L.P.
> +  Copyright (c) 2016-2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Base.h>
> +#include <Pi/PiMmCis.h>
> +#include <Library/Arm/StandaloneMmCoreEntryPoint.h>
> +#include <Library/DebugLib.h>
> +#include <Library/ArmSvcLib.h>
> +#include <Library/ArmLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/HobLib.h>
> +
> +#include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT
> +
> +#include <Guid/ZeroGuid.h>
> +#include <Guid/MmramMemoryReserve.h>
> +
> +
> +#include "PiMmStandloneArmTfCpuDriver.h"
> +
> +// GUID to identify HOB with whereabouts of communication buffer with Normal
> +// World
> +extern EFI_GUID gEfiStandaloneMmNonSecureBufferGuid;
> +
> +// GUID to identify HOB where the entry point of this CPU driver will be
> +// populated to allow the entry point driver to invoke it upon receipt of an
> +// event
> +extern EFI_GUID gEfiArmTfCpuDriverEpDescriptorGuid;
> +
> +//
> +// Private copy of the MM system table for future use
> +//
> +EFI_MM_SYSTEM_TABLE *mMmst = NULL;
> +
> +//
> +// Globals used to initialize the protocol
> +//
> +static EFI_HANDLE            mMmCpuHandle = NULL;
> +
> +EFI_STATUS
> +GetGuidedHobData (
> +  IN  VOID *HobList,
> +  IN  CONST EFI_GUID *HobGuid,
> +  OUT VOID **HobData)
> +{
> +  EFI_HOB_GUID_TYPE *Hob;
> +
> +  if (!HobList || !HobGuid || !HobData)
> +    return EFI_INVALID_PARAMETER;
> +
> +  Hob = GetNextGuidHob (HobGuid, HobList);
> +  if (!Hob)
> +    return EFI_NOT_FOUND;
> +
> +  *HobData = GET_GUID_HOB_DATA (Hob);
> +  if (!HobData)
> +    return EFI_NOT_FOUND;
> +
> +  return EFI_SUCCESS;
> +}
> +
> +EFI_STATUS
> +PiMmStandloneArmTfCpuDriverInitialize (
> +  IN EFI_HANDLE         ImageHandle,  // not actual imagehandle
> +  IN EFI_MM_SYSTEM_TABLE   *SystemTable  // not actual systemtable
> +  )
> +{
> +  ARM_TF_CPU_DRIVER_EP_DESCRIPTOR *CpuDriverEntryPointDesc;
> +  EFI_CONFIGURATION_TABLE         *ConfigurationTable;
> +  MP_INFORMATION_HOB_DATA         *MpInformationHobData;
> +  EFI_MMRAM_DESCRIPTOR            *NsCommBufMmramRange;
> +  EFI_STATUS                       Status;
> +  EFI_HANDLE                       DispatchHandle;
> +  UINT32                           MpInfoSize;
> +  UINTN                            Index;
> +  UINTN                            ArraySize;
> +  VOID                            *HobStart;
> +
> +  ASSERT (SystemTable != NULL);
> +  mMmst = SystemTable;
> +
> +  // publish the MM config protocol so the MM core can register its entry point
> +  Status = mMmst->MmInstallProtocolInterface (&mMmCpuHandle,
> +                                              &gEfiMmConfigurationProtocolGuid,
> +                                              EFI_NATIVE_INTERFACE,
> +                                              &mMmConfig);
> +  if (EFI_ERROR(Status)) {
> +    return Status;
> +  }
> +
> +  // publish the MM CPU save state protocol
> +  Status = mMmst->MmInstallProtocolInterface (&mMmCpuHandle,
> +    &gEfiMmCpuProtocolGuid, EFI_NATIVE_INTERFACE, &mMmCpuState);
> +  if (EFI_ERROR(Status)) {
> +    return Status;
> +  }
> +
> +  // register the root MMI handler
> +  Status = mMmst->MmiHandlerRegister (PiMmCpuTpFwRootMmiHandler,
> +                                      NULL,
> +                                      &DispatchHandle);
> +  if (EFI_ERROR(Status)) {
> +    return Status;
> +  }
> +
> +  // Retrieve the Hoblist from the MMST to extract the details of the NS
> +  // communication buffer that has been reserved by S-EL1/EL3
> +  ConfigurationTable = mMmst->MmConfigurationTable;
> +  for (Index = 0; Index < mMmst->NumberOfTableEntries; Index++) {
> +    if (CompareGuid (&gEfiHobListGuid, &(ConfigurationTable[Index].VendorGuid))) {
> +      break;
> +    }
> +  }
> +
> +  // Bail out if the Hoblist could not be found
> +  // TODO: This could also mean that
> +  // the normal world will never interact synchronously with the MM environment
> +  if (Index >= mMmst->NumberOfTableEntries) {
> +    DEBUG ((DEBUG_INFO, "Hoblist not found - 0x%x\n", Index));
> +    return EFI_OUT_OF_RESOURCES;
> +  }
> +
> +  HobStart = ConfigurationTable[Index].VendorTable;
> +
> +  //
> +  // Locate the HOB with the buffer to populate the entry point of this driver
> +  //
> +  Status = GetGuidedHobData (
> +            HobStart,
> +            &gEfiArmTfCpuDriverEpDescriptorGuid,
> +            (VOID **) &CpuDriverEntryPointDesc);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_INFO, "ArmTfCpuDriverEpDesc HOB data extraction failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  // Share the entry point of the CPU driver
> +  DEBUG ((DEBUG_INFO, "Sharing Cpu Driver EP *0x%lx = 0x%lx\n",
> +    (UINT64) CpuDriverEntryPointDesc->ArmTfCpuDriverEpPtr,
> +    (UINT64) PiMmStandloneArmTfCpuDriverEntry));
> +  *(CpuDriverEntryPointDesc->ArmTfCpuDriverEpPtr) = PiMmStandloneArmTfCpuDriverEntry;
> +
> +  // Find the descriptor that contains the whereabouts of the buffer for
> +  // communication with the Normal world.
> +  Status = GetGuidedHobData (
> +            HobStart,
> +            &gEfiStandaloneMmNonSecureBufferGuid,
> +            (VOID **) &NsCommBufMmramRange);
> +  if (EFI_ERROR(Status)) {
> +    DEBUG ((DEBUG_INFO, "NsCommBufMmramRange HOB data extraction failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  DEBUG ((DEBUG_INFO, "mNsCommBuffer.PhysicalStart - 0x%lx\n", (UINT64) NsCommBufMmramRange->PhysicalStart));
> +  DEBUG ((DEBUG_INFO, "mNsCommBuffer.PhysicalSize - 0x%lx\n", (UINT64) NsCommBufMmramRange->PhysicalSize));
> +
> +  CopyMem (&mNsCommBuffer, NsCommBufMmramRange, sizeof(EFI_MMRAM_DESCRIPTOR));
> +  DEBUG ((DEBUG_INFO, "mNsCommBuffer: 0x%016lx - 0x%lx\n", mNsCommBuffer.CpuStart, mNsCommBuffer.PhysicalSize));
> +
> +  //
> +  // Extract the MP information from the Hoblist
> +  //
> +  Status = GetGuidedHobData (HobStart,
> +                             &gMpInformationHobGuid,
> +                             (VOID **) &MpInformationHobData);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_INFO, "MpInformationHob extraction failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  //
> +  // Allocate memory for the MP information and copy over the MP information
> +  // passed by Trusted Firmware. Use the number of processors passed in the HOB
> +  // to copy the processor information
> +  //
> +  MpInfoSize = sizeof (MP_INFORMATION_HOB_DATA) +
> +               (sizeof (EFI_PROCESSOR_INFORMATION) *
> +               MpInformationHobData->NumberOfProcessors);
> +  Status = mMmst->MmAllocatePool (EfiRuntimeServicesData,
> +                                  MpInfoSize,
> +                                  (void **) &mMpInformationHobData);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_INFO, "mMpInformationHobData mem alloc failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  CopyMem (mMpInformationHobData, MpInformationHobData, MpInfoSize);
> +
> +  // Print MP information
> +  DEBUG ((DEBUG_INFO, "mMpInformationHobData: 0x%016lx - 0x%lx\n",
> +          mMpInformationHobData->NumberOfProcessors,
> +          mMpInformationHobData->NumberOfEnabledProcessors));
> +  for (Index = 0; Index < mMpInformationHobData->NumberOfProcessors; Index++) {
> +    DEBUG ((DEBUG_INFO, "mMpInformationHobData[0x%lx]: %d, %d, %d\n",
> +            mMpInformationHobData->ProcessorInfoBuffer[Index].ProcessorId,
> +            mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Package,
> +            mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Core,
> +            mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Thread));
> +  }
> +
> +  //
> +  // Allocate memory for a table to hold pointers to a
> +  // EFI_MM_COMMUNICATE_HEADER for each CPU
> +  //
> +  ArraySize = sizeof (EFI_MM_COMMUNICATE_HEADER *) *
> +              mMpInformationHobData->NumberOfEnabledProcessors;
> +  Status = mMmst->MmAllocatePool (EfiRuntimeServicesData,
> +                                  ArraySize,
> +                                  (VOID **) &PerCpuGuidedEventContext);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_INFO, "PerCpuGuidedEventContext mem alloc failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
> new file mode 100644
> index 0000000000..17cefd171c
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
> @@ -0,0 +1,89 @@
> +/** @file
> +  Private header with declarations and definitions specific to the MM Standalone
> +  CPU driver
> +
> +  Copyright (c) 2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _ARM_TF_CPU_DRIVER_H_
> +#define _ARM_TF_CPU_DRIVER_H_
> +
> +#include <Protocol/MmCommunication.h>
> +#include <Protocol/MmConfiguration.h>
> +#include <Protocol/MmCpu.h>
> +#include <Guid/MpInformation.h>
> +
> +//
> +// Common declarations and definitions
> +//
> +#define EVENT_ID_MM_COMMUNICATE_SMC     0x10
> +
> +//
> +// CPU driver initialization specific declarations
> +//
> +extern EFI_MM_SYSTEM_TABLE *mMmst;
> +
> +//
> +// CPU State Save protocol specific declarations
> +//
> +extern EFI_MM_CPU_PROTOCOL mMmCpuState;
> +
> +EFI_STATUS
> +EFIAPI
> +MmReadSaveState (
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  OUT VOID                        *Buffer
> +  );
> +
> +EFI_STATUS
> +EFIAPI
> +MmWriteSaveState (
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  IN CONST VOID                   *Buffer
> +  );
> +
> +//
> +// MM event handling specific declarations
> +//
> +extern EFI_MM_COMMUNICATE_HEADER    **PerCpuGuidedEventContext;
> +extern EFI_MMRAM_DESCRIPTOR          mNsCommBuffer;
> +extern MP_INFORMATION_HOB_DATA       *mMpInformationHobData;
> +extern EFI_MM_CONFIGURATION_PROTOCOL mMmConfig;
> +
> +EFI_STATUS
> +PiMmStandloneArmTfCpuDriverEntry (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  );
> +
> +EFI_STATUS
> +EFIAPI
> +PiMmCpuTpFwRootMmiHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +EFI_STATUS _PiMmStandloneArmTfCpuDriverEntry (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  );
> +
> +#endif
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> new file mode 100644
> index 0000000000..baf6d957bb
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> @@ -0,0 +1,60 @@
> +#/** @file
> +#
> +#  Standalone MM CPU driver for ARM Standard Platforms
> +#
> +#  Copyright (c) 2009, Apple Inc. All rights reserved.<BR>
> +#  Copyright (c) 2016 HP Development Company, L.P.
> +#  Copyright (c) 2017, ARM Limited. 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
> +#
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#**/
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = PiMmStandloneArmTfCpuDriver
> +  FILE_GUID                      = 58F7A62B-6280-42A7-BC38-10535A64A92C
> +  MODULE_TYPE                    = MM_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  ENTRY_POINT                    = PiMmStandloneArmTfCpuDriverInitialize
> +
> +[Sources]
> +  Init.c
> +  EventHandle.c
> +  StateSave.c
> +
> +[Packages]
> +  ArmPkg/ArmPkg.dec
> +  MdePkg/MdePkg.dec
> +  MdeModulePkg/MdeModulePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[LibraryClasses]
> +  ArmLib
> +  ArmSvcLib
> +  BaseMemoryLib
> +  DebugLib
> +  HobLib
> +  StandaloneMmDriverEntryPoint
> +
> +[Protocols]
> +  gEfiMmConfigurationProtocolGuid                        # PROTOCOL ALWAYS_PRODUCED
> +  gEfiMmCpuProtocolGuid                                  # PROTOCOL ALWAYS_PRODUCED
> +
> +[Guids]
> +  gEfiHobListGuid
> +  gEfiMmPeiMmramMemoryReserveGuid
> +  gZeroGuid
> +  gMpInformationHobGuid
> +  gEfiStandaloneMmNonSecureBufferGuid
> +  gEfiArmTfCpuDriverEpDescriptorGuid
> +
> +[Depex]
> +  TRUE
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c b/StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
> new file mode 100644
> index 0000000000..c5155e1b31
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
> @@ -0,0 +1,51 @@
> +/** @file
> +
> +  Copyright (c) 2016 HP Development Company, L.P.
> +  Copyright (c) 2016-2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Base.h>
> +#include <Pi/PiSmmCis.h>
> +#include <Library/DebugLib.h>
> +
> +#include "PiMmStandloneArmTfCpuDriver.h"
> +
> +EFI_MM_CPU_PROTOCOL mMmCpuState = {
> +  MmReadSaveState,
> +  MmWriteSaveState
> +};
> +
> +EFI_STATUS
> +EFIAPI
> +MmReadSaveState(
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  OUT VOID                        *Buffer
> +  ) {
> +  // todo: implement
> +  return EFI_UNSUPPORTED;
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +MmWriteSaveState(
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  IN CONST VOID                   *Buffer
> +  ) {
> +  // todo: implement
> +  return EFI_UNSUPPORTED;
> +}
> diff --git a/StandaloneMmPkg/Include/Guid/MpInformation.h b/StandaloneMmPkg/Include/Guid/MpInformation.h
> new file mode 100644
> index 0000000000..4e9a3c04ec
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Guid/MpInformation.h
> @@ -0,0 +1,41 @@
> +/** @file
> +  EFI MP information protocol provides a lightweight MP_SERVICES_PROTOCOL.
> +
> +  MP information protocol only provides static information of MP processor.
> +
> +  Copyright (c) 2009, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _MP_INFORMATION_H_
> +#define _MP_INFORMATION_H_
> +
> +#include <Protocol/MpService.h>
> +#include <PiPei.h>
> +#include <Ppi/SecPlatformInformation.h>
> +
> +#define MP_INFORMATION_GUID \
> +  { \
> +    0xba33f15d, 0x4000, 0x45c1, {0x8e, 0x88, 0xf9, 0x16, 0x92, 0xd4, 0x57, 0xe3}  \
> +  }
> +
> +#pragma pack(1)
> +typedef struct {
> +  UINT64                     NumberOfProcessors;
> +  UINT64                     NumberOfEnabledProcessors;
> +  EFI_PROCESSOR_INFORMATION  ProcessorInfoBuffer[];
> +} MP_INFORMATION_HOB_DATA;
> +#pragma pack()
> +
> +extern EFI_GUID gMpInformationHobGuid;
> +
> +#endif
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 08/18] StandaloneMmPkg/MemLib: AARCH64 Specific instance of memory check library.
  2018-04-16 22:30     ` Yao, Jiewen
@ 2018-04-25 10:35       ` Achin Gupta
  2018-04-26 13:02         ` Yao, Jiewen
  0 siblings, 1 reply; 70+ messages in thread
From: Achin Gupta @ 2018-04-25 10:35 UTC (permalink / raw)
  To: Yao, Jiewen
  Cc: Supreeth Venkatesh, ard.biesheuvel@linaro.org,
	edk2-devel@lists.01.org, leif.lindholm@linaro.org, Gao, Liming,
	Kinney, Michael D, nd@arm.com

Hi Jiewen,

On Mon, Apr 16, 2018 at 10:30:55PM +0000, Yao, Jiewen wrote:
> Hi
> I don't think this lib is generic, because it hardcode the physical address bits.
> 
> PhysicalAddressBits = 36;
> 
> For X86 CPU, we get it from CPUID. :-)
> 
> As enhancement, we may put most common C-code logic (such as CopyMem, or memmap calculation) to StandaloneMmPkg/MemLib, and only include the PhysicalAddresBit calculation under StandaloneMmPkg/MemLib/Arm folder.
> 
> As such, we know clearly on which one is ARM specific.

My point was that the hardocoded PA bits were not introduced to make this code
work on Arm. This has been present in the StandaloneMmPkg from the outset. I
guess for x86 you have moved on to getting this information from the
CPUID. Afaics, this function is not be used on Arm platforms but Supreeth will
double check. If that is the case then only the generic library will be required
minus this function.

cheers,
Achin

> 
> Thank you
> Yao Jiewen
> 
> > -----Original Message-----
> > From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Achin
> > Gupta
> > Sent: Monday, April 16, 2018 11:13 PM
> > To: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> > Cc: ard.biesheuvel@linaro.org; edk2-devel@lists.01.org;
> > leif.lindholm@linaro.org; Yao, Jiewen <jiewen.yao@intel.com>; Gao, Liming
> > <liming.gao@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>;
> > nd@arm.com
> > Subject: Re: [edk2] [PATCH v1 08/18] StandaloneMmPkg/MemLib: AARCH64
> > Specific instance of memory check library.
> > 
> > Hi Supreeth,
> > 
> > On Fri, Apr 06, 2018 at 03:42:13PM +0100, Supreeth Venkatesh wrote:
> > > MM memory check library library implementation. This library consumes
> > > MM_ACCESS_PROTOCOL to get MMRAM information. In order to use this
> > > library instance, the platform should produce all MMRAM range via
> > > MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core
> > > and MM driver) and/or specific dedicated hardware.
> > >
> > > This patch provides services for MM Memory Operation.
> > > The management mode Mem Library provides function for checking if buffer
> > > is outside MMRAM and valid. It also provides functions for copy data
> > > from MMRAM to non-MMRAM, from non-MMRAM to MMRAM,
> > > from non-MMRAM to non-MMRAM, or set data in non-MMRAM.
> > >
> > > Contributed-under: TianoCore Contribution Agreement 1.1
> > > Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> > > Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> > > ---
> > >  StandaloneMmPkg/Include/Library/MemLib.h    | 140 ++++++++++++++
> > >  StandaloneMmPkg/Library/MemLib/Arm/MemLib.c | 276
> > ++++++++++++++++++++++++++++
> > 
> > Why is this Library Arm specific. Apart from cosmetics tweaks, it has not
> > changed since it was originally contributed?
> > 
> > cheers,
> > Achin
> > 
> > >  StandaloneMmPkg/Library/MemLib/MemLib.inf   |  47 +++++
> > >  3 files changed, 463 insertions(+)
> > >  create mode 100644 StandaloneMmPkg/Include/Library/MemLib.h
> > >  create mode 100644 StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> > >  create mode 100644 StandaloneMmPkg/Library/MemLib/MemLib.inf
> > >
> > > diff --git a/StandaloneMmPkg/Include/Library/MemLib.h
> > b/StandaloneMmPkg/Include/Library/MemLib.h
> > > new file mode 100644
> > > index 0000000000..3264f10010
> > > --- /dev/null
> > > +++ b/StandaloneMmPkg/Include/Library/MemLib.h
> > > @@ -0,0 +1,140 @@
> > > +/** @file
> > > +  Provides services for MM Memory Operation.
> > > +
> > > +  The MM Mem Library provides function for checking if buffer is outside
> > MMRAM and valid.
> > > +  It also provides functions for copy data from MMRAM to non-MMRAM,
> > from non-MMRAM to MMRAM,
> > > +  from non-MMRAM to non-MMRAM, or set data in non-MMRAM.
> > > +
> > > +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > > +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > > +
> > > +  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
> > > +
> > > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> > BASIS,
> > > +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> > EXPRESS OR IMPLIED.
> > > +
> > > +**/
> > > +
> > > +#ifndef _MM_MEM_LIB_H_
> > > +#define _MM_MEM_LIB_H_
> > > +
> > > +/**
> > > +  This function check if the buffer is valid per processor architecture and not
> > overlap with MMRAM.
> > > +
> > > +  @param Buffer  The buffer start address to be checked.
> > > +  @param Length  The buffer length to be checked.
> > > +
> > > +  @retval TRUE  This buffer is valid per processor architecture and not
> > overlap with MMRAM.
> > > +  @retval FALSE This buffer is not valid per processor architecture or overlap
> > with MMRAM.
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +MmIsBufferOutsideMmValid (
> > > +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> > > +  IN UINT64                Length
> > > +  );
> > > +
> > > +/**
> > > +  Copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> > > +
> > > +  This function copies a source buffer (non-MMRAM) to a destination buffer
> > (MMRAM).
> > > +  It checks if source buffer is valid per processor architecture and not overlap
> > with MMRAM.
> > > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > > +  If the check fails, it return EFI_SECURITY_VIOLATION.
> > > +  The implementation must be reentrant.
> > > +
> > > +  @param  DestinationBuffer   The pointer to the destination buffer of the
> > memory copy.
> > > +  @param  SourceBuffer        The pointer to the source buffer of the
> > memory copy.
> > > +  @param  Length              The number of bytes to copy from
> > SourceBuffer to DestinationBuffer.
> > > +
> > > +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per
> > processor architecture or overlap with MMRAM.
> > > +  @retval EFI_SUCCESS            Memory is copied.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +MmCopyMemToSmram (
> > > +  OUT VOID       *DestinationBuffer,
> > > +  IN CONST VOID  *SourceBuffer,
> > > +  IN UINTN       Length
> > > +  );
> > > +
> > > +/**
> > > +  Copies a source buffer (MMRAM) to a destination buffer (NON-MMRAM).
> > > +
> > > +  This function copies a source buffer (non-MMRAM) to a destination buffer
> > (MMRAM).
> > > +  It checks if destination buffer is valid per processor architecture and not
> > overlap with MMRAM.
> > > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > > +  The implementation must be reentrant.
> > > +
> > > +  @param  DestinationBuffer   The pointer to the destination buffer of the
> > memory copy.
> > > +  @param  SourceBuffer        The pointer to the source buffer of the
> > memory copy.
> > > +  @param  Length              The number of bytes to copy from
> > SourceBuffer to DestinationBuffer.
> > > +
> > > +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per
> > processor architecture or overlap with MMRAM.
> > > +  @retval EFI_SUCCESS            Memory is copied.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +MmCopyMemFromSmram (
> > > +  OUT VOID       *DestinationBuffer,
> > > +  IN CONST VOID  *SourceBuffer,
> > > +  IN UINTN       Length
> > > +  );
> > > +
> > > +/**
> > > +  Copies a source buffer (NON-MMRAM) to a destination buffer
> > (NON-MMRAM).
> > > +
> > > +  This function copies a source buffer (non-MMRAM) to a destination buffer
> > (MMRAM).
> > > +  It checks if source buffer and destination buffer are valid per processor
> > architecture and not overlap with MMRAM.
> > > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > > +  The implementation must be reentrant, and it must handle the case where
> > source buffer overlaps destination buffer.
> > > +
> > > +  @param  DestinationBuffer   The pointer to the destination buffer of the
> > memory copy.
> > > +  @param  SourceBuffer        The pointer to the source buffer of the
> > memory copy.
> > > +  @param  Length              The number of bytes to copy from
> > SourceBuffer to DestinationBuffer.
> > > +
> > > +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per
> > processor architecture or overlap with MMRAM.
> > > +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per
> > processor architecture or overlap with MMRAM.
> > > +  @retval EFI_SUCCESS            Memory is copied.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +MmCopyMem (
> > > +  OUT VOID       *DestinationBuffer,
> > > +  IN CONST VOID  *SourceBuffer,
> > > +  IN UINTN       Length
> > > +  );
> > > +
> > > +/**
> > > +  Fills a target buffer (NON-MMRAM) with a byte value.
> > > +
> > > +  This function fills a target buffer (non-MMRAM) with a byte value.
> > > +  It checks if target buffer is valid per processor architecture and not overlap
> > with MMRAM.
> > > +  If the check passes, it fills memory and returns EFI_SUCCESS.
> > > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > > +
> > > +  @param  Buffer    The memory to set.
> > > +  @param  Length    The number of bytes to set.
> > > +  @param  Value     The value with which to fill Length bytes of Buffer.
> > > +
> > > +  @retval EFI_SECURITY_VIOLATION The Buffer is invalid per processor
> > architecture or overlap with MMRAM.
> > > +  @retval EFI_SUCCESS            Memory is set.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +MmSetMem (
> > > +  OUT VOID  *Buffer,
> > > +  IN UINTN  Length,
> > > +  IN UINT8  Value
> > > +  );
> > > +
> > > +#endif
> > > diff --git a/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> > b/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> > > new file mode 100644
> > > index 0000000000..432a45698b
> > > --- /dev/null
> > > +++ b/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> > > @@ -0,0 +1,276 @@
> > > +/** @file
> > > +  Instance of MM memory check library.
> > > +
> > > +  MM memory check library library implementation. This library consumes
> > MM_ACCESS_PROTOCOL
> > > +  to get MMRAM information. In order to use this library instance, the
> > platform should produce
> > > +  all MMRAM range via MM_ACCESS_PROTOCOL, including the range for
> > firmware (like MM Core
> > > +  and MM driver) and/or specific dedicated hardware.
> > > +
> > > +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > > +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > > +
> > > +  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
> > > +
> > > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> > BASIS,
> > > +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> > EXPRESS OR IMPLIED.
> > > +
> > > +**/
> > > +
> > > +
> > > +#include <PiMm.h>
> > > +
> > > +#include <Library/BaseLib.h>
> > > +#include <Library/BaseMemoryLib.h>
> > > +#include <Library/DebugLib.h>
> > > +
> > > +EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges;
> > > +UINTN                mMmMemLibInternalMmramCount;
> > > +
> > > +//
> > > +// Maximum support address used to check input buffer
> > > +//
> > > +EFI_PHYSICAL_ADDRESS  mMmMemLibInternalMaximumSupportAddress =
> > 0;
> > > +
> > > +/**
> > > +  Calculate and save the maximum support address.
> > > +
> > > +**/
> > > +VOID
> > > +MmMemLibInternalCalculateMaximumSupportAddress (
> > > +  VOID
> > > +  )
> > > +{
> > > +  UINT8        PhysicalAddressBits;
> > > +
> > > +  PhysicalAddressBits = 36;
> > > +
> > > +  //
> > > +  // Save the maximum support address in one global variable
> > > +  //
> > > +  mMmMemLibInternalMaximumSupportAddress =
> > (EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, PhysicalAddressBits) - 1);
> > > +  DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress =
> > 0x%lx\n", mMmMemLibInternalMaximumSupportAddress));
> > > +}
> > > +
> > > +/**
> > > +  This function check if the buffer is valid per processor architecture and not
> > overlap with MMRAM.
> > > +
> > > +  @param Buffer  The buffer start address to be checked.
> > > +  @param Length  The buffer length to be checked.
> > > +
> > > +  @retval TRUE  This buffer is valid per processor architecture and not
> > overlap with MMRAM.
> > > +  @retval FALSE This buffer is not valid per processor architecture or overlap
> > with MMRAM.
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +MmIsBufferOutsideMmValid (
> > > +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> > > +  IN UINT64                Length
> > > +  )
> > > +{
> > > +  UINTN  Index;
> > > +
> > > +  //
> > > +  // Check override.
> > > +  // NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1) is
> > valid.
> > > +  //
> > > +  if ((Length > mMmMemLibInternalMaximumSupportAddress) ||
> > > +      (Buffer > mMmMemLibInternalMaximumSupportAddress) ||
> > > +      ((Length != 0) && (Buffer >
> > (mMmMemLibInternalMaximumSupportAddress - (Length - 1)))) ) {
> > > +    //
> > > +    // Overflow happen
> > > +    //
> > > +    DEBUG ((
> > > +      DEBUG_ERROR,
> > > +      "MmIsBufferOutsideMmValid: Overflow: Buffer (0x%lx) - Length
> > (0x%lx), MaximumSupportAddress (0x%lx)\n",
> > > +      Buffer,
> > > +      Length,
> > > +      mMmMemLibInternalMaximumSupportAddress
> > > +      ));
> > > +    return FALSE;
> > > +  }
> > > +
> > > +  for (Index = 0; Index < mMmMemLibInternalMmramCount; Index ++) {
> > > +    if (((Buffer >= mMmMemLibInternalMmramRanges[Index].CpuStart) &&
> > (Buffer < mMmMemLibInternalMmramRanges[Index].CpuStart +
> > mMmMemLibInternalMmramRanges[Index].PhysicalSize)) ||
> > > +        ((mMmMemLibInternalMmramRanges[Index].CpuStart >= Buffer)
> > && (mMmMemLibInternalMmramRanges[Index].CpuStart < Buffer + Length))) {
> > > +      DEBUG ((
> > > +        DEBUG_ERROR,
> > > +        "MmIsBufferOutsideMmValid: Overlap: Buffer (0x%lx) - Length
> > (0x%lx), ",
> > > +        Buffer,
> > > +        Length
> > > +        ));
> > > +      DEBUG ((
> > > +        DEBUG_ERROR,
> > > +        "CpuStart (0x%lx) - PhysicalSize (0x%lx)\n",
> > > +        mMmMemLibInternalMmramRanges[Index].CpuStart,
> > > +        mMmMemLibInternalMmramRanges[Index].PhysicalSize
> > > +        ));
> > > +      return FALSE;
> > > +    }
> > > +  }
> > > +
> > > +  return TRUE;
> > > +}
> > > +
> > > +/**
> > > +  Copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> > > +
> > > +  This function copies a source buffer (non-MMRAM) to a destination buffer
> > (MMRAM).
> > > +  It checks if source buffer is valid per processor architecture and not overlap
> > with MMRAM.
> > > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > > +  If the check fails, it return EFI_SECURITY_VIOLATION.
> > > +  The implementation must be reentrant.
> > > +
> > > +  @param  DestinationBuffer   The pointer to the destination buffer of the
> > memory copy.
> > > +  @param  SourceBuffer        The pointer to the source buffer of the
> > memory copy.
> > > +  @param  Length              The number of bytes to copy from
> > SourceBuffer to DestinationBuffer.
> > > +
> > > +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per
> > processor architecture or overlap with MMRAM.
> > > +  @retval EFI_SUCCESS            Memory is copied.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +MmCopyMemToMmram (
> > > +  OUT VOID       *DestinationBuffer,
> > > +  IN CONST VOID  *SourceBuffer,
> > > +  IN UINTN       Length
> > > +  )
> > > +{
> > > +  if (!MmIsBufferOutsideMmValid
> > ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {
> > > +    DEBUG ((DEBUG_ERROR, "MmCopyMemToMmram: Security Violation:
> > Source (0x%x), Length (0x%x)\n", SourceBuffer, Length));
> > > +    return EFI_SECURITY_VIOLATION;
> > > +  }
> > > +  CopyMem (DestinationBuffer, SourceBuffer, Length);
> > > +  return EFI_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Copies a source buffer (MMRAM) to a destination buffer (NON-MMRAM).
> > > +
> > > +  This function copies a source buffer (non-MMRAM) to a destination buffer
> > (MMRAM).
> > > +  It checks if destination buffer is valid per processor architecture and not
> > overlap with MMRAM.
> > > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > > +  The implementation must be reentrant.
> > > +
> > > +  @param  DestinationBuffer   The pointer to the destination buffer of the
> > memory copy.
> > > +  @param  SourceBuffer        The pointer to the source buffer of the
> > memory copy.
> > > +  @param  Length              The number of bytes to copy from
> > SourceBuffer to DestinationBuffer.
> > > +
> > > +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per
> > processor architecture or overlap with MMRAM.
> > > +  @retval EFI_SUCCESS            Memory is copied.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +MmCopyMemFromMmram (
> > > +  OUT VOID       *DestinationBuffer,
> > > +  IN CONST VOID  *SourceBuffer,
> > > +  IN UINTN       Length
> > > +  )
> > > +{
> > > +  if (!MmIsBufferOutsideMmValid
> > ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {
> > > +    DEBUG ((DEBUG_ERROR, "MmCopyMemFromMmram: Security
> > Violation: Destination (0x%x), Length (0x%x)\n", DestinationBuffer, Length));
> > > +    return EFI_SECURITY_VIOLATION;
> > > +  }
> > > +  CopyMem (DestinationBuffer, SourceBuffer, Length);
> > > +  return EFI_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Copies a source buffer (NON-MMRAM) to a destination buffer
> > (NON-MMRAM).
> > > +
> > > +  This function copies a source buffer (non-MMRAM) to a destination buffer
> > (MMRAM).
> > > +  It checks if source buffer and destination buffer are valid per processor
> > architecture and not overlap with MMRAM.
> > > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > > +  The implementation must be reentrant, and it must handle the case where
> > source buffer overlaps destination buffer.
> > > +
> > > +  @param  DestinationBuffer   The pointer to the destination buffer of the
> > memory copy.
> > > +  @param  SourceBuffer        The pointer to the source buffer of the
> > memory copy.
> > > +  @param  Length              The number of bytes to copy from
> > SourceBuffer to DestinationBuffer.
> > > +
> > > +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per
> > processor architecture or overlap with MMRAM.
> > > +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per
> > processor architecture or overlap with MMRAM.
> > > +  @retval EFI_SUCCESS            Memory is copied.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +MmCopyMem (
> > > +  OUT VOID       *DestinationBuffer,
> > > +  IN CONST VOID  *SourceBuffer,
> > > +  IN UINTN       Length
> > > +  )
> > > +{
> > > +  if (!MmIsBufferOutsideMmValid
> > ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {
> > > +    DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation: Destination
> > (0x%x), Length (0x%x)\n", DestinationBuffer, Length));
> > > +    return EFI_SECURITY_VIOLATION;
> > > +  }
> > > +  if (!MmIsBufferOutsideMmValid
> > ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {
> > > +    DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation: Source
> > (0x%x), Length (0x%x)\n", SourceBuffer, Length));
> > > +    return EFI_SECURITY_VIOLATION;
> > > +  }
> > > +  CopyMem (DestinationBuffer, SourceBuffer, Length);
> > > +  return EFI_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Fills a target buffer (NON-MMRAM) with a byte value.
> > > +
> > > +  This function fills a target buffer (non-MMRAM) with a byte value.
> > > +  It checks if target buffer is valid per processor architecture and not overlap
> > with MMRAM.
> > > +  If the check passes, it fills memory and returns EFI_SUCCESS.
> > > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > > +
> > > +  @param  Buffer    The memory to set.
> > > +  @param  Length    The number of bytes to set.
> > > +  @param  Value     The value with which to fill Length bytes of Buffer.
> > > +
> > > +  @retval EFI_SECURITY_VIOLATION The Buffer is invalid per processor
> > architecture or overlap with MMRAM.
> > > +  @retval EFI_SUCCESS            Memory is set.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +MmSetMem (
> > > +  OUT VOID  *Buffer,
> > > +  IN UINTN  Length,
> > > +  IN UINT8  Value
> > > +  )
> > > +{
> > > +  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer,
> > Length)) {
> > > +    DEBUG ((DEBUG_ERROR, "MmSetMem: Security Violation: Source
> > (0x%x), Length (0x%x)\n", Buffer, Length));
> > > +    return EFI_SECURITY_VIOLATION;
> > > +  }
> > > +  SetMem (Buffer, Length, Value);
> > > +  return EFI_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  The constructor function initializes the Mm Mem library
> > > +
> > > +  @param  ImageHandle   The firmware allocated handle for the EFI
> > image.
> > > +  @param  SystemTable   A pointer to the EFI System Table.
> > > +
> > > +  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +MemLibConstructor (
> > > +  IN EFI_HANDLE             ImageHandle,
> > > +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> > > +  )
> > > +{
> > > +
> > > +  //
> > > +  // Calculate and save maximum support address
> > > +  //
> > > +  MmMemLibInternalCalculateMaximumSupportAddress ();
> > > +
> > > +  return EFI_SUCCESS;
> > > +}
> > > diff --git a/StandaloneMmPkg/Library/MemLib/MemLib.inf
> > b/StandaloneMmPkg/Library/MemLib/MemLib.inf
> > > new file mode 100644
> > > index 0000000000..52b7c06397
> > > --- /dev/null
> > > +++ b/StandaloneMmPkg/Library/MemLib/MemLib.inf
> > > @@ -0,0 +1,47 @@
> > > +## @file
> > > +#  Instance of MM memory check library.
> > > +#
> > > +#  MM memory check library library implementation. This library consumes
> > MM_ACCESS_PROTOCOL
> > > +#  to get MMRAM information. In order to use this library instance, the
> > platform should produce
> > > +#  all MMRAM range via MM_ACCESS_PROTOCOL, including the range for
> > firmware (like MM Core
> > > +#  and MM driver) and/or specific dedicated hardware.
> > > +#
> > > +#  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > > +#  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > > +#
> > > +#  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
> > > +#
> > > +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> > BASIS,
> > > +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> > EXPRESS OR IMPLIED.
> > > +#
> > > +##
> > > +
> > > +[Defines]
> > > +  INF_VERSION                    = 0x0001001A
> > > +  BASE_NAME                      = MemLib
> > > +  FILE_GUID                      =
> > EA355F14-6409-4716-829F-37B3BC7C7F26
> > > +  MODULE_TYPE                    = MM_STANDALONE
> > > +  VERSION_STRING                 = 1.0
> > > +  PI_SPECIFICATION_VERSION       = 0x00010032
> > > +  LIBRARY_CLASS                  = MemLib|MM_STANDALONE
> > MM_CORE_STANDALONE
> > > +  CONSTRUCTOR                    = MemLibConstructor
> > > +
> > > +#
> > > +# The following information is for reference only and not required by the
> > build tools.
> > > +#
> > > +#  VALID_ARCHITECTURES           = AARCH64
> > > +#
> > > +
> > > +[Sources.AARCH64]
> > > +  Arm/MemLib.c
> > > +
> > > +[Packages]
> > > +  MdePkg/MdePkg.dec
> > > +  StandaloneMmPkg/StandaloneMmPkg.dec
> > > +
> > > +[LibraryClasses]
> > > +  BaseMemoryLib
> > > +  DebugLib
> > > --
> > > 2.16.2
> > >
> > _______________________________________________
> > edk2-devel mailing list
> > edk2-devel@lists.01.org
> > https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 09/18] StandaloneMmPkg/MemoryAllocationLib: Add MM memory allocation library.
  2018-04-06 14:42 ` [PATCH v1 09/18] StandaloneMmPkg/MemoryAllocationLib: Add MM memory allocation library Supreeth Venkatesh
@ 2018-04-25 14:33   ` Achin Gupta
  2018-04-26 13:05     ` Yao, Jiewen
  2018-05-04 23:21     ` Supreeth Venkatesh
  0 siblings, 2 replies; 70+ messages in thread
From: Achin Gupta @ 2018-04-25 14:33 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

Hi Supreeth,

On Fri, Apr 06, 2018 at 03:42:14PM +0100, Supreeth Venkatesh wrote:
> This patch implements management mode memory allocation services.The
> implementation borrows the MM Core Memory Allocation services as the
> primitive for memory allocation instead of using MM System Table
> services.

The commit message did not really register with me. Once the MMRAM ranges have
been conveyed to the MMST memory allocation services (through
MemoryAllocationLibConstructor()), all functions in MemoryAllocationLib.c use
the MMST services. The message seems to indicate otherwise.

On Arm, the gEfiMmPeiMmramMemoryReserveGuid HOB is used to convey the MMRAM
ranges. It seems x86 uses gMmCoreDataHobGuid HOB. So it worth getting this
reviewed by Jiewen.

The copyright years in the files need to be updated.

With that in mind..

Acked-by: Achin Gupta <achin.gupta@arm.com>

> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  StandaloneMmPkg/Include/Guid/MmCoreData.h          | 132 +++
>  StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h  |  62 ++
>  .../MemoryAllocationLib/MemoryAllocationLib.c      | 907 +++++++++++++++++++++
>  .../MemoryAllocationLib/MemoryAllocationLib.inf    |  49 ++
>  .../MemoryAllocationLib/MemoryAllocationServices.h |  38 +
>  5 files changed, 1188 insertions(+)
>  create mode 100644 StandaloneMmPkg/Include/Guid/MmCoreData.h
>  create mode 100644 StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
>  create mode 100644 StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
>  create mode 100644 StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
>  create mode 100644 StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.h
> 
> diff --git a/StandaloneMmPkg/Include/Guid/MmCoreData.h b/StandaloneMmPkg/Include/Guid/MmCoreData.h
> new file mode 100644
> index 0000000000..c0ac772014
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Guid/MmCoreData.h
> @@ -0,0 +1,132 @@
> +/** @file
> +  MM Core data.
> +
> +Copyright (c) 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 that accompanies this distribution.
> +The full text of the license may be found at
> +http://opensource.org/licenses/bsd-license.php.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef __MM_CORE_DATA_H__
> +#define __MM_CORE_DATA_H__
> +
> +#define MM_CORE_DATA_HOB_GUID \
> +  { 0xa160bf99, 0x2aa4, 0x4d7d, { 0x99, 0x93, 0x89, 0x9c, 0xb1, 0x2d, 0xf3, 0x76 }}
> +
> +extern EFI_GUID gMmCoreDataHobGuid;
> +
> +typedef struct {
> +  //
> +  // Address pointer to MM_CORE_PRIVATE_DATA
> +  //
> +  EFI_PHYSICAL_ADDRESS   Address;
> +} MM_CORE_DATA_HOB_DATA;
> +
> +
> +///
> +/// Define values for the communications buffer used when gEfiEventDxeDispatchGuid is
> +/// event signaled.  This event is signaled by the DXE Core each time the DXE Core
> +/// dispatcher has completed its work.  When this event is signaled, the MM Core
> +/// if notified, so the MM Core can dispatch MM drivers.  If COMM_BUFFER_MM_DISPATCH_ERROR
> +/// is returned in the communication buffer, then an error occurred dispatching MM
> +/// Drivers.  If COMM_BUFFER_MM_DISPATCH_SUCCESS is returned, then the MM Core
> +/// dispatched all the drivers it could.  If COMM_BUFFER_MM_DISPATCH_RESTART is
> +/// returned, then the MM Core just dispatched the MM Driver that registered
> +/// the MM Entry Point enabling the use of MM Mode.  In this case, the MM Core
> +/// should be notified again to dispatch more MM Drivers using MM Mode.
> +///
> +#define COMM_BUFFER_MM_DISPATCH_ERROR    0x00
> +#define COMM_BUFFER_MM_DISPATCH_SUCCESS  0x01
> +#define COMM_BUFFER_MM_DISPATCH_RESTART  0x02
> +
> +///
> +/// Signature for the private structure shared between the MM IPL and the MM Core
> +///
> +#define MM_CORE_PRIVATE_DATA_SIGNATURE  SIGNATURE_32 ('m', 'm', 'i', 'c')
> +
> +///
> +/// Private structure that is used to share information between the MM IPL and
> +/// the MM Core.  This structure is allocated from memory of type EfiRuntimeServicesData.
> +/// Since runtime memory types are converted to available memory when a legacy boot
> +/// is performed, the MM Core must not access any fields of this structure if a legacy
> +/// boot is performed.  As a result, the MM IPL must create an event notification
> +/// for the Legacy Boot event and notify the MM Core that a legacy boot is being
> +/// performed.  The MM Core can then use this information to filter accesses to
> +/// thos structure.
> +///
> +typedef struct {
> +  UINT64                          Signature;
> +
> +  ///
> +  /// The number of MMRAM ranges passed from the MM IPL to the MM Core.  The MM
> +  /// Core uses these ranges of MMRAM to initialize the MM Core memory manager.
> +  ///
> +  UINT64                          MmramRangeCount;
> +
> +  ///
> +  /// A table of MMRAM ranges passed from the MM IPL to the MM Core.  The MM
> +  /// Core uses these ranges of MMRAM to initialize the MM Core memory manager.
> +  ///
> +  EFI_PHYSICAL_ADDRESS            MmramRanges;
> +
> +  ///
> +  /// The MM Foundation Entry Point.  The MM Core fills in this field when the
> +  /// MM Core is initialized.  The MM IPL is responsbile for registering this entry
> +  /// point with the MM Configuration Protocol.  The MM Configuration Protocol may
> +  /// not be available at the time the MM IPL and MM Core are started, so the MM IPL
> +  /// sets up a protocol notification on the MM Configuration Protocol and registers
> +  /// the MM Foundation Entry Point as soon as the MM Configuration Protocol is
> +  /// available.
> +  ///
> +  EFI_PHYSICAL_ADDRESS            MmEntryPoint;
> +
> +  ///
> +  /// Boolean flag set to TRUE while an MMI is being processed by the MM Core.
> +  ///
> +  BOOLEAN                         MmEntryPointRegistered;
> +
> +  ///
> +  /// Boolean flag set to TRUE while an MMI is being processed by the MM Core.
> +  ///
> +  BOOLEAN                         InMm;
> +
> +  ///
> +  /// This field is set by the MM Core then the MM Core is initialized.  This field is
> +  /// used by the MM Base 2 Protocol and MM Communication Protocol implementations in
> +  /// the MM IPL.
> +  ///
> +  EFI_PHYSICAL_ADDRESS            Mmst;
> +
> +  ///
> +  /// This field is used by the MM Communicatioon Protocol to pass a buffer into
> +  /// a software MMI handler and for the software MMI handler to pass a buffer back to
> +  /// the caller of the MM Communication Protocol.
> +  ///
> +  EFI_PHYSICAL_ADDRESS            CommunicationBuffer;
> +
> +  ///
> +  /// This field is used by the MM Communicatioon Protocol to pass the size of a buffer,
> +  /// in bytes, into a software MMI handler and for the software MMI handler to pass the
> +  /// size, in bytes, of a buffer back to the caller of the MM Communication Protocol.
> +  ///
> +  UINT64                          BufferSize;
> +
> +  ///
> +  /// This field is used by the MM Communication Protocol to pass the return status from
> +  /// a software MMI handler back to the caller of the MM Communication Protocol.
> +  ///
> +  UINT64                          ReturnStatus;
> +
> +  EFI_PHYSICAL_ADDRESS            MmCoreImageBase;
> +  UINT64                          MmCoreImageSize;
> +  EFI_PHYSICAL_ADDRESS            MmCoreEntryPoint;
> +
> +  EFI_PHYSICAL_ADDRESS            StandaloneBfvAddress;
> +} MM_CORE_PRIVATE_DATA;
> +
> +#endif
> diff --git a/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h b/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
> new file mode 100644
> index 0000000000..c4104b755d
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
> @@ -0,0 +1,62 @@
> +/** @file
> +  Definition of GUIDed HOB for reserving MMRAM regions.
> +
> +  This file defines:
> +  * the GUID used to identify the GUID HOB for reserving MMRAM regions.
> +  * the data structure of MMRAM descriptor to describe MMRAM candidate regions
> +  * values of state of MMRAM candidate regions
> +  * the GUID specific data structure of HOB for reserving MMRAM regions.
> +  This GUIDed HOB can be used to convey the existence of the T-SEG reservation and H-SEG usage
> +
> +Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +This program and the accompanying materials are licensed and made available under
> +the terms and conditions of the BSD License that accompanies this distribution.
> +The full text of the license may be found at
> +http://opensource.org/licenses/bsd-license.php.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +  @par Revision Reference:
> +  GUIDs defined in MmCis spec version 0.9.
> +
> +**/
> +
> +#ifndef _EFI_MM_PEI_MMRAM_MEMORY_RESERVE_H_
> +#define _EFI_MM_PEI_MMRAM_MEMORY_RESERVE_H_
> +
> +#define EFI_MM_PEI_MMRAM_MEMORY_RESERVE \
> +  { \
> +    0x0703f912, 0xbf8d, 0x4e2a, {0xbe, 0x07, 0xab, 0x27, 0x25, 0x25, 0xc5, 0x92 } \
> +  }
> +
> +/**
> +* GUID specific data structure of HOB for reserving MMRAM regions.
> +*
> +* Inconsistent with specification here:
> +* EFI_HOB_MMRAM_DESCRIPTOR_BLOCK has been changed to EFI_MMRAM_HOB_DESCRIPTOR_BLOCK.
> +* This inconsistency is kept in code in order for backward compatibility.
> +**/
> +typedef struct {
> +  ///
> +  /// Designates the number of possible regions in the system
> +  /// that can be usable for MMRAM.
> +  ///
> +  /// Inconsistent with specification here:
> +  /// In Framework MM CIS 0.91 specification, it defines the field type as UINTN.
> +  /// However, HOBs are supposed to be CPU neutral, so UINT32 should be used instead.
> +  ///
> +  UINT32                NumberOfMmReservedRegions;
> +  ///
> +  /// Used throughout this protocol to describe the candidate
> +  /// regions for MMRAM that are supported by this platform.
> +  ///
> +  EFI_MMRAM_DESCRIPTOR  Descriptor[1];
> +} EFI_MMRAM_HOB_DESCRIPTOR_BLOCK;
> +
> +extern EFI_GUID gEfiMmPeiSmramMemoryReserveGuid;
> +
> +#endif
> +
> diff --git a/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
> new file mode 100644
> index 0000000000..c177a8f538
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
> @@ -0,0 +1,907 @@
> +/** @file
> +  Support routines for memory allocation routines based on Standalone MM Core internal functions.
> +
> +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <PiMm.h>
> +
> +#include <Guid/MmramMemoryReserve.h>
> +#include <Library/MemoryAllocationLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/HobLib.h>
> +#include "MemoryAllocationServices.h"
> +
> +EFI_MM_SYSTEM_TABLE   *gMmst = NULL;
> +
> +/**
> +  Allocates one or more 4KB pages of a certain memory type.
> +
> +  Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
> +  buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0, then NULL is returned.
> +  If there is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  @param  MemoryType            The type of memory to allocate.
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +InternalAllocatePages (
> +  IN EFI_MEMORY_TYPE  MemoryType,
> +  IN UINTN            Pages
> +  )
> +{
> +  EFI_STATUS            Status;
> +  EFI_PHYSICAL_ADDRESS  Memory;
> +
> +  if (Pages == 0) {
> +    return NULL;
> +  }
> +
> +  Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);
> +  if (EFI_ERROR (Status)) {
> +    return NULL;
> +  }
> +  return (VOID *) (UINTN) Memory;
> +}
> +
> +/**
> +  Allocates one or more 4KB pages of type EfiBootServicesData.
> +
> +  Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
> +  allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0, then NULL
> +  is returned.  If there is not enough memory remaining to satisfy the request, then NULL is
> +  returned.
> +
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocatePages (
> +  IN UINTN  Pages
> +  )
> +{
> +  return InternalAllocatePages (EfiRuntimeServicesData, Pages);
> +}
> +
> +/**
> +  Allocates one or more 4KB pages of type EfiRuntimeServicesData.
> +
> +  Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
> +  allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0, then NULL
> +  is returned.  If there is not enough memory remaining to satisfy the request, then NULL is
> +  returned.
> +
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateRuntimePages (
> +  IN UINTN  Pages
> +  )
> +{
> +  return InternalAllocatePages (EfiRuntimeServicesData, Pages);
> +}
> +
> +/**
> +  Allocates one or more 4KB pages of type EfiReservedMemoryType.
> +
> +  Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
> +  allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0, then NULL
> +  is returned.  If there is not enough memory remaining to satisfy the request, then NULL is
> +  returned.
> +
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateReservedPages (
> +  IN UINTN  Pages
> +  )
> +{
> +  return NULL;
> +}
> +
> +/**
> +  Frees one or more 4KB pages that were previously allocated with one of the page allocation
> +  functions in the Memory Allocation Library.
> +
> +  Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer.  Buffer
> +  must have been allocated on a previous call to the page allocation services of the Memory
> +  Allocation Library.  If it is not possible to free allocated pages, then this function will
> +  perform no actions.
> +
> +  If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
> +  then ASSERT().
> +  If Pages is zero, then ASSERT().
> +
> +  @param  Buffer                Pointer to the buffer of pages to free.
> +  @param  Pages                 The number of 4 KB pages to free.
> +
> +**/
> +VOID
> +EFIAPI
> +FreePages (
> +  IN VOID   *Buffer,
> +  IN UINTN  Pages
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  ASSERT (Pages != 0);
> +  Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);
> +  ASSERT_EFI_ERROR (Status);
> +}
> +
> +/**
> +  Allocates one or more 4KB pages of a certain memory type at a specified alignment.
> +
> +  Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
> +  specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is returned.
> +  If there is not enough memory at the specified alignment remaining to satisfy the request, then
> +  NULL is returned.
> +  If Alignment is not a power of two and Alignment is not zero, then ASSERT().
> +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> +
> +  @param  MemoryType            The type of memory to allocate.
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.
> +                                If Alignment is zero, then byte alignment is used.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +InternalAllocateAlignedPages (
> +  IN EFI_MEMORY_TYPE  MemoryType,
> +  IN UINTN            Pages,
> +  IN UINTN            Alignment
> +  )
> +{
> +  EFI_STATUS            Status;
> +  EFI_PHYSICAL_ADDRESS  Memory;
> +  UINTN                 AlignedMemory;
> +  UINTN                 AlignmentMask;
> +  UINTN                 UnalignedPages;
> +  UINTN                 RealPages;
> +
> +  //
> +  // Alignment must be a power of two or zero.
> +  //
> +  ASSERT ((Alignment & (Alignment - 1)) == 0);
> +
> +  if (Pages == 0) {
> +    return NULL;
> +  }
> +  if (Alignment > EFI_PAGE_SIZE) {
> +    //
> +    // Calculate the total number of pages since alignment is larger than page size.
> +    //
> +    AlignmentMask  = Alignment - 1;
> +    RealPages      = Pages + EFI_SIZE_TO_PAGES (Alignment);
> +    //
> +    // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
> +    //
> +    ASSERT (RealPages > Pages);
> +
> +    Status         = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);
> +    if (EFI_ERROR (Status)) {
> +      return NULL;
> +    }
> +    AlignedMemory  = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;
> +    UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);
> +    if (UnalignedPages > 0) {
> +      //
> +      // Free first unaligned page(s).
> +      //
> +      Status = gMmst->MmFreePages (Memory, UnalignedPages);
> +      ASSERT_EFI_ERROR (Status);
> +    }
> +    Memory         = (EFI_PHYSICAL_ADDRESS) (AlignedMemory + EFI_PAGES_TO_SIZE (Pages));
> +    UnalignedPages = RealPages - Pages - UnalignedPages;
> +    if (UnalignedPages > 0) {
> +      //
> +      // Free last unaligned page(s).
> +      //
> +      Status = gMmst->MmFreePages (Memory, UnalignedPages);
> +      ASSERT_EFI_ERROR (Status);
> +    }
> +  } else {
> +    //
> +    // Do not over-allocate pages in this case.
> +    //
> +    Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);
> +    if (EFI_ERROR (Status)) {
> +      return NULL;
> +    }
> +    AlignedMemory  = (UINTN) Memory;
> +  }
> +  return (VOID *) AlignedMemory;
> +}
> +
> +/**
> +  Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
> +
> +  Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
> +  alignment specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is
> +  returned.  If there is not enough memory at the specified alignment remaining to satisfy the
> +  request, then NULL is returned.
> +
> +  If Alignment is not a power of two and Alignment is not zero, then ASSERT().
> +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> +
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.
> +                                If Alignment is zero, then byte alignment is used.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateAlignedPages (
> +  IN UINTN  Pages,
> +  IN UINTN  Alignment
> +  )
> +{
> +  return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);
> +}
> +
> +/**
> +  Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
> +
> +  Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
> +  alignment specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is
> +  returned.  If there is not enough memory at the specified alignment remaining to satisfy the
> +  request, then NULL is returned.
> +
> +  If Alignment is not a power of two and Alignment is not zero, then ASSERT().
> +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> +
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.
> +                                If Alignment is zero, then byte alignment is used.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateAlignedRuntimePages (
> +  IN UINTN  Pages,
> +  IN UINTN  Alignment
> +  )
> +{
> +  return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);
> +}
> +
> +/**
> +  Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
> +
> +  Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
> +  alignment specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is
> +  returned.  If there is not enough memory at the specified alignment remaining to satisfy the
> +  request, then NULL is returned.
> +
> +  If Alignment is not a power of two and Alignment is not zero, then ASSERT().
> +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> +
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.
> +                                If Alignment is zero, then byte alignment is used.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateAlignedReservedPages (
> +  IN UINTN  Pages,
> +  IN UINTN  Alignment
> +  )
> +{
> +  return NULL;
> +}
> +
> +/**
> +  Frees one or more 4KB pages that were previously allocated with one of the aligned page
> +  allocation functions in the Memory Allocation Library.
> +
> +  Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer.  Buffer
> +  must have been allocated on a previous call to the aligned page allocation services of the Memory
> +  Allocation Library.  If it is not possible to free allocated pages, then this function will
> +  perform no actions.
> +
> +  If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
> +  Library, then ASSERT().
> +  If Pages is zero, then ASSERT().
> +
> +  @param  Buffer                Pointer to the buffer of pages to free.
> +  @param  Pages                 The number of 4 KB pages to free.
> +
> +**/
> +VOID
> +EFIAPI
> +FreeAlignedPages (
> +  IN VOID   *Buffer,
> +  IN UINTN  Pages
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  ASSERT (Pages != 0);
> +  Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);
> +  ASSERT_EFI_ERROR (Status);
> +}
> +
> +/**
> +  Allocates a buffer of a certain pool type.
> +
> +  Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
> +  pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
> +  returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  @param  MemoryType            The type of memory to allocate.
> +  @param  AllocationSize        The number of bytes to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +InternalAllocatePool (
> +  IN EFI_MEMORY_TYPE  MemoryType,
> +  IN UINTN            AllocationSize
> +  )
> +{
> +  EFI_STATUS  Status;
> +  VOID        *Memory;
> +
> +  Memory = NULL;
> +
> +  Status = gMmst->MmAllocatePool (MemoryType, AllocationSize, &Memory);
> +  if (EFI_ERROR (Status)) {
> +    Memory = NULL;
> +  }
> +  return Memory;
> +}
> +
> +/**
> +  Allocates a buffer of type EfiBootServicesData.
> +
> +  Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
> +  pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
> +  returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  @param  AllocationSize        The number of bytes to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocatePool (
> +  IN UINTN  AllocationSize
> +  )
> +{
> +  return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);
> +}
> +
> +/**
> +  Allocates a buffer of type EfiRuntimeServicesData.
> +
> +  Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
> +  a pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
> +  returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  @param  AllocationSize        The number of bytes to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateRuntimePool (
> +  IN UINTN  AllocationSize
> +  )
> +{
> +  return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);
> +}
> +
> +/**
> +  Allocates a buffer of type EfiReservedMemoryType.
> +
> +  Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns
> +  a pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
> +  returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  @param  AllocationSize        The number of bytes to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateReservedPool (
> +  IN UINTN  AllocationSize
> +  )
> +{
> +  return NULL;
> +}
> +
> +/**
> +  Allocates and zeros a buffer of a certain pool type.
> +
> +  Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer
> +  with zeros, and returns a pointer to the allocated buffer.  If AllocationSize is 0, then a valid
> +  buffer of 0 size is returned.  If there is not enough memory remaining to satisfy the request,
> +  then NULL is returned.
> +
> +  @param  PoolType              The type of memory to allocate.
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +InternalAllocateZeroPool (
> +  IN EFI_MEMORY_TYPE  PoolType,
> +  IN UINTN            AllocationSize
> +  )
> +{
> +  VOID  *Memory;
> +
> +  Memory = InternalAllocatePool (PoolType, AllocationSize);
> +  if (Memory != NULL) {
> +    Memory = ZeroMem (Memory, AllocationSize);
> +  }
> +  return Memory;
> +}
> +
> +/**
> +  Allocates and zeros a buffer of type EfiBootServicesData.
> +
> +  Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
> +  buffer with zeros, and returns a pointer to the allocated buffer.  If AllocationSize is 0, then a
> +  valid buffer of 0 size is returned.  If there is not enough memory remaining to satisfy the
> +  request, then NULL is returned.
> +
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateZeroPool (
> +  IN UINTN  AllocationSize
> +  )
> +{
> +  return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);
> +}
> +
> +/**
> +  Allocates and zeros a buffer of type EfiRuntimeServicesData.
> +
> +  Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
> +  buffer with zeros, and returns a pointer to the allocated buffer.  If AllocationSize is 0, then a
> +  valid buffer of 0 size is returned.  If there is not enough memory remaining to satisfy the
> +  request, then NULL is returned.
> +
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateRuntimeZeroPool (
> +  IN UINTN  AllocationSize
> +  )
> +{
> +  return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);
> +}
> +
> +/**
> +  Allocates and zeros a buffer of type EfiReservedMemoryType.
> +
> +  Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
> +  buffer with zeros, and returns a pointer to the allocated buffer.  If AllocationSize is 0, then a
> +  valid buffer of 0 size is returned.  If there is not enough memory remaining to satisfy the
> +  request, then NULL is returned.
> +
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateReservedZeroPool (
> +  IN UINTN  AllocationSize
> +  )
> +{
> +  return NULL;
> +}
> +
> +/**
> +  Copies a buffer to an allocated buffer of a certain pool type.
> +
> +  Allocates the number bytes specified by AllocationSize of a certain pool type, copies
> +  AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
> +  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there
> +  is not enough memory remaining to satisfy the request, then NULL is returned.
> +  If Buffer is NULL, then ASSERT().
> +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
> +
> +  @param  PoolType              The type of pool to allocate.
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +  @param  Buffer                The buffer to copy to the allocated buffer.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +InternalAllocateCopyPool (
> +  IN EFI_MEMORY_TYPE  PoolType,
> +  IN UINTN            AllocationSize,
> +  IN CONST VOID       *Buffer
> +  )
> +{
> +  VOID  *Memory;
> +
> +  ASSERT (Buffer != NULL);
> +  ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));
> +
> +  Memory = InternalAllocatePool (PoolType, AllocationSize);
> +  if (Memory != NULL) {
> +     Memory = CopyMem (Memory, Buffer, AllocationSize);
> +  }
> +  return Memory;
> +}
> +
> +/**
> +  Copies a buffer to an allocated buffer of type EfiBootServicesData.
> +
> +  Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
> +  AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
> +  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there
> +  is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  If Buffer is NULL, then ASSERT().
> +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
> +
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +  @param  Buffer                The buffer to copy to the allocated buffer.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateCopyPool (
> +  IN UINTN       AllocationSize,
> +  IN CONST VOID  *Buffer
> +  )
> +{
> +  return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);
> +}
> +
> +/**
> +  Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
> +
> +  Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
> +  AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
> +  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there
> +  is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  If Buffer is NULL, then ASSERT().
> +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
> +
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +  @param  Buffer                The buffer to copy to the allocated buffer.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateRuntimeCopyPool (
> +  IN UINTN       AllocationSize,
> +  IN CONST VOID  *Buffer
> +  )
> +{
> +  return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);
> +}
> +
> +/**
> +  Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
> +
> +  Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
> +  AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
> +  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there
> +  is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  If Buffer is NULL, then ASSERT().
> +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
> +
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +  @param  Buffer                The buffer to copy to the allocated buffer.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateReservedCopyPool (
> +  IN UINTN       AllocationSize,
> +  IN CONST VOID  *Buffer
> +  )
> +{
> +  return NULL;
> +}
> +
> +/**
> +  Reallocates a buffer of a specified memory type.
> +
> +  Allocates and zeros the number bytes specified by NewSize from memory of the type
> +  specified by PoolType.  If OldBuffer is not NULL, then the smaller of OldSize and
> +  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
> +  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
> +  enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
> +  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> +
> +  @param  PoolType       The type of pool to allocate.
> +  @param  OldSize        The size, in bytes, of OldBuffer.
> +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> +  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional
> +                         parameter that may be NULL.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +InternalReallocatePool (
> +  IN EFI_MEMORY_TYPE  PoolType,
> +  IN UINTN            OldSize,
> +  IN UINTN            NewSize,
> +  IN VOID             *OldBuffer  OPTIONAL
> +  )
> +{
> +  VOID  *NewBuffer;
> +
> +  NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);
> +  if (NewBuffer != NULL && OldBuffer != NULL) {
> +    CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));
> +    FreePool (OldBuffer);
> +  }
> +  return NewBuffer;
> +}
> +
> +/**
> +  Reallocates a buffer of type EfiBootServicesData.
> +
> +  Allocates and zeros the number bytes specified by NewSize from memory of type
> +  EfiBootServicesData.  If OldBuffer is not NULL, then the smaller of OldSize and
> +  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
> +  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
> +  enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
> +  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> +
> +  @param  OldSize        The size, in bytes, of OldBuffer.
> +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> +  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional
> +                         parameter that may be NULL.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +ReallocatePool (
> +  IN UINTN  OldSize,
> +  IN UINTN  NewSize,
> +  IN VOID   *OldBuffer  OPTIONAL
> +  )
> +{
> +  return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);
> +}
> +
> +/**
> +  Reallocates a buffer of type EfiRuntimeServicesData.
> +
> +  Allocates and zeros the number bytes specified by NewSize from memory of type
> +  EfiRuntimeServicesData.  If OldBuffer is not NULL, then the smaller of OldSize and
> +  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
> +  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
> +  enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
> +  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> +
> +  @param  OldSize        The size, in bytes, of OldBuffer.
> +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> +  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional
> +                         parameter that may be NULL.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +ReallocateRuntimePool (
> +  IN UINTN  OldSize,
> +  IN UINTN  NewSize,
> +  IN VOID   *OldBuffer  OPTIONAL
> +  )
> +{
> +  return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);
> +}
> +
> +/**
> +  Reallocates a buffer of type EfiReservedMemoryType.
> +
> +  Allocates and zeros the number bytes specified by NewSize from memory of type
> +  EfiReservedMemoryType.  If OldBuffer is not NULL, then the smaller of OldSize and
> +  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
> +  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
> +  enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
> +  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> +
> +  @param  OldSize        The size, in bytes, of OldBuffer.
> +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> +  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional
> +                         parameter that may be NULL.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +ReallocateReservedPool (
> +  IN UINTN  OldSize,
> +  IN UINTN  NewSize,
> +  IN VOID   *OldBuffer  OPTIONAL
> +  )
> +{
> +  return NULL;
> +}
> +
> +/**
> +  Frees a buffer that was previously allocated with one of the pool allocation functions in the
> +  Memory Allocation Library.
> +
> +  Frees the buffer specified by Buffer.  Buffer must have been allocated on a previous call to the
> +  pool allocation services of the Memory Allocation Library.  If it is not possible to free pool
> +  resources, then this function will perform no actions.
> +
> +  If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
> +  then ASSERT().
> +
> +  @param  Buffer                Pointer to the buffer to free.
> +
> +**/
> +VOID
> +EFIAPI
> +FreePool (
> +  IN VOID   *Buffer
> +  )
> +{
> +  EFI_STATUS    Status;
> +
> +  Status = gMmst->MmFreePool (Buffer);
> +  ASSERT_EFI_ERROR (Status);
> +}
> +
> +/**
> +  The constructor function calls MmInitializeMemoryServices to initialize
> +  memory in MMRAM and caches EFI_MM_SYSTEM_TABLE pointer.
> +
> +  @param  ImageHandle   The firmware allocated handle for the EFI image.
> +  @param  SystemTable   A pointer to the Management mode System Table.
> +
> +  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MemoryAllocationLibConstructor (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  )
> +{
> +  MM_CORE_PRIVATE_DATA           *MmCorePrivate;
> +  EFI_HOB_GUID_TYPE               *GuidHob;
> +  MM_CORE_DATA_HOB_DATA          *DataInHob;
> +  VOID                            *HobStart;
> +  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
> +  EFI_MMRAM_DESCRIPTOR            *MmramRanges;
> +  UINT32                           MmramRangeCount;
> +  EFI_HOB_GUID_TYPE               *MmramRangesHob;
> +
> +  HobStart = GetHobList ();
> +  DEBUG ((DEBUG_INFO, "StandaloneMmCoreMemoryAllocationLibConstructor - 0x%x\n", HobStart));
> +
> +  //
> +  // Extract MM Core Private context from the Hob. If absent search for
> +  // a Hob containing the MMRAM ranges
> +  //
> +  GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);
> +  if (GuidHob == NULL) {
> +    MmramRangesHob = GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid, HobStart);
> +    if (MmramRangesHob == NULL) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +    MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
> +    if (MmramRangesHobData == NULL) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +    MmramRanges = MmramRangesHobData->Descriptor;
> +    if (MmramRanges == NULL) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +    MmramRangeCount = MmramRangesHobData->NumberOfMmReservedRegions;
> +    if (MmramRanges == NULL) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +  } else {
> +    DataInHob      = GET_GUID_HOB_DATA (GuidHob);
> +    MmCorePrivate = (MM_CORE_PRIVATE_DATA *)(UINTN)DataInHob->Address;
> +    MmramRanges     = (EFI_MMRAM_DESCRIPTOR *)(UINTN)MmCorePrivate->MmramRanges;
> +    MmramRangeCount = MmCorePrivate->MmramRangeCount;
> +  }
> +
> +  {
> +    UINTN                Index;
> +
> +    DEBUG ((DEBUG_INFO, "MmramRangeCount - 0x%x\n", MmramRangeCount));
> +    for (Index = 0; Index < MmramRangeCount; Index++) {
> +      DEBUG ((DEBUG_INFO, "MmramRanges[%d]: 0x%016lx - 0x%016lx\n", Index, MmramRanges[Index].CpuStart, MmramRanges[Index].PhysicalSize));
> +    }
> +  }
> +
> +  //
> +  // Initialize memory service using free MMRAM
> +  //
> +  DEBUG ((DEBUG_INFO, "MmInitializeMemoryServices\n"));
> +  MmInitializeMemoryServices ((UINTN)MmramRangeCount, (VOID *)(UINTN)MmramRanges);
> +
> +  // Initialize MM Services Table
> +  gMmst = MmSystemTable;
> +  return EFI_SUCCESS;
> +}
> diff --git a/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
> new file mode 100644
> index 0000000000..068607f90e
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
> @@ -0,0 +1,49 @@
> +## @file
> +# Memory Allocation Library instance dedicated to MM Core.
> +# The implementation borrows the MM Core Memory Allocation services as the primitive
> +# for memory allocation instead of using MM System Table servces in an indirect way.
> +# It is assumed that this library instance must be linked with MM Core in this package.
> +#
> +# Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
> +# Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +#
> +#  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
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = MemoryAllocationLib
> +  FILE_GUID                      = DCDCBE1D-E760-4E1D-85B4-96E3F0439C41
> +  MODULE_TYPE                    = MM_CORE_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  LIBRARY_CLASS                  = MemoryAllocationLib|MM_CORE_STANDALONE
> +  CONSTRUCTOR                    = MemoryAllocationLibConstructor
> +
> +#
> +# The following information is for reference only and not required by the build tools.
> +#
> +#  VALID_ARCHITECTURES           = IA32 X64
> +#
> +
> +[Sources]
> +  MemoryAllocationLib.c
> +  MemoryAllocationServices.h
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[LibraryClasses]
> +  BaseMemoryLib
> +  DebugLib
> +  HobLib
> +
> +[Guids]
> +  gEfiMmPeiMmramMemoryReserveGuid
> diff --git a/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.h b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.h
> new file mode 100644
> index 0000000000..eb4f4c3984
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.h
> @@ -0,0 +1,38 @@
> +/** @file
> +  Contains function prototypes for Memory Services in the MM Core.
> +
> +  This header file borrows the StandaloneMmCore Memory Allocation services as the primitive
> +  for memory allocation.
> +
> +  Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _PI_MM_CORE_MEMORY_ALLOCATION_SERVICES_H_
> +#define _PI_MM_CORE_MEMORY_ALLOCATION_SERVICES_H_
> +
> +#include <Guid/MmCoreData.h>
> +
> +/**
> +  Called to initialize the memory service.
> +
> +  @param   MmramRangeCount       Number of MMRAM Regions
> +  @param   MmramRanges           Pointer to MMRAM Descriptors
> +
> +**/
> +VOID
> +MmInitializeMemoryServices (
> +  IN UINTN                 MmramRangeCount,
> +  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
> +  );
> +
> +#endif
> -- 
> 2.16.2
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 10/18] StandaloneMmPkg/HobLib: Add AARCH64 Specific HOB Library for management mode.
  2018-04-06 14:42 ` [PATCH v1 10/18] StandaloneMmPkg/HobLib: Add AARCH64 Specific HOB Library for management mode Supreeth Venkatesh
@ 2018-04-25 14:50   ` Achin Gupta
  2018-04-26 13:04     ` Yao, Jiewen
  2018-05-04 23:25     ` Supreeth Venkatesh
  0 siblings, 2 replies; 70+ messages in thread
From: Achin Gupta @ 2018-04-25 14:50 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

Hi Supreeth,

On Fri, Apr 06, 2018 at 03:42:15PM +0100, Supreeth Venkatesh wrote:
> The Standalone MM environment is initialized during the SEC phase on ARM
> Standard Platforms. The MM Core driver implements an entry point module
> which is architecture specific and runs prior to the generic core driver
> code. The former creates a Hob list that the latter consumes. This
> happens in the same phase.
> 
> This patch implements a Hob library that can be used by the entry point
> module to produce a Hob list and by the core driver code to consume it.

References to DXE core need to be removed and the copyright years needs to be
updated.

I think it is worth getting this hoblib reviewed by the ArmPkg maintainers. 

Acked-by: Achin Gupta <achin.gupta@arm.com>

> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  StandaloneMmPkg/Library/HobLib/Arm/HobLib.c | 697 ++++++++++++++++++++++++++++
>  StandaloneMmPkg/Library/HobLib/HobLib.inf   |  45 ++
>  2 files changed, 742 insertions(+)
>  create mode 100644 StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
>  create mode 100644 StandaloneMmPkg/Library/HobLib/HobLib.inf
> 
> diff --git a/StandaloneMmPkg/Library/HobLib/Arm/HobLib.c b/StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
> new file mode 100644
> index 0000000000..62abf47f95
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
> @@ -0,0 +1,697 @@
> +/** @file
> +  HOB Library implementation for DxeCore driver.
> +
> +Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2017, ARM Limited. All rights reserved.<BR>
> +
> +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.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <PiMm.h>
> +
> +#include <Library/HobLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/BaseMemoryLib.h>
> +
> +#include <Guid/MemoryAllocationHob.h>
> +
> +//
> +// Cache copy of HobList pointer.
> +//
> +VOID *gHobList = NULL;
> +
> +/**
> +  Returns the pointer to the HOB list.
> +
> +  This function returns the pointer to first HOB in the list.
> +  For PEI phase, the PEI service GetHobList() can be used to retrieve the pointer
> +  to the HOB list.  For the DXE phase, the HOB list pointer can be retrieved through
> +  the EFI System Table by looking up theHOB list GUID in the System Configuration Table.
> +  Since the System Configuration Table does not exist that the time the DXE Core is
> +  launched, the DXE Core uses a global variable from the DXE Core Entry Point Library
> +  to manage the pointer to the HOB list.
> +
> +  If the pointer to the HOB list is NULL, then ASSERT().
> +
> +  @return The pointer to the HOB list.
> +
> +**/
> +VOID *
> +EFIAPI
> +GetHobList (
> +  VOID
> +  )
> +{
> +  ASSERT (gHobList != NULL);
> +  return gHobList;
> +}
> +
> +/**
> +  Returns the next instance of a HOB type from the starting HOB.
> +
> +  This function searches the first instance of a HOB type from the starting HOB pointer.
> +  If there does not exist such HOB type from the starting HOB pointer, it will return NULL.
> +  In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
> +  unconditionally: it returns HobStart back if HobStart itself meets the requirement;
> +  caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
> +
> +  If HobStart is NULL, then ASSERT().
> +
> +  @param  Type          The HOB type to return.
> +  @param  HobStart      The starting HOB pointer to search from.
> +
> +  @return The next instance of a HOB type from the starting HOB.
> +
> +**/
> +VOID *
> +EFIAPI
> +GetNextHob (
> +  IN UINT16                 Type,
> +  IN CONST VOID             *HobStart
> +  )
> +{
> +  EFI_PEI_HOB_POINTERS  Hob;
> +
> +  ASSERT (HobStart != NULL);
> +
> +  Hob.Raw = (UINT8 *) HobStart;
> +  //
> +  // Parse the HOB list until end of list or matching type is found.
> +  //
> +  while (!END_OF_HOB_LIST (Hob)) {
> +    if (Hob.Header->HobType == Type) {
> +      return Hob.Raw;
> +    }
> +    Hob.Raw = GET_NEXT_HOB (Hob);
> +  }
> +  return NULL;
> +}
> +
> +/**
> +  Returns the first instance of a HOB type among the whole HOB list.
> +
> +  This function searches the first instance of a HOB type among the whole HOB list.
> +  If there does not exist such HOB type in the HOB list, it will return NULL.
> +
> +  If the pointer to the HOB list is NULL, then ASSERT().
> +
> +  @param  Type          The HOB type to return.
> +
> +  @return The next instance of a HOB type from the starting HOB.
> +
> +**/
> +VOID *
> +EFIAPI
> +GetFirstHob (
> +  IN UINT16                 Type
> +  )
> +{
> +  VOID      *HobList;
> +
> +  HobList = GetHobList ();
> +  return GetNextHob (Type, HobList);
> +}
> +
> +/**
> +  Returns the next instance of the matched GUID HOB from the starting HOB.
> +
> +  This function searches the first instance of a HOB from the starting HOB pointer.
> +  Such HOB should satisfy two conditions:
> +  its HOB type is EFI_HOB_TYPE_GUID_EXTENSION, and its GUID Name equals to the input Guid.
> +  If such a HOB from the starting HOB pointer does not exist, it will return NULL.
> +  Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
> +  to extract the data section and its size information, respectively.
> +  In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
> +  unconditionally: it returns HobStart back if HobStart itself meets the requirement;
> +  caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
> +
> +  If Guid is NULL, then ASSERT().
> +  If HobStart is NULL, then ASSERT().
> +
> +  @param  Guid          The GUID to match with in the HOB list.
> +  @param  HobStart      A pointer to a Guid.
> +
> +  @return The next instance of the matched GUID HOB from the starting HOB.
> +
> +**/
> +VOID *
> +EFIAPI
> +GetNextGuidHob (
> +  IN CONST EFI_GUID         *Guid,
> +  IN CONST VOID             *HobStart
> +  )
> +{
> +  EFI_PEI_HOB_POINTERS  GuidHob;
> +
> +  GuidHob.Raw = (UINT8 *) HobStart;
> +  while ((GuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, GuidHob.Raw)) != NULL) {
> +    if (CompareGuid (Guid, &GuidHob.Guid->Name)) {
> +      break;
> +    }
> +    GuidHob.Raw = GET_NEXT_HOB (GuidHob);
> +  }
> +  return GuidHob.Raw;
> +}
> +
> +/**
> +  Returns the first instance of the matched GUID HOB among the whole HOB list.
> +
> +  This function searches the first instance of a HOB among the whole HOB list.
> +  Such HOB should satisfy two conditions:
> +  its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
> +  If such a HOB from the starting HOB pointer does not exist, it will return NULL.
> +  Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
> +  to extract the data section and its size information, respectively.
> +
> +  If the pointer to the HOB list is NULL, then ASSERT().
> +  If Guid is NULL, then ASSERT().
> +
> +  @param  Guid          The GUID to match with in the HOB list.
> +
> +  @return The first instance of the matched GUID HOB among the whole HOB list.
> +
> +**/
> +VOID *
> +EFIAPI
> +GetFirstGuidHob (
> +  IN CONST EFI_GUID         *Guid
> +  )
> +{
> +  VOID      *HobList;
> +
> +  HobList = GetHobList ();
> +  return GetNextGuidHob (Guid, HobList);
> +}
> +
> +/**
> +  Get the system boot mode from the HOB list.
> +
> +  This function returns the system boot mode information from the
> +  PHIT HOB in HOB list.
> +
> +  If the pointer to the HOB list is NULL, then ASSERT().
> +
> +  @param  VOID
> +
> +  @return The Boot Mode.
> +
> +**/
> +EFI_BOOT_MODE
> +EFIAPI
> +GetBootModeHob (
> +  VOID
> +  )
> +{
> +  EFI_HOB_HANDOFF_INFO_TABLE    *HandOffHob;
> +
> +  HandOffHob = (EFI_HOB_HANDOFF_INFO_TABLE *) GetHobList ();
> +
> +  return  HandOffHob->BootMode;
> +}
> +
> +
> +/**
> +
> +
> +**/
> +EFI_HOB_HANDOFF_INFO_TABLE*
> +HobConstructor (
> +  IN VOID   *EfiMemoryBegin,
> +  IN UINTN  EfiMemoryLength,
> +  IN VOID   *EfiFreeMemoryBottom,
> +  IN VOID   *EfiFreeMemoryTop
> +  )
> +{
> +  EFI_HOB_HANDOFF_INFO_TABLE  *Hob;
> +  EFI_HOB_GENERIC_HEADER      *HobEnd;
> +
> +  Hob    = EfiFreeMemoryBottom;
> +  HobEnd = (EFI_HOB_GENERIC_HEADER *)(Hob+1);
> +
> +  Hob->Header.HobType     = EFI_HOB_TYPE_HANDOFF;
> +  Hob->Header.HobLength   = sizeof(EFI_HOB_HANDOFF_INFO_TABLE);
> +  Hob->Header.Reserved    = 0;
> +
> +  HobEnd->HobType     = EFI_HOB_TYPE_END_OF_HOB_LIST;
> +  HobEnd->HobLength   = sizeof(EFI_HOB_GENERIC_HEADER);
> +  HobEnd->Reserved    = 0;
> +
> +  Hob->Version             = EFI_HOB_HANDOFF_TABLE_VERSION;
> +  Hob->BootMode            = BOOT_WITH_FULL_CONFIGURATION;
> +
> +  Hob->EfiMemoryTop        = (UINTN)EfiMemoryBegin + EfiMemoryLength;
> +  Hob->EfiMemoryBottom     = (UINTN)EfiMemoryBegin;
> +  Hob->EfiFreeMemoryTop    = (UINTN)EfiFreeMemoryTop;
> +  Hob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS)(UINTN)(HobEnd+1);
> +  Hob->EfiEndOfHobList     = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd;
> +
> +  gHobList = Hob;
> +
> +  return Hob;
> +}
> +
> +VOID *
> +CreateHob (
> +  IN  UINT16    HobType,
> +  IN  UINT16    HobLength
> +  )
> +{
> +  EFI_HOB_HANDOFF_INFO_TABLE  *HandOffHob;
> +  EFI_HOB_GENERIC_HEADER      *HobEnd;
> +  EFI_PHYSICAL_ADDRESS        FreeMemory;
> +  VOID                        *Hob;
> +
> +  HandOffHob = GetHobList ();
> +
> +  HobLength = (UINT16)((HobLength + 0x7) & (~0x7));
> +
> +  FreeMemory = HandOffHob->EfiFreeMemoryTop - HandOffHob->EfiFreeMemoryBottom;
> +
> +  if (FreeMemory < HobLength) {
> +      return NULL;
> +  }
> +
> +  Hob = (VOID*) (UINTN) HandOffHob->EfiEndOfHobList;
> +  ((EFI_HOB_GENERIC_HEADER*) Hob)->HobType = HobType;
> +  ((EFI_HOB_GENERIC_HEADER*) Hob)->HobLength = HobLength;
> +  ((EFI_HOB_GENERIC_HEADER*) Hob)->Reserved = 0;
> +
> +  HobEnd = (EFI_HOB_GENERIC_HEADER*) ((UINTN)Hob + HobLength);
> +  HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
> +
> +  HobEnd->HobType   = EFI_HOB_TYPE_END_OF_HOB_LIST;
> +  HobEnd->HobLength = sizeof(EFI_HOB_GENERIC_HEADER);
> +  HobEnd->Reserved  = 0;
> +  HobEnd++;
> +  HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
> +
> +  return Hob;
> +}
> +
> +/**
> +  Builds a HOB for a loaded PE32 module.
> +
> +  This function builds a HOB for a loaded PE32 module.
> +  It can only be invoked during PEI phase;
> +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> +  If ModuleName is NULL, then ASSERT().
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  ModuleName              The GUID File Name of the module.
> +  @param  MemoryAllocationModule  The 64 bit physical address of the module.
> +  @param  ModuleLength            The length of the module in bytes.
> +  @param  EntryPoint              The 64 bit physical address of the module entry point.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildModuleHob (
> +  IN CONST EFI_GUID         *ModuleName,
> +  IN EFI_PHYSICAL_ADDRESS   MemoryAllocationModule,
> +  IN UINT64                 ModuleLength,
> +  IN EFI_PHYSICAL_ADDRESS   EntryPoint
> +  )
> +{
> +  EFI_HOB_MEMORY_ALLOCATION_MODULE  *Hob;
> +
> +  ASSERT (((MemoryAllocationModule & (EFI_PAGE_SIZE - 1)) == 0) &&
> +          ((ModuleLength & (EFI_PAGE_SIZE - 1)) == 0));
> +
> +  Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE));
> +
> +  CopyGuid (&(Hob->MemoryAllocationHeader.Name), &gEfiHobMemoryAllocModuleGuid);
> +  Hob->MemoryAllocationHeader.MemoryBaseAddress = MemoryAllocationModule;
> +  Hob->MemoryAllocationHeader.MemoryLength      = ModuleLength;
> +  Hob->MemoryAllocationHeader.MemoryType        = EfiBootServicesCode;
> +
> +  //
> +  // Zero the reserved space to match HOB spec
> +  //
> +  ZeroMem (Hob->MemoryAllocationHeader.Reserved, sizeof (Hob->MemoryAllocationHeader.Reserved));
> +
> +  CopyGuid (&Hob->ModuleName, ModuleName);
> +  Hob->EntryPoint = EntryPoint;
> +}
> +
> +/**
> +  Builds a HOB that describes a chunk of system memory.
> +
> +  This function builds a HOB that describes a chunk of system memory.
> +  It can only be invoked during PEI phase;
> +  for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.
> +
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  ResourceType        The type of resource described by this HOB.
> +  @param  ResourceAttribute   The resource attributes of the memory described by this HOB.
> +  @param  PhysicalStart       The 64 bit physical address of memory described by this HOB.
> +  @param  NumberOfBytes       The length of the memory described by this HOB in bytes.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildResourceDescriptorHob (
> +  IN EFI_RESOURCE_TYPE            ResourceType,
> +  IN EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttribute,
> +  IN EFI_PHYSICAL_ADDRESS         PhysicalStart,
> +  IN UINT64                       NumberOfBytes
> +  )
> +{
> +  EFI_HOB_RESOURCE_DESCRIPTOR  *Hob;
> +
> +  Hob = CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR));
> +  ASSERT(Hob != NULL);
> +
> +  Hob->ResourceType      = ResourceType;
> +  Hob->ResourceAttribute = ResourceAttribute;
> +  Hob->PhysicalStart     = PhysicalStart;
> +  Hob->ResourceLength    = NumberOfBytes;
> +}
> +
> +/**
> +  Builds a GUID HOB with a certain data length.
> +
> +  This function builds a customized HOB tagged with a GUID for identification
> +  and returns the start address of GUID HOB data so that caller can fill the customized data.
> +  The HOB Header and Name field is already stripped.
> +  It can only be invoked during PEI phase;
> +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> +  If Guid is NULL, then ASSERT().
> +  If there is no additional space for HOB creation, then ASSERT().
> +  If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
> +
> +  @param  Guid          The GUID to tag the customized HOB.
> +  @param  DataLength    The size of the data payload for the GUID HOB.
> +
> +  @return The start address of GUID HOB data.
> +
> +**/
> +VOID *
> +EFIAPI
> +BuildGuidHob (
> +  IN CONST EFI_GUID              *Guid,
> +  IN UINTN                       DataLength
> +  )
> +{
> +  EFI_HOB_GUID_TYPE *Hob;
> +
> +  //
> +  // Make sure that data length is not too long.
> +  //
> +  ASSERT (DataLength <= (0xffff - sizeof (EFI_HOB_GUID_TYPE)));
> +
> +  Hob = CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16) (sizeof (EFI_HOB_GUID_TYPE) + DataLength));
> +  CopyGuid (&Hob->Name, Guid);
> +  return Hob + 1;
> +}
> +
> +
> +/**
> +  Copies a data buffer to a newly-built HOB.
> +
> +  This function builds a customized HOB tagged with a GUID for identification,
> +  copies the input data to the HOB data field and returns the start address of the GUID HOB data.
> +  The HOB Header and Name field is already stripped.
> +  It can only be invoked during PEI phase;
> +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> +  If Guid is NULL, then ASSERT().
> +  If Data is NULL and DataLength > 0, then ASSERT().
> +  If there is no additional space for HOB creation, then ASSERT().
> +  If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
> +
> +  @param  Guid          The GUID to tag the customized HOB.
> +  @param  Data          The data to be copied into the data field of the GUID HOB.
> +  @param  DataLength    The size of the data payload for the GUID HOB.
> +
> +  @return The start address of GUID HOB data.
> +
> +**/
> +VOID *
> +EFIAPI
> +BuildGuidDataHob (
> +  IN CONST EFI_GUID              *Guid,
> +  IN VOID                        *Data,
> +  IN UINTN                       DataLength
> +  )
> +{
> +  VOID  *HobData;
> +
> +  ASSERT (Data != NULL || DataLength == 0);
> +
> +  HobData = BuildGuidHob (Guid, DataLength);
> +
> +  return CopyMem (HobData, Data, DataLength);
> +}
> +
> +/**
> +  Builds a Firmware Volume HOB.
> +
> +  This function builds a Firmware Volume HOB.
> +  It can only be invoked during PEI phase;
> +  for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.
> +
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  BaseAddress   The base address of the Firmware Volume.
> +  @param  Length        The size of the Firmware Volume in bytes.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildFvHob (
> +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> +  IN UINT64                      Length
> +  )
> +{
> +  EFI_HOB_FIRMWARE_VOLUME  *Hob;
> +
> +  Hob = CreateHob (EFI_HOB_TYPE_FV, sizeof (EFI_HOB_FIRMWARE_VOLUME));
> +
> +  Hob->BaseAddress = BaseAddress;
> +  Hob->Length      = Length;
> +}
> +
> +
> +/**
> +  Builds a EFI_HOB_TYPE_FV2 HOB.
> +
> +  This function builds a EFI_HOB_TYPE_FV2 HOB.
> +  It can only be invoked during PEI phase;
> +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  BaseAddress   The base address of the Firmware Volume.
> +  @param  Length        The size of the Firmware Volume in bytes.
> +  @param  FvName       The name of the Firmware Volume.
> +  @param  FileName      The name of the file.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildFv2Hob (
> +  IN          EFI_PHYSICAL_ADDRESS        BaseAddress,
> +  IN          UINT64                      Length,
> +  IN CONST    EFI_GUID                    *FvName,
> +  IN CONST    EFI_GUID                    *FileName
> +  )
> +{
> +  EFI_HOB_FIRMWARE_VOLUME2  *Hob;
> +
> +  Hob = CreateHob (EFI_HOB_TYPE_FV2, sizeof (EFI_HOB_FIRMWARE_VOLUME2));
> +
> +  Hob->BaseAddress = BaseAddress;
> +  Hob->Length      = Length;
> +  CopyGuid (&Hob->FvName, FvName);
> +  CopyGuid (&Hob->FileName, FileName);
> +}
> +
> +
> +/**
> +  Builds a HOB for the CPU.
> +
> +  This function builds a HOB for the CPU.
> +  It can only be invoked during PEI phase;
> +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  SizeOfMemorySpace   The maximum physical memory addressability of the processor.
> +  @param  SizeOfIoSpace       The maximum physical I/O addressability of the processor.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildCpuHob (
> +  IN UINT8                       SizeOfMemorySpace,
> +  IN UINT8                       SizeOfIoSpace
> +  )
> +{
> +  EFI_HOB_CPU  *Hob;
> +
> +  Hob = CreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));
> +
> +  Hob->SizeOfMemorySpace = SizeOfMemorySpace;
> +  Hob->SizeOfIoSpace     = SizeOfIoSpace;
> +
> +  //
> +  // Zero the reserved space to match HOB spec
> +  //
> +  ZeroMem (Hob->Reserved, sizeof (Hob->Reserved));
> +}
> +
> +/**
> +  Builds a HOB for the memory allocation.
> +
> +  This function builds a HOB for the memory allocation.
> +  It can only be invoked during PEI phase;
> +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  BaseAddress   The 64 bit physical address of the memory.
> +  @param  Length        The length of the memory allocation in bytes.
> +  @param  MemoryType    Type of memory allocated by this HOB.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildMemoryAllocationHob (
> +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> +  IN UINT64                      Length,
> +  IN EFI_MEMORY_TYPE             MemoryType
> +  )
> +{
> +  EFI_HOB_MEMORY_ALLOCATION  *Hob;
> +
> +  ASSERT (((BaseAddress & (EFI_PAGE_SIZE - 1)) == 0) &&
> +          ((Length & (EFI_PAGE_SIZE - 1)) == 0));
> +
> +  Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION));
> +
> +  ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));
> +  Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
> +  Hob->AllocDescriptor.MemoryLength      = Length;
> +  Hob->AllocDescriptor.MemoryType        = MemoryType;
> +  //
> +  // Zero the reserved space to match HOB spec
> +  //
> +  ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));
> +}
> +
> +/**
> +  Builds a HOB that describes a chunk of system memory with Owner GUID.
> +
> +  This function builds a HOB that describes a chunk of system memory.
> +  It can only be invoked during PEI phase;
> +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> +
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  ResourceType        The type of resource described by this HOB.
> +  @param  ResourceAttribute   The resource attributes of the memory described by this HOB.
> +  @param  PhysicalStart       The 64 bit physical address of memory described by this HOB.
> +  @param  NumberOfBytes       The length of the memory described by this HOB in bytes.
> +  @param  OwnerGUID           GUID for the owner of this resource.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildResourceDescriptorWithOwnerHob (
> +  IN EFI_RESOURCE_TYPE            ResourceType,
> +  IN EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttribute,
> +  IN EFI_PHYSICAL_ADDRESS         PhysicalStart,
> +  IN UINT64                       NumberOfBytes,
> +  IN EFI_GUID                     *OwnerGUID
> +  )
> +{
> +  //
> +  // PEI HOB is read only for DXE phase
> +  //
> +  ASSERT (FALSE);
> +}
> +
> +/**
> +  Builds a Capsule Volume HOB.
> +
> +  This function builds a Capsule Volume HOB.
> +  It can only be invoked during PEI phase;
> +  for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.
> +
> +  If the platform does not support Capsule Volume HOBs, then ASSERT().
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  BaseAddress   The base address of the Capsule Volume.
> +  @param  Length        The size of the Capsule Volume in bytes.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildCvHob (
> +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> +  IN UINT64                      Length
> +  )
> +{
> +  //
> +  // PEI HOB is read only for DXE phase
> +  //
> +  ASSERT (FALSE);
> +}
> +
> +
> +/**
> +  Builds a HOB for the BSP store.
> +
> +  This function builds a HOB for BSP store.
> +  It can only be invoked during PEI phase;
> +  for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.
> +
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  BaseAddress   The 64 bit physical address of the BSP.
> +  @param  Length        The length of the BSP store in bytes.
> +  @param  MemoryType    Type of memory allocated by this HOB.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildBspStoreHob (
> +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> +  IN UINT64                      Length,
> +  IN EFI_MEMORY_TYPE             MemoryType
> +  )
> +{
> +  //
> +  // PEI HOB is read only for DXE phase
> +  //
> +  ASSERT (FALSE);
> +}
> +
> +/**
> +  Builds a HOB for the Stack.
> +
> +  This function builds a HOB for the stack.
> +  It can only be invoked during PEI phase;
> +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  BaseAddress   The 64 bit physical address of the Stack.
> +  @param  Length        The length of the stack in bytes.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildStackHob (
> +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> +  IN UINT64                      Length
> +  )
> +{
> +  //
> +  // PEI HOB is read only for DXE phase
> +  //
> +  ASSERT (FALSE);
> +}
> diff --git a/StandaloneMmPkg/Library/HobLib/HobLib.inf b/StandaloneMmPkg/Library/HobLib/HobLib.inf
> new file mode 100644
> index 0000000000..42273b6d66
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/HobLib/HobLib.inf
> @@ -0,0 +1,45 @@
> +## @file
> +# Instance of HOB Library for DXE Core.
> +#
> +# HOB Library implementation for the DXE Core. Does not have a constructor.
> +#  Uses gHobList defined in the DXE Core Entry Point Library.
> +#
> +# Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
> +# Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +#
> +#  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.
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = HobLib
> +  FILE_GUID                      = CF56EF2C-68D8-4BD5-9A8B-8A7BFCFF751C
> +  MODULE_TYPE                    = MM_CORE_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  LIBRARY_CLASS                  = HobLib|MM_CORE_STANDALONE MM_STANDALONE
> +
> +#
> +#  VALID_ARCHITECTURES           = AARCH64
> +#
> +
> +[Sources.AARCH64]
> +  Arm/HobLib.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +
> +
> +[LibraryClasses]
> +  BaseMemoryLib
> +  DebugLib
> +
> +[Guids]
> +  gEfiHobListGuid                               ## CONSUMES  ## SystemTable
> -- 
> 2.16.2
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 08/18] StandaloneMmPkg/MemLib: AARCH64 Specific instance of memory check library.
  2018-04-25 10:35       ` Achin Gupta
@ 2018-04-26 13:02         ` Yao, Jiewen
  0 siblings, 0 replies; 70+ messages in thread
From: Yao, Jiewen @ 2018-04-26 13:02 UTC (permalink / raw)
  To: Achin Gupta
  Cc: Supreeth Venkatesh, ard.biesheuvel@linaro.org,
	edk2-devel@lists.01.org, leif.lindholm@linaro.org, Gao, Liming,
	Kinney, Michael D, nd@arm.com

You are right.

I propose to add Arm and X86 specific folder - just include PA calculation.
We can leave the generic function in common folder.

The final layout can be:
StandaloneMmPkg/Library/MmMemLib/MmMemLib.c
StandaloneMmPkg/Library/MmMemLib/MmMemLib.inf
StandaloneMmPkg/Library/MmMemLib/Arm/ArchSpecific.c
StandaloneMmPkg/Library/MmMemLib/AArch64/ArchSpecific.c StandaloneMmPkg/Library/MmMemLib/Ia32/ArchSpecific.c
StandaloneMmPkg/Library/MmMemLib/X64/ArchSpecific.c

Thank you
Yao Jiewen

> -----Original Message-----
> From: Achin Gupta [mailto:achin.gupta@arm.com]
> Sent: Wednesday, April 25, 2018 3:36 AM
> To: Yao, Jiewen <jiewen.yao@intel.com>
> Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com>;
> ard.biesheuvel@linaro.org; edk2-devel@lists.01.org; leif.lindholm@linaro.org;
> Gao, Liming <liming.gao@intel.com>; Kinney, Michael D
> <michael.d.kinney@intel.com>; nd@arm.com
> Subject: Re: [edk2] [PATCH v1 08/18] StandaloneMmPkg/MemLib: AARCH64
> Specific instance of memory check library.
> 
> Hi Jiewen,
> 
> On Mon, Apr 16, 2018 at 10:30:55PM +0000, Yao, Jiewen wrote:
> > Hi
> > I don't think this lib is generic, because it hardcode the physical address bits.
> >
> > PhysicalAddressBits = 36;
> >
> > For X86 CPU, we get it from CPUID. :-)
> >
> > As enhancement, we may put most common C-code logic (such as CopyMem,
> or memmap calculation) to StandaloneMmPkg/MemLib, and only include the
> PhysicalAddresBit calculation under StandaloneMmPkg/MemLib/Arm folder.
> >
> > As such, we know clearly on which one is ARM specific.
> 
> My point was that the hardocoded PA bits were not introduced to make this code
> work on Arm. This has been present in the StandaloneMmPkg from the outset. I
> guess for x86 you have moved on to getting this information from the
> CPUID. Afaics, this function is not be used on Arm platforms but Supreeth will
> double check. If that is the case then only the generic library will be required
> minus this function.
> 
> cheers,
> Achin
> 
> >
> > Thank you
> > Yao Jiewen
> >
> > > -----Original Message-----
> > > From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of
> Achin
> > > Gupta
> > > Sent: Monday, April 16, 2018 11:13 PM
> > > To: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> > > Cc: ard.biesheuvel@linaro.org; edk2-devel@lists.01.org;
> > > leif.lindholm@linaro.org; Yao, Jiewen <jiewen.yao@intel.com>; Gao, Liming
> > > <liming.gao@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>;
> > > nd@arm.com
> > > Subject: Re: [edk2] [PATCH v1 08/18] StandaloneMmPkg/MemLib: AARCH64
> > > Specific instance of memory check library.
> > >
> > > Hi Supreeth,
> > >
> > > On Fri, Apr 06, 2018 at 03:42:13PM +0100, Supreeth Venkatesh wrote:
> > > > MM memory check library library implementation. This library consumes
> > > > MM_ACCESS_PROTOCOL to get MMRAM information. In order to use this
> > > > library instance, the platform should produce all MMRAM range via
> > > > MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core
> > > > and MM driver) and/or specific dedicated hardware.
> > > >
> > > > This patch provides services for MM Memory Operation.
> > > > The management mode Mem Library provides function for checking if
> buffer
> > > > is outside MMRAM and valid. It also provides functions for copy data
> > > > from MMRAM to non-MMRAM, from non-MMRAM to MMRAM,
> > > > from non-MMRAM to non-MMRAM, or set data in non-MMRAM.
> > > >
> > > > Contributed-under: TianoCore Contribution Agreement 1.1
> > > > Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> > > > Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> > > > ---
> > > >  StandaloneMmPkg/Include/Library/MemLib.h    | 140
> ++++++++++++++
> > > >  StandaloneMmPkg/Library/MemLib/Arm/MemLib.c | 276
> > > ++++++++++++++++++++++++++++
> > >
> > > Why is this Library Arm specific. Apart from cosmetics tweaks, it has not
> > > changed since it was originally contributed?
> > >
> > > cheers,
> > > Achin
> > >
> > > >  StandaloneMmPkg/Library/MemLib/MemLib.inf   |  47 +++++
> > > >  3 files changed, 463 insertions(+)
> > > >  create mode 100644 StandaloneMmPkg/Include/Library/MemLib.h
> > > >  create mode 100644 StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> > > >  create mode 100644 StandaloneMmPkg/Library/MemLib/MemLib.inf
> > > >
> > > > diff --git a/StandaloneMmPkg/Include/Library/MemLib.h
> > > b/StandaloneMmPkg/Include/Library/MemLib.h
> > > > new file mode 100644
> > > > index 0000000000..3264f10010
> > > > --- /dev/null
> > > > +++ b/StandaloneMmPkg/Include/Library/MemLib.h
> > > > @@ -0,0 +1,140 @@
> > > > +/** @file
> > > > +  Provides services for MM Memory Operation.
> > > > +
> > > > +  The MM Mem Library provides function for checking if buffer is outside
> > > MMRAM and valid.
> > > > +  It also provides functions for copy data from MMRAM to non-MMRAM,
> > > from non-MMRAM to MMRAM,
> > > > +  from non-MMRAM to non-MMRAM, or set data in non-MMRAM.
> > > > +
> > > > +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > > > +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > > > +
> > > > +  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
> > > > +
> > > > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS
> IS"
> > > BASIS,
> > > > +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> > > EXPRESS OR IMPLIED.
> > > > +
> > > > +**/
> > > > +
> > > > +#ifndef _MM_MEM_LIB_H_
> > > > +#define _MM_MEM_LIB_H_
> > > > +
> > > > +/**
> > > > +  This function check if the buffer is valid per processor architecture and
> not
> > > overlap with MMRAM.
> > > > +
> > > > +  @param Buffer  The buffer start address to be checked.
> > > > +  @param Length  The buffer length to be checked.
> > > > +
> > > > +  @retval TRUE  This buffer is valid per processor architecture and not
> > > overlap with MMRAM.
> > > > +  @retval FALSE This buffer is not valid per processor architecture or
> overlap
> > > with MMRAM.
> > > > +**/
> > > > +BOOLEAN
> > > > +EFIAPI
> > > > +MmIsBufferOutsideMmValid (
> > > > +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> > > > +  IN UINT64                Length
> > > > +  );
> > > > +
> > > > +/**
> > > > +  Copies a source buffer (non-MMRAM) to a destination buffer
> (MMRAM).
> > > > +
> > > > +  This function copies a source buffer (non-MMRAM) to a destination
> buffer
> > > (MMRAM).
> > > > +  It checks if source buffer is valid per processor architecture and not
> overlap
> > > with MMRAM.
> > > > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > > > +  If the check fails, it return EFI_SECURITY_VIOLATION.
> > > > +  The implementation must be reentrant.
> > > > +
> > > > +  @param  DestinationBuffer   The pointer to the destination buffer of
> the
> > > memory copy.
> > > > +  @param  SourceBuffer        The pointer to the source buffer of
> the
> > > memory copy.
> > > > +  @param  Length              The number of bytes to copy from
> > > SourceBuffer to DestinationBuffer.
> > > > +
> > > > +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per
> > > processor architecture or overlap with MMRAM.
> > > > +  @retval EFI_SUCCESS            Memory is copied.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +MmCopyMemToSmram (
> > > > +  OUT VOID       *DestinationBuffer,
> > > > +  IN CONST VOID  *SourceBuffer,
> > > > +  IN UINTN       Length
> > > > +  );
> > > > +
> > > > +/**
> > > > +  Copies a source buffer (MMRAM) to a destination buffer
> (NON-MMRAM).
> > > > +
> > > > +  This function copies a source buffer (non-MMRAM) to a destination
> buffer
> > > (MMRAM).
> > > > +  It checks if destination buffer is valid per processor architecture and not
> > > overlap with MMRAM.
> > > > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > > > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > > > +  The implementation must be reentrant.
> > > > +
> > > > +  @param  DestinationBuffer   The pointer to the destination buffer of
> the
> > > memory copy.
> > > > +  @param  SourceBuffer        The pointer to the source buffer of
> the
> > > memory copy.
> > > > +  @param  Length              The number of bytes to copy from
> > > SourceBuffer to DestinationBuffer.
> > > > +
> > > > +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per
> > > processor architecture or overlap with MMRAM.
> > > > +  @retval EFI_SUCCESS            Memory is copied.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +MmCopyMemFromSmram (
> > > > +  OUT VOID       *DestinationBuffer,
> > > > +  IN CONST VOID  *SourceBuffer,
> > > > +  IN UINTN       Length
> > > > +  );
> > > > +
> > > > +/**
> > > > +  Copies a source buffer (NON-MMRAM) to a destination buffer
> > > (NON-MMRAM).
> > > > +
> > > > +  This function copies a source buffer (non-MMRAM) to a destination
> buffer
> > > (MMRAM).
> > > > +  It checks if source buffer and destination buffer are valid per processor
> > > architecture and not overlap with MMRAM.
> > > > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > > > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > > > +  The implementation must be reentrant, and it must handle the case
> where
> > > source buffer overlaps destination buffer.
> > > > +
> > > > +  @param  DestinationBuffer   The pointer to the destination buffer of
> the
> > > memory copy.
> > > > +  @param  SourceBuffer        The pointer to the source buffer of
> the
> > > memory copy.
> > > > +  @param  Length              The number of bytes to copy from
> > > SourceBuffer to DestinationBuffer.
> > > > +
> > > > +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per
> > > processor architecture or overlap with MMRAM.
> > > > +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per
> > > processor architecture or overlap with MMRAM.
> > > > +  @retval EFI_SUCCESS            Memory is copied.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +MmCopyMem (
> > > > +  OUT VOID       *DestinationBuffer,
> > > > +  IN CONST VOID  *SourceBuffer,
> > > > +  IN UINTN       Length
> > > > +  );
> > > > +
> > > > +/**
> > > > +  Fills a target buffer (NON-MMRAM) with a byte value.
> > > > +
> > > > +  This function fills a target buffer (non-MMRAM) with a byte value.
> > > > +  It checks if target buffer is valid per processor architecture and not
> overlap
> > > with MMRAM.
> > > > +  If the check passes, it fills memory and returns EFI_SUCCESS.
> > > > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > > > +
> > > > +  @param  Buffer    The memory to set.
> > > > +  @param  Length    The number of bytes to set.
> > > > +  @param  Value     The value with which to fill Length bytes of
> Buffer.
> > > > +
> > > > +  @retval EFI_SECURITY_VIOLATION The Buffer is invalid per processor
> > > architecture or overlap with MMRAM.
> > > > +  @retval EFI_SUCCESS            Memory is set.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +MmSetMem (
> > > > +  OUT VOID  *Buffer,
> > > > +  IN UINTN  Length,
> > > > +  IN UINT8  Value
> > > > +  );
> > > > +
> > > > +#endif
> > > > diff --git a/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> > > b/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> > > > new file mode 100644
> > > > index 0000000000..432a45698b
> > > > --- /dev/null
> > > > +++ b/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> > > > @@ -0,0 +1,276 @@
> > > > +/** @file
> > > > +  Instance of MM memory check library.
> > > > +
> > > > +  MM memory check library library implementation. This library
> consumes
> > > MM_ACCESS_PROTOCOL
> > > > +  to get MMRAM information. In order to use this library instance, the
> > > platform should produce
> > > > +  all MMRAM range via MM_ACCESS_PROTOCOL, including the range for
> > > firmware (like MM Core
> > > > +  and MM driver) and/or specific dedicated hardware.
> > > > +
> > > > +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > > > +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > > > +
> > > > +  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
> > > > +
> > > > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS
> IS"
> > > BASIS,
> > > > +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> > > EXPRESS OR IMPLIED.
> > > > +
> > > > +**/
> > > > +
> > > > +
> > > > +#include <PiMm.h>
> > > > +
> > > > +#include <Library/BaseLib.h>
> > > > +#include <Library/BaseMemoryLib.h>
> > > > +#include <Library/DebugLib.h>
> > > > +
> > > > +EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges;
> > > > +UINTN                mMmMemLibInternalMmramCount;
> > > > +
> > > > +//
> > > > +// Maximum support address used to check input buffer
> > > > +//
> > > > +EFI_PHYSICAL_ADDRESS
> mMmMemLibInternalMaximumSupportAddress =
> > > 0;
> > > > +
> > > > +/**
> > > > +  Calculate and save the maximum support address.
> > > > +
> > > > +**/
> > > > +VOID
> > > > +MmMemLibInternalCalculateMaximumSupportAddress (
> > > > +  VOID
> > > > +  )
> > > > +{
> > > > +  UINT8        PhysicalAddressBits;
> > > > +
> > > > +  PhysicalAddressBits = 36;
> > > > +
> > > > +  //
> > > > +  // Save the maximum support address in one global variable
> > > > +  //
> > > > +  mMmMemLibInternalMaximumSupportAddress =
> > > (EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, PhysicalAddressBits) - 1);
> > > > +  DEBUG ((DEBUG_INFO,
> "mMmMemLibInternalMaximumSupportAddress =
> > > 0x%lx\n", mMmMemLibInternalMaximumSupportAddress));
> > > > +}
> > > > +
> > > > +/**
> > > > +  This function check if the buffer is valid per processor architecture and
> not
> > > overlap with MMRAM.
> > > > +
> > > > +  @param Buffer  The buffer start address to be checked.
> > > > +  @param Length  The buffer length to be checked.
> > > > +
> > > > +  @retval TRUE  This buffer is valid per processor architecture and not
> > > overlap with MMRAM.
> > > > +  @retval FALSE This buffer is not valid per processor architecture or
> overlap
> > > with MMRAM.
> > > > +**/
> > > > +BOOLEAN
> > > > +EFIAPI
> > > > +MmIsBufferOutsideMmValid (
> > > > +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> > > > +  IN UINT64                Length
> > > > +  )
> > > > +{
> > > > +  UINTN  Index;
> > > > +
> > > > +  //
> > > > +  // Check override.
> > > > +  // NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1)
> is
> > > valid.
> > > > +  //
> > > > +  if ((Length > mMmMemLibInternalMaximumSupportAddress) ||
> > > > +      (Buffer > mMmMemLibInternalMaximumSupportAddress) ||
> > > > +      ((Length != 0) && (Buffer >
> > > (mMmMemLibInternalMaximumSupportAddress - (Length - 1)))) ) {
> > > > +    //
> > > > +    // Overflow happen
> > > > +    //
> > > > +    DEBUG ((
> > > > +      DEBUG_ERROR,
> > > > +      "MmIsBufferOutsideMmValid: Overflow: Buffer (0x%lx) - Length
> > > (0x%lx), MaximumSupportAddress (0x%lx)\n",
> > > > +      Buffer,
> > > > +      Length,
> > > > +      mMmMemLibInternalMaximumSupportAddress
> > > > +      ));
> > > > +    return FALSE;
> > > > +  }
> > > > +
> > > > +  for (Index = 0; Index < mMmMemLibInternalMmramCount; Index ++) {
> > > > +    if (((Buffer >= mMmMemLibInternalMmramRanges[Index].CpuStart)
> &&
> > > (Buffer < mMmMemLibInternalMmramRanges[Index].CpuStart +
> > > mMmMemLibInternalMmramRanges[Index].PhysicalSize)) ||
> > > > +        ((mMmMemLibInternalMmramRanges[Index].CpuStart >=
> Buffer)
> > > && (mMmMemLibInternalMmramRanges[Index].CpuStart < Buffer +
> Length))) {
> > > > +      DEBUG ((
> > > > +        DEBUG_ERROR,
> > > > +        "MmIsBufferOutsideMmValid: Overlap: Buffer (0x%lx) - Length
> > > (0x%lx), ",
> > > > +        Buffer,
> > > > +        Length
> > > > +        ));
> > > > +      DEBUG ((
> > > > +        DEBUG_ERROR,
> > > > +        "CpuStart (0x%lx) - PhysicalSize (0x%lx)\n",
> > > > +        mMmMemLibInternalMmramRanges[Index].CpuStart,
> > > > +        mMmMemLibInternalMmramRanges[Index].PhysicalSize
> > > > +        ));
> > > > +      return FALSE;
> > > > +    }
> > > > +  }
> > > > +
> > > > +  return TRUE;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Copies a source buffer (non-MMRAM) to a destination buffer
> (MMRAM).
> > > > +
> > > > +  This function copies a source buffer (non-MMRAM) to a destination
> buffer
> > > (MMRAM).
> > > > +  It checks if source buffer is valid per processor architecture and not
> overlap
> > > with MMRAM.
> > > > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > > > +  If the check fails, it return EFI_SECURITY_VIOLATION.
> > > > +  The implementation must be reentrant.
> > > > +
> > > > +  @param  DestinationBuffer   The pointer to the destination buffer of
> the
> > > memory copy.
> > > > +  @param  SourceBuffer        The pointer to the source buffer of
> the
> > > memory copy.
> > > > +  @param  Length              The number of bytes to copy from
> > > SourceBuffer to DestinationBuffer.
> > > > +
> > > > +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per
> > > processor architecture or overlap with MMRAM.
> > > > +  @retval EFI_SUCCESS            Memory is copied.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +MmCopyMemToMmram (
> > > > +  OUT VOID       *DestinationBuffer,
> > > > +  IN CONST VOID  *SourceBuffer,
> > > > +  IN UINTN       Length
> > > > +  )
> > > > +{
> > > > +  if (!MmIsBufferOutsideMmValid
> > > ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {
> > > > +    DEBUG ((DEBUG_ERROR, "MmCopyMemToMmram: Security
> Violation:
> > > Source (0x%x), Length (0x%x)\n", SourceBuffer, Length));
> > > > +    return EFI_SECURITY_VIOLATION;
> > > > +  }
> > > > +  CopyMem (DestinationBuffer, SourceBuffer, Length);
> > > > +  return EFI_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Copies a source buffer (MMRAM) to a destination buffer
> (NON-MMRAM).
> > > > +
> > > > +  This function copies a source buffer (non-MMRAM) to a destination
> buffer
> > > (MMRAM).
> > > > +  It checks if destination buffer is valid per processor architecture and not
> > > overlap with MMRAM.
> > > > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > > > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > > > +  The implementation must be reentrant.
> > > > +
> > > > +  @param  DestinationBuffer   The pointer to the destination buffer of
> the
> > > memory copy.
> > > > +  @param  SourceBuffer        The pointer to the source buffer of
> the
> > > memory copy.
> > > > +  @param  Length              The number of bytes to copy from
> > > SourceBuffer to DestinationBuffer.
> > > > +
> > > > +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per
> > > processor architecture or overlap with MMRAM.
> > > > +  @retval EFI_SUCCESS            Memory is copied.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +MmCopyMemFromMmram (
> > > > +  OUT VOID       *DestinationBuffer,
> > > > +  IN CONST VOID  *SourceBuffer,
> > > > +  IN UINTN       Length
> > > > +  )
> > > > +{
> > > > +  if (!MmIsBufferOutsideMmValid
> > > ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {
> > > > +    DEBUG ((DEBUG_ERROR, "MmCopyMemFromMmram: Security
> > > Violation: Destination (0x%x), Length (0x%x)\n", DestinationBuffer, Length));
> > > > +    return EFI_SECURITY_VIOLATION;
> > > > +  }
> > > > +  CopyMem (DestinationBuffer, SourceBuffer, Length);
> > > > +  return EFI_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Copies a source buffer (NON-MMRAM) to a destination buffer
> > > (NON-MMRAM).
> > > > +
> > > > +  This function copies a source buffer (non-MMRAM) to a destination
> buffer
> > > (MMRAM).
> > > > +  It checks if source buffer and destination buffer are valid per processor
> > > architecture and not overlap with MMRAM.
> > > > +  If the check passes, it copies memory and returns EFI_SUCCESS.
> > > > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > > > +  The implementation must be reentrant, and it must handle the case
> where
> > > source buffer overlaps destination buffer.
> > > > +
> > > > +  @param  DestinationBuffer   The pointer to the destination buffer of
> the
> > > memory copy.
> > > > +  @param  SourceBuffer        The pointer to the source buffer of
> the
> > > memory copy.
> > > > +  @param  Length              The number of bytes to copy from
> > > SourceBuffer to DestinationBuffer.
> > > > +
> > > > +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per
> > > processor architecture or overlap with MMRAM.
> > > > +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per
> > > processor architecture or overlap with MMRAM.
> > > > +  @retval EFI_SUCCESS            Memory is copied.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +MmCopyMem (
> > > > +  OUT VOID       *DestinationBuffer,
> > > > +  IN CONST VOID  *SourceBuffer,
> > > > +  IN UINTN       Length
> > > > +  )
> > > > +{
> > > > +  if (!MmIsBufferOutsideMmValid
> > > ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {
> > > > +    DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation:
> Destination
> > > (0x%x), Length (0x%x)\n", DestinationBuffer, Length));
> > > > +    return EFI_SECURITY_VIOLATION;
> > > > +  }
> > > > +  if (!MmIsBufferOutsideMmValid
> > > ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {
> > > > +    DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation: Source
> > > (0x%x), Length (0x%x)\n", SourceBuffer, Length));
> > > > +    return EFI_SECURITY_VIOLATION;
> > > > +  }
> > > > +  CopyMem (DestinationBuffer, SourceBuffer, Length);
> > > > +  return EFI_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Fills a target buffer (NON-MMRAM) with a byte value.
> > > > +
> > > > +  This function fills a target buffer (non-MMRAM) with a byte value.
> > > > +  It checks if target buffer is valid per processor architecture and not
> overlap
> > > with MMRAM.
> > > > +  If the check passes, it fills memory and returns EFI_SUCCESS.
> > > > +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> > > > +
> > > > +  @param  Buffer    The memory to set.
> > > > +  @param  Length    The number of bytes to set.
> > > > +  @param  Value     The value with which to fill Length bytes of
> Buffer.
> > > > +
> > > > +  @retval EFI_SECURITY_VIOLATION The Buffer is invalid per processor
> > > architecture or overlap with MMRAM.
> > > > +  @retval EFI_SUCCESS            Memory is set.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +MmSetMem (
> > > > +  OUT VOID  *Buffer,
> > > > +  IN UINTN  Length,
> > > > +  IN UINT8  Value
> > > > +  )
> > > > +{
> > > > +  if (!MmIsBufferOutsideMmValid
> ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer,
> > > Length)) {
> > > > +    DEBUG ((DEBUG_ERROR, "MmSetMem: Security Violation: Source
> > > (0x%x), Length (0x%x)\n", Buffer, Length));
> > > > +    return EFI_SECURITY_VIOLATION;
> > > > +  }
> > > > +  SetMem (Buffer, Length, Value);
> > > > +  return EFI_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  The constructor function initializes the Mm Mem library
> > > > +
> > > > +  @param  ImageHandle   The firmware allocated handle for the EFI
> > > image.
> > > > +  @param  SystemTable   A pointer to the EFI System Table.
> > > > +
> > > > +  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +MemLibConstructor (
> > > > +  IN EFI_HANDLE             ImageHandle,
> > > > +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> > > > +  )
> > > > +{
> > > > +
> > > > +  //
> > > > +  // Calculate and save maximum support address
> > > > +  //
> > > > +  MmMemLibInternalCalculateMaximumSupportAddress ();
> > > > +
> > > > +  return EFI_SUCCESS;
> > > > +}
> > > > diff --git a/StandaloneMmPkg/Library/MemLib/MemLib.inf
> > > b/StandaloneMmPkg/Library/MemLib/MemLib.inf
> > > > new file mode 100644
> > > > index 0000000000..52b7c06397
> > > > --- /dev/null
> > > > +++ b/StandaloneMmPkg/Library/MemLib/MemLib.inf
> > > > @@ -0,0 +1,47 @@
> > > > +## @file
> > > > +#  Instance of MM memory check library.
> > > > +#
> > > > +#  MM memory check library library implementation. This library
> consumes
> > > MM_ACCESS_PROTOCOL
> > > > +#  to get MMRAM information. In order to use this library instance, the
> > > platform should produce
> > > > +#  all MMRAM range via MM_ACCESS_PROTOCOL, including the range
> for
> > > firmware (like MM Core
> > > > +#  and MM driver) and/or specific dedicated hardware.
> > > > +#
> > > > +#  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > > > +#  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > > > +#
> > > > +#  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
> > > > +#
> > > > +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS
> IS"
> > > BASIS,
> > > > +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> > > EXPRESS OR IMPLIED.
> > > > +#
> > > > +##
> > > > +
> > > > +[Defines]
> > > > +  INF_VERSION                    = 0x0001001A
> > > > +  BASE_NAME                      = MemLib
> > > > +  FILE_GUID                      =
> > > EA355F14-6409-4716-829F-37B3BC7C7F26
> > > > +  MODULE_TYPE                    = MM_STANDALONE
> > > > +  VERSION_STRING                 = 1.0
> > > > +  PI_SPECIFICATION_VERSION       = 0x00010032
> > > > +  LIBRARY_CLASS                  = MemLib|MM_STANDALONE
> > > MM_CORE_STANDALONE
> > > > +  CONSTRUCTOR                    = MemLibConstructor
> > > > +
> > > > +#
> > > > +# The following information is for reference only and not required by the
> > > build tools.
> > > > +#
> > > > +#  VALID_ARCHITECTURES           = AARCH64
> > > > +#
> > > > +
> > > > +[Sources.AARCH64]
> > > > +  Arm/MemLib.c
> > > > +
> > > > +[Packages]
> > > > +  MdePkg/MdePkg.dec
> > > > +  StandaloneMmPkg/StandaloneMmPkg.dec
> > > > +
> > > > +[LibraryClasses]
> > > > +  BaseMemoryLib
> > > > +  DebugLib
> > > > --
> > > > 2.16.2
> > > >
> > > _______________________________________________
> > > edk2-devel mailing list
> > > edk2-devel@lists.01.org
> > > https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 10/18] StandaloneMmPkg/HobLib: Add AARCH64 Specific HOB Library for management mode.
  2018-04-25 14:50   ` Achin Gupta
@ 2018-04-26 13:04     ` Yao, Jiewen
  2018-05-04 23:22       ` Supreeth Venkatesh
  2018-05-04 23:25     ` Supreeth Venkatesh
  1 sibling, 1 reply; 70+ messages in thread
From: Yao, Jiewen @ 2018-04-26 13:04 UTC (permalink / raw)
  To: Achin Gupta, Supreeth Venkatesh
  Cc: edk2-devel@lists.01.org, Kinney, Michael D, Gao, Liming,
	leif.lindholm@linaro.org, ard.biesheuvel@linaro.org, nd@arm.com

Maybe we can use same layout as MmMemLib.

It seems only HobConstructor() is Arm specific. Other functions are quite generic.

Thank you

> -----Original Message-----
> From: Achin Gupta [mailto:achin.gupta@arm.com]
> Sent: Wednesday, April 25, 2018 7:50 AM
> To: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> Cc: edk2-devel@lists.01.org; Kinney, Michael D <michael.d.kinney@intel.com>;
> Gao, Liming <liming.gao@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>;
> leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd@arm.com
> Subject: Re: [PATCH v1 10/18] StandaloneMmPkg/HobLib: Add AARCH64
> Specific HOB Library for management mode.
> 
> Hi Supreeth,
> 
> On Fri, Apr 06, 2018 at 03:42:15PM +0100, Supreeth Venkatesh wrote:
> > The Standalone MM environment is initialized during the SEC phase on ARM
> > Standard Platforms. The MM Core driver implements an entry point module
> > which is architecture specific and runs prior to the generic core driver
> > code. The former creates a Hob list that the latter consumes. This
> > happens in the same phase.
> >
> > This patch implements a Hob library that can be used by the entry point
> > module to produce a Hob list and by the core driver code to consume it.
> 
> References to DXE core need to be removed and the copyright years needs to be
> updated.
> 
> I think it is worth getting this hoblib reviewed by the ArmPkg maintainers.
> 
> Acked-by: Achin Gupta <achin.gupta@arm.com>
> 
> >
> > Contributed-under: TianoCore Contribution Agreement 1.1
> > Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> > Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> > ---
> >  StandaloneMmPkg/Library/HobLib/Arm/HobLib.c | 697
> ++++++++++++++++++++++++++++
> >  StandaloneMmPkg/Library/HobLib/HobLib.inf   |  45 ++
> >  2 files changed, 742 insertions(+)
> >  create mode 100644 StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
> >  create mode 100644 StandaloneMmPkg/Library/HobLib/HobLib.inf
> >
> > diff --git a/StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
> b/StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
> > new file mode 100644
> > index 0000000000..62abf47f95
> > --- /dev/null
> > +++ b/StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
> > @@ -0,0 +1,697 @@
> > +/** @file
> > +  HOB Library implementation for DxeCore driver.
> > +
> > +Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
> > +Copyright (c) 2017, ARM Limited. All rights reserved.<BR>
> > +
> > +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.
> > +
> > +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +
> > +**/
> > +
> > +#include <PiMm.h>
> > +
> > +#include <Library/HobLib.h>
> > +#include <Library/DebugLib.h>
> > +#include <Library/BaseMemoryLib.h>
> > +
> > +#include <Guid/MemoryAllocationHob.h>
> > +
> > +//
> > +// Cache copy of HobList pointer.
> > +//
> > +VOID *gHobList = NULL;
> > +
> > +/**
> > +  Returns the pointer to the HOB list.
> > +
> > +  This function returns the pointer to first HOB in the list.
> > +  For PEI phase, the PEI service GetHobList() can be used to retrieve the
> pointer
> > +  to the HOB list.  For the DXE phase, the HOB list pointer can be retrieved
> through
> > +  the EFI System Table by looking up theHOB list GUID in the System
> Configuration Table.
> > +  Since the System Configuration Table does not exist that the time the DXE
> Core is
> > +  launched, the DXE Core uses a global variable from the DXE Core Entry
> Point Library
> > +  to manage the pointer to the HOB list.
> > +
> > +  If the pointer to the HOB list is NULL, then ASSERT().
> > +
> > +  @return The pointer to the HOB list.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +GetHobList (
> > +  VOID
> > +  )
> > +{
> > +  ASSERT (gHobList != NULL);
> > +  return gHobList;
> > +}
> > +
> > +/**
> > +  Returns the next instance of a HOB type from the starting HOB.
> > +
> > +  This function searches the first instance of a HOB type from the starting
> HOB pointer.
> > +  If there does not exist such HOB type from the starting HOB pointer, it will
> return NULL.
> > +  In contrast with macro GET_NEXT_HOB(), this function does not skip the
> starting HOB pointer
> > +  unconditionally: it returns HobStart back if HobStart itself meets the
> requirement;
> > +  caller is required to use GET_NEXT_HOB() if it wishes to skip current
> HobStart.
> > +
> > +  If HobStart is NULL, then ASSERT().
> > +
> > +  @param  Type          The HOB type to return.
> > +  @param  HobStart      The starting HOB pointer to search from.
> > +
> > +  @return The next instance of a HOB type from the starting HOB.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +GetNextHob (
> > +  IN UINT16                 Type,
> > +  IN CONST VOID             *HobStart
> > +  )
> > +{
> > +  EFI_PEI_HOB_POINTERS  Hob;
> > +
> > +  ASSERT (HobStart != NULL);
> > +
> > +  Hob.Raw = (UINT8 *) HobStart;
> > +  //
> > +  // Parse the HOB list until end of list or matching type is found.
> > +  //
> > +  while (!END_OF_HOB_LIST (Hob)) {
> > +    if (Hob.Header->HobType == Type) {
> > +      return Hob.Raw;
> > +    }
> > +    Hob.Raw = GET_NEXT_HOB (Hob);
> > +  }
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Returns the first instance of a HOB type among the whole HOB list.
> > +
> > +  This function searches the first instance of a HOB type among the whole
> HOB list.
> > +  If there does not exist such HOB type in the HOB list, it will return NULL.
> > +
> > +  If the pointer to the HOB list is NULL, then ASSERT().
> > +
> > +  @param  Type          The HOB type to return.
> > +
> > +  @return The next instance of a HOB type from the starting HOB.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +GetFirstHob (
> > +  IN UINT16                 Type
> > +  )
> > +{
> > +  VOID      *HobList;
> > +
> > +  HobList = GetHobList ();
> > +  return GetNextHob (Type, HobList);
> > +}
> > +
> > +/**
> > +  Returns the next instance of the matched GUID HOB from the starting HOB.
> > +
> > +  This function searches the first instance of a HOB from the starting HOB
> pointer.
> > +  Such HOB should satisfy two conditions:
> > +  its HOB type is EFI_HOB_TYPE_GUID_EXTENSION, and its GUID Name
> equals to the input Guid.
> > +  If such a HOB from the starting HOB pointer does not exist, it will return
> NULL.
> > +  Caller is required to apply GET_GUID_HOB_DATA () and
> GET_GUID_HOB_DATA_SIZE ()
> > +  to extract the data section and its size information, respectively.
> > +  In contrast with macro GET_NEXT_HOB(), this function does not skip the
> starting HOB pointer
> > +  unconditionally: it returns HobStart back if HobStart itself meets the
> requirement;
> > +  caller is required to use GET_NEXT_HOB() if it wishes to skip current
> HobStart.
> > +
> > +  If Guid is NULL, then ASSERT().
> > +  If HobStart is NULL, then ASSERT().
> > +
> > +  @param  Guid          The GUID to match with in the HOB list.
> > +  @param  HobStart      A pointer to a Guid.
> > +
> > +  @return The next instance of the matched GUID HOB from the starting
> HOB.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +GetNextGuidHob (
> > +  IN CONST EFI_GUID         *Guid,
> > +  IN CONST VOID             *HobStart
> > +  )
> > +{
> > +  EFI_PEI_HOB_POINTERS  GuidHob;
> > +
> > +  GuidHob.Raw = (UINT8 *) HobStart;
> > +  while ((GuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION,
> GuidHob.Raw)) != NULL) {
> > +    if (CompareGuid (Guid, &GuidHob.Guid->Name)) {
> > +      break;
> > +    }
> > +    GuidHob.Raw = GET_NEXT_HOB (GuidHob);
> > +  }
> > +  return GuidHob.Raw;
> > +}
> > +
> > +/**
> > +  Returns the first instance of the matched GUID HOB among the whole HOB
> list.
> > +
> > +  This function searches the first instance of a HOB among the whole HOB
> list.
> > +  Such HOB should satisfy two conditions:
> > +  its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name
> equals to the input Guid.
> > +  If such a HOB from the starting HOB pointer does not exist, it will return
> NULL.
> > +  Caller is required to apply GET_GUID_HOB_DATA () and
> GET_GUID_HOB_DATA_SIZE ()
> > +  to extract the data section and its size information, respectively.
> > +
> > +  If the pointer to the HOB list is NULL, then ASSERT().
> > +  If Guid is NULL, then ASSERT().
> > +
> > +  @param  Guid          The GUID to match with in the HOB list.
> > +
> > +  @return The first instance of the matched GUID HOB among the whole
> HOB list.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +GetFirstGuidHob (
> > +  IN CONST EFI_GUID         *Guid
> > +  )
> > +{
> > +  VOID      *HobList;
> > +
> > +  HobList = GetHobList ();
> > +  return GetNextGuidHob (Guid, HobList);
> > +}
> > +
> > +/**
> > +  Get the system boot mode from the HOB list.
> > +
> > +  This function returns the system boot mode information from the
> > +  PHIT HOB in HOB list.
> > +
> > +  If the pointer to the HOB list is NULL, then ASSERT().
> > +
> > +  @param  VOID
> > +
> > +  @return The Boot Mode.
> > +
> > +**/
> > +EFI_BOOT_MODE
> > +EFIAPI
> > +GetBootModeHob (
> > +  VOID
> > +  )
> > +{
> > +  EFI_HOB_HANDOFF_INFO_TABLE    *HandOffHob;
> > +
> > +  HandOffHob = (EFI_HOB_HANDOFF_INFO_TABLE *) GetHobList ();
> > +
> > +  return  HandOffHob->BootMode;
> > +}
> > +
> > +
> > +/**
> > +
> > +
> > +**/
> > +EFI_HOB_HANDOFF_INFO_TABLE*
> > +HobConstructor (
> > +  IN VOID   *EfiMemoryBegin,
> > +  IN UINTN  EfiMemoryLength,
> > +  IN VOID   *EfiFreeMemoryBottom,
> > +  IN VOID   *EfiFreeMemoryTop
> > +  )
> > +{
> > +  EFI_HOB_HANDOFF_INFO_TABLE  *Hob;
> > +  EFI_HOB_GENERIC_HEADER      *HobEnd;
> > +
> > +  Hob    = EfiFreeMemoryBottom;
> > +  HobEnd = (EFI_HOB_GENERIC_HEADER *)(Hob+1);
> > +
> > +  Hob->Header.HobType     = EFI_HOB_TYPE_HANDOFF;
> > +  Hob->Header.HobLength   = sizeof(EFI_HOB_HANDOFF_INFO_TABLE);
> > +  Hob->Header.Reserved    = 0;
> > +
> > +  HobEnd->HobType     = EFI_HOB_TYPE_END_OF_HOB_LIST;
> > +  HobEnd->HobLength   = sizeof(EFI_HOB_GENERIC_HEADER);
> > +  HobEnd->Reserved    = 0;
> > +
> > +  Hob->Version             = EFI_HOB_HANDOFF_TABLE_VERSION;
> > +  Hob->BootMode            = BOOT_WITH_FULL_CONFIGURATION;
> > +
> > +  Hob->EfiMemoryTop        = (UINTN)EfiMemoryBegin +
> EfiMemoryLength;
> > +  Hob->EfiMemoryBottom     = (UINTN)EfiMemoryBegin;
> > +  Hob->EfiFreeMemoryTop    = (UINTN)EfiFreeMemoryTop;
> > +  Hob->EfiFreeMemoryBottom =
> (EFI_PHYSICAL_ADDRESS)(UINTN)(HobEnd+1);
> > +  Hob->EfiEndOfHobList     = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd;
> > +
> > +  gHobList = Hob;
> > +
> > +  return Hob;
> > +}
> > +
> > +VOID *
> > +CreateHob (
> > +  IN  UINT16    HobType,
> > +  IN  UINT16    HobLength
> > +  )
> > +{
> > +  EFI_HOB_HANDOFF_INFO_TABLE  *HandOffHob;
> > +  EFI_HOB_GENERIC_HEADER      *HobEnd;
> > +  EFI_PHYSICAL_ADDRESS        FreeMemory;
> > +  VOID                        *Hob;
> > +
> > +  HandOffHob = GetHobList ();
> > +
> > +  HobLength = (UINT16)((HobLength + 0x7) & (~0x7));
> > +
> > +  FreeMemory = HandOffHob->EfiFreeMemoryTop -
> HandOffHob->EfiFreeMemoryBottom;
> > +
> > +  if (FreeMemory < HobLength) {
> > +      return NULL;
> > +  }
> > +
> > +  Hob = (VOID*) (UINTN) HandOffHob->EfiEndOfHobList;
> > +  ((EFI_HOB_GENERIC_HEADER*) Hob)->HobType = HobType;
> > +  ((EFI_HOB_GENERIC_HEADER*) Hob)->HobLength = HobLength;
> > +  ((EFI_HOB_GENERIC_HEADER*) Hob)->Reserved = 0;
> > +
> > +  HobEnd = (EFI_HOB_GENERIC_HEADER*) ((UINTN)Hob + HobLength);
> > +  HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN)
> HobEnd;
> > +
> > +  HobEnd->HobType   = EFI_HOB_TYPE_END_OF_HOB_LIST;
> > +  HobEnd->HobLength = sizeof(EFI_HOB_GENERIC_HEADER);
> > +  HobEnd->Reserved  = 0;
> > +  HobEnd++;
> > +  HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS) (UINTN)
> HobEnd;
> > +
> > +  return Hob;
> > +}
> > +
> > +/**
> > +  Builds a HOB for a loaded PE32 module.
> > +
> > +  This function builds a HOB for a loaded PE32 module.
> > +  It can only be invoked during PEI phase;
> > +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> > +  If ModuleName is NULL, then ASSERT().
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  ModuleName              The GUID File Name of the
> module.
> > +  @param  MemoryAllocationModule  The 64 bit physical address of the
> module.
> > +  @param  ModuleLength            The length of the module in bytes.
> > +  @param  EntryPoint              The 64 bit physical address of the
> module entry point.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildModuleHob (
> > +  IN CONST EFI_GUID         *ModuleName,
> > +  IN EFI_PHYSICAL_ADDRESS   MemoryAllocationModule,
> > +  IN UINT64                 ModuleLength,
> > +  IN EFI_PHYSICAL_ADDRESS   EntryPoint
> > +  )
> > +{
> > +  EFI_HOB_MEMORY_ALLOCATION_MODULE  *Hob;
> > +
> > +  ASSERT (((MemoryAllocationModule & (EFI_PAGE_SIZE - 1)) == 0) &&
> > +          ((ModuleLength & (EFI_PAGE_SIZE - 1)) == 0));
> > +
> > +  Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof
> (EFI_HOB_MEMORY_ALLOCATION_MODULE));
> > +
> > +  CopyGuid (&(Hob->MemoryAllocationHeader.Name),
> &gEfiHobMemoryAllocModuleGuid);
> > +  Hob->MemoryAllocationHeader.MemoryBaseAddress =
> MemoryAllocationModule;
> > +  Hob->MemoryAllocationHeader.MemoryLength      = ModuleLength;
> > +  Hob->MemoryAllocationHeader.MemoryType        =
> EfiBootServicesCode;
> > +
> > +  //
> > +  // Zero the reserved space to match HOB spec
> > +  //
> > +  ZeroMem (Hob->MemoryAllocationHeader.Reserved, sizeof
> (Hob->MemoryAllocationHeader.Reserved));
> > +
> > +  CopyGuid (&Hob->ModuleName, ModuleName);
> > +  Hob->EntryPoint = EntryPoint;
> > +}
> > +
> > +/**
> > +  Builds a HOB that describes a chunk of system memory.
> > +
> > +  This function builds a HOB that describes a chunk of system memory.
> > +  It can only be invoked during PEI phase;
> > +  for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.
> > +
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  ResourceType        The type of resource described by this
> HOB.
> > +  @param  ResourceAttribute   The resource attributes of the memory
> described by this HOB.
> > +  @param  PhysicalStart       The 64 bit physical address of memory
> described by this HOB.
> > +  @param  NumberOfBytes       The length of the memory described by
> this HOB in bytes.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildResourceDescriptorHob (
> > +  IN EFI_RESOURCE_TYPE            ResourceType,
> > +  IN EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttribute,
> > +  IN EFI_PHYSICAL_ADDRESS         PhysicalStart,
> > +  IN UINT64                       NumberOfBytes
> > +  )
> > +{
> > +  EFI_HOB_RESOURCE_DESCRIPTOR  *Hob;
> > +
> > +  Hob = CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof
> (EFI_HOB_RESOURCE_DESCRIPTOR));
> > +  ASSERT(Hob != NULL);
> > +
> > +  Hob->ResourceType      = ResourceType;
> > +  Hob->ResourceAttribute = ResourceAttribute;
> > +  Hob->PhysicalStart     = PhysicalStart;
> > +  Hob->ResourceLength    = NumberOfBytes;
> > +}
> > +
> > +/**
> > +  Builds a GUID HOB with a certain data length.
> > +
> > +  This function builds a customized HOB tagged with a GUID for identification
> > +  and returns the start address of GUID HOB data so that caller can fill the
> customized data.
> > +  The HOB Header and Name field is already stripped.
> > +  It can only be invoked during PEI phase;
> > +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> > +  If Guid is NULL, then ASSERT().
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +  If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
> > +
> > +  @param  Guid          The GUID to tag the customized HOB.
> > +  @param  DataLength    The size of the data payload for the GUID HOB.
> > +
> > +  @return The start address of GUID HOB data.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +BuildGuidHob (
> > +  IN CONST EFI_GUID              *Guid,
> > +  IN UINTN                       DataLength
> > +  )
> > +{
> > +  EFI_HOB_GUID_TYPE *Hob;
> > +
> > +  //
> > +  // Make sure that data length is not too long.
> > +  //
> > +  ASSERT (DataLength <= (0xffff - sizeof (EFI_HOB_GUID_TYPE)));
> > +
> > +  Hob = CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16) (sizeof
> (EFI_HOB_GUID_TYPE) + DataLength));
> > +  CopyGuid (&Hob->Name, Guid);
> > +  return Hob + 1;
> > +}
> > +
> > +
> > +/**
> > +  Copies a data buffer to a newly-built HOB.
> > +
> > +  This function builds a customized HOB tagged with a GUID for
> identification,
> > +  copies the input data to the HOB data field and returns the start address of
> the GUID HOB data.
> > +  The HOB Header and Name field is already stripped.
> > +  It can only be invoked during PEI phase;
> > +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> > +  If Guid is NULL, then ASSERT().
> > +  If Data is NULL and DataLength > 0, then ASSERT().
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +  If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
> > +
> > +  @param  Guid          The GUID to tag the customized HOB.
> > +  @param  Data          The data to be copied into the data field of the
> GUID HOB.
> > +  @param  DataLength    The size of the data payload for the GUID HOB.
> > +
> > +  @return The start address of GUID HOB data.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +BuildGuidDataHob (
> > +  IN CONST EFI_GUID              *Guid,
> > +  IN VOID                        *Data,
> > +  IN UINTN                       DataLength
> > +  )
> > +{
> > +  VOID  *HobData;
> > +
> > +  ASSERT (Data != NULL || DataLength == 0);
> > +
> > +  HobData = BuildGuidHob (Guid, DataLength);
> > +
> > +  return CopyMem (HobData, Data, DataLength);
> > +}
> > +
> > +/**
> > +  Builds a Firmware Volume HOB.
> > +
> > +  This function builds a Firmware Volume HOB.
> > +  It can only be invoked during PEI phase;
> > +  for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.
> > +
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  BaseAddress   The base address of the Firmware Volume.
> > +  @param  Length        The size of the Firmware Volume in bytes.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildFvHob (
> > +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> > +  IN UINT64                      Length
> > +  )
> > +{
> > +  EFI_HOB_FIRMWARE_VOLUME  *Hob;
> > +
> > +  Hob = CreateHob (EFI_HOB_TYPE_FV, sizeof
> (EFI_HOB_FIRMWARE_VOLUME));
> > +
> > +  Hob->BaseAddress = BaseAddress;
> > +  Hob->Length      = Length;
> > +}
> > +
> > +
> > +/**
> > +  Builds a EFI_HOB_TYPE_FV2 HOB.
> > +
> > +  This function builds a EFI_HOB_TYPE_FV2 HOB.
> > +  It can only be invoked during PEI phase;
> > +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  BaseAddress   The base address of the Firmware Volume.
> > +  @param  Length        The size of the Firmware Volume in bytes.
> > +  @param  FvName       The name of the Firmware Volume.
> > +  @param  FileName      The name of the file.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildFv2Hob (
> > +  IN          EFI_PHYSICAL_ADDRESS        BaseAddress,
> > +  IN          UINT64                      Length,
> > +  IN CONST    EFI_GUID                    *FvName,
> > +  IN CONST    EFI_GUID                    *FileName
> > +  )
> > +{
> > +  EFI_HOB_FIRMWARE_VOLUME2  *Hob;
> > +
> > +  Hob = CreateHob (EFI_HOB_TYPE_FV2, sizeof
> (EFI_HOB_FIRMWARE_VOLUME2));
> > +
> > +  Hob->BaseAddress = BaseAddress;
> > +  Hob->Length      = Length;
> > +  CopyGuid (&Hob->FvName, FvName);
> > +  CopyGuid (&Hob->FileName, FileName);
> > +}
> > +
> > +
> > +/**
> > +  Builds a HOB for the CPU.
> > +
> > +  This function builds a HOB for the CPU.
> > +  It can only be invoked during PEI phase;
> > +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  SizeOfMemorySpace   The maximum physical memory
> addressability of the processor.
> > +  @param  SizeOfIoSpace       The maximum physical I/O addressability
> of the processor.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildCpuHob (
> > +  IN UINT8                       SizeOfMemorySpace,
> > +  IN UINT8                       SizeOfIoSpace
> > +  )
> > +{
> > +  EFI_HOB_CPU  *Hob;
> > +
> > +  Hob = CreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));
> > +
> > +  Hob->SizeOfMemorySpace = SizeOfMemorySpace;
> > +  Hob->SizeOfIoSpace     = SizeOfIoSpace;
> > +
> > +  //
> > +  // Zero the reserved space to match HOB spec
> > +  //
> > +  ZeroMem (Hob->Reserved, sizeof (Hob->Reserved));
> > +}
> > +
> > +/**
> > +  Builds a HOB for the memory allocation.
> > +
> > +  This function builds a HOB for the memory allocation.
> > +  It can only be invoked during PEI phase;
> > +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  BaseAddress   The 64 bit physical address of the memory.
> > +  @param  Length        The length of the memory allocation in bytes.
> > +  @param  MemoryType    Type of memory allocated by this HOB.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildMemoryAllocationHob (
> > +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> > +  IN UINT64                      Length,
> > +  IN EFI_MEMORY_TYPE             MemoryType
> > +  )
> > +{
> > +  EFI_HOB_MEMORY_ALLOCATION  *Hob;
> > +
> > +  ASSERT (((BaseAddress & (EFI_PAGE_SIZE - 1)) == 0) &&
> > +          ((Length & (EFI_PAGE_SIZE - 1)) == 0));
> > +
> > +  Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof
> (EFI_HOB_MEMORY_ALLOCATION));
> > +
> > +  ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));
> > +  Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
> > +  Hob->AllocDescriptor.MemoryLength      = Length;
> > +  Hob->AllocDescriptor.MemoryType        = MemoryType;
> > +  //
> > +  // Zero the reserved space to match HOB spec
> > +  //
> > +  ZeroMem (Hob->AllocDescriptor.Reserved, sizeof
> (Hob->AllocDescriptor.Reserved));
> > +}
> > +
> > +/**
> > +  Builds a HOB that describes a chunk of system memory with Owner GUID.
> > +
> > +  This function builds a HOB that describes a chunk of system memory.
> > +  It can only be invoked during PEI phase;
> > +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> > +
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  ResourceType        The type of resource described by this
> HOB.
> > +  @param  ResourceAttribute   The resource attributes of the memory
> described by this HOB.
> > +  @param  PhysicalStart       The 64 bit physical address of memory
> described by this HOB.
> > +  @param  NumberOfBytes       The length of the memory described by
> this HOB in bytes.
> > +  @param  OwnerGUID           GUID for the owner of this resource.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildResourceDescriptorWithOwnerHob (
> > +  IN EFI_RESOURCE_TYPE            ResourceType,
> > +  IN EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttribute,
> > +  IN EFI_PHYSICAL_ADDRESS         PhysicalStart,
> > +  IN UINT64                       NumberOfBytes,
> > +  IN EFI_GUID                     *OwnerGUID
> > +  )
> > +{
> > +  //
> > +  // PEI HOB is read only for DXE phase
> > +  //
> > +  ASSERT (FALSE);
> > +}
> > +
> > +/**
> > +  Builds a Capsule Volume HOB.
> > +
> > +  This function builds a Capsule Volume HOB.
> > +  It can only be invoked during PEI phase;
> > +  for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.
> > +
> > +  If the platform does not support Capsule Volume HOBs, then ASSERT().
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  BaseAddress   The base address of the Capsule Volume.
> > +  @param  Length        The size of the Capsule Volume in bytes.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildCvHob (
> > +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> > +  IN UINT64                      Length
> > +  )
> > +{
> > +  //
> > +  // PEI HOB is read only for DXE phase
> > +  //
> > +  ASSERT (FALSE);
> > +}
> > +
> > +
> > +/**
> > +  Builds a HOB for the BSP store.
> > +
> > +  This function builds a HOB for BSP store.
> > +  It can only be invoked during PEI phase;
> > +  for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.
> > +
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  BaseAddress   The 64 bit physical address of the BSP.
> > +  @param  Length        The length of the BSP store in bytes.
> > +  @param  MemoryType    Type of memory allocated by this HOB.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildBspStoreHob (
> > +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> > +  IN UINT64                      Length,
> > +  IN EFI_MEMORY_TYPE             MemoryType
> > +  )
> > +{
> > +  //
> > +  // PEI HOB is read only for DXE phase
> > +  //
> > +  ASSERT (FALSE);
> > +}
> > +
> > +/**
> > +  Builds a HOB for the Stack.
> > +
> > +  This function builds a HOB for the stack.
> > +  It can only be invoked during PEI phase;
> > +  for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  BaseAddress   The 64 bit physical address of the Stack.
> > +  @param  Length        The length of the stack in bytes.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildStackHob (
> > +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> > +  IN UINT64                      Length
> > +  )
> > +{
> > +  //
> > +  // PEI HOB is read only for DXE phase
> > +  //
> > +  ASSERT (FALSE);
> > +}
> > diff --git a/StandaloneMmPkg/Library/HobLib/HobLib.inf
> b/StandaloneMmPkg/Library/HobLib/HobLib.inf
> > new file mode 100644
> > index 0000000000..42273b6d66
> > --- /dev/null
> > +++ b/StandaloneMmPkg/Library/HobLib/HobLib.inf
> > @@ -0,0 +1,45 @@
> > +## @file
> > +# Instance of HOB Library for DXE Core.
> > +#
> > +# HOB Library implementation for the DXE Core. Does not have a constructor.
> > +#  Uses gHobList defined in the DXE Core Entry Point Library.
> > +#
> > +# Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
> > +# Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > +#
> > +#  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.
> > +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +#
> > +#
> > +##
> > +
> > +[Defines]
> > +  INF_VERSION                    = 0x0001001A
> > +  BASE_NAME                      = HobLib
> > +  FILE_GUID                      =
> CF56EF2C-68D8-4BD5-9A8B-8A7BFCFF751C
> > +  MODULE_TYPE                    = MM_CORE_STANDALONE
> > +  VERSION_STRING                 = 1.0
> > +  PI_SPECIFICATION_VERSION       = 0x00010032
> > +  LIBRARY_CLASS                  = HobLib|MM_CORE_STANDALONE
> MM_STANDALONE
> > +
> > +#
> > +#  VALID_ARCHITECTURES           = AARCH64
> > +#
> > +
> > +[Sources.AARCH64]
> > +  Arm/HobLib.c
> > +
> > +[Packages]
> > +  MdePkg/MdePkg.dec
> > +
> > +
> > +[LibraryClasses]
> > +  BaseMemoryLib
> > +  DebugLib
> > +
> > +[Guids]
> > +  gEfiHobListGuid                               ## CONSUMES  ##
> SystemTable
> > --
> > 2.16.2
> >


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 09/18] StandaloneMmPkg/MemoryAllocationLib: Add MM memory allocation library.
  2018-04-25 14:33   ` Achin Gupta
@ 2018-04-26 13:05     ` Yao, Jiewen
  2018-05-04 23:23       ` Supreeth Venkatesh
  2018-05-04 23:21     ` Supreeth Venkatesh
  1 sibling, 1 reply; 70+ messages in thread
From: Yao, Jiewen @ 2018-04-26 13:05 UTC (permalink / raw)
  To: Achin Gupta, Supreeth Venkatesh
  Cc: ard.biesheuvel@linaro.org, edk2-devel@lists.01.org,
	leif.lindholm@linaro.org, Gao, Liming, Kinney, Michael D,
	nd@arm.com

Same comment as previous 2.

Maybe to separate ARM specific function from generic function.

Thank you



> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Achin
> Gupta
> Sent: Wednesday, April 25, 2018 7:34 AM
> To: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> Cc: ard.biesheuvel@linaro.org; edk2-devel@lists.01.org;
> leif.lindholm@linaro.org; Yao, Jiewen <jiewen.yao@intel.com>; Gao, Liming
> <liming.gao@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>;
> nd@arm.com
> Subject: Re: [edk2] [PATCH v1 09/18] StandaloneMmPkg/MemoryAllocationLib:
> Add MM memory allocation library.
> 
> Hi Supreeth,
> 
> On Fri, Apr 06, 2018 at 03:42:14PM +0100, Supreeth Venkatesh wrote:
> > This patch implements management mode memory allocation services.The
> > implementation borrows the MM Core Memory Allocation services as the
> > primitive for memory allocation instead of using MM System Table
> > services.
> 
> The commit message did not really register with me. Once the MMRAM ranges
> have
> been conveyed to the MMST memory allocation services (through
> MemoryAllocationLibConstructor()), all functions in MemoryAllocationLib.c use
> the MMST services. The message seems to indicate otherwise.
> 
> On Arm, the gEfiMmPeiMmramMemoryReserveGuid HOB is used to convey the
> MMRAM
> ranges. It seems x86 uses gMmCoreDataHobGuid HOB. So it worth getting this
> reviewed by Jiewen.
> 
> The copyright years in the files need to be updated.
> 
> With that in mind..
> 
> Acked-by: Achin Gupta <achin.gupta@arm.com>
> 
> >
> > Contributed-under: TianoCore Contribution Agreement 1.1
> > Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> > Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> > ---
> >  StandaloneMmPkg/Include/Guid/MmCoreData.h          | 132 +++
> >  StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h  |  62 ++
> >  .../MemoryAllocationLib/MemoryAllocationLib.c      | 907
> +++++++++++++++++++++
> >  .../MemoryAllocationLib/MemoryAllocationLib.inf    |  49 ++
> >  .../MemoryAllocationLib/MemoryAllocationServices.h |  38 +
> >  5 files changed, 1188 insertions(+)
> >  create mode 100644 StandaloneMmPkg/Include/Guid/MmCoreData.h
> >  create mode 100644
> StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
> >  create mode 100644
> StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
> >  create mode 100644
> StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
> >  create mode 100644
> StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.h
> >
> > diff --git a/StandaloneMmPkg/Include/Guid/MmCoreData.h
> b/StandaloneMmPkg/Include/Guid/MmCoreData.h
> > new file mode 100644
> > index 0000000000..c0ac772014
> > --- /dev/null
> > +++ b/StandaloneMmPkg/Include/Guid/MmCoreData.h
> > @@ -0,0 +1,132 @@
> > +/** @file
> > +  MM Core data.
> > +
> > +Copyright (c) 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 that accompanies this
> distribution.
> > +The full text of the license may be found at
> > +http://opensource.org/licenses/bsd-license.php.
> > +
> > +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +
> > +**/
> > +
> > +#ifndef __MM_CORE_DATA_H__
> > +#define __MM_CORE_DATA_H__
> > +
> > +#define MM_CORE_DATA_HOB_GUID \
> > +  { 0xa160bf99, 0x2aa4, 0x4d7d, { 0x99, 0x93, 0x89, 0x9c, 0xb1, 0x2d, 0xf3,
> 0x76 }}
> > +
> > +extern EFI_GUID gMmCoreDataHobGuid;
> > +
> > +typedef struct {
> > +  //
> > +  // Address pointer to MM_CORE_PRIVATE_DATA
> > +  //
> > +  EFI_PHYSICAL_ADDRESS   Address;
> > +} MM_CORE_DATA_HOB_DATA;
> > +
> > +
> > +///
> > +/// Define values for the communications buffer used when
> gEfiEventDxeDispatchGuid is
> > +/// event signaled.  This event is signaled by the DXE Core each time the DXE
> Core
> > +/// dispatcher has completed its work.  When this event is signaled, the MM
> Core
> > +/// if notified, so the MM Core can dispatch MM drivers.  If
> COMM_BUFFER_MM_DISPATCH_ERROR
> > +/// is returned in the communication buffer, then an error occurred
> dispatching MM
> > +/// Drivers.  If COMM_BUFFER_MM_DISPATCH_SUCCESS is returned, then
> the MM Core
> > +/// dispatched all the drivers it could.  If
> COMM_BUFFER_MM_DISPATCH_RESTART is
> > +/// returned, then the MM Core just dispatched the MM Driver that
> registered
> > +/// the MM Entry Point enabling the use of MM Mode.  In this case, the MM
> Core
> > +/// should be notified again to dispatch more MM Drivers using MM Mode.
> > +///
> > +#define COMM_BUFFER_MM_DISPATCH_ERROR    0x00
> > +#define COMM_BUFFER_MM_DISPATCH_SUCCESS  0x01
> > +#define COMM_BUFFER_MM_DISPATCH_RESTART  0x02
> > +
> > +///
> > +/// Signature for the private structure shared between the MM IPL and the
> MM Core
> > +///
> > +#define MM_CORE_PRIVATE_DATA_SIGNATURE  SIGNATURE_32 ('m', 'm', 'i',
> 'c')
> > +
> > +///
> > +/// Private structure that is used to share information between the MM IPL
> and
> > +/// the MM Core.  This structure is allocated from memory of type
> EfiRuntimeServicesData.
> > +/// Since runtime memory types are converted to available memory when a
> legacy boot
> > +/// is performed, the MM Core must not access any fields of this structure if a
> legacy
> > +/// boot is performed.  As a result, the MM IPL must create an event
> notification
> > +/// for the Legacy Boot event and notify the MM Core that a legacy boot is
> being
> > +/// performed.  The MM Core can then use this information to filter accesses
> to
> > +/// thos structure.
> > +///
> > +typedef struct {
> > +  UINT64                          Signature;
> > +
> > +  ///
> > +  /// The number of MMRAM ranges passed from the MM IPL to the MM
> Core.  The MM
> > +  /// Core uses these ranges of MMRAM to initialize the MM Core memory
> manager.
> > +  ///
> > +  UINT64                          MmramRangeCount;
> > +
> > +  ///
> > +  /// A table of MMRAM ranges passed from the MM IPL to the MM Core.
> The MM
> > +  /// Core uses these ranges of MMRAM to initialize the MM Core memory
> manager.
> > +  ///
> > +  EFI_PHYSICAL_ADDRESS            MmramRanges;
> > +
> > +  ///
> > +  /// The MM Foundation Entry Point.  The MM Core fills in this field when
> the
> > +  /// MM Core is initialized.  The MM IPL is responsbile for registering this
> entry
> > +  /// point with the MM Configuration Protocol.  The MM Configuration
> Protocol may
> > +  /// not be available at the time the MM IPL and MM Core are started, so the
> MM IPL
> > +  /// sets up a protocol notification on the MM Configuration Protocol and
> registers
> > +  /// the MM Foundation Entry Point as soon as the MM Configuration
> Protocol is
> > +  /// available.
> > +  ///
> > +  EFI_PHYSICAL_ADDRESS            MmEntryPoint;
> > +
> > +  ///
> > +  /// Boolean flag set to TRUE while an MMI is being processed by the MM
> Core.
> > +  ///
> > +  BOOLEAN                         MmEntryPointRegistered;
> > +
> > +  ///
> > +  /// Boolean flag set to TRUE while an MMI is being processed by the MM
> Core.
> > +  ///
> > +  BOOLEAN                         InMm;
> > +
> > +  ///
> > +  /// This field is set by the MM Core then the MM Core is initialized.  This
> field is
> > +  /// used by the MM Base 2 Protocol and MM Communication Protocol
> implementations in
> > +  /// the MM IPL.
> > +  ///
> > +  EFI_PHYSICAL_ADDRESS            Mmst;
> > +
> > +  ///
> > +  /// This field is used by the MM Communicatioon Protocol to pass a buffer
> into
> > +  /// a software MMI handler and for the software MMI handler to pass a
> buffer back to
> > +  /// the caller of the MM Communication Protocol.
> > +  ///
> > +  EFI_PHYSICAL_ADDRESS            CommunicationBuffer;
> > +
> > +  ///
> > +  /// This field is used by the MM Communicatioon Protocol to pass the size
> of a buffer,
> > +  /// in bytes, into a software MMI handler and for the software MMI
> handler to pass the
> > +  /// size, in bytes, of a buffer back to the caller of the MM Communication
> Protocol.
> > +  ///
> > +  UINT64                          BufferSize;
> > +
> > +  ///
> > +  /// This field is used by the MM Communication Protocol to pass the return
> status from
> > +  /// a software MMI handler back to the caller of the MM Communication
> Protocol.
> > +  ///
> > +  UINT64                          ReturnStatus;
> > +
> > +  EFI_PHYSICAL_ADDRESS            MmCoreImageBase;
> > +  UINT64                          MmCoreImageSize;
> > +  EFI_PHYSICAL_ADDRESS            MmCoreEntryPoint;
> > +
> > +  EFI_PHYSICAL_ADDRESS            StandaloneBfvAddress;
> > +} MM_CORE_PRIVATE_DATA;
> > +
> > +#endif
> > diff --git a/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
> b/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
> > new file mode 100644
> > index 0000000000..c4104b755d
> > --- /dev/null
> > +++ b/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
> > @@ -0,0 +1,62 @@
> > +/** @file
> > +  Definition of GUIDed HOB for reserving MMRAM regions.
> > +
> > +  This file defines:
> > +  * the GUID used to identify the GUID HOB for reserving MMRAM regions.
> > +  * the data structure of MMRAM descriptor to describe MMRAM candidate
> regions
> > +  * values of state of MMRAM candidate regions
> > +  * the GUID specific data structure of HOB for reserving MMRAM regions.
> > +  This GUIDed HOB can be used to convey the existence of the T-SEG
> reservation and H-SEG usage
> > +
> > +Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
> > +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > +
> > +This program and the accompanying materials are licensed and made
> available under
> > +the terms and conditions of the BSD License that accompanies this
> distribution.
> > +The full text of the license may be found at
> > +http://opensource.org/licenses/bsd-license.php.
> > +
> > +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +
> > +  @par Revision Reference:
> > +  GUIDs defined in MmCis spec version 0.9.
> > +
> > +**/
> > +
> > +#ifndef _EFI_MM_PEI_MMRAM_MEMORY_RESERVE_H_
> > +#define _EFI_MM_PEI_MMRAM_MEMORY_RESERVE_H_
> > +
> > +#define EFI_MM_PEI_MMRAM_MEMORY_RESERVE \
> > +  { \
> > +    0x0703f912, 0xbf8d, 0x4e2a, {0xbe, 0x07, 0xab, 0x27, 0x25, 0x25, 0xc5,
> 0x92 } \
> > +  }
> > +
> > +/**
> > +* GUID specific data structure of HOB for reserving MMRAM regions.
> > +*
> > +* Inconsistent with specification here:
> > +* EFI_HOB_MMRAM_DESCRIPTOR_BLOCK has been changed to
> EFI_MMRAM_HOB_DESCRIPTOR_BLOCK.
> > +* This inconsistency is kept in code in order for backward compatibility.
> > +**/
> > +typedef struct {
> > +  ///
> > +  /// Designates the number of possible regions in the system
> > +  /// that can be usable for MMRAM.
> > +  ///
> > +  /// Inconsistent with specification here:
> > +  /// In Framework MM CIS 0.91 specification, it defines the field type as
> UINTN.
> > +  /// However, HOBs are supposed to be CPU neutral, so UINT32 should be
> used instead.
> > +  ///
> > +  UINT32                NumberOfMmReservedRegions;
> > +  ///
> > +  /// Used throughout this protocol to describe the candidate
> > +  /// regions for MMRAM that are supported by this platform.
> > +  ///
> > +  EFI_MMRAM_DESCRIPTOR  Descriptor[1];
> > +} EFI_MMRAM_HOB_DESCRIPTOR_BLOCK;
> > +
> > +extern EFI_GUID gEfiMmPeiSmramMemoryReserveGuid;
> > +
> > +#endif
> > +
> > diff --git
> a/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
> b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
> > new file mode 100644
> > index 0000000000..c177a8f538
> > --- /dev/null
> > +++
> b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
> > @@ -0,0 +1,907 @@
> > +/** @file
> > +  Support routines for memory allocation routines based on Standalone MM
> Core internal functions.
> > +
> > +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > +
> > +  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
> > +
> > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +
> > +**/
> > +
> > +#include <PiMm.h>
> > +
> > +#include <Guid/MmramMemoryReserve.h>
> > +#include <Library/MemoryAllocationLib.h>
> > +#include <Library/BaseMemoryLib.h>
> > +#include <Library/DebugLib.h>
> > +#include <Library/HobLib.h>
> > +#include "MemoryAllocationServices.h"
> > +
> > +EFI_MM_SYSTEM_TABLE   *gMmst = NULL;
> > +
> > +/**
> > +  Allocates one or more 4KB pages of a certain memory type.
> > +
> > +  Allocates the number of 4KB pages of a certain memory type and returns a
> pointer to the allocated
> > +  buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0,
> then NULL is returned.
> > +  If there is not enough memory remaining to satisfy the request, then NULL
> is returned.
> > +
> > +  @param  MemoryType            The type of memory to allocate.
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +InternalAllocatePages (
> > +  IN EFI_MEMORY_TYPE  MemoryType,
> > +  IN UINTN            Pages
> > +  )
> > +{
> > +  EFI_STATUS            Status;
> > +  EFI_PHYSICAL_ADDRESS  Memory;
> > +
> > +  if (Pages == 0) {
> > +    return NULL;
> > +  }
> > +
> > +  Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType,
> Pages, &Memory);
> > +  if (EFI_ERROR (Status)) {
> > +    return NULL;
> > +  }
> > +  return (VOID *) (UINTN) Memory;
> > +}
> > +
> > +/**
> > +  Allocates one or more 4KB pages of type EfiBootServicesData.
> > +
> > +  Allocates the number of 4KB pages of type EfiBootServicesData and returns
> a pointer to the
> > +  allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If
> Pages is 0, then NULL
> > +  is returned.  If there is not enough memory remaining to satisfy the
> request, then NULL is
> > +  returned.
> > +
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocatePages (
> > +  IN UINTN  Pages
> > +  )
> > +{
> > +  return InternalAllocatePages (EfiRuntimeServicesData, Pages);
> > +}
> > +
> > +/**
> > +  Allocates one or more 4KB pages of type EfiRuntimeServicesData.
> > +
> > +  Allocates the number of 4KB pages of type EfiRuntimeServicesData and
> returns a pointer to the
> > +  allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If
> Pages is 0, then NULL
> > +  is returned.  If there is not enough memory remaining to satisfy the
> request, then NULL is
> > +  returned.
> > +
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateRuntimePages (
> > +  IN UINTN  Pages
> > +  )
> > +{
> > +  return InternalAllocatePages (EfiRuntimeServicesData, Pages);
> > +}
> > +
> > +/**
> > +  Allocates one or more 4KB pages of type EfiReservedMemoryType.
> > +
> > +  Allocates the number of 4KB pages of type EfiReservedMemoryType and
> returns a pointer to the
> > +  allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If
> Pages is 0, then NULL
> > +  is returned.  If there is not enough memory remaining to satisfy the
> request, then NULL is
> > +  returned.
> > +
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateReservedPages (
> > +  IN UINTN  Pages
> > +  )
> > +{
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Frees one or more 4KB pages that were previously allocated with one of
> the page allocation
> > +  functions in the Memory Allocation Library.
> > +
> > +  Frees the number of 4KB pages specified by Pages from the buffer specified
> by Buffer.  Buffer
> > +  must have been allocated on a previous call to the page allocation services
> of the Memory
> > +  Allocation Library.  If it is not possible to free allocated pages, then this
> function will
> > +  perform no actions.
> > +
> > +  If Buffer was not allocated with a page allocation function in the Memory
> Allocation Library,
> > +  then ASSERT().
> > +  If Pages is zero, then ASSERT().
> > +
> > +  @param  Buffer                Pointer to the buffer of pages to free.
> > +  @param  Pages                 The number of 4 KB pages to free.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +FreePages (
> > +  IN VOID   *Buffer,
> > +  IN UINTN  Pages
> > +  )
> > +{
> > +  EFI_STATUS  Status;
> > +
> > +  ASSERT (Pages != 0);
> > +  Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer,
> Pages);
> > +  ASSERT_EFI_ERROR (Status);
> > +}
> > +
> > +/**
> > +  Allocates one or more 4KB pages of a certain memory type at a specified
> alignment.
> > +
> > +  Allocates the number of 4KB pages specified by Pages of a certain memory
> type with an alignment
> > +  specified by Alignment.  The allocated buffer is returned.  If Pages is 0,
> then NULL is returned.
> > +  If there is not enough memory at the specified alignment remaining to
> satisfy the request, then
> > +  NULL is returned.
> > +  If Alignment is not a power of two and Alignment is not zero, then
> ASSERT().
> > +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> > +
> > +  @param  MemoryType            The type of memory to allocate.
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +  @param  Alignment             The requested alignment of the
> allocation.  Must be a power of two.
> > +                                If Alignment is zero, then byte
> alignment is used.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +InternalAllocateAlignedPages (
> > +  IN EFI_MEMORY_TYPE  MemoryType,
> > +  IN UINTN            Pages,
> > +  IN UINTN            Alignment
> > +  )
> > +{
> > +  EFI_STATUS            Status;
> > +  EFI_PHYSICAL_ADDRESS  Memory;
> > +  UINTN                 AlignedMemory;
> > +  UINTN                 AlignmentMask;
> > +  UINTN                 UnalignedPages;
> > +  UINTN                 RealPages;
> > +
> > +  //
> > +  // Alignment must be a power of two or zero.
> > +  //
> > +  ASSERT ((Alignment & (Alignment - 1)) == 0);
> > +
> > +  if (Pages == 0) {
> > +    return NULL;
> > +  }
> > +  if (Alignment > EFI_PAGE_SIZE) {
> > +    //
> > +    // Calculate the total number of pages since alignment is larger than page
> size.
> > +    //
> > +    AlignmentMask  = Alignment - 1;
> > +    RealPages      = Pages + EFI_SIZE_TO_PAGES (Alignment);
> > +    //
> > +    // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not
> overflow.
> > +    //
> > +    ASSERT (RealPages > Pages);
> > +
> > +    Status         = gMmst->MmAllocatePages (AllocateAnyPages,
> MemoryType, RealPages, &Memory);
> > +    if (EFI_ERROR (Status)) {
> > +      return NULL;
> > +    }
> > +    AlignedMemory  = ((UINTN) Memory + AlignmentMask) &
> ~AlignmentMask;
> > +    UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)
> Memory);
> > +    if (UnalignedPages > 0) {
> > +      //
> > +      // Free first unaligned page(s).
> > +      //
> > +      Status = gMmst->MmFreePages (Memory, UnalignedPages);
> > +      ASSERT_EFI_ERROR (Status);
> > +    }
> > +    Memory         = (EFI_PHYSICAL_ADDRESS) (AlignedMemory +
> EFI_PAGES_TO_SIZE (Pages));
> > +    UnalignedPages = RealPages - Pages - UnalignedPages;
> > +    if (UnalignedPages > 0) {
> > +      //
> > +      // Free last unaligned page(s).
> > +      //
> > +      Status = gMmst->MmFreePages (Memory, UnalignedPages);
> > +      ASSERT_EFI_ERROR (Status);
> > +    }
> > +  } else {
> > +    //
> > +    // Do not over-allocate pages in this case.
> > +    //
> > +    Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType,
> Pages, &Memory);
> > +    if (EFI_ERROR (Status)) {
> > +      return NULL;
> > +    }
> > +    AlignedMemory  = (UINTN) Memory;
> > +  }
> > +  return (VOID *) AlignedMemory;
> > +}
> > +
> > +/**
> > +  Allocates one or more 4KB pages of type EfiBootServicesData at a specified
> alignment.
> > +
> > +  Allocates the number of 4KB pages specified by Pages of type
> EfiBootServicesData with an
> > +  alignment specified by Alignment.  The allocated buffer is returned.  If
> Pages is 0, then NULL is
> > +  returned.  If there is not enough memory at the specified alignment
> remaining to satisfy the
> > +  request, then NULL is returned.
> > +
> > +  If Alignment is not a power of two and Alignment is not zero, then
> ASSERT().
> > +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> > +
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +  @param  Alignment             The requested alignment of the
> allocation.  Must be a power of two.
> > +                                If Alignment is zero, then byte
> alignment is used.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateAlignedPages (
> > +  IN UINTN  Pages,
> > +  IN UINTN  Alignment
> > +  )
> > +{
> > +  return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages,
> Alignment);
> > +}
> > +
> > +/**
> > +  Allocates one or more 4KB pages of type EfiRuntimeServicesData at a
> specified alignment.
> > +
> > +  Allocates the number of 4KB pages specified by Pages of type
> EfiRuntimeServicesData with an
> > +  alignment specified by Alignment.  The allocated buffer is returned.  If
> Pages is 0, then NULL is
> > +  returned.  If there is not enough memory at the specified alignment
> remaining to satisfy the
> > +  request, then NULL is returned.
> > +
> > +  If Alignment is not a power of two and Alignment is not zero, then
> ASSERT().
> > +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> > +
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +  @param  Alignment             The requested alignment of the
> allocation.  Must be a power of two.
> > +                                If Alignment is zero, then byte
> alignment is used.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateAlignedRuntimePages (
> > +  IN UINTN  Pages,
> > +  IN UINTN  Alignment
> > +  )
> > +{
> > +  return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages,
> Alignment);
> > +}
> > +
> > +/**
> > +  Allocates one or more 4KB pages of type EfiReservedMemoryType at a
> specified alignment.
> > +
> > +  Allocates the number of 4KB pages specified by Pages of type
> EfiReservedMemoryType with an
> > +  alignment specified by Alignment.  The allocated buffer is returned.  If
> Pages is 0, then NULL is
> > +  returned.  If there is not enough memory at the specified alignment
> remaining to satisfy the
> > +  request, then NULL is returned.
> > +
> > +  If Alignment is not a power of two and Alignment is not zero, then
> ASSERT().
> > +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> > +
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +  @param  Alignment             The requested alignment of the
> allocation.  Must be a power of two.
> > +                                If Alignment is zero, then byte
> alignment is used.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateAlignedReservedPages (
> > +  IN UINTN  Pages,
> > +  IN UINTN  Alignment
> > +  )
> > +{
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Frees one or more 4KB pages that were previously allocated with one of
> the aligned page
> > +  allocation functions in the Memory Allocation Library.
> > +
> > +  Frees the number of 4KB pages specified by Pages from the buffer specified
> by Buffer.  Buffer
> > +  must have been allocated on a previous call to the aligned page allocation
> services of the Memory
> > +  Allocation Library.  If it is not possible to free allocated pages, then this
> function will
> > +  perform no actions.
> > +
> > +  If Buffer was not allocated with an aligned page allocation function in the
> Memory Allocation
> > +  Library, then ASSERT().
> > +  If Pages is zero, then ASSERT().
> > +
> > +  @param  Buffer                Pointer to the buffer of pages to free.
> > +  @param  Pages                 The number of 4 KB pages to free.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +FreeAlignedPages (
> > +  IN VOID   *Buffer,
> > +  IN UINTN  Pages
> > +  )
> > +{
> > +  EFI_STATUS  Status;
> > +
> > +  ASSERT (Pages != 0);
> > +  Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer,
> Pages);
> > +  ASSERT_EFI_ERROR (Status);
> > +}
> > +
> > +/**
> > +  Allocates a buffer of a certain pool type.
> > +
> > +  Allocates the number bytes specified by AllocationSize of a certain pool
> type and returns a
> > +  pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of
> 0 size is
> > +  returned.  If there is not enough memory remaining to satisfy the request,
> then NULL is returned.
> > +
> > +  @param  MemoryType            The type of memory to allocate.
> > +  @param  AllocationSize        The number of bytes to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +InternalAllocatePool (
> > +  IN EFI_MEMORY_TYPE  MemoryType,
> > +  IN UINTN            AllocationSize
> > +  )
> > +{
> > +  EFI_STATUS  Status;
> > +  VOID        *Memory;
> > +
> > +  Memory = NULL;
> > +
> > +  Status = gMmst->MmAllocatePool (MemoryType, AllocationSize,
> &Memory);
> > +  if (EFI_ERROR (Status)) {
> > +    Memory = NULL;
> > +  }
> > +  return Memory;
> > +}
> > +
> > +/**
> > +  Allocates a buffer of type EfiBootServicesData.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiBootServicesData and returns a
> > +  pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of
> 0 size is
> > +  returned.  If there is not enough memory remaining to satisfy the request,
> then NULL is returned.
> > +
> > +  @param  AllocationSize        The number of bytes to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocatePool (
> > +  IN UINTN  AllocationSize
> > +  )
> > +{
> > +  return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);
> > +}
> > +
> > +/**
> > +  Allocates a buffer of type EfiRuntimeServicesData.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiRuntimeServicesData and returns
> > +  a pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer
> of 0 size is
> > +  returned.  If there is not enough memory remaining to satisfy the request,
> then NULL is returned.
> > +
> > +  @param  AllocationSize        The number of bytes to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateRuntimePool (
> > +  IN UINTN  AllocationSize
> > +  )
> > +{
> > +  return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);
> > +}
> > +
> > +/**
> > +  Allocates a buffer of type EfiReservedMemoryType.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiReservedMemoryType and returns
> > +  a pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer
> of 0 size is
> > +  returned.  If there is not enough memory remaining to satisfy the request,
> then NULL is returned.
> > +
> > +  @param  AllocationSize        The number of bytes to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateReservedPool (
> > +  IN UINTN  AllocationSize
> > +  )
> > +{
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Allocates and zeros a buffer of a certain pool type.
> > +
> > +  Allocates the number bytes specified by AllocationSize of a certain pool
> type, clears the buffer
> > +  with zeros, and returns a pointer to the allocated buffer.  If AllocationSize
> is 0, then a valid
> > +  buffer of 0 size is returned.  If there is not enough memory remaining to
> satisfy the request,
> > +  then NULL is returned.
> > +
> > +  @param  PoolType              The type of memory to allocate.
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +InternalAllocateZeroPool (
> > +  IN EFI_MEMORY_TYPE  PoolType,
> > +  IN UINTN            AllocationSize
> > +  )
> > +{
> > +  VOID  *Memory;
> > +
> > +  Memory = InternalAllocatePool (PoolType, AllocationSize);
> > +  if (Memory != NULL) {
> > +    Memory = ZeroMem (Memory, AllocationSize);
> > +  }
> > +  return Memory;
> > +}
> > +
> > +/**
> > +  Allocates and zeros a buffer of type EfiBootServicesData.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiBootServicesData, clears the
> > +  buffer with zeros, and returns a pointer to the allocated buffer.  If
> AllocationSize is 0, then a
> > +  valid buffer of 0 size is returned.  If there is not enough memory
> remaining to satisfy the
> > +  request, then NULL is returned.
> > +
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateZeroPool (
> > +  IN UINTN  AllocationSize
> > +  )
> > +{
> > +  return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);
> > +}
> > +
> > +/**
> > +  Allocates and zeros a buffer of type EfiRuntimeServicesData.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiRuntimeServicesData, clears the
> > +  buffer with zeros, and returns a pointer to the allocated buffer.  If
> AllocationSize is 0, then a
> > +  valid buffer of 0 size is returned.  If there is not enough memory
> remaining to satisfy the
> > +  request, then NULL is returned.
> > +
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateRuntimeZeroPool (
> > +  IN UINTN  AllocationSize
> > +  )
> > +{
> > +  return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);
> > +}
> > +
> > +/**
> > +  Allocates and zeros a buffer of type EfiReservedMemoryType.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiReservedMemoryType, clears the
> > +  buffer with zeros, and returns a pointer to the allocated buffer.  If
> AllocationSize is 0, then a
> > +  valid buffer of 0 size is returned.  If there is not enough memory
> remaining to satisfy the
> > +  request, then NULL is returned.
> > +
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateReservedZeroPool (
> > +  IN UINTN  AllocationSize
> > +  )
> > +{
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Copies a buffer to an allocated buffer of a certain pool type.
> > +
> > +  Allocates the number bytes specified by AllocationSize of a certain pool
> type, copies
> > +  AllocationSize bytes from Buffer to the newly allocated buffer, and returns
> a pointer to the
> > +  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
> returned.  If there
> > +  is not enough memory remaining to satisfy the request, then NULL is
> returned.
> > +  If Buffer is NULL, then ASSERT().
> > +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then
> ASSERT().
> > +
> > +  @param  PoolType              The type of pool to allocate.
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +  @param  Buffer                The buffer to copy to the allocated
> buffer.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +InternalAllocateCopyPool (
> > +  IN EFI_MEMORY_TYPE  PoolType,
> > +  IN UINTN            AllocationSize,
> > +  IN CONST VOID       *Buffer
> > +  )
> > +{
> > +  VOID  *Memory;
> > +
> > +  ASSERT (Buffer != NULL);
> > +  ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));
> > +
> > +  Memory = InternalAllocatePool (PoolType, AllocationSize);
> > +  if (Memory != NULL) {
> > +     Memory = CopyMem (Memory, Buffer, AllocationSize);
> > +  }
> > +  return Memory;
> > +}
> > +
> > +/**
> > +  Copies a buffer to an allocated buffer of type EfiBootServicesData.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiBootServicesData, copies
> > +  AllocationSize bytes from Buffer to the newly allocated buffer, and returns
> a pointer to the
> > +  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
> returned.  If there
> > +  is not enough memory remaining to satisfy the request, then NULL is
> returned.
> > +
> > +  If Buffer is NULL, then ASSERT().
> > +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then
> ASSERT().
> > +
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +  @param  Buffer                The buffer to copy to the allocated
> buffer.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateCopyPool (
> > +  IN UINTN       AllocationSize,
> > +  IN CONST VOID  *Buffer
> > +  )
> > +{
> > +  return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize,
> Buffer);
> > +}
> > +
> > +/**
> > +  Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiRuntimeServicesData, copies
> > +  AllocationSize bytes from Buffer to the newly allocated buffer, and returns
> a pointer to the
> > +  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
> returned.  If there
> > +  is not enough memory remaining to satisfy the request, then NULL is
> returned.
> > +
> > +  If Buffer is NULL, then ASSERT().
> > +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then
> ASSERT().
> > +
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +  @param  Buffer                The buffer to copy to the allocated
> buffer.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateRuntimeCopyPool (
> > +  IN UINTN       AllocationSize,
> > +  IN CONST VOID  *Buffer
> > +  )
> > +{
> > +  return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize,
> Buffer);
> > +}
> > +
> > +/**
> > +  Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiReservedMemoryType, copies
> > +  AllocationSize bytes from Buffer to the newly allocated buffer, and returns
> a pointer to the
> > +  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
> returned.  If there
> > +  is not enough memory remaining to satisfy the request, then NULL is
> returned.
> > +
> > +  If Buffer is NULL, then ASSERT().
> > +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then
> ASSERT().
> > +
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +  @param  Buffer                The buffer to copy to the allocated
> buffer.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateReservedCopyPool (
> > +  IN UINTN       AllocationSize,
> > +  IN CONST VOID  *Buffer
> > +  )
> > +{
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Reallocates a buffer of a specified memory type.
> > +
> > +  Allocates and zeros the number bytes specified by NewSize from memory
> of the type
> > +  specified by PoolType.  If OldBuffer is not NULL, then the smaller of
> OldSize and
> > +  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
> > +  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> > +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
> > +  enough memory remaining to satisfy the request, then NULL is returned.
> > +
> > +  If the allocation of the new buffer is successful and the smaller of NewSize
> and OldSize
> > +  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> > +
> > +  @param  PoolType       The type of pool to allocate.
> > +  @param  OldSize        The size, in bytes, of OldBuffer.
> > +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> > +  @param  OldBuffer      The buffer to copy to the allocated buffer.
> This is an optional
> > +                         parameter that may be NULL.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +InternalReallocatePool (
> > +  IN EFI_MEMORY_TYPE  PoolType,
> > +  IN UINTN            OldSize,
> > +  IN UINTN            NewSize,
> > +  IN VOID             *OldBuffer  OPTIONAL
> > +  )
> > +{
> > +  VOID  *NewBuffer;
> > +
> > +  NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);
> > +  if (NewBuffer != NULL && OldBuffer != NULL) {
> > +    CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));
> > +    FreePool (OldBuffer);
> > +  }
> > +  return NewBuffer;
> > +}
> > +
> > +/**
> > +  Reallocates a buffer of type EfiBootServicesData.
> > +
> > +  Allocates and zeros the number bytes specified by NewSize from memory
> of type
> > +  EfiBootServicesData.  If OldBuffer is not NULL, then the smaller of OldSize
> and
> > +  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
> > +  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> > +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
> > +  enough memory remaining to satisfy the request, then NULL is returned.
> > +
> > +  If the allocation of the new buffer is successful and the smaller of NewSize
> and OldSize
> > +  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> > +
> > +  @param  OldSize        The size, in bytes, of OldBuffer.
> > +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> > +  @param  OldBuffer      The buffer to copy to the allocated buffer.
> This is an optional
> > +                         parameter that may be NULL.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +ReallocatePool (
> > +  IN UINTN  OldSize,
> > +  IN UINTN  NewSize,
> > +  IN VOID   *OldBuffer  OPTIONAL
> > +  )
> > +{
> > +  return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize,
> OldBuffer);
> > +}
> > +
> > +/**
> > +  Reallocates a buffer of type EfiRuntimeServicesData.
> > +
> > +  Allocates and zeros the number bytes specified by NewSize from memory
> of type
> > +  EfiRuntimeServicesData.  If OldBuffer is not NULL, then the smaller of
> OldSize and
> > +  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
> > +  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> > +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
> > +  enough memory remaining to satisfy the request, then NULL is returned.
> > +
> > +  If the allocation of the new buffer is successful and the smaller of NewSize
> and OldSize
> > +  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> > +
> > +  @param  OldSize        The size, in bytes, of OldBuffer.
> > +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> > +  @param  OldBuffer      The buffer to copy to the allocated buffer.
> This is an optional
> > +                         parameter that may be NULL.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +ReallocateRuntimePool (
> > +  IN UINTN  OldSize,
> > +  IN UINTN  NewSize,
> > +  IN VOID   *OldBuffer  OPTIONAL
> > +  )
> > +{
> > +  return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize,
> OldBuffer);
> > +}
> > +
> > +/**
> > +  Reallocates a buffer of type EfiReservedMemoryType.
> > +
> > +  Allocates and zeros the number bytes specified by NewSize from memory
> of type
> > +  EfiReservedMemoryType.  If OldBuffer is not NULL, then the smaller of
> OldSize and
> > +  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
> > +  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> > +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
> > +  enough memory remaining to satisfy the request, then NULL is returned.
> > +
> > +  If the allocation of the new buffer is successful and the smaller of NewSize
> and OldSize
> > +  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> > +
> > +  @param  OldSize        The size, in bytes, of OldBuffer.
> > +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> > +  @param  OldBuffer      The buffer to copy to the allocated buffer.
> This is an optional
> > +                         parameter that may be NULL.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +ReallocateReservedPool (
> > +  IN UINTN  OldSize,
> > +  IN UINTN  NewSize,
> > +  IN VOID   *OldBuffer  OPTIONAL
> > +  )
> > +{
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Frees a buffer that was previously allocated with one of the pool allocation
> functions in the
> > +  Memory Allocation Library.
> > +
> > +  Frees the buffer specified by Buffer.  Buffer must have been allocated on a
> previous call to the
> > +  pool allocation services of the Memory Allocation Library.  If it is not
> possible to free pool
> > +  resources, then this function will perform no actions.
> > +
> > +  If Buffer was not allocated with a pool allocation function in the Memory
> Allocation Library,
> > +  then ASSERT().
> > +
> > +  @param  Buffer                Pointer to the buffer to free.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +FreePool (
> > +  IN VOID   *Buffer
> > +  )
> > +{
> > +  EFI_STATUS    Status;
> > +
> > +  Status = gMmst->MmFreePool (Buffer);
> > +  ASSERT_EFI_ERROR (Status);
> > +}
> > +
> > +/**
> > +  The constructor function calls MmInitializeMemoryServices to initialize
> > +  memory in MMRAM and caches EFI_MM_SYSTEM_TABLE pointer.
> > +
> > +  @param  ImageHandle   The firmware allocated handle for the EFI
> image.
> > +  @param  SystemTable   A pointer to the Management mode System
> Table.
> > +
> > +  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +MemoryAllocationLibConstructor (
> > +  IN EFI_HANDLE             ImageHandle,
> > +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> > +  )
> > +{
> > +  MM_CORE_PRIVATE_DATA           *MmCorePrivate;
> > +  EFI_HOB_GUID_TYPE               *GuidHob;
> > +  MM_CORE_DATA_HOB_DATA          *DataInHob;
> > +  VOID                            *HobStart;
> > +  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
> > +  EFI_MMRAM_DESCRIPTOR            *MmramRanges;
> > +  UINT32                           MmramRangeCount;
> > +  EFI_HOB_GUID_TYPE               *MmramRangesHob;
> > +
> > +  HobStart = GetHobList ();
> > +  DEBUG ((DEBUG_INFO,
> "StandaloneMmCoreMemoryAllocationLibConstructor - 0x%x\n", HobStart));
> > +
> > +  //
> > +  // Extract MM Core Private context from the Hob. If absent search for
> > +  // a Hob containing the MMRAM ranges
> > +  //
> > +  GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);
> > +  if (GuidHob == NULL) {
> > +    MmramRangesHob = GetNextGuidHob
> (&gEfiMmPeiMmramMemoryReserveGuid, HobStart);
> > +    if (MmramRangesHob == NULL) {
> > +      return EFI_UNSUPPORTED;
> > +    }
> > +
> > +    MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
> > +    if (MmramRangesHobData == NULL) {
> > +      return EFI_UNSUPPORTED;
> > +    }
> > +
> > +    MmramRanges = MmramRangesHobData->Descriptor;
> > +    if (MmramRanges == NULL) {
> > +      return EFI_UNSUPPORTED;
> > +    }
> > +
> > +    MmramRangeCount =
> MmramRangesHobData->NumberOfMmReservedRegions;
> > +    if (MmramRanges == NULL) {
> > +      return EFI_UNSUPPORTED;
> > +    }
> > +
> > +  } else {
> > +    DataInHob      = GET_GUID_HOB_DATA (GuidHob);
> > +    MmCorePrivate = (MM_CORE_PRIVATE_DATA
> *)(UINTN)DataInHob->Address;
> > +    MmramRanges     = (EFI_MMRAM_DESCRIPTOR
> *)(UINTN)MmCorePrivate->MmramRanges;
> > +    MmramRangeCount = MmCorePrivate->MmramRangeCount;
> > +  }
> > +
> > +  {
> > +    UINTN                Index;
> > +
> > +    DEBUG ((DEBUG_INFO, "MmramRangeCount - 0x%x\n",
> MmramRangeCount));
> > +    for (Index = 0; Index < MmramRangeCount; Index++) {
> > +      DEBUG ((DEBUG_INFO, "MmramRanges[%d]: 0x%016lx -
> 0x%016lx\n", Index, MmramRanges[Index].CpuStart,
> MmramRanges[Index].PhysicalSize));
> > +    }
> > +  }
> > +
> > +  //
> > +  // Initialize memory service using free MMRAM
> > +  //
> > +  DEBUG ((DEBUG_INFO, "MmInitializeMemoryServices\n"));
> > +  MmInitializeMemoryServices ((UINTN)MmramRangeCount, (VOID
> *)(UINTN)MmramRanges);
> > +
> > +  // Initialize MM Services Table
> > +  gMmst = MmSystemTable;
> > +  return EFI_SUCCESS;
> > +}
> > diff --git
> a/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
> b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
> > new file mode 100644
> > index 0000000000..068607f90e
> > --- /dev/null
> > +++
> b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
> > @@ -0,0 +1,49 @@
> > +## @file
> > +# Memory Allocation Library instance dedicated to MM Core.
> > +# The implementation borrows the MM Core Memory Allocation services as
> the primitive
> > +# for memory allocation instead of using MM System Table servces in an
> indirect way.
> > +# It is assumed that this library instance must be linked with MM Core in this
> package.
> > +#
> > +# Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
> > +# Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > +#
> > +#  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
> > +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +#
> > +##
> > +
> > +[Defines]
> > +  INF_VERSION                    = 0x0001001A
> > +  BASE_NAME                      = MemoryAllocationLib
> > +  FILE_GUID                      =
> DCDCBE1D-E760-4E1D-85B4-96E3F0439C41
> > +  MODULE_TYPE                    = MM_CORE_STANDALONE
> > +  VERSION_STRING                 = 1.0
> > +  PI_SPECIFICATION_VERSION       = 0x00010032
> > +  LIBRARY_CLASS                  =
> MemoryAllocationLib|MM_CORE_STANDALONE
> > +  CONSTRUCTOR                    = MemoryAllocationLibConstructor
> > +
> > +#
> > +# The following information is for reference only and not required by the
> build tools.
> > +#
> > +#  VALID_ARCHITECTURES           = IA32 X64
> > +#
> > +
> > +[Sources]
> > +  MemoryAllocationLib.c
> > +  MemoryAllocationServices.h
> > +
> > +[Packages]
> > +  MdePkg/MdePkg.dec
> > +  StandaloneMmPkg/StandaloneMmPkg.dec
> > +
> > +[LibraryClasses]
> > +  BaseMemoryLib
> > +  DebugLib
> > +  HobLib
> > +
> > +[Guids]
> > +  gEfiMmPeiMmramMemoryReserveGuid
> > diff --git
> a/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.
> h
> b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.
> h
> > new file mode 100644
> > index 0000000000..eb4f4c3984
> > --- /dev/null
> > +++
> b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.
> h
> > @@ -0,0 +1,38 @@
> > +/** @file
> > +  Contains function prototypes for Memory Services in the MM Core.
> > +
> > +  This header file borrows the StandaloneMmCore Memory Allocation
> services as the primitive
> > +  for memory allocation.
> > +
> > +  Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.<BR>
> > +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > +
> > +  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
> > +
> > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +
> > +**/
> > +
> > +#ifndef _PI_MM_CORE_MEMORY_ALLOCATION_SERVICES_H_
> > +#define _PI_MM_CORE_MEMORY_ALLOCATION_SERVICES_H_
> > +
> > +#include <Guid/MmCoreData.h>
> > +
> > +/**
> > +  Called to initialize the memory service.
> > +
> > +  @param   MmramRangeCount       Number of MMRAM Regions
> > +  @param   MmramRanges           Pointer to MMRAM Descriptors
> > +
> > +**/
> > +VOID
> > +MmInitializeMemoryServices (
> > +  IN UINTN                 MmramRangeCount,
> > +  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
> > +  );
> > +
> > +#endif
> > --
> > 2.16.2
> >
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 11/18] StandaloneMmPkg: MM driver entry point library.
  2018-04-06 14:42 ` [PATCH v1 11/18] StandaloneMmPkg: MM driver entry point library Supreeth Venkatesh
@ 2018-04-30 14:29   ` Achin Gupta
  2018-05-04 23:24     ` Supreeth Venkatesh
  0 siblings, 1 reply; 70+ messages in thread
From: Achin Gupta @ 2018-04-30 14:29 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

Hi Supreeth,

Some of the DXE references will have to be removed and copyright years need to
be updated. If that sounds reasonable then..

Acked-by: Achin Gupta <achin.gupta@arm.com>

cheers,
Achin

On Fri, Apr 06, 2018 at 03:42:16PM +0100, Supreeth Venkatesh wrote:
> This patch implements module entry point library for Standalone
> management mode (MM) Drivers.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  .../Include/Library/MmDriverStandaloneEntryPoint.h | 148 +++++++++++++++++++++
>  .../StandaloneMmDriverEntryPoint.c                 | 102 ++++++++++++++
>  .../StandaloneMmDriverEntryPoint.inf               |  41 ++++++
>  3 files changed, 291 insertions(+)
>  create mode 100644 StandaloneMmPkg/Include/Library/MmDriverStandaloneEntryPoint.h
>  create mode 100644 StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.c
>  create mode 100644 StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf
> 
> diff --git a/StandaloneMmPkg/Include/Library/MmDriverStandaloneEntryPoint.h b/StandaloneMmPkg/Include/Library/MmDriverStandaloneEntryPoint.h
> new file mode 100644
> index 0000000000..6fb9224e2e
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Library/MmDriverStandaloneEntryPoint.h
> @@ -0,0 +1,148 @@
> +/** @file
> +  Module entry point library for UEFI drivers, DXE Drivers, DXE Runtime Drivers,
> +  and DXE SMM Drivers.
> +
> +Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef __MODULE_ENTRY_POINT_H__
> +#define __MODULE_ENTRY_POINT_H__
> +
> +///
> +///Declare the PI Specification Revision that this driver requires to execute correctly.
> +///
> +extern CONST UINT32                   _gMmRevision;
> +
> +/**
> +  The entry point of PE/COFF Image for a DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
> +
> +  This function is the entry point for a DXE Driver, DXE Runtime Driver, DXE SMM Driver,
> +  or UEFI Driver.  This function must call ProcessLibraryConstructorList() and
> +  ProcessModuleEntryPointList(). If the return status from ProcessModuleEntryPointList()
> +  is an error status, then ProcessLibraryDestructorList() must be called. The return value
> +  from ProcessModuleEntryPointList() is returned. If _gDriverUnloadImageCount is greater
> +  than zero, then an unload handler must be registered for this image and the unload handler
> +  must invoke ProcessModuleUnloadList().
> +  If _gUefiDriverRevision is not zero and SystemTable->Hdr.Revision is less than _gUefiDriverRevison,
> +  then return EFI_INCOMPATIBLE_VERSION.
> +
> +
> +  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +  @retval  EFI_SUCCESS               The DXE Driver, DXE Runtime Driver, DXE SMM Driver,
> +                                     or UEFI Driver exited normally.
> +  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than SystemTable->Hdr.Revision.
> +  @retval  Other                     Return value from ProcessModuleEntryPointList().
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +_ModuleEntryPoint (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  );
> +
> +
> +/**
> +  Required by the EBC compiler and identical in functionality to _ModuleEntryPoint().
> +
> +  This function is required to call _ModuleEntryPoint() passing in ImageHandle, and SystemTable.
> +
> +  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +  @retval  EFI_SUCCESS               The DXE Driver, DXE Runtime Driver, DXE SMM Driver,
> +                                     or UEFI Driver exited normally.
> +  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than SystemTable->Hdr.Revision.
> +  @retval  Other                     Return value from ProcessModuleEntryPointList().
> +**/
> +EFI_STATUS
> +EFIAPI
> +EfiMain (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  );
> +
> +
> +/**
> +  Autogenerated function that calls the library constructors for all of the module's
> +  dependent libraries.
> +
> +  This function must be called by _ModuleEntryPoint().
> +  This function calls the set of library constructors for the set of library instances
> +  that a module depends on.  This includes library instances that a module depends on
> +  directly and library instances that a module depends on indirectly through other libraries.
> +  This function is autogenerated by build tools and those build tools are responsible
> +  for collecting the set of library instances, determine which ones have constructors,
> +  and calling the library constructors in the proper order based upon each of the library
> +  instances own dependencies.
> +
> +  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +**/
> +VOID
> +EFIAPI
> +ProcessLibraryConstructorList (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  );
> +
> +
> +/**
> +  Autogenerated function that calls the library descructors for all of the module's
> +  dependent libraries.
> +
> +  This function may be called by _ModuleEntryPoint() or ExitDriver().
> +  This function calls the set of library destructors for the set of library instances
> +  that a module depends on. This includes library instances that a module depends on
> +  directly and library instances that a module depends on indirectly through other libraries.
> +  This function is autogenerated by build tools and those build tools are responsible for
> +  collecting the set of library instances, determine which ones have destructors, and calling
> +  the library destructors in the proper order based upon each of the library instances own dependencies.
> +
> +  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +**/
> +VOID
> +EFIAPI
> +ProcessLibraryDestructorList (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  );
> +
> +
> +/**
> +  Autogenerated function that calls a set of module entry points.
> +
> +  This function must be called by _ModuleEntryPoint().
> +  This function calls the set of module entry points.
> +  This function is autogenerated by build tools and those build tools are responsible
> +  for collecting the module entry points and calling them in a specified order.
> +
> +  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +  @retval  EFI_SUCCESS   The DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver executed normally.
> +  @retval  !EFI_SUCCESS  The DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver failed to execute normally.
> +**/
> +EFI_STATUS
> +EFIAPI
> +ProcessModuleEntryPointList (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  );
> +
> +#endif
> diff --git a/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.c b/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.c
> new file mode 100644
> index 0000000000..84b3d9cd08
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.c
> @@ -0,0 +1,102 @@
> +/** @file
> +  Entry point to a Standalone SMM driver.
> +
> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016-2017, ARM Ltd. All rights reserved.<BR>
> +
> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +
> +
> +#include <PiMm.h>
> +
> +#include <Library/BaseLib.h>
> +#include <Library/DebugLib.h>
> +
> +VOID
> +EFIAPI
> +ProcessLibraryConstructorList (
> +  IN EFI_HANDLE               ImageHandle,
> +  IN IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
> +  );
> +
> +EFI_STATUS
> +EFIAPI
> +ProcessModuleEntryPointList (
> +  IN EFI_HANDLE               ImageHandle,
> +  IN IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
> +  );
> +
> +VOID
> +EFIAPI
> +ProcessLibraryDestructorList (
> +  IN EFI_HANDLE               ImageHandle,
> +  IN IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
> +  );
> +
> +/**
> +  The entry point of PE/COFF Image for a DXE Driver, DXE Runtime Driver, DXE SMM
> +  Driver, or UEFI Driver.
> +
> +  This function is the entry point for a DXE Driver, DXE Runtime Driver, DXE SMM Driver,
> +  or UEFI Driver.  This function must call ProcessLibraryConstructorList() and
> +  ProcessModuleEntryPointList(). If the return status from ProcessModuleEntryPointList()
> +  is an error status, then ProcessLibraryDestructorList() must be called. The return
> +  value from ProcessModuleEntryPointList() is returned. If _gDriverUnloadImageCount
> +  is greater than zero, then an unload handler must be registered for this image
> +  and the unload handler must invoke ProcessModuleUnloadList().
> +  If _gUefiDriverRevision is not zero and SystemTable->Hdr.Revision is less than
> +  _gUefiDriverRevison, then return EFI_INCOMPATIBLE_VERSION.
> +
> +
> +  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver,
> +                       DXE SMM Driver, or UEFI Driver.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +  @retval  EFI_SUCCESS               The DXE Driver, DXE Runtime Driver, DXE SMM
> +                                     Driver, or UEFI Driver exited normally.
> +  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than
> +                                    SystemTable->Hdr.Revision.
> +  @retval  Other                     Return value from ProcessModuleEntryPointList().
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +_ModuleEntryPoint (
> +  IN EFI_HANDLE               ImageHandle,
> +  IN IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
> +  )
> +{
> +  EFI_STATUS                 Status;
> +
> +  //
> +  // Call constructor for all libraries
> +  //
> +  ProcessLibraryConstructorList (ImageHandle, MmSystemTable);
> +
> +  //
> +  // Call the driver entry point
> +  //
> +  Status = ProcessModuleEntryPointList (ImageHandle, MmSystemTable);
> +
> +  //
> +  // If all of the drivers returned errors, then invoke all of the library destructors
> +  //
> +  if (EFI_ERROR (Status)) {
> +    ProcessLibraryDestructorList (ImageHandle, MmSystemTable);
> +  }
> +
> +  //
> +  // Return the cumulative return status code from all of the driver entry points
> +  //
> +  return Status;
> +}
> +
> diff --git a/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf b/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf
> new file mode 100644
> index 0000000000..564a3f14c8
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf
> @@ -0,0 +1,41 @@
> +## @file
> +# Module entry point library for Standalone SMM driver.
> +#
> +# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +# Copyright (c) 2016-2017, ARM Ltd. All rights reserved.<BR>
> +#
> +#  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.
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = StandaloneMmDriverEntryPoint
> +  FILE_GUID                      = BBC33478-98F8-4B78-B29D-574D681B7E43
> +  MODULE_TYPE                    = MM_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  LIBRARY_CLASS                  = StandaloneMmDriverEntryPoint|MM_STANDALONE
> +
> +#
> +# The following information is for reference only and not required by the build tools.
> +#
> +#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC
> +#
> +
> +[Sources]
> +  StandaloneMmDriverEntryPoint.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +
> +[LibraryClasses]
> +  BaseLib
> +  DebugLib
> +
> -- 
> 2.16.2
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 12/18] StandaloneMmPkg/CpuMm: Add CPU driver suitable for ARM Platforms.
  2018-04-06 14:42 ` [PATCH v1 12/18] StandaloneMmPkg/CpuMm: Add CPU driver suitable for ARM Platforms Supreeth Venkatesh
  2018-04-18 22:09   ` Daniil Egranov
@ 2018-04-30 15:50   ` Achin Gupta
  2018-05-04 23:24     ` Supreeth Venkatesh
  1 sibling, 1 reply; 70+ messages in thread
From: Achin Gupta @ 2018-04-30 15:50 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

Hi Supreeth,

Usual comment about copyright years and invalid comments. I also noticed some
TODOs and have provided comments for them. Please see inline

On Fri, Apr 06, 2018 at 03:42:17PM +0100, Supreeth Venkatesh wrote:
> This patch adds a simple CPU driver that exports the
> EFI_MM_CONFIGURATION_PROTOCOL to allow registration of the Standalone
> MM Foundation entry point. It preserves the existing notification
> mechanism for the configuration protocol.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S          |  33 +++

This file is not used.

>  StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c    | 231 +++++++++++++++++++++
>  StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c           | 229 ++++++++++++++++++++
>  .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h        |  89 ++++++++
>  .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf      |  60 ++++++
>  StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c      |  51 +++++
>  StandaloneMmPkg/Include/Guid/MpInformation.h       |  41 ++++
>  7 files changed, 734 insertions(+)
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
>  create mode 100644 StandaloneMmPkg/Include/Guid/MpInformation.h
> 
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S b/StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
> new file mode 100644
> index 0000000000..0b6e1c330d
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
> @@ -0,0 +1,33 @@
> +//
> +//  Copyright (c) 2017, ARM Limited. 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
> +//
> +//  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +//  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +//
> +//
> +
> +#include <Base.h>
> +#include <AutoGen.h>
> +#include <IndustryStandard/ArmStdSmc.h>
> +
> +.text
> +.align 3
> +
> +GCC_ASM_IMPORT(PiMmStandloneArmTfCpuDriverEntry)
> +GCC_ASM_EXPORT(_PiMmStandloneArmTfCpuDriverEntry)
> +
> +// Stub entry point to ensure that the stacks are completely unwound before
> +// signalling completion of event handling
> +ASM_PFX(_PiMmStandloneArmTfCpuDriverEntry):
> +  bl    PiMmStandloneArmTfCpuDriverEntry
> +  mov   x1, x0
> +  mov   x0, #(ARM_SMC_ID_MM_EVENT_COMPLETE_AARCH64 & 0xffff)
> +  movk  x0, #((ARM_SMC_ID_MM_EVENT_COMPLETE_AARCH64 >> 16) & 0xffff), lsl #16
> +  svc   #0
> +LoopForever:
> +  b     LoopForever
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c b/StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
> new file mode 100644
> index 0000000000..7b19f53ecb
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
> @@ -0,0 +1,231 @@
> +/** @file
> +
> +  Copyright (c) 2016 HP Development Company, L.P.
> +  Copyright (c) 2016 - 2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Base.h>
> +#include <Pi/PiMmCis.h>
> +
> +
> +#include <Library/ArmSvcLib.h>
> +#include <Library/ArmLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/HobLib.h>
> +
> +#include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT
> +
> +#include <Guid/ZeroGuid.h>
> +#include <Guid/MmramMemoryReserve.h>
> +
> +#include <IndustryStandard/ArmStdSmc.h>
> +
> +#include "PiMmStandloneArmTfCpuDriver.h"
> +
> +EFI_STATUS
> +EFIAPI
> +MmFoundationEntryRegister(
> +  IN CONST EFI_MM_CONFIGURATION_PROTOCOL  *This,
> +  IN EFI_MM_ENTRY_POINT                    MmEntryPoint
> +  );
> +
> +//
> +// On ARM platforms every event is expected to have a GUID associated with
> +// it. It will be used by the MM Entry point to find the handler for the
> +// event. It will either be populated in a EFI_MM_COMMUNICATE_HEADER by the
> +// caller of the event (e.g. MM_COMMUNICATE SMC) or by the CPU driver
> +// (e.g. during an asynchronous event). In either case, this context is
> +// maintained in an array which has an entry for each CPU. The pointer to this
> +// array is held in PerCpuGuidedEventContext. Memory is allocated once the
> +// number of CPUs in the system are made known through the
> +// MP_INFORMATION_HOB_DATA.
> +//
> +EFI_MM_COMMUNICATE_HEADER **PerCpuGuidedEventContext = NULL;
> +
> +//
> +// When an event is received by the CPU driver, it could correspond to a unique
> +// GUID (e.g. interrupt events) or to multiple GUIDs (e.g. MM_COMMUNICATE
> +// SMC). A table is used by the CPU driver to find the GUID corresponding to the
> +// event id in case there is a 1:1 mapping between the two. If an event id has
> +// multiple GUIDs associated with it then such an entry will not be found in
> +// this table.
> +//
> +// TODO: Currently NULL since there are no asynchronous events
> +static EFI_GUID *EventIdToGuidLookupTable = NULL;

This data structure is not being used at the moment since only MM_COMMUNICATE
SMCs are delegated to the partition. Lets remove it.

> +
> +// Descriptor with whereabouts of memory used for communication with the normal world
> +EFI_MMRAM_DESCRIPTOR  mNsCommBuffer;
> +
> +MP_INFORMATION_HOB_DATA *mMpInformationHobData;
> +
> +EFI_MM_CONFIGURATION_PROTOCOL mMmConfig = {
> +  0,
> +  MmFoundationEntryRegister
> +};
> +
> +static EFI_MM_ENTRY_POINT     mMmEntryPoint = NULL;
> +
> +EFI_STATUS
> +PiMmStandloneArmTfCpuDriverEntry (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  )
> +{
> +  EFI_MM_COMMUNICATE_HEADER *GuidedEventContext = NULL;
> +  EFI_MM_ENTRY_CONTEXT        MmEntryPointContext = {0};
> +  EFI_STATUS                  Status;
> +  UINTN                       NsCommBufferSize;
> +
> +  DEBUG ((DEBUG_INFO, "Received event - 0x%x on cpu %d\n", EventId, CpuNumber));
> +
> +  //
> +  // ARM TF passes SMC FID of the MM_COMMUNICATE interface as the Event ID upon
> +  // receipt of a synchronous MM request. Use the Event ID to distinguish
> +  // between synchronous and asynchronous events.
> +  //
> +  if (ARM_SMC_ID_MM_COMMUNICATE_AARCH64 != EventId) {

Lets change the if condition to return an error status if the EventID is not
ARM_SMC_ID_MM_COMMUNICATE_AARCH64.

> +    // Found a GUID, allocate memory to populate a communication buffer
> +    // with the GUID in it
> +    Status = mMmst->MmAllocatePool(EfiRuntimeServicesData, sizeof(EFI_MM_COMMUNICATE_HEADER), (VOID **) &GuidedEventContext);
> +    if (Status != EFI_SUCCESS) {
> +      DEBUG ((DEBUG_INFO, "Mem alloc failed - 0x%x\n", EventId));
> +      return Status;
> +    }
> +
> +    // Copy the GUID
> +    CopyGuid(&GuidedEventContext->HeaderGuid, &EventIdToGuidLookupTable[EventId]);
> +
> +    // Message Length is 0 'cause of the assumption mentioned above
> +    GuidedEventContext->MessageLength = 0;
> +  } else {
> +    // TODO: Perform parameter validation of NsCommBufferAddr

If this is being done (which is a must) then remove the TODO.

> +
> +    // This event id is the parent of multiple GUIDed handlers. Retrieve
> +    // the specific GUID from the communication buffer passed by the
> +    // caller.

This comment is outdated. It should be removed.

> +
> +    if (NsCommBufferAddr && (NsCommBufferAddr < mNsCommBuffer.PhysicalStart))
> +      return EFI_INVALID_PARAMETER;
> +

This check is not enough before the NsCommBufferAddr is dereferenced in the next
statement. There must be a check to ensure that NsCommBufferAddr points to
enough memory to hold a EFI_MM_COMMUNICATE_HEADER i.e.

if (NsCommBufferAddr + sizeof(EFI_MM_COMMUNICATE_HEADER) >=
    mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize)
      return EFI_INVALID_PARAMETER;

> +    // Find out the size of the buffer passed
> +    NsCommBufferSize = ((EFI_MM_COMMUNICATE_HEADER *) NsCommBufferAddr)->MessageLength +
> +                        sizeof(((EFI_MM_COMMUNICATE_HEADER *)NsCommBufferAddr)->MessageLength) +
> +                        sizeof(((EFI_MM_COMMUNICATE_HEADER *)NsCommBufferAddr)->HeaderGuid);

This check is more complicated that it should be. Can it be changed to:

((EFI_MM_COMMUNICATE_HEADER *) NsCommBufferAddr)->MessageLength + sizeof(EFI_MM_COMMUNICATE_HEADER)

> +
> +    // Alternative approach in case EL3 has preallocated the non-secure
> +    // buffer. MM Foundation is told about the buffer through the Hoblist
> +    // and is responsible for performing the bounds check.

Can this comment be reworded as the "alternative" approach is the only approach
possible at the moment.

> +    if (NsCommBufferAddr + NsCommBufferSize >=
> +      mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize)
> +        return EFI_INVALID_PARAMETER;
> +
> +
> +    // Now that the secure world can see the normal world buffer, allocate
> +    // memory to copy the communication buffer to the secure world.
> +    Status = mMmst->MmAllocatePool(EfiRuntimeServicesData, NsCommBufferSize, (VOID **) &GuidedEventContext);
> +    if (Status != EFI_SUCCESS) {
> +      DEBUG ((DEBUG_INFO, "Mem alloc failed - 0x%x\n", EventId));
> +      // TODO: Unmap secure memory before exiting to the normal world

This TODO is no longer relevant it seems.

> +      return Status;

These return statuses will be returned to the normal world as MM_COMMUNICATE
return codes. AllocatePool returns EFI_OUT_OF_RESOURCES which must be mapped to
a suitable error code as per the MM interface. Ditto for all other status values
that this file returns.
 
> +    }
> +
> +    // X1 contains the VA of the normal world memory accessible from
> +    // S-EL0
> +    CopyMem(GuidedEventContext, (CONST VOID *) NsCommBufferAddr, NsCommBufferSize);
> +  }
> +
> +  // Stash the pointer to the allocated Event Context for this CPU
> +  PerCpuGuidedEventContext[CpuNumber] = GuidedEventContext;
> +
> +  // TODO: Populate entire entry point context with valid information

I don't think we will be able to do this on AArch64. So lets remove this TODO.

> +  MmEntryPointContext.CurrentlyExecutingCpu = CpuNumber;
> +  MmEntryPointContext.NumberOfCpus = mMpInformationHobData->NumberOfProcessors;
> +
> +  // Populate the MM system table with MP and state information
> +  mMmst->CurrentlyExecutingCpu = CpuNumber;
> +  mMmst->NumberOfCpus = mMpInformationHobData->NumberOfProcessors;
> +  mMmst->CpuSaveStateSize = 0;
> +  mMmst->CpuSaveState = NULL;
> +
> +  mMmEntryPoint(&MmEntryPointContext);

It is worth NULL checking mMmEntryPoint!

> +
> +  // Free the memory allocation done earlier and reset the per-cpu context
> +  // TODO: Check for the return status of the FreePool API

Indeed

> +  ASSERT (GuidedEventContext);
> +  CopyMem ((VOID *)NsCommBufferAddr, (CONST VOID *) GuidedEventContext, NsCommBufferSize);
> +  mMmst->MmFreePool((VOID *) GuidedEventContext);

If this fails in the unlikely case them we should just return NO_MEMORY to the
normal world?

cheers,
Achin

> +  PerCpuGuidedEventContext[CpuNumber] = NULL;
> +
> +  return Status;
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +MmFoundationEntryRegister(
> +  IN CONST EFI_MM_CONFIGURATION_PROTOCOL  *This,
> +  IN EFI_MM_ENTRY_POINT                    MmEntryPoint
> +  ) {
> +  // store the entry point in a global
> +  mMmEntryPoint = MmEntryPoint;
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +PiMmCpuTpFwRootMmiHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_STATUS Status;
> +  UINTN      CpuNumber;
> +
> +  ASSERT (Context == NULL);
> +  ASSERT (CommBuffer == NULL);
> +  ASSERT (CommBufferSize == NULL);
> +
> +  CpuNumber = mMmst->CurrentlyExecutingCpu;
> +  if (!PerCpuGuidedEventContext[CpuNumber])
> +    return EFI_NOT_FOUND;
> +
> +  DEBUG ((DEBUG_INFO, "CommBuffer - 0x%x, CommBufferSize - 0x%x\n",
> +          PerCpuGuidedEventContext[CpuNumber],
> +          PerCpuGuidedEventContext[CpuNumber]->MessageLength));
> +
> +  Status = mMmst->MmiManage(&PerCpuGuidedEventContext[CpuNumber]->HeaderGuid,
> +                     NULL,
> +                     PerCpuGuidedEventContext[CpuNumber]->Data,
> +                     &PerCpuGuidedEventContext[CpuNumber]->MessageLength);
> +
> +  if (Status != EFI_SUCCESS) {
> +    DEBUG ((DEBUG_WARN, "Unable to manage Guided Event - %d\n", Status));
> +  }
> +
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c b/StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
> new file mode 100644
> index 0000000000..9b48ea15c1
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
> @@ -0,0 +1,229 @@
> +/** @file
> +
> +  Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
> +  Copyright (c) 2011, ARM Limited. All rights reserved.

2011?

> +  Copyright (c) 2016 HP Development Company, L.P.
> +  Copyright (c) 2016-2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Base.h>
> +#include <Pi/PiMmCis.h>
> +#include <Library/Arm/StandaloneMmCoreEntryPoint.h>
> +#include <Library/DebugLib.h>
> +#include <Library/ArmSvcLib.h>
> +#include <Library/ArmLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/HobLib.h>
> +
> +#include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT
> +
> +#include <Guid/ZeroGuid.h>
> +#include <Guid/MmramMemoryReserve.h>
> +
> +
> +#include "PiMmStandloneArmTfCpuDriver.h"
> +
> +// GUID to identify HOB with whereabouts of communication buffer with Normal
> +// World
> +extern EFI_GUID gEfiStandaloneMmNonSecureBufferGuid;
> +
> +// GUID to identify HOB where the entry point of this CPU driver will be
> +// populated to allow the entry point driver to invoke it upon receipt of an
> +// event
> +extern EFI_GUID gEfiArmTfCpuDriverEpDescriptorGuid;
> +
> +//
> +// Private copy of the MM system table for future use
> +//
> +EFI_MM_SYSTEM_TABLE *mMmst = NULL;
> +
> +//
> +// Globals used to initialize the protocol
> +//
> +static EFI_HANDLE            mMmCpuHandle = NULL;
> +
> +EFI_STATUS
> +GetGuidedHobData (
> +  IN  VOID *HobList,
> +  IN  CONST EFI_GUID *HobGuid,
> +  OUT VOID **HobData)
> +{
> +  EFI_HOB_GUID_TYPE *Hob;
> +
> +  if (!HobList || !HobGuid || !HobData)
> +    return EFI_INVALID_PARAMETER;
> +
> +  Hob = GetNextGuidHob (HobGuid, HobList);
> +  if (!Hob)
> +    return EFI_NOT_FOUND;
> +
> +  *HobData = GET_GUID_HOB_DATA (Hob);
> +  if (!HobData)
> +    return EFI_NOT_FOUND;
> +
> +  return EFI_SUCCESS;
> +}
> +
> +EFI_STATUS
> +PiMmStandloneArmTfCpuDriverInitialize (
> +  IN EFI_HANDLE         ImageHandle,  // not actual imagehandle
> +  IN EFI_MM_SYSTEM_TABLE   *SystemTable  // not actual systemtable
> +  )
> +{
> +  ARM_TF_CPU_DRIVER_EP_DESCRIPTOR *CpuDriverEntryPointDesc;
> +  EFI_CONFIGURATION_TABLE         *ConfigurationTable;
> +  MP_INFORMATION_HOB_DATA         *MpInformationHobData;
> +  EFI_MMRAM_DESCRIPTOR            *NsCommBufMmramRange;
> +  EFI_STATUS                       Status;
> +  EFI_HANDLE                       DispatchHandle;
> +  UINT32                           MpInfoSize;
> +  UINTN                            Index;
> +  UINTN                            ArraySize;
> +  VOID                            *HobStart;
> +
> +  ASSERT (SystemTable != NULL);
> +  mMmst = SystemTable;
> +
> +  // publish the MM config protocol so the MM core can register its entry point
> +  Status = mMmst->MmInstallProtocolInterface (&mMmCpuHandle,
> +                                              &gEfiMmConfigurationProtocolGuid,
> +                                              EFI_NATIVE_INTERFACE,
> +                                              &mMmConfig);
> +  if (EFI_ERROR(Status)) {
> +    return Status;
> +  }
> +
> +  // publish the MM CPU save state protocol
> +  Status = mMmst->MmInstallProtocolInterface (&mMmCpuHandle,
> +    &gEfiMmCpuProtocolGuid, EFI_NATIVE_INTERFACE, &mMmCpuState);
> +  if (EFI_ERROR(Status)) {
> +    return Status;
> +  }

CPU State save access services have not been implemented on AArch64. Lets remove
this protocol.

> +
> +  // register the root MMI handler
> +  Status = mMmst->MmiHandlerRegister (PiMmCpuTpFwRootMmiHandler,
> +                                      NULL,
> +                                      &DispatchHandle);
> +  if (EFI_ERROR(Status)) {
> +    return Status;
> +  }
> +
> +  // Retrieve the Hoblist from the MMST to extract the details of the NS
> +  // communication buffer that has been reserved by S-EL1/EL3
> +  ConfigurationTable = mMmst->MmConfigurationTable;
> +  for (Index = 0; Index < mMmst->NumberOfTableEntries; Index++) {
> +    if (CompareGuid (&gEfiHobListGuid, &(ConfigurationTable[Index].VendorGuid))) {
> +      break;
> +    }
> +  }
> +
> +  // Bail out if the Hoblist could not be found
> +  // TODO: This could also mean that
> +  // the normal world will never interact synchronously with the MM environment

All TODOs must be removed and this one in particular is not relevant now afaics.

> +  if (Index >= mMmst->NumberOfTableEntries) {
> +    DEBUG ((DEBUG_INFO, "Hoblist not found - 0x%x\n", Index));
> +    return EFI_OUT_OF_RESOURCES;
> +  }
> +
> +  HobStart = ConfigurationTable[Index].VendorTable;
> +
> +  //
> +  // Locate the HOB with the buffer to populate the entry point of this driver
> +  //
> +  Status = GetGuidedHobData (
> +            HobStart,
> +            &gEfiArmTfCpuDriverEpDescriptorGuid,
> +            (VOID **) &CpuDriverEntryPointDesc);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_INFO, "ArmTfCpuDriverEpDesc HOB data extraction failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  // Share the entry point of the CPU driver
> +  DEBUG ((DEBUG_INFO, "Sharing Cpu Driver EP *0x%lx = 0x%lx\n",
> +    (UINT64) CpuDriverEntryPointDesc->ArmTfCpuDriverEpPtr,
> +    (UINT64) PiMmStandloneArmTfCpuDriverEntry));
> +  *(CpuDriverEntryPointDesc->ArmTfCpuDriverEpPtr) = PiMmStandloneArmTfCpuDriverEntry;
> +
> +  // Find the descriptor that contains the whereabouts of the buffer for
> +  // communication with the Normal world.
> +  Status = GetGuidedHobData (
> +            HobStart,
> +            &gEfiStandaloneMmNonSecureBufferGuid,
> +            (VOID **) &NsCommBufMmramRange);
> +  if (EFI_ERROR(Status)) {
> +    DEBUG ((DEBUG_INFO, "NsCommBufMmramRange HOB data extraction failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  DEBUG ((DEBUG_INFO, "mNsCommBuffer.PhysicalStart - 0x%lx\n", (UINT64) NsCommBufMmramRange->PhysicalStart));
> +  DEBUG ((DEBUG_INFO, "mNsCommBuffer.PhysicalSize - 0x%lx\n", (UINT64) NsCommBufMmramRange->PhysicalSize));
> +
> +  CopyMem (&mNsCommBuffer, NsCommBufMmramRange, sizeof(EFI_MMRAM_DESCRIPTOR));
> +  DEBUG ((DEBUG_INFO, "mNsCommBuffer: 0x%016lx - 0x%lx\n", mNsCommBuffer.CpuStart, mNsCommBuffer.PhysicalSize));
> +
> +  //
> +  // Extract the MP information from the Hoblist
> +  //
> +  Status = GetGuidedHobData (HobStart,
> +                             &gMpInformationHobGuid,
> +                             (VOID **) &MpInformationHobData);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_INFO, "MpInformationHob extraction failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  //
> +  // Allocate memory for the MP information and copy over the MP information
> +  // passed by Trusted Firmware. Use the number of processors passed in the HOB
> +  // to copy the processor information
> +  //
> +  MpInfoSize = sizeof (MP_INFORMATION_HOB_DATA) +
> +               (sizeof (EFI_PROCESSOR_INFORMATION) *
> +               MpInformationHobData->NumberOfProcessors);
> +  Status = mMmst->MmAllocatePool (EfiRuntimeServicesData,
> +                                  MpInfoSize,
> +                                  (void **) &mMpInformationHobData);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_INFO, "mMpInformationHobData mem alloc failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  CopyMem (mMpInformationHobData, MpInformationHobData, MpInfoSize);
> +
> +  // Print MP information
> +  DEBUG ((DEBUG_INFO, "mMpInformationHobData: 0x%016lx - 0x%lx\n",
> +          mMpInformationHobData->NumberOfProcessors,
> +          mMpInformationHobData->NumberOfEnabledProcessors));
> +  for (Index = 0; Index < mMpInformationHobData->NumberOfProcessors; Index++) {
> +    DEBUG ((DEBUG_INFO, "mMpInformationHobData[0x%lx]: %d, %d, %d\n",
> +            mMpInformationHobData->ProcessorInfoBuffer[Index].ProcessorId,
> +            mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Package,
> +            mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Core,
> +            mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Thread));
> +  }
> +
> +  //
> +  // Allocate memory for a table to hold pointers to a
> +  // EFI_MM_COMMUNICATE_HEADER for each CPU
> +  //
> +  ArraySize = sizeof (EFI_MM_COMMUNICATE_HEADER *) *
> +              mMpInformationHobData->NumberOfEnabledProcessors;
> +  Status = mMmst->MmAllocatePool (EfiRuntimeServicesData,
> +                                  ArraySize,
> +                                  (VOID **) &PerCpuGuidedEventContext);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_INFO, "PerCpuGuidedEventContext mem alloc failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
> new file mode 100644
> index 0000000000..17cefd171c
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
> @@ -0,0 +1,89 @@
> +/** @file
> +  Private header with declarations and definitions specific to the MM Standalone
> +  CPU driver
> +
> +  Copyright (c) 2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _ARM_TF_CPU_DRIVER_H_
> +#define _ARM_TF_CPU_DRIVER_H_
> +
> +#include <Protocol/MmCommunication.h>
> +#include <Protocol/MmConfiguration.h>
> +#include <Protocol/MmCpu.h>
> +#include <Guid/MpInformation.h>
> +
> +//
> +// Common declarations and definitions
> +//
> +#define EVENT_ID_MM_COMMUNICATE_SMC     0x10

This is not used any longer

> +
> +//
> +// CPU driver initialization specific declarations
> +//
> +extern EFI_MM_SYSTEM_TABLE *mMmst;
> +
> +//
> +// CPU State Save protocol specific declarations
> +//
> +extern EFI_MM_CPU_PROTOCOL mMmCpuState;
> +
> +EFI_STATUS
> +EFIAPI
> +MmReadSaveState (
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  OUT VOID                        *Buffer
> +  );
> +
> +EFI_STATUS
> +EFIAPI
> +MmWriteSaveState (
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  IN CONST VOID                   *Buffer
> +  );

This protoocl is not implemented

> +
> +//
> +// MM event handling specific declarations
> +//
> +extern EFI_MM_COMMUNICATE_HEADER    **PerCpuGuidedEventContext;
> +extern EFI_MMRAM_DESCRIPTOR          mNsCommBuffer;
> +extern MP_INFORMATION_HOB_DATA       *mMpInformationHobData;
> +extern EFI_MM_CONFIGURATION_PROTOCOL mMmConfig;
> +
> +EFI_STATUS
> +PiMmStandloneArmTfCpuDriverEntry (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  );
> +
> +EFI_STATUS
> +EFIAPI
> +PiMmCpuTpFwRootMmiHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +EFI_STATUS _PiMmStandloneArmTfCpuDriverEntry (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  );
> +
> +#endif
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> new file mode 100644
> index 0000000000..baf6d957bb
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> @@ -0,0 +1,60 @@
> +#/** @file
> +#
> +#  Standalone MM CPU driver for ARM Standard Platforms
> +#
> +#  Copyright (c) 2009, Apple Inc. All rights reserved.<BR>
> +#  Copyright (c) 2016 HP Development Company, L.P.
> +#  Copyright (c) 2017, ARM Limited. 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
> +#
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#**/
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = PiMmStandloneArmTfCpuDriver
> +  FILE_GUID                      = 58F7A62B-6280-42A7-BC38-10535A64A92C
> +  MODULE_TYPE                    = MM_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  ENTRY_POINT                    = PiMmStandloneArmTfCpuDriverInitialize
> +
> +[Sources]
> +  Init.c
> +  EventHandle.c
> +  StateSave.c

Not needed.

> +
> +[Packages]
> +  ArmPkg/ArmPkg.dec
> +  MdePkg/MdePkg.dec
> +  MdeModulePkg/MdeModulePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[LibraryClasses]
> +  ArmLib
> +  ArmSvcLib
> +  BaseMemoryLib
> +  DebugLib
> +  HobLib
> +  StandaloneMmDriverEntryPoint
> +
> +[Protocols]
> +  gEfiMmConfigurationProtocolGuid                        # PROTOCOL ALWAYS_PRODUCED
> +  gEfiMmCpuProtocolGuid                                  # PROTOCOL ALWAYS_PRODUCED
> +
> +[Guids]
> +  gEfiHobListGuid
> +  gEfiMmPeiMmramMemoryReserveGuid
> +  gZeroGuid
> +  gMpInformationHobGuid
> +  gEfiStandaloneMmNonSecureBufferGuid
> +  gEfiArmTfCpuDriverEpDescriptorGuid
> +
> +[Depex]
> +  TRUE
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c b/StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
> new file mode 100644
> index 0000000000..c5155e1b31
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
> @@ -0,0 +1,51 @@
> +/** @file
> +
> +  Copyright (c) 2016 HP Development Company, L.P.
> +  Copyright (c) 2016-2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Base.h>
> +#include <Pi/PiSmmCis.h>
> +#include <Library/DebugLib.h>
> +
> +#include "PiMmStandloneArmTfCpuDriver.h"
> +
> +EFI_MM_CPU_PROTOCOL mMmCpuState = {
> +  MmReadSaveState,
> +  MmWriteSaveState
> +};
> +
> +EFI_STATUS
> +EFIAPI
> +MmReadSaveState(
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  OUT VOID                        *Buffer
> +  ) {
> +  // todo: implement
> +  return EFI_UNSUPPORTED;
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +MmWriteSaveState(
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  IN CONST VOID                   *Buffer
> +  ) {
> +  // todo: implement
> +  return EFI_UNSUPPORTED;
> +}

This file must be removed as described earlier.

> diff --git a/StandaloneMmPkg/Include/Guid/MpInformation.h b/StandaloneMmPkg/Include/Guid/MpInformation.h
> new file mode 100644
> index 0000000000..4e9a3c04ec
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Guid/MpInformation.h
> @@ -0,0 +1,41 @@
> +/** @file
> +  EFI MP information protocol provides a lightweight MP_SERVICES_PROTOCOL.
> +
> +  MP information protocol only provides static information of MP processor.
> +
> +  Copyright (c) 2009, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _MP_INFORMATION_H_
> +#define _MP_INFORMATION_H_
> +
> +#include <Protocol/MpService.h>
> +#include <PiPei.h>
> +#include <Ppi/SecPlatformInformation.h>
> +
> +#define MP_INFORMATION_GUID \
> +  { \
> +    0xba33f15d, 0x4000, 0x45c1, {0x8e, 0x88, 0xf9, 0x16, 0x92, 0xd4, 0x57, 0xe3}  \
> +  }
> +
> +#pragma pack(1)
> +typedef struct {
> +  UINT64                     NumberOfProcessors;
> +  UINT64                     NumberOfEnabledProcessors;
> +  EFI_PROCESSOR_INFORMATION  ProcessorInfoBuffer[];
> +} MP_INFORMATION_HOB_DATA;
> +#pragma pack()
> +
> +extern EFI_GUID gMpInformationHobGuid;
> +
> +#endif
> -- 
> 2.16.2
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 13/18] StandaloneMmPkg/Core: Implementation of Standalone MM Core Module.
  2018-04-06 14:42 ` [PATCH v1 13/18] StandaloneMmPkg/Core: Implementation of Standalone MM Core Module Supreeth Venkatesh
@ 2018-04-30 19:19   ` Achin Gupta
  2018-04-30 19:28     ` Ard Biesheuvel
  2018-05-04 23:28     ` Supreeth Venkatesh
  0 siblings, 2 replies; 70+ messages in thread
From: Achin Gupta @ 2018-04-30 19:19 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

Hi Supreeth,

I think it is worth adding a signed off by Jiewen since he originally
contributed the code and it has not changed much since. Please update the
correct year in the copyright headers too.

Acked-by: Achin Gupta <achin.gupta@arm.com>

cheers,
Achin

On Fri, Apr 06, 2018 at 03:42:18PM +0100, Supreeth Venkatesh wrote:
> Management Mode (MM) is a generic term used to describe a secure
> execution environment provided by the CPU and related silicon that is
> entered when the CPU detects a MMI. For x86 systems, this can be
> implemented with System Management Mode (SMM). For ARM systems, this can
> be implemented with TrustZone (TZ).
> A MMI can be a CPU instruction or interrupt. Upon detection of a MMI, a
> CPU will jump to the MM Entry Point and save some portion of its state
> (the "save state") such that execution can be resumed.
> The MMI can be generated synchronously by software or asynchronously by
> a hardware event. Each MMI source can be detected, cleared and disabled.
> Some systems provide for special memory (Management Mode RAM or MMRAM)
> which is set aside for software running in MM. Usually the MMRAM is
> hidden during normal CPU execution, but this is not required. Usually,
> after MMRAM is hidden it cannot be exposed until the next system reset.
> 
> The MM Core Interface Specification describes three pieces of the PI
> Management Mode architecture:
> 1. MM Dispatch
>    During DXE, the DXE Foundation works with the MM Foundation to
>    schedule MM drivers for execution in the discovered firmware volumes.
> 2. MM Initialization
>    MM related code opens MMRAM, creates the MMRAM memory map, and
>    launches the MM Foundation, which provides the necessary services to
>    launch MM-related drivers. Then, sometime before boot, MMRAM is
>    closed and locked. This piece may be completed during the
>    SEC, PEI or DXE phases.
> 3. MMI Management
>    When an MMI generated, the MM environment is created and then the MMI
> 
>    sources are detected and MMI handlers called.
> 
> This patch implements the MM Core.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  StandaloneMmPkg/Core/Dependency.c                  |  389 +++++++
>  StandaloneMmPkg/Core/Dispatcher.c                  | 1071 ++++++++++++++++++++
>  StandaloneMmPkg/Core/FwVol.c                       |  104 ++
>  StandaloneMmPkg/Core/Handle.c                      |  533 ++++++++++
>  StandaloneMmPkg/Core/InstallConfigurationTable.c   |  178 ++++
>  StandaloneMmPkg/Core/Locate.c                      |  496 +++++++++
>  StandaloneMmPkg/Core/Mmi.c                         |  337 ++++++
>  StandaloneMmPkg/Core/Notify.c                      |  203 ++++
>  StandaloneMmPkg/Core/Page.c                        |  384 +++++++
>  StandaloneMmPkg/Core/Pool.c                        |  287 ++++++
>  StandaloneMmPkg/Core/StandaloneMmCore.c            |  708 +++++++++++++
>  StandaloneMmPkg/Core/StandaloneMmCore.h            |  903 +++++++++++++++++
>  StandaloneMmPkg/Core/StandaloneMmCore.inf          |   80 ++
>  StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h |   66 ++
>  StandaloneMmPkg/Include/Guid/MmFvDispatch.h        |   38 +
>  StandaloneMmPkg/Include/StandaloneMm.h             |   36 +
>  16 files changed, 5813 insertions(+)
>  create mode 100644 StandaloneMmPkg/Core/Dependency.c
>  create mode 100644 StandaloneMmPkg/Core/Dispatcher.c
>  create mode 100644 StandaloneMmPkg/Core/FwVol.c
>  create mode 100644 StandaloneMmPkg/Core/Handle.c
>  create mode 100644 StandaloneMmPkg/Core/InstallConfigurationTable.c
>  create mode 100644 StandaloneMmPkg/Core/Locate.c
>  create mode 100644 StandaloneMmPkg/Core/Mmi.c
>  create mode 100644 StandaloneMmPkg/Core/Notify.c
>  create mode 100644 StandaloneMmPkg/Core/Page.c
>  create mode 100644 StandaloneMmPkg/Core/Pool.c
>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.c
>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.h
>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.inf
>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
>  create mode 100644 StandaloneMmPkg/Include/Guid/MmFvDispatch.h
>  create mode 100644 StandaloneMmPkg/Include/StandaloneMm.h
> 
> diff --git a/StandaloneMmPkg/Core/Dependency.c b/StandaloneMmPkg/Core/Dependency.c
> new file mode 100644
> index 0000000000..e501369130
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Dependency.c
> @@ -0,0 +1,389 @@
> +/** @file
> +  MM Driver Dispatcher Dependency Evaluator
> +
> +  This routine evaluates a dependency expression (DEPENDENCY_EXPRESSION) to determine
> +  if a driver can be scheduled for execution.  The criteria for
> +  schedulability is that the dependency expression is satisfied.
> +
> +  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +///
> +/// EFI_DEP_REPLACE_TRUE - Used to dynamically patch the dependency expression
> +///                        to save time.  A EFI_DEP_PUSH is evaluated one an
> +///                        replaced with EFI_DEP_REPLACE_TRUE. If PI spec's Vol 2
> +///                        Driver Execution Environment Core Interface use 0xff
> +///                        as new DEPEX opcode. EFI_DEP_REPLACE_TRUE should be
> +///                        defined to a new value that is not conflicting with PI spec.
> +///
> +#define EFI_DEP_REPLACE_TRUE  0xff
> +
> +///
> +/// Define the initial size of the dependency expression evaluation stack
> +///
> +#define DEPEX_STACK_SIZE_INCREMENT  0x1000
> +
> +//
> +// Global stack used to evaluate dependency expressions
> +//
> +BOOLEAN  *mDepexEvaluationStack        = NULL;
> +BOOLEAN  *mDepexEvaluationStackEnd     = NULL;
> +BOOLEAN  *mDepexEvaluationStackPointer = NULL;
> +
> +/**
> +  Grow size of the Depex stack
> +
> +  @retval EFI_SUCCESS           Stack successfully growed.
> +  @retval EFI_OUT_OF_RESOURCES  There is not enough system memory to grow the stack.
> +
> +**/
> +EFI_STATUS
> +GrowDepexStack (
> +  VOID
> +  )
> +{
> +  BOOLEAN     *NewStack;
> +  UINTN       Size;
> +
> +  Size = DEPEX_STACK_SIZE_INCREMENT;
> +  if (mDepexEvaluationStack != NULL) {
> +    Size = Size + (mDepexEvaluationStackEnd - mDepexEvaluationStack);
> +  }
> +
> +  NewStack = AllocatePool (Size * sizeof (BOOLEAN));
> +  if (NewStack == NULL) {
> +    return EFI_OUT_OF_RESOURCES;
> +  }
> +
> +  if (mDepexEvaluationStack != NULL) {
> +    //
> +    // Copy to Old Stack to the New Stack
> +    //
> +    CopyMem (
> +      NewStack,
> +      mDepexEvaluationStack,
> +      (mDepexEvaluationStackEnd - mDepexEvaluationStack) * sizeof (BOOLEAN)
> +      );
> +
> +    //
> +    // Free The Old Stack
> +    //
> +    FreePool (mDepexEvaluationStack);
> +  }
> +
> +  //
> +  // Make the Stack pointer point to the old data in the new stack
> +  //
> +  mDepexEvaluationStackPointer = NewStack + (mDepexEvaluationStackPointer - mDepexEvaluationStack);
> +  mDepexEvaluationStack        = NewStack;
> +  mDepexEvaluationStackEnd     = NewStack + Size;
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Push an element onto the Boolean Stack.
> +
> +  @param  Value                 BOOLEAN to push.
> +
> +  @retval EFI_SUCCESS           The value was pushed onto the stack.
> +  @retval EFI_OUT_OF_RESOURCES  There is not enough system memory to grow the stack.
> +
> +**/
> +EFI_STATUS
> +PushBool (
> +  IN BOOLEAN  Value
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  //
> +  // Check for a stack overflow condition
> +  //
> +  if (mDepexEvaluationStackPointer == mDepexEvaluationStackEnd) {
> +    //
> +    // Grow the stack
> +    //
> +    Status = GrowDepexStack ();
> +    if (EFI_ERROR (Status)) {
> +      return Status;
> +    }
> +  }
> +
> +  //
> +  // Push the item onto the stack
> +  //
> +  *mDepexEvaluationStackPointer = Value;
> +  mDepexEvaluationStackPointer++;
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Pop an element from the Boolean stack.
> +
> +  @param  Value                 BOOLEAN to pop.
> +
> +  @retval EFI_SUCCESS           The value was popped onto the stack.
> +  @retval EFI_ACCESS_DENIED     The pop operation underflowed the stack.
> +
> +**/
> +EFI_STATUS
> +PopBool (
> +  OUT BOOLEAN  *Value
> +  )
> +{
> +  //
> +  // Check for a stack underflow condition
> +  //
> +  if (mDepexEvaluationStackPointer == mDepexEvaluationStack) {
> +    return EFI_ACCESS_DENIED;
> +  }
> +
> +  //
> +  // Pop the item off the stack
> +  //
> +  mDepexEvaluationStackPointer--;
> +  *Value = *mDepexEvaluationStackPointer;
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  This is the POSTFIX version of the dependency evaluator.  This code does
> +  not need to handle Before or After, as it is not valid to call this
> +  routine in this case. POSTFIX means all the math is done on top of the stack.
> +
> +  @param  DriverEntry           DriverEntry element to update.
> +
> +  @retval TRUE                  If driver is ready to run.
> +  @retval FALSE                 If driver is not ready to run or some fatal error
> +                                was found.
> +
> +**/
> +BOOLEAN
> +MmIsSchedulable (
> +  IN  EFI_MM_DRIVER_ENTRY   *DriverEntry
> +  )
> +{
> +  EFI_STATUS  Status;
> +  UINT8       *Iterator;
> +  BOOLEAN     Operator;
> +  BOOLEAN     Operator2;
> +  EFI_GUID    DriverGuid;
> +  VOID        *Interface;
> +
> +  Operator = FALSE;
> +  Operator2 = FALSE;
> +
> +  if (DriverEntry->After || DriverEntry->Before) {
> +    //
> +    // If Before or After Depex skip as MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
> +    // processes them.
> +    //
> +    return FALSE;
> +  }
> +
> +  DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
> +
> +  if (DriverEntry->Depex == NULL) {
> +    //
> +    // A NULL Depex means that the MM driver is not built correctly.
> +    // All MM drivers must have a valid depex expressiion.
> +    //
> +    DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Depex is empty)\n"));
> +    ASSERT (FALSE);
> +    return FALSE;
> +  }
> +
> +  //
> +  // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
> +  // incorrectly formed DEPEX expressions
> +  //
> +  mDepexEvaluationStackPointer = mDepexEvaluationStack;
> +
> +
> +  Iterator = DriverEntry->Depex;
> +
> +  while (TRUE) {
> +    //
> +    // Check to see if we are attempting to fetch dependency expression instructions
> +    // past the end of the dependency expression.
> +    //
> +    if (((UINTN)Iterator - (UINTN)DriverEntry->Depex) >= DriverEntry->DepexSize) {
> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Attempt to fetch past end of depex)\n"));
> +      return FALSE;
> +    }
> +
> +    //
> +    // Look at the opcode of the dependency expression instruction.
> +    //
> +    switch (*Iterator) {
> +    case EFI_DEP_BEFORE:
> +    case EFI_DEP_AFTER:
> +      //
> +      // For a well-formed Dependency Expression, the code should never get here.
> +      // The BEFORE and AFTER are processed prior to this routine's invocation.
> +      // If the code flow arrives at this point, there was a BEFORE or AFTER
> +      // that were not the first opcodes.
> +      //
> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));
> +      ASSERT (FALSE);
> +
> +    case EFI_DEP_PUSH:
> +      //
> +      // Push operator is followed by a GUID. Test to see if the GUID protocol
> +      // is installed and push the boolean result on the stack.
> +      //
> +      CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));
> +
> +      Status = MmLocateProtocol (&DriverGuid, NULL, &Interface);
> +      if (EFI_ERROR (Status) && (mEfiSystemTable != NULL)) {
> +        //
> +        // For MM Driver, it may depend on uefi protocols
> +        //
> +        Status = mEfiSystemTable->BootServices->LocateProtocol (&DriverGuid, NULL, &Interface);
> +      }
> +
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  PUSH GUID(%g) = FALSE\n", &DriverGuid));
> +        Status = PushBool (FALSE);
> +      } else {
> +        DEBUG ((DEBUG_DISPATCH, "  PUSH GUID(%g) = TRUE\n", &DriverGuid));
> +        *Iterator = EFI_DEP_REPLACE_TRUE;
> +        Status = PushBool (TRUE);
> +      }
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +
> +      Iterator += sizeof (EFI_GUID);
> +      break;
> +
> +    case EFI_DEP_AND:
> +      DEBUG ((DEBUG_DISPATCH, "  AND\n"));
> +      Status = PopBool (&Operator);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +
> +      Status = PopBool (&Operator2);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +
> +      Status = PushBool ((BOOLEAN)(Operator && Operator2));
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +      break;
> +
> +    case EFI_DEP_OR:
> +      DEBUG ((DEBUG_DISPATCH, "  OR\n"));
> +      Status = PopBool (&Operator);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +
> +      Status = PopBool (&Operator2);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +
> +      Status = PushBool ((BOOLEAN)(Operator || Operator2));
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +      break;
> +
> +    case EFI_DEP_NOT:
> +      DEBUG ((DEBUG_DISPATCH, "  NOT\n"));
> +      Status = PopBool (&Operator);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +
> +      Status = PushBool ((BOOLEAN)(!Operator));
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +      break;
> +
> +    case EFI_DEP_TRUE:
> +      DEBUG ((DEBUG_DISPATCH, "  TRUE\n"));
> +      Status = PushBool (TRUE);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +      break;
> +
> +    case EFI_DEP_FALSE:
> +      DEBUG ((DEBUG_DISPATCH, "  FALSE\n"));
> +      Status = PushBool (FALSE);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +      break;
> +
> +    case EFI_DEP_END:
> +      DEBUG ((DEBUG_DISPATCH, "  END\n"));
> +      Status = PopBool (&Operator);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = %a\n", Operator ? "TRUE" : "FALSE"));
> +      return Operator;
> +
> +    case EFI_DEP_REPLACE_TRUE:
> +      CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));
> +      DEBUG ((DEBUG_DISPATCH, "  PUSH GUID(%g) = TRUE\n", &DriverGuid));
> +      Status = PushBool (TRUE);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +
> +      Iterator += sizeof (EFI_GUID);
> +      break;
> +
> +    default:
> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unknown opcode)\n"));
> +      goto Done;
> +    }
> +
> +    //
> +    // Skip over the Dependency Op Code we just processed in the switch.
> +    // The math is done out of order, but it should not matter. That is
> +    // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
> +    // This is not an issue, since we just need the correct end result. You
> +    // need to be careful using Iterator in the loop as it's intermediate value
> +    // may be strange.
> +    //
> +    Iterator++;
> +  }
> +
> +Done:
> +  return FALSE;
> +}
> diff --git a/StandaloneMmPkg/Core/Dispatcher.c b/StandaloneMmPkg/Core/Dispatcher.c
> new file mode 100644
> index 0000000000..af18fa7eaa
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Dispatcher.c
> @@ -0,0 +1,1071 @@
> +/** @file
> +  MM Driver Dispatcher.
> +
> +  Step #1 - When a FV protocol is added to the system every driver in the FV
> +            is added to the mDiscoveredList. The Before, and After Depex are
> +            pre-processed as drivers are added to the mDiscoveredList. If an Apriori
> +            file exists in the FV those drivers are addeded to the
> +            mScheduledQueue. The mFvHandleList is used to make sure a
> +            FV is only processed once.
> +
> +  Step #2 - Dispatch. Remove driver from the mScheduledQueue and load and
> +            start it. After mScheduledQueue is drained check the
> +            mDiscoveredList to see if any item has a Depex that is ready to
> +            be placed on the mScheduledQueue.
> +
> +  Step #3 - Adding to the mScheduledQueue requires that you process Before
> +            and After dependencies. This is done recursively as the call to add
> +            to the mScheduledQueue checks for Before and recursively adds
> +            all Befores. It then addes the item that was passed in and then
> +            processess the After dependecies by recursively calling the routine.
> +
> +  Dispatcher Rules:
> +  The rules for the dispatcher are similar to the DXE dispatcher.
> +
> +  The rules for DXE dispatcher are in chapter 10 of the DXE CIS. Figure 10-3
> +  is the state diagram for the DXE dispatcher
> +
> +  Depex - Dependency Expresion.
> +
> +  Copyright (c) 2014, Hewlett-Packard Development Company, L.P.
> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +//
> +// MM Dispatcher Data structures
> +//
> +#define KNOWN_HANDLE_SIGNATURE  SIGNATURE_32('k','n','o','w')
> +typedef struct {
> +  UINTN           Signature;
> +  LIST_ENTRY      Link;         // mFvHandleList
> +  EFI_HANDLE      Handle;
> +} KNOWN_HANDLE;
> +
> +//
> +// Function Prototypes
> +//
> +
> +EFI_STATUS
> +MmCoreFfsFindMmDriver (
> +  IN  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader
> +  );
> +
> +/**
> +  Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
> +  must add any driver with a before dependency on InsertedDriverEntry first.
> +  You do this by recursively calling this routine. After all the Befores are
> +  processed you can add InsertedDriverEntry to the mScheduledQueue.
> +  Then you can add any driver with an After dependency on InsertedDriverEntry
> +  by recursively calling this routine.
> +
> +  @param  InsertedDriverEntry   The driver to insert on the ScheduledLink Queue
> +
> +**/
> +VOID
> +MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
> +  IN  EFI_MM_DRIVER_ENTRY   *InsertedDriverEntry
> +  );
> +
> +//
> +// The Driver List contains one copy of every driver that has been discovered.
> +// Items are never removed from the driver list. List of EFI_MM_DRIVER_ENTRY
> +//
> +LIST_ENTRY  mDiscoveredList = INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList);
> +
> +//
> +// Queue of drivers that are ready to dispatch. This queue is a subset of the
> +// mDiscoveredList.list of EFI_MM_DRIVER_ENTRY.
> +//
> +LIST_ENTRY  mScheduledQueue = INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue);
> +
> +//
> +// List of handles who's Fv's have been parsed and added to the mFwDriverList.
> +//
> +LIST_ENTRY  mFvHandleList = INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList);
> +
> +//
> +// Flag for the MM Dispacher.  TRUE if dispatcher is execuing.
> +//
> +BOOLEAN  gDispatcherRunning = FALSE;
> +
> +//
> +// Flag for the MM Dispacher.  TRUE if there is one or more MM drivers ready to be dispatched
> +//
> +BOOLEAN  gRequestDispatch = FALSE;
> +
> +//
> +// The global variable is defined for Loading modules at fixed address feature to track the MM code
> +// memory range usage. It is a bit mapped array in which every bit indicates the correspoding
> +// memory page available or not.
> +//
> +GLOBAL_REMOVE_IF_UNREFERENCED    UINT64                *mMmCodeMemoryRangeUsageBitMap=NULL;
> +
> +/**
> +  To check memory usage bit map array to figure out if the memory range in which the image will be loaded is available or not. If
> +  memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.
> +  The function is only invoked when load modules at fixed address feature is enabled.
> +
> +  @param  ImageBase                The base addres the image will be loaded at.
> +  @param  ImageSize                The size of the image
> +
> +  @retval EFI_SUCCESS              The memory range the image will be loaded in is available
> +  @retval EFI_NOT_FOUND            The memory range the image will be loaded in is not available
> +**/
> +EFI_STATUS
> +CheckAndMarkFixLoadingMemoryUsageBitMap (
> +  IN  EFI_PHYSICAL_ADDRESS          ImageBase,
> +  IN  UINTN                         ImageSize
> +  )
> +{
> +   UINT32                             MmCodePageNumber;
> +   UINT64                             MmCodeSize;
> +   EFI_PHYSICAL_ADDRESS               MmCodeBase;
> +   UINTN                              BaseOffsetPageNumber;
> +   UINTN                              TopOffsetPageNumber;
> +   UINTN                              Index;
> +   //
> +   // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressMmCodePageNumber
> +   //
> +   MmCodePageNumber = 0;
> +   MmCodeSize = EFI_PAGES_TO_SIZE (MmCodePageNumber);
> +   MmCodeBase = gLoadModuleAtFixAddressMmramBase;
> +
> +   //
> +   // If the memory usage bit map is not initialized,  do it. Every bit in the array
> +   // indicate the status of the corresponding memory page, available or not
> +   //
> +   if (mMmCodeMemoryRangeUsageBitMap == NULL) {
> +     mMmCodeMemoryRangeUsageBitMap = AllocateZeroPool(((MmCodePageNumber / 64) + 1)*sizeof(UINT64));
> +   }
> +   //
> +   // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
> +   //
> +   if (mMmCodeMemoryRangeUsageBitMap == NULL) {
> +     return EFI_NOT_FOUND;
> +   }
> +   //
> +   // see if the memory range for loading the image is in the MM code range.
> +   //
> +   if (MmCodeBase + MmCodeSize <  ImageBase + ImageSize || MmCodeBase >  ImageBase) {
> +     return EFI_NOT_FOUND;
> +   }
> +   //
> +   // Test if the memory is avalaible or not.
> +   //
> +   BaseOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase - MmCodeBase));
> +   TopOffsetPageNumber  = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - MmCodeBase));
> +   for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
> +     if ((mMmCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {
> +       //
> +       // This page is already used.
> +       //
> +       return EFI_NOT_FOUND;
> +     }
> +   }
> +
> +   //
> +   // Being here means the memory range is available.  So mark the bits for the memory range
> +   //
> +   for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
> +     mMmCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));
> +   }
> +   return  EFI_SUCCESS;
> +}
> +/**
> +  Get the fixed loading address from image header assigned by build tool. This function only be called
> +  when Loading module at Fixed address feature enabled.
> +
> +  @param  ImageContext              Pointer to the image context structure that describes the PE/COFF
> +                                    image that needs to be examined by this function.
> +  @retval EFI_SUCCESS               An fixed loading address is assigned to this image by build tools .
> +  @retval EFI_NOT_FOUND             The image has no assigned fixed loadding address.
> +
> +**/
> +EFI_STATUS
> +GetPeCoffImageFixLoadingAssignedAddress(
> +  IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext
> +  )
> +{
> +  UINTN                              SectionHeaderOffset;
> +  EFI_STATUS                         Status;
> +  EFI_IMAGE_SECTION_HEADER           SectionHeader;
> +  EFI_IMAGE_OPTIONAL_HEADER_UNION    *ImgHdr;
> +  EFI_PHYSICAL_ADDRESS               FixLoadingAddress;
> +  UINT16                             Index;
> +  UINTN                              Size;
> +  UINT16                             NumberOfSections;
> +  UINT64                             ValueInSectionHeader;
> +
> +  FixLoadingAddress = 0;
> +  Status = EFI_NOT_FOUND;
> +
> +  //
> +  // Get PeHeader pointer
> +  //
> +  ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
> +  SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +
> +                        sizeof (UINT32) +
> +                        sizeof (EFI_IMAGE_FILE_HEADER) +
> +                        ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;
> +  NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
> +
> +  //
> +  // Get base address from the first section header that doesn't point to code section.
> +  //
> +  for (Index = 0; Index < NumberOfSections; Index++) {
> +    //
> +    // Read section header from file
> +    //
> +    Size = sizeof (EFI_IMAGE_SECTION_HEADER);
> +    Status = ImageContext->ImageRead (
> +                              ImageContext->Handle,
> +                              SectionHeaderOffset,
> +                              &Size,
> +                              &SectionHeader
> +                              );
> +    if (EFI_ERROR (Status)) {
> +      return Status;
> +    }
> +
> +    Status = EFI_NOT_FOUND;
> +
> +    if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
> +      //
> +      // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
> +      // that doesn't point to code section in image header.So there is an assumption that when the feature is enabled,
> +      // if a module with a loading address assigned by tools, the PointerToRelocations & PointerToLineNumbers fields
> +      // should not be Zero, or else, these 2 fields should be set to Zero
> +      //
> +      ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);
> +      if (ValueInSectionHeader != 0) {
> +        //
> +        // Found first section header that doesn't point to code section in which build tool saves the
> +        // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
> +        //
> +        FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressMmramBase + (INT64)ValueInSectionHeader);
> +        //
> +        // Check if the memory range is available.
> +        //
> +        Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));
> +        if (!EFI_ERROR(Status)) {
> +          //
> +          // The assigned address is valid. Return the specified loading address
> +          //
> +          ImageContext->ImageAddress = FixLoadingAddress;
> +        }
> +      }
> +      break;
> +    }
> +    SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
> +  }
> +  DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n", FixLoadingAddress, Status));
> +  return Status;
> +}
> +/**
> +  Loads an EFI image into SMRAM.
> +
> +  @param  DriverEntry             EFI_MM_DRIVER_ENTRY instance
> +
> +  @return EFI_STATUS
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLoadImage (
> +  IN OUT EFI_MM_DRIVER_ENTRY  *DriverEntry
> +  )
> +{
> +  VOID                           *Buffer;
> +  UINTN                          PageCount;
> +  EFI_STATUS                     Status;
> +  EFI_PHYSICAL_ADDRESS           DstBuffer;
> +  PE_COFF_LOADER_IMAGE_CONTEXT   ImageContext;
> +
> +  DEBUG ((DEBUG_INFO, "MmLoadImage - %g\n", &DriverEntry->FileName));
> +
> +  Buffer = AllocateCopyPool (DriverEntry->Pe32DataSize, DriverEntry->Pe32Data);
> +  if (Buffer == NULL) {
> +    return EFI_OUT_OF_RESOURCES;
> +  }
> +
> +  Status               = EFI_SUCCESS;
> +
> +  //
> +  // Initialize ImageContext
> +  //
> +  ImageContext.Handle = Buffer;
> +  ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
> +
> +  //
> +  // Get information about the image being loaded
> +  //
> +  Status = PeCoffLoaderGetImageInfo (&ImageContext);
> +  if (EFI_ERROR (Status)) {
> +    if (Buffer != NULL) {
> +      MmFreePool (Buffer);
> +    }
> +    return Status;
> +  }
> +
> +  PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
> +  DstBuffer = (UINTN)(-1);
> +
> +  Status = MmAllocatePages (
> +              AllocateMaxAddress,
> +              EfiRuntimeServicesCode,
> +              PageCount,
> +              &DstBuffer
> +              );
> +  if (EFI_ERROR (Status)) {
> +    if (Buffer != NULL) {
> +      MmFreePool (Buffer);
> +    }
> +    return Status;
> +  }
> +
> +  ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;
> +
> +  //
> +  // Align buffer on section boundry
> +  //
> +  ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
> +  ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)(ImageContext.SectionAlignment - 1));
> +
> +  //
> +  // Load the image to our new buffer
> +  //
> +  Status = PeCoffLoaderLoadImage (&ImageContext);
> +  if (EFI_ERROR (Status)) {
> +    if (Buffer != NULL) {
> +      MmFreePool (Buffer);
> +    }
> +    MmFreePages (DstBuffer, PageCount);
> +    return Status;
> +  }
> +
> +  //
> +  // Relocate the image in our new buffer
> +  //
> +  Status = PeCoffLoaderRelocateImage (&ImageContext);
> +  if (EFI_ERROR (Status)) {
> +    if (Buffer != NULL) {
> +      MmFreePool (Buffer);
> +    }
> +    MmFreePages (DstBuffer, PageCount);
> +    return Status;
> +  }
> +
> +  //
> +  // Flush the instruction cache so the image data are written before we execute it
> +  //
> +  InvalidateInstructionCacheRange ((VOID *)(UINTN) ImageContext.ImageAddress, (UINTN) ImageContext.ImageSize);
> +
> +  //
> +  // Save Image EntryPoint in DriverEntry
> +  //
> +  DriverEntry->ImageEntryPoint  = ImageContext.EntryPoint;
> +  DriverEntry->ImageBuffer      = DstBuffer;
> +  DriverEntry->NumberOfPage     = PageCount;
> +
> +  if (mEfiSystemTable != NULL) {
> +    Status = mEfiSystemTable->BootServices->AllocatePool (EfiBootServicesData, sizeof (EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&DriverEntry->LoadedImage);
> +    if (EFI_ERROR (Status)) {
> +      if (Buffer != NULL) {
> +        MmFreePool (Buffer);
> +      }
> +      MmFreePages (DstBuffer, PageCount);
> +      return Status;
> +    }
> +
> +    ZeroMem (DriverEntry->LoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));
> +    //
> +    // Fill in the remaining fields of the Loaded Image Protocol instance.
> +    // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
> +    //
> +    DriverEntry->LoadedImage->Revision      = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
> +    DriverEntry->LoadedImage->ParentHandle  = NULL;
> +    DriverEntry->LoadedImage->SystemTable   = mEfiSystemTable;
> +    DriverEntry->LoadedImage->DeviceHandle  = NULL;
> +    DriverEntry->LoadedImage->FilePath      = NULL;
> +
> +    DriverEntry->LoadedImage->ImageBase     = (VOID *)(UINTN)DriverEntry->ImageBuffer;
> +    DriverEntry->LoadedImage->ImageSize     = ImageContext.ImageSize;
> +    DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;
> +    DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;
> +
> +    //
> +    // Create a new image handle in the UEFI handle database for the MM Driver
> +    //
> +    DriverEntry->ImageHandle = NULL;
> +    Status = mEfiSystemTable->BootServices->InstallMultipleProtocolInterfaces (
> +                    &DriverEntry->ImageHandle,
> +                    &gEfiLoadedImageProtocolGuid, DriverEntry->LoadedImage,
> +                    NULL
> +                    );
> +  }
> +
> +  //
> +  // Print the load address and the PDB file name if it is available
> +  //
> +
> +  DEBUG_CODE_BEGIN ();
> +
> +    UINTN Index;
> +    UINTN StartIndex;
> +    CHAR8 EfiFileName[256];
> +
> +
> +    DEBUG ((DEBUG_INFO | DEBUG_LOAD,
> +           "Loading MM driver at 0x%11p EntryPoint=0x%11p ",
> +           (VOID *)(UINTN) ImageContext.ImageAddress,
> +           FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));
> +
> +
> +    //
> +    // Print Module Name by Pdb file path.
> +    // Windows and Unix style file path are all trimmed correctly.
> +    //
> +    if (ImageContext.PdbPointer != NULL) {
> +      StartIndex = 0;
> +      for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {
> +        if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {
> +          StartIndex = Index + 1;
> +        }
> +      }
> +      //
> +      // Copy the PDB file name to our temporary string, and replace .pdb with .efi
> +      // The PDB file name is limited in the range of 0~255.
> +      // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
> +      //
> +      for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {
> +        EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];
> +        if (EfiFileName[Index] == 0) {
> +          EfiFileName[Index] = '.';
> +        }
> +        if (EfiFileName[Index] == '.') {
> +          EfiFileName[Index + 1] = 'e';
> +          EfiFileName[Index + 2] = 'f';
> +          EfiFileName[Index + 3] = 'i';
> +          EfiFileName[Index + 4] = 0;
> +          break;
> +        }
> +      }
> +
> +      if (Index == sizeof (EfiFileName) - 4) {
> +        EfiFileName[Index] = 0;
> +      }
> +      DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));
> +    }
> +    DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));
> +
> +  DEBUG_CODE_END ();
> +
> +  //
> +  // Free buffer allocated by Fv->ReadSection.
> +  //
> +  // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
> +  // used the UEFI Boot Services AllocatePool() function
> +  //
> +  MmFreePool(Buffer);
> +  return Status;
> +}
> +
> +/**
> +  Preprocess dependency expression and update DriverEntry to reflect the
> +  state of  Before and After dependencies. If DriverEntry->Before
> +  or DriverEntry->After is set it will never be cleared.
> +
> +  @param  DriverEntry           DriverEntry element to update .
> +
> +  @retval EFI_SUCCESS           It always works.
> +
> +**/
> +EFI_STATUS
> +MmPreProcessDepex (
> +  IN EFI_MM_DRIVER_ENTRY  *DriverEntry
> +  )
> +{
> +  UINT8  *Iterator;
> +
> +  Iterator = DriverEntry->Depex;
> +  DriverEntry->Dependent = TRUE;
> +
> +  if (*Iterator == EFI_DEP_BEFORE) {
> +    DriverEntry->Before = TRUE;
> +  } else if (*Iterator == EFI_DEP_AFTER) {
> +    DriverEntry->After = TRUE;
> +  }
> +
> +  if (DriverEntry->Before || DriverEntry->After) {
> +    CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));
> +  }
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Read Depex and pre-process the Depex for Before and After. If Section Extraction
> +  protocol returns an error via ReadSection defer the reading of the Depex.
> +
> +  @param  DriverEntry           Driver to work on.
> +
> +  @retval EFI_SUCCESS           Depex read and preprossesed
> +  @retval EFI_PROTOCOL_ERROR    The section extraction protocol returned an error
> +                                and  Depex reading needs to be retried.
> +  @retval Error                 DEPEX not found.
> +
> +**/
> +EFI_STATUS
> +MmGetDepexSectionAndPreProccess (
> +  IN EFI_MM_DRIVER_ENTRY  *DriverEntry
> +  )
> +{
> +  EFI_STATUS                     Status;
> +
> +  //
> +  // Data already read
> +  //
> +  if (DriverEntry->Depex == NULL) {
> +    Status = EFI_NOT_FOUND;
> +  } else {
> +    Status = EFI_SUCCESS;
> +  }
> +  if (EFI_ERROR (Status)) {
> +    if (Status == EFI_PROTOCOL_ERROR) {
> +      //
> +      // The section extraction protocol failed so set protocol error flag
> +      //
> +      DriverEntry->DepexProtocolError = TRUE;
> +    } else {
> +      //
> +      // If no Depex assume depend on all architectural protocols
> +      //
> +      DriverEntry->Depex = NULL;
> +      DriverEntry->Dependent = TRUE;
> +      DriverEntry->DepexProtocolError = FALSE;
> +    }
> +  } else {
> +    //
> +    // Set Before and After state information based on Depex
> +    // Driver will be put in Dependent state
> +    //
> +    MmPreProcessDepex (DriverEntry);
> +    DriverEntry->DepexProtocolError = FALSE;
> +  }
> +
> +  return Status;
> +}
> +
> +/**
> +  This is the main Dispatcher for MM and it exits when there are no more
> +  drivers to run. Drain the mScheduledQueue and load and start a PE
> +  image for each driver. Search the mDiscoveredList to see if any driver can
> +  be placed on the mScheduledQueue. If no drivers are placed on the
> +  mScheduledQueue exit the function.
> +
> +  @retval EFI_SUCCESS           All of the MM Drivers that could be dispatched
> +                                have been run and the MM Entry Point has been
> +                                registered.
> +  @retval EFI_NOT_READY         The MM Driver that registered the MM Entry Point
> +                                was just dispatched.
> +  @retval EFI_NOT_FOUND         There are no MM Drivers available to be dispatched.
> +  @retval EFI_ALREADY_STARTED   The MM Dispatcher is already running
> +
> +**/
> +EFI_STATUS
> +MmDispatcher (
> +  VOID
> +  )
> +{
> +  EFI_STATUS            Status;
> +  LIST_ENTRY            *Link;
> +  EFI_MM_DRIVER_ENTRY  *DriverEntry;
> +  BOOLEAN               ReadyToRun;
> +  BOOLEAN               PreviousMmEntryPointRegistered;
> +
> +  DEBUG ((DEBUG_INFO, "MmDispatcher\n"));
> +
> +  if (!gRequestDispatch) {
> +    DEBUG ((DEBUG_INFO, "  !gRequestDispatch\n"));
> +    return EFI_NOT_FOUND;
> +  }
> +
> +  if (gDispatcherRunning) {
> +    DEBUG ((DEBUG_INFO, "  gDispatcherRunning\n"));
> +    //
> +    // If the dispatcher is running don't let it be restarted.
> +    //
> +    return EFI_ALREADY_STARTED;
> +  }
> +
> +  gDispatcherRunning = TRUE;
> +
> +  do {
> +    //
> +    // Drain the Scheduled Queue
> +    //
> +    DEBUG ((DEBUG_INFO, "  Drain the Scheduled Queue\n"));
> +    while (!IsListEmpty (&mScheduledQueue)) {
> +      DriverEntry = CR (
> +                      mScheduledQueue.ForwardLink,
> +                      EFI_MM_DRIVER_ENTRY,
> +                      ScheduledLink,
> +                      EFI_MM_DRIVER_ENTRY_SIGNATURE
> +                      );
> +      DEBUG ((DEBUG_INFO, "  DriverEntry (Scheduled) - %g\n", &DriverEntry->FileName));
> +
> +      //
> +      // Load the MM Driver image into memory. If the Driver was transitioned from
> +      // Untrused to Scheduled it would have already been loaded so we may need to
> +      // skip the LoadImage
> +      //
> +      if (DriverEntry->ImageHandle == NULL) {
> +        Status = MmLoadImage (DriverEntry);
> +
> +        //
> +        // Update the driver state to reflect that it's been loaded
> +        //
> +        if (EFI_ERROR (Status)) {
> +          //
> +          // The MM Driver could not be loaded, and do not attempt to load or start it again.
> +          // Take driver from Scheduled to Initialized.
> +          //
> +          DriverEntry->Initialized  = TRUE;
> +          DriverEntry->Scheduled = FALSE;
> +          RemoveEntryList (&DriverEntry->ScheduledLink);
> +
> +          //
> +          // If it's an error don't try the StartImage
> +          //
> +          continue;
> +        }
> +      }
> +
> +      DriverEntry->Scheduled    = FALSE;
> +      DriverEntry->Initialized  = TRUE;
> +      RemoveEntryList (&DriverEntry->ScheduledLink);
> +
> +      /*REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
> +        EFI_PROGRESS_CODE,
> +        EFI_SOFTWARE_MM_DRIVER | EFI_SW_PC_INIT_BEGIN,
> +        &DriverEntry->ImageHandle,
> +        sizeof (DriverEntry->ImageHandle)
> +        );*/
> +
> +      //
> +      // Cache state of MmEntryPointRegistered before calling entry point
> +      //
> +      PreviousMmEntryPointRegistered = gMmCorePrivate->MmEntryPointRegistered;
> +
> +      //
> +      // For each MM driver, pass NULL as ImageHandle
> +      //
> +      if (mEfiSystemTable == NULL) {
> +        DEBUG ((DEBUG_INFO, "StartImage - 0x%x (Standalone Mode)\n", DriverEntry->ImageEntryPoint));
> +        Status = ((MM_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, &gMmCoreMmst);
> +      } else {
> +        DEBUG ((DEBUG_INFO, "StartImage - 0x%x (Tradition Mode)\n", DriverEntry->ImageEntryPoint));
> +        Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, mEfiSystemTable);
> +      }
> +      if (EFI_ERROR(Status)){
> +        DEBUG ((DEBUG_INFO, "StartImage Status - %r\n", Status));
> +        MmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);
> +      }
> +
> +      /*REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
> +        EFI_PROGRESS_CODE,
> +        EFI_SOFTWARE_MM_DRIVER | EFI_SW_PC_INIT_END,
> +        &DriverEntry->ImageHandle,
> +        sizeof (DriverEntry->ImageHandle)
> +        );*/
> +
> +      if (!PreviousMmEntryPointRegistered && gMmCorePrivate->MmEntryPointRegistered) {
> +        //
> +        // Return immediately if the MM Entry Point was registered by the MM
> +        // Driver that was just dispatched.  The MM IPL will reinvoke the MM
> +        // Core Dispatcher.  This is required so MM Mode may be enabled as soon
> +        // as all the dependent MM Drivers for MM Mode have been dispatched.
> +        // Once the MM Entry Point has been registered, then MM Mode will be
> +        // used.
> +        //
> +        gRequestDispatch = TRUE;
> +        gDispatcherRunning = FALSE;
> +        return EFI_NOT_READY;
> +      }
> +    }
> +
> +    //
> +    // Search DriverList for items to place on Scheduled Queue
> +    //
> +    DEBUG ((DEBUG_INFO, "  Search DriverList for items to place on Scheduled Queue\n"));
> +    ReadyToRun = FALSE;
> +    for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
> +      DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
> +      DEBUG ((DEBUG_INFO, "  DriverEntry (Discovered) - %g\n", &DriverEntry->FileName));
> +
> +      if (DriverEntry->DepexProtocolError){
> +        //
> +        // If Section Extraction Protocol did not let the Depex be read before retry the read
> +        //
> +        Status = MmGetDepexSectionAndPreProccess (DriverEntry);
> +      }
> +
> +      if (DriverEntry->Dependent) {
> +        if (MmIsSchedulable (DriverEntry)) {
> +          MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
> +          ReadyToRun = TRUE;
> +        }
> +      }
> +    }
> +  } while (ReadyToRun);
> +
> +  //
> +  // If there is no more MM driver to dispatch, stop the dispatch request
> +  //
> +  DEBUG ((DEBUG_INFO, "  no more MM driver to dispatch, stop the dispatch request\n"));
> +  gRequestDispatch = FALSE;
> +  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
> +    DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
> +    DEBUG ((DEBUG_INFO, "  DriverEntry (Discovered) - %g\n", &DriverEntry->FileName));
> +
> +    if (!DriverEntry->Initialized){
> +      //
> +      // We have MM driver pending to dispatch
> +      //
> +      gRequestDispatch = TRUE;
> +      break;
> +    }
> +  }
> +
> +  gDispatcherRunning = FALSE;
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
> +  must add any driver with a before dependency on InsertedDriverEntry first.
> +  You do this by recursively calling this routine. After all the Befores are
> +  processed you can add InsertedDriverEntry to the mScheduledQueue.
> +  Then you can add any driver with an After dependency on InsertedDriverEntry
> +  by recursively calling this routine.
> +
> +  @param  InsertedDriverEntry   The driver to insert on the ScheduledLink Queue
> +
> +**/
> +VOID
> +MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
> +  IN  EFI_MM_DRIVER_ENTRY   *InsertedDriverEntry
> +  )
> +{
> +  LIST_ENTRY            *Link;
> +  EFI_MM_DRIVER_ENTRY *DriverEntry;
> +
> +  //
> +  // Process Before Dependency
> +  //
> +  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
> +    DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
> +    if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
> +      DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
> +      DEBUG ((DEBUG_DISPATCH, "  BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
> +      if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
> +        //
> +        // Recursively process BEFORE
> +        //
> +        DEBUG ((DEBUG_DISPATCH, "TRUE\n  END\n  RESULT = TRUE\n"));
> +        MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
> +      } else {
> +        DEBUG ((DEBUG_DISPATCH, "FALSE\n  END\n  RESULT = FALSE\n"));
> +      }
> +    }
> +  }
> +
> +  //
> +  // Convert driver from Dependent to Scheduled state
> +  //
> +
> +  InsertedDriverEntry->Dependent = FALSE;
> +  InsertedDriverEntry->Scheduled = TRUE;
> +  InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);
> +
> +
> +  //
> +  // Process After Dependency
> +  //
> +  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
> +    DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
> +    if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
> +      DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
> +      DEBUG ((DEBUG_DISPATCH, "  AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
> +      if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
> +        //
> +        // Recursively process AFTER
> +        //
> +        DEBUG ((DEBUG_DISPATCH, "TRUE\n  END\n  RESULT = TRUE\n"));
> +        MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
> +      } else {
> +        DEBUG ((DEBUG_DISPATCH, "FALSE\n  END\n  RESULT = FALSE\n"));
> +      }
> +    }
> +  }
> +}
> +
> +/**
> +  Return TRUE if the Fv has been processed, FALSE if not.
> +
> +  @param  FvHandle              The handle of a FV that's being tested
> +
> +  @retval TRUE                  Fv protocol on FvHandle has been processed
> +  @retval FALSE                 Fv protocol on FvHandle has not yet been
> +                                processed
> +
> +**/
> +BOOLEAN
> +FvHasBeenProcessed (
> +  IN EFI_HANDLE  FvHandle
> +  )
> +{
> +  LIST_ENTRY    *Link;
> +  KNOWN_HANDLE  *KnownHandle;
> +
> +  for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {
> +    KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);
> +    if (KnownHandle->Handle == FvHandle) {
> +      return TRUE;
> +    }
> +  }
> +  return FALSE;
> +}
> +
> +/**
> +  Remember that Fv protocol on FvHandle has had it's drivers placed on the
> +  mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are
> +  never removed/freed from the mFvHandleList.
> +
> +  @param  FvHandle              The handle of a FV that has been processed
> +
> +**/
> +VOID
> +FvIsBeingProcesssed (
> +  IN EFI_HANDLE  FvHandle
> +  )
> +{
> +  KNOWN_HANDLE  *KnownHandle;
> +
> +  DEBUG ((DEBUG_INFO, "FvIsBeingProcesssed - 0x%08x\n", FvHandle));
> +
> +  KnownHandle = AllocatePool (sizeof (KNOWN_HANDLE));
> +  ASSERT (KnownHandle != NULL);
> +
> +  KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;
> +  KnownHandle->Handle = FvHandle;
> +  InsertTailList (&mFvHandleList, &KnownHandle->Link);
> +}
> +
> +/**
> +  Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
> +  and initilize any state variables. Read the Depex from the FV and store it
> +  in DriverEntry. Pre-process the Depex to set the Before and After state.
> +  The Discovered list is never free'ed and contains booleans that represent the
> +  other possible MM driver states.
> +
> +  @param  Fv                    Fv protocol, needed to read Depex info out of
> +                                FLASH.
> +  @param  FvHandle              Handle for Fv, needed in the
> +                                EFI_MM_DRIVER_ENTRY so that the PE image can be
> +                                read out of the FV at a later time.
> +  @param  DriverName            Name of driver to add to mDiscoveredList.
> +
> +  @retval EFI_SUCCESS           If driver was added to the mDiscoveredList.
> +  @retval EFI_ALREADY_STARTED   The driver has already been started. Only one
> +                                DriverName may be active in the system at any one
> +                                time.
> +
> +**/
> +EFI_STATUS
> +MmAddToDriverList (
> +  IN EFI_HANDLE   FvHandle,
> +  IN VOID         *Pe32Data,
> +  IN UINTN        Pe32DataSize,
> +  IN VOID         *Depex,
> +  IN UINTN        DepexSize,
> +  IN EFI_GUID     *DriverName
> +  )
> +{
> +  EFI_MM_DRIVER_ENTRY  *DriverEntry;
> +
> +  DEBUG ((DEBUG_INFO, "MmAddToDriverList - %g (0x%08x)\n", DriverName, Pe32Data));
> +
> +  //
> +  // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
> +  // NULL or FALSE.
> +  //
> +  DriverEntry = AllocateZeroPool (sizeof (EFI_MM_DRIVER_ENTRY));
> +  ASSERT (DriverEntry != NULL);
> +
> +  DriverEntry->Signature        = EFI_MM_DRIVER_ENTRY_SIGNATURE;
> +  CopyGuid (&DriverEntry->FileName, DriverName);
> +  DriverEntry->FvHandle         = FvHandle;
> +  DriverEntry->Pe32Data         = Pe32Data;
> +  DriverEntry->Pe32DataSize     = Pe32DataSize;
> +  DriverEntry->Depex            = Depex;
> +  DriverEntry->DepexSize        = DepexSize;
> +
> +  MmGetDepexSectionAndPreProccess (DriverEntry);
> +
> +  InsertTailList (&mDiscoveredList, &DriverEntry->Link);
> +  gRequestDispatch = TRUE;
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  Event notification that is fired every time a FV dispatch protocol is added.
> +  More than one protocol may have been added when this event is fired, so you
> +  must loop on MmLocateHandle () to see how many protocols were added and
> +  do the following to each FV:
> +  If the Fv has already been processed, skip it. If the Fv has not been
> +  processed then mark it as being processed, as we are about to process it.
> +  Read the Fv and add any driver in the Fv to the mDiscoveredList.The
> +  mDiscoveredList is never free'ed and contains variables that define
> +  the other states the MM driver transitions to..
> +  While you are at it read the A Priori file into memory.
> +  Place drivers in the A Priori list onto the mScheduledQueue.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmDriverDispatchHandler (
> +  IN     EFI_HANDLE  DispatchHandle,
> +  IN     CONST VOID  *Context,        OPTIONAL
> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_STATUS                            Status;
> +
> +  DEBUG ((DEBUG_INFO, "MmDriverDispatchHandler\n"));
> +
> +  //
> +  // Execute the MM Dispatcher on any newly discovered FVs and previously
> +  // discovered MM drivers that have been discovered but not dispatched.
> +  //
> +  Status = MmDispatcher ();
> +
> +  //
> +  // Check to see if CommBuffer and CommBufferSize are valid
> +  //
> +  if (CommBuffer != NULL && CommBufferSize != NULL) {
> +    if (*CommBufferSize > 0) {
> +      if (Status == EFI_NOT_READY) {
> +        //
> +        // If a the MM Core Entry Point was just registered, then set flag to
> +        // request the MM Dispatcher to be restarted.
> +        //
> +        *(UINT8 *)CommBuffer = COMM_BUFFER_MM_DISPATCH_RESTART;
> +      } else if (!EFI_ERROR (Status)) {
> +        //
> +        // Set the flag to show that the MM Dispatcher executed without errors
> +        //
> +        *(UINT8 *)CommBuffer = COMM_BUFFER_MM_DISPATCH_SUCCESS;
> +      } else {
> +        //
> +        // Set the flag to show that the MM Dispatcher encountered an error
> +        //
> +        *(UINT8 *)CommBuffer = COMM_BUFFER_MM_DISPATCH_ERROR;
> +      }
> +    }
> +  }
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmFvDispatchHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_STATUS                            Status;
> +  EFI_MM_COMMUNICATE_FV_DISPATCH_DATA  *CommunicationFvDispatchData;
> +  EFI_FIRMWARE_VOLUME_HEADER            *FwVolHeader;
> +
> +  DEBUG ((DEBUG_INFO, "MmFvDispatchHandler\n"));
> +
> +  CommunicationFvDispatchData = CommBuffer;
> +
> +  DEBUG ((DEBUG_INFO, "  Dispatch - 0x%016lx - 0x%016lx\n", CommunicationFvDispatchData->Address, CommunicationFvDispatchData->Size));
> +
> +  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)CommunicationFvDispatchData->Address;
> +
> +  MmCoreFfsFindMmDriver (FwVolHeader);
> +
> +  //
> +  // Execute the MM Dispatcher on any newly discovered FVs and previously
> +  // discovered MM drivers that have been discovered but not dispatched.
> +  //
> +  Status = MmDispatcher ();
> +
> +  return Status;
> +}
> +
> +/**
> +  Traverse the discovered list for any drivers that were discovered but not loaded
> +  because the dependency experessions evaluated to false.
> +
> +**/
> +VOID
> +MmDisplayDiscoveredNotDispatched (
> +  VOID
> +  )
> +{
> +  LIST_ENTRY                   *Link;
> +  EFI_MM_DRIVER_ENTRY         *DriverEntry;
> +
> +  for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {
> +    DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
> +    if (DriverEntry->Dependent) {
> +      DEBUG ((DEBUG_LOAD, "MM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));
> +    }
> +  }
> +}
> diff --git a/StandaloneMmPkg/Core/FwVol.c b/StandaloneMmPkg/Core/FwVol.c
> new file mode 100644
> index 0000000000..901c58bc53
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/FwVol.c
> @@ -0,0 +1,104 @@
> +/**@file
> +
> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +#include <Library/FvLib.h>
> +
> +//
> +// List of file types supported by dispatcher
> +//
> +EFI_FV_FILETYPE mMmFileTypes[] = {
> +  EFI_FV_FILETYPE_MM,
> +  0xE, //EFI_FV_FILETYPE_MM_STANDALONE,
> +       //
> +       // Note: DXE core will process the FV image file, so skip it in MM core
> +       // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
> +       //
> +};
> +
> +EFI_STATUS
> +MmAddToDriverList (
> +  IN EFI_HANDLE   FvHandle,
> +  IN VOID         *Pe32Data,
> +  IN UINTN        Pe32DataSize,
> +  IN VOID         *Depex,
> +  IN UINTN        DepexSize,
> +  IN EFI_GUID     *DriverName
> +  );
> +
> +BOOLEAN
> +FvHasBeenProcessed (
> +  IN EFI_HANDLE  FvHandle
> +  );
> +
> +VOID
> +FvIsBeingProcesssed (
> +  IN EFI_HANDLE  FvHandle
> +  );
> +
> +EFI_STATUS
> +MmCoreFfsFindMmDriver (
> +  IN  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader
> +  )
> +/*++
> +
> +Routine Description:
> +  Given the pointer to the Firmware Volume Header find the
> +  MM driver and return it's PE32 image.
> +
> +Arguments:
> +  FwVolHeader - Pointer to memory mapped FV
> +
> +Returns:
> +  other       - Failure
> +
> +--*/
> +{
> +  EFI_STATUS          Status;
> +  EFI_STATUS          DepexStatus;
> +  EFI_FFS_FILE_HEADER *FileHeader;
> +  EFI_FV_FILETYPE     FileType;
> +  VOID                *Pe32Data;
> +  UINTN               Pe32DataSize;
> +  VOID                *Depex;
> +  UINTN               DepexSize;
> +  UINTN               Index;
> +
> +  DEBUG((DEBUG_INFO, "MmCoreFfsFindMmDriver - 0x%x\n", FwVolHeader));
> +
> +  if (FvHasBeenProcessed (FwVolHeader)) {
> +    return EFI_SUCCESS;
> +  }
> +
> +  FvIsBeingProcesssed (FwVolHeader);
> +
> +  for (Index = 0; Index < sizeof(mMmFileTypes) / sizeof(mMmFileTypes[0]); Index++) {
> +    DEBUG((DEBUG_INFO, "Check MmFileTypes - 0x%x\n", mMmFileTypes[Index]));
> +    FileType = mMmFileTypes[Index];
> +    FileHeader = NULL;
> +    do {
> +      Status = FfsFindNextFile(FileType, FwVolHeader, &FileHeader);
> +      if (!EFI_ERROR(Status)) {
> +        Status = FfsFindSectionData(EFI_SECTION_PE32, FileHeader, &Pe32Data, &Pe32DataSize);
> +        DEBUG((DEBUG_INFO, "Find PE data - 0x%x\n", Pe32Data));
> +        DepexStatus = FfsFindSectionData(EFI_SECTION_MM_DEPEX, FileHeader, &Depex, &DepexSize);
> +        if (!EFI_ERROR(DepexStatus)) {
> +          MmAddToDriverList(FwVolHeader, Pe32Data, Pe32DataSize, Depex, DepexSize, &FileHeader->Name);
> +        }
> +      }
> +    } while (!EFI_ERROR(Status));
> +  }
> +
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Core/Handle.c b/StandaloneMmPkg/Core/Handle.c
> new file mode 100644
> index 0000000000..01832f4bbe
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Handle.c
> @@ -0,0 +1,533 @@
> +/** @file
> +  SMM handle & protocol handling.
> +
> +  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +//
> +// mProtocolDatabase     - A list of all protocols in the system.  (simple list for now)
> +// gHandleList           - A list of all the handles in the system
> +//
> +LIST_ENTRY  mProtocolDatabase  = INITIALIZE_LIST_HEAD_VARIABLE (mProtocolDatabase);
> +LIST_ENTRY  gHandleList        = INITIALIZE_LIST_HEAD_VARIABLE (gHandleList);
> +
> +/**
> +  Check whether a handle is a valid EFI_HANDLE
> +
> +  @param  UserHandle             The handle to check
> +
> +  @retval EFI_INVALID_PARAMETER  The handle is NULL or not a valid EFI_HANDLE.
> +  @retval EFI_SUCCESS            The handle is valid EFI_HANDLE.
> +
> +**/
> +EFI_STATUS
> +MmValidateHandle (
> +  IN EFI_HANDLE  UserHandle
> +  )
> +{
> +  IHANDLE  *Handle;
> +
> +  Handle = (IHANDLE *)UserHandle;
> +  if (Handle == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +  if (Handle->Signature != EFI_HANDLE_SIGNATURE) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Finds the protocol entry for the requested protocol.
> +
> +  @param  Protocol               The ID of the protocol
> +  @param  Create                 Create a new entry if not found
> +
> +  @return Protocol entry
> +
> +**/
> +PROTOCOL_ENTRY  *
> +MmFindProtocolEntry (
> +  IN EFI_GUID   *Protocol,
> +  IN BOOLEAN    Create
> +  )
> +{
> +  LIST_ENTRY          *Link;
> +  PROTOCOL_ENTRY      *Item;
> +  PROTOCOL_ENTRY      *ProtEntry;
> +
> +  //
> +  // Search the database for the matching GUID
> +  //
> +
> +  ProtEntry = NULL;
> +  for (Link = mProtocolDatabase.ForwardLink;
> +       Link != &mProtocolDatabase;
> +       Link = Link->ForwardLink) {
> +
> +    Item = CR(Link, PROTOCOL_ENTRY, AllEntries, PROTOCOL_ENTRY_SIGNATURE);
> +    if (CompareGuid (&Item->ProtocolID, Protocol)) {
> +      //
> +      // This is the protocol entry
> +      //
> +      ProtEntry = Item;
> +      break;
> +    }
> +  }
> +
> +  //
> +  // If the protocol entry was not found and Create is TRUE, then
> +  // allocate a new entry
> +  //
> +  if ((ProtEntry == NULL) && Create) {
> +    ProtEntry = AllocatePool (sizeof(PROTOCOL_ENTRY));
> +    if (ProtEntry != NULL) {
> +      //
> +      // Initialize new protocol entry structure
> +      //
> +      ProtEntry->Signature = PROTOCOL_ENTRY_SIGNATURE;
> +      CopyGuid ((VOID *)&ProtEntry->ProtocolID, Protocol);
> +      InitializeListHead (&ProtEntry->Protocols);
> +      InitializeListHead (&ProtEntry->Notify);
> +
> +      //
> +      // Add it to protocol database
> +      //
> +      InsertTailList (&mProtocolDatabase, &ProtEntry->AllEntries);
> +    }
> +  }
> +  return ProtEntry;
> +}
> +
> +/**
> +  Finds the protocol instance for the requested handle and protocol.
> +  Note: This function doesn't do parameters checking, it's caller's responsibility
> +  to pass in valid parameters.
> +
> +  @param  Handle                 The handle to search the protocol on
> +  @param  Protocol               GUID of the protocol
> +  @param  Interface              The interface for the protocol being searched
> +
> +  @return Protocol instance (NULL: Not found)
> +
> +**/
> +PROTOCOL_INTERFACE *
> +MmFindProtocolInterface (
> +  IN IHANDLE   *Handle,
> +  IN EFI_GUID  *Protocol,
> +  IN VOID      *Interface
> +  )
> +{
> +  PROTOCOL_INTERFACE  *Prot;
> +  PROTOCOL_ENTRY      *ProtEntry;
> +  LIST_ENTRY          *Link;
> +
> +  Prot = NULL;
> +
> +  //
> +  // Lookup the protocol entry for this protocol ID
> +  //
> +  ProtEntry = MmFindProtocolEntry (Protocol, FALSE);
> +  if (ProtEntry != NULL) {
> +    //
> +    // Look at each protocol interface for any matches
> +    //
> +    for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link=Link->ForwardLink) {
> +      //
> +      // If this protocol interface matches, remove it
> +      //
> +      Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);
> +      if (Prot->Interface == Interface && Prot->Protocol == ProtEntry) {
> +        break;
> +      }
> +      Prot = NULL;
> +    }
> +  }
> +  return Prot;
> +}
> +
> +/**
> +  Wrapper function to MmInstallProtocolInterfaceNotify.  This is the public API which
> +  Calls the private one which contains a BOOLEAN parameter for notifications
> +
> +  @param  UserHandle             The handle to install the protocol handler on,
> +                                 or NULL if a new handle is to be allocated
> +  @param  Protocol               The protocol to add to the handle
> +  @param  InterfaceType          Indicates whether Interface is supplied in
> +                                 native form.
> +  @param  Interface              The interface for the protocol being added
> +
> +  @return Status code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInstallProtocolInterface (
> +  IN OUT EFI_HANDLE      *UserHandle,
> +  IN EFI_GUID            *Protocol,
> +  IN EFI_INTERFACE_TYPE  InterfaceType,
> +  IN VOID                *Interface
> +  )
> +{
> +  return MmInstallProtocolInterfaceNotify (
> +           UserHandle,
> +           Protocol,
> +           InterfaceType,
> +           Interface,
> +           TRUE
> +           );
> +}
> +
> +/**
> +  Installs a protocol interface into the boot services environment.
> +
> +  @param  UserHandle             The handle to install the protocol handler on,
> +                                 or NULL if a new handle is to be allocated
> +  @param  Protocol               The protocol to add to the handle
> +  @param  InterfaceType          Indicates whether Interface is supplied in
> +                                 native form.
> +  @param  Interface              The interface for the protocol being added
> +  @param  Notify                 indicates whether notify the notification list
> +                                 for this protocol
> +
> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> +  @retval EFI_OUT_OF_RESOURCES   No enough buffer to allocate
> +  @retval EFI_SUCCESS            Protocol interface successfully installed
> +
> +**/
> +EFI_STATUS
> +MmInstallProtocolInterfaceNotify (
> +  IN OUT EFI_HANDLE          *UserHandle,
> +  IN     EFI_GUID            *Protocol,
> +  IN     EFI_INTERFACE_TYPE  InterfaceType,
> +  IN     VOID                *Interface,
> +  IN     BOOLEAN             Notify
> +  )
> +{
> +  PROTOCOL_INTERFACE  *Prot;
> +  PROTOCOL_ENTRY      *ProtEntry;
> +  IHANDLE             *Handle;
> +  EFI_STATUS          Status;
> +  VOID                *ExistingInterface;
> +
> +  //
> +  // returns EFI_INVALID_PARAMETER if InterfaceType is invalid.
> +  // Also added check for invalid UserHandle and Protocol pointers.
> +  //
> +  if (UserHandle == NULL || Protocol == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  if (InterfaceType != EFI_NATIVE_INTERFACE) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  //
> +  // Print debug message
> +  //
> +  DEBUG((DEBUG_LOAD | DEBUG_INFO, "MmInstallProtocolInterface: %g %p\n", Protocol, Interface));
> +
> +  Status = EFI_OUT_OF_RESOURCES;
> +  Prot = NULL;
> +  Handle = NULL;
> +
> +  if (*UserHandle != NULL) {
> +    Status = MmHandleProtocol (*UserHandle, Protocol, (VOID **)&ExistingInterface);
> +    if (!EFI_ERROR (Status)) {
> +      return EFI_INVALID_PARAMETER;
> +    }
> +  }
> +
> +  //
> +  // Lookup the Protocol Entry for the requested protocol
> +  //
> +  ProtEntry = MmFindProtocolEntry (Protocol, TRUE);
> +  if (ProtEntry == NULL) {
> +    goto Done;
> +  }
> +
> +  //
> +  // Allocate a new protocol interface structure
> +  //
> +  Prot = AllocateZeroPool (sizeof(PROTOCOL_INTERFACE));
> +  if (Prot == NULL) {
> +    Status = EFI_OUT_OF_RESOURCES;
> +    goto Done;
> +  }
> +
> +  //
> +  // If caller didn't supply a handle, allocate a new one
> +  //
> +  Handle = (IHANDLE *)*UserHandle;
> +  if (Handle == NULL) {
> +    Handle = AllocateZeroPool (sizeof(IHANDLE));
> +    if (Handle == NULL) {
> +      Status = EFI_OUT_OF_RESOURCES;
> +      goto Done;
> +    }
> +
> +    //
> +    // Initialize new handler structure
> +    //
> +    Handle->Signature = EFI_HANDLE_SIGNATURE;
> +    InitializeListHead (&Handle->Protocols);
> +
> +    //
> +    // Add this handle to the list global list of all handles
> +    // in the system
> +    //
> +    InsertTailList (&gHandleList, &Handle->AllHandles);
> +  }
> +
> +  Status = MmValidateHandle (Handle);
> +  if (EFI_ERROR (Status)) {
> +    goto Done;
> +  }
> +
> +  //
> +  // Each interface that is added must be unique
> +  //
> +  ASSERT (MmFindProtocolInterface (Handle, Protocol, Interface) == NULL);
> +
> +  //
> +  // Initialize the protocol interface structure
> +  //
> +  Prot->Signature = PROTOCOL_INTERFACE_SIGNATURE;
> +  Prot->Handle = Handle;
> +  Prot->Protocol = ProtEntry;
> +  Prot->Interface = Interface;
> +
> +  //
> +  // Add this protocol interface to the head of the supported
> +  // protocol list for this handle
> +  //
> +  InsertHeadList (&Handle->Protocols, &Prot->Link);
> +
> +  //
> +  // Add this protocol interface to the tail of the
> +  // protocol entry
> +  //
> +  InsertTailList (&ProtEntry->Protocols, &Prot->ByProtocol);
> +
> +  //
> +  // Notify the notification list for this protocol
> +  //
> +  if (Notify) {
> +    MmNotifyProtocol (Prot);
> +  }
> +  Status = EFI_SUCCESS;
> +
> +Done:
> +  if (!EFI_ERROR (Status)) {
> +    //
> +    // Return the new handle back to the caller
> +    //
> +    *UserHandle = Handle;
> +  } else {
> +    //
> +    // There was an error, clean up
> +    //
> +    if (Prot != NULL) {
> +      FreePool (Prot);
> +    }
> +  }
> +  return Status;
> +}
> +
> +/**
> +  Uninstalls all instances of a protocol:interfacer from a handle.
> +  If the last protocol interface is remove from the handle, the
> +  handle is freed.
> +
> +  @param  UserHandle             The handle to remove the protocol handler from
> +  @param  Protocol               The protocol, of protocol:interface, to remove
> +  @param  Interface              The interface, of protocol:interface, to remove
> +
> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL.
> +  @retval EFI_SUCCESS            Protocol interface successfully uninstalled.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmUninstallProtocolInterface (
> +  IN EFI_HANDLE  UserHandle,
> +  IN EFI_GUID    *Protocol,
> +  IN VOID        *Interface
> +  )
> +{
> +  EFI_STATUS          Status;
> +  IHANDLE             *Handle;
> +  PROTOCOL_INTERFACE  *Prot;
> +
> +  //
> +  // Check that Protocol is valid
> +  //
> +  if (Protocol == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  //
> +  // Check that UserHandle is a valid handle
> +  //
> +  Status = MmValidateHandle (UserHandle);
> +  if (EFI_ERROR (Status)) {
> +    return Status;
> +  }
> +
> +  //
> +  // Check that Protocol exists on UserHandle, and Interface matches the interface in the database
> +  //
> +  Prot = MmFindProtocolInterface (UserHandle, Protocol, Interface);
> +  if (Prot == NULL) {
> +    return EFI_NOT_FOUND;
> +  }
> +
> +  //
> +  // Remove the protocol interface from the protocol
> +  //
> +  Status = EFI_NOT_FOUND;
> +  Handle = (IHANDLE *)UserHandle;
> +  Prot   = MmRemoveInterfaceFromProtocol (Handle, Protocol, Interface);
> +
> +  if (Prot != NULL) {
> +    //
> +    // Remove the protocol interface from the handle
> +    //
> +    RemoveEntryList (&Prot->Link);
> +
> +    //
> +    // Free the memory
> +    //
> +    Prot->Signature = 0;
> +    FreePool (Prot);
> +    Status = EFI_SUCCESS;
> +  }
> +
> +  //
> +  // If there are no more handlers for the handle, free the handle
> +  //
> +  if (IsListEmpty (&Handle->Protocols)) {
> +    Handle->Signature = 0;
> +    RemoveEntryList (&Handle->AllHandles);
> +    FreePool (Handle);
> +  }
> +  return Status;
> +}
> +
> +/**
> +  Locate a certain GUID protocol interface in a Handle's protocols.
> +
> +  @param  UserHandle             The handle to obtain the protocol interface on
> +  @param  Protocol               The GUID of the protocol
> +
> +  @return The requested protocol interface for the handle
> +
> +**/
> +PROTOCOL_INTERFACE  *
> +MmGetProtocolInterface (
> +  IN EFI_HANDLE  UserHandle,
> +  IN EFI_GUID    *Protocol
> +  )
> +{
> +  EFI_STATUS          Status;
> +  PROTOCOL_ENTRY      *ProtEntry;
> +  PROTOCOL_INTERFACE  *Prot;
> +  IHANDLE             *Handle;
> +  LIST_ENTRY          *Link;
> +
> +  Status = MmValidateHandle (UserHandle);
> +  if (EFI_ERROR (Status)) {
> +    return NULL;
> +  }
> +
> +  Handle = (IHANDLE *)UserHandle;
> +
> +  //
> +  // Look at each protocol interface for a match
> +  //
> +  for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link = Link->ForwardLink) {
> +    Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);
> +    ProtEntry = Prot->Protocol;
> +    if (CompareGuid (&ProtEntry->ProtocolID, Protocol)) {
> +      return Prot;
> +    }
> +  }
> +  return NULL;
> +}
> +
> +/**
> +  Queries a handle to determine if it supports a specified protocol.
> +
> +  @param  UserHandle             The handle being queried.
> +  @param  Protocol               The published unique identifier of the protocol.
> +  @param  Interface              Supplies the address where a pointer to the
> +                                 corresponding Protocol Interface is returned.
> +
> +  @retval EFI_SUCCESS            The interface information for the specified protocol was returned.
> +  @retval EFI_UNSUPPORTED        The device does not support the specified protocol.
> +  @retval EFI_INVALID_PARAMETER  Handle is not a valid EFI_HANDLE..
> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL.
> +  @retval EFI_INVALID_PARAMETER  Interface is NULL.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmHandleProtocol (
> +  IN  EFI_HANDLE  UserHandle,
> +  IN  EFI_GUID    *Protocol,
> +  OUT VOID        **Interface
> +  )
> +{
> +  EFI_STATUS          Status;
> +  PROTOCOL_INTERFACE  *Prot;
> +
> +  //
> +  // Check for invalid Protocol
> +  //
> +  if (Protocol == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  //
> +  // Check for invalid Interface
> +  //
> +  if (Interface == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  } else {
> +    *Interface = NULL;
> +  }
> +
> +  //
> +  // Check for invalid UserHandle
> +  //
> +  Status = MmValidateHandle (UserHandle);
> +  if (EFI_ERROR (Status)) {
> +    return Status;
> +  }
> +
> +  //
> +  // Look at each protocol interface for a match
> +  //
> +  Prot = MmGetProtocolInterface (UserHandle, Protocol);
> +  if (Prot == NULL) {
> +    return EFI_UNSUPPORTED;
> +  }
> +
> +  //
> +  // This is the protocol interface entry for this protocol
> +  //
> +  *Interface = Prot->Interface;
> +
> +  return EFI_SUCCESS;
> +}
> diff --git a/StandaloneMmPkg/Core/InstallConfigurationTable.c b/StandaloneMmPkg/Core/InstallConfigurationTable.c
> new file mode 100644
> index 0000000000..3a31c63f94
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/InstallConfigurationTable.c
> @@ -0,0 +1,178 @@
> +/** @file
> +  System Management System Table Services MmInstallConfigurationTable service
> +
> +  Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +#define CONFIG_TABLE_SIZE_INCREASED 0x10
> +
> +UINTN  mMmSystemTableAllocateSize = 0;
> +
> +/**
> +  The MmInstallConfigurationTable() function is used to maintain the list
> +  of configuration tables that are stored in the System Management System
> +  Table.  The list is stored as an array of (GUID, Pointer) pairs.  The list
> +  must be allocated from pool memory with PoolType set to EfiRuntimeServicesData.
> +
> +  @param  SystemTable      A pointer to the SMM System Table (SMST).
> +  @param  Guid             A pointer to the GUID for the entry to add, update, or remove.
> +  @param  Table            A pointer to the buffer of the table to add.
> +  @param  TableSize        The size of the table to install.
> +
> +  @retval EFI_SUCCESS           The (Guid, Table) pair was added, updated, or removed.
> +  @retval EFI_INVALID_PARAMETER Guid is not valid.
> +  @retval EFI_NOT_FOUND         An attempt was made to delete a non-existent entry.
> +  @retval EFI_OUT_OF_RESOURCES  There is not enough memory available to complete the operation.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInstallConfigurationTable (
> +  IN  CONST EFI_MM_SYSTEM_TABLE    *SystemTable,
> +  IN  CONST EFI_GUID               *Guid,
> +  IN  VOID                         *Table,
> +  IN  UINTN                        TableSize
> +  )
> +{
> +  UINTN                    Index;
> +  EFI_CONFIGURATION_TABLE  *ConfigurationTable;
> +  EFI_CONFIGURATION_TABLE  *OldTable;
> +
> +  //
> +  // If Guid is NULL, then this operation cannot be performed
> +  //
> +  if (Guid == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  ConfigurationTable = gMmCoreMmst.MmConfigurationTable;
> +
> +  //
> +  // Search all the table for an entry that matches Guid
> +  //
> +  for (Index = 0; Index < gMmCoreMmst.NumberOfTableEntries; Index++) {
> +    if (CompareGuid (Guid, &(ConfigurationTable[Index].VendorGuid))) {
> +      break;
> +    }
> +  }
> +
> +  if (Index < gMmCoreMmst.NumberOfTableEntries) {
> +    //
> +    // A match was found, so this is either a modify or a delete operation
> +    //
> +    if (Table != NULL) {
> +      //
> +      // If Table is not NULL, then this is a modify operation.
> +      // Modify the table entry and return.
> +      //
> +      ConfigurationTable[Index].VendorTable = Table;
> +      return EFI_SUCCESS;
> +    }
> +
> +    //
> +    // A match was found and Table is NULL, so this is a delete operation.
> +    //
> +    gMmCoreMmst.NumberOfTableEntries--;
> +
> +    //
> +    // Copy over deleted entry
> +    //
> +    CopyMem (
> +      &(ConfigurationTable[Index]),
> +      &(ConfigurationTable[Index + 1]),
> +      (gMmCoreMmst.NumberOfTableEntries - Index) * sizeof (EFI_CONFIGURATION_TABLE)
> +      );
> +
> +  } else {
> +    //
> +    // No matching GUIDs were found, so this is an add operation.
> +    //
> +    if (Table == NULL) {
> +      //
> +      // If Table is NULL on an add operation, then return an error.
> +      //
> +      return EFI_NOT_FOUND;
> +    }
> +
> +    //
> +    // Assume that Index == gMmCoreMmst.NumberOfTableEntries
> +    //
> +    if ((Index * sizeof (EFI_CONFIGURATION_TABLE)) >= mMmSystemTableAllocateSize) {
> +      //
> +      // Allocate a table with one additional entry.
> +      //
> +      mMmSystemTableAllocateSize += (CONFIG_TABLE_SIZE_INCREASED * sizeof (EFI_CONFIGURATION_TABLE));
> +      ConfigurationTable = AllocatePool (mMmSystemTableAllocateSize);
> +      if (ConfigurationTable == NULL) {
> +        //
> +        // If a new table could not be allocated, then return an error.
> +        //
> +        return EFI_OUT_OF_RESOURCES;
> +      }
> +
> +      if (gMmCoreMmst.MmConfigurationTable != NULL) {
> +        //
> +        // Copy the old table to the new table.
> +        //
> +        CopyMem (
> +          ConfigurationTable,
> +          gMmCoreMmst.MmConfigurationTable,
> +          Index * sizeof (EFI_CONFIGURATION_TABLE)
> +          );
> +
> +        //
> +        // Record the old table pointer.
> +        //
> +        OldTable = gMmCoreMmst.MmConfigurationTable;
> +
> +        //
> +        // As the MmInstallConfigurationTable() may be re-entered by FreePool() in
> +        // its calling stack, updating System table to the new table pointer must
> +        // be done before calling FreePool() to free the old table.
> +        // It can make sure the gMmCoreMmst.MmConfigurationTable point to the new
> +        // table and avoid the errors of use-after-free to the old table by the
> +        // reenter of MmInstallConfigurationTable() in FreePool()'s calling stack.
> +        //
> +        gMmCoreMmst.MmConfigurationTable = ConfigurationTable;
> +
> +        //
> +        // Free the old table after updating System Table to the new table pointer.
> +        //
> +        FreePool (OldTable);
> +      } else {
> +        //
> +        // Update System Table
> +        //
> +        gMmCoreMmst.MmConfigurationTable = ConfigurationTable;
> +      }
> +    }
> +
> +    //
> +    // Fill in the new entry
> +    //
> +    CopyGuid ((VOID *)&ConfigurationTable[Index].VendorGuid, Guid);
> +    ConfigurationTable[Index].VendorTable = Table;
> +
> +    //
> +    // This is an add operation, so increment the number of table entries
> +    //
> +    gMmCoreMmst.NumberOfTableEntries++;
> +  }
> +
> +  //
> +  // CRC-32 field is ignorable for SMM System Table and should be set to zero
> +  //
> +
> +  return EFI_SUCCESS;
> +}
> diff --git a/StandaloneMmPkg/Core/Locate.c b/StandaloneMmPkg/Core/Locate.c
> new file mode 100644
> index 0000000000..6a90575f99
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Locate.c
> @@ -0,0 +1,496 @@
> +/** @file
> +  Locate handle functions
> +
> +  Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +//
> +// ProtocolRequest - Last LocateHandle request ID
> +//
> +UINTN mEfiLocateHandleRequest = 0;
> +
> +//
> +// Internal prototypes
> +//
> +
> +typedef struct {
> +  EFI_GUID        *Protocol;
> +  VOID            *SearchKey;
> +  LIST_ENTRY      *Position;
> +  PROTOCOL_ENTRY  *ProtEntry;
> +} LOCATE_POSITION;
> +
> +typedef
> +IHANDLE *
> +(* CORE_GET_NEXT) (
> +  IN OUT LOCATE_POSITION    *Position,
> +  OUT VOID                  **Interface
> +  );
> +
> +/**
> +  Routine to get the next Handle, when you are searching for all handles.
> +
> +  @param  Position               Information about which Handle to seach for.
> +  @param  Interface              Return the interface structure for the matching
> +                                 protocol.
> +
> +  @return An pointer to IHANDLE if the next Position is not the end of the list.
> +          Otherwise,NULL is returned.
> +
> +**/
> +IHANDLE *
> +MmGetNextLocateAllHandles (
> +  IN OUT LOCATE_POSITION  *Position,
> +  OUT    VOID             **Interface
> +  )
> +{
> +  IHANDLE     *Handle;
> +
> +  //
> +  // Next handle
> +  //
> +  Position->Position = Position->Position->ForwardLink;
> +
> +  //
> +  // If not at the end of the list, get the handle
> +  //
> +  Handle      = NULL;
> +  *Interface  = NULL;
> +  if (Position->Position != &gHandleList) {
> +    Handle = CR (Position->Position, IHANDLE, AllHandles, EFI_HANDLE_SIGNATURE);
> +  }
> +  return Handle;
> +}
> +
> +/**
> +  Routine to get the next Handle, when you are searching for register protocol
> +  notifies.
> +
> +  @param  Position               Information about which Handle to seach for.
> +  @param  Interface              Return the interface structure for the matching
> +                                 protocol.
> +
> +  @return An pointer to IHANDLE if the next Position is not the end of the list.
> +          Otherwise,NULL is returned.
> +
> +**/
> +IHANDLE *
> +MmGetNextLocateByRegisterNotify (
> +  IN OUT LOCATE_POSITION  *Position,
> +  OUT    VOID             **Interface
> +  )
> +{
> +  IHANDLE             *Handle;
> +  PROTOCOL_NOTIFY     *ProtNotify;
> +  PROTOCOL_INTERFACE  *Prot;
> +  LIST_ENTRY          *Link;
> +
> +  Handle      = NULL;
> +  *Interface  = NULL;
> +  ProtNotify = Position->SearchKey;
> +
> +  //
> +  // If this is the first request, get the next handle
> +  //
> +  if (ProtNotify != NULL) {
> +    ASSERT(ProtNotify->Signature == PROTOCOL_NOTIFY_SIGNATURE);
> +    Position->SearchKey = NULL;
> +
> +    //
> +    // If not at the end of the list, get the next handle
> +    //
> +    Link = ProtNotify->Position->ForwardLink;
> +    if (Link != &ProtNotify->Protocol->Protocols) {
> +      Prot = CR (Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_INTERFACE_SIGNATURE);
> +      Handle = Prot->Handle;
> +      *Interface = Prot->Interface;
> +    }
> +  }
> +  return Handle;
> +}
> +
> +/**
> +  Routine to get the next Handle, when you are searching for a given protocol.
> +
> +  @param  Position               Information about which Handle to seach for.
> +  @param  Interface              Return the interface structure for the matching
> +                                 protocol.
> +
> +  @return An pointer to IHANDLE if the next Position is not the end of the list.
> +          Otherwise,NULL is returned.
> +
> +**/
> +IHANDLE *
> +MmGetNextLocateByProtocol (
> +  IN OUT LOCATE_POSITION  *Position,
> +  OUT    VOID             **Interface
> +  )
> +{
> +  IHANDLE             *Handle;
> +  LIST_ENTRY          *Link;
> +  PROTOCOL_INTERFACE  *Prot;
> +
> +  Handle      = NULL;
> +  *Interface  = NULL;
> +  for (; ;) {
> +    //
> +    // Next entry
> +    //
> +    Link = Position->Position->ForwardLink;
> +    Position->Position = Link;
> +
> +    //
> +    // If not at the end, return the handle
> +    //
> +    if (Link == &Position->ProtEntry->Protocols) {
> +      Handle = NULL;
> +      break;
> +    }
> +
> +    //
> +    // Get the handle
> +    //
> +    Prot = CR(Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_INTERFACE_SIGNATURE);
> +    Handle = Prot->Handle;
> +    *Interface = Prot->Interface;
> +
> +    //
> +    // If this handle has not been returned this request, then
> +    // return it now
> +    //
> +    if (Handle->LocateRequest != mEfiLocateHandleRequest) {
> +      Handle->LocateRequest = mEfiLocateHandleRequest;
> +      break;
> +    }
> +  }
> +  return Handle;
> +}
> +
> +/**
> +  Return the first Protocol Interface that matches the Protocol GUID. If
> +  Registration is pasased in return a Protocol Instance that was just add
> +  to the system. If Retistration is NULL return the first Protocol Interface
> +  you find.
> +
> +  @param  Protocol               The protocol to search for
> +  @param  Registration           Optional Registration Key returned from
> +                                 RegisterProtocolNotify()
> +  @param  Interface              Return the Protocol interface (instance).
> +
> +  @retval EFI_SUCCESS            If a valid Interface is returned
> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> +  @retval EFI_NOT_FOUND          Protocol interface not found
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLocateProtocol (
> +  IN  EFI_GUID  *Protocol,
> +  IN  VOID      *Registration OPTIONAL,
> +  OUT VOID      **Interface
> +  )
> +{
> +  EFI_STATUS              Status;
> +  LOCATE_POSITION         Position;
> +  PROTOCOL_NOTIFY         *ProtNotify;
> +  IHANDLE                 *Handle;
> +
> +  if ((Interface == NULL) || (Protocol == NULL)) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  *Interface = NULL;
> +  Status = EFI_SUCCESS;
> +
> +  //
> +  // Set initial position
> +  //
> +  Position.Protocol  = Protocol;
> +  Position.SearchKey = Registration;
> +  Position.Position  = &gHandleList;
> +
> +  mEfiLocateHandleRequest += 1;
> +
> +  if (Registration == NULL) {
> +    //
> +    // Look up the protocol entry and set the head pointer
> +    //
> +    Position.ProtEntry = MmFindProtocolEntry (Protocol, FALSE);
> +    if (Position.ProtEntry == NULL) {
> +      return EFI_NOT_FOUND;
> +    }
> +    Position.Position = &Position.ProtEntry->Protocols;
> +
> +    Handle = MmGetNextLocateByProtocol (&Position, Interface);
> +  } else {
> +    Handle = MmGetNextLocateByRegisterNotify (&Position, Interface);
> +  }
> +
> +  if (Handle == NULL) {
> +    Status = EFI_NOT_FOUND;
> +  } else if (Registration != NULL) {
> +    //
> +    // If this is a search by register notify and a handle was
> +    // returned, update the register notification position
> +    //
> +    ProtNotify = Registration;
> +    ProtNotify->Position = ProtNotify->Position->ForwardLink;
> +  }
> +
> +  return Status;
> +}
> +
> +/**
> +  Locates the requested handle(s) and returns them in Buffer.
> +
> +  @param  SearchType             The type of search to perform to locate the
> +                                 handles
> +  @param  Protocol               The protocol to search for
> +  @param  SearchKey              Dependant on SearchType
> +  @param  BufferSize             On input the size of Buffer.  On output the
> +                                 size of data returned.
> +  @param  Buffer                 The buffer to return the results in
> +
> +  @retval EFI_BUFFER_TOO_SMALL   Buffer too small, required buffer size is
> +                                 returned in BufferSize.
> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> +  @retval EFI_SUCCESS            Successfully found the requested handle(s) and
> +                                 returns them in Buffer.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLocateHandle (
> +  IN     EFI_LOCATE_SEARCH_TYPE  SearchType,
> +  IN     EFI_GUID                *Protocol   OPTIONAL,
> +  IN     VOID                    *SearchKey  OPTIONAL,
> +  IN OUT UINTN                   *BufferSize,
> +  OUT    EFI_HANDLE              *Buffer
> +  )
> +{
> +  EFI_STATUS       Status;
> +  LOCATE_POSITION  Position;
> +  PROTOCOL_NOTIFY  *ProtNotify;
> +  CORE_GET_NEXT    GetNext;
> +  UINTN            ResultSize;
> +  IHANDLE          *Handle;
> +  IHANDLE          **ResultBuffer;
> +  VOID             *Interface;
> +
> +  if (BufferSize == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  if ((*BufferSize > 0) && (Buffer == NULL)) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  GetNext = NULL;
> +
> +  //
> +  // Set initial position
> +  //
> +  Position.Protocol  = Protocol;
> +  Position.SearchKey = SearchKey;
> +  Position.Position  = &gHandleList;
> +
> +  ResultSize = 0;
> +  ResultBuffer = (IHANDLE **) Buffer;
> +  Status = EFI_SUCCESS;
> +
> +  //
> +  // Get the search function based on type
> +  //
> +  switch (SearchType) {
> +  case AllHandles:
> +    GetNext = MmGetNextLocateAllHandles;
> +    break;
> +
> +  case ByRegisterNotify:
> +    GetNext = MmGetNextLocateByRegisterNotify;
> +    //
> +    // Must have SearchKey for locate ByRegisterNotify
> +    //
> +    if (SearchKey == NULL) {
> +      Status = EFI_INVALID_PARAMETER;
> +    }
> +    break;
> +
> +  case ByProtocol:
> +    GetNext = MmGetNextLocateByProtocol;
> +    if (Protocol == NULL) {
> +      Status = EFI_INVALID_PARAMETER;
> +      break;
> +    }
> +    //
> +    // Look up the protocol entry and set the head pointer
> +    //
> +    Position.ProtEntry = MmFindProtocolEntry (Protocol, FALSE);
> +    if (Position.ProtEntry == NULL) {
> +      Status = EFI_NOT_FOUND;
> +      break;
> +    }
> +    Position.Position = &Position.ProtEntry->Protocols;
> +    break;
> +
> +  default:
> +    Status = EFI_INVALID_PARAMETER;
> +    break;
> +  }
> +
> +  if (EFI_ERROR(Status)) {
> +    return Status;
> +  }
> +
> +  //
> +  // Enumerate out the matching handles
> +  //
> +  mEfiLocateHandleRequest += 1;
> +  for (; ;) {
> +    //
> +    // Get the next handle.  If no more handles, stop
> +    //
> +    Handle = GetNext (&Position, &Interface);
> +    if (NULL == Handle) {
> +      break;
> +    }
> +
> +    //
> +    // Increase the resulting buffer size, and if this handle
> +    // fits return it
> +    //
> +    ResultSize += sizeof(Handle);
> +    if (ResultSize <= *BufferSize) {
> +        *ResultBuffer = Handle;
> +        ResultBuffer += 1;
> +    }
> +  }
> +
> +  //
> +  // If the result is a zero length buffer, then there were no
> +  // matching handles
> +  //
> +  if (ResultSize == 0) {
> +    Status = EFI_NOT_FOUND;
> +  } else {
> +    //
> +    // Return the resulting buffer size.  If it's larger than what
> +    // was passed, then set the error code
> +    //
> +    if (ResultSize > *BufferSize) {
> +      Status = EFI_BUFFER_TOO_SMALL;
> +    }
> +
> +    *BufferSize = ResultSize;
> +
> +    if (SearchType == ByRegisterNotify && !EFI_ERROR(Status)) {
> +      ASSERT (SearchKey != NULL);
> +      //
> +      // If this is a search by register notify and a handle was
> +      // returned, update the register notification position
> +      //
> +      ProtNotify = SearchKey;
> +      ProtNotify->Position = ProtNotify->Position->ForwardLink;
> +    }
> +  }
> +
> +  return Status;
> +}
> +
> +/**
> +  Function returns an array of handles that support the requested protocol
> +  in a buffer allocated from pool. This is a version of MmLocateHandle()
> +  that allocates a buffer for the caller.
> +
> +  @param  SearchType             Specifies which handle(s) are to be returned.
> +  @param  Protocol               Provides the protocol to search by.    This
> +                                 parameter is only valid for SearchType
> +                                 ByProtocol.
> +  @param  SearchKey              Supplies the search key depending on the
> +                                 SearchType.
> +  @param  NumberHandles          The number of handles returned in Buffer.
> +  @param  Buffer                 A pointer to the buffer to return the requested
> +                                 array of  handles that support Protocol.
> +
> +  @retval EFI_SUCCESS            The result array of handles was returned.
> +  @retval EFI_NOT_FOUND          No handles match the search.
> +  @retval EFI_OUT_OF_RESOURCES   There is not enough pool memory to store the
> +                                 matching results.
> +  @retval EFI_INVALID_PARAMETER  One or more parameters are not valid.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLocateHandleBuffer (
> +  IN     EFI_LOCATE_SEARCH_TYPE  SearchType,
> +  IN     EFI_GUID                *Protocol OPTIONAL,
> +  IN     VOID                    *SearchKey OPTIONAL,
> +  IN OUT UINTN                   *NumberHandles,
> +  OUT    EFI_HANDLE              **Buffer
> +  )
> +{
> +  EFI_STATUS  Status;
> +  UINTN       BufferSize;
> +
> +  if (NumberHandles == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  if (Buffer == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  BufferSize = 0;
> +  *NumberHandles = 0;
> +  *Buffer = NULL;
> +  Status = MmLocateHandle (
> +             SearchType,
> +             Protocol,
> +             SearchKey,
> +             &BufferSize,
> +             *Buffer
> +             );
> +  //
> +  // LocateHandleBuffer() returns incorrect status code if SearchType is
> +  // invalid.
> +  //
> +  // Add code to correctly handle expected errors from MmLocateHandle().
> +  //
> +  if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {
> +    if (Status != EFI_INVALID_PARAMETER) {
> +      Status = EFI_NOT_FOUND;
> +    }
> +    return Status;
> +  }
> +
> +  *Buffer = AllocatePool (BufferSize);
> +  if (*Buffer == NULL) {
> +    return EFI_OUT_OF_RESOURCES;
> +  }
> +
> +  Status = MmLocateHandle (
> +             SearchType,
> +             Protocol,
> +             SearchKey,
> +             &BufferSize,
> +             *Buffer
> +             );
> +
> +  *NumberHandles = BufferSize / sizeof(EFI_HANDLE);
> +  if (EFI_ERROR(Status)) {
> +    *NumberHandles = 0;
> +  }
> +
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Core/Mmi.c b/StandaloneMmPkg/Core/Mmi.c
> new file mode 100644
> index 0000000000..29aba7b53d
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Mmi.c
> @@ -0,0 +1,337 @@
> +/** @file
> +  MMI management.
> +
> +  Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +//
> +// MM_HANDLER_STATE_NOTIFIER
> +//
> +
> +//
> +// MM_HANDLER - used for each MM handler
> +//
> +
> +#define MMI_ENTRY_SIGNATURE  SIGNATURE_32('m','m','i','e')
> +
> + typedef struct {
> +  UINTN       Signature;
> +  LIST_ENTRY  AllEntries;  // All entries
> +
> +  EFI_GUID    HandlerType; // Type of interrupt
> +  LIST_ENTRY  MmiHandlers; // All handlers
> +} MMI_ENTRY;
> +
> +#define MMI_HANDLER_SIGNATURE  SIGNATURE_32('m','m','i','h')
> +
> + typedef struct {
> +  UINTN                         Signature;
> +  LIST_ENTRY                    Link;        // Link on MMI_ENTRY.MmiHandlers
> +  EFI_MM_HANDLER_ENTRY_POINT    Handler;     // The mm handler's entry point
> +  MMI_ENTRY                     *MmiEntry;
> +} MMI_HANDLER;
> +
> +LIST_ENTRY  mRootMmiHandlerList = INITIALIZE_LIST_HEAD_VARIABLE (mRootMmiHandlerList);
> +LIST_ENTRY  mMmiEntryList       = INITIALIZE_LIST_HEAD_VARIABLE (mMmiEntryList);
> +
> +/**
> +  Finds the MMI entry for the requested handler type.
> +
> +  @param  HandlerType            The type of the interrupt
> +  @param  Create                 Create a new entry if not found
> +
> +  @return MMI entry
> +
> +**/
> +MMI_ENTRY  *
> +EFIAPI
> +MmCoreFindMmiEntry (
> +  IN EFI_GUID  *HandlerType,
> +  IN BOOLEAN   Create
> +  )
> +{
> +  LIST_ENTRY  *Link;
> +  MMI_ENTRY   *Item;
> +  MMI_ENTRY   *MmiEntry;
> +
> +  //
> +  // Search the MMI entry list for the matching GUID
> +  //
> +  MmiEntry = NULL;
> +  for (Link = mMmiEntryList.ForwardLink;
> +       Link != &mMmiEntryList;
> +       Link = Link->ForwardLink) {
> +
> +    Item = CR (Link, MMI_ENTRY, AllEntries, MMI_ENTRY_SIGNATURE);
> +    if (CompareGuid (&Item->HandlerType, HandlerType)) {
> +      //
> +      // This is the MMI entry
> +      //
> +      MmiEntry = Item;
> +      break;
> +    }
> +  }
> +
> +  //
> +  // If the protocol entry was not found and Create is TRUE, then
> +  // allocate a new entry
> +  //
> +  if ((MmiEntry == NULL) && Create) {
> +    MmiEntry = AllocatePool (sizeof(MMI_ENTRY));
> +    if (MmiEntry != NULL) {
> +      //
> +      // Initialize new MMI entry structure
> +      //
> +      MmiEntry->Signature = MMI_ENTRY_SIGNATURE;
> +      CopyGuid ((VOID *)&MmiEntry->HandlerType, HandlerType);
> +      InitializeListHead (&MmiEntry->MmiHandlers);
> +
> +      //
> +      // Add it to MMI entry list
> +      //
> +      InsertTailList (&mMmiEntryList, &MmiEntry->AllEntries);
> +    }
> +  }
> +  return MmiEntry;
> +}
> +
> +/**
> +  Manage MMI of a particular type.
> +
> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
> +  @param  Context        Points to an optional context buffer.
> +  @param  CommBuffer     Points to the optional communication buffer.
> +  @param  CommBufferSize Points to the size of the optional communication buffer.
> +
> +  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING  Interrupt source was processed successfully but not quiesced.
> +  @retval EFI_INTERRUPT_PENDING              One or more MMI sources could not be quiesced.
> +  @retval EFI_NOT_FOUND                      Interrupt source was not handled or quiesced.
> +  @retval EFI_SUCCESS                        Interrupt source was handled and quiesced.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmiManage (
> +  IN     CONST EFI_GUID  *HandlerType,
> +  IN     CONST VOID      *Context         OPTIONAL,
> +  IN OUT VOID            *CommBuffer      OPTIONAL,
> +  IN OUT UINTN           *CommBufferSize  OPTIONAL
> +  )
> +{
> +  LIST_ENTRY   *Link;
> +  LIST_ENTRY   *Head;
> +  MMI_ENTRY    *MmiEntry;
> +  MMI_HANDLER  *MmiHandler;
> +  BOOLEAN      SuccessReturn;
> +  EFI_STATUS   Status;
> +
> +  Status = EFI_NOT_FOUND;
> +  SuccessReturn = FALSE;
> +  if (HandlerType == NULL) {
> +    //
> +    // Root MMI handler
> +    //
> +
> +    Head = &mRootMmiHandlerList;
> +  } else {
> +    //
> +    // Non-root MMI handler
> +    //
> +    MmiEntry = MmCoreFindMmiEntry ((EFI_GUID *) HandlerType, FALSE);
> +    if (MmiEntry == NULL) {
> +      //
> +      // There is no handler registered for this interrupt source
> +      //
> +      return Status;
> +    }
> +
> +    Head = &MmiEntry->MmiHandlers;
> +  }
> +
> +  for (Link = Head->ForwardLink; Link != Head; Link = Link->ForwardLink) {
> +    MmiHandler = CR (Link, MMI_HANDLER, Link, MMI_HANDLER_SIGNATURE);
> +
> +    Status = MmiHandler->Handler (
> +               (EFI_HANDLE) MmiHandler,
> +               Context,
> +               CommBuffer,
> +               CommBufferSize
> +               );
> +
> +    switch (Status) {
> +    case EFI_INTERRUPT_PENDING:
> +      //
> +      // If a handler returns EFI_INTERRUPT_PENDING and HandlerType is not NULL then
> +      // no additional handlers will be processed and EFI_INTERRUPT_PENDING will be returned.
> +      //
> +      if (HandlerType != NULL) {
> +        return EFI_INTERRUPT_PENDING;
> +      }
> +      break;
> +
> +    case EFI_SUCCESS:
> +      //
> +      // If at least one of the handlers returns EFI_SUCCESS then the function will return
> +      // EFI_SUCCESS. If a handler returns EFI_SUCCESS and HandlerType is not NULL then no
> +      // additional handlers will be processed.
> +      //
> +      if (HandlerType != NULL) {
> +        return EFI_SUCCESS;
> +      }
> +      SuccessReturn = TRUE;
> +      break;
> +
> +    case EFI_WARN_INTERRUPT_SOURCE_QUIESCED:
> +      //
> +      // If at least one of the handlers returns EFI_WARN_INTERRUPT_SOURCE_QUIESCED
> +      // then the function will return EFI_SUCCESS.
> +      //
> +      SuccessReturn = TRUE;
> +      break;
> +
> +    case EFI_WARN_INTERRUPT_SOURCE_PENDING:
> +      //
> +      // If all the handlers returned EFI_WARN_INTERRUPT_SOURCE_PENDING
> +      // then EFI_WARN_INTERRUPT_SOURCE_PENDING will be returned.
> +      //
> +      break;
> +
> +    default:
> +      //
> +      // Unexpected status code returned.
> +      //
> +      ASSERT (FALSE);
> +      break;
> +    }
> +  }
> +
> +  if (SuccessReturn) {
> +    Status = EFI_SUCCESS;
> +  }
> +
> +  return Status;
> +}
> +
> +/**
> +  Registers a handler to execute within MM.
> +
> +  @param  Handler        Handler service funtion pointer.
> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
> +  @param  DispatchHandle On return, contains a unique handle which can be used to later unregister the handler function.
> +
> +  @retval EFI_SUCCESS           Handler register success.
> +  @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmiHandlerRegister (
> +  IN  EFI_MM_HANDLER_ENTRY_POINT    Handler,
> +  IN  CONST EFI_GUID                *HandlerType  OPTIONAL,
> +  OUT EFI_HANDLE                    *DispatchHandle
> +  )
> +{
> +  MMI_HANDLER  *MmiHandler;
> +  MMI_ENTRY    *MmiEntry;
> +  LIST_ENTRY   *List;
> +
> +  if (Handler == NULL || DispatchHandle == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  MmiHandler = AllocateZeroPool (sizeof (MMI_HANDLER));
> +  if (MmiHandler == NULL) {
> +    return EFI_OUT_OF_RESOURCES;
> +  }
> +
> +  MmiHandler->Signature = MMI_HANDLER_SIGNATURE;
> +  MmiHandler->Handler = Handler;
> +
> +  if (HandlerType == NULL) {
> +    //
> +    // This is root MMI handler
> +    //
> +    MmiEntry = NULL;
> +    List = &mRootMmiHandlerList;
> +  } else {
> +    //
> +    // None root MMI handler
> +    //
> +    MmiEntry = MmCoreFindMmiEntry ((EFI_GUID *) HandlerType, TRUE);
> +    if (MmiEntry == NULL) {
> +      return EFI_OUT_OF_RESOURCES;
> +    }
> +
> +    List = &MmiEntry->MmiHandlers;
> +  }
> +
> +  MmiHandler->MmiEntry = MmiEntry;
> +  InsertTailList (List, &MmiHandler->Link);
> +
> +  *DispatchHandle = (EFI_HANDLE) MmiHandler;
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Unregister a handler in MM.
> +
> +  @param  DispatchHandle  The handle that was specified when the handler was registered.
> +
> +  @retval EFI_SUCCESS           Handler function was successfully unregistered.
> +  @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid handle.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmiHandlerUnRegister (
> +  IN EFI_HANDLE  DispatchHandle
> +  )
> +{
> +  MMI_HANDLER  *MmiHandler;
> +  MMI_ENTRY    *MmiEntry;
> +
> +  MmiHandler = (MMI_HANDLER *) DispatchHandle;
> +
> +  if (MmiHandler == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  if (MmiHandler->Signature != MMI_HANDLER_SIGNATURE) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  MmiEntry = MmiHandler->MmiEntry;
> +
> +  RemoveEntryList (&MmiHandler->Link);
> +  FreePool (MmiHandler);
> +
> +  if (MmiEntry == NULL) {
> +    //
> +    // This is root MMI handler
> +    //
> +    return EFI_SUCCESS;
> +  }
> +
> +  if (IsListEmpty (&MmiEntry->MmiHandlers)) {
> +    //
> +    // No handler registered for this interrupt now, remove the MMI_ENTRY
> +    //
> +    RemoveEntryList (&MmiEntry->AllEntries);
> +
> +    FreePool (MmiEntry);
> +  }
> +
> +  return EFI_SUCCESS;
> +}
> diff --git a/StandaloneMmPkg/Core/Notify.c b/StandaloneMmPkg/Core/Notify.c
> new file mode 100644
> index 0000000000..d5fc8f50d1
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Notify.c
> @@ -0,0 +1,203 @@
> +/** @file
> +  Support functions for UEFI protocol notification infrastructure.
> +
> +  Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +/**
> +  Signal event for every protocol in protocol entry.
> +
> +  @param  Prot                   Protocol interface
> +
> +**/
> +VOID
> +MmNotifyProtocol (
> +  IN PROTOCOL_INTERFACE  *Prot
> +  )
> +{
> +  PROTOCOL_ENTRY   *ProtEntry;
> +  PROTOCOL_NOTIFY  *ProtNotify;
> +  LIST_ENTRY       *Link;
> +
> +  ProtEntry = Prot->Protocol;
> +  for (Link=ProtEntry->Notify.ForwardLink; Link != &ProtEntry->Notify; Link=Link->ForwardLink) {
> +    ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
> +    ProtNotify->Function (&ProtEntry->ProtocolID, Prot->Interface, Prot->Handle);
> +  }
> +}
> +
> +/**
> +  Removes Protocol from the protocol list (but not the handle list).
> +
> +  @param  Handle                 The handle to remove protocol on.
> +  @param  Protocol               GUID of the protocol to be moved
> +  @param  Interface              The interface of the protocol
> +
> +  @return Protocol Entry
> +
> +**/
> +PROTOCOL_INTERFACE *
> +MmRemoveInterfaceFromProtocol (
> +  IN IHANDLE   *Handle,
> +  IN EFI_GUID  *Protocol,
> +  IN VOID      *Interface
> +  )
> +{
> +  PROTOCOL_INTERFACE  *Prot;
> +  PROTOCOL_NOTIFY     *ProtNotify;
> +  PROTOCOL_ENTRY      *ProtEntry;
> +  LIST_ENTRY          *Link;
> +
> +  Prot = MmFindProtocolInterface (Handle, Protocol, Interface);
> +  if (Prot != NULL) {
> +
> +    ProtEntry = Prot->Protocol;
> +
> +    //
> +    // If there's a protocol notify location pointing to this entry, back it up one
> +    //
> +    for(Link = ProtEntry->Notify.ForwardLink; Link != &ProtEntry->Notify; Link=Link->ForwardLink) {
> +      ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
> +
> +      if (ProtNotify->Position == &Prot->ByProtocol) {
> +        ProtNotify->Position = Prot->ByProtocol.BackLink;
> +      }
> +    }
> +
> +    //
> +    // Remove the protocol interface entry
> +    //
> +    RemoveEntryList (&Prot->ByProtocol);
> +  }
> +
> +  return Prot;
> +}
> +
> +/**
> +  Add a new protocol notification record for the request protocol.
> +
> +  @param  Protocol               The requested protocol to add the notify
> +                                 registration
> +  @param  Function               Points to the notification function
> +  @param  Registration           Returns the registration record
> +
> +  @retval EFI_SUCCESS            Successfully returned the registration record
> +                                 that has been added or unhooked
> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL or Registration is NULL
> +  @retval EFI_OUT_OF_RESOURCES   Not enough memory resource to finish the request
> +  @retval EFI_NOT_FOUND          If the registration is not found when Function == NULL
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmRegisterProtocolNotify (
> +  IN  CONST EFI_GUID     *Protocol,
> +  IN  EFI_MM_NOTIFY_FN  Function,
> +  OUT VOID               **Registration
> +  )
> +{
> +  PROTOCOL_ENTRY   *ProtEntry;
> +  PROTOCOL_NOTIFY  *ProtNotify;
> +  LIST_ENTRY       *Link;
> +  EFI_STATUS       Status;
> +
> +  if (Protocol == NULL || Registration == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  if (Function == NULL) {
> +    //
> +    // Get the protocol entry per Protocol
> +    //
> +    ProtEntry = MmFindProtocolEntry ((EFI_GUID *) Protocol, FALSE);
> +    if (ProtEntry != NULL) {
> +      ProtNotify = (PROTOCOL_NOTIFY * )*Registration;
> +      for (Link = ProtEntry->Notify.ForwardLink;
> +           Link != &ProtEntry->Notify;
> +           Link = Link->ForwardLink) {
> +        //
> +        // Compare the notification record
> +        //
> +        if (ProtNotify == (CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE))){
> +          //
> +          // If Registration is an existing registration, then unhook it
> +          //
> +          ProtNotify->Signature = 0;
> +          RemoveEntryList (&ProtNotify->Link);
> +          FreePool (ProtNotify);
> +          return EFI_SUCCESS;
> +        }
> +      }
> +    }
> +    //
> +    // If the registration is not found
> +    //
> +    return EFI_NOT_FOUND;
> +  }
> +
> +  ProtNotify = NULL;
> +
> +  //
> +  // Get the protocol entry to add the notification too
> +  //
> +  ProtEntry = MmFindProtocolEntry ((EFI_GUID *) Protocol, TRUE);
> +  if (ProtEntry != NULL) {
> +    //
> +    // Find whether notification already exist
> +    //
> +    for (Link = ProtEntry->Notify.ForwardLink;
> +         Link != &ProtEntry->Notify;
> +         Link = Link->ForwardLink) {
> +
> +      ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
> +      if (CompareGuid (&ProtNotify->Protocol->ProtocolID, Protocol) &&
> +          (ProtNotify->Function == Function)) {
> +
> +        //
> +        // Notification already exist
> +        //
> +        *Registration = ProtNotify;
> +
> +        return EFI_SUCCESS;
> +      }
> +    }
> +
> +    //
> +    // Allocate a new notification record
> +    //
> +    ProtNotify = AllocatePool (sizeof(PROTOCOL_NOTIFY));
> +    if (ProtNotify != NULL) {
> +      ProtNotify->Signature = PROTOCOL_NOTIFY_SIGNATURE;
> +      ProtNotify->Protocol = ProtEntry;
> +      ProtNotify->Function = Function;
> +      //
> +      // Start at the ending
> +      //
> +      ProtNotify->Position = ProtEntry->Protocols.BackLink;
> +
> +      InsertTailList (&ProtEntry->Notify, &ProtNotify->Link);
> +    }
> +  }
> +
> +  //
> +  // Done.  If we have a protocol notify entry, then return it.
> +  // Otherwise, we must have run out of resources trying to add one
> +  //
> +  Status = EFI_OUT_OF_RESOURCES;
> +  if (ProtNotify != NULL) {
> +    *Registration = ProtNotify;
> +    Status = EFI_SUCCESS;
> +  }
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Core/Page.c b/StandaloneMmPkg/Core/Page.c
> new file mode 100644
> index 0000000000..ba3e7cea74
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Page.c
> @@ -0,0 +1,384 @@
> +/** @file
> +  MM Memory page management functions.
> +
> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +#define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \
> +  ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size)))
> +
> +#define TRUNCATE_TO_PAGES(a)  ((a) >> EFI_PAGE_SHIFT)
> +
> +LIST_ENTRY  mMmMemoryMap = INITIALIZE_LIST_HEAD_VARIABLE (mMmMemoryMap);
> +
> +UINTN mMapKey;
> +
> +/**
> +  Internal Function. Allocate n pages from given free page node.
> +
> +  @param  Pages                  The free page node.
> +  @param  NumberOfPages          Number of pages to be allocated.
> +  @param  MaxAddress             Request to allocate memory below this address.
> +
> +  @return Memory address of allocated pages.
> +
> +**/
> +UINTN
> +InternalAllocPagesOnOneNode (
> +  IN OUT FREE_PAGE_LIST  *Pages,
> +  IN     UINTN           NumberOfPages,
> +  IN     UINTN           MaxAddress
> +  )
> +{
> +  UINTN           Top;
> +  UINTN           Bottom;
> +  FREE_PAGE_LIST  *Node;
> +
> +  Top = TRUNCATE_TO_PAGES (MaxAddress + 1 - (UINTN)Pages);
> +  if (Top > Pages->NumberOfPages) {
> +    Top = Pages->NumberOfPages;
> +  }
> +  Bottom = Top - NumberOfPages;
> +
> +  if (Top < Pages->NumberOfPages) {
> +    Node = (FREE_PAGE_LIST*)((UINTN)Pages + EFI_PAGES_TO_SIZE (Top));
> +    Node->NumberOfPages = Pages->NumberOfPages - Top;
> +    InsertHeadList (&Pages->Link, &Node->Link);
> +  }
> +
> +  if (Bottom > 0) {
> +    Pages->NumberOfPages = Bottom;
> +  } else {
> +    RemoveEntryList (&Pages->Link);
> +  }
> +
> +  return (UINTN)Pages + EFI_PAGES_TO_SIZE (Bottom);
> +}
> +
> +/**
> +  Internal Function. Allocate n pages from free page list below MaxAddress.
> +
> +  @param  FreePageList           The free page node.
> +  @param  NumberOfPages          Number of pages to be allocated.
> +  @param  MaxAddress             Request to allocate memory below this address.
> +
> +  @return Memory address of allocated pages.
> +
> +**/
> +UINTN
> +InternalAllocMaxAddress (
> +  IN OUT LIST_ENTRY  *FreePageList,
> +  IN     UINTN       NumberOfPages,
> +  IN     UINTN       MaxAddress
> +  )
> +{
> +  LIST_ENTRY      *Node;
> +  FREE_PAGE_LIST  *Pages;
> +
> +  for (Node = FreePageList->BackLink; Node != FreePageList; Node = Node->BackLink) {
> +    Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
> +    if (Pages->NumberOfPages >= NumberOfPages &&
> +        (UINTN)Pages + EFI_PAGES_TO_SIZE (NumberOfPages) - 1 <= MaxAddress) {
> +      return InternalAllocPagesOnOneNode (Pages, NumberOfPages, MaxAddress);
> +    }
> +  }
> +  return (UINTN)(-1);
> +}
> +
> +/**
> +  Internal Function. Allocate n pages from free page list at given address.
> +
> +  @param  FreePageList           The free page node.
> +  @param  NumberOfPages          Number of pages to be allocated.
> +  @param  MaxAddress             Request to allocate memory below this address.
> +
> +  @return Memory address of allocated pages.
> +
> +**/
> +UINTN
> +InternalAllocAddress (
> +  IN OUT LIST_ENTRY  *FreePageList,
> +  IN     UINTN       NumberOfPages,
> +  IN     UINTN       Address
> +  )
> +{
> +  UINTN           EndAddress;
> +  LIST_ENTRY      *Node;
> +  FREE_PAGE_LIST  *Pages;
> +
> +  if ((Address & EFI_PAGE_MASK) != 0) {
> +    return ~Address;
> +  }
> +
> +  EndAddress = Address + EFI_PAGES_TO_SIZE (NumberOfPages);
> +  for (Node = FreePageList->BackLink; Node!= FreePageList; Node = Node->BackLink) {
> +    Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
> +    if ((UINTN)Pages <= Address) {
> +      if ((UINTN)Pages + EFI_PAGES_TO_SIZE (Pages->NumberOfPages) < EndAddress) {
> +        break;
> +      }
> +      return InternalAllocPagesOnOneNode (Pages, NumberOfPages, EndAddress);
> +    }
> +  }
> +  return ~Address;
> +}
> +
> +/**
> +  Allocates pages from the memory map.
> +
> +  @param  Type                   The type of allocation to perform.
> +  @param  MemoryType             The type of memory to turn the allocated pages
> +                                 into.
> +  @param  NumberOfPages          The number of pages to allocate.
> +  @param  Memory                 A pointer to receive the base allocated memory
> +                                 address.
> +
> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
> +  @retval EFI_SUCCESS            Pages successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalAllocatePages (
> +  IN  EFI_ALLOCATE_TYPE     Type,
> +  IN  EFI_MEMORY_TYPE       MemoryType,
> +  IN  UINTN                 NumberOfPages,
> +  OUT EFI_PHYSICAL_ADDRESS  *Memory
> +  )
> +{
> +  UINTN  RequestedAddress;
> +
> +  if (MemoryType != EfiRuntimeServicesCode &&
> +      MemoryType != EfiRuntimeServicesData) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  if (NumberOfPages > TRUNCATE_TO_PAGES ((UINTN)-1) + 1) {
> +    return EFI_OUT_OF_RESOURCES;
> +  }
> +
> +  //
> +  // We don't track memory type in MM
> +  //
> +  RequestedAddress = (UINTN)*Memory;
> +  switch (Type) {
> +    case AllocateAnyPages:
> +      RequestedAddress = (UINTN)(-1);
> +    case AllocateMaxAddress:
> +      *Memory = InternalAllocMaxAddress (
> +                  &mMmMemoryMap,
> +                  NumberOfPages,
> +                  RequestedAddress
> +                  );
> +      if (*Memory == (UINTN)-1) {
> +        return EFI_OUT_OF_RESOURCES;
> +      }
> +      break;
> +    case AllocateAddress:
> +      *Memory = InternalAllocAddress (
> +                  &mMmMemoryMap,
> +                  NumberOfPages,
> +                  RequestedAddress
> +                  );
> +      if (*Memory != RequestedAddress) {
> +        return EFI_NOT_FOUND;
> +      }
> +      break;
> +    default:
> +      return EFI_INVALID_PARAMETER;
> +  }
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Allocates pages from the memory map.
> +
> +  @param  Type                   The type of allocation to perform.
> +  @param  MemoryType             The type of memory to turn the allocated pages
> +                                 into.
> +  @param  NumberOfPages          The number of pages to allocate.
> +  @param  Memory                 A pointer to receive the base allocated memory
> +                                 address.
> +
> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
> +  @retval EFI_SUCCESS            Pages successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmAllocatePages (
> +  IN  EFI_ALLOCATE_TYPE     Type,
> +  IN  EFI_MEMORY_TYPE       MemoryType,
> +  IN  UINTN                 NumberOfPages,
> +  OUT EFI_PHYSICAL_ADDRESS  *Memory
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  Status = MmInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory);
> +  return Status;
> +}
> +
> +/**
> +  Internal Function. Merge two adjacent nodes.
> +
> +  @param  First             The first of two nodes to merge.
> +
> +  @return Pointer to node after merge (if success) or pointer to next node (if fail).
> +
> +**/
> +FREE_PAGE_LIST *
> +InternalMergeNodes (
> +  IN FREE_PAGE_LIST  *First
> +  )
> +{
> +  FREE_PAGE_LIST  *Next;
> +
> +  Next = BASE_CR (First->Link.ForwardLink, FREE_PAGE_LIST, Link);
> +  ASSERT (
> +    TRUNCATE_TO_PAGES ((UINTN)Next - (UINTN)First) >= First->NumberOfPages);
> +
> +  if (TRUNCATE_TO_PAGES ((UINTN)Next - (UINTN)First) == First->NumberOfPages) {
> +    First->NumberOfPages += Next->NumberOfPages;
> +    RemoveEntryList (&Next->Link);
> +    Next = First;
> +  }
> +  return Next;
> +}
> +
> +/**
> +  Frees previous allocated pages.
> +
> +  @param  Memory                 Base address of memory being freed.
> +  @param  NumberOfPages          The number of pages to free.
> +
> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range.
> +  @retval EFI_INVALID_PARAMETER  Address not aligned.
> +  @return EFI_SUCCESS            Pages successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalFreePages (
> +  IN EFI_PHYSICAL_ADDRESS  Memory,
> +  IN UINTN                 NumberOfPages
> +  )
> +{
> +  LIST_ENTRY      *Node;
> +  FREE_PAGE_LIST  *Pages;
> +
> +  if ((Memory & EFI_PAGE_MASK) != 0) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  Pages = NULL;
> +  Node = mMmMemoryMap.ForwardLink;
> +  while (Node != &mMmMemoryMap) {
> +    Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
> +    if (Memory < (UINTN)Pages) {
> +      break;
> +    }
> +    Node = Node->ForwardLink;
> +  }
> +
> +  if (Node != &mMmMemoryMap &&
> +      Memory + EFI_PAGES_TO_SIZE (NumberOfPages) > (UINTN)Pages) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  if (Node->BackLink != &mMmMemoryMap) {
> +    Pages = BASE_CR (Node->BackLink, FREE_PAGE_LIST, Link);
> +    if ((UINTN)Pages + EFI_PAGES_TO_SIZE (Pages->NumberOfPages) > Memory) {
> +      return EFI_INVALID_PARAMETER;
> +    }
> +  }
> +
> +  Pages = (FREE_PAGE_LIST*)(UINTN)Memory;
> +  Pages->NumberOfPages = NumberOfPages;
> +  InsertTailList (Node, &Pages->Link);
> +
> +  if (Pages->Link.BackLink != &mMmMemoryMap) {
> +    Pages = InternalMergeNodes (
> +              BASE_CR (Pages->Link.BackLink, FREE_PAGE_LIST, Link)
> +              );
> +  }
> +
> +  if (Node != &mMmMemoryMap) {
> +    InternalMergeNodes (Pages);
> +  }
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Frees previous allocated pages.
> +
> +  @param  Memory                 Base address of memory being freed.
> +  @param  NumberOfPages          The number of pages to free.
> +
> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range.
> +  @retval EFI_INVALID_PARAMETER  Address not aligned.
> +  @return EFI_SUCCESS            Pages successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmFreePages (
> +  IN EFI_PHYSICAL_ADDRESS  Memory,
> +  IN UINTN                 NumberOfPages
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  Status = MmInternalFreePages (Memory, NumberOfPages);
> +  return Status;
> +}
> +
> +/**
> +  Add free MMRAM region for use by memory service.
> +
> +  @param  MemBase                Base address of memory region.
> +  @param  MemLength              Length of the memory region.
> +  @param  Type                   Memory type.
> +  @param  Attributes             Memory region state.
> +
> +**/
> +VOID
> +MmAddMemoryRegion (
> +  IN  EFI_PHYSICAL_ADDRESS  MemBase,
> +  IN  UINT64                MemLength,
> +  IN  EFI_MEMORY_TYPE       Type,
> +  IN  UINT64                Attributes
> +  )
> +{
> +  UINTN  AlignedMemBase;
> +
> +  //
> +  // Do not add memory regions that is already allocated, needs testing, or needs ECC initialization
> +  //
> +  if ((Attributes & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {
> +    return;
> +  }
> +
> +  //
> +  // Align range on an EFI_PAGE_SIZE boundary
> +  //
> +  AlignedMemBase = (UINTN)(MemBase + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
> +  MemLength -= AlignedMemBase - MemBase;
> +  MmFreePages (AlignedMemBase, TRUNCATE_TO_PAGES ((UINTN)MemLength));
> +}
> diff --git a/StandaloneMmPkg/Core/Pool.c b/StandaloneMmPkg/Core/Pool.c
> new file mode 100644
> index 0000000000..bdf1258381
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Pool.c
> @@ -0,0 +1,287 @@
> +/** @file
> +  SMM Memory pool management functions.
> +
> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +LIST_ENTRY  mMmPoolLists[MAX_POOL_INDEX];
> +//
> +// To cache the MMRAM base since when Loading modules At fixed address feature is enabled,
> +// all module is assigned an offset relative the MMRAM base in build time.
> +//
> +GLOBAL_REMOVE_IF_UNREFERENCED  EFI_PHYSICAL_ADDRESS       gLoadModuleAtFixAddressMmramBase = 0;
> +
> +/**
> +  Called to initialize the memory service.
> +
> +  @param   MmramRangeCount       Number of MMRAM Regions
> +  @param   MmramRanges           Pointer to MMRAM Descriptors
> +
> +**/
> +VOID
> +MmInitializeMemoryServices (
> +  IN UINTN                 MmramRangeCount,
> +  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
> +  )
> +{
> +  UINTN                  Index;
> +
> +  //
> +  // Initialize Pool list
> +  //
> +  for (Index = sizeof (mMmPoolLists) / sizeof (*mMmPoolLists); Index > 0;) {
> +    InitializeListHead (&mMmPoolLists[--Index]);
> +  }
> +
> +
> +  //
> +  // Initialize free MMRAM regions
> +  //
> +  for (Index = 0; Index < MmramRangeCount; Index++) {
> +    //
> +    // BUGBUG: Add legacy MMRAM region is buggy.
> +    //
> +    if (MmramRanges[Index].CpuStart < BASE_1MB) {
> +      continue;
> +    }
> +    DEBUG ((DEBUG_INFO, "MmAddMemoryRegion %d : 0x%016lx - 0x%016lx\n", Index, MmramRanges[Index].CpuStart, MmramRanges[Index].PhysicalSize));
> +    MmAddMemoryRegion (
> +      MmramRanges[Index].CpuStart,
> +      MmramRanges[Index].PhysicalSize,
> +      EfiConventionalMemory,
> +      MmramRanges[Index].RegionState
> +      );
> +  }
> +
> +}
> +
> +/**
> +  Internal Function. Allocate a pool by specified PoolIndex.
> +
> +  @param  PoolIndex             Index which indicate the Pool size.
> +  @param  FreePoolHdr           The returned Free pool.
> +
> +  @retval EFI_OUT_OF_RESOURCES   Allocation failed.
> +  @retval EFI_SUCCESS            Pool successfully allocated.
> +
> +**/
> +EFI_STATUS
> +InternalAllocPoolByIndex (
> +  IN  UINTN             PoolIndex,
> +  OUT FREE_POOL_HEADER  **FreePoolHdr
> +  )
> +{
> +  EFI_STATUS            Status;
> +  FREE_POOL_HEADER      *Hdr;
> +  EFI_PHYSICAL_ADDRESS  Address;
> +
> +  ASSERT (PoolIndex <= MAX_POOL_INDEX);
> +  Status = EFI_SUCCESS;
> +  Hdr = NULL;
> +  if (PoolIndex == MAX_POOL_INDEX) {
> +    Status = MmInternalAllocatePages (AllocateAnyPages, EfiRuntimeServicesData, EFI_SIZE_TO_PAGES (MAX_POOL_SIZE << 1), &Address);
> +    if (EFI_ERROR (Status)) {
> +      return EFI_OUT_OF_RESOURCES;
> +    }
> +    Hdr = (FREE_POOL_HEADER *) (UINTN) Address;
> +  } else if (!IsListEmpty (&mMmPoolLists[PoolIndex])) {
> +    Hdr = BASE_CR (GetFirstNode (&mMmPoolLists[PoolIndex]), FREE_POOL_HEADER, Link);
> +    RemoveEntryList (&Hdr->Link);
> +  } else {
> +    Status = InternalAllocPoolByIndex (PoolIndex + 1, &Hdr);
> +    if (!EFI_ERROR (Status)) {
> +      Hdr->Header.Size >>= 1;
> +      Hdr->Header.Available = TRUE;
> +      InsertHeadList (&mMmPoolLists[PoolIndex], &Hdr->Link);
> +      Hdr = (FREE_POOL_HEADER*)((UINT8*)Hdr + Hdr->Header.Size);
> +    }
> +  }
> +
> +  if (!EFI_ERROR (Status)) {
> +    Hdr->Header.Size = MIN_POOL_SIZE << PoolIndex;
> +    Hdr->Header.Available = FALSE;
> +  }
> +
> +  *FreePoolHdr = Hdr;
> +  return Status;
> +}
> +
> +/**
> +  Internal Function. Free a pool by specified PoolIndex.
> +
> +  @param  FreePoolHdr           The pool to free.
> +
> +  @retval EFI_SUCCESS           Pool successfully freed.
> +
> +**/
> +EFI_STATUS
> +InternalFreePoolByIndex (
> +  IN FREE_POOL_HEADER  *FreePoolHdr
> +  )
> +{
> +  UINTN  PoolIndex;
> +
> +  ASSERT ((FreePoolHdr->Header.Size & (FreePoolHdr->Header.Size - 1)) == 0);
> +  ASSERT (((UINTN)FreePoolHdr & (FreePoolHdr->Header.Size - 1)) == 0);
> +  ASSERT (FreePoolHdr->Header.Size >= MIN_POOL_SIZE);
> +
> +  PoolIndex = (UINTN) (HighBitSet32 ((UINT32)FreePoolHdr->Header.Size) - MIN_POOL_SHIFT);
> +  FreePoolHdr->Header.Available = TRUE;
> +  ASSERT (PoolIndex < MAX_POOL_INDEX);
> +  InsertHeadList (&mMmPoolLists[PoolIndex], &FreePoolHdr->Link);
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Allocate pool of a particular type.
> +
> +  @param  PoolType               Type of pool to allocate.
> +  @param  Size                   The amount of pool to allocate.
> +  @param  Buffer                 The address to return a pointer to the allocated
> +                                 pool.
> +
> +  @retval EFI_INVALID_PARAMETER  PoolType not valid.
> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
> +  @retval EFI_SUCCESS            Pool successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalAllocatePool (
> +  IN   EFI_MEMORY_TYPE  PoolType,
> +  IN   UINTN            Size,
> +  OUT  VOID             **Buffer
> +  )
> +{
> +  POOL_HEADER           *PoolHdr;
> +  FREE_POOL_HEADER      *FreePoolHdr;
> +  EFI_STATUS            Status;
> +  EFI_PHYSICAL_ADDRESS  Address;
> +  UINTN                 PoolIndex;
> +
> +  if (PoolType != EfiRuntimeServicesCode &&
> +      PoolType != EfiRuntimeServicesData) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  Size += sizeof (*PoolHdr);
> +  if (Size > MAX_POOL_SIZE) {
> +    Size = EFI_SIZE_TO_PAGES (Size);
> +    Status = MmInternalAllocatePages (AllocateAnyPages, PoolType, Size, &Address);
> +    if (EFI_ERROR (Status)) {
> +      return Status;
> +    }
> +
> +    PoolHdr = (POOL_HEADER*)(UINTN)Address;
> +    PoolHdr->Size = EFI_PAGES_TO_SIZE (Size);
> +    PoolHdr->Available = FALSE;
> +    *Buffer = PoolHdr + 1;
> +    return Status;
> +  }
> +
> +  Size = (Size + MIN_POOL_SIZE - 1) >> MIN_POOL_SHIFT;
> +  PoolIndex = (UINTN) HighBitSet32 ((UINT32)Size);
> +  if ((Size & (Size - 1)) != 0) {
> +    PoolIndex++;
> +  }
> +
> +  Status = InternalAllocPoolByIndex (PoolIndex, &FreePoolHdr);
> +  if (!EFI_ERROR(Status)) {
> +    *Buffer = &FreePoolHdr->Header + 1;
> +  }
> +  return Status;
> +}
> +
> +/**
> +  Allocate pool of a particular type.
> +
> +  @param  PoolType               Type of pool to allocate.
> +  @param  Size                   The amount of pool to allocate.
> +  @param  Buffer                 The address to return a pointer to the allocated
> +                                 pool.
> +
> +  @retval EFI_INVALID_PARAMETER  PoolType not valid.
> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
> +  @retval EFI_SUCCESS            Pool successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmAllocatePool (
> +  IN   EFI_MEMORY_TYPE  PoolType,
> +  IN   UINTN            Size,
> +  OUT  VOID             **Buffer
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  Status = MmInternalAllocatePool (PoolType, Size, Buffer);
> +  return Status;
> +}
> +
> +/**
> +  Frees pool.
> +
> +  @param  Buffer                 The allocated pool entry to free.
> +
> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
> +  @retval EFI_SUCCESS            Pool successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalFreePool (
> +  IN VOID  *Buffer
> +  )
> +{
> +  FREE_POOL_HEADER  *FreePoolHdr;
> +
> +  if (Buffer == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  FreePoolHdr = (FREE_POOL_HEADER*)((POOL_HEADER*)Buffer - 1);
> +  ASSERT (!FreePoolHdr->Header.Available);
> +
> +  if (FreePoolHdr->Header.Size > MAX_POOL_SIZE) {
> +    ASSERT (((UINTN)FreePoolHdr & EFI_PAGE_MASK) == 0);
> +    ASSERT ((FreePoolHdr->Header.Size & EFI_PAGE_MASK) == 0);
> +    return MmInternalFreePages (
> +             (EFI_PHYSICAL_ADDRESS)(UINTN)FreePoolHdr,
> +             EFI_SIZE_TO_PAGES (FreePoolHdr->Header.Size)
> +             );
> +  }
> +  return InternalFreePoolByIndex (FreePoolHdr);
> +}
> +
> +/**
> +  Frees pool.
> +
> +  @param  Buffer                 The allocated pool entry to free.
> +
> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
> +  @retval EFI_SUCCESS            Pool successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmFreePool (
> +  IN VOID  *Buffer
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  Status = MmInternalFreePool (Buffer);
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.c b/StandaloneMmPkg/Core/StandaloneMmCore.c
> new file mode 100644
> index 0000000000..0bb99b9710
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/StandaloneMmCore.c
> @@ -0,0 +1,708 @@
> +/** @file
> +  MM Core Main Entry Point
> +
> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +EFI_STATUS
> +MmCoreFfsFindMmDriver (
> +  IN  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader
> +  );
> +
> +EFI_STATUS
> +MmDispatcher (
> +  VOID
> +  );
> +
> +//
> +// Globals used to initialize the protocol
> +//
> +EFI_HANDLE            mMmCpuHandle = NULL;
> +
> +//
> +// Physical pointer to private structure shared between MM IPL and the MM Core
> +//
> +MM_CORE_PRIVATE_DATA  *gMmCorePrivate;
> +
> +//
> +// MM Core global variable for MM System Table.  Only accessed as a physical structure in MMRAM.
> +//
> +EFI_MM_SYSTEM_TABLE  gMmCoreMmst = {
> +
> +  // The table header for the MMST.
> +  {
> +    MM_MMST_SIGNATURE,
> +    EFI_MM_SYSTEM_TABLE_REVISION,
> +    sizeof (gMmCoreMmst.Hdr)
> +  },
> +  // MmFirmwareVendor
> +  NULL,
> +  // MmFirmwareRevision
> +  0,
> +  // MmInstallConfigurationTable
> +  MmInstallConfigurationTable,
> +  // I/O Service
> +  {
> +    {
> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5,       // MmMemRead
> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5        // MmMemWrite
> +    },
> +    {
> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5,       // MmIoRead
> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5        // MmIoWrite
> +    }
> +  },
> +  // Runtime memory services
> +  MmAllocatePool,
> +  MmFreePool,
> +  MmAllocatePages,
> +  MmFreePages,
> +  // MP service
> +  NULL,                          // MmStartupThisAp
> +  0,                             // CurrentlyExecutingCpu
> +  0,                             // NumberOfCpus
> +  NULL,                          // CpuSaveStateSize
> +  NULL,                          // CpuSaveState
> +  0,                             // NumberOfTableEntries
> +  NULL,                          // MmConfigurationTable
> +  MmInstallProtocolInterface,
> +  MmUninstallProtocolInterface,
> +  MmHandleProtocol,
> +  MmRegisterProtocolNotify,
> +  MmLocateHandle,
> +  MmLocateProtocol,
> +  MmiManage,
> +  MmiHandlerRegister,
> +  MmiHandlerUnRegister
> +};
> +
> +//
> +// Flag to determine if the platform has performed a legacy boot.
> +// If this flag is TRUE, then the runtime code and runtime data associated with the
> +// MM IPL are converted to free memory, so the MM Core must guarantee that is
> +// does not touch of the code/data associated with the MM IPL if this flag is TRUE.
> +//
> +BOOLEAN  mInLegacyBoot = FALSE;
> +
> +//
> +// Table of MMI Handlers that are registered by the MM Core when it is initialized
> +//
> +MM_CORE_MMI_HANDLERS  mMmCoreMmiHandlers[] = {
> +  { MmFvDispatchHandler,     &gMmFvDispatchGuid,                 NULL, TRUE  },
> +  { MmDriverDispatchHandler, &gEfiEventDxeDispatchGuid,          NULL, TRUE  },
> +  { MmReadyToLockHandler,    &gEfiDxeMmReadyToLockProtocolGuid,  NULL, TRUE  },
> +  { MmEndOfDxeHandler,       &gEfiEndOfDxeEventGroupGuid,        NULL, FALSE },
> +  { MmLegacyBootHandler,     &gEfiEventLegacyBootGuid,           NULL, FALSE },
> +  { MmExitBootServiceHandler,&gEfiEventExitBootServicesGuid,     NULL, FALSE },
> +  { MmReadyToBootHandler,    &gEfiEventReadyToBootGuid,          NULL, FALSE },
> +  { NULL,                    NULL,                               NULL, FALSE },
> +};
> +
> +EFI_SYSTEM_TABLE                *mEfiSystemTable;
> +UINTN                           mMmramRangeCount;
> +EFI_MMRAM_DESCRIPTOR            *mMmramRanges;
> +
> +/**
> +  Place holder function until all the MM System Table Service are available.
> +
> +  Note: This function is only used by MMRAM invocation.  It is never used by DXE invocation.
> +
> +  @param  Arg1                   Undefined
> +  @param  Arg2                   Undefined
> +  @param  Arg3                   Undefined
> +  @param  Arg4                   Undefined
> +  @param  Arg5                   Undefined
> +
> +  @return EFI_NOT_AVAILABLE_YET
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmEfiNotAvailableYetArg5 (
> +  UINTN Arg1,
> +  UINTN Arg2,
> +  UINTN Arg3,
> +  UINTN Arg4,
> +  UINTN Arg5
> +  )
> +{
> +  //
> +  // This function should never be executed.  If it does, then the architectural protocols
> +  // have not been designed correctly.
> +  //
> +  return EFI_NOT_AVAILABLE_YET;
> +}
> +
> +/**
> +  Software MMI handler that is called when a Legacy Boot event is signaled.  The MM
> +  Core uses this signal to know that a Legacy Boot has been performed and that
> +  gMmCorePrivate that is shared between the UEFI and MM execution environments can
> +  not be accessed from MM anymore since that structure is considered free memory by
> +  a legacy OS.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLegacyBootHandler (
> +  IN     EFI_HANDLE  DispatchHandle,
> +  IN     CONST VOID  *Context,        OPTIONAL
> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_HANDLE  MmHandle;
> +  EFI_STATUS  Status = EFI_SUCCESS;
> +
> +  if (!mInLegacyBoot) {
> +    MmHandle = NULL;
> +    Status = MmInstallProtocolInterface (
> +               &MmHandle,
> +               &gEfiEventLegacyBootGuid,
> +               EFI_NATIVE_INTERFACE,
> +               NULL
> +               );
> +  }
> +  mInLegacyBoot = TRUE;
> +  return Status;
> +}
> +
> +/**
> +  Software MMI handler that is called when a ExitBoot Service event is signaled.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmExitBootServiceHandler (
> +  IN     EFI_HANDLE  DispatchHandle,
> +  IN     CONST VOID  *Context,        OPTIONAL
> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_HANDLE  MmHandle;
> +  EFI_STATUS  Status = EFI_SUCCESS;
> +  STATIC BOOLEAN mInExitBootServices = FALSE;
> +
> +  if (!mInExitBootServices) {
> +    MmHandle = NULL;
> +    Status = MmInstallProtocolInterface (
> +               &MmHandle,
> +               &gEfiEventExitBootServicesGuid,
> +               EFI_NATIVE_INTERFACE,
> +               NULL
> +               );
> +  }
> +  mInExitBootServices = TRUE;
> +  return Status;
> +}
> +
> +/**
> +  Software MMI handler that is called when a ExitBoot Service event is signaled.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmReadyToBootHandler (
> +  IN     EFI_HANDLE  DispatchHandle,
> +  IN     CONST VOID  *Context,        OPTIONAL
> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_HANDLE  MmHandle;
> +  EFI_STATUS  Status = EFI_SUCCESS;
> +  STATIC BOOLEAN mInReadyToBoot = FALSE;
> +
> +  if (!mInReadyToBoot) {
> +    MmHandle = NULL;
> +    Status = MmInstallProtocolInterface (
> +               &MmHandle,
> +               &gEfiEventReadyToBootGuid,
> +               EFI_NATIVE_INTERFACE,
> +               NULL
> +               );
> +  }
> +  mInReadyToBoot = TRUE;
> +  return Status;
> +}
> +
> +/**
> +  Software MMI handler that is called when the DxeMmReadyToLock protocol is added
> +  or if gEfiEventReadyToBootGuid is signaled.  This function unregisters the
> +  Software SMIs that are nor required after MMRAM is locked and installs the
> +  MM Ready To Lock Protocol so MM Drivers are informed that MMRAM is about
> +  to be locked.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmReadyToLockHandler (
> +  IN     EFI_HANDLE  DispatchHandle,
> +  IN     CONST VOID  *Context,        OPTIONAL
> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_STATUS  Status;
> +  UINTN       Index;
> +  EFI_HANDLE  MmHandle;
> +
> +  DEBUG ((DEBUG_INFO, "MmReadyToLockHandler\n"));
> +
> +  //
> +  // Unregister MMI Handlers that are no longer required after the MM driver dispatch is stopped
> +  //
> +  for (Index = 0; mMmCoreMmiHandlers[Index].HandlerType != NULL; Index++) {
> +    if (mMmCoreMmiHandlers[Index].UnRegister) {
> +      MmiHandlerUnRegister (mMmCoreMmiHandlers[Index].DispatchHandle);
> +    }
> +  }
> +
> +  //
> +  // Install MM Ready to lock protocol
> +  //
> +  MmHandle = NULL;
> +  Status = MmInstallProtocolInterface (
> +             &MmHandle,
> +             &gEfiMmReadyToLockProtocolGuid,
> +             EFI_NATIVE_INTERFACE,
> +             NULL
> +             );
> +
> +  //
> +  // Make sure MM CPU I/O 2 Protocol has been installed into the handle database
> +  //
> +  //Status = MmLocateProtocol (&EFI_MM_CPU_IO_PROTOCOL_GUID, NULL, &Interface);
> +
> +  //
> +  // Print a message on a debug build if the MM CPU I/O 2 Protocol is not installed
> +  //
> +  //if (EFI_ERROR (Status)) {
> +      //DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));
> +  //}
> +
> +
> +  //
> +  // Assert if the CPU I/O 2 Protocol is not installed
> +  //
> +  //ASSERT_EFI_ERROR (Status);
> +
> +  //
> +  // Display any drivers that were not dispatched because dependency expression
> +  // evaluated to false if this is a debug build
> +  //
> +  //MmDisplayDiscoveredNotDispatched ();
> +
> +  return Status;
> +}
> +
> +/**
> +  Software MMI handler that is called when the EndOfDxe event is signaled.
> +  This function installs the MM EndOfDxe Protocol so MM Drivers are informed that
> +  platform code will invoke 3rd part code.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmEndOfDxeHandler (
> +  IN     EFI_HANDLE  DispatchHandle,
> +  IN     CONST VOID  *Context,        OPTIONAL
> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_STATUS  Status;
> +  EFI_HANDLE  MmHandle;
> +
> +  DEBUG ((DEBUG_INFO, "MmEndOfDxeHandler\n"));
> +  //
> +  // Install MM EndOfDxe protocol
> +  //
> +  MmHandle = NULL;
> +  Status = MmInstallProtocolInterface (
> +             &MmHandle,
> +             &gEfiMmEndOfDxeProtocolGuid,
> +             EFI_NATIVE_INTERFACE,
> +             NULL
> +             );
> +  return Status;
> +}
> +
> +
> +
> +/**
> +  The main entry point to MM Foundation.
> +
> +  Note: This function is only used by MMRAM invocation.  It is never used by DXE invocation.
> +
> +  @param  MmEntryContext           Processor information and functionality
> +                                    needed by MM Foundation.
> +
> +**/
> +VOID
> +EFIAPI
> +MmEntryPoint (
> +  IN CONST EFI_MM_ENTRY_CONTEXT  *MmEntryContext
> +)
> +{
> +  EFI_STATUS                  Status;
> +  EFI_MM_COMMUNICATE_HEADER  *CommunicateHeader;
> +  BOOLEAN                     InLegacyBoot;
> +
> +  DEBUG ((DEBUG_INFO, "MmEntryPoint ...\n"));
> +
> +  //
> +  // Update MMST using the context
> +  //
> +  CopyMem (&gMmCoreMmst.MmStartupThisAp, MmEntryContext, sizeof (EFI_MM_ENTRY_CONTEXT));
> +
> +  //
> +  // Call platform hook before Mm Dispatch
> +  //
> +  //PlatformHookBeforeMmDispatch ();
> +
> +  //
> +  // If a legacy boot has occured, then make sure gMmCorePrivate is not accessed
> +  //
> +  InLegacyBoot = mInLegacyBoot;
> +  if (!InLegacyBoot) {
> +    //
> +    // TBD: Mark the InMm flag as TRUE
> +    //
> +    gMmCorePrivate->InMm = TRUE;
> +
> +    //
> +    // Check to see if this is a Synchronous MMI sent through the MM Communication
> +    // Protocol or an Asynchronous MMI
> +    //
> +    if (gMmCorePrivate->CommunicationBuffer != 0) {
> +      //
> +      // Synchronous MMI for MM Core or request from Communicate protocol
> +      //
> +      if (!MmIsBufferOutsideMmValid ((UINTN)gMmCorePrivate->CommunicationBuffer, gMmCorePrivate->BufferSize)) {
> +        //
> +        // If CommunicationBuffer is not in valid address scope, return EFI_INVALID_PARAMETER
> +        //
> +        gMmCorePrivate->CommunicationBuffer = 0;
> +        gMmCorePrivate->ReturnStatus = EFI_INVALID_PARAMETER;
> +      } else {
> +        CommunicateHeader = (EFI_MM_COMMUNICATE_HEADER *)(UINTN)gMmCorePrivate->CommunicationBuffer;
> +        gMmCorePrivate->BufferSize -= OFFSET_OF (EFI_MM_COMMUNICATE_HEADER, Data);
> +        //DEBUG ((DEBUG_INFO, "CommunicateHeader->HeaderGuid - %g\n", &CommunicateHeader->HeaderGuid));
> +        Status = MmiManage (
> +                   &CommunicateHeader->HeaderGuid,
> +                   NULL,
> +                   CommunicateHeader->Data,
> +                   (UINTN *)&gMmCorePrivate->BufferSize
> +                   );
> +        //
> +        // Update CommunicationBuffer, BufferSize and ReturnStatus
> +        // Communicate service finished, reset the pointer to CommBuffer to NULL
> +        //
> +        gMmCorePrivate->BufferSize += OFFSET_OF (EFI_MM_COMMUNICATE_HEADER, Data);
> +        gMmCorePrivate->CommunicationBuffer = 0;
> +        gMmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
> +      }
> +    }
> +  }
> +
> +  //
> +  // Process Asynchronous MMI sources
> +  //
> +  MmiManage (NULL, NULL, NULL, NULL);
> +
> +  //
> +  // TBD: Do not use private data structure ?
> +  //
> +
> +  //
> +  // If a legacy boot has occured, then make sure gMmCorePrivate is not accessed
> +  //
> +  if (!InLegacyBoot) {
> +    //
> +    // Clear the InMm flag as we are going to leave MM
> +    //
> +    gMmCorePrivate->InMm = FALSE;
> +  }
> +
> +  DEBUG ((DEBUG_INFO, "MmEntryPoint Done\n"));
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +MmConfigurationMmNotify (
> +  IN CONST EFI_GUID *Protocol,
> +  IN VOID           *Interface,
> +  IN EFI_HANDLE      Handle
> +  )
> +{
> +  EFI_STATUS                      Status;
> +  EFI_MM_CONFIGURATION_PROTOCOL  *MmConfiguration;
> +
> +  DEBUG ((DEBUG_INFO, "MmConfigurationMmNotify(%g) - %x\n", Protocol, Interface));
> +
> +  MmConfiguration = Interface;
> +
> +  //
> +  // Register the MM Entry Point provided by the MM Core with the MM COnfiguration protocol
> +  //
> +  Status = MmConfiguration->RegisterMmEntry (MmConfiguration, (EFI_MM_ENTRY_POINT)(UINTN)gMmCorePrivate->MmEntryPoint);
> +  ASSERT_EFI_ERROR (Status);
> +
> +  //
> +  // Set flag to indicate that the MM Entry Point has been registered which
> +  // means that MMIs are now fully operational.
> +  //
> +  gMmCorePrivate->MmEntryPointRegistered = TRUE;
> +
> +  //
> +  // Print debug message showing MM Core entry point address.
> +  //
> +  DEBUG ((DEBUG_INFO, "MM Core registered MM Entry Point address %p\n", (VOID *)(UINTN)gMmCorePrivate->MmEntryPoint));
> +  return EFI_SUCCESS;
> +}
> +
> +UINTN
> +GetHobListSize (
> +  IN VOID *HobStart
> +  )
> +{
> +  EFI_PEI_HOB_POINTERS  Hob;
> +
> +  ASSERT (HobStart != NULL);
> +
> +  Hob.Raw = (UINT8 *) HobStart;
> +  while (!END_OF_HOB_LIST (Hob)) {
> +    Hob.Raw = GET_NEXT_HOB (Hob);
> +  }
> +  //
> +  // Need plus END_OF_HOB_LIST
> +  //
> +  return (UINTN)Hob.Raw - (UINTN)HobStart + sizeof(EFI_HOB_GENERIC_HEADER);
> +}
> +
> +/**
> +  The Entry Point for MM Core
> +
> +  Install DXE Protocols and reload MM Core into MMRAM and register MM Core
> +  EntryPoint on the MMI vector.
> +
> +  Note: This function is called for both DXE invocation and MMRAM invocation.
> +
> +  @param  ImageHandle    The firmware allocated handle for the EFI image.
> +  @param  SystemTable    A pointer to the EFI System Table.
> +
> +  @retval EFI_SUCCESS    The entry point is executed successfully.
> +  @retval Other          Some error occurred when executing this entry point.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +StandaloneMmMain (
> +  IN VOID  *HobStart
> +  )
> +{
> +  EFI_STATUS                      Status;
> +  UINTN                           Index;
> +  VOID                            *MmHobStart;
> +  UINTN                           HobSize;
> +  VOID                            *Registration;
> +  EFI_HOB_GUID_TYPE               *GuidHob;
> +  MM_CORE_DATA_HOB_DATA           *DataInHob;
> +  EFI_HOB_GUID_TYPE               *MmramRangesHob;
> +  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
> +  EFI_MMRAM_DESCRIPTOR            *MmramRanges;
> +  UINT32                          MmramRangeCount;
> +  EFI_HOB_FIRMWARE_VOLUME         *BfvHob;
> +
> +  ProcessLibraryConstructorList (HobStart, &gMmCoreMmst);
> +
> +  DEBUG ((DEBUG_INFO, "MmMain - 0x%x\n", HobStart));
> +
> +  //
> +  // Determine if the caller has passed a reference to a MM_CORE_PRIVATE_DATA
> +  // structure in the Hoblist. This choice will govern how boot information is
> +  // extracted later.
> +  //
> +  GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);
> +  if (GuidHob == NULL) {
> +    //
> +    // Allocate and zero memory for a MM_CORE_PRIVATE_DATA table and then
> +    // initialise it
> +    //
> +    gMmCorePrivate = (MM_CORE_PRIVATE_DATA *) AllocateRuntimePages(EFI_SIZE_TO_PAGES(sizeof(MM_CORE_PRIVATE_DATA)));
> +    SetMem ((VOID *)(UINTN)gMmCorePrivate, sizeof(MM_CORE_PRIVATE_DATA), 0);
> +    gMmCorePrivate->Signature = MM_CORE_PRIVATE_DATA_SIGNATURE;
> +    gMmCorePrivate->MmEntryPointRegistered = FALSE;
> +    gMmCorePrivate->InMm = FALSE;
> +    gMmCorePrivate->ReturnStatus = EFI_SUCCESS;
> +
> +    //
> +    // Extract the MMRAM ranges from the MMRAM descriptor HOB
> +    //
> +    MmramRangesHob = GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid, HobStart);
> +    if (MmramRangesHob == NULL)
> +      return EFI_UNSUPPORTED;
> +
> +    MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
> +    ASSERT (MmramRangesHobData != NULL);
> +    MmramRanges = MmramRangesHobData->Descriptor;
> +    MmramRangeCount = MmramRangesHobData->NumberOfMmReservedRegions;
> +    ASSERT (MmramRanges);
> +    ASSERT (MmramRangeCount);
> +
> +    //
> +    // Copy the MMRAM ranges into MM_CORE_PRIVATE_DATA table just in case any
> +    // code relies on them being present there
> +    //
> +    gMmCorePrivate->MmramRangeCount = MmramRangeCount;
> +    gMmCorePrivate->MmramRanges = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (MmramRangeCount * sizeof(EFI_MMRAM_DESCRIPTOR));
> +    ASSERT (gMmCorePrivate->MmramRanges != 0);
> +    CopyMem ((VOID *)(UINTN)gMmCorePrivate->MmramRanges,
> +             MmramRanges,
> +             MmramRangeCount * sizeof(EFI_MMRAM_DESCRIPTOR));
> +  } else {
> +    DataInHob       = GET_GUID_HOB_DATA (GuidHob);
> +    gMmCorePrivate = (MM_CORE_PRIVATE_DATA *)(UINTN)DataInHob->Address;
> +    MmramRanges     = (EFI_MMRAM_DESCRIPTOR *)(UINTN)gMmCorePrivate->MmramRanges;
> +    MmramRangeCount = gMmCorePrivate->MmramRangeCount;
> +  }
> +
> +  //
> +  // Print the MMRAM ranges passed by the caller
> +  //
> +  DEBUG ((DEBUG_INFO, "MmramRangeCount - 0x%x\n", MmramRangeCount));
> +  for (Index = 0; Index < MmramRangeCount; Index++) {
> +          DEBUG ((DEBUG_INFO, "MmramRanges[%d]: 0x%016lx - 0x%lx\n", Index,
> +                  MmramRanges[Index].CpuStart,
> +                  MmramRanges[Index].PhysicalSize));
> +  }
> +
> +  //
> +  // Copy the MMRAM ranges into private MMRAM
> +  //
> +  mMmramRangeCount = MmramRangeCount;
> +  DEBUG ((DEBUG_INFO, "mMmramRangeCount - 0x%x\n", mMmramRangeCount));
> +  mMmramRanges = AllocatePool (mMmramRangeCount * sizeof (EFI_MMRAM_DESCRIPTOR));
> +  DEBUG ((DEBUG_INFO, "mMmramRanges - 0x%x\n", mMmramRanges));
> +  ASSERT (mMmramRanges != NULL);
> +  CopyMem (mMmramRanges, (VOID *)(UINTN)MmramRanges, mMmramRangeCount * sizeof (EFI_MMRAM_DESCRIPTOR));
> +
> +  //
> +  // Get Boot Firmware Volume address from the BFV Hob
> +  //
> +  BfvHob = GetFirstHob (EFI_HOB_TYPE_FV);
> +  if (BfvHob != NULL) {
> +    DEBUG ((DEBUG_INFO, "BFV address - 0x%x\n", BfvHob->BaseAddress));
> +    DEBUG ((DEBUG_INFO, "BFV size    - 0x%x\n", BfvHob->Length));
> +    gMmCorePrivate->StandaloneBfvAddress = BfvHob->BaseAddress;
> +  }
> +
> +  gMmCorePrivate->Mmst          = (EFI_PHYSICAL_ADDRESS)(UINTN)&gMmCoreMmst;
> +  gMmCorePrivate->MmEntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)MmEntryPoint;
> +
> +  //
> +  // No need to initialize memory service.
> +  // It is done in constructor of StandaloneMmCoreMemoryAllocationLib(),
> +  // so that the library linked with StandaloneMmCore can use AllocatePool() in constuctor.
> +  //
> +
> +  DEBUG ((DEBUG_INFO, "MmInstallConfigurationTable For HobList\n"));
> +  //
> +  // Install HobList
> +  //
> +  HobSize = GetHobListSize (HobStart);
> +  DEBUG ((DEBUG_INFO, "HobSize - 0x%x\n", HobSize));
> +  MmHobStart = AllocatePool (HobSize);
> +  DEBUG ((DEBUG_INFO, "MmHobStart - 0x%x\n", MmHobStart));
> +  ASSERT (MmHobStart != NULL);
> +  CopyMem (MmHobStart, HobStart, HobSize);
> +  Status = MmInstallConfigurationTable (&gMmCoreMmst, &gEfiHobListGuid, MmHobStart, HobSize);
> +  ASSERT_EFI_ERROR (Status);
> +
> +  //
> +  // Register notification for EFI_MM_CONFIGURATION_PROTOCOL registration and
> +  // use it to register the MM Foundation entrypoint
> +  //
> +  DEBUG ((DEBUG_INFO, "MmRegisterProtocolNotify - MmConfigurationMmProtocol\n"));
> +  Status = MmRegisterProtocolNotify (
> +             &gEfiMmConfigurationProtocolGuid,
> +             MmConfigurationMmNotify,
> +             &Registration
> +             );
> +  ASSERT_EFI_ERROR (Status);
> +
> +  //
> +  // Dispatch standalone BFV
> +  //
> +  DEBUG ((DEBUG_INFO, "Mm Dispatch StandaloneBfvAddress - 0x%08x\n", gMmCorePrivate->StandaloneBfvAddress));
> +  if (gMmCorePrivate->StandaloneBfvAddress != 0) {
> +    MmCoreFfsFindMmDriver ((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)gMmCorePrivate->StandaloneBfvAddress);
> +    MmDispatcher ();
> +  }
> +
> +  //
> +  // Register all handlers in the core table
> +  //
> +  for (Index = 0; mMmCoreMmiHandlers[Index].HandlerType != NULL; Index++) {
> +    Status = MmiHandlerRegister(mMmCoreMmiHandlers[Index].Handler,
> +                                mMmCoreMmiHandlers[Index].HandlerType,
> +                                &mMmCoreMmiHandlers[Index].DispatchHandle);
> +    DEBUG ((DEBUG_INFO, "MmiHandlerRegister - GUID %g - Status %d\n", mMmCoreMmiHandlers[Index].HandlerType, Status));
> +  }
> +
> +  DEBUG ((DEBUG_INFO, "MmMain Done!\n"));
> +
> +  return EFI_SUCCESS;
> +}
> diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.h b/StandaloneMmPkg/Core/StandaloneMmCore.h
> new file mode 100644
> index 0000000000..53921b7844
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/StandaloneMmCore.h
> @@ -0,0 +1,903 @@
> +/** @file
> +  The internal header file includes the common header files, defines
> +  internal structure and functions used by MmCore module.
> +
> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _MM_CORE_H_
> +#define _MM_CORE_H_
> +
> +#include <PiMm.h>
> +#include <StandaloneMm.h>
> +
> +#include <Protocol/DxeMmReadyToLock.h>
> +#include <Protocol/MmReadyToLock.h>
> +#include <Protocol/MmEndOfDxe.h>
> +#include <Protocol/MmCommunication.h>
> +#include <Protocol/LoadedImage.h>
> +#include <Protocol/MmConfiguration.h>
> +
> +#include <Guid/Apriori.h>
> +#include <Guid/EventGroup.h>
> +#include <Guid/EventLegacyBios.h>
> +#include <Guid/ZeroGuid.h>
> +#include <Guid/MemoryProfile.h>
> +#include <Guid/HobList.h>
> +#include <Guid/MmFvDispatch.h>
> +#include <Guid/MmramMemoryReserve.h>
> +
> +#include <Library/MmCoreStandaloneEntryPoint.h>
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/PeCoffLib.h>
> +#include <Library/CacheMaintenanceLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/ReportStatusCodeLib.h>
> +#include <Library/MemoryAllocationLib.h>
> +#include <Library/PcdLib.h>
> +//#include <Library/MmCorePlatformHookLib.h>
> +#include <Library/MemLib.h>
> +#include <Library/HobLib.h>
> +
> +#include "StandaloneMmCorePrivateData.h"
> +
> +//
> +// Used to build a table of MMI Handlers that the MM Core registers
> +//
> +typedef struct {
> +  EFI_MM_HANDLER_ENTRY_POINT    Handler;
> +  EFI_GUID                      *HandlerType;
> +  EFI_HANDLE                    DispatchHandle;
> +  BOOLEAN                       UnRegister;
> +} MM_CORE_MMI_HANDLERS;
> +
> +//
> +// Structure for recording the state of an MM Driver
> +//
> +#define EFI_MM_DRIVER_ENTRY_SIGNATURE SIGNATURE_32('s', 'd','r','v')
> +
> +typedef struct {
> +  UINTN                           Signature;
> +  LIST_ENTRY                      Link;             // mDriverList
> +
> +  LIST_ENTRY                      ScheduledLink;    // mScheduledQueue
> +
> +  EFI_HANDLE                      FvHandle;
> +  EFI_GUID                        FileName;
> +  VOID                            *Pe32Data;
> +  UINTN                           Pe32DataSize;
> +
> +  VOID                            *Depex;
> +  UINTN                           DepexSize;
> +
> +  BOOLEAN                         Before;
> +  BOOLEAN                         After;
> +  EFI_GUID                        BeforeAfterGuid;
> +
> +  BOOLEAN                         Dependent;
> +  BOOLEAN                         Scheduled;
> +  BOOLEAN                         Initialized;
> +  BOOLEAN                         DepexProtocolError;
> +
> +  EFI_HANDLE                      ImageHandle;
> +  EFI_LOADED_IMAGE_PROTOCOL       *LoadedImage;
> +  //
> +  // Image EntryPoint in MMRAM
> +  //
> +  PHYSICAL_ADDRESS                ImageEntryPoint;
> +  //
> +  // Image Buffer in MMRAM
> +  //
> +  PHYSICAL_ADDRESS                ImageBuffer;
> +  //
> +  // Image Page Number
> +  //
> +  UINTN                           NumberOfPage;
> +} EFI_MM_DRIVER_ENTRY;
> +
> +#define EFI_HANDLE_SIGNATURE            SIGNATURE_32('h','n','d','l')
> +
> +///
> +/// IHANDLE - contains a list of protocol handles
> +///
> +typedef struct {
> +  UINTN               Signature;
> +  /// All handles list of IHANDLE
> +  LIST_ENTRY          AllHandles;
> +  /// List of PROTOCOL_INTERFACE's for this handle
> +  LIST_ENTRY          Protocols;
> +  UINTN               LocateRequest;
> +} IHANDLE;
> +
> +#define ASSERT_IS_HANDLE(a)  ASSERT((a)->Signature == EFI_HANDLE_SIGNATURE)
> +
> +#define PROTOCOL_ENTRY_SIGNATURE        SIGNATURE_32('p','r','t','e')
> +
> +///
> +/// PROTOCOL_ENTRY - each different protocol has 1 entry in the protocol
> +/// database.  Each handler that supports this protocol is listed, along
> +/// with a list of registered notifies.
> +///
> +typedef struct {
> +  UINTN               Signature;
> +  /// Link Entry inserted to mProtocolDatabase
> +  LIST_ENTRY          AllEntries;
> +  /// ID of the protocol
> +  EFI_GUID            ProtocolID;
> +  /// All protocol interfaces
> +  LIST_ENTRY          Protocols;
> +  /// Registerd notification handlers
> +  LIST_ENTRY          Notify;
> +} PROTOCOL_ENTRY;
> +
> +#define PROTOCOL_INTERFACE_SIGNATURE  SIGNATURE_32('p','i','f','c')
> +
> +///
> +/// PROTOCOL_INTERFACE - each protocol installed on a handle is tracked
> +/// with a protocol interface structure
> +///
> +typedef struct {
> +  UINTN                       Signature;
> +  /// Link on IHANDLE.Protocols
> +  LIST_ENTRY                  Link;
> +  /// Back pointer
> +  IHANDLE                     *Handle;
> +  /// Link on PROTOCOL_ENTRY.Protocols
> +  LIST_ENTRY                  ByProtocol;
> +  /// The protocol ID
> +  PROTOCOL_ENTRY              *Protocol;
> +  /// The interface value
> +  VOID                        *Interface;
> +} PROTOCOL_INTERFACE;
> +
> +#define PROTOCOL_NOTIFY_SIGNATURE       SIGNATURE_32('p','r','t','n')
> +
> +///
> +/// PROTOCOL_NOTIFY - used for each register notification for a protocol
> +///
> +typedef struct {
> +  UINTN               Signature;
> +  PROTOCOL_ENTRY      *Protocol;
> +  /// All notifications for this protocol
> +  LIST_ENTRY          Link;
> +  /// Notification function
> +  EFI_MM_NOTIFY_FN   Function;
> +  /// Last position notified
> +  LIST_ENTRY          *Position;
> +} PROTOCOL_NOTIFY;
> +
> +//
> +// MM Core Global Variables
> +//
> +extern MM_CORE_PRIVATE_DATA  *gMmCorePrivate;
> +extern EFI_MM_SYSTEM_TABLE   gMmCoreMmst;
> +extern LIST_ENTRY            gHandleList;
> +extern EFI_PHYSICAL_ADDRESS  gLoadModuleAtFixAddressMmramBase;
> +
> +/**
> +  Called to initialize the memory service.
> +
> +  @param   MmramRangeCount       Number of MMRAM Regions
> +  @param   MmramRanges           Pointer to MMRAM Descriptors
> +
> +**/
> +VOID
> +MmInitializeMemoryServices (
> +  IN UINTN                 MmramRangeCount,
> +  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
> +  );
> +
> +/**
> +  The MmInstallConfigurationTable() function is used to maintain the list
> +  of configuration tables that are stored in the System Management System
> +  Table.  The list is stored as an array of (GUID, Pointer) pairs.  The list
> +  must be allocated from pool memory with PoolType set to EfiRuntimeServicesData.
> +
> +  @param  SystemTable      A pointer to the MM System Table (SMST).
> +  @param  Guid             A pointer to the GUID for the entry to add, update, or remove.
> +  @param  Table            A pointer to the buffer of the table to add.
> +  @param  TableSize        The size of the table to install.
> +
> +  @retval EFI_SUCCESS           The (Guid, Table) pair was added, updated, or removed.
> +  @retval EFI_INVALID_PARAMETER Guid is not valid.
> +  @retval EFI_NOT_FOUND         An attempt was made to delete a non-existent entry.
> +  @retval EFI_OUT_OF_RESOURCES  There is not enough memory available to complete the operation.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInstallConfigurationTable (
> +  IN  CONST EFI_MM_SYSTEM_TABLE  *SystemTable,
> +  IN  CONST EFI_GUID              *Guid,
> +  IN  VOID                        *Table,
> +  IN  UINTN                       TableSize
> +  );
> +
> +/**
> +  Wrapper function to MmInstallProtocolInterfaceNotify.  This is the public API which
> +  Calls the private one which contains a BOOLEAN parameter for notifications
> +
> +  @param  UserHandle             The handle to install the protocol handler on,
> +                                 or NULL if a new handle is to be allocated
> +  @param  Protocol               The protocol to add to the handle
> +  @param  InterfaceType          Indicates whether Interface is supplied in
> +                                 native form.
> +  @param  Interface              The interface for the protocol being added
> +
> +  @return Status code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInstallProtocolInterface (
> +  IN OUT EFI_HANDLE     *UserHandle,
> +  IN EFI_GUID           *Protocol,
> +  IN EFI_INTERFACE_TYPE InterfaceType,
> +  IN VOID               *Interface
> +  );
> +
> +/**
> +  Allocates pages from the memory map.
> +
> +  @param  Type                   The type of allocation to perform
> +  @param  MemoryType             The type of memory to turn the allocated pages
> +                                 into
> +  @param  NumberOfPages          The number of pages to allocate
> +  @param  Memory                 A pointer to receive the base allocated memory
> +                                 address
> +
> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
> +  @retval EFI_SUCCESS            Pages successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmAllocatePages (
> +  IN      EFI_ALLOCATE_TYPE         Type,
> +  IN      EFI_MEMORY_TYPE           MemoryType,
> +  IN      UINTN                     NumberOfPages,
> +  OUT     EFI_PHYSICAL_ADDRESS      *Memory
> +  );
> +
> +/**
> +  Allocates pages from the memory map.
> +
> +  @param  Type                   The type of allocation to perform
> +  @param  MemoryType             The type of memory to turn the allocated pages
> +                                 into
> +  @param  NumberOfPages          The number of pages to allocate
> +  @param  Memory                 A pointer to receive the base allocated memory
> +                                 address
> +
> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
> +  @retval EFI_SUCCESS            Pages successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalAllocatePages (
> +  IN      EFI_ALLOCATE_TYPE         Type,
> +  IN      EFI_MEMORY_TYPE           MemoryType,
> +  IN      UINTN                     NumberOfPages,
> +  OUT     EFI_PHYSICAL_ADDRESS      *Memory
> +  );
> +
> +/**
> +  Frees previous allocated pages.
> +
> +  @param  Memory                 Base address of memory being freed
> +  @param  NumberOfPages          The number of pages to free
> +
> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range
> +  @retval EFI_INVALID_PARAMETER  Address not aligned, Address is zero or NumberOfPages is zero.
> +  @return EFI_SUCCESS            Pages successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmFreePages (
> +  IN      EFI_PHYSICAL_ADDRESS      Memory,
> +  IN      UINTN                     NumberOfPages
> +  );
> +
> +/**
> +  Frees previous allocated pages.
> +
> +  @param  Memory                 Base address of memory being freed
> +  @param  NumberOfPages          The number of pages to free
> +
> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range
> +  @retval EFI_INVALID_PARAMETER  Address not aligned, Address is zero or NumberOfPages is zero.
> +  @return EFI_SUCCESS            Pages successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalFreePages (
> +  IN      EFI_PHYSICAL_ADDRESS      Memory,
> +  IN      UINTN                     NumberOfPages
> +  );
> +
> +/**
> +  Allocate pool of a particular type.
> +
> +  @param  PoolType               Type of pool to allocate
> +  @param  Size                   The amount of pool to allocate
> +  @param  Buffer                 The address to return a pointer to the allocated
> +                                 pool
> +
> +  @retval EFI_INVALID_PARAMETER  PoolType not valid
> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
> +  @retval EFI_SUCCESS            Pool successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmAllocatePool (
> +  IN      EFI_MEMORY_TYPE           PoolType,
> +  IN      UINTN                     Size,
> +  OUT     VOID                      **Buffer
> +  );
> +
> +/**
> +  Allocate pool of a particular type.
> +
> +  @param  PoolType               Type of pool to allocate
> +  @param  Size                   The amount of pool to allocate
> +  @param  Buffer                 The address to return a pointer to the allocated
> +                                 pool
> +
> +  @retval EFI_INVALID_PARAMETER  PoolType not valid
> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
> +  @retval EFI_SUCCESS            Pool successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalAllocatePool (
> +  IN      EFI_MEMORY_TYPE           PoolType,
> +  IN      UINTN                     Size,
> +  OUT     VOID                      **Buffer
> +  );
> +
> +/**
> +  Frees pool.
> +
> +  @param  Buffer                 The allocated pool entry to free
> +
> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
> +  @retval EFI_SUCCESS            Pool successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmFreePool (
> +  IN      VOID                      *Buffer
> +  );
> +
> +/**
> +  Frees pool.
> +
> +  @param  Buffer                 The allocated pool entry to free
> +
> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
> +  @retval EFI_SUCCESS            Pool successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalFreePool (
> +  IN      VOID                      *Buffer
> +  );
> +
> +/**
> +  Installs a protocol interface into the boot services environment.
> +
> +  @param  UserHandle             The handle to install the protocol handler on,
> +                                 or NULL if a new handle is to be allocated
> +  @param  Protocol               The protocol to add to the handle
> +  @param  InterfaceType          Indicates whether Interface is supplied in
> +                                 native form.
> +  @param  Interface              The interface for the protocol being added
> +  @param  Notify                 indicates whether notify the notification list
> +                                 for this protocol
> +
> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> +  @retval EFI_OUT_OF_RESOURCES   No enough buffer to allocate
> +  @retval EFI_SUCCESS            Protocol interface successfully installed
> +
> +**/
> +EFI_STATUS
> +MmInstallProtocolInterfaceNotify (
> +  IN OUT EFI_HANDLE     *UserHandle,
> +  IN EFI_GUID           *Protocol,
> +  IN EFI_INTERFACE_TYPE InterfaceType,
> +  IN VOID               *Interface,
> +  IN BOOLEAN            Notify
> +  );
> +
> +/**
> +  Uninstalls all instances of a protocol:interfacer from a handle.
> +  If the last protocol interface is remove from the handle, the
> +  handle is freed.
> +
> +  @param  UserHandle             The handle to remove the protocol handler from
> +  @param  Protocol               The protocol, of protocol:interface, to remove
> +  @param  Interface              The interface, of protocol:interface, to remove
> +
> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL.
> +  @retval EFI_SUCCESS            Protocol interface successfully uninstalled.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmUninstallProtocolInterface (
> +  IN EFI_HANDLE       UserHandle,
> +  IN EFI_GUID         *Protocol,
> +  IN VOID             *Interface
> +  );
> +
> +/**
> +  Queries a handle to determine if it supports a specified protocol.
> +
> +  @param  UserHandle             The handle being queried.
> +  @param  Protocol               The published unique identifier of the protocol.
> +  @param  Interface              Supplies the address where a pointer to the
> +                                 corresponding Protocol Interface is returned.
> +
> +  @return The requested protocol interface for the handle
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmHandleProtocol (
> +  IN EFI_HANDLE       UserHandle,
> +  IN EFI_GUID         *Protocol,
> +  OUT VOID            **Interface
> +  );
> +
> +/**
> +  Add a new protocol notification record for the request protocol.
> +
> +  @param  Protocol               The requested protocol to add the notify
> +                                 registration
> +  @param  Function               Points to the notification function
> +  @param  Registration           Returns the registration record
> +
> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> +  @retval EFI_SUCCESS            Successfully returned the registration record
> +                                 that has been added
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmRegisterProtocolNotify (
> +  IN  CONST EFI_GUID              *Protocol,
> +  IN  EFI_MM_NOTIFY_FN           Function,
> +  OUT VOID                        **Registration
> +  );
> +
> +/**
> +  Locates the requested handle(s) and returns them in Buffer.
> +
> +  @param  SearchType             The type of search to perform to locate the
> +                                 handles
> +  @param  Protocol               The protocol to search for
> +  @param  SearchKey              Dependant on SearchType
> +  @param  BufferSize             On input the size of Buffer.  On output the
> +                                 size of data returned.
> +  @param  Buffer                 The buffer to return the results in
> +
> +  @retval EFI_BUFFER_TOO_SMALL   Buffer too small, required buffer size is
> +                                 returned in BufferSize.
> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> +  @retval EFI_SUCCESS            Successfully found the requested handle(s) and
> +                                 returns them in Buffer.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLocateHandle (
> +  IN EFI_LOCATE_SEARCH_TYPE   SearchType,
> +  IN EFI_GUID                 *Protocol   OPTIONAL,
> +  IN VOID                     *SearchKey  OPTIONAL,
> +  IN OUT UINTN                *BufferSize,
> +  OUT EFI_HANDLE              *Buffer
> +  );
> +
> +/**
> +  Return the first Protocol Interface that matches the Protocol GUID. If
> +  Registration is pasased in return a Protocol Instance that was just add
> +  to the system. If Retistration is NULL return the first Protocol Interface
> +  you find.
> +
> +  @param  Protocol               The protocol to search for
> +  @param  Registration           Optional Registration Key returned from
> +                                 RegisterProtocolNotify()
> +  @param  Interface              Return the Protocol interface (instance).
> +
> +  @retval EFI_SUCCESS            If a valid Interface is returned
> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> +  @retval EFI_NOT_FOUND          Protocol interface not found
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLocateProtocol (
> +  IN  EFI_GUID  *Protocol,
> +  IN  VOID      *Registration OPTIONAL,
> +  OUT VOID      **Interface
> +  );
> +
> +/**
> +  Manage MMI of a particular type.
> +
> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
> +  @param  Context        Points to an optional context buffer.
> +  @param  CommBuffer     Points to the optional communication buffer.
> +  @param  CommBufferSize Points to the size of the optional communication buffer.
> +
> +  @retval EFI_SUCCESS                        Interrupt source was processed successfully but not quiesced.
> +  @retval EFI_INTERRUPT_PENDING              One or more MMI sources could not be quiesced.
> +  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING  Interrupt source was not handled or quiesced.
> +  @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED Interrupt source was handled and quiesced.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmiManage (
> +  IN     CONST EFI_GUID           *HandlerType,
> +  IN     CONST VOID               *Context         OPTIONAL,
> +  IN OUT VOID                     *CommBuffer      OPTIONAL,
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  Registers a handler to execute within MM.
> +
> +  @param  Handler        Handler service funtion pointer.
> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
> +  @param  DispatchHandle On return, contains a unique handle which can be used to later unregister the handler function.
> +
> +  @retval EFI_SUCCESS           Handler register success.
> +  @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmiHandlerRegister (
> +  IN   EFI_MM_HANDLER_ENTRY_POINT     Handler,
> +  IN   CONST EFI_GUID                 *HandlerType  OPTIONAL,
> +  OUT  EFI_HANDLE                     *DispatchHandle
> +  );
> +
> +/**
> +  Unregister a handler in MM.
> +
> +  @param  DispatchHandle  The handle that was specified when the handler was registered.
> +
> +  @retval EFI_SUCCESS           Handler function was successfully unregistered.
> +  @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid handle.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmiHandlerUnRegister (
> +  IN  EFI_HANDLE                      DispatchHandle
> +  );
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmDriverDispatchHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmFvDispatchHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLegacyBootHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmExitBootServiceHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmReadyToBootHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmReadyToLockHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmEndOfDxeHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  Place holder function until all the MM System Table Service are available.
> +
> +  @param  Arg1                   Undefined
> +  @param  Arg2                   Undefined
> +  @param  Arg3                   Undefined
> +  @param  Arg4                   Undefined
> +  @param  Arg5                   Undefined
> +
> +  @return EFI_NOT_AVAILABLE_YET
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmEfiNotAvailableYetArg5 (
> +  UINTN Arg1,
> +  UINTN Arg2,
> +  UINTN Arg3,
> +  UINTN Arg4,
> +  UINTN Arg5
> +  );
> +
> +//
> +//Functions used during debug buils
> +//
> +
> +/**
> +  Traverse the discovered list for any drivers that were discovered but not loaded
> +  because the dependency expressions evaluated to false.
> +
> +**/
> +VOID
> +MmDisplayDiscoveredNotDispatched (
> +  VOID
> +  );
> +
> +/**
> +  Add free MMRAM region for use by memory service.
> +
> +  @param  MemBase                Base address of memory region.
> +  @param  MemLength              Length of the memory region.
> +  @param  Type                   Memory type.
> +  @param  Attributes             Memory region state.
> +
> +**/
> +VOID
> +MmAddMemoryRegion (
> +  IN      EFI_PHYSICAL_ADDRESS      MemBase,
> +  IN      UINT64                    MemLength,
> +  IN      EFI_MEMORY_TYPE           Type,
> +  IN      UINT64                    Attributes
> +  );
> +
> +/**
> +  Finds the protocol entry for the requested protocol.
> +
> +  @param  Protocol               The ID of the protocol
> +  @param  Create                 Create a new entry if not found
> +
> +  @return Protocol entry
> +
> +**/
> +PROTOCOL_ENTRY  *
> +MmFindProtocolEntry (
> +  IN EFI_GUID   *Protocol,
> +  IN BOOLEAN    Create
> +  );
> +
> +/**
> +  Signal event for every protocol in protocol entry.
> +
> +  @param  Prot                   Protocol interface
> +
> +**/
> +VOID
> +MmNotifyProtocol (
> +  IN PROTOCOL_INTERFACE   *Prot
> +  );
> +
> +/**
> +  Finds the protocol instance for the requested handle and protocol.
> +  Note: This function doesn't do parameters checking, it's caller's responsibility
> +  to pass in valid parameters.
> +
> +  @param  Handle                 The handle to search the protocol on
> +  @param  Protocol               GUID of the protocol
> +  @param  Interface              The interface for the protocol being searched
> +
> +  @return Protocol instance (NULL: Not found)
> +
> +**/
> +PROTOCOL_INTERFACE *
> +MmFindProtocolInterface (
> +  IN IHANDLE        *Handle,
> +  IN EFI_GUID       *Protocol,
> +  IN VOID           *Interface
> +  );
> +
> +/**
> +  Removes Protocol from the protocol list (but not the handle list).
> +
> +  @param  Handle                 The handle to remove protocol on.
> +  @param  Protocol               GUID of the protocol to be moved
> +  @param  Interface              The interface of the protocol
> +
> +  @return Protocol Entry
> +
> +**/
> +PROTOCOL_INTERFACE *
> +MmRemoveInterfaceFromProtocol (
> +  IN IHANDLE        *Handle,
> +  IN EFI_GUID       *Protocol,
> +  IN VOID           *Interface
> +  );
> +
> +/**
> +  This is the POSTFIX version of the dependency evaluator.  This code does
> +  not need to handle Before or After, as it is not valid to call this
> +  routine in this case. POSTFIX means all the math is done on top of the stack.
> +
> +  @param  DriverEntry           DriverEntry element to update.
> +
> +  @retval TRUE                  If driver is ready to run.
> +  @retval FALSE                 If driver is not ready to run or some fatal error
> +                                was found.
> +
> +**/
> +BOOLEAN
> +MmIsSchedulable (
> +  IN  EFI_MM_DRIVER_ENTRY   *DriverEntry
> +  );
> +
> +/**
> +  Dump MMRAM information.
> +
> +**/
> +VOID
> +DumpMmramInfo (
> +  VOID
> +  );
> +
> +extern UINTN                    mMmramRangeCount;
> +extern EFI_MMRAM_DESCRIPTOR     *mMmramRanges;
> +extern EFI_SYSTEM_TABLE         *mEfiSystemTable;
> +
> +#endif
> diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.inf b/StandaloneMmPkg/Core/StandaloneMmCore.inf
> new file mode 100644
> index 0000000000..c5eaa14ba3
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/StandaloneMmCore.inf
> @@ -0,0 +1,80 @@
> +## @file
> +# This module provide an SMM CIS compliant implementation of SMM Core.
> +#
> +# Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
> +# Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +#
> +# 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
> +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = StandaloneMmCore
> +  FILE_GUID                      = 6E14B6FD-3600-4DD6-A17A-206B3B6DCE16
> +  MODULE_TYPE                    = MM_CORE_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  ENTRY_POINT                    = StandaloneMmMain
> +
> +#  VALID_ARCHITECTURES           = IA32 X64 AARCH64
> +
> +[Sources]
> +  StandaloneMmCore.c
> +  StandaloneMmCore.h
> +  StandaloneMmCorePrivateData.h
> +  Page.c
> +  Pool.c
> +  Handle.c
> +  Locate.c
> +  Notify.c
> +  Dependency.c
> +  Dispatcher.c
> +  Mmi.c
> +  InstallConfigurationTable.c
> +  FwVol.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +  MdeModulePkg/MdeModulePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[LibraryClasses]
> +  BaseLib
> +  BaseMemoryLib
> +  CacheMaintenanceLib
> +  DebugLib
> +  FvLib
> +  HobLib
> +  MemoryAllocationLib
> +  MemLib
> +  PeCoffLib
> +  ReportStatusCodeLib
> +  StandaloneMmCoreEntryPoint
> +
> +[Protocols]
> +  gEfiDxeMmReadyToLockProtocolGuid             ## UNDEFINED # SmiHandlerRegister
> +  gEfiMmReadyToLockProtocolGuid                ## PRODUCES
> +  gEfiMmEndOfDxeProtocolGuid                   ## PRODUCES
> +  gEfiLoadedImageProtocolGuid                   ## PRODUCES
> +  gEfiMmConfigurationProtocolGuid               ## CONSUMES
> +
> +[Guids]
> +  gAprioriGuid                                  ## SOMETIMES_CONSUMES   ## File
> +  gEfiEventDxeDispatchGuid                      ## PRODUCES             ## GUID # SmiHandlerRegister
> +  gEfiEndOfDxeEventGroupGuid                    ## PRODUCES             ## GUID # SmiHandlerRegister
> +  ## SOMETIMES_CONSUMES   ## GUID # Locate protocol
> +  ## SOMETIMES_PRODUCES   ## GUID # SmiHandlerRegister
> +  gEdkiiMemoryProfileGuid
> +  gZeroGuid                                     ## SOMETIMES_CONSUMES   ## GUID
> +  gEfiHobListGuid
> +  gMmCoreDataHobGuid
> +  gMmFvDispatchGuid
> +  gEfiEventLegacyBootGuid
> +  gEfiEventExitBootServicesGuid
> +  gEfiEventReadyToBootGuid
> diff --git a/StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h b/StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
> new file mode 100644
> index 0000000000..faedf3ff2d
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
> @@ -0,0 +1,66 @@
> +/** @file
> +  The internal header file that declared a data structure that is shared
> +  between the MM IPL and the MM Core.
> +
> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _STANDALONE_MM_CORE_PRIVATE_DATA_H_
> +#define _STANDALONE_MM_CORE_PRIVATE_DATA_H_
> +
> +#include <Guid/MmCoreData.h>
> +
> +//
> +// Page management
> +//
> +
> +typedef struct {
> +  LIST_ENTRY  Link;
> +  UINTN       NumberOfPages;
> +} FREE_PAGE_LIST;
> +
> +extern LIST_ENTRY  mMmMemoryMap;
> +
> +//
> +// Pool management
> +//
> +
> +//
> +// MIN_POOL_SHIFT must not be less than 5
> +//
> +#define MIN_POOL_SHIFT  6
> +#define MIN_POOL_SIZE   (1 << MIN_POOL_SHIFT)
> +
> +//
> +// MAX_POOL_SHIFT must not be less than EFI_PAGE_SHIFT - 1
> +//
> +#define MAX_POOL_SHIFT  (EFI_PAGE_SHIFT - 1)
> +#define MAX_POOL_SIZE   (1 << MAX_POOL_SHIFT)
> +
> +//
> +// MAX_POOL_INDEX are calculated by maximum and minimum pool sizes
> +//
> +#define MAX_POOL_INDEX  (MAX_POOL_SHIFT - MIN_POOL_SHIFT + 1)
> +
> +typedef struct {
> +  UINTN        Size;
> +  BOOLEAN      Available;
> +} POOL_HEADER;
> +
> +typedef struct {
> +  POOL_HEADER  Header;
> +  LIST_ENTRY   Link;
> +} FREE_POOL_HEADER;
> +
> +extern LIST_ENTRY  mMmPoolLists[MAX_POOL_INDEX];
> +
> +#endif
> diff --git a/StandaloneMmPkg/Include/Guid/MmFvDispatch.h b/StandaloneMmPkg/Include/Guid/MmFvDispatch.h
> new file mode 100644
> index 0000000000..fb194d3474
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Guid/MmFvDispatch.h
> @@ -0,0 +1,38 @@
> +/** @file
> +  GUIDs for MM Event.
> +
> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +This program and the accompanying materials are licensed and made available under
> +the terms and conditions of the BSD License that accompanies this distribution.
> +The full text of the license may be found at
> +http://opensource.org/licenses/bsd-license.php.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef __MM_FV_DISPATCH_H__
> +#define __MM_FV_DISPATCH_H__
> +
> +#define MM_FV_DISPATCH_GUID \
> +  { 0xb65694cc, 0x9e3, 0x4c3b, { 0xb5, 0xcd, 0x5, 0xf4, 0x4d, 0x3c, 0xdb, 0xff }}
> +
> +extern EFI_GUID gMmFvDispatchGuid;
> +
> +#pragma pack(1)
> +typedef struct {
> +  EFI_PHYSICAL_ADDRESS  Address;
> +  UINT64                Size;
> +} EFI_MM_COMMUNICATE_FV_DISPATCH_DATA;
> +
> +typedef struct {
> +  EFI_GUID                              HeaderGuid;
> +  UINTN                                 MessageLength;
> +  EFI_MM_COMMUNICATE_FV_DISPATCH_DATA   Data;
> +} EFI_MM_COMMUNICATE_FV_DISPATCH;
> +#pragma pack()
> +
> +#endif
> diff --git a/StandaloneMmPkg/Include/StandaloneMm.h b/StandaloneMmPkg/Include/StandaloneMm.h
> new file mode 100644
> index 0000000000..0e420315bb
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/StandaloneMm.h
> @@ -0,0 +1,36 @@
> +/** @file
> +  Standalone MM.
> +
> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _STANDALONE_MM_H_
> +#define _STANDALONE_MM_H_
> +
> +#include <PiMm.h>
> +
> +typedef
> +EFI_STATUS
> +(EFIAPI *MM_IMAGE_ENTRY_POINT) (
> +  IN EFI_HANDLE            ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
> +  );
> +
> +typedef
> +EFI_STATUS
> +(EFIAPI *STANDALONE_MM_FOUNDATION_ENTRY_POINT) (
> +  IN VOID  *HobStart
> +  );
> +
> +#endif
> -- 
> 2.16.2
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 13/18] StandaloneMmPkg/Core: Implementation of Standalone MM Core Module.
  2018-04-30 19:19   ` Achin Gupta
@ 2018-04-30 19:28     ` Ard Biesheuvel
  2018-04-30 20:17       ` Achin Gupta
  2018-05-01  8:18       ` Laszlo Ersek
  2018-05-04 23:28     ` Supreeth Venkatesh
  1 sibling, 2 replies; 70+ messages in thread
From: Ard Biesheuvel @ 2018-04-30 19:28 UTC (permalink / raw)
  To: Achin Gupta, Laszlo Ersek, Kinney, Michael D, Leif Lindholm,
	Andrew Fish
  Cc: Supreeth Venkatesh, edk2-devel@lists.01.org, Gao, Liming,
	Yao, Jiewen, nd

On 30 April 2018 at 21:19, Achin Gupta <achin.gupta@arm.com> wrote:
> Hi Supreeth,
>
> I think it is worth adding a signed off by Jiewen since he originally
> contributed the code and it has not changed much since.

I disagree. A signoff does not assert authorship, it only means that
the contributor asserts that the license permits him to contribute
this code under the tianocore contribution agreement. Adding a signoff
on behalf of someone else should be avoided in my opinion, because it
suggests that code can only be contributed by the original author.
Also, even if the author made the code available under a compatible
license, it does not mean he subscribes to the Tianocore contribution
agreement, and adding a signoff on behalf of someone else does imply
that (although this should not be a problem in this particular case)

Anyone can contribute code that is available under a compatible
license, and it is not generally possible to decide who should be
credited as authors for code that originates in other projects.

If you want to credit the author, you can do that in the commit log.



> Please update the
> correct year in the copyright headers too.
>
> Acked-by: Achin Gupta <achin.gupta@arm.com>
>
> cheers,
> Achin
>
> On Fri, Apr 06, 2018 at 03:42:18PM +0100, Supreeth Venkatesh wrote:
>> Management Mode (MM) is a generic term used to describe a secure
>> execution environment provided by the CPU and related silicon that is
>> entered when the CPU detects a MMI. For x86 systems, this can be
>> implemented with System Management Mode (SMM). For ARM systems, this can
>> be implemented with TrustZone (TZ).
>> A MMI can be a CPU instruction or interrupt. Upon detection of a MMI, a
>> CPU will jump to the MM Entry Point and save some portion of its state
>> (the "save state") such that execution can be resumed.
>> The MMI can be generated synchronously by software or asynchronously by
>> a hardware event. Each MMI source can be detected, cleared and disabled.
>> Some systems provide for special memory (Management Mode RAM or MMRAM)
>> which is set aside for software running in MM. Usually the MMRAM is
>> hidden during normal CPU execution, but this is not required. Usually,
>> after MMRAM is hidden it cannot be exposed until the next system reset.
>>
>> The MM Core Interface Specification describes three pieces of the PI
>> Management Mode architecture:
>> 1. MM Dispatch
>>    During DXE, the DXE Foundation works with the MM Foundation to
>>    schedule MM drivers for execution in the discovered firmware volumes.
>> 2. MM Initialization
>>    MM related code opens MMRAM, creates the MMRAM memory map, and
>>    launches the MM Foundation, which provides the necessary services to
>>    launch MM-related drivers. Then, sometime before boot, MMRAM is
>>    closed and locked. This piece may be completed during the
>>    SEC, PEI or DXE phases.
>> 3. MMI Management
>>    When an MMI generated, the MM environment is created and then the MMI
>>
>>    sources are detected and MMI handlers called.
>>
>> This patch implements the MM Core.
>>
>> Contributed-under: TianoCore Contribution Agreement 1.1
>> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
>> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
>> ---
>>  StandaloneMmPkg/Core/Dependency.c                  |  389 +++++++
>>  StandaloneMmPkg/Core/Dispatcher.c                  | 1071 ++++++++++++++++++++
>>  StandaloneMmPkg/Core/FwVol.c                       |  104 ++
>>  StandaloneMmPkg/Core/Handle.c                      |  533 ++++++++++
>>  StandaloneMmPkg/Core/InstallConfigurationTable.c   |  178 ++++
>>  StandaloneMmPkg/Core/Locate.c                      |  496 +++++++++
>>  StandaloneMmPkg/Core/Mmi.c                         |  337 ++++++
>>  StandaloneMmPkg/Core/Notify.c                      |  203 ++++
>>  StandaloneMmPkg/Core/Page.c                        |  384 +++++++
>>  StandaloneMmPkg/Core/Pool.c                        |  287 ++++++
>>  StandaloneMmPkg/Core/StandaloneMmCore.c            |  708 +++++++++++++
>>  StandaloneMmPkg/Core/StandaloneMmCore.h            |  903 +++++++++++++++++
>>  StandaloneMmPkg/Core/StandaloneMmCore.inf          |   80 ++
>>  StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h |   66 ++
>>  StandaloneMmPkg/Include/Guid/MmFvDispatch.h        |   38 +
>>  StandaloneMmPkg/Include/StandaloneMm.h             |   36 +
>>  16 files changed, 5813 insertions(+)
>>  create mode 100644 StandaloneMmPkg/Core/Dependency.c
>>  create mode 100644 StandaloneMmPkg/Core/Dispatcher.c
>>  create mode 100644 StandaloneMmPkg/Core/FwVol.c
>>  create mode 100644 StandaloneMmPkg/Core/Handle.c
>>  create mode 100644 StandaloneMmPkg/Core/InstallConfigurationTable.c
>>  create mode 100644 StandaloneMmPkg/Core/Locate.c
>>  create mode 100644 StandaloneMmPkg/Core/Mmi.c
>>  create mode 100644 StandaloneMmPkg/Core/Notify.c
>>  create mode 100644 StandaloneMmPkg/Core/Page.c
>>  create mode 100644 StandaloneMmPkg/Core/Pool.c
>>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.c
>>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.h
>>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.inf
>>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
>>  create mode 100644 StandaloneMmPkg/Include/Guid/MmFvDispatch.h
>>  create mode 100644 StandaloneMmPkg/Include/StandaloneMm.h
>>
>> diff --git a/StandaloneMmPkg/Core/Dependency.c b/StandaloneMmPkg/Core/Dependency.c
>> new file mode 100644
>> index 0000000000..e501369130
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Core/Dependency.c
>> @@ -0,0 +1,389 @@
>> +/** @file
>> +  MM Driver Dispatcher Dependency Evaluator
>> +
>> +  This routine evaluates a dependency expression (DEPENDENCY_EXPRESSION) to determine
>> +  if a driver can be scheduled for execution.  The criteria for
>> +  schedulability is that the dependency expression is satisfied.
>> +
>> +  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
>> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +  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
>> +
>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +
>> +**/
>> +
>> +#include "StandaloneMmCore.h"
>> +
>> +///
>> +/// EFI_DEP_REPLACE_TRUE - Used to dynamically patch the dependency expression
>> +///                        to save time.  A EFI_DEP_PUSH is evaluated one an
>> +///                        replaced with EFI_DEP_REPLACE_TRUE. If PI spec's Vol 2
>> +///                        Driver Execution Environment Core Interface use 0xff
>> +///                        as new DEPEX opcode. EFI_DEP_REPLACE_TRUE should be
>> +///                        defined to a new value that is not conflicting with PI spec.
>> +///
>> +#define EFI_DEP_REPLACE_TRUE  0xff
>> +
>> +///
>> +/// Define the initial size of the dependency expression evaluation stack
>> +///
>> +#define DEPEX_STACK_SIZE_INCREMENT  0x1000
>> +
>> +//
>> +// Global stack used to evaluate dependency expressions
>> +//
>> +BOOLEAN  *mDepexEvaluationStack        = NULL;
>> +BOOLEAN  *mDepexEvaluationStackEnd     = NULL;
>> +BOOLEAN  *mDepexEvaluationStackPointer = NULL;
>> +
>> +/**
>> +  Grow size of the Depex stack
>> +
>> +  @retval EFI_SUCCESS           Stack successfully growed.
>> +  @retval EFI_OUT_OF_RESOURCES  There is not enough system memory to grow the stack.
>> +
>> +**/
>> +EFI_STATUS
>> +GrowDepexStack (
>> +  VOID
>> +  )
>> +{
>> +  BOOLEAN     *NewStack;
>> +  UINTN       Size;
>> +
>> +  Size = DEPEX_STACK_SIZE_INCREMENT;
>> +  if (mDepexEvaluationStack != NULL) {
>> +    Size = Size + (mDepexEvaluationStackEnd - mDepexEvaluationStack);
>> +  }
>> +
>> +  NewStack = AllocatePool (Size * sizeof (BOOLEAN));
>> +  if (NewStack == NULL) {
>> +    return EFI_OUT_OF_RESOURCES;
>> +  }
>> +
>> +  if (mDepexEvaluationStack != NULL) {
>> +    //
>> +    // Copy to Old Stack to the New Stack
>> +    //
>> +    CopyMem (
>> +      NewStack,
>> +      mDepexEvaluationStack,
>> +      (mDepexEvaluationStackEnd - mDepexEvaluationStack) * sizeof (BOOLEAN)
>> +      );
>> +
>> +    //
>> +    // Free The Old Stack
>> +    //
>> +    FreePool (mDepexEvaluationStack);
>> +  }
>> +
>> +  //
>> +  // Make the Stack pointer point to the old data in the new stack
>> +  //
>> +  mDepexEvaluationStackPointer = NewStack + (mDepexEvaluationStackPointer - mDepexEvaluationStack);
>> +  mDepexEvaluationStack        = NewStack;
>> +  mDepexEvaluationStackEnd     = NewStack + Size;
>> +
>> +  return EFI_SUCCESS;
>> +}
>> +
>> +/**
>> +  Push an element onto the Boolean Stack.
>> +
>> +  @param  Value                 BOOLEAN to push.
>> +
>> +  @retval EFI_SUCCESS           The value was pushed onto the stack.
>> +  @retval EFI_OUT_OF_RESOURCES  There is not enough system memory to grow the stack.
>> +
>> +**/
>> +EFI_STATUS
>> +PushBool (
>> +  IN BOOLEAN  Value
>> +  )
>> +{
>> +  EFI_STATUS  Status;
>> +
>> +  //
>> +  // Check for a stack overflow condition
>> +  //
>> +  if (mDepexEvaluationStackPointer == mDepexEvaluationStackEnd) {
>> +    //
>> +    // Grow the stack
>> +    //
>> +    Status = GrowDepexStack ();
>> +    if (EFI_ERROR (Status)) {
>> +      return Status;
>> +    }
>> +  }
>> +
>> +  //
>> +  // Push the item onto the stack
>> +  //
>> +  *mDepexEvaluationStackPointer = Value;
>> +  mDepexEvaluationStackPointer++;
>> +
>> +  return EFI_SUCCESS;
>> +}
>> +
>> +/**
>> +  Pop an element from the Boolean stack.
>> +
>> +  @param  Value                 BOOLEAN to pop.
>> +
>> +  @retval EFI_SUCCESS           The value was popped onto the stack.
>> +  @retval EFI_ACCESS_DENIED     The pop operation underflowed the stack.
>> +
>> +**/
>> +EFI_STATUS
>> +PopBool (
>> +  OUT BOOLEAN  *Value
>> +  )
>> +{
>> +  //
>> +  // Check for a stack underflow condition
>> +  //
>> +  if (mDepexEvaluationStackPointer == mDepexEvaluationStack) {
>> +    return EFI_ACCESS_DENIED;
>> +  }
>> +
>> +  //
>> +  // Pop the item off the stack
>> +  //
>> +  mDepexEvaluationStackPointer--;
>> +  *Value = *mDepexEvaluationStackPointer;
>> +  return EFI_SUCCESS;
>> +}
>> +
>> +/**
>> +  This is the POSTFIX version of the dependency evaluator.  This code does
>> +  not need to handle Before or After, as it is not valid to call this
>> +  routine in this case. POSTFIX means all the math is done on top of the stack.
>> +
>> +  @param  DriverEntry           DriverEntry element to update.
>> +
>> +  @retval TRUE                  If driver is ready to run.
>> +  @retval FALSE                 If driver is not ready to run or some fatal error
>> +                                was found.
>> +
>> +**/
>> +BOOLEAN
>> +MmIsSchedulable (
>> +  IN  EFI_MM_DRIVER_ENTRY   *DriverEntry
>> +  )
>> +{
>> +  EFI_STATUS  Status;
>> +  UINT8       *Iterator;
>> +  BOOLEAN     Operator;
>> +  BOOLEAN     Operator2;
>> +  EFI_GUID    DriverGuid;
>> +  VOID        *Interface;
>> +
>> +  Operator = FALSE;
>> +  Operator2 = FALSE;
>> +
>> +  if (DriverEntry->After || DriverEntry->Before) {
>> +    //
>> +    // If Before or After Depex skip as MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
>> +    // processes them.
>> +    //
>> +    return FALSE;
>> +  }
>> +
>> +  DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
>> +
>> +  if (DriverEntry->Depex == NULL) {
>> +    //
>> +    // A NULL Depex means that the MM driver is not built correctly.
>> +    // All MM drivers must have a valid depex expressiion.
>> +    //
>> +    DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Depex is empty)\n"));
>> +    ASSERT (FALSE);
>> +    return FALSE;
>> +  }
>> +
>> +  //
>> +  // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
>> +  // incorrectly formed DEPEX expressions
>> +  //
>> +  mDepexEvaluationStackPointer = mDepexEvaluationStack;
>> +
>> +
>> +  Iterator = DriverEntry->Depex;
>> +
>> +  while (TRUE) {
>> +    //
>> +    // Check to see if we are attempting to fetch dependency expression instructions
>> +    // past the end of the dependency expression.
>> +    //
>> +    if (((UINTN)Iterator - (UINTN)DriverEntry->Depex) >= DriverEntry->DepexSize) {
>> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Attempt to fetch past end of depex)\n"));
>> +      return FALSE;
>> +    }
>> +
>> +    //
>> +    // Look at the opcode of the dependency expression instruction.
>> +    //
>> +    switch (*Iterator) {
>> +    case EFI_DEP_BEFORE:
>> +    case EFI_DEP_AFTER:
>> +      //
>> +      // For a well-formed Dependency Expression, the code should never get here.
>> +      // The BEFORE and AFTER are processed prior to this routine's invocation.
>> +      // If the code flow arrives at this point, there was a BEFORE or AFTER
>> +      // that were not the first opcodes.
>> +      //
>> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));
>> +      ASSERT (FALSE);
>> +
>> +    case EFI_DEP_PUSH:
>> +      //
>> +      // Push operator is followed by a GUID. Test to see if the GUID protocol
>> +      // is installed and push the boolean result on the stack.
>> +      //
>> +      CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));
>> +
>> +      Status = MmLocateProtocol (&DriverGuid, NULL, &Interface);
>> +      if (EFI_ERROR (Status) && (mEfiSystemTable != NULL)) {
>> +        //
>> +        // For MM Driver, it may depend on uefi protocols
>> +        //
>> +        Status = mEfiSystemTable->BootServices->LocateProtocol (&DriverGuid, NULL, &Interface);
>> +      }
>> +
>> +      if (EFI_ERROR (Status)) {
>> +        DEBUG ((DEBUG_DISPATCH, "  PUSH GUID(%g) = FALSE\n", &DriverGuid));
>> +        Status = PushBool (FALSE);
>> +      } else {
>> +        DEBUG ((DEBUG_DISPATCH, "  PUSH GUID(%g) = TRUE\n", &DriverGuid));
>> +        *Iterator = EFI_DEP_REPLACE_TRUE;
>> +        Status = PushBool (TRUE);
>> +      }
>> +      if (EFI_ERROR (Status)) {
>> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
>> +        return FALSE;
>> +      }
>> +
>> +      Iterator += sizeof (EFI_GUID);
>> +      break;
>> +
>> +    case EFI_DEP_AND:
>> +      DEBUG ((DEBUG_DISPATCH, "  AND\n"));
>> +      Status = PopBool (&Operator);
>> +      if (EFI_ERROR (Status)) {
>> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
>> +        return FALSE;
>> +      }
>> +
>> +      Status = PopBool (&Operator2);
>> +      if (EFI_ERROR (Status)) {
>> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
>> +        return FALSE;
>> +      }
>> +
>> +      Status = PushBool ((BOOLEAN)(Operator && Operator2));
>> +      if (EFI_ERROR (Status)) {
>> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
>> +        return FALSE;
>> +      }
>> +      break;
>> +
>> +    case EFI_DEP_OR:
>> +      DEBUG ((DEBUG_DISPATCH, "  OR\n"));
>> +      Status = PopBool (&Operator);
>> +      if (EFI_ERROR (Status)) {
>> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
>> +        return FALSE;
>> +      }
>> +
>> +      Status = PopBool (&Operator2);
>> +      if (EFI_ERROR (Status)) {
>> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
>> +        return FALSE;
>> +      }
>> +
>> +      Status = PushBool ((BOOLEAN)(Operator || Operator2));
>> +      if (EFI_ERROR (Status)) {
>> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
>> +        return FALSE;
>> +      }
>> +      break;
>> +
>> +    case EFI_DEP_NOT:
>> +      DEBUG ((DEBUG_DISPATCH, "  NOT\n"));
>> +      Status = PopBool (&Operator);
>> +      if (EFI_ERROR (Status)) {
>> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
>> +        return FALSE;
>> +      }
>> +
>> +      Status = PushBool ((BOOLEAN)(!Operator));
>> +      if (EFI_ERROR (Status)) {
>> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
>> +        return FALSE;
>> +      }
>> +      break;
>> +
>> +    case EFI_DEP_TRUE:
>> +      DEBUG ((DEBUG_DISPATCH, "  TRUE\n"));
>> +      Status = PushBool (TRUE);
>> +      if (EFI_ERROR (Status)) {
>> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
>> +        return FALSE;
>> +      }
>> +      break;
>> +
>> +    case EFI_DEP_FALSE:
>> +      DEBUG ((DEBUG_DISPATCH, "  FALSE\n"));
>> +      Status = PushBool (FALSE);
>> +      if (EFI_ERROR (Status)) {
>> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
>> +        return FALSE;
>> +      }
>> +      break;
>> +
>> +    case EFI_DEP_END:
>> +      DEBUG ((DEBUG_DISPATCH, "  END\n"));
>> +      Status = PopBool (&Operator);
>> +      if (EFI_ERROR (Status)) {
>> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
>> +        return FALSE;
>> +      }
>> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = %a\n", Operator ? "TRUE" : "FALSE"));
>> +      return Operator;
>> +
>> +    case EFI_DEP_REPLACE_TRUE:
>> +      CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));
>> +      DEBUG ((DEBUG_DISPATCH, "  PUSH GUID(%g) = TRUE\n", &DriverGuid));
>> +      Status = PushBool (TRUE);
>> +      if (EFI_ERROR (Status)) {
>> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
>> +        return FALSE;
>> +      }
>> +
>> +      Iterator += sizeof (EFI_GUID);
>> +      break;
>> +
>> +    default:
>> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unknown opcode)\n"));
>> +      goto Done;
>> +    }
>> +
>> +    //
>> +    // Skip over the Dependency Op Code we just processed in the switch.
>> +    // The math is done out of order, but it should not matter. That is
>> +    // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
>> +    // This is not an issue, since we just need the correct end result. You
>> +    // need to be careful using Iterator in the loop as it's intermediate value
>> +    // may be strange.
>> +    //
>> +    Iterator++;
>> +  }
>> +
>> +Done:
>> +  return FALSE;
>> +}
>> diff --git a/StandaloneMmPkg/Core/Dispatcher.c b/StandaloneMmPkg/Core/Dispatcher.c
>> new file mode 100644
>> index 0000000000..af18fa7eaa
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Core/Dispatcher.c
>> @@ -0,0 +1,1071 @@
>> +/** @file
>> +  MM Driver Dispatcher.
>> +
>> +  Step #1 - When a FV protocol is added to the system every driver in the FV
>> +            is added to the mDiscoveredList. The Before, and After Depex are
>> +            pre-processed as drivers are added to the mDiscoveredList. If an Apriori
>> +            file exists in the FV those drivers are addeded to the
>> +            mScheduledQueue. The mFvHandleList is used to make sure a
>> +            FV is only processed once.
>> +
>> +  Step #2 - Dispatch. Remove driver from the mScheduledQueue and load and
>> +            start it. After mScheduledQueue is drained check the
>> +            mDiscoveredList to see if any item has a Depex that is ready to
>> +            be placed on the mScheduledQueue.
>> +
>> +  Step #3 - Adding to the mScheduledQueue requires that you process Before
>> +            and After dependencies. This is done recursively as the call to add
>> +            to the mScheduledQueue checks for Before and recursively adds
>> +            all Befores. It then addes the item that was passed in and then
>> +            processess the After dependecies by recursively calling the routine.
>> +
>> +  Dispatcher Rules:
>> +  The rules for the dispatcher are similar to the DXE dispatcher.
>> +
>> +  The rules for DXE dispatcher are in chapter 10 of the DXE CIS. Figure 10-3
>> +  is the state diagram for the DXE dispatcher
>> +
>> +  Depex - Dependency Expresion.
>> +
>> +  Copyright (c) 2014, Hewlett-Packard Development Company, L.P.
>> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
>> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +
>> +  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
>> +
>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +
>> +**/
>> +
>> +#include "StandaloneMmCore.h"
>> +
>> +//
>> +// MM Dispatcher Data structures
>> +//
>> +#define KNOWN_HANDLE_SIGNATURE  SIGNATURE_32('k','n','o','w')
>> +typedef struct {
>> +  UINTN           Signature;
>> +  LIST_ENTRY      Link;         // mFvHandleList
>> +  EFI_HANDLE      Handle;
>> +} KNOWN_HANDLE;
>> +
>> +//
>> +// Function Prototypes
>> +//
>> +
>> +EFI_STATUS
>> +MmCoreFfsFindMmDriver (
>> +  IN  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader
>> +  );
>> +
>> +/**
>> +  Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
>> +  must add any driver with a before dependency on InsertedDriverEntry first.
>> +  You do this by recursively calling this routine. After all the Befores are
>> +  processed you can add InsertedDriverEntry to the mScheduledQueue.
>> +  Then you can add any driver with an After dependency on InsertedDriverEntry
>> +  by recursively calling this routine.
>> +
>> +  @param  InsertedDriverEntry   The driver to insert on the ScheduledLink Queue
>> +
>> +**/
>> +VOID
>> +MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
>> +  IN  EFI_MM_DRIVER_ENTRY   *InsertedDriverEntry
>> +  );
>> +
>> +//
>> +// The Driver List contains one copy of every driver that has been discovered.
>> +// Items are never removed from the driver list. List of EFI_MM_DRIVER_ENTRY
>> +//
>> +LIST_ENTRY  mDiscoveredList = INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList);
>> +
>> +//
>> +// Queue of drivers that are ready to dispatch. This queue is a subset of the
>> +// mDiscoveredList.list of EFI_MM_DRIVER_ENTRY.
>> +//
>> +LIST_ENTRY  mScheduledQueue = INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue);
>> +
>> +//
>> +// List of handles who's Fv's have been parsed and added to the mFwDriverList.
>> +//
>> +LIST_ENTRY  mFvHandleList = INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList);
>> +
>> +//
>> +// Flag for the MM Dispacher.  TRUE if dispatcher is execuing.
>> +//
>> +BOOLEAN  gDispatcherRunning = FALSE;
>> +
>> +//
>> +// Flag for the MM Dispacher.  TRUE if there is one or more MM drivers ready to be dispatched
>> +//
>> +BOOLEAN  gRequestDispatch = FALSE;
>> +
>> +//
>> +// The global variable is defined for Loading modules at fixed address feature to track the MM code
>> +// memory range usage. It is a bit mapped array in which every bit indicates the correspoding
>> +// memory page available or not.
>> +//
>> +GLOBAL_REMOVE_IF_UNREFERENCED    UINT64                *mMmCodeMemoryRangeUsageBitMap=NULL;
>> +
>> +/**
>> +  To check memory usage bit map array to figure out if the memory range in which the image will be loaded is available or not. If
>> +  memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.
>> +  The function is only invoked when load modules at fixed address feature is enabled.
>> +
>> +  @param  ImageBase                The base addres the image will be loaded at.
>> +  @param  ImageSize                The size of the image
>> +
>> +  @retval EFI_SUCCESS              The memory range the image will be loaded in is available
>> +  @retval EFI_NOT_FOUND            The memory range the image will be loaded in is not available
>> +**/
>> +EFI_STATUS
>> +CheckAndMarkFixLoadingMemoryUsageBitMap (
>> +  IN  EFI_PHYSICAL_ADDRESS          ImageBase,
>> +  IN  UINTN                         ImageSize
>> +  )
>> +{
>> +   UINT32                             MmCodePageNumber;
>> +   UINT64                             MmCodeSize;
>> +   EFI_PHYSICAL_ADDRESS               MmCodeBase;
>> +   UINTN                              BaseOffsetPageNumber;
>> +   UINTN                              TopOffsetPageNumber;
>> +   UINTN                              Index;
>> +   //
>> +   // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressMmCodePageNumber
>> +   //
>> +   MmCodePageNumber = 0;
>> +   MmCodeSize = EFI_PAGES_TO_SIZE (MmCodePageNumber);
>> +   MmCodeBase = gLoadModuleAtFixAddressMmramBase;
>> +
>> +   //
>> +   // If the memory usage bit map is not initialized,  do it. Every bit in the array
>> +   // indicate the status of the corresponding memory page, available or not
>> +   //
>> +   if (mMmCodeMemoryRangeUsageBitMap == NULL) {
>> +     mMmCodeMemoryRangeUsageBitMap = AllocateZeroPool(((MmCodePageNumber / 64) + 1)*sizeof(UINT64));
>> +   }
>> +   //
>> +   // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
>> +   //
>> +   if (mMmCodeMemoryRangeUsageBitMap == NULL) {
>> +     return EFI_NOT_FOUND;
>> +   }
>> +   //
>> +   // see if the memory range for loading the image is in the MM code range.
>> +   //
>> +   if (MmCodeBase + MmCodeSize <  ImageBase + ImageSize || MmCodeBase >  ImageBase) {
>> +     return EFI_NOT_FOUND;
>> +   }
>> +   //
>> +   // Test if the memory is avalaible or not.
>> +   //
>> +   BaseOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase - MmCodeBase));
>> +   TopOffsetPageNumber  = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - MmCodeBase));
>> +   for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
>> +     if ((mMmCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {
>> +       //
>> +       // This page is already used.
>> +       //
>> +       return EFI_NOT_FOUND;
>> +     }
>> +   }
>> +
>> +   //
>> +   // Being here means the memory range is available.  So mark the bits for the memory range
>> +   //
>> +   for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
>> +     mMmCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));
>> +   }
>> +   return  EFI_SUCCESS;
>> +}
>> +/**
>> +  Get the fixed loading address from image header assigned by build tool. This function only be called
>> +  when Loading module at Fixed address feature enabled.
>> +
>> +  @param  ImageContext              Pointer to the image context structure that describes the PE/COFF
>> +                                    image that needs to be examined by this function.
>> +  @retval EFI_SUCCESS               An fixed loading address is assigned to this image by build tools .
>> +  @retval EFI_NOT_FOUND             The image has no assigned fixed loadding address.
>> +
>> +**/
>> +EFI_STATUS
>> +GetPeCoffImageFixLoadingAssignedAddress(
>> +  IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext
>> +  )
>> +{
>> +  UINTN                              SectionHeaderOffset;
>> +  EFI_STATUS                         Status;
>> +  EFI_IMAGE_SECTION_HEADER           SectionHeader;
>> +  EFI_IMAGE_OPTIONAL_HEADER_UNION    *ImgHdr;
>> +  EFI_PHYSICAL_ADDRESS               FixLoadingAddress;
>> +  UINT16                             Index;
>> +  UINTN                              Size;
>> +  UINT16                             NumberOfSections;
>> +  UINT64                             ValueInSectionHeader;
>> +
>> +  FixLoadingAddress = 0;
>> +  Status = EFI_NOT_FOUND;
>> +
>> +  //
>> +  // Get PeHeader pointer
>> +  //
>> +  ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
>> +  SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +
>> +                        sizeof (UINT32) +
>> +                        sizeof (EFI_IMAGE_FILE_HEADER) +
>> +                        ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;
>> +  NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
>> +
>> +  //
>> +  // Get base address from the first section header that doesn't point to code section.
>> +  //
>> +  for (Index = 0; Index < NumberOfSections; Index++) {
>> +    //
>> +    // Read section header from file
>> +    //
>> +    Size = sizeof (EFI_IMAGE_SECTION_HEADER);
>> +    Status = ImageContext->ImageRead (
>> +                              ImageContext->Handle,
>> +                              SectionHeaderOffset,
>> +                              &Size,
>> +                              &SectionHeader
>> +                              );
>> +    if (EFI_ERROR (Status)) {
>> +      return Status;
>> +    }
>> +
>> +    Status = EFI_NOT_FOUND;
>> +
>> +    if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
>> +      //
>> +      // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
>> +      // that doesn't point to code section in image header.So there is an assumption that when the feature is enabled,
>> +      // if a module with a loading address assigned by tools, the PointerToRelocations & PointerToLineNumbers fields
>> +      // should not be Zero, or else, these 2 fields should be set to Zero
>> +      //
>> +      ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);
>> +      if (ValueInSectionHeader != 0) {
>> +        //
>> +        // Found first section header that doesn't point to code section in which build tool saves the
>> +        // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
>> +        //
>> +        FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressMmramBase + (INT64)ValueInSectionHeader);
>> +        //
>> +        // Check if the memory range is available.
>> +        //
>> +        Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));
>> +        if (!EFI_ERROR(Status)) {
>> +          //
>> +          // The assigned address is valid. Return the specified loading address
>> +          //
>> +          ImageContext->ImageAddress = FixLoadingAddress;
>> +        }
>> +      }
>> +      break;
>> +    }
>> +    SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
>> +  }
>> +  DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n", FixLoadingAddress, Status));
>> +  return Status;
>> +}
>> +/**
>> +  Loads an EFI image into SMRAM.
>> +
>> +  @param  DriverEntry             EFI_MM_DRIVER_ENTRY instance
>> +
>> +  @return EFI_STATUS
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmLoadImage (
>> +  IN OUT EFI_MM_DRIVER_ENTRY  *DriverEntry
>> +  )
>> +{
>> +  VOID                           *Buffer;
>> +  UINTN                          PageCount;
>> +  EFI_STATUS                     Status;
>> +  EFI_PHYSICAL_ADDRESS           DstBuffer;
>> +  PE_COFF_LOADER_IMAGE_CONTEXT   ImageContext;
>> +
>> +  DEBUG ((DEBUG_INFO, "MmLoadImage - %g\n", &DriverEntry->FileName));
>> +
>> +  Buffer = AllocateCopyPool (DriverEntry->Pe32DataSize, DriverEntry->Pe32Data);
>> +  if (Buffer == NULL) {
>> +    return EFI_OUT_OF_RESOURCES;
>> +  }
>> +
>> +  Status               = EFI_SUCCESS;
>> +
>> +  //
>> +  // Initialize ImageContext
>> +  //
>> +  ImageContext.Handle = Buffer;
>> +  ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
>> +
>> +  //
>> +  // Get information about the image being loaded
>> +  //
>> +  Status = PeCoffLoaderGetImageInfo (&ImageContext);
>> +  if (EFI_ERROR (Status)) {
>> +    if (Buffer != NULL) {
>> +      MmFreePool (Buffer);
>> +    }
>> +    return Status;
>> +  }
>> +
>> +  PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
>> +  DstBuffer = (UINTN)(-1);
>> +
>> +  Status = MmAllocatePages (
>> +              AllocateMaxAddress,
>> +              EfiRuntimeServicesCode,
>> +              PageCount,
>> +              &DstBuffer
>> +              );
>> +  if (EFI_ERROR (Status)) {
>> +    if (Buffer != NULL) {
>> +      MmFreePool (Buffer);
>> +    }
>> +    return Status;
>> +  }
>> +
>> +  ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;
>> +
>> +  //
>> +  // Align buffer on section boundry
>> +  //
>> +  ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
>> +  ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)(ImageContext.SectionAlignment - 1));
>> +
>> +  //
>> +  // Load the image to our new buffer
>> +  //
>> +  Status = PeCoffLoaderLoadImage (&ImageContext);
>> +  if (EFI_ERROR (Status)) {
>> +    if (Buffer != NULL) {
>> +      MmFreePool (Buffer);
>> +    }
>> +    MmFreePages (DstBuffer, PageCount);
>> +    return Status;
>> +  }
>> +
>> +  //
>> +  // Relocate the image in our new buffer
>> +  //
>> +  Status = PeCoffLoaderRelocateImage (&ImageContext);
>> +  if (EFI_ERROR (Status)) {
>> +    if (Buffer != NULL) {
>> +      MmFreePool (Buffer);
>> +    }
>> +    MmFreePages (DstBuffer, PageCount);
>> +    return Status;
>> +  }
>> +
>> +  //
>> +  // Flush the instruction cache so the image data are written before we execute it
>> +  //
>> +  InvalidateInstructionCacheRange ((VOID *)(UINTN) ImageContext.ImageAddress, (UINTN) ImageContext.ImageSize);
>> +
>> +  //
>> +  // Save Image EntryPoint in DriverEntry
>> +  //
>> +  DriverEntry->ImageEntryPoint  = ImageContext.EntryPoint;
>> +  DriverEntry->ImageBuffer      = DstBuffer;
>> +  DriverEntry->NumberOfPage     = PageCount;
>> +
>> +  if (mEfiSystemTable != NULL) {
>> +    Status = mEfiSystemTable->BootServices->AllocatePool (EfiBootServicesData, sizeof (EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&DriverEntry->LoadedImage);
>> +    if (EFI_ERROR (Status)) {
>> +      if (Buffer != NULL) {
>> +        MmFreePool (Buffer);
>> +      }
>> +      MmFreePages (DstBuffer, PageCount);
>> +      return Status;
>> +    }
>> +
>> +    ZeroMem (DriverEntry->LoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));
>> +    //
>> +    // Fill in the remaining fields of the Loaded Image Protocol instance.
>> +    // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
>> +    //
>> +    DriverEntry->LoadedImage->Revision      = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
>> +    DriverEntry->LoadedImage->ParentHandle  = NULL;
>> +    DriverEntry->LoadedImage->SystemTable   = mEfiSystemTable;
>> +    DriverEntry->LoadedImage->DeviceHandle  = NULL;
>> +    DriverEntry->LoadedImage->FilePath      = NULL;
>> +
>> +    DriverEntry->LoadedImage->ImageBase     = (VOID *)(UINTN)DriverEntry->ImageBuffer;
>> +    DriverEntry->LoadedImage->ImageSize     = ImageContext.ImageSize;
>> +    DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;
>> +    DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;
>> +
>> +    //
>> +    // Create a new image handle in the UEFI handle database for the MM Driver
>> +    //
>> +    DriverEntry->ImageHandle = NULL;
>> +    Status = mEfiSystemTable->BootServices->InstallMultipleProtocolInterfaces (
>> +                    &DriverEntry->ImageHandle,
>> +                    &gEfiLoadedImageProtocolGuid, DriverEntry->LoadedImage,
>> +                    NULL
>> +                    );
>> +  }
>> +
>> +  //
>> +  // Print the load address and the PDB file name if it is available
>> +  //
>> +
>> +  DEBUG_CODE_BEGIN ();
>> +
>> +    UINTN Index;
>> +    UINTN StartIndex;
>> +    CHAR8 EfiFileName[256];
>> +
>> +
>> +    DEBUG ((DEBUG_INFO | DEBUG_LOAD,
>> +           "Loading MM driver at 0x%11p EntryPoint=0x%11p ",
>> +           (VOID *)(UINTN) ImageContext.ImageAddress,
>> +           FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));
>> +
>> +
>> +    //
>> +    // Print Module Name by Pdb file path.
>> +    // Windows and Unix style file path are all trimmed correctly.
>> +    //
>> +    if (ImageContext.PdbPointer != NULL) {
>> +      StartIndex = 0;
>> +      for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {
>> +        if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {
>> +          StartIndex = Index + 1;
>> +        }
>> +      }
>> +      //
>> +      // Copy the PDB file name to our temporary string, and replace .pdb with .efi
>> +      // The PDB file name is limited in the range of 0~255.
>> +      // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
>> +      //
>> +      for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {
>> +        EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];
>> +        if (EfiFileName[Index] == 0) {
>> +          EfiFileName[Index] = '.';
>> +        }
>> +        if (EfiFileName[Index] == '.') {
>> +          EfiFileName[Index + 1] = 'e';
>> +          EfiFileName[Index + 2] = 'f';
>> +          EfiFileName[Index + 3] = 'i';
>> +          EfiFileName[Index + 4] = 0;
>> +          break;
>> +        }
>> +      }
>> +
>> +      if (Index == sizeof (EfiFileName) - 4) {
>> +        EfiFileName[Index] = 0;
>> +      }
>> +      DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));
>> +    }
>> +    DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));
>> +
>> +  DEBUG_CODE_END ();
>> +
>> +  //
>> +  // Free buffer allocated by Fv->ReadSection.
>> +  //
>> +  // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
>> +  // used the UEFI Boot Services AllocatePool() function
>> +  //
>> +  MmFreePool(Buffer);
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Preprocess dependency expression and update DriverEntry to reflect the
>> +  state of  Before and After dependencies. If DriverEntry->Before
>> +  or DriverEntry->After is set it will never be cleared.
>> +
>> +  @param  DriverEntry           DriverEntry element to update .
>> +
>> +  @retval EFI_SUCCESS           It always works.
>> +
>> +**/
>> +EFI_STATUS
>> +MmPreProcessDepex (
>> +  IN EFI_MM_DRIVER_ENTRY  *DriverEntry
>> +  )
>> +{
>> +  UINT8  *Iterator;
>> +
>> +  Iterator = DriverEntry->Depex;
>> +  DriverEntry->Dependent = TRUE;
>> +
>> +  if (*Iterator == EFI_DEP_BEFORE) {
>> +    DriverEntry->Before = TRUE;
>> +  } else if (*Iterator == EFI_DEP_AFTER) {
>> +    DriverEntry->After = TRUE;
>> +  }
>> +
>> +  if (DriverEntry->Before || DriverEntry->After) {
>> +    CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));
>> +  }
>> +
>> +  return EFI_SUCCESS;
>> +}
>> +
>> +/**
>> +  Read Depex and pre-process the Depex for Before and After. If Section Extraction
>> +  protocol returns an error via ReadSection defer the reading of the Depex.
>> +
>> +  @param  DriverEntry           Driver to work on.
>> +
>> +  @retval EFI_SUCCESS           Depex read and preprossesed
>> +  @retval EFI_PROTOCOL_ERROR    The section extraction protocol returned an error
>> +                                and  Depex reading needs to be retried.
>> +  @retval Error                 DEPEX not found.
>> +
>> +**/
>> +EFI_STATUS
>> +MmGetDepexSectionAndPreProccess (
>> +  IN EFI_MM_DRIVER_ENTRY  *DriverEntry
>> +  )
>> +{
>> +  EFI_STATUS                     Status;
>> +
>> +  //
>> +  // Data already read
>> +  //
>> +  if (DriverEntry->Depex == NULL) {
>> +    Status = EFI_NOT_FOUND;
>> +  } else {
>> +    Status = EFI_SUCCESS;
>> +  }
>> +  if (EFI_ERROR (Status)) {
>> +    if (Status == EFI_PROTOCOL_ERROR) {
>> +      //
>> +      // The section extraction protocol failed so set protocol error flag
>> +      //
>> +      DriverEntry->DepexProtocolError = TRUE;
>> +    } else {
>> +      //
>> +      // If no Depex assume depend on all architectural protocols
>> +      //
>> +      DriverEntry->Depex = NULL;
>> +      DriverEntry->Dependent = TRUE;
>> +      DriverEntry->DepexProtocolError = FALSE;
>> +    }
>> +  } else {
>> +    //
>> +    // Set Before and After state information based on Depex
>> +    // Driver will be put in Dependent state
>> +    //
>> +    MmPreProcessDepex (DriverEntry);
>> +    DriverEntry->DepexProtocolError = FALSE;
>> +  }
>> +
>> +  return Status;
>> +}
>> +
>> +/**
>> +  This is the main Dispatcher for MM and it exits when there are no more
>> +  drivers to run. Drain the mScheduledQueue and load and start a PE
>> +  image for each driver. Search the mDiscoveredList to see if any driver can
>> +  be placed on the mScheduledQueue. If no drivers are placed on the
>> +  mScheduledQueue exit the function.
>> +
>> +  @retval EFI_SUCCESS           All of the MM Drivers that could be dispatched
>> +                                have been run and the MM Entry Point has been
>> +                                registered.
>> +  @retval EFI_NOT_READY         The MM Driver that registered the MM Entry Point
>> +                                was just dispatched.
>> +  @retval EFI_NOT_FOUND         There are no MM Drivers available to be dispatched.
>> +  @retval EFI_ALREADY_STARTED   The MM Dispatcher is already running
>> +
>> +**/
>> +EFI_STATUS
>> +MmDispatcher (
>> +  VOID
>> +  )
>> +{
>> +  EFI_STATUS            Status;
>> +  LIST_ENTRY            *Link;
>> +  EFI_MM_DRIVER_ENTRY  *DriverEntry;
>> +  BOOLEAN               ReadyToRun;
>> +  BOOLEAN               PreviousMmEntryPointRegistered;
>> +
>> +  DEBUG ((DEBUG_INFO, "MmDispatcher\n"));
>> +
>> +  if (!gRequestDispatch) {
>> +    DEBUG ((DEBUG_INFO, "  !gRequestDispatch\n"));
>> +    return EFI_NOT_FOUND;
>> +  }
>> +
>> +  if (gDispatcherRunning) {
>> +    DEBUG ((DEBUG_INFO, "  gDispatcherRunning\n"));
>> +    //
>> +    // If the dispatcher is running don't let it be restarted.
>> +    //
>> +    return EFI_ALREADY_STARTED;
>> +  }
>> +
>> +  gDispatcherRunning = TRUE;
>> +
>> +  do {
>> +    //
>> +    // Drain the Scheduled Queue
>> +    //
>> +    DEBUG ((DEBUG_INFO, "  Drain the Scheduled Queue\n"));
>> +    while (!IsListEmpty (&mScheduledQueue)) {
>> +      DriverEntry = CR (
>> +                      mScheduledQueue.ForwardLink,
>> +                      EFI_MM_DRIVER_ENTRY,
>> +                      ScheduledLink,
>> +                      EFI_MM_DRIVER_ENTRY_SIGNATURE
>> +                      );
>> +      DEBUG ((DEBUG_INFO, "  DriverEntry (Scheduled) - %g\n", &DriverEntry->FileName));
>> +
>> +      //
>> +      // Load the MM Driver image into memory. If the Driver was transitioned from
>> +      // Untrused to Scheduled it would have already been loaded so we may need to
>> +      // skip the LoadImage
>> +      //
>> +      if (DriverEntry->ImageHandle == NULL) {
>> +        Status = MmLoadImage (DriverEntry);
>> +
>> +        //
>> +        // Update the driver state to reflect that it's been loaded
>> +        //
>> +        if (EFI_ERROR (Status)) {
>> +          //
>> +          // The MM Driver could not be loaded, and do not attempt to load or start it again.
>> +          // Take driver from Scheduled to Initialized.
>> +          //
>> +          DriverEntry->Initialized  = TRUE;
>> +          DriverEntry->Scheduled = FALSE;
>> +          RemoveEntryList (&DriverEntry->ScheduledLink);
>> +
>> +          //
>> +          // If it's an error don't try the StartImage
>> +          //
>> +          continue;
>> +        }
>> +      }
>> +
>> +      DriverEntry->Scheduled    = FALSE;
>> +      DriverEntry->Initialized  = TRUE;
>> +      RemoveEntryList (&DriverEntry->ScheduledLink);
>> +
>> +      /*REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
>> +        EFI_PROGRESS_CODE,
>> +        EFI_SOFTWARE_MM_DRIVER | EFI_SW_PC_INIT_BEGIN,
>> +        &DriverEntry->ImageHandle,
>> +        sizeof (DriverEntry->ImageHandle)
>> +        );*/
>> +
>> +      //
>> +      // Cache state of MmEntryPointRegistered before calling entry point
>> +      //
>> +      PreviousMmEntryPointRegistered = gMmCorePrivate->MmEntryPointRegistered;
>> +
>> +      //
>> +      // For each MM driver, pass NULL as ImageHandle
>> +      //
>> +      if (mEfiSystemTable == NULL) {
>> +        DEBUG ((DEBUG_INFO, "StartImage - 0x%x (Standalone Mode)\n", DriverEntry->ImageEntryPoint));
>> +        Status = ((MM_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, &gMmCoreMmst);
>> +      } else {
>> +        DEBUG ((DEBUG_INFO, "StartImage - 0x%x (Tradition Mode)\n", DriverEntry->ImageEntryPoint));
>> +        Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, mEfiSystemTable);
>> +      }
>> +      if (EFI_ERROR(Status)){
>> +        DEBUG ((DEBUG_INFO, "StartImage Status - %r\n", Status));
>> +        MmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);
>> +      }
>> +
>> +      /*REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
>> +        EFI_PROGRESS_CODE,
>> +        EFI_SOFTWARE_MM_DRIVER | EFI_SW_PC_INIT_END,
>> +        &DriverEntry->ImageHandle,
>> +        sizeof (DriverEntry->ImageHandle)
>> +        );*/
>> +
>> +      if (!PreviousMmEntryPointRegistered && gMmCorePrivate->MmEntryPointRegistered) {
>> +        //
>> +        // Return immediately if the MM Entry Point was registered by the MM
>> +        // Driver that was just dispatched.  The MM IPL will reinvoke the MM
>> +        // Core Dispatcher.  This is required so MM Mode may be enabled as soon
>> +        // as all the dependent MM Drivers for MM Mode have been dispatched.
>> +        // Once the MM Entry Point has been registered, then MM Mode will be
>> +        // used.
>> +        //
>> +        gRequestDispatch = TRUE;
>> +        gDispatcherRunning = FALSE;
>> +        return EFI_NOT_READY;
>> +      }
>> +    }
>> +
>> +    //
>> +    // Search DriverList for items to place on Scheduled Queue
>> +    //
>> +    DEBUG ((DEBUG_INFO, "  Search DriverList for items to place on Scheduled Queue\n"));
>> +    ReadyToRun = FALSE;
>> +    for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
>> +      DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
>> +      DEBUG ((DEBUG_INFO, "  DriverEntry (Discovered) - %g\n", &DriverEntry->FileName));
>> +
>> +      if (DriverEntry->DepexProtocolError){
>> +        //
>> +        // If Section Extraction Protocol did not let the Depex be read before retry the read
>> +        //
>> +        Status = MmGetDepexSectionAndPreProccess (DriverEntry);
>> +      }
>> +
>> +      if (DriverEntry->Dependent) {
>> +        if (MmIsSchedulable (DriverEntry)) {
>> +          MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
>> +          ReadyToRun = TRUE;
>> +        }
>> +      }
>> +    }
>> +  } while (ReadyToRun);
>> +
>> +  //
>> +  // If there is no more MM driver to dispatch, stop the dispatch request
>> +  //
>> +  DEBUG ((DEBUG_INFO, "  no more MM driver to dispatch, stop the dispatch request\n"));
>> +  gRequestDispatch = FALSE;
>> +  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
>> +    DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
>> +    DEBUG ((DEBUG_INFO, "  DriverEntry (Discovered) - %g\n", &DriverEntry->FileName));
>> +
>> +    if (!DriverEntry->Initialized){
>> +      //
>> +      // We have MM driver pending to dispatch
>> +      //
>> +      gRequestDispatch = TRUE;
>> +      break;
>> +    }
>> +  }
>> +
>> +  gDispatcherRunning = FALSE;
>> +
>> +  return EFI_SUCCESS;
>> +}
>> +
>> +/**
>> +  Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
>> +  must add any driver with a before dependency on InsertedDriverEntry first.
>> +  You do this by recursively calling this routine. After all the Befores are
>> +  processed you can add InsertedDriverEntry to the mScheduledQueue.
>> +  Then you can add any driver with an After dependency on InsertedDriverEntry
>> +  by recursively calling this routine.
>> +
>> +  @param  InsertedDriverEntry   The driver to insert on the ScheduledLink Queue
>> +
>> +**/
>> +VOID
>> +MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
>> +  IN  EFI_MM_DRIVER_ENTRY   *InsertedDriverEntry
>> +  )
>> +{
>> +  LIST_ENTRY            *Link;
>> +  EFI_MM_DRIVER_ENTRY *DriverEntry;
>> +
>> +  //
>> +  // Process Before Dependency
>> +  //
>> +  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
>> +    DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
>> +    if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
>> +      DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
>> +      DEBUG ((DEBUG_DISPATCH, "  BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
>> +      if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
>> +        //
>> +        // Recursively process BEFORE
>> +        //
>> +        DEBUG ((DEBUG_DISPATCH, "TRUE\n  END\n  RESULT = TRUE\n"));
>> +        MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
>> +      } else {
>> +        DEBUG ((DEBUG_DISPATCH, "FALSE\n  END\n  RESULT = FALSE\n"));
>> +      }
>> +    }
>> +  }
>> +
>> +  //
>> +  // Convert driver from Dependent to Scheduled state
>> +  //
>> +
>> +  InsertedDriverEntry->Dependent = FALSE;
>> +  InsertedDriverEntry->Scheduled = TRUE;
>> +  InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);
>> +
>> +
>> +  //
>> +  // Process After Dependency
>> +  //
>> +  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
>> +    DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
>> +    if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
>> +      DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
>> +      DEBUG ((DEBUG_DISPATCH, "  AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
>> +      if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
>> +        //
>> +        // Recursively process AFTER
>> +        //
>> +        DEBUG ((DEBUG_DISPATCH, "TRUE\n  END\n  RESULT = TRUE\n"));
>> +        MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
>> +      } else {
>> +        DEBUG ((DEBUG_DISPATCH, "FALSE\n  END\n  RESULT = FALSE\n"));
>> +      }
>> +    }
>> +  }
>> +}
>> +
>> +/**
>> +  Return TRUE if the Fv has been processed, FALSE if not.
>> +
>> +  @param  FvHandle              The handle of a FV that's being tested
>> +
>> +  @retval TRUE                  Fv protocol on FvHandle has been processed
>> +  @retval FALSE                 Fv protocol on FvHandle has not yet been
>> +                                processed
>> +
>> +**/
>> +BOOLEAN
>> +FvHasBeenProcessed (
>> +  IN EFI_HANDLE  FvHandle
>> +  )
>> +{
>> +  LIST_ENTRY    *Link;
>> +  KNOWN_HANDLE  *KnownHandle;
>> +
>> +  for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {
>> +    KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);
>> +    if (KnownHandle->Handle == FvHandle) {
>> +      return TRUE;
>> +    }
>> +  }
>> +  return FALSE;
>> +}
>> +
>> +/**
>> +  Remember that Fv protocol on FvHandle has had it's drivers placed on the
>> +  mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are
>> +  never removed/freed from the mFvHandleList.
>> +
>> +  @param  FvHandle              The handle of a FV that has been processed
>> +
>> +**/
>> +VOID
>> +FvIsBeingProcesssed (
>> +  IN EFI_HANDLE  FvHandle
>> +  )
>> +{
>> +  KNOWN_HANDLE  *KnownHandle;
>> +
>> +  DEBUG ((DEBUG_INFO, "FvIsBeingProcesssed - 0x%08x\n", FvHandle));
>> +
>> +  KnownHandle = AllocatePool (sizeof (KNOWN_HANDLE));
>> +  ASSERT (KnownHandle != NULL);
>> +
>> +  KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;
>> +  KnownHandle->Handle = FvHandle;
>> +  InsertTailList (&mFvHandleList, &KnownHandle->Link);
>> +}
>> +
>> +/**
>> +  Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
>> +  and initilize any state variables. Read the Depex from the FV and store it
>> +  in DriverEntry. Pre-process the Depex to set the Before and After state.
>> +  The Discovered list is never free'ed and contains booleans that represent the
>> +  other possible MM driver states.
>> +
>> +  @param  Fv                    Fv protocol, needed to read Depex info out of
>> +                                FLASH.
>> +  @param  FvHandle              Handle for Fv, needed in the
>> +                                EFI_MM_DRIVER_ENTRY so that the PE image can be
>> +                                read out of the FV at a later time.
>> +  @param  DriverName            Name of driver to add to mDiscoveredList.
>> +
>> +  @retval EFI_SUCCESS           If driver was added to the mDiscoveredList.
>> +  @retval EFI_ALREADY_STARTED   The driver has already been started. Only one
>> +                                DriverName may be active in the system at any one
>> +                                time.
>> +
>> +**/
>> +EFI_STATUS
>> +MmAddToDriverList (
>> +  IN EFI_HANDLE   FvHandle,
>> +  IN VOID         *Pe32Data,
>> +  IN UINTN        Pe32DataSize,
>> +  IN VOID         *Depex,
>> +  IN UINTN        DepexSize,
>> +  IN EFI_GUID     *DriverName
>> +  )
>> +{
>> +  EFI_MM_DRIVER_ENTRY  *DriverEntry;
>> +
>> +  DEBUG ((DEBUG_INFO, "MmAddToDriverList - %g (0x%08x)\n", DriverName, Pe32Data));
>> +
>> +  //
>> +  // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
>> +  // NULL or FALSE.
>> +  //
>> +  DriverEntry = AllocateZeroPool (sizeof (EFI_MM_DRIVER_ENTRY));
>> +  ASSERT (DriverEntry != NULL);
>> +
>> +  DriverEntry->Signature        = EFI_MM_DRIVER_ENTRY_SIGNATURE;
>> +  CopyGuid (&DriverEntry->FileName, DriverName);
>> +  DriverEntry->FvHandle         = FvHandle;
>> +  DriverEntry->Pe32Data         = Pe32Data;
>> +  DriverEntry->Pe32DataSize     = Pe32DataSize;
>> +  DriverEntry->Depex            = Depex;
>> +  DriverEntry->DepexSize        = DepexSize;
>> +
>> +  MmGetDepexSectionAndPreProccess (DriverEntry);
>> +
>> +  InsertTailList (&mDiscoveredList, &DriverEntry->Link);
>> +  gRequestDispatch = TRUE;
>> +
>> +  return EFI_SUCCESS;
>> +}
>> +
>> +/**
>> +  This function is the main entry point for an MM handler dispatch
>> +  or communicate-based callback.
>> +
>> +  Event notification that is fired every time a FV dispatch protocol is added.
>> +  More than one protocol may have been added when this event is fired, so you
>> +  must loop on MmLocateHandle () to see how many protocols were added and
>> +  do the following to each FV:
>> +  If the Fv has already been processed, skip it. If the Fv has not been
>> +  processed then mark it as being processed, as we are about to process it.
>> +  Read the Fv and add any driver in the Fv to the mDiscoveredList.The
>> +  mDiscoveredList is never free'ed and contains variables that define
>> +  the other states the MM driver transitions to..
>> +  While you are at it read the A Priori file into memory.
>> +  Place drivers in the A Priori list onto the mScheduledQueue.
>> +
>> +  @param  DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
>> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
>> +  @param  CommBuffer      A pointer to a collection of data in memory that will
>> +                          be conveyed from a non-MM environment into an MM environment.
>> +  @param  CommBufferSize  The size of the CommBuffer.
>> +
>> +  @return Status Code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmDriverDispatchHandler (
>> +  IN     EFI_HANDLE  DispatchHandle,
>> +  IN     CONST VOID  *Context,        OPTIONAL
>> +  IN OUT VOID        *CommBuffer,     OPTIONAL
>> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
>> +  )
>> +{
>> +  EFI_STATUS                            Status;
>> +
>> +  DEBUG ((DEBUG_INFO, "MmDriverDispatchHandler\n"));
>> +
>> +  //
>> +  // Execute the MM Dispatcher on any newly discovered FVs and previously
>> +  // discovered MM drivers that have been discovered but not dispatched.
>> +  //
>> +  Status = MmDispatcher ();
>> +
>> +  //
>> +  // Check to see if CommBuffer and CommBufferSize are valid
>> +  //
>> +  if (CommBuffer != NULL && CommBufferSize != NULL) {
>> +    if (*CommBufferSize > 0) {
>> +      if (Status == EFI_NOT_READY) {
>> +        //
>> +        // If a the MM Core Entry Point was just registered, then set flag to
>> +        // request the MM Dispatcher to be restarted.
>> +        //
>> +        *(UINT8 *)CommBuffer = COMM_BUFFER_MM_DISPATCH_RESTART;
>> +      } else if (!EFI_ERROR (Status)) {
>> +        //
>> +        // Set the flag to show that the MM Dispatcher executed without errors
>> +        //
>> +        *(UINT8 *)CommBuffer = COMM_BUFFER_MM_DISPATCH_SUCCESS;
>> +      } else {
>> +        //
>> +        // Set the flag to show that the MM Dispatcher encountered an error
>> +        //
>> +        *(UINT8 *)CommBuffer = COMM_BUFFER_MM_DISPATCH_ERROR;
>> +      }
>> +    }
>> +  }
>> +
>> +  return EFI_SUCCESS;
>> +}
>> +
>> +/**
>> +  This function is the main entry point for an MM handler dispatch
>> +  or communicate-based callback.
>> +
>> +  @param  DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
>> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
>> +  @param  CommBuffer      A pointer to a collection of data in memory that will
>> +                          be conveyed from a non-MM environment into an MM environment.
>> +  @param  CommBufferSize  The size of the CommBuffer.
>> +
>> +  @return Status Code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmFvDispatchHandler (
>> +  IN     EFI_HANDLE               DispatchHandle,
>> +  IN     CONST VOID               *Context,        OPTIONAL
>> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
>> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
>> +  )
>> +{
>> +  EFI_STATUS                            Status;
>> +  EFI_MM_COMMUNICATE_FV_DISPATCH_DATA  *CommunicationFvDispatchData;
>> +  EFI_FIRMWARE_VOLUME_HEADER            *FwVolHeader;
>> +
>> +  DEBUG ((DEBUG_INFO, "MmFvDispatchHandler\n"));
>> +
>> +  CommunicationFvDispatchData = CommBuffer;
>> +
>> +  DEBUG ((DEBUG_INFO, "  Dispatch - 0x%016lx - 0x%016lx\n", CommunicationFvDispatchData->Address, CommunicationFvDispatchData->Size));
>> +
>> +  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)CommunicationFvDispatchData->Address;
>> +
>> +  MmCoreFfsFindMmDriver (FwVolHeader);
>> +
>> +  //
>> +  // Execute the MM Dispatcher on any newly discovered FVs and previously
>> +  // discovered MM drivers that have been discovered but not dispatched.
>> +  //
>> +  Status = MmDispatcher ();
>> +
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Traverse the discovered list for any drivers that were discovered but not loaded
>> +  because the dependency experessions evaluated to false.
>> +
>> +**/
>> +VOID
>> +MmDisplayDiscoveredNotDispatched (
>> +  VOID
>> +  )
>> +{
>> +  LIST_ENTRY                   *Link;
>> +  EFI_MM_DRIVER_ENTRY         *DriverEntry;
>> +
>> +  for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {
>> +    DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
>> +    if (DriverEntry->Dependent) {
>> +      DEBUG ((DEBUG_LOAD, "MM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));
>> +    }
>> +  }
>> +}
>> diff --git a/StandaloneMmPkg/Core/FwVol.c b/StandaloneMmPkg/Core/FwVol.c
>> new file mode 100644
>> index 0000000000..901c58bc53
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Core/FwVol.c
>> @@ -0,0 +1,104 @@
>> +/**@file
>> +
>> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
>> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +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
>> +
>> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +
>> +**/
>> +
>> +#include "StandaloneMmCore.h"
>> +#include <Library/FvLib.h>
>> +
>> +//
>> +// List of file types supported by dispatcher
>> +//
>> +EFI_FV_FILETYPE mMmFileTypes[] = {
>> +  EFI_FV_FILETYPE_MM,
>> +  0xE, //EFI_FV_FILETYPE_MM_STANDALONE,
>> +       //
>> +       // Note: DXE core will process the FV image file, so skip it in MM core
>> +       // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
>> +       //
>> +};
>> +
>> +EFI_STATUS
>> +MmAddToDriverList (
>> +  IN EFI_HANDLE   FvHandle,
>> +  IN VOID         *Pe32Data,
>> +  IN UINTN        Pe32DataSize,
>> +  IN VOID         *Depex,
>> +  IN UINTN        DepexSize,
>> +  IN EFI_GUID     *DriverName
>> +  );
>> +
>> +BOOLEAN
>> +FvHasBeenProcessed (
>> +  IN EFI_HANDLE  FvHandle
>> +  );
>> +
>> +VOID
>> +FvIsBeingProcesssed (
>> +  IN EFI_HANDLE  FvHandle
>> +  );
>> +
>> +EFI_STATUS
>> +MmCoreFfsFindMmDriver (
>> +  IN  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader
>> +  )
>> +/*++
>> +
>> +Routine Description:
>> +  Given the pointer to the Firmware Volume Header find the
>> +  MM driver and return it's PE32 image.
>> +
>> +Arguments:
>> +  FwVolHeader - Pointer to memory mapped FV
>> +
>> +Returns:
>> +  other       - Failure
>> +
>> +--*/
>> +{
>> +  EFI_STATUS          Status;
>> +  EFI_STATUS          DepexStatus;
>> +  EFI_FFS_FILE_HEADER *FileHeader;
>> +  EFI_FV_FILETYPE     FileType;
>> +  VOID                *Pe32Data;
>> +  UINTN               Pe32DataSize;
>> +  VOID                *Depex;
>> +  UINTN               DepexSize;
>> +  UINTN               Index;
>> +
>> +  DEBUG((DEBUG_INFO, "MmCoreFfsFindMmDriver - 0x%x\n", FwVolHeader));
>> +
>> +  if (FvHasBeenProcessed (FwVolHeader)) {
>> +    return EFI_SUCCESS;
>> +  }
>> +
>> +  FvIsBeingProcesssed (FwVolHeader);
>> +
>> +  for (Index = 0; Index < sizeof(mMmFileTypes) / sizeof(mMmFileTypes[0]); Index++) {
>> +    DEBUG((DEBUG_INFO, "Check MmFileTypes - 0x%x\n", mMmFileTypes[Index]));
>> +    FileType = mMmFileTypes[Index];
>> +    FileHeader = NULL;
>> +    do {
>> +      Status = FfsFindNextFile(FileType, FwVolHeader, &FileHeader);
>> +      if (!EFI_ERROR(Status)) {
>> +        Status = FfsFindSectionData(EFI_SECTION_PE32, FileHeader, &Pe32Data, &Pe32DataSize);
>> +        DEBUG((DEBUG_INFO, "Find PE data - 0x%x\n", Pe32Data));
>> +        DepexStatus = FfsFindSectionData(EFI_SECTION_MM_DEPEX, FileHeader, &Depex, &DepexSize);
>> +        if (!EFI_ERROR(DepexStatus)) {
>> +          MmAddToDriverList(FwVolHeader, Pe32Data, Pe32DataSize, Depex, DepexSize, &FileHeader->Name);
>> +        }
>> +      }
>> +    } while (!EFI_ERROR(Status));
>> +  }
>> +
>> +  return Status;
>> +}
>> diff --git a/StandaloneMmPkg/Core/Handle.c b/StandaloneMmPkg/Core/Handle.c
>> new file mode 100644
>> index 0000000000..01832f4bbe
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Core/Handle.c
>> @@ -0,0 +1,533 @@
>> +/** @file
>> +  SMM handle & protocol handling.
>> +
>> +  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
>> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +  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
>> +
>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +
>> +**/
>> +
>> +#include "StandaloneMmCore.h"
>> +
>> +//
>> +// mProtocolDatabase     - A list of all protocols in the system.  (simple list for now)
>> +// gHandleList           - A list of all the handles in the system
>> +//
>> +LIST_ENTRY  mProtocolDatabase  = INITIALIZE_LIST_HEAD_VARIABLE (mProtocolDatabase);
>> +LIST_ENTRY  gHandleList        = INITIALIZE_LIST_HEAD_VARIABLE (gHandleList);
>> +
>> +/**
>> +  Check whether a handle is a valid EFI_HANDLE
>> +
>> +  @param  UserHandle             The handle to check
>> +
>> +  @retval EFI_INVALID_PARAMETER  The handle is NULL or not a valid EFI_HANDLE.
>> +  @retval EFI_SUCCESS            The handle is valid EFI_HANDLE.
>> +
>> +**/
>> +EFI_STATUS
>> +MmValidateHandle (
>> +  IN EFI_HANDLE  UserHandle
>> +  )
>> +{
>> +  IHANDLE  *Handle;
>> +
>> +  Handle = (IHANDLE *)UserHandle;
>> +  if (Handle == NULL) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +  if (Handle->Signature != EFI_HANDLE_SIGNATURE) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +  return EFI_SUCCESS;
>> +}
>> +
>> +/**
>> +  Finds the protocol entry for the requested protocol.
>> +
>> +  @param  Protocol               The ID of the protocol
>> +  @param  Create                 Create a new entry if not found
>> +
>> +  @return Protocol entry
>> +
>> +**/
>> +PROTOCOL_ENTRY  *
>> +MmFindProtocolEntry (
>> +  IN EFI_GUID   *Protocol,
>> +  IN BOOLEAN    Create
>> +  )
>> +{
>> +  LIST_ENTRY          *Link;
>> +  PROTOCOL_ENTRY      *Item;
>> +  PROTOCOL_ENTRY      *ProtEntry;
>> +
>> +  //
>> +  // Search the database for the matching GUID
>> +  //
>> +
>> +  ProtEntry = NULL;
>> +  for (Link = mProtocolDatabase.ForwardLink;
>> +       Link != &mProtocolDatabase;
>> +       Link = Link->ForwardLink) {
>> +
>> +    Item = CR(Link, PROTOCOL_ENTRY, AllEntries, PROTOCOL_ENTRY_SIGNATURE);
>> +    if (CompareGuid (&Item->ProtocolID, Protocol)) {
>> +      //
>> +      // This is the protocol entry
>> +      //
>> +      ProtEntry = Item;
>> +      break;
>> +    }
>> +  }
>> +
>> +  //
>> +  // If the protocol entry was not found and Create is TRUE, then
>> +  // allocate a new entry
>> +  //
>> +  if ((ProtEntry == NULL) && Create) {
>> +    ProtEntry = AllocatePool (sizeof(PROTOCOL_ENTRY));
>> +    if (ProtEntry != NULL) {
>> +      //
>> +      // Initialize new protocol entry structure
>> +      //
>> +      ProtEntry->Signature = PROTOCOL_ENTRY_SIGNATURE;
>> +      CopyGuid ((VOID *)&ProtEntry->ProtocolID, Protocol);
>> +      InitializeListHead (&ProtEntry->Protocols);
>> +      InitializeListHead (&ProtEntry->Notify);
>> +
>> +      //
>> +      // Add it to protocol database
>> +      //
>> +      InsertTailList (&mProtocolDatabase, &ProtEntry->AllEntries);
>> +    }
>> +  }
>> +  return ProtEntry;
>> +}
>> +
>> +/**
>> +  Finds the protocol instance for the requested handle and protocol.
>> +  Note: This function doesn't do parameters checking, it's caller's responsibility
>> +  to pass in valid parameters.
>> +
>> +  @param  Handle                 The handle to search the protocol on
>> +  @param  Protocol               GUID of the protocol
>> +  @param  Interface              The interface for the protocol being searched
>> +
>> +  @return Protocol instance (NULL: Not found)
>> +
>> +**/
>> +PROTOCOL_INTERFACE *
>> +MmFindProtocolInterface (
>> +  IN IHANDLE   *Handle,
>> +  IN EFI_GUID  *Protocol,
>> +  IN VOID      *Interface
>> +  )
>> +{
>> +  PROTOCOL_INTERFACE  *Prot;
>> +  PROTOCOL_ENTRY      *ProtEntry;
>> +  LIST_ENTRY          *Link;
>> +
>> +  Prot = NULL;
>> +
>> +  //
>> +  // Lookup the protocol entry for this protocol ID
>> +  //
>> +  ProtEntry = MmFindProtocolEntry (Protocol, FALSE);
>> +  if (ProtEntry != NULL) {
>> +    //
>> +    // Look at each protocol interface for any matches
>> +    //
>> +    for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link=Link->ForwardLink) {
>> +      //
>> +      // If this protocol interface matches, remove it
>> +      //
>> +      Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);
>> +      if (Prot->Interface == Interface && Prot->Protocol == ProtEntry) {
>> +        break;
>> +      }
>> +      Prot = NULL;
>> +    }
>> +  }
>> +  return Prot;
>> +}
>> +
>> +/**
>> +  Wrapper function to MmInstallProtocolInterfaceNotify.  This is the public API which
>> +  Calls the private one which contains a BOOLEAN parameter for notifications
>> +
>> +  @param  UserHandle             The handle to install the protocol handler on,
>> +                                 or NULL if a new handle is to be allocated
>> +  @param  Protocol               The protocol to add to the handle
>> +  @param  InterfaceType          Indicates whether Interface is supplied in
>> +                                 native form.
>> +  @param  Interface              The interface for the protocol being added
>> +
>> +  @return Status code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmInstallProtocolInterface (
>> +  IN OUT EFI_HANDLE      *UserHandle,
>> +  IN EFI_GUID            *Protocol,
>> +  IN EFI_INTERFACE_TYPE  InterfaceType,
>> +  IN VOID                *Interface
>> +  )
>> +{
>> +  return MmInstallProtocolInterfaceNotify (
>> +           UserHandle,
>> +           Protocol,
>> +           InterfaceType,
>> +           Interface,
>> +           TRUE
>> +           );
>> +}
>> +
>> +/**
>> +  Installs a protocol interface into the boot services environment.
>> +
>> +  @param  UserHandle             The handle to install the protocol handler on,
>> +                                 or NULL if a new handle is to be allocated
>> +  @param  Protocol               The protocol to add to the handle
>> +  @param  InterfaceType          Indicates whether Interface is supplied in
>> +                                 native form.
>> +  @param  Interface              The interface for the protocol being added
>> +  @param  Notify                 indicates whether notify the notification list
>> +                                 for this protocol
>> +
>> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
>> +  @retval EFI_OUT_OF_RESOURCES   No enough buffer to allocate
>> +  @retval EFI_SUCCESS            Protocol interface successfully installed
>> +
>> +**/
>> +EFI_STATUS
>> +MmInstallProtocolInterfaceNotify (
>> +  IN OUT EFI_HANDLE          *UserHandle,
>> +  IN     EFI_GUID            *Protocol,
>> +  IN     EFI_INTERFACE_TYPE  InterfaceType,
>> +  IN     VOID                *Interface,
>> +  IN     BOOLEAN             Notify
>> +  )
>> +{
>> +  PROTOCOL_INTERFACE  *Prot;
>> +  PROTOCOL_ENTRY      *ProtEntry;
>> +  IHANDLE             *Handle;
>> +  EFI_STATUS          Status;
>> +  VOID                *ExistingInterface;
>> +
>> +  //
>> +  // returns EFI_INVALID_PARAMETER if InterfaceType is invalid.
>> +  // Also added check for invalid UserHandle and Protocol pointers.
>> +  //
>> +  if (UserHandle == NULL || Protocol == NULL) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  if (InterfaceType != EFI_NATIVE_INTERFACE) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  //
>> +  // Print debug message
>> +  //
>> +  DEBUG((DEBUG_LOAD | DEBUG_INFO, "MmInstallProtocolInterface: %g %p\n", Protocol, Interface));
>> +
>> +  Status = EFI_OUT_OF_RESOURCES;
>> +  Prot = NULL;
>> +  Handle = NULL;
>> +
>> +  if (*UserHandle != NULL) {
>> +    Status = MmHandleProtocol (*UserHandle, Protocol, (VOID **)&ExistingInterface);
>> +    if (!EFI_ERROR (Status)) {
>> +      return EFI_INVALID_PARAMETER;
>> +    }
>> +  }
>> +
>> +  //
>> +  // Lookup the Protocol Entry for the requested protocol
>> +  //
>> +  ProtEntry = MmFindProtocolEntry (Protocol, TRUE);
>> +  if (ProtEntry == NULL) {
>> +    goto Done;
>> +  }
>> +
>> +  //
>> +  // Allocate a new protocol interface structure
>> +  //
>> +  Prot = AllocateZeroPool (sizeof(PROTOCOL_INTERFACE));
>> +  if (Prot == NULL) {
>> +    Status = EFI_OUT_OF_RESOURCES;
>> +    goto Done;
>> +  }
>> +
>> +  //
>> +  // If caller didn't supply a handle, allocate a new one
>> +  //
>> +  Handle = (IHANDLE *)*UserHandle;
>> +  if (Handle == NULL) {
>> +    Handle = AllocateZeroPool (sizeof(IHANDLE));
>> +    if (Handle == NULL) {
>> +      Status = EFI_OUT_OF_RESOURCES;
>> +      goto Done;
>> +    }
>> +
>> +    //
>> +    // Initialize new handler structure
>> +    //
>> +    Handle->Signature = EFI_HANDLE_SIGNATURE;
>> +    InitializeListHead (&Handle->Protocols);
>> +
>> +    //
>> +    // Add this handle to the list global list of all handles
>> +    // in the system
>> +    //
>> +    InsertTailList (&gHandleList, &Handle->AllHandles);
>> +  }
>> +
>> +  Status = MmValidateHandle (Handle);
>> +  if (EFI_ERROR (Status)) {
>> +    goto Done;
>> +  }
>> +
>> +  //
>> +  // Each interface that is added must be unique
>> +  //
>> +  ASSERT (MmFindProtocolInterface (Handle, Protocol, Interface) == NULL);
>> +
>> +  //
>> +  // Initialize the protocol interface structure
>> +  //
>> +  Prot->Signature = PROTOCOL_INTERFACE_SIGNATURE;
>> +  Prot->Handle = Handle;
>> +  Prot->Protocol = ProtEntry;
>> +  Prot->Interface = Interface;
>> +
>> +  //
>> +  // Add this protocol interface to the head of the supported
>> +  // protocol list for this handle
>> +  //
>> +  InsertHeadList (&Handle->Protocols, &Prot->Link);
>> +
>> +  //
>> +  // Add this protocol interface to the tail of the
>> +  // protocol entry
>> +  //
>> +  InsertTailList (&ProtEntry->Protocols, &Prot->ByProtocol);
>> +
>> +  //
>> +  // Notify the notification list for this protocol
>> +  //
>> +  if (Notify) {
>> +    MmNotifyProtocol (Prot);
>> +  }
>> +  Status = EFI_SUCCESS;
>> +
>> +Done:
>> +  if (!EFI_ERROR (Status)) {
>> +    //
>> +    // Return the new handle back to the caller
>> +    //
>> +    *UserHandle = Handle;
>> +  } else {
>> +    //
>> +    // There was an error, clean up
>> +    //
>> +    if (Prot != NULL) {
>> +      FreePool (Prot);
>> +    }
>> +  }
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Uninstalls all instances of a protocol:interfacer from a handle.
>> +  If the last protocol interface is remove from the handle, the
>> +  handle is freed.
>> +
>> +  @param  UserHandle             The handle to remove the protocol handler from
>> +  @param  Protocol               The protocol, of protocol:interface, to remove
>> +  @param  Interface              The interface, of protocol:interface, to remove
>> +
>> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL.
>> +  @retval EFI_SUCCESS            Protocol interface successfully uninstalled.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmUninstallProtocolInterface (
>> +  IN EFI_HANDLE  UserHandle,
>> +  IN EFI_GUID    *Protocol,
>> +  IN VOID        *Interface
>> +  )
>> +{
>> +  EFI_STATUS          Status;
>> +  IHANDLE             *Handle;
>> +  PROTOCOL_INTERFACE  *Prot;
>> +
>> +  //
>> +  // Check that Protocol is valid
>> +  //
>> +  if (Protocol == NULL) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  //
>> +  // Check that UserHandle is a valid handle
>> +  //
>> +  Status = MmValidateHandle (UserHandle);
>> +  if (EFI_ERROR (Status)) {
>> +    return Status;
>> +  }
>> +
>> +  //
>> +  // Check that Protocol exists on UserHandle, and Interface matches the interface in the database
>> +  //
>> +  Prot = MmFindProtocolInterface (UserHandle, Protocol, Interface);
>> +  if (Prot == NULL) {
>> +    return EFI_NOT_FOUND;
>> +  }
>> +
>> +  //
>> +  // Remove the protocol interface from the protocol
>> +  //
>> +  Status = EFI_NOT_FOUND;
>> +  Handle = (IHANDLE *)UserHandle;
>> +  Prot   = MmRemoveInterfaceFromProtocol (Handle, Protocol, Interface);
>> +
>> +  if (Prot != NULL) {
>> +    //
>> +    // Remove the protocol interface from the handle
>> +    //
>> +    RemoveEntryList (&Prot->Link);
>> +
>> +    //
>> +    // Free the memory
>> +    //
>> +    Prot->Signature = 0;
>> +    FreePool (Prot);
>> +    Status = EFI_SUCCESS;
>> +  }
>> +
>> +  //
>> +  // If there are no more handlers for the handle, free the handle
>> +  //
>> +  if (IsListEmpty (&Handle->Protocols)) {
>> +    Handle->Signature = 0;
>> +    RemoveEntryList (&Handle->AllHandles);
>> +    FreePool (Handle);
>> +  }
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Locate a certain GUID protocol interface in a Handle's protocols.
>> +
>> +  @param  UserHandle             The handle to obtain the protocol interface on
>> +  @param  Protocol               The GUID of the protocol
>> +
>> +  @return The requested protocol interface for the handle
>> +
>> +**/
>> +PROTOCOL_INTERFACE  *
>> +MmGetProtocolInterface (
>> +  IN EFI_HANDLE  UserHandle,
>> +  IN EFI_GUID    *Protocol
>> +  )
>> +{
>> +  EFI_STATUS          Status;
>> +  PROTOCOL_ENTRY      *ProtEntry;
>> +  PROTOCOL_INTERFACE  *Prot;
>> +  IHANDLE             *Handle;
>> +  LIST_ENTRY          *Link;
>> +
>> +  Status = MmValidateHandle (UserHandle);
>> +  if (EFI_ERROR (Status)) {
>> +    return NULL;
>> +  }
>> +
>> +  Handle = (IHANDLE *)UserHandle;
>> +
>> +  //
>> +  // Look at each protocol interface for a match
>> +  //
>> +  for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link = Link->ForwardLink) {
>> +    Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);
>> +    ProtEntry = Prot->Protocol;
>> +    if (CompareGuid (&ProtEntry->ProtocolID, Protocol)) {
>> +      return Prot;
>> +    }
>> +  }
>> +  return NULL;
>> +}
>> +
>> +/**
>> +  Queries a handle to determine if it supports a specified protocol.
>> +
>> +  @param  UserHandle             The handle being queried.
>> +  @param  Protocol               The published unique identifier of the protocol.
>> +  @param  Interface              Supplies the address where a pointer to the
>> +                                 corresponding Protocol Interface is returned.
>> +
>> +  @retval EFI_SUCCESS            The interface information for the specified protocol was returned.
>> +  @retval EFI_UNSUPPORTED        The device does not support the specified protocol.
>> +  @retval EFI_INVALID_PARAMETER  Handle is not a valid EFI_HANDLE..
>> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL.
>> +  @retval EFI_INVALID_PARAMETER  Interface is NULL.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmHandleProtocol (
>> +  IN  EFI_HANDLE  UserHandle,
>> +  IN  EFI_GUID    *Protocol,
>> +  OUT VOID        **Interface
>> +  )
>> +{
>> +  EFI_STATUS          Status;
>> +  PROTOCOL_INTERFACE  *Prot;
>> +
>> +  //
>> +  // Check for invalid Protocol
>> +  //
>> +  if (Protocol == NULL) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  //
>> +  // Check for invalid Interface
>> +  //
>> +  if (Interface == NULL) {
>> +    return EFI_INVALID_PARAMETER;
>> +  } else {
>> +    *Interface = NULL;
>> +  }
>> +
>> +  //
>> +  // Check for invalid UserHandle
>> +  //
>> +  Status = MmValidateHandle (UserHandle);
>> +  if (EFI_ERROR (Status)) {
>> +    return Status;
>> +  }
>> +
>> +  //
>> +  // Look at each protocol interface for a match
>> +  //
>> +  Prot = MmGetProtocolInterface (UserHandle, Protocol);
>> +  if (Prot == NULL) {
>> +    return EFI_UNSUPPORTED;
>> +  }
>> +
>> +  //
>> +  // This is the protocol interface entry for this protocol
>> +  //
>> +  *Interface = Prot->Interface;
>> +
>> +  return EFI_SUCCESS;
>> +}
>> diff --git a/StandaloneMmPkg/Core/InstallConfigurationTable.c b/StandaloneMmPkg/Core/InstallConfigurationTable.c
>> new file mode 100644
>> index 0000000000..3a31c63f94
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Core/InstallConfigurationTable.c
>> @@ -0,0 +1,178 @@
>> +/** @file
>> +  System Management System Table Services MmInstallConfigurationTable service
>> +
>> +  Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
>> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +  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
>> +
>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +
>> +**/
>> +
>> +#include "StandaloneMmCore.h"
>> +
>> +#define CONFIG_TABLE_SIZE_INCREASED 0x10
>> +
>> +UINTN  mMmSystemTableAllocateSize = 0;
>> +
>> +/**
>> +  The MmInstallConfigurationTable() function is used to maintain the list
>> +  of configuration tables that are stored in the System Management System
>> +  Table.  The list is stored as an array of (GUID, Pointer) pairs.  The list
>> +  must be allocated from pool memory with PoolType set to EfiRuntimeServicesData.
>> +
>> +  @param  SystemTable      A pointer to the SMM System Table (SMST).
>> +  @param  Guid             A pointer to the GUID for the entry to add, update, or remove.
>> +  @param  Table            A pointer to the buffer of the table to add.
>> +  @param  TableSize        The size of the table to install.
>> +
>> +  @retval EFI_SUCCESS           The (Guid, Table) pair was added, updated, or removed.
>> +  @retval EFI_INVALID_PARAMETER Guid is not valid.
>> +  @retval EFI_NOT_FOUND         An attempt was made to delete a non-existent entry.
>> +  @retval EFI_OUT_OF_RESOURCES  There is not enough memory available to complete the operation.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmInstallConfigurationTable (
>> +  IN  CONST EFI_MM_SYSTEM_TABLE    *SystemTable,
>> +  IN  CONST EFI_GUID               *Guid,
>> +  IN  VOID                         *Table,
>> +  IN  UINTN                        TableSize
>> +  )
>> +{
>> +  UINTN                    Index;
>> +  EFI_CONFIGURATION_TABLE  *ConfigurationTable;
>> +  EFI_CONFIGURATION_TABLE  *OldTable;
>> +
>> +  //
>> +  // If Guid is NULL, then this operation cannot be performed
>> +  //
>> +  if (Guid == NULL) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  ConfigurationTable = gMmCoreMmst.MmConfigurationTable;
>> +
>> +  //
>> +  // Search all the table for an entry that matches Guid
>> +  //
>> +  for (Index = 0; Index < gMmCoreMmst.NumberOfTableEntries; Index++) {
>> +    if (CompareGuid (Guid, &(ConfigurationTable[Index].VendorGuid))) {
>> +      break;
>> +    }
>> +  }
>> +
>> +  if (Index < gMmCoreMmst.NumberOfTableEntries) {
>> +    //
>> +    // A match was found, so this is either a modify or a delete operation
>> +    //
>> +    if (Table != NULL) {
>> +      //
>> +      // If Table is not NULL, then this is a modify operation.
>> +      // Modify the table entry and return.
>> +      //
>> +      ConfigurationTable[Index].VendorTable = Table;
>> +      return EFI_SUCCESS;
>> +    }
>> +
>> +    //
>> +    // A match was found and Table is NULL, so this is a delete operation.
>> +    //
>> +    gMmCoreMmst.NumberOfTableEntries--;
>> +
>> +    //
>> +    // Copy over deleted entry
>> +    //
>> +    CopyMem (
>> +      &(ConfigurationTable[Index]),
>> +      &(ConfigurationTable[Index + 1]),
>> +      (gMmCoreMmst.NumberOfTableEntries - Index) * sizeof (EFI_CONFIGURATION_TABLE)
>> +      );
>> +
>> +  } else {
>> +    //
>> +    // No matching GUIDs were found, so this is an add operation.
>> +    //
>> +    if (Table == NULL) {
>> +      //
>> +      // If Table is NULL on an add operation, then return an error.
>> +      //
>> +      return EFI_NOT_FOUND;
>> +    }
>> +
>> +    //
>> +    // Assume that Index == gMmCoreMmst.NumberOfTableEntries
>> +    //
>> +    if ((Index * sizeof (EFI_CONFIGURATION_TABLE)) >= mMmSystemTableAllocateSize) {
>> +      //
>> +      // Allocate a table with one additional entry.
>> +      //
>> +      mMmSystemTableAllocateSize += (CONFIG_TABLE_SIZE_INCREASED * sizeof (EFI_CONFIGURATION_TABLE));
>> +      ConfigurationTable = AllocatePool (mMmSystemTableAllocateSize);
>> +      if (ConfigurationTable == NULL) {
>> +        //
>> +        // If a new table could not be allocated, then return an error.
>> +        //
>> +        return EFI_OUT_OF_RESOURCES;
>> +      }
>> +
>> +      if (gMmCoreMmst.MmConfigurationTable != NULL) {
>> +        //
>> +        // Copy the old table to the new table.
>> +        //
>> +        CopyMem (
>> +          ConfigurationTable,
>> +          gMmCoreMmst.MmConfigurationTable,
>> +          Index * sizeof (EFI_CONFIGURATION_TABLE)
>> +          );
>> +
>> +        //
>> +        // Record the old table pointer.
>> +        //
>> +        OldTable = gMmCoreMmst.MmConfigurationTable;
>> +
>> +        //
>> +        // As the MmInstallConfigurationTable() may be re-entered by FreePool() in
>> +        // its calling stack, updating System table to the new table pointer must
>> +        // be done before calling FreePool() to free the old table.
>> +        // It can make sure the gMmCoreMmst.MmConfigurationTable point to the new
>> +        // table and avoid the errors of use-after-free to the old table by the
>> +        // reenter of MmInstallConfigurationTable() in FreePool()'s calling stack.
>> +        //
>> +        gMmCoreMmst.MmConfigurationTable = ConfigurationTable;
>> +
>> +        //
>> +        // Free the old table after updating System Table to the new table pointer.
>> +        //
>> +        FreePool (OldTable);
>> +      } else {
>> +        //
>> +        // Update System Table
>> +        //
>> +        gMmCoreMmst.MmConfigurationTable = ConfigurationTable;
>> +      }
>> +    }
>> +
>> +    //
>> +    // Fill in the new entry
>> +    //
>> +    CopyGuid ((VOID *)&ConfigurationTable[Index].VendorGuid, Guid);
>> +    ConfigurationTable[Index].VendorTable = Table;
>> +
>> +    //
>> +    // This is an add operation, so increment the number of table entries
>> +    //
>> +    gMmCoreMmst.NumberOfTableEntries++;
>> +  }
>> +
>> +  //
>> +  // CRC-32 field is ignorable for SMM System Table and should be set to zero
>> +  //
>> +
>> +  return EFI_SUCCESS;
>> +}
>> diff --git a/StandaloneMmPkg/Core/Locate.c b/StandaloneMmPkg/Core/Locate.c
>> new file mode 100644
>> index 0000000000..6a90575f99
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Core/Locate.c
>> @@ -0,0 +1,496 @@
>> +/** @file
>> +  Locate handle functions
>> +
>> +  Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
>> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +  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
>> +
>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +
>> +**/
>> +
>> +#include "StandaloneMmCore.h"
>> +
>> +//
>> +// ProtocolRequest - Last LocateHandle request ID
>> +//
>> +UINTN mEfiLocateHandleRequest = 0;
>> +
>> +//
>> +// Internal prototypes
>> +//
>> +
>> +typedef struct {
>> +  EFI_GUID        *Protocol;
>> +  VOID            *SearchKey;
>> +  LIST_ENTRY      *Position;
>> +  PROTOCOL_ENTRY  *ProtEntry;
>> +} LOCATE_POSITION;
>> +
>> +typedef
>> +IHANDLE *
>> +(* CORE_GET_NEXT) (
>> +  IN OUT LOCATE_POSITION    *Position,
>> +  OUT VOID                  **Interface
>> +  );
>> +
>> +/**
>> +  Routine to get the next Handle, when you are searching for all handles.
>> +
>> +  @param  Position               Information about which Handle to seach for.
>> +  @param  Interface              Return the interface structure for the matching
>> +                                 protocol.
>> +
>> +  @return An pointer to IHANDLE if the next Position is not the end of the list.
>> +          Otherwise,NULL is returned.
>> +
>> +**/
>> +IHANDLE *
>> +MmGetNextLocateAllHandles (
>> +  IN OUT LOCATE_POSITION  *Position,
>> +  OUT    VOID             **Interface
>> +  )
>> +{
>> +  IHANDLE     *Handle;
>> +
>> +  //
>> +  // Next handle
>> +  //
>> +  Position->Position = Position->Position->ForwardLink;
>> +
>> +  //
>> +  // If not at the end of the list, get the handle
>> +  //
>> +  Handle      = NULL;
>> +  *Interface  = NULL;
>> +  if (Position->Position != &gHandleList) {
>> +    Handle = CR (Position->Position, IHANDLE, AllHandles, EFI_HANDLE_SIGNATURE);
>> +  }
>> +  return Handle;
>> +}
>> +
>> +/**
>> +  Routine to get the next Handle, when you are searching for register protocol
>> +  notifies.
>> +
>> +  @param  Position               Information about which Handle to seach for.
>> +  @param  Interface              Return the interface structure for the matching
>> +                                 protocol.
>> +
>> +  @return An pointer to IHANDLE if the next Position is not the end of the list.
>> +          Otherwise,NULL is returned.
>> +
>> +**/
>> +IHANDLE *
>> +MmGetNextLocateByRegisterNotify (
>> +  IN OUT LOCATE_POSITION  *Position,
>> +  OUT    VOID             **Interface
>> +  )
>> +{
>> +  IHANDLE             *Handle;
>> +  PROTOCOL_NOTIFY     *ProtNotify;
>> +  PROTOCOL_INTERFACE  *Prot;
>> +  LIST_ENTRY          *Link;
>> +
>> +  Handle      = NULL;
>> +  *Interface  = NULL;
>> +  ProtNotify = Position->SearchKey;
>> +
>> +  //
>> +  // If this is the first request, get the next handle
>> +  //
>> +  if (ProtNotify != NULL) {
>> +    ASSERT(ProtNotify->Signature == PROTOCOL_NOTIFY_SIGNATURE);
>> +    Position->SearchKey = NULL;
>> +
>> +    //
>> +    // If not at the end of the list, get the next handle
>> +    //
>> +    Link = ProtNotify->Position->ForwardLink;
>> +    if (Link != &ProtNotify->Protocol->Protocols) {
>> +      Prot = CR (Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_INTERFACE_SIGNATURE);
>> +      Handle = Prot->Handle;
>> +      *Interface = Prot->Interface;
>> +    }
>> +  }
>> +  return Handle;
>> +}
>> +
>> +/**
>> +  Routine to get the next Handle, when you are searching for a given protocol.
>> +
>> +  @param  Position               Information about which Handle to seach for.
>> +  @param  Interface              Return the interface structure for the matching
>> +                                 protocol.
>> +
>> +  @return An pointer to IHANDLE if the next Position is not the end of the list.
>> +          Otherwise,NULL is returned.
>> +
>> +**/
>> +IHANDLE *
>> +MmGetNextLocateByProtocol (
>> +  IN OUT LOCATE_POSITION  *Position,
>> +  OUT    VOID             **Interface
>> +  )
>> +{
>> +  IHANDLE             *Handle;
>> +  LIST_ENTRY          *Link;
>> +  PROTOCOL_INTERFACE  *Prot;
>> +
>> +  Handle      = NULL;
>> +  *Interface  = NULL;
>> +  for (; ;) {
>> +    //
>> +    // Next entry
>> +    //
>> +    Link = Position->Position->ForwardLink;
>> +    Position->Position = Link;
>> +
>> +    //
>> +    // If not at the end, return the handle
>> +    //
>> +    if (Link == &Position->ProtEntry->Protocols) {
>> +      Handle = NULL;
>> +      break;
>> +    }
>> +
>> +    //
>> +    // Get the handle
>> +    //
>> +    Prot = CR(Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_INTERFACE_SIGNATURE);
>> +    Handle = Prot->Handle;
>> +    *Interface = Prot->Interface;
>> +
>> +    //
>> +    // If this handle has not been returned this request, then
>> +    // return it now
>> +    //
>> +    if (Handle->LocateRequest != mEfiLocateHandleRequest) {
>> +      Handle->LocateRequest = mEfiLocateHandleRequest;
>> +      break;
>> +    }
>> +  }
>> +  return Handle;
>> +}
>> +
>> +/**
>> +  Return the first Protocol Interface that matches the Protocol GUID. If
>> +  Registration is pasased in return a Protocol Instance that was just add
>> +  to the system. If Retistration is NULL return the first Protocol Interface
>> +  you find.
>> +
>> +  @param  Protocol               The protocol to search for
>> +  @param  Registration           Optional Registration Key returned from
>> +                                 RegisterProtocolNotify()
>> +  @param  Interface              Return the Protocol interface (instance).
>> +
>> +  @retval EFI_SUCCESS            If a valid Interface is returned
>> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
>> +  @retval EFI_NOT_FOUND          Protocol interface not found
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmLocateProtocol (
>> +  IN  EFI_GUID  *Protocol,
>> +  IN  VOID      *Registration OPTIONAL,
>> +  OUT VOID      **Interface
>> +  )
>> +{
>> +  EFI_STATUS              Status;
>> +  LOCATE_POSITION         Position;
>> +  PROTOCOL_NOTIFY         *ProtNotify;
>> +  IHANDLE                 *Handle;
>> +
>> +  if ((Interface == NULL) || (Protocol == NULL)) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  *Interface = NULL;
>> +  Status = EFI_SUCCESS;
>> +
>> +  //
>> +  // Set initial position
>> +  //
>> +  Position.Protocol  = Protocol;
>> +  Position.SearchKey = Registration;
>> +  Position.Position  = &gHandleList;
>> +
>> +  mEfiLocateHandleRequest += 1;
>> +
>> +  if (Registration == NULL) {
>> +    //
>> +    // Look up the protocol entry and set the head pointer
>> +    //
>> +    Position.ProtEntry = MmFindProtocolEntry (Protocol, FALSE);
>> +    if (Position.ProtEntry == NULL) {
>> +      return EFI_NOT_FOUND;
>> +    }
>> +    Position.Position = &Position.ProtEntry->Protocols;
>> +
>> +    Handle = MmGetNextLocateByProtocol (&Position, Interface);
>> +  } else {
>> +    Handle = MmGetNextLocateByRegisterNotify (&Position, Interface);
>> +  }
>> +
>> +  if (Handle == NULL) {
>> +    Status = EFI_NOT_FOUND;
>> +  } else if (Registration != NULL) {
>> +    //
>> +    // If this is a search by register notify and a handle was
>> +    // returned, update the register notification position
>> +    //
>> +    ProtNotify = Registration;
>> +    ProtNotify->Position = ProtNotify->Position->ForwardLink;
>> +  }
>> +
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Locates the requested handle(s) and returns them in Buffer.
>> +
>> +  @param  SearchType             The type of search to perform to locate the
>> +                                 handles
>> +  @param  Protocol               The protocol to search for
>> +  @param  SearchKey              Dependant on SearchType
>> +  @param  BufferSize             On input the size of Buffer.  On output the
>> +                                 size of data returned.
>> +  @param  Buffer                 The buffer to return the results in
>> +
>> +  @retval EFI_BUFFER_TOO_SMALL   Buffer too small, required buffer size is
>> +                                 returned in BufferSize.
>> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
>> +  @retval EFI_SUCCESS            Successfully found the requested handle(s) and
>> +                                 returns them in Buffer.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmLocateHandle (
>> +  IN     EFI_LOCATE_SEARCH_TYPE  SearchType,
>> +  IN     EFI_GUID                *Protocol   OPTIONAL,
>> +  IN     VOID                    *SearchKey  OPTIONAL,
>> +  IN OUT UINTN                   *BufferSize,
>> +  OUT    EFI_HANDLE              *Buffer
>> +  )
>> +{
>> +  EFI_STATUS       Status;
>> +  LOCATE_POSITION  Position;
>> +  PROTOCOL_NOTIFY  *ProtNotify;
>> +  CORE_GET_NEXT    GetNext;
>> +  UINTN            ResultSize;
>> +  IHANDLE          *Handle;
>> +  IHANDLE          **ResultBuffer;
>> +  VOID             *Interface;
>> +
>> +  if (BufferSize == NULL) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  if ((*BufferSize > 0) && (Buffer == NULL)) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  GetNext = NULL;
>> +
>> +  //
>> +  // Set initial position
>> +  //
>> +  Position.Protocol  = Protocol;
>> +  Position.SearchKey = SearchKey;
>> +  Position.Position  = &gHandleList;
>> +
>> +  ResultSize = 0;
>> +  ResultBuffer = (IHANDLE **) Buffer;
>> +  Status = EFI_SUCCESS;
>> +
>> +  //
>> +  // Get the search function based on type
>> +  //
>> +  switch (SearchType) {
>> +  case AllHandles:
>> +    GetNext = MmGetNextLocateAllHandles;
>> +    break;
>> +
>> +  case ByRegisterNotify:
>> +    GetNext = MmGetNextLocateByRegisterNotify;
>> +    //
>> +    // Must have SearchKey for locate ByRegisterNotify
>> +    //
>> +    if (SearchKey == NULL) {
>> +      Status = EFI_INVALID_PARAMETER;
>> +    }
>> +    break;
>> +
>> +  case ByProtocol:
>> +    GetNext = MmGetNextLocateByProtocol;
>> +    if (Protocol == NULL) {
>> +      Status = EFI_INVALID_PARAMETER;
>> +      break;
>> +    }
>> +    //
>> +    // Look up the protocol entry and set the head pointer
>> +    //
>> +    Position.ProtEntry = MmFindProtocolEntry (Protocol, FALSE);
>> +    if (Position.ProtEntry == NULL) {
>> +      Status = EFI_NOT_FOUND;
>> +      break;
>> +    }
>> +    Position.Position = &Position.ProtEntry->Protocols;
>> +    break;
>> +
>> +  default:
>> +    Status = EFI_INVALID_PARAMETER;
>> +    break;
>> +  }
>> +
>> +  if (EFI_ERROR(Status)) {
>> +    return Status;
>> +  }
>> +
>> +  //
>> +  // Enumerate out the matching handles
>> +  //
>> +  mEfiLocateHandleRequest += 1;
>> +  for (; ;) {
>> +    //
>> +    // Get the next handle.  If no more handles, stop
>> +    //
>> +    Handle = GetNext (&Position, &Interface);
>> +    if (NULL == Handle) {
>> +      break;
>> +    }
>> +
>> +    //
>> +    // Increase the resulting buffer size, and if this handle
>> +    // fits return it
>> +    //
>> +    ResultSize += sizeof(Handle);
>> +    if (ResultSize <= *BufferSize) {
>> +        *ResultBuffer = Handle;
>> +        ResultBuffer += 1;
>> +    }
>> +  }
>> +
>> +  //
>> +  // If the result is a zero length buffer, then there were no
>> +  // matching handles
>> +  //
>> +  if (ResultSize == 0) {
>> +    Status = EFI_NOT_FOUND;
>> +  } else {
>> +    //
>> +    // Return the resulting buffer size.  If it's larger than what
>> +    // was passed, then set the error code
>> +    //
>> +    if (ResultSize > *BufferSize) {
>> +      Status = EFI_BUFFER_TOO_SMALL;
>> +    }
>> +
>> +    *BufferSize = ResultSize;
>> +
>> +    if (SearchType == ByRegisterNotify && !EFI_ERROR(Status)) {
>> +      ASSERT (SearchKey != NULL);
>> +      //
>> +      // If this is a search by register notify and a handle was
>> +      // returned, update the register notification position
>> +      //
>> +      ProtNotify = SearchKey;
>> +      ProtNotify->Position = ProtNotify->Position->ForwardLink;
>> +    }
>> +  }
>> +
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Function returns an array of handles that support the requested protocol
>> +  in a buffer allocated from pool. This is a version of MmLocateHandle()
>> +  that allocates a buffer for the caller.
>> +
>> +  @param  SearchType             Specifies which handle(s) are to be returned.
>> +  @param  Protocol               Provides the protocol to search by.    This
>> +                                 parameter is only valid for SearchType
>> +                                 ByProtocol.
>> +  @param  SearchKey              Supplies the search key depending on the
>> +                                 SearchType.
>> +  @param  NumberHandles          The number of handles returned in Buffer.
>> +  @param  Buffer                 A pointer to the buffer to return the requested
>> +                                 array of  handles that support Protocol.
>> +
>> +  @retval EFI_SUCCESS            The result array of handles was returned.
>> +  @retval EFI_NOT_FOUND          No handles match the search.
>> +  @retval EFI_OUT_OF_RESOURCES   There is not enough pool memory to store the
>> +                                 matching results.
>> +  @retval EFI_INVALID_PARAMETER  One or more parameters are not valid.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmLocateHandleBuffer (
>> +  IN     EFI_LOCATE_SEARCH_TYPE  SearchType,
>> +  IN     EFI_GUID                *Protocol OPTIONAL,
>> +  IN     VOID                    *SearchKey OPTIONAL,
>> +  IN OUT UINTN                   *NumberHandles,
>> +  OUT    EFI_HANDLE              **Buffer
>> +  )
>> +{
>> +  EFI_STATUS  Status;
>> +  UINTN       BufferSize;
>> +
>> +  if (NumberHandles == NULL) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  if (Buffer == NULL) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  BufferSize = 0;
>> +  *NumberHandles = 0;
>> +  *Buffer = NULL;
>> +  Status = MmLocateHandle (
>> +             SearchType,
>> +             Protocol,
>> +             SearchKey,
>> +             &BufferSize,
>> +             *Buffer
>> +             );
>> +  //
>> +  // LocateHandleBuffer() returns incorrect status code if SearchType is
>> +  // invalid.
>> +  //
>> +  // Add code to correctly handle expected errors from MmLocateHandle().
>> +  //
>> +  if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {
>> +    if (Status != EFI_INVALID_PARAMETER) {
>> +      Status = EFI_NOT_FOUND;
>> +    }
>> +    return Status;
>> +  }
>> +
>> +  *Buffer = AllocatePool (BufferSize);
>> +  if (*Buffer == NULL) {
>> +    return EFI_OUT_OF_RESOURCES;
>> +  }
>> +
>> +  Status = MmLocateHandle (
>> +             SearchType,
>> +             Protocol,
>> +             SearchKey,
>> +             &BufferSize,
>> +             *Buffer
>> +             );
>> +
>> +  *NumberHandles = BufferSize / sizeof(EFI_HANDLE);
>> +  if (EFI_ERROR(Status)) {
>> +    *NumberHandles = 0;
>> +  }
>> +
>> +  return Status;
>> +}
>> diff --git a/StandaloneMmPkg/Core/Mmi.c b/StandaloneMmPkg/Core/Mmi.c
>> new file mode 100644
>> index 0000000000..29aba7b53d
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Core/Mmi.c
>> @@ -0,0 +1,337 @@
>> +/** @file
>> +  MMI management.
>> +
>> +  Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
>> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +  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
>> +
>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +
>> +**/
>> +
>> +#include "StandaloneMmCore.h"
>> +
>> +//
>> +// MM_HANDLER_STATE_NOTIFIER
>> +//
>> +
>> +//
>> +// MM_HANDLER - used for each MM handler
>> +//
>> +
>> +#define MMI_ENTRY_SIGNATURE  SIGNATURE_32('m','m','i','e')
>> +
>> + typedef struct {
>> +  UINTN       Signature;
>> +  LIST_ENTRY  AllEntries;  // All entries
>> +
>> +  EFI_GUID    HandlerType; // Type of interrupt
>> +  LIST_ENTRY  MmiHandlers; // All handlers
>> +} MMI_ENTRY;
>> +
>> +#define MMI_HANDLER_SIGNATURE  SIGNATURE_32('m','m','i','h')
>> +
>> + typedef struct {
>> +  UINTN                         Signature;
>> +  LIST_ENTRY                    Link;        // Link on MMI_ENTRY.MmiHandlers
>> +  EFI_MM_HANDLER_ENTRY_POINT    Handler;     // The mm handler's entry point
>> +  MMI_ENTRY                     *MmiEntry;
>> +} MMI_HANDLER;
>> +
>> +LIST_ENTRY  mRootMmiHandlerList = INITIALIZE_LIST_HEAD_VARIABLE (mRootMmiHandlerList);
>> +LIST_ENTRY  mMmiEntryList       = INITIALIZE_LIST_HEAD_VARIABLE (mMmiEntryList);
>> +
>> +/**
>> +  Finds the MMI entry for the requested handler type.
>> +
>> +  @param  HandlerType            The type of the interrupt
>> +  @param  Create                 Create a new entry if not found
>> +
>> +  @return MMI entry
>> +
>> +**/
>> +MMI_ENTRY  *
>> +EFIAPI
>> +MmCoreFindMmiEntry (
>> +  IN EFI_GUID  *HandlerType,
>> +  IN BOOLEAN   Create
>> +  )
>> +{
>> +  LIST_ENTRY  *Link;
>> +  MMI_ENTRY   *Item;
>> +  MMI_ENTRY   *MmiEntry;
>> +
>> +  //
>> +  // Search the MMI entry list for the matching GUID
>> +  //
>> +  MmiEntry = NULL;
>> +  for (Link = mMmiEntryList.ForwardLink;
>> +       Link != &mMmiEntryList;
>> +       Link = Link->ForwardLink) {
>> +
>> +    Item = CR (Link, MMI_ENTRY, AllEntries, MMI_ENTRY_SIGNATURE);
>> +    if (CompareGuid (&Item->HandlerType, HandlerType)) {
>> +      //
>> +      // This is the MMI entry
>> +      //
>> +      MmiEntry = Item;
>> +      break;
>> +    }
>> +  }
>> +
>> +  //
>> +  // If the protocol entry was not found and Create is TRUE, then
>> +  // allocate a new entry
>> +  //
>> +  if ((MmiEntry == NULL) && Create) {
>> +    MmiEntry = AllocatePool (sizeof(MMI_ENTRY));
>> +    if (MmiEntry != NULL) {
>> +      //
>> +      // Initialize new MMI entry structure
>> +      //
>> +      MmiEntry->Signature = MMI_ENTRY_SIGNATURE;
>> +      CopyGuid ((VOID *)&MmiEntry->HandlerType, HandlerType);
>> +      InitializeListHead (&MmiEntry->MmiHandlers);
>> +
>> +      //
>> +      // Add it to MMI entry list
>> +      //
>> +      InsertTailList (&mMmiEntryList, &MmiEntry->AllEntries);
>> +    }
>> +  }
>> +  return MmiEntry;
>> +}
>> +
>> +/**
>> +  Manage MMI of a particular type.
>> +
>> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
>> +  @param  Context        Points to an optional context buffer.
>> +  @param  CommBuffer     Points to the optional communication buffer.
>> +  @param  CommBufferSize Points to the size of the optional communication buffer.
>> +
>> +  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING  Interrupt source was processed successfully but not quiesced.
>> +  @retval EFI_INTERRUPT_PENDING              One or more MMI sources could not be quiesced.
>> +  @retval EFI_NOT_FOUND                      Interrupt source was not handled or quiesced.
>> +  @retval EFI_SUCCESS                        Interrupt source was handled and quiesced.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmiManage (
>> +  IN     CONST EFI_GUID  *HandlerType,
>> +  IN     CONST VOID      *Context         OPTIONAL,
>> +  IN OUT VOID            *CommBuffer      OPTIONAL,
>> +  IN OUT UINTN           *CommBufferSize  OPTIONAL
>> +  )
>> +{
>> +  LIST_ENTRY   *Link;
>> +  LIST_ENTRY   *Head;
>> +  MMI_ENTRY    *MmiEntry;
>> +  MMI_HANDLER  *MmiHandler;
>> +  BOOLEAN      SuccessReturn;
>> +  EFI_STATUS   Status;
>> +
>> +  Status = EFI_NOT_FOUND;
>> +  SuccessReturn = FALSE;
>> +  if (HandlerType == NULL) {
>> +    //
>> +    // Root MMI handler
>> +    //
>> +
>> +    Head = &mRootMmiHandlerList;
>> +  } else {
>> +    //
>> +    // Non-root MMI handler
>> +    //
>> +    MmiEntry = MmCoreFindMmiEntry ((EFI_GUID *) HandlerType, FALSE);
>> +    if (MmiEntry == NULL) {
>> +      //
>> +      // There is no handler registered for this interrupt source
>> +      //
>> +      return Status;
>> +    }
>> +
>> +    Head = &MmiEntry->MmiHandlers;
>> +  }
>> +
>> +  for (Link = Head->ForwardLink; Link != Head; Link = Link->ForwardLink) {
>> +    MmiHandler = CR (Link, MMI_HANDLER, Link, MMI_HANDLER_SIGNATURE);
>> +
>> +    Status = MmiHandler->Handler (
>> +               (EFI_HANDLE) MmiHandler,
>> +               Context,
>> +               CommBuffer,
>> +               CommBufferSize
>> +               );
>> +
>> +    switch (Status) {
>> +    case EFI_INTERRUPT_PENDING:
>> +      //
>> +      // If a handler returns EFI_INTERRUPT_PENDING and HandlerType is not NULL then
>> +      // no additional handlers will be processed and EFI_INTERRUPT_PENDING will be returned.
>> +      //
>> +      if (HandlerType != NULL) {
>> +        return EFI_INTERRUPT_PENDING;
>> +      }
>> +      break;
>> +
>> +    case EFI_SUCCESS:
>> +      //
>> +      // If at least one of the handlers returns EFI_SUCCESS then the function will return
>> +      // EFI_SUCCESS. If a handler returns EFI_SUCCESS and HandlerType is not NULL then no
>> +      // additional handlers will be processed.
>> +      //
>> +      if (HandlerType != NULL) {
>> +        return EFI_SUCCESS;
>> +      }
>> +      SuccessReturn = TRUE;
>> +      break;
>> +
>> +    case EFI_WARN_INTERRUPT_SOURCE_QUIESCED:
>> +      //
>> +      // If at least one of the handlers returns EFI_WARN_INTERRUPT_SOURCE_QUIESCED
>> +      // then the function will return EFI_SUCCESS.
>> +      //
>> +      SuccessReturn = TRUE;
>> +      break;
>> +
>> +    case EFI_WARN_INTERRUPT_SOURCE_PENDING:
>> +      //
>> +      // If all the handlers returned EFI_WARN_INTERRUPT_SOURCE_PENDING
>> +      // then EFI_WARN_INTERRUPT_SOURCE_PENDING will be returned.
>> +      //
>> +      break;
>> +
>> +    default:
>> +      //
>> +      // Unexpected status code returned.
>> +      //
>> +      ASSERT (FALSE);
>> +      break;
>> +    }
>> +  }
>> +
>> +  if (SuccessReturn) {
>> +    Status = EFI_SUCCESS;
>> +  }
>> +
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Registers a handler to execute within MM.
>> +
>> +  @param  Handler        Handler service funtion pointer.
>> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
>> +  @param  DispatchHandle On return, contains a unique handle which can be used to later unregister the handler function.
>> +
>> +  @retval EFI_SUCCESS           Handler register success.
>> +  @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmiHandlerRegister (
>> +  IN  EFI_MM_HANDLER_ENTRY_POINT    Handler,
>> +  IN  CONST EFI_GUID                *HandlerType  OPTIONAL,
>> +  OUT EFI_HANDLE                    *DispatchHandle
>> +  )
>> +{
>> +  MMI_HANDLER  *MmiHandler;
>> +  MMI_ENTRY    *MmiEntry;
>> +  LIST_ENTRY   *List;
>> +
>> +  if (Handler == NULL || DispatchHandle == NULL) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  MmiHandler = AllocateZeroPool (sizeof (MMI_HANDLER));
>> +  if (MmiHandler == NULL) {
>> +    return EFI_OUT_OF_RESOURCES;
>> +  }
>> +
>> +  MmiHandler->Signature = MMI_HANDLER_SIGNATURE;
>> +  MmiHandler->Handler = Handler;
>> +
>> +  if (HandlerType == NULL) {
>> +    //
>> +    // This is root MMI handler
>> +    //
>> +    MmiEntry = NULL;
>> +    List = &mRootMmiHandlerList;
>> +  } else {
>> +    //
>> +    // None root MMI handler
>> +    //
>> +    MmiEntry = MmCoreFindMmiEntry ((EFI_GUID *) HandlerType, TRUE);
>> +    if (MmiEntry == NULL) {
>> +      return EFI_OUT_OF_RESOURCES;
>> +    }
>> +
>> +    List = &MmiEntry->MmiHandlers;
>> +  }
>> +
>> +  MmiHandler->MmiEntry = MmiEntry;
>> +  InsertTailList (List, &MmiHandler->Link);
>> +
>> +  *DispatchHandle = (EFI_HANDLE) MmiHandler;
>> +
>> +  return EFI_SUCCESS;
>> +}
>> +
>> +/**
>> +  Unregister a handler in MM.
>> +
>> +  @param  DispatchHandle  The handle that was specified when the handler was registered.
>> +
>> +  @retval EFI_SUCCESS           Handler function was successfully unregistered.
>> +  @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid handle.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmiHandlerUnRegister (
>> +  IN EFI_HANDLE  DispatchHandle
>> +  )
>> +{
>> +  MMI_HANDLER  *MmiHandler;
>> +  MMI_ENTRY    *MmiEntry;
>> +
>> +  MmiHandler = (MMI_HANDLER *) DispatchHandle;
>> +
>> +  if (MmiHandler == NULL) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  if (MmiHandler->Signature != MMI_HANDLER_SIGNATURE) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  MmiEntry = MmiHandler->MmiEntry;
>> +
>> +  RemoveEntryList (&MmiHandler->Link);
>> +  FreePool (MmiHandler);
>> +
>> +  if (MmiEntry == NULL) {
>> +    //
>> +    // This is root MMI handler
>> +    //
>> +    return EFI_SUCCESS;
>> +  }
>> +
>> +  if (IsListEmpty (&MmiEntry->MmiHandlers)) {
>> +    //
>> +    // No handler registered for this interrupt now, remove the MMI_ENTRY
>> +    //
>> +    RemoveEntryList (&MmiEntry->AllEntries);
>> +
>> +    FreePool (MmiEntry);
>> +  }
>> +
>> +  return EFI_SUCCESS;
>> +}
>> diff --git a/StandaloneMmPkg/Core/Notify.c b/StandaloneMmPkg/Core/Notify.c
>> new file mode 100644
>> index 0000000000..d5fc8f50d1
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Core/Notify.c
>> @@ -0,0 +1,203 @@
>> +/** @file
>> +  Support functions for UEFI protocol notification infrastructure.
>> +
>> +  Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
>> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +  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
>> +
>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +
>> +**/
>> +
>> +#include "StandaloneMmCore.h"
>> +
>> +/**
>> +  Signal event for every protocol in protocol entry.
>> +
>> +  @param  Prot                   Protocol interface
>> +
>> +**/
>> +VOID
>> +MmNotifyProtocol (
>> +  IN PROTOCOL_INTERFACE  *Prot
>> +  )
>> +{
>> +  PROTOCOL_ENTRY   *ProtEntry;
>> +  PROTOCOL_NOTIFY  *ProtNotify;
>> +  LIST_ENTRY       *Link;
>> +
>> +  ProtEntry = Prot->Protocol;
>> +  for (Link=ProtEntry->Notify.ForwardLink; Link != &ProtEntry->Notify; Link=Link->ForwardLink) {
>> +    ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
>> +    ProtNotify->Function (&ProtEntry->ProtocolID, Prot->Interface, Prot->Handle);
>> +  }
>> +}
>> +
>> +/**
>> +  Removes Protocol from the protocol list (but not the handle list).
>> +
>> +  @param  Handle                 The handle to remove protocol on.
>> +  @param  Protocol               GUID of the protocol to be moved
>> +  @param  Interface              The interface of the protocol
>> +
>> +  @return Protocol Entry
>> +
>> +**/
>> +PROTOCOL_INTERFACE *
>> +MmRemoveInterfaceFromProtocol (
>> +  IN IHANDLE   *Handle,
>> +  IN EFI_GUID  *Protocol,
>> +  IN VOID      *Interface
>> +  )
>> +{
>> +  PROTOCOL_INTERFACE  *Prot;
>> +  PROTOCOL_NOTIFY     *ProtNotify;
>> +  PROTOCOL_ENTRY      *ProtEntry;
>> +  LIST_ENTRY          *Link;
>> +
>> +  Prot = MmFindProtocolInterface (Handle, Protocol, Interface);
>> +  if (Prot != NULL) {
>> +
>> +    ProtEntry = Prot->Protocol;
>> +
>> +    //
>> +    // If there's a protocol notify location pointing to this entry, back it up one
>> +    //
>> +    for(Link = ProtEntry->Notify.ForwardLink; Link != &ProtEntry->Notify; Link=Link->ForwardLink) {
>> +      ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
>> +
>> +      if (ProtNotify->Position == &Prot->ByProtocol) {
>> +        ProtNotify->Position = Prot->ByProtocol.BackLink;
>> +      }
>> +    }
>> +
>> +    //
>> +    // Remove the protocol interface entry
>> +    //
>> +    RemoveEntryList (&Prot->ByProtocol);
>> +  }
>> +
>> +  return Prot;
>> +}
>> +
>> +/**
>> +  Add a new protocol notification record for the request protocol.
>> +
>> +  @param  Protocol               The requested protocol to add the notify
>> +                                 registration
>> +  @param  Function               Points to the notification function
>> +  @param  Registration           Returns the registration record
>> +
>> +  @retval EFI_SUCCESS            Successfully returned the registration record
>> +                                 that has been added or unhooked
>> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL or Registration is NULL
>> +  @retval EFI_OUT_OF_RESOURCES   Not enough memory resource to finish the request
>> +  @retval EFI_NOT_FOUND          If the registration is not found when Function == NULL
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmRegisterProtocolNotify (
>> +  IN  CONST EFI_GUID     *Protocol,
>> +  IN  EFI_MM_NOTIFY_FN  Function,
>> +  OUT VOID               **Registration
>> +  )
>> +{
>> +  PROTOCOL_ENTRY   *ProtEntry;
>> +  PROTOCOL_NOTIFY  *ProtNotify;
>> +  LIST_ENTRY       *Link;
>> +  EFI_STATUS       Status;
>> +
>> +  if (Protocol == NULL || Registration == NULL) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  if (Function == NULL) {
>> +    //
>> +    // Get the protocol entry per Protocol
>> +    //
>> +    ProtEntry = MmFindProtocolEntry ((EFI_GUID *) Protocol, FALSE);
>> +    if (ProtEntry != NULL) {
>> +      ProtNotify = (PROTOCOL_NOTIFY * )*Registration;
>> +      for (Link = ProtEntry->Notify.ForwardLink;
>> +           Link != &ProtEntry->Notify;
>> +           Link = Link->ForwardLink) {
>> +        //
>> +        // Compare the notification record
>> +        //
>> +        if (ProtNotify == (CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE))){
>> +          //
>> +          // If Registration is an existing registration, then unhook it
>> +          //
>> +          ProtNotify->Signature = 0;
>> +          RemoveEntryList (&ProtNotify->Link);
>> +          FreePool (ProtNotify);
>> +          return EFI_SUCCESS;
>> +        }
>> +      }
>> +    }
>> +    //
>> +    // If the registration is not found
>> +    //
>> +    return EFI_NOT_FOUND;
>> +  }
>> +
>> +  ProtNotify = NULL;
>> +
>> +  //
>> +  // Get the protocol entry to add the notification too
>> +  //
>> +  ProtEntry = MmFindProtocolEntry ((EFI_GUID *) Protocol, TRUE);
>> +  if (ProtEntry != NULL) {
>> +    //
>> +    // Find whether notification already exist
>> +    //
>> +    for (Link = ProtEntry->Notify.ForwardLink;
>> +         Link != &ProtEntry->Notify;
>> +         Link = Link->ForwardLink) {
>> +
>> +      ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
>> +      if (CompareGuid (&ProtNotify->Protocol->ProtocolID, Protocol) &&
>> +          (ProtNotify->Function == Function)) {
>> +
>> +        //
>> +        // Notification already exist
>> +        //
>> +        *Registration = ProtNotify;
>> +
>> +        return EFI_SUCCESS;
>> +      }
>> +    }
>> +
>> +    //
>> +    // Allocate a new notification record
>> +    //
>> +    ProtNotify = AllocatePool (sizeof(PROTOCOL_NOTIFY));
>> +    if (ProtNotify != NULL) {
>> +      ProtNotify->Signature = PROTOCOL_NOTIFY_SIGNATURE;
>> +      ProtNotify->Protocol = ProtEntry;
>> +      ProtNotify->Function = Function;
>> +      //
>> +      // Start at the ending
>> +      //
>> +      ProtNotify->Position = ProtEntry->Protocols.BackLink;
>> +
>> +      InsertTailList (&ProtEntry->Notify, &ProtNotify->Link);
>> +    }
>> +  }
>> +
>> +  //
>> +  // Done.  If we have a protocol notify entry, then return it.
>> +  // Otherwise, we must have run out of resources trying to add one
>> +  //
>> +  Status = EFI_OUT_OF_RESOURCES;
>> +  if (ProtNotify != NULL) {
>> +    *Registration = ProtNotify;
>> +    Status = EFI_SUCCESS;
>> +  }
>> +  return Status;
>> +}
>> diff --git a/StandaloneMmPkg/Core/Page.c b/StandaloneMmPkg/Core/Page.c
>> new file mode 100644
>> index 0000000000..ba3e7cea74
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Core/Page.c
>> @@ -0,0 +1,384 @@
>> +/** @file
>> +  MM Memory page management functions.
>> +
>> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
>> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +  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
>> +
>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +
>> +**/
>> +
>> +#include "StandaloneMmCore.h"
>> +
>> +#define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \
>> +  ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size)))
>> +
>> +#define TRUNCATE_TO_PAGES(a)  ((a) >> EFI_PAGE_SHIFT)
>> +
>> +LIST_ENTRY  mMmMemoryMap = INITIALIZE_LIST_HEAD_VARIABLE (mMmMemoryMap);
>> +
>> +UINTN mMapKey;
>> +
>> +/**
>> +  Internal Function. Allocate n pages from given free page node.
>> +
>> +  @param  Pages                  The free page node.
>> +  @param  NumberOfPages          Number of pages to be allocated.
>> +  @param  MaxAddress             Request to allocate memory below this address.
>> +
>> +  @return Memory address of allocated pages.
>> +
>> +**/
>> +UINTN
>> +InternalAllocPagesOnOneNode (
>> +  IN OUT FREE_PAGE_LIST  *Pages,
>> +  IN     UINTN           NumberOfPages,
>> +  IN     UINTN           MaxAddress
>> +  )
>> +{
>> +  UINTN           Top;
>> +  UINTN           Bottom;
>> +  FREE_PAGE_LIST  *Node;
>> +
>> +  Top = TRUNCATE_TO_PAGES (MaxAddress + 1 - (UINTN)Pages);
>> +  if (Top > Pages->NumberOfPages) {
>> +    Top = Pages->NumberOfPages;
>> +  }
>> +  Bottom = Top - NumberOfPages;
>> +
>> +  if (Top < Pages->NumberOfPages) {
>> +    Node = (FREE_PAGE_LIST*)((UINTN)Pages + EFI_PAGES_TO_SIZE (Top));
>> +    Node->NumberOfPages = Pages->NumberOfPages - Top;
>> +    InsertHeadList (&Pages->Link, &Node->Link);
>> +  }
>> +
>> +  if (Bottom > 0) {
>> +    Pages->NumberOfPages = Bottom;
>> +  } else {
>> +    RemoveEntryList (&Pages->Link);
>> +  }
>> +
>> +  return (UINTN)Pages + EFI_PAGES_TO_SIZE (Bottom);
>> +}
>> +
>> +/**
>> +  Internal Function. Allocate n pages from free page list below MaxAddress.
>> +
>> +  @param  FreePageList           The free page node.
>> +  @param  NumberOfPages          Number of pages to be allocated.
>> +  @param  MaxAddress             Request to allocate memory below this address.
>> +
>> +  @return Memory address of allocated pages.
>> +
>> +**/
>> +UINTN
>> +InternalAllocMaxAddress (
>> +  IN OUT LIST_ENTRY  *FreePageList,
>> +  IN     UINTN       NumberOfPages,
>> +  IN     UINTN       MaxAddress
>> +  )
>> +{
>> +  LIST_ENTRY      *Node;
>> +  FREE_PAGE_LIST  *Pages;
>> +
>> +  for (Node = FreePageList->BackLink; Node != FreePageList; Node = Node->BackLink) {
>> +    Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
>> +    if (Pages->NumberOfPages >= NumberOfPages &&
>> +        (UINTN)Pages + EFI_PAGES_TO_SIZE (NumberOfPages) - 1 <= MaxAddress) {
>> +      return InternalAllocPagesOnOneNode (Pages, NumberOfPages, MaxAddress);
>> +    }
>> +  }
>> +  return (UINTN)(-1);
>> +}
>> +
>> +/**
>> +  Internal Function. Allocate n pages from free page list at given address.
>> +
>> +  @param  FreePageList           The free page node.
>> +  @param  NumberOfPages          Number of pages to be allocated.
>> +  @param  MaxAddress             Request to allocate memory below this address.
>> +
>> +  @return Memory address of allocated pages.
>> +
>> +**/
>> +UINTN
>> +InternalAllocAddress (
>> +  IN OUT LIST_ENTRY  *FreePageList,
>> +  IN     UINTN       NumberOfPages,
>> +  IN     UINTN       Address
>> +  )
>> +{
>> +  UINTN           EndAddress;
>> +  LIST_ENTRY      *Node;
>> +  FREE_PAGE_LIST  *Pages;
>> +
>> +  if ((Address & EFI_PAGE_MASK) != 0) {
>> +    return ~Address;
>> +  }
>> +
>> +  EndAddress = Address + EFI_PAGES_TO_SIZE (NumberOfPages);
>> +  for (Node = FreePageList->BackLink; Node!= FreePageList; Node = Node->BackLink) {
>> +    Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
>> +    if ((UINTN)Pages <= Address) {
>> +      if ((UINTN)Pages + EFI_PAGES_TO_SIZE (Pages->NumberOfPages) < EndAddress) {
>> +        break;
>> +      }
>> +      return InternalAllocPagesOnOneNode (Pages, NumberOfPages, EndAddress);
>> +    }
>> +  }
>> +  return ~Address;
>> +}
>> +
>> +/**
>> +  Allocates pages from the memory map.
>> +
>> +  @param  Type                   The type of allocation to perform.
>> +  @param  MemoryType             The type of memory to turn the allocated pages
>> +                                 into.
>> +  @param  NumberOfPages          The number of pages to allocate.
>> +  @param  Memory                 A pointer to receive the base allocated memory
>> +                                 address.
>> +
>> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
>> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
>> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
>> +  @retval EFI_SUCCESS            Pages successfully allocated.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmInternalAllocatePages (
>> +  IN  EFI_ALLOCATE_TYPE     Type,
>> +  IN  EFI_MEMORY_TYPE       MemoryType,
>> +  IN  UINTN                 NumberOfPages,
>> +  OUT EFI_PHYSICAL_ADDRESS  *Memory
>> +  )
>> +{
>> +  UINTN  RequestedAddress;
>> +
>> +  if (MemoryType != EfiRuntimeServicesCode &&
>> +      MemoryType != EfiRuntimeServicesData) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  if (NumberOfPages > TRUNCATE_TO_PAGES ((UINTN)-1) + 1) {
>> +    return EFI_OUT_OF_RESOURCES;
>> +  }
>> +
>> +  //
>> +  // We don't track memory type in MM
>> +  //
>> +  RequestedAddress = (UINTN)*Memory;
>> +  switch (Type) {
>> +    case AllocateAnyPages:
>> +      RequestedAddress = (UINTN)(-1);
>> +    case AllocateMaxAddress:
>> +      *Memory = InternalAllocMaxAddress (
>> +                  &mMmMemoryMap,
>> +                  NumberOfPages,
>> +                  RequestedAddress
>> +                  );
>> +      if (*Memory == (UINTN)-1) {
>> +        return EFI_OUT_OF_RESOURCES;
>> +      }
>> +      break;
>> +    case AllocateAddress:
>> +      *Memory = InternalAllocAddress (
>> +                  &mMmMemoryMap,
>> +                  NumberOfPages,
>> +                  RequestedAddress
>> +                  );
>> +      if (*Memory != RequestedAddress) {
>> +        return EFI_NOT_FOUND;
>> +      }
>> +      break;
>> +    default:
>> +      return EFI_INVALID_PARAMETER;
>> +  }
>> +  return EFI_SUCCESS;
>> +}
>> +
>> +/**
>> +  Allocates pages from the memory map.
>> +
>> +  @param  Type                   The type of allocation to perform.
>> +  @param  MemoryType             The type of memory to turn the allocated pages
>> +                                 into.
>> +  @param  NumberOfPages          The number of pages to allocate.
>> +  @param  Memory                 A pointer to receive the base allocated memory
>> +                                 address.
>> +
>> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
>> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
>> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
>> +  @retval EFI_SUCCESS            Pages successfully allocated.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmAllocatePages (
>> +  IN  EFI_ALLOCATE_TYPE     Type,
>> +  IN  EFI_MEMORY_TYPE       MemoryType,
>> +  IN  UINTN                 NumberOfPages,
>> +  OUT EFI_PHYSICAL_ADDRESS  *Memory
>> +  )
>> +{
>> +  EFI_STATUS  Status;
>> +
>> +  Status = MmInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory);
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Internal Function. Merge two adjacent nodes.
>> +
>> +  @param  First             The first of two nodes to merge.
>> +
>> +  @return Pointer to node after merge (if success) or pointer to next node (if fail).
>> +
>> +**/
>> +FREE_PAGE_LIST *
>> +InternalMergeNodes (
>> +  IN FREE_PAGE_LIST  *First
>> +  )
>> +{
>> +  FREE_PAGE_LIST  *Next;
>> +
>> +  Next = BASE_CR (First->Link.ForwardLink, FREE_PAGE_LIST, Link);
>> +  ASSERT (
>> +    TRUNCATE_TO_PAGES ((UINTN)Next - (UINTN)First) >= First->NumberOfPages);
>> +
>> +  if (TRUNCATE_TO_PAGES ((UINTN)Next - (UINTN)First) == First->NumberOfPages) {
>> +    First->NumberOfPages += Next->NumberOfPages;
>> +    RemoveEntryList (&Next->Link);
>> +    Next = First;
>> +  }
>> +  return Next;
>> +}
>> +
>> +/**
>> +  Frees previous allocated pages.
>> +
>> +  @param  Memory                 Base address of memory being freed.
>> +  @param  NumberOfPages          The number of pages to free.
>> +
>> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range.
>> +  @retval EFI_INVALID_PARAMETER  Address not aligned.
>> +  @return EFI_SUCCESS            Pages successfully freed.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmInternalFreePages (
>> +  IN EFI_PHYSICAL_ADDRESS  Memory,
>> +  IN UINTN                 NumberOfPages
>> +  )
>> +{
>> +  LIST_ENTRY      *Node;
>> +  FREE_PAGE_LIST  *Pages;
>> +
>> +  if ((Memory & EFI_PAGE_MASK) != 0) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  Pages = NULL;
>> +  Node = mMmMemoryMap.ForwardLink;
>> +  while (Node != &mMmMemoryMap) {
>> +    Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
>> +    if (Memory < (UINTN)Pages) {
>> +      break;
>> +    }
>> +    Node = Node->ForwardLink;
>> +  }
>> +
>> +  if (Node != &mMmMemoryMap &&
>> +      Memory + EFI_PAGES_TO_SIZE (NumberOfPages) > (UINTN)Pages) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  if (Node->BackLink != &mMmMemoryMap) {
>> +    Pages = BASE_CR (Node->BackLink, FREE_PAGE_LIST, Link);
>> +    if ((UINTN)Pages + EFI_PAGES_TO_SIZE (Pages->NumberOfPages) > Memory) {
>> +      return EFI_INVALID_PARAMETER;
>> +    }
>> +  }
>> +
>> +  Pages = (FREE_PAGE_LIST*)(UINTN)Memory;
>> +  Pages->NumberOfPages = NumberOfPages;
>> +  InsertTailList (Node, &Pages->Link);
>> +
>> +  if (Pages->Link.BackLink != &mMmMemoryMap) {
>> +    Pages = InternalMergeNodes (
>> +              BASE_CR (Pages->Link.BackLink, FREE_PAGE_LIST, Link)
>> +              );
>> +  }
>> +
>> +  if (Node != &mMmMemoryMap) {
>> +    InternalMergeNodes (Pages);
>> +  }
>> +
>> +  return EFI_SUCCESS;
>> +}
>> +
>> +/**
>> +  Frees previous allocated pages.
>> +
>> +  @param  Memory                 Base address of memory being freed.
>> +  @param  NumberOfPages          The number of pages to free.
>> +
>> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range.
>> +  @retval EFI_INVALID_PARAMETER  Address not aligned.
>> +  @return EFI_SUCCESS            Pages successfully freed.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmFreePages (
>> +  IN EFI_PHYSICAL_ADDRESS  Memory,
>> +  IN UINTN                 NumberOfPages
>> +  )
>> +{
>> +  EFI_STATUS  Status;
>> +
>> +  Status = MmInternalFreePages (Memory, NumberOfPages);
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Add free MMRAM region for use by memory service.
>> +
>> +  @param  MemBase                Base address of memory region.
>> +  @param  MemLength              Length of the memory region.
>> +  @param  Type                   Memory type.
>> +  @param  Attributes             Memory region state.
>> +
>> +**/
>> +VOID
>> +MmAddMemoryRegion (
>> +  IN  EFI_PHYSICAL_ADDRESS  MemBase,
>> +  IN  UINT64                MemLength,
>> +  IN  EFI_MEMORY_TYPE       Type,
>> +  IN  UINT64                Attributes
>> +  )
>> +{
>> +  UINTN  AlignedMemBase;
>> +
>> +  //
>> +  // Do not add memory regions that is already allocated, needs testing, or needs ECC initialization
>> +  //
>> +  if ((Attributes & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {
>> +    return;
>> +  }
>> +
>> +  //
>> +  // Align range on an EFI_PAGE_SIZE boundary
>> +  //
>> +  AlignedMemBase = (UINTN)(MemBase + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
>> +  MemLength -= AlignedMemBase - MemBase;
>> +  MmFreePages (AlignedMemBase, TRUNCATE_TO_PAGES ((UINTN)MemLength));
>> +}
>> diff --git a/StandaloneMmPkg/Core/Pool.c b/StandaloneMmPkg/Core/Pool.c
>> new file mode 100644
>> index 0000000000..bdf1258381
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Core/Pool.c
>> @@ -0,0 +1,287 @@
>> +/** @file
>> +  SMM Memory pool management functions.
>> +
>> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
>> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +  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
>> +
>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +
>> +**/
>> +
>> +#include "StandaloneMmCore.h"
>> +
>> +LIST_ENTRY  mMmPoolLists[MAX_POOL_INDEX];
>> +//
>> +// To cache the MMRAM base since when Loading modules At fixed address feature is enabled,
>> +// all module is assigned an offset relative the MMRAM base in build time.
>> +//
>> +GLOBAL_REMOVE_IF_UNREFERENCED  EFI_PHYSICAL_ADDRESS       gLoadModuleAtFixAddressMmramBase = 0;
>> +
>> +/**
>> +  Called to initialize the memory service.
>> +
>> +  @param   MmramRangeCount       Number of MMRAM Regions
>> +  @param   MmramRanges           Pointer to MMRAM Descriptors
>> +
>> +**/
>> +VOID
>> +MmInitializeMemoryServices (
>> +  IN UINTN                 MmramRangeCount,
>> +  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
>> +  )
>> +{
>> +  UINTN                  Index;
>> +
>> +  //
>> +  // Initialize Pool list
>> +  //
>> +  for (Index = sizeof (mMmPoolLists) / sizeof (*mMmPoolLists); Index > 0;) {
>> +    InitializeListHead (&mMmPoolLists[--Index]);
>> +  }
>> +
>> +
>> +  //
>> +  // Initialize free MMRAM regions
>> +  //
>> +  for (Index = 0; Index < MmramRangeCount; Index++) {
>> +    //
>> +    // BUGBUG: Add legacy MMRAM region is buggy.
>> +    //
>> +    if (MmramRanges[Index].CpuStart < BASE_1MB) {
>> +      continue;
>> +    }
>> +    DEBUG ((DEBUG_INFO, "MmAddMemoryRegion %d : 0x%016lx - 0x%016lx\n", Index, MmramRanges[Index].CpuStart, MmramRanges[Index].PhysicalSize));
>> +    MmAddMemoryRegion (
>> +      MmramRanges[Index].CpuStart,
>> +      MmramRanges[Index].PhysicalSize,
>> +      EfiConventionalMemory,
>> +      MmramRanges[Index].RegionState
>> +      );
>> +  }
>> +
>> +}
>> +
>> +/**
>> +  Internal Function. Allocate a pool by specified PoolIndex.
>> +
>> +  @param  PoolIndex             Index which indicate the Pool size.
>> +  @param  FreePoolHdr           The returned Free pool.
>> +
>> +  @retval EFI_OUT_OF_RESOURCES   Allocation failed.
>> +  @retval EFI_SUCCESS            Pool successfully allocated.
>> +
>> +**/
>> +EFI_STATUS
>> +InternalAllocPoolByIndex (
>> +  IN  UINTN             PoolIndex,
>> +  OUT FREE_POOL_HEADER  **FreePoolHdr
>> +  )
>> +{
>> +  EFI_STATUS            Status;
>> +  FREE_POOL_HEADER      *Hdr;
>> +  EFI_PHYSICAL_ADDRESS  Address;
>> +
>> +  ASSERT (PoolIndex <= MAX_POOL_INDEX);
>> +  Status = EFI_SUCCESS;
>> +  Hdr = NULL;
>> +  if (PoolIndex == MAX_POOL_INDEX) {
>> +    Status = MmInternalAllocatePages (AllocateAnyPages, EfiRuntimeServicesData, EFI_SIZE_TO_PAGES (MAX_POOL_SIZE << 1), &Address);
>> +    if (EFI_ERROR (Status)) {
>> +      return EFI_OUT_OF_RESOURCES;
>> +    }
>> +    Hdr = (FREE_POOL_HEADER *) (UINTN) Address;
>> +  } else if (!IsListEmpty (&mMmPoolLists[PoolIndex])) {
>> +    Hdr = BASE_CR (GetFirstNode (&mMmPoolLists[PoolIndex]), FREE_POOL_HEADER, Link);
>> +    RemoveEntryList (&Hdr->Link);
>> +  } else {
>> +    Status = InternalAllocPoolByIndex (PoolIndex + 1, &Hdr);
>> +    if (!EFI_ERROR (Status)) {
>> +      Hdr->Header.Size >>= 1;
>> +      Hdr->Header.Available = TRUE;
>> +      InsertHeadList (&mMmPoolLists[PoolIndex], &Hdr->Link);
>> +      Hdr = (FREE_POOL_HEADER*)((UINT8*)Hdr + Hdr->Header.Size);
>> +    }
>> +  }
>> +
>> +  if (!EFI_ERROR (Status)) {
>> +    Hdr->Header.Size = MIN_POOL_SIZE << PoolIndex;
>> +    Hdr->Header.Available = FALSE;
>> +  }
>> +
>> +  *FreePoolHdr = Hdr;
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Internal Function. Free a pool by specified PoolIndex.
>> +
>> +  @param  FreePoolHdr           The pool to free.
>> +
>> +  @retval EFI_SUCCESS           Pool successfully freed.
>> +
>> +**/
>> +EFI_STATUS
>> +InternalFreePoolByIndex (
>> +  IN FREE_POOL_HEADER  *FreePoolHdr
>> +  )
>> +{
>> +  UINTN  PoolIndex;
>> +
>> +  ASSERT ((FreePoolHdr->Header.Size & (FreePoolHdr->Header.Size - 1)) == 0);
>> +  ASSERT (((UINTN)FreePoolHdr & (FreePoolHdr->Header.Size - 1)) == 0);
>> +  ASSERT (FreePoolHdr->Header.Size >= MIN_POOL_SIZE);
>> +
>> +  PoolIndex = (UINTN) (HighBitSet32 ((UINT32)FreePoolHdr->Header.Size) - MIN_POOL_SHIFT);
>> +  FreePoolHdr->Header.Available = TRUE;
>> +  ASSERT (PoolIndex < MAX_POOL_INDEX);
>> +  InsertHeadList (&mMmPoolLists[PoolIndex], &FreePoolHdr->Link);
>> +  return EFI_SUCCESS;
>> +}
>> +
>> +/**
>> +  Allocate pool of a particular type.
>> +
>> +  @param  PoolType               Type of pool to allocate.
>> +  @param  Size                   The amount of pool to allocate.
>> +  @param  Buffer                 The address to return a pointer to the allocated
>> +                                 pool.
>> +
>> +  @retval EFI_INVALID_PARAMETER  PoolType not valid.
>> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
>> +  @retval EFI_SUCCESS            Pool successfully allocated.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmInternalAllocatePool (
>> +  IN   EFI_MEMORY_TYPE  PoolType,
>> +  IN   UINTN            Size,
>> +  OUT  VOID             **Buffer
>> +  )
>> +{
>> +  POOL_HEADER           *PoolHdr;
>> +  FREE_POOL_HEADER      *FreePoolHdr;
>> +  EFI_STATUS            Status;
>> +  EFI_PHYSICAL_ADDRESS  Address;
>> +  UINTN                 PoolIndex;
>> +
>> +  if (PoolType != EfiRuntimeServicesCode &&
>> +      PoolType != EfiRuntimeServicesData) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  Size += sizeof (*PoolHdr);
>> +  if (Size > MAX_POOL_SIZE) {
>> +    Size = EFI_SIZE_TO_PAGES (Size);
>> +    Status = MmInternalAllocatePages (AllocateAnyPages, PoolType, Size, &Address);
>> +    if (EFI_ERROR (Status)) {
>> +      return Status;
>> +    }
>> +
>> +    PoolHdr = (POOL_HEADER*)(UINTN)Address;
>> +    PoolHdr->Size = EFI_PAGES_TO_SIZE (Size);
>> +    PoolHdr->Available = FALSE;
>> +    *Buffer = PoolHdr + 1;
>> +    return Status;
>> +  }
>> +
>> +  Size = (Size + MIN_POOL_SIZE - 1) >> MIN_POOL_SHIFT;
>> +  PoolIndex = (UINTN) HighBitSet32 ((UINT32)Size);
>> +  if ((Size & (Size - 1)) != 0) {
>> +    PoolIndex++;
>> +  }
>> +
>> +  Status = InternalAllocPoolByIndex (PoolIndex, &FreePoolHdr);
>> +  if (!EFI_ERROR(Status)) {
>> +    *Buffer = &FreePoolHdr->Header + 1;
>> +  }
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Allocate pool of a particular type.
>> +
>> +  @param  PoolType               Type of pool to allocate.
>> +  @param  Size                   The amount of pool to allocate.
>> +  @param  Buffer                 The address to return a pointer to the allocated
>> +                                 pool.
>> +
>> +  @retval EFI_INVALID_PARAMETER  PoolType not valid.
>> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
>> +  @retval EFI_SUCCESS            Pool successfully allocated.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmAllocatePool (
>> +  IN   EFI_MEMORY_TYPE  PoolType,
>> +  IN   UINTN            Size,
>> +  OUT  VOID             **Buffer
>> +  )
>> +{
>> +  EFI_STATUS  Status;
>> +
>> +  Status = MmInternalAllocatePool (PoolType, Size, Buffer);
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Frees pool.
>> +
>> +  @param  Buffer                 The allocated pool entry to free.
>> +
>> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
>> +  @retval EFI_SUCCESS            Pool successfully freed.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmInternalFreePool (
>> +  IN VOID  *Buffer
>> +  )
>> +{
>> +  FREE_POOL_HEADER  *FreePoolHdr;
>> +
>> +  if (Buffer == NULL) {
>> +    return EFI_INVALID_PARAMETER;
>> +  }
>> +
>> +  FreePoolHdr = (FREE_POOL_HEADER*)((POOL_HEADER*)Buffer - 1);
>> +  ASSERT (!FreePoolHdr->Header.Available);
>> +
>> +  if (FreePoolHdr->Header.Size > MAX_POOL_SIZE) {
>> +    ASSERT (((UINTN)FreePoolHdr & EFI_PAGE_MASK) == 0);
>> +    ASSERT ((FreePoolHdr->Header.Size & EFI_PAGE_MASK) == 0);
>> +    return MmInternalFreePages (
>> +             (EFI_PHYSICAL_ADDRESS)(UINTN)FreePoolHdr,
>> +             EFI_SIZE_TO_PAGES (FreePoolHdr->Header.Size)
>> +             );
>> +  }
>> +  return InternalFreePoolByIndex (FreePoolHdr);
>> +}
>> +
>> +/**
>> +  Frees pool.
>> +
>> +  @param  Buffer                 The allocated pool entry to free.
>> +
>> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
>> +  @retval EFI_SUCCESS            Pool successfully freed.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmFreePool (
>> +  IN VOID  *Buffer
>> +  )
>> +{
>> +  EFI_STATUS  Status;
>> +
>> +  Status = MmInternalFreePool (Buffer);
>> +  return Status;
>> +}
>> diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.c b/StandaloneMmPkg/Core/StandaloneMmCore.c
>> new file mode 100644
>> index 0000000000..0bb99b9710
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Core/StandaloneMmCore.c
>> @@ -0,0 +1,708 @@
>> +/** @file
>> +  MM Core Main Entry Point
>> +
>> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
>> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +  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
>> +
>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +
>> +**/
>> +
>> +#include "StandaloneMmCore.h"
>> +
>> +EFI_STATUS
>> +MmCoreFfsFindMmDriver (
>> +  IN  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader
>> +  );
>> +
>> +EFI_STATUS
>> +MmDispatcher (
>> +  VOID
>> +  );
>> +
>> +//
>> +// Globals used to initialize the protocol
>> +//
>> +EFI_HANDLE            mMmCpuHandle = NULL;
>> +
>> +//
>> +// Physical pointer to private structure shared between MM IPL and the MM Core
>> +//
>> +MM_CORE_PRIVATE_DATA  *gMmCorePrivate;
>> +
>> +//
>> +// MM Core global variable for MM System Table.  Only accessed as a physical structure in MMRAM.
>> +//
>> +EFI_MM_SYSTEM_TABLE  gMmCoreMmst = {
>> +
>> +  // The table header for the MMST.
>> +  {
>> +    MM_MMST_SIGNATURE,
>> +    EFI_MM_SYSTEM_TABLE_REVISION,
>> +    sizeof (gMmCoreMmst.Hdr)
>> +  },
>> +  // MmFirmwareVendor
>> +  NULL,
>> +  // MmFirmwareRevision
>> +  0,
>> +  // MmInstallConfigurationTable
>> +  MmInstallConfigurationTable,
>> +  // I/O Service
>> +  {
>> +    {
>> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5,       // MmMemRead
>> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5        // MmMemWrite
>> +    },
>> +    {
>> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5,       // MmIoRead
>> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5        // MmIoWrite
>> +    }
>> +  },
>> +  // Runtime memory services
>> +  MmAllocatePool,
>> +  MmFreePool,
>> +  MmAllocatePages,
>> +  MmFreePages,
>> +  // MP service
>> +  NULL,                          // MmStartupThisAp
>> +  0,                             // CurrentlyExecutingCpu
>> +  0,                             // NumberOfCpus
>> +  NULL,                          // CpuSaveStateSize
>> +  NULL,                          // CpuSaveState
>> +  0,                             // NumberOfTableEntries
>> +  NULL,                          // MmConfigurationTable
>> +  MmInstallProtocolInterface,
>> +  MmUninstallProtocolInterface,
>> +  MmHandleProtocol,
>> +  MmRegisterProtocolNotify,
>> +  MmLocateHandle,
>> +  MmLocateProtocol,
>> +  MmiManage,
>> +  MmiHandlerRegister,
>> +  MmiHandlerUnRegister
>> +};
>> +
>> +//
>> +// Flag to determine if the platform has performed a legacy boot.
>> +// If this flag is TRUE, then the runtime code and runtime data associated with the
>> +// MM IPL are converted to free memory, so the MM Core must guarantee that is
>> +// does not touch of the code/data associated with the MM IPL if this flag is TRUE.
>> +//
>> +BOOLEAN  mInLegacyBoot = FALSE;
>> +
>> +//
>> +// Table of MMI Handlers that are registered by the MM Core when it is initialized
>> +//
>> +MM_CORE_MMI_HANDLERS  mMmCoreMmiHandlers[] = {
>> +  { MmFvDispatchHandler,     &gMmFvDispatchGuid,                 NULL, TRUE  },
>> +  { MmDriverDispatchHandler, &gEfiEventDxeDispatchGuid,          NULL, TRUE  },
>> +  { MmReadyToLockHandler,    &gEfiDxeMmReadyToLockProtocolGuid,  NULL, TRUE  },
>> +  { MmEndOfDxeHandler,       &gEfiEndOfDxeEventGroupGuid,        NULL, FALSE },
>> +  { MmLegacyBootHandler,     &gEfiEventLegacyBootGuid,           NULL, FALSE },
>> +  { MmExitBootServiceHandler,&gEfiEventExitBootServicesGuid,     NULL, FALSE },
>> +  { MmReadyToBootHandler,    &gEfiEventReadyToBootGuid,          NULL, FALSE },
>> +  { NULL,                    NULL,                               NULL, FALSE },
>> +};
>> +
>> +EFI_SYSTEM_TABLE                *mEfiSystemTable;
>> +UINTN                           mMmramRangeCount;
>> +EFI_MMRAM_DESCRIPTOR            *mMmramRanges;
>> +
>> +/**
>> +  Place holder function until all the MM System Table Service are available.
>> +
>> +  Note: This function is only used by MMRAM invocation.  It is never used by DXE invocation.
>> +
>> +  @param  Arg1                   Undefined
>> +  @param  Arg2                   Undefined
>> +  @param  Arg3                   Undefined
>> +  @param  Arg4                   Undefined
>> +  @param  Arg5                   Undefined
>> +
>> +  @return EFI_NOT_AVAILABLE_YET
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmEfiNotAvailableYetArg5 (
>> +  UINTN Arg1,
>> +  UINTN Arg2,
>> +  UINTN Arg3,
>> +  UINTN Arg4,
>> +  UINTN Arg5
>> +  )
>> +{
>> +  //
>> +  // This function should never be executed.  If it does, then the architectural protocols
>> +  // have not been designed correctly.
>> +  //
>> +  return EFI_NOT_AVAILABLE_YET;
>> +}
>> +
>> +/**
>> +  Software MMI handler that is called when a Legacy Boot event is signaled.  The MM
>> +  Core uses this signal to know that a Legacy Boot has been performed and that
>> +  gMmCorePrivate that is shared between the UEFI and MM execution environments can
>> +  not be accessed from MM anymore since that structure is considered free memory by
>> +  a legacy OS.
>> +
>> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
>> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
>> +  @param  CommBuffer      A pointer to a collection of data in memory that will
>> +                          be conveyed from a non-MM environment into an MM environment.
>> +  @param  CommBufferSize  The size of the CommBuffer.
>> +
>> +  @return Status Code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmLegacyBootHandler (
>> +  IN     EFI_HANDLE  DispatchHandle,
>> +  IN     CONST VOID  *Context,        OPTIONAL
>> +  IN OUT VOID        *CommBuffer,     OPTIONAL
>> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
>> +  )
>> +{
>> +  EFI_HANDLE  MmHandle;
>> +  EFI_STATUS  Status = EFI_SUCCESS;
>> +
>> +  if (!mInLegacyBoot) {
>> +    MmHandle = NULL;
>> +    Status = MmInstallProtocolInterface (
>> +               &MmHandle,
>> +               &gEfiEventLegacyBootGuid,
>> +               EFI_NATIVE_INTERFACE,
>> +               NULL
>> +               );
>> +  }
>> +  mInLegacyBoot = TRUE;
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Software MMI handler that is called when a ExitBoot Service event is signaled.
>> +
>> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
>> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
>> +  @param  CommBuffer      A pointer to a collection of data in memory that will
>> +                          be conveyed from a non-MM environment into an MM environment.
>> +  @param  CommBufferSize  The size of the CommBuffer.
>> +
>> +  @return Status Code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmExitBootServiceHandler (
>> +  IN     EFI_HANDLE  DispatchHandle,
>> +  IN     CONST VOID  *Context,        OPTIONAL
>> +  IN OUT VOID        *CommBuffer,     OPTIONAL
>> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
>> +  )
>> +{
>> +  EFI_HANDLE  MmHandle;
>> +  EFI_STATUS  Status = EFI_SUCCESS;
>> +  STATIC BOOLEAN mInExitBootServices = FALSE;
>> +
>> +  if (!mInExitBootServices) {
>> +    MmHandle = NULL;
>> +    Status = MmInstallProtocolInterface (
>> +               &MmHandle,
>> +               &gEfiEventExitBootServicesGuid,
>> +               EFI_NATIVE_INTERFACE,
>> +               NULL
>> +               );
>> +  }
>> +  mInExitBootServices = TRUE;
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Software MMI handler that is called when a ExitBoot Service event is signaled.
>> +
>> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
>> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
>> +  @param  CommBuffer      A pointer to a collection of data in memory that will
>> +                          be conveyed from a non-MM environment into an MM environment.
>> +  @param  CommBufferSize  The size of the CommBuffer.
>> +
>> +  @return Status Code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmReadyToBootHandler (
>> +  IN     EFI_HANDLE  DispatchHandle,
>> +  IN     CONST VOID  *Context,        OPTIONAL
>> +  IN OUT VOID        *CommBuffer,     OPTIONAL
>> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
>> +  )
>> +{
>> +  EFI_HANDLE  MmHandle;
>> +  EFI_STATUS  Status = EFI_SUCCESS;
>> +  STATIC BOOLEAN mInReadyToBoot = FALSE;
>> +
>> +  if (!mInReadyToBoot) {
>> +    MmHandle = NULL;
>> +    Status = MmInstallProtocolInterface (
>> +               &MmHandle,
>> +               &gEfiEventReadyToBootGuid,
>> +               EFI_NATIVE_INTERFACE,
>> +               NULL
>> +               );
>> +  }
>> +  mInReadyToBoot = TRUE;
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Software MMI handler that is called when the DxeMmReadyToLock protocol is added
>> +  or if gEfiEventReadyToBootGuid is signaled.  This function unregisters the
>> +  Software SMIs that are nor required after MMRAM is locked and installs the
>> +  MM Ready To Lock Protocol so MM Drivers are informed that MMRAM is about
>> +  to be locked.
>> +
>> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
>> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
>> +  @param  CommBuffer      A pointer to a collection of data in memory that will
>> +                          be conveyed from a non-MM environment into an MM environment.
>> +  @param  CommBufferSize  The size of the CommBuffer.
>> +
>> +  @return Status Code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmReadyToLockHandler (
>> +  IN     EFI_HANDLE  DispatchHandle,
>> +  IN     CONST VOID  *Context,        OPTIONAL
>> +  IN OUT VOID        *CommBuffer,     OPTIONAL
>> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
>> +  )
>> +{
>> +  EFI_STATUS  Status;
>> +  UINTN       Index;
>> +  EFI_HANDLE  MmHandle;
>> +
>> +  DEBUG ((DEBUG_INFO, "MmReadyToLockHandler\n"));
>> +
>> +  //
>> +  // Unregister MMI Handlers that are no longer required after the MM driver dispatch is stopped
>> +  //
>> +  for (Index = 0; mMmCoreMmiHandlers[Index].HandlerType != NULL; Index++) {
>> +    if (mMmCoreMmiHandlers[Index].UnRegister) {
>> +      MmiHandlerUnRegister (mMmCoreMmiHandlers[Index].DispatchHandle);
>> +    }
>> +  }
>> +
>> +  //
>> +  // Install MM Ready to lock protocol
>> +  //
>> +  MmHandle = NULL;
>> +  Status = MmInstallProtocolInterface (
>> +             &MmHandle,
>> +             &gEfiMmReadyToLockProtocolGuid,
>> +             EFI_NATIVE_INTERFACE,
>> +             NULL
>> +             );
>> +
>> +  //
>> +  // Make sure MM CPU I/O 2 Protocol has been installed into the handle database
>> +  //
>> +  //Status = MmLocateProtocol (&EFI_MM_CPU_IO_PROTOCOL_GUID, NULL, &Interface);
>> +
>> +  //
>> +  // Print a message on a debug build if the MM CPU I/O 2 Protocol is not installed
>> +  //
>> +  //if (EFI_ERROR (Status)) {
>> +      //DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));
>> +  //}
>> +
>> +
>> +  //
>> +  // Assert if the CPU I/O 2 Protocol is not installed
>> +  //
>> +  //ASSERT_EFI_ERROR (Status);
>> +
>> +  //
>> +  // Display any drivers that were not dispatched because dependency expression
>> +  // evaluated to false if this is a debug build
>> +  //
>> +  //MmDisplayDiscoveredNotDispatched ();
>> +
>> +  return Status;
>> +}
>> +
>> +/**
>> +  Software MMI handler that is called when the EndOfDxe event is signaled.
>> +  This function installs the MM EndOfDxe Protocol so MM Drivers are informed that
>> +  platform code will invoke 3rd part code.
>> +
>> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
>> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
>> +  @param  CommBuffer      A pointer to a collection of data in memory that will
>> +                          be conveyed from a non-MM environment into an MM environment.
>> +  @param  CommBufferSize  The size of the CommBuffer.
>> +
>> +  @return Status Code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmEndOfDxeHandler (
>> +  IN     EFI_HANDLE  DispatchHandle,
>> +  IN     CONST VOID  *Context,        OPTIONAL
>> +  IN OUT VOID        *CommBuffer,     OPTIONAL
>> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
>> +  )
>> +{
>> +  EFI_STATUS  Status;
>> +  EFI_HANDLE  MmHandle;
>> +
>> +  DEBUG ((DEBUG_INFO, "MmEndOfDxeHandler\n"));
>> +  //
>> +  // Install MM EndOfDxe protocol
>> +  //
>> +  MmHandle = NULL;
>> +  Status = MmInstallProtocolInterface (
>> +             &MmHandle,
>> +             &gEfiMmEndOfDxeProtocolGuid,
>> +             EFI_NATIVE_INTERFACE,
>> +             NULL
>> +             );
>> +  return Status;
>> +}
>> +
>> +
>> +
>> +/**
>> +  The main entry point to MM Foundation.
>> +
>> +  Note: This function is only used by MMRAM invocation.  It is never used by DXE invocation.
>> +
>> +  @param  MmEntryContext           Processor information and functionality
>> +                                    needed by MM Foundation.
>> +
>> +**/
>> +VOID
>> +EFIAPI
>> +MmEntryPoint (
>> +  IN CONST EFI_MM_ENTRY_CONTEXT  *MmEntryContext
>> +)
>> +{
>> +  EFI_STATUS                  Status;
>> +  EFI_MM_COMMUNICATE_HEADER  *CommunicateHeader;
>> +  BOOLEAN                     InLegacyBoot;
>> +
>> +  DEBUG ((DEBUG_INFO, "MmEntryPoint ...\n"));
>> +
>> +  //
>> +  // Update MMST using the context
>> +  //
>> +  CopyMem (&gMmCoreMmst.MmStartupThisAp, MmEntryContext, sizeof (EFI_MM_ENTRY_CONTEXT));
>> +
>> +  //
>> +  // Call platform hook before Mm Dispatch
>> +  //
>> +  //PlatformHookBeforeMmDispatch ();
>> +
>> +  //
>> +  // If a legacy boot has occured, then make sure gMmCorePrivate is not accessed
>> +  //
>> +  InLegacyBoot = mInLegacyBoot;
>> +  if (!InLegacyBoot) {
>> +    //
>> +    // TBD: Mark the InMm flag as TRUE
>> +    //
>> +    gMmCorePrivate->InMm = TRUE;
>> +
>> +    //
>> +    // Check to see if this is a Synchronous MMI sent through the MM Communication
>> +    // Protocol or an Asynchronous MMI
>> +    //
>> +    if (gMmCorePrivate->CommunicationBuffer != 0) {
>> +      //
>> +      // Synchronous MMI for MM Core or request from Communicate protocol
>> +      //
>> +      if (!MmIsBufferOutsideMmValid ((UINTN)gMmCorePrivate->CommunicationBuffer, gMmCorePrivate->BufferSize)) {
>> +        //
>> +        // If CommunicationBuffer is not in valid address scope, return EFI_INVALID_PARAMETER
>> +        //
>> +        gMmCorePrivate->CommunicationBuffer = 0;
>> +        gMmCorePrivate->ReturnStatus = EFI_INVALID_PARAMETER;
>> +      } else {
>> +        CommunicateHeader = (EFI_MM_COMMUNICATE_HEADER *)(UINTN)gMmCorePrivate->CommunicationBuffer;
>> +        gMmCorePrivate->BufferSize -= OFFSET_OF (EFI_MM_COMMUNICATE_HEADER, Data);
>> +        //DEBUG ((DEBUG_INFO, "CommunicateHeader->HeaderGuid - %g\n", &CommunicateHeader->HeaderGuid));
>> +        Status = MmiManage (
>> +                   &CommunicateHeader->HeaderGuid,
>> +                   NULL,
>> +                   CommunicateHeader->Data,
>> +                   (UINTN *)&gMmCorePrivate->BufferSize
>> +                   );
>> +        //
>> +        // Update CommunicationBuffer, BufferSize and ReturnStatus
>> +        // Communicate service finished, reset the pointer to CommBuffer to NULL
>> +        //
>> +        gMmCorePrivate->BufferSize += OFFSET_OF (EFI_MM_COMMUNICATE_HEADER, Data);
>> +        gMmCorePrivate->CommunicationBuffer = 0;
>> +        gMmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
>> +      }
>> +    }
>> +  }
>> +
>> +  //
>> +  // Process Asynchronous MMI sources
>> +  //
>> +  MmiManage (NULL, NULL, NULL, NULL);
>> +
>> +  //
>> +  // TBD: Do not use private data structure ?
>> +  //
>> +
>> +  //
>> +  // If a legacy boot has occured, then make sure gMmCorePrivate is not accessed
>> +  //
>> +  if (!InLegacyBoot) {
>> +    //
>> +    // Clear the InMm flag as we are going to leave MM
>> +    //
>> +    gMmCorePrivate->InMm = FALSE;
>> +  }
>> +
>> +  DEBUG ((DEBUG_INFO, "MmEntryPoint Done\n"));
>> +}
>> +
>> +EFI_STATUS
>> +EFIAPI
>> +MmConfigurationMmNotify (
>> +  IN CONST EFI_GUID *Protocol,
>> +  IN VOID           *Interface,
>> +  IN EFI_HANDLE      Handle
>> +  )
>> +{
>> +  EFI_STATUS                      Status;
>> +  EFI_MM_CONFIGURATION_PROTOCOL  *MmConfiguration;
>> +
>> +  DEBUG ((DEBUG_INFO, "MmConfigurationMmNotify(%g) - %x\n", Protocol, Interface));
>> +
>> +  MmConfiguration = Interface;
>> +
>> +  //
>> +  // Register the MM Entry Point provided by the MM Core with the MM COnfiguration protocol
>> +  //
>> +  Status = MmConfiguration->RegisterMmEntry (MmConfiguration, (EFI_MM_ENTRY_POINT)(UINTN)gMmCorePrivate->MmEntryPoint);
>> +  ASSERT_EFI_ERROR (Status);
>> +
>> +  //
>> +  // Set flag to indicate that the MM Entry Point has been registered which
>> +  // means that MMIs are now fully operational.
>> +  //
>> +  gMmCorePrivate->MmEntryPointRegistered = TRUE;
>> +
>> +  //
>> +  // Print debug message showing MM Core entry point address.
>> +  //
>> +  DEBUG ((DEBUG_INFO, "MM Core registered MM Entry Point address %p\n", (VOID *)(UINTN)gMmCorePrivate->MmEntryPoint));
>> +  return EFI_SUCCESS;
>> +}
>> +
>> +UINTN
>> +GetHobListSize (
>> +  IN VOID *HobStart
>> +  )
>> +{
>> +  EFI_PEI_HOB_POINTERS  Hob;
>> +
>> +  ASSERT (HobStart != NULL);
>> +
>> +  Hob.Raw = (UINT8 *) HobStart;
>> +  while (!END_OF_HOB_LIST (Hob)) {
>> +    Hob.Raw = GET_NEXT_HOB (Hob);
>> +  }
>> +  //
>> +  // Need plus END_OF_HOB_LIST
>> +  //
>> +  return (UINTN)Hob.Raw - (UINTN)HobStart + sizeof(EFI_HOB_GENERIC_HEADER);
>> +}
>> +
>> +/**
>> +  The Entry Point for MM Core
>> +
>> +  Install DXE Protocols and reload MM Core into MMRAM and register MM Core
>> +  EntryPoint on the MMI vector.
>> +
>> +  Note: This function is called for both DXE invocation and MMRAM invocation.
>> +
>> +  @param  ImageHandle    The firmware allocated handle for the EFI image.
>> +  @param  SystemTable    A pointer to the EFI System Table.
>> +
>> +  @retval EFI_SUCCESS    The entry point is executed successfully.
>> +  @retval Other          Some error occurred when executing this entry point.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +StandaloneMmMain (
>> +  IN VOID  *HobStart
>> +  )
>> +{
>> +  EFI_STATUS                      Status;
>> +  UINTN                           Index;
>> +  VOID                            *MmHobStart;
>> +  UINTN                           HobSize;
>> +  VOID                            *Registration;
>> +  EFI_HOB_GUID_TYPE               *GuidHob;
>> +  MM_CORE_DATA_HOB_DATA           *DataInHob;
>> +  EFI_HOB_GUID_TYPE               *MmramRangesHob;
>> +  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
>> +  EFI_MMRAM_DESCRIPTOR            *MmramRanges;
>> +  UINT32                          MmramRangeCount;
>> +  EFI_HOB_FIRMWARE_VOLUME         *BfvHob;
>> +
>> +  ProcessLibraryConstructorList (HobStart, &gMmCoreMmst);
>> +
>> +  DEBUG ((DEBUG_INFO, "MmMain - 0x%x\n", HobStart));
>> +
>> +  //
>> +  // Determine if the caller has passed a reference to a MM_CORE_PRIVATE_DATA
>> +  // structure in the Hoblist. This choice will govern how boot information is
>> +  // extracted later.
>> +  //
>> +  GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);
>> +  if (GuidHob == NULL) {
>> +    //
>> +    // Allocate and zero memory for a MM_CORE_PRIVATE_DATA table and then
>> +    // initialise it
>> +    //
>> +    gMmCorePrivate = (MM_CORE_PRIVATE_DATA *) AllocateRuntimePages(EFI_SIZE_TO_PAGES(sizeof(MM_CORE_PRIVATE_DATA)));
>> +    SetMem ((VOID *)(UINTN)gMmCorePrivate, sizeof(MM_CORE_PRIVATE_DATA), 0);
>> +    gMmCorePrivate->Signature = MM_CORE_PRIVATE_DATA_SIGNATURE;
>> +    gMmCorePrivate->MmEntryPointRegistered = FALSE;
>> +    gMmCorePrivate->InMm = FALSE;
>> +    gMmCorePrivate->ReturnStatus = EFI_SUCCESS;
>> +
>> +    //
>> +    // Extract the MMRAM ranges from the MMRAM descriptor HOB
>> +    //
>> +    MmramRangesHob = GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid, HobStart);
>> +    if (MmramRangesHob == NULL)
>> +      return EFI_UNSUPPORTED;
>> +
>> +    MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
>> +    ASSERT (MmramRangesHobData != NULL);
>> +    MmramRanges = MmramRangesHobData->Descriptor;
>> +    MmramRangeCount = MmramRangesHobData->NumberOfMmReservedRegions;
>> +    ASSERT (MmramRanges);
>> +    ASSERT (MmramRangeCount);
>> +
>> +    //
>> +    // Copy the MMRAM ranges into MM_CORE_PRIVATE_DATA table just in case any
>> +    // code relies on them being present there
>> +    //
>> +    gMmCorePrivate->MmramRangeCount = MmramRangeCount;
>> +    gMmCorePrivate->MmramRanges = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (MmramRangeCount * sizeof(EFI_MMRAM_DESCRIPTOR));
>> +    ASSERT (gMmCorePrivate->MmramRanges != 0);
>> +    CopyMem ((VOID *)(UINTN)gMmCorePrivate->MmramRanges,
>> +             MmramRanges,
>> +             MmramRangeCount * sizeof(EFI_MMRAM_DESCRIPTOR));
>> +  } else {
>> +    DataInHob       = GET_GUID_HOB_DATA (GuidHob);
>> +    gMmCorePrivate = (MM_CORE_PRIVATE_DATA *)(UINTN)DataInHob->Address;
>> +    MmramRanges     = (EFI_MMRAM_DESCRIPTOR *)(UINTN)gMmCorePrivate->MmramRanges;
>> +    MmramRangeCount = gMmCorePrivate->MmramRangeCount;
>> +  }
>> +
>> +  //
>> +  // Print the MMRAM ranges passed by the caller
>> +  //
>> +  DEBUG ((DEBUG_INFO, "MmramRangeCount - 0x%x\n", MmramRangeCount));
>> +  for (Index = 0; Index < MmramRangeCount; Index++) {
>> +          DEBUG ((DEBUG_INFO, "MmramRanges[%d]: 0x%016lx - 0x%lx\n", Index,
>> +                  MmramRanges[Index].CpuStart,
>> +                  MmramRanges[Index].PhysicalSize));
>> +  }
>> +
>> +  //
>> +  // Copy the MMRAM ranges into private MMRAM
>> +  //
>> +  mMmramRangeCount = MmramRangeCount;
>> +  DEBUG ((DEBUG_INFO, "mMmramRangeCount - 0x%x\n", mMmramRangeCount));
>> +  mMmramRanges = AllocatePool (mMmramRangeCount * sizeof (EFI_MMRAM_DESCRIPTOR));
>> +  DEBUG ((DEBUG_INFO, "mMmramRanges - 0x%x\n", mMmramRanges));
>> +  ASSERT (mMmramRanges != NULL);
>> +  CopyMem (mMmramRanges, (VOID *)(UINTN)MmramRanges, mMmramRangeCount * sizeof (EFI_MMRAM_DESCRIPTOR));
>> +
>> +  //
>> +  // Get Boot Firmware Volume address from the BFV Hob
>> +  //
>> +  BfvHob = GetFirstHob (EFI_HOB_TYPE_FV);
>> +  if (BfvHob != NULL) {
>> +    DEBUG ((DEBUG_INFO, "BFV address - 0x%x\n", BfvHob->BaseAddress));
>> +    DEBUG ((DEBUG_INFO, "BFV size    - 0x%x\n", BfvHob->Length));
>> +    gMmCorePrivate->StandaloneBfvAddress = BfvHob->BaseAddress;
>> +  }
>> +
>> +  gMmCorePrivate->Mmst          = (EFI_PHYSICAL_ADDRESS)(UINTN)&gMmCoreMmst;
>> +  gMmCorePrivate->MmEntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)MmEntryPoint;
>> +
>> +  //
>> +  // No need to initialize memory service.
>> +  // It is done in constructor of StandaloneMmCoreMemoryAllocationLib(),
>> +  // so that the library linked with StandaloneMmCore can use AllocatePool() in constuctor.
>> +  //
>> +
>> +  DEBUG ((DEBUG_INFO, "MmInstallConfigurationTable For HobList\n"));
>> +  //
>> +  // Install HobList
>> +  //
>> +  HobSize = GetHobListSize (HobStart);
>> +  DEBUG ((DEBUG_INFO, "HobSize - 0x%x\n", HobSize));
>> +  MmHobStart = AllocatePool (HobSize);
>> +  DEBUG ((DEBUG_INFO, "MmHobStart - 0x%x\n", MmHobStart));
>> +  ASSERT (MmHobStart != NULL);
>> +  CopyMem (MmHobStart, HobStart, HobSize);
>> +  Status = MmInstallConfigurationTable (&gMmCoreMmst, &gEfiHobListGuid, MmHobStart, HobSize);
>> +  ASSERT_EFI_ERROR (Status);
>> +
>> +  //
>> +  // Register notification for EFI_MM_CONFIGURATION_PROTOCOL registration and
>> +  // use it to register the MM Foundation entrypoint
>> +  //
>> +  DEBUG ((DEBUG_INFO, "MmRegisterProtocolNotify - MmConfigurationMmProtocol\n"));
>> +  Status = MmRegisterProtocolNotify (
>> +             &gEfiMmConfigurationProtocolGuid,
>> +             MmConfigurationMmNotify,
>> +             &Registration
>> +             );
>> +  ASSERT_EFI_ERROR (Status);
>> +
>> +  //
>> +  // Dispatch standalone BFV
>> +  //
>> +  DEBUG ((DEBUG_INFO, "Mm Dispatch StandaloneBfvAddress - 0x%08x\n", gMmCorePrivate->StandaloneBfvAddress));
>> +  if (gMmCorePrivate->StandaloneBfvAddress != 0) {
>> +    MmCoreFfsFindMmDriver ((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)gMmCorePrivate->StandaloneBfvAddress);
>> +    MmDispatcher ();
>> +  }
>> +
>> +  //
>> +  // Register all handlers in the core table
>> +  //
>> +  for (Index = 0; mMmCoreMmiHandlers[Index].HandlerType != NULL; Index++) {
>> +    Status = MmiHandlerRegister(mMmCoreMmiHandlers[Index].Handler,
>> +                                mMmCoreMmiHandlers[Index].HandlerType,
>> +                                &mMmCoreMmiHandlers[Index].DispatchHandle);
>> +    DEBUG ((DEBUG_INFO, "MmiHandlerRegister - GUID %g - Status %d\n", mMmCoreMmiHandlers[Index].HandlerType, Status));
>> +  }
>> +
>> +  DEBUG ((DEBUG_INFO, "MmMain Done!\n"));
>> +
>> +  return EFI_SUCCESS;
>> +}
>> diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.h b/StandaloneMmPkg/Core/StandaloneMmCore.h
>> new file mode 100644
>> index 0000000000..53921b7844
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Core/StandaloneMmCore.h
>> @@ -0,0 +1,903 @@
>> +/** @file
>> +  The internal header file includes the common header files, defines
>> +  internal structure and functions used by MmCore module.
>> +
>> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
>> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +
>> +  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
>> +
>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +
>> +**/
>> +
>> +#ifndef _MM_CORE_H_
>> +#define _MM_CORE_H_
>> +
>> +#include <PiMm.h>
>> +#include <StandaloneMm.h>
>> +
>> +#include <Protocol/DxeMmReadyToLock.h>
>> +#include <Protocol/MmReadyToLock.h>
>> +#include <Protocol/MmEndOfDxe.h>
>> +#include <Protocol/MmCommunication.h>
>> +#include <Protocol/LoadedImage.h>
>> +#include <Protocol/MmConfiguration.h>
>> +
>> +#include <Guid/Apriori.h>
>> +#include <Guid/EventGroup.h>
>> +#include <Guid/EventLegacyBios.h>
>> +#include <Guid/ZeroGuid.h>
>> +#include <Guid/MemoryProfile.h>
>> +#include <Guid/HobList.h>
>> +#include <Guid/MmFvDispatch.h>
>> +#include <Guid/MmramMemoryReserve.h>
>> +
>> +#include <Library/MmCoreStandaloneEntryPoint.h>
>> +#include <Library/BaseLib.h>
>> +#include <Library/BaseMemoryLib.h>
>> +#include <Library/PeCoffLib.h>
>> +#include <Library/CacheMaintenanceLib.h>
>> +#include <Library/DebugLib.h>
>> +#include <Library/ReportStatusCodeLib.h>
>> +#include <Library/MemoryAllocationLib.h>
>> +#include <Library/PcdLib.h>
>> +//#include <Library/MmCorePlatformHookLib.h>
>> +#include <Library/MemLib.h>
>> +#include <Library/HobLib.h>
>> +
>> +#include "StandaloneMmCorePrivateData.h"
>> +
>> +//
>> +// Used to build a table of MMI Handlers that the MM Core registers
>> +//
>> +typedef struct {
>> +  EFI_MM_HANDLER_ENTRY_POINT    Handler;
>> +  EFI_GUID                      *HandlerType;
>> +  EFI_HANDLE                    DispatchHandle;
>> +  BOOLEAN                       UnRegister;
>> +} MM_CORE_MMI_HANDLERS;
>> +
>> +//
>> +// Structure for recording the state of an MM Driver
>> +//
>> +#define EFI_MM_DRIVER_ENTRY_SIGNATURE SIGNATURE_32('s', 'd','r','v')
>> +
>> +typedef struct {
>> +  UINTN                           Signature;
>> +  LIST_ENTRY                      Link;             // mDriverList
>> +
>> +  LIST_ENTRY                      ScheduledLink;    // mScheduledQueue
>> +
>> +  EFI_HANDLE                      FvHandle;
>> +  EFI_GUID                        FileName;
>> +  VOID                            *Pe32Data;
>> +  UINTN                           Pe32DataSize;
>> +
>> +  VOID                            *Depex;
>> +  UINTN                           DepexSize;
>> +
>> +  BOOLEAN                         Before;
>> +  BOOLEAN                         After;
>> +  EFI_GUID                        BeforeAfterGuid;
>> +
>> +  BOOLEAN                         Dependent;
>> +  BOOLEAN                         Scheduled;
>> +  BOOLEAN                         Initialized;
>> +  BOOLEAN                         DepexProtocolError;
>> +
>> +  EFI_HANDLE                      ImageHandle;
>> +  EFI_LOADED_IMAGE_PROTOCOL       *LoadedImage;
>> +  //
>> +  // Image EntryPoint in MMRAM
>> +  //
>> +  PHYSICAL_ADDRESS                ImageEntryPoint;
>> +  //
>> +  // Image Buffer in MMRAM
>> +  //
>> +  PHYSICAL_ADDRESS                ImageBuffer;
>> +  //
>> +  // Image Page Number
>> +  //
>> +  UINTN                           NumberOfPage;
>> +} EFI_MM_DRIVER_ENTRY;
>> +
>> +#define EFI_HANDLE_SIGNATURE            SIGNATURE_32('h','n','d','l')
>> +
>> +///
>> +/// IHANDLE - contains a list of protocol handles
>> +///
>> +typedef struct {
>> +  UINTN               Signature;
>> +  /// All handles list of IHANDLE
>> +  LIST_ENTRY          AllHandles;
>> +  /// List of PROTOCOL_INTERFACE's for this handle
>> +  LIST_ENTRY          Protocols;
>> +  UINTN               LocateRequest;
>> +} IHANDLE;
>> +
>> +#define ASSERT_IS_HANDLE(a)  ASSERT((a)->Signature == EFI_HANDLE_SIGNATURE)
>> +
>> +#define PROTOCOL_ENTRY_SIGNATURE        SIGNATURE_32('p','r','t','e')
>> +
>> +///
>> +/// PROTOCOL_ENTRY - each different protocol has 1 entry in the protocol
>> +/// database.  Each handler that supports this protocol is listed, along
>> +/// with a list of registered notifies.
>> +///
>> +typedef struct {
>> +  UINTN               Signature;
>> +  /// Link Entry inserted to mProtocolDatabase
>> +  LIST_ENTRY          AllEntries;
>> +  /// ID of the protocol
>> +  EFI_GUID            ProtocolID;
>> +  /// All protocol interfaces
>> +  LIST_ENTRY          Protocols;
>> +  /// Registerd notification handlers
>> +  LIST_ENTRY          Notify;
>> +} PROTOCOL_ENTRY;
>> +
>> +#define PROTOCOL_INTERFACE_SIGNATURE  SIGNATURE_32('p','i','f','c')
>> +
>> +///
>> +/// PROTOCOL_INTERFACE - each protocol installed on a handle is tracked
>> +/// with a protocol interface structure
>> +///
>> +typedef struct {
>> +  UINTN                       Signature;
>> +  /// Link on IHANDLE.Protocols
>> +  LIST_ENTRY                  Link;
>> +  /// Back pointer
>> +  IHANDLE                     *Handle;
>> +  /// Link on PROTOCOL_ENTRY.Protocols
>> +  LIST_ENTRY                  ByProtocol;
>> +  /// The protocol ID
>> +  PROTOCOL_ENTRY              *Protocol;
>> +  /// The interface value
>> +  VOID                        *Interface;
>> +} PROTOCOL_INTERFACE;
>> +
>> +#define PROTOCOL_NOTIFY_SIGNATURE       SIGNATURE_32('p','r','t','n')
>> +
>> +///
>> +/// PROTOCOL_NOTIFY - used for each register notification for a protocol
>> +///
>> +typedef struct {
>> +  UINTN               Signature;
>> +  PROTOCOL_ENTRY      *Protocol;
>> +  /// All notifications for this protocol
>> +  LIST_ENTRY          Link;
>> +  /// Notification function
>> +  EFI_MM_NOTIFY_FN   Function;
>> +  /// Last position notified
>> +  LIST_ENTRY          *Position;
>> +} PROTOCOL_NOTIFY;
>> +
>> +//
>> +// MM Core Global Variables
>> +//
>> +extern MM_CORE_PRIVATE_DATA  *gMmCorePrivate;
>> +extern EFI_MM_SYSTEM_TABLE   gMmCoreMmst;
>> +extern LIST_ENTRY            gHandleList;
>> +extern EFI_PHYSICAL_ADDRESS  gLoadModuleAtFixAddressMmramBase;
>> +
>> +/**
>> +  Called to initialize the memory service.
>> +
>> +  @param   MmramRangeCount       Number of MMRAM Regions
>> +  @param   MmramRanges           Pointer to MMRAM Descriptors
>> +
>> +**/
>> +VOID
>> +MmInitializeMemoryServices (
>> +  IN UINTN                 MmramRangeCount,
>> +  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
>> +  );
>> +
>> +/**
>> +  The MmInstallConfigurationTable() function is used to maintain the list
>> +  of configuration tables that are stored in the System Management System
>> +  Table.  The list is stored as an array of (GUID, Pointer) pairs.  The list
>> +  must be allocated from pool memory with PoolType set to EfiRuntimeServicesData.
>> +
>> +  @param  SystemTable      A pointer to the MM System Table (SMST).
>> +  @param  Guid             A pointer to the GUID for the entry to add, update, or remove.
>> +  @param  Table            A pointer to the buffer of the table to add.
>> +  @param  TableSize        The size of the table to install.
>> +
>> +  @retval EFI_SUCCESS           The (Guid, Table) pair was added, updated, or removed.
>> +  @retval EFI_INVALID_PARAMETER Guid is not valid.
>> +  @retval EFI_NOT_FOUND         An attempt was made to delete a non-existent entry.
>> +  @retval EFI_OUT_OF_RESOURCES  There is not enough memory available to complete the operation.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmInstallConfigurationTable (
>> +  IN  CONST EFI_MM_SYSTEM_TABLE  *SystemTable,
>> +  IN  CONST EFI_GUID              *Guid,
>> +  IN  VOID                        *Table,
>> +  IN  UINTN                       TableSize
>> +  );
>> +
>> +/**
>> +  Wrapper function to MmInstallProtocolInterfaceNotify.  This is the public API which
>> +  Calls the private one which contains a BOOLEAN parameter for notifications
>> +
>> +  @param  UserHandle             The handle to install the protocol handler on,
>> +                                 or NULL if a new handle is to be allocated
>> +  @param  Protocol               The protocol to add to the handle
>> +  @param  InterfaceType          Indicates whether Interface is supplied in
>> +                                 native form.
>> +  @param  Interface              The interface for the protocol being added
>> +
>> +  @return Status code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmInstallProtocolInterface (
>> +  IN OUT EFI_HANDLE     *UserHandle,
>> +  IN EFI_GUID           *Protocol,
>> +  IN EFI_INTERFACE_TYPE InterfaceType,
>> +  IN VOID               *Interface
>> +  );
>> +
>> +/**
>> +  Allocates pages from the memory map.
>> +
>> +  @param  Type                   The type of allocation to perform
>> +  @param  MemoryType             The type of memory to turn the allocated pages
>> +                                 into
>> +  @param  NumberOfPages          The number of pages to allocate
>> +  @param  Memory                 A pointer to receive the base allocated memory
>> +                                 address
>> +
>> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
>> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
>> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
>> +  @retval EFI_SUCCESS            Pages successfully allocated.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmAllocatePages (
>> +  IN      EFI_ALLOCATE_TYPE         Type,
>> +  IN      EFI_MEMORY_TYPE           MemoryType,
>> +  IN      UINTN                     NumberOfPages,
>> +  OUT     EFI_PHYSICAL_ADDRESS      *Memory
>> +  );
>> +
>> +/**
>> +  Allocates pages from the memory map.
>> +
>> +  @param  Type                   The type of allocation to perform
>> +  @param  MemoryType             The type of memory to turn the allocated pages
>> +                                 into
>> +  @param  NumberOfPages          The number of pages to allocate
>> +  @param  Memory                 A pointer to receive the base allocated memory
>> +                                 address
>> +
>> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
>> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
>> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
>> +  @retval EFI_SUCCESS            Pages successfully allocated.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmInternalAllocatePages (
>> +  IN      EFI_ALLOCATE_TYPE         Type,
>> +  IN      EFI_MEMORY_TYPE           MemoryType,
>> +  IN      UINTN                     NumberOfPages,
>> +  OUT     EFI_PHYSICAL_ADDRESS      *Memory
>> +  );
>> +
>> +/**
>> +  Frees previous allocated pages.
>> +
>> +  @param  Memory                 Base address of memory being freed
>> +  @param  NumberOfPages          The number of pages to free
>> +
>> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range
>> +  @retval EFI_INVALID_PARAMETER  Address not aligned, Address is zero or NumberOfPages is zero.
>> +  @return EFI_SUCCESS            Pages successfully freed.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmFreePages (
>> +  IN      EFI_PHYSICAL_ADDRESS      Memory,
>> +  IN      UINTN                     NumberOfPages
>> +  );
>> +
>> +/**
>> +  Frees previous allocated pages.
>> +
>> +  @param  Memory                 Base address of memory being freed
>> +  @param  NumberOfPages          The number of pages to free
>> +
>> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range
>> +  @retval EFI_INVALID_PARAMETER  Address not aligned, Address is zero or NumberOfPages is zero.
>> +  @return EFI_SUCCESS            Pages successfully freed.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmInternalFreePages (
>> +  IN      EFI_PHYSICAL_ADDRESS      Memory,
>> +  IN      UINTN                     NumberOfPages
>> +  );
>> +
>> +/**
>> +  Allocate pool of a particular type.
>> +
>> +  @param  PoolType               Type of pool to allocate
>> +  @param  Size                   The amount of pool to allocate
>> +  @param  Buffer                 The address to return a pointer to the allocated
>> +                                 pool
>> +
>> +  @retval EFI_INVALID_PARAMETER  PoolType not valid
>> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
>> +  @retval EFI_SUCCESS            Pool successfully allocated.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmAllocatePool (
>> +  IN      EFI_MEMORY_TYPE           PoolType,
>> +  IN      UINTN                     Size,
>> +  OUT     VOID                      **Buffer
>> +  );
>> +
>> +/**
>> +  Allocate pool of a particular type.
>> +
>> +  @param  PoolType               Type of pool to allocate
>> +  @param  Size                   The amount of pool to allocate
>> +  @param  Buffer                 The address to return a pointer to the allocated
>> +                                 pool
>> +
>> +  @retval EFI_INVALID_PARAMETER  PoolType not valid
>> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
>> +  @retval EFI_SUCCESS            Pool successfully allocated.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmInternalAllocatePool (
>> +  IN      EFI_MEMORY_TYPE           PoolType,
>> +  IN      UINTN                     Size,
>> +  OUT     VOID                      **Buffer
>> +  );
>> +
>> +/**
>> +  Frees pool.
>> +
>> +  @param  Buffer                 The allocated pool entry to free
>> +
>> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
>> +  @retval EFI_SUCCESS            Pool successfully freed.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmFreePool (
>> +  IN      VOID                      *Buffer
>> +  );
>> +
>> +/**
>> +  Frees pool.
>> +
>> +  @param  Buffer                 The allocated pool entry to free
>> +
>> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
>> +  @retval EFI_SUCCESS            Pool successfully freed.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmInternalFreePool (
>> +  IN      VOID                      *Buffer
>> +  );
>> +
>> +/**
>> +  Installs a protocol interface into the boot services environment.
>> +
>> +  @param  UserHandle             The handle to install the protocol handler on,
>> +                                 or NULL if a new handle is to be allocated
>> +  @param  Protocol               The protocol to add to the handle
>> +  @param  InterfaceType          Indicates whether Interface is supplied in
>> +                                 native form.
>> +  @param  Interface              The interface for the protocol being added
>> +  @param  Notify                 indicates whether notify the notification list
>> +                                 for this protocol
>> +
>> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
>> +  @retval EFI_OUT_OF_RESOURCES   No enough buffer to allocate
>> +  @retval EFI_SUCCESS            Protocol interface successfully installed
>> +
>> +**/
>> +EFI_STATUS
>> +MmInstallProtocolInterfaceNotify (
>> +  IN OUT EFI_HANDLE     *UserHandle,
>> +  IN EFI_GUID           *Protocol,
>> +  IN EFI_INTERFACE_TYPE InterfaceType,
>> +  IN VOID               *Interface,
>> +  IN BOOLEAN            Notify
>> +  );
>> +
>> +/**
>> +  Uninstalls all instances of a protocol:interfacer from a handle.
>> +  If the last protocol interface is remove from the handle, the
>> +  handle is freed.
>> +
>> +  @param  UserHandle             The handle to remove the protocol handler from
>> +  @param  Protocol               The protocol, of protocol:interface, to remove
>> +  @param  Interface              The interface, of protocol:interface, to remove
>> +
>> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL.
>> +  @retval EFI_SUCCESS            Protocol interface successfully uninstalled.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmUninstallProtocolInterface (
>> +  IN EFI_HANDLE       UserHandle,
>> +  IN EFI_GUID         *Protocol,
>> +  IN VOID             *Interface
>> +  );
>> +
>> +/**
>> +  Queries a handle to determine if it supports a specified protocol.
>> +
>> +  @param  UserHandle             The handle being queried.
>> +  @param  Protocol               The published unique identifier of the protocol.
>> +  @param  Interface              Supplies the address where a pointer to the
>> +                                 corresponding Protocol Interface is returned.
>> +
>> +  @return The requested protocol interface for the handle
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmHandleProtocol (
>> +  IN EFI_HANDLE       UserHandle,
>> +  IN EFI_GUID         *Protocol,
>> +  OUT VOID            **Interface
>> +  );
>> +
>> +/**
>> +  Add a new protocol notification record for the request protocol.
>> +
>> +  @param  Protocol               The requested protocol to add the notify
>> +                                 registration
>> +  @param  Function               Points to the notification function
>> +  @param  Registration           Returns the registration record
>> +
>> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
>> +  @retval EFI_SUCCESS            Successfully returned the registration record
>> +                                 that has been added
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmRegisterProtocolNotify (
>> +  IN  CONST EFI_GUID              *Protocol,
>> +  IN  EFI_MM_NOTIFY_FN           Function,
>> +  OUT VOID                        **Registration
>> +  );
>> +
>> +/**
>> +  Locates the requested handle(s) and returns them in Buffer.
>> +
>> +  @param  SearchType             The type of search to perform to locate the
>> +                                 handles
>> +  @param  Protocol               The protocol to search for
>> +  @param  SearchKey              Dependant on SearchType
>> +  @param  BufferSize             On input the size of Buffer.  On output the
>> +                                 size of data returned.
>> +  @param  Buffer                 The buffer to return the results in
>> +
>> +  @retval EFI_BUFFER_TOO_SMALL   Buffer too small, required buffer size is
>> +                                 returned in BufferSize.
>> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
>> +  @retval EFI_SUCCESS            Successfully found the requested handle(s) and
>> +                                 returns them in Buffer.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmLocateHandle (
>> +  IN EFI_LOCATE_SEARCH_TYPE   SearchType,
>> +  IN EFI_GUID                 *Protocol   OPTIONAL,
>> +  IN VOID                     *SearchKey  OPTIONAL,
>> +  IN OUT UINTN                *BufferSize,
>> +  OUT EFI_HANDLE              *Buffer
>> +  );
>> +
>> +/**
>> +  Return the first Protocol Interface that matches the Protocol GUID. If
>> +  Registration is pasased in return a Protocol Instance that was just add
>> +  to the system. If Retistration is NULL return the first Protocol Interface
>> +  you find.
>> +
>> +  @param  Protocol               The protocol to search for
>> +  @param  Registration           Optional Registration Key returned from
>> +                                 RegisterProtocolNotify()
>> +  @param  Interface              Return the Protocol interface (instance).
>> +
>> +  @retval EFI_SUCCESS            If a valid Interface is returned
>> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
>> +  @retval EFI_NOT_FOUND          Protocol interface not found
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmLocateProtocol (
>> +  IN  EFI_GUID  *Protocol,
>> +  IN  VOID      *Registration OPTIONAL,
>> +  OUT VOID      **Interface
>> +  );
>> +
>> +/**
>> +  Manage MMI of a particular type.
>> +
>> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
>> +  @param  Context        Points to an optional context buffer.
>> +  @param  CommBuffer     Points to the optional communication buffer.
>> +  @param  CommBufferSize Points to the size of the optional communication buffer.
>> +
>> +  @retval EFI_SUCCESS                        Interrupt source was processed successfully but not quiesced.
>> +  @retval EFI_INTERRUPT_PENDING              One or more MMI sources could not be quiesced.
>> +  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING  Interrupt source was not handled or quiesced.
>> +  @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED Interrupt source was handled and quiesced.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmiManage (
>> +  IN     CONST EFI_GUID           *HandlerType,
>> +  IN     CONST VOID               *Context         OPTIONAL,
>> +  IN OUT VOID                     *CommBuffer      OPTIONAL,
>> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
>> +  );
>> +
>> +/**
>> +  Registers a handler to execute within MM.
>> +
>> +  @param  Handler        Handler service funtion pointer.
>> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
>> +  @param  DispatchHandle On return, contains a unique handle which can be used to later unregister the handler function.
>> +
>> +  @retval EFI_SUCCESS           Handler register success.
>> +  @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmiHandlerRegister (
>> +  IN   EFI_MM_HANDLER_ENTRY_POINT     Handler,
>> +  IN   CONST EFI_GUID                 *HandlerType  OPTIONAL,
>> +  OUT  EFI_HANDLE                     *DispatchHandle
>> +  );
>> +
>> +/**
>> +  Unregister a handler in MM.
>> +
>> +  @param  DispatchHandle  The handle that was specified when the handler was registered.
>> +
>> +  @retval EFI_SUCCESS           Handler function was successfully unregistered.
>> +  @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid handle.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmiHandlerUnRegister (
>> +  IN  EFI_HANDLE                      DispatchHandle
>> +  );
>> +
>> +/**
>> +  This function is the main entry point for an MM handler dispatch
>> +  or communicate-based callback.
>> +
>> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
>> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
>> +  @param  CommBuffer      A pointer to a collection of data in memory that will
>> +                          be conveyed from a non-MM environment into an MM environment.
>> +  @param  CommBufferSize  The size of the CommBuffer.
>> +
>> +  @return Status Code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmDriverDispatchHandler (
>> +  IN     EFI_HANDLE               DispatchHandle,
>> +  IN     CONST VOID               *Context,        OPTIONAL
>> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
>> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
>> +  );
>> +
>> +/**
>> +  This function is the main entry point for an MM handler dispatch
>> +  or communicate-based callback.
>> +
>> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
>> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
>> +  @param  CommBuffer      A pointer to a collection of data in memory that will
>> +                          be conveyed from a non-MM environment into an MM environment.
>> +  @param  CommBufferSize  The size of the CommBuffer.
>> +
>> +  @return Status Code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmFvDispatchHandler (
>> +  IN     EFI_HANDLE               DispatchHandle,
>> +  IN     CONST VOID               *Context,        OPTIONAL
>> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
>> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
>> +  );
>> +
>> +/**
>> +  This function is the main entry point for an MM handler dispatch
>> +  or communicate-based callback.
>> +
>> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
>> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
>> +  @param  CommBuffer      A pointer to a collection of data in memory that will
>> +                          be conveyed from a non-MM environment into an MM environment.
>> +  @param  CommBufferSize  The size of the CommBuffer.
>> +
>> +  @return Status Code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmLegacyBootHandler (
>> +  IN     EFI_HANDLE               DispatchHandle,
>> +  IN     CONST VOID               *Context,        OPTIONAL
>> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
>> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
>> +  );
>> +
>> +/**
>> +  This function is the main entry point for an MM handler dispatch
>> +  or communicate-based callback.
>> +
>> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
>> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
>> +  @param  CommBuffer      A pointer to a collection of data in memory that will
>> +                          be conveyed from a non-MM environment into an MM environment.
>> +  @param  CommBufferSize  The size of the CommBuffer.
>> +
>> +  @return Status Code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmExitBootServiceHandler (
>> +  IN     EFI_HANDLE               DispatchHandle,
>> +  IN     CONST VOID               *Context,        OPTIONAL
>> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
>> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
>> +  );
>> +
>> +/**
>> +  This function is the main entry point for an MM handler dispatch
>> +  or communicate-based callback.
>> +
>> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
>> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
>> +  @param  CommBuffer      A pointer to a collection of data in memory that will
>> +                          be conveyed from a non-MM environment into an MM environment.
>> +  @param  CommBufferSize  The size of the CommBuffer.
>> +
>> +  @return Status Code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmReadyToBootHandler (
>> +  IN     EFI_HANDLE               DispatchHandle,
>> +  IN     CONST VOID               *Context,        OPTIONAL
>> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
>> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
>> +  );
>> +
>> +/**
>> +  This function is the main entry point for an MM handler dispatch
>> +  or communicate-based callback.
>> +
>> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
>> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
>> +  @param  CommBuffer      A pointer to a collection of data in memory that will
>> +                          be conveyed from a non-MM environment into an MM environment.
>> +  @param  CommBufferSize  The size of the CommBuffer.
>> +
>> +  @return Status Code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmReadyToLockHandler (
>> +  IN     EFI_HANDLE               DispatchHandle,
>> +  IN     CONST VOID               *Context,        OPTIONAL
>> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
>> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
>> +  );
>> +
>> +/**
>> +  This function is the main entry point for an MM handler dispatch
>> +  or communicate-based callback.
>> +
>> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
>> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
>> +  @param  CommBuffer      A pointer to a collection of data in memory that will
>> +                          be conveyed from a non-MM environment into an MM environment.
>> +  @param  CommBufferSize  The size of the CommBuffer.
>> +
>> +  @return Status Code
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmEndOfDxeHandler (
>> +  IN     EFI_HANDLE               DispatchHandle,
>> +  IN     CONST VOID               *Context,        OPTIONAL
>> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
>> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
>> +  );
>> +
>> +/**
>> +  Place holder function until all the MM System Table Service are available.
>> +
>> +  @param  Arg1                   Undefined
>> +  @param  Arg2                   Undefined
>> +  @param  Arg3                   Undefined
>> +  @param  Arg4                   Undefined
>> +  @param  Arg5                   Undefined
>> +
>> +  @return EFI_NOT_AVAILABLE_YET
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +MmEfiNotAvailableYetArg5 (
>> +  UINTN Arg1,
>> +  UINTN Arg2,
>> +  UINTN Arg3,
>> +  UINTN Arg4,
>> +  UINTN Arg5
>> +  );
>> +
>> +//
>> +//Functions used during debug buils
>> +//
>> +
>> +/**
>> +  Traverse the discovered list for any drivers that were discovered but not loaded
>> +  because the dependency expressions evaluated to false.
>> +
>> +**/
>> +VOID
>> +MmDisplayDiscoveredNotDispatched (
>> +  VOID
>> +  );
>> +
>> +/**
>> +  Add free MMRAM region for use by memory service.
>> +
>> +  @param  MemBase                Base address of memory region.
>> +  @param  MemLength              Length of the memory region.
>> +  @param  Type                   Memory type.
>> +  @param  Attributes             Memory region state.
>> +
>> +**/
>> +VOID
>> +MmAddMemoryRegion (
>> +  IN      EFI_PHYSICAL_ADDRESS      MemBase,
>> +  IN      UINT64                    MemLength,
>> +  IN      EFI_MEMORY_TYPE           Type,
>> +  IN      UINT64                    Attributes
>> +  );
>> +
>> +/**
>> +  Finds the protocol entry for the requested protocol.
>> +
>> +  @param  Protocol               The ID of the protocol
>> +  @param  Create                 Create a new entry if not found
>> +
>> +  @return Protocol entry
>> +
>> +**/
>> +PROTOCOL_ENTRY  *
>> +MmFindProtocolEntry (
>> +  IN EFI_GUID   *Protocol,
>> +  IN BOOLEAN    Create
>> +  );
>> +
>> +/**
>> +  Signal event for every protocol in protocol entry.
>> +
>> +  @param  Prot                   Protocol interface
>> +
>> +**/
>> +VOID
>> +MmNotifyProtocol (
>> +  IN PROTOCOL_INTERFACE   *Prot
>> +  );
>> +
>> +/**
>> +  Finds the protocol instance for the requested handle and protocol.
>> +  Note: This function doesn't do parameters checking, it's caller's responsibility
>> +  to pass in valid parameters.
>> +
>> +  @param  Handle                 The handle to search the protocol on
>> +  @param  Protocol               GUID of the protocol
>> +  @param  Interface              The interface for the protocol being searched
>> +
>> +  @return Protocol instance (NULL: Not found)
>> +
>> +**/
>> +PROTOCOL_INTERFACE *
>> +MmFindProtocolInterface (
>> +  IN IHANDLE        *Handle,
>> +  IN EFI_GUID       *Protocol,
>> +  IN VOID           *Interface
>> +  );
>> +
>> +/**
>> +  Removes Protocol from the protocol list (but not the handle list).
>> +
>> +  @param  Handle                 The handle to remove protocol on.
>> +  @param  Protocol               GUID of the protocol to be moved
>> +  @param  Interface              The interface of the protocol
>> +
>> +  @return Protocol Entry
>> +
>> +**/
>> +PROTOCOL_INTERFACE *
>> +MmRemoveInterfaceFromProtocol (
>> +  IN IHANDLE        *Handle,
>> +  IN EFI_GUID       *Protocol,
>> +  IN VOID           *Interface
>> +  );
>> +
>> +/**
>> +  This is the POSTFIX version of the dependency evaluator.  This code does
>> +  not need to handle Before or After, as it is not valid to call this
>> +  routine in this case. POSTFIX means all the math is done on top of the stack.
>> +
>> +  @param  DriverEntry           DriverEntry element to update.
>> +
>> +  @retval TRUE                  If driver is ready to run.
>> +  @retval FALSE                 If driver is not ready to run or some fatal error
>> +                                was found.
>> +
>> +**/
>> +BOOLEAN
>> +MmIsSchedulable (
>> +  IN  EFI_MM_DRIVER_ENTRY   *DriverEntry
>> +  );
>> +
>> +/**
>> +  Dump MMRAM information.
>> +
>> +**/
>> +VOID
>> +DumpMmramInfo (
>> +  VOID
>> +  );
>> +
>> +extern UINTN                    mMmramRangeCount;
>> +extern EFI_MMRAM_DESCRIPTOR     *mMmramRanges;
>> +extern EFI_SYSTEM_TABLE         *mEfiSystemTable;
>> +
>> +#endif
>> diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.inf b/StandaloneMmPkg/Core/StandaloneMmCore.inf
>> new file mode 100644
>> index 0000000000..c5eaa14ba3
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Core/StandaloneMmCore.inf
>> @@ -0,0 +1,80 @@
>> +## @file
>> +# This module provide an SMM CIS compliant implementation of SMM Core.
>> +#
>> +# Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
>> +# Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +#
>> +# 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
>> +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +#
>> +##
>> +
>> +[Defines]
>> +  INF_VERSION                    = 0x0001001A
>> +  BASE_NAME                      = StandaloneMmCore
>> +  FILE_GUID                      = 6E14B6FD-3600-4DD6-A17A-206B3B6DCE16
>> +  MODULE_TYPE                    = MM_CORE_STANDALONE
>> +  VERSION_STRING                 = 1.0
>> +  PI_SPECIFICATION_VERSION       = 0x00010032
>> +  ENTRY_POINT                    = StandaloneMmMain
>> +
>> +#  VALID_ARCHITECTURES           = IA32 X64 AARCH64
>> +
>> +[Sources]
>> +  StandaloneMmCore.c
>> +  StandaloneMmCore.h
>> +  StandaloneMmCorePrivateData.h
>> +  Page.c
>> +  Pool.c
>> +  Handle.c
>> +  Locate.c
>> +  Notify.c
>> +  Dependency.c
>> +  Dispatcher.c
>> +  Mmi.c
>> +  InstallConfigurationTable.c
>> +  FwVol.c
>> +
>> +[Packages]
>> +  MdePkg/MdePkg.dec
>> +  MdeModulePkg/MdeModulePkg.dec
>> +  StandaloneMmPkg/StandaloneMmPkg.dec
>> +
>> +[LibraryClasses]
>> +  BaseLib
>> +  BaseMemoryLib
>> +  CacheMaintenanceLib
>> +  DebugLib
>> +  FvLib
>> +  HobLib
>> +  MemoryAllocationLib
>> +  MemLib
>> +  PeCoffLib
>> +  ReportStatusCodeLib
>> +  StandaloneMmCoreEntryPoint
>> +
>> +[Protocols]
>> +  gEfiDxeMmReadyToLockProtocolGuid             ## UNDEFINED # SmiHandlerRegister
>> +  gEfiMmReadyToLockProtocolGuid                ## PRODUCES
>> +  gEfiMmEndOfDxeProtocolGuid                   ## PRODUCES
>> +  gEfiLoadedImageProtocolGuid                   ## PRODUCES
>> +  gEfiMmConfigurationProtocolGuid               ## CONSUMES
>> +
>> +[Guids]
>> +  gAprioriGuid                                  ## SOMETIMES_CONSUMES   ## File
>> +  gEfiEventDxeDispatchGuid                      ## PRODUCES             ## GUID # SmiHandlerRegister
>> +  gEfiEndOfDxeEventGroupGuid                    ## PRODUCES             ## GUID # SmiHandlerRegister
>> +  ## SOMETIMES_CONSUMES   ## GUID # Locate protocol
>> +  ## SOMETIMES_PRODUCES   ## GUID # SmiHandlerRegister
>> +  gEdkiiMemoryProfileGuid
>> +  gZeroGuid                                     ## SOMETIMES_CONSUMES   ## GUID
>> +  gEfiHobListGuid
>> +  gMmCoreDataHobGuid
>> +  gMmFvDispatchGuid
>> +  gEfiEventLegacyBootGuid
>> +  gEfiEventExitBootServicesGuid
>> +  gEfiEventReadyToBootGuid
>> diff --git a/StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h b/StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
>> new file mode 100644
>> index 0000000000..faedf3ff2d
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
>> @@ -0,0 +1,66 @@
>> +/** @file
>> +  The internal header file that declared a data structure that is shared
>> +  between the MM IPL and the MM Core.
>> +
>> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
>> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +  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
>> +
>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +
>> +**/
>> +
>> +#ifndef _STANDALONE_MM_CORE_PRIVATE_DATA_H_
>> +#define _STANDALONE_MM_CORE_PRIVATE_DATA_H_
>> +
>> +#include <Guid/MmCoreData.h>
>> +
>> +//
>> +// Page management
>> +//
>> +
>> +typedef struct {
>> +  LIST_ENTRY  Link;
>> +  UINTN       NumberOfPages;
>> +} FREE_PAGE_LIST;
>> +
>> +extern LIST_ENTRY  mMmMemoryMap;
>> +
>> +//
>> +// Pool management
>> +//
>> +
>> +//
>> +// MIN_POOL_SHIFT must not be less than 5
>> +//
>> +#define MIN_POOL_SHIFT  6
>> +#define MIN_POOL_SIZE   (1 << MIN_POOL_SHIFT)
>> +
>> +//
>> +// MAX_POOL_SHIFT must not be less than EFI_PAGE_SHIFT - 1
>> +//
>> +#define MAX_POOL_SHIFT  (EFI_PAGE_SHIFT - 1)
>> +#define MAX_POOL_SIZE   (1 << MAX_POOL_SHIFT)
>> +
>> +//
>> +// MAX_POOL_INDEX are calculated by maximum and minimum pool sizes
>> +//
>> +#define MAX_POOL_INDEX  (MAX_POOL_SHIFT - MIN_POOL_SHIFT + 1)
>> +
>> +typedef struct {
>> +  UINTN        Size;
>> +  BOOLEAN      Available;
>> +} POOL_HEADER;
>> +
>> +typedef struct {
>> +  POOL_HEADER  Header;
>> +  LIST_ENTRY   Link;
>> +} FREE_POOL_HEADER;
>> +
>> +extern LIST_ENTRY  mMmPoolLists[MAX_POOL_INDEX];
>> +
>> +#endif
>> diff --git a/StandaloneMmPkg/Include/Guid/MmFvDispatch.h b/StandaloneMmPkg/Include/Guid/MmFvDispatch.h
>> new file mode 100644
>> index 0000000000..fb194d3474
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Include/Guid/MmFvDispatch.h
>> @@ -0,0 +1,38 @@
>> +/** @file
>> +  GUIDs for MM Event.
>> +
>> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
>> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +
>> +This program and the accompanying materials are licensed and made available under
>> +the terms and conditions of the BSD License that accompanies this distribution.
>> +The full text of the license may be found at
>> +http://opensource.org/licenses/bsd-license.php.
>> +
>> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +
>> +**/
>> +
>> +#ifndef __MM_FV_DISPATCH_H__
>> +#define __MM_FV_DISPATCH_H__
>> +
>> +#define MM_FV_DISPATCH_GUID \
>> +  { 0xb65694cc, 0x9e3, 0x4c3b, { 0xb5, 0xcd, 0x5, 0xf4, 0x4d, 0x3c, 0xdb, 0xff }}
>> +
>> +extern EFI_GUID gMmFvDispatchGuid;
>> +
>> +#pragma pack(1)
>> +typedef struct {
>> +  EFI_PHYSICAL_ADDRESS  Address;
>> +  UINT64                Size;
>> +} EFI_MM_COMMUNICATE_FV_DISPATCH_DATA;
>> +
>> +typedef struct {
>> +  EFI_GUID                              HeaderGuid;
>> +  UINTN                                 MessageLength;
>> +  EFI_MM_COMMUNICATE_FV_DISPATCH_DATA   Data;
>> +} EFI_MM_COMMUNICATE_FV_DISPATCH;
>> +#pragma pack()
>> +
>> +#endif
>> diff --git a/StandaloneMmPkg/Include/StandaloneMm.h b/StandaloneMmPkg/Include/StandaloneMm.h
>> new file mode 100644
>> index 0000000000..0e420315bb
>> --- /dev/null
>> +++ b/StandaloneMmPkg/Include/StandaloneMm.h
>> @@ -0,0 +1,36 @@
>> +/** @file
>> +  Standalone MM.
>> +
>> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
>> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
>> +
>> +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
>> +
>> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
>> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>> +
>> +**/
>> +
>> +#ifndef _STANDALONE_MM_H_
>> +#define _STANDALONE_MM_H_
>> +
>> +#include <PiMm.h>
>> +
>> +typedef
>> +EFI_STATUS
>> +(EFIAPI *MM_IMAGE_ENTRY_POINT) (
>> +  IN EFI_HANDLE            ImageHandle,
>> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
>> +  );
>> +
>> +typedef
>> +EFI_STATUS
>> +(EFIAPI *STANDALONE_MM_FOUNDATION_ENTRY_POINT) (
>> +  IN VOID  *HobStart
>> +  );
>> +
>> +#endif
>> --
>> 2.16.2
>>


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 14/18] StandaloneMmPkg: Describe the declaration, definition and fdf files.
  2018-04-06 14:42 ` [PATCH v1 14/18] StandaloneMmPkg: Describe the declaration, definition and fdf files Supreeth Venkatesh
  2018-04-18 19:50   ` Daniil Egranov
@ 2018-04-30 19:32   ` Achin Gupta
  2018-05-04 23:28     ` Supreeth Venkatesh
  1 sibling, 1 reply; 70+ messages in thread
From: Achin Gupta @ 2018-04-30 19:32 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

Hi Supreeth,

One CIL.

On Fri, Apr 06, 2018 at 03:42:19PM +0100, Supreeth Venkatesh wrote:
> This patch describes the package declarations, definitions and firmware
> device files for creating standalone management mode image with
> core/foundation and drivers.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  StandaloneMmPkg/StandaloneMmPkg.dec |  47 +++++++++
>  StandaloneMmPkg/StandaloneMmPkg.dsc | 132 ++++++++++++++++++++++++++
>  StandaloneMmPkg/StandaloneMmPkg.fdf | 184 ++++++++++++++++++++++++++++++++++++
>  3 files changed, 363 insertions(+)
>  create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dec
>  create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dsc
>  create mode 100644 StandaloneMmPkg/StandaloneMmPkg.fdf
> 
> diff --git a/StandaloneMmPkg/StandaloneMmPkg.dec b/StandaloneMmPkg/StandaloneMmPkg.dec
> new file mode 100644
> index 0000000000..36521bb039
> --- /dev/null
> +++ b/StandaloneMmPkg/StandaloneMmPkg.dec
> @@ -0,0 +1,47 @@
> +## @file
> +# This package is a platform package that provide platform module/library
> +# required by Standalone MM platform.
> +#
> +# Copyright (c) 2016-2017, ARM Ltd. All rights reserved.<BR>
> +#
> +# 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
> +#
> +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#
> +
> +[Defines]
> +  DEC_SPECIFICATION              = 0x0001001A
> +  PACKAGE_NAME                   = StandaloneMmPkg
> +  PACKAGE_GUID                   = 2AE82968-7769-4A85-A5BC-A0954CE54A5C
> +  PACKAGE_VERSION                = 1.0
> +
> +[Includes]
> +  Include
> +
> +[LibraryClasses]
> +
> +[Guids]
> +  gStandaloneMmPkgTokenSpaceGuid           = { 0x18fe7632, 0xf5c8, 0x4e63, { 0x8d, 0xe8, 0x17, 0xa5, 0x5c, 0x59, 0x13, 0xbd }}
> +  gMpInformationHobGuid                    = { 0xba33f15d, 0x4000, 0x45c1, { 0x8e, 0x88, 0xf9, 0x16, 0x92, 0xd4, 0x57, 0xe3 }}
> +  gMmFvDispatchGuid                        = { 0xb65694cc, 0x09e3, 0x4c3b, { 0xb5, 0xcd, 0x05, 0xf4, 0x4d, 0x3c, 0xdb, 0xff }}
> +
> +  ## Include/Guid/MmCoreData.h
> +  gMmCoreDataHobGuid                       = { 0xa160bf99, 0x2aa4, 0x4d7d, { 0x99, 0x93, 0x89, 0x9c, 0xb1, 0x2d, 0xf3, 0x76 }}
> +
> +  ## Include/Guid/MmramMemoryReserve.h
> +  gEfiMmPeiMmramMemoryReserveGuid          = { 0x0703f912, 0xbf8d, 0x4e2a, { 0xbe, 0x07, 0xab, 0x27, 0x25, 0x25, 0xc5, 0x92 }}
> +
> +  gEfiStandaloneMmNonSecureBufferGuid      = { 0xf00497e3, 0xbfa2, 0x41a1, { 0x9d, 0x29, 0x54, 0xc2, 0xe9, 0x37, 0x21, 0xc5 }}
> +  gEfiArmTfCpuDriverEpDescriptorGuid       = { 0x6ecbd5a1, 0xc0f8, 0x4702, { 0x83, 0x01, 0x4f, 0xc2, 0xc5, 0x47, 0x0a, 0x51 }}
> +
> +[PcdsFeatureFlag]
> +  gStandaloneMmPkgTokenSpaceGuid.PcdStandaloneMmEnable|FALSE|BOOLEAN|0x00000001
> +
> +[Protocols]
> +  gEfiMmConfigurationProtocolGuid          = { 0xc109319, 0xc149, 0x450e,  { 0xa3, 0xe3, 0xb9, 0xba, 0xdd, 0x9d, 0xc3, 0xa4 }}
> +
> diff --git a/StandaloneMmPkg/StandaloneMmPkg.dsc b/StandaloneMmPkg/StandaloneMmPkg.dsc
> new file mode 100644
> index 0000000000..8cc996f6b0
> --- /dev/null
> +++ b/StandaloneMmPkg/StandaloneMmPkg.dsc
> @@ -0,0 +1,132 @@
> +## @file
> +# Standalone MM Platform.
> +#
> +# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +# Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +#
> +#    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
> +#
> +#    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +##
> +
> +################################################################################
> +#
> +# Defines Section - statements that will be processed to create a Makefile.
> +#
> +################################################################################
> +[Defines]
> +  PLATFORM_NAME                  = StandaloneMm
> +  PLATFORM_GUID                  = 9A4BBA60-B4F9-47C7-9258-3BD77CAE9322
> +  PLATFORM_VERSION               = 1.0
> +  DSC_SPECIFICATION              = 0x00010011
> +  OUTPUT_DIRECTORY               = Build/StandaloneMmPkg
> +  SUPPORTED_ARCHITECTURES        = IA32|X64|AARCH64
> +  BUILD_TARGETS                  = DEBUG|RELEASE
> +  SKUID_IDENTIFIER               = DEFAULT
> +  FLASH_DEFINITION               = StandaloneMmPkg/StandaloneMmPkg.fdf
> +  DEFINE DEBUG_MESSAGE           = TRUE
> +
> +  # LzmaF86
> +  DEFINE COMPRESSION_TOOL_GUID   = D42AE6BD-1352-4bfb-909A-CA72A6EAE889
> +
> +################################################################################
> +#
> +# Library Class section - list of all Library Classes needed by this Platform.
> +#
> +################################################################################
> +[LibraryClasses]
> +  #
> +  # Basic
> +  #
> +  BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
> +  BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
> +  DebugLib|MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort.inf
> +  DebugPrintErrorLevelLib|MdePkg/Library/BaseDebugPrintErrorLevelLib/BaseDebugPrintErrorLevelLib.inf
> +  FvLib|StandaloneMmPkg/Library/FvLib/FvLib.inf
> +  IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
> +  MemoryAllocationLib|StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
> +  PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
> +  PeCoffLib|MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf
> +  PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf
> +  ReportStatusCodeLib|MdePkg/Library/BaseReportStatusCodeLibNull/BaseReportStatusCodeLibNull.inf
> +
> +  #
> +  # Entry point
> +  #
> +  StandaloneMmDriverEntryPoint|StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf
> +
> +[LibraryClasses.AARCH64]
> +  ArmLib|ArmPkg/Library/ArmLib/ArmBaseLib.inf
> +  ArmMmuLib|ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
> +  ArmSvcLib|ArmPkg/Library/ArmSvcLib/ArmSvcLib.inf
> +  CacheMaintenanceLib|ArmPkg/Library/ArmCacheMaintenanceLib/ArmCacheMaintenanceLib.inf
> +  HobLib|StandaloneMmPkg/Library/HobLib/HobLib.inf
> +  MemLib|StandaloneMmPkg/Library/MemLib/MemLib.inf
> +  PeCoffExtraActionLib|ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.inf
> +  PL011UartLib|ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.inf
> +  # ARM PL011 UART Driver
> +  SerialPortLib|ArmPlatformPkg/Library/PL011SerialPortLib/PL011SerialPortLib.inf
> +
> +  StandaloneMmCoreEntryPoint|StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
> +
> +################################################################################
> +#
> +# Pcd Section - list of all EDK II PCD Entries defined by this Platform
> +#
> +################################################################################
> +[PcdsFeatureFlag]
> +  gStandaloneMmPkgTokenSpaceGuid.PcdStandaloneMmEnable|TRUE
> +
> +[PcdsFixedAtBuild]
> +  gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x800000CF
> +  gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0xff
> +  gEfiMdePkgTokenSpaceGuid.PcdReportStatusCodePropertyMask|0x0f
> +
> +[PcdsFixedAtBuild.AARCH64]
> +  ## PL011 - Serial Terminal
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialRegisterBase|0x1c0b0000
> +  gEfiMdePkgTokenSpaceGuid.PcdUartDefaultBaudRate|115200
> +
> +###################################################################################################
> +#
> +# Components Section - list of the modules and components that will be processed by compilation
> +#                      tools and the EDK II tools to generate PE32/PE32+/Coff image files.
> +#
> +# Note: The EDK II DSC file is not used to specify how compiled binary images get placed
> +#       into firmware volume images. This section is just a list of modules to compile from
> +#       source into UEFI-compliant binaries.
> +#       It is the FDF file that contains information on combining binary files into firmware
> +#       volume images, whose concept is beyond UEFI and is described in PI specification.
> +#       Binary modules do not need to be listed in this section, as they should be
> +#       specified in the FDF file. For example: Shell binary (Shell_Full.efi), FAT binary (Fat.efi),
> +#       Logo (Logo.bmp), and etc.
> +#       There may also be modules listed in this section that are not required in the FDF file,
> +#       When a module listed here is excluded from FDF file, then UEFI-compliant binary will be
> +#       generated for it, but the binary will not be put into any firmware volume.
> +#
> +###################################################################################################
> +[Components.common]
> +  #
> +  # MM Core
> +  #
> +  StandaloneMmPkg/Core/StandaloneMmCore.inf
> +
> +[Components.AARCH64]
> +  StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> +
> +###################################################################################################
> +#
> +# BuildOptions Section - Define the module specific tool chain flags that should be used as
> +#                        the default flags for a module. These flags are appended to any
> +#                        standard flags that are defined by the build process. They can be
> +#                        applied for any modules or only those modules with the specific
> +#                        module style (EDK or EDKII) specified in [Components] section.
> +#
> +###################################################################################################
> +[BuildOptions.Common]
> +GCC:*_*_*_DLINK_FLAGS = -z common-page-size=0x1000

I think these must be AArch64 specific build options. Also as discussed, use of FP/SIMD must be disabled here.

cheers,
Achin

> diff --git a/StandaloneMmPkg/StandaloneMmPkg.fdf b/StandaloneMmPkg/StandaloneMmPkg.fdf
> new file mode 100644
> index 0000000000..7a22a51b4c
> --- /dev/null
> +++ b/StandaloneMmPkg/StandaloneMmPkg.fdf
> @@ -0,0 +1,184 @@
> +#
> +#  Copyright (c) 2011 - 2017, ARM Limited. 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
> +#
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +
> +################################################################################
> +#
> +# FD Section
> +# The [FD] Section is made up of the definition statements and a
> +# description of what goes into  the Flash Device Image.  Each FD section
> +# defines one flash "device" image.  A flash device image may be one of
> +# the following: Removable media bootable image (like a boot floppy
> +# image,) an Option ROM image (that would be "flashed" into an add-in
> +# card,) a System "Flash"  image (that would be burned into a system's
> +# flash) or an Update ("Capsule") image that will be used to update and
> +# existing system flash.
> +#
> +################################################################################
> +
> +[FD.]
> +!ifdef ARM_FVP_RUN_NORFLASH
> +BaseAddress   = 0x08000000|gArmTokenSpaceGuid.PcdFdBaseAddress  # The base address of the Firmware in Flash0.
> +!else
> +BaseAddress   = 0xff200000|gArmTokenSpaceGuid.PcdFdBaseAddress  # UEFI in DRAM + 128MB.
> +!endif
> +Size          = 0x00e00000|gArmTokenSpaceGuid.PcdFdSize         # The size in bytes of the device (64MiB).
> +ErasePolarity = 1
> +
> +# This one is tricky, it must be: BlockSize * NumBlocks = Size
> +BlockSize     = 0x00001000
> +NumBlocks     = 0x0e00
> +
> +0x00000000|0x00280000
> +gArmTokenSpaceGuid.PcdFvBaseAddress|gArmTokenSpaceGuid.PcdFvSize
> +FV = FVMAIN_COMPACT
> +
> +[FV.FVMAIN_COMPACT]
> +FvAlignment        = 16
> +ERASE_POLARITY     = 1
> +MEMORY_MAPPED      = TRUE
> +STICKY_WRITE       = TRUE
> +LOCK_CAP           = TRUE
> +LOCK_STATUS        = TRUE
> +WRITE_DISABLED_CAP = TRUE
> +WRITE_ENABLED_CAP  = TRUE
> +WRITE_STATUS       = TRUE
> +WRITE_LOCK_CAP     = TRUE
> +WRITE_LOCK_STATUS  = TRUE
> +READ_DISABLED_CAP  = TRUE
> +READ_ENABLED_CAP   = TRUE
> +READ_STATUS        = TRUE
> +READ_LOCK_CAP      = TRUE
> +READ_LOCK_STATUS   = TRUE
> +
> +INF StandaloneMmPkg/Core/StandaloneMmCore.inf
> +INF StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> +
> +################################################################################
> +#
> +# Rules are use with the [FV] section's module INF type to define
> +# how an FFS file is created for a given INF file. The following Rule are the default
> +# rules for the different module type. User can add the customized rules to define the
> +# content of the FFS file.
> +#
> +################################################################################
> +
> +
> +############################################################################
> +# Example of a DXE_DRIVER FFS file with a Checksum encapsulation section   #
> +############################################################################
> +#
> +#[Rule.Common.DXE_DRIVER]
> +#  FILE DRIVER = $(NAMED_GUID) {
> +#    DXE_DEPEX    DXE_DEPEX               Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +#    COMPRESS PI_STD {
> +#      GUIDED {
> +#        PE32     PE32                    $(INF_OUTPUT)/$(MODULE_NAME).efi
> +#        UI       STRING="$(MODULE_NAME)" Optional
> +#        VERSION  STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +#      }
> +#    }
> +#  }
> +#
> +############################################################################
> +
> +[Rule.Common.SEC]
> +  FILE SEC = $(NAMED_GUID) RELOCS_STRIPPED FIXED {
> +    TE  TE Align = Auto                 $(INF_OUTPUT)/$(MODULE_NAME).efi
> +  }
> +
> +[Rule.Common.MM_CORE_STANDALONE]
> +  FILE SEC = $(NAMED_GUID) RELOCS_STRIPPED FIXED {
> +    PE32  PE32 Align = Auto             $(INF_OUTPUT)/$(MODULE_NAME).efi
> +  }
> +
> +[Rule.Common.MM_STANDALONE]
> +  FILE MM_STANDALONE = $(NAMED_GUID) {
> +    SMM_DEPEX SMM_DEPEX Optional       $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32      PE32                     $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI        STRING="$(MODULE_NAME)" Optional
> +    VERSION   STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +  }
> +
> +[Rule.Common.PEI_CORE]
> +  FILE PEI_CORE = $(NAMED_GUID) FIXED {
> +    TE     TE Align = Auto              $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI     STRING ="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.PEIM]
> +  FILE PEIM = $(NAMED_GUID) FIXED {
> +     PEI_DEPEX PEI_DEPEX Optional       $(INF_OUTPUT)/$(MODULE_NAME).depex
> +     TE       TE Align = Auto           $(INF_OUTPUT)/$(MODULE_NAME).efi
> +     UI       STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.PEIM.TIANOCOMPRESSED]
> +  FILE PEIM = $(NAMED_GUID) DEBUG_MYTOOLS_IA32 {
> +    PEI_DEPEX PEI_DEPEX Optional        $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    GUIDED A31280AD-481E-41B6-95E8-127F4C984779 PROCESSING_REQUIRED = TRUE {
> +      PE32      PE32                    $(INF_OUTPUT)/$(MODULE_NAME).efi
> +      UI        STRING="$(MODULE_NAME)" Optional
> +    }
> +  }
> +
> +[Rule.Common.DXE_CORE]
> +  FILE DXE_CORE = $(NAMED_GUID) {
> +    PE32     PE32                       $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI       STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.UEFI_DRIVER]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32         PE32                   $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI           STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.DXE_DRIVER]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32         PE32                   $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI           STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.DXE_RUNTIME_DRIVER]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32         PE32                   $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI           STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.UEFI_APPLICATION]
> +  FILE APPLICATION = $(NAMED_GUID) {
> +    UI     STRING ="$(MODULE_NAME)"     Optional
> +    PE32   PE32                         $(INF_OUTPUT)/$(MODULE_NAME).efi
> +  }
> +
> +[Rule.Common.UEFI_DRIVER.BINARY]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX DXE_DEPEX Optional      |.depex
> +    PE32      PE32                    |.efi
> +    UI        STRING="$(MODULE_NAME)" Optional
> +    VERSION   STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +  }
> +
> +[Rule.Common.UEFI_APPLICATION.BINARY]
> +  FILE APPLICATION = $(NAMED_GUID) {
> +    PE32      PE32                    |.efi
> +    UI        STRING="$(MODULE_NAME)" Optional
> +    VERSION   STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +  }
> +
> +[Rule.Common.USER_DEFINED.ACPITABLE]
> +  FILE FREEFORM = $(NAMED_GUID) {
> +    RAW ASL                |.aml
> +  }
> -- 
> 2.16.2
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 15/18] ArmPkg: Extra action to update permissions for S-ELO MM Image.
  2018-04-06 14:42 ` [PATCH v1 15/18] ArmPkg: Extra action to update permissions for S-ELO MM Image Supreeth Venkatesh
@ 2018-04-30 19:49   ` Achin Gupta
  2018-05-04 23:30     ` Supreeth Venkatesh
  0 siblings, 1 reply; 70+ messages in thread
From: Achin Gupta @ 2018-04-30 19:49 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

Hi Supreeth,

This file was originally contributed by Ard a while back so worth poking him and
Leif for review. If MM is expected to be the only use case of this library then
it might make sense to pull in under the StandaloneMmPkg instead of relying on
PcdStandaloneMmEnable.

Cheers,
Achin

On Fri, Apr 06, 2018 at 03:42:20PM +0100, Supreeth Venkatesh wrote:
> The Standalone MM drivers runs in S-EL0 in AArch64 on ARM Standard
> Platforms and is deployed during SEC phase. The memory allocated to the
> Standalone MM drivers should be marked as RO+X.
> 
> During PE/COFF Image section parsing, this patch implements extra action
> "UpdatePeCoffPermissions" to request the privileged firmware in EL3 to
> update the permissions.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  .../DebugPeCoffExtraActionLib.c                    | 185 +++++++++++++++++++--
>  .../DebugPeCoffExtraActionLib.inf                  |   7 +
>  2 files changed, 181 insertions(+), 11 deletions(-)
> 
> diff --git a/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.c b/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.c
> index f298e58cdf..c87aaf05c7 100644
> --- a/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.c
> +++ b/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.c
> @@ -15,14 +15,165 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>  **/
>  
>  #include <PiDxe.h>
> -#include <Library/PeCoffLib.h>
>  
> +#include <Library/ArmMmuLib.h>
>  #include <Library/BaseLib.h>
> -#include <Library/DebugLib.h>
>  #include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/PcdLib.h>
> +#include <Library/PeCoffLib.h>
>  #include <Library/PeCoffExtraActionLib.h>
>  #include <Library/PrintLib.h>
>  
> +typedef RETURN_STATUS (*REGION_PERMISSION_UPDATE_FUNC) (
> +  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
> +  IN  UINT64                    Length
> +  );
> +
> +STATIC
> +RETURN_STATUS
> +UpdatePeCoffPermissions (
> +  IN  CONST PE_COFF_LOADER_IMAGE_CONTEXT      *ImageContext,
> +  IN  REGION_PERMISSION_UPDATE_FUNC           NoExecUpdater,
> +  IN  REGION_PERMISSION_UPDATE_FUNC           ReadOnlyUpdater
> +  )
> +{
> +  RETURN_STATUS                         Status;
> +  EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION   Hdr;
> +  EFI_IMAGE_OPTIONAL_HEADER_UNION       HdrData;
> +  UINTN                                 Size;
> +  UINTN                                 ReadSize;
> +  UINT32                                SectionHeaderOffset;
> +  UINTN                                 NumberOfSections;
> +  UINTN                                 Index;
> +  EFI_IMAGE_SECTION_HEADER              SectionHeader;
> +  PE_COFF_LOADER_IMAGE_CONTEXT          TmpContext;
> +  EFI_PHYSICAL_ADDRESS                  Base;
> +
> +  //
> +  // We need to copy ImageContext since PeCoffLoaderGetImageInfo ()
> +  // will mangle the ImageAddress field
> +  //
> +  CopyMem (&TmpContext, ImageContext, sizeof (TmpContext));
> +
> +  if (TmpContext.PeCoffHeaderOffset == 0) {
> +    Status = PeCoffLoaderGetImageInfo (&TmpContext);
> +    if (RETURN_ERROR (Status)) {
> +      DEBUG ((DEBUG_ERROR,
> +        "%a: PeCoffLoaderGetImageInfo () failed (Status = %r)\n",
> +        __FUNCTION__, Status));
> +      return Status;
> +    }
> +  }
> +
> +  if (TmpContext.IsTeImage &&
> +      TmpContext.ImageAddress == ImageContext->ImageAddress) {
> +    DEBUG ((DEBUG_INFO, "%a: ignoring XIP TE image at 0x%lx\n", __FUNCTION__,
> +      ImageContext->ImageAddress));
> +    return RETURN_SUCCESS;
> +  }
> +
> +  if (TmpContext.SectionAlignment < EFI_PAGE_SIZE) {
> +    //
> +    // The sections need to be at least 4 KB aligned, since that is the
> +    // granularity at which we can tighten permissions. So just clear the
> +    // noexec permissions on the entire region.
> +    //
> +    if (!TmpContext.IsTeImage) {
> +      DEBUG ((DEBUG_WARN,
> +        "%a: non-TE Image at 0x%lx has SectionAlignment < 4 KB (%lu)\n",
> +        __FUNCTION__, ImageContext->ImageAddress, TmpContext.SectionAlignment));
> +    }
> +    Base = ImageContext->ImageAddress & ~(EFI_PAGE_SIZE - 1);
> +    Size = ImageContext->ImageAddress - Base + ImageContext->ImageSize;
> +    return NoExecUpdater (Base, ALIGN_VALUE (Size, EFI_PAGE_SIZE));
> +  }
> +
> +  //
> +  // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
> +  // data, but that should not hurt anything. Hdr.Pe32->OptionalHeader.Magic
> +  // determines if this is a PE32 or PE32+ image. The magic is in the same
> +  // location in both images.
> +  //
> +  Hdr.Union = &HdrData;
> +  Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);
> +  ReadSize = Size;
> +  Status = TmpContext.ImageRead (TmpContext.Handle,
> +                         TmpContext.PeCoffHeaderOffset, &Size, Hdr.Pe32);
> +  if (RETURN_ERROR (Status) || (Size != ReadSize)) {
> +    DEBUG ((DEBUG_ERROR,
> +      "%a: TmpContext.ImageRead () failed (Status = %r)\n",
> +      __FUNCTION__, Status));
> +    return Status;
> +  }
> +
> +  ASSERT (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE);
> +
> +  SectionHeaderOffset = TmpContext.PeCoffHeaderOffset + sizeof (UINT32) +
> +                        sizeof (EFI_IMAGE_FILE_HEADER);
> +  NumberOfSections    = (UINTN)(Hdr.Pe32->FileHeader.NumberOfSections);
> +
> +  switch (Hdr.Pe32->OptionalHeader.Magic) {
> +    case EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC:
> +      SectionHeaderOffset += Hdr.Pe32->FileHeader.SizeOfOptionalHeader;
> +      break;
> +    case EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC:
> +      SectionHeaderOffset += Hdr.Pe32Plus->FileHeader.SizeOfOptionalHeader;
> +      break;
> +    default:
> +      ASSERT (FALSE);
> +  }
> +
> +  //
> +  // Iterate over the sections
> +  //
> +  for (Index = 0; Index < NumberOfSections; Index++) {
> +    //
> +    // Read section header from file
> +    //
> +    Size = sizeof (EFI_IMAGE_SECTION_HEADER);
> +    ReadSize = Size;
> +    Status = TmpContext.ImageRead (TmpContext.Handle, SectionHeaderOffset,
> +                                   &Size, &SectionHeader);
> +    if (RETURN_ERROR (Status) || (Size != ReadSize)) {
> +      DEBUG ((DEBUG_ERROR,
> +        "%a: TmpContext.ImageRead () failed (Status = %r)\n",
> +        __FUNCTION__, Status));
> +      return Status;
> +    }
> +
> +    Base = TmpContext.ImageAddress + SectionHeader.VirtualAddress;
> +
> +    if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_MEM_EXECUTE) == 0) {
> +
> +      if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_MEM_WRITE) == 0 &&
> +          TmpContext.ImageType != EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
> +
> +        DEBUG ((DEBUG_INFO,
> +          "%a: Mapping section %d of image at 0x%lx with RO-XN permissions and size 0x%x\n",
> +          __FUNCTION__, Index, Base, SectionHeader.Misc.VirtualSize));
> +        ReadOnlyUpdater (Base, SectionHeader.Misc.VirtualSize);
> +      } else {
> +        DEBUG ((DEBUG_WARN,
> +          "%a: Mapping section %d of image at 0x%lx with RW-XN permissions and size 0x%x\n",
> +          __FUNCTION__, Index, Base, SectionHeader.Misc.VirtualSize));
> +      }
> +    } else {
> +        DEBUG ((DEBUG_INFO,
> +          "%a: Mapping section %d of image at 0x%lx with RO-XN permissions and size 0x%x\n",
> +           __FUNCTION__, Index, Base, SectionHeader.Misc.VirtualSize));
> +        ReadOnlyUpdater (Base, SectionHeader.Misc.VirtualSize);
> +
> +        DEBUG ((DEBUG_INFO,
> +          "%a: Mapping section %d of image at 0x%lx with RO-X permissions and size 0x%x\n",
> +          __FUNCTION__, Index, Base, SectionHeader.Misc.VirtualSize));
> +        NoExecUpdater (Base, SectionHeader.Misc.VirtualSize);
> +    }
> +
> +    SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
> +  }
> +  return RETURN_SUCCESS;
> +}
>  
>  /**
>    If the build is done on cygwin the paths are cygpaths.
> @@ -83,23 +234,29 @@ PeCoffLoaderRelocateImageExtraAction (
>    CHAR8 Temp[512];
>  #endif
>  
> +  if (PcdGetBool(PcdStandaloneMmEnable) == TRUE)
> +  {
> +     UpdatePeCoffPermissions (ImageContext, ArmClearMemoryRegionNoExec,
> +                              ArmSetMemoryRegionReadOnly);
> +  }
> +
>    if (ImageContext->PdbPointer) {
>  #ifdef __CC_ARM
>  #if (__ARMCC_VERSION < 500000)
>      // Print out the command for the RVD debugger to load symbols for this image
> -    DEBUG ((EFI_D_LOAD | EFI_D_INFO, "load /a /ni /np %a &0x%p\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
> +    DEBUG ((DEBUG_LOAD | DEBUG_INFO, "load /a /ni /np %a &0x%p\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
>  #else
>      // Print out the command for the DS-5 to load symbols for this image
> -    DEBUG ((EFI_D_LOAD | EFI_D_INFO, "add-symbol-file %a 0x%p\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
> +    DEBUG ((DEBUG_LOAD | DEBUG_INFO, "add-symbol-file %a 0x%p\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
>  #endif
>  #elif __GNUC__
>      // This may not work correctly if you generate PE/COFF directlyas then the Offset would not be required
> -    DEBUG ((EFI_D_LOAD | EFI_D_INFO, "add-symbol-file %a 0x%p\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
> +    DEBUG ((DEBUG_LOAD | DEBUG_INFO, "add-symbol-file %a 0x%p\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
>  #else
> -    DEBUG ((EFI_D_LOAD | EFI_D_INFO, "Loading driver at 0x%11p EntryPoint=0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress, FUNCTION_ENTRY_POINT (ImageContext->EntryPoint)));
> +    DEBUG ((DEBUG_LOAD | DEBUG_INFO, "Loading driver at 0x%11p EntryPoint=0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress, FUNCTION_ENTRY_POINT (ImageContext->EntryPoint)));
>  #endif
>    } else {
> -    DEBUG ((EFI_D_LOAD | EFI_D_INFO, "Loading driver at 0x%11p EntryPoint=0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress, FUNCTION_ENTRY_POINT (ImageContext->EntryPoint)));
> +    DEBUG ((DEBUG_LOAD | DEBUG_INFO, "Loading driver at 0x%11p EntryPoint=0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress, FUNCTION_ENTRY_POINT (ImageContext->EntryPoint)));
>    }
>  }
>  
> @@ -125,17 +282,23 @@ PeCoffLoaderUnloadImageExtraAction (
>    CHAR8 Temp[512];
>  #endif
>  
> +  if (PcdGetBool(PcdStandaloneMmEnable) == TRUE)
> +  {
> +     UpdatePeCoffPermissions (ImageContext, ArmSetMemoryRegionNoExec,
> +                              ArmClearMemoryRegionReadOnly);
> +  }
> +
>    if (ImageContext->PdbPointer) {
>  #ifdef __CC_ARM
>      // Print out the command for the RVD debugger to load symbols for this image
> -    DEBUG ((EFI_D_ERROR, "unload symbols_only %a\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp))));
> +    DEBUG ((DEBUG_ERROR, "unload symbols_only %a\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp))));
>  #elif __GNUC__
>      // This may not work correctly if you generate PE/COFF directlyas then the Offset would not be required
> -    DEBUG ((EFI_D_ERROR, "remove-symbol-file %a 0x%08x\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
> +    DEBUG ((DEBUG_ERROR, "remove-symbol-file %a 0x%08x\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
>  #else
> -    DEBUG ((EFI_D_ERROR, "Unloading %a\n", ImageContext->PdbPointer));
> +    DEBUG ((DEBUG_ERROR, "Unloading %a\n", ImageContext->PdbPointer));
>  #endif
>    } else {
> -    DEBUG ((EFI_D_ERROR, "Unloading driver at 0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress));
> +    DEBUG ((DEBUG_ERROR, "Unloading driver at 0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress));
>    }
>  }
> diff --git a/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.inf b/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.inf
> index c1f717e5bd..38bf3993ae 100644
> --- a/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.inf
> +++ b/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.inf
> @@ -33,7 +33,14 @@
>    DebugPeCoffExtraActionLib.c
>  
>  [Packages]
> +  ArmPkg/ArmPkg.dec
>    MdePkg/MdePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[FeaturePcd]
> +  gStandaloneMmPkgTokenSpaceGuid.PcdStandaloneMmEnable
>  
>  [LibraryClasses]
> +  ArmMmuLib
>    DebugLib
> +  PcdLib
> -- 
> 2.16.2
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 16/18] BaseTools/AutoGen: Update header file for MM modules.
  2018-04-06 14:42 ` [PATCH v1 16/18] BaseTools/AutoGen: Update header file for MM modules Supreeth Venkatesh
@ 2018-04-30 19:52   ` Achin Gupta
  2018-05-04 23:30     ` Supreeth Venkatesh
  0 siblings, 1 reply; 70+ messages in thread
From: Achin Gupta @ 2018-04-30 19:52 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

Hi Supreeth,

CIL.

On Fri, Apr 06, 2018 at 03:42:21PM +0100, Supreeth Venkatesh wrote:
> This patch corrects the Module Type Header file for Management Mode(MM)
> as specified in PI v1.6 Specification. Also, it updates parameter for
> auto generated template functions from EFI_SMM_SYSTEM_TABLE2 to
> EFI_MM_SYSTEM_TABLE.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>

-Achin & +Jiewen if possible!

Acked-by: Achin Gupta <achin.gupta@arm.com>

cheers,
Achin

> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  BaseTools/Source/Python/AutoGen/GenC.py | 16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)
> 
> diff --git a/BaseTools/Source/Python/AutoGen/GenC.py b/BaseTools/Source/Python/AutoGen/GenC.py
> index 4d9ea1b2a8..8601e4ee70 100644
> --- a/BaseTools/Source/Python/AutoGen/GenC.py
> +++ b/BaseTools/Source/Python/AutoGen/GenC.py
> @@ -270,7 +270,7 @@ EFI_STATUS
>  EFIAPI
>  ${Function} (
>    IN EFI_HANDLE            ImageHandle,
> -  IN EFI_SMM_SYSTEM_TABLE2 *MmSystemTable
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
>    );
>  ${END}
>  """)
> @@ -283,7 +283,7 @@ EFI_STATUS
>  EFIAPI
>  ProcessModuleEntryPointList (
>    IN EFI_HANDLE            ImageHandle,
> -  IN EFI_SMM_SYSTEM_TABLE2 *MmSystemTable
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
>    )
>  
>  {
> @@ -297,7 +297,7 @@ EFI_STATUS
>  EFIAPI
>  ProcessModuleEntryPointList (
>    IN EFI_HANDLE            ImageHandle,
> -  IN EFI_SMM_SYSTEM_TABLE2 *MmSystemTable
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
>    )
>  
>  {
> @@ -312,7 +312,7 @@ EFI_STATUS
>  EFIAPI
>  ProcessModuleEntryPointList (
>    IN EFI_HANDLE            ImageHandle,
> -  IN EFI_SMM_SYSTEM_TABLE2 *MmSystemTable
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
>    )
>  
>  {
> @@ -680,7 +680,7 @@ EFI_STATUS
>  EFIAPI
>  ${Function} (
>    IN EFI_HANDLE            ImageHandle,
> -  IN EFI_SMM_SYSTEM_TABLE2  *MmSystemTable
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
>    );${END}
>  """),
>  }
> @@ -760,7 +760,7 @@ VOID
>  EFIAPI
>  ProcessLibrary${Type}List (
>    IN EFI_HANDLE            ImageHandle,
> -  IN EFI_SMM_SYSTEM_TABLE2  *MmSystemTable
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
>    )
>  {
>  ${BEGIN}  EFI_STATUS  Status;
> @@ -784,8 +784,8 @@ gModuleTypeHeaderFile = {
>      "UEFI_DRIVER"       :   ["Uefi.h",  "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiDriverEntryPoint.h"],
>      "UEFI_APPLICATION"  :   ["Uefi.h",  "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiApplicationEntryPoint.h"],
>      "SMM_CORE"          :   ["PiDxe.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiDriverEntryPoint.h"],
> -    "MM_STANDALONE"     :   ["PiSmm.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/SmmDriverStandaloneEntryPoint.h"],
> -    "MM_CORE_STANDALONE" :  ["PiSmm.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/SmmCoreStandaloneEntryPoint.h"],
> +    "MM_STANDALONE"     :   ["PiMm.h",  "Library/BaseLib.h", "Library/DebugLib.h", "Library/MmDriverStandaloneEntryPoint.h"],
> +    "MM_CORE_STANDALONE":   ["PiMm.h",  "Library/BaseLib.h", "Library/DebugLib.h", "Library/MmCoreStandaloneEntryPoint.h"],
>      "USER_DEFINED"      :   [gBasicHeaderFile]
>  }
>  
> -- 
> 2.16.2
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 17/18] StandaloneMmPkg: Add application to test MM communication protocol.
  2018-04-06 14:42 ` [PATCH v1 17/18] StandaloneMmPkg: Add application to test MM communication protocol Supreeth Venkatesh
@ 2018-04-30 20:02   ` Achin Gupta
  2018-05-04 23:31     ` Supreeth Venkatesh
  0 siblings, 1 reply; 70+ messages in thread
From: Achin Gupta @ 2018-04-30 20:02 UTC (permalink / raw)
  To: Supreeth Venkatesh
  Cc: edk2-devel, michael.d.kinney, liming.gao, jiewen.yao,
	leif.lindholm, ard.biesheuvel, nd

Hi Supreeth,

I agree with Jiewen that these should not be a part of this series. Can you push
these in a separate series?

cheers,
Achin

On Fri, Apr 06, 2018 at 03:42:22PM +0100, Supreeth Venkatesh wrote:
> This patch adds a simple application that uses the MM
> communication protocol to pass a copy of the UEFI system table to
> the MM environment in the secure world.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  .../Application/MmCommTestApp/MmCommTest.c         | 81 ++++++++++++++++++++++
>  .../Application/MmCommTestApp/MmCommTest.h         | 37 ++++++++++
>  .../Application/MmCommTestApp/MmCommTest.inf       | 57 +++++++++++++++
>  3 files changed, 175 insertions(+)
>  create mode 100644 StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.c
>  create mode 100644 StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.h
>  create mode 100644 StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.inf
> 
> diff --git a/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.c b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.c
> new file mode 100644
> index 0000000000..efbafdde62
> --- /dev/null
> +++ b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.c
> @@ -0,0 +1,81 @@
> +/** @file
> +  This sample application demos how to communicate
> +  with secure partition using MM communication protocol
> +
> +  Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Uefi.h>
> +#include <Library/PcdLib.h>
> +#include <Library/UefiLib.h>
> +#include <Library/UefiApplicationEntryPoint.h>
> +#include <Library/DebugLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/UefiBootServicesTableLib.h>
> +
> +#include "MmCommTest.h"
> +
> +#include <Library/ArmSmcLib.h>
> +
> +#include <Protocol/MmCommunication.h>
> +
> +EFI_MM_COMMUNICATION_PROTOCOL  *mMmCommunication = NULL;
> +
> +EFI_STATUS
> +MmIplNotifyCommTest (
> +  VOID
> +  )
> +{
> +  EFI_MM_COMMUNICATE_TEST    MmCommTest;
> +  UINTN                      Size;
> +
> +  DEBUG ((DEBUG_INFO, "MmIplNotifyCommTest\n"));
> +
> +  CopyGuid (&MmCommTest.HeaderGuid, &gMmCommTestGuid);
> +  CopyMem (&MmCommTest.Data.EfiSystemTable, gST, sizeof (EFI_SYSTEM_TABLE));
> +  MmCommTest.MessageLength = sizeof (EFI_MM_COMMUNICATE_TEST_DATA);
> +
> +  //
> +  // Generate the MM_COMMUNICATE SMC and return the result
> +  //
> +  Size = sizeof (MmCommTest);
> +  return mMmCommunication->Communicate (NULL, &MmCommTest, &Size);
> +}
> +
> +/**
> +  The user Entry Point for Application. The user code starts with this function
> +  as the real entry point for the application.
> +
> +  @param[in] ImageHandle    The firmware allocated handle for the EFI image.
> +  @param[in] SystemTable    A pointer to the EFI System Table.
> +
> +  @retval EFI_SUCCESS       The entry point is executed successfully.
> +  @retval other             Some error occurs when executing this entry point.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCommTestEntryPoint (
> +  IN EFI_HANDLE        ImageHandle,
> +  IN EFI_SYSTEM_TABLE  *SystemTable
> +  )
> +{
> +  EFI_STATUS Status;
> +
> +  Status = gBS->LocateProtocol (&gEfiMmCommunicationProtocolGuid, NULL, (VOID **) &mMmCommunication);
> +  if (EFI_ERROR (Status)) {
> +    return Status;
> +  }
> +
> +  return MmIplNotifyCommTest ();
> +}
> diff --git a/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.h b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.h
> new file mode 100644
> index 0000000000..8e8305a060
> --- /dev/null
> +++ b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.h
> @@ -0,0 +1,37 @@
> +/** @file
> +  GUIDs for MM Event.
> +
> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
> +
> +This program and the accompanying materials are licensed and made available under
> +the terms and conditions of the BSD License that accompanies this distribution.
> +The full text of the license may be found at
> +http://opensource.org/licenses/bsd-license.php.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef __MM_COMM_TEST_H__
> +#define __MM_COMM_TEST_H__
> +
> +#define MM_COMM_TEST_GUID \
> +  { 0xa37721e4, 0x8c0b, 0x4bca, { 0xb5, 0xe8, 0xe9, 0x2, 0xa0, 0x25, 0x51, 0x4e }}
> +
> +extern EFI_GUID gMmCommTestGuid;
> +
> +#pragma pack(1)
> +typedef struct {
> +  EFI_SYSTEM_TABLE      EfiSystemTable;
> +} EFI_MM_COMMUNICATE_TEST_DATA;
> +
> +typedef struct {
> +  EFI_GUID                         HeaderGuid;
> +  UINTN                            MessageLength;
> +  EFI_MM_COMMUNICATE_TEST_DATA     Data;
> +} EFI_MM_COMMUNICATE_TEST;
> +#pragma pack()
> +
> +#endif
> diff --git a/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.inf b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.inf
> new file mode 100644
> index 0000000000..1828cd7e13
> --- /dev/null
> +++ b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.inf
> @@ -0,0 +1,57 @@
> +## @file
> +#  Sample UEFI Application Reference EDKII Module.
> +#
> +#  This is a sample shell application that will print "UEFI firmware version Info!" to the
> +#  UEFI Console.
> +#
> +#  It demos how to communicate with secure partition using MM
> +#  communication protocol.
> +#
> +#  Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.<BR>
> +#  Copyright (c) 2016 - 2018, ARM Limited. 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
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = MmCommTest
> +  FILE_GUID                      = 6987936E-ED34-44db-AE97-1FA5E4ED2116
> +  MODULE_TYPE                    = UEFI_APPLICATION
> +  VERSION_STRING                 = 1.0
> +  ENTRY_POINT                    = MmCommTestEntryPoint
> +
> +[Sources]
> +  MmCommTest.c
> +
> +[Packages]
> +  ArmPkg/ArmPkg.dec
> +  ArmPlatformPkg/ArmPlatformPkg.dec
> +  MdePkg/MdePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[LibraryClasses]
> +  ArmSmcLib
> +  BaseMemoryLib
> +  DebugLib
> +  PrintLib
> +  UefiApplicationEntryPoint
> +  UefiBootServicesTableLib
> +  UefiLib
> +
> +[FeaturePcd]
> +
> +[Pcd]
> +
> +[Guids]
> +  gMmCommTestGuid
> +
> +[Protocols]
> +  gEfiMmCommunicationProtocolGuid
> -- 
> 2.16.2
> 


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 13/18] StandaloneMmPkg/Core: Implementation of Standalone MM Core Module.
  2018-04-30 19:28     ` Ard Biesheuvel
@ 2018-04-30 20:17       ` Achin Gupta
  2018-05-01  8:18       ` Laszlo Ersek
  1 sibling, 0 replies; 70+ messages in thread
From: Achin Gupta @ 2018-04-30 20:17 UTC (permalink / raw)
  To: Ard Biesheuvel
  Cc: Laszlo Ersek, Kinney, Michael D, Leif Lindholm, Andrew Fish,
	Supreeth Venkatesh, edk2-devel@lists.01.org, Gao, Liming,
	Yao, Jiewen, nd

On Mon, Apr 30, 2018 at 09:28:57PM +0200, Ard Biesheuvel wrote:
> On 30 April 2018 at 21:19, Achin Gupta <achin.gupta@arm.com> wrote:
> > Hi Supreeth,
> >
> > I think it is worth adding a signed off by Jiewen since he originally
> > contributed the code and it has not changed much since.
> 
> I disagree. A signoff does not assert authorship, it only means that
> the contributor asserts that the license permits him to contribute
> this code under the tianocore contribution agreement. Adding a signoff
> on behalf of someone else should be avoided in my opinion, because it
> suggests that code can only be contributed by the original author.
> Also, even if the author made the code available under a compatible
> license, it does not mean he subscribes to the Tianocore contribution
> agreement, and adding a signoff on behalf of someone else does imply
> that (although this should not be a problem in this particular case)
> 
> Anyone can contribute code that is available under a compatible
> license, and it is not generally possible to decide who should be
> credited as authors for code that originates in other projects.
> 
> If you want to credit the author, you can do that in the commit log.

Thanks for the clarification. I am fine with that.

cheers,
Achin

> 
> 
> 
> > Please update the
> > correct year in the copyright headers too.
> >
> > Acked-by: Achin Gupta <achin.gupta@arm.com>
> >
> > cheers,
> > Achin
> >
> > On Fri, Apr 06, 2018 at 03:42:18PM +0100, Supreeth Venkatesh wrote:
> >> Management Mode (MM) is a generic term used to describe a secure
> >> execution environment provided by the CPU and related silicon that is
> >> entered when the CPU detects a MMI. For x86 systems, this can be
> >> implemented with System Management Mode (SMM). For ARM systems, this can
> >> be implemented with TrustZone (TZ).
> >> A MMI can be a CPU instruction or interrupt. Upon detection of a MMI, a
> >> CPU will jump to the MM Entry Point and save some portion of its state
> >> (the "save state") such that execution can be resumed.
> >> The MMI can be generated synchronously by software or asynchronously by
> >> a hardware event. Each MMI source can be detected, cleared and disabled.
> >> Some systems provide for special memory (Management Mode RAM or MMRAM)
> >> which is set aside for software running in MM. Usually the MMRAM is
> >> hidden during normal CPU execution, but this is not required. Usually,
> >> after MMRAM is hidden it cannot be exposed until the next system reset.
> >>
> >> The MM Core Interface Specification describes three pieces of the PI
> >> Management Mode architecture:
> >> 1. MM Dispatch
> >>    During DXE, the DXE Foundation works with the MM Foundation to
> >>    schedule MM drivers for execution in the discovered firmware volumes.
> >> 2. MM Initialization
> >>    MM related code opens MMRAM, creates the MMRAM memory map, and
> >>    launches the MM Foundation, which provides the necessary services to
> >>    launch MM-related drivers. Then, sometime before boot, MMRAM is
> >>    closed and locked. This piece may be completed during the
> >>    SEC, PEI or DXE phases.
> >> 3. MMI Management
> >>    When an MMI generated, the MM environment is created and then the MMI
> >>
> >>    sources are detected and MMI handlers called.
> >>
> >> This patch implements the MM Core.
> >>
> >> Contributed-under: TianoCore Contribution Agreement 1.1
> >> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> >> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> >> ---
> >>  StandaloneMmPkg/Core/Dependency.c                  |  389 +++++++
> >>  StandaloneMmPkg/Core/Dispatcher.c                  | 1071 ++++++++++++++++++++
> >>  StandaloneMmPkg/Core/FwVol.c                       |  104 ++
> >>  StandaloneMmPkg/Core/Handle.c                      |  533 ++++++++++
> >>  StandaloneMmPkg/Core/InstallConfigurationTable.c   |  178 ++++
> >>  StandaloneMmPkg/Core/Locate.c                      |  496 +++++++++
> >>  StandaloneMmPkg/Core/Mmi.c                         |  337 ++++++
> >>  StandaloneMmPkg/Core/Notify.c                      |  203 ++++
> >>  StandaloneMmPkg/Core/Page.c                        |  384 +++++++
> >>  StandaloneMmPkg/Core/Pool.c                        |  287 ++++++
> >>  StandaloneMmPkg/Core/StandaloneMmCore.c            |  708 +++++++++++++
> >>  StandaloneMmPkg/Core/StandaloneMmCore.h            |  903 +++++++++++++++++
> >>  StandaloneMmPkg/Core/StandaloneMmCore.inf          |   80 ++
> >>  StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h |   66 ++
> >>  StandaloneMmPkg/Include/Guid/MmFvDispatch.h        |   38 +
> >>  StandaloneMmPkg/Include/StandaloneMm.h             |   36 +
> >>  16 files changed, 5813 insertions(+)
> >>  create mode 100644 StandaloneMmPkg/Core/Dependency.c
> >>  create mode 100644 StandaloneMmPkg/Core/Dispatcher.c
> >>  create mode 100644 StandaloneMmPkg/Core/FwVol.c
> >>  create mode 100644 StandaloneMmPkg/Core/Handle.c
> >>  create mode 100644 StandaloneMmPkg/Core/InstallConfigurationTable.c
> >>  create mode 100644 StandaloneMmPkg/Core/Locate.c
> >>  create mode 100644 StandaloneMmPkg/Core/Mmi.c
> >>  create mode 100644 StandaloneMmPkg/Core/Notify.c
> >>  create mode 100644 StandaloneMmPkg/Core/Page.c
> >>  create mode 100644 StandaloneMmPkg/Core/Pool.c
> >>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.c
> >>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.h
> >>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.inf
> >>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
> >>  create mode 100644 StandaloneMmPkg/Include/Guid/MmFvDispatch.h
> >>  create mode 100644 StandaloneMmPkg/Include/StandaloneMm.h
> >>
> >> diff --git a/StandaloneMmPkg/Core/Dependency.c b/StandaloneMmPkg/Core/Dependency.c
> >> new file mode 100644
> >> index 0000000000..e501369130
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Core/Dependency.c
> >> @@ -0,0 +1,389 @@
> >> +/** @file
> >> +  MM Driver Dispatcher Dependency Evaluator
> >> +
> >> +  This routine evaluates a dependency expression (DEPENDENCY_EXPRESSION) to determine
> >> +  if a driver can be scheduled for execution.  The criteria for
> >> +  schedulability is that the dependency expression is satisfied.
> >> +
> >> +  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
> >> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +  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
> >> +
> >> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +
> >> +**/
> >> +
> >> +#include "StandaloneMmCore.h"
> >> +
> >> +///
> >> +/// EFI_DEP_REPLACE_TRUE - Used to dynamically patch the dependency expression
> >> +///                        to save time.  A EFI_DEP_PUSH is evaluated one an
> >> +///                        replaced with EFI_DEP_REPLACE_TRUE. If PI spec's Vol 2
> >> +///                        Driver Execution Environment Core Interface use 0xff
> >> +///                        as new DEPEX opcode. EFI_DEP_REPLACE_TRUE should be
> >> +///                        defined to a new value that is not conflicting with PI spec.
> >> +///
> >> +#define EFI_DEP_REPLACE_TRUE  0xff
> >> +
> >> +///
> >> +/// Define the initial size of the dependency expression evaluation stack
> >> +///
> >> +#define DEPEX_STACK_SIZE_INCREMENT  0x1000
> >> +
> >> +//
> >> +// Global stack used to evaluate dependency expressions
> >> +//
> >> +BOOLEAN  *mDepexEvaluationStack        = NULL;
> >> +BOOLEAN  *mDepexEvaluationStackEnd     = NULL;
> >> +BOOLEAN  *mDepexEvaluationStackPointer = NULL;
> >> +
> >> +/**
> >> +  Grow size of the Depex stack
> >> +
> >> +  @retval EFI_SUCCESS           Stack successfully growed.
> >> +  @retval EFI_OUT_OF_RESOURCES  There is not enough system memory to grow the stack.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +GrowDepexStack (
> >> +  VOID
> >> +  )
> >> +{
> >> +  BOOLEAN     *NewStack;
> >> +  UINTN       Size;
> >> +
> >> +  Size = DEPEX_STACK_SIZE_INCREMENT;
> >> +  if (mDepexEvaluationStack != NULL) {
> >> +    Size = Size + (mDepexEvaluationStackEnd - mDepexEvaluationStack);
> >> +  }
> >> +
> >> +  NewStack = AllocatePool (Size * sizeof (BOOLEAN));
> >> +  if (NewStack == NULL) {
> >> +    return EFI_OUT_OF_RESOURCES;
> >> +  }
> >> +
> >> +  if (mDepexEvaluationStack != NULL) {
> >> +    //
> >> +    // Copy to Old Stack to the New Stack
> >> +    //
> >> +    CopyMem (
> >> +      NewStack,
> >> +      mDepexEvaluationStack,
> >> +      (mDepexEvaluationStackEnd - mDepexEvaluationStack) * sizeof (BOOLEAN)
> >> +      );
> >> +
> >> +    //
> >> +    // Free The Old Stack
> >> +    //
> >> +    FreePool (mDepexEvaluationStack);
> >> +  }
> >> +
> >> +  //
> >> +  // Make the Stack pointer point to the old data in the new stack
> >> +  //
> >> +  mDepexEvaluationStackPointer = NewStack + (mDepexEvaluationStackPointer - mDepexEvaluationStack);
> >> +  mDepexEvaluationStack        = NewStack;
> >> +  mDepexEvaluationStackEnd     = NewStack + Size;
> >> +
> >> +  return EFI_SUCCESS;
> >> +}
> >> +
> >> +/**
> >> +  Push an element onto the Boolean Stack.
> >> +
> >> +  @param  Value                 BOOLEAN to push.
> >> +
> >> +  @retval EFI_SUCCESS           The value was pushed onto the stack.
> >> +  @retval EFI_OUT_OF_RESOURCES  There is not enough system memory to grow the stack.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +PushBool (
> >> +  IN BOOLEAN  Value
> >> +  )
> >> +{
> >> +  EFI_STATUS  Status;
> >> +
> >> +  //
> >> +  // Check for a stack overflow condition
> >> +  //
> >> +  if (mDepexEvaluationStackPointer == mDepexEvaluationStackEnd) {
> >> +    //
> >> +    // Grow the stack
> >> +    //
> >> +    Status = GrowDepexStack ();
> >> +    if (EFI_ERROR (Status)) {
> >> +      return Status;
> >> +    }
> >> +  }
> >> +
> >> +  //
> >> +  // Push the item onto the stack
> >> +  //
> >> +  *mDepexEvaluationStackPointer = Value;
> >> +  mDepexEvaluationStackPointer++;
> >> +
> >> +  return EFI_SUCCESS;
> >> +}
> >> +
> >> +/**
> >> +  Pop an element from the Boolean stack.
> >> +
> >> +  @param  Value                 BOOLEAN to pop.
> >> +
> >> +  @retval EFI_SUCCESS           The value was popped onto the stack.
> >> +  @retval EFI_ACCESS_DENIED     The pop operation underflowed the stack.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +PopBool (
> >> +  OUT BOOLEAN  *Value
> >> +  )
> >> +{
> >> +  //
> >> +  // Check for a stack underflow condition
> >> +  //
> >> +  if (mDepexEvaluationStackPointer == mDepexEvaluationStack) {
> >> +    return EFI_ACCESS_DENIED;
> >> +  }
> >> +
> >> +  //
> >> +  // Pop the item off the stack
> >> +  //
> >> +  mDepexEvaluationStackPointer--;
> >> +  *Value = *mDepexEvaluationStackPointer;
> >> +  return EFI_SUCCESS;
> >> +}
> >> +
> >> +/**
> >> +  This is the POSTFIX version of the dependency evaluator.  This code does
> >> +  not need to handle Before or After, as it is not valid to call this
> >> +  routine in this case. POSTFIX means all the math is done on top of the stack.
> >> +
> >> +  @param  DriverEntry           DriverEntry element to update.
> >> +
> >> +  @retval TRUE                  If driver is ready to run.
> >> +  @retval FALSE                 If driver is not ready to run or some fatal error
> >> +                                was found.
> >> +
> >> +**/
> >> +BOOLEAN
> >> +MmIsSchedulable (
> >> +  IN  EFI_MM_DRIVER_ENTRY   *DriverEntry
> >> +  )
> >> +{
> >> +  EFI_STATUS  Status;
> >> +  UINT8       *Iterator;
> >> +  BOOLEAN     Operator;
> >> +  BOOLEAN     Operator2;
> >> +  EFI_GUID    DriverGuid;
> >> +  VOID        *Interface;
> >> +
> >> +  Operator = FALSE;
> >> +  Operator2 = FALSE;
> >> +
> >> +  if (DriverEntry->After || DriverEntry->Before) {
> >> +    //
> >> +    // If Before or After Depex skip as MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
> >> +    // processes them.
> >> +    //
> >> +    return FALSE;
> >> +  }
> >> +
> >> +  DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
> >> +
> >> +  if (DriverEntry->Depex == NULL) {
> >> +    //
> >> +    // A NULL Depex means that the MM driver is not built correctly.
> >> +    // All MM drivers must have a valid depex expressiion.
> >> +    //
> >> +    DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Depex is empty)\n"));
> >> +    ASSERT (FALSE);
> >> +    return FALSE;
> >> +  }
> >> +
> >> +  //
> >> +  // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
> >> +  // incorrectly formed DEPEX expressions
> >> +  //
> >> +  mDepexEvaluationStackPointer = mDepexEvaluationStack;
> >> +
> >> +
> >> +  Iterator = DriverEntry->Depex;
> >> +
> >> +  while (TRUE) {
> >> +    //
> >> +    // Check to see if we are attempting to fetch dependency expression instructions
> >> +    // past the end of the dependency expression.
> >> +    //
> >> +    if (((UINTN)Iterator - (UINTN)DriverEntry->Depex) >= DriverEntry->DepexSize) {
> >> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Attempt to fetch past end of depex)\n"));
> >> +      return FALSE;
> >> +    }
> >> +
> >> +    //
> >> +    // Look at the opcode of the dependency expression instruction.
> >> +    //
> >> +    switch (*Iterator) {
> >> +    case EFI_DEP_BEFORE:
> >> +    case EFI_DEP_AFTER:
> >> +      //
> >> +      // For a well-formed Dependency Expression, the code should never get here.
> >> +      // The BEFORE and AFTER are processed prior to this routine's invocation.
> >> +      // If the code flow arrives at this point, there was a BEFORE or AFTER
> >> +      // that were not the first opcodes.
> >> +      //
> >> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));
> >> +      ASSERT (FALSE);
> >> +
> >> +    case EFI_DEP_PUSH:
> >> +      //
> >> +      // Push operator is followed by a GUID. Test to see if the GUID protocol
> >> +      // is installed and push the boolean result on the stack.
> >> +      //
> >> +      CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));
> >> +
> >> +      Status = MmLocateProtocol (&DriverGuid, NULL, &Interface);
> >> +      if (EFI_ERROR (Status) && (mEfiSystemTable != NULL)) {
> >> +        //
> >> +        // For MM Driver, it may depend on uefi protocols
> >> +        //
> >> +        Status = mEfiSystemTable->BootServices->LocateProtocol (&DriverGuid, NULL, &Interface);
> >> +      }
> >> +
> >> +      if (EFI_ERROR (Status)) {
> >> +        DEBUG ((DEBUG_DISPATCH, "  PUSH GUID(%g) = FALSE\n", &DriverGuid));
> >> +        Status = PushBool (FALSE);
> >> +      } else {
> >> +        DEBUG ((DEBUG_DISPATCH, "  PUSH GUID(%g) = TRUE\n", &DriverGuid));
> >> +        *Iterator = EFI_DEP_REPLACE_TRUE;
> >> +        Status = PushBool (TRUE);
> >> +      }
> >> +      if (EFI_ERROR (Status)) {
> >> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> >> +        return FALSE;
> >> +      }
> >> +
> >> +      Iterator += sizeof (EFI_GUID);
> >> +      break;
> >> +
> >> +    case EFI_DEP_AND:
> >> +      DEBUG ((DEBUG_DISPATCH, "  AND\n"));
> >> +      Status = PopBool (&Operator);
> >> +      if (EFI_ERROR (Status)) {
> >> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> >> +        return FALSE;
> >> +      }
> >> +
> >> +      Status = PopBool (&Operator2);
> >> +      if (EFI_ERROR (Status)) {
> >> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> >> +        return FALSE;
> >> +      }
> >> +
> >> +      Status = PushBool ((BOOLEAN)(Operator && Operator2));
> >> +      if (EFI_ERROR (Status)) {
> >> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> >> +        return FALSE;
> >> +      }
> >> +      break;
> >> +
> >> +    case EFI_DEP_OR:
> >> +      DEBUG ((DEBUG_DISPATCH, "  OR\n"));
> >> +      Status = PopBool (&Operator);
> >> +      if (EFI_ERROR (Status)) {
> >> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> >> +        return FALSE;
> >> +      }
> >> +
> >> +      Status = PopBool (&Operator2);
> >> +      if (EFI_ERROR (Status)) {
> >> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> >> +        return FALSE;
> >> +      }
> >> +
> >> +      Status = PushBool ((BOOLEAN)(Operator || Operator2));
> >> +      if (EFI_ERROR (Status)) {
> >> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> >> +        return FALSE;
> >> +      }
> >> +      break;
> >> +
> >> +    case EFI_DEP_NOT:
> >> +      DEBUG ((DEBUG_DISPATCH, "  NOT\n"));
> >> +      Status = PopBool (&Operator);
> >> +      if (EFI_ERROR (Status)) {
> >> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> >> +        return FALSE;
> >> +      }
> >> +
> >> +      Status = PushBool ((BOOLEAN)(!Operator));
> >> +      if (EFI_ERROR (Status)) {
> >> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> >> +        return FALSE;
> >> +      }
> >> +      break;
> >> +
> >> +    case EFI_DEP_TRUE:
> >> +      DEBUG ((DEBUG_DISPATCH, "  TRUE\n"));
> >> +      Status = PushBool (TRUE);
> >> +      if (EFI_ERROR (Status)) {
> >> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> >> +        return FALSE;
> >> +      }
> >> +      break;
> >> +
> >> +    case EFI_DEP_FALSE:
> >> +      DEBUG ((DEBUG_DISPATCH, "  FALSE\n"));
> >> +      Status = PushBool (FALSE);
> >> +      if (EFI_ERROR (Status)) {
> >> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> >> +        return FALSE;
> >> +      }
> >> +      break;
> >> +
> >> +    case EFI_DEP_END:
> >> +      DEBUG ((DEBUG_DISPATCH, "  END\n"));
> >> +      Status = PopBool (&Operator);
> >> +      if (EFI_ERROR (Status)) {
> >> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> >> +        return FALSE;
> >> +      }
> >> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = %a\n", Operator ? "TRUE" : "FALSE"));
> >> +      return Operator;
> >> +
> >> +    case EFI_DEP_REPLACE_TRUE:
> >> +      CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));
> >> +      DEBUG ((DEBUG_DISPATCH, "  PUSH GUID(%g) = TRUE\n", &DriverGuid));
> >> +      Status = PushBool (TRUE);
> >> +      if (EFI_ERROR (Status)) {
> >> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> >> +        return FALSE;
> >> +      }
> >> +
> >> +      Iterator += sizeof (EFI_GUID);
> >> +      break;
> >> +
> >> +    default:
> >> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unknown opcode)\n"));
> >> +      goto Done;
> >> +    }
> >> +
> >> +    //
> >> +    // Skip over the Dependency Op Code we just processed in the switch.
> >> +    // The math is done out of order, but it should not matter. That is
> >> +    // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
> >> +    // This is not an issue, since we just need the correct end result. You
> >> +    // need to be careful using Iterator in the loop as it's intermediate value
> >> +    // may be strange.
> >> +    //
> >> +    Iterator++;
> >> +  }
> >> +
> >> +Done:
> >> +  return FALSE;
> >> +}
> >> diff --git a/StandaloneMmPkg/Core/Dispatcher.c b/StandaloneMmPkg/Core/Dispatcher.c
> >> new file mode 100644
> >> index 0000000000..af18fa7eaa
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Core/Dispatcher.c
> >> @@ -0,0 +1,1071 @@
> >> +/** @file
> >> +  MM Driver Dispatcher.
> >> +
> >> +  Step #1 - When a FV protocol is added to the system every driver in the FV
> >> +            is added to the mDiscoveredList. The Before, and After Depex are
> >> +            pre-processed as drivers are added to the mDiscoveredList. If an Apriori
> >> +            file exists in the FV those drivers are addeded to the
> >> +            mScheduledQueue. The mFvHandleList is used to make sure a
> >> +            FV is only processed once.
> >> +
> >> +  Step #2 - Dispatch. Remove driver from the mScheduledQueue and load and
> >> +            start it. After mScheduledQueue is drained check the
> >> +            mDiscoveredList to see if any item has a Depex that is ready to
> >> +            be placed on the mScheduledQueue.
> >> +
> >> +  Step #3 - Adding to the mScheduledQueue requires that you process Before
> >> +            and After dependencies. This is done recursively as the call to add
> >> +            to the mScheduledQueue checks for Before and recursively adds
> >> +            all Befores. It then addes the item that was passed in and then
> >> +            processess the After dependecies by recursively calling the routine.
> >> +
> >> +  Dispatcher Rules:
> >> +  The rules for the dispatcher are similar to the DXE dispatcher.
> >> +
> >> +  The rules for DXE dispatcher are in chapter 10 of the DXE CIS. Figure 10-3
> >> +  is the state diagram for the DXE dispatcher
> >> +
> >> +  Depex - Dependency Expresion.
> >> +
> >> +  Copyright (c) 2014, Hewlett-Packard Development Company, L.P.
> >> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> >> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +
> >> +  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
> >> +
> >> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +
> >> +**/
> >> +
> >> +#include "StandaloneMmCore.h"
> >> +
> >> +//
> >> +// MM Dispatcher Data structures
> >> +//
> >> +#define KNOWN_HANDLE_SIGNATURE  SIGNATURE_32('k','n','o','w')
> >> +typedef struct {
> >> +  UINTN           Signature;
> >> +  LIST_ENTRY      Link;         // mFvHandleList
> >> +  EFI_HANDLE      Handle;
> >> +} KNOWN_HANDLE;
> >> +
> >> +//
> >> +// Function Prototypes
> >> +//
> >> +
> >> +EFI_STATUS
> >> +MmCoreFfsFindMmDriver (
> >> +  IN  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader
> >> +  );
> >> +
> >> +/**
> >> +  Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
> >> +  must add any driver with a before dependency on InsertedDriverEntry first.
> >> +  You do this by recursively calling this routine. After all the Befores are
> >> +  processed you can add InsertedDriverEntry to the mScheduledQueue.
> >> +  Then you can add any driver with an After dependency on InsertedDriverEntry
> >> +  by recursively calling this routine.
> >> +
> >> +  @param  InsertedDriverEntry   The driver to insert on the ScheduledLink Queue
> >> +
> >> +**/
> >> +VOID
> >> +MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
> >> +  IN  EFI_MM_DRIVER_ENTRY   *InsertedDriverEntry
> >> +  );
> >> +
> >> +//
> >> +// The Driver List contains one copy of every driver that has been discovered.
> >> +// Items are never removed from the driver list. List of EFI_MM_DRIVER_ENTRY
> >> +//
> >> +LIST_ENTRY  mDiscoveredList = INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList);
> >> +
> >> +//
> >> +// Queue of drivers that are ready to dispatch. This queue is a subset of the
> >> +// mDiscoveredList.list of EFI_MM_DRIVER_ENTRY.
> >> +//
> >> +LIST_ENTRY  mScheduledQueue = INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue);
> >> +
> >> +//
> >> +// List of handles who's Fv's have been parsed and added to the mFwDriverList.
> >> +//
> >> +LIST_ENTRY  mFvHandleList = INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList);
> >> +
> >> +//
> >> +// Flag for the MM Dispacher.  TRUE if dispatcher is execuing.
> >> +//
> >> +BOOLEAN  gDispatcherRunning = FALSE;
> >> +
> >> +//
> >> +// Flag for the MM Dispacher.  TRUE if there is one or more MM drivers ready to be dispatched
> >> +//
> >> +BOOLEAN  gRequestDispatch = FALSE;
> >> +
> >> +//
> >> +// The global variable is defined for Loading modules at fixed address feature to track the MM code
> >> +// memory range usage. It is a bit mapped array in which every bit indicates the correspoding
> >> +// memory page available or not.
> >> +//
> >> +GLOBAL_REMOVE_IF_UNREFERENCED    UINT64                *mMmCodeMemoryRangeUsageBitMap=NULL;
> >> +
> >> +/**
> >> +  To check memory usage bit map array to figure out if the memory range in which the image will be loaded is available or not. If
> >> +  memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.
> >> +  The function is only invoked when load modules at fixed address feature is enabled.
> >> +
> >> +  @param  ImageBase                The base addres the image will be loaded at.
> >> +  @param  ImageSize                The size of the image
> >> +
> >> +  @retval EFI_SUCCESS              The memory range the image will be loaded in is available
> >> +  @retval EFI_NOT_FOUND            The memory range the image will be loaded in is not available
> >> +**/
> >> +EFI_STATUS
> >> +CheckAndMarkFixLoadingMemoryUsageBitMap (
> >> +  IN  EFI_PHYSICAL_ADDRESS          ImageBase,
> >> +  IN  UINTN                         ImageSize
> >> +  )
> >> +{
> >> +   UINT32                             MmCodePageNumber;
> >> +   UINT64                             MmCodeSize;
> >> +   EFI_PHYSICAL_ADDRESS               MmCodeBase;
> >> +   UINTN                              BaseOffsetPageNumber;
> >> +   UINTN                              TopOffsetPageNumber;
> >> +   UINTN                              Index;
> >> +   //
> >> +   // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressMmCodePageNumber
> >> +   //
> >> +   MmCodePageNumber = 0;
> >> +   MmCodeSize = EFI_PAGES_TO_SIZE (MmCodePageNumber);
> >> +   MmCodeBase = gLoadModuleAtFixAddressMmramBase;
> >> +
> >> +   //
> >> +   // If the memory usage bit map is not initialized,  do it. Every bit in the array
> >> +   // indicate the status of the corresponding memory page, available or not
> >> +   //
> >> +   if (mMmCodeMemoryRangeUsageBitMap == NULL) {
> >> +     mMmCodeMemoryRangeUsageBitMap = AllocateZeroPool(((MmCodePageNumber / 64) + 1)*sizeof(UINT64));
> >> +   }
> >> +   //
> >> +   // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
> >> +   //
> >> +   if (mMmCodeMemoryRangeUsageBitMap == NULL) {
> >> +     return EFI_NOT_FOUND;
> >> +   }
> >> +   //
> >> +   // see if the memory range for loading the image is in the MM code range.
> >> +   //
> >> +   if (MmCodeBase + MmCodeSize <  ImageBase + ImageSize || MmCodeBase >  ImageBase) {
> >> +     return EFI_NOT_FOUND;
> >> +   }
> >> +   //
> >> +   // Test if the memory is avalaible or not.
> >> +   //
> >> +   BaseOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase - MmCodeBase));
> >> +   TopOffsetPageNumber  = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - MmCodeBase));
> >> +   for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
> >> +     if ((mMmCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {
> >> +       //
> >> +       // This page is already used.
> >> +       //
> >> +       return EFI_NOT_FOUND;
> >> +     }
> >> +   }
> >> +
> >> +   //
> >> +   // Being here means the memory range is available.  So mark the bits for the memory range
> >> +   //
> >> +   for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
> >> +     mMmCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));
> >> +   }
> >> +   return  EFI_SUCCESS;
> >> +}
> >> +/**
> >> +  Get the fixed loading address from image header assigned by build tool. This function only be called
> >> +  when Loading module at Fixed address feature enabled.
> >> +
> >> +  @param  ImageContext              Pointer to the image context structure that describes the PE/COFF
> >> +                                    image that needs to be examined by this function.
> >> +  @retval EFI_SUCCESS               An fixed loading address is assigned to this image by build tools .
> >> +  @retval EFI_NOT_FOUND             The image has no assigned fixed loadding address.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +GetPeCoffImageFixLoadingAssignedAddress(
> >> +  IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext
> >> +  )
> >> +{
> >> +  UINTN                              SectionHeaderOffset;
> >> +  EFI_STATUS                         Status;
> >> +  EFI_IMAGE_SECTION_HEADER           SectionHeader;
> >> +  EFI_IMAGE_OPTIONAL_HEADER_UNION    *ImgHdr;
> >> +  EFI_PHYSICAL_ADDRESS               FixLoadingAddress;
> >> +  UINT16                             Index;
> >> +  UINTN                              Size;
> >> +  UINT16                             NumberOfSections;
> >> +  UINT64                             ValueInSectionHeader;
> >> +
> >> +  FixLoadingAddress = 0;
> >> +  Status = EFI_NOT_FOUND;
> >> +
> >> +  //
> >> +  // Get PeHeader pointer
> >> +  //
> >> +  ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
> >> +  SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +
> >> +                        sizeof (UINT32) +
> >> +                        sizeof (EFI_IMAGE_FILE_HEADER) +
> >> +                        ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;
> >> +  NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
> >> +
> >> +  //
> >> +  // Get base address from the first section header that doesn't point to code section.
> >> +  //
> >> +  for (Index = 0; Index < NumberOfSections; Index++) {
> >> +    //
> >> +    // Read section header from file
> >> +    //
> >> +    Size = sizeof (EFI_IMAGE_SECTION_HEADER);
> >> +    Status = ImageContext->ImageRead (
> >> +                              ImageContext->Handle,
> >> +                              SectionHeaderOffset,
> >> +                              &Size,
> >> +                              &SectionHeader
> >> +                              );
> >> +    if (EFI_ERROR (Status)) {
> >> +      return Status;
> >> +    }
> >> +
> >> +    Status = EFI_NOT_FOUND;
> >> +
> >> +    if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
> >> +      //
> >> +      // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
> >> +      // that doesn't point to code section in image header.So there is an assumption that when the feature is enabled,
> >> +      // if a module with a loading address assigned by tools, the PointerToRelocations & PointerToLineNumbers fields
> >> +      // should not be Zero, or else, these 2 fields should be set to Zero
> >> +      //
> >> +      ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);
> >> +      if (ValueInSectionHeader != 0) {
> >> +        //
> >> +        // Found first section header that doesn't point to code section in which build tool saves the
> >> +        // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
> >> +        //
> >> +        FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressMmramBase + (INT64)ValueInSectionHeader);
> >> +        //
> >> +        // Check if the memory range is available.
> >> +        //
> >> +        Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));
> >> +        if (!EFI_ERROR(Status)) {
> >> +          //
> >> +          // The assigned address is valid. Return the specified loading address
> >> +          //
> >> +          ImageContext->ImageAddress = FixLoadingAddress;
> >> +        }
> >> +      }
> >> +      break;
> >> +    }
> >> +    SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
> >> +  }
> >> +  DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n", FixLoadingAddress, Status));
> >> +  return Status;
> >> +}
> >> +/**
> >> +  Loads an EFI image into SMRAM.
> >> +
> >> +  @param  DriverEntry             EFI_MM_DRIVER_ENTRY instance
> >> +
> >> +  @return EFI_STATUS
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmLoadImage (
> >> +  IN OUT EFI_MM_DRIVER_ENTRY  *DriverEntry
> >> +  )
> >> +{
> >> +  VOID                           *Buffer;
> >> +  UINTN                          PageCount;
> >> +  EFI_STATUS                     Status;
> >> +  EFI_PHYSICAL_ADDRESS           DstBuffer;
> >> +  PE_COFF_LOADER_IMAGE_CONTEXT   ImageContext;
> >> +
> >> +  DEBUG ((DEBUG_INFO, "MmLoadImage - %g\n", &DriverEntry->FileName));
> >> +
> >> +  Buffer = AllocateCopyPool (DriverEntry->Pe32DataSize, DriverEntry->Pe32Data);
> >> +  if (Buffer == NULL) {
> >> +    return EFI_OUT_OF_RESOURCES;
> >> +  }
> >> +
> >> +  Status               = EFI_SUCCESS;
> >> +
> >> +  //
> >> +  // Initialize ImageContext
> >> +  //
> >> +  ImageContext.Handle = Buffer;
> >> +  ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
> >> +
> >> +  //
> >> +  // Get information about the image being loaded
> >> +  //
> >> +  Status = PeCoffLoaderGetImageInfo (&ImageContext);
> >> +  if (EFI_ERROR (Status)) {
> >> +    if (Buffer != NULL) {
> >> +      MmFreePool (Buffer);
> >> +    }
> >> +    return Status;
> >> +  }
> >> +
> >> +  PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
> >> +  DstBuffer = (UINTN)(-1);
> >> +
> >> +  Status = MmAllocatePages (
> >> +              AllocateMaxAddress,
> >> +              EfiRuntimeServicesCode,
> >> +              PageCount,
> >> +              &DstBuffer
> >> +              );
> >> +  if (EFI_ERROR (Status)) {
> >> +    if (Buffer != NULL) {
> >> +      MmFreePool (Buffer);
> >> +    }
> >> +    return Status;
> >> +  }
> >> +
> >> +  ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;
> >> +
> >> +  //
> >> +  // Align buffer on section boundry
> >> +  //
> >> +  ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
> >> +  ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)(ImageContext.SectionAlignment - 1));
> >> +
> >> +  //
> >> +  // Load the image to our new buffer
> >> +  //
> >> +  Status = PeCoffLoaderLoadImage (&ImageContext);
> >> +  if (EFI_ERROR (Status)) {
> >> +    if (Buffer != NULL) {
> >> +      MmFreePool (Buffer);
> >> +    }
> >> +    MmFreePages (DstBuffer, PageCount);
> >> +    return Status;
> >> +  }
> >> +
> >> +  //
> >> +  // Relocate the image in our new buffer
> >> +  //
> >> +  Status = PeCoffLoaderRelocateImage (&ImageContext);
> >> +  if (EFI_ERROR (Status)) {
> >> +    if (Buffer != NULL) {
> >> +      MmFreePool (Buffer);
> >> +    }
> >> +    MmFreePages (DstBuffer, PageCount);
> >> +    return Status;
> >> +  }
> >> +
> >> +  //
> >> +  // Flush the instruction cache so the image data are written before we execute it
> >> +  //
> >> +  InvalidateInstructionCacheRange ((VOID *)(UINTN) ImageContext.ImageAddress, (UINTN) ImageContext.ImageSize);
> >> +
> >> +  //
> >> +  // Save Image EntryPoint in DriverEntry
> >> +  //
> >> +  DriverEntry->ImageEntryPoint  = ImageContext.EntryPoint;
> >> +  DriverEntry->ImageBuffer      = DstBuffer;
> >> +  DriverEntry->NumberOfPage     = PageCount;
> >> +
> >> +  if (mEfiSystemTable != NULL) {
> >> +    Status = mEfiSystemTable->BootServices->AllocatePool (EfiBootServicesData, sizeof (EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&DriverEntry->LoadedImage);
> >> +    if (EFI_ERROR (Status)) {
> >> +      if (Buffer != NULL) {
> >> +        MmFreePool (Buffer);
> >> +      }
> >> +      MmFreePages (DstBuffer, PageCount);
> >> +      return Status;
> >> +    }
> >> +
> >> +    ZeroMem (DriverEntry->LoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));
> >> +    //
> >> +    // Fill in the remaining fields of the Loaded Image Protocol instance.
> >> +    // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
> >> +    //
> >> +    DriverEntry->LoadedImage->Revision      = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
> >> +    DriverEntry->LoadedImage->ParentHandle  = NULL;
> >> +    DriverEntry->LoadedImage->SystemTable   = mEfiSystemTable;
> >> +    DriverEntry->LoadedImage->DeviceHandle  = NULL;
> >> +    DriverEntry->LoadedImage->FilePath      = NULL;
> >> +
> >> +    DriverEntry->LoadedImage->ImageBase     = (VOID *)(UINTN)DriverEntry->ImageBuffer;
> >> +    DriverEntry->LoadedImage->ImageSize     = ImageContext.ImageSize;
> >> +    DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;
> >> +    DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;
> >> +
> >> +    //
> >> +    // Create a new image handle in the UEFI handle database for the MM Driver
> >> +    //
> >> +    DriverEntry->ImageHandle = NULL;
> >> +    Status = mEfiSystemTable->BootServices->InstallMultipleProtocolInterfaces (
> >> +                    &DriverEntry->ImageHandle,
> >> +                    &gEfiLoadedImageProtocolGuid, DriverEntry->LoadedImage,
> >> +                    NULL
> >> +                    );
> >> +  }
> >> +
> >> +  //
> >> +  // Print the load address and the PDB file name if it is available
> >> +  //
> >> +
> >> +  DEBUG_CODE_BEGIN ();
> >> +
> >> +    UINTN Index;
> >> +    UINTN StartIndex;
> >> +    CHAR8 EfiFileName[256];
> >> +
> >> +
> >> +    DEBUG ((DEBUG_INFO | DEBUG_LOAD,
> >> +           "Loading MM driver at 0x%11p EntryPoint=0x%11p ",
> >> +           (VOID *)(UINTN) ImageContext.ImageAddress,
> >> +           FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));
> >> +
> >> +
> >> +    //
> >> +    // Print Module Name by Pdb file path.
> >> +    // Windows and Unix style file path are all trimmed correctly.
> >> +    //
> >> +    if (ImageContext.PdbPointer != NULL) {
> >> +      StartIndex = 0;
> >> +      for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {
> >> +        if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {
> >> +          StartIndex = Index + 1;
> >> +        }
> >> +      }
> >> +      //
> >> +      // Copy the PDB file name to our temporary string, and replace .pdb with .efi
> >> +      // The PDB file name is limited in the range of 0~255.
> >> +      // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
> >> +      //
> >> +      for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {
> >> +        EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];
> >> +        if (EfiFileName[Index] == 0) {
> >> +          EfiFileName[Index] = '.';
> >> +        }
> >> +        if (EfiFileName[Index] == '.') {
> >> +          EfiFileName[Index + 1] = 'e';
> >> +          EfiFileName[Index + 2] = 'f';
> >> +          EfiFileName[Index + 3] = 'i';
> >> +          EfiFileName[Index + 4] = 0;
> >> +          break;
> >> +        }
> >> +      }
> >> +
> >> +      if (Index == sizeof (EfiFileName) - 4) {
> >> +        EfiFileName[Index] = 0;
> >> +      }
> >> +      DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));
> >> +    }
> >> +    DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));
> >> +
> >> +  DEBUG_CODE_END ();
> >> +
> >> +  //
> >> +  // Free buffer allocated by Fv->ReadSection.
> >> +  //
> >> +  // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
> >> +  // used the UEFI Boot Services AllocatePool() function
> >> +  //
> >> +  MmFreePool(Buffer);
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Preprocess dependency expression and update DriverEntry to reflect the
> >> +  state of  Before and After dependencies. If DriverEntry->Before
> >> +  or DriverEntry->After is set it will never be cleared.
> >> +
> >> +  @param  DriverEntry           DriverEntry element to update .
> >> +
> >> +  @retval EFI_SUCCESS           It always works.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +MmPreProcessDepex (
> >> +  IN EFI_MM_DRIVER_ENTRY  *DriverEntry
> >> +  )
> >> +{
> >> +  UINT8  *Iterator;
> >> +
> >> +  Iterator = DriverEntry->Depex;
> >> +  DriverEntry->Dependent = TRUE;
> >> +
> >> +  if (*Iterator == EFI_DEP_BEFORE) {
> >> +    DriverEntry->Before = TRUE;
> >> +  } else if (*Iterator == EFI_DEP_AFTER) {
> >> +    DriverEntry->After = TRUE;
> >> +  }
> >> +
> >> +  if (DriverEntry->Before || DriverEntry->After) {
> >> +    CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));
> >> +  }
> >> +
> >> +  return EFI_SUCCESS;
> >> +}
> >> +
> >> +/**
> >> +  Read Depex and pre-process the Depex for Before and After. If Section Extraction
> >> +  protocol returns an error via ReadSection defer the reading of the Depex.
> >> +
> >> +  @param  DriverEntry           Driver to work on.
> >> +
> >> +  @retval EFI_SUCCESS           Depex read and preprossesed
> >> +  @retval EFI_PROTOCOL_ERROR    The section extraction protocol returned an error
> >> +                                and  Depex reading needs to be retried.
> >> +  @retval Error                 DEPEX not found.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +MmGetDepexSectionAndPreProccess (
> >> +  IN EFI_MM_DRIVER_ENTRY  *DriverEntry
> >> +  )
> >> +{
> >> +  EFI_STATUS                     Status;
> >> +
> >> +  //
> >> +  // Data already read
> >> +  //
> >> +  if (DriverEntry->Depex == NULL) {
> >> +    Status = EFI_NOT_FOUND;
> >> +  } else {
> >> +    Status = EFI_SUCCESS;
> >> +  }
> >> +  if (EFI_ERROR (Status)) {
> >> +    if (Status == EFI_PROTOCOL_ERROR) {
> >> +      //
> >> +      // The section extraction protocol failed so set protocol error flag
> >> +      //
> >> +      DriverEntry->DepexProtocolError = TRUE;
> >> +    } else {
> >> +      //
> >> +      // If no Depex assume depend on all architectural protocols
> >> +      //
> >> +      DriverEntry->Depex = NULL;
> >> +      DriverEntry->Dependent = TRUE;
> >> +      DriverEntry->DepexProtocolError = FALSE;
> >> +    }
> >> +  } else {
> >> +    //
> >> +    // Set Before and After state information based on Depex
> >> +    // Driver will be put in Dependent state
> >> +    //
> >> +    MmPreProcessDepex (DriverEntry);
> >> +    DriverEntry->DepexProtocolError = FALSE;
> >> +  }
> >> +
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  This is the main Dispatcher for MM and it exits when there are no more
> >> +  drivers to run. Drain the mScheduledQueue and load and start a PE
> >> +  image for each driver. Search the mDiscoveredList to see if any driver can
> >> +  be placed on the mScheduledQueue. If no drivers are placed on the
> >> +  mScheduledQueue exit the function.
> >> +
> >> +  @retval EFI_SUCCESS           All of the MM Drivers that could be dispatched
> >> +                                have been run and the MM Entry Point has been
> >> +                                registered.
> >> +  @retval EFI_NOT_READY         The MM Driver that registered the MM Entry Point
> >> +                                was just dispatched.
> >> +  @retval EFI_NOT_FOUND         There are no MM Drivers available to be dispatched.
> >> +  @retval EFI_ALREADY_STARTED   The MM Dispatcher is already running
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +MmDispatcher (
> >> +  VOID
> >> +  )
> >> +{
> >> +  EFI_STATUS            Status;
> >> +  LIST_ENTRY            *Link;
> >> +  EFI_MM_DRIVER_ENTRY  *DriverEntry;
> >> +  BOOLEAN               ReadyToRun;
> >> +  BOOLEAN               PreviousMmEntryPointRegistered;
> >> +
> >> +  DEBUG ((DEBUG_INFO, "MmDispatcher\n"));
> >> +
> >> +  if (!gRequestDispatch) {
> >> +    DEBUG ((DEBUG_INFO, "  !gRequestDispatch\n"));
> >> +    return EFI_NOT_FOUND;
> >> +  }
> >> +
> >> +  if (gDispatcherRunning) {
> >> +    DEBUG ((DEBUG_INFO, "  gDispatcherRunning\n"));
> >> +    //
> >> +    // If the dispatcher is running don't let it be restarted.
> >> +    //
> >> +    return EFI_ALREADY_STARTED;
> >> +  }
> >> +
> >> +  gDispatcherRunning = TRUE;
> >> +
> >> +  do {
> >> +    //
> >> +    // Drain the Scheduled Queue
> >> +    //
> >> +    DEBUG ((DEBUG_INFO, "  Drain the Scheduled Queue\n"));
> >> +    while (!IsListEmpty (&mScheduledQueue)) {
> >> +      DriverEntry = CR (
> >> +                      mScheduledQueue.ForwardLink,
> >> +                      EFI_MM_DRIVER_ENTRY,
> >> +                      ScheduledLink,
> >> +                      EFI_MM_DRIVER_ENTRY_SIGNATURE
> >> +                      );
> >> +      DEBUG ((DEBUG_INFO, "  DriverEntry (Scheduled) - %g\n", &DriverEntry->FileName));
> >> +
> >> +      //
> >> +      // Load the MM Driver image into memory. If the Driver was transitioned from
> >> +      // Untrused to Scheduled it would have already been loaded so we may need to
> >> +      // skip the LoadImage
> >> +      //
> >> +      if (DriverEntry->ImageHandle == NULL) {
> >> +        Status = MmLoadImage (DriverEntry);
> >> +
> >> +        //
> >> +        // Update the driver state to reflect that it's been loaded
> >> +        //
> >> +        if (EFI_ERROR (Status)) {
> >> +          //
> >> +          // The MM Driver could not be loaded, and do not attempt to load or start it again.
> >> +          // Take driver from Scheduled to Initialized.
> >> +          //
> >> +          DriverEntry->Initialized  = TRUE;
> >> +          DriverEntry->Scheduled = FALSE;
> >> +          RemoveEntryList (&DriverEntry->ScheduledLink);
> >> +
> >> +          //
> >> +          // If it's an error don't try the StartImage
> >> +          //
> >> +          continue;
> >> +        }
> >> +      }
> >> +
> >> +      DriverEntry->Scheduled    = FALSE;
> >> +      DriverEntry->Initialized  = TRUE;
> >> +      RemoveEntryList (&DriverEntry->ScheduledLink);
> >> +
> >> +      /*REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
> >> +        EFI_PROGRESS_CODE,
> >> +        EFI_SOFTWARE_MM_DRIVER | EFI_SW_PC_INIT_BEGIN,
> >> +        &DriverEntry->ImageHandle,
> >> +        sizeof (DriverEntry->ImageHandle)
> >> +        );*/
> >> +
> >> +      //
> >> +      // Cache state of MmEntryPointRegistered before calling entry point
> >> +      //
> >> +      PreviousMmEntryPointRegistered = gMmCorePrivate->MmEntryPointRegistered;
> >> +
> >> +      //
> >> +      // For each MM driver, pass NULL as ImageHandle
> >> +      //
> >> +      if (mEfiSystemTable == NULL) {
> >> +        DEBUG ((DEBUG_INFO, "StartImage - 0x%x (Standalone Mode)\n", DriverEntry->ImageEntryPoint));
> >> +        Status = ((MM_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, &gMmCoreMmst);
> >> +      } else {
> >> +        DEBUG ((DEBUG_INFO, "StartImage - 0x%x (Tradition Mode)\n", DriverEntry->ImageEntryPoint));
> >> +        Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, mEfiSystemTable);
> >> +      }
> >> +      if (EFI_ERROR(Status)){
> >> +        DEBUG ((DEBUG_INFO, "StartImage Status - %r\n", Status));
> >> +        MmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);
> >> +      }
> >> +
> >> +      /*REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
> >> +        EFI_PROGRESS_CODE,
> >> +        EFI_SOFTWARE_MM_DRIVER | EFI_SW_PC_INIT_END,
> >> +        &DriverEntry->ImageHandle,
> >> +        sizeof (DriverEntry->ImageHandle)
> >> +        );*/
> >> +
> >> +      if (!PreviousMmEntryPointRegistered && gMmCorePrivate->MmEntryPointRegistered) {
> >> +        //
> >> +        // Return immediately if the MM Entry Point was registered by the MM
> >> +        // Driver that was just dispatched.  The MM IPL will reinvoke the MM
> >> +        // Core Dispatcher.  This is required so MM Mode may be enabled as soon
> >> +        // as all the dependent MM Drivers for MM Mode have been dispatched.
> >> +        // Once the MM Entry Point has been registered, then MM Mode will be
> >> +        // used.
> >> +        //
> >> +        gRequestDispatch = TRUE;
> >> +        gDispatcherRunning = FALSE;
> >> +        return EFI_NOT_READY;
> >> +      }
> >> +    }
> >> +
> >> +    //
> >> +    // Search DriverList for items to place on Scheduled Queue
> >> +    //
> >> +    DEBUG ((DEBUG_INFO, "  Search DriverList for items to place on Scheduled Queue\n"));
> >> +    ReadyToRun = FALSE;
> >> +    for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
> >> +      DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
> >> +      DEBUG ((DEBUG_INFO, "  DriverEntry (Discovered) - %g\n", &DriverEntry->FileName));
> >> +
> >> +      if (DriverEntry->DepexProtocolError){
> >> +        //
> >> +        // If Section Extraction Protocol did not let the Depex be read before retry the read
> >> +        //
> >> +        Status = MmGetDepexSectionAndPreProccess (DriverEntry);
> >> +      }
> >> +
> >> +      if (DriverEntry->Dependent) {
> >> +        if (MmIsSchedulable (DriverEntry)) {
> >> +          MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
> >> +          ReadyToRun = TRUE;
> >> +        }
> >> +      }
> >> +    }
> >> +  } while (ReadyToRun);
> >> +
> >> +  //
> >> +  // If there is no more MM driver to dispatch, stop the dispatch request
> >> +  //
> >> +  DEBUG ((DEBUG_INFO, "  no more MM driver to dispatch, stop the dispatch request\n"));
> >> +  gRequestDispatch = FALSE;
> >> +  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
> >> +    DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
> >> +    DEBUG ((DEBUG_INFO, "  DriverEntry (Discovered) - %g\n", &DriverEntry->FileName));
> >> +
> >> +    if (!DriverEntry->Initialized){
> >> +      //
> >> +      // We have MM driver pending to dispatch
> >> +      //
> >> +      gRequestDispatch = TRUE;
> >> +      break;
> >> +    }
> >> +  }
> >> +
> >> +  gDispatcherRunning = FALSE;
> >> +
> >> +  return EFI_SUCCESS;
> >> +}
> >> +
> >> +/**
> >> +  Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
> >> +  must add any driver with a before dependency on InsertedDriverEntry first.
> >> +  You do this by recursively calling this routine. After all the Befores are
> >> +  processed you can add InsertedDriverEntry to the mScheduledQueue.
> >> +  Then you can add any driver with an After dependency on InsertedDriverEntry
> >> +  by recursively calling this routine.
> >> +
> >> +  @param  InsertedDriverEntry   The driver to insert on the ScheduledLink Queue
> >> +
> >> +**/
> >> +VOID
> >> +MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
> >> +  IN  EFI_MM_DRIVER_ENTRY   *InsertedDriverEntry
> >> +  )
> >> +{
> >> +  LIST_ENTRY            *Link;
> >> +  EFI_MM_DRIVER_ENTRY *DriverEntry;
> >> +
> >> +  //
> >> +  // Process Before Dependency
> >> +  //
> >> +  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
> >> +    DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
> >> +    if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
> >> +      DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
> >> +      DEBUG ((DEBUG_DISPATCH, "  BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
> >> +      if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
> >> +        //
> >> +        // Recursively process BEFORE
> >> +        //
> >> +        DEBUG ((DEBUG_DISPATCH, "TRUE\n  END\n  RESULT = TRUE\n"));
> >> +        MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
> >> +      } else {
> >> +        DEBUG ((DEBUG_DISPATCH, "FALSE\n  END\n  RESULT = FALSE\n"));
> >> +      }
> >> +    }
> >> +  }
> >> +
> >> +  //
> >> +  // Convert driver from Dependent to Scheduled state
> >> +  //
> >> +
> >> +  InsertedDriverEntry->Dependent = FALSE;
> >> +  InsertedDriverEntry->Scheduled = TRUE;
> >> +  InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);
> >> +
> >> +
> >> +  //
> >> +  // Process After Dependency
> >> +  //
> >> +  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
> >> +    DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
> >> +    if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
> >> +      DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
> >> +      DEBUG ((DEBUG_DISPATCH, "  AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
> >> +      if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
> >> +        //
> >> +        // Recursively process AFTER
> >> +        //
> >> +        DEBUG ((DEBUG_DISPATCH, "TRUE\n  END\n  RESULT = TRUE\n"));
> >> +        MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
> >> +      } else {
> >> +        DEBUG ((DEBUG_DISPATCH, "FALSE\n  END\n  RESULT = FALSE\n"));
> >> +      }
> >> +    }
> >> +  }
> >> +}
> >> +
> >> +/**
> >> +  Return TRUE if the Fv has been processed, FALSE if not.
> >> +
> >> +  @param  FvHandle              The handle of a FV that's being tested
> >> +
> >> +  @retval TRUE                  Fv protocol on FvHandle has been processed
> >> +  @retval FALSE                 Fv protocol on FvHandle has not yet been
> >> +                                processed
> >> +
> >> +**/
> >> +BOOLEAN
> >> +FvHasBeenProcessed (
> >> +  IN EFI_HANDLE  FvHandle
> >> +  )
> >> +{
> >> +  LIST_ENTRY    *Link;
> >> +  KNOWN_HANDLE  *KnownHandle;
> >> +
> >> +  for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {
> >> +    KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);
> >> +    if (KnownHandle->Handle == FvHandle) {
> >> +      return TRUE;
> >> +    }
> >> +  }
> >> +  return FALSE;
> >> +}
> >> +
> >> +/**
> >> +  Remember that Fv protocol on FvHandle has had it's drivers placed on the
> >> +  mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are
> >> +  never removed/freed from the mFvHandleList.
> >> +
> >> +  @param  FvHandle              The handle of a FV that has been processed
> >> +
> >> +**/
> >> +VOID
> >> +FvIsBeingProcesssed (
> >> +  IN EFI_HANDLE  FvHandle
> >> +  )
> >> +{
> >> +  KNOWN_HANDLE  *KnownHandle;
> >> +
> >> +  DEBUG ((DEBUG_INFO, "FvIsBeingProcesssed - 0x%08x\n", FvHandle));
> >> +
> >> +  KnownHandle = AllocatePool (sizeof (KNOWN_HANDLE));
> >> +  ASSERT (KnownHandle != NULL);
> >> +
> >> +  KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;
> >> +  KnownHandle->Handle = FvHandle;
> >> +  InsertTailList (&mFvHandleList, &KnownHandle->Link);
> >> +}
> >> +
> >> +/**
> >> +  Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
> >> +  and initilize any state variables. Read the Depex from the FV and store it
> >> +  in DriverEntry. Pre-process the Depex to set the Before and After state.
> >> +  The Discovered list is never free'ed and contains booleans that represent the
> >> +  other possible MM driver states.
> >> +
> >> +  @param  Fv                    Fv protocol, needed to read Depex info out of
> >> +                                FLASH.
> >> +  @param  FvHandle              Handle for Fv, needed in the
> >> +                                EFI_MM_DRIVER_ENTRY so that the PE image can be
> >> +                                read out of the FV at a later time.
> >> +  @param  DriverName            Name of driver to add to mDiscoveredList.
> >> +
> >> +  @retval EFI_SUCCESS           If driver was added to the mDiscoveredList.
> >> +  @retval EFI_ALREADY_STARTED   The driver has already been started. Only one
> >> +                                DriverName may be active in the system at any one
> >> +                                time.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +MmAddToDriverList (
> >> +  IN EFI_HANDLE   FvHandle,
> >> +  IN VOID         *Pe32Data,
> >> +  IN UINTN        Pe32DataSize,
> >> +  IN VOID         *Depex,
> >> +  IN UINTN        DepexSize,
> >> +  IN EFI_GUID     *DriverName
> >> +  )
> >> +{
> >> +  EFI_MM_DRIVER_ENTRY  *DriverEntry;
> >> +
> >> +  DEBUG ((DEBUG_INFO, "MmAddToDriverList - %g (0x%08x)\n", DriverName, Pe32Data));
> >> +
> >> +  //
> >> +  // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
> >> +  // NULL or FALSE.
> >> +  //
> >> +  DriverEntry = AllocateZeroPool (sizeof (EFI_MM_DRIVER_ENTRY));
> >> +  ASSERT (DriverEntry != NULL);
> >> +
> >> +  DriverEntry->Signature        = EFI_MM_DRIVER_ENTRY_SIGNATURE;
> >> +  CopyGuid (&DriverEntry->FileName, DriverName);
> >> +  DriverEntry->FvHandle         = FvHandle;
> >> +  DriverEntry->Pe32Data         = Pe32Data;
> >> +  DriverEntry->Pe32DataSize     = Pe32DataSize;
> >> +  DriverEntry->Depex            = Depex;
> >> +  DriverEntry->DepexSize        = DepexSize;
> >> +
> >> +  MmGetDepexSectionAndPreProccess (DriverEntry);
> >> +
> >> +  InsertTailList (&mDiscoveredList, &DriverEntry->Link);
> >> +  gRequestDispatch = TRUE;
> >> +
> >> +  return EFI_SUCCESS;
> >> +}
> >> +
> >> +/**
> >> +  This function is the main entry point for an MM handler dispatch
> >> +  or communicate-based callback.
> >> +
> >> +  Event notification that is fired every time a FV dispatch protocol is added.
> >> +  More than one protocol may have been added when this event is fired, so you
> >> +  must loop on MmLocateHandle () to see how many protocols were added and
> >> +  do the following to each FV:
> >> +  If the Fv has already been processed, skip it. If the Fv has not been
> >> +  processed then mark it as being processed, as we are about to process it.
> >> +  Read the Fv and add any driver in the Fv to the mDiscoveredList.The
> >> +  mDiscoveredList is never free'ed and contains variables that define
> >> +  the other states the MM driver transitions to..
> >> +  While you are at it read the A Priori file into memory.
> >> +  Place drivers in the A Priori list onto the mScheduledQueue.
> >> +
> >> +  @param  DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
> >> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> >> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> >> +                          be conveyed from a non-MM environment into an MM environment.
> >> +  @param  CommBufferSize  The size of the CommBuffer.
> >> +
> >> +  @return Status Code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmDriverDispatchHandler (
> >> +  IN     EFI_HANDLE  DispatchHandle,
> >> +  IN     CONST VOID  *Context,        OPTIONAL
> >> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> >> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> >> +  )
> >> +{
> >> +  EFI_STATUS                            Status;
> >> +
> >> +  DEBUG ((DEBUG_INFO, "MmDriverDispatchHandler\n"));
> >> +
> >> +  //
> >> +  // Execute the MM Dispatcher on any newly discovered FVs and previously
> >> +  // discovered MM drivers that have been discovered but not dispatched.
> >> +  //
> >> +  Status = MmDispatcher ();
> >> +
> >> +  //
> >> +  // Check to see if CommBuffer and CommBufferSize are valid
> >> +  //
> >> +  if (CommBuffer != NULL && CommBufferSize != NULL) {
> >> +    if (*CommBufferSize > 0) {
> >> +      if (Status == EFI_NOT_READY) {
> >> +        //
> >> +        // If a the MM Core Entry Point was just registered, then set flag to
> >> +        // request the MM Dispatcher to be restarted.
> >> +        //
> >> +        *(UINT8 *)CommBuffer = COMM_BUFFER_MM_DISPATCH_RESTART;
> >> +      } else if (!EFI_ERROR (Status)) {
> >> +        //
> >> +        // Set the flag to show that the MM Dispatcher executed without errors
> >> +        //
> >> +        *(UINT8 *)CommBuffer = COMM_BUFFER_MM_DISPATCH_SUCCESS;
> >> +      } else {
> >> +        //
> >> +        // Set the flag to show that the MM Dispatcher encountered an error
> >> +        //
> >> +        *(UINT8 *)CommBuffer = COMM_BUFFER_MM_DISPATCH_ERROR;
> >> +      }
> >> +    }
> >> +  }
> >> +
> >> +  return EFI_SUCCESS;
> >> +}
> >> +
> >> +/**
> >> +  This function is the main entry point for an MM handler dispatch
> >> +  or communicate-based callback.
> >> +
> >> +  @param  DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
> >> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> >> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> >> +                          be conveyed from a non-MM environment into an MM environment.
> >> +  @param  CommBufferSize  The size of the CommBuffer.
> >> +
> >> +  @return Status Code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmFvDispatchHandler (
> >> +  IN     EFI_HANDLE               DispatchHandle,
> >> +  IN     CONST VOID               *Context,        OPTIONAL
> >> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> >> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> >> +  )
> >> +{
> >> +  EFI_STATUS                            Status;
> >> +  EFI_MM_COMMUNICATE_FV_DISPATCH_DATA  *CommunicationFvDispatchData;
> >> +  EFI_FIRMWARE_VOLUME_HEADER            *FwVolHeader;
> >> +
> >> +  DEBUG ((DEBUG_INFO, "MmFvDispatchHandler\n"));
> >> +
> >> +  CommunicationFvDispatchData = CommBuffer;
> >> +
> >> +  DEBUG ((DEBUG_INFO, "  Dispatch - 0x%016lx - 0x%016lx\n", CommunicationFvDispatchData->Address, CommunicationFvDispatchData->Size));
> >> +
> >> +  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)CommunicationFvDispatchData->Address;
> >> +
> >> +  MmCoreFfsFindMmDriver (FwVolHeader);
> >> +
> >> +  //
> >> +  // Execute the MM Dispatcher on any newly discovered FVs and previously
> >> +  // discovered MM drivers that have been discovered but not dispatched.
> >> +  //
> >> +  Status = MmDispatcher ();
> >> +
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Traverse the discovered list for any drivers that were discovered but not loaded
> >> +  because the dependency experessions evaluated to false.
> >> +
> >> +**/
> >> +VOID
> >> +MmDisplayDiscoveredNotDispatched (
> >> +  VOID
> >> +  )
> >> +{
> >> +  LIST_ENTRY                   *Link;
> >> +  EFI_MM_DRIVER_ENTRY         *DriverEntry;
> >> +
> >> +  for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {
> >> +    DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
> >> +    if (DriverEntry->Dependent) {
> >> +      DEBUG ((DEBUG_LOAD, "MM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));
> >> +    }
> >> +  }
> >> +}
> >> diff --git a/StandaloneMmPkg/Core/FwVol.c b/StandaloneMmPkg/Core/FwVol.c
> >> new file mode 100644
> >> index 0000000000..901c58bc53
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Core/FwVol.c
> >> @@ -0,0 +1,104 @@
> >> +/**@file
> >> +
> >> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> >> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +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
> >> +
> >> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +
> >> +**/
> >> +
> >> +#include "StandaloneMmCore.h"
> >> +#include <Library/FvLib.h>
> >> +
> >> +//
> >> +// List of file types supported by dispatcher
> >> +//
> >> +EFI_FV_FILETYPE mMmFileTypes[] = {
> >> +  EFI_FV_FILETYPE_MM,
> >> +  0xE, //EFI_FV_FILETYPE_MM_STANDALONE,
> >> +       //
> >> +       // Note: DXE core will process the FV image file, so skip it in MM core
> >> +       // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
> >> +       //
> >> +};
> >> +
> >> +EFI_STATUS
> >> +MmAddToDriverList (
> >> +  IN EFI_HANDLE   FvHandle,
> >> +  IN VOID         *Pe32Data,
> >> +  IN UINTN        Pe32DataSize,
> >> +  IN VOID         *Depex,
> >> +  IN UINTN        DepexSize,
> >> +  IN EFI_GUID     *DriverName
> >> +  );
> >> +
> >> +BOOLEAN
> >> +FvHasBeenProcessed (
> >> +  IN EFI_HANDLE  FvHandle
> >> +  );
> >> +
> >> +VOID
> >> +FvIsBeingProcesssed (
> >> +  IN EFI_HANDLE  FvHandle
> >> +  );
> >> +
> >> +EFI_STATUS
> >> +MmCoreFfsFindMmDriver (
> >> +  IN  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader
> >> +  )
> >> +/*++
> >> +
> >> +Routine Description:
> >> +  Given the pointer to the Firmware Volume Header find the
> >> +  MM driver and return it's PE32 image.
> >> +
> >> +Arguments:
> >> +  FwVolHeader - Pointer to memory mapped FV
> >> +
> >> +Returns:
> >> +  other       - Failure
> >> +
> >> +--*/
> >> +{
> >> +  EFI_STATUS          Status;
> >> +  EFI_STATUS          DepexStatus;
> >> +  EFI_FFS_FILE_HEADER *FileHeader;
> >> +  EFI_FV_FILETYPE     FileType;
> >> +  VOID                *Pe32Data;
> >> +  UINTN               Pe32DataSize;
> >> +  VOID                *Depex;
> >> +  UINTN               DepexSize;
> >> +  UINTN               Index;
> >> +
> >> +  DEBUG((DEBUG_INFO, "MmCoreFfsFindMmDriver - 0x%x\n", FwVolHeader));
> >> +
> >> +  if (FvHasBeenProcessed (FwVolHeader)) {
> >> +    return EFI_SUCCESS;
> >> +  }
> >> +
> >> +  FvIsBeingProcesssed (FwVolHeader);
> >> +
> >> +  for (Index = 0; Index < sizeof(mMmFileTypes) / sizeof(mMmFileTypes[0]); Index++) {
> >> +    DEBUG((DEBUG_INFO, "Check MmFileTypes - 0x%x\n", mMmFileTypes[Index]));
> >> +    FileType = mMmFileTypes[Index];
> >> +    FileHeader = NULL;
> >> +    do {
> >> +      Status = FfsFindNextFile(FileType, FwVolHeader, &FileHeader);
> >> +      if (!EFI_ERROR(Status)) {
> >> +        Status = FfsFindSectionData(EFI_SECTION_PE32, FileHeader, &Pe32Data, &Pe32DataSize);
> >> +        DEBUG((DEBUG_INFO, "Find PE data - 0x%x\n", Pe32Data));
> >> +        DepexStatus = FfsFindSectionData(EFI_SECTION_MM_DEPEX, FileHeader, &Depex, &DepexSize);
> >> +        if (!EFI_ERROR(DepexStatus)) {
> >> +          MmAddToDriverList(FwVolHeader, Pe32Data, Pe32DataSize, Depex, DepexSize, &FileHeader->Name);
> >> +        }
> >> +      }
> >> +    } while (!EFI_ERROR(Status));
> >> +  }
> >> +
> >> +  return Status;
> >> +}
> >> diff --git a/StandaloneMmPkg/Core/Handle.c b/StandaloneMmPkg/Core/Handle.c
> >> new file mode 100644
> >> index 0000000000..01832f4bbe
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Core/Handle.c
> >> @@ -0,0 +1,533 @@
> >> +/** @file
> >> +  SMM handle & protocol handling.
> >> +
> >> +  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
> >> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +  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
> >> +
> >> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +
> >> +**/
> >> +
> >> +#include "StandaloneMmCore.h"
> >> +
> >> +//
> >> +// mProtocolDatabase     - A list of all protocols in the system.  (simple list for now)
> >> +// gHandleList           - A list of all the handles in the system
> >> +//
> >> +LIST_ENTRY  mProtocolDatabase  = INITIALIZE_LIST_HEAD_VARIABLE (mProtocolDatabase);
> >> +LIST_ENTRY  gHandleList        = INITIALIZE_LIST_HEAD_VARIABLE (gHandleList);
> >> +
> >> +/**
> >> +  Check whether a handle is a valid EFI_HANDLE
> >> +
> >> +  @param  UserHandle             The handle to check
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  The handle is NULL or not a valid EFI_HANDLE.
> >> +  @retval EFI_SUCCESS            The handle is valid EFI_HANDLE.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +MmValidateHandle (
> >> +  IN EFI_HANDLE  UserHandle
> >> +  )
> >> +{
> >> +  IHANDLE  *Handle;
> >> +
> >> +  Handle = (IHANDLE *)UserHandle;
> >> +  if (Handle == NULL) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +  if (Handle->Signature != EFI_HANDLE_SIGNATURE) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +  return EFI_SUCCESS;
> >> +}
> >> +
> >> +/**
> >> +  Finds the protocol entry for the requested protocol.
> >> +
> >> +  @param  Protocol               The ID of the protocol
> >> +  @param  Create                 Create a new entry if not found
> >> +
> >> +  @return Protocol entry
> >> +
> >> +**/
> >> +PROTOCOL_ENTRY  *
> >> +MmFindProtocolEntry (
> >> +  IN EFI_GUID   *Protocol,
> >> +  IN BOOLEAN    Create
> >> +  )
> >> +{
> >> +  LIST_ENTRY          *Link;
> >> +  PROTOCOL_ENTRY      *Item;
> >> +  PROTOCOL_ENTRY      *ProtEntry;
> >> +
> >> +  //
> >> +  // Search the database for the matching GUID
> >> +  //
> >> +
> >> +  ProtEntry = NULL;
> >> +  for (Link = mProtocolDatabase.ForwardLink;
> >> +       Link != &mProtocolDatabase;
> >> +       Link = Link->ForwardLink) {
> >> +
> >> +    Item = CR(Link, PROTOCOL_ENTRY, AllEntries, PROTOCOL_ENTRY_SIGNATURE);
> >> +    if (CompareGuid (&Item->ProtocolID, Protocol)) {
> >> +      //
> >> +      // This is the protocol entry
> >> +      //
> >> +      ProtEntry = Item;
> >> +      break;
> >> +    }
> >> +  }
> >> +
> >> +  //
> >> +  // If the protocol entry was not found and Create is TRUE, then
> >> +  // allocate a new entry
> >> +  //
> >> +  if ((ProtEntry == NULL) && Create) {
> >> +    ProtEntry = AllocatePool (sizeof(PROTOCOL_ENTRY));
> >> +    if (ProtEntry != NULL) {
> >> +      //
> >> +      // Initialize new protocol entry structure
> >> +      //
> >> +      ProtEntry->Signature = PROTOCOL_ENTRY_SIGNATURE;
> >> +      CopyGuid ((VOID *)&ProtEntry->ProtocolID, Protocol);
> >> +      InitializeListHead (&ProtEntry->Protocols);
> >> +      InitializeListHead (&ProtEntry->Notify);
> >> +
> >> +      //
> >> +      // Add it to protocol database
> >> +      //
> >> +      InsertTailList (&mProtocolDatabase, &ProtEntry->AllEntries);
> >> +    }
> >> +  }
> >> +  return ProtEntry;
> >> +}
> >> +
> >> +/**
> >> +  Finds the protocol instance for the requested handle and protocol.
> >> +  Note: This function doesn't do parameters checking, it's caller's responsibility
> >> +  to pass in valid parameters.
> >> +
> >> +  @param  Handle                 The handle to search the protocol on
> >> +  @param  Protocol               GUID of the protocol
> >> +  @param  Interface              The interface for the protocol being searched
> >> +
> >> +  @return Protocol instance (NULL: Not found)
> >> +
> >> +**/
> >> +PROTOCOL_INTERFACE *
> >> +MmFindProtocolInterface (
> >> +  IN IHANDLE   *Handle,
> >> +  IN EFI_GUID  *Protocol,
> >> +  IN VOID      *Interface
> >> +  )
> >> +{
> >> +  PROTOCOL_INTERFACE  *Prot;
> >> +  PROTOCOL_ENTRY      *ProtEntry;
> >> +  LIST_ENTRY          *Link;
> >> +
> >> +  Prot = NULL;
> >> +
> >> +  //
> >> +  // Lookup the protocol entry for this protocol ID
> >> +  //
> >> +  ProtEntry = MmFindProtocolEntry (Protocol, FALSE);
> >> +  if (ProtEntry != NULL) {
> >> +    //
> >> +    // Look at each protocol interface for any matches
> >> +    //
> >> +    for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link=Link->ForwardLink) {
> >> +      //
> >> +      // If this protocol interface matches, remove it
> >> +      //
> >> +      Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);
> >> +      if (Prot->Interface == Interface && Prot->Protocol == ProtEntry) {
> >> +        break;
> >> +      }
> >> +      Prot = NULL;
> >> +    }
> >> +  }
> >> +  return Prot;
> >> +}
> >> +
> >> +/**
> >> +  Wrapper function to MmInstallProtocolInterfaceNotify.  This is the public API which
> >> +  Calls the private one which contains a BOOLEAN parameter for notifications
> >> +
> >> +  @param  UserHandle             The handle to install the protocol handler on,
> >> +                                 or NULL if a new handle is to be allocated
> >> +  @param  Protocol               The protocol to add to the handle
> >> +  @param  InterfaceType          Indicates whether Interface is supplied in
> >> +                                 native form.
> >> +  @param  Interface              The interface for the protocol being added
> >> +
> >> +  @return Status code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmInstallProtocolInterface (
> >> +  IN OUT EFI_HANDLE      *UserHandle,
> >> +  IN EFI_GUID            *Protocol,
> >> +  IN EFI_INTERFACE_TYPE  InterfaceType,
> >> +  IN VOID                *Interface
> >> +  )
> >> +{
> >> +  return MmInstallProtocolInterfaceNotify (
> >> +           UserHandle,
> >> +           Protocol,
> >> +           InterfaceType,
> >> +           Interface,
> >> +           TRUE
> >> +           );
> >> +}
> >> +
> >> +/**
> >> +  Installs a protocol interface into the boot services environment.
> >> +
> >> +  @param  UserHandle             The handle to install the protocol handler on,
> >> +                                 or NULL if a new handle is to be allocated
> >> +  @param  Protocol               The protocol to add to the handle
> >> +  @param  InterfaceType          Indicates whether Interface is supplied in
> >> +                                 native form.
> >> +  @param  Interface              The interface for the protocol being added
> >> +  @param  Notify                 indicates whether notify the notification list
> >> +                                 for this protocol
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> >> +  @retval EFI_OUT_OF_RESOURCES   No enough buffer to allocate
> >> +  @retval EFI_SUCCESS            Protocol interface successfully installed
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +MmInstallProtocolInterfaceNotify (
> >> +  IN OUT EFI_HANDLE          *UserHandle,
> >> +  IN     EFI_GUID            *Protocol,
> >> +  IN     EFI_INTERFACE_TYPE  InterfaceType,
> >> +  IN     VOID                *Interface,
> >> +  IN     BOOLEAN             Notify
> >> +  )
> >> +{
> >> +  PROTOCOL_INTERFACE  *Prot;
> >> +  PROTOCOL_ENTRY      *ProtEntry;
> >> +  IHANDLE             *Handle;
> >> +  EFI_STATUS          Status;
> >> +  VOID                *ExistingInterface;
> >> +
> >> +  //
> >> +  // returns EFI_INVALID_PARAMETER if InterfaceType is invalid.
> >> +  // Also added check for invalid UserHandle and Protocol pointers.
> >> +  //
> >> +  if (UserHandle == NULL || Protocol == NULL) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  if (InterfaceType != EFI_NATIVE_INTERFACE) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  //
> >> +  // Print debug message
> >> +  //
> >> +  DEBUG((DEBUG_LOAD | DEBUG_INFO, "MmInstallProtocolInterface: %g %p\n", Protocol, Interface));
> >> +
> >> +  Status = EFI_OUT_OF_RESOURCES;
> >> +  Prot = NULL;
> >> +  Handle = NULL;
> >> +
> >> +  if (*UserHandle != NULL) {
> >> +    Status = MmHandleProtocol (*UserHandle, Protocol, (VOID **)&ExistingInterface);
> >> +    if (!EFI_ERROR (Status)) {
> >> +      return EFI_INVALID_PARAMETER;
> >> +    }
> >> +  }
> >> +
> >> +  //
> >> +  // Lookup the Protocol Entry for the requested protocol
> >> +  //
> >> +  ProtEntry = MmFindProtocolEntry (Protocol, TRUE);
> >> +  if (ProtEntry == NULL) {
> >> +    goto Done;
> >> +  }
> >> +
> >> +  //
> >> +  // Allocate a new protocol interface structure
> >> +  //
> >> +  Prot = AllocateZeroPool (sizeof(PROTOCOL_INTERFACE));
> >> +  if (Prot == NULL) {
> >> +    Status = EFI_OUT_OF_RESOURCES;
> >> +    goto Done;
> >> +  }
> >> +
> >> +  //
> >> +  // If caller didn't supply a handle, allocate a new one
> >> +  //
> >> +  Handle = (IHANDLE *)*UserHandle;
> >> +  if (Handle == NULL) {
> >> +    Handle = AllocateZeroPool (sizeof(IHANDLE));
> >> +    if (Handle == NULL) {
> >> +      Status = EFI_OUT_OF_RESOURCES;
> >> +      goto Done;
> >> +    }
> >> +
> >> +    //
> >> +    // Initialize new handler structure
> >> +    //
> >> +    Handle->Signature = EFI_HANDLE_SIGNATURE;
> >> +    InitializeListHead (&Handle->Protocols);
> >> +
> >> +    //
> >> +    // Add this handle to the list global list of all handles
> >> +    // in the system
> >> +    //
> >> +    InsertTailList (&gHandleList, &Handle->AllHandles);
> >> +  }
> >> +
> >> +  Status = MmValidateHandle (Handle);
> >> +  if (EFI_ERROR (Status)) {
> >> +    goto Done;
> >> +  }
> >> +
> >> +  //
> >> +  // Each interface that is added must be unique
> >> +  //
> >> +  ASSERT (MmFindProtocolInterface (Handle, Protocol, Interface) == NULL);
> >> +
> >> +  //
> >> +  // Initialize the protocol interface structure
> >> +  //
> >> +  Prot->Signature = PROTOCOL_INTERFACE_SIGNATURE;
> >> +  Prot->Handle = Handle;
> >> +  Prot->Protocol = ProtEntry;
> >> +  Prot->Interface = Interface;
> >> +
> >> +  //
> >> +  // Add this protocol interface to the head of the supported
> >> +  // protocol list for this handle
> >> +  //
> >> +  InsertHeadList (&Handle->Protocols, &Prot->Link);
> >> +
> >> +  //
> >> +  // Add this protocol interface to the tail of the
> >> +  // protocol entry
> >> +  //
> >> +  InsertTailList (&ProtEntry->Protocols, &Prot->ByProtocol);
> >> +
> >> +  //
> >> +  // Notify the notification list for this protocol
> >> +  //
> >> +  if (Notify) {
> >> +    MmNotifyProtocol (Prot);
> >> +  }
> >> +  Status = EFI_SUCCESS;
> >> +
> >> +Done:
> >> +  if (!EFI_ERROR (Status)) {
> >> +    //
> >> +    // Return the new handle back to the caller
> >> +    //
> >> +    *UserHandle = Handle;
> >> +  } else {
> >> +    //
> >> +    // There was an error, clean up
> >> +    //
> >> +    if (Prot != NULL) {
> >> +      FreePool (Prot);
> >> +    }
> >> +  }
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Uninstalls all instances of a protocol:interfacer from a handle.
> >> +  If the last protocol interface is remove from the handle, the
> >> +  handle is freed.
> >> +
> >> +  @param  UserHandle             The handle to remove the protocol handler from
> >> +  @param  Protocol               The protocol, of protocol:interface, to remove
> >> +  @param  Interface              The interface, of protocol:interface, to remove
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL.
> >> +  @retval EFI_SUCCESS            Protocol interface successfully uninstalled.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmUninstallProtocolInterface (
> >> +  IN EFI_HANDLE  UserHandle,
> >> +  IN EFI_GUID    *Protocol,
> >> +  IN VOID        *Interface
> >> +  )
> >> +{
> >> +  EFI_STATUS          Status;
> >> +  IHANDLE             *Handle;
> >> +  PROTOCOL_INTERFACE  *Prot;
> >> +
> >> +  //
> >> +  // Check that Protocol is valid
> >> +  //
> >> +  if (Protocol == NULL) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  //
> >> +  // Check that UserHandle is a valid handle
> >> +  //
> >> +  Status = MmValidateHandle (UserHandle);
> >> +  if (EFI_ERROR (Status)) {
> >> +    return Status;
> >> +  }
> >> +
> >> +  //
> >> +  // Check that Protocol exists on UserHandle, and Interface matches the interface in the database
> >> +  //
> >> +  Prot = MmFindProtocolInterface (UserHandle, Protocol, Interface);
> >> +  if (Prot == NULL) {
> >> +    return EFI_NOT_FOUND;
> >> +  }
> >> +
> >> +  //
> >> +  // Remove the protocol interface from the protocol
> >> +  //
> >> +  Status = EFI_NOT_FOUND;
> >> +  Handle = (IHANDLE *)UserHandle;
> >> +  Prot   = MmRemoveInterfaceFromProtocol (Handle, Protocol, Interface);
> >> +
> >> +  if (Prot != NULL) {
> >> +    //
> >> +    // Remove the protocol interface from the handle
> >> +    //
> >> +    RemoveEntryList (&Prot->Link);
> >> +
> >> +    //
> >> +    // Free the memory
> >> +    //
> >> +    Prot->Signature = 0;
> >> +    FreePool (Prot);
> >> +    Status = EFI_SUCCESS;
> >> +  }
> >> +
> >> +  //
> >> +  // If there are no more handlers for the handle, free the handle
> >> +  //
> >> +  if (IsListEmpty (&Handle->Protocols)) {
> >> +    Handle->Signature = 0;
> >> +    RemoveEntryList (&Handle->AllHandles);
> >> +    FreePool (Handle);
> >> +  }
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Locate a certain GUID protocol interface in a Handle's protocols.
> >> +
> >> +  @param  UserHandle             The handle to obtain the protocol interface on
> >> +  @param  Protocol               The GUID of the protocol
> >> +
> >> +  @return The requested protocol interface for the handle
> >> +
> >> +**/
> >> +PROTOCOL_INTERFACE  *
> >> +MmGetProtocolInterface (
> >> +  IN EFI_HANDLE  UserHandle,
> >> +  IN EFI_GUID    *Protocol
> >> +  )
> >> +{
> >> +  EFI_STATUS          Status;
> >> +  PROTOCOL_ENTRY      *ProtEntry;
> >> +  PROTOCOL_INTERFACE  *Prot;
> >> +  IHANDLE             *Handle;
> >> +  LIST_ENTRY          *Link;
> >> +
> >> +  Status = MmValidateHandle (UserHandle);
> >> +  if (EFI_ERROR (Status)) {
> >> +    return NULL;
> >> +  }
> >> +
> >> +  Handle = (IHANDLE *)UserHandle;
> >> +
> >> +  //
> >> +  // Look at each protocol interface for a match
> >> +  //
> >> +  for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link = Link->ForwardLink) {
> >> +    Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);
> >> +    ProtEntry = Prot->Protocol;
> >> +    if (CompareGuid (&ProtEntry->ProtocolID, Protocol)) {
> >> +      return Prot;
> >> +    }
> >> +  }
> >> +  return NULL;
> >> +}
> >> +
> >> +/**
> >> +  Queries a handle to determine if it supports a specified protocol.
> >> +
> >> +  @param  UserHandle             The handle being queried.
> >> +  @param  Protocol               The published unique identifier of the protocol.
> >> +  @param  Interface              Supplies the address where a pointer to the
> >> +                                 corresponding Protocol Interface is returned.
> >> +
> >> +  @retval EFI_SUCCESS            The interface information for the specified protocol was returned.
> >> +  @retval EFI_UNSUPPORTED        The device does not support the specified protocol.
> >> +  @retval EFI_INVALID_PARAMETER  Handle is not a valid EFI_HANDLE..
> >> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL.
> >> +  @retval EFI_INVALID_PARAMETER  Interface is NULL.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmHandleProtocol (
> >> +  IN  EFI_HANDLE  UserHandle,
> >> +  IN  EFI_GUID    *Protocol,
> >> +  OUT VOID        **Interface
> >> +  )
> >> +{
> >> +  EFI_STATUS          Status;
> >> +  PROTOCOL_INTERFACE  *Prot;
> >> +
> >> +  //
> >> +  // Check for invalid Protocol
> >> +  //
> >> +  if (Protocol == NULL) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  //
> >> +  // Check for invalid Interface
> >> +  //
> >> +  if (Interface == NULL) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  } else {
> >> +    *Interface = NULL;
> >> +  }
> >> +
> >> +  //
> >> +  // Check for invalid UserHandle
> >> +  //
> >> +  Status = MmValidateHandle (UserHandle);
> >> +  if (EFI_ERROR (Status)) {
> >> +    return Status;
> >> +  }
> >> +
> >> +  //
> >> +  // Look at each protocol interface for a match
> >> +  //
> >> +  Prot = MmGetProtocolInterface (UserHandle, Protocol);
> >> +  if (Prot == NULL) {
> >> +    return EFI_UNSUPPORTED;
> >> +  }
> >> +
> >> +  //
> >> +  // This is the protocol interface entry for this protocol
> >> +  //
> >> +  *Interface = Prot->Interface;
> >> +
> >> +  return EFI_SUCCESS;
> >> +}
> >> diff --git a/StandaloneMmPkg/Core/InstallConfigurationTable.c b/StandaloneMmPkg/Core/InstallConfigurationTable.c
> >> new file mode 100644
> >> index 0000000000..3a31c63f94
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Core/InstallConfigurationTable.c
> >> @@ -0,0 +1,178 @@
> >> +/** @file
> >> +  System Management System Table Services MmInstallConfigurationTable service
> >> +
> >> +  Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
> >> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +  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
> >> +
> >> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +
> >> +**/
> >> +
> >> +#include "StandaloneMmCore.h"
> >> +
> >> +#define CONFIG_TABLE_SIZE_INCREASED 0x10
> >> +
> >> +UINTN  mMmSystemTableAllocateSize = 0;
> >> +
> >> +/**
> >> +  The MmInstallConfigurationTable() function is used to maintain the list
> >> +  of configuration tables that are stored in the System Management System
> >> +  Table.  The list is stored as an array of (GUID, Pointer) pairs.  The list
> >> +  must be allocated from pool memory with PoolType set to EfiRuntimeServicesData.
> >> +
> >> +  @param  SystemTable      A pointer to the SMM System Table (SMST).
> >> +  @param  Guid             A pointer to the GUID for the entry to add, update, or remove.
> >> +  @param  Table            A pointer to the buffer of the table to add.
> >> +  @param  TableSize        The size of the table to install.
> >> +
> >> +  @retval EFI_SUCCESS           The (Guid, Table) pair was added, updated, or removed.
> >> +  @retval EFI_INVALID_PARAMETER Guid is not valid.
> >> +  @retval EFI_NOT_FOUND         An attempt was made to delete a non-existent entry.
> >> +  @retval EFI_OUT_OF_RESOURCES  There is not enough memory available to complete the operation.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmInstallConfigurationTable (
> >> +  IN  CONST EFI_MM_SYSTEM_TABLE    *SystemTable,
> >> +  IN  CONST EFI_GUID               *Guid,
> >> +  IN  VOID                         *Table,
> >> +  IN  UINTN                        TableSize
> >> +  )
> >> +{
> >> +  UINTN                    Index;
> >> +  EFI_CONFIGURATION_TABLE  *ConfigurationTable;
> >> +  EFI_CONFIGURATION_TABLE  *OldTable;
> >> +
> >> +  //
> >> +  // If Guid is NULL, then this operation cannot be performed
> >> +  //
> >> +  if (Guid == NULL) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  ConfigurationTable = gMmCoreMmst.MmConfigurationTable;
> >> +
> >> +  //
> >> +  // Search all the table for an entry that matches Guid
> >> +  //
> >> +  for (Index = 0; Index < gMmCoreMmst.NumberOfTableEntries; Index++) {
> >> +    if (CompareGuid (Guid, &(ConfigurationTable[Index].VendorGuid))) {
> >> +      break;
> >> +    }
> >> +  }
> >> +
> >> +  if (Index < gMmCoreMmst.NumberOfTableEntries) {
> >> +    //
> >> +    // A match was found, so this is either a modify or a delete operation
> >> +    //
> >> +    if (Table != NULL) {
> >> +      //
> >> +      // If Table is not NULL, then this is a modify operation.
> >> +      // Modify the table entry and return.
> >> +      //
> >> +      ConfigurationTable[Index].VendorTable = Table;
> >> +      return EFI_SUCCESS;
> >> +    }
> >> +
> >> +    //
> >> +    // A match was found and Table is NULL, so this is a delete operation.
> >> +    //
> >> +    gMmCoreMmst.NumberOfTableEntries--;
> >> +
> >> +    //
> >> +    // Copy over deleted entry
> >> +    //
> >> +    CopyMem (
> >> +      &(ConfigurationTable[Index]),
> >> +      &(ConfigurationTable[Index + 1]),
> >> +      (gMmCoreMmst.NumberOfTableEntries - Index) * sizeof (EFI_CONFIGURATION_TABLE)
> >> +      );
> >> +
> >> +  } else {
> >> +    //
> >> +    // No matching GUIDs were found, so this is an add operation.
> >> +    //
> >> +    if (Table == NULL) {
> >> +      //
> >> +      // If Table is NULL on an add operation, then return an error.
> >> +      //
> >> +      return EFI_NOT_FOUND;
> >> +    }
> >> +
> >> +    //
> >> +    // Assume that Index == gMmCoreMmst.NumberOfTableEntries
> >> +    //
> >> +    if ((Index * sizeof (EFI_CONFIGURATION_TABLE)) >= mMmSystemTableAllocateSize) {
> >> +      //
> >> +      // Allocate a table with one additional entry.
> >> +      //
> >> +      mMmSystemTableAllocateSize += (CONFIG_TABLE_SIZE_INCREASED * sizeof (EFI_CONFIGURATION_TABLE));
> >> +      ConfigurationTable = AllocatePool (mMmSystemTableAllocateSize);
> >> +      if (ConfigurationTable == NULL) {
> >> +        //
> >> +        // If a new table could not be allocated, then return an error.
> >> +        //
> >> +        return EFI_OUT_OF_RESOURCES;
> >> +      }
> >> +
> >> +      if (gMmCoreMmst.MmConfigurationTable != NULL) {
> >> +        //
> >> +        // Copy the old table to the new table.
> >> +        //
> >> +        CopyMem (
> >> +          ConfigurationTable,
> >> +          gMmCoreMmst.MmConfigurationTable,
> >> +          Index * sizeof (EFI_CONFIGURATION_TABLE)
> >> +          );
> >> +
> >> +        //
> >> +        // Record the old table pointer.
> >> +        //
> >> +        OldTable = gMmCoreMmst.MmConfigurationTable;
> >> +
> >> +        //
> >> +        // As the MmInstallConfigurationTable() may be re-entered by FreePool() in
> >> +        // its calling stack, updating System table to the new table pointer must
> >> +        // be done before calling FreePool() to free the old table.
> >> +        // It can make sure the gMmCoreMmst.MmConfigurationTable point to the new
> >> +        // table and avoid the errors of use-after-free to the old table by the
> >> +        // reenter of MmInstallConfigurationTable() in FreePool()'s calling stack.
> >> +        //
> >> +        gMmCoreMmst.MmConfigurationTable = ConfigurationTable;
> >> +
> >> +        //
> >> +        // Free the old table after updating System Table to the new table pointer.
> >> +        //
> >> +        FreePool (OldTable);
> >> +      } else {
> >> +        //
> >> +        // Update System Table
> >> +        //
> >> +        gMmCoreMmst.MmConfigurationTable = ConfigurationTable;
> >> +      }
> >> +    }
> >> +
> >> +    //
> >> +    // Fill in the new entry
> >> +    //
> >> +    CopyGuid ((VOID *)&ConfigurationTable[Index].VendorGuid, Guid);
> >> +    ConfigurationTable[Index].VendorTable = Table;
> >> +
> >> +    //
> >> +    // This is an add operation, so increment the number of table entries
> >> +    //
> >> +    gMmCoreMmst.NumberOfTableEntries++;
> >> +  }
> >> +
> >> +  //
> >> +  // CRC-32 field is ignorable for SMM System Table and should be set to zero
> >> +  //
> >> +
> >> +  return EFI_SUCCESS;
> >> +}
> >> diff --git a/StandaloneMmPkg/Core/Locate.c b/StandaloneMmPkg/Core/Locate.c
> >> new file mode 100644
> >> index 0000000000..6a90575f99
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Core/Locate.c
> >> @@ -0,0 +1,496 @@
> >> +/** @file
> >> +  Locate handle functions
> >> +
> >> +  Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
> >> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +  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
> >> +
> >> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +
> >> +**/
> >> +
> >> +#include "StandaloneMmCore.h"
> >> +
> >> +//
> >> +// ProtocolRequest - Last LocateHandle request ID
> >> +//
> >> +UINTN mEfiLocateHandleRequest = 0;
> >> +
> >> +//
> >> +// Internal prototypes
> >> +//
> >> +
> >> +typedef struct {
> >> +  EFI_GUID        *Protocol;
> >> +  VOID            *SearchKey;
> >> +  LIST_ENTRY      *Position;
> >> +  PROTOCOL_ENTRY  *ProtEntry;
> >> +} LOCATE_POSITION;
> >> +
> >> +typedef
> >> +IHANDLE *
> >> +(* CORE_GET_NEXT) (
> >> +  IN OUT LOCATE_POSITION    *Position,
> >> +  OUT VOID                  **Interface
> >> +  );
> >> +
> >> +/**
> >> +  Routine to get the next Handle, when you are searching for all handles.
> >> +
> >> +  @param  Position               Information about which Handle to seach for.
> >> +  @param  Interface              Return the interface structure for the matching
> >> +                                 protocol.
> >> +
> >> +  @return An pointer to IHANDLE if the next Position is not the end of the list.
> >> +          Otherwise,NULL is returned.
> >> +
> >> +**/
> >> +IHANDLE *
> >> +MmGetNextLocateAllHandles (
> >> +  IN OUT LOCATE_POSITION  *Position,
> >> +  OUT    VOID             **Interface
> >> +  )
> >> +{
> >> +  IHANDLE     *Handle;
> >> +
> >> +  //
> >> +  // Next handle
> >> +  //
> >> +  Position->Position = Position->Position->ForwardLink;
> >> +
> >> +  //
> >> +  // If not at the end of the list, get the handle
> >> +  //
> >> +  Handle      = NULL;
> >> +  *Interface  = NULL;
> >> +  if (Position->Position != &gHandleList) {
> >> +    Handle = CR (Position->Position, IHANDLE, AllHandles, EFI_HANDLE_SIGNATURE);
> >> +  }
> >> +  return Handle;
> >> +}
> >> +
> >> +/**
> >> +  Routine to get the next Handle, when you are searching for register protocol
> >> +  notifies.
> >> +
> >> +  @param  Position               Information about which Handle to seach for.
> >> +  @param  Interface              Return the interface structure for the matching
> >> +                                 protocol.
> >> +
> >> +  @return An pointer to IHANDLE if the next Position is not the end of the list.
> >> +          Otherwise,NULL is returned.
> >> +
> >> +**/
> >> +IHANDLE *
> >> +MmGetNextLocateByRegisterNotify (
> >> +  IN OUT LOCATE_POSITION  *Position,
> >> +  OUT    VOID             **Interface
> >> +  )
> >> +{
> >> +  IHANDLE             *Handle;
> >> +  PROTOCOL_NOTIFY     *ProtNotify;
> >> +  PROTOCOL_INTERFACE  *Prot;
> >> +  LIST_ENTRY          *Link;
> >> +
> >> +  Handle      = NULL;
> >> +  *Interface  = NULL;
> >> +  ProtNotify = Position->SearchKey;
> >> +
> >> +  //
> >> +  // If this is the first request, get the next handle
> >> +  //
> >> +  if (ProtNotify != NULL) {
> >> +    ASSERT(ProtNotify->Signature == PROTOCOL_NOTIFY_SIGNATURE);
> >> +    Position->SearchKey = NULL;
> >> +
> >> +    //
> >> +    // If not at the end of the list, get the next handle
> >> +    //
> >> +    Link = ProtNotify->Position->ForwardLink;
> >> +    if (Link != &ProtNotify->Protocol->Protocols) {
> >> +      Prot = CR (Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_INTERFACE_SIGNATURE);
> >> +      Handle = Prot->Handle;
> >> +      *Interface = Prot->Interface;
> >> +    }
> >> +  }
> >> +  return Handle;
> >> +}
> >> +
> >> +/**
> >> +  Routine to get the next Handle, when you are searching for a given protocol.
> >> +
> >> +  @param  Position               Information about which Handle to seach for.
> >> +  @param  Interface              Return the interface structure for the matching
> >> +                                 protocol.
> >> +
> >> +  @return An pointer to IHANDLE if the next Position is not the end of the list.
> >> +          Otherwise,NULL is returned.
> >> +
> >> +**/
> >> +IHANDLE *
> >> +MmGetNextLocateByProtocol (
> >> +  IN OUT LOCATE_POSITION  *Position,
> >> +  OUT    VOID             **Interface
> >> +  )
> >> +{
> >> +  IHANDLE             *Handle;
> >> +  LIST_ENTRY          *Link;
> >> +  PROTOCOL_INTERFACE  *Prot;
> >> +
> >> +  Handle      = NULL;
> >> +  *Interface  = NULL;
> >> +  for (; ;) {
> >> +    //
> >> +    // Next entry
> >> +    //
> >> +    Link = Position->Position->ForwardLink;
> >> +    Position->Position = Link;
> >> +
> >> +    //
> >> +    // If not at the end, return the handle
> >> +    //
> >> +    if (Link == &Position->ProtEntry->Protocols) {
> >> +      Handle = NULL;
> >> +      break;
> >> +    }
> >> +
> >> +    //
> >> +    // Get the handle
> >> +    //
> >> +    Prot = CR(Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_INTERFACE_SIGNATURE);
> >> +    Handle = Prot->Handle;
> >> +    *Interface = Prot->Interface;
> >> +
> >> +    //
> >> +    // If this handle has not been returned this request, then
> >> +    // return it now
> >> +    //
> >> +    if (Handle->LocateRequest != mEfiLocateHandleRequest) {
> >> +      Handle->LocateRequest = mEfiLocateHandleRequest;
> >> +      break;
> >> +    }
> >> +  }
> >> +  return Handle;
> >> +}
> >> +
> >> +/**
> >> +  Return the first Protocol Interface that matches the Protocol GUID. If
> >> +  Registration is pasased in return a Protocol Instance that was just add
> >> +  to the system. If Retistration is NULL return the first Protocol Interface
> >> +  you find.
> >> +
> >> +  @param  Protocol               The protocol to search for
> >> +  @param  Registration           Optional Registration Key returned from
> >> +                                 RegisterProtocolNotify()
> >> +  @param  Interface              Return the Protocol interface (instance).
> >> +
> >> +  @retval EFI_SUCCESS            If a valid Interface is returned
> >> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> >> +  @retval EFI_NOT_FOUND          Protocol interface not found
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmLocateProtocol (
> >> +  IN  EFI_GUID  *Protocol,
> >> +  IN  VOID      *Registration OPTIONAL,
> >> +  OUT VOID      **Interface
> >> +  )
> >> +{
> >> +  EFI_STATUS              Status;
> >> +  LOCATE_POSITION         Position;
> >> +  PROTOCOL_NOTIFY         *ProtNotify;
> >> +  IHANDLE                 *Handle;
> >> +
> >> +  if ((Interface == NULL) || (Protocol == NULL)) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  *Interface = NULL;
> >> +  Status = EFI_SUCCESS;
> >> +
> >> +  //
> >> +  // Set initial position
> >> +  //
> >> +  Position.Protocol  = Protocol;
> >> +  Position.SearchKey = Registration;
> >> +  Position.Position  = &gHandleList;
> >> +
> >> +  mEfiLocateHandleRequest += 1;
> >> +
> >> +  if (Registration == NULL) {
> >> +    //
> >> +    // Look up the protocol entry and set the head pointer
> >> +    //
> >> +    Position.ProtEntry = MmFindProtocolEntry (Protocol, FALSE);
> >> +    if (Position.ProtEntry == NULL) {
> >> +      return EFI_NOT_FOUND;
> >> +    }
> >> +    Position.Position = &Position.ProtEntry->Protocols;
> >> +
> >> +    Handle = MmGetNextLocateByProtocol (&Position, Interface);
> >> +  } else {
> >> +    Handle = MmGetNextLocateByRegisterNotify (&Position, Interface);
> >> +  }
> >> +
> >> +  if (Handle == NULL) {
> >> +    Status = EFI_NOT_FOUND;
> >> +  } else if (Registration != NULL) {
> >> +    //
> >> +    // If this is a search by register notify and a handle was
> >> +    // returned, update the register notification position
> >> +    //
> >> +    ProtNotify = Registration;
> >> +    ProtNotify->Position = ProtNotify->Position->ForwardLink;
> >> +  }
> >> +
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Locates the requested handle(s) and returns them in Buffer.
> >> +
> >> +  @param  SearchType             The type of search to perform to locate the
> >> +                                 handles
> >> +  @param  Protocol               The protocol to search for
> >> +  @param  SearchKey              Dependant on SearchType
> >> +  @param  BufferSize             On input the size of Buffer.  On output the
> >> +                                 size of data returned.
> >> +  @param  Buffer                 The buffer to return the results in
> >> +
> >> +  @retval EFI_BUFFER_TOO_SMALL   Buffer too small, required buffer size is
> >> +                                 returned in BufferSize.
> >> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> >> +  @retval EFI_SUCCESS            Successfully found the requested handle(s) and
> >> +                                 returns them in Buffer.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmLocateHandle (
> >> +  IN     EFI_LOCATE_SEARCH_TYPE  SearchType,
> >> +  IN     EFI_GUID                *Protocol   OPTIONAL,
> >> +  IN     VOID                    *SearchKey  OPTIONAL,
> >> +  IN OUT UINTN                   *BufferSize,
> >> +  OUT    EFI_HANDLE              *Buffer
> >> +  )
> >> +{
> >> +  EFI_STATUS       Status;
> >> +  LOCATE_POSITION  Position;
> >> +  PROTOCOL_NOTIFY  *ProtNotify;
> >> +  CORE_GET_NEXT    GetNext;
> >> +  UINTN            ResultSize;
> >> +  IHANDLE          *Handle;
> >> +  IHANDLE          **ResultBuffer;
> >> +  VOID             *Interface;
> >> +
> >> +  if (BufferSize == NULL) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  if ((*BufferSize > 0) && (Buffer == NULL)) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  GetNext = NULL;
> >> +
> >> +  //
> >> +  // Set initial position
> >> +  //
> >> +  Position.Protocol  = Protocol;
> >> +  Position.SearchKey = SearchKey;
> >> +  Position.Position  = &gHandleList;
> >> +
> >> +  ResultSize = 0;
> >> +  ResultBuffer = (IHANDLE **) Buffer;
> >> +  Status = EFI_SUCCESS;
> >> +
> >> +  //
> >> +  // Get the search function based on type
> >> +  //
> >> +  switch (SearchType) {
> >> +  case AllHandles:
> >> +    GetNext = MmGetNextLocateAllHandles;
> >> +    break;
> >> +
> >> +  case ByRegisterNotify:
> >> +    GetNext = MmGetNextLocateByRegisterNotify;
> >> +    //
> >> +    // Must have SearchKey for locate ByRegisterNotify
> >> +    //
> >> +    if (SearchKey == NULL) {
> >> +      Status = EFI_INVALID_PARAMETER;
> >> +    }
> >> +    break;
> >> +
> >> +  case ByProtocol:
> >> +    GetNext = MmGetNextLocateByProtocol;
> >> +    if (Protocol == NULL) {
> >> +      Status = EFI_INVALID_PARAMETER;
> >> +      break;
> >> +    }
> >> +    //
> >> +    // Look up the protocol entry and set the head pointer
> >> +    //
> >> +    Position.ProtEntry = MmFindProtocolEntry (Protocol, FALSE);
> >> +    if (Position.ProtEntry == NULL) {
> >> +      Status = EFI_NOT_FOUND;
> >> +      break;
> >> +    }
> >> +    Position.Position = &Position.ProtEntry->Protocols;
> >> +    break;
> >> +
> >> +  default:
> >> +    Status = EFI_INVALID_PARAMETER;
> >> +    break;
> >> +  }
> >> +
> >> +  if (EFI_ERROR(Status)) {
> >> +    return Status;
> >> +  }
> >> +
> >> +  //
> >> +  // Enumerate out the matching handles
> >> +  //
> >> +  mEfiLocateHandleRequest += 1;
> >> +  for (; ;) {
> >> +    //
> >> +    // Get the next handle.  If no more handles, stop
> >> +    //
> >> +    Handle = GetNext (&Position, &Interface);
> >> +    if (NULL == Handle) {
> >> +      break;
> >> +    }
> >> +
> >> +    //
> >> +    // Increase the resulting buffer size, and if this handle
> >> +    // fits return it
> >> +    //
> >> +    ResultSize += sizeof(Handle);
> >> +    if (ResultSize <= *BufferSize) {
> >> +        *ResultBuffer = Handle;
> >> +        ResultBuffer += 1;
> >> +    }
> >> +  }
> >> +
> >> +  //
> >> +  // If the result is a zero length buffer, then there were no
> >> +  // matching handles
> >> +  //
> >> +  if (ResultSize == 0) {
> >> +    Status = EFI_NOT_FOUND;
> >> +  } else {
> >> +    //
> >> +    // Return the resulting buffer size.  If it's larger than what
> >> +    // was passed, then set the error code
> >> +    //
> >> +    if (ResultSize > *BufferSize) {
> >> +      Status = EFI_BUFFER_TOO_SMALL;
> >> +    }
> >> +
> >> +    *BufferSize = ResultSize;
> >> +
> >> +    if (SearchType == ByRegisterNotify && !EFI_ERROR(Status)) {
> >> +      ASSERT (SearchKey != NULL);
> >> +      //
> >> +      // If this is a search by register notify and a handle was
> >> +      // returned, update the register notification position
> >> +      //
> >> +      ProtNotify = SearchKey;
> >> +      ProtNotify->Position = ProtNotify->Position->ForwardLink;
> >> +    }
> >> +  }
> >> +
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Function returns an array of handles that support the requested protocol
> >> +  in a buffer allocated from pool. This is a version of MmLocateHandle()
> >> +  that allocates a buffer for the caller.
> >> +
> >> +  @param  SearchType             Specifies which handle(s) are to be returned.
> >> +  @param  Protocol               Provides the protocol to search by.    This
> >> +                                 parameter is only valid for SearchType
> >> +                                 ByProtocol.
> >> +  @param  SearchKey              Supplies the search key depending on the
> >> +                                 SearchType.
> >> +  @param  NumberHandles          The number of handles returned in Buffer.
> >> +  @param  Buffer                 A pointer to the buffer to return the requested
> >> +                                 array of  handles that support Protocol.
> >> +
> >> +  @retval EFI_SUCCESS            The result array of handles was returned.
> >> +  @retval EFI_NOT_FOUND          No handles match the search.
> >> +  @retval EFI_OUT_OF_RESOURCES   There is not enough pool memory to store the
> >> +                                 matching results.
> >> +  @retval EFI_INVALID_PARAMETER  One or more parameters are not valid.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmLocateHandleBuffer (
> >> +  IN     EFI_LOCATE_SEARCH_TYPE  SearchType,
> >> +  IN     EFI_GUID                *Protocol OPTIONAL,
> >> +  IN     VOID                    *SearchKey OPTIONAL,
> >> +  IN OUT UINTN                   *NumberHandles,
> >> +  OUT    EFI_HANDLE              **Buffer
> >> +  )
> >> +{
> >> +  EFI_STATUS  Status;
> >> +  UINTN       BufferSize;
> >> +
> >> +  if (NumberHandles == NULL) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  if (Buffer == NULL) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  BufferSize = 0;
> >> +  *NumberHandles = 0;
> >> +  *Buffer = NULL;
> >> +  Status = MmLocateHandle (
> >> +             SearchType,
> >> +             Protocol,
> >> +             SearchKey,
> >> +             &BufferSize,
> >> +             *Buffer
> >> +             );
> >> +  //
> >> +  // LocateHandleBuffer() returns incorrect status code if SearchType is
> >> +  // invalid.
> >> +  //
> >> +  // Add code to correctly handle expected errors from MmLocateHandle().
> >> +  //
> >> +  if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {
> >> +    if (Status != EFI_INVALID_PARAMETER) {
> >> +      Status = EFI_NOT_FOUND;
> >> +    }
> >> +    return Status;
> >> +  }
> >> +
> >> +  *Buffer = AllocatePool (BufferSize);
> >> +  if (*Buffer == NULL) {
> >> +    return EFI_OUT_OF_RESOURCES;
> >> +  }
> >> +
> >> +  Status = MmLocateHandle (
> >> +             SearchType,
> >> +             Protocol,
> >> +             SearchKey,
> >> +             &BufferSize,
> >> +             *Buffer
> >> +             );
> >> +
> >> +  *NumberHandles = BufferSize / sizeof(EFI_HANDLE);
> >> +  if (EFI_ERROR(Status)) {
> >> +    *NumberHandles = 0;
> >> +  }
> >> +
> >> +  return Status;
> >> +}
> >> diff --git a/StandaloneMmPkg/Core/Mmi.c b/StandaloneMmPkg/Core/Mmi.c
> >> new file mode 100644
> >> index 0000000000..29aba7b53d
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Core/Mmi.c
> >> @@ -0,0 +1,337 @@
> >> +/** @file
> >> +  MMI management.
> >> +
> >> +  Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
> >> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +  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
> >> +
> >> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +
> >> +**/
> >> +
> >> +#include "StandaloneMmCore.h"
> >> +
> >> +//
> >> +// MM_HANDLER_STATE_NOTIFIER
> >> +//
> >> +
> >> +//
> >> +// MM_HANDLER - used for each MM handler
> >> +//
> >> +
> >> +#define MMI_ENTRY_SIGNATURE  SIGNATURE_32('m','m','i','e')
> >> +
> >> + typedef struct {
> >> +  UINTN       Signature;
> >> +  LIST_ENTRY  AllEntries;  // All entries
> >> +
> >> +  EFI_GUID    HandlerType; // Type of interrupt
> >> +  LIST_ENTRY  MmiHandlers; // All handlers
> >> +} MMI_ENTRY;
> >> +
> >> +#define MMI_HANDLER_SIGNATURE  SIGNATURE_32('m','m','i','h')
> >> +
> >> + typedef struct {
> >> +  UINTN                         Signature;
> >> +  LIST_ENTRY                    Link;        // Link on MMI_ENTRY.MmiHandlers
> >> +  EFI_MM_HANDLER_ENTRY_POINT    Handler;     // The mm handler's entry point
> >> +  MMI_ENTRY                     *MmiEntry;
> >> +} MMI_HANDLER;
> >> +
> >> +LIST_ENTRY  mRootMmiHandlerList = INITIALIZE_LIST_HEAD_VARIABLE (mRootMmiHandlerList);
> >> +LIST_ENTRY  mMmiEntryList       = INITIALIZE_LIST_HEAD_VARIABLE (mMmiEntryList);
> >> +
> >> +/**
> >> +  Finds the MMI entry for the requested handler type.
> >> +
> >> +  @param  HandlerType            The type of the interrupt
> >> +  @param  Create                 Create a new entry if not found
> >> +
> >> +  @return MMI entry
> >> +
> >> +**/
> >> +MMI_ENTRY  *
> >> +EFIAPI
> >> +MmCoreFindMmiEntry (
> >> +  IN EFI_GUID  *HandlerType,
> >> +  IN BOOLEAN   Create
> >> +  )
> >> +{
> >> +  LIST_ENTRY  *Link;
> >> +  MMI_ENTRY   *Item;
> >> +  MMI_ENTRY   *MmiEntry;
> >> +
> >> +  //
> >> +  // Search the MMI entry list for the matching GUID
> >> +  //
> >> +  MmiEntry = NULL;
> >> +  for (Link = mMmiEntryList.ForwardLink;
> >> +       Link != &mMmiEntryList;
> >> +       Link = Link->ForwardLink) {
> >> +
> >> +    Item = CR (Link, MMI_ENTRY, AllEntries, MMI_ENTRY_SIGNATURE);
> >> +    if (CompareGuid (&Item->HandlerType, HandlerType)) {
> >> +      //
> >> +      // This is the MMI entry
> >> +      //
> >> +      MmiEntry = Item;
> >> +      break;
> >> +    }
> >> +  }
> >> +
> >> +  //
> >> +  // If the protocol entry was not found and Create is TRUE, then
> >> +  // allocate a new entry
> >> +  //
> >> +  if ((MmiEntry == NULL) && Create) {
> >> +    MmiEntry = AllocatePool (sizeof(MMI_ENTRY));
> >> +    if (MmiEntry != NULL) {
> >> +      //
> >> +      // Initialize new MMI entry structure
> >> +      //
> >> +      MmiEntry->Signature = MMI_ENTRY_SIGNATURE;
> >> +      CopyGuid ((VOID *)&MmiEntry->HandlerType, HandlerType);
> >> +      InitializeListHead (&MmiEntry->MmiHandlers);
> >> +
> >> +      //
> >> +      // Add it to MMI entry list
> >> +      //
> >> +      InsertTailList (&mMmiEntryList, &MmiEntry->AllEntries);
> >> +    }
> >> +  }
> >> +  return MmiEntry;
> >> +}
> >> +
> >> +/**
> >> +  Manage MMI of a particular type.
> >> +
> >> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
> >> +  @param  Context        Points to an optional context buffer.
> >> +  @param  CommBuffer     Points to the optional communication buffer.
> >> +  @param  CommBufferSize Points to the size of the optional communication buffer.
> >> +
> >> +  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING  Interrupt source was processed successfully but not quiesced.
> >> +  @retval EFI_INTERRUPT_PENDING              One or more MMI sources could not be quiesced.
> >> +  @retval EFI_NOT_FOUND                      Interrupt source was not handled or quiesced.
> >> +  @retval EFI_SUCCESS                        Interrupt source was handled and quiesced.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmiManage (
> >> +  IN     CONST EFI_GUID  *HandlerType,
> >> +  IN     CONST VOID      *Context         OPTIONAL,
> >> +  IN OUT VOID            *CommBuffer      OPTIONAL,
> >> +  IN OUT UINTN           *CommBufferSize  OPTIONAL
> >> +  )
> >> +{
> >> +  LIST_ENTRY   *Link;
> >> +  LIST_ENTRY   *Head;
> >> +  MMI_ENTRY    *MmiEntry;
> >> +  MMI_HANDLER  *MmiHandler;
> >> +  BOOLEAN      SuccessReturn;
> >> +  EFI_STATUS   Status;
> >> +
> >> +  Status = EFI_NOT_FOUND;
> >> +  SuccessReturn = FALSE;
> >> +  if (HandlerType == NULL) {
> >> +    //
> >> +    // Root MMI handler
> >> +    //
> >> +
> >> +    Head = &mRootMmiHandlerList;
> >> +  } else {
> >> +    //
> >> +    // Non-root MMI handler
> >> +    //
> >> +    MmiEntry = MmCoreFindMmiEntry ((EFI_GUID *) HandlerType, FALSE);
> >> +    if (MmiEntry == NULL) {
> >> +      //
> >> +      // There is no handler registered for this interrupt source
> >> +      //
> >> +      return Status;
> >> +    }
> >> +
> >> +    Head = &MmiEntry->MmiHandlers;
> >> +  }
> >> +
> >> +  for (Link = Head->ForwardLink; Link != Head; Link = Link->ForwardLink) {
> >> +    MmiHandler = CR (Link, MMI_HANDLER, Link, MMI_HANDLER_SIGNATURE);
> >> +
> >> +    Status = MmiHandler->Handler (
> >> +               (EFI_HANDLE) MmiHandler,
> >> +               Context,
> >> +               CommBuffer,
> >> +               CommBufferSize
> >> +               );
> >> +
> >> +    switch (Status) {
> >> +    case EFI_INTERRUPT_PENDING:
> >> +      //
> >> +      // If a handler returns EFI_INTERRUPT_PENDING and HandlerType is not NULL then
> >> +      // no additional handlers will be processed and EFI_INTERRUPT_PENDING will be returned.
> >> +      //
> >> +      if (HandlerType != NULL) {
> >> +        return EFI_INTERRUPT_PENDING;
> >> +      }
> >> +      break;
> >> +
> >> +    case EFI_SUCCESS:
> >> +      //
> >> +      // If at least one of the handlers returns EFI_SUCCESS then the function will return
> >> +      // EFI_SUCCESS. If a handler returns EFI_SUCCESS and HandlerType is not NULL then no
> >> +      // additional handlers will be processed.
> >> +      //
> >> +      if (HandlerType != NULL) {
> >> +        return EFI_SUCCESS;
> >> +      }
> >> +      SuccessReturn = TRUE;
> >> +      break;
> >> +
> >> +    case EFI_WARN_INTERRUPT_SOURCE_QUIESCED:
> >> +      //
> >> +      // If at least one of the handlers returns EFI_WARN_INTERRUPT_SOURCE_QUIESCED
> >> +      // then the function will return EFI_SUCCESS.
> >> +      //
> >> +      SuccessReturn = TRUE;
> >> +      break;
> >> +
> >> +    case EFI_WARN_INTERRUPT_SOURCE_PENDING:
> >> +      //
> >> +      // If all the handlers returned EFI_WARN_INTERRUPT_SOURCE_PENDING
> >> +      // then EFI_WARN_INTERRUPT_SOURCE_PENDING will be returned.
> >> +      //
> >> +      break;
> >> +
> >> +    default:
> >> +      //
> >> +      // Unexpected status code returned.
> >> +      //
> >> +      ASSERT (FALSE);
> >> +      break;
> >> +    }
> >> +  }
> >> +
> >> +  if (SuccessReturn) {
> >> +    Status = EFI_SUCCESS;
> >> +  }
> >> +
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Registers a handler to execute within MM.
> >> +
> >> +  @param  Handler        Handler service funtion pointer.
> >> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
> >> +  @param  DispatchHandle On return, contains a unique handle which can be used to later unregister the handler function.
> >> +
> >> +  @retval EFI_SUCCESS           Handler register success.
> >> +  @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmiHandlerRegister (
> >> +  IN  EFI_MM_HANDLER_ENTRY_POINT    Handler,
> >> +  IN  CONST EFI_GUID                *HandlerType  OPTIONAL,
> >> +  OUT EFI_HANDLE                    *DispatchHandle
> >> +  )
> >> +{
> >> +  MMI_HANDLER  *MmiHandler;
> >> +  MMI_ENTRY    *MmiEntry;
> >> +  LIST_ENTRY   *List;
> >> +
> >> +  if (Handler == NULL || DispatchHandle == NULL) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  MmiHandler = AllocateZeroPool (sizeof (MMI_HANDLER));
> >> +  if (MmiHandler == NULL) {
> >> +    return EFI_OUT_OF_RESOURCES;
> >> +  }
> >> +
> >> +  MmiHandler->Signature = MMI_HANDLER_SIGNATURE;
> >> +  MmiHandler->Handler = Handler;
> >> +
> >> +  if (HandlerType == NULL) {
> >> +    //
> >> +    // This is root MMI handler
> >> +    //
> >> +    MmiEntry = NULL;
> >> +    List = &mRootMmiHandlerList;
> >> +  } else {
> >> +    //
> >> +    // None root MMI handler
> >> +    //
> >> +    MmiEntry = MmCoreFindMmiEntry ((EFI_GUID *) HandlerType, TRUE);
> >> +    if (MmiEntry == NULL) {
> >> +      return EFI_OUT_OF_RESOURCES;
> >> +    }
> >> +
> >> +    List = &MmiEntry->MmiHandlers;
> >> +  }
> >> +
> >> +  MmiHandler->MmiEntry = MmiEntry;
> >> +  InsertTailList (List, &MmiHandler->Link);
> >> +
> >> +  *DispatchHandle = (EFI_HANDLE) MmiHandler;
> >> +
> >> +  return EFI_SUCCESS;
> >> +}
> >> +
> >> +/**
> >> +  Unregister a handler in MM.
> >> +
> >> +  @param  DispatchHandle  The handle that was specified when the handler was registered.
> >> +
> >> +  @retval EFI_SUCCESS           Handler function was successfully unregistered.
> >> +  @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid handle.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmiHandlerUnRegister (
> >> +  IN EFI_HANDLE  DispatchHandle
> >> +  )
> >> +{
> >> +  MMI_HANDLER  *MmiHandler;
> >> +  MMI_ENTRY    *MmiEntry;
> >> +
> >> +  MmiHandler = (MMI_HANDLER *) DispatchHandle;
> >> +
> >> +  if (MmiHandler == NULL) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  if (MmiHandler->Signature != MMI_HANDLER_SIGNATURE) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  MmiEntry = MmiHandler->MmiEntry;
> >> +
> >> +  RemoveEntryList (&MmiHandler->Link);
> >> +  FreePool (MmiHandler);
> >> +
> >> +  if (MmiEntry == NULL) {
> >> +    //
> >> +    // This is root MMI handler
> >> +    //
> >> +    return EFI_SUCCESS;
> >> +  }
> >> +
> >> +  if (IsListEmpty (&MmiEntry->MmiHandlers)) {
> >> +    //
> >> +    // No handler registered for this interrupt now, remove the MMI_ENTRY
> >> +    //
> >> +    RemoveEntryList (&MmiEntry->AllEntries);
> >> +
> >> +    FreePool (MmiEntry);
> >> +  }
> >> +
> >> +  return EFI_SUCCESS;
> >> +}
> >> diff --git a/StandaloneMmPkg/Core/Notify.c b/StandaloneMmPkg/Core/Notify.c
> >> new file mode 100644
> >> index 0000000000..d5fc8f50d1
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Core/Notify.c
> >> @@ -0,0 +1,203 @@
> >> +/** @file
> >> +  Support functions for UEFI protocol notification infrastructure.
> >> +
> >> +  Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
> >> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +  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
> >> +
> >> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +
> >> +**/
> >> +
> >> +#include "StandaloneMmCore.h"
> >> +
> >> +/**
> >> +  Signal event for every protocol in protocol entry.
> >> +
> >> +  @param  Prot                   Protocol interface
> >> +
> >> +**/
> >> +VOID
> >> +MmNotifyProtocol (
> >> +  IN PROTOCOL_INTERFACE  *Prot
> >> +  )
> >> +{
> >> +  PROTOCOL_ENTRY   *ProtEntry;
> >> +  PROTOCOL_NOTIFY  *ProtNotify;
> >> +  LIST_ENTRY       *Link;
> >> +
> >> +  ProtEntry = Prot->Protocol;
> >> +  for (Link=ProtEntry->Notify.ForwardLink; Link != &ProtEntry->Notify; Link=Link->ForwardLink) {
> >> +    ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
> >> +    ProtNotify->Function (&ProtEntry->ProtocolID, Prot->Interface, Prot->Handle);
> >> +  }
> >> +}
> >> +
> >> +/**
> >> +  Removes Protocol from the protocol list (but not the handle list).
> >> +
> >> +  @param  Handle                 The handle to remove protocol on.
> >> +  @param  Protocol               GUID of the protocol to be moved
> >> +  @param  Interface              The interface of the protocol
> >> +
> >> +  @return Protocol Entry
> >> +
> >> +**/
> >> +PROTOCOL_INTERFACE *
> >> +MmRemoveInterfaceFromProtocol (
> >> +  IN IHANDLE   *Handle,
> >> +  IN EFI_GUID  *Protocol,
> >> +  IN VOID      *Interface
> >> +  )
> >> +{
> >> +  PROTOCOL_INTERFACE  *Prot;
> >> +  PROTOCOL_NOTIFY     *ProtNotify;
> >> +  PROTOCOL_ENTRY      *ProtEntry;
> >> +  LIST_ENTRY          *Link;
> >> +
> >> +  Prot = MmFindProtocolInterface (Handle, Protocol, Interface);
> >> +  if (Prot != NULL) {
> >> +
> >> +    ProtEntry = Prot->Protocol;
> >> +
> >> +    //
> >> +    // If there's a protocol notify location pointing to this entry, back it up one
> >> +    //
> >> +    for(Link = ProtEntry->Notify.ForwardLink; Link != &ProtEntry->Notify; Link=Link->ForwardLink) {
> >> +      ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
> >> +
> >> +      if (ProtNotify->Position == &Prot->ByProtocol) {
> >> +        ProtNotify->Position = Prot->ByProtocol.BackLink;
> >> +      }
> >> +    }
> >> +
> >> +    //
> >> +    // Remove the protocol interface entry
> >> +    //
> >> +    RemoveEntryList (&Prot->ByProtocol);
> >> +  }
> >> +
> >> +  return Prot;
> >> +}
> >> +
> >> +/**
> >> +  Add a new protocol notification record for the request protocol.
> >> +
> >> +  @param  Protocol               The requested protocol to add the notify
> >> +                                 registration
> >> +  @param  Function               Points to the notification function
> >> +  @param  Registration           Returns the registration record
> >> +
> >> +  @retval EFI_SUCCESS            Successfully returned the registration record
> >> +                                 that has been added or unhooked
> >> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL or Registration is NULL
> >> +  @retval EFI_OUT_OF_RESOURCES   Not enough memory resource to finish the request
> >> +  @retval EFI_NOT_FOUND          If the registration is not found when Function == NULL
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmRegisterProtocolNotify (
> >> +  IN  CONST EFI_GUID     *Protocol,
> >> +  IN  EFI_MM_NOTIFY_FN  Function,
> >> +  OUT VOID               **Registration
> >> +  )
> >> +{
> >> +  PROTOCOL_ENTRY   *ProtEntry;
> >> +  PROTOCOL_NOTIFY  *ProtNotify;
> >> +  LIST_ENTRY       *Link;
> >> +  EFI_STATUS       Status;
> >> +
> >> +  if (Protocol == NULL || Registration == NULL) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  if (Function == NULL) {
> >> +    //
> >> +    // Get the protocol entry per Protocol
> >> +    //
> >> +    ProtEntry = MmFindProtocolEntry ((EFI_GUID *) Protocol, FALSE);
> >> +    if (ProtEntry != NULL) {
> >> +      ProtNotify = (PROTOCOL_NOTIFY * )*Registration;
> >> +      for (Link = ProtEntry->Notify.ForwardLink;
> >> +           Link != &ProtEntry->Notify;
> >> +           Link = Link->ForwardLink) {
> >> +        //
> >> +        // Compare the notification record
> >> +        //
> >> +        if (ProtNotify == (CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE))){
> >> +          //
> >> +          // If Registration is an existing registration, then unhook it
> >> +          //
> >> +          ProtNotify->Signature = 0;
> >> +          RemoveEntryList (&ProtNotify->Link);
> >> +          FreePool (ProtNotify);
> >> +          return EFI_SUCCESS;
> >> +        }
> >> +      }
> >> +    }
> >> +    //
> >> +    // If the registration is not found
> >> +    //
> >> +    return EFI_NOT_FOUND;
> >> +  }
> >> +
> >> +  ProtNotify = NULL;
> >> +
> >> +  //
> >> +  // Get the protocol entry to add the notification too
> >> +  //
> >> +  ProtEntry = MmFindProtocolEntry ((EFI_GUID *) Protocol, TRUE);
> >> +  if (ProtEntry != NULL) {
> >> +    //
> >> +    // Find whether notification already exist
> >> +    //
> >> +    for (Link = ProtEntry->Notify.ForwardLink;
> >> +         Link != &ProtEntry->Notify;
> >> +         Link = Link->ForwardLink) {
> >> +
> >> +      ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
> >> +      if (CompareGuid (&ProtNotify->Protocol->ProtocolID, Protocol) &&
> >> +          (ProtNotify->Function == Function)) {
> >> +
> >> +        //
> >> +        // Notification already exist
> >> +        //
> >> +        *Registration = ProtNotify;
> >> +
> >> +        return EFI_SUCCESS;
> >> +      }
> >> +    }
> >> +
> >> +    //
> >> +    // Allocate a new notification record
> >> +    //
> >> +    ProtNotify = AllocatePool (sizeof(PROTOCOL_NOTIFY));
> >> +    if (ProtNotify != NULL) {
> >> +      ProtNotify->Signature = PROTOCOL_NOTIFY_SIGNATURE;
> >> +      ProtNotify->Protocol = ProtEntry;
> >> +      ProtNotify->Function = Function;
> >> +      //
> >> +      // Start at the ending
> >> +      //
> >> +      ProtNotify->Position = ProtEntry->Protocols.BackLink;
> >> +
> >> +      InsertTailList (&ProtEntry->Notify, &ProtNotify->Link);
> >> +    }
> >> +  }
> >> +
> >> +  //
> >> +  // Done.  If we have a protocol notify entry, then return it.
> >> +  // Otherwise, we must have run out of resources trying to add one
> >> +  //
> >> +  Status = EFI_OUT_OF_RESOURCES;
> >> +  if (ProtNotify != NULL) {
> >> +    *Registration = ProtNotify;
> >> +    Status = EFI_SUCCESS;
> >> +  }
> >> +  return Status;
> >> +}
> >> diff --git a/StandaloneMmPkg/Core/Page.c b/StandaloneMmPkg/Core/Page.c
> >> new file mode 100644
> >> index 0000000000..ba3e7cea74
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Core/Page.c
> >> @@ -0,0 +1,384 @@
> >> +/** @file
> >> +  MM Memory page management functions.
> >> +
> >> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> >> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +  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
> >> +
> >> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +
> >> +**/
> >> +
> >> +#include "StandaloneMmCore.h"
> >> +
> >> +#define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \
> >> +  ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size)))
> >> +
> >> +#define TRUNCATE_TO_PAGES(a)  ((a) >> EFI_PAGE_SHIFT)
> >> +
> >> +LIST_ENTRY  mMmMemoryMap = INITIALIZE_LIST_HEAD_VARIABLE (mMmMemoryMap);
> >> +
> >> +UINTN mMapKey;
> >> +
> >> +/**
> >> +  Internal Function. Allocate n pages from given free page node.
> >> +
> >> +  @param  Pages                  The free page node.
> >> +  @param  NumberOfPages          Number of pages to be allocated.
> >> +  @param  MaxAddress             Request to allocate memory below this address.
> >> +
> >> +  @return Memory address of allocated pages.
> >> +
> >> +**/
> >> +UINTN
> >> +InternalAllocPagesOnOneNode (
> >> +  IN OUT FREE_PAGE_LIST  *Pages,
> >> +  IN     UINTN           NumberOfPages,
> >> +  IN     UINTN           MaxAddress
> >> +  )
> >> +{
> >> +  UINTN           Top;
> >> +  UINTN           Bottom;
> >> +  FREE_PAGE_LIST  *Node;
> >> +
> >> +  Top = TRUNCATE_TO_PAGES (MaxAddress + 1 - (UINTN)Pages);
> >> +  if (Top > Pages->NumberOfPages) {
> >> +    Top = Pages->NumberOfPages;
> >> +  }
> >> +  Bottom = Top - NumberOfPages;
> >> +
> >> +  if (Top < Pages->NumberOfPages) {
> >> +    Node = (FREE_PAGE_LIST*)((UINTN)Pages + EFI_PAGES_TO_SIZE (Top));
> >> +    Node->NumberOfPages = Pages->NumberOfPages - Top;
> >> +    InsertHeadList (&Pages->Link, &Node->Link);
> >> +  }
> >> +
> >> +  if (Bottom > 0) {
> >> +    Pages->NumberOfPages = Bottom;
> >> +  } else {
> >> +    RemoveEntryList (&Pages->Link);
> >> +  }
> >> +
> >> +  return (UINTN)Pages + EFI_PAGES_TO_SIZE (Bottom);
> >> +}
> >> +
> >> +/**
> >> +  Internal Function. Allocate n pages from free page list below MaxAddress.
> >> +
> >> +  @param  FreePageList           The free page node.
> >> +  @param  NumberOfPages          Number of pages to be allocated.
> >> +  @param  MaxAddress             Request to allocate memory below this address.
> >> +
> >> +  @return Memory address of allocated pages.
> >> +
> >> +**/
> >> +UINTN
> >> +InternalAllocMaxAddress (
> >> +  IN OUT LIST_ENTRY  *FreePageList,
> >> +  IN     UINTN       NumberOfPages,
> >> +  IN     UINTN       MaxAddress
> >> +  )
> >> +{
> >> +  LIST_ENTRY      *Node;
> >> +  FREE_PAGE_LIST  *Pages;
> >> +
> >> +  for (Node = FreePageList->BackLink; Node != FreePageList; Node = Node->BackLink) {
> >> +    Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
> >> +    if (Pages->NumberOfPages >= NumberOfPages &&
> >> +        (UINTN)Pages + EFI_PAGES_TO_SIZE (NumberOfPages) - 1 <= MaxAddress) {
> >> +      return InternalAllocPagesOnOneNode (Pages, NumberOfPages, MaxAddress);
> >> +    }
> >> +  }
> >> +  return (UINTN)(-1);
> >> +}
> >> +
> >> +/**
> >> +  Internal Function. Allocate n pages from free page list at given address.
> >> +
> >> +  @param  FreePageList           The free page node.
> >> +  @param  NumberOfPages          Number of pages to be allocated.
> >> +  @param  MaxAddress             Request to allocate memory below this address.
> >> +
> >> +  @return Memory address of allocated pages.
> >> +
> >> +**/
> >> +UINTN
> >> +InternalAllocAddress (
> >> +  IN OUT LIST_ENTRY  *FreePageList,
> >> +  IN     UINTN       NumberOfPages,
> >> +  IN     UINTN       Address
> >> +  )
> >> +{
> >> +  UINTN           EndAddress;
> >> +  LIST_ENTRY      *Node;
> >> +  FREE_PAGE_LIST  *Pages;
> >> +
> >> +  if ((Address & EFI_PAGE_MASK) != 0) {
> >> +    return ~Address;
> >> +  }
> >> +
> >> +  EndAddress = Address + EFI_PAGES_TO_SIZE (NumberOfPages);
> >> +  for (Node = FreePageList->BackLink; Node!= FreePageList; Node = Node->BackLink) {
> >> +    Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
> >> +    if ((UINTN)Pages <= Address) {
> >> +      if ((UINTN)Pages + EFI_PAGES_TO_SIZE (Pages->NumberOfPages) < EndAddress) {
> >> +        break;
> >> +      }
> >> +      return InternalAllocPagesOnOneNode (Pages, NumberOfPages, EndAddress);
> >> +    }
> >> +  }
> >> +  return ~Address;
> >> +}
> >> +
> >> +/**
> >> +  Allocates pages from the memory map.
> >> +
> >> +  @param  Type                   The type of allocation to perform.
> >> +  @param  MemoryType             The type of memory to turn the allocated pages
> >> +                                 into.
> >> +  @param  NumberOfPages          The number of pages to allocate.
> >> +  @param  Memory                 A pointer to receive the base allocated memory
> >> +                                 address.
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
> >> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
> >> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
> >> +  @retval EFI_SUCCESS            Pages successfully allocated.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmInternalAllocatePages (
> >> +  IN  EFI_ALLOCATE_TYPE     Type,
> >> +  IN  EFI_MEMORY_TYPE       MemoryType,
> >> +  IN  UINTN                 NumberOfPages,
> >> +  OUT EFI_PHYSICAL_ADDRESS  *Memory
> >> +  )
> >> +{
> >> +  UINTN  RequestedAddress;
> >> +
> >> +  if (MemoryType != EfiRuntimeServicesCode &&
> >> +      MemoryType != EfiRuntimeServicesData) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  if (NumberOfPages > TRUNCATE_TO_PAGES ((UINTN)-1) + 1) {
> >> +    return EFI_OUT_OF_RESOURCES;
> >> +  }
> >> +
> >> +  //
> >> +  // We don't track memory type in MM
> >> +  //
> >> +  RequestedAddress = (UINTN)*Memory;
> >> +  switch (Type) {
> >> +    case AllocateAnyPages:
> >> +      RequestedAddress = (UINTN)(-1);
> >> +    case AllocateMaxAddress:
> >> +      *Memory = InternalAllocMaxAddress (
> >> +                  &mMmMemoryMap,
> >> +                  NumberOfPages,
> >> +                  RequestedAddress
> >> +                  );
> >> +      if (*Memory == (UINTN)-1) {
> >> +        return EFI_OUT_OF_RESOURCES;
> >> +      }
> >> +      break;
> >> +    case AllocateAddress:
> >> +      *Memory = InternalAllocAddress (
> >> +                  &mMmMemoryMap,
> >> +                  NumberOfPages,
> >> +                  RequestedAddress
> >> +                  );
> >> +      if (*Memory != RequestedAddress) {
> >> +        return EFI_NOT_FOUND;
> >> +      }
> >> +      break;
> >> +    default:
> >> +      return EFI_INVALID_PARAMETER;
> >> +  }
> >> +  return EFI_SUCCESS;
> >> +}
> >> +
> >> +/**
> >> +  Allocates pages from the memory map.
> >> +
> >> +  @param  Type                   The type of allocation to perform.
> >> +  @param  MemoryType             The type of memory to turn the allocated pages
> >> +                                 into.
> >> +  @param  NumberOfPages          The number of pages to allocate.
> >> +  @param  Memory                 A pointer to receive the base allocated memory
> >> +                                 address.
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
> >> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
> >> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
> >> +  @retval EFI_SUCCESS            Pages successfully allocated.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmAllocatePages (
> >> +  IN  EFI_ALLOCATE_TYPE     Type,
> >> +  IN  EFI_MEMORY_TYPE       MemoryType,
> >> +  IN  UINTN                 NumberOfPages,
> >> +  OUT EFI_PHYSICAL_ADDRESS  *Memory
> >> +  )
> >> +{
> >> +  EFI_STATUS  Status;
> >> +
> >> +  Status = MmInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory);
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Internal Function. Merge two adjacent nodes.
> >> +
> >> +  @param  First             The first of two nodes to merge.
> >> +
> >> +  @return Pointer to node after merge (if success) or pointer to next node (if fail).
> >> +
> >> +**/
> >> +FREE_PAGE_LIST *
> >> +InternalMergeNodes (
> >> +  IN FREE_PAGE_LIST  *First
> >> +  )
> >> +{
> >> +  FREE_PAGE_LIST  *Next;
> >> +
> >> +  Next = BASE_CR (First->Link.ForwardLink, FREE_PAGE_LIST, Link);
> >> +  ASSERT (
> >> +    TRUNCATE_TO_PAGES ((UINTN)Next - (UINTN)First) >= First->NumberOfPages);
> >> +
> >> +  if (TRUNCATE_TO_PAGES ((UINTN)Next - (UINTN)First) == First->NumberOfPages) {
> >> +    First->NumberOfPages += Next->NumberOfPages;
> >> +    RemoveEntryList (&Next->Link);
> >> +    Next = First;
> >> +  }
> >> +  return Next;
> >> +}
> >> +
> >> +/**
> >> +  Frees previous allocated pages.
> >> +
> >> +  @param  Memory                 Base address of memory being freed.
> >> +  @param  NumberOfPages          The number of pages to free.
> >> +
> >> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range.
> >> +  @retval EFI_INVALID_PARAMETER  Address not aligned.
> >> +  @return EFI_SUCCESS            Pages successfully freed.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmInternalFreePages (
> >> +  IN EFI_PHYSICAL_ADDRESS  Memory,
> >> +  IN UINTN                 NumberOfPages
> >> +  )
> >> +{
> >> +  LIST_ENTRY      *Node;
> >> +  FREE_PAGE_LIST  *Pages;
> >> +
> >> +  if ((Memory & EFI_PAGE_MASK) != 0) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  Pages = NULL;
> >> +  Node = mMmMemoryMap.ForwardLink;
> >> +  while (Node != &mMmMemoryMap) {
> >> +    Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
> >> +    if (Memory < (UINTN)Pages) {
> >> +      break;
> >> +    }
> >> +    Node = Node->ForwardLink;
> >> +  }
> >> +
> >> +  if (Node != &mMmMemoryMap &&
> >> +      Memory + EFI_PAGES_TO_SIZE (NumberOfPages) > (UINTN)Pages) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  if (Node->BackLink != &mMmMemoryMap) {
> >> +    Pages = BASE_CR (Node->BackLink, FREE_PAGE_LIST, Link);
> >> +    if ((UINTN)Pages + EFI_PAGES_TO_SIZE (Pages->NumberOfPages) > Memory) {
> >> +      return EFI_INVALID_PARAMETER;
> >> +    }
> >> +  }
> >> +
> >> +  Pages = (FREE_PAGE_LIST*)(UINTN)Memory;
> >> +  Pages->NumberOfPages = NumberOfPages;
> >> +  InsertTailList (Node, &Pages->Link);
> >> +
> >> +  if (Pages->Link.BackLink != &mMmMemoryMap) {
> >> +    Pages = InternalMergeNodes (
> >> +              BASE_CR (Pages->Link.BackLink, FREE_PAGE_LIST, Link)
> >> +              );
> >> +  }
> >> +
> >> +  if (Node != &mMmMemoryMap) {
> >> +    InternalMergeNodes (Pages);
> >> +  }
> >> +
> >> +  return EFI_SUCCESS;
> >> +}
> >> +
> >> +/**
> >> +  Frees previous allocated pages.
> >> +
> >> +  @param  Memory                 Base address of memory being freed.
> >> +  @param  NumberOfPages          The number of pages to free.
> >> +
> >> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range.
> >> +  @retval EFI_INVALID_PARAMETER  Address not aligned.
> >> +  @return EFI_SUCCESS            Pages successfully freed.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmFreePages (
> >> +  IN EFI_PHYSICAL_ADDRESS  Memory,
> >> +  IN UINTN                 NumberOfPages
> >> +  )
> >> +{
> >> +  EFI_STATUS  Status;
> >> +
> >> +  Status = MmInternalFreePages (Memory, NumberOfPages);
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Add free MMRAM region for use by memory service.
> >> +
> >> +  @param  MemBase                Base address of memory region.
> >> +  @param  MemLength              Length of the memory region.
> >> +  @param  Type                   Memory type.
> >> +  @param  Attributes             Memory region state.
> >> +
> >> +**/
> >> +VOID
> >> +MmAddMemoryRegion (
> >> +  IN  EFI_PHYSICAL_ADDRESS  MemBase,
> >> +  IN  UINT64                MemLength,
> >> +  IN  EFI_MEMORY_TYPE       Type,
> >> +  IN  UINT64                Attributes
> >> +  )
> >> +{
> >> +  UINTN  AlignedMemBase;
> >> +
> >> +  //
> >> +  // Do not add memory regions that is already allocated, needs testing, or needs ECC initialization
> >> +  //
> >> +  if ((Attributes & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {
> >> +    return;
> >> +  }
> >> +
> >> +  //
> >> +  // Align range on an EFI_PAGE_SIZE boundary
> >> +  //
> >> +  AlignedMemBase = (UINTN)(MemBase + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
> >> +  MemLength -= AlignedMemBase - MemBase;
> >> +  MmFreePages (AlignedMemBase, TRUNCATE_TO_PAGES ((UINTN)MemLength));
> >> +}
> >> diff --git a/StandaloneMmPkg/Core/Pool.c b/StandaloneMmPkg/Core/Pool.c
> >> new file mode 100644
> >> index 0000000000..bdf1258381
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Core/Pool.c
> >> @@ -0,0 +1,287 @@
> >> +/** @file
> >> +  SMM Memory pool management functions.
> >> +
> >> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> >> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +  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
> >> +
> >> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +
> >> +**/
> >> +
> >> +#include "StandaloneMmCore.h"
> >> +
> >> +LIST_ENTRY  mMmPoolLists[MAX_POOL_INDEX];
> >> +//
> >> +// To cache the MMRAM base since when Loading modules At fixed address feature is enabled,
> >> +// all module is assigned an offset relative the MMRAM base in build time.
> >> +//
> >> +GLOBAL_REMOVE_IF_UNREFERENCED  EFI_PHYSICAL_ADDRESS       gLoadModuleAtFixAddressMmramBase = 0;
> >> +
> >> +/**
> >> +  Called to initialize the memory service.
> >> +
> >> +  @param   MmramRangeCount       Number of MMRAM Regions
> >> +  @param   MmramRanges           Pointer to MMRAM Descriptors
> >> +
> >> +**/
> >> +VOID
> >> +MmInitializeMemoryServices (
> >> +  IN UINTN                 MmramRangeCount,
> >> +  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
> >> +  )
> >> +{
> >> +  UINTN                  Index;
> >> +
> >> +  //
> >> +  // Initialize Pool list
> >> +  //
> >> +  for (Index = sizeof (mMmPoolLists) / sizeof (*mMmPoolLists); Index > 0;) {
> >> +    InitializeListHead (&mMmPoolLists[--Index]);
> >> +  }
> >> +
> >> +
> >> +  //
> >> +  // Initialize free MMRAM regions
> >> +  //
> >> +  for (Index = 0; Index < MmramRangeCount; Index++) {
> >> +    //
> >> +    // BUGBUG: Add legacy MMRAM region is buggy.
> >> +    //
> >> +    if (MmramRanges[Index].CpuStart < BASE_1MB) {
> >> +      continue;
> >> +    }
> >> +    DEBUG ((DEBUG_INFO, "MmAddMemoryRegion %d : 0x%016lx - 0x%016lx\n", Index, MmramRanges[Index].CpuStart, MmramRanges[Index].PhysicalSize));
> >> +    MmAddMemoryRegion (
> >> +      MmramRanges[Index].CpuStart,
> >> +      MmramRanges[Index].PhysicalSize,
> >> +      EfiConventionalMemory,
> >> +      MmramRanges[Index].RegionState
> >> +      );
> >> +  }
> >> +
> >> +}
> >> +
> >> +/**
> >> +  Internal Function. Allocate a pool by specified PoolIndex.
> >> +
> >> +  @param  PoolIndex             Index which indicate the Pool size.
> >> +  @param  FreePoolHdr           The returned Free pool.
> >> +
> >> +  @retval EFI_OUT_OF_RESOURCES   Allocation failed.
> >> +  @retval EFI_SUCCESS            Pool successfully allocated.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +InternalAllocPoolByIndex (
> >> +  IN  UINTN             PoolIndex,
> >> +  OUT FREE_POOL_HEADER  **FreePoolHdr
> >> +  )
> >> +{
> >> +  EFI_STATUS            Status;
> >> +  FREE_POOL_HEADER      *Hdr;
> >> +  EFI_PHYSICAL_ADDRESS  Address;
> >> +
> >> +  ASSERT (PoolIndex <= MAX_POOL_INDEX);
> >> +  Status = EFI_SUCCESS;
> >> +  Hdr = NULL;
> >> +  if (PoolIndex == MAX_POOL_INDEX) {
> >> +    Status = MmInternalAllocatePages (AllocateAnyPages, EfiRuntimeServicesData, EFI_SIZE_TO_PAGES (MAX_POOL_SIZE << 1), &Address);
> >> +    if (EFI_ERROR (Status)) {
> >> +      return EFI_OUT_OF_RESOURCES;
> >> +    }
> >> +    Hdr = (FREE_POOL_HEADER *) (UINTN) Address;
> >> +  } else if (!IsListEmpty (&mMmPoolLists[PoolIndex])) {
> >> +    Hdr = BASE_CR (GetFirstNode (&mMmPoolLists[PoolIndex]), FREE_POOL_HEADER, Link);
> >> +    RemoveEntryList (&Hdr->Link);
> >> +  } else {
> >> +    Status = InternalAllocPoolByIndex (PoolIndex + 1, &Hdr);
> >> +    if (!EFI_ERROR (Status)) {
> >> +      Hdr->Header.Size >>= 1;
> >> +      Hdr->Header.Available = TRUE;
> >> +      InsertHeadList (&mMmPoolLists[PoolIndex], &Hdr->Link);
> >> +      Hdr = (FREE_POOL_HEADER*)((UINT8*)Hdr + Hdr->Header.Size);
> >> +    }
> >> +  }
> >> +
> >> +  if (!EFI_ERROR (Status)) {
> >> +    Hdr->Header.Size = MIN_POOL_SIZE << PoolIndex;
> >> +    Hdr->Header.Available = FALSE;
> >> +  }
> >> +
> >> +  *FreePoolHdr = Hdr;
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Internal Function. Free a pool by specified PoolIndex.
> >> +
> >> +  @param  FreePoolHdr           The pool to free.
> >> +
> >> +  @retval EFI_SUCCESS           Pool successfully freed.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +InternalFreePoolByIndex (
> >> +  IN FREE_POOL_HEADER  *FreePoolHdr
> >> +  )
> >> +{
> >> +  UINTN  PoolIndex;
> >> +
> >> +  ASSERT ((FreePoolHdr->Header.Size & (FreePoolHdr->Header.Size - 1)) == 0);
> >> +  ASSERT (((UINTN)FreePoolHdr & (FreePoolHdr->Header.Size - 1)) == 0);
> >> +  ASSERT (FreePoolHdr->Header.Size >= MIN_POOL_SIZE);
> >> +
> >> +  PoolIndex = (UINTN) (HighBitSet32 ((UINT32)FreePoolHdr->Header.Size) - MIN_POOL_SHIFT);
> >> +  FreePoolHdr->Header.Available = TRUE;
> >> +  ASSERT (PoolIndex < MAX_POOL_INDEX);
> >> +  InsertHeadList (&mMmPoolLists[PoolIndex], &FreePoolHdr->Link);
> >> +  return EFI_SUCCESS;
> >> +}
> >> +
> >> +/**
> >> +  Allocate pool of a particular type.
> >> +
> >> +  @param  PoolType               Type of pool to allocate.
> >> +  @param  Size                   The amount of pool to allocate.
> >> +  @param  Buffer                 The address to return a pointer to the allocated
> >> +                                 pool.
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  PoolType not valid.
> >> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
> >> +  @retval EFI_SUCCESS            Pool successfully allocated.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmInternalAllocatePool (
> >> +  IN   EFI_MEMORY_TYPE  PoolType,
> >> +  IN   UINTN            Size,
> >> +  OUT  VOID             **Buffer
> >> +  )
> >> +{
> >> +  POOL_HEADER           *PoolHdr;
> >> +  FREE_POOL_HEADER      *FreePoolHdr;
> >> +  EFI_STATUS            Status;
> >> +  EFI_PHYSICAL_ADDRESS  Address;
> >> +  UINTN                 PoolIndex;
> >> +
> >> +  if (PoolType != EfiRuntimeServicesCode &&
> >> +      PoolType != EfiRuntimeServicesData) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  Size += sizeof (*PoolHdr);
> >> +  if (Size > MAX_POOL_SIZE) {
> >> +    Size = EFI_SIZE_TO_PAGES (Size);
> >> +    Status = MmInternalAllocatePages (AllocateAnyPages, PoolType, Size, &Address);
> >> +    if (EFI_ERROR (Status)) {
> >> +      return Status;
> >> +    }
> >> +
> >> +    PoolHdr = (POOL_HEADER*)(UINTN)Address;
> >> +    PoolHdr->Size = EFI_PAGES_TO_SIZE (Size);
> >> +    PoolHdr->Available = FALSE;
> >> +    *Buffer = PoolHdr + 1;
> >> +    return Status;
> >> +  }
> >> +
> >> +  Size = (Size + MIN_POOL_SIZE - 1) >> MIN_POOL_SHIFT;
> >> +  PoolIndex = (UINTN) HighBitSet32 ((UINT32)Size);
> >> +  if ((Size & (Size - 1)) != 0) {
> >> +    PoolIndex++;
> >> +  }
> >> +
> >> +  Status = InternalAllocPoolByIndex (PoolIndex, &FreePoolHdr);
> >> +  if (!EFI_ERROR(Status)) {
> >> +    *Buffer = &FreePoolHdr->Header + 1;
> >> +  }
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Allocate pool of a particular type.
> >> +
> >> +  @param  PoolType               Type of pool to allocate.
> >> +  @param  Size                   The amount of pool to allocate.
> >> +  @param  Buffer                 The address to return a pointer to the allocated
> >> +                                 pool.
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  PoolType not valid.
> >> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
> >> +  @retval EFI_SUCCESS            Pool successfully allocated.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmAllocatePool (
> >> +  IN   EFI_MEMORY_TYPE  PoolType,
> >> +  IN   UINTN            Size,
> >> +  OUT  VOID             **Buffer
> >> +  )
> >> +{
> >> +  EFI_STATUS  Status;
> >> +
> >> +  Status = MmInternalAllocatePool (PoolType, Size, Buffer);
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Frees pool.
> >> +
> >> +  @param  Buffer                 The allocated pool entry to free.
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
> >> +  @retval EFI_SUCCESS            Pool successfully freed.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmInternalFreePool (
> >> +  IN VOID  *Buffer
> >> +  )
> >> +{
> >> +  FREE_POOL_HEADER  *FreePoolHdr;
> >> +
> >> +  if (Buffer == NULL) {
> >> +    return EFI_INVALID_PARAMETER;
> >> +  }
> >> +
> >> +  FreePoolHdr = (FREE_POOL_HEADER*)((POOL_HEADER*)Buffer - 1);
> >> +  ASSERT (!FreePoolHdr->Header.Available);
> >> +
> >> +  if (FreePoolHdr->Header.Size > MAX_POOL_SIZE) {
> >> +    ASSERT (((UINTN)FreePoolHdr & EFI_PAGE_MASK) == 0);
> >> +    ASSERT ((FreePoolHdr->Header.Size & EFI_PAGE_MASK) == 0);
> >> +    return MmInternalFreePages (
> >> +             (EFI_PHYSICAL_ADDRESS)(UINTN)FreePoolHdr,
> >> +             EFI_SIZE_TO_PAGES (FreePoolHdr->Header.Size)
> >> +             );
> >> +  }
> >> +  return InternalFreePoolByIndex (FreePoolHdr);
> >> +}
> >> +
> >> +/**
> >> +  Frees pool.
> >> +
> >> +  @param  Buffer                 The allocated pool entry to free.
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
> >> +  @retval EFI_SUCCESS            Pool successfully freed.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmFreePool (
> >> +  IN VOID  *Buffer
> >> +  )
> >> +{
> >> +  EFI_STATUS  Status;
> >> +
> >> +  Status = MmInternalFreePool (Buffer);
> >> +  return Status;
> >> +}
> >> diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.c b/StandaloneMmPkg/Core/StandaloneMmCore.c
> >> new file mode 100644
> >> index 0000000000..0bb99b9710
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Core/StandaloneMmCore.c
> >> @@ -0,0 +1,708 @@
> >> +/** @file
> >> +  MM Core Main Entry Point
> >> +
> >> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> >> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +  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
> >> +
> >> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +
> >> +**/
> >> +
> >> +#include "StandaloneMmCore.h"
> >> +
> >> +EFI_STATUS
> >> +MmCoreFfsFindMmDriver (
> >> +  IN  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader
> >> +  );
> >> +
> >> +EFI_STATUS
> >> +MmDispatcher (
> >> +  VOID
> >> +  );
> >> +
> >> +//
> >> +// Globals used to initialize the protocol
> >> +//
> >> +EFI_HANDLE            mMmCpuHandle = NULL;
> >> +
> >> +//
> >> +// Physical pointer to private structure shared between MM IPL and the MM Core
> >> +//
> >> +MM_CORE_PRIVATE_DATA  *gMmCorePrivate;
> >> +
> >> +//
> >> +// MM Core global variable for MM System Table.  Only accessed as a physical structure in MMRAM.
> >> +//
> >> +EFI_MM_SYSTEM_TABLE  gMmCoreMmst = {
> >> +
> >> +  // The table header for the MMST.
> >> +  {
> >> +    MM_MMST_SIGNATURE,
> >> +    EFI_MM_SYSTEM_TABLE_REVISION,
> >> +    sizeof (gMmCoreMmst.Hdr)
> >> +  },
> >> +  // MmFirmwareVendor
> >> +  NULL,
> >> +  // MmFirmwareRevision
> >> +  0,
> >> +  // MmInstallConfigurationTable
> >> +  MmInstallConfigurationTable,
> >> +  // I/O Service
> >> +  {
> >> +    {
> >> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5,       // MmMemRead
> >> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5        // MmMemWrite
> >> +    },
> >> +    {
> >> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5,       // MmIoRead
> >> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5        // MmIoWrite
> >> +    }
> >> +  },
> >> +  // Runtime memory services
> >> +  MmAllocatePool,
> >> +  MmFreePool,
> >> +  MmAllocatePages,
> >> +  MmFreePages,
> >> +  // MP service
> >> +  NULL,                          // MmStartupThisAp
> >> +  0,                             // CurrentlyExecutingCpu
> >> +  0,                             // NumberOfCpus
> >> +  NULL,                          // CpuSaveStateSize
> >> +  NULL,                          // CpuSaveState
> >> +  0,                             // NumberOfTableEntries
> >> +  NULL,                          // MmConfigurationTable
> >> +  MmInstallProtocolInterface,
> >> +  MmUninstallProtocolInterface,
> >> +  MmHandleProtocol,
> >> +  MmRegisterProtocolNotify,
> >> +  MmLocateHandle,
> >> +  MmLocateProtocol,
> >> +  MmiManage,
> >> +  MmiHandlerRegister,
> >> +  MmiHandlerUnRegister
> >> +};
> >> +
> >> +//
> >> +// Flag to determine if the platform has performed a legacy boot.
> >> +// If this flag is TRUE, then the runtime code and runtime data associated with the
> >> +// MM IPL are converted to free memory, so the MM Core must guarantee that is
> >> +// does not touch of the code/data associated with the MM IPL if this flag is TRUE.
> >> +//
> >> +BOOLEAN  mInLegacyBoot = FALSE;
> >> +
> >> +//
> >> +// Table of MMI Handlers that are registered by the MM Core when it is initialized
> >> +//
> >> +MM_CORE_MMI_HANDLERS  mMmCoreMmiHandlers[] = {
> >> +  { MmFvDispatchHandler,     &gMmFvDispatchGuid,                 NULL, TRUE  },
> >> +  { MmDriverDispatchHandler, &gEfiEventDxeDispatchGuid,          NULL, TRUE  },
> >> +  { MmReadyToLockHandler,    &gEfiDxeMmReadyToLockProtocolGuid,  NULL, TRUE  },
> >> +  { MmEndOfDxeHandler,       &gEfiEndOfDxeEventGroupGuid,        NULL, FALSE },
> >> +  { MmLegacyBootHandler,     &gEfiEventLegacyBootGuid,           NULL, FALSE },
> >> +  { MmExitBootServiceHandler,&gEfiEventExitBootServicesGuid,     NULL, FALSE },
> >> +  { MmReadyToBootHandler,    &gEfiEventReadyToBootGuid,          NULL, FALSE },
> >> +  { NULL,                    NULL,                               NULL, FALSE },
> >> +};
> >> +
> >> +EFI_SYSTEM_TABLE                *mEfiSystemTable;
> >> +UINTN                           mMmramRangeCount;
> >> +EFI_MMRAM_DESCRIPTOR            *mMmramRanges;
> >> +
> >> +/**
> >> +  Place holder function until all the MM System Table Service are available.
> >> +
> >> +  Note: This function is only used by MMRAM invocation.  It is never used by DXE invocation.
> >> +
> >> +  @param  Arg1                   Undefined
> >> +  @param  Arg2                   Undefined
> >> +  @param  Arg3                   Undefined
> >> +  @param  Arg4                   Undefined
> >> +  @param  Arg5                   Undefined
> >> +
> >> +  @return EFI_NOT_AVAILABLE_YET
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmEfiNotAvailableYetArg5 (
> >> +  UINTN Arg1,
> >> +  UINTN Arg2,
> >> +  UINTN Arg3,
> >> +  UINTN Arg4,
> >> +  UINTN Arg5
> >> +  )
> >> +{
> >> +  //
> >> +  // This function should never be executed.  If it does, then the architectural protocols
> >> +  // have not been designed correctly.
> >> +  //
> >> +  return EFI_NOT_AVAILABLE_YET;
> >> +}
> >> +
> >> +/**
> >> +  Software MMI handler that is called when a Legacy Boot event is signaled.  The MM
> >> +  Core uses this signal to know that a Legacy Boot has been performed and that
> >> +  gMmCorePrivate that is shared between the UEFI and MM execution environments can
> >> +  not be accessed from MM anymore since that structure is considered free memory by
> >> +  a legacy OS.
> >> +
> >> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> >> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> >> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> >> +                          be conveyed from a non-MM environment into an MM environment.
> >> +  @param  CommBufferSize  The size of the CommBuffer.
> >> +
> >> +  @return Status Code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmLegacyBootHandler (
> >> +  IN     EFI_HANDLE  DispatchHandle,
> >> +  IN     CONST VOID  *Context,        OPTIONAL
> >> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> >> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> >> +  )
> >> +{
> >> +  EFI_HANDLE  MmHandle;
> >> +  EFI_STATUS  Status = EFI_SUCCESS;
> >> +
> >> +  if (!mInLegacyBoot) {
> >> +    MmHandle = NULL;
> >> +    Status = MmInstallProtocolInterface (
> >> +               &MmHandle,
> >> +               &gEfiEventLegacyBootGuid,
> >> +               EFI_NATIVE_INTERFACE,
> >> +               NULL
> >> +               );
> >> +  }
> >> +  mInLegacyBoot = TRUE;
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Software MMI handler that is called when a ExitBoot Service event is signaled.
> >> +
> >> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> >> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> >> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> >> +                          be conveyed from a non-MM environment into an MM environment.
> >> +  @param  CommBufferSize  The size of the CommBuffer.
> >> +
> >> +  @return Status Code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmExitBootServiceHandler (
> >> +  IN     EFI_HANDLE  DispatchHandle,
> >> +  IN     CONST VOID  *Context,        OPTIONAL
> >> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> >> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> >> +  )
> >> +{
> >> +  EFI_HANDLE  MmHandle;
> >> +  EFI_STATUS  Status = EFI_SUCCESS;
> >> +  STATIC BOOLEAN mInExitBootServices = FALSE;
> >> +
> >> +  if (!mInExitBootServices) {
> >> +    MmHandle = NULL;
> >> +    Status = MmInstallProtocolInterface (
> >> +               &MmHandle,
> >> +               &gEfiEventExitBootServicesGuid,
> >> +               EFI_NATIVE_INTERFACE,
> >> +               NULL
> >> +               );
> >> +  }
> >> +  mInExitBootServices = TRUE;
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Software MMI handler that is called when a ExitBoot Service event is signaled.
> >> +
> >> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> >> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> >> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> >> +                          be conveyed from a non-MM environment into an MM environment.
> >> +  @param  CommBufferSize  The size of the CommBuffer.
> >> +
> >> +  @return Status Code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmReadyToBootHandler (
> >> +  IN     EFI_HANDLE  DispatchHandle,
> >> +  IN     CONST VOID  *Context,        OPTIONAL
> >> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> >> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> >> +  )
> >> +{
> >> +  EFI_HANDLE  MmHandle;
> >> +  EFI_STATUS  Status = EFI_SUCCESS;
> >> +  STATIC BOOLEAN mInReadyToBoot = FALSE;
> >> +
> >> +  if (!mInReadyToBoot) {
> >> +    MmHandle = NULL;
> >> +    Status = MmInstallProtocolInterface (
> >> +               &MmHandle,
> >> +               &gEfiEventReadyToBootGuid,
> >> +               EFI_NATIVE_INTERFACE,
> >> +               NULL
> >> +               );
> >> +  }
> >> +  mInReadyToBoot = TRUE;
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Software MMI handler that is called when the DxeMmReadyToLock protocol is added
> >> +  or if gEfiEventReadyToBootGuid is signaled.  This function unregisters the
> >> +  Software SMIs that are nor required after MMRAM is locked and installs the
> >> +  MM Ready To Lock Protocol so MM Drivers are informed that MMRAM is about
> >> +  to be locked.
> >> +
> >> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> >> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> >> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> >> +                          be conveyed from a non-MM environment into an MM environment.
> >> +  @param  CommBufferSize  The size of the CommBuffer.
> >> +
> >> +  @return Status Code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmReadyToLockHandler (
> >> +  IN     EFI_HANDLE  DispatchHandle,
> >> +  IN     CONST VOID  *Context,        OPTIONAL
> >> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> >> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> >> +  )
> >> +{
> >> +  EFI_STATUS  Status;
> >> +  UINTN       Index;
> >> +  EFI_HANDLE  MmHandle;
> >> +
> >> +  DEBUG ((DEBUG_INFO, "MmReadyToLockHandler\n"));
> >> +
> >> +  //
> >> +  // Unregister MMI Handlers that are no longer required after the MM driver dispatch is stopped
> >> +  //
> >> +  for (Index = 0; mMmCoreMmiHandlers[Index].HandlerType != NULL; Index++) {
> >> +    if (mMmCoreMmiHandlers[Index].UnRegister) {
> >> +      MmiHandlerUnRegister (mMmCoreMmiHandlers[Index].DispatchHandle);
> >> +    }
> >> +  }
> >> +
> >> +  //
> >> +  // Install MM Ready to lock protocol
> >> +  //
> >> +  MmHandle = NULL;
> >> +  Status = MmInstallProtocolInterface (
> >> +             &MmHandle,
> >> +             &gEfiMmReadyToLockProtocolGuid,
> >> +             EFI_NATIVE_INTERFACE,
> >> +             NULL
> >> +             );
> >> +
> >> +  //
> >> +  // Make sure MM CPU I/O 2 Protocol has been installed into the handle database
> >> +  //
> >> +  //Status = MmLocateProtocol (&EFI_MM_CPU_IO_PROTOCOL_GUID, NULL, &Interface);
> >> +
> >> +  //
> >> +  // Print a message on a debug build if the MM CPU I/O 2 Protocol is not installed
> >> +  //
> >> +  //if (EFI_ERROR (Status)) {
> >> +      //DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));
> >> +  //}
> >> +
> >> +
> >> +  //
> >> +  // Assert if the CPU I/O 2 Protocol is not installed
> >> +  //
> >> +  //ASSERT_EFI_ERROR (Status);
> >> +
> >> +  //
> >> +  // Display any drivers that were not dispatched because dependency expression
> >> +  // evaluated to false if this is a debug build
> >> +  //
> >> +  //MmDisplayDiscoveredNotDispatched ();
> >> +
> >> +  return Status;
> >> +}
> >> +
> >> +/**
> >> +  Software MMI handler that is called when the EndOfDxe event is signaled.
> >> +  This function installs the MM EndOfDxe Protocol so MM Drivers are informed that
> >> +  platform code will invoke 3rd part code.
> >> +
> >> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> >> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> >> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> >> +                          be conveyed from a non-MM environment into an MM environment.
> >> +  @param  CommBufferSize  The size of the CommBuffer.
> >> +
> >> +  @return Status Code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmEndOfDxeHandler (
> >> +  IN     EFI_HANDLE  DispatchHandle,
> >> +  IN     CONST VOID  *Context,        OPTIONAL
> >> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> >> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> >> +  )
> >> +{
> >> +  EFI_STATUS  Status;
> >> +  EFI_HANDLE  MmHandle;
> >> +
> >> +  DEBUG ((DEBUG_INFO, "MmEndOfDxeHandler\n"));
> >> +  //
> >> +  // Install MM EndOfDxe protocol
> >> +  //
> >> +  MmHandle = NULL;
> >> +  Status = MmInstallProtocolInterface (
> >> +             &MmHandle,
> >> +             &gEfiMmEndOfDxeProtocolGuid,
> >> +             EFI_NATIVE_INTERFACE,
> >> +             NULL
> >> +             );
> >> +  return Status;
> >> +}
> >> +
> >> +
> >> +
> >> +/**
> >> +  The main entry point to MM Foundation.
> >> +
> >> +  Note: This function is only used by MMRAM invocation.  It is never used by DXE invocation.
> >> +
> >> +  @param  MmEntryContext           Processor information and functionality
> >> +                                    needed by MM Foundation.
> >> +
> >> +**/
> >> +VOID
> >> +EFIAPI
> >> +MmEntryPoint (
> >> +  IN CONST EFI_MM_ENTRY_CONTEXT  *MmEntryContext
> >> +)
> >> +{
> >> +  EFI_STATUS                  Status;
> >> +  EFI_MM_COMMUNICATE_HEADER  *CommunicateHeader;
> >> +  BOOLEAN                     InLegacyBoot;
> >> +
> >> +  DEBUG ((DEBUG_INFO, "MmEntryPoint ...\n"));
> >> +
> >> +  //
> >> +  // Update MMST using the context
> >> +  //
> >> +  CopyMem (&gMmCoreMmst.MmStartupThisAp, MmEntryContext, sizeof (EFI_MM_ENTRY_CONTEXT));
> >> +
> >> +  //
> >> +  // Call platform hook before Mm Dispatch
> >> +  //
> >> +  //PlatformHookBeforeMmDispatch ();
> >> +
> >> +  //
> >> +  // If a legacy boot has occured, then make sure gMmCorePrivate is not accessed
> >> +  //
> >> +  InLegacyBoot = mInLegacyBoot;
> >> +  if (!InLegacyBoot) {
> >> +    //
> >> +    // TBD: Mark the InMm flag as TRUE
> >> +    //
> >> +    gMmCorePrivate->InMm = TRUE;
> >> +
> >> +    //
> >> +    // Check to see if this is a Synchronous MMI sent through the MM Communication
> >> +    // Protocol or an Asynchronous MMI
> >> +    //
> >> +    if (gMmCorePrivate->CommunicationBuffer != 0) {
> >> +      //
> >> +      // Synchronous MMI for MM Core or request from Communicate protocol
> >> +      //
> >> +      if (!MmIsBufferOutsideMmValid ((UINTN)gMmCorePrivate->CommunicationBuffer, gMmCorePrivate->BufferSize)) {
> >> +        //
> >> +        // If CommunicationBuffer is not in valid address scope, return EFI_INVALID_PARAMETER
> >> +        //
> >> +        gMmCorePrivate->CommunicationBuffer = 0;
> >> +        gMmCorePrivate->ReturnStatus = EFI_INVALID_PARAMETER;
> >> +      } else {
> >> +        CommunicateHeader = (EFI_MM_COMMUNICATE_HEADER *)(UINTN)gMmCorePrivate->CommunicationBuffer;
> >> +        gMmCorePrivate->BufferSize -= OFFSET_OF (EFI_MM_COMMUNICATE_HEADER, Data);
> >> +        //DEBUG ((DEBUG_INFO, "CommunicateHeader->HeaderGuid - %g\n", &CommunicateHeader->HeaderGuid));
> >> +        Status = MmiManage (
> >> +                   &CommunicateHeader->HeaderGuid,
> >> +                   NULL,
> >> +                   CommunicateHeader->Data,
> >> +                   (UINTN *)&gMmCorePrivate->BufferSize
> >> +                   );
> >> +        //
> >> +        // Update CommunicationBuffer, BufferSize and ReturnStatus
> >> +        // Communicate service finished, reset the pointer to CommBuffer to NULL
> >> +        //
> >> +        gMmCorePrivate->BufferSize += OFFSET_OF (EFI_MM_COMMUNICATE_HEADER, Data);
> >> +        gMmCorePrivate->CommunicationBuffer = 0;
> >> +        gMmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
> >> +      }
> >> +    }
> >> +  }
> >> +
> >> +  //
> >> +  // Process Asynchronous MMI sources
> >> +  //
> >> +  MmiManage (NULL, NULL, NULL, NULL);
> >> +
> >> +  //
> >> +  // TBD: Do not use private data structure ?
> >> +  //
> >> +
> >> +  //
> >> +  // If a legacy boot has occured, then make sure gMmCorePrivate is not accessed
> >> +  //
> >> +  if (!InLegacyBoot) {
> >> +    //
> >> +    // Clear the InMm flag as we are going to leave MM
> >> +    //
> >> +    gMmCorePrivate->InMm = FALSE;
> >> +  }
> >> +
> >> +  DEBUG ((DEBUG_INFO, "MmEntryPoint Done\n"));
> >> +}
> >> +
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmConfigurationMmNotify (
> >> +  IN CONST EFI_GUID *Protocol,
> >> +  IN VOID           *Interface,
> >> +  IN EFI_HANDLE      Handle
> >> +  )
> >> +{
> >> +  EFI_STATUS                      Status;
> >> +  EFI_MM_CONFIGURATION_PROTOCOL  *MmConfiguration;
> >> +
> >> +  DEBUG ((DEBUG_INFO, "MmConfigurationMmNotify(%g) - %x\n", Protocol, Interface));
> >> +
> >> +  MmConfiguration = Interface;
> >> +
> >> +  //
> >> +  // Register the MM Entry Point provided by the MM Core with the MM COnfiguration protocol
> >> +  //
> >> +  Status = MmConfiguration->RegisterMmEntry (MmConfiguration, (EFI_MM_ENTRY_POINT)(UINTN)gMmCorePrivate->MmEntryPoint);
> >> +  ASSERT_EFI_ERROR (Status);
> >> +
> >> +  //
> >> +  // Set flag to indicate that the MM Entry Point has been registered which
> >> +  // means that MMIs are now fully operational.
> >> +  //
> >> +  gMmCorePrivate->MmEntryPointRegistered = TRUE;
> >> +
> >> +  //
> >> +  // Print debug message showing MM Core entry point address.
> >> +  //
> >> +  DEBUG ((DEBUG_INFO, "MM Core registered MM Entry Point address %p\n", (VOID *)(UINTN)gMmCorePrivate->MmEntryPoint));
> >> +  return EFI_SUCCESS;
> >> +}
> >> +
> >> +UINTN
> >> +GetHobListSize (
> >> +  IN VOID *HobStart
> >> +  )
> >> +{
> >> +  EFI_PEI_HOB_POINTERS  Hob;
> >> +
> >> +  ASSERT (HobStart != NULL);
> >> +
> >> +  Hob.Raw = (UINT8 *) HobStart;
> >> +  while (!END_OF_HOB_LIST (Hob)) {
> >> +    Hob.Raw = GET_NEXT_HOB (Hob);
> >> +  }
> >> +  //
> >> +  // Need plus END_OF_HOB_LIST
> >> +  //
> >> +  return (UINTN)Hob.Raw - (UINTN)HobStart + sizeof(EFI_HOB_GENERIC_HEADER);
> >> +}
> >> +
> >> +/**
> >> +  The Entry Point for MM Core
> >> +
> >> +  Install DXE Protocols and reload MM Core into MMRAM and register MM Core
> >> +  EntryPoint on the MMI vector.
> >> +
> >> +  Note: This function is called for both DXE invocation and MMRAM invocation.
> >> +
> >> +  @param  ImageHandle    The firmware allocated handle for the EFI image.
> >> +  @param  SystemTable    A pointer to the EFI System Table.
> >> +
> >> +  @retval EFI_SUCCESS    The entry point is executed successfully.
> >> +  @retval Other          Some error occurred when executing this entry point.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +StandaloneMmMain (
> >> +  IN VOID  *HobStart
> >> +  )
> >> +{
> >> +  EFI_STATUS                      Status;
> >> +  UINTN                           Index;
> >> +  VOID                            *MmHobStart;
> >> +  UINTN                           HobSize;
> >> +  VOID                            *Registration;
> >> +  EFI_HOB_GUID_TYPE               *GuidHob;
> >> +  MM_CORE_DATA_HOB_DATA           *DataInHob;
> >> +  EFI_HOB_GUID_TYPE               *MmramRangesHob;
> >> +  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
> >> +  EFI_MMRAM_DESCRIPTOR            *MmramRanges;
> >> +  UINT32                          MmramRangeCount;
> >> +  EFI_HOB_FIRMWARE_VOLUME         *BfvHob;
> >> +
> >> +  ProcessLibraryConstructorList (HobStart, &gMmCoreMmst);
> >> +
> >> +  DEBUG ((DEBUG_INFO, "MmMain - 0x%x\n", HobStart));
> >> +
> >> +  //
> >> +  // Determine if the caller has passed a reference to a MM_CORE_PRIVATE_DATA
> >> +  // structure in the Hoblist. This choice will govern how boot information is
> >> +  // extracted later.
> >> +  //
> >> +  GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);
> >> +  if (GuidHob == NULL) {
> >> +    //
> >> +    // Allocate and zero memory for a MM_CORE_PRIVATE_DATA table and then
> >> +    // initialise it
> >> +    //
> >> +    gMmCorePrivate = (MM_CORE_PRIVATE_DATA *) AllocateRuntimePages(EFI_SIZE_TO_PAGES(sizeof(MM_CORE_PRIVATE_DATA)));
> >> +    SetMem ((VOID *)(UINTN)gMmCorePrivate, sizeof(MM_CORE_PRIVATE_DATA), 0);
> >> +    gMmCorePrivate->Signature = MM_CORE_PRIVATE_DATA_SIGNATURE;
> >> +    gMmCorePrivate->MmEntryPointRegistered = FALSE;
> >> +    gMmCorePrivate->InMm = FALSE;
> >> +    gMmCorePrivate->ReturnStatus = EFI_SUCCESS;
> >> +
> >> +    //
> >> +    // Extract the MMRAM ranges from the MMRAM descriptor HOB
> >> +    //
> >> +    MmramRangesHob = GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid, HobStart);
> >> +    if (MmramRangesHob == NULL)
> >> +      return EFI_UNSUPPORTED;
> >> +
> >> +    MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
> >> +    ASSERT (MmramRangesHobData != NULL);
> >> +    MmramRanges = MmramRangesHobData->Descriptor;
> >> +    MmramRangeCount = MmramRangesHobData->NumberOfMmReservedRegions;
> >> +    ASSERT (MmramRanges);
> >> +    ASSERT (MmramRangeCount);
> >> +
> >> +    //
> >> +    // Copy the MMRAM ranges into MM_CORE_PRIVATE_DATA table just in case any
> >> +    // code relies on them being present there
> >> +    //
> >> +    gMmCorePrivate->MmramRangeCount = MmramRangeCount;
> >> +    gMmCorePrivate->MmramRanges = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (MmramRangeCount * sizeof(EFI_MMRAM_DESCRIPTOR));
> >> +    ASSERT (gMmCorePrivate->MmramRanges != 0);
> >> +    CopyMem ((VOID *)(UINTN)gMmCorePrivate->MmramRanges,
> >> +             MmramRanges,
> >> +             MmramRangeCount * sizeof(EFI_MMRAM_DESCRIPTOR));
> >> +  } else {
> >> +    DataInHob       = GET_GUID_HOB_DATA (GuidHob);
> >> +    gMmCorePrivate = (MM_CORE_PRIVATE_DATA *)(UINTN)DataInHob->Address;
> >> +    MmramRanges     = (EFI_MMRAM_DESCRIPTOR *)(UINTN)gMmCorePrivate->MmramRanges;
> >> +    MmramRangeCount = gMmCorePrivate->MmramRangeCount;
> >> +  }
> >> +
> >> +  //
> >> +  // Print the MMRAM ranges passed by the caller
> >> +  //
> >> +  DEBUG ((DEBUG_INFO, "MmramRangeCount - 0x%x\n", MmramRangeCount));
> >> +  for (Index = 0; Index < MmramRangeCount; Index++) {
> >> +          DEBUG ((DEBUG_INFO, "MmramRanges[%d]: 0x%016lx - 0x%lx\n", Index,
> >> +                  MmramRanges[Index].CpuStart,
> >> +                  MmramRanges[Index].PhysicalSize));
> >> +  }
> >> +
> >> +  //
> >> +  // Copy the MMRAM ranges into private MMRAM
> >> +  //
> >> +  mMmramRangeCount = MmramRangeCount;
> >> +  DEBUG ((DEBUG_INFO, "mMmramRangeCount - 0x%x\n", mMmramRangeCount));
> >> +  mMmramRanges = AllocatePool (mMmramRangeCount * sizeof (EFI_MMRAM_DESCRIPTOR));
> >> +  DEBUG ((DEBUG_INFO, "mMmramRanges - 0x%x\n", mMmramRanges));
> >> +  ASSERT (mMmramRanges != NULL);
> >> +  CopyMem (mMmramRanges, (VOID *)(UINTN)MmramRanges, mMmramRangeCount * sizeof (EFI_MMRAM_DESCRIPTOR));
> >> +
> >> +  //
> >> +  // Get Boot Firmware Volume address from the BFV Hob
> >> +  //
> >> +  BfvHob = GetFirstHob (EFI_HOB_TYPE_FV);
> >> +  if (BfvHob != NULL) {
> >> +    DEBUG ((DEBUG_INFO, "BFV address - 0x%x\n", BfvHob->BaseAddress));
> >> +    DEBUG ((DEBUG_INFO, "BFV size    - 0x%x\n", BfvHob->Length));
> >> +    gMmCorePrivate->StandaloneBfvAddress = BfvHob->BaseAddress;
> >> +  }
> >> +
> >> +  gMmCorePrivate->Mmst          = (EFI_PHYSICAL_ADDRESS)(UINTN)&gMmCoreMmst;
> >> +  gMmCorePrivate->MmEntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)MmEntryPoint;
> >> +
> >> +  //
> >> +  // No need to initialize memory service.
> >> +  // It is done in constructor of StandaloneMmCoreMemoryAllocationLib(),
> >> +  // so that the library linked with StandaloneMmCore can use AllocatePool() in constuctor.
> >> +  //
> >> +
> >> +  DEBUG ((DEBUG_INFO, "MmInstallConfigurationTable For HobList\n"));
> >> +  //
> >> +  // Install HobList
> >> +  //
> >> +  HobSize = GetHobListSize (HobStart);
> >> +  DEBUG ((DEBUG_INFO, "HobSize - 0x%x\n", HobSize));
> >> +  MmHobStart = AllocatePool (HobSize);
> >> +  DEBUG ((DEBUG_INFO, "MmHobStart - 0x%x\n", MmHobStart));
> >> +  ASSERT (MmHobStart != NULL);
> >> +  CopyMem (MmHobStart, HobStart, HobSize);
> >> +  Status = MmInstallConfigurationTable (&gMmCoreMmst, &gEfiHobListGuid, MmHobStart, HobSize);
> >> +  ASSERT_EFI_ERROR (Status);
> >> +
> >> +  //
> >> +  // Register notification for EFI_MM_CONFIGURATION_PROTOCOL registration and
> >> +  // use it to register the MM Foundation entrypoint
> >> +  //
> >> +  DEBUG ((DEBUG_INFO, "MmRegisterProtocolNotify - MmConfigurationMmProtocol\n"));
> >> +  Status = MmRegisterProtocolNotify (
> >> +             &gEfiMmConfigurationProtocolGuid,
> >> +             MmConfigurationMmNotify,
> >> +             &Registration
> >> +             );
> >> +  ASSERT_EFI_ERROR (Status);
> >> +
> >> +  //
> >> +  // Dispatch standalone BFV
> >> +  //
> >> +  DEBUG ((DEBUG_INFO, "Mm Dispatch StandaloneBfvAddress - 0x%08x\n", gMmCorePrivate->StandaloneBfvAddress));
> >> +  if (gMmCorePrivate->StandaloneBfvAddress != 0) {
> >> +    MmCoreFfsFindMmDriver ((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)gMmCorePrivate->StandaloneBfvAddress);
> >> +    MmDispatcher ();
> >> +  }
> >> +
> >> +  //
> >> +  // Register all handlers in the core table
> >> +  //
> >> +  for (Index = 0; mMmCoreMmiHandlers[Index].HandlerType != NULL; Index++) {
> >> +    Status = MmiHandlerRegister(mMmCoreMmiHandlers[Index].Handler,
> >> +                                mMmCoreMmiHandlers[Index].HandlerType,
> >> +                                &mMmCoreMmiHandlers[Index].DispatchHandle);
> >> +    DEBUG ((DEBUG_INFO, "MmiHandlerRegister - GUID %g - Status %d\n", mMmCoreMmiHandlers[Index].HandlerType, Status));
> >> +  }
> >> +
> >> +  DEBUG ((DEBUG_INFO, "MmMain Done!\n"));
> >> +
> >> +  return EFI_SUCCESS;
> >> +}
> >> diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.h b/StandaloneMmPkg/Core/StandaloneMmCore.h
> >> new file mode 100644
> >> index 0000000000..53921b7844
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Core/StandaloneMmCore.h
> >> @@ -0,0 +1,903 @@
> >> +/** @file
> >> +  The internal header file includes the common header files, defines
> >> +  internal structure and functions used by MmCore module.
> >> +
> >> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> >> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +
> >> +  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
> >> +
> >> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +
> >> +**/
> >> +
> >> +#ifndef _MM_CORE_H_
> >> +#define _MM_CORE_H_
> >> +
> >> +#include <PiMm.h>
> >> +#include <StandaloneMm.h>
> >> +
> >> +#include <Protocol/DxeMmReadyToLock.h>
> >> +#include <Protocol/MmReadyToLock.h>
> >> +#include <Protocol/MmEndOfDxe.h>
> >> +#include <Protocol/MmCommunication.h>
> >> +#include <Protocol/LoadedImage.h>
> >> +#include <Protocol/MmConfiguration.h>
> >> +
> >> +#include <Guid/Apriori.h>
> >> +#include <Guid/EventGroup.h>
> >> +#include <Guid/EventLegacyBios.h>
> >> +#include <Guid/ZeroGuid.h>
> >> +#include <Guid/MemoryProfile.h>
> >> +#include <Guid/HobList.h>
> >> +#include <Guid/MmFvDispatch.h>
> >> +#include <Guid/MmramMemoryReserve.h>
> >> +
> >> +#include <Library/MmCoreStandaloneEntryPoint.h>
> >> +#include <Library/BaseLib.h>
> >> +#include <Library/BaseMemoryLib.h>
> >> +#include <Library/PeCoffLib.h>
> >> +#include <Library/CacheMaintenanceLib.h>
> >> +#include <Library/DebugLib.h>
> >> +#include <Library/ReportStatusCodeLib.h>
> >> +#include <Library/MemoryAllocationLib.h>
> >> +#include <Library/PcdLib.h>
> >> +//#include <Library/MmCorePlatformHookLib.h>
> >> +#include <Library/MemLib.h>
> >> +#include <Library/HobLib.h>
> >> +
> >> +#include "StandaloneMmCorePrivateData.h"
> >> +
> >> +//
> >> +// Used to build a table of MMI Handlers that the MM Core registers
> >> +//
> >> +typedef struct {
> >> +  EFI_MM_HANDLER_ENTRY_POINT    Handler;
> >> +  EFI_GUID                      *HandlerType;
> >> +  EFI_HANDLE                    DispatchHandle;
> >> +  BOOLEAN                       UnRegister;
> >> +} MM_CORE_MMI_HANDLERS;
> >> +
> >> +//
> >> +// Structure for recording the state of an MM Driver
> >> +//
> >> +#define EFI_MM_DRIVER_ENTRY_SIGNATURE SIGNATURE_32('s', 'd','r','v')
> >> +
> >> +typedef struct {
> >> +  UINTN                           Signature;
> >> +  LIST_ENTRY                      Link;             // mDriverList
> >> +
> >> +  LIST_ENTRY                      ScheduledLink;    // mScheduledQueue
> >> +
> >> +  EFI_HANDLE                      FvHandle;
> >> +  EFI_GUID                        FileName;
> >> +  VOID                            *Pe32Data;
> >> +  UINTN                           Pe32DataSize;
> >> +
> >> +  VOID                            *Depex;
> >> +  UINTN                           DepexSize;
> >> +
> >> +  BOOLEAN                         Before;
> >> +  BOOLEAN                         After;
> >> +  EFI_GUID                        BeforeAfterGuid;
> >> +
> >> +  BOOLEAN                         Dependent;
> >> +  BOOLEAN                         Scheduled;
> >> +  BOOLEAN                         Initialized;
> >> +  BOOLEAN                         DepexProtocolError;
> >> +
> >> +  EFI_HANDLE                      ImageHandle;
> >> +  EFI_LOADED_IMAGE_PROTOCOL       *LoadedImage;
> >> +  //
> >> +  // Image EntryPoint in MMRAM
> >> +  //
> >> +  PHYSICAL_ADDRESS                ImageEntryPoint;
> >> +  //
> >> +  // Image Buffer in MMRAM
> >> +  //
> >> +  PHYSICAL_ADDRESS                ImageBuffer;
> >> +  //
> >> +  // Image Page Number
> >> +  //
> >> +  UINTN                           NumberOfPage;
> >> +} EFI_MM_DRIVER_ENTRY;
> >> +
> >> +#define EFI_HANDLE_SIGNATURE            SIGNATURE_32('h','n','d','l')
> >> +
> >> +///
> >> +/// IHANDLE - contains a list of protocol handles
> >> +///
> >> +typedef struct {
> >> +  UINTN               Signature;
> >> +  /// All handles list of IHANDLE
> >> +  LIST_ENTRY          AllHandles;
> >> +  /// List of PROTOCOL_INTERFACE's for this handle
> >> +  LIST_ENTRY          Protocols;
> >> +  UINTN               LocateRequest;
> >> +} IHANDLE;
> >> +
> >> +#define ASSERT_IS_HANDLE(a)  ASSERT((a)->Signature == EFI_HANDLE_SIGNATURE)
> >> +
> >> +#define PROTOCOL_ENTRY_SIGNATURE        SIGNATURE_32('p','r','t','e')
> >> +
> >> +///
> >> +/// PROTOCOL_ENTRY - each different protocol has 1 entry in the protocol
> >> +/// database.  Each handler that supports this protocol is listed, along
> >> +/// with a list of registered notifies.
> >> +///
> >> +typedef struct {
> >> +  UINTN               Signature;
> >> +  /// Link Entry inserted to mProtocolDatabase
> >> +  LIST_ENTRY          AllEntries;
> >> +  /// ID of the protocol
> >> +  EFI_GUID            ProtocolID;
> >> +  /// All protocol interfaces
> >> +  LIST_ENTRY          Protocols;
> >> +  /// Registerd notification handlers
> >> +  LIST_ENTRY          Notify;
> >> +} PROTOCOL_ENTRY;
> >> +
> >> +#define PROTOCOL_INTERFACE_SIGNATURE  SIGNATURE_32('p','i','f','c')
> >> +
> >> +///
> >> +/// PROTOCOL_INTERFACE - each protocol installed on a handle is tracked
> >> +/// with a protocol interface structure
> >> +///
> >> +typedef struct {
> >> +  UINTN                       Signature;
> >> +  /// Link on IHANDLE.Protocols
> >> +  LIST_ENTRY                  Link;
> >> +  /// Back pointer
> >> +  IHANDLE                     *Handle;
> >> +  /// Link on PROTOCOL_ENTRY.Protocols
> >> +  LIST_ENTRY                  ByProtocol;
> >> +  /// The protocol ID
> >> +  PROTOCOL_ENTRY              *Protocol;
> >> +  /// The interface value
> >> +  VOID                        *Interface;
> >> +} PROTOCOL_INTERFACE;
> >> +
> >> +#define PROTOCOL_NOTIFY_SIGNATURE       SIGNATURE_32('p','r','t','n')
> >> +
> >> +///
> >> +/// PROTOCOL_NOTIFY - used for each register notification for a protocol
> >> +///
> >> +typedef struct {
> >> +  UINTN               Signature;
> >> +  PROTOCOL_ENTRY      *Protocol;
> >> +  /// All notifications for this protocol
> >> +  LIST_ENTRY          Link;
> >> +  /// Notification function
> >> +  EFI_MM_NOTIFY_FN   Function;
> >> +  /// Last position notified
> >> +  LIST_ENTRY          *Position;
> >> +} PROTOCOL_NOTIFY;
> >> +
> >> +//
> >> +// MM Core Global Variables
> >> +//
> >> +extern MM_CORE_PRIVATE_DATA  *gMmCorePrivate;
> >> +extern EFI_MM_SYSTEM_TABLE   gMmCoreMmst;
> >> +extern LIST_ENTRY            gHandleList;
> >> +extern EFI_PHYSICAL_ADDRESS  gLoadModuleAtFixAddressMmramBase;
> >> +
> >> +/**
> >> +  Called to initialize the memory service.
> >> +
> >> +  @param   MmramRangeCount       Number of MMRAM Regions
> >> +  @param   MmramRanges           Pointer to MMRAM Descriptors
> >> +
> >> +**/
> >> +VOID
> >> +MmInitializeMemoryServices (
> >> +  IN UINTN                 MmramRangeCount,
> >> +  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
> >> +  );
> >> +
> >> +/**
> >> +  The MmInstallConfigurationTable() function is used to maintain the list
> >> +  of configuration tables that are stored in the System Management System
> >> +  Table.  The list is stored as an array of (GUID, Pointer) pairs.  The list
> >> +  must be allocated from pool memory with PoolType set to EfiRuntimeServicesData.
> >> +
> >> +  @param  SystemTable      A pointer to the MM System Table (SMST).
> >> +  @param  Guid             A pointer to the GUID for the entry to add, update, or remove.
> >> +  @param  Table            A pointer to the buffer of the table to add.
> >> +  @param  TableSize        The size of the table to install.
> >> +
> >> +  @retval EFI_SUCCESS           The (Guid, Table) pair was added, updated, or removed.
> >> +  @retval EFI_INVALID_PARAMETER Guid is not valid.
> >> +  @retval EFI_NOT_FOUND         An attempt was made to delete a non-existent entry.
> >> +  @retval EFI_OUT_OF_RESOURCES  There is not enough memory available to complete the operation.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmInstallConfigurationTable (
> >> +  IN  CONST EFI_MM_SYSTEM_TABLE  *SystemTable,
> >> +  IN  CONST EFI_GUID              *Guid,
> >> +  IN  VOID                        *Table,
> >> +  IN  UINTN                       TableSize
> >> +  );
> >> +
> >> +/**
> >> +  Wrapper function to MmInstallProtocolInterfaceNotify.  This is the public API which
> >> +  Calls the private one which contains a BOOLEAN parameter for notifications
> >> +
> >> +  @param  UserHandle             The handle to install the protocol handler on,
> >> +                                 or NULL if a new handle is to be allocated
> >> +  @param  Protocol               The protocol to add to the handle
> >> +  @param  InterfaceType          Indicates whether Interface is supplied in
> >> +                                 native form.
> >> +  @param  Interface              The interface for the protocol being added
> >> +
> >> +  @return Status code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmInstallProtocolInterface (
> >> +  IN OUT EFI_HANDLE     *UserHandle,
> >> +  IN EFI_GUID           *Protocol,
> >> +  IN EFI_INTERFACE_TYPE InterfaceType,
> >> +  IN VOID               *Interface
> >> +  );
> >> +
> >> +/**
> >> +  Allocates pages from the memory map.
> >> +
> >> +  @param  Type                   The type of allocation to perform
> >> +  @param  MemoryType             The type of memory to turn the allocated pages
> >> +                                 into
> >> +  @param  NumberOfPages          The number of pages to allocate
> >> +  @param  Memory                 A pointer to receive the base allocated memory
> >> +                                 address
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
> >> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
> >> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
> >> +  @retval EFI_SUCCESS            Pages successfully allocated.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmAllocatePages (
> >> +  IN      EFI_ALLOCATE_TYPE         Type,
> >> +  IN      EFI_MEMORY_TYPE           MemoryType,
> >> +  IN      UINTN                     NumberOfPages,
> >> +  OUT     EFI_PHYSICAL_ADDRESS      *Memory
> >> +  );
> >> +
> >> +/**
> >> +  Allocates pages from the memory map.
> >> +
> >> +  @param  Type                   The type of allocation to perform
> >> +  @param  MemoryType             The type of memory to turn the allocated pages
> >> +                                 into
> >> +  @param  NumberOfPages          The number of pages to allocate
> >> +  @param  Memory                 A pointer to receive the base allocated memory
> >> +                                 address
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
> >> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
> >> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
> >> +  @retval EFI_SUCCESS            Pages successfully allocated.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmInternalAllocatePages (
> >> +  IN      EFI_ALLOCATE_TYPE         Type,
> >> +  IN      EFI_MEMORY_TYPE           MemoryType,
> >> +  IN      UINTN                     NumberOfPages,
> >> +  OUT     EFI_PHYSICAL_ADDRESS      *Memory
> >> +  );
> >> +
> >> +/**
> >> +  Frees previous allocated pages.
> >> +
> >> +  @param  Memory                 Base address of memory being freed
> >> +  @param  NumberOfPages          The number of pages to free
> >> +
> >> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range
> >> +  @retval EFI_INVALID_PARAMETER  Address not aligned, Address is zero or NumberOfPages is zero.
> >> +  @return EFI_SUCCESS            Pages successfully freed.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmFreePages (
> >> +  IN      EFI_PHYSICAL_ADDRESS      Memory,
> >> +  IN      UINTN                     NumberOfPages
> >> +  );
> >> +
> >> +/**
> >> +  Frees previous allocated pages.
> >> +
> >> +  @param  Memory                 Base address of memory being freed
> >> +  @param  NumberOfPages          The number of pages to free
> >> +
> >> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range
> >> +  @retval EFI_INVALID_PARAMETER  Address not aligned, Address is zero or NumberOfPages is zero.
> >> +  @return EFI_SUCCESS            Pages successfully freed.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmInternalFreePages (
> >> +  IN      EFI_PHYSICAL_ADDRESS      Memory,
> >> +  IN      UINTN                     NumberOfPages
> >> +  );
> >> +
> >> +/**
> >> +  Allocate pool of a particular type.
> >> +
> >> +  @param  PoolType               Type of pool to allocate
> >> +  @param  Size                   The amount of pool to allocate
> >> +  @param  Buffer                 The address to return a pointer to the allocated
> >> +                                 pool
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  PoolType not valid
> >> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
> >> +  @retval EFI_SUCCESS            Pool successfully allocated.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmAllocatePool (
> >> +  IN      EFI_MEMORY_TYPE           PoolType,
> >> +  IN      UINTN                     Size,
> >> +  OUT     VOID                      **Buffer
> >> +  );
> >> +
> >> +/**
> >> +  Allocate pool of a particular type.
> >> +
> >> +  @param  PoolType               Type of pool to allocate
> >> +  @param  Size                   The amount of pool to allocate
> >> +  @param  Buffer                 The address to return a pointer to the allocated
> >> +                                 pool
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  PoolType not valid
> >> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
> >> +  @retval EFI_SUCCESS            Pool successfully allocated.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmInternalAllocatePool (
> >> +  IN      EFI_MEMORY_TYPE           PoolType,
> >> +  IN      UINTN                     Size,
> >> +  OUT     VOID                      **Buffer
> >> +  );
> >> +
> >> +/**
> >> +  Frees pool.
> >> +
> >> +  @param  Buffer                 The allocated pool entry to free
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
> >> +  @retval EFI_SUCCESS            Pool successfully freed.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmFreePool (
> >> +  IN      VOID                      *Buffer
> >> +  );
> >> +
> >> +/**
> >> +  Frees pool.
> >> +
> >> +  @param  Buffer                 The allocated pool entry to free
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
> >> +  @retval EFI_SUCCESS            Pool successfully freed.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmInternalFreePool (
> >> +  IN      VOID                      *Buffer
> >> +  );
> >> +
> >> +/**
> >> +  Installs a protocol interface into the boot services environment.
> >> +
> >> +  @param  UserHandle             The handle to install the protocol handler on,
> >> +                                 or NULL if a new handle is to be allocated
> >> +  @param  Protocol               The protocol to add to the handle
> >> +  @param  InterfaceType          Indicates whether Interface is supplied in
> >> +                                 native form.
> >> +  @param  Interface              The interface for the protocol being added
> >> +  @param  Notify                 indicates whether notify the notification list
> >> +                                 for this protocol
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> >> +  @retval EFI_OUT_OF_RESOURCES   No enough buffer to allocate
> >> +  @retval EFI_SUCCESS            Protocol interface successfully installed
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +MmInstallProtocolInterfaceNotify (
> >> +  IN OUT EFI_HANDLE     *UserHandle,
> >> +  IN EFI_GUID           *Protocol,
> >> +  IN EFI_INTERFACE_TYPE InterfaceType,
> >> +  IN VOID               *Interface,
> >> +  IN BOOLEAN            Notify
> >> +  );
> >> +
> >> +/**
> >> +  Uninstalls all instances of a protocol:interfacer from a handle.
> >> +  If the last protocol interface is remove from the handle, the
> >> +  handle is freed.
> >> +
> >> +  @param  UserHandle             The handle to remove the protocol handler from
> >> +  @param  Protocol               The protocol, of protocol:interface, to remove
> >> +  @param  Interface              The interface, of protocol:interface, to remove
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL.
> >> +  @retval EFI_SUCCESS            Protocol interface successfully uninstalled.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmUninstallProtocolInterface (
> >> +  IN EFI_HANDLE       UserHandle,
> >> +  IN EFI_GUID         *Protocol,
> >> +  IN VOID             *Interface
> >> +  );
> >> +
> >> +/**
> >> +  Queries a handle to determine if it supports a specified protocol.
> >> +
> >> +  @param  UserHandle             The handle being queried.
> >> +  @param  Protocol               The published unique identifier of the protocol.
> >> +  @param  Interface              Supplies the address where a pointer to the
> >> +                                 corresponding Protocol Interface is returned.
> >> +
> >> +  @return The requested protocol interface for the handle
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmHandleProtocol (
> >> +  IN EFI_HANDLE       UserHandle,
> >> +  IN EFI_GUID         *Protocol,
> >> +  OUT VOID            **Interface
> >> +  );
> >> +
> >> +/**
> >> +  Add a new protocol notification record for the request protocol.
> >> +
> >> +  @param  Protocol               The requested protocol to add the notify
> >> +                                 registration
> >> +  @param  Function               Points to the notification function
> >> +  @param  Registration           Returns the registration record
> >> +
> >> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> >> +  @retval EFI_SUCCESS            Successfully returned the registration record
> >> +                                 that has been added
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmRegisterProtocolNotify (
> >> +  IN  CONST EFI_GUID              *Protocol,
> >> +  IN  EFI_MM_NOTIFY_FN           Function,
> >> +  OUT VOID                        **Registration
> >> +  );
> >> +
> >> +/**
> >> +  Locates the requested handle(s) and returns them in Buffer.
> >> +
> >> +  @param  SearchType             The type of search to perform to locate the
> >> +                                 handles
> >> +  @param  Protocol               The protocol to search for
> >> +  @param  SearchKey              Dependant on SearchType
> >> +  @param  BufferSize             On input the size of Buffer.  On output the
> >> +                                 size of data returned.
> >> +  @param  Buffer                 The buffer to return the results in
> >> +
> >> +  @retval EFI_BUFFER_TOO_SMALL   Buffer too small, required buffer size is
> >> +                                 returned in BufferSize.
> >> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> >> +  @retval EFI_SUCCESS            Successfully found the requested handle(s) and
> >> +                                 returns them in Buffer.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmLocateHandle (
> >> +  IN EFI_LOCATE_SEARCH_TYPE   SearchType,
> >> +  IN EFI_GUID                 *Protocol   OPTIONAL,
> >> +  IN VOID                     *SearchKey  OPTIONAL,
> >> +  IN OUT UINTN                *BufferSize,
> >> +  OUT EFI_HANDLE              *Buffer
> >> +  );
> >> +
> >> +/**
> >> +  Return the first Protocol Interface that matches the Protocol GUID. If
> >> +  Registration is pasased in return a Protocol Instance that was just add
> >> +  to the system. If Retistration is NULL return the first Protocol Interface
> >> +  you find.
> >> +
> >> +  @param  Protocol               The protocol to search for
> >> +  @param  Registration           Optional Registration Key returned from
> >> +                                 RegisterProtocolNotify()
> >> +  @param  Interface              Return the Protocol interface (instance).
> >> +
> >> +  @retval EFI_SUCCESS            If a valid Interface is returned
> >> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> >> +  @retval EFI_NOT_FOUND          Protocol interface not found
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmLocateProtocol (
> >> +  IN  EFI_GUID  *Protocol,
> >> +  IN  VOID      *Registration OPTIONAL,
> >> +  OUT VOID      **Interface
> >> +  );
> >> +
> >> +/**
> >> +  Manage MMI of a particular type.
> >> +
> >> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
> >> +  @param  Context        Points to an optional context buffer.
> >> +  @param  CommBuffer     Points to the optional communication buffer.
> >> +  @param  CommBufferSize Points to the size of the optional communication buffer.
> >> +
> >> +  @retval EFI_SUCCESS                        Interrupt source was processed successfully but not quiesced.
> >> +  @retval EFI_INTERRUPT_PENDING              One or more MMI sources could not be quiesced.
> >> +  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING  Interrupt source was not handled or quiesced.
> >> +  @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED Interrupt source was handled and quiesced.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmiManage (
> >> +  IN     CONST EFI_GUID           *HandlerType,
> >> +  IN     CONST VOID               *Context         OPTIONAL,
> >> +  IN OUT VOID                     *CommBuffer      OPTIONAL,
> >> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> >> +  );
> >> +
> >> +/**
> >> +  Registers a handler to execute within MM.
> >> +
> >> +  @param  Handler        Handler service funtion pointer.
> >> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
> >> +  @param  DispatchHandle On return, contains a unique handle which can be used to later unregister the handler function.
> >> +
> >> +  @retval EFI_SUCCESS           Handler register success.
> >> +  @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmiHandlerRegister (
> >> +  IN   EFI_MM_HANDLER_ENTRY_POINT     Handler,
> >> +  IN   CONST EFI_GUID                 *HandlerType  OPTIONAL,
> >> +  OUT  EFI_HANDLE                     *DispatchHandle
> >> +  );
> >> +
> >> +/**
> >> +  Unregister a handler in MM.
> >> +
> >> +  @param  DispatchHandle  The handle that was specified when the handler was registered.
> >> +
> >> +  @retval EFI_SUCCESS           Handler function was successfully unregistered.
> >> +  @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid handle.
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmiHandlerUnRegister (
> >> +  IN  EFI_HANDLE                      DispatchHandle
> >> +  );
> >> +
> >> +/**
> >> +  This function is the main entry point for an MM handler dispatch
> >> +  or communicate-based callback.
> >> +
> >> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> >> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> >> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> >> +                          be conveyed from a non-MM environment into an MM environment.
> >> +  @param  CommBufferSize  The size of the CommBuffer.
> >> +
> >> +  @return Status Code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmDriverDispatchHandler (
> >> +  IN     EFI_HANDLE               DispatchHandle,
> >> +  IN     CONST VOID               *Context,        OPTIONAL
> >> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> >> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> >> +  );
> >> +
> >> +/**
> >> +  This function is the main entry point for an MM handler dispatch
> >> +  or communicate-based callback.
> >> +
> >> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> >> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> >> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> >> +                          be conveyed from a non-MM environment into an MM environment.
> >> +  @param  CommBufferSize  The size of the CommBuffer.
> >> +
> >> +  @return Status Code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmFvDispatchHandler (
> >> +  IN     EFI_HANDLE               DispatchHandle,
> >> +  IN     CONST VOID               *Context,        OPTIONAL
> >> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> >> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> >> +  );
> >> +
> >> +/**
> >> +  This function is the main entry point for an MM handler dispatch
> >> +  or communicate-based callback.
> >> +
> >> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> >> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> >> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> >> +                          be conveyed from a non-MM environment into an MM environment.
> >> +  @param  CommBufferSize  The size of the CommBuffer.
> >> +
> >> +  @return Status Code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmLegacyBootHandler (
> >> +  IN     EFI_HANDLE               DispatchHandle,
> >> +  IN     CONST VOID               *Context,        OPTIONAL
> >> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> >> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> >> +  );
> >> +
> >> +/**
> >> +  This function is the main entry point for an MM handler dispatch
> >> +  or communicate-based callback.
> >> +
> >> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> >> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> >> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> >> +                          be conveyed from a non-MM environment into an MM environment.
> >> +  @param  CommBufferSize  The size of the CommBuffer.
> >> +
> >> +  @return Status Code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmExitBootServiceHandler (
> >> +  IN     EFI_HANDLE               DispatchHandle,
> >> +  IN     CONST VOID               *Context,        OPTIONAL
> >> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> >> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> >> +  );
> >> +
> >> +/**
> >> +  This function is the main entry point for an MM handler dispatch
> >> +  or communicate-based callback.
> >> +
> >> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> >> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> >> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> >> +                          be conveyed from a non-MM environment into an MM environment.
> >> +  @param  CommBufferSize  The size of the CommBuffer.
> >> +
> >> +  @return Status Code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmReadyToBootHandler (
> >> +  IN     EFI_HANDLE               DispatchHandle,
> >> +  IN     CONST VOID               *Context,        OPTIONAL
> >> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> >> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> >> +  );
> >> +
> >> +/**
> >> +  This function is the main entry point for an MM handler dispatch
> >> +  or communicate-based callback.
> >> +
> >> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> >> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> >> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> >> +                          be conveyed from a non-MM environment into an MM environment.
> >> +  @param  CommBufferSize  The size of the CommBuffer.
> >> +
> >> +  @return Status Code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmReadyToLockHandler (
> >> +  IN     EFI_HANDLE               DispatchHandle,
> >> +  IN     CONST VOID               *Context,        OPTIONAL
> >> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> >> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> >> +  );
> >> +
> >> +/**
> >> +  This function is the main entry point for an MM handler dispatch
> >> +  or communicate-based callback.
> >> +
> >> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> >> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> >> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> >> +                          be conveyed from a non-MM environment into an MM environment.
> >> +  @param  CommBufferSize  The size of the CommBuffer.
> >> +
> >> +  @return Status Code
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmEndOfDxeHandler (
> >> +  IN     EFI_HANDLE               DispatchHandle,
> >> +  IN     CONST VOID               *Context,        OPTIONAL
> >> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> >> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> >> +  );
> >> +
> >> +/**
> >> +  Place holder function until all the MM System Table Service are available.
> >> +
> >> +  @param  Arg1                   Undefined
> >> +  @param  Arg2                   Undefined
> >> +  @param  Arg3                   Undefined
> >> +  @param  Arg4                   Undefined
> >> +  @param  Arg5                   Undefined
> >> +
> >> +  @return EFI_NOT_AVAILABLE_YET
> >> +
> >> +**/
> >> +EFI_STATUS
> >> +EFIAPI
> >> +MmEfiNotAvailableYetArg5 (
> >> +  UINTN Arg1,
> >> +  UINTN Arg2,
> >> +  UINTN Arg3,
> >> +  UINTN Arg4,
> >> +  UINTN Arg5
> >> +  );
> >> +
> >> +//
> >> +//Functions used during debug buils
> >> +//
> >> +
> >> +/**
> >> +  Traverse the discovered list for any drivers that were discovered but not loaded
> >> +  because the dependency expressions evaluated to false.
> >> +
> >> +**/
> >> +VOID
> >> +MmDisplayDiscoveredNotDispatched (
> >> +  VOID
> >> +  );
> >> +
> >> +/**
> >> +  Add free MMRAM region for use by memory service.
> >> +
> >> +  @param  MemBase                Base address of memory region.
> >> +  @param  MemLength              Length of the memory region.
> >> +  @param  Type                   Memory type.
> >> +  @param  Attributes             Memory region state.
> >> +
> >> +**/
> >> +VOID
> >> +MmAddMemoryRegion (
> >> +  IN      EFI_PHYSICAL_ADDRESS      MemBase,
> >> +  IN      UINT64                    MemLength,
> >> +  IN      EFI_MEMORY_TYPE           Type,
> >> +  IN      UINT64                    Attributes
> >> +  );
> >> +
> >> +/**
> >> +  Finds the protocol entry for the requested protocol.
> >> +
> >> +  @param  Protocol               The ID of the protocol
> >> +  @param  Create                 Create a new entry if not found
> >> +
> >> +  @return Protocol entry
> >> +
> >> +**/
> >> +PROTOCOL_ENTRY  *
> >> +MmFindProtocolEntry (
> >> +  IN EFI_GUID   *Protocol,
> >> +  IN BOOLEAN    Create
> >> +  );
> >> +
> >> +/**
> >> +  Signal event for every protocol in protocol entry.
> >> +
> >> +  @param  Prot                   Protocol interface
> >> +
> >> +**/
> >> +VOID
> >> +MmNotifyProtocol (
> >> +  IN PROTOCOL_INTERFACE   *Prot
> >> +  );
> >> +
> >> +/**
> >> +  Finds the protocol instance for the requested handle and protocol.
> >> +  Note: This function doesn't do parameters checking, it's caller's responsibility
> >> +  to pass in valid parameters.
> >> +
> >> +  @param  Handle                 The handle to search the protocol on
> >> +  @param  Protocol               GUID of the protocol
> >> +  @param  Interface              The interface for the protocol being searched
> >> +
> >> +  @return Protocol instance (NULL: Not found)
> >> +
> >> +**/
> >> +PROTOCOL_INTERFACE *
> >> +MmFindProtocolInterface (
> >> +  IN IHANDLE        *Handle,
> >> +  IN EFI_GUID       *Protocol,
> >> +  IN VOID           *Interface
> >> +  );
> >> +
> >> +/**
> >> +  Removes Protocol from the protocol list (but not the handle list).
> >> +
> >> +  @param  Handle                 The handle to remove protocol on.
> >> +  @param  Protocol               GUID of the protocol to be moved
> >> +  @param  Interface              The interface of the protocol
> >> +
> >> +  @return Protocol Entry
> >> +
> >> +**/
> >> +PROTOCOL_INTERFACE *
> >> +MmRemoveInterfaceFromProtocol (
> >> +  IN IHANDLE        *Handle,
> >> +  IN EFI_GUID       *Protocol,
> >> +  IN VOID           *Interface
> >> +  );
> >> +
> >> +/**
> >> +  This is the POSTFIX version of the dependency evaluator.  This code does
> >> +  not need to handle Before or After, as it is not valid to call this
> >> +  routine in this case. POSTFIX means all the math is done on top of the stack.
> >> +
> >> +  @param  DriverEntry           DriverEntry element to update.
> >> +
> >> +  @retval TRUE                  If driver is ready to run.
> >> +  @retval FALSE                 If driver is not ready to run or some fatal error
> >> +                                was found.
> >> +
> >> +**/
> >> +BOOLEAN
> >> +MmIsSchedulable (
> >> +  IN  EFI_MM_DRIVER_ENTRY   *DriverEntry
> >> +  );
> >> +
> >> +/**
> >> +  Dump MMRAM information.
> >> +
> >> +**/
> >> +VOID
> >> +DumpMmramInfo (
> >> +  VOID
> >> +  );
> >> +
> >> +extern UINTN                    mMmramRangeCount;
> >> +extern EFI_MMRAM_DESCRIPTOR     *mMmramRanges;
> >> +extern EFI_SYSTEM_TABLE         *mEfiSystemTable;
> >> +
> >> +#endif
> >> diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.inf b/StandaloneMmPkg/Core/StandaloneMmCore.inf
> >> new file mode 100644
> >> index 0000000000..c5eaa14ba3
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Core/StandaloneMmCore.inf
> >> @@ -0,0 +1,80 @@
> >> +## @file
> >> +# This module provide an SMM CIS compliant implementation of SMM Core.
> >> +#
> >> +# Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
> >> +# Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +#
> >> +# 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
> >> +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +#
> >> +##
> >> +
> >> +[Defines]
> >> +  INF_VERSION                    = 0x0001001A
> >> +  BASE_NAME                      = StandaloneMmCore
> >> +  FILE_GUID                      = 6E14B6FD-3600-4DD6-A17A-206B3B6DCE16
> >> +  MODULE_TYPE                    = MM_CORE_STANDALONE
> >> +  VERSION_STRING                 = 1.0
> >> +  PI_SPECIFICATION_VERSION       = 0x00010032
> >> +  ENTRY_POINT                    = StandaloneMmMain
> >> +
> >> +#  VALID_ARCHITECTURES           = IA32 X64 AARCH64
> >> +
> >> +[Sources]
> >> +  StandaloneMmCore.c
> >> +  StandaloneMmCore.h
> >> +  StandaloneMmCorePrivateData.h
> >> +  Page.c
> >> +  Pool.c
> >> +  Handle.c
> >> +  Locate.c
> >> +  Notify.c
> >> +  Dependency.c
> >> +  Dispatcher.c
> >> +  Mmi.c
> >> +  InstallConfigurationTable.c
> >> +  FwVol.c
> >> +
> >> +[Packages]
> >> +  MdePkg/MdePkg.dec
> >> +  MdeModulePkg/MdeModulePkg.dec
> >> +  StandaloneMmPkg/StandaloneMmPkg.dec
> >> +
> >> +[LibraryClasses]
> >> +  BaseLib
> >> +  BaseMemoryLib
> >> +  CacheMaintenanceLib
> >> +  DebugLib
> >> +  FvLib
> >> +  HobLib
> >> +  MemoryAllocationLib
> >> +  MemLib
> >> +  PeCoffLib
> >> +  ReportStatusCodeLib
> >> +  StandaloneMmCoreEntryPoint
> >> +
> >> +[Protocols]
> >> +  gEfiDxeMmReadyToLockProtocolGuid             ## UNDEFINED # SmiHandlerRegister
> >> +  gEfiMmReadyToLockProtocolGuid                ## PRODUCES
> >> +  gEfiMmEndOfDxeProtocolGuid                   ## PRODUCES
> >> +  gEfiLoadedImageProtocolGuid                   ## PRODUCES
> >> +  gEfiMmConfigurationProtocolGuid               ## CONSUMES
> >> +
> >> +[Guids]
> >> +  gAprioriGuid                                  ## SOMETIMES_CONSUMES   ## File
> >> +  gEfiEventDxeDispatchGuid                      ## PRODUCES             ## GUID # SmiHandlerRegister
> >> +  gEfiEndOfDxeEventGroupGuid                    ## PRODUCES             ## GUID # SmiHandlerRegister
> >> +  ## SOMETIMES_CONSUMES   ## GUID # Locate protocol
> >> +  ## SOMETIMES_PRODUCES   ## GUID # SmiHandlerRegister
> >> +  gEdkiiMemoryProfileGuid
> >> +  gZeroGuid                                     ## SOMETIMES_CONSUMES   ## GUID
> >> +  gEfiHobListGuid
> >> +  gMmCoreDataHobGuid
> >> +  gMmFvDispatchGuid
> >> +  gEfiEventLegacyBootGuid
> >> +  gEfiEventExitBootServicesGuid
> >> +  gEfiEventReadyToBootGuid
> >> diff --git a/StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h b/StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
> >> new file mode 100644
> >> index 0000000000..faedf3ff2d
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
> >> @@ -0,0 +1,66 @@
> >> +/** @file
> >> +  The internal header file that declared a data structure that is shared
> >> +  between the MM IPL and the MM Core.
> >> +
> >> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> >> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +  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
> >> +
> >> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +
> >> +**/
> >> +
> >> +#ifndef _STANDALONE_MM_CORE_PRIVATE_DATA_H_
> >> +#define _STANDALONE_MM_CORE_PRIVATE_DATA_H_
> >> +
> >> +#include <Guid/MmCoreData.h>
> >> +
> >> +//
> >> +// Page management
> >> +//
> >> +
> >> +typedef struct {
> >> +  LIST_ENTRY  Link;
> >> +  UINTN       NumberOfPages;
> >> +} FREE_PAGE_LIST;
> >> +
> >> +extern LIST_ENTRY  mMmMemoryMap;
> >> +
> >> +//
> >> +// Pool management
> >> +//
> >> +
> >> +//
> >> +// MIN_POOL_SHIFT must not be less than 5
> >> +//
> >> +#define MIN_POOL_SHIFT  6
> >> +#define MIN_POOL_SIZE   (1 << MIN_POOL_SHIFT)
> >> +
> >> +//
> >> +// MAX_POOL_SHIFT must not be less than EFI_PAGE_SHIFT - 1
> >> +//
> >> +#define MAX_POOL_SHIFT  (EFI_PAGE_SHIFT - 1)
> >> +#define MAX_POOL_SIZE   (1 << MAX_POOL_SHIFT)
> >> +
> >> +//
> >> +// MAX_POOL_INDEX are calculated by maximum and minimum pool sizes
> >> +//
> >> +#define MAX_POOL_INDEX  (MAX_POOL_SHIFT - MIN_POOL_SHIFT + 1)
> >> +
> >> +typedef struct {
> >> +  UINTN        Size;
> >> +  BOOLEAN      Available;
> >> +} POOL_HEADER;
> >> +
> >> +typedef struct {
> >> +  POOL_HEADER  Header;
> >> +  LIST_ENTRY   Link;
> >> +} FREE_POOL_HEADER;
> >> +
> >> +extern LIST_ENTRY  mMmPoolLists[MAX_POOL_INDEX];
> >> +
> >> +#endif
> >> diff --git a/StandaloneMmPkg/Include/Guid/MmFvDispatch.h b/StandaloneMmPkg/Include/Guid/MmFvDispatch.h
> >> new file mode 100644
> >> index 0000000000..fb194d3474
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Include/Guid/MmFvDispatch.h
> >> @@ -0,0 +1,38 @@
> >> +/** @file
> >> +  GUIDs for MM Event.
> >> +
> >> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> >> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +
> >> +This program and the accompanying materials are licensed and made available under
> >> +the terms and conditions of the BSD License that accompanies this distribution.
> >> +The full text of the license may be found at
> >> +http://opensource.org/licenses/bsd-license.php.
> >> +
> >> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +
> >> +**/
> >> +
> >> +#ifndef __MM_FV_DISPATCH_H__
> >> +#define __MM_FV_DISPATCH_H__
> >> +
> >> +#define MM_FV_DISPATCH_GUID \
> >> +  { 0xb65694cc, 0x9e3, 0x4c3b, { 0xb5, 0xcd, 0x5, 0xf4, 0x4d, 0x3c, 0xdb, 0xff }}
> >> +
> >> +extern EFI_GUID gMmFvDispatchGuid;
> >> +
> >> +#pragma pack(1)
> >> +typedef struct {
> >> +  EFI_PHYSICAL_ADDRESS  Address;
> >> +  UINT64                Size;
> >> +} EFI_MM_COMMUNICATE_FV_DISPATCH_DATA;
> >> +
> >> +typedef struct {
> >> +  EFI_GUID                              HeaderGuid;
> >> +  UINTN                                 MessageLength;
> >> +  EFI_MM_COMMUNICATE_FV_DISPATCH_DATA   Data;
> >> +} EFI_MM_COMMUNICATE_FV_DISPATCH;
> >> +#pragma pack()
> >> +
> >> +#endif
> >> diff --git a/StandaloneMmPkg/Include/StandaloneMm.h b/StandaloneMmPkg/Include/StandaloneMm.h
> >> new file mode 100644
> >> index 0000000000..0e420315bb
> >> --- /dev/null
> >> +++ b/StandaloneMmPkg/Include/StandaloneMm.h
> >> @@ -0,0 +1,36 @@
> >> +/** @file
> >> +  Standalone MM.
> >> +
> >> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> >> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> >> +
> >> +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
> >> +
> >> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> >> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> >> +
> >> +**/
> >> +
> >> +#ifndef _STANDALONE_MM_H_
> >> +#define _STANDALONE_MM_H_
> >> +
> >> +#include <PiMm.h>
> >> +
> >> +typedef
> >> +EFI_STATUS
> >> +(EFIAPI *MM_IMAGE_ENTRY_POINT) (
> >> +  IN EFI_HANDLE            ImageHandle,
> >> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
> >> +  );
> >> +
> >> +typedef
> >> +EFI_STATUS
> >> +(EFIAPI *STANDALONE_MM_FOUNDATION_ENTRY_POINT) (
> >> +  IN VOID  *HobStart
> >> +  );
> >> +
> >> +#endif
> >> --
> >> 2.16.2
> >>


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 13/18] StandaloneMmPkg/Core: Implementation of Standalone MM Core Module.
  2018-04-30 19:28     ` Ard Biesheuvel
  2018-04-30 20:17       ` Achin Gupta
@ 2018-05-01  8:18       ` Laszlo Ersek
  1 sibling, 0 replies; 70+ messages in thread
From: Laszlo Ersek @ 2018-05-01  8:18 UTC (permalink / raw)
  To: Ard Biesheuvel, Achin Gupta, Kinney, Michael D, Leif Lindholm,
	Andrew Fish
  Cc: Supreeth Venkatesh, edk2-devel@lists.01.org, Gao, Liming,
	Yao, Jiewen, nd

On 04/30/18 21:28, Ard Biesheuvel wrote:
> On 30 April 2018 at 21:19, Achin Gupta <achin.gupta@arm.com> wrote:
>> Hi Supreeth,
>>
>> I think it is worth adding a signed off by Jiewen since he originally
>> contributed the code and it has not changed much since.
> 
> I disagree. A signoff does not assert authorship, it only means that
> the contributor asserts that the license permits him to contribute
> this code under the tianocore contribution agreement. Adding a signoff
> on behalf of someone else should be avoided in my opinion, because it
> suggests that code can only be contributed by the original author.
> Also, even if the author made the code available under a compatible
> license, it does not mean he subscribes to the Tianocore contribution
> agreement, and adding a signoff on behalf of someone else does imply
> that (although this should not be a problem in this particular case)
> 
> Anyone can contribute code that is available under a compatible
> license, and it is not generally possible to decide who should be
> credited as authors for code that originates in other projects.
> 
> If you want to credit the author, you can do that in the commit log.

Right, this is consistent with Ard's earlier explanation of the subject
-- it's something that's proved surprisingly difficult for me to
remember as well. Thankfully Ard keeps us in check :)

Laszlo


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 01/18] ArmPkg: Add PCDs needed for MM communication driver.
       [not found]     ` <AM4PR0802MB23063743A3B2F5A552BE320580870@AM4PR0802MB2306.eurprd08.prod.outlook.com>
@ 2018-05-04 23:13       ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:13 UTC (permalink / raw)
  To: Achin Gupta; +Cc: edk2-devel@lists.01.org


> -----Original Message-----
> From: Achin Gupta 
> Sent: Wednesday, April 11, 2018 9:43 AM
> To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
> Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@i
> ntel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheu
> vel@linaro.org; nd <nd@arm.com>
> Subject: Re: [PATCH v1 01/18] ArmPkg: Add PCDs needed for MM
> communication driver.
> 
> On Fri, Apr 06, 2018 at 03:42:06PM +0100, Supreeth Venkatesh wrote:
> > 
> > This patch defines PCDs to describe the base address and size of 
> > communication buffer between normal world (uefi) and standalone MM 
> > environment in the secure world.
> > 
> > Contributed-under: TianoCore Contribution Agreement 1.1
> > Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> > Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> > ---
> >  ArmPkg/ArmPkg.dec | 3 +++
> >  1 file changed, 3 insertions(+)
> > 
> > diff --git a/ArmPkg/ArmPkg.dec b/ArmPkg/ArmPkg.dec index 
> > a55b6268ff..b64942220b 100644
> > --- a/ArmPkg/ArmPkg.dec
> > +++ b/ArmPkg/ArmPkg.dec
> > @@ -223,6 +223,9 @@
> >    gArmTokenSpaceGuid.PcdSystemMemoryBase|0|UINT64|0x00000029
> >    gArmTokenSpaceGuid.PcdSystemMemorySize|0|UINT64|0x0000002A
> >  
> > +  gArmTokenSpaceGuid.PcdMmBufferBase|0|UINT64|0x00000045
> > +  gArmTokenSpaceGuid.PcdMmBufferSize|0|UINT64|0x00000046
> > +
> >  [PcdsFixedAtBuild.common, PcdsDynamic.common]
> >    #
> >    # ARM Architectural Timer
> > --
> > 2.16.2
> > 
> Acked-by: Achin Gupta <achin.gupta@arm.com>
 Thanks.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms ***
  2018-04-08  6:01 ` [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Yao, Jiewen
@ 2018-05-04 23:15   ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:15 UTC (permalink / raw)
  To: Yao, Jiewen, edk2-devel@lists.01.org
  Cc: Kinney, Michael D, Gao, Liming, Achin Gupta,
	leif.lindholm@linaro.org, ard.biesheuvel@linaro.org

My response inline.

-----Original Message-----
From: Yao, Jiewen <jiewen.yao@intel.com>
Sent: Sunday, April 8, 2018 1:02 AM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>; edk2-devel@lists.01.org
Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming <liming.gao@intel.com>; Achin Gupta <Achin.Gupta@arm.com>; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org
Subject: RE: [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms ***

Thank you for this great work.

Comment below:

1) I don't have comment for ArmPkg update (0001~0005, 0015) - I expect Arm expert will help on that. :-)

2) BaseTool (0016), reviewed-by: Jiewen.yao@intel.com

3) StandaloneMmPkg
3.1) 0007 - I think we need add ExHeader check and alignment enforcement for FfsFileHeader.

    FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FwVolHeader + FwVolHeader->HeaderLength);

You may refer to https://github.com/tianocore/edk2/blob/master/SignedCapsulePkg/Library/EdkiiSystemCapsuleLib/EdkiiSystemCapsuleLib.c GetFfsByName()

    if (FvHeader->ExtHeaderOffset != 0) {
      FvExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *)((UINT8 *)FvHeader + FvHeader->ExtHeaderOffset);
      FfsHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FvExtHeader + FvExtHeader->ExtHeaderSize);
    } else {
      FfsHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FvHeader + FvHeader->HeaderLength);
    }
    FfsHeader = (EFI_FFS_FILE_HEADER *)((UINTN)FvHeader + ALIGN_VALUE((UINTN)FfsHeader - (UINTN)FvHeader, 8));


[Supreeth] Ok. Done. Thanks. Please see version 2.

3.2) 0008 - I think we need rename MmCopyMemToSmram to MmCopyMemToMmram, and MmCopyMemFromSmram to MmCopyMemFromMmram.
Also if this MemLib is only for MM_STANDALONE, we should name it StandaloneMmMemLib.
[Supreeth] Ok. Done. Thanks. Please see version 2.

3.3) 0009 - If this MemoryAllocationLib is only for MM_CORE_STANDALONE, we should name it StandaloneMmCoreMemoryAllocationLib.
Also do we need StandaloneMmMemoryAllocationLib (non-Core version) for MmDriver ?
(You may find that my POC zip has both StandaloneSmmCoreMemoryAllocationLib and StandaloneSmmMemoryAllocationLib.)
[Supreeth] Renaming Done. In Arm Reference code, there is no sample MM driver which uses this.
Hence it can be added whenever a sample MM driver which uses this library will be up-streamed.

3.4) 0010 - If this HobLib is only for MM_STANDALONE, we should name it StandaloneMmHobLib.
[Supreeth] Ok. Done. Thanks. Please see version 2.

3.5) 0014 - I do not believe we should add FDF file here. StandaloneMmPkg should only provide the component. (such as MdeModulePkg). The FDF file should be in a real project.
[Supreeth] Ok. Thanks. I have moved into edk2-platforms where it belongs rightfully. Please see version 2.

3.6) 0017/0018 - they are only for unit test. I do not think we should check in them. Please discard them when you check in the series.
[Supreeth] Ok. Thanks. I will not be sending these patches with version 2.

3.x) 0006/0011/0012/0013, they are good so far. We may do some enhancement to move common logic out of Arm directory. But that can be done later, when we add X86 content there. reviewed-by: Jiewen.yao@intel.com
[Supreeth] Thanks.

Thank you
Yao Jiewen



> -----Original Message-----
> From: Supreeth Venkatesh [mailto:supreeth.venkatesh@arm.com]
> Sent: Friday, April 6, 2018 10:42 PM
> To: edk2-devel@lists.01.org
> Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming
> <liming.gao@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>;
> achin.gupta@arm.com; leif.lindholm@linaro.org;
> ard.biesheuvel@linaro.org; Supreeth Venkatesh
> <supreeth.venkatesh@arm.com>
> Subject: [PATCH v1 00/18] *** Standalone Management Mode Core
> Interface for AARCH64 Platforms ***
>
> ***
> PI Specification v1.5  "Volume 4: Management Mode Core Interface"
> introduces the concept of MM Standalone Mode. Initialization of this
> mode can be done during the SEC phase (Section 1.5.2).
> On ARMv8-A systems, ARM Trusted Firmware is responsible for launching
> the normal world firmware e.g. UEFI.
>
> The Standalone MM environment is instantiated in Secure EL0 as a
> separate firmware volume. It executes as BL32 Image under control of
> ARM TF which is instantiated in EL3. Both components execute in the
> AArch64 execution state.
> This patchset will build upon the StandaloneSmmPkg module originally
> contributed by Intel.
>
> This package can be used in conjunction with ARM Trusted Firmware to
> recreate a simple MM secure environment that demonstrates
> communication between two UEFI images where one is executing in the
> normal world and the other is executing in the secure world.
>
> The normal world image includes:
> MM Communication DXE runtime driver that implements the protocol for
> communication with the MM environment in the secure world.
>
> The secure world image includes:
> The MM Standalone framework.
>
> This patchset includes the proposed organization/structure.
> In order to be able to review the changes more effectively, the
> changes are present here:
> https://github.com/supven01/edk2.git (Branch: master)
>
> Steps to build MM Standalone images
> In user preferred "work" directory, execute the following shell
> commands
>
> git clone https://github.com/tianocore/edk2.git
> git checkout master
>
> git clone https://github.com/tianocore/edk2-platforms.git
> git checkout master
>
> mkdir arm-tf
> cd arm-tf
> git clone https://github.com/ARM-software/arm-trusted-firmware.git .
> git checkout master
> cd ..
>
> git clone https://git.linaro.org/uefi/uefi-tools.git .
> git checkout master
>
> The following will build the MM Standalone image which runs in secure world.
> ./uefi-tools/edk2-build.sh -b DEBUG fvp_mm_standalone
>
> The follwing will build the normal world UEFI image, ARM Trusted
> Firmware and a Firmware Image Package (FIP) that includes both the UEFI images.
> ./uefi-tools/edk2-build.sh -a ./arm-tf -b DEBUG fvp_mm_normal
>
> Boot Loader Stage 1 (BL1) binary and combined arm-tf/uefi firmware
> image package (fip) binary will be generated at:
>
> Build Output
> Build/ArmVExpress-FVP-AArch64-MM-Normal/DEBUG_GCC5/FV/bl1.bin
> Build/ArmVExpress-FVP-AArch64-MM-Normal/DEBUG_GCC5/FV/fip.bin
>
> Steps to run MM Standalone image
> 1. Download the ARMv8 Architecture FVP from
>
> https://silver.arm.com/download/download.tm?pv=3744408&p=1424570
>      For more information, please refer
>
> https://developer.arm.com/products/system-design/fixed-virtual-platfor
> ms 2.  Install FVP into preferred "work" directory.
> 3.  Create a shell script "run_mm.sh" in the same folder where
> "FVP_Base_AEMv8A-AEMv8A" is present.
>      Sample Shell script below:
>     ./FVP_Base_AEMv8A-AEMv8A
>     -C cache_state_modelled=0
>     -C bp.secure_memory=1
>     -C bp.tzc_400.diagnostics=1
>     -C bp.pl011_uart0.untimed_fifos=0
>     -C cluster1.NUM_CORES=4
>     -C cluster0.NUM_CORES=4
>     -C bp.pl011_uart0.out_file=uart0.output
>     -C bp.pl011_uart1.out_file=uart1.output
>     -C bp.pl011_uart2.out_file=uart2.output
>     -C bp.pl011_uart3.out_file=uart3.output
>     -C bp.secureflashloader.fname=""
>     -C bp.flashloader0.fname=""
>     -S -R
> 4. ./run_mm.sh
> 5. Output can be seen on FVP console.
> 6. The normal world will boot to the UEFI shell.
>
> Sample Output
>
> MM Standalone Output (FVP UART2)
> SPM Version: Major=0x0, Minor=0x1
> NumSpMemRegions - 0x6
> SpMemBase       - 0xFF200000
> SpMemLimit      - 0x100000000
> SpImageBase     - 0xFF200000
> SpStackBase     - 0xFF610000
> SpHeapBase      - 0xFF620000
> SpNsCommBufBase - 0xFF600000
> SpSharedBufBase - 0xFF500000
> SpImageSize     - 0x300000
> SpPcpuStackSize - 0x2000
> SpHeapSize      - 0x9E0000
> SpNsCommBufSize - 0x10000
> SpPcpuSharedBufSize - 0x20000
> NumCpus         - 0x8
> CpuInfo         - 0xFF500680
> Mpidr           - 0x80000000
> LinearId        - 0x0
> Flags           - 0x1
> Mpidr           - 0x80000001
> LinearId        - 0x1
> Flags           - 0x0
> Mpidr           - 0x80000002
> LinearId        - 0x2
> Flags           - 0x0
> Mpidr           - 0x80000003
> LinearId        - 0x3
> Flags           - 0x0
> Mpidr           - 0x80000100
> LinearId        - 0x4
> Flags           - 0x0
> Mpidr           - 0x80000101
> LinearId        - 0x5
> Flags           - 0x0
> Mpidr           - 0x80000102
> LinearId        - 0x6
> Flags           - 0x0
> Mpidr           - 0x80000103
> LinearId        - 0x7
> Flags           - 0x0
> Found Standalone MM PE data - 0xFF201000 Found Standalone MM PE data -
> 0xFF201000 Standalone MM Core PE-COFF SectionHeaderOffset - 0xF60,
> NumberOfSections
> - 3
> UpdateMmFoundationPeCoffPermissions: Section 0 of image at 0xFF201000
> has
> 0x60000020 permissions
> UpdateMmFoundationPeCoffPermissions: Section 0 of image at 0xFF201000
> has .et name
> UpdateMmFoundationPeCoffPermissions: Section 0 of image at 0xFF201000
> has
> 0xFF202000 address
> UpdateMmFoundationPeCoffPermissions: Section 0 of image at 0xFF201000
> has
> 0x1000 data
> UpdateMmFoundationPeCoffPermissions: Ignoring section 0 of image at
> 0xFF201000 with 0x60000020 permissions
> UpdateMmFoundationPeCoffPermissions: Section 1 of image at 0xFF201000
> has
> 0xC0000040 permissions
> UpdateMmFoundationPeCoffPermissions: Section 1 of image at 0xFF201000
> has .aa name
> UpdateMmFoundationPeCoffPermissions: Section 1 of image at 0xFF201000
> has
> 0xFF217000 address
> UpdateMmFoundationPeCoffPermissions: Section 1 of image at 0xFF201000
> has
> 0x16000 data
> UpdateMmFoundationPeCoffPermissions: Mapping section 1 of image at
> 0xFF201000 with RW-XN permissions
> UpdateMmFoundationPeCoffPermissions: Section 2 of image at 0xFF201000
> has
> 0x42000040 permissions
> UpdateMmFoundationPeCoffPermissions: Section 2 of image at 0xFF201000
> has .eo name
> UpdateMmFoundationPeCoffPermissions: Section 2 of image at 0xFF201000
> has
> 0xFF218000 address
> UpdateMmFoundationPeCoffPermissions: Section 2 of image at 0xFF201000
> has
> 0x17000 data
> UpdateMmFoundationPeCoffPermissions: Mapping section 2 of image at
> 0xFF201000 with RO-XN permissions
> StandaloneMmCoreMemoryAllocationLibConstructor - 0xFF620000
> MmramRangeCount - 0x6
> MmramRanges[0]: 0x00000000FF200000 - 0x0000000000300000
> MmramRanges[1]: 0x00000000FF500000 - 0x0000000000100000
> MmramRanges[2]: 0x00000000FF600000 - 0x0000000000010000
> MmramRanges[3]: 0x00000000FF610000 - 0x0000000000010000
> MmramRanges[4]: 0x00000000FF620000 - 0x00000000000002C8
> MmramRanges[5]: 0x00000000FF6202C8 - 0x00000000009DFD38
> MmInitializeMemoryServices MmAddMemoryRegion 0 : 0x00000000FF200000 -
> 0x0000000000300000 MmAddMemoryRegion 1 : 0x00000000FF500000 -
> 0x0000000000100000 MmAddMemoryRegion 2 : 0x00000000FF600000 -
> 0x0000000000010000 MmAddMemoryRegion 3 : 0x00000000FF610000 -
> 0x0000000000010000 MmAddMemoryRegion 4 : 0x00000000FF620000 -
> 0x00000000000002C8 MmAddMemoryRegion 5 : 0x00000000FF6202C8 -
> 0x00000000009DFD38 mMmMemLibInternalMaximumSupportAddress =
> 0xFFFFFFFFF MmMain - 0xFF620000 MmramRangeCount - 0x6
> MmramRanges[0]: 0x00000000FF200000 - 0x300000
> MmramRanges[1]: 0x00000000FF500000 - 0x100000
> MmramRanges[2]: 0x00000000FF600000 - 0x10000
> MmramRanges[3]: 0x00000000FF610000 - 0x10000
> MmramRanges[4]: 0x00000000FF620000 - 0x2C8
> MmramRanges[5]: 0x00000000FF6202C8 - 0x9DFD38 mMmramRangeCount - 0x6
> mMmramRanges - 0xFFFFEE10 BFV address - 0xFF200000
> BFV size    - 0x300000
> MmInstallConfigurationTable For HobList HobSize - 0x2C8 MmHobStart -
> 0xFFFFE810 MmRegisterProtocolNotify - MmConfigurationMmProtocol Mm
> Dispatch StandaloneBfvAddress - 0xFF200000 MmCoreFfsFindMmDriver -
> 0xFF200000 FvIsBeingProcesssed - 0xFF200000 Check MmFileTypes - 0xA
> Check MmFileTypes - 0xE Find PE data - 0xFF219024 MmAddToDriverList -
> 58F7A62B-6280-42A7-BC38-10535A64A92C
> (0xFF219024)
> MmDispatcher
>   Drain the Scheduled Queue
>   Search DriverList for items to place on Scheduled Queue
>   DriverEntry (Discovered) - 58F7A62B-6280-42A7-BC38-10535A64A92C
> Evaluate MM DEPEX for FFS(58F7A62B-6280-42A7-BC38-10535A64A92C)
>   TRUE
>   END
>   RESULT = TRUE
>   Drain the Scheduled Queue
>   DriverEntry (Scheduled) - 58F7A62B-6280-42A7-BC38-10535A64A92C
> MmLoadImage - 58F7A62B-6280-42A7-BC38-10535A64A92C
> UpdatePeCoffPermissions: Mapping section 0 of image at 0xFFFE7000 with
> RO-XN permissions and size 0x7000
> UpdatePeCoffPermissions: Mapping section 0 of image at 0xFFFE7000 with
> RO-X permissions and size 0x7000
> UpdatePeCoffPermissions: Mapping section 1 of image at 0xFFFEE000 with
> RW-XN permissions and size 0x1000
> UpdatePeCoffPermissions: Mapping section 2 of image at 0xFFFEF000 with
> RO-XN permissions and size 0x1000 add-symbol-file
> /home/supven01/work/mm_upstream/Build/StandaloneMmPkg/DEBUG_GCC5
> /AARCH64/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpu
> Driver/DEBUG/PiMmStandloneArmTfCpuDriver.dll 0xFFFE7000 Loading MM
> driver at 0x000FFFE6000 EntryPoint=0x000FFFE7000
> PiMmStandloneArmTfCpuDriver.efi StartImage - 0xFFFE7000 (Standalone
> Mode)
> MmInstallProtocolInterface: 26EEB3DE-B689-492E-80F0-BE8BD7DA4BA7
> FFFEE008
> MmConfigurationMmNotify(26EEB3DE-B689-492E-80F0-BE8BD7DA4BA7) -
> FFFEE008
> MM Core registered MM Entry Point address FF2041C0
> MmInstallProtocolInterface: EB346B97-975F-4A9F-8B22-F8E92BB3D569
> FFFEE018
> Sharing Cpu Driver EP *0xFF217280 = 0xFFFE798C
> mNsCommBuffer.PhysicalStart - 0xFF600000 mNsCommBuffer.PhysicalSize -
> 0x10000
> mNsCommBuffer: 0x00000000FF600000 - 0x10000
> mMpInformationHobData: 0x0000000000000008 - 0x8
> mMpInformationHobData[0x80000000]: 0, 0, 0
> mMpInformationHobData[0x80000001]: 0, 1, 1
> mMpInformationHobData[0x80000002]: 0, 2, 2
> mMpInformationHobData[0x80000003]: 0, 3, 3
> mMpInformationHobData[0x80000100]: 1, 0, 0
> mMpInformationHobData[0x80000101]: 1, 1, 1
> mMpInformationHobData[0x80000102]: 1, 2, 2
> mMpInformationHobData[0x80000103]: 1, 3, 3 MmiHandlerRegister - GUID
> B65694CC-09E3-4C3B-B5CD-05F44D3CDBFF - Status 0 MmiHandlerRegister -
> GUID 7081E22F-CAC6-4053-9468-675782CF88E5 - Status 0
> MmiHandlerRegister - GUID 60FF8964-E906-41D0-AFED-F241E974E08E -
> Status
> 0
> MmiHandlerRegister - GUID 02CE967A-DD7E-4FFC-9EE7-810CF0470880 -
> Status 0 MmiHandlerRegister - GUID
> 2A571201-4966-47F6-8B86-F31E41F32F10 - Status
> 0
> MmiHandlerRegister - GUID 27ABF055-B1B8-4C26-8048-748F37BAA2DF -
> Status 0 MmiHandlerRegister - GUID
> 7CE88FB3-4BD7-4679-87A8-A8D8DEE50D2B - Status 0 MmMain Done!
> Shared Cpu Driver EP 0xFFFE798C
>
> ARM TF (FVP UART0)
> NOTICE:  Booting Trusted Firmware
> NOTICE:  BL1: v1.4(debug):v1.4-96-gae48aad7-dirty
> NOTICE:  BL1: Built : 16:14:26, Nov  1 2017
> INFO:    BL1: RAM 0x4036000 - 0x403c000
> INFO:    BL1: Loading BL2
> INFO:    Loading image id=1 at address 0x4027000
> INFO:    Image id=1 loaded: 0x4027000 - 0x402c2a8
> NOTICE:  BL1: Booting BL2
> INFO:    Entry point address = 0x4027000
> INFO:    SPSR = 0x3c5
> NOTICE:  BL2: v1.4(debug):v1.4-96-gae48aad7-dirty
> NOTICE:  BL2: Built : 16:14:26, Nov  1 2017
> INFO:    BL2: Doing platform setup
> INFO:    Configuring TrustZone Controller
> INFO:    BL2: Loading image id 3
> INFO:    Loading image id=3 at address 0xff000000
> INFO:    Image id=3 loaded: 0xff000000 - 0xff00c230
> INFO:    BL2: Loading image id 4
> INFO:    Loading image id=4 at address 0xff200000
> INFO:    Image id=4 loaded: 0xff200000 - 0xff480000
> INFO:    BL2: Loading image id 5
> INFO:    Loading image id=5 at address 0x88000000
> INFO:    Image id=5 loaded: 0x88000000 - 0x88280000
> NOTICE:  BL1: Booting BL31
> INFO:    Entry point address = 0xff000000
> INFO:    SPSR = 0x3cd
> NOTICE:  BL31: v1.4(debug):v1.4-96-gae48aad7-dirty
> NOTICE:  BL31: Built : 16:14:26, Nov  1 2017
> INFO:    GICv3 with legacy support detected. ARM GICV3 driver initialized in
> EL3
> INFO:    BL31: Initializing runtime services
> INFO:    BL31: Initializing BL32
> NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
> NOTICE:    Start address  : 0xff217000
> NOTICE:    Number of pages: 1 (4096 bytes)
> NOTICE:    Attributes     : 0x7
> NOTICE:    (Equivalent TF attributes: 0x22)
> NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
> NOTICE:    Start address  : 0xff217000
> NOTICE:    Number of pages: 1 (4096 bytes)
> NOTICE:    Attributes     : 0x5
> NOTICE:    (Equivalent TF attributes: 0x2a)
> NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
> NOTICE:    Start address  : 0xff218000
> NOTICE:    Number of pages: 1 (4096 bytes)
> NOTICE:    Attributes     : 0x7
> NOTICE:    (Equivalent TF attributes: 0x22)
> NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
> NOTICE:    Start address  : 0xfffe7000
> NOTICE:    Number of pages: 7 (28672 bytes)
> NOTICE:    Attributes     : 0x7
> NOTICE:    (Equivalent TF attributes: 0x22)
> NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
> NOTICE:    Start address  : 0xfffe7000
> NOTICE:    Number of pages: 7 (28672 bytes)
> NOTICE:    Attributes     : 0x3
> NOTICE:    (Equivalent TF attributes: 0x2)
> NOTICE:  Received SPM_MEMORY_ATTRIBUTES_SET SMC
> NOTICE:    Start address  : 0xfffef000
> NOTICE:    Number of pages: 1 (4096 bytes)
> NOTICE:    Attributes     : 0x7
> NOTICE:    (Equivalent TF attributes: 0x22)
> INFO:    BL31: Preparing for EL3 exit to normal world
> INFO:    Entry point address = 0x88000000
> INFO:    SPSR = 0x3c9
> UEFI firmware (version  built at 16:14:14 on Nov  1 2017)
> ***
>
> Supreeth Venkatesh (18):
>   ArmPkg: Add PCDs needed for MM communication driver.
>   ArmPkg/Drivers: Add EFI_MM_COMMUNICATION_PROTOCOL DXE driver.
>   ArmPkg/Include: Add MM interface SVC return codes.
>   ArmPkg/ArmMmuLib: Add MMU Library suitable for use in S-EL0.
>   ArmPkg/ArmMmuLib: Add MMU library inf file suitable for use in S-EL0.
>   StandaloneMmPkg: Add an AArch64 specific entry point library.
>   StandaloneMmPkg/FvLib: Add a common FV Library for management mode.
>   StandaloneMmPkg/MemLib: AARCH64 Specific instance of memory check
>     library.
>   StandaloneMmPkg/MemoryAllocationLib: Add MM memory allocation library.
>   StandaloneMmPkg/HobLib: Add AARCH64 Specific HOB Library for
>     management mode.
>   StandaloneMmPkg: MM driver entry point library.
>   StandaloneMmPkg/CpuMm: Add CPU driver suitable for ARM Platforms.
>   StandaloneMmPkg/Core: Implementation of Standalone MM Core Module.
>   StandaloneMmPkg: Describe the declaration, definition and fdf files.
>   ArmPkg: Extra action to update permissions for S-ELO MM Image.
>   BaseTools/AutoGen: Update header file for MM modules.
>   StandaloneMmPkg: Add application to test MM communication protocol.
>   StandaloneMmPkg: Add handler to handle event received from Normal
>     World.
>
>  ArmPkg/ArmPkg.dec                                  |    3 +
>  .../Drivers/MmCommunicationDxe/MmCommunication.c   |  339 +++++++
>  .../Drivers/MmCommunicationDxe/MmCommunication.inf |   50 +
>  ArmPkg/Include/IndustryStandard/ArmMmSvc.h         |    9 +-
>  ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c    |  146 +++
>  ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf          |   37 +
>  .../DebugPeCoffExtraActionLib.c                    |  185 +++-
>  .../DebugPeCoffExtraActionLib.inf                  |    7 +
>  BaseTools/Source/Python/AutoGen/GenC.py            |   16 +-
>  .../Application/MmCommTestApp/MmCommTest.c         |   81 ++
>  .../Application/MmCommTestApp/MmCommTest.h         |   37 +
>  .../Application/MmCommTestApp/MmCommTest.inf       |   57 ++
>  StandaloneMmPkg/Core/Dependency.c                  |  389 +++++++
>  StandaloneMmPkg/Core/Dispatcher.c                  | 1071
> ++++++++++++++++++++
>  StandaloneMmPkg/Core/FwVol.c                       |  104 ++
>  StandaloneMmPkg/Core/Handle.c                      |  533
> ++++++++++
>  StandaloneMmPkg/Core/InstallConfigurationTable.c   |  178 ++++
>  StandaloneMmPkg/Core/Locate.c                      |  496 +++++++++
>  StandaloneMmPkg/Core/Mmi.c                         |  337 ++++++
>  StandaloneMmPkg/Core/Notify.c                      |  203 ++++
>  StandaloneMmPkg/Core/Page.c                        |  384 +++++++
>  StandaloneMmPkg/Core/Pool.c                        |  287 ++++++
>  StandaloneMmPkg/Core/StandaloneMmCore.c            |  747
> ++++++++++++++
>  StandaloneMmPkg/Core/StandaloneMmCore.h            |  903
> +++++++++++++++++
>  StandaloneMmPkg/Core/StandaloneMmCore.inf          |   82 ++
>  StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h |   66 ++
>  StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S          |   33 +
>  StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c    |  231 +++++
>  StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c           |  229 +++++
>  .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h        |   89 ++
>  .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf      |   60 ++
>  StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c      |   51 +
>  StandaloneMmPkg/Include/Guid/MmCoreData.h          |  132 +++
>  StandaloneMmPkg/Include/Guid/MmFvDispatch.h        |   38 +
>  StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h  |   62 ++
>  StandaloneMmPkg/Include/Guid/MpInformation.h       |   41 +
>  .../Library/Arm/StandaloneMmCoreEntryPoint.h       |  232 +++++
>  StandaloneMmPkg/Include/Library/FvLib.h            |  109 ++
>  StandaloneMmPkg/Include/Library/MemLib.h           |  140 +++
>  .../Include/Library/MmCoreStandaloneEntryPoint.h   |  101 ++
>  .../Include/Library/MmDriverStandaloneEntryPoint.h |  148 +++
>  StandaloneMmPkg/Include/StandaloneMm.h             |   36 +
>  StandaloneMmPkg/Library/FvLib/FvLib.c              |  366 +++++++
>  StandaloneMmPkg/Library/FvLib/FvLib.inf            |   57 ++
>  StandaloneMmPkg/Library/HobLib/Arm/HobLib.c        |  697
> +++++++++++++
>  StandaloneMmPkg/Library/HobLib/HobLib.inf          |   45 +
>  StandaloneMmPkg/Library/MemLib/Arm/MemLib.c        |  276 +++++
>  StandaloneMmPkg/Library/MemLib/MemLib.inf          |   47 +
>  .../MemoryAllocationLib/MemoryAllocationLib.c      |  907
> +++++++++++++++++
>  .../MemoryAllocationLib/MemoryAllocationLib.inf    |   49 +
>  .../MemoryAllocationLib/MemoryAllocationServices.h |   38 +
>  .../StandaloneMmCoreEntryPoint/Arm/CreateHobList.c |  200 ++++
>  .../Arm/SetPermissions.c                           |  278 +++++
>  .../Arm/StandaloneMmCoreEntryPoint.c               |  264 +++++
>  .../StandaloneMmCoreEntryPoint.inf                 |   53 +
>  .../StandaloneMmDriverEntryPoint.c                 |  102 ++
>  .../StandaloneMmDriverEntryPoint.inf               |   41 +
>  StandaloneMmPkg/StandaloneMmPkg.dec                |   49 +
>  StandaloneMmPkg/StandaloneMmPkg.dsc                |  132 +++
>  StandaloneMmPkg/StandaloneMmPkg.fdf                |  184 ++++
>  StandaloneMmPkg => StandaloneMmPkg~HEAD            |    0
>  61 files changed, 12244 insertions(+), 20 deletions(-)  create mode
> 100644 ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.c
>  create mode 100644
> ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.inf
>  create mode 100644 ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c
>  create mode 100644 ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
>  create mode 100644
> StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.c
>  create mode 100644
> StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.h
>  create mode 100644
> StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.inf
>  create mode 100644 StandaloneMmPkg/Core/Dependency.c  create mode
> 100644 StandaloneMmPkg/Core/Dispatcher.c  create mode 100644
> StandaloneMmPkg/Core/FwVol.c  create mode 100644
> StandaloneMmPkg/Core/Handle.c  create mode 100644
> StandaloneMmPkg/Core/InstallConfigurationTable.c
>  create mode 100644 StandaloneMmPkg/Core/Locate.c  create mode 100644
> StandaloneMmPkg/Core/Mmi.c  create mode 100644
> StandaloneMmPkg/Core/Notify.c  create mode 100644
> StandaloneMmPkg/Core/Page.c  create mode 100644
> StandaloneMmPkg/Core/Pool.c  create mode 100644
> StandaloneMmPkg/Core/StandaloneMmCore.c
>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.h
>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.inf
>  create mode 100644
> StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
>  create mode 100644
> StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
>  create mode 100644
> StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
>  create mode 100644 StandaloneMmPkg/Include/Guid/MmCoreData.h
>  create mode 100644 StandaloneMmPkg/Include/Guid/MmFvDispatch.h
>  create mode 100644
> StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
>  create mode 100644 StandaloneMmPkg/Include/Guid/MpInformation.h
>  create mode 100644
> StandaloneMmPkg/Include/Library/Arm/StandaloneMmCoreEntryPoint.h
>  create mode 100644 StandaloneMmPkg/Include/Library/FvLib.h
>  create mode 100644 StandaloneMmPkg/Include/Library/MemLib.h
>  create mode 100644
> StandaloneMmPkg/Include/Library/MmCoreStandaloneEntryPoint.h
>  create mode 100644
> StandaloneMmPkg/Include/Library/MmDriverStandaloneEntryPoint.h
>  create mode 100644 StandaloneMmPkg/Include/StandaloneMm.h
>  create mode 100644 StandaloneMmPkg/Library/FvLib/FvLib.c
>  create mode 100644 StandaloneMmPkg/Library/FvLib/FvLib.inf
>  create mode 100644 StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
>  create mode 100644 StandaloneMmPkg/Library/HobLib/HobLib.inf
>  create mode 100644 StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
>  create mode 100644 StandaloneMmPkg/Library/MemLib/MemLib.inf
>  create mode 100644
> StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
>  create mode 100644
> StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
>  create mode 100644
> StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.h
>  create mode 100644
> StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/CreateHobList.c
>  create mode 100644
> StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/SetPermissions.
> c
>  create mode 100644
> StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMm
> CoreEntryPoint.c
>  create mode 100644
> StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreE
> ntryPoint.inf
>  create mode 100644
> StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriv
> erEntryPoint.c
>  create mode 100644
> StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriv
> erEntryPoint.inf
>  create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dec
>  create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dsc
>  create mode 100644 StandaloneMmPkg/StandaloneMmPkg.fdf
>  rename StandaloneMmPkg => StandaloneMmPkg~HEAD (100%)
>
> --
> 2.16.2

IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 01/18] ArmPkg: Add PCDs needed for MM communication driver.
  2018-04-11 14:43   ` Achin Gupta
       [not found]     ` <AM4PR0802MB23063743A3B2F5A552BE320580870@AM4PR0802MB2306.eurprd08.prod.outlook.com>
@ 2018-05-04 23:17     ` Supreeth Venkatesh
  1 sibling, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:17 UTC (permalink / raw)
  To: Achin Gupta
  Cc: edk2-devel@lists.01.org, michael.d.kinney@intel.com,
	liming.gao@intel.com, jiewen.yao@intel.com,
	leif.lindholm@linaro.org, ard.biesheuvel@linaro.org, nd

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Wednesday, April 11, 2018 9:43 AM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 01/18] ArmPkg: Add PCDs needed for MM communication driver.

On Fri, Apr 06, 2018 at 03:42:06PM +0100, Supreeth Venkatesh wrote:
> This patch defines PCDs to describe the base address and size of
> communication buffer between normal world (uefi) and standalone MM
> environment in the secure world.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  ArmPkg/ArmPkg.dec | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/ArmPkg/ArmPkg.dec b/ArmPkg/ArmPkg.dec index
> a55b6268ff..b64942220b 100644
> --- a/ArmPkg/ArmPkg.dec
> +++ b/ArmPkg/ArmPkg.dec
> @@ -223,6 +223,9 @@
>    gArmTokenSpaceGuid.PcdSystemMemoryBase|0|UINT64|0x00000029
>    gArmTokenSpaceGuid.PcdSystemMemorySize|0|UINT64|0x0000002A
>
> +  gArmTokenSpaceGuid.PcdMmBufferBase|0|UINT64|0x00000045
> +  gArmTokenSpaceGuid.PcdMmBufferSize|0|UINT64|0x00000046
> +
>  [PcdsFixedAtBuild.common, PcdsDynamic.common]
>    #
>    # ARM Architectural Timer
> --
> 2.16.2
>

Acked-by: Achin Gupta <achin.gupta@arm.com>
[Supreeth] Thanks.
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 02/18] ArmPkg/Drivers: Add EFI_MM_COMMUNICATION_PROTOCOL DXE driver.
  2018-04-11 14:00   ` Achin Gupta
@ 2018-05-04 23:18     ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:18 UTC (permalink / raw)
  To: Achin Gupta; +Cc: edk2-devel@lists.01.org

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Wednesday, April 11, 2018 9:00 AM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 02/18] ArmPkg/Drivers: Add EFI_MM_COMMUNICATION_PROTOCOL DXE driver.

Hi Supreeth,

CIL.

On Fri, Apr 06, 2018 at 03:42:07PM +0100, Supreeth Venkatesh wrote:
> PI v1.5 Specification Volume 4 defines Management Mode Core Interface
> and defines EFI_MM_COMMUNICATION_PROTOCOL. This protocol provides a
> means of communicating between drivers outside of MM and MMI handlers
> inside of MM.
>
> This patch implements the EFI_MM_COMMUNICATION_PROTOCOL DXE runtime
> driver for AARCH64 platforms. It uses SMCs allocated from the standard
> SMC range defined in DEN0060A_ARM_MM_Interface_Specification.pdf
> to communicate with the standalone MM environment in the secure world.
>
> This patch also adds the MM Communication driver (.inf) file to define
> entry point for this driver and other compile related information the
> driver needs.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  .../Drivers/MmCommunicationDxe/MmCommunication.c   | 339 +++++++++++++++++++++
>  .../Drivers/MmCommunicationDxe/MmCommunication.inf |  50 +++
>  2 files changed, 389 insertions(+)
>  create mode 100644
> ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.c
>  create mode 100644
> ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.inf
>
> diff --git a/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.c
> b/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.c
> new file mode 100644
> index 0000000000..e801c1c601
> --- /dev/null
> +++ b/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.c
> @@ -0,0 +1,339 @@
> +/** @file
> +
> +  Copyright (c) 2016-2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Library/ArmLib.h>
> +#include <Library/ArmSmcLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/DxeServicesTableLib.h> #include <Library/HobLib.h>
> +#include <Library/PcdLib.h> #include
> +<Library/UefiBootServicesTableLib.h>
> +#include <Library/UefiRuntimeServicesTableLib.h>
> +
> +#include <Protocol/MmCommunication.h>
> +
> +#include <IndustryStandard/ArmStdSmc.h>
> +
> +//
> +// Address, Length of the pre-allocated buffer for communication with
> +the secure // world.
> +//
> +STATIC ARM_MEMORY_REGION_DESCRIPTOR  mNsCommBuffMemRegion;
> +
> +// Notification event when virtual address map is set.
> +STATIC EFI_EVENT  mSetVirtualAddressMapEvent;
> +
> +//
> +// Handle to install the MM Communication Protocol // STATIC
> +EFI_HANDLE  mMmCommunicateHandle;
> +
> +/**
> +  Communicates with a registered handler.
> +
> +  This function provides an interface to send and receive messages to
> + the  Standalone MM environment on behalf of UEFI services.  This
> + function is part  of the MM Communication Protocol that may be
> + called in physical mode prior to
> +  SetVirtualAddressMap() and in virtual mode after SetVirtualAddressMap().
> +
> +  @param[in]      This                The EFI_MM_COMMUNICATION_PROTOCOL
> +                                      instance.
> +  @param[in, out] CommBuffer          A pointer to the buffer to convey
> +                                      into MMRAM.
> +  @param[in, out] CommSize            The size of the data buffer being
> +                                      passed in. This is optional.
> +
> +  @retval EFI_SUCCESS                 The message was successfully posted.
> +  @retval EFI_INVALID_PARAMETER       The CommBuffer was NULL.
> +  @retval EFI_BAD_BUFFER_SIZE         The buffer size is incorrect for the MM
> +                                      implementation. If this error is
> +                                      returned, the MessageLength field in
> +                                      the CommBuffer header or the integer
> +                                      pointed by CommSize are updated to reflect
> +                                      the maximum payload size the
> +                                      implementation can accommodate.
> +  @retval EFI_ACCESS_DENIED           The CommunicateBuffer parameter
> +                                      or CommSize parameter, if not omitted,
> +                                      are in address range that cannot be
> +                                      accessed by the MM environment
> +**/ STATIC EFI_STATUS EFIAPI MmCommunicationCommunicate (
> +  IN CONST EFI_MM_COMMUNICATION_PROTOCOL  *This,
> +  IN OUT VOID                             *CommBuffer,
> +  IN OUT UINTN                            *CommSize OPTIONAL
> +  )
> +{
> +  EFI_MM_COMMUNICATE_HEADER   *CommunicateHeader;
> +  ARM_SMC_ARGS                CommunicateSmcArgs;
> +  EFI_STATUS                  Status;
> +  UINTN                       BufferSize;
> +
> +  CommunicateHeader = CommBuffer;
> +  Status = EFI_ACCESS_DENIED;
> +  BufferSize = 0;
> +
> +  ZeroMem (&CommunicateSmcArgs, sizeof (ARM_SMC_ARGS));
> +
> +  //
> +  // Check parameters
> +  //
> +  if (CommBuffer == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  // If the length of the CommBuffer is 0 then return the expected length.
> +  if (CommSize) {
> +    if (*CommSize == 0) {
> +      *CommSize = mNsCommBuffMemRegion.Length;
> +      return EFI_BAD_BUFFER_SIZE;
> +    }
> +    //
> +    // CommSize must hold HeaderGuid and MessageLength
> +    //
> +    if (*CommSize < sizeof (EFI_MM_COMMUNICATE_HEADER)) {
> +        return EFI_INVALID_PARAMETER;
> +    }

The check looks inadequate as it does not cater for the MessageLength specified in the EFI_MM_COMMUNICATE_HEADER i.e it will ensure there is at least a EFI_MM_COMMUNICATE_HEADER but will not ensure that the message is included as well.

Nit: It would be better if the parameters are checked before Status and CommunicateSmcArgs are updated above.
[Supreeth] As discussed, I will be relying on CommunicateHeader->MessageLength + Sizeof (Header) to calculate total size of communication payload as
CommBuffer is a Mandatory parameter as opposed to CommSize which is optional.
I have revamped the logic completely. Please check version 2.

> +    BufferSize = *CommSize;
> +  } else {
> +    BufferSize = CommunicateHeader->MessageLength +
> +                 sizeof (CommunicateHeader->HeaderGuid) +
> +                 sizeof (CommunicateHeader->MessageLength);
> +  }
> +
> +  //
> +  // If the buffer size is 0 or greater than what can be tolerated by
> + the MM  // environment then return the expected size.
> +  //
> +  if ((BufferSize == 0) ||
> +      (BufferSize > mNsCommBuffMemRegion.Length)) {
> +    CommunicateHeader->MessageLength = mNsCommBuffMemRegion.Length -
> +                                       sizeof (CommunicateHeader->HeaderGuid) -
> +                                       sizeof (CommunicateHeader->MessageLength);
> +    return EFI_BAD_BUFFER_SIZE;
> +  }

The 'if' condition works as expected to return the maximum buffer size if 'CommSize' is NULL. When 'CommSize' is used and the 'BufferSize' (derived from
'CommSize') is greater than what can be tolerated, then it is 'CommSize' that must be updated as per the PI spec. but the 'if'condition will still update the MessageLength.
[Supreeth] PI specification does not say MessageLength should not be updated either. I have revamped the logic completely. Please check version 2.
I plan to submit ECR w.r.t MessageLength.

> +
> +  // SMC Function ID
> +  CommunicateSmcArgs.Arg0 = ARM_SMC_ID_MM_COMMUNICATE_AARCH64;
> +
> +  // Reserved for Future. Must be Zero.
> +  CommunicateSmcArgs.Arg1 = 0;
> +
> +  if (mNsCommBuffMemRegion.VirtualBase) {
> +    CopyMem ((VOID *)mNsCommBuffMemRegion.VirtualBase, CommBuffer,
> + BufferSize);  } else {
> +    return EFI_ACCESS_DENIED;
> +  }

VirtualBase was set to PhysicalBase in MmCommunicationInitialize(). Is there any reason why it would be unset here apart from a corruption of mNsCommBuffMemRegion structure? If not, then this is a panic condition. It is not the caller's fault and returning EFI_ACCESS_DENIED does not really help.
[Supreeth] This is not a critical event to affect to stop the working system in entirety. If there is a critical failure,
Secure side should anyway reset the system. The "if" condition was added as a result of someone else's feedback from earlier version of the patches.
I realize this is an extra check and I have removed this "if" condition.

> +
> +  // For the SMC64 version, this parameter is a 64-bit Physical
> + Address (PA)  // or Intermediate Physical Address (IPA).
> +  // For the SMC32 version, this parameter is a 32-bit PA or IPA.
> +  CommunicateSmcArgs.Arg2 = (UINTN)mNsCommBuffMemRegion.PhysicalBase;
> +
> +  // comm_size_address is a PA or an IPA that holds the size of the
> + // communication buffer being passed in. This parameter is optional
> + // and can be omitted by passing a zero.
> +  // ARM does not recommend using it since this might require the  //
> + implementation to create a separate memory mapping for the parameter.
> +  // ARM recommends storing the buffer size in the buffer itself.
> +  CommunicateSmcArgs.Arg3 = 0;
> +
> +  // Call the Standalone MM environment.
> +  ArmCallSmc (&CommunicateSmcArgs);
> +
> +  switch (CommunicateSmcArgs.Arg0) {
> +  case ARM_SMC_MM_RET_SUCCESS:
> +    // On exit, the size of data being returned is inferred from
> +    // CommSize or MessageLength + Header.
> +    CopyMem (CommBuffer,
> +             (const VOID *)mNsCommBuffMemRegion.VirtualBase,
> +             BufferSize);

Umm! I vaguely remember we had a chat about this but this does not seem right. The PI spec. does not help by being vague about how data should be copied out and its length reported. However, here is my take.

If CommSize is used, then it must be updated by the handler with the size of the data returned, zero otherwise. This is as far as the PI spec. goes in this regard.

Even though the spec. does not say this, if CommSize is not used then, MessageLength in the EFI_MM_COMMUNICATE_HEADER must be updated. This also means that the handler has to preserve the EFI_MM_COMMUNICATE_HEADER in the CommBuffer in the return path (again something the PI spec. does not say).

In the SMC ABI, CommSize maps to comm_size_address. We do not use this. So we must rely on the MessageLength on the way back.

Using BufferSize does not seem right to me. We are screwed if the input size is not equal to the output size. It is better to retrieve this information from the MessageLength as the comment says.
[Supreeth] As per our previous discussion, You had mentioned that "VariableDxe" that uses this driver assumes input size equal to output size. However, After looking into this further,
there are other drivers that consume this and for those drivers input size is not equal to output size.
When input size is not equal to output size, this may lead to overflow issues. PI specification does not provide any guidance regarding this. I plan to submit an ECR along the below lines.
   // Note: Very important to ensure that the consumer of this driver
    // has allocated CommBuffer sufficiently so that the return data
    // can be copied. Otherwise, this will lead to buffer overflow.
    // Assumption: CommBuffer = malloc (mNsCommBuffMemRegion.Length)
    // This guidance should be in the PI specification. TODO: ECR.

> +    Status = EFI_SUCCESS;
> +    break;
> +
> +  case ARM_SMC_MM_RET_NOT_SUPPORTED:

NOT_SUPPORTED means that MM_COMMUNICATE is not implemented which must not be the case if we are here. MmCommunicationInitialize() must call MM_VERSION to check this. In fact, the MM interface specification should be updated to remove NOT_SUPPORTED for MM_COMMUNICATE if MM_VERSION is implemented.

Can you panic() if this is returned?

[Supreeth] This is not a critical event to affect to stop the working system in entirety. If there is a critical failure, Secure side should anyway reset the system.
I have removed this case.

> +  case ARM_SMC_MM_RET_INVALID_PARAMS:
> +    Status = EFI_INVALID_PARAMETER;
> +    break;
> +
> +  case ARM_SMC_MM_RET_DENIED:
> +    Status = EFI_ACCESS_DENIED;
> +    break;
> +
> +  case ARM_SMC_MM_RET_NO_MEMORY:
> +    // Unexpected error since the CommSize was checked for zero length
> +    // prior to issuing the SMC
> +  default:
> +    Status = EFI_ACCESS_DENIED;
> +    ASSERT (0);
> +  }
> +
> +  return Status;
> +}
> +
> +//
> +// MM Communication Protocol instance //
> +EFI_MM_COMMUNICATION_PROTOCOL  mMmCommunication = {
> +  MmCommunicationCommunicate
> +};
> +
> +/**
> +  Notification callback on SetVirtualAddressMap event.
> +
> +  This function notifies the MM communication protocol interface on
> + SetVirtualAddressMap event and converts pointers used in this driver
> + from physical to virtual address.
> +
> +  @param  Event          SetVirtualAddressMap event.
> +  @param  Context        A context when the SetVirtualAddressMap triggered.
> +
> +  @retval EFI_SUCCESS    The function executed successfully.
> +  @retval Other          Some error occurred when executing this function.
> +
> +**/
> +STATIC
> +VOID
> +EFIAPI
> +NotifySetVirtualAddressMap (
> +  IN EFI_EVENT  Event,
> +  IN VOID      *Context
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  Status = gRT->ConvertPointer (EFI_OPTIONAL_PTR,
> +                                (VOID **)&mNsCommBuffMemRegion.VirtualBase
> +                               );
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_ERROR, "NotifySetVirtualAddressMap():"
> +            " Unable to convert MM runtime pointer. Status:0x%r\n",
> + Status));  }
> +
> +}
> +
> +/**
> +  The Entry Point for MM Communication
> +
> +  This function installs the MM communication protocol interface and
> + finds out  what type of buffer management will be required prior to
> + invoking the  communication SMC.
> +
> +  @param  ImageHandle    The firmware allocated handle for the EFI image.
> +  @param  SystemTable    A pointer to the EFI System Table.
> +
> +  @retval EFI_SUCCESS    The entry point is executed successfully.
> +  @retval Other          Some error occurred when executing this entry point.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCommunicationInitialize (
> +  IN EFI_HANDLE         ImageHandle,
> +  IN EFI_SYSTEM_TABLE  *SystemTable
> +  )
> +{
> +  EFI_STATUS                 Status;
> +
> +  mNsCommBuffMemRegion.PhysicalBase = PcdGet64 (PcdMmBufferBase);  //
> + During boot , Virtual and Physical are same
> + mNsCommBuffMemRegion.VirtualBase =
> + mNsCommBuffMemRegion.PhysicalBase;
> +  mNsCommBuffMemRegion.Length = PcdGet64 (PcdMmBufferSize);
> +
> +  if (mNsCommBuffMemRegion.PhysicalBase == 0) {
> +    DEBUG ((DEBUG_ERROR, "MmCommunicateInitialize: "
> +            "Invalid MM Buffer Base Address.\n"));
> +    goto ReturnErrorStatus;
> +  }
> +

We need to call MM_VERSION to check whether there is a StandaloneMm imp. in the Secure world or not.
[Supreeth] Agreed. Please see version 2.


> +  if (mNsCommBuffMemRegion.Length == 0) {
> +    DEBUG ((DEBUG_ERROR, "MmCommunicateInitialize: "
> +            "Maximum Buffer Size is zero.\n"));
> +    goto ReturnErrorStatus;
> +  }
> +
> +  Status = gDS->AddMemorySpace (EfiGcdMemoryTypeSystemMemory,
> +                                mNsCommBuffMemRegion.PhysicalBase,
> +                                mNsCommBuffMemRegion.Length,
> +                                EFI_MEMORY_WB |
> +                                EFI_MEMORY_XP |
> +                                EFI_MEMORY_RUNTIME);  if (EFI_ERROR
> + (Status)) {
> +    DEBUG ((DEBUG_ERROR, "MmCommunicateInitialize: "
> +            "Failed to add MM-NS Buffer Memory Space\n"));
> +    goto ReturnErrorStatus;
> +  }
> +
> +  Status = gDS->SetMemorySpaceAttributes(mNsCommBuffMemRegion.PhysicalBase,
> +                                         mNsCommBuffMemRegion.Length,
> +                                         EFI_MEMORY_WB |
> + EFI_MEMORY_XP);  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_ERROR, "MmCommunicateInitialize: "
> +            "Failed to set MM-NS Buffer Memory attributes\n"));
> +    goto CleanAddedMemorySpace;
> +  }
> +
> +  Status = gBS->AllocatePages (AllocateAddress,
> +                               EfiRuntimeServicesData,
> +                               EFI_SIZE_TO_PAGES (mNsCommBuffMemRegion.Length),
> +                               &mNsCommBuffMemRegion.PhysicalBase);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_ERROR, "MmCommunicateInitialize: "
> +            "Failed to allocate MM-NS Buffer Memory Space\n"));
> +    goto CleanAddedMemorySpace;
> +  }
> +
> +  // Install the communication protocol  Status =
> + gBS->InstallProtocolInterface (&mMmCommunicateHandle,
> +                                          &gEfiMmCommunicationProtocolGuid,
> +                                          EFI_NATIVE_INTERFACE,
> +                                          &mMmCommunication);  if
> + (EFI_ERROR(Status)) {
> +    DEBUG ((DEBUG_ERROR, "MmCommunicationInitialize: "
> +            "Failed to install MM communication protocol\n"));
> +    goto CleanAllocatedPages;
> +  }
> +
> +  // Register notification callback when  virtual address is
> + associated  // with the physical address.
> +  // Create a Set Virtual Address Map event.
> +  //
> +  Status = gBS->CreateEvent (EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,  // Type
> +                             TPL_NOTIFY,                         // NotifyTpl
> +                             NotifySetVirtualAddressMap,         // NotifyFunction
> +                             NULL,                               // NotifyContext
> +                             &mSetVirtualAddressMapEvent         // Event
> +                            );
> +  if (Status == EFI_SUCCESS) {
> +    return Status;
> +  }
> +
> +  gBS->UninstallProtocolInterface(mMmCommunicateHandle,
> +                                  &gEfiMmCommunicationProtocolGuid,
> +                                  &mMmCommunication);
> +
> +CleanAllocatedPages:
> +  gBS->FreePages (mNsCommBuffMemRegion.PhysicalBase,
> +                  EFI_SIZE_TO_PAGES (mNsCommBuffMemRegion.Length));
> +
> +CleanAddedMemorySpace:
> +  gDS->RemoveMemorySpace (mNsCommBuffMemRegion.PhysicalBase,
> +                          mNsCommBuffMemRegion.Length);
> +
> +ReturnErrorStatus:
> +  return EFI_INVALID_PARAMETER;
> +}
> diff --git a/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.inf
> b/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.inf
> new file mode 100644
> index 0000000000..344d55f333
> --- /dev/null
> +++ b/ArmPkg/Drivers/MmCommunicationDxe/MmCommunication.inf
> @@ -0,0 +1,50 @@
> +#/** @file
> +#
> +#  DXE MM Communicate driver
> +#
> +#  Copyright (c) 2016 - 2017, ARM Limited. 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
> +#
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, #  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#**/
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = ArmMmCommunication
> +  FILE_GUID                      = 09EE81D3-F15E-43F4-85B4-CB9873DA5D6B
> +  MODULE_TYPE                    = DXE_RUNTIME_DRIVER
> +  VERSION_STRING                 = 1.0
> +
> +  ENTRY_POINT                    = MmCommunicationInitialize
> +
> +[Sources.Common]
> +  MmCommunication.c

Since this is a AArch64 only driver. Should it not be under Sources.Common.AArch64?
[Supreeth] It is in ArmPkg,  so it implies it's a 32 bit or AArch64 driver, but as an added measure, I will move it under Sources. AArch64 and not Sources.Common.AArch64. (to be pedantic)
Cheers,
Achin

> +
> +[Packages]
> +  ArmPkg/ArmPkg.dec
> +  MdePkg/MdePkg.dec
> +
> +[LibraryClasses]
> +  ArmLib
> +  ArmSmcLib
> +  BaseMemoryLib
> +  DebugLib
> +  DxeServicesTableLib
> +  HobLib
> +  UefiDriverEntryPoint
> +
> +[Protocols]
> +  gEfiMmCommunicationProtocolGuid              ## PRODUCES
> +
> +[Pcd.common]
> +  gArmTokenSpaceGuid.PcdMmBufferBase
> +  gArmTokenSpaceGuid.PcdMmBufferSize
> +
> +[Depex]
> +  AFTER gArmGicDxeFileGuid
> --
> 2.16.2
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 03/18] ArmPkg/Include: Add MM interface SVC return codes.
  2018-04-11 14:38   ` Achin Gupta
@ 2018-05-04 23:19     ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:19 UTC (permalink / raw)
  To: Achin Gupta; +Cc: edk2-devel@lists.01.org

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Wednesday, April 11, 2018 9:38 AM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 03/18] ArmPkg/Include: Add MM interface SVC return codes.

Hi Supreeth,

On Fri, Apr 06, 2018 at 03:42:08PM +0100, Supreeth Venkatesh wrote:
> This patch adds the Management Mode(MM) SVC return codes as specified
> in http://infocenter.arm.com/help/topic/com.arm.doc.den0060a/DEN0060A_ARM_MM_Interface_Specification.pdf.
> Also, corrects SVC ID for retrieving SPM version information.

The MM interface specification says nothing about these SVCs. At the moment, this interface is exported by Arm TF. So lets say that instead. Also, it would make sense to rename this file to ArmTfSpmSvc.h or similar 'cause this SVC interface is usable by non-MM partitions too.
[Supreeth] The file exists already. How would having  "ArmTfSpmSvc.h" in edk2 code base help? This is only for edk2 based MM partition.
I have removed the references to http://infocenter.arm.com/help/topic/com.arm.doc.den0060a/DEN0060A_ARM_MM_Interface_Specification.pdf in the commit message.

cheers,
Achin

>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  ArmPkg/Include/IndustryStandard/ArmMmSvc.h | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/ArmPkg/Include/IndustryStandard/ArmMmSvc.h
> b/ArmPkg/Include/IndustryStandard/ArmMmSvc.h
> index 4c7b6c3386..a64b9ec23c 100644
> --- a/ArmPkg/Include/IndustryStandard/ArmMmSvc.h
> +++ b/ArmPkg/Include/IndustryStandard/ArmMmSvc.h
> @@ -20,7 +20,7 @@
>   * delegated events and request the Secure partition manager to perform
>   * privileged operations on its behalf.
>   */
> -#define ARM_SVC_ID_SPM_VERSION_AARCH64             0xC4000060
> +#define ARM_SVC_ID_SPM_VERSION_AARCH32             0x84000060
>  #define ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64       0xC4000061
>  #define ARM_SVC_ID_SP_GET_MEM_ATTRIBUTES_AARCH64   0xC4000064
>  #define ARM_SVC_ID_SP_SET_MEM_ATTRIBUTES_AARCH64   0xC4000065
> @@ -40,4 +40,11 @@
>      ((((c_perm) & SET_MEM_ATTR_CODE_PERM_MASK) << SET_MEM_ATTR_CODE_PERM_SHIFT) | \
>      (( (d_perm) & SET_MEM_ATTR_DATA_PERM_MASK) <<
> SET_MEM_ATTR_DATA_PERM_SHIFT))
>
> +/* MM SVC Return error codes */
> +#define ARM_SVC_SPM_RET_SUCCESS               0
> +#define ARM_SVC_SPM_RET_NOT_SUPPORTED        -1
> +#define ARM_SVC_SPM_RET_INVALID_PARAMS       -2
> +#define ARM_SVC_SPM_RET_DENIED               -3
> +#define ARM_SVC_SPM_RET_NO_MEMORY            -5
> +
>  #endif
> --
> 2.16.2
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 04/18] ArmPkg/ArmMmuLib: Add MMU Library suitable for use in S-EL0.
  2018-04-11 19:21   ` Achin Gupta
@ 2018-05-04 23:19     ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:19 UTC (permalink / raw)
  To: Achin Gupta; +Cc: edk2-devel@lists.01.org

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Wednesday, April 11, 2018 2:22 PM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 04/18] ArmPkg/ArmMmuLib: Add MMU Library suitable for use in S-EL0.

Hi Supreeth,

On Fri, Apr 06, 2018 at 03:42:09PM +0100, Supreeth Venkatesh wrote:
> The Standalone MM environment runs in S-EL0 in AArch64 on ARM Standard
> Platforms. Privileged firmware e.g. ARM Trusted Firmware sets up its
> architectural context including the initial translation tables for the
> S-EL1/EL0 translation regime. The MM environment could still request
> ARM TF to change the memory attributes of memory regions during
> initialization.

The commit message needs more detail to better flesh out why we are doing what we are doing here i.e. the StandaloneMm image is a FV that encapsulates the MM foundation and drivers. These are PE-COFF images with data and text segments. Arm TF does not have visibility of the contents of the FV. Moreover, the driver images are relocated upon dispatch. However, to initialise the MM environment, Arm TF has to create translation tables with sane default attributes for the memory occupied by the FV............

I am hoping you can extrapolate from here and clearly describe what problem this library solves.
[Supreeth] Ok.
>
> This patch adds a simple MMU library suitable for execution in S-EL0
> and requesting operations from higher exception levels.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c | 146
> ++++++++++++++++++++++++
>  1 file changed, 146 insertions(+)
>  create mode 100644 ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c
>
> diff --git a/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c
> b/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c

I am not sure about the name of the library. ArmMmuSecLib sounds like an MMU library for the SEC phase in the Normal world. Can we call it ArmMmuSecStandaloneMmLib or similar.
[Supreeth] I have renamed it as ArmMmuStandaloneMmCoreLib. Please see version 2.

> new file mode 100644
> index 0000000000..56969e31d1
> --- /dev/null
> +++ b/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuSecLib.c
> @@ -0,0 +1,146 @@
> +/** @file
> +*  File managing the MMU for ARMv8 architecture in S-EL0
> +*
> +*  Copyright (c) 2017, ARM Limited. All rights reserved.

Nit: Copyright 2018? For this and other files?

> +*
> +*  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
> +*
> +*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS,
> +*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +*
> +**/
> +
> +#include <Uefi.h>
> +#include <Chipset/AArch64.h>
> +#include <IndustryStandard/ArmMmSvc.h>
> +
> +#include <Library/ArmLib.h>
> +#include <Library/ArmMmuLib.h>
> +#include <Library/ArmSvcLib.h>
> +#include <Library/BaseLib.h>
> +#include <Library/DebugLib.h>
> +
> +EFI_STATUS
> +RequestMemoryPermissionChange(
> +  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
> +  IN  UINT64                    Length,
> +  IN  UINTN                     Permissions
> +  )
> +{
> +  EFI_STATUS    Status;
> +  ARM_SVC_ARGS  ChangeMemoryPermissionsSvcArgs = {0};
> +
> +  ChangeMemoryPermissionsSvcArgs.Arg0 =
> + ARM_SVC_ID_SP_SET_MEM_ATTRIBUTES_AARCH64;
> +  ChangeMemoryPermissionsSvcArgs.Arg1 = BaseAddress;
> +  ChangeMemoryPermissionsSvcArgs.Arg2 = (Length >= EFI_PAGE_SIZE) ? \
> +                                         Length >> EFI_PAGE_SHIFT :
> + 1;
> +  ChangeMemoryPermissionsSvcArgs.Arg3 = Permissions;
> +
> +  ArmCallSvc(&ChangeMemoryPermissionsSvcArgs);
> +
> +  Status = ChangeMemoryPermissionsSvcArgs.Arg0;
> +
> +  switch (Status) {
> +  case ARM_SVC_SPM_RET_SUCCESS:
> +    Status = EFI_SUCCESS;
> +    break;
> +
> +  case ARM_SVC_SPM_RET_NOT_SUPPORTED:
> +    Status = EFI_UNSUPPORTED;
> +    break;
> +
> +  case ARM_SVC_SPM_RET_INVALID_PARAMS:
> +    Status = EFI_INVALID_PARAMETER;
> +    break;
> +
> +  case ARM_SVC_SPM_RET_DENIED:
> +    Status = EFI_ACCESS_DENIED;
> +    break;
> +
> +  case ARM_SVC_SPM_RET_NO_MEMORY:
> +    Status = EFI_BAD_BUFFER_SIZE;
> +    break;
> +
> +  default:
> +    Status = EFI_ACCESS_DENIED;
> +    ASSERT (0);
> +  }
> +
> +  return Status;
> +}
> +
> +EFI_STATUS
> +ArmSetMemoryRegionNoExec (
> +  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
> +  IN  UINT64                    Length
> +  )
> +{
> +  return RequestMemoryPermissionChange(BaseAddress,
> +                                       Length,
> +                                       SET_MEM_ATTR_MAKE_PERM_REQUEST( \
> +                                         SET_MEM_ATTR_DATA_PERM_RO, \
> +                                         SET_MEM_ATTR_CODE_PERM_XN));
> +}
> +
> +EFI_STATUS
> +ArmClearMemoryRegionNoExec (
> +  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
> +  IN  UINT64                    Length
> +  )
> +{
> +  return RequestMemoryPermissionChange(BaseAddress,
> +                                       Length,
> +                                       SET_MEM_ATTR_MAKE_PERM_REQUEST( \
> +                                         SET_MEM_ATTR_DATA_PERM_RO, \
> +                                         SET_MEM_ATTR_CODE_PERM_X));
> +}
> +
> +EFI_STATUS
> +ArmSetMemoryRegionReadOnly (
> +  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
> +  IN  UINT64                    Length
> +  )
> +{
> +  return RequestMemoryPermissionChange(BaseAddress,
> +                                       Length,
> +                                       SET_MEM_ATTR_MAKE_PERM_REQUEST( \
> +                                         SET_MEM_ATTR_DATA_PERM_RO, \
> +                                         SET_MEM_ATTR_CODE_PERM_XN));
> +}
> +
> +EFI_STATUS
> +ArmClearMemoryRegionReadOnly (
> +  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
> +  IN  UINT64                    Length
> +  )
> +{
> +  return RequestMemoryPermissionChange(BaseAddress,
> +                                       Length,
> +                                       SET_MEM_ATTR_MAKE_PERM_REQUEST( \
> +                                         SET_MEM_ATTR_DATA_PERM_RW, \
> +                                         SET_MEM_ATTR_CODE_PERM_XN));
> +}

The above four functions were written as prototypes in the edk2-staging branch. I do not think they are adequate for upstreaming since each function makes assumptions about the current data or instruction access permission of the input memory region instead of only doing what the function's name suggests.

For example, ArmSetMemoryRegionNoExec() is supposed to only set the XN bit. However, it also sets the data access permission to RO. If the region was RW then this will lead to incorrect behaviour. Ditto for the other functions.

We need a GetMemoryPermission() function that first uses the
ARM_SVC_ID_SP_GET_MEM_ATTRIBUTES_AARCH64 call to obtain the memory attributes of the input region. Each of the above functions must use this new function and only change the data or instruction permission attribute as appropriate.
[Supreeth] Ok. Please see version 2.

cheers,
Achin

> +
> +EFI_STATUS
> +EFIAPI
> +ArmConfigureMmu (
> +  IN  ARM_MEMORY_REGION_DESCRIPTOR  *MemoryTable,
> +  OUT VOID                          **TranslationTableBase OPTIONAL,
> +  OUT UINTN                         *TranslationTableSize OPTIONAL
> +  )
> +{
> +  return EFI_UNSUPPORTED;
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +ArmMmuSecLibConstructor (
> +  IN EFI_HANDLE            ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
> +  )
> +{
> +  return EFI_SUCCESS;
> +}
> --
> 2.16.2
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 05/18] ArmPkg/ArmMmuLib: Add MMU library inf file suitable for use in S-EL0.
  2018-04-11 19:24   ` Achin Gupta
@ 2018-05-04 23:19     ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:19 UTC (permalink / raw)
  To: Achin Gupta; +Cc: edk2-devel@lists.01.org

Thanks.

-----Original Message-----
From: Achin Gupta
Sent: Wednesday, April 11, 2018 2:24 PM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 05/18] ArmPkg/ArmMmuLib: Add MMU library inf file suitable for use in S-EL0.

On Fri, Apr 06, 2018 at 03:42:10PM +0100, Supreeth Venkatesh wrote:
> This patch adds the definitions, sources, packages and library classes
> needed to compile and link MMU Library suitable for use in S-EL0.
>
> Currently, this is used only during the Standalone MM Core
> initialization and hence defined as MM_CORE_STANDALONE Module.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf | 37
> +++++++++++++++++++++++++++++++
>  1 file changed, 37 insertions(+)
>  create mode 100644 ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
>
> diff --git a/ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
> b/ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
> new file mode 100644
> index 0000000000..5c802923da
> --- /dev/null
> +++ b/ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
> @@ -0,0 +1,37 @@
> +#/** @file
> +#
> +#  Copyright (c) 2017, ARM Limited. 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
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, #  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#
> +#**/
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = ArmMmuSecLib
> +  FILE_GUID                      = da8f0232-fb14-42f0-922c-63104d2c70bd
> +  MODULE_TYPE                    = MM_CORE_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  LIBRARY_CLASS                  = ArmMmuSecLib|MM_CORE_STANDALONE
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  CONSTRUCTOR                    = ArmMmuSecLibConstructor
> +
> +[Sources.AARCH64]
> +  AArch64/ArmMmuSecLib.c
> +
> +[Packages]
> +  ArmPkg/ArmPkg.dec
> +  MdePkg/MdePkg.dec
> +
> +[LibraryClasses]
> +  ArmLib
> +  CacheMaintenanceLib
> +  MemoryAllocationLib
> +
> +
> --
> 2.16.2
>

Acked-by: Achin Gupta <achin.gupta@arm.com>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 06/18] StandaloneMmPkg: Add an AArch64 specific entry point library.
  2018-04-16 14:04   ` Achin Gupta
@ 2018-05-04 23:20     ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:20 UTC (permalink / raw)
  To: Achin Gupta; +Cc: edk2-devel@lists.01.org

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Monday, April 16, 2018 9:04 AM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 06/18] StandaloneMmPkg: Add an AArch64 specific entry point library.

Hi Supreeth,

On Fri, Apr 06, 2018 at 03:42:11PM +0100, Supreeth Venkatesh wrote:
> The Standalone MM environment runs in S-EL0 in AArch64 on ARM Standard
> Platforms and is initialised during the SEC phase. ARM Trusted firmware
> in EL3 is responsible for initialising the architectural context for
> S-EL0 and loading the Standalone MM image. The memory allocated to this
> image is marked as RO+X. Heap memory is marked as RW+XN.
>
> Certain actions have to be completed prior to executing the generic code
> in the Standalone MM Core module. These are:
>
> 1. Memory permission attributes for each section of the Standalone MM
>    Core module need to be changed prior to accessing any RW data.
>
> 2. A Hob list has to be created with information that allows the MM
>    environment to initialise and dispatch drivers.
>
> Furthermore, this module is responsible for handing over runtime MM
> events to the Standalone MM CPU driver and returning control to ARM
> Trusted Firmware upon event completion. Hence it needs to know the CPU
> driver entry point.
>
> This patch implements an entry point module that ARM Trusted Firmware
> jumps to in S-EL0. It then performs the above actions before calling the
> Standalone MM Foundation entry point and handling subsequent MM events.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  .../Library/Arm/StandaloneMmCoreEntryPoint.h       | 232 +++++++++++++++++
>  .../Include/Library/MmCoreStandaloneEntryPoint.h   | 101 ++++++++
>  .../StandaloneMmCoreEntryPoint/Arm/CreateHobList.c | 200 +++++++++++++++
>  .../Arm/SetPermissions.c                           | 278 +++++++++++++++++++++
>  .../Arm/StandaloneMmCoreEntryPoint.c               | 264 +++++++++++++++++++
>  .../StandaloneMmCoreEntryPoint.inf                 |  53 ++++
>  StandaloneMmPkg => StandaloneMmPkg~HEAD            |   0
>  7 files changed, 1128 insertions(+)
>  create mode 100644 StandaloneMmPkg/Include/Library/Arm/StandaloneMmCoreEntryPoint.h

The names of this file and the one below are re-arrangements of the same
words. This is confusing. It would be better to stick to one convention. Since
the name of the package starts with StandaloneMm, everything else should follow
suit.

So can this file be renamed to ArmStandaloneMmCoreEntryPoint.h and the one below
to StandaloneMmCoreEntryPoint.h unless you think this can be done differently?
[Supreeth] Ok. I have renamed these as " StandaloneMmCoreEntryPoint.h" and "AArch64/ StandaloneMmCoreEntryPoint.h".

>  create mode 100644 StandaloneMmPkg/Include/Library/MmCoreStandaloneEntryPoint.h

<tag>

>  create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/CreateHobList.c
>  create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/SetPermissions.c

PeCoffExtraActionLib and the HobLib are pre-requisites for these two
files. Should they not appear first in the patch stack?
[Supreeth] Hoblib - yes.
PeCoffExtraActionLib - No, as it has no compilation dependencies. Of course, it will fail at runtime, but PeCoffExtraActionLib has a reverse dependency
on StandaloneMmPkg PCD. Anyway, since this is being sent as a series. I hope it will be checked in together.

>  create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c
>  create mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf

Can this and the tagged file above go in a separate commit? Cannot see how they
are related to the Arm specific entry point.
[Supreeth] I will move the commit of .../Include/Library/StandaloneMmCoreEntryPoint.h  to the "core" commit id. Rest of them are AArch64 specific entry points.

>  rename StandaloneMmPkg => StandaloneMmPkg~HEAD (100%)
>
> diff --git a/StandaloneMmPkg/Include/Library/Arm/StandaloneMmCoreEntryPoint.h b/StandaloneMmPkg/Include/Library/Arm/StandaloneMmCoreEntryPoint.h
> new file mode 100644
> index 0000000000..029c6c476c
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Library/Arm/StandaloneMmCoreEntryPoint.h
> @@ -0,0 +1,232 @@
> +/** @file
> +  Entry point to the Standalone MM Foundation when initialised during the SEC
> +  phase on ARM platforms
> +
> +Copyright (c) 2017, ARM Ltd. All rights reserved.<BR>

Copyright year needs to be updated.
[Supreeth] Ok.

> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef __MODULE_ENTRY_POINT_H__
> +#define __MODULE_ENTRY_POINT_H__

Name of this define needs to be changed
[Supreeth] Ok.

> +
> +#include <Library/PeCoffLib.h>
> +#include <Library/FvLib.h>
> +
> +#define CPU_INFO_FLAG_PRIMARY_CPU  0x00000001
> +
> +typedef
> +EFI_STATUS
> +(*PI_MM_CPU_TP_FW_ENTRYPOINT) (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  );

This typedef is not being used.
[Supreeth] Ok. Removed.

> +
> +typedef struct {
> +  UINT8  Type;       /* type of the structure */
> +  UINT8  Version;    /* version of this structure */
> +  UINT16 Size;      /* size of this structure in bytes */
> +  UINT32 Attr;      /* attributes: unused bits SBZ */
> +} EFI_PARAM_HEADER;
> +
> +typedef struct {
> +  UINT64 Mpidr;
> +  UINT32 LinearId;
> +  UINT32 Flags;
> +} EFI_SECURE_PARTITION_CPU_INFO;
> +
> +typedef struct {
> +  EFI_PARAM_HEADER              Header;
> +  UINT64                        SpMemBase;
> +  UINT64                        SpMemLimit;
> +  UINT64                        SpImageBase;
> +  UINT64                        SpStackBase;
> +  UINT64                        SpHeapBase;
> +  UINT64                        SpNsCommBufBase;
> +  UINT64                        SpSharedBufBase;
> +  UINT64                        SpImageSize;
> +  UINT64                        SpPcpuStackSize;
> +  UINT64                        SpHeapSize;
> +  UINT64                        SpNsCommBufSize;
> +  UINT64                        SpPcpuSharedBufSize;
> +  UINT32                        NumSpMemRegions;
> +  UINT32                        NumCpus;
> +  EFI_SECURE_PARTITION_CPU_INFO *CpuInfo;
> +} EFI_SECURE_PARTITION_BOOT_INFO;
> +
> +typedef struct {
> +  EFI_PARAM_HEADER               h;
> +  UINT64                         SpStackBase;
> +  UINT64                         SpSharedBufBase;
> +  UINT32                         SpPcpuStackSize;
> +  UINT32                         SpPcpuSharedBufSize;
> +  EFI_SECURE_PARTITION_CPU_INFO  CpuInfo;
> +} EFI_SECURE_PARTITION_WARM_BOOT_INFO;

This data structure is not being used.
[Supreeth] Ok. Removed.

> +
> +
> +typedef
> +EFI_STATUS
> +(*PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT) (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  );

This prototype interface is not adequate for upstreaming. I am thinking of the
following:

typedef
EFI_STATUS
(*PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT) (
  IN UINT64 EventInfoAddr,
  IN UINT32 EventInfoSize,
  IN UINT32 CpuNumber
  );

[Supreeth] I am Ok with it. However, we should still use "UINTN" as data types, as these are derived from either SMC or SVC arguments and
they depend on  register length which is architecture specific. (not that we are supporting 32 bit but still future proofing it).

When the MM_COMMUNICATE SMC is invoked, EventInfoAddr -> MM communication
buffer. Ditto for EventInfoSize.

I don't think there is a need to pass EventId any longer. The original thinking
was that for asynchronous events e.g. interrupts handled in an MM partition, Arm
TF will not use UUIDs. Instead EventIDs will be used. Hence there was a need to
convert an EventID into a UUID in the MM CPU Driver. It would then use the UUID
to call SmiManage(). This restriction is no longer true. So, the SPM can
delegate an asynchronous event using the GUID of a StandaloneMM driver just like
the normal world populates the communication buffer.

This will result in changes to the MM CPU driver. I will comment on those
separately.

We also need to tweak the ABI during an ERET from SP_EVENT_COMPLETE to the
following.

x0 -> Address of versioned data structure populated in RO memory shared between
      SPM and SP + Paylod
x1 -> MBZ. Reserved.
x2 -> MBZ. Reserved.
x3 -> MBZ. Reserved.

The versioned data structure could look like this:

typedef struct {^M
  EFI_PARAM_HEADER              Header;^M
  UINT32                        CpuNumber;^M
  UINT64                        EventBufferAddress;^M // MM Communication buffer
  UINT32                        EventBufferSize;^M    // MM Comm. buffer size
} EFI_SECURE_PARTITION_EVENT_INFO;^M

The point being that just like the StandaloneMmCoreEntryPoint and SPM share a
data structure to pass information during initialisation, the above data
structure will be used to pass information when an event has to be delegated
from the SPM.
[Supreeth] Yes. But Currently, up-streamed arm-tf gets back event complete ID from Arg0 and status from Arg1.
Hence these changes in edk2 should be up-streamed when arm-tf completes the implementation as per new SPRT interface.

> +
> +typedef struct {
> +  PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT *ArmTfCpuDriverEpPtr;
> +} ARM_TF_CPU_DRIVER_EP_DESCRIPTOR;
> +
> +typedef RETURN_STATUS (*REGION_PERMISSION_UPDATE_FUNC) (
> +  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
> +  IN  UINT64                    Length
> +  );
> +
> +/**
> +  Privileged firmware assigns RO & Executable attributes to all memory occupied
> +  by the Boot Firmware Volume. This function sets the correct permissions of
> +  sections in the Standalone MM Core module to be able to access RO and RW data
> +  and make further progress in the boot process.
> +
> +  @param  ImageContext           Pointer to PE/COFF image context
> +  @param  SectionHeaderOffset    Offset of PE/COFF image section header
> +  @param  NumberOfSections       Number of Sections
> +  @param  TextUpdater            Function to change code permissions
> +  @param  ReadOnlyUpdater        Function to change RO permissions
> +  @param  ReadWriteUpdater       Function to change RW permissions
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +UpdateMmFoundationPeCoffPermissions (
> +  IN  CONST PE_COFF_LOADER_IMAGE_CONTEXT      *ImageContext,
> +  IN  UINT32                                  SectionHeaderOffset,
> +  IN  CONST  UINTN                            NumberOfSections,

UINT32?
[Supreeth] NumberOfSections is actually UINT16 as per as the peimage.h header file.

> +  IN  REGION_PERMISSION_UPDATE_FUNC           TextUpdater,
> +  IN  REGION_PERMISSION_UPDATE_FUNC           ReadOnlyUpdater,
> +  IN  REGION_PERMISSION_UPDATE_FUNC           ReadWriteUpdater
> +  );
> +
> +
> +/**
> +  Privileged firmware assigns RO & Executable attributes to all memory occupied
> +  by the Boot Firmware Volume. This function locates the section information of
> +  the Standalone MM Core module to be able to change permissions of the
> +  individual sections later in the boot process.
> +
> +  @param  TeData                 Pointer to PE/COFF image data
> +  @param  ImageContext           Pointer to PE/COFF image context
> +  @param  SectionHeaderOffset    Offset of PE/COFF image section header
> +  @param  NumberOfSections       Number of Sections
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +GetStandaloneMmCorePeCoffSections (
> +  IN        VOID                            *TeData,
> +  IN  OUT   PE_COFF_LOADER_IMAGE_CONTEXT    *ImageContext,
> +  IN  OUT   UINT32                          *SectionHeaderOffset;
> +  IN  OUT   UINTN                           *NumberOfSections;
> +  );
> +
> +
> +/**
> +  Privileged firmware assigns RO & Executable attributes to all memory occupied
> +  by the Boot Firmware Volume. This function locates the Standalone MM Core
> +  module PE/COFF image in the BFV and returns this information.
> +
> +  @param  BfvAddress             Base Address of Boot Firmware Volume
> +  @param  TeData                 Pointer to address for allocating memory for
> +                                 PE/COFF image data
> +  @param  TeDataSize             Pointer to size of PE/COFF image data
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +LocateStandaloneMmCorePeCoffData (
> +  IN        EFI_FIRMWARE_VOLUME_HEADER      *BfvAddress,
> +  IN  OUT   VOID                            **TeData;

UINT64**?

> +  IN  OUT   UINTN                           *TeDataSize;

UINT32?

[Supreeth] FfsFindSectionData (
  IN EFI_SECTION_TYPE              SectionType,
  IN EFI_FFS_FILE_HEADER           *FfsFileHeader,
  OUT VOID                         **SectionData,
  OUT UINTN                        *SectionDataSize
  );
In FV Library has these, so not changing them.

> +  );
> +
> +
> +/**
> +  Use the boot information passed by privileged firmware to populate a HOB list
> +  suitable for consumption by the MM Core and drivers.
> +
> +  @param  CpuDriverEntryPoint    Address of MM CPU driver entrypoint
> +  @param  PayloadBootInfo        Boot information passed by privileged firmware
> +
> +**/
> +VOID *
> +EFIAPI
> +CreateHobListFromBootInfo (
> +  IN  OUT  PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT *CpuDriverEntryPoint,
> +  IN       EFI_SECURE_PARTITION_BOOT_INFO     *PayloadBootInfo
> +  );
> +
> +
> +/**
> +  The entry point of Standalone MM Foundation.
> +
> +  @param  HobStart  Pointer to the beginning of the HOB List.

Comment needs to be updated.
[Supreeth] Ok. Done.

> +
> +**/
> +VOID
> +EFIAPI
> +_ModuleEntryPoint (
> +  IN VOID    *SharedBufAddress,
> +  IN UINT64  SharedBufSize,
> +  IN UINT64  cookie1,
> +  IN UINT64  cookie2
> +  );

The ABI for the first ERET into the partition needs to be tweaked. I don't think
we need SharedBufSize as this is discoverable from Header.Size in
EFI_SECURE_PARTITION_BOOT_INFO.
[Supreeth] Ok. Done.

> +
> +
> +/**
> +  Autogenerated function that calls the library constructors for all of the module's dependent libraries.
> +
> +  This function must be called by _ModuleEntryPoint().
> +  This function calls the set of library constructors for the set of library instances
> +  that a module depends on.  This includes library instances that a module depends on
> +  directly and library instances that a module depends on indirectly through other
> +  libraries. This function is autogenerated by build tools and those build tools are
> +  responsible for collecting the set of library instances, determine which ones have
> +  constructors, and calling the library constructors in the proper order based upon
> +  each of the library instances own dependencies.
> +
> +  @param  ImageHandle  The image handle of the DXE Core.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +**/
> +VOID
> +EFIAPI
> +ProcessLibraryConstructorList (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE  *MmSystemTable
> +  );
> +
> +
> +/**
> +  Autogenerated function that calls a set of module entry points.
> +
> +  This function must be called by _ModuleEntryPoint().
> +  This function calls the set of module entry points.
> +  This function is autogenerated by build tools and those build tools are responsible
> +  for collecting the module entry points and calling them in a specified order.
> +
> +  @param  HobStart  Pointer to the beginning of the HOB List passed in from the PEI Phase.
> +
> +**/
> +VOID
> +EFIAPI
> +ProcessModuleEntryPointList (
> +  IN VOID  *HobStart
> +  );
> +
> +#endif
> diff --git a/StandaloneMmPkg/Include/Library/MmCoreStandaloneEntryPoint.h b/StandaloneMmPkg/Include/Library/MmCoreStandaloneEntryPoint.h
> new file mode 100644
> index 0000000000..d6105d4935
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Library/MmCoreStandaloneEntryPoint.h
> @@ -0,0 +1,101 @@
> +/** @file
> +  Module entry point library for DXE core.

This comment needs to be changed.
[Supreeth] Ok. Done.
> +
> +Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>

Copyright year here and everywhere else.
[Supreeth] Ok. Done.
> +
> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef __MODULE_ENTRY_POINT_H__
> +#define __MODULE_ENTRY_POINT_H__
> +
> +///
> +/// Global variable that contains a pointer to the Hob List passed into the DXE Core entry point.
> +///
> +extern VOID  *gHobList;
> +
> +
> +/**
> +  The entry point of PE/COFF Image for the DXE Core.
> +
> +  This function is the entry point for the DXE Core. This function is required to call
> +  ProcessModuleEntryPointList() and ProcessModuleEntryPointList() is never expected to return.
> +  The DXE Core is responsible for calling ProcessLibraryConstructorList() as soon as the EFI
> +  System Table and the image handle for the DXE Core itself have been established.
> +  If ProcessModuleEntryPointList() returns, then ASSERT() and halt the system.

This comment needs to be changed as this is not the DXE core.
[Supreeth] Ok. Done.

> +
> +  @param  HobStart  Pointer to the beginning of the HOB List passed in from the PEI Phase.
> +
> +**/
> +VOID
> +EFIAPI
> +_ModuleEntryPoint (
> +  IN VOID  *HobStart
> +  );
> +
> +
> +/**
> +  Required by the EBC compiler and identical in functionality to _ModuleEntryPoint().
> +
> +  This function is required to call _ModuleEntryPoint() passing in HobStart.
> +
> +  @param  HobStart  Pointer to the beginning of the HOB List passed in from the PEI Phase.
> +
> +**/
> +VOID
> +EFIAPI
> +EfiMain (
> +  IN VOID  *HobStart
> +  );
> +
> +
> +/**
> +  Autogenerated function that calls the library constructors for all of the module's dependent libraries.
> +
> +  This function must be called by _ModuleEntryPoint().
> +  This function calls the set of library constructors for the set of library instances
> +  that a module depends on.  This includes library instances that a module depends on
> +  directly and library instances that a module depends on indirectly through other
> +  libraries. This function is autogenerated by build tools and those build tools are
> +  responsible for collecting the set of library instances, determine which ones have
> +  constructors, and calling the library constructors in the proper order based upon
> +  each of the library instances own dependencies.
> +
> +  @param  ImageHandle  The image handle of the DXE Core.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +**/
> +VOID
> +EFIAPI
> +ProcessLibraryConstructorList (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  );
> +
> +
> +/**
> +  Autogenerated function that calls a set of module entry points.
> +
> +  This function must be called by _ModuleEntryPoint().
> +  This function calls the set of module entry points.
> +  This function is auto generated by build tools and those build tools are responsible
> +  for collecting the module entry points and calling them in a specified order.
> +
> +  @param  HobStart  Pointer to the beginning of the HOB List passed in from the PEI Phase.
> +
> +**/
> +VOID
> +EFIAPI
> +ProcessModuleEntryPointList (
> +  IN VOID  *HobStart
> +  );
> +
> +#endif
> diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/CreateHobList.c b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/CreateHobList.c
> new file mode 100644
> index 0000000000..f9b3faea8f
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/CreateHobList.c
> @@ -0,0 +1,200 @@
> +/** @file
> +  Entry point to the Standalone MM Foundation when initialized during the SEC
> +  phase on ARM platforms

The comment needs to be updated.
[Supreeth] Ok. Done.
> +
> +Copyright (c) 2017, ARM Ltd. All rights reserved.<BR>
> +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.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +
> +#include <PiMm.h>
> +
> +#include <PiPei.h>
> +#include <Guid/MmramMemoryReserve.h>
> +#include <Guid/MpInformation.h>
> +
> +#include <Library/Arm/StandaloneMmCoreEntryPoint.h>
> +#include <Library/ArmMmuLib.h>
> +#include <Library/ArmSvcLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/HobLib.h>
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/SerialPortLib.h>
> +
> +#include <IndustryStandard/ArmStdSmc.h>
> +
> +extern EFI_HOB_HANDOFF_INFO_TABLE*
> +HobConstructor (
> +  IN VOID   *EfiMemoryBegin,
> +  IN UINTN  EfiMemoryLength,

UINT32 or UINT64?
[Supreeth] UINTN is better.

> +  IN VOID   *EfiFreeMemoryBottom,
> +  IN VOID   *EfiFreeMemoryTop
> +  );
> +
> +// GUID to identify HOB with whereabouts of communication buffer with Normal
> +// World
> +extern EFI_GUID gEfiStandaloneMmNonSecureBufferGuid;
> +
> +// GUID to identify HOB where the entry point of the CPU driver will be
> +// populated to allow this entry point driver to invoke it upon receipt of an
> +// event
> +extern EFI_GUID gEfiArmTfCpuDriverEpDescriptorGuid;
> +
> +/**
> +  Use the boot information passed by privileged firmware to populate a HOB list
> +  suitable for consumption by the MM Core and drivers.
> +
> +  @param  PayloadBootInfo    Boot information passed by privileged firmware
> +
> +**/
> +VOID *
> +CreateHobListFromBootInfo (
> +  IN  OUT  PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT *CpuDriverEntryPoint,
> +  IN       EFI_SECURE_PARTITION_BOOT_INFO     *PayloadBootInfo
> +)
> +{
> +  EFI_HOB_HANDOFF_INFO_TABLE      *HobStart;
> +  EFI_RESOURCE_ATTRIBUTE_TYPE     Attributes;
> +  UINT32                          Index;
> +  UINT32                          BufferSize;
> +  UINT32                          Flags;
> +  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHob;
> +  EFI_MMRAM_DESCRIPTOR            *MmramRanges;
> +  EFI_MMRAM_DESCRIPTOR            *NsCommBufMmramRange;
> +  MP_INFORMATION_HOB_DATA         *MpInformationHobData;
> +  EFI_PROCESSOR_INFORMATION       *ProcInfoBuffer;
> +  EFI_SECURE_PARTITION_CPU_INFO   *CpuInfo;
> +  ARM_TF_CPU_DRIVER_EP_DESCRIPTOR *CpuDriverEntryPointDesc;
> +
> +  // Create a hoblist with a PHIT and EOH
> +  HobStart = HobConstructor ((VOID *) PayloadBootInfo->SpMemBase,
> +    (UINTN)  PayloadBootInfo->SpMemLimit - PayloadBootInfo->SpMemBase,
> +    (VOID *) PayloadBootInfo->SpHeapBase,
> +    (VOID *) (PayloadBootInfo->SpHeapBase + PayloadBootInfo->SpHeapSize));
> +
> +  // Check that the Hoblist starts at the bottom of the Heap
> +  ASSERT (HobStart == (VOID *) PayloadBootInfo->SpHeapBase);
> +
> +  // Build a Boot Firmware Volume HOB
> +  BuildFvHob(PayloadBootInfo->SpImageBase, PayloadBootInfo->SpImageSize);
> +
> +  // Build a resource descriptor Hob that describes the available physical
> +  // memory range
> +  Attributes =(
> +    EFI_RESOURCE_ATTRIBUTE_PRESENT |
> +    EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
> +    EFI_RESOURCE_ATTRIBUTE_TESTED |
> +    EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
> +    EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
> +    EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
> +    EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
> +  );
> +
> +  BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,
> +    Attributes,
> +    (UINTN) PayloadBootInfo->SpMemBase,
> +    PayloadBootInfo->SpMemLimit - PayloadBootInfo->SpMemBase);
> +
> +  // Find the size of the GUIDed HOB with MP information
> +  BufferSize = sizeof (MP_INFORMATION_HOB_DATA);
> +  BufferSize += sizeof (EFI_PROCESSOR_INFORMATION) * PayloadBootInfo->NumCpus;
> +
> +  // Create a Guided MP information HOB to enable the ARM TF CPU driver to
> +  // perform per-cpu allocations.
> +  MpInformationHobData = BuildGuidHob(&gMpInformationHobGuid, BufferSize);
> +
> +  // Populate the MP information HOB with the topology information passed by
> +  // privileged firmware
> +  MpInformationHobData->NumberOfProcessors = PayloadBootInfo->NumCpus;
> +  MpInformationHobData->NumberOfEnabledProcessors = PayloadBootInfo->NumCpus;
> +  ProcInfoBuffer = MpInformationHobData->ProcessorInfoBuffer;
> +  CpuInfo = PayloadBootInfo->CpuInfo;
> +
> +  for (Index = 0; Index < PayloadBootInfo->NumCpus; Index++) {
> +    ProcInfoBuffer[Index].ProcessorId      = CpuInfo[Index].Mpidr;
> +    ProcInfoBuffer[Index].Location.Package = GET_CLUSTER_ID(CpuInfo[Index].Mpidr);
> +    ProcInfoBuffer[Index].Location.Core    = GET_CORE_ID(CpuInfo[Index].Mpidr);
> +    ProcInfoBuffer[Index].Location.Thread  = GET_CORE_ID(CpuInfo[Index].Mpidr);
> +
> +    Flags = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;
> +    if (CpuInfo[Index].Flags & CPU_INFO_FLAG_PRIMARY_CPU) {
> +      Flags |= PROCESSOR_AS_BSP_BIT;
> +    }
> +    ProcInfoBuffer[Index].StatusFlag = Flags;
> +  }
> +
> +  // Create a Guided HOB to tell the ARM TF CPU driver the location and length
> +  // of the communication buffer shared with the Normal world.
> +  NsCommBufMmramRange = (EFI_MMRAM_DESCRIPTOR *) BuildGuidHob (&gEfiStandaloneMmNonSecureBufferGuid, sizeof(EFI_MMRAM_DESCRIPTOR));
> +  NsCommBufMmramRange->PhysicalStart = PayloadBootInfo->SpNsCommBufBase;
> +  NsCommBufMmramRange->CpuStart      = PayloadBootInfo->SpNsCommBufBase;
> +  NsCommBufMmramRange->PhysicalSize  = PayloadBootInfo->SpNsCommBufSize;
> +  NsCommBufMmramRange->RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
> +
> +  // Create a Guided HOB to enable the ARM TF CPU driver to share its entry
> +  // point and populate it with the address of the shared buffer
> +  CpuDriverEntryPointDesc = (ARM_TF_CPU_DRIVER_EP_DESCRIPTOR *) BuildGuidHob (&gEfiArmTfCpuDriverEpDescriptorGuid, sizeof(ARM_TF_CPU_DRIVER_EP_DESCRIPTOR));
> +
> +  *CpuDriverEntryPoint = NULL;
> +  CpuDriverEntryPointDesc->ArmTfCpuDriverEpPtr = CpuDriverEntryPoint;
> +
> +  // Find the size of the GUIDed HOB with SRAM ranges
> +  BufferSize = sizeof (EFI_MMRAM_HOB_DESCRIPTOR_BLOCK);
> +  BufferSize += PayloadBootInfo->NumSpMemRegions *
> +    sizeof(EFI_MMRAM_DESCRIPTOR);
> +
> +  // Create a GUIDed HOB with SRAM ranges
> +  MmramRangesHob = BuildGuidHob (&gEfiMmPeiMmramMemoryReserveGuid, BufferSize);
> +
> +  // Fill up the number of MMRAM memory regions
> +  MmramRangesHob->NumberOfMmReservedRegions = PayloadBootInfo->NumSpMemRegions;
> +  // Fill up the MMRAM ranges
> +  MmramRanges = &MmramRangesHob->Descriptor[0];
> +
> +  // Base and size of memory occupied by the Standalone MM image
> +  MmramRanges[0].PhysicalStart = PayloadBootInfo->SpImageBase;
> +  MmramRanges[0].CpuStart      = PayloadBootInfo->SpImageBase;
> +  MmramRanges[0].PhysicalSize  = PayloadBootInfo->SpImageSize;
> +  MmramRanges[0].RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
> +
> +  // Base and size of buffer shared with privileged Secure world software
> +  MmramRanges[1].PhysicalStart = PayloadBootInfo->SpSharedBufBase;
> +  MmramRanges[1].CpuStart      = PayloadBootInfo->SpSharedBufBase;
> +  MmramRanges[1].PhysicalSize  = PayloadBootInfo->SpPcpuSharedBufSize * PayloadBootInfo->NumCpus;
> +  MmramRanges[1].RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
> +
> +  // Base and size of buffer used for synchronous communication with Normal
> +  // world software
> +  MmramRanges[2].PhysicalStart = PayloadBootInfo->SpNsCommBufBase;
> +  MmramRanges[2].CpuStart      = PayloadBootInfo->SpNsCommBufBase;
> +  MmramRanges[2].PhysicalSize  = PayloadBootInfo->SpNsCommBufSize;
> +  MmramRanges[2].RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
> +
> +  // Base and size of memory allocated for stacks for all cpus
> +  MmramRanges[3].PhysicalStart = PayloadBootInfo->SpStackBase;
> +  MmramRanges[3].CpuStart      = PayloadBootInfo->SpStackBase;
> +  MmramRanges[3].PhysicalSize  = PayloadBootInfo->SpPcpuStackSize * PayloadBootInfo->NumCpus;
> +  MmramRanges[3].RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
> +
> +  // Base and size of heap memory shared by all cpus
> +  MmramRanges[4].PhysicalStart = (EFI_PHYSICAL_ADDRESS) HobStart;
> +  MmramRanges[4].CpuStart      = (EFI_PHYSICAL_ADDRESS) HobStart;
> +  MmramRanges[4].PhysicalSize  = HobStart->EfiFreeMemoryBottom - (EFI_PHYSICAL_ADDRESS) HobStart;
> +  MmramRanges[4].RegionState   = EFI_CACHEABLE | EFI_ALLOCATED;
> +
> +  // Base and size of heap memory shared by all cpus
> +  MmramRanges[5].PhysicalStart = HobStart->EfiFreeMemoryBottom;
> +  MmramRanges[5].CpuStart      = HobStart->EfiFreeMemoryBottom;
> +  MmramRanges[5].PhysicalSize  = HobStart->EfiFreeMemoryTop - HobStart->EfiFreeMemoryBottom;
> +  MmramRanges[5].RegionState   = EFI_CACHEABLE;
> +
> +  return HobStart;
> +}
> diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/SetPermissions.c b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/SetPermissions.c
> new file mode 100644
> index 0000000000..e96b81cdc0
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/SetPermissions.c

This file needs to be reviewed by Ard as it is heavily based upon his original
work and has not changed much since I wrote it.
[Supreeth] Yes. He is on the reviewers list.

> @@ -0,0 +1,278 @@
> +/** @file
> +  Entry point to the Standalone MM Foundation when initialised during the SEC
> +  phase on ARM platforms

Comment needs to be updated.
[Supreeth] Yes. Done.
> +
> +Copyright (c) 2017, ARM Ltd. All rights reserved.<BR>
> +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.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +
> +#include <PiMm.h>
> +
> +
> +
> +#include <PiPei.h>
> +#include <Guid/MmramMemoryReserve.h>
> +#include <Guid/MpInformation.h>
> +
> +#include <Library/Arm/StandaloneMmCoreEntryPoint.h>
> +#include <Library/ArmMmuLib.h>
> +#include <Library/ArmSvcLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/HobLib.h>
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/SerialPortLib.h>
> +
> +
> +#include <IndustryStandard/ArmStdSmc.h>
> +
> +EFI_STATUS
> +EFIAPI
> +UpdateMmFoundationPeCoffPermissions (
> +  IN  CONST PE_COFF_LOADER_IMAGE_CONTEXT      *ImageContext,
> +  IN  UINT32                                  SectionHeaderOffset,
> +  IN  CONST  UINTN                            NumberOfSections,

UINT32?
[Supreeth] its actually UINT16.

> +  IN  REGION_PERMISSION_UPDATE_FUNC           TextUpdater,
> +  IN  REGION_PERMISSION_UPDATE_FUNC           ReadOnlyUpdater,
> +  IN  REGION_PERMISSION_UPDATE_FUNC           ReadWriteUpdater
> +  )
> +{
> +  EFI_IMAGE_SECTION_HEADER         SectionHeader;
> +  RETURN_STATUS                    Status;
> +  EFI_PHYSICAL_ADDRESS             Base;
> +  UINTN                            Size;
> +  UINTN                            ReadSize;
> +  UINTN                            Index;
> +
> +  ASSERT (ImageContext != NULL);
> +
> +  //
> +  // Iterate over the sections
> +  //
> +  for (Index = 0; Index < NumberOfSections; Index++) {
> +    //
> +    // Read section header from file
> +    //
> +    Size = sizeof (EFI_IMAGE_SECTION_HEADER);
> +    ReadSize = Size;
> +    Status = ImageContext->ImageRead (ImageContext->Handle, SectionHeaderOffset,
> +                                   &Size, &SectionHeader);
> +    if (RETURN_ERROR (Status) || (Size != ReadSize)) {
> +      DEBUG ((DEBUG_ERROR,
> +        "%a: ImageContext->ImageRead () failed (Status = %r)\n",
> +        __FUNCTION__, Status));
> +      return Status;
> +    }
> +
> +    DEBUG ((DEBUG_INFO,
> +            "%a: Section %d of image at 0x%lx has 0x%x permissions\n",
> +            __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.Characteristics));
> +    DEBUG ((DEBUG_INFO,
> +            "%a: Section %d of image at 0x%lx has %s name\n",
> +            __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.Name));
> +    DEBUG ((DEBUG_INFO,
> +            "%a: Section %d of image at 0x%lx has 0x%x address\n",
> +            __FUNCTION__, Index, ImageContext->ImageAddress, ImageContext->ImageAddress + SectionHeader.VirtualAddress));
> +    DEBUG ((DEBUG_INFO,
> +            "%a: Section %d of image at 0x%lx has 0x%x data\n",
> +            __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.PointerToRawData));
> +
> +    //
> +    // If the section is marked as XN then remove the X attribute. Furthermore,
> +    // if it is a writeable section then mark it appropriately as well.
> +    //
> +    if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_MEM_EXECUTE) == 0) {
> +      Base = ImageContext->ImageAddress + SectionHeader.VirtualAddress;
> +
> +      TextUpdater (Base, SectionHeader.Misc.VirtualSize);
> +
> +      if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_MEM_WRITE) != 0) {
> +        ReadWriteUpdater (Base, SectionHeader.Misc.VirtualSize);
> +        DEBUG ((DEBUG_INFO,
> +                "%a: Mapping section %d of image at 0x%lx with RW-XN permissions\n",
> +                __FUNCTION__, Index, ImageContext->ImageAddress));
> +      } else {
> +        DEBUG ((DEBUG_INFO,
> +                "%a: Mapping section %d of image at 0x%lx with RO-XN permissions\n",
> +                __FUNCTION__, Index, ImageContext->ImageAddress));
> +      }
> +    } else {
> +        DEBUG ((DEBUG_INFO,
> +                "%a: Ignoring section %d of image at 0x%lx with 0x%x permissions\n",
> +                __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.Characteristics));
> +    }
> +    SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
> +  }
> +
> +  return RETURN_SUCCESS;
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +LocateStandaloneMmCorePeCoffData (
> +  IN        EFI_FIRMWARE_VOLUME_HEADER      *BfvAddress,
> +  IN  OUT   VOID                            **TeData,
> +  IN  OUT   UINTN                           *TeDataSize
> +  )
> +{
> +  EFI_FFS_FILE_HEADER             *FileHeader = NULL;
> +  EFI_STATUS                      Status;
> +
> +  Status = FfsFindNextFile (
> +                  EFI_FV_FILETYPE_SECURITY_CORE,
> +                  BfvAddress,
> +                  &FileHeader);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_ERROR, "Unable to locate Standalone MM FFS file - 0x%x\n",
> +      Status));
> +    return Status;
> +  }
> +
> +  Status = FfsFindSectionData (EFI_SECTION_PE32, FileHeader, TeData, TeDataSize);
> +  if (EFI_ERROR (Status)) {
> +      DEBUG ((DEBUG_ERROR, "Unable to locate Standalone MM Section data - 0x%x\n",
> +              Status));
> +    return Status;
> +  }
> +
> +  DEBUG ((DEBUG_INFO, "Found Standalone MM PE data - 0x%x\n", *TeData));
> +  return Status;
> +}
> +
> +static
> +EFI_STATUS
> +GetPeCoffSectionInformation (
> +  IN  CONST PE_COFF_LOADER_IMAGE_CONTEXT      *ImageContext,
> +  IN  OUT   PE_COFF_LOADER_IMAGE_CONTEXT      *TmpContext,
> +  IN  OUT   UINT32                            *SectionHeaderOffset,
> +  IN  OUT   UINTN                             *NumberOfSections
> +  )
> +{
> +  RETURN_STATUS                         Status;
> +  EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION   Hdr;
> +  EFI_IMAGE_OPTIONAL_HEADER_UNION       HdrData;
> +  UINTN                                 Size;
> +  UINTN                                 ReadSize;
> +
> +  ASSERT (ImageContext != NULL);
> +  ASSERT (TmpContext != NULL);
> +  ASSERT (SectionHeaderOffset != NULL);
> +  ASSERT (NumberOfSections != NULL);
> +
> +  //
> +  // We need to copy ImageContext since PeCoffLoaderGetImageInfo ()
> +  // will mangle the ImageAddress field
> +  //
> +  CopyMem (TmpContext, ImageContext, sizeof (*TmpContext));
> +
> +  if (TmpContext->PeCoffHeaderOffset == 0) {
> +    Status = PeCoffLoaderGetImageInfo (TmpContext);
> +    if (RETURN_ERROR (Status)) {
> +      DEBUG ((DEBUG_ERROR,
> +        "%a: PeCoffLoaderGetImageInfo () failed (Status = %r)\n",
> +        __FUNCTION__, Status));
> +      return Status;
> +    }
> +  }
> +
> +  if (TmpContext->IsTeImage &&
> +      TmpContext->ImageAddress == ImageContext->ImageAddress) {
> +    DEBUG ((DEBUG_INFO, "%a: ignoring XIP TE image at 0x%lx\n", __FUNCTION__,
> +      ImageContext->ImageAddress));
> +    return RETURN_UNSUPPORTED;
> +  }
> +
> +  if (TmpContext->SectionAlignment < EFI_PAGE_SIZE) {
> +    //
> +    // The sections need to be at least 4 KB aligned, since that is the
> +    // granularity at which we can tighten permissions.
> +    //
> +    if (!TmpContext->IsTeImage) {
> +      DEBUG ((DEBUG_WARN,
> +        "%a: non-TE Image at 0x%lx has SectionAlignment < 4 KB (%lu)\n",
> +        __FUNCTION__, ImageContext->ImageAddress, TmpContext->SectionAlignment));
> +    }
> +    return RETURN_UNSUPPORTED;
> +  }
> +
> +  //
> +  // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
> +  // data, but that should not hurt anything. Hdr.Pe32->OptionalHeader.Magic
> +  // determines if this is a PE32 or PE32+ image. The magic is in the same
> +  // location in both images.
> +  //
> +  Hdr.Union = &HdrData;
> +  Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);
> +  ReadSize = Size;
> +  Status = TmpContext->ImageRead (TmpContext->Handle,
> +                         TmpContext->PeCoffHeaderOffset, &Size, Hdr.Pe32);
> +  if (RETURN_ERROR (Status) || (Size != ReadSize)) {
> +    DEBUG ((DEBUG_ERROR,
> +      "%a: TmpContext->ImageRead () failed (Status = %r)\n",
> +      __FUNCTION__, Status));
> +    return Status;
> +  }
> +
> +  ASSERT (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE);
> +
> +  *SectionHeaderOffset = TmpContext->PeCoffHeaderOffset + sizeof (UINT32) +
> +                        sizeof (EFI_IMAGE_FILE_HEADER);
> +  *NumberOfSections    = (UINTN)(Hdr.Pe32->FileHeader.NumberOfSections);
> +
> +  switch (Hdr.Pe32->OptionalHeader.Magic) {
> +    case EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC:
> +      *SectionHeaderOffset += Hdr.Pe32->FileHeader.SizeOfOptionalHeader;
> +      break;
> +    case EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC:
> +      *SectionHeaderOffset += Hdr.Pe32Plus->FileHeader.SizeOfOptionalHeader;
> +      break;
> +    default:
> +      ASSERT (FALSE);
> +  }
> +
> +  return RETURN_SUCCESS;
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +GetStandaloneMmCorePeCoffSections (
> +  IN        VOID                            *TeData,
> +  IN  OUT   PE_COFF_LOADER_IMAGE_CONTEXT    *ImageContext,
> +  IN  OUT   UINT32                          *SectionHeaderOffset,
> +  IN  OUT   UINTN                           *NumberOfSections
> +  )
> +{
> +  EFI_STATUS                   Status;
> +  PE_COFF_LOADER_IMAGE_CONTEXT TmpContext;
> +
> +  // Initialize the Image Context
> +  ZeroMem (ImageContext, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT));
> +  ImageContext->Handle    = TeData;
> +  ImageContext->ImageRead = PeCoffLoaderImageReadFromMemory;
> +
> +  DEBUG((DEBUG_INFO, "Found Standalone MM PE data - 0x%x\n", TeData));
> +
> +  Status = PeCoffLoaderGetImageInfo (ImageContext);
> +  if (EFI_ERROR(Status)) {
> +    DEBUG((DEBUG_ERROR, "Unable to locate Standalone MM Core PE-COFF Image information - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  Status = GetPeCoffSectionInformation(ImageContext, &TmpContext, SectionHeaderOffset, NumberOfSections);
> +  if (EFI_ERROR(Status)) {
> +    DEBUG((DEBUG_ERROR, "Unable to locate Standalone MM Core PE-COFF Section information - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  DEBUG((DEBUG_INFO, "Standalone MM Core PE-COFF SectionHeaderOffset - 0x%x, NumberOfSections - %d\n", *SectionHeaderOffset, *NumberOfSections));
> +
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c
> new file mode 100644
> index 0000000000..72e3b834d4
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c
> @@ -0,0 +1,264 @@
> +/** @file
> +  Entry point to the Standalone MM Foundation when initialized during the SEC
> +  phase on ARM platforms
> +
> +Copyright (c) 2017, ARM Ltd. All rights reserved.<BR>
> +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.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +
> +#include <PiMm.h>
> +
> +#include <Library/Arm/StandaloneMmCoreEntryPoint.h>
> +
> +#include <PiPei.h>
> +#include <Guid/MmramMemoryReserve.h>
> +#include <Guid/MpInformation.h>
> +
> +#include <Library/ArmMmuLib.h>
> +#include <Library/ArmSvcLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/HobLib.h>
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/SerialPortLib.h>
> +
> +#include <IndustryStandard/ArmStdSmc.h>
> +#include <IndustryStandard/ArmMmSvc.h>
> +
> +#define SPM_MAJOR_VER_MASK        0xFFFF0000
> +#define SPM_MINOR_VER_MASK        0x0000FFFF
> +#define SPM_MAJOR_VER_SHIFT       16
> +
> +const UINT32 SPM_MAJOR_VER = 0;
> +const UINT32 SPM_MINOR_VER = 1;
> +
> +const UINT8 BOOT_PAYLOAD_VERSION = 1;
> +
> +PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT      CpuDriverEntryPoint = NULL;
> +
> +/**
> +  Retrieve a pointer to and print the boot information passed by privileged
> +  secure firmware
> +
> +  @param  SharedBufAddress The pointer memory shared with privileged firmware
> +
> +**/
> +EFI_SECURE_PARTITION_BOOT_INFO *
> +GetAndPrintBootinformation (
> +  IN VOID                      *SharedBufAddress
> +)
> +{
> +  EFI_SECURE_PARTITION_BOOT_INFO *PayloadBootInfo;
> +  EFI_SECURE_PARTITION_CPU_INFO  *PayloadCpuInfo;
> +  UINTN                          Index;
> +
> +  PayloadBootInfo = (EFI_SECURE_PARTITION_BOOT_INFO *) SharedBufAddress;
> +
> +  if (PayloadBootInfo->Header.Version != BOOT_PAYLOAD_VERSION)
> +  {
> +    DEBUG((DEBUG_ERROR, "Boot Information Version Mismatch. Current=0x%x, Expected=0x%x.\n", PayloadBootInfo->Header.Version, BOOT_PAYLOAD_VERSION));
> +    return NULL;
> +  }
> +  DEBUG((DEBUG_INFO, "NumSpMemRegions - 0x%x\n", PayloadBootInfo->NumSpMemRegions));
> +  DEBUG((DEBUG_INFO, "SpMemBase       - 0x%lx\n", PayloadBootInfo->SpMemBase));
> +  DEBUG((DEBUG_INFO, "SpMemLimit      - 0x%lx\n", PayloadBootInfo->SpMemLimit));
> +  DEBUG((DEBUG_INFO, "SpImageBase     - 0x%lx\n", PayloadBootInfo->SpImageBase));
> +  DEBUG((DEBUG_INFO, "SpStackBase     - 0x%lx\n", PayloadBootInfo->SpStackBase));
> +  DEBUG((DEBUG_INFO, "SpHeapBase      - 0x%lx\n", PayloadBootInfo->SpHeapBase));
> +  DEBUG((DEBUG_INFO, "SpNsCommBufBase - 0x%lx\n", PayloadBootInfo->SpNsCommBufBase));
> +  DEBUG((DEBUG_INFO, "SpSharedBufBase - 0x%lx\n", PayloadBootInfo->SpSharedBufBase));
> +
> +  DEBUG((DEBUG_INFO, "SpImageSize     - 0x%x\n", PayloadBootInfo->SpImageSize));
> +  DEBUG((DEBUG_INFO, "SpPcpuStackSize - 0x%x\n", PayloadBootInfo->SpPcpuStackSize));
> +  DEBUG((DEBUG_INFO, "SpHeapSize      - 0x%x\n", PayloadBootInfo->SpHeapSize));
> +  DEBUG((DEBUG_INFO, "SpNsCommBufSize - 0x%x\n", PayloadBootInfo->SpNsCommBufSize));
> +  DEBUG((DEBUG_INFO, "SpPcpuSharedBufSize - 0x%x\n", PayloadBootInfo->SpPcpuSharedBufSize));
> +
> +  DEBUG((DEBUG_INFO, "NumCpus         - 0x%x\n", PayloadBootInfo->NumCpus));
> +  DEBUG((DEBUG_INFO, "CpuInfo         - 0x%p\n", PayloadBootInfo->CpuInfo));
> +
> +  PayloadCpuInfo = (EFI_SECURE_PARTITION_CPU_INFO *) PayloadBootInfo->CpuInfo;
> +
> +  for (Index = 0; Index < PayloadBootInfo->NumCpus; Index++) {
> +    DEBUG((DEBUG_INFO, "Mpidr           - 0x%lx\n", PayloadCpuInfo[Index].Mpidr));
> +    DEBUG((DEBUG_INFO, "LinearId        - 0x%x\n", PayloadCpuInfo[Index].LinearId));
> +    DEBUG((DEBUG_INFO, "Flags           - 0x%x\n", PayloadCpuInfo[Index].Flags));
> +  }
> +
> +  return PayloadBootInfo;
> +}
> +
> +VOID
> +EFIAPI
> +DelegatedEventLoop (
> +  IN ARM_SVC_ARGS *EventCompleteSvcArgs
> +  )
> +{
> +  EFI_STATUS Status;
> +
> +  while(TRUE) {
> +    ArmCallSvc(EventCompleteSvcArgs);
> +
> +    DEBUG ((DEBUG_INFO, "Received delegated event\n"));
> +    DEBUG ((DEBUG_INFO, "X0 :  0x%x\n", (UINT32) EventCompleteSvcArgs->Arg0));
> +    DEBUG ((DEBUG_INFO, "X1 :  0x%x\n", (UINT32) EventCompleteSvcArgs->Arg1));
> +    DEBUG ((DEBUG_INFO, "X2 :  0x%x\n", (UINT32) EventCompleteSvcArgs->Arg2));
> +    DEBUG ((DEBUG_INFO, "X3 :  0x%x\n", (UINT32) EventCompleteSvcArgs->Arg3));
> +
> +    Status = CpuDriverEntryPoint(
> +      EventCompleteSvcArgs->Arg0,
> +      EventCompleteSvcArgs->Arg2,
> +      EventCompleteSvcArgs->Arg1);
> +    if (EFI_ERROR(Status)) {
> +      DEBUG((DEBUG_ERROR, "Failed delegated event 0x%x, Status 0x%x\n",
> +        EventCompleteSvcArgs->Arg0, Status));
> +    }
> +
> +    EventCompleteSvcArgs->Arg0 = ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64;
> +    EventCompleteSvcArgs->Arg1 = Status;
> +  }
> +}
> +
> +STATIC
> +EFI_STATUS
> +GetSpmVersion ()
> +{
> +  EFI_STATUS   Status;
> +  UINT16       SpmMajorVersion;
> +  UINT16       SpmMinorVersion;
> +  UINT32       SpmVersion;
> +  ARM_SVC_ARGS SpmVersionArgs;
> +
> +  SpmVersionArgs.Arg0 = ARM_SVC_ID_SPM_VERSION_AARCH32;
> +
> +  ArmCallSvc (&SpmVersionArgs);
> +
> +  SpmVersion = SpmVersionArgs.Arg0;
> +
> +  SpmMajorVersion = ((SpmVersion & SPM_MAJOR_VER_MASK) >> SPM_MAJOR_VER_SHIFT);
> +  SpmMinorVersion = ((SpmVersion & SPM_MINOR_VER_MASK) >> 0);
> +
> +  // Different major revision values indicate possibly incompatible functions.
> +  // For two revisions, A and B, for which the major revision values are
> +  // identical, if the minor revision value of revision B is greater than
> +  // the minor revision value of revision A, then every function in
> +  // revision A must work in a compatible way with revision B.
> +  // However, it is possible for revision B to have a higher
> +  // function count than revision A.
> +  if ((SpmMajorVersion == SPM_MAJOR_VER) &&
> +      (SpmMinorVersion >= SPM_MINOR_VER))
> +  {
> +    DEBUG ((DEBUG_INFO, "SPM Version: Major=0x%x, Minor=0x%x\n",
> +           SpmMajorVersion, SpmMinorVersion));
> +    Status = EFI_SUCCESS;
> +  }
> +  else
> +  {
> +    DEBUG ((DEBUG_INFO, "Incompatible SPM Versions.\n Current Version: Major=0x%x, Minor=0x%x.\n Expected: Major=0x%x, Minor>=0x%x.\n",
> +            SpmMajorVersion, SpmMinorVersion, SPM_MAJOR_VER, SPM_MINOR_VER));
> +    Status = EFI_UNSUPPORTED;
> +  }
> +
> +  return Status;
> +}
> +
> +/**
> +  The entry point of Standalone MM Foundation.
> +
> +  @param  HobStart  The pointer to the beginning of the HOB List.
> +
> +**/
> +VOID
> +EFIAPI
> +_ModuleEntryPoint (
> +  IN VOID    *SharedBufAddress,
> +  IN UINT64  SharedBufSize,
> +  IN UINT64  cookie1,
> +  IN UINT64  cookie2

As described earlier, SharedBufSize is not required. The format of the header in
SharedBufAddress should provide enough information to discover the size.
[Supreeth] Ok.

> +  )
> +{
> +  PE_COFF_LOADER_IMAGE_CONTEXT            ImageContext;
> +  EFI_SECURE_PARTITION_BOOT_INFO          *PayloadBootInfo;
> +  ARM_SVC_ARGS                            InitMmFoundationSvcArgs = {0};
> +  EFI_STATUS                              Status;
> +  UINT32                                  SectionHeaderOffset;
> +  UINTN                                   NumberOfSections;
> +  VOID                                    *HobStart;
> +  VOID                                    *TeData;
> +  UINTN                                   TeDataSize;
> +
> +  Status = SerialPortInitialize ();

It is worth commenting here that this function will succeed as the Arm TF
platform port has granted access to the UART. It is also worth checking in
edk2-platforms that the UART allocated is different from the one used by Arm TF.
[Supreeth] When I create Standalone MM image's description file for AArch64 FVP,
I specific this PCD
gEfiMdeModulePkgTokenSpaceGuid.PcdSerialRegisterBase
which will determine where the serial port messages go.
Platform owner will choose this as per his/her platform 😊.

cheers,
Achin

> +  ASSERT_EFI_ERROR (Status);
> +
> +  // Get Secure Partition Manager Version Information
> +  Status = GetSpmVersion();
> +  if (EFI_ERROR(Status)) {
> +    goto finish;
> +  }
> +
> +  PayloadBootInfo = GetAndPrintBootinformation(SharedBufAddress);
> +  if (PayloadBootInfo == NULL) {
> +    Status = EFI_UNSUPPORTED;
> +    goto finish;
> +  }
> +
> +  // Locate PE/COFF File information for the Standalone MM core module
> +  Status = LocateStandaloneMmCorePeCoffData (
> +                 (EFI_FIRMWARE_VOLUME_HEADER *) PayloadBootInfo->SpImageBase,
> +                 &TeData,
> +                 &TeDataSize);
> +  if (EFI_ERROR(Status)) {
> +    goto finish;
> +  }
> +
> +  // Obtain the PE/COFF Section information for the Standalone MM core module
> +  Status = GetStandaloneMmCorePeCoffSections (
> +            TeData,
> +            &ImageContext,
> +            &SectionHeaderOffset,
> +            &NumberOfSections);
> +  if (EFI_ERROR(Status)) {
> +    goto finish;
> +  }
> +
> +  // Update the memory access permissions of individual sections in the
> +  // Standalone MM core module
> +  Status = UpdateMmFoundationPeCoffPermissions(
> +            &ImageContext,
> +            SectionHeaderOffset,
> +            NumberOfSections,
> +            ArmSetMemoryRegionNoExec,
> +            ArmSetMemoryRegionReadOnly,
> +            ArmClearMemoryRegionReadOnly);
> +  if (EFI_ERROR(Status)) {
> +    goto finish;
> +  }
> +
> +  //
> +  // Create Hoblist based upon boot information passed by privileged software
> +  //
> +  HobStart = CreateHobListFromBootInfo(&CpuDriverEntryPoint, PayloadBootInfo);
> +
> +  //
> +  // Call the MM Core entry point
> +  //
> +  ProcessModuleEntryPointList (HobStart);
> +
> +  ASSERT_EFI_ERROR (CpuDriverEntryPoint);
> +  DEBUG ((DEBUG_INFO, "Shared Cpu Driver EP 0x%lx\n", (UINT64) CpuDriverEntryPoint));
> +
> +finish:
> +  InitMmFoundationSvcArgs.Arg0 = ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64;
> +  InitMmFoundationSvcArgs.Arg1 = Status;
> +  DelegatedEventLoop(&InitMmFoundationSvcArgs);
> +  ASSERT_EFI_ERROR(0);
> +
> +}
> diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
> new file mode 100644
> index 0000000000..9edc85d406
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
> @@ -0,0 +1,53 @@
> +## @file
> +# Module entry point library for DXE core.
> +#
> +# Copyright (c) 2017, ARM Ltd. All rights reserved.<BR>
> +#
> +#  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.
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = StandaloneMmCoreEntryPoint
> +  FILE_GUID                      = C97AC593-109A-4C63-905C-675FDE2689E8
> +  MODULE_TYPE                    = MM_CORE_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  LIBRARY_CLASS                  = StandaloneMmCoreEntryPoint|MM_CORE_STANDALONE
> +
> +#
> +#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC (EBC is for build only)
> +#
> +
> +[Sources.AARCH64]
> +  Arm/StandaloneMmCoreEntryPoint.c
> +  Arm/SetPermissions.c
> +  Arm/CreateHobList.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +  MdeModulePkg/MdeModulePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[Packages.AARCH64]
> +  ArmPkg/ArmPkg.dec
> +  ArmPlatformPkg/ArmPlatformPkg.dec
> +
> +[LibraryClasses]
> +  ArmMmuLib
> +  ArmSvcLib
> +  BaseLib
> +  DebugLib
> +
> +[Guids]
> +  gMpInformationHobGuid
> +  gEfiMmPeiMmramMemoryReserveGuid
> +  gEfiStandaloneMmNonSecureBufferGuid
> +  gEfiArmTfCpuDriverEpDescriptorGuid
> diff --git a/StandaloneMmPkg b/StandaloneMmPkg~HEAD
> similarity index 100%
> rename from StandaloneMmPkg
> rename to StandaloneMmPkg~HEAD
> --
> 2.16.2
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 07/18] StandaloneMmPkg/FvLib: Add a common FV Library for management mode.
  2018-04-16 14:44   ` Achin Gupta
@ 2018-05-04 23:21     ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:21 UTC (permalink / raw)
  To: Achin Gupta; +Cc: edk2-devel@lists.01.org

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Monday, April 16, 2018 9:44 AM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 07/18] StandaloneMmPkg/FvLib: Add a common FV Library for management mode.

Hi Supreeth,

On Fri, Apr 06, 2018 at 03:42:12PM +0100, Supreeth Venkatesh wrote:
> This patch implements a firmware volume library that can be used by
> the Standalone management mode core module to parse the firmware volume.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>

I have not contributed to this patch at all. Could you please remove me?

Not being an expert, I will wait for Jiewen's feedback cycle to complete.
[Supreeth] Ok. Jiewen has given his feedback already.

cheers,
Achin

> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  StandaloneMmPkg/Include/Library/FvLib.h | 109 ++++++++++
>  StandaloneMmPkg/Library/FvLib/FvLib.c   | 366 ++++++++++++++++++++++++++++++++
>  StandaloneMmPkg/Library/FvLib/FvLib.inf |  57 +++++
>  3 files changed, 532 insertions(+)
>  create mode 100644 StandaloneMmPkg/Include/Library/FvLib.h
>  create mode 100644 StandaloneMmPkg/Library/FvLib/FvLib.c
>  create mode 100644 StandaloneMmPkg/Library/FvLib/FvLib.inf
>
> diff --git a/StandaloneMmPkg/Include/Library/FvLib.h
> b/StandaloneMmPkg/Include/Library/FvLib.h
> new file mode 100644
> index 0000000000..13e1ae2b01
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Library/FvLib.h
> @@ -0,0 +1,109 @@
> +/** @file
> +
> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _FV_LIB_H_
> +#define _FV_LIB_H_
> +
> +#include <Uefi.h>
> +#include <Pi/PiFirmwareVolume.h>
> +#include <Pi/PiFirmwareFile.h>
> +
> +/**
> +  Given the input file pointer, search for the next matching file in
> +the
> +  FFS volume as defined by SearchType. The search starts from
> +FileHeader inside
> +  the Firmware Volume defined by FwVolHeader.
> +
> +  @param  SearchType  Filter to find only files of this type.
> +                      Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
> +  @param  FwVolHeader Pointer to the FV header of the volume to search.
> +                      This parameter must point to a valid FFS volume.
> +  @param  FileHeader  Pointer to the current file from which to begin searching.
> +                      This pointer will be updated upon return to reflect the file found.
> +
> +  @retval EFI_NOT_FOUND  No files matching the search criteria were
> +found
> +  @retval EFI_SUCCESS
> +**/
> +EFI_STATUS
> +EFIAPI
> +FfsFindNextFile (
> +  IN EFI_FV_FILETYPE             SearchType,
> +  IN EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader,
> +  IN OUT EFI_FFS_FILE_HEADER     **FileHeader
> +  );
> +
> +/**
> +  Given the input file pointer, search for the next matching section
> +in the
> +  FFS volume.
> +
> +  @param  SearchType    Filter to find only sections of this type.
> +  @param  FfsFileHeader Pointer to the current file to search.
> +  @param  SectionHeader Pointer to the Section matching SectionType in FfsFileHeader.
> +                        NULL if section not found
> +
> +  @retval  EFI_NOT_FOUND  No files matching the search criteria were
> +found
> +  @retval  EFI_SUCCESS
> +**/
> +EFI_STATUS
> +FfsFindSection (
> +  IN EFI_SECTION_TYPE              SectionType,
> +  IN EFI_FFS_FILE_HEADER           *FfsFileHeader,
> +  IN OUT EFI_COMMON_SECTION_HEADER **SectionHeader
> +  );
> +
> +/**
> +  Locates a section within a series of sections
> +  with the specified section type.
> +
> +  @param[in]   Sections        The sections to search
> +  @param[in]   SizeOfSections  Total size of all sections
> +  @param[in]   SectionType     The section type to locate
> +  @param[out]  FoundSection    The FFS section if found
> +
> +  @retval EFI_SUCCESS           The file and section was found
> +  @retval EFI_NOT_FOUND         The file and section was not found
> +  @retval EFI_VOLUME_CORRUPTED  The firmware volume was corrupted **/
> +EFI_STATUS EFIAPI FindFfsSectionInSections (
> +  IN  VOID                             *Sections,
> +  IN  UINTN                            SizeOfSections,
> +  IN  EFI_SECTION_TYPE                 SectionType,
> +  OUT EFI_COMMON_SECTION_HEADER        **FoundSection
> +  );
> +
> +/**
> +  Given the input file pointer, search for the next matching section
> +in the
> +  FFS volume.
> +
> +  @param  SearchType      Filter to find only sections of this type.
> +  @param  FfsFileHeader   Pointer to the current file to search.
> +  @param  SectionData     Pointer to the Section matching SectionType in FfsFileHeader.
> +                          NULL if section not found  @param
> + SectionDataSize The size of SectionData
> +
> +  @retval  EFI_NOT_FOUND  No files matching the search criteria were
> +found
> +  @retval  EFI_SUCCESS
> +**/
> +EFI_STATUS
> +EFIAPI
> +FfsFindSectionData (
> +  IN EFI_SECTION_TYPE              SectionType,
> +  IN EFI_FFS_FILE_HEADER           *FfsFileHeader,
> +  OUT VOID                         **SectionData,
> +  OUT UINTN                        *SectionDataSize
> +  );
> +
> +#endif
> diff --git a/StandaloneMmPkg/Library/FvLib/FvLib.c
> b/StandaloneMmPkg/Library/FvLib/FvLib.c
> new file mode 100644
> index 0000000000..cacfc81338
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/FvLib/FvLib.c
> @@ -0,0 +1,366 @@
> +/** @file
> +
> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Library/FvLib.h>
> +
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +
> +#define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
> +  (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1)))
> +& ((Alignment) - 1))
> +
> +/**
> +  Returns the highest bit set of the State field
> +
> +  @param ErasePolarity   Erase Polarity  as defined by EFI_FVB_ERASE_POLARITY
> +                         in the Attributes field.
> +  @param FfsHeader       Pointer to FFS File Header.
> +
> +  @return the highest bit in the State field **/ EFI_FFS_FILE_STATE
> +GetFileState (
> +  IN UINT8                ErasePolarity,
> +  IN EFI_FFS_FILE_HEADER  *FfsHeader
> +  )
> +{
> +  EFI_FFS_FILE_STATE  FileState;
> +  EFI_FFS_FILE_STATE  HighestBit;
> +
> +  FileState = FfsHeader->State;
> +
> +  if (ErasePolarity != 0) {
> +    FileState = (EFI_FFS_FILE_STATE)~FileState;  }
> +
> +  HighestBit = 0x80;
> +  while (HighestBit != 0 && (HighestBit & FileState) == 0) {
> +    HighestBit >>= 1;
> +  }
> +
> +  return HighestBit;
> +}
> +
> +/**
> +  Calculates the checksum of the header of a file.
> +
> +  @param FileHeader       Pointer to FFS File Header.
> +
> +  @return Checksum of the header.
> +**/
> +UINT8
> +CalculateHeaderChecksum (
> +  IN EFI_FFS_FILE_HEADER  *FileHeader
> +  )
> +{
> +  UINT8 *ptr;
> +  UINTN Index;
> +  UINT8 Sum;
> +
> +  Sum = 0;
> +  ptr = (UINT8 *) FileHeader;
> +
> +  for (Index = 0; Index < sizeof (EFI_FFS_FILE_HEADER) - 3; Index += 4) {
> +    Sum = (UINT8) (Sum + ptr[Index]);
> +    Sum = (UINT8) (Sum + ptr[Index + 1]);
> +    Sum = (UINT8) (Sum + ptr[Index + 2]);
> +    Sum = (UINT8) (Sum + ptr[Index + 3]);  }
> +
> +  for (; Index < sizeof (EFI_FFS_FILE_HEADER); Index++) {
> +    Sum = (UINT8) (Sum + ptr[Index]);  }  //  // State field (since
> + this indicates the different state of file).
> +  //
> +  Sum = (UINT8) (Sum - FileHeader->State);  //  // Checksum field of
> + the file is not part of the header checksum.
> +  //
> +  Sum = (UINT8) (Sum - FileHeader->IntegrityCheck.Checksum.File);
> +
> +  return Sum;
> +}
> +
> +/**
> +  Given the input file pointer, search for the next matching file in
> +the
> +  FFS volume as defined by SearchType. The search starts from
> +FileHeader inside
> +  the Firmware Volume defined by FwVolHeader.
> +
> +  @param  SearchType  Filter to find only files of this type.
> +                      Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
> +  @param  FwVolHeader Pointer to the FV header of the volume to search.
> +                      This parameter must point to a valid FFS volume.
> +  @param  FileHeader  Pointer to the current file from which to begin searching.
> +                      This pointer will be updated upon return to reflect the file found.
> +
> +  @retval EFI_NOT_FOUND  No files matching the search criteria were
> +found
> +  @retval EFI_SUCCESS
> +**/
> +EFI_STATUS
> +EFIAPI
> +FfsFindNextFile (
> +  IN EFI_FV_FILETYPE             SearchType,
> +  IN EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader,
> +  IN OUT EFI_FFS_FILE_HEADER     **FileHeader
> +  )
> +{
> +  EFI_FFS_FILE_HEADER *FfsFileHeader;
> +  UINT32              FileLength;
> +  UINT32              FileOccupiedSize;
> +  UINT32              FileOffset;
> +  UINT64              FvLength;
> +  UINT8               ErasePolarity;
> +  UINT8               FileState;
> +
> +  FvLength = FwVolHeader->FvLength;
> +  if (FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {
> +    ErasePolarity = 1;
> +  } else {
> +    ErasePolarity = 0;
> +  }
> +  //
> +  // If FileHeader is not specified (NULL) start with the first file
> + in the  // firmware volume.  Otherwise, start from the FileHeader.
> +  //
> +  if (*FileHeader == NULL) {
> +    FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FwVolHeader +
> + FwVolHeader->HeaderLength);  } else {
> +    //
> +    // Length is 24 bits wide so mask upper 8 bits
> +    // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
> +    //
> +    FileLength        = FFS_FILE_SIZE(*FileHeader);
> +    FileOccupiedSize  = GET_OCCUPIED_SIZE (FileLength, 8);
> +    FfsFileHeader     = (EFI_FFS_FILE_HEADER *) ((UINT8 *) *FileHeader + FileOccupiedSize);
> +  }
> +
> +  FileOffset = (UINT32) ((UINT8 *) FfsFileHeader - (UINT8 *)
> + FwVolHeader);
> +
> +  while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {
> +    //
> +    // Get FileState which is the highest bit of the State
> +    //
> +    FileState = GetFileState (ErasePolarity, FfsFileHeader);
> +
> +    switch (FileState) {
> +
> +    case EFI_FILE_HEADER_INVALID:
> +      FileOffset += sizeof (EFI_FFS_FILE_HEADER);
> +      FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
> +      break;
> +
> +    case EFI_FILE_DATA_VALID:
> +    case EFI_FILE_MARKED_FOR_UPDATE:
> +      if (CalculateHeaderChecksum (FfsFileHeader) == 0) {
> +        FileLength        = FFS_FILE_SIZE(FfsFileHeader);
> +        FileOccupiedSize  = GET_OCCUPIED_SIZE (FileLength, 8);
> +
> +        if ((SearchType == FfsFileHeader->Type) || (SearchType ==
> + EFI_FV_FILETYPE_ALL)) {
> +
> +          *FileHeader = FfsFileHeader;
> +
> +          return EFI_SUCCESS;
> +        }
> +
> +        FileOffset += FileOccupiedSize;
> +        FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + FileOccupiedSize);
> +      } else {
> +        return EFI_NOT_FOUND;
> +      }
> +      break;
> +
> +    case EFI_FILE_DELETED:
> +      FileLength        = FFS_FILE_SIZE(FfsFileHeader);
> +      FileOccupiedSize  = GET_OCCUPIED_SIZE (FileLength, 8);
> +      FileOffset += FileOccupiedSize;
> +      FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + FileOccupiedSize);
> +      break;
> +
> +    default:
> +      return EFI_NOT_FOUND;
> +
> +    }
> +  }
> +
> +  return EFI_NOT_FOUND;
> +}
> +
> +/**
> +  Locates a section within a series of sections
> +  with the specified section type.
> +
> +  @param[in]   Sections        The sections to search
> +  @param[in]   SizeOfSections  Total size of all sections
> +  @param[in]   SectionType     The section type to locate
> +  @param[out]  FoundSection    The FFS section if found
> +
> +  @retval EFI_SUCCESS           The file and section was found
> +  @retval EFI_NOT_FOUND         The file and section was not found
> +  @retval EFI_VOLUME_CORRUPTED  The firmware volume was corrupted **/
> +EFI_STATUS EFIAPI FindFfsSectionInSections (
> +  IN  VOID                             *Sections,
> +  IN  UINTN                            SizeOfSections,
> +  IN  EFI_SECTION_TYPE                 SectionType,
> +  OUT EFI_COMMON_SECTION_HEADER        **FoundSection
> +  )
> +{
> +  EFI_PHYSICAL_ADDRESS        CurrentAddress;
> +  UINT32                      Size;
> +  EFI_PHYSICAL_ADDRESS        EndOfSections;
> +  EFI_COMMON_SECTION_HEADER   *Section;
> +  EFI_PHYSICAL_ADDRESS        EndOfSection;
> +
> +  //
> +  // Loop through the FFS file sections
> +  //
> +  EndOfSection = (EFI_PHYSICAL_ADDRESS)(UINTN) Sections;
> +  EndOfSections = EndOfSection + SizeOfSections;
> +  for (;;) {
> +    if (EndOfSection == EndOfSections) {
> +      break;
> +    }
> +    CurrentAddress = EndOfSection;
> +
> +    Section = (EFI_COMMON_SECTION_HEADER*)(UINTN) CurrentAddress;
> +//    printf ("Section->Type: 0x%08x\n", Section->Type);
> +
> +    Size = SECTION_SIZE (Section);
> +    if (Size < sizeof (*Section)) {
> +      return EFI_VOLUME_CORRUPTED;
> +    }
> +
> +    EndOfSection = CurrentAddress + Size;
> +    if (EndOfSection > EndOfSections) {
> +      return EFI_VOLUME_CORRUPTED;
> +    }
> +    Size = GET_OCCUPIED_SIZE (Size, 4);
> +
> +    //
> +    // Look for the requested section type
> +    //
> +    if (Section->Type == SectionType) {
> +      *FoundSection = Section;
> +      return EFI_SUCCESS;
> +    }
> +  }
> +
> +  return EFI_NOT_FOUND;
> +}
> +
> +/**
> +  Given the input file pointer, search for the next matching section in the
> +  FFS volume.
> +
> +  @param  SearchType    Filter to find only sections of this type.
> +  @param  FfsFileHeader Pointer to the current file to search.
> +  @param  SectionHeader Pointer to the Section matching SectionType in FfsFileHeader.
> +                        NULL if section not found
> +
> +  @retval  EFI_NOT_FOUND  No files matching the search criteria were found
> +  @retval  EFI_SUCCESS
> +**/
> +EFI_STATUS
> +EFIAPI
> +FfsFindSection (
> +  IN EFI_SECTION_TYPE              SectionType,
> +  IN EFI_FFS_FILE_HEADER           *FfsFileHeader,
> +  IN OUT EFI_COMMON_SECTION_HEADER **SectionHeader
> +  )
> +{
> +  UINT32                    FileSize;
> +  EFI_COMMON_SECTION_HEADER *Section;
> +  EFI_STATUS                Status;
> +
> +  //
> +  // Size is 24 bits wide so mask upper 8 bits.
> +  //    Does not include FfsFileHeader header size
> +  // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
> +  //
> +  Section   = (EFI_COMMON_SECTION_HEADER *) (FfsFileHeader + 1);
> +  FileSize  = FFS_FILE_SIZE(FfsFileHeader);
> +  FileSize -= sizeof (EFI_FFS_FILE_HEADER);
> +
> +  Status = FindFfsSectionInSections (
> +             Section,
> +             FileSize,
> +             SectionType,
> +             SectionHeader
> +             );
> +  return Status;
> +}
> +
> +/**
> +  Given the input file pointer, search for the next matching section in the
> +  FFS volume.
> +
> +  @param  SearchType      Filter to find only sections of this type.
> +  @param  FfsFileHeader   Pointer to the current file to search.
> +  @param  SectionData     Pointer to the Section matching SectionType in FfsFileHeader.
> +                          NULL if section not found
> +  @param  SectionDataSize The size of SectionData
> +
> +  @retval  EFI_NOT_FOUND  No files matching the search criteria were found
> +  @retval  EFI_SUCCESS
> +**/
> +EFI_STATUS
> +EFIAPI
> +FfsFindSectionData (
> +  IN EFI_SECTION_TYPE      SectionType,
> +  IN EFI_FFS_FILE_HEADER   *FfsFileHeader,
> +  IN OUT VOID              **SectionData,
> +  IN OUT UINTN             *SectionDataSize
> +  )
> +{
> +  UINT32                    FileSize;
> +  EFI_COMMON_SECTION_HEADER *Section;
> +  UINT32                    SectionLength;
> +  UINT32                    ParsedLength;
> +
> +  //
> +  // Size is 24 bits wide so mask upper 8 bits.
> +  //    Does not include FfsFileHeader header size
> +  // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
> +  //
> +  Section   = (EFI_COMMON_SECTION_HEADER *) (FfsFileHeader + 1);
> +  FileSize  = FFS_FILE_SIZE(FfsFileHeader);
> +  FileSize -= sizeof (EFI_FFS_FILE_HEADER);
> +
> +  *SectionData  = NULL;
> +  ParsedLength  = 0;
> +  while (ParsedLength < FileSize) {
> +    if (Section->Type == SectionType) {
> +      *SectionData = (VOID *) (Section + 1);
> +      *SectionDataSize = SECTION_SIZE(Section);
> +      return EFI_SUCCESS;
> +    }
> +    //
> +    // Size is 24 bits wide so mask upper 8 bits.
> +    // SectionLength is adjusted it is 4 byte aligned.
> +    // Go to the next section
> +    //
> +    SectionLength = SECTION_SIZE(Section);
> +    SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
> +
> +    ParsedLength += SectionLength;
> +    Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + SectionLength);
> +  }
> +
> +  return EFI_NOT_FOUND;
> +}
> diff --git a/StandaloneMmPkg/Library/FvLib/FvLib.inf b/StandaloneMmPkg/Library/FvLib/FvLib.inf
> new file mode 100644
> index 0000000000..6df50ab364
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/FvLib/FvLib.inf
> @@ -0,0 +1,57 @@
> +## @file
> +#
> +#  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +#  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +#
> +#  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
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +##
> +
> +################################################################################
> +#
> +# Defines Section - statements that will be processed to create a Makefile.
> +#
> +################################################################################
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = FvLib
> +  FILE_GUID                      = C20085E9-E3AB-4938-A727-C10935FEEE2B
> +  MODULE_TYPE                    = BASE
> +  VERSION_STRING                 = 1.0
> +  LIBRARY_CLASS                  = FvLib
> +
> +#
> +# The following information is for reference only and not required by the build tools.
> +#
> +#  VALID_ARCHITECTURES           = IA32
> +#
> +
> +################################################################################
> +#
> +# Sources Section - list of files that are required for the build to succeed.
> +#
> +################################################################################
> +
> +[Sources]
> +  FvLib.c
> +
> +################################################################################
> +#
> +# Package Dependency Section - list of Package files that are required for
> +#                              this module.
> +#
> +################################################################################
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[LibraryClasses]
> +  BaseLib
> +  BaseMemoryLib
> +  DebugLib
> --
> 2.16.2
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 08/18] StandaloneMmPkg/MemLib: AARCH64 Specific instance of memory check library.
  2018-04-16 15:12   ` Achin Gupta
  2018-04-16 22:30     ` Yao, Jiewen
@ 2018-05-04 23:21     ` Supreeth Venkatesh
  1 sibling, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:21 UTC (permalink / raw)
  To: Achin Gupta; +Cc: edk2-devel@lists.01.org

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Monday, April 16, 2018 10:13 AM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 08/18] StandaloneMmPkg/MemLib: AARCH64 Specific instance of memory check library.

Hi Supreeth,

On Fri, Apr 06, 2018 at 03:42:13PM +0100, Supreeth Venkatesh wrote:
> MM memory check library library implementation. This library consumes
> MM_ACCESS_PROTOCOL to get MMRAM information. In order to use this
> library instance, the platform should produce all MMRAM range via
> MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core and
> MM driver) and/or specific dedicated hardware.
>
> This patch provides services for MM Memory Operation.
> The management mode Mem Library provides function for checking if
> buffer is outside MMRAM and valid. It also provides functions for copy
> data from MMRAM to non-MMRAM, from non-MMRAM to MMRAM, from non-MMRAM
> to non-MMRAM, or set data in non-MMRAM.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  StandaloneMmPkg/Include/Library/MemLib.h    | 140 ++++++++++++++
>  StandaloneMmPkg/Library/MemLib/Arm/MemLib.c | 276
> ++++++++++++++++++++++++++++

Why is this Library Arm specific. Apart from cosmetics tweaks, it has not changed since it was originally contributed?
[Supreeth] Modified it to separate out MmMemLibInternalCalculateMaximumSupportAddress() specific to archictecture as mentioned by Jiewen. Rest of them are generic.

cheers,
Achin

>  StandaloneMmPkg/Library/MemLib/MemLib.inf   |  47 +++++
>  3 files changed, 463 insertions(+)
>  create mode 100644 StandaloneMmPkg/Include/Library/MemLib.h
>  create mode 100644 StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
>  create mode 100644 StandaloneMmPkg/Library/MemLib/MemLib.inf
>
> diff --git a/StandaloneMmPkg/Include/Library/MemLib.h
> b/StandaloneMmPkg/Include/Library/MemLib.h
> new file mode 100644
> index 0000000000..3264f10010
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Library/MemLib.h
> @@ -0,0 +1,140 @@
> +/** @file
> +  Provides services for MM Memory Operation.
> +
> +  The MM Mem Library provides function for checking if buffer is outside MMRAM and valid.
> +  It also provides functions for copy data from MMRAM to non-MMRAM,
> + from non-MMRAM to MMRAM,  from non-MMRAM to non-MMRAM, or set data in non-MMRAM.
> +
> +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> + Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _MM_MEM_LIB_H_
> +#define _MM_MEM_LIB_H_
> +
> +/**
> +  This function check if the buffer is valid per processor architecture and not overlap with MMRAM.
> +
> +  @param Buffer  The buffer start address to be checked.
> +  @param Length  The buffer length to be checked.
> +
> +  @retval TRUE  This buffer is valid per processor architecture and not overlap with MMRAM.
> +  @retval FALSE This buffer is not valid per processor architecture or overlap with MMRAM.
> +**/
> +BOOLEAN
> +EFIAPI
> +MmIsBufferOutsideMmValid (
> +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> +  IN UINT64                Length
> +  );
> +
> +/**
> +  Copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +
> +  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +  It checks if source buffer is valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it copies memory and returns EFI_SUCCESS.
> +  If the check fails, it return EFI_SECURITY_VIOLATION.
> +  The implementation must be reentrant.
> +
> +  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
> +  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
> +  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is copied.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCopyMemToSmram (
> +  OUT VOID       *DestinationBuffer,
> +  IN CONST VOID  *SourceBuffer,
> +  IN UINTN       Length
> +  );
> +
> +/**
> +  Copies a source buffer (MMRAM) to a destination buffer (NON-MMRAM).
> +
> +  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +  It checks if destination buffer is valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it copies memory and returns EFI_SUCCESS.
> +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> +  The implementation must be reentrant.
> +
> +  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
> +  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
> +  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is copied.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCopyMemFromSmram (
> +  OUT VOID       *DestinationBuffer,
> +  IN CONST VOID  *SourceBuffer,
> +  IN UINTN       Length
> +  );
> +
> +/**
> +  Copies a source buffer (NON-MMRAM) to a destination buffer (NON-MMRAM).
> +
> +  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +  It checks if source buffer and destination buffer are valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it copies memory and returns EFI_SUCCESS.
> +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> +  The implementation must be reentrant, and it must handle the case where source buffer overlaps destination buffer.
> +
> +  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
> +  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
> +  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is copied.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCopyMem (
> +  OUT VOID       *DestinationBuffer,
> +  IN CONST VOID  *SourceBuffer,
> +  IN UINTN       Length
> +  );
> +
> +/**
> +  Fills a target buffer (NON-MMRAM) with a byte value.
> +
> +  This function fills a target buffer (non-MMRAM) with a byte value.
> +  It checks if target buffer is valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it fills memory and returns EFI_SUCCESS.
> +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> +
> +  @param  Buffer    The memory to set.
> +  @param  Length    The number of bytes to set.
> +  @param  Value     The value with which to fill Length bytes of Buffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The Buffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is set.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmSetMem (
> +  OUT VOID  *Buffer,
> +  IN UINTN  Length,
> +  IN UINT8  Value
> +  );
> +
> +#endif
> diff --git a/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> b/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> new file mode 100644
> index 0000000000..432a45698b
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/MemLib/Arm/MemLib.c
> @@ -0,0 +1,276 @@
> +/** @file
> +  Instance of MM memory check library.
> +
> +  MM memory check library library implementation. This library
> + consumes MM_ACCESS_PROTOCOL  to get MMRAM information. In order to
> + use this library instance, the platform should produce  all MMRAM
> + range via MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core  and MM driver) and/or specific dedicated hardware.
> +
> +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> + Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +
> +#include <PiMm.h>
> +
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +
> +EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges;
> +UINTN                mMmMemLibInternalMmramCount;
> +
> +//
> +// Maximum support address used to check input buffer //
> +EFI_PHYSICAL_ADDRESS  mMmMemLibInternalMaximumSupportAddress = 0;
> +
> +/**
> +  Calculate and save the maximum support address.
> +
> +**/
> +VOID
> +MmMemLibInternalCalculateMaximumSupportAddress (
> +  VOID
> +  )
> +{
> +  UINT8        PhysicalAddressBits;
> +
> +  PhysicalAddressBits = 36;
> +
> +  //
> +  // Save the maximum support address in one global variable
> +  //
> +  mMmMemLibInternalMaximumSupportAddress =
> +(EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, PhysicalAddressBits) -
> +1);
> +  DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress =
> +0x%lx\n", mMmMemLibInternalMaximumSupportAddress));
> +}
> +
> +/**
> +  This function check if the buffer is valid per processor architecture and not overlap with MMRAM.
> +
> +  @param Buffer  The buffer start address to be checked.
> +  @param Length  The buffer length to be checked.
> +
> +  @retval TRUE  This buffer is valid per processor architecture and not overlap with MMRAM.
> +  @retval FALSE This buffer is not valid per processor architecture or overlap with MMRAM.
> +**/
> +BOOLEAN
> +EFIAPI
> +MmIsBufferOutsideMmValid (
> +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> +  IN UINT64                Length
> +  )
> +{
> +  UINTN  Index;
> +
> +  //
> +  // Check override.
> +  // NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1) is valid.
> +  //
> +  if ((Length > mMmMemLibInternalMaximumSupportAddress) ||
> +      (Buffer > mMmMemLibInternalMaximumSupportAddress) ||
> +      ((Length != 0) && (Buffer > (mMmMemLibInternalMaximumSupportAddress - (Length - 1)))) ) {
> +    //
> +    // Overflow happen
> +    //
> +    DEBUG ((
> +      DEBUG_ERROR,
> +      "MmIsBufferOutsideMmValid: Overflow: Buffer (0x%lx) - Length (0x%lx), MaximumSupportAddress (0x%lx)\n",
> +      Buffer,
> +      Length,
> +      mMmMemLibInternalMaximumSupportAddress
> +      ));
> +    return FALSE;
> +  }
> +
> +  for (Index = 0; Index < mMmMemLibInternalMmramCount; Index ++) {
> +    if (((Buffer >= mMmMemLibInternalMmramRanges[Index].CpuStart) && (Buffer < mMmMemLibInternalMmramRanges[Index].CpuStart + mMmMemLibInternalMmramRanges[Index].PhysicalSize)) ||
> +        ((mMmMemLibInternalMmramRanges[Index].CpuStart >= Buffer) && (mMmMemLibInternalMmramRanges[Index].CpuStart < Buffer + Length))) {
> +      DEBUG ((
> +        DEBUG_ERROR,
> +        "MmIsBufferOutsideMmValid: Overlap: Buffer (0x%lx) - Length (0x%lx), ",
> +        Buffer,
> +        Length
> +        ));
> +      DEBUG ((
> +        DEBUG_ERROR,
> +        "CpuStart (0x%lx) - PhysicalSize (0x%lx)\n",
> +        mMmMemLibInternalMmramRanges[Index].CpuStart,
> +        mMmMemLibInternalMmramRanges[Index].PhysicalSize
> +        ));
> +      return FALSE;
> +    }
> +  }
> +
> +  return TRUE;
> +}
> +
> +/**
> +  Copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +
> +  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +  It checks if source buffer is valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it copies memory and returns EFI_SUCCESS.
> +  If the check fails, it return EFI_SECURITY_VIOLATION.
> +  The implementation must be reentrant.
> +
> +  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
> +  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
> +  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is copied.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCopyMemToMmram (
> +  OUT VOID       *DestinationBuffer,
> +  IN CONST VOID  *SourceBuffer,
> +  IN UINTN       Length
> +  )
> +{
> +  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {
> +    DEBUG ((DEBUG_ERROR, "MmCopyMemToMmram: Security Violation: Source (0x%x), Length (0x%x)\n", SourceBuffer, Length));
> +    return EFI_SECURITY_VIOLATION;
> +  }
> +  CopyMem (DestinationBuffer, SourceBuffer, Length);
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Copies a source buffer (MMRAM) to a destination buffer (NON-MMRAM).
> +
> +  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +  It checks if destination buffer is valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it copies memory and returns EFI_SUCCESS.
> +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> +  The implementation must be reentrant.
> +
> +  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
> +  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
> +  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is copied.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCopyMemFromMmram (
> +  OUT VOID       *DestinationBuffer,
> +  IN CONST VOID  *SourceBuffer,
> +  IN UINTN       Length
> +  )
> +{
> +  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {
> +    DEBUG ((DEBUG_ERROR, "MmCopyMemFromMmram: Security Violation: Destination (0x%x), Length (0x%x)\n", DestinationBuffer, Length));
> +    return EFI_SECURITY_VIOLATION;
> +  }
> +  CopyMem (DestinationBuffer, SourceBuffer, Length);
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Copies a source buffer (NON-MMRAM) to a destination buffer (NON-MMRAM).
> +
> +  This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
> +  It checks if source buffer and destination buffer are valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it copies memory and returns EFI_SUCCESS.
> +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> +  The implementation must be reentrant, and it must handle the case where source buffer overlaps destination buffer.
> +
> +  @param  DestinationBuffer   The pointer to the destination buffer of the memory copy.
> +  @param  SourceBuffer        The pointer to the source buffer of the memory copy.
> +  @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is copied.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCopyMem (
> +  OUT VOID       *DestinationBuffer,
> +  IN CONST VOID  *SourceBuffer,
> +  IN UINTN       Length
> +  )
> +{
> +  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {
> +    DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation: Destination (0x%x), Length (0x%x)\n", DestinationBuffer, Length));
> +    return EFI_SECURITY_VIOLATION;
> +  }
> +  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {
> +    DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation: Source (0x%x), Length (0x%x)\n", SourceBuffer, Length));
> +    return EFI_SECURITY_VIOLATION;
> +  }
> +  CopyMem (DestinationBuffer, SourceBuffer, Length);
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Fills a target buffer (NON-MMRAM) with a byte value.
> +
> +  This function fills a target buffer (non-MMRAM) with a byte value.
> +  It checks if target buffer is valid per processor architecture and not overlap with MMRAM.
> +  If the check passes, it fills memory and returns EFI_SUCCESS.
> +  If the check fails, it returns EFI_SECURITY_VIOLATION.
> +
> +  @param  Buffer    The memory to set.
> +  @param  Length    The number of bytes to set.
> +  @param  Value     The value with which to fill Length bytes of Buffer.
> +
> +  @retval EFI_SECURITY_VIOLATION The Buffer is invalid per processor architecture or overlap with MMRAM.
> +  @retval EFI_SUCCESS            Memory is set.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmSetMem (
> +  OUT VOID  *Buffer,
> +  IN UINTN  Length,
> +  IN UINT8  Value
> +  )
> +{
> +  if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Length)) {
> +    DEBUG ((DEBUG_ERROR, "MmSetMem: Security Violation: Source (0x%x), Length (0x%x)\n", Buffer, Length));
> +    return EFI_SECURITY_VIOLATION;
> +  }
> +  SetMem (Buffer, Length, Value);
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  The constructor function initializes the Mm Mem library
> +
> +  @param  ImageHandle   The firmware allocated handle for the EFI image.
> +  @param  SystemTable   A pointer to the EFI System Table.
> +
> +  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MemLibConstructor (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  )
> +{
> +
> +  //
> +  // Calculate and save maximum support address  //
> + MmMemLibInternalCalculateMaximumSupportAddress ();
> +
> +  return EFI_SUCCESS;
> +}
> diff --git a/StandaloneMmPkg/Library/MemLib/MemLib.inf
> b/StandaloneMmPkg/Library/MemLib/MemLib.inf
> new file mode 100644
> index 0000000000..52b7c06397
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/MemLib/MemLib.inf
> @@ -0,0 +1,47 @@
> +## @file
> +#  Instance of MM memory check library.
> +#
> +#  MM memory check library library implementation. This library
> +consumes MM_ACCESS_PROTOCOL #  to get MMRAM information. In order to
> +use this library instance, the platform should produce #  all MMRAM
> +range via MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core #  and MM driver) and/or specific dedicated hardware.
> +#
> +#  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR> #
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR> # #
> +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
> +#
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, #  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = MemLib
> +  FILE_GUID                      = EA355F14-6409-4716-829F-37B3BC7C7F26
> +  MODULE_TYPE                    = MM_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  LIBRARY_CLASS                  = MemLib|MM_STANDALONE MM_CORE_STANDALONE
> +  CONSTRUCTOR                    = MemLibConstructor
> +
> +#
> +# The following information is for reference only and not required by the build tools.
> +#
> +#  VALID_ARCHITECTURES           = AARCH64
> +#
> +
> +[Sources.AARCH64]
> +  Arm/MemLib.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[LibraryClasses]
> +  BaseMemoryLib
> +  DebugLib
> --
> 2.16.2
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 09/18] StandaloneMmPkg/MemoryAllocationLib: Add MM memory allocation library.
  2018-04-25 14:33   ` Achin Gupta
  2018-04-26 13:05     ` Yao, Jiewen
@ 2018-05-04 23:21     ` Supreeth Venkatesh
  1 sibling, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:21 UTC (permalink / raw)
  To: Achin Gupta; +Cc: edk2-devel@lists.01.org

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Wednesday, April 25, 2018 9:34 AM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 09/18] StandaloneMmPkg/MemoryAllocationLib: Add MM memory allocation library.

Hi Supreeth,

On Fri, Apr 06, 2018 at 03:42:14PM +0100, Supreeth Venkatesh wrote:
> This patch implements management mode memory allocation services.The
> implementation borrows the MM Core Memory Allocation services as the
> primitive for memory allocation instead of using MM System Table
> services.

The commit message did not really register with me. Once the MMRAM ranges have been conveyed to the MMST memory allocation services (through MemoryAllocationLibConstructor()), all functions in MemoryAllocationLib.c use the MMST services. The message seems to indicate otherwise.

On Arm, the gEfiMmPeiMmramMemoryReserveGuid HOB is used to convey the MMRAM ranges. It seems x86 uses gMmCoreDataHobGuid HOB. So it worth getting this reviewed by Jiewen.

The copyright years in the files need to be updated.
[Supreeth] Ok. Done.

With that in mind..

Acked-by: Achin Gupta <achin.gupta@arm.com>

>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  StandaloneMmPkg/Include/Guid/MmCoreData.h          | 132 +++
>  StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h  |  62 ++
>  .../MemoryAllocationLib/MemoryAllocationLib.c      | 907 +++++++++++++++++++++
>  .../MemoryAllocationLib/MemoryAllocationLib.inf    |  49 ++
>  .../MemoryAllocationLib/MemoryAllocationServices.h |  38 +
>  5 files changed, 1188 insertions(+)
>  create mode 100644 StandaloneMmPkg/Include/Guid/MmCoreData.h
>  create mode 100644 StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
>  create mode 100644
> StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
>  create mode 100644
> StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
>  create mode 100644
> StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.h
>
> diff --git a/StandaloneMmPkg/Include/Guid/MmCoreData.h
> b/StandaloneMmPkg/Include/Guid/MmCoreData.h
> new file mode 100644
> index 0000000000..c0ac772014
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Guid/MmCoreData.h
> @@ -0,0 +1,132 @@
> +/** @file
> +  MM Core data.
> +
> +Copyright (c) 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 that accompanies this distribution.
> +The full text of the license may be found at
> +http://opensource.org/licenses/bsd-license.php.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef __MM_CORE_DATA_H__
> +#define __MM_CORE_DATA_H__
> +
> +#define MM_CORE_DATA_HOB_GUID \
> +  { 0xa160bf99, 0x2aa4, 0x4d7d, { 0x99, 0x93, 0x89, 0x9c, 0xb1, 0x2d,
> +0xf3, 0x76 }}
> +
> +extern EFI_GUID gMmCoreDataHobGuid;
> +
> +typedef struct {
> +  //
> +  // Address pointer to MM_CORE_PRIVATE_DATA
> +  //
> +  EFI_PHYSICAL_ADDRESS   Address;
> +} MM_CORE_DATA_HOB_DATA;
> +
> +
> +///
> +/// Define values for the communications buffer used when
> +gEfiEventDxeDispatchGuid is /// event signaled.  This event is
> +signaled by the DXE Core each time the DXE Core /// dispatcher has
> +completed its work.  When this event is signaled, the MM Core /// if
> +notified, so the MM Core can dispatch MM drivers.  If
> +COMM_BUFFER_MM_DISPATCH_ERROR /// is returned in the communication
> +buffer, then an error occurred dispatching MM /// Drivers.  If
> +COMM_BUFFER_MM_DISPATCH_SUCCESS is returned, then the MM Core ///
> +dispatched all the drivers it could.  If
> +COMM_BUFFER_MM_DISPATCH_RESTART is /// returned, then the MM Core just dispatched the MM Driver that registered /// the MM Entry Point enabling the use of MM Mode.  In this case, the MM Core /// should be notified again to dispatch more MM Drivers using MM Mode.
> +///
> +#define COMM_BUFFER_MM_DISPATCH_ERROR    0x00
> +#define COMM_BUFFER_MM_DISPATCH_SUCCESS  0x01 #define
> +COMM_BUFFER_MM_DISPATCH_RESTART  0x02
> +
> +///
> +/// Signature for the private structure shared between the MM IPL and
> +the MM Core /// #define MM_CORE_PRIVATE_DATA_SIGNATURE  SIGNATURE_32
> +('m', 'm', 'i', 'c')
> +
> +///
> +/// Private structure that is used to share information between the
> +MM IPL and /// the MM Core.  This structure is allocated from memory of type EfiRuntimeServicesData.
> +/// Since runtime memory types are converted to available memory when
> +a legacy boot /// is performed, the MM Core must not access any
> +fields of this structure if a legacy /// boot is performed.  As a
> +result, the MM IPL must create an event notification /// for the
> +Legacy Boot event and notify the MM Core that a legacy boot is being
> +/// performed.  The MM Core can then use this information to filter accesses to /// thos structure.
> +///
> +typedef struct {
> +  UINT64                          Signature;
> +
> +  ///
> +  /// The number of MMRAM ranges passed from the MM IPL to the MM
> + Core.  The MM  /// Core uses these ranges of MMRAM to initialize the MM Core memory manager.
> +  ///
> +  UINT64                          MmramRangeCount;
> +
> +  ///
> +  /// A table of MMRAM ranges passed from the MM IPL to the MM Core.
> + The MM  /// Core uses these ranges of MMRAM to initialize the MM Core memory manager.
> +  ///
> +  EFI_PHYSICAL_ADDRESS            MmramRanges;
> +
> +  ///
> +  /// The MM Foundation Entry Point.  The MM Core fills in this field
> + when the  /// MM Core is initialized.  The MM IPL is responsbile for
> + registering this entry  /// point with the MM Configuration
> + Protocol.  The MM Configuration Protocol may  /// not be available
> + at the time the MM IPL and MM Core are started, so the MM IPL  ///
> + sets up a protocol notification on the MM Configuration Protocol and
> + registers  /// the MM Foundation Entry Point as soon as the MM Configuration Protocol is  /// available.
> +  ///
> +  EFI_PHYSICAL_ADDRESS            MmEntryPoint;
> +
> +  ///
> +  /// Boolean flag set to TRUE while an MMI is being processed by the MM Core.
> +  ///
> +  BOOLEAN                         MmEntryPointRegistered;
> +
> +  ///
> +  /// Boolean flag set to TRUE while an MMI is being processed by the MM Core.
> +  ///
> +  BOOLEAN                         InMm;
> +
> +  ///
> +  /// This field is set by the MM Core then the MM Core is
> + initialized.  This field is  /// used by the MM Base 2 Protocol and
> + MM Communication Protocol implementations in  /// the MM IPL.
> +  ///
> +  EFI_PHYSICAL_ADDRESS            Mmst;
> +
> +  ///
> +  /// This field is used by the MM Communicatioon Protocol to pass a
> + buffer into  /// a software MMI handler and for the software MMI
> + handler to pass a buffer back to  /// the caller of the MM Communication Protocol.
> +  ///
> +  EFI_PHYSICAL_ADDRESS            CommunicationBuffer;
> +
> +  ///
> +  /// This field is used by the MM Communicatioon Protocol to pass
> + the size of a buffer,  /// in bytes, into a software MMI handler and
> + for the software MMI handler to pass the  /// size, in bytes, of a buffer back to the caller of the MM Communication Protocol.
> +  ///
> +  UINT64                          BufferSize;
> +
> +  ///
> +  /// This field is used by the MM Communication Protocol to pass the
> + return status from  /// a software MMI handler back to the caller of the MM Communication Protocol.
> +  ///
> +  UINT64                          ReturnStatus;
> +
> +  EFI_PHYSICAL_ADDRESS            MmCoreImageBase;
> +  UINT64                          MmCoreImageSize;
> +  EFI_PHYSICAL_ADDRESS            MmCoreEntryPoint;
> +
> +  EFI_PHYSICAL_ADDRESS            StandaloneBfvAddress;
> +} MM_CORE_PRIVATE_DATA;
> +
> +#endif
> diff --git a/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
> b/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
> new file mode 100644
> index 0000000000..c4104b755d
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
> @@ -0,0 +1,62 @@
> +/** @file
> +  Definition of GUIDed HOB for reserving MMRAM regions.
> +
> +  This file defines:
> +  * the GUID used to identify the GUID HOB for reserving MMRAM regions.
> +  * the data structure of MMRAM descriptor to describe MMRAM
> + candidate regions
> +  * values of state of MMRAM candidate regions
> +  * the GUID specific data structure of HOB for reserving MMRAM regions.
> +  This GUIDed HOB can be used to convey the existence of the T-SEG
> + reservation and H-SEG usage
> +
> +Copyright (c) 2007 - 2010, Intel Corporation. All rights
> +reserved.<BR> Copyright (c) 2016 - 2017, ARM Limited. All rights
> +reserved.<BR>
> +
> +This program and the accompanying materials are licensed and made
> +available under the terms and conditions of the BSD License that accompanies this distribution.
> +The full text of the license may be found at
> +http://opensource.org/licenses/bsd-license.php.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +  @par Revision Reference:
> +  GUIDs defined in MmCis spec version 0.9.
> +
> +**/
> +
> +#ifndef _EFI_MM_PEI_MMRAM_MEMORY_RESERVE_H_
> +#define _EFI_MM_PEI_MMRAM_MEMORY_RESERVE_H_
> +
> +#define EFI_MM_PEI_MMRAM_MEMORY_RESERVE \
> +  { \
> +    0x0703f912, 0xbf8d, 0x4e2a, {0xbe, 0x07, 0xab, 0x27, 0x25, 0x25,
> +0xc5, 0x92 } \
> +  }
> +
> +/**
> +* GUID specific data structure of HOB for reserving MMRAM regions.
> +*
> +* Inconsistent with specification here:
> +* EFI_HOB_MMRAM_DESCRIPTOR_BLOCK has been changed to EFI_MMRAM_HOB_DESCRIPTOR_BLOCK.
> +* This inconsistency is kept in code in order for backward compatibility.
> +**/
> +typedef struct {
> +  ///
> +  /// Designates the number of possible regions in the system
> +  /// that can be usable for MMRAM.
> +  ///
> +  /// Inconsistent with specification here:
> +  /// In Framework MM CIS 0.91 specification, it defines the field type as UINTN.
> +  /// However, HOBs are supposed to be CPU neutral, so UINT32 should be used instead.
> +  ///
> +  UINT32                NumberOfMmReservedRegions;
> +  ///
> +  /// Used throughout this protocol to describe the candidate
> +  /// regions for MMRAM that are supported by this platform.
> +  ///
> +  EFI_MMRAM_DESCRIPTOR  Descriptor[1]; }
> +EFI_MMRAM_HOB_DESCRIPTOR_BLOCK;
> +
> +extern EFI_GUID gEfiMmPeiSmramMemoryReserveGuid;
> +
> +#endif
> +
> diff --git
> a/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
> b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
> new file mode 100644
> index 0000000000..c177a8f538
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.
> +++ c
> @@ -0,0 +1,907 @@
> +/** @file
> +  Support routines for memory allocation routines based on Standalone MM Core internal functions.
> +
> +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> + Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <PiMm.h>
> +
> +#include <Guid/MmramMemoryReserve.h>
> +#include <Library/MemoryAllocationLib.h> #include
> +<Library/BaseMemoryLib.h> #include <Library/DebugLib.h> #include
> +<Library/HobLib.h> #include "MemoryAllocationServices.h"
> +
> +EFI_MM_SYSTEM_TABLE   *gMmst = NULL;
> +
> +/**
> +  Allocates one or more 4KB pages of a certain memory type.
> +
> +  Allocates the number of 4KB pages of a certain memory type and
> + returns a pointer to the allocated  buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0, then NULL is returned.
> +  If there is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  @param  MemoryType            The type of memory to allocate.
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +InternalAllocatePages (
> +  IN EFI_MEMORY_TYPE  MemoryType,
> +  IN UINTN            Pages
> +  )
> +{
> +  EFI_STATUS            Status;
> +  EFI_PHYSICAL_ADDRESS  Memory;
> +
> +  if (Pages == 0) {
> +    return NULL;
> +  }
> +
> +  Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType,
> +Pages, &Memory);
> +  if (EFI_ERROR (Status)) {
> +    return NULL;
> +  }
> +  return (VOID *) (UINTN) Memory;
> +}
> +
> +/**
> +  Allocates one or more 4KB pages of type EfiBootServicesData.
> +
> +  Allocates the number of 4KB pages of type EfiBootServicesData and
> + returns a pointer to the  allocated buffer.  The buffer returned is
> + aligned on a 4KB boundary.  If Pages is 0, then NULL  is returned.
> + If there is not enough memory remaining to satisfy the request, then NULL is  returned.
> +
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocatePages (
> +  IN UINTN  Pages
> +  )
> +{
> +  return InternalAllocatePages (EfiRuntimeServicesData, Pages); }
> +
> +/**
> +  Allocates one or more 4KB pages of type EfiRuntimeServicesData.
> +
> +  Allocates the number of 4KB pages of type EfiRuntimeServicesData
> + and returns a pointer to the  allocated buffer.  The buffer returned
> + is aligned on a 4KB boundary.  If Pages is 0, then NULL  is
> + returned.  If there is not enough memory remaining to satisfy the request, then NULL is  returned.
> +
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateRuntimePages (
> +  IN UINTN  Pages
> +  )
> +{
> +  return InternalAllocatePages (EfiRuntimeServicesData, Pages); }
> +
> +/**
> +  Allocates one or more 4KB pages of type EfiReservedMemoryType.
> +
> +  Allocates the number of 4KB pages of type EfiReservedMemoryType and
> + returns a pointer to the  allocated buffer.  The buffer returned is
> + aligned on a 4KB boundary.  If Pages is 0, then NULL  is returned.
> + If there is not enough memory remaining to satisfy the request, then NULL is  returned.
> +
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateReservedPages (
> +  IN UINTN  Pages
> +  )
> +{
> +  return NULL;
> +}
> +
> +/**
> +  Frees one or more 4KB pages that were previously allocated with one
> +of the page allocation
> +  functions in the Memory Allocation Library.
> +
> +  Frees the number of 4KB pages specified by Pages from the buffer
> + specified by Buffer.  Buffer  must have been allocated on a previous
> + call to the page allocation services of the Memory  Allocation
> + Library.  If it is not possible to free allocated pages, then this function will  perform no actions.
> +
> +  If Buffer was not allocated with a page allocation function in the
> + Memory Allocation Library,  then ASSERT().
> +  If Pages is zero, then ASSERT().
> +
> +  @param  Buffer                Pointer to the buffer of pages to free.
> +  @param  Pages                 The number of 4 KB pages to free.
> +
> +**/
> +VOID
> +EFIAPI
> +FreePages (
> +  IN VOID   *Buffer,
> +  IN UINTN  Pages
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  ASSERT (Pages != 0);
> +  Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer,
> +Pages);
> +  ASSERT_EFI_ERROR (Status);
> +}
> +
> +/**
> +  Allocates one or more 4KB pages of a certain memory type at a specified alignment.
> +
> +  Allocates the number of 4KB pages specified by Pages of a certain
> + memory type with an alignment  specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is returned.
> +  If there is not enough memory at the specified alignment remaining
> + to satisfy the request, then  NULL is returned.
> +  If Alignment is not a power of two and Alignment is not zero, then ASSERT().
> +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> +
> +  @param  MemoryType            The type of memory to allocate.
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.
> +                                If Alignment is zero, then byte alignment is used.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +InternalAllocateAlignedPages (
> +  IN EFI_MEMORY_TYPE  MemoryType,
> +  IN UINTN            Pages,
> +  IN UINTN            Alignment
> +  )
> +{
> +  EFI_STATUS            Status;
> +  EFI_PHYSICAL_ADDRESS  Memory;
> +  UINTN                 AlignedMemory;
> +  UINTN                 AlignmentMask;
> +  UINTN                 UnalignedPages;
> +  UINTN                 RealPages;
> +
> +  //
> +  // Alignment must be a power of two or zero.
> +  //
> +  ASSERT ((Alignment & (Alignment - 1)) == 0);
> +
> +  if (Pages == 0) {
> +    return NULL;
> +  }
> +  if (Alignment > EFI_PAGE_SIZE) {
> +    //
> +    // Calculate the total number of pages since alignment is larger than page size.
> +    //
> +    AlignmentMask  = Alignment - 1;
> +    RealPages      = Pages + EFI_SIZE_TO_PAGES (Alignment);
> +    //
> +    // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
> +    //
> +    ASSERT (RealPages > Pages);
> +
> +    Status         = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);
> +    if (EFI_ERROR (Status)) {
> +      return NULL;
> +    }
> +    AlignedMemory  = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;
> +    UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);
> +    if (UnalignedPages > 0) {
> +      //
> +      // Free first unaligned page(s).
> +      //
> +      Status = gMmst->MmFreePages (Memory, UnalignedPages);
> +      ASSERT_EFI_ERROR (Status);
> +    }
> +    Memory         = (EFI_PHYSICAL_ADDRESS) (AlignedMemory + EFI_PAGES_TO_SIZE (Pages));
> +    UnalignedPages = RealPages - Pages - UnalignedPages;
> +    if (UnalignedPages > 0) {
> +      //
> +      // Free last unaligned page(s).
> +      //
> +      Status = gMmst->MmFreePages (Memory, UnalignedPages);
> +      ASSERT_EFI_ERROR (Status);
> +    }
> +  } else {
> +    //
> +    // Do not over-allocate pages in this case.
> +    //
> +    Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);
> +    if (EFI_ERROR (Status)) {
> +      return NULL;
> +    }
> +    AlignedMemory  = (UINTN) Memory;
> +  }
> +  return (VOID *) AlignedMemory;
> +}
> +
> +/**
> +  Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
> +
> +  Allocates the number of 4KB pages specified by Pages of type
> + EfiBootServicesData with an  alignment specified by Alignment.  The
> + allocated buffer is returned.  If Pages is 0, then NULL is
> + returned.  If there is not enough memory at the specified alignment remaining to satisfy the  request, then NULL is returned.
> +
> +  If Alignment is not a power of two and Alignment is not zero, then ASSERT().
> +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> +
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.
> +                                If Alignment is zero, then byte alignment is used.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateAlignedPages (
> +  IN UINTN  Pages,
> +  IN UINTN  Alignment
> +  )
> +{
> +  return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages,
> +Alignment); }
> +
> +/**
> +  Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
> +
> +  Allocates the number of 4KB pages specified by Pages of type
> + EfiRuntimeServicesData with an  alignment specified by Alignment.
> + The allocated buffer is returned.  If Pages is 0, then NULL is
> + returned.  If there is not enough memory at the specified alignment remaining to satisfy the  request, then NULL is returned.
> +
> +  If Alignment is not a power of two and Alignment is not zero, then ASSERT().
> +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> +
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.
> +                                If Alignment is zero, then byte alignment is used.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateAlignedRuntimePages (
> +  IN UINTN  Pages,
> +  IN UINTN  Alignment
> +  )
> +{
> +  return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages,
> +Alignment); }
> +
> +/**
> +  Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
> +
> +  Allocates the number of 4KB pages specified by Pages of type
> + EfiReservedMemoryType with an  alignment specified by Alignment.
> + The allocated buffer is returned.  If Pages is 0, then NULL is
> + returned.  If there is not enough memory at the specified alignment remaining to satisfy the  request, then NULL is returned.
> +
> +  If Alignment is not a power of two and Alignment is not zero, then ASSERT().
> +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> +
> +  @param  Pages                 The number of 4 KB pages to allocate.
> +  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.
> +                                If Alignment is zero, then byte alignment is used.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateAlignedReservedPages (
> +  IN UINTN  Pages,
> +  IN UINTN  Alignment
> +  )
> +{
> +  return NULL;
> +}
> +
> +/**
> +  Frees one or more 4KB pages that were previously allocated with one
> +of the aligned page
> +  allocation functions in the Memory Allocation Library.
> +
> +  Frees the number of 4KB pages specified by Pages from the buffer
> + specified by Buffer.  Buffer  must have been allocated on a previous
> + call to the aligned page allocation services of the Memory
> + Allocation Library.  If it is not possible to free allocated pages, then this function will  perform no actions.
> +
> +  If Buffer was not allocated with an aligned page allocation
> + function in the Memory Allocation  Library, then ASSERT().
> +  If Pages is zero, then ASSERT().
> +
> +  @param  Buffer                Pointer to the buffer of pages to free.
> +  @param  Pages                 The number of 4 KB pages to free.
> +
> +**/
> +VOID
> +EFIAPI
> +FreeAlignedPages (
> +  IN VOID   *Buffer,
> +  IN UINTN  Pages
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  ASSERT (Pages != 0);
> +  Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer,
> +Pages);
> +  ASSERT_EFI_ERROR (Status);
> +}
> +
> +/**
> +  Allocates a buffer of a certain pool type.
> +
> +  Allocates the number bytes specified by AllocationSize of a certain
> + pool type and returns a  pointer to the allocated buffer.  If
> + AllocationSize is 0, then a valid buffer of 0 size is  returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  @param  MemoryType            The type of memory to allocate.
> +  @param  AllocationSize        The number of bytes to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +InternalAllocatePool (
> +  IN EFI_MEMORY_TYPE  MemoryType,
> +  IN UINTN            AllocationSize
> +  )
> +{
> +  EFI_STATUS  Status;
> +  VOID        *Memory;
> +
> +  Memory = NULL;
> +
> +  Status = gMmst->MmAllocatePool (MemoryType, AllocationSize,
> +&Memory);
> +  if (EFI_ERROR (Status)) {
> +    Memory = NULL;
> +  }
> +  return Memory;
> +}
> +
> +/**
> +  Allocates a buffer of type EfiBootServicesData.
> +
> +  Allocates the number bytes specified by AllocationSize of type
> + EfiBootServicesData and returns a  pointer to the allocated buffer.
> + If AllocationSize is 0, then a valid buffer of 0 size is  returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  @param  AllocationSize        The number of bytes to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocatePool (
> +  IN UINTN  AllocationSize
> +  )
> +{
> +  return InternalAllocatePool (EfiRuntimeServicesData,
> +AllocationSize); }
> +
> +/**
> +  Allocates a buffer of type EfiRuntimeServicesData.
> +
> +  Allocates the number bytes specified by AllocationSize of type
> + EfiRuntimeServicesData and returns  a pointer to the allocated
> + buffer.  If AllocationSize is 0, then a valid buffer of 0 size is  returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  @param  AllocationSize        The number of bytes to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateRuntimePool (
> +  IN UINTN  AllocationSize
> +  )
> +{
> +  return InternalAllocatePool (EfiRuntimeServicesData,
> +AllocationSize); }
> +
> +/**
> +  Allocates a buffer of type EfiReservedMemoryType.
> +
> +  Allocates the number bytes specified by AllocationSize of type
> + EfiReservedMemoryType and returns  a pointer to the allocated
> + buffer.  If AllocationSize is 0, then a valid buffer of 0 size is  returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  @param  AllocationSize        The number of bytes to allocate.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateReservedPool (
> +  IN UINTN  AllocationSize
> +  )
> +{
> +  return NULL;
> +}
> +
> +/**
> +  Allocates and zeros a buffer of a certain pool type.
> +
> +  Allocates the number bytes specified by AllocationSize of a certain
> + pool type, clears the buffer  with zeros, and returns a pointer to
> + the allocated buffer.  If AllocationSize is 0, then a valid  buffer
> + of 0 size is returned.  If there is not enough memory remaining to satisfy the request,  then NULL is returned.
> +
> +  @param  PoolType              The type of memory to allocate.
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +InternalAllocateZeroPool (
> +  IN EFI_MEMORY_TYPE  PoolType,
> +  IN UINTN            AllocationSize
> +  )
> +{
> +  VOID  *Memory;
> +
> +  Memory = InternalAllocatePool (PoolType, AllocationSize);
> +  if (Memory != NULL) {
> +    Memory = ZeroMem (Memory, AllocationSize);
> +  }
> +  return Memory;
> +}
> +
> +/**
> +  Allocates and zeros a buffer of type EfiBootServicesData.
> +
> +  Allocates the number bytes specified by AllocationSize of type
> + EfiBootServicesData, clears the  buffer with zeros, and returns a
> + pointer to the allocated buffer.  If AllocationSize is 0, then a
> + valid buffer of 0 size is returned.  If there is not enough memory remaining to satisfy the  request, then NULL is returned.
> +
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateZeroPool (
> +  IN UINTN  AllocationSize
> +  )
> +{
> +  return InternalAllocateZeroPool (EfiRuntimeServicesData,
> +AllocationSize); }
> +
> +/**
> +  Allocates and zeros a buffer of type EfiRuntimeServicesData.
> +
> +  Allocates the number bytes specified by AllocationSize of type
> + EfiRuntimeServicesData, clears the  buffer with zeros, and returns a
> + pointer to the allocated buffer.  If AllocationSize is 0, then a
> + valid buffer of 0 size is returned.  If there is not enough memory remaining to satisfy the  request, then NULL is returned.
> +
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateRuntimeZeroPool (
> +  IN UINTN  AllocationSize
> +  )
> +{
> +  return InternalAllocateZeroPool (EfiRuntimeServicesData,
> +AllocationSize); }
> +
> +/**
> +  Allocates and zeros a buffer of type EfiReservedMemoryType.
> +
> +  Allocates the number bytes specified by AllocationSize of type
> + EfiReservedMemoryType, clears the  buffer with zeros, and returns a
> + pointer to the allocated buffer.  If AllocationSize is 0, then a
> + valid buffer of 0 size is returned.  If there is not enough memory remaining to satisfy the  request, then NULL is returned.
> +
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateReservedZeroPool (
> +  IN UINTN  AllocationSize
> +  )
> +{
> +  return NULL;
> +}
> +
> +/**
> +  Copies a buffer to an allocated buffer of a certain pool type.
> +
> +  Allocates the number bytes specified by AllocationSize of a certain
> + pool type, copies  AllocationSize bytes from Buffer to the newly
> + allocated buffer, and returns a pointer to the  allocated buffer.
> + If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there  is not enough memory remaining to satisfy the request, then NULL is returned.
> +  If Buffer is NULL, then ASSERT().
> +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
> +
> +  @param  PoolType              The type of pool to allocate.
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +  @param  Buffer                The buffer to copy to the allocated buffer.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +InternalAllocateCopyPool (
> +  IN EFI_MEMORY_TYPE  PoolType,
> +  IN UINTN            AllocationSize,
> +  IN CONST VOID       *Buffer
> +  )
> +{
> +  VOID  *Memory;
> +
> +  ASSERT (Buffer != NULL);
> +  ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));
> +
> +  Memory = InternalAllocatePool (PoolType, AllocationSize);
> +  if (Memory != NULL) {
> +     Memory = CopyMem (Memory, Buffer, AllocationSize);
> +  }
> +  return Memory;
> +}
> +
> +/**
> +  Copies a buffer to an allocated buffer of type EfiBootServicesData.
> +
> +  Allocates the number bytes specified by AllocationSize of type
> + EfiBootServicesData, copies  AllocationSize bytes from Buffer to the
> + newly allocated buffer, and returns a pointer to the  allocated
> + buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there  is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  If Buffer is NULL, then ASSERT().
> +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
> +
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +  @param  Buffer                The buffer to copy to the allocated buffer.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateCopyPool (
> +  IN UINTN       AllocationSize,
> +  IN CONST VOID  *Buffer
> +  )
> +{
> +  return InternalAllocateCopyPool (EfiRuntimeServicesData,
> +AllocationSize, Buffer); }
> +
> +/**
> +  Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
> +
> +  Allocates the number bytes specified by AllocationSize of type
> + EfiRuntimeServicesData, copies  AllocationSize bytes from Buffer to
> + the newly allocated buffer, and returns a pointer to the  allocated
> + buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there  is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  If Buffer is NULL, then ASSERT().
> +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
> +
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +  @param  Buffer                The buffer to copy to the allocated buffer.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateRuntimeCopyPool (
> +  IN UINTN       AllocationSize,
> +  IN CONST VOID  *Buffer
> +  )
> +{
> +  return InternalAllocateCopyPool (EfiRuntimeServicesData,
> +AllocationSize, Buffer); }
> +
> +/**
> +  Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
> +
> +  Allocates the number bytes specified by AllocationSize of type
> + EfiReservedMemoryType, copies  AllocationSize bytes from Buffer to
> + the newly allocated buffer, and returns a pointer to the  allocated
> + buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there  is not enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  If Buffer is NULL, then ASSERT().
> +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
> +
> +  @param  AllocationSize        The number of bytes to allocate and zero.
> +  @param  Buffer                The buffer to copy to the allocated buffer.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +AllocateReservedCopyPool (
> +  IN UINTN       AllocationSize,
> +  IN CONST VOID  *Buffer
> +  )
> +{
> +  return NULL;
> +}
> +
> +/**
> +  Reallocates a buffer of a specified memory type.
> +
> +  Allocates and zeros the number bytes specified by NewSize from
> + memory of the type  specified by PoolType.  If OldBuffer is not
> + NULL, then the smaller of OldSize and  NewSize bytes are copied from
> + OldBuffer to the newly allocated buffer, and  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If
> + there is not  enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  If the allocation of the new buffer is successful and the smaller
> + of NewSize and OldSize  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> +
> +  @param  PoolType       The type of pool to allocate.
> +  @param  OldSize        The size, in bytes, of OldBuffer.
> +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> +  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional
> +                         parameter that may be NULL.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +InternalReallocatePool (
> +  IN EFI_MEMORY_TYPE  PoolType,
> +  IN UINTN            OldSize,
> +  IN UINTN            NewSize,
> +  IN VOID             *OldBuffer  OPTIONAL
> +  )
> +{
> +  VOID  *NewBuffer;
> +
> +  NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);
> +  if (NewBuffer != NULL && OldBuffer != NULL) {
> +    CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));
> +    FreePool (OldBuffer);
> +  }
> +  return NewBuffer;
> +}
> +
> +/**
> +  Reallocates a buffer of type EfiBootServicesData.
> +
> +  Allocates and zeros the number bytes specified by NewSize from
> + memory of type  EfiBootServicesData.  If OldBuffer is not NULL, then
> + the smaller of OldSize and  NewSize bytes are copied from OldBuffer
> + to the newly allocated buffer, and  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If
> + there is not  enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  If the allocation of the new buffer is successful and the smaller
> + of NewSize and OldSize  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> +
> +  @param  OldSize        The size, in bytes, of OldBuffer.
> +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> +  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional
> +                         parameter that may be NULL.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +ReallocatePool (
> +  IN UINTN  OldSize,
> +  IN UINTN  NewSize,
> +  IN VOID   *OldBuffer  OPTIONAL
> +  )
> +{
> +  return InternalReallocatePool (EfiRuntimeServicesData, OldSize,
> +NewSize, OldBuffer); }
> +
> +/**
> +  Reallocates a buffer of type EfiRuntimeServicesData.
> +
> +  Allocates and zeros the number bytes specified by NewSize from
> + memory of type  EfiRuntimeServicesData.  If OldBuffer is not NULL,
> + then the smaller of OldSize and  NewSize bytes are copied from
> + OldBuffer to the newly allocated buffer, and  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If
> + there is not  enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  If the allocation of the new buffer is successful and the smaller
> + of NewSize and OldSize  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> +
> +  @param  OldSize        The size, in bytes, of OldBuffer.
> +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> +  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional
> +                         parameter that may be NULL.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +ReallocateRuntimePool (
> +  IN UINTN  OldSize,
> +  IN UINTN  NewSize,
> +  IN VOID   *OldBuffer  OPTIONAL
> +  )
> +{
> +  return InternalReallocatePool (EfiRuntimeServicesData, OldSize,
> +NewSize, OldBuffer); }
> +
> +/**
> +  Reallocates a buffer of type EfiReservedMemoryType.
> +
> +  Allocates and zeros the number bytes specified by NewSize from
> + memory of type  EfiReservedMemoryType.  If OldBuffer is not NULL,
> + then the smaller of OldSize and  NewSize bytes are copied from
> + OldBuffer to the newly allocated buffer, and  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If
> + there is not  enough memory remaining to satisfy the request, then NULL is returned.
> +
> +  If the allocation of the new buffer is successful and the smaller
> + of NewSize and OldSize  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> +
> +  @param  OldSize        The size, in bytes, of OldBuffer.
> +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> +  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional
> +                         parameter that may be NULL.
> +
> +  @return A pointer to the allocated buffer or NULL if allocation fails.
> +
> +**/
> +VOID *
> +EFIAPI
> +ReallocateReservedPool (
> +  IN UINTN  OldSize,
> +  IN UINTN  NewSize,
> +  IN VOID   *OldBuffer  OPTIONAL
> +  )
> +{
> +  return NULL;
> +}
> +
> +/**
> +  Frees a buffer that was previously allocated with one of the pool
> +allocation functions in the
> +  Memory Allocation Library.
> +
> +  Frees the buffer specified by Buffer.  Buffer must have been
> + allocated on a previous call to the  pool allocation services of the
> + Memory Allocation Library.  If it is not possible to free pool  resources, then this function will perform no actions.
> +
> +  If Buffer was not allocated with a pool allocation function in the
> + Memory Allocation Library,  then ASSERT().
> +
> +  @param  Buffer                Pointer to the buffer to free.
> +
> +**/
> +VOID
> +EFIAPI
> +FreePool (
> +  IN VOID   *Buffer
> +  )
> +{
> +  EFI_STATUS    Status;
> +
> +  Status = gMmst->MmFreePool (Buffer);
> +  ASSERT_EFI_ERROR (Status);
> +}
> +
> +/**
> +  The constructor function calls MmInitializeMemoryServices to
> +initialize
> +  memory in MMRAM and caches EFI_MM_SYSTEM_TABLE pointer.
> +
> +  @param  ImageHandle   The firmware allocated handle for the EFI image.
> +  @param  SystemTable   A pointer to the Management mode System Table.
> +
> +  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MemoryAllocationLibConstructor (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  )
> +{
> +  MM_CORE_PRIVATE_DATA           *MmCorePrivate;
> +  EFI_HOB_GUID_TYPE               *GuidHob;
> +  MM_CORE_DATA_HOB_DATA          *DataInHob;
> +  VOID                            *HobStart;
> +  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
> +  EFI_MMRAM_DESCRIPTOR            *MmramRanges;
> +  UINT32                           MmramRangeCount;
> +  EFI_HOB_GUID_TYPE               *MmramRangesHob;
> +
> +  HobStart = GetHobList ();
> +  DEBUG ((DEBUG_INFO, "StandaloneMmCoreMemoryAllocationLibConstructor
> + - 0x%x\n", HobStart));
> +
> +  //
> +  // Extract MM Core Private context from the Hob. If absent search
> + for  // a Hob containing the MMRAM ranges  //  GuidHob =
> + GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);  if (GuidHob ==
> + NULL) {
> +    MmramRangesHob = GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid, HobStart);
> +    if (MmramRangesHob == NULL) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +    MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
> +    if (MmramRangesHobData == NULL) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +    MmramRanges = MmramRangesHobData->Descriptor;
> +    if (MmramRanges == NULL) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +    MmramRangeCount = MmramRangesHobData->NumberOfMmReservedRegions;
> +    if (MmramRanges == NULL) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +  } else {
> +    DataInHob      = GET_GUID_HOB_DATA (GuidHob);
> +    MmCorePrivate = (MM_CORE_PRIVATE_DATA *)(UINTN)DataInHob->Address;
> +    MmramRanges     = (EFI_MMRAM_DESCRIPTOR *)(UINTN)MmCorePrivate->MmramRanges;
> +    MmramRangeCount = MmCorePrivate->MmramRangeCount;  }
> +
> +  {
> +    UINTN                Index;
> +
> +    DEBUG ((DEBUG_INFO, "MmramRangeCount - 0x%x\n", MmramRangeCount));
> +    for (Index = 0; Index < MmramRangeCount; Index++) {
> +      DEBUG ((DEBUG_INFO, "MmramRanges[%d]: 0x%016lx - 0x%016lx\n", Index, MmramRanges[Index].CpuStart, MmramRanges[Index].PhysicalSize));
> +    }
> +  }
> +
> +  //
> +  // Initialize memory service using free MMRAM  //  DEBUG
> + ((DEBUG_INFO, "MmInitializeMemoryServices\n"));
> + MmInitializeMemoryServices ((UINTN)MmramRangeCount, (VOID
> + *)(UINTN)MmramRanges);
> +
> +  // Initialize MM Services Table
> +  gMmst = MmSystemTable;
> +  return EFI_SUCCESS;
> +}
> diff --git
> a/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
> b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
> new file mode 100644
> index 0000000000..068607f90e
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.
> +++ inf
> @@ -0,0 +1,49 @@
> +## @file
> +# Memory Allocation Library instance dedicated to MM Core.
> +# The implementation borrows the MM Core Memory Allocation services
> +as the primitive # for memory allocation instead of using MM System Table servces in an indirect way.
> +# It is assumed that this library instance must be linked with MM Core in this package.
> +#
> +# Copyright (c) 2010 - 2015, Intel Corporation. All rights
> +reserved.<BR> # Copyright (c) 2016 - 2017, ARM Limited. All rights
> +reserved.<BR> # #  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
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, #  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = MemoryAllocationLib
> +  FILE_GUID                      = DCDCBE1D-E760-4E1D-85B4-96E3F0439C41
> +  MODULE_TYPE                    = MM_CORE_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  LIBRARY_CLASS                  = MemoryAllocationLib|MM_CORE_STANDALONE
> +  CONSTRUCTOR                    = MemoryAllocationLibConstructor
> +
> +#
> +# The following information is for reference only and not required by the build tools.
> +#
> +#  VALID_ARCHITECTURES           = IA32 X64
> +#
> +
> +[Sources]
> +  MemoryAllocationLib.c
> +  MemoryAllocationServices.h
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[LibraryClasses]
> +  BaseMemoryLib
> +  DebugLib
> +  HobLib
> +
> +[Guids]
> +  gEfiMmPeiMmramMemoryReserveGuid
> diff --git
> a/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices
> .h
> b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices
> .h
> new file mode 100644
> index 0000000000..eb4f4c3984
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServ
> +++ ices.h
> @@ -0,0 +1,38 @@
> +/** @file
> +  Contains function prototypes for Memory Services in the MM Core.
> +
> +  This header file borrows the StandaloneMmCore Memory Allocation
> + services as the primitive  for memory allocation.
> +
> +  Copyright (c) 2008 - 2015, Intel Corporation. All rights
> + reserved.<BR>  Copyright (c) 2016 - 2017, ARM Limited. All rights
> + reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _PI_MM_CORE_MEMORY_ALLOCATION_SERVICES_H_
> +#define _PI_MM_CORE_MEMORY_ALLOCATION_SERVICES_H_
> +
> +#include <Guid/MmCoreData.h>
> +
> +/**
> +  Called to initialize the memory service.
> +
> +  @param   MmramRangeCount       Number of MMRAM Regions
> +  @param   MmramRanges           Pointer to MMRAM Descriptors
> +
> +**/
> +VOID
> +MmInitializeMemoryServices (
> +  IN UINTN                 MmramRangeCount,
> +  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
> +  );
> +
> +#endif
> --
> 2.16.2
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 10/18] StandaloneMmPkg/HobLib: Add AARCH64 Specific HOB Library for management mode.
  2018-04-26 13:04     ` Yao, Jiewen
@ 2018-05-04 23:22       ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:22 UTC (permalink / raw)
  To: Yao, Jiewen, Achin Gupta; +Cc: edk2-devel@lists.01.org

My response inline.

-----Original Message-----
From: Yao, Jiewen <jiewen.yao@intel.com>
Sent: Thursday, April 26, 2018 8:04 AM
To: Achin Gupta <Achin.Gupta@arm.com>; Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming <liming.gao@intel.com>; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: RE: [PATCH v1 10/18] StandaloneMmPkg/HobLib: Add AARCH64 Specific HOB Library for management mode.

Maybe we can use same layout as MmMemLib.

It seems only HobConstructor() is Arm specific. Other functions are quite generic.

[Supreeth] Ok. I made the changes. Please check version 2.

Thank you

> -----Original Message-----
> From: Achin Gupta [mailto:achin.gupta@arm.com]
> Sent: Wednesday, April 25, 2018 7:50 AM
> To: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> Cc: edk2-devel@lists.01.org; Kinney, Michael D
> <michael.d.kinney@intel.com>; Gao, Liming <liming.gao@intel.com>; Yao,
> Jiewen <jiewen.yao@intel.com>; leif.lindholm@linaro.org;
> ard.biesheuvel@linaro.org; nd@arm.com
> Subject: Re: [PATCH v1 10/18] StandaloneMmPkg/HobLib: Add AARCH64
> Specific HOB Library for management mode.
>
> Hi Supreeth,
>
> On Fri, Apr 06, 2018 at 03:42:15PM +0100, Supreeth Venkatesh wrote:
> > The Standalone MM environment is initialized during the SEC phase on
> > ARM Standard Platforms. The MM Core driver implements an entry point
> > module which is architecture specific and runs prior to the generic
> > core driver code. The former creates a Hob list that the latter
> > consumes. This happens in the same phase.
> >
> > This patch implements a Hob library that can be used by the entry
> > point module to produce a Hob list and by the core driver code to consume it.
>
> References to DXE core need to be removed and the copyright years
> needs to be updated.
>
> I think it is worth getting this hoblib reviewed by the ArmPkg maintainers.
>
> Acked-by: Achin Gupta <achin.gupta@arm.com>
>
> >
> > Contributed-under: TianoCore Contribution Agreement 1.1
> > Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> > Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> > ---
> >  StandaloneMmPkg/Library/HobLib/Arm/HobLib.c | 697
> ++++++++++++++++++++++++++++
> >  StandaloneMmPkg/Library/HobLib/HobLib.inf   |  45 ++
> >  2 files changed, 742 insertions(+)
> >  create mode 100644 StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
> >  create mode 100644 StandaloneMmPkg/Library/HobLib/HobLib.inf
> >
> > diff --git a/StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
> b/StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
> > new file mode 100644
> > index 0000000000..62abf47f95
> > --- /dev/null
> > +++ b/StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
> > @@ -0,0 +1,697 @@
> > +/** @file
> > +  HOB Library implementation for DxeCore driver.
> > +
> > +Copyright (c) 2006 - 2014, Intel Corporation. All rights
> > +reserved.<BR> Copyright (c) 2017, ARM Limited. All rights
> > +reserved.<BR>
> > +
> > +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.
> > +
> > +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +
> > +**/
> > +
> > +#include <PiMm.h>
> > +
> > +#include <Library/HobLib.h>
> > +#include <Library/DebugLib.h>
> > +#include <Library/BaseMemoryLib.h>
> > +
> > +#include <Guid/MemoryAllocationHob.h>
> > +
> > +//
> > +// Cache copy of HobList pointer.
> > +//
> > +VOID *gHobList = NULL;
> > +
> > +/**
> > +  Returns the pointer to the HOB list.
> > +
> > +  This function returns the pointer to first HOB in the list.
> > +  For PEI phase, the PEI service GetHobList() can be used to
> > + retrieve the
> pointer
> > +  to the HOB list.  For the DXE phase, the HOB list pointer can be
> > + retrieved
> through
> > +  the EFI System Table by looking up theHOB list GUID in the System
> Configuration Table.
> > +  Since the System Configuration Table does not exist that the time
> > + the DXE
> Core is
> > +  launched, the DXE Core uses a global variable from the DXE Core
> > + Entry
> Point Library
> > +  to manage the pointer to the HOB list.
> > +
> > +  If the pointer to the HOB list is NULL, then ASSERT().
> > +
> > +  @return The pointer to the HOB list.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +GetHobList (
> > +  VOID
> > +  )
> > +{
> > +  ASSERT (gHobList != NULL);
> > +  return gHobList;
> > +}
> > +
> > +/**
> > +  Returns the next instance of a HOB type from the starting HOB.
> > +
> > +  This function searches the first instance of a HOB type from the
> > + starting
> HOB pointer.
> > +  If there does not exist such HOB type from the starting HOB
> > + pointer, it will
> return NULL.
> > +  In contrast with macro GET_NEXT_HOB(), this function does not
> > + skip the
> starting HOB pointer
> > +  unconditionally: it returns HobStart back if HobStart itself
> > + meets the
> requirement;
> > +  caller is required to use GET_NEXT_HOB() if it wishes to skip
> > + current
> HobStart.
> > +
> > +  If HobStart is NULL, then ASSERT().
> > +
> > +  @param  Type          The HOB type to return.
> > +  @param  HobStart      The starting HOB pointer to search from.
> > +
> > +  @return The next instance of a HOB type from the starting HOB.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +GetNextHob (
> > +  IN UINT16                 Type,
> > +  IN CONST VOID             *HobStart
> > +  )
> > +{
> > +  EFI_PEI_HOB_POINTERS  Hob;
> > +
> > +  ASSERT (HobStart != NULL);
> > +
> > +  Hob.Raw = (UINT8 *) HobStart;
> > +  //
> > +  // Parse the HOB list until end of list or matching type is found.
> > +  //
> > +  while (!END_OF_HOB_LIST (Hob)) {
> > +    if (Hob.Header->HobType == Type) {
> > +      return Hob.Raw;
> > +    }
> > +    Hob.Raw = GET_NEXT_HOB (Hob);
> > +  }
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Returns the first instance of a HOB type among the whole HOB list.
> > +
> > +  This function searches the first instance of a HOB type among the
> > + whole
> HOB list.
> > +  If there does not exist such HOB type in the HOB list, it will return NULL.
> > +
> > +  If the pointer to the HOB list is NULL, then ASSERT().
> > +
> > +  @param  Type          The HOB type to return.
> > +
> > +  @return The next instance of a HOB type from the starting HOB.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +GetFirstHob (
> > +  IN UINT16                 Type
> > +  )
> > +{
> > +  VOID      *HobList;
> > +
> > +  HobList = GetHobList ();
> > +  return GetNextHob (Type, HobList); }
> > +
> > +/**
> > +  Returns the next instance of the matched GUID HOB from the starting HOB.
> > +
> > +  This function searches the first instance of a HOB from the
> > + starting HOB
> pointer.
> > +  Such HOB should satisfy two conditions:
> > +  its HOB type is EFI_HOB_TYPE_GUID_EXTENSION, and its GUID Name
> equals to the input Guid.
> > +  If such a HOB from the starting HOB pointer does not exist, it
> > + will return
> NULL.
> > +  Caller is required to apply GET_GUID_HOB_DATA () and
> GET_GUID_HOB_DATA_SIZE ()
> > +  to extract the data section and its size information, respectively.
> > +  In contrast with macro GET_NEXT_HOB(), this function does not
> > + skip the
> starting HOB pointer
> > +  unconditionally: it returns HobStart back if HobStart itself
> > + meets the
> requirement;
> > +  caller is required to use GET_NEXT_HOB() if it wishes to skip
> > + current
> HobStart.
> > +
> > +  If Guid is NULL, then ASSERT().
> > +  If HobStart is NULL, then ASSERT().
> > +
> > +  @param  Guid          The GUID to match with in the HOB list.
> > +  @param  HobStart      A pointer to a Guid.
> > +
> > +  @return The next instance of the matched GUID HOB from the
> > + starting
> HOB.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +GetNextGuidHob (
> > +  IN CONST EFI_GUID         *Guid,
> > +  IN CONST VOID             *HobStart
> > +  )
> > +{
> > +  EFI_PEI_HOB_POINTERS  GuidHob;
> > +
> > +  GuidHob.Raw = (UINT8 *) HobStart;  while ((GuidHob.Raw =
> > + GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION,
> GuidHob.Raw)) != NULL) {
> > +    if (CompareGuid (Guid, &GuidHob.Guid->Name)) {
> > +      break;
> > +    }
> > +    GuidHob.Raw = GET_NEXT_HOB (GuidHob);
> > +  }
> > +  return GuidHob.Raw;
> > +}
> > +
> > +/**
> > +  Returns the first instance of the matched GUID HOB among the
> > +whole HOB
> list.
> > +
> > +  This function searches the first instance of a HOB among the
> > + whole HOB
> list.
> > +  Such HOB should satisfy two conditions:
> > +  its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name
> equals to the input Guid.
> > +  If such a HOB from the starting HOB pointer does not exist, it
> > + will return
> NULL.
> > +  Caller is required to apply GET_GUID_HOB_DATA () and
> GET_GUID_HOB_DATA_SIZE ()
> > +  to extract the data section and its size information, respectively.
> > +
> > +  If the pointer to the HOB list is NULL, then ASSERT().
> > +  If Guid is NULL, then ASSERT().
> > +
> > +  @param  Guid          The GUID to match with in the HOB list.
> > +
> > +  @return The first instance of the matched GUID HOB among the
> > + whole
> HOB list.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +GetFirstGuidHob (
> > +  IN CONST EFI_GUID         *Guid
> > +  )
> > +{
> > +  VOID      *HobList;
> > +
> > +  HobList = GetHobList ();
> > +  return GetNextGuidHob (Guid, HobList); }
> > +
> > +/**
> > +  Get the system boot mode from the HOB list.
> > +
> > +  This function returns the system boot mode information from the
> > + PHIT HOB in HOB list.
> > +
> > +  If the pointer to the HOB list is NULL, then ASSERT().
> > +
> > +  @param  VOID
> > +
> > +  @return The Boot Mode.
> > +
> > +**/
> > +EFI_BOOT_MODE
> > +EFIAPI
> > +GetBootModeHob (
> > +  VOID
> > +  )
> > +{
> > +  EFI_HOB_HANDOFF_INFO_TABLE    *HandOffHob;
> > +
> > +  HandOffHob = (EFI_HOB_HANDOFF_INFO_TABLE *) GetHobList ();
> > +
> > +  return  HandOffHob->BootMode;
> > +}
> > +
> > +
> > +/**
> > +
> > +
> > +**/
> > +EFI_HOB_HANDOFF_INFO_TABLE*
> > +HobConstructor (
> > +  IN VOID   *EfiMemoryBegin,
> > +  IN UINTN  EfiMemoryLength,
> > +  IN VOID   *EfiFreeMemoryBottom,
> > +  IN VOID   *EfiFreeMemoryTop
> > +  )
> > +{
> > +  EFI_HOB_HANDOFF_INFO_TABLE  *Hob;
> > +  EFI_HOB_GENERIC_HEADER      *HobEnd;
> > +
> > +  Hob    = EfiFreeMemoryBottom;
> > +  HobEnd = (EFI_HOB_GENERIC_HEADER *)(Hob+1);
> > +
> > +  Hob->Header.HobType     = EFI_HOB_TYPE_HANDOFF;
> > +  Hob->Header.HobLength   = sizeof(EFI_HOB_HANDOFF_INFO_TABLE);
> > +  Hob->Header.Reserved    = 0;
> > +
> > +  HobEnd->HobType     = EFI_HOB_TYPE_END_OF_HOB_LIST;
> > +  HobEnd->HobLength   = sizeof(EFI_HOB_GENERIC_HEADER);
> > +  HobEnd->Reserved    = 0;
> > +
> > +  Hob->Version             = EFI_HOB_HANDOFF_TABLE_VERSION;
> > +  Hob->BootMode            = BOOT_WITH_FULL_CONFIGURATION;
> > +
> > +  Hob->EfiMemoryTop        = (UINTN)EfiMemoryBegin +
> EfiMemoryLength;
> > +  Hob->EfiMemoryBottom     = (UINTN)EfiMemoryBegin;
> > +  Hob->EfiFreeMemoryTop    = (UINTN)EfiFreeMemoryTop;
> > +  Hob->EfiFreeMemoryBottom =
> (EFI_PHYSICAL_ADDRESS)(UINTN)(HobEnd+1);
> > +  Hob->EfiEndOfHobList     = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd;
> > +
> > +  gHobList = Hob;
> > +
> > +  return Hob;
> > +}
> > +
> > +VOID *
> > +CreateHob (
> > +  IN  UINT16    HobType,
> > +  IN  UINT16    HobLength
> > +  )
> > +{
> > +  EFI_HOB_HANDOFF_INFO_TABLE  *HandOffHob;
> > +  EFI_HOB_GENERIC_HEADER      *HobEnd;
> > +  EFI_PHYSICAL_ADDRESS        FreeMemory;
> > +  VOID                        *Hob;
> > +
> > +  HandOffHob = GetHobList ();
> > +
> > +  HobLength = (UINT16)((HobLength + 0x7) & (~0x7));
> > +
> > +  FreeMemory = HandOffHob->EfiFreeMemoryTop -
> HandOffHob->EfiFreeMemoryBottom;
> > +
> > +  if (FreeMemory < HobLength) {
> > +      return NULL;
> > +  }
> > +
> > +  Hob = (VOID*) (UINTN) HandOffHob->EfiEndOfHobList;
> > +  ((EFI_HOB_GENERIC_HEADER*) Hob)->HobType = HobType;
> > +  ((EFI_HOB_GENERIC_HEADER*) Hob)->HobLength = HobLength;
> > +  ((EFI_HOB_GENERIC_HEADER*) Hob)->Reserved = 0;
> > +
> > +  HobEnd = (EFI_HOB_GENERIC_HEADER*) ((UINTN)Hob + HobLength);
> > + HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN)
> HobEnd;
> > +
> > +  HobEnd->HobType   = EFI_HOB_TYPE_END_OF_HOB_LIST;
> > +  HobEnd->HobLength = sizeof(EFI_HOB_GENERIC_HEADER);
> > + HobEnd->Reserved  = 0;  HobEnd++;  HandOffHob->EfiFreeMemoryBottom
> > + = (EFI_PHYSICAL_ADDRESS) (UINTN)
> HobEnd;
> > +
> > +  return Hob;
> > +}
> > +
> > +/**
> > +  Builds a HOB for a loaded PE32 module.
> > +
> > +  This function builds a HOB for a loaded PE32 module.
> > +  It can only be invoked during PEI phase;  for DXE phase, it will
> > + ASSERT() since PEI HOB is read-only for DXE phase.
> > +  If ModuleName is NULL, then ASSERT().
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  ModuleName              The GUID File Name of the
> module.
> > +  @param  MemoryAllocationModule  The 64 bit physical address of
> > + the
> module.
> > +  @param  ModuleLength            The length of the module in bytes.
> > +  @param  EntryPoint              The 64 bit physical address of the
> module entry point.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildModuleHob (
> > +  IN CONST EFI_GUID         *ModuleName,
> > +  IN EFI_PHYSICAL_ADDRESS   MemoryAllocationModule,
> > +  IN UINT64                 ModuleLength,
> > +  IN EFI_PHYSICAL_ADDRESS   EntryPoint
> > +  )
> > +{
> > +  EFI_HOB_MEMORY_ALLOCATION_MODULE  *Hob;
> > +
> > +  ASSERT (((MemoryAllocationModule & (EFI_PAGE_SIZE - 1)) == 0) &&
> > +          ((ModuleLength & (EFI_PAGE_SIZE - 1)) == 0));
> > +
> > +  Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof
> (EFI_HOB_MEMORY_ALLOCATION_MODULE));
> > +
> > +  CopyGuid (&(Hob->MemoryAllocationHeader.Name),
> &gEfiHobMemoryAllocModuleGuid);
> > +  Hob->MemoryAllocationHeader.MemoryBaseAddress =
> MemoryAllocationModule;
> > +  Hob->MemoryAllocationHeader.MemoryLength      = ModuleLength;
> > +  Hob->MemoryAllocationHeader.MemoryType        =
> EfiBootServicesCode;
> > +
> > +  //
> > +  // Zero the reserved space to match HOB spec  //  ZeroMem
> > + (Hob->MemoryAllocationHeader.Reserved, sizeof
> (Hob->MemoryAllocationHeader.Reserved));
> > +
> > +  CopyGuid (&Hob->ModuleName, ModuleName);
> > +  Hob->EntryPoint = EntryPoint;
> > +}
> > +
> > +/**
> > +  Builds a HOB that describes a chunk of system memory.
> > +
> > +  This function builds a HOB that describes a chunk of system memory.
> > +  It can only be invoked during PEI phase;  for DXE phase, it will
> > + ASSERT() because PEI HOB is read-only for DXE phase.
> > +
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  ResourceType        The type of resource described by this
> HOB.
> > +  @param  ResourceAttribute   The resource attributes of the memory
> described by this HOB.
> > +  @param  PhysicalStart       The 64 bit physical address of memory
> described by this HOB.
> > +  @param  NumberOfBytes       The length of the memory described by
> this HOB in bytes.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildResourceDescriptorHob (
> > +  IN EFI_RESOURCE_TYPE            ResourceType,
> > +  IN EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttribute,
> > +  IN EFI_PHYSICAL_ADDRESS         PhysicalStart,
> > +  IN UINT64                       NumberOfBytes
> > +  )
> > +{
> > +  EFI_HOB_RESOURCE_DESCRIPTOR  *Hob;
> > +
> > +  Hob = CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof
> (EFI_HOB_RESOURCE_DESCRIPTOR));
> > +  ASSERT(Hob != NULL);
> > +
> > +  Hob->ResourceType      = ResourceType;
> > +  Hob->ResourceAttribute = ResourceAttribute;
> > +  Hob->PhysicalStart     = PhysicalStart;
> > +  Hob->ResourceLength    = NumberOfBytes;
> > +}
> > +
> > +/**
> > +  Builds a GUID HOB with a certain data length.
> > +
> > +  This function builds a customized HOB tagged with a GUID for
> > + identification  and returns the start address of GUID HOB data so
> > + that caller can fill the
> customized data.
> > +  The HOB Header and Name field is already stripped.
> > +  It can only be invoked during PEI phase;  for DXE phase, it will
> > + ASSERT() since PEI HOB is read-only for DXE phase.
> > +  If Guid is NULL, then ASSERT().
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +  If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
> > +
> > +  @param  Guid          The GUID to tag the customized HOB.
> > +  @param  DataLength    The size of the data payload for the GUID HOB.
> > +
> > +  @return The start address of GUID HOB data.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +BuildGuidHob (
> > +  IN CONST EFI_GUID              *Guid,
> > +  IN UINTN                       DataLength
> > +  )
> > +{
> > +  EFI_HOB_GUID_TYPE *Hob;
> > +
> > +  //
> > +  // Make sure that data length is not too long.
> > +  //
> > +  ASSERT (DataLength <= (0xffff - sizeof (EFI_HOB_GUID_TYPE)));
> > +
> > +  Hob = CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16) (sizeof
> (EFI_HOB_GUID_TYPE) + DataLength));
> > +  CopyGuid (&Hob->Name, Guid);
> > +  return Hob + 1;
> > +}
> > +
> > +
> > +/**
> > +  Copies a data buffer to a newly-built HOB.
> > +
> > +  This function builds a customized HOB tagged with a GUID for
> identification,
> > +  copies the input data to the HOB data field and returns the start
> > + address of
> the GUID HOB data.
> > +  The HOB Header and Name field is already stripped.
> > +  It can only be invoked during PEI phase;  for DXE phase, it will
> > + ASSERT() since PEI HOB is read-only for DXE phase.
> > +  If Guid is NULL, then ASSERT().
> > +  If Data is NULL and DataLength > 0, then ASSERT().
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +  If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
> > +
> > +  @param  Guid          The GUID to tag the customized HOB.
> > +  @param  Data          The data to be copied into the data field of the
> GUID HOB.
> > +  @param  DataLength    The size of the data payload for the GUID HOB.
> > +
> > +  @return The start address of GUID HOB data.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +BuildGuidDataHob (
> > +  IN CONST EFI_GUID              *Guid,
> > +  IN VOID                        *Data,
> > +  IN UINTN                       DataLength
> > +  )
> > +{
> > +  VOID  *HobData;
> > +
> > +  ASSERT (Data != NULL || DataLength == 0);
> > +
> > +  HobData = BuildGuidHob (Guid, DataLength);
> > +
> > +  return CopyMem (HobData, Data, DataLength); }
> > +
> > +/**
> > +  Builds a Firmware Volume HOB.
> > +
> > +  This function builds a Firmware Volume HOB.
> > +  It can only be invoked during PEI phase;  for DXE phase, it will
> > + ASSERT() because PEI HOB is read-only for DXE phase.
> > +
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  BaseAddress   The base address of the Firmware Volume.
> > +  @param  Length        The size of the Firmware Volume in bytes.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildFvHob (
> > +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> > +  IN UINT64                      Length
> > +  )
> > +{
> > +  EFI_HOB_FIRMWARE_VOLUME  *Hob;
> > +
> > +  Hob = CreateHob (EFI_HOB_TYPE_FV, sizeof
> (EFI_HOB_FIRMWARE_VOLUME));
> > +
> > +  Hob->BaseAddress = BaseAddress;
> > +  Hob->Length      = Length;
> > +}
> > +
> > +
> > +/**
> > +  Builds a EFI_HOB_TYPE_FV2 HOB.
> > +
> > +  This function builds a EFI_HOB_TYPE_FV2 HOB.
> > +  It can only be invoked during PEI phase;  for DXE phase, it will
> > + ASSERT() since PEI HOB is read-only for DXE phase.
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  BaseAddress   The base address of the Firmware Volume.
> > +  @param  Length        The size of the Firmware Volume in bytes.
> > +  @param  FvName       The name of the Firmware Volume.
> > +  @param  FileName      The name of the file.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildFv2Hob (
> > +  IN          EFI_PHYSICAL_ADDRESS        BaseAddress,
> > +  IN          UINT64                      Length,
> > +  IN CONST    EFI_GUID                    *FvName,
> > +  IN CONST    EFI_GUID                    *FileName
> > +  )
> > +{
> > +  EFI_HOB_FIRMWARE_VOLUME2  *Hob;
> > +
> > +  Hob = CreateHob (EFI_HOB_TYPE_FV2, sizeof
> (EFI_HOB_FIRMWARE_VOLUME2));
> > +
> > +  Hob->BaseAddress = BaseAddress;
> > +  Hob->Length      = Length;
> > +  CopyGuid (&Hob->FvName, FvName);
> > +  CopyGuid (&Hob->FileName, FileName); }
> > +
> > +
> > +/**
> > +  Builds a HOB for the CPU.
> > +
> > +  This function builds a HOB for the CPU.
> > +  It can only be invoked during PEI phase;  for DXE phase, it will
> > + ASSERT() since PEI HOB is read-only for DXE phase.
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  SizeOfMemorySpace   The maximum physical memory
> addressability of the processor.
> > +  @param  SizeOfIoSpace       The maximum physical I/O addressability
> of the processor.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildCpuHob (
> > +  IN UINT8                       SizeOfMemorySpace,
> > +  IN UINT8                       SizeOfIoSpace
> > +  )
> > +{
> > +  EFI_HOB_CPU  *Hob;
> > +
> > +  Hob = CreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));
> > +
> > +  Hob->SizeOfMemorySpace = SizeOfMemorySpace;
> > +  Hob->SizeOfIoSpace     = SizeOfIoSpace;
> > +
> > +  //
> > +  // Zero the reserved space to match HOB spec
> > +  //
> > +  ZeroMem (Hob->Reserved, sizeof (Hob->Reserved)); }
> > +
> > +/**
> > +  Builds a HOB for the memory allocation.
> > +
> > +  This function builds a HOB for the memory allocation.
> > +  It can only be invoked during PEI phase;  for DXE phase, it will
> > + ASSERT() since PEI HOB is read-only for DXE phase.
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  BaseAddress   The 64 bit physical address of the memory.
> > +  @param  Length        The length of the memory allocation in bytes.
> > +  @param  MemoryType    Type of memory allocated by this HOB.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildMemoryAllocationHob (
> > +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> > +  IN UINT64                      Length,
> > +  IN EFI_MEMORY_TYPE             MemoryType
> > +  )
> > +{
> > +  EFI_HOB_MEMORY_ALLOCATION  *Hob;
> > +
> > +  ASSERT (((BaseAddress & (EFI_PAGE_SIZE - 1)) == 0) &&
> > +          ((Length & (EFI_PAGE_SIZE - 1)) == 0));
> > +
> > +  Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof
> (EFI_HOB_MEMORY_ALLOCATION));
> > +
> > +  ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));
> > + Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
> > +  Hob->AllocDescriptor.MemoryLength      = Length;
> > +  Hob->AllocDescriptor.MemoryType        = MemoryType;
> > +  //
> > +  // Zero the reserved space to match HOB spec  //  ZeroMem
> > + (Hob->AllocDescriptor.Reserved, sizeof
> (Hob->AllocDescriptor.Reserved));
> > +}
> > +
> > +/**
> > +  Builds a HOB that describes a chunk of system memory with Owner GUID.
> > +
> > +  This function builds a HOB that describes a chunk of system memory.
> > +  It can only be invoked during PEI phase;  for DXE phase, it will
> > + ASSERT() since PEI HOB is read-only for DXE phase.
> > +
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  ResourceType        The type of resource described by this
> HOB.
> > +  @param  ResourceAttribute   The resource attributes of the memory
> described by this HOB.
> > +  @param  PhysicalStart       The 64 bit physical address of memory
> described by this HOB.
> > +  @param  NumberOfBytes       The length of the memory described by
> this HOB in bytes.
> > +  @param  OwnerGUID           GUID for the owner of this resource.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildResourceDescriptorWithOwnerHob (
> > +  IN EFI_RESOURCE_TYPE            ResourceType,
> > +  IN EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttribute,
> > +  IN EFI_PHYSICAL_ADDRESS         PhysicalStart,
> > +  IN UINT64                       NumberOfBytes,
> > +  IN EFI_GUID                     *OwnerGUID
> > +  )
> > +{
> > +  //
> > +  // PEI HOB is read only for DXE phase
> > +  //
> > +  ASSERT (FALSE);
> > +}
> > +
> > +/**
> > +  Builds a Capsule Volume HOB.
> > +
> > +  This function builds a Capsule Volume HOB.
> > +  It can only be invoked during PEI phase;  for DXE phase, it will
> > + ASSERT() because PEI HOB is read-only for DXE phase.
> > +
> > +  If the platform does not support Capsule Volume HOBs, then ASSERT().
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  BaseAddress   The base address of the Capsule Volume.
> > +  @param  Length        The size of the Capsule Volume in bytes.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildCvHob (
> > +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> > +  IN UINT64                      Length
> > +  )
> > +{
> > +  //
> > +  // PEI HOB is read only for DXE phase
> > +  //
> > +  ASSERT (FALSE);
> > +}
> > +
> > +
> > +/**
> > +  Builds a HOB for the BSP store.
> > +
> > +  This function builds a HOB for BSP store.
> > +  It can only be invoked during PEI phase;  for DXE phase, it will
> > + ASSERT() because PEI HOB is read-only for DXE phase.
> > +
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  BaseAddress   The 64 bit physical address of the BSP.
> > +  @param  Length        The length of the BSP store in bytes.
> > +  @param  MemoryType    Type of memory allocated by this HOB.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildBspStoreHob (
> > +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> > +  IN UINT64                      Length,
> > +  IN EFI_MEMORY_TYPE             MemoryType
> > +  )
> > +{
> > +  //
> > +  // PEI HOB is read only for DXE phase
> > +  //
> > +  ASSERT (FALSE);
> > +}
> > +
> > +/**
> > +  Builds a HOB for the Stack.
> > +
> > +  This function builds a HOB for the stack.
> > +  It can only be invoked during PEI phase;  for DXE phase, it will
> > + ASSERT() since PEI HOB is read-only for DXE phase.
> > +  If there is no additional space for HOB creation, then ASSERT().
> > +
> > +  @param  BaseAddress   The 64 bit physical address of the Stack.
> > +  @param  Length        The length of the stack in bytes.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +BuildStackHob (
> > +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> > +  IN UINT64                      Length
> > +  )
> > +{
> > +  //
> > +  // PEI HOB is read only for DXE phase
> > +  //
> > +  ASSERT (FALSE);
> > +}
> > diff --git a/StandaloneMmPkg/Library/HobLib/HobLib.inf
> b/StandaloneMmPkg/Library/HobLib/HobLib.inf
> > new file mode 100644
> > index 0000000000..42273b6d66
> > --- /dev/null
> > +++ b/StandaloneMmPkg/Library/HobLib/HobLib.inf
> > @@ -0,0 +1,45 @@
> > +## @file
> > +# Instance of HOB Library for DXE Core.
> > +#
> > +# HOB Library implementation for the DXE Core. Does not have a constructor.
> > +#  Uses gHobList defined in the DXE Core Entry Point Library.
> > +#
> > +# Copyright (c) 2007 - 2014, Intel Corporation. All rights
> > +reserved.<BR> # Copyright (c) 2016 - 2017, ARM Limited. All rights
> > +reserved.<BR> # #  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.
> > +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +#
> > +#
> > +##
> > +
> > +[Defines]
> > +  INF_VERSION                    = 0x0001001A
> > +  BASE_NAME                      = HobLib
> > +  FILE_GUID                      =
> CF56EF2C-68D8-4BD5-9A8B-8A7BFCFF751C
> > +  MODULE_TYPE                    = MM_CORE_STANDALONE
> > +  VERSION_STRING                 = 1.0
> > +  PI_SPECIFICATION_VERSION       = 0x00010032
> > +  LIBRARY_CLASS                  = HobLib|MM_CORE_STANDALONE
> MM_STANDALONE
> > +
> > +#
> > +#  VALID_ARCHITECTURES           = AARCH64
> > +#
> > +
> > +[Sources.AARCH64]
> > +  Arm/HobLib.c
> > +
> > +[Packages]
> > +  MdePkg/MdePkg.dec
> > +
> > +
> > +[LibraryClasses]
> > +  BaseMemoryLib
> > +  DebugLib
> > +
> > +[Guids]
> > +  gEfiHobListGuid                               ## CONSUMES  ##
> SystemTable
> > --
> > 2.16.2
> >
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 09/18] StandaloneMmPkg/MemoryAllocationLib: Add MM memory allocation library.
  2018-04-26 13:05     ` Yao, Jiewen
@ 2018-05-04 23:23       ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:23 UTC (permalink / raw)
  To: Yao, Jiewen, Achin Gupta; +Cc: edk2-devel@lists.01.org

Jiewen,

It checks for HOB by  GUID gMmCoreDataHobGuid first,
if it's not present, then it checks for HOB by GUID gEfiMmPeiMmramMemoryReserveGuid. (which is the case for Arm and intel won't be affected by this)
So it is as generic as possible and hence I think there is no need for ARM specific and intel specific funtions.
Please review it and let me know if you see issues.

Thanks,
Supreeth

-----Original Message-----
From: Yao, Jiewen <jiewen.yao@intel.com>
Sent: Thursday, April 26, 2018 8:06 AM
To: Achin Gupta <Achin.Gupta@arm.com>; Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: ard.biesheuvel@linaro.org; edk2-devel@lists.01.org; leif.lindholm@linaro.org; Gao, Liming <liming.gao@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>; nd <nd@arm.com>
Subject: RE: [edk2] [PATCH v1 09/18] StandaloneMmPkg/MemoryAllocationLib: Add MM memory allocation library.

Same comment as previous 2.

Maybe to separate ARM specific function from generic function.

Thank you



> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Achin
> Gupta
> Sent: Wednesday, April 25, 2018 7:34 AM
> To: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> Cc: ard.biesheuvel@linaro.org; edk2-devel@lists.01.org;
> leif.lindholm@linaro.org; Yao, Jiewen <jiewen.yao@intel.com>; Gao, Liming
> <liming.gao@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>;
> nd@arm.com
> Subject: Re: [edk2] [PATCH v1 09/18] StandaloneMmPkg/MemoryAllocationLib:
> Add MM memory allocation library.
>
> Hi Supreeth,
>
> On Fri, Apr 06, 2018 at 03:42:14PM +0100, Supreeth Venkatesh wrote:
> > This patch implements management mode memory allocation services.The
> > implementation borrows the MM Core Memory Allocation services as the
> > primitive for memory allocation instead of using MM System Table
> > services.
>
> The commit message did not really register with me. Once the MMRAM ranges
> have
> been conveyed to the MMST memory allocation services (through
> MemoryAllocationLibConstructor()), all functions in MemoryAllocationLib.c use
> the MMST services. The message seems to indicate otherwise.
>
> On Arm, the gEfiMmPeiMmramMemoryReserveGuid HOB is used to convey the
> MMRAM
> ranges. It seems x86 uses gMmCoreDataHobGuid HOB. So it worth getting this
> reviewed by Jiewen.
>
> The copyright years in the files need to be updated.
>
> With that in mind..
>
> Acked-by: Achin Gupta <achin.gupta@arm.com>
>
> >
> > Contributed-under: TianoCore Contribution Agreement 1.1
> > Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> > Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> > ---
> >  StandaloneMmPkg/Include/Guid/MmCoreData.h          | 132 +++
> >  StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h  |  62 ++
> >  .../MemoryAllocationLib/MemoryAllocationLib.c      | 907
> +++++++++++++++++++++
> >  .../MemoryAllocationLib/MemoryAllocationLib.inf    |  49 ++
> >  .../MemoryAllocationLib/MemoryAllocationServices.h |  38 +
> >  5 files changed, 1188 insertions(+)
> >  create mode 100644 StandaloneMmPkg/Include/Guid/MmCoreData.h
> >  create mode 100644
> StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
> >  create mode 100644
> StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
> >  create mode 100644
> StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
> >  create mode 100644
> StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.h
> >
> > diff --git a/StandaloneMmPkg/Include/Guid/MmCoreData.h
> b/StandaloneMmPkg/Include/Guid/MmCoreData.h
> > new file mode 100644
> > index 0000000000..c0ac772014
> > --- /dev/null
> > +++ b/StandaloneMmPkg/Include/Guid/MmCoreData.h
> > @@ -0,0 +1,132 @@
> > +/** @file
> > +  MM Core data.
> > +
> > +Copyright (c) 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 that accompanies this
> distribution.
> > +The full text of the license may be found at
> > +http://opensource.org/licenses/bsd-license.php.
> > +
> > +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +
> > +**/
> > +
> > +#ifndef __MM_CORE_DATA_H__
> > +#define __MM_CORE_DATA_H__
> > +
> > +#define MM_CORE_DATA_HOB_GUID \
> > +  { 0xa160bf99, 0x2aa4, 0x4d7d, { 0x99, 0x93, 0x89, 0x9c, 0xb1, 0x2d, 0xf3,
> 0x76 }}
> > +
> > +extern EFI_GUID gMmCoreDataHobGuid;
> > +
> > +typedef struct {
> > +  //
> > +  // Address pointer to MM_CORE_PRIVATE_DATA
> > +  //
> > +  EFI_PHYSICAL_ADDRESS   Address;
> > +} MM_CORE_DATA_HOB_DATA;
> > +
> > +
> > +///
> > +/// Define values for the communications buffer used when
> gEfiEventDxeDispatchGuid is
> > +/// event signaled.  This event is signaled by the DXE Core each time the DXE
> Core
> > +/// dispatcher has completed its work.  When this event is signaled, the MM
> Core
> > +/// if notified, so the MM Core can dispatch MM drivers.  If
> COMM_BUFFER_MM_DISPATCH_ERROR
> > +/// is returned in the communication buffer, then an error occurred
> dispatching MM
> > +/// Drivers.  If COMM_BUFFER_MM_DISPATCH_SUCCESS is returned, then
> the MM Core
> > +/// dispatched all the drivers it could.  If
> COMM_BUFFER_MM_DISPATCH_RESTART is
> > +/// returned, then the MM Core just dispatched the MM Driver that
> registered
> > +/// the MM Entry Point enabling the use of MM Mode.  In this case, the MM
> Core
> > +/// should be notified again to dispatch more MM Drivers using MM Mode.
> > +///
> > +#define COMM_BUFFER_MM_DISPATCH_ERROR    0x00
> > +#define COMM_BUFFER_MM_DISPATCH_SUCCESS  0x01
> > +#define COMM_BUFFER_MM_DISPATCH_RESTART  0x02
> > +
> > +///
> > +/// Signature for the private structure shared between the MM IPL and the
> MM Core
> > +///
> > +#define MM_CORE_PRIVATE_DATA_SIGNATURE  SIGNATURE_32 ('m', 'm', 'i',
> 'c')
> > +
> > +///
> > +/// Private structure that is used to share information between the MM IPL
> and
> > +/// the MM Core.  This structure is allocated from memory of type
> EfiRuntimeServicesData.
> > +/// Since runtime memory types are converted to available memory when a
> legacy boot
> > +/// is performed, the MM Core must not access any fields of this structure if a
> legacy
> > +/// boot is performed.  As a result, the MM IPL must create an event
> notification
> > +/// for the Legacy Boot event and notify the MM Core that a legacy boot is
> being
> > +/// performed.  The MM Core can then use this information to filter accesses
> to
> > +/// thos structure.
> > +///
> > +typedef struct {
> > +  UINT64                          Signature;
> > +
> > +  ///
> > +  /// The number of MMRAM ranges passed from the MM IPL to the MM
> Core.  The MM
> > +  /// Core uses these ranges of MMRAM to initialize the MM Core memory
> manager.
> > +  ///
> > +  UINT64                          MmramRangeCount;
> > +
> > +  ///
> > +  /// A table of MMRAM ranges passed from the MM IPL to the MM Core.
> The MM
> > +  /// Core uses these ranges of MMRAM to initialize the MM Core memory
> manager.
> > +  ///
> > +  EFI_PHYSICAL_ADDRESS            MmramRanges;
> > +
> > +  ///
> > +  /// The MM Foundation Entry Point.  The MM Core fills in this field when
> the
> > +  /// MM Core is initialized.  The MM IPL is responsbile for registering this
> entry
> > +  /// point with the MM Configuration Protocol.  The MM Configuration
> Protocol may
> > +  /// not be available at the time the MM IPL and MM Core are started, so the
> MM IPL
> > +  /// sets up a protocol notification on the MM Configuration Protocol and
> registers
> > +  /// the MM Foundation Entry Point as soon as the MM Configuration
> Protocol is
> > +  /// available.
> > +  ///
> > +  EFI_PHYSICAL_ADDRESS            MmEntryPoint;
> > +
> > +  ///
> > +  /// Boolean flag set to TRUE while an MMI is being processed by the MM
> Core.
> > +  ///
> > +  BOOLEAN                         MmEntryPointRegistered;
> > +
> > +  ///
> > +  /// Boolean flag set to TRUE while an MMI is being processed by the MM
> Core.
> > +  ///
> > +  BOOLEAN                         InMm;
> > +
> > +  ///
> > +  /// This field is set by the MM Core then the MM Core is initialized.  This
> field is
> > +  /// used by the MM Base 2 Protocol and MM Communication Protocol
> implementations in
> > +  /// the MM IPL.
> > +  ///
> > +  EFI_PHYSICAL_ADDRESS            Mmst;
> > +
> > +  ///
> > +  /// This field is used by the MM Communicatioon Protocol to pass a buffer
> into
> > +  /// a software MMI handler and for the software MMI handler to pass a
> buffer back to
> > +  /// the caller of the MM Communication Protocol.
> > +  ///
> > +  EFI_PHYSICAL_ADDRESS            CommunicationBuffer;
> > +
> > +  ///
> > +  /// This field is used by the MM Communicatioon Protocol to pass the size
> of a buffer,
> > +  /// in bytes, into a software MMI handler and for the software MMI
> handler to pass the
> > +  /// size, in bytes, of a buffer back to the caller of the MM Communication
> Protocol.
> > +  ///
> > +  UINT64                          BufferSize;
> > +
> > +  ///
> > +  /// This field is used by the MM Communication Protocol to pass the return
> status from
> > +  /// a software MMI handler back to the caller of the MM Communication
> Protocol.
> > +  ///
> > +  UINT64                          ReturnStatus;
> > +
> > +  EFI_PHYSICAL_ADDRESS            MmCoreImageBase;
> > +  UINT64                          MmCoreImageSize;
> > +  EFI_PHYSICAL_ADDRESS            MmCoreEntryPoint;
> > +
> > +  EFI_PHYSICAL_ADDRESS            StandaloneBfvAddress;
> > +} MM_CORE_PRIVATE_DATA;
> > +
> > +#endif
> > diff --git a/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
> b/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
> > new file mode 100644
> > index 0000000000..c4104b755d
> > --- /dev/null
> > +++ b/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
> > @@ -0,0 +1,62 @@
> > +/** @file
> > +  Definition of GUIDed HOB for reserving MMRAM regions.
> > +
> > +  This file defines:
> > +  * the GUID used to identify the GUID HOB for reserving MMRAM regions.
> > +  * the data structure of MMRAM descriptor to describe MMRAM candidate
> regions
> > +  * values of state of MMRAM candidate regions
> > +  * the GUID specific data structure of HOB for reserving MMRAM regions.
> > +  This GUIDed HOB can be used to convey the existence of the T-SEG
> reservation and H-SEG usage
> > +
> > +Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
> > +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > +
> > +This program and the accompanying materials are licensed and made
> available under
> > +the terms and conditions of the BSD License that accompanies this
> distribution.
> > +The full text of the license may be found at
> > +http://opensource.org/licenses/bsd-license.php.
> > +
> > +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +
> > +  @par Revision Reference:
> > +  GUIDs defined in MmCis spec version 0.9.
> > +
> > +**/
> > +
> > +#ifndef _EFI_MM_PEI_MMRAM_MEMORY_RESERVE_H_
> > +#define _EFI_MM_PEI_MMRAM_MEMORY_RESERVE_H_
> > +
> > +#define EFI_MM_PEI_MMRAM_MEMORY_RESERVE \
> > +  { \
> > +    0x0703f912, 0xbf8d, 0x4e2a, {0xbe, 0x07, 0xab, 0x27, 0x25, 0x25, 0xc5,
> 0x92 } \
> > +  }
> > +
> > +/**
> > +* GUID specific data structure of HOB for reserving MMRAM regions.
> > +*
> > +* Inconsistent with specification here:
> > +* EFI_HOB_MMRAM_DESCRIPTOR_BLOCK has been changed to
> EFI_MMRAM_HOB_DESCRIPTOR_BLOCK.
> > +* This inconsistency is kept in code in order for backward compatibility.
> > +**/
> > +typedef struct {
> > +  ///
> > +  /// Designates the number of possible regions in the system
> > +  /// that can be usable for MMRAM.
> > +  ///
> > +  /// Inconsistent with specification here:
> > +  /// In Framework MM CIS 0.91 specification, it defines the field type as
> UINTN.
> > +  /// However, HOBs are supposed to be CPU neutral, so UINT32 should be
> used instead.
> > +  ///
> > +  UINT32                NumberOfMmReservedRegions;
> > +  ///
> > +  /// Used throughout this protocol to describe the candidate
> > +  /// regions for MMRAM that are supported by this platform.
> > +  ///
> > +  EFI_MMRAM_DESCRIPTOR  Descriptor[1];
> > +} EFI_MMRAM_HOB_DESCRIPTOR_BLOCK;
> > +
> > +extern EFI_GUID gEfiMmPeiSmramMemoryReserveGuid;
> > +
> > +#endif
> > +
> > diff --git
> a/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
> b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
> > new file mode 100644
> > index 0000000000..c177a8f538
> > --- /dev/null
> > +++
> b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.c
> > @@ -0,0 +1,907 @@
> > +/** @file
> > +  Support routines for memory allocation routines based on Standalone MM
> Core internal functions.
> > +
> > +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > +
> > +  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
> > +
> > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +
> > +**/
> > +
> > +#include <PiMm.h>
> > +
> > +#include <Guid/MmramMemoryReserve.h>
> > +#include <Library/MemoryAllocationLib.h>
> > +#include <Library/BaseMemoryLib.h>
> > +#include <Library/DebugLib.h>
> > +#include <Library/HobLib.h>
> > +#include "MemoryAllocationServices.h"
> > +
> > +EFI_MM_SYSTEM_TABLE   *gMmst = NULL;
> > +
> > +/**
> > +  Allocates one or more 4KB pages of a certain memory type.
> > +
> > +  Allocates the number of 4KB pages of a certain memory type and returns a
> pointer to the allocated
> > +  buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0,
> then NULL is returned.
> > +  If there is not enough memory remaining to satisfy the request, then NULL
> is returned.
> > +
> > +  @param  MemoryType            The type of memory to allocate.
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +InternalAllocatePages (
> > +  IN EFI_MEMORY_TYPE  MemoryType,
> > +  IN UINTN            Pages
> > +  )
> > +{
> > +  EFI_STATUS            Status;
> > +  EFI_PHYSICAL_ADDRESS  Memory;
> > +
> > +  if (Pages == 0) {
> > +    return NULL;
> > +  }
> > +
> > +  Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType,
> Pages, &Memory);
> > +  if (EFI_ERROR (Status)) {
> > +    return NULL;
> > +  }
> > +  return (VOID *) (UINTN) Memory;
> > +}
> > +
> > +/**
> > +  Allocates one or more 4KB pages of type EfiBootServicesData.
> > +
> > +  Allocates the number of 4KB pages of type EfiBootServicesData and returns
> a pointer to the
> > +  allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If
> Pages is 0, then NULL
> > +  is returned.  If there is not enough memory remaining to satisfy the
> request, then NULL is
> > +  returned.
> > +
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocatePages (
> > +  IN UINTN  Pages
> > +  )
> > +{
> > +  return InternalAllocatePages (EfiRuntimeServicesData, Pages);
> > +}
> > +
> > +/**
> > +  Allocates one or more 4KB pages of type EfiRuntimeServicesData.
> > +
> > +  Allocates the number of 4KB pages of type EfiRuntimeServicesData and
> returns a pointer to the
> > +  allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If
> Pages is 0, then NULL
> > +  is returned.  If there is not enough memory remaining to satisfy the
> request, then NULL is
> > +  returned.
> > +
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateRuntimePages (
> > +  IN UINTN  Pages
> > +  )
> > +{
> > +  return InternalAllocatePages (EfiRuntimeServicesData, Pages);
> > +}
> > +
> > +/**
> > +  Allocates one or more 4KB pages of type EfiReservedMemoryType.
> > +
> > +  Allocates the number of 4KB pages of type EfiReservedMemoryType and
> returns a pointer to the
> > +  allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If
> Pages is 0, then NULL
> > +  is returned.  If there is not enough memory remaining to satisfy the
> request, then NULL is
> > +  returned.
> > +
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateReservedPages (
> > +  IN UINTN  Pages
> > +  )
> > +{
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Frees one or more 4KB pages that were previously allocated with one of
> the page allocation
> > +  functions in the Memory Allocation Library.
> > +
> > +  Frees the number of 4KB pages specified by Pages from the buffer specified
> by Buffer.  Buffer
> > +  must have been allocated on a previous call to the page allocation services
> of the Memory
> > +  Allocation Library.  If it is not possible to free allocated pages, then this
> function will
> > +  perform no actions.
> > +
> > +  If Buffer was not allocated with a page allocation function in the Memory
> Allocation Library,
> > +  then ASSERT().
> > +  If Pages is zero, then ASSERT().
> > +
> > +  @param  Buffer                Pointer to the buffer of pages to free.
> > +  @param  Pages                 The number of 4 KB pages to free.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +FreePages (
> > +  IN VOID   *Buffer,
> > +  IN UINTN  Pages
> > +  )
> > +{
> > +  EFI_STATUS  Status;
> > +
> > +  ASSERT (Pages != 0);
> > +  Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer,
> Pages);
> > +  ASSERT_EFI_ERROR (Status);
> > +}
> > +
> > +/**
> > +  Allocates one or more 4KB pages of a certain memory type at a specified
> alignment.
> > +
> > +  Allocates the number of 4KB pages specified by Pages of a certain memory
> type with an alignment
> > +  specified by Alignment.  The allocated buffer is returned.  If Pages is 0,
> then NULL is returned.
> > +  If there is not enough memory at the specified alignment remaining to
> satisfy the request, then
> > +  NULL is returned.
> > +  If Alignment is not a power of two and Alignment is not zero, then
> ASSERT().
> > +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> > +
> > +  @param  MemoryType            The type of memory to allocate.
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +  @param  Alignment             The requested alignment of the
> allocation.  Must be a power of two.
> > +                                If Alignment is zero, then byte
> alignment is used.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +InternalAllocateAlignedPages (
> > +  IN EFI_MEMORY_TYPE  MemoryType,
> > +  IN UINTN            Pages,
> > +  IN UINTN            Alignment
> > +  )
> > +{
> > +  EFI_STATUS            Status;
> > +  EFI_PHYSICAL_ADDRESS  Memory;
> > +  UINTN                 AlignedMemory;
> > +  UINTN                 AlignmentMask;
> > +  UINTN                 UnalignedPages;
> > +  UINTN                 RealPages;
> > +
> > +  //
> > +  // Alignment must be a power of two or zero.
> > +  //
> > +  ASSERT ((Alignment & (Alignment - 1)) == 0);
> > +
> > +  if (Pages == 0) {
> > +    return NULL;
> > +  }
> > +  if (Alignment > EFI_PAGE_SIZE) {
> > +    //
> > +    // Calculate the total number of pages since alignment is larger than page
> size.
> > +    //
> > +    AlignmentMask  = Alignment - 1;
> > +    RealPages      = Pages + EFI_SIZE_TO_PAGES (Alignment);
> > +    //
> > +    // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not
> overflow.
> > +    //
> > +    ASSERT (RealPages > Pages);
> > +
> > +    Status         = gMmst->MmAllocatePages (AllocateAnyPages,
> MemoryType, RealPages, &Memory);
> > +    if (EFI_ERROR (Status)) {
> > +      return NULL;
> > +    }
> > +    AlignedMemory  = ((UINTN) Memory + AlignmentMask) &
> ~AlignmentMask;
> > +    UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)
> Memory);
> > +    if (UnalignedPages > 0) {
> > +      //
> > +      // Free first unaligned page(s).
> > +      //
> > +      Status = gMmst->MmFreePages (Memory, UnalignedPages);
> > +      ASSERT_EFI_ERROR (Status);
> > +    }
> > +    Memory         = (EFI_PHYSICAL_ADDRESS) (AlignedMemory +
> EFI_PAGES_TO_SIZE (Pages));
> > +    UnalignedPages = RealPages - Pages - UnalignedPages;
> > +    if (UnalignedPages > 0) {
> > +      //
> > +      // Free last unaligned page(s).
> > +      //
> > +      Status = gMmst->MmFreePages (Memory, UnalignedPages);
> > +      ASSERT_EFI_ERROR (Status);
> > +    }
> > +  } else {
> > +    //
> > +    // Do not over-allocate pages in this case.
> > +    //
> > +    Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType,
> Pages, &Memory);
> > +    if (EFI_ERROR (Status)) {
> > +      return NULL;
> > +    }
> > +    AlignedMemory  = (UINTN) Memory;
> > +  }
> > +  return (VOID *) AlignedMemory;
> > +}
> > +
> > +/**
> > +  Allocates one or more 4KB pages of type EfiBootServicesData at a specified
> alignment.
> > +
> > +  Allocates the number of 4KB pages specified by Pages of type
> EfiBootServicesData with an
> > +  alignment specified by Alignment.  The allocated buffer is returned.  If
> Pages is 0, then NULL is
> > +  returned.  If there is not enough memory at the specified alignment
> remaining to satisfy the
> > +  request, then NULL is returned.
> > +
> > +  If Alignment is not a power of two and Alignment is not zero, then
> ASSERT().
> > +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> > +
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +  @param  Alignment             The requested alignment of the
> allocation.  Must be a power of two.
> > +                                If Alignment is zero, then byte
> alignment is used.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateAlignedPages (
> > +  IN UINTN  Pages,
> > +  IN UINTN  Alignment
> > +  )
> > +{
> > +  return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages,
> Alignment);
> > +}
> > +
> > +/**
> > +  Allocates one or more 4KB pages of type EfiRuntimeServicesData at a
> specified alignment.
> > +
> > +  Allocates the number of 4KB pages specified by Pages of type
> EfiRuntimeServicesData with an
> > +  alignment specified by Alignment.  The allocated buffer is returned.  If
> Pages is 0, then NULL is
> > +  returned.  If there is not enough memory at the specified alignment
> remaining to satisfy the
> > +  request, then NULL is returned.
> > +
> > +  If Alignment is not a power of two and Alignment is not zero, then
> ASSERT().
> > +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> > +
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +  @param  Alignment             The requested alignment of the
> allocation.  Must be a power of two.
> > +                                If Alignment is zero, then byte
> alignment is used.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateAlignedRuntimePages (
> > +  IN UINTN  Pages,
> > +  IN UINTN  Alignment
> > +  )
> > +{
> > +  return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages,
> Alignment);
> > +}
> > +
> > +/**
> > +  Allocates one or more 4KB pages of type EfiReservedMemoryType at a
> specified alignment.
> > +
> > +  Allocates the number of 4KB pages specified by Pages of type
> EfiReservedMemoryType with an
> > +  alignment specified by Alignment.  The allocated buffer is returned.  If
> Pages is 0, then NULL is
> > +  returned.  If there is not enough memory at the specified alignment
> remaining to satisfy the
> > +  request, then NULL is returned.
> > +
> > +  If Alignment is not a power of two and Alignment is not zero, then
> ASSERT().
> > +  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
> > +
> > +  @param  Pages                 The number of 4 KB pages to allocate.
> > +  @param  Alignment             The requested alignment of the
> allocation.  Must be a power of two.
> > +                                If Alignment is zero, then byte
> alignment is used.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateAlignedReservedPages (
> > +  IN UINTN  Pages,
> > +  IN UINTN  Alignment
> > +  )
> > +{
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Frees one or more 4KB pages that were previously allocated with one of
> the aligned page
> > +  allocation functions in the Memory Allocation Library.
> > +
> > +  Frees the number of 4KB pages specified by Pages from the buffer specified
> by Buffer.  Buffer
> > +  must have been allocated on a previous call to the aligned page allocation
> services of the Memory
> > +  Allocation Library.  If it is not possible to free allocated pages, then this
> function will
> > +  perform no actions.
> > +
> > +  If Buffer was not allocated with an aligned page allocation function in the
> Memory Allocation
> > +  Library, then ASSERT().
> > +  If Pages is zero, then ASSERT().
> > +
> > +  @param  Buffer                Pointer to the buffer of pages to free.
> > +  @param  Pages                 The number of 4 KB pages to free.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +FreeAlignedPages (
> > +  IN VOID   *Buffer,
> > +  IN UINTN  Pages
> > +  )
> > +{
> > +  EFI_STATUS  Status;
> > +
> > +  ASSERT (Pages != 0);
> > +  Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer,
> Pages);
> > +  ASSERT_EFI_ERROR (Status);
> > +}
> > +
> > +/**
> > +  Allocates a buffer of a certain pool type.
> > +
> > +  Allocates the number bytes specified by AllocationSize of a certain pool
> type and returns a
> > +  pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of
> 0 size is
> > +  returned.  If there is not enough memory remaining to satisfy the request,
> then NULL is returned.
> > +
> > +  @param  MemoryType            The type of memory to allocate.
> > +  @param  AllocationSize        The number of bytes to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +InternalAllocatePool (
> > +  IN EFI_MEMORY_TYPE  MemoryType,
> > +  IN UINTN            AllocationSize
> > +  )
> > +{
> > +  EFI_STATUS  Status;
> > +  VOID        *Memory;
> > +
> > +  Memory = NULL;
> > +
> > +  Status = gMmst->MmAllocatePool (MemoryType, AllocationSize,
> &Memory);
> > +  if (EFI_ERROR (Status)) {
> > +    Memory = NULL;
> > +  }
> > +  return Memory;
> > +}
> > +
> > +/**
> > +  Allocates a buffer of type EfiBootServicesData.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiBootServicesData and returns a
> > +  pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of
> 0 size is
> > +  returned.  If there is not enough memory remaining to satisfy the request,
> then NULL is returned.
> > +
> > +  @param  AllocationSize        The number of bytes to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocatePool (
> > +  IN UINTN  AllocationSize
> > +  )
> > +{
> > +  return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);
> > +}
> > +
> > +/**
> > +  Allocates a buffer of type EfiRuntimeServicesData.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiRuntimeServicesData and returns
> > +  a pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer
> of 0 size is
> > +  returned.  If there is not enough memory remaining to satisfy the request,
> then NULL is returned.
> > +
> > +  @param  AllocationSize        The number of bytes to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateRuntimePool (
> > +  IN UINTN  AllocationSize
> > +  )
> > +{
> > +  return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);
> > +}
> > +
> > +/**
> > +  Allocates a buffer of type EfiReservedMemoryType.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiReservedMemoryType and returns
> > +  a pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer
> of 0 size is
> > +  returned.  If there is not enough memory remaining to satisfy the request,
> then NULL is returned.
> > +
> > +  @param  AllocationSize        The number of bytes to allocate.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateReservedPool (
> > +  IN UINTN  AllocationSize
> > +  )
> > +{
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Allocates and zeros a buffer of a certain pool type.
> > +
> > +  Allocates the number bytes specified by AllocationSize of a certain pool
> type, clears the buffer
> > +  with zeros, and returns a pointer to the allocated buffer.  If AllocationSize
> is 0, then a valid
> > +  buffer of 0 size is returned.  If there is not enough memory remaining to
> satisfy the request,
> > +  then NULL is returned.
> > +
> > +  @param  PoolType              The type of memory to allocate.
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +InternalAllocateZeroPool (
> > +  IN EFI_MEMORY_TYPE  PoolType,
> > +  IN UINTN            AllocationSize
> > +  )
> > +{
> > +  VOID  *Memory;
> > +
> > +  Memory = InternalAllocatePool (PoolType, AllocationSize);
> > +  if (Memory != NULL) {
> > +    Memory = ZeroMem (Memory, AllocationSize);
> > +  }
> > +  return Memory;
> > +}
> > +
> > +/**
> > +  Allocates and zeros a buffer of type EfiBootServicesData.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiBootServicesData, clears the
> > +  buffer with zeros, and returns a pointer to the allocated buffer.  If
> AllocationSize is 0, then a
> > +  valid buffer of 0 size is returned.  If there is not enough memory
> remaining to satisfy the
> > +  request, then NULL is returned.
> > +
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateZeroPool (
> > +  IN UINTN  AllocationSize
> > +  )
> > +{
> > +  return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);
> > +}
> > +
> > +/**
> > +  Allocates and zeros a buffer of type EfiRuntimeServicesData.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiRuntimeServicesData, clears the
> > +  buffer with zeros, and returns a pointer to the allocated buffer.  If
> AllocationSize is 0, then a
> > +  valid buffer of 0 size is returned.  If there is not enough memory
> remaining to satisfy the
> > +  request, then NULL is returned.
> > +
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateRuntimeZeroPool (
> > +  IN UINTN  AllocationSize
> > +  )
> > +{
> > +  return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);
> > +}
> > +
> > +/**
> > +  Allocates and zeros a buffer of type EfiReservedMemoryType.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiReservedMemoryType, clears the
> > +  buffer with zeros, and returns a pointer to the allocated buffer.  If
> AllocationSize is 0, then a
> > +  valid buffer of 0 size is returned.  If there is not enough memory
> remaining to satisfy the
> > +  request, then NULL is returned.
> > +
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateReservedZeroPool (
> > +  IN UINTN  AllocationSize
> > +  )
> > +{
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Copies a buffer to an allocated buffer of a certain pool type.
> > +
> > +  Allocates the number bytes specified by AllocationSize of a certain pool
> type, copies
> > +  AllocationSize bytes from Buffer to the newly allocated buffer, and returns
> a pointer to the
> > +  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
> returned.  If there
> > +  is not enough memory remaining to satisfy the request, then NULL is
> returned.
> > +  If Buffer is NULL, then ASSERT().
> > +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then
> ASSERT().
> > +
> > +  @param  PoolType              The type of pool to allocate.
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +  @param  Buffer                The buffer to copy to the allocated
> buffer.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +InternalAllocateCopyPool (
> > +  IN EFI_MEMORY_TYPE  PoolType,
> > +  IN UINTN            AllocationSize,
> > +  IN CONST VOID       *Buffer
> > +  )
> > +{
> > +  VOID  *Memory;
> > +
> > +  ASSERT (Buffer != NULL);
> > +  ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));
> > +
> > +  Memory = InternalAllocatePool (PoolType, AllocationSize);
> > +  if (Memory != NULL) {
> > +     Memory = CopyMem (Memory, Buffer, AllocationSize);
> > +  }
> > +  return Memory;
> > +}
> > +
> > +/**
> > +  Copies a buffer to an allocated buffer of type EfiBootServicesData.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiBootServicesData, copies
> > +  AllocationSize bytes from Buffer to the newly allocated buffer, and returns
> a pointer to the
> > +  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
> returned.  If there
> > +  is not enough memory remaining to satisfy the request, then NULL is
> returned.
> > +
> > +  If Buffer is NULL, then ASSERT().
> > +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then
> ASSERT().
> > +
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +  @param  Buffer                The buffer to copy to the allocated
> buffer.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateCopyPool (
> > +  IN UINTN       AllocationSize,
> > +  IN CONST VOID  *Buffer
> > +  )
> > +{
> > +  return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize,
> Buffer);
> > +}
> > +
> > +/**
> > +  Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiRuntimeServicesData, copies
> > +  AllocationSize bytes from Buffer to the newly allocated buffer, and returns
> a pointer to the
> > +  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
> returned.  If there
> > +  is not enough memory remaining to satisfy the request, then NULL is
> returned.
> > +
> > +  If Buffer is NULL, then ASSERT().
> > +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then
> ASSERT().
> > +
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +  @param  Buffer                The buffer to copy to the allocated
> buffer.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateRuntimeCopyPool (
> > +  IN UINTN       AllocationSize,
> > +  IN CONST VOID  *Buffer
> > +  )
> > +{
> > +  return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize,
> Buffer);
> > +}
> > +
> > +/**
> > +  Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
> > +
> > +  Allocates the number bytes specified by AllocationSize of type
> EfiReservedMemoryType, copies
> > +  AllocationSize bytes from Buffer to the newly allocated buffer, and returns
> a pointer to the
> > +  allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is
> returned.  If there
> > +  is not enough memory remaining to satisfy the request, then NULL is
> returned.
> > +
> > +  If Buffer is NULL, then ASSERT().
> > +  If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then
> ASSERT().
> > +
> > +  @param  AllocationSize        The number of bytes to allocate and
> zero.
> > +  @param  Buffer                The buffer to copy to the allocated
> buffer.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +AllocateReservedCopyPool (
> > +  IN UINTN       AllocationSize,
> > +  IN CONST VOID  *Buffer
> > +  )
> > +{
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Reallocates a buffer of a specified memory type.
> > +
> > +  Allocates and zeros the number bytes specified by NewSize from memory
> of the type
> > +  specified by PoolType.  If OldBuffer is not NULL, then the smaller of
> OldSize and
> > +  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
> > +  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> > +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
> > +  enough memory remaining to satisfy the request, then NULL is returned.
> > +
> > +  If the allocation of the new buffer is successful and the smaller of NewSize
> and OldSize
> > +  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> > +
> > +  @param  PoolType       The type of pool to allocate.
> > +  @param  OldSize        The size, in bytes, of OldBuffer.
> > +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> > +  @param  OldBuffer      The buffer to copy to the allocated buffer.
> This is an optional
> > +                         parameter that may be NULL.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +InternalReallocatePool (
> > +  IN EFI_MEMORY_TYPE  PoolType,
> > +  IN UINTN            OldSize,
> > +  IN UINTN            NewSize,
> > +  IN VOID             *OldBuffer  OPTIONAL
> > +  )
> > +{
> > +  VOID  *NewBuffer;
> > +
> > +  NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);
> > +  if (NewBuffer != NULL && OldBuffer != NULL) {
> > +    CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));
> > +    FreePool (OldBuffer);
> > +  }
> > +  return NewBuffer;
> > +}
> > +
> > +/**
> > +  Reallocates a buffer of type EfiBootServicesData.
> > +
> > +  Allocates and zeros the number bytes specified by NewSize from memory
> of type
> > +  EfiBootServicesData.  If OldBuffer is not NULL, then the smaller of OldSize
> and
> > +  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
> > +  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> > +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
> > +  enough memory remaining to satisfy the request, then NULL is returned.
> > +
> > +  If the allocation of the new buffer is successful and the smaller of NewSize
> and OldSize
> > +  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> > +
> > +  @param  OldSize        The size, in bytes, of OldBuffer.
> > +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> > +  @param  OldBuffer      The buffer to copy to the allocated buffer.
> This is an optional
> > +                         parameter that may be NULL.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +ReallocatePool (
> > +  IN UINTN  OldSize,
> > +  IN UINTN  NewSize,
> > +  IN VOID   *OldBuffer  OPTIONAL
> > +  )
> > +{
> > +  return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize,
> OldBuffer);
> > +}
> > +
> > +/**
> > +  Reallocates a buffer of type EfiRuntimeServicesData.
> > +
> > +  Allocates and zeros the number bytes specified by NewSize from memory
> of type
> > +  EfiRuntimeServicesData.  If OldBuffer is not NULL, then the smaller of
> OldSize and
> > +  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
> > +  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> > +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
> > +  enough memory remaining to satisfy the request, then NULL is returned.
> > +
> > +  If the allocation of the new buffer is successful and the smaller of NewSize
> and OldSize
> > +  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> > +
> > +  @param  OldSize        The size, in bytes, of OldBuffer.
> > +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> > +  @param  OldBuffer      The buffer to copy to the allocated buffer.
> This is an optional
> > +                         parameter that may be NULL.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +ReallocateRuntimePool (
> > +  IN UINTN  OldSize,
> > +  IN UINTN  NewSize,
> > +  IN VOID   *OldBuffer  OPTIONAL
> > +  )
> > +{
> > +  return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize,
> OldBuffer);
> > +}
> > +
> > +/**
> > +  Reallocates a buffer of type EfiReservedMemoryType.
> > +
> > +  Allocates and zeros the number bytes specified by NewSize from memory
> of type
> > +  EfiReservedMemoryType.  If OldBuffer is not NULL, then the smaller of
> OldSize and
> > +  NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
> > +  OldBuffer is freed.  A pointer to the newly allocated buffer is returned.
> > +  If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not
> > +  enough memory remaining to satisfy the request, then NULL is returned.
> > +
> > +  If the allocation of the new buffer is successful and the smaller of NewSize
> and OldSize
> > +  is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
> > +
> > +  @param  OldSize        The size, in bytes, of OldBuffer.
> > +  @param  NewSize        The size, in bytes, of the buffer to reallocate.
> > +  @param  OldBuffer      The buffer to copy to the allocated buffer.
> This is an optional
> > +                         parameter that may be NULL.
> > +
> > +  @return A pointer to the allocated buffer or NULL if allocation fails.
> > +
> > +**/
> > +VOID *
> > +EFIAPI
> > +ReallocateReservedPool (
> > +  IN UINTN  OldSize,
> > +  IN UINTN  NewSize,
> > +  IN VOID   *OldBuffer  OPTIONAL
> > +  )
> > +{
> > +  return NULL;
> > +}
> > +
> > +/**
> > +  Frees a buffer that was previously allocated with one of the pool allocation
> functions in the
> > +  Memory Allocation Library.
> > +
> > +  Frees the buffer specified by Buffer.  Buffer must have been allocated on a
> previous call to the
> > +  pool allocation services of the Memory Allocation Library.  If it is not
> possible to free pool
> > +  resources, then this function will perform no actions.
> > +
> > +  If Buffer was not allocated with a pool allocation function in the Memory
> Allocation Library,
> > +  then ASSERT().
> > +
> > +  @param  Buffer                Pointer to the buffer to free.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +FreePool (
> > +  IN VOID   *Buffer
> > +  )
> > +{
> > +  EFI_STATUS    Status;
> > +
> > +  Status = gMmst->MmFreePool (Buffer);
> > +  ASSERT_EFI_ERROR (Status);
> > +}
> > +
> > +/**
> > +  The constructor function calls MmInitializeMemoryServices to initialize
> > +  memory in MMRAM and caches EFI_MM_SYSTEM_TABLE pointer.
> > +
> > +  @param  ImageHandle   The firmware allocated handle for the EFI
> image.
> > +  @param  SystemTable   A pointer to the Management mode System
> Table.
> > +
> > +  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +MemoryAllocationLibConstructor (
> > +  IN EFI_HANDLE             ImageHandle,
> > +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> > +  )
> > +{
> > +  MM_CORE_PRIVATE_DATA           *MmCorePrivate;
> > +  EFI_HOB_GUID_TYPE               *GuidHob;
> > +  MM_CORE_DATA_HOB_DATA          *DataInHob;
> > +  VOID                            *HobStart;
> > +  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
> > +  EFI_MMRAM_DESCRIPTOR            *MmramRanges;
> > +  UINT32                           MmramRangeCount;
> > +  EFI_HOB_GUID_TYPE               *MmramRangesHob;
> > +
> > +  HobStart = GetHobList ();
> > +  DEBUG ((DEBUG_INFO,
> "StandaloneMmCoreMemoryAllocationLibConstructor - 0x%x\n", HobStart));
> > +
> > +  //
> > +  // Extract MM Core Private context from the Hob. If absent search for
> > +  // a Hob containing the MMRAM ranges
> > +  //
> > +  GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);
> > +  if (GuidHob == NULL) {
> > +    MmramRangesHob = GetNextGuidHob
> (&gEfiMmPeiMmramMemoryReserveGuid, HobStart);
> > +    if (MmramRangesHob == NULL) {
> > +      return EFI_UNSUPPORTED;
> > +    }
> > +
> > +    MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
> > +    if (MmramRangesHobData == NULL) {
> > +      return EFI_UNSUPPORTED;
> > +    }
> > +
> > +    MmramRanges = MmramRangesHobData->Descriptor;
> > +    if (MmramRanges == NULL) {
> > +      return EFI_UNSUPPORTED;
> > +    }
> > +
> > +    MmramRangeCount =
> MmramRangesHobData->NumberOfMmReservedRegions;
> > +    if (MmramRanges == NULL) {
> > +      return EFI_UNSUPPORTED;
> > +    }
> > +
> > +  } else {
> > +    DataInHob      = GET_GUID_HOB_DATA (GuidHob);
> > +    MmCorePrivate = (MM_CORE_PRIVATE_DATA
> *)(UINTN)DataInHob->Address;
> > +    MmramRanges     = (EFI_MMRAM_DESCRIPTOR
> *)(UINTN)MmCorePrivate->MmramRanges;
> > +    MmramRangeCount = MmCorePrivate->MmramRangeCount;
> > +  }
> > +
> > +  {
> > +    UINTN                Index;
> > +
> > +    DEBUG ((DEBUG_INFO, "MmramRangeCount - 0x%x\n",
> MmramRangeCount));
> > +    for (Index = 0; Index < MmramRangeCount; Index++) {
> > +      DEBUG ((DEBUG_INFO, "MmramRanges[%d]: 0x%016lx -
> 0x%016lx\n", Index, MmramRanges[Index].CpuStart,
> MmramRanges[Index].PhysicalSize));
> > +    }
> > +  }
> > +
> > +  //
> > +  // Initialize memory service using free MMRAM
> > +  //
> > +  DEBUG ((DEBUG_INFO, "MmInitializeMemoryServices\n"));
> > +  MmInitializeMemoryServices ((UINTN)MmramRangeCount, (VOID
> *)(UINTN)MmramRanges);
> > +
> > +  // Initialize MM Services Table
> > +  gMmst = MmSystemTable;
> > +  return EFI_SUCCESS;
> > +}
> > diff --git
> a/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
> b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
> > new file mode 100644
> > index 0000000000..068607f90e
> > --- /dev/null
> > +++
> b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationLib.inf
> > @@ -0,0 +1,49 @@
> > +## @file
> > +# Memory Allocation Library instance dedicated to MM Core.
> > +# The implementation borrows the MM Core Memory Allocation services as
> the primitive
> > +# for memory allocation instead of using MM System Table servces in an
> indirect way.
> > +# It is assumed that this library instance must be linked with MM Core in this
> package.
> > +#
> > +# Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
> > +# Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > +#
> > +#  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
> > +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +#
> > +##
> > +
> > +[Defines]
> > +  INF_VERSION                    = 0x0001001A
> > +  BASE_NAME                      = MemoryAllocationLib
> > +  FILE_GUID                      =
> DCDCBE1D-E760-4E1D-85B4-96E3F0439C41
> > +  MODULE_TYPE                    = MM_CORE_STANDALONE
> > +  VERSION_STRING                 = 1.0
> > +  PI_SPECIFICATION_VERSION       = 0x00010032
> > +  LIBRARY_CLASS                  =
> MemoryAllocationLib|MM_CORE_STANDALONE
> > +  CONSTRUCTOR                    = MemoryAllocationLibConstructor
> > +
> > +#
> > +# The following information is for reference only and not required by the
> build tools.
> > +#
> > +#  VALID_ARCHITECTURES           = IA32 X64
> > +#
> > +
> > +[Sources]
> > +  MemoryAllocationLib.c
> > +  MemoryAllocationServices.h
> > +
> > +[Packages]
> > +  MdePkg/MdePkg.dec
> > +  StandaloneMmPkg/StandaloneMmPkg.dec
> > +
> > +[LibraryClasses]
> > +  BaseMemoryLib
> > +  DebugLib
> > +  HobLib
> > +
> > +[Guids]
> > +  gEfiMmPeiMmramMemoryReserveGuid
> > diff --git
> a/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.
> h
> b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.
> h
> > new file mode 100644
> > index 0000000000..eb4f4c3984
> > --- /dev/null
> > +++
> b/StandaloneMmPkg/Library/MemoryAllocationLib/MemoryAllocationServices.
> h
> > @@ -0,0 +1,38 @@
> > +/** @file
> > +  Contains function prototypes for Memory Services in the MM Core.
> > +
> > +  This header file borrows the StandaloneMmCore Memory Allocation
> services as the primitive
> > +  for memory allocation.
> > +
> > +  Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.<BR>
> > +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> > +
> > +  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
> > +
> > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> > +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> > +
> > +**/
> > +
> > +#ifndef _PI_MM_CORE_MEMORY_ALLOCATION_SERVICES_H_
> > +#define _PI_MM_CORE_MEMORY_ALLOCATION_SERVICES_H_
> > +
> > +#include <Guid/MmCoreData.h>
> > +
> > +/**
> > +  Called to initialize the memory service.
> > +
> > +  @param   MmramRangeCount       Number of MMRAM Regions
> > +  @param   MmramRanges           Pointer to MMRAM Descriptors
> > +
> > +**/
> > +VOID
> > +MmInitializeMemoryServices (
> > +  IN UINTN                 MmramRangeCount,
> > +  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
> > +  );
> > +
> > +#endif
> > --
> > 2.16.2
> >
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 12/18] StandaloneMmPkg/CpuMm: Add CPU driver suitable for ARM Platforms.
  2018-04-30 15:50   ` Achin Gupta
@ 2018-05-04 23:24     ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:24 UTC (permalink / raw)
  To: Achin Gupta; +Cc: edk2-devel@lists.01.org

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Monday, April 30, 2018 10:51 AM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 12/18] StandaloneMmPkg/CpuMm: Add CPU driver suitable for ARM Platforms.

Hi Supreeth,

Usual comment about copyright years and invalid comments. I also noticed some TODOs and have provided comments for them. Please see inline
[Supreeth] Ok.

On Fri, Apr 06, 2018 at 03:42:17PM +0100, Supreeth Venkatesh wrote:
> This patch adds a simple CPU driver that exports the
> EFI_MM_CONFIGURATION_PROTOCOL to allow registration of the Standalone
> MM Foundation entry point. It preserves the existing notification
> mechanism for the configuration protocol.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S          |  33 +++

This file is not used.
[Supreeth] Ok.

>  StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c    | 231 +++++++++++++++++++++
>  StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c           | 229 ++++++++++++++++++++
>  .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h        |  89 ++++++++
>  .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf      |  60 ++++++
>  StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c      |  51 +++++
>  StandaloneMmPkg/Include/Guid/MpInformation.h       |  41 ++++
>  7 files changed, 734 insertions(+)
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
>  create mode 100644
> StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
>  create mode 100644
> StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
>  create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
>  create mode 100644 StandaloneMmPkg/Include/Guid/MpInformation.h
>
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
> b/StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
> new file mode 100644
> index 0000000000..0b6e1c330d
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
> @@ -0,0 +1,33 @@
> +//
> +//  Copyright (c) 2017, ARM Limited. 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
> +//
> +//  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, //  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +//
> +//
> +
> +#include <Base.h>
> +#include <AutoGen.h>
> +#include <IndustryStandard/ArmStdSmc.h>
> +
> +.text
> +.align 3
> +
> +GCC_ASM_IMPORT(PiMmStandloneArmTfCpuDriverEntry)
> +GCC_ASM_EXPORT(_PiMmStandloneArmTfCpuDriverEntry)
> +
> +// Stub entry point to ensure that the stacks are completely unwound
> +before // signalling completion of event handling
> +ASM_PFX(_PiMmStandloneArmTfCpuDriverEntry):
> +  bl    PiMmStandloneArmTfCpuDriverEntry
> +  mov   x1, x0
> +  mov   x0, #(ARM_SMC_ID_MM_EVENT_COMPLETE_AARCH64 & 0xffff)
> +  movk  x0, #((ARM_SMC_ID_MM_EVENT_COMPLETE_AARCH64 >> 16) & 0xffff), lsl #16
> +  svc   #0
> +LoopForever:
> +  b     LoopForever
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
> b/StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
> new file mode 100644
> index 0000000000..7b19f53ecb
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
> @@ -0,0 +1,231 @@
> +/** @file
> +
> +  Copyright (c) 2016 HP Development Company, L.P.
> +  Copyright (c) 2016 - 2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Base.h>
> +#include <Pi/PiMmCis.h>
> +
> +
> +#include <Library/ArmSvcLib.h>
> +#include <Library/ArmLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/HobLib.h>
> +
> +#include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT
> +
> +#include <Guid/ZeroGuid.h>
> +#include <Guid/MmramMemoryReserve.h>
> +
> +#include <IndustryStandard/ArmStdSmc.h>
> +
> +#include "PiMmStandloneArmTfCpuDriver.h"
> +
> +EFI_STATUS
> +EFIAPI
> +MmFoundationEntryRegister(
> +  IN CONST EFI_MM_CONFIGURATION_PROTOCOL  *This,
> +  IN EFI_MM_ENTRY_POINT                    MmEntryPoint
> +  );
> +
> +//
> +// On ARM platforms every event is expected to have a GUID associated
> +with // it. It will be used by the MM Entry point to find the handler
> +for the // event. It will either be populated in a
> +EFI_MM_COMMUNICATE_HEADER by the // caller of the event (e.g.
> +MM_COMMUNICATE SMC) or by the CPU driver // (e.g. during an
> +asynchronous event). In either case, this context is // maintained in
> +an array which has an entry for each CPU. The pointer to this //
> +array is held in PerCpuGuidedEventContext. Memory is allocated once
> +the // number of CPUs in the system are made known through the // MP_INFORMATION_HOB_DATA.
> +//
> +EFI_MM_COMMUNICATE_HEADER **PerCpuGuidedEventContext = NULL;
> +
> +//
> +// When an event is received by the CPU driver, it could correspond
> +to a unique // GUID (e.g. interrupt events) or to multiple GUIDs
> +(e.g. MM_COMMUNICATE // SMC). A table is used by the CPU driver to
> +find the GUID corresponding to the // event id in case there is a 1:1
> +mapping between the two. If an event id has // multiple GUIDs
> +associated with it then such an entry will not be found in // this table.
> +//
> +// TODO: Currently NULL since there are no asynchronous events static
> +EFI_GUID *EventIdToGuidLookupTable = NULL;

This data structure is not being used at the moment since only MM_COMMUNICATE SMCs are delegated to the partition. Lets remove it.
[Supreeth] Ok.

> +
> +// Descriptor with whereabouts of memory used for communication with
> +the normal world EFI_MMRAM_DESCRIPTOR  mNsCommBuffer;
> +
> +MP_INFORMATION_HOB_DATA *mMpInformationHobData;
> +
> +EFI_MM_CONFIGURATION_PROTOCOL mMmConfig = {
> +  0,
> +  MmFoundationEntryRegister
> +};
> +
> +static EFI_MM_ENTRY_POINT     mMmEntryPoint = NULL;
> +
> +EFI_STATUS
> +PiMmStandloneArmTfCpuDriverEntry (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  )
> +{
> +  EFI_MM_COMMUNICATE_HEADER *GuidedEventContext = NULL;
> +  EFI_MM_ENTRY_CONTEXT        MmEntryPointContext = {0};
> +  EFI_STATUS                  Status;
> +  UINTN                       NsCommBufferSize;
> +
> +  DEBUG ((DEBUG_INFO, "Received event - 0x%x on cpu %d\n", EventId,
> + CpuNumber));
> +
> +  //
> +  // ARM TF passes SMC FID of the MM_COMMUNICATE interface as the
> + Event ID upon  // receipt of a synchronous MM request. Use the Event
> + ID to distinguish  // between synchronous and asynchronous events.
> +  //
> +  if (ARM_SMC_ID_MM_COMMUNICATE_AARCH64 != EventId) {

Lets change the if condition to return an error status if the EventID is not ARM_SMC_ID_MM_COMMUNICATE_AARCH64.
[Supreeth] Ok.

> +    // Found a GUID, allocate memory to populate a communication buffer
> +    // with the GUID in it
> +    Status = mMmst->MmAllocatePool(EfiRuntimeServicesData, sizeof(EFI_MM_COMMUNICATE_HEADER), (VOID **) &GuidedEventContext);
> +    if (Status != EFI_SUCCESS) {
> +      DEBUG ((DEBUG_INFO, "Mem alloc failed - 0x%x\n", EventId));
> +      return Status;
> +    }
> +
> +    // Copy the GUID
> +    CopyGuid(&GuidedEventContext->HeaderGuid,
> + &EventIdToGuidLookupTable[EventId]);
> +
> +    // Message Length is 0 'cause of the assumption mentioned above
> +    GuidedEventContext->MessageLength = 0;  } else {
> +    // TODO: Perform parameter validation of NsCommBufferAddr

If this is being done (which is a must) then remove the TODO.
[Supreeth] Ok.

> +
> +    // This event id is the parent of multiple GUIDed handlers. Retrieve
> +    // the specific GUID from the communication buffer passed by the
> +    // caller.

This comment is outdated. It should be removed.
[Supreeth] Ok.

> +
> +    if (NsCommBufferAddr && (NsCommBufferAddr < mNsCommBuffer.PhysicalStart))
> +      return EFI_INVALID_PARAMETER;
> +

This check is not enough before the NsCommBufferAddr is dereferenced in the next statement. There must be a check to ensure that NsCommBufferAddr points to enough memory to hold a EFI_MM_COMMUNICATE_HEADER i.e.
[Supreeth] Sorry. Need More Clarification. This is an arbitrary address that is passed without the size argument. How will we ensure this?

if (NsCommBufferAddr + sizeof(EFI_MM_COMMUNICATE_HEADER) >=
    mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize)
      return EFI_INVALID_PARAMETER;

> +    // Find out the size of the buffer passed
> +    NsCommBufferSize = ((EFI_MM_COMMUNICATE_HEADER *) NsCommBufferAddr)->MessageLength +
> +                        sizeof(((EFI_MM_COMMUNICATE_HEADER *)NsCommBufferAddr)->MessageLength) +
> +                        sizeof(((EFI_MM_COMMUNICATE_HEADER
> + *)NsCommBufferAddr)->HeaderGuid);

This check is more complicated that it should be. Can it be changed to:

((EFI_MM_COMMUNICATE_HEADER *) NsCommBufferAddr)->MessageLength + sizeof(EFI_MM_COMMUNICATE_HEADER)
[Supreeth] No. it is not a check (to be pedantic). The statements will return different values.
> +
> +    // Alternative approach in case EL3 has preallocated the non-secure
> +    // buffer. MM Foundation is told about the buffer through the Hoblist
> +    // and is responsible for performing the bounds check.

Can this comment be reworded as the "alternative" approach is the only approach possible at the moment.
[Supreeth] Ok.

> +    if (NsCommBufferAddr + NsCommBufferSize >=
> +      mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize)
> +        return EFI_INVALID_PARAMETER;
> +
> +
> +    // Now that the secure world can see the normal world buffer, allocate
> +    // memory to copy the communication buffer to the secure world.
> +    Status = mMmst->MmAllocatePool(EfiRuntimeServicesData, NsCommBufferSize, (VOID **) &GuidedEventContext);
> +    if (Status != EFI_SUCCESS) {
> +      DEBUG ((DEBUG_INFO, "Mem alloc failed - 0x%x\n", EventId));
> +      // TODO: Unmap secure memory before exiting to the normal world

This TODO is no longer relevant it seems.
[Supreeth] Ok.

> +      return Status;

These return statuses will be returned to the normal world as MM_COMMUNICATE return codes. AllocatePool returns EFI_OUT_OF_RESOURCES which must be mapped to a suitable error code as per the MM interface. Ditto for all other status values that this file returns.
[Supreeth] Ok. This is being done in "StandaloneMmCoreEntryPoint.c". See Version 2 of the patch.
> +    }
> +
> +    // X1 contains the VA of the normal world memory accessible from
> +    // S-EL0
> +    CopyMem(GuidedEventContext, (CONST VOID *) NsCommBufferAddr,
> + NsCommBufferSize);  }
> +
> +  // Stash the pointer to the allocated Event Context for this CPU
> + PerCpuGuidedEventContext[CpuNumber] = GuidedEventContext;
> +
> +  // TODO: Populate entire entry point context with valid information

I don't think we will be able to do this on AArch64. So lets remove this TODO.
[Supreeth] Ok.

> +  MmEntryPointContext.CurrentlyExecutingCpu = CpuNumber;
> + MmEntryPointContext.NumberOfCpus =
> + mMpInformationHobData->NumberOfProcessors;
> +
> +  // Populate the MM system table with MP and state information
> + mMmst->CurrentlyExecutingCpu = CpuNumber;  mMmst->NumberOfCpus =
> + mMpInformationHobData->NumberOfProcessors;
> +  mMmst->CpuSaveStateSize = 0;
> +  mMmst->CpuSaveState = NULL;
> +
> +  mMmEntryPoint(&MmEntryPointContext);

It is worth NULL checking mMmEntryPoint!
[Supreeth] Ok.
> +
> +  // Free the memory allocation done earlier and reset the per-cpu
> + context  // TODO: Check for the return status of the FreePool API

Indeed

> +  ASSERT (GuidedEventContext);
> +  CopyMem ((VOID *)NsCommBufferAddr, (CONST VOID *)
> + GuidedEventContext, NsCommBufferSize);  mMmst->MmFreePool((VOID *)
> + GuidedEventContext);

If this fails in the unlikely case them we should just return NO_MEMORY to the normal world?
[Supreeth] Yes. Ok.

cheers,
Achin

> +  PerCpuGuidedEventContext[CpuNumber] = NULL;
> +
> +  return Status;
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +MmFoundationEntryRegister(
> +  IN CONST EFI_MM_CONFIGURATION_PROTOCOL  *This,
> +  IN EFI_MM_ENTRY_POINT                    MmEntryPoint
> +  ) {
> +  // store the entry point in a global
> +  mMmEntryPoint = MmEntryPoint;
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +PiMmCpuTpFwRootMmiHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_STATUS Status;
> +  UINTN      CpuNumber;
> +
> +  ASSERT (Context == NULL);
> +  ASSERT (CommBuffer == NULL);
> +  ASSERT (CommBufferSize == NULL);
> +
> +  CpuNumber = mMmst->CurrentlyExecutingCpu;  if
> + (!PerCpuGuidedEventContext[CpuNumber])
> +    return EFI_NOT_FOUND;
> +
> +  DEBUG ((DEBUG_INFO, "CommBuffer - 0x%x, CommBufferSize - 0x%x\n",
> +          PerCpuGuidedEventContext[CpuNumber],
> +          PerCpuGuidedEventContext[CpuNumber]->MessageLength));
> +
> +  Status = mMmst->MmiManage(&PerCpuGuidedEventContext[CpuNumber]->HeaderGuid,
> +                     NULL,
> +                     PerCpuGuidedEventContext[CpuNumber]->Data,
> +
> + &PerCpuGuidedEventContext[CpuNumber]->MessageLength);
> +
> +  if (Status != EFI_SUCCESS) {
> +    DEBUG ((DEBUG_WARN, "Unable to manage Guided Event - %d\n",
> + Status));  }
> +
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
> b/StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
> new file mode 100644
> index 0000000000..9b48ea15c1
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
> @@ -0,0 +1,229 @@
> +/** @file
> +
> +  Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
> + Copyright (c) 2011, ARM Limited. All rights reserved.

2011?
[Supreeth] Ok. Updated.

> +  Copyright (c) 2016 HP Development Company, L.P.
> +  Copyright (c) 2016-2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Base.h>
> +#include <Pi/PiMmCis.h>
> +#include <Library/Arm/StandaloneMmCoreEntryPoint.h>
> +#include <Library/DebugLib.h>
> +#include <Library/ArmSvcLib.h>
> +#include <Library/ArmLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/HobLib.h>
> +
> +#include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT
> +
> +#include <Guid/ZeroGuid.h>
> +#include <Guid/MmramMemoryReserve.h>
> +
> +
> +#include "PiMmStandloneArmTfCpuDriver.h"
> +
> +// GUID to identify HOB with whereabouts of communication buffer with
> +Normal // World extern EFI_GUID gEfiStandaloneMmNonSecureBufferGuid;
> +
> +// GUID to identify HOB where the entry point of this CPU driver will
> +be // populated to allow the entry point driver to invoke it upon
> +receipt of an // event extern EFI_GUID
> +gEfiArmTfCpuDriverEpDescriptorGuid;
> +
> +//
> +// Private copy of the MM system table for future use //
> +EFI_MM_SYSTEM_TABLE *mMmst = NULL;
> +
> +//
> +// Globals used to initialize the protocol //
> +static EFI_HANDLE            mMmCpuHandle = NULL;
> +
> +EFI_STATUS
> +GetGuidedHobData (
> +  IN  VOID *HobList,
> +  IN  CONST EFI_GUID *HobGuid,
> +  OUT VOID **HobData)
> +{
> +  EFI_HOB_GUID_TYPE *Hob;
> +
> +  if (!HobList || !HobGuid || !HobData)
> +    return EFI_INVALID_PARAMETER;
> +
> +  Hob = GetNextGuidHob (HobGuid, HobList);  if (!Hob)
> +    return EFI_NOT_FOUND;
> +
> +  *HobData = GET_GUID_HOB_DATA (Hob);  if (!HobData)
> +    return EFI_NOT_FOUND;
> +
> +  return EFI_SUCCESS;
> +}
> +
> +EFI_STATUS
> +PiMmStandloneArmTfCpuDriverInitialize (
> +  IN EFI_HANDLE         ImageHandle,  // not actual imagehandle
> +  IN EFI_MM_SYSTEM_TABLE   *SystemTable  // not actual systemtable
> +  )
> +{
> +  ARM_TF_CPU_DRIVER_EP_DESCRIPTOR *CpuDriverEntryPointDesc;
> +  EFI_CONFIGURATION_TABLE         *ConfigurationTable;
> +  MP_INFORMATION_HOB_DATA         *MpInformationHobData;
> +  EFI_MMRAM_DESCRIPTOR            *NsCommBufMmramRange;
> +  EFI_STATUS                       Status;
> +  EFI_HANDLE                       DispatchHandle;
> +  UINT32                           MpInfoSize;
> +  UINTN                            Index;
> +  UINTN                            ArraySize;
> +  VOID                            *HobStart;
> +
> +  ASSERT (SystemTable != NULL);
> +  mMmst = SystemTable;
> +
> +  // publish the MM config protocol so the MM core can register its
> + entry point  Status = mMmst->MmInstallProtocolInterface (&mMmCpuHandle,
> +                                              &gEfiMmConfigurationProtocolGuid,
> +                                              EFI_NATIVE_INTERFACE,
> +                                              &mMmConfig);  if
> + (EFI_ERROR(Status)) {
> +    return Status;
> +  }
> +
> +  // publish the MM CPU save state protocol  Status =
> + mMmst->MmInstallProtocolInterface (&mMmCpuHandle,
> +    &gEfiMmCpuProtocolGuid, EFI_NATIVE_INTERFACE, &mMmCpuState);  if
> + (EFI_ERROR(Status)) {
> +    return Status;
> +  }

CPU State save access services have not been implemented on AArch64. Lets remove this protocol.
[Supreeth] Ok.

> +
> +  // register the root MMI handler
> +  Status = mMmst->MmiHandlerRegister (PiMmCpuTpFwRootMmiHandler,
> +                                      NULL,
> +                                      &DispatchHandle);  if
> + (EFI_ERROR(Status)) {
> +    return Status;
> +  }
> +
> +  // Retrieve the Hoblist from the MMST to extract the details of the
> + NS  // communication buffer that has been reserved by S-EL1/EL3
> + ConfigurationTable = mMmst->MmConfigurationTable;  for (Index = 0;
> + Index < mMmst->NumberOfTableEntries; Index++) {
> +    if (CompareGuid (&gEfiHobListGuid, &(ConfigurationTable[Index].VendorGuid))) {
> +      break;
> +    }
> +  }
> +
> +  // Bail out if the Hoblist could not be found  // TODO: This could
> + also mean that  // the normal world will never interact
> + synchronously with the MM environment

All TODOs must be removed and this one in particular is not relevant now afaics.
[Supreeth] Ok.

> +  if (Index >= mMmst->NumberOfTableEntries) {
> +    DEBUG ((DEBUG_INFO, "Hoblist not found - 0x%x\n", Index));
> +    return EFI_OUT_OF_RESOURCES;
> +  }
> +
> +  HobStart = ConfigurationTable[Index].VendorTable;
> +
> +  //
> +  // Locate the HOB with the buffer to populate the entry point of
> + this driver  //  Status = GetGuidedHobData (
> +            HobStart,
> +            &gEfiArmTfCpuDriverEpDescriptorGuid,
> +            (VOID **) &CpuDriverEntryPointDesc);  if (EFI_ERROR
> + (Status)) {
> +    DEBUG ((DEBUG_INFO, "ArmTfCpuDriverEpDesc HOB data extraction failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  // Share the entry point of the CPU driver  DEBUG ((DEBUG_INFO,
> + "Sharing Cpu Driver EP *0x%lx = 0x%lx\n",
> +    (UINT64) CpuDriverEntryPointDesc->ArmTfCpuDriverEpPtr,
> +    (UINT64) PiMmStandloneArmTfCpuDriverEntry));
> +  *(CpuDriverEntryPointDesc->ArmTfCpuDriverEpPtr) =
> + PiMmStandloneArmTfCpuDriverEntry;
> +
> +  // Find the descriptor that contains the whereabouts of the buffer
> + for  // communication with the Normal world.
> +  Status = GetGuidedHobData (
> +            HobStart,
> +            &gEfiStandaloneMmNonSecureBufferGuid,
> +            (VOID **) &NsCommBufMmramRange);  if (EFI_ERROR(Status))
> + {
> +    DEBUG ((DEBUG_INFO, "NsCommBufMmramRange HOB data extraction failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  DEBUG ((DEBUG_INFO, "mNsCommBuffer.PhysicalStart - 0x%lx\n",
> + (UINT64) NsCommBufMmramRange->PhysicalStart));
> +  DEBUG ((DEBUG_INFO, "mNsCommBuffer.PhysicalSize - 0x%lx\n",
> + (UINT64) NsCommBufMmramRange->PhysicalSize));
> +
> +  CopyMem (&mNsCommBuffer, NsCommBufMmramRange,
> + sizeof(EFI_MMRAM_DESCRIPTOR));  DEBUG ((DEBUG_INFO, "mNsCommBuffer:
> + 0x%016lx - 0x%lx\n", mNsCommBuffer.CpuStart,
> + mNsCommBuffer.PhysicalSize));
> +
> +  //
> +  // Extract the MP information from the Hoblist  //  Status =
> + GetGuidedHobData (HobStart,
> +                             &gMpInformationHobGuid,
> +                             (VOID **) &MpInformationHobData);  if
> + (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_INFO, "MpInformationHob extraction failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  //
> +  // Allocate memory for the MP information and copy over the MP
> + information  // passed by Trusted Firmware. Use the number of
> + processors passed in the HOB  // to copy the processor information
> + //  MpInfoSize = sizeof (MP_INFORMATION_HOB_DATA) +
> +               (sizeof (EFI_PROCESSOR_INFORMATION) *
> +               MpInformationHobData->NumberOfProcessors);
> +  Status = mMmst->MmAllocatePool (EfiRuntimeServicesData,
> +                                  MpInfoSize,
> +                                  (void **) &mMpInformationHobData);
> + if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_INFO, "mMpInformationHobData mem alloc failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  CopyMem (mMpInformationHobData, MpInformationHobData, MpInfoSize);
> +
> +  // Print MP information
> +  DEBUG ((DEBUG_INFO, "mMpInformationHobData: 0x%016lx - 0x%lx\n",
> +          mMpInformationHobData->NumberOfProcessors,
> +          mMpInformationHobData->NumberOfEnabledProcessors));
> +  for (Index = 0; Index < mMpInformationHobData->NumberOfProcessors; Index++) {
> +    DEBUG ((DEBUG_INFO, "mMpInformationHobData[0x%lx]: %d, %d, %d\n",
> +            mMpInformationHobData->ProcessorInfoBuffer[Index].ProcessorId,
> +            mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Package,
> +            mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Core,
> +
> + mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Thread));
> +  }
> +
> +  //
> +  // Allocate memory for a table to hold pointers to a
> +  // EFI_MM_COMMUNICATE_HEADER for each CPU
> +  //
> +  ArraySize = sizeof (EFI_MM_COMMUNICATE_HEADER *) *
> +              mMpInformationHobData->NumberOfEnabledProcessors;
> +  Status = mMmst->MmAllocatePool (EfiRuntimeServicesData,
> +                                  ArraySize,
> +                                  (VOID **)
> +&PerCpuGuidedEventContext);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_INFO, "PerCpuGuidedEventContext mem alloc failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +  return Status;
> +}
> diff --git
> a/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
> b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
> new file mode 100644
> index 0000000000..17cefd171c
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
> @@ -0,0 +1,89 @@
> +/** @file
> +  Private header with declarations and definitions specific to the MM
> +Standalone
> +  CPU driver
> +
> +  Copyright (c) 2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _ARM_TF_CPU_DRIVER_H_
> +#define _ARM_TF_CPU_DRIVER_H_
> +
> +#include <Protocol/MmCommunication.h> #include
> +<Protocol/MmConfiguration.h> #include <Protocol/MmCpu.h> #include
> +<Guid/MpInformation.h>
> +
> +//
> +// Common declarations and definitions //
> +#define EVENT_ID_MM_COMMUNICATE_SMC     0x10

This is not used any longer

> +
> +//
> +// CPU driver initialization specific declarations // extern
> +EFI_MM_SYSTEM_TABLE *mMmst;
> +
> +//
> +// CPU State Save protocol specific declarations // extern
> +EFI_MM_CPU_PROTOCOL mMmCpuState;
> +
> +EFI_STATUS
> +EFIAPI
> +MmReadSaveState (
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  OUT VOID                        *Buffer
> +  );
> +
> +EFI_STATUS
> +EFIAPI
> +MmWriteSaveState (
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  IN CONST VOID                   *Buffer
> +  );

This protoocl is not implemented

> +
> +//
> +// MM event handling specific declarations //
> +extern EFI_MM_COMMUNICATE_HEADER    **PerCpuGuidedEventContext;
> +extern EFI_MMRAM_DESCRIPTOR          mNsCommBuffer;
> +extern MP_INFORMATION_HOB_DATA       *mMpInformationHobData;
> +extern EFI_MM_CONFIGURATION_PROTOCOL mMmConfig;
> +
> +EFI_STATUS
> +PiMmStandloneArmTfCpuDriverEntry (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  );
> +
> +EFI_STATUS
> +EFIAPI
> +PiMmCpuTpFwRootMmiHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +EFI_STATUS _PiMmStandloneArmTfCpuDriverEntry (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  );
> +
> +#endif
> diff --git
> a/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> new file mode 100644
> index 0000000000..baf6d957bb
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.in
> +++ f
> @@ -0,0 +1,60 @@
> +#/** @file
> +#
> +#  Standalone MM CPU driver for ARM Standard Platforms # #  Copyright
> +(c) 2009, Apple Inc. All rights reserved.<BR> #  Copyright (c) 2016
> +HP Development Company, L.P.
> +#  Copyright (c) 2017, ARM Limited. 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
> +#
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, #  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#**/
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = PiMmStandloneArmTfCpuDriver
> +  FILE_GUID                      = 58F7A62B-6280-42A7-BC38-10535A64A92C
> +  MODULE_TYPE                    = MM_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  ENTRY_POINT                    = PiMmStandloneArmTfCpuDriverInitialize
> +
> +[Sources]
> +  Init.c
> +  EventHandle.c
> +  StateSave.c

Not needed.

> +
> +[Packages]
> +  ArmPkg/ArmPkg.dec
> +  MdePkg/MdePkg.dec
> +  MdeModulePkg/MdeModulePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[LibraryClasses]
> +  ArmLib
> +  ArmSvcLib
> +  BaseMemoryLib
> +  DebugLib
> +  HobLib
> +  StandaloneMmDriverEntryPoint
> +
> +[Protocols]
> +  gEfiMmConfigurationProtocolGuid                        # PROTOCOL ALWAYS_PRODUCED
> +  gEfiMmCpuProtocolGuid                                  # PROTOCOL ALWAYS_PRODUCED
> +
> +[Guids]
> +  gEfiHobListGuid
> +  gEfiMmPeiMmramMemoryReserveGuid
> +  gZeroGuid
> +  gMpInformationHobGuid
> +  gEfiStandaloneMmNonSecureBufferGuid
> +  gEfiArmTfCpuDriverEpDescriptorGuid
> +
> +[Depex]
> +  TRUE
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
> b/StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
> new file mode 100644
> index 0000000000..c5155e1b31
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
> @@ -0,0 +1,51 @@
> +/** @file
> +
> +  Copyright (c) 2016 HP Development Company, L.P.
> +  Copyright (c) 2016-2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Base.h>
> +#include <Pi/PiSmmCis.h>
> +#include <Library/DebugLib.h>
> +
> +#include "PiMmStandloneArmTfCpuDriver.h"
> +
> +EFI_MM_CPU_PROTOCOL mMmCpuState = {
> +  MmReadSaveState,
> +  MmWriteSaveState
> +};
> +
> +EFI_STATUS
> +EFIAPI
> +MmReadSaveState(
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  OUT VOID                        *Buffer
> +  ) {
> +  // todo: implement
> +  return EFI_UNSUPPORTED;
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +MmWriteSaveState(
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  IN CONST VOID                   *Buffer
> +  ) {
> +  // todo: implement
> +  return EFI_UNSUPPORTED;
> +}

This file must be removed as described earlier.

> diff --git a/StandaloneMmPkg/Include/Guid/MpInformation.h
> b/StandaloneMmPkg/Include/Guid/MpInformation.h
> new file mode 100644
> index 0000000000..4e9a3c04ec
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Guid/MpInformation.h
> @@ -0,0 +1,41 @@
> +/** @file
> +  EFI MP information protocol provides a lightweight MP_SERVICES_PROTOCOL.
> +
> +  MP information protocol only provides static information of MP processor.
> +
> +  Copyright (c) 2009, Intel Corporation. All rights reserved.<BR>
> + Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _MP_INFORMATION_H_
> +#define _MP_INFORMATION_H_
> +
> +#include <Protocol/MpService.h>
> +#include <PiPei.h>
> +#include <Ppi/SecPlatformInformation.h>
> +
> +#define MP_INFORMATION_GUID \
> +  { \
> +    0xba33f15d, 0x4000, 0x45c1, {0x8e, 0x88, 0xf9, 0x16, 0x92, 0xd4,
> +0x57, 0xe3}  \
> +  }
> +
> +#pragma pack(1)
> +typedef struct {
> +  UINT64                     NumberOfProcessors;
> +  UINT64                     NumberOfEnabledProcessors;
> +  EFI_PROCESSOR_INFORMATION  ProcessorInfoBuffer[]; }
> +MP_INFORMATION_HOB_DATA; #pragma pack()
> +
> +extern EFI_GUID gMpInformationHobGuid;
> +
> +#endif
> --
> 2.16.2
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 11/18] StandaloneMmPkg: MM driver entry point library.
  2018-04-30 14:29   ` Achin Gupta
@ 2018-05-04 23:24     ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:24 UTC (permalink / raw)
  To: Achin Gupta; +Cc: edk2-devel@lists.01.org

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Monday, April 30, 2018 9:30 AM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 11/18] StandaloneMmPkg: MM driver entry point library.

Hi Supreeth,

Some of the DXE references will have to be removed and copyright years need to be updated. If that sounds reasonable then..

[Supreeth] Ok.

Acked-by: Achin Gupta <achin.gupta@arm.com>

cheers,
Achin

On Fri, Apr 06, 2018 at 03:42:16PM +0100, Supreeth Venkatesh wrote:
> This patch implements module entry point library for Standalone
> management mode (MM) Drivers.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  .../Include/Library/MmDriverStandaloneEntryPoint.h | 148 +++++++++++++++++++++
>  .../StandaloneMmDriverEntryPoint.c                 | 102 ++++++++++++++
>  .../StandaloneMmDriverEntryPoint.inf               |  41 ++++++
>  3 files changed, 291 insertions(+)
>  create mode 100644
> StandaloneMmPkg/Include/Library/MmDriverStandaloneEntryPoint.h
>  create mode 100644
> StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDrive
> rEntryPoint.c  create mode 100644
> StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDrive
> rEntryPoint.inf
>
> diff --git
> a/StandaloneMmPkg/Include/Library/MmDriverStandaloneEntryPoint.h
> b/StandaloneMmPkg/Include/Library/MmDriverStandaloneEntryPoint.h
> new file mode 100644
> index 0000000000..6fb9224e2e
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Library/MmDriverStandaloneEntryPoint.h
> @@ -0,0 +1,148 @@
> +/** @file
> +  Module entry point library for UEFI drivers, DXE Drivers, DXE
> +Runtime Drivers,
> +  and DXE SMM Drivers.
> +
> +Copyright (c) 2006 - 2008, Intel Corporation. All rights
> +reserved.<BR> Copyright (c) 2016 - 2017, ARM Limited. All rights
> +reserved.<BR>
> +
> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef __MODULE_ENTRY_POINT_H__
> +#define __MODULE_ENTRY_POINT_H__
> +
> +///
> +///Declare the PI Specification Revision that this driver requires to execute correctly.
> +///
> +extern CONST UINT32                   _gMmRevision;
> +
> +/**
> +  The entry point of PE/COFF Image for a DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
> +
> +  This function is the entry point for a DXE Driver, DXE Runtime
> + Driver, DXE SMM Driver,  or UEFI Driver.  This function must call
> + ProcessLibraryConstructorList() and  ProcessModuleEntryPointList().
> + If the return status from ProcessModuleEntryPointList()  is an error
> + status, then ProcessLibraryDestructorList() must be called. The
> + return value  from ProcessModuleEntryPointList() is returned. If
> + _gDriverUnloadImageCount is greater  than zero, then an unload handler must be registered for this image and the unload handler  must invoke ProcessModuleUnloadList().
> +  If _gUefiDriverRevision is not zero and SystemTable->Hdr.Revision
> + is less than _gUefiDriverRevison,  then return EFI_INCOMPATIBLE_VERSION.
> +
> +
> +  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +  @retval  EFI_SUCCESS               The DXE Driver, DXE Runtime Driver, DXE SMM Driver,
> +                                     or UEFI Driver exited normally.
> +  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than SystemTable->Hdr.Revision.
> +  @retval  Other                     Return value from ProcessModuleEntryPointList().
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +_ModuleEntryPoint (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  );
> +
> +
> +/**
> +  Required by the EBC compiler and identical in functionality to _ModuleEntryPoint().
> +
> +  This function is required to call _ModuleEntryPoint() passing in ImageHandle, and SystemTable.
> +
> +  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +  @retval  EFI_SUCCESS               The DXE Driver, DXE Runtime Driver, DXE SMM Driver,
> +                                     or UEFI Driver exited normally.
> +  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than SystemTable->Hdr.Revision.
> +  @retval  Other                     Return value from ProcessModuleEntryPointList().
> +**/
> +EFI_STATUS
> +EFIAPI
> +EfiMain (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  );
> +
> +
> +/**
> +  Autogenerated function that calls the library constructors for all
> +of the module's
> +  dependent libraries.
> +
> +  This function must be called by _ModuleEntryPoint().
> +  This function calls the set of library constructors for the set of
> + library instances  that a module depends on.  This includes library
> + instances that a module depends on  directly and library instances that a module depends on indirectly through other libraries.
> +  This function is autogenerated by build tools and those build tools
> + are responsible  for collecting the set of library instances,
> + determine which ones have constructors,  and calling the library
> + constructors in the proper order based upon each of the library  instances own dependencies.
> +
> +  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +**/
> +VOID
> +EFIAPI
> +ProcessLibraryConstructorList (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  );
> +
> +
> +/**
> +  Autogenerated function that calls the library descructors for all
> +of the module's
> +  dependent libraries.
> +
> +  This function may be called by _ModuleEntryPoint() or ExitDriver().
> +  This function calls the set of library destructors for the set of
> + library instances  that a module depends on. This includes library
> + instances that a module depends on  directly and library instances that a module depends on indirectly through other libraries.
> +  This function is autogenerated by build tools and those build tools
> + are responsible for  collecting the set of library instances,
> + determine which ones have destructors, and calling  the library destructors in the proper order based upon each of the library instances own dependencies.
> +
> +  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +**/
> +VOID
> +EFIAPI
> +ProcessLibraryDestructorList (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  );
> +
> +
> +/**
> +  Autogenerated function that calls a set of module entry points.
> +
> +  This function must be called by _ModuleEntryPoint().
> +  This function calls the set of module entry points.
> +  This function is autogenerated by build tools and those build tools
> + are responsible  for collecting the module entry points and calling them in a specified order.
> +
> +  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +  @retval  EFI_SUCCESS   The DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver executed normally.
> +  @retval  !EFI_SUCCESS  The DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver failed to execute normally.
> +**/
> +EFI_STATUS
> +EFIAPI
> +ProcessModuleEntryPointList (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  );
> +
> +#endif
> diff --git
> a/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDri
> verEntryPoint.c
> b/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDri
> verEntryPoint.c
> new file mode 100644
> index 0000000000..84b3d9cd08
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneM
> +++ mDriverEntryPoint.c
> @@ -0,0 +1,102 @@
> +/** @file
> +  Entry point to a Standalone SMM driver.
> +
> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016-2017, ARM Ltd. All rights reserved.<BR>
> +
> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +
> +
> +#include <PiMm.h>
> +
> +#include <Library/BaseLib.h>
> +#include <Library/DebugLib.h>
> +
> +VOID
> +EFIAPI
> +ProcessLibraryConstructorList (
> +  IN EFI_HANDLE               ImageHandle,
> +  IN IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
> +  );
> +
> +EFI_STATUS
> +EFIAPI
> +ProcessModuleEntryPointList (
> +  IN EFI_HANDLE               ImageHandle,
> +  IN IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
> +  );
> +
> +VOID
> +EFIAPI
> +ProcessLibraryDestructorList (
> +  IN EFI_HANDLE               ImageHandle,
> +  IN IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
> +  );
> +
> +/**
> +  The entry point of PE/COFF Image for a DXE Driver, DXE Runtime
> +Driver, DXE SMM
> +  Driver, or UEFI Driver.
> +
> +  This function is the entry point for a DXE Driver, DXE Runtime
> + Driver, DXE SMM Driver,  or UEFI Driver.  This function must call
> + ProcessLibraryConstructorList() and  ProcessModuleEntryPointList().
> + If the return status from ProcessModuleEntryPointList()  is an error
> + status, then ProcessLibraryDestructorList() must be called. The
> + return  value from ProcessModuleEntryPointList() is returned. If
> + _gDriverUnloadImageCount  is greater than zero, then an unload handler must be registered for this image  and the unload handler must invoke ProcessModuleUnloadList().
> +  If _gUefiDriverRevision is not zero and SystemTable->Hdr.Revision
> + is less than  _gUefiDriverRevison, then return EFI_INCOMPATIBLE_VERSION.
> +
> +
> +  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver,
> +                       DXE SMM Driver, or UEFI Driver.
> +  @param  SystemTable  A pointer to the EFI System Table.
> +
> +  @retval  EFI_SUCCESS               The DXE Driver, DXE Runtime Driver, DXE SMM
> +                                     Driver, or UEFI Driver exited normally.
> +  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than
> +                                    SystemTable->Hdr.Revision.
> +  @retval  Other                     Return value from ProcessModuleEntryPointList().
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +_ModuleEntryPoint (
> +  IN EFI_HANDLE               ImageHandle,
> +  IN IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
> +  )
> +{
> +  EFI_STATUS                 Status;
> +
> +  //
> +  // Call constructor for all libraries  //
> + ProcessLibraryConstructorList (ImageHandle, MmSystemTable);
> +
> +  //
> +  // Call the driver entry point
> +  //
> +  Status = ProcessModuleEntryPointList (ImageHandle, MmSystemTable);
> +
> +  //
> +  // If all of the drivers returned errors, then invoke all of the
> + library destructors  //  if (EFI_ERROR (Status)) {
> +    ProcessLibraryDestructorList (ImageHandle, MmSystemTable);  }
> +
> +  //
> +  // Return the cumulative return status code from all of the driver
> +entry points
> +  //
> +  return Status;
> +}
> +
> diff --git
> a/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDri
> verEntryPoint.inf
> b/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDri
> verEntryPoint.inf
> new file mode 100644
> index 0000000000..564a3f14c8
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/StandaloneMmDriverEntryPoint/StandaloneM
> +++ mDriverEntryPoint.inf
> @@ -0,0 +1,41 @@
> +## @file
> +# Module entry point library for Standalone SMM driver.
> +#
> +# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR> #
> +Copyright (c) 2016-2017, ARM Ltd. All rights reserved.<BR> # #  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.
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, #  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = StandaloneMmDriverEntryPoint
> +  FILE_GUID                      = BBC33478-98F8-4B78-B29D-574D681B7E43
> +  MODULE_TYPE                    = MM_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  LIBRARY_CLASS                  = StandaloneMmDriverEntryPoint|MM_STANDALONE
> +
> +#
> +# The following information is for reference only and not required by the build tools.
> +#
> +#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC
> +#
> +
> +[Sources]
> +  StandaloneMmDriverEntryPoint.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +
> +[LibraryClasses]
> +  BaseLib
> +  DebugLib
> +
> --
> 2.16.2
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 10/18] StandaloneMmPkg/HobLib: Add AARCH64 Specific HOB Library for management mode.
  2018-04-25 14:50   ` Achin Gupta
  2018-04-26 13:04     ` Yao, Jiewen
@ 2018-05-04 23:25     ` Supreeth Venkatesh
  1 sibling, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:25 UTC (permalink / raw)
  To: Achin Gupta; +Cc: edk2-devel@lists.01.org

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Wednesday, April 25, 2018 9:50 AM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 10/18] StandaloneMmPkg/HobLib: Add AARCH64 Specific HOB Library for management mode.

Hi Supreeth,

On Fri, Apr 06, 2018 at 03:42:15PM +0100, Supreeth Venkatesh wrote:
> The Standalone MM environment is initialized during the SEC phase on
> ARM Standard Platforms. The MM Core driver implements an entry point
> module which is architecture specific and runs prior to the generic
> core driver code. The former creates a Hob list that the latter
> consumes. This happens in the same phase.
>
> This patch implements a Hob library that can be used by the entry
> point module to produce a Hob list and by the core driver code to consume it.

References to DXE core need to be removed and the copyright years needs to be updated.

I think it is worth getting this hoblib reviewed by the ArmPkg maintainers.
[Supreeth] I have modified it as Jiewen mentioned as it is StandaloneMmPkg specific. However, More the merrier.

Acked-by: Achin Gupta <achin.gupta@arm.com>

>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  StandaloneMmPkg/Library/HobLib/Arm/HobLib.c | 697 ++++++++++++++++++++++++++++
>  StandaloneMmPkg/Library/HobLib/HobLib.inf   |  45 ++
>  2 files changed, 742 insertions(+)
>  create mode 100644 StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
>  create mode 100644 StandaloneMmPkg/Library/HobLib/HobLib.inf
>
> diff --git a/StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
> b/StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
> new file mode 100644
> index 0000000000..62abf47f95
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/HobLib/Arm/HobLib.c
> @@ -0,0 +1,697 @@
> +/** @file
> +  HOB Library implementation for DxeCore driver.
> +
> +Copyright (c) 2006 - 2014, Intel Corporation. All rights
> +reserved.<BR> Copyright (c) 2017, ARM Limited. All rights
> +reserved.<BR>
> +
> +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.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <PiMm.h>
> +
> +#include <Library/HobLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/BaseMemoryLib.h>
> +
> +#include <Guid/MemoryAllocationHob.h>
> +
> +//
> +// Cache copy of HobList pointer.
> +//
> +VOID *gHobList = NULL;
> +
> +/**
> +  Returns the pointer to the HOB list.
> +
> +  This function returns the pointer to first HOB in the list.
> +  For PEI phase, the PEI service GetHobList() can be used to retrieve
> + the pointer  to the HOB list.  For the DXE phase, the HOB list
> + pointer can be retrieved through  the EFI System Table by looking up theHOB list GUID in the System Configuration Table.
> +  Since the System Configuration Table does not exist that the time
> + the DXE Core is  launched, the DXE Core uses a global variable from
> + the DXE Core Entry Point Library  to manage the pointer to the HOB list.
> +
> +  If the pointer to the HOB list is NULL, then ASSERT().
> +
> +  @return The pointer to the HOB list.
> +
> +**/
> +VOID *
> +EFIAPI
> +GetHobList (
> +  VOID
> +  )
> +{
> +  ASSERT (gHobList != NULL);
> +  return gHobList;
> +}
> +
> +/**
> +  Returns the next instance of a HOB type from the starting HOB.
> +
> +  This function searches the first instance of a HOB type from the starting HOB pointer.
> +  If there does not exist such HOB type from the starting HOB pointer, it will return NULL.
> +  In contrast with macro GET_NEXT_HOB(), this function does not skip
> + the starting HOB pointer
> +  unconditionally: it returns HobStart back if HobStart itself meets
> + the requirement;  caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
> +
> +  If HobStart is NULL, then ASSERT().
> +
> +  @param  Type          The HOB type to return.
> +  @param  HobStart      The starting HOB pointer to search from.
> +
> +  @return The next instance of a HOB type from the starting HOB.
> +
> +**/
> +VOID *
> +EFIAPI
> +GetNextHob (
> +  IN UINT16                 Type,
> +  IN CONST VOID             *HobStart
> +  )
> +{
> +  EFI_PEI_HOB_POINTERS  Hob;
> +
> +  ASSERT (HobStart != NULL);
> +
> +  Hob.Raw = (UINT8 *) HobStart;
> +  //
> +  // Parse the HOB list until end of list or matching type is found.
> +  //
> +  while (!END_OF_HOB_LIST (Hob)) {
> +    if (Hob.Header->HobType == Type) {
> +      return Hob.Raw;
> +    }
> +    Hob.Raw = GET_NEXT_HOB (Hob);
> +  }
> +  return NULL;
> +}
> +
> +/**
> +  Returns the first instance of a HOB type among the whole HOB list.
> +
> +  This function searches the first instance of a HOB type among the whole HOB list.
> +  If there does not exist such HOB type in the HOB list, it will return NULL.
> +
> +  If the pointer to the HOB list is NULL, then ASSERT().
> +
> +  @param  Type          The HOB type to return.
> +
> +  @return The next instance of a HOB type from the starting HOB.
> +
> +**/
> +VOID *
> +EFIAPI
> +GetFirstHob (
> +  IN UINT16                 Type
> +  )
> +{
> +  VOID      *HobList;
> +
> +  HobList = GetHobList ();
> +  return GetNextHob (Type, HobList);
> +}
> +
> +/**
> +  Returns the next instance of the matched GUID HOB from the starting HOB.
> +
> +  This function searches the first instance of a HOB from the starting HOB pointer.
> +  Such HOB should satisfy two conditions:
> +  its HOB type is EFI_HOB_TYPE_GUID_EXTENSION, and its GUID Name equals to the input Guid.
> +  If such a HOB from the starting HOB pointer does not exist, it will return NULL.
> +  Caller is required to apply GET_GUID_HOB_DATA () and
> + GET_GUID_HOB_DATA_SIZE ()  to extract the data section and its size information, respectively.
> +  In contrast with macro GET_NEXT_HOB(), this function does not skip
> + the starting HOB pointer
> +  unconditionally: it returns HobStart back if HobStart itself meets
> + the requirement;  caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
> +
> +  If Guid is NULL, then ASSERT().
> +  If HobStart is NULL, then ASSERT().
> +
> +  @param  Guid          The GUID to match with in the HOB list.
> +  @param  HobStart      A pointer to a Guid.
> +
> +  @return The next instance of the matched GUID HOB from the starting HOB.
> +
> +**/
> +VOID *
> +EFIAPI
> +GetNextGuidHob (
> +  IN CONST EFI_GUID         *Guid,
> +  IN CONST VOID             *HobStart
> +  )
> +{
> +  EFI_PEI_HOB_POINTERS  GuidHob;
> +
> +  GuidHob.Raw = (UINT8 *) HobStart;
> +  while ((GuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, GuidHob.Raw)) != NULL) {
> +    if (CompareGuid (Guid, &GuidHob.Guid->Name)) {
> +      break;
> +    }
> +    GuidHob.Raw = GET_NEXT_HOB (GuidHob);
> +  }
> +  return GuidHob.Raw;
> +}
> +
> +/**
> +  Returns the first instance of the matched GUID HOB among the whole HOB list.
> +
> +  This function searches the first instance of a HOB among the whole HOB list.
> +  Such HOB should satisfy two conditions:
> +  its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
> +  If such a HOB from the starting HOB pointer does not exist, it will return NULL.
> +  Caller is required to apply GET_GUID_HOB_DATA () and
> + GET_GUID_HOB_DATA_SIZE ()  to extract the data section and its size information, respectively.
> +
> +  If the pointer to the HOB list is NULL, then ASSERT().
> +  If Guid is NULL, then ASSERT().
> +
> +  @param  Guid          The GUID to match with in the HOB list.
> +
> +  @return The first instance of the matched GUID HOB among the whole HOB list.
> +
> +**/
> +VOID *
> +EFIAPI
> +GetFirstGuidHob (
> +  IN CONST EFI_GUID         *Guid
> +  )
> +{
> +  VOID      *HobList;
> +
> +  HobList = GetHobList ();
> +  return GetNextGuidHob (Guid, HobList); }
> +
> +/**
> +  Get the system boot mode from the HOB list.
> +
> +  This function returns the system boot mode information from the
> + PHIT HOB in HOB list.
> +
> +  If the pointer to the HOB list is NULL, then ASSERT().
> +
> +  @param  VOID
> +
> +  @return The Boot Mode.
> +
> +**/
> +EFI_BOOT_MODE
> +EFIAPI
> +GetBootModeHob (
> +  VOID
> +  )
> +{
> +  EFI_HOB_HANDOFF_INFO_TABLE    *HandOffHob;
> +
> +  HandOffHob = (EFI_HOB_HANDOFF_INFO_TABLE *) GetHobList ();
> +
> +  return  HandOffHob->BootMode;
> +}
> +
> +
> +/**
> +
> +
> +**/
> +EFI_HOB_HANDOFF_INFO_TABLE*
> +HobConstructor (
> +  IN VOID   *EfiMemoryBegin,
> +  IN UINTN  EfiMemoryLength,
> +  IN VOID   *EfiFreeMemoryBottom,
> +  IN VOID   *EfiFreeMemoryTop
> +  )
> +{
> +  EFI_HOB_HANDOFF_INFO_TABLE  *Hob;
> +  EFI_HOB_GENERIC_HEADER      *HobEnd;
> +
> +  Hob    = EfiFreeMemoryBottom;
> +  HobEnd = (EFI_HOB_GENERIC_HEADER *)(Hob+1);
> +
> +  Hob->Header.HobType     = EFI_HOB_TYPE_HANDOFF;
> +  Hob->Header.HobLength   = sizeof(EFI_HOB_HANDOFF_INFO_TABLE);
> +  Hob->Header.Reserved    = 0;
> +
> +  HobEnd->HobType     = EFI_HOB_TYPE_END_OF_HOB_LIST;
> +  HobEnd->HobLength   = sizeof(EFI_HOB_GENERIC_HEADER);
> +  HobEnd->Reserved    = 0;
> +
> +  Hob->Version             = EFI_HOB_HANDOFF_TABLE_VERSION;
> +  Hob->BootMode            = BOOT_WITH_FULL_CONFIGURATION;
> +
> +  Hob->EfiMemoryTop        = (UINTN)EfiMemoryBegin + EfiMemoryLength;
> +  Hob->EfiMemoryBottom     = (UINTN)EfiMemoryBegin;
> +  Hob->EfiFreeMemoryTop    = (UINTN)EfiFreeMemoryTop;
> +  Hob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS)(UINTN)(HobEnd+1);
> +  Hob->EfiEndOfHobList     = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd;
> +
> +  gHobList = Hob;
> +
> +  return Hob;
> +}
> +
> +VOID *
> +CreateHob (
> +  IN  UINT16    HobType,
> +  IN  UINT16    HobLength
> +  )
> +{
> +  EFI_HOB_HANDOFF_INFO_TABLE  *HandOffHob;
> +  EFI_HOB_GENERIC_HEADER      *HobEnd;
> +  EFI_PHYSICAL_ADDRESS        FreeMemory;
> +  VOID                        *Hob;
> +
> +  HandOffHob = GetHobList ();
> +
> +  HobLength = (UINT16)((HobLength + 0x7) & (~0x7));
> +
> +  FreeMemory = HandOffHob->EfiFreeMemoryTop -
> + HandOffHob->EfiFreeMemoryBottom;
> +
> +  if (FreeMemory < HobLength) {
> +      return NULL;
> +  }
> +
> +  Hob = (VOID*) (UINTN) HandOffHob->EfiEndOfHobList;
> +  ((EFI_HOB_GENERIC_HEADER*) Hob)->HobType = HobType;
> +  ((EFI_HOB_GENERIC_HEADER*) Hob)->HobLength = HobLength;
> +  ((EFI_HOB_GENERIC_HEADER*) Hob)->Reserved = 0;
> +
> +  HobEnd = (EFI_HOB_GENERIC_HEADER*) ((UINTN)Hob + HobLength);
> + HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
> +
> +  HobEnd->HobType   = EFI_HOB_TYPE_END_OF_HOB_LIST;
> +  HobEnd->HobLength = sizeof(EFI_HOB_GENERIC_HEADER);
> + HobEnd->Reserved  = 0;  HobEnd++;  HandOffHob->EfiFreeMemoryBottom =
> + (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
> +
> +  return Hob;
> +}
> +
> +/**
> +  Builds a HOB for a loaded PE32 module.
> +
> +  This function builds a HOB for a loaded PE32 module.
> +  It can only be invoked during PEI phase;  for DXE phase, it will
> + ASSERT() since PEI HOB is read-only for DXE phase.
> +  If ModuleName is NULL, then ASSERT().
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  ModuleName              The GUID File Name of the module.
> +  @param  MemoryAllocationModule  The 64 bit physical address of the module.
> +  @param  ModuleLength            The length of the module in bytes.
> +  @param  EntryPoint              The 64 bit physical address of the module entry point.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildModuleHob (
> +  IN CONST EFI_GUID         *ModuleName,
> +  IN EFI_PHYSICAL_ADDRESS   MemoryAllocationModule,
> +  IN UINT64                 ModuleLength,
> +  IN EFI_PHYSICAL_ADDRESS   EntryPoint
> +  )
> +{
> +  EFI_HOB_MEMORY_ALLOCATION_MODULE  *Hob;
> +
> +  ASSERT (((MemoryAllocationModule & (EFI_PAGE_SIZE - 1)) == 0) &&
> +          ((ModuleLength & (EFI_PAGE_SIZE - 1)) == 0));
> +
> +  Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof
> + (EFI_HOB_MEMORY_ALLOCATION_MODULE));
> +
> +  CopyGuid (&(Hob->MemoryAllocationHeader.Name),
> + &gEfiHobMemoryAllocModuleGuid);  Hob->MemoryAllocationHeader.MemoryBaseAddress = MemoryAllocationModule;
> +  Hob->MemoryAllocationHeader.MemoryLength      = ModuleLength;
> +  Hob->MemoryAllocationHeader.MemoryType        = EfiBootServicesCode;
> +
> +  //
> +  // Zero the reserved space to match HOB spec  //  ZeroMem
> + (Hob->MemoryAllocationHeader.Reserved, sizeof
> + (Hob->MemoryAllocationHeader.Reserved));
> +
> +  CopyGuid (&Hob->ModuleName, ModuleName);
> +  Hob->EntryPoint = EntryPoint;
> +}
> +
> +/**
> +  Builds a HOB that describes a chunk of system memory.
> +
> +  This function builds a HOB that describes a chunk of system memory.
> +  It can only be invoked during PEI phase;  for DXE phase, it will
> + ASSERT() because PEI HOB is read-only for DXE phase.
> +
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  ResourceType        The type of resource described by this HOB.
> +  @param  ResourceAttribute   The resource attributes of the memory described by this HOB.
> +  @param  PhysicalStart       The 64 bit physical address of memory described by this HOB.
> +  @param  NumberOfBytes       The length of the memory described by this HOB in bytes.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildResourceDescriptorHob (
> +  IN EFI_RESOURCE_TYPE            ResourceType,
> +  IN EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttribute,
> +  IN EFI_PHYSICAL_ADDRESS         PhysicalStart,
> +  IN UINT64                       NumberOfBytes
> +  )
> +{
> +  EFI_HOB_RESOURCE_DESCRIPTOR  *Hob;
> +
> +  Hob = CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof
> + (EFI_HOB_RESOURCE_DESCRIPTOR));  ASSERT(Hob != NULL);
> +
> +  Hob->ResourceType      = ResourceType;
> +  Hob->ResourceAttribute = ResourceAttribute;
> +  Hob->PhysicalStart     = PhysicalStart;
> +  Hob->ResourceLength    = NumberOfBytes;
> +}
> +
> +/**
> +  Builds a GUID HOB with a certain data length.
> +
> +  This function builds a customized HOB tagged with a GUID for
> + identification  and returns the start address of GUID HOB data so that caller can fill the customized data.
> +  The HOB Header and Name field is already stripped.
> +  It can only be invoked during PEI phase;  for DXE phase, it will
> + ASSERT() since PEI HOB is read-only for DXE phase.
> +  If Guid is NULL, then ASSERT().
> +  If there is no additional space for HOB creation, then ASSERT().
> +  If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
> +
> +  @param  Guid          The GUID to tag the customized HOB.
> +  @param  DataLength    The size of the data payload for the GUID HOB.
> +
> +  @return The start address of GUID HOB data.
> +
> +**/
> +VOID *
> +EFIAPI
> +BuildGuidHob (
> +  IN CONST EFI_GUID              *Guid,
> +  IN UINTN                       DataLength
> +  )
> +{
> +  EFI_HOB_GUID_TYPE *Hob;
> +
> +  //
> +  // Make sure that data length is not too long.
> +  //
> +  ASSERT (DataLength <= (0xffff - sizeof (EFI_HOB_GUID_TYPE)));
> +
> +  Hob = CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16) (sizeof
> +(EFI_HOB_GUID_TYPE) + DataLength));
> +  CopyGuid (&Hob->Name, Guid);
> +  return Hob + 1;
> +}
> +
> +
> +/**
> +  Copies a data buffer to a newly-built HOB.
> +
> +  This function builds a customized HOB tagged with a GUID for
> + identification,  copies the input data to the HOB data field and returns the start address of the GUID HOB data.
> +  The HOB Header and Name field is already stripped.
> +  It can only be invoked during PEI phase;  for DXE phase, it will
> + ASSERT() since PEI HOB is read-only for DXE phase.
> +  If Guid is NULL, then ASSERT().
> +  If Data is NULL and DataLength > 0, then ASSERT().
> +  If there is no additional space for HOB creation, then ASSERT().
> +  If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
> +
> +  @param  Guid          The GUID to tag the customized HOB.
> +  @param  Data          The data to be copied into the data field of the GUID HOB.
> +  @param  DataLength    The size of the data payload for the GUID HOB.
> +
> +  @return The start address of GUID HOB data.
> +
> +**/
> +VOID *
> +EFIAPI
> +BuildGuidDataHob (
> +  IN CONST EFI_GUID              *Guid,
> +  IN VOID                        *Data,
> +  IN UINTN                       DataLength
> +  )
> +{
> +  VOID  *HobData;
> +
> +  ASSERT (Data != NULL || DataLength == 0);
> +
> +  HobData = BuildGuidHob (Guid, DataLength);
> +
> +  return CopyMem (HobData, Data, DataLength); }
> +
> +/**
> +  Builds a Firmware Volume HOB.
> +
> +  This function builds a Firmware Volume HOB.
> +  It can only be invoked during PEI phase;  for DXE phase, it will
> + ASSERT() because PEI HOB is read-only for DXE phase.
> +
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  BaseAddress   The base address of the Firmware Volume.
> +  @param  Length        The size of the Firmware Volume in bytes.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildFvHob (
> +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> +  IN UINT64                      Length
> +  )
> +{
> +  EFI_HOB_FIRMWARE_VOLUME  *Hob;
> +
> +  Hob = CreateHob (EFI_HOB_TYPE_FV, sizeof
> + (EFI_HOB_FIRMWARE_VOLUME));
> +
> +  Hob->BaseAddress = BaseAddress;
> +  Hob->Length      = Length;
> +}
> +
> +
> +/**
> +  Builds a EFI_HOB_TYPE_FV2 HOB.
> +
> +  This function builds a EFI_HOB_TYPE_FV2 HOB.
> +  It can only be invoked during PEI phase;  for DXE phase, it will
> + ASSERT() since PEI HOB is read-only for DXE phase.
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  BaseAddress   The base address of the Firmware Volume.
> +  @param  Length        The size of the Firmware Volume in bytes.
> +  @param  FvName       The name of the Firmware Volume.
> +  @param  FileName      The name of the file.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildFv2Hob (
> +  IN          EFI_PHYSICAL_ADDRESS        BaseAddress,
> +  IN          UINT64                      Length,
> +  IN CONST    EFI_GUID                    *FvName,
> +  IN CONST    EFI_GUID                    *FileName
> +  )
> +{
> +  EFI_HOB_FIRMWARE_VOLUME2  *Hob;
> +
> +  Hob = CreateHob (EFI_HOB_TYPE_FV2, sizeof
> + (EFI_HOB_FIRMWARE_VOLUME2));
> +
> +  Hob->BaseAddress = BaseAddress;
> +  Hob->Length      = Length;
> +  CopyGuid (&Hob->FvName, FvName);
> +  CopyGuid (&Hob->FileName, FileName); }
> +
> +
> +/**
> +  Builds a HOB for the CPU.
> +
> +  This function builds a HOB for the CPU.
> +  It can only be invoked during PEI phase;  for DXE phase, it will
> + ASSERT() since PEI HOB is read-only for DXE phase.
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  SizeOfMemorySpace   The maximum physical memory addressability of the processor.
> +  @param  SizeOfIoSpace       The maximum physical I/O addressability of the processor.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildCpuHob (
> +  IN UINT8                       SizeOfMemorySpace,
> +  IN UINT8                       SizeOfIoSpace
> +  )
> +{
> +  EFI_HOB_CPU  *Hob;
> +
> +  Hob = CreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));
> +
> +  Hob->SizeOfMemorySpace = SizeOfMemorySpace;
> +  Hob->SizeOfIoSpace     = SizeOfIoSpace;
> +
> +  //
> +  // Zero the reserved space to match HOB spec
> +  //
> +  ZeroMem (Hob->Reserved, sizeof (Hob->Reserved)); }
> +
> +/**
> +  Builds a HOB for the memory allocation.
> +
> +  This function builds a HOB for the memory allocation.
> +  It can only be invoked during PEI phase;  for DXE phase, it will
> + ASSERT() since PEI HOB is read-only for DXE phase.
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  BaseAddress   The 64 bit physical address of the memory.
> +  @param  Length        The length of the memory allocation in bytes.
> +  @param  MemoryType    Type of memory allocated by this HOB.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildMemoryAllocationHob (
> +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> +  IN UINT64                      Length,
> +  IN EFI_MEMORY_TYPE             MemoryType
> +  )
> +{
> +  EFI_HOB_MEMORY_ALLOCATION  *Hob;
> +
> +  ASSERT (((BaseAddress & (EFI_PAGE_SIZE - 1)) == 0) &&
> +          ((Length & (EFI_PAGE_SIZE - 1)) == 0));
> +
> +  Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof
> + (EFI_HOB_MEMORY_ALLOCATION));
> +
> +  ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));
> +  Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
> +  Hob->AllocDescriptor.MemoryLength      = Length;
> +  Hob->AllocDescriptor.MemoryType        = MemoryType;
> +  //
> +  // Zero the reserved space to match HOB spec
> +  //
> +  ZeroMem (Hob->AllocDescriptor.Reserved, sizeof
> +(Hob->AllocDescriptor.Reserved)); }
> +
> +/**
> +  Builds a HOB that describes a chunk of system memory with Owner GUID.
> +
> +  This function builds a HOB that describes a chunk of system memory.
> +  It can only be invoked during PEI phase;  for DXE phase, it will
> + ASSERT() since PEI HOB is read-only for DXE phase.
> +
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  ResourceType        The type of resource described by this HOB.
> +  @param  ResourceAttribute   The resource attributes of the memory described by this HOB.
> +  @param  PhysicalStart       The 64 bit physical address of memory described by this HOB.
> +  @param  NumberOfBytes       The length of the memory described by this HOB in bytes.
> +  @param  OwnerGUID           GUID for the owner of this resource.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildResourceDescriptorWithOwnerHob (
> +  IN EFI_RESOURCE_TYPE            ResourceType,
> +  IN EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttribute,
> +  IN EFI_PHYSICAL_ADDRESS         PhysicalStart,
> +  IN UINT64                       NumberOfBytes,
> +  IN EFI_GUID                     *OwnerGUID
> +  )
> +{
> +  //
> +  // PEI HOB is read only for DXE phase
> +  //
> +  ASSERT (FALSE);
> +}
> +
> +/**
> +  Builds a Capsule Volume HOB.
> +
> +  This function builds a Capsule Volume HOB.
> +  It can only be invoked during PEI phase;  for DXE phase, it will
> + ASSERT() because PEI HOB is read-only for DXE phase.
> +
> +  If the platform does not support Capsule Volume HOBs, then ASSERT().
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  BaseAddress   The base address of the Capsule Volume.
> +  @param  Length        The size of the Capsule Volume in bytes.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildCvHob (
> +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> +  IN UINT64                      Length
> +  )
> +{
> +  //
> +  // PEI HOB is read only for DXE phase
> +  //
> +  ASSERT (FALSE);
> +}
> +
> +
> +/**
> +  Builds a HOB for the BSP store.
> +
> +  This function builds a HOB for BSP store.
> +  It can only be invoked during PEI phase;  for DXE phase, it will
> + ASSERT() because PEI HOB is read-only for DXE phase.
> +
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  BaseAddress   The 64 bit physical address of the BSP.
> +  @param  Length        The length of the BSP store in bytes.
> +  @param  MemoryType    Type of memory allocated by this HOB.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildBspStoreHob (
> +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> +  IN UINT64                      Length,
> +  IN EFI_MEMORY_TYPE             MemoryType
> +  )
> +{
> +  //
> +  // PEI HOB is read only for DXE phase
> +  //
> +  ASSERT (FALSE);
> +}
> +
> +/**
> +  Builds a HOB for the Stack.
> +
> +  This function builds a HOB for the stack.
> +  It can only be invoked during PEI phase;  for DXE phase, it will
> + ASSERT() since PEI HOB is read-only for DXE phase.
> +  If there is no additional space for HOB creation, then ASSERT().
> +
> +  @param  BaseAddress   The 64 bit physical address of the Stack.
> +  @param  Length        The length of the stack in bytes.
> +
> +**/
> +VOID
> +EFIAPI
> +BuildStackHob (
> +  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
> +  IN UINT64                      Length
> +  )
> +{
> +  //
> +  // PEI HOB is read only for DXE phase
> +  //
> +  ASSERT (FALSE);
> +}
> diff --git a/StandaloneMmPkg/Library/HobLib/HobLib.inf
> b/StandaloneMmPkg/Library/HobLib/HobLib.inf
> new file mode 100644
> index 0000000000..42273b6d66
> --- /dev/null
> +++ b/StandaloneMmPkg/Library/HobLib/HobLib.inf
> @@ -0,0 +1,45 @@
> +## @file
> +# Instance of HOB Library for DXE Core.
> +#
> +# HOB Library implementation for the DXE Core. Does not have a constructor.
> +#  Uses gHobList defined in the DXE Core Entry Point Library.
> +#
> +# Copyright (c) 2007 - 2014, Intel Corporation. All rights
> +reserved.<BR> # Copyright (c) 2016 - 2017, ARM Limited. All rights
> +reserved.<BR> # #  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.
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, #  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = HobLib
> +  FILE_GUID                      = CF56EF2C-68D8-4BD5-9A8B-8A7BFCFF751C
> +  MODULE_TYPE                    = MM_CORE_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  LIBRARY_CLASS                  = HobLib|MM_CORE_STANDALONE MM_STANDALONE
> +
> +#
> +#  VALID_ARCHITECTURES           = AARCH64
> +#
> +
> +[Sources.AARCH64]
> +  Arm/HobLib.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +
> +
> +[LibraryClasses]
> +  BaseMemoryLib
> +  DebugLib
> +
> +[Guids]
> +  gEfiHobListGuid                               ## CONSUMES  ## SystemTable
> --
> 2.16.2
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 12/18] StandaloneMmPkg/CpuMm: Add CPU driver suitable for ARM Platforms.
  2018-04-18 22:09   ` Daniil Egranov
@ 2018-05-04 23:25     ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:25 UTC (permalink / raw)
  To: Daniil Egranov, edk2-devel@lists.01.org

My response inline.

-----Original Message-----
From: Daniil Egranov <daniil.egranov@arm.com>
Sent: Wednesday, April 18, 2018 5:09 PM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>; edk2-devel@lists.01.org
Cc: ard.biesheuvel@linaro.org; leif.lindholm@linaro.org; jiewen.yao@intel.com; liming.gao@intel.com; michael.d.kinney@intel.com
Subject: Re: [edk2] [PATCH v1 12/18] StandaloneMmPkg/CpuMm: Add CPU driver suitable for ARM Platforms.

Hi Supreeth,

I am not sure how much it enforced in edk2 but in most of the cases, the driver's name matches its directory name.

[Supreeth] Thanks. Renamed it. Please check version 2.

Thanks,
Daniil

On 04/06/2018 09:42 AM, Supreeth Venkatesh wrote:
> This patch adds a simple CPU driver that exports the
> EFI_MM_CONFIGURATION_PROTOCOL to allow registration of the Standalone
> MM Foundation entry point. It preserves the existing notification
> mechanism for the configuration protocol.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>   StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S          |  33 +++
>   StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c    | 231 +++++++++++++++++++++
>   StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c           | 229 ++++++++++++++++++++
>   .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h        |  89 ++++++++
>   .../CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf      |  60 ++++++
>   StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c      |  51 +++++
>   StandaloneMmPkg/Include/Guid/MpInformation.h       |  41 ++++
>   7 files changed, 734 insertions(+)
>   create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
>   create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
>   create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
>   create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
>   create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
>   create mode 100644 StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
>   create mode 100644 StandaloneMmPkg/Include/Guid/MpInformation.h
>
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
> b/StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
> new file mode 100644
> index 0000000000..0b6e1c330d
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/Entry.S
> @@ -0,0 +1,33 @@
> +//
> +//  Copyright (c) 2017, ARM Limited. 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
> +//
> +//  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, //  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +//
> +//
> +
> +#include <Base.h>
> +#include <AutoGen.h>
> +#include <IndustryStandard/ArmStdSmc.h>
> +
> +.text
> +.align 3
> +
> +GCC_ASM_IMPORT(PiMmStandloneArmTfCpuDriverEntry)
> +GCC_ASM_EXPORT(_PiMmStandloneArmTfCpuDriverEntry)
> +
> +// Stub entry point to ensure that the stacks are completely unwound
> +before // signalling completion of event handling
> +ASM_PFX(_PiMmStandloneArmTfCpuDriverEntry):
> +  bl    PiMmStandloneArmTfCpuDriverEntry
> +  mov   x1, x0
> +  mov   x0, #(ARM_SMC_ID_MM_EVENT_COMPLETE_AARCH64 & 0xffff)
> +  movk  x0, #((ARM_SMC_ID_MM_EVENT_COMPLETE_AARCH64 >> 16) & 0xffff), lsl #16
> +  svc   #0
> +LoopForever:
> +  b     LoopForever
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
> b/StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
> new file mode 100644
> index 0000000000..7b19f53ecb
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/EventHandle.c
> @@ -0,0 +1,231 @@
> +/** @file
> +
> +  Copyright (c) 2016 HP Development Company, L.P.
> +  Copyright (c) 2016 - 2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Base.h>
> +#include <Pi/PiMmCis.h>
> +
> +
> +#include <Library/ArmSvcLib.h>
> +#include <Library/ArmLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/HobLib.h>
> +
> +#include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT
> +
> +#include <Guid/ZeroGuid.h>
> +#include <Guid/MmramMemoryReserve.h>
> +
> +#include <IndustryStandard/ArmStdSmc.h>
> +
> +#include "PiMmStandloneArmTfCpuDriver.h"
> +
> +EFI_STATUS
> +EFIAPI
> +MmFoundationEntryRegister(
> +  IN CONST EFI_MM_CONFIGURATION_PROTOCOL  *This,
> +  IN EFI_MM_ENTRY_POINT                    MmEntryPoint
> +  );
> +
> +//
> +// On ARM platforms every event is expected to have a GUID associated
> +with // it. It will be used by the MM Entry point to find the handler
> +for the // event. It will either be populated in a
> +EFI_MM_COMMUNICATE_HEADER by the // caller of the event (e.g.
> +MM_COMMUNICATE SMC) or by the CPU driver // (e.g. during an
> +asynchronous event). In either case, this context is // maintained in
> +an array which has an entry for each CPU. The pointer to this //
> +array is held in PerCpuGuidedEventContext. Memory is allocated once
> +the // number of CPUs in the system are made known through the // MP_INFORMATION_HOB_DATA.
> +//
> +EFI_MM_COMMUNICATE_HEADER **PerCpuGuidedEventContext = NULL;
> +
> +//
> +// When an event is received by the CPU driver, it could correspond
> +to a unique // GUID (e.g. interrupt events) or to multiple GUIDs
> +(e.g. MM_COMMUNICATE // SMC). A table is used by the CPU driver to
> +find the GUID corresponding to the // event id in case there is a 1:1
> +mapping between the two. If an event id has // multiple GUIDs
> +associated with it then such an entry will not be found in // this table.
> +//
> +// TODO: Currently NULL since there are no asynchronous events static
> +EFI_GUID *EventIdToGuidLookupTable = NULL;
> +
> +// Descriptor with whereabouts of memory used for communication with
> +the normal world EFI_MMRAM_DESCRIPTOR  mNsCommBuffer;
> +
> +MP_INFORMATION_HOB_DATA *mMpInformationHobData;
> +
> +EFI_MM_CONFIGURATION_PROTOCOL mMmConfig = {
> +  0,
> +  MmFoundationEntryRegister
> +};
> +
> +static EFI_MM_ENTRY_POINT     mMmEntryPoint = NULL;
> +
> +EFI_STATUS
> +PiMmStandloneArmTfCpuDriverEntry (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  )
> +{
> +  EFI_MM_COMMUNICATE_HEADER *GuidedEventContext = NULL;
> +  EFI_MM_ENTRY_CONTEXT        MmEntryPointContext = {0};
> +  EFI_STATUS                  Status;
> +  UINTN                       NsCommBufferSize;
> +
> +  DEBUG ((DEBUG_INFO, "Received event - 0x%x on cpu %d\n", EventId,
> + CpuNumber));
> +
> +  //
> +  // ARM TF passes SMC FID of the MM_COMMUNICATE interface as the
> + Event ID upon  // receipt of a synchronous MM request. Use the Event
> + ID to distinguish  // between synchronous and asynchronous events.
> +  //
> +  if (ARM_SMC_ID_MM_COMMUNICATE_AARCH64 != EventId) {
> +    // Found a GUID, allocate memory to populate a communication buffer
> +    // with the GUID in it
> +    Status = mMmst->MmAllocatePool(EfiRuntimeServicesData, sizeof(EFI_MM_COMMUNICATE_HEADER), (VOID **) &GuidedEventContext);
> +    if (Status != EFI_SUCCESS) {
> +      DEBUG ((DEBUG_INFO, "Mem alloc failed - 0x%x\n", EventId));
> +      return Status;
> +    }
> +
> +    // Copy the GUID
> +    CopyGuid(&GuidedEventContext->HeaderGuid,
> + &EventIdToGuidLookupTable[EventId]);
> +
> +    // Message Length is 0 'cause of the assumption mentioned above
> +    GuidedEventContext->MessageLength = 0;  } else {
> +    // TODO: Perform parameter validation of NsCommBufferAddr
> +
> +    // This event id is the parent of multiple GUIDed handlers. Retrieve
> +    // the specific GUID from the communication buffer passed by the
> +    // caller.
> +
> +    if (NsCommBufferAddr && (NsCommBufferAddr < mNsCommBuffer.PhysicalStart))
> +      return EFI_INVALID_PARAMETER;
> +
> +    // Find out the size of the buffer passed
> +    NsCommBufferSize = ((EFI_MM_COMMUNICATE_HEADER *) NsCommBufferAddr)->MessageLength +
> +                        sizeof(((EFI_MM_COMMUNICATE_HEADER *)NsCommBufferAddr)->MessageLength) +
> +                        sizeof(((EFI_MM_COMMUNICATE_HEADER
> + *)NsCommBufferAddr)->HeaderGuid);
> +
> +    // Alternative approach in case EL3 has preallocated the non-secure
> +    // buffer. MM Foundation is told about the buffer through the Hoblist
> +    // and is responsible for performing the bounds check.
> +    if (NsCommBufferAddr + NsCommBufferSize >=
> +      mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize)
> +        return EFI_INVALID_PARAMETER;
> +
> +
> +    // Now that the secure world can see the normal world buffer, allocate
> +    // memory to copy the communication buffer to the secure world.
> +    Status = mMmst->MmAllocatePool(EfiRuntimeServicesData, NsCommBufferSize, (VOID **) &GuidedEventContext);
> +    if (Status != EFI_SUCCESS) {
> +      DEBUG ((DEBUG_INFO, "Mem alloc failed - 0x%x\n", EventId));
> +      // TODO: Unmap secure memory before exiting to the normal world
> +      return Status;
> +    }
> +
> +    // X1 contains the VA of the normal world memory accessible from
> +    // S-EL0
> +    CopyMem(GuidedEventContext, (CONST VOID *) NsCommBufferAddr,
> + NsCommBufferSize);  }
> +
> +  // Stash the pointer to the allocated Event Context for this CPU
> + PerCpuGuidedEventContext[CpuNumber] = GuidedEventContext;
> +
> +  // TODO: Populate entire entry point context with valid information
> + MmEntryPointContext.CurrentlyExecutingCpu = CpuNumber;
> + MmEntryPointContext.NumberOfCpus =
> + mMpInformationHobData->NumberOfProcessors;
> +
> +  // Populate the MM system table with MP and state information
> + mMmst->CurrentlyExecutingCpu = CpuNumber;  mMmst->NumberOfCpus =
> + mMpInformationHobData->NumberOfProcessors;
> +  mMmst->CpuSaveStateSize = 0;
> +  mMmst->CpuSaveState = NULL;
> +
> +  mMmEntryPoint(&MmEntryPointContext);
> +
> +  // Free the memory allocation done earlier and reset the per-cpu
> + context  // TODO: Check for the return status of the FreePool API
> + ASSERT (GuidedEventContext);  CopyMem ((VOID *)NsCommBufferAddr,
> + (CONST VOID *) GuidedEventContext, NsCommBufferSize);
> + mMmst->MmFreePool((VOID *) GuidedEventContext);
> + PerCpuGuidedEventContext[CpuNumber] = NULL;
> +
> +  return Status;
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +MmFoundationEntryRegister(
> +  IN CONST EFI_MM_CONFIGURATION_PROTOCOL  *This,
> +  IN EFI_MM_ENTRY_POINT                    MmEntryPoint
> +  ) {
> +  // store the entry point in a global
> +  mMmEntryPoint = MmEntryPoint;
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +PiMmCpuTpFwRootMmiHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_STATUS Status;
> +  UINTN      CpuNumber;
> +
> +  ASSERT (Context == NULL);
> +  ASSERT (CommBuffer == NULL);
> +  ASSERT (CommBufferSize == NULL);
> +
> +  CpuNumber = mMmst->CurrentlyExecutingCpu;  if
> + (!PerCpuGuidedEventContext[CpuNumber])
> +    return EFI_NOT_FOUND;
> +
> +  DEBUG ((DEBUG_INFO, "CommBuffer - 0x%x, CommBufferSize - 0x%x\n",
> +          PerCpuGuidedEventContext[CpuNumber],
> +          PerCpuGuidedEventContext[CpuNumber]->MessageLength));
> +
> +  Status = mMmst->MmiManage(&PerCpuGuidedEventContext[CpuNumber]->HeaderGuid,
> +                     NULL,
> +                     PerCpuGuidedEventContext[CpuNumber]->Data,
> +
> + &PerCpuGuidedEventContext[CpuNumber]->MessageLength);
> +
> +  if (Status != EFI_SUCCESS) {
> +    DEBUG ((DEBUG_WARN, "Unable to manage Guided Event - %d\n",
> + Status));  }
> +
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
> b/StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
> new file mode 100644
> index 0000000000..9b48ea15c1
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/Init.c
> @@ -0,0 +1,229 @@
> +/** @file
> +
> +  Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
> + Copyright (c) 2011, ARM Limited. All rights reserved.
> +  Copyright (c) 2016 HP Development Company, L.P.
> +  Copyright (c) 2016-2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Base.h>
> +#include <Pi/PiMmCis.h>
> +#include <Library/Arm/StandaloneMmCoreEntryPoint.h>
> +#include <Library/DebugLib.h>
> +#include <Library/ArmSvcLib.h>
> +#include <Library/ArmLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/HobLib.h>
> +
> +#include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT
> +
> +#include <Guid/ZeroGuid.h>
> +#include <Guid/MmramMemoryReserve.h>
> +
> +
> +#include "PiMmStandloneArmTfCpuDriver.h"
> +
> +// GUID to identify HOB with whereabouts of communication buffer with
> +Normal // World extern EFI_GUID gEfiStandaloneMmNonSecureBufferGuid;
> +
> +// GUID to identify HOB where the entry point of this CPU driver will
> +be // populated to allow the entry point driver to invoke it upon
> +receipt of an // event extern EFI_GUID
> +gEfiArmTfCpuDriverEpDescriptorGuid;
> +
> +//
> +// Private copy of the MM system table for future use //
> +EFI_MM_SYSTEM_TABLE *mMmst = NULL;
> +
> +//
> +// Globals used to initialize the protocol //
> +static EFI_HANDLE            mMmCpuHandle = NULL;
> +
> +EFI_STATUS
> +GetGuidedHobData (
> +  IN  VOID *HobList,
> +  IN  CONST EFI_GUID *HobGuid,
> +  OUT VOID **HobData)
> +{
> +  EFI_HOB_GUID_TYPE *Hob;
> +
> +  if (!HobList || !HobGuid || !HobData)
> +    return EFI_INVALID_PARAMETER;
> +
> +  Hob = GetNextGuidHob (HobGuid, HobList);  if (!Hob)
> +    return EFI_NOT_FOUND;
> +
> +  *HobData = GET_GUID_HOB_DATA (Hob);  if (!HobData)
> +    return EFI_NOT_FOUND;
> +
> +  return EFI_SUCCESS;
> +}
> +
> +EFI_STATUS
> +PiMmStandloneArmTfCpuDriverInitialize (
> +  IN EFI_HANDLE         ImageHandle,  // not actual imagehandle
> +  IN EFI_MM_SYSTEM_TABLE   *SystemTable  // not actual systemtable
> +  )
> +{
> +  ARM_TF_CPU_DRIVER_EP_DESCRIPTOR *CpuDriverEntryPointDesc;
> +  EFI_CONFIGURATION_TABLE         *ConfigurationTable;
> +  MP_INFORMATION_HOB_DATA         *MpInformationHobData;
> +  EFI_MMRAM_DESCRIPTOR            *NsCommBufMmramRange;
> +  EFI_STATUS                       Status;
> +  EFI_HANDLE                       DispatchHandle;
> +  UINT32                           MpInfoSize;
> +  UINTN                            Index;
> +  UINTN                            ArraySize;
> +  VOID                            *HobStart;
> +
> +  ASSERT (SystemTable != NULL);
> +  mMmst = SystemTable;
> +
> +  // publish the MM config protocol so the MM core can register its
> + entry point  Status = mMmst->MmInstallProtocolInterface (&mMmCpuHandle,
> +                                              &gEfiMmConfigurationProtocolGuid,
> +                                              EFI_NATIVE_INTERFACE,
> +                                              &mMmConfig);  if
> + (EFI_ERROR(Status)) {
> +    return Status;
> +  }
> +
> +  // publish the MM CPU save state protocol  Status =
> + mMmst->MmInstallProtocolInterface (&mMmCpuHandle,
> +    &gEfiMmCpuProtocolGuid, EFI_NATIVE_INTERFACE, &mMmCpuState);  if
> + (EFI_ERROR(Status)) {
> +    return Status;
> +  }
> +
> +  // register the root MMI handler
> +  Status = mMmst->MmiHandlerRegister (PiMmCpuTpFwRootMmiHandler,
> +                                      NULL,
> +                                      &DispatchHandle);  if
> + (EFI_ERROR(Status)) {
> +    return Status;
> +  }
> +
> +  // Retrieve the Hoblist from the MMST to extract the details of the
> + NS  // communication buffer that has been reserved by S-EL1/EL3
> + ConfigurationTable = mMmst->MmConfigurationTable;  for (Index = 0;
> + Index < mMmst->NumberOfTableEntries; Index++) {
> +    if (CompareGuid (&gEfiHobListGuid, &(ConfigurationTable[Index].VendorGuid))) {
> +      break;
> +    }
> +  }
> +
> +  // Bail out if the Hoblist could not be found  // TODO: This could
> + also mean that  // the normal world will never interact
> + synchronously with the MM environment  if (Index >=
> + mMmst->NumberOfTableEntries) {
> +    DEBUG ((DEBUG_INFO, "Hoblist not found - 0x%x\n", Index));
> +    return EFI_OUT_OF_RESOURCES;
> +  }
> +
> +  HobStart = ConfigurationTable[Index].VendorTable;
> +
> +  //
> +  // Locate the HOB with the buffer to populate the entry point of
> + this driver  //  Status = GetGuidedHobData (
> +            HobStart,
> +            &gEfiArmTfCpuDriverEpDescriptorGuid,
> +            (VOID **) &CpuDriverEntryPointDesc);  if (EFI_ERROR
> + (Status)) {
> +    DEBUG ((DEBUG_INFO, "ArmTfCpuDriverEpDesc HOB data extraction failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  // Share the entry point of the CPU driver  DEBUG ((DEBUG_INFO,
> + "Sharing Cpu Driver EP *0x%lx = 0x%lx\n",
> +    (UINT64) CpuDriverEntryPointDesc->ArmTfCpuDriverEpPtr,
> +    (UINT64) PiMmStandloneArmTfCpuDriverEntry));
> +  *(CpuDriverEntryPointDesc->ArmTfCpuDriverEpPtr) =
> + PiMmStandloneArmTfCpuDriverEntry;
> +
> +  // Find the descriptor that contains the whereabouts of the buffer
> + for  // communication with the Normal world.
> +  Status = GetGuidedHobData (
> +            HobStart,
> +            &gEfiStandaloneMmNonSecureBufferGuid,
> +            (VOID **) &NsCommBufMmramRange);  if (EFI_ERROR(Status))
> + {
> +    DEBUG ((DEBUG_INFO, "NsCommBufMmramRange HOB data extraction failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  DEBUG ((DEBUG_INFO, "mNsCommBuffer.PhysicalStart - 0x%lx\n",
> + (UINT64) NsCommBufMmramRange->PhysicalStart));
> +  DEBUG ((DEBUG_INFO, "mNsCommBuffer.PhysicalSize - 0x%lx\n",
> + (UINT64) NsCommBufMmramRange->PhysicalSize));
> +
> +  CopyMem (&mNsCommBuffer, NsCommBufMmramRange,
> + sizeof(EFI_MMRAM_DESCRIPTOR));  DEBUG ((DEBUG_INFO, "mNsCommBuffer:
> + 0x%016lx - 0x%lx\n", mNsCommBuffer.CpuStart,
> + mNsCommBuffer.PhysicalSize));
> +
> +  //
> +  // Extract the MP information from the Hoblist  //  Status =
> + GetGuidedHobData (HobStart,
> +                             &gMpInformationHobGuid,
> +                             (VOID **) &MpInformationHobData);  if
> + (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_INFO, "MpInformationHob extraction failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  //
> +  // Allocate memory for the MP information and copy over the MP
> + information  // passed by Trusted Firmware. Use the number of
> + processors passed in the HOB  // to copy the processor information
> + //  MpInfoSize = sizeof (MP_INFORMATION_HOB_DATA) +
> +               (sizeof (EFI_PROCESSOR_INFORMATION) *
> +               MpInformationHobData->NumberOfProcessors);
> +  Status = mMmst->MmAllocatePool (EfiRuntimeServicesData,
> +                                  MpInfoSize,
> +                                  (void **) &mMpInformationHobData);
> + if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_INFO, "mMpInformationHobData mem alloc failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +
> +  CopyMem (mMpInformationHobData, MpInformationHobData, MpInfoSize);
> +
> +  // Print MP information
> +  DEBUG ((DEBUG_INFO, "mMpInformationHobData: 0x%016lx - 0x%lx\n",
> +          mMpInformationHobData->NumberOfProcessors,
> +          mMpInformationHobData->NumberOfEnabledProcessors));
> +  for (Index = 0; Index < mMpInformationHobData->NumberOfProcessors; Index++) {
> +    DEBUG ((DEBUG_INFO, "mMpInformationHobData[0x%lx]: %d, %d, %d\n",
> +            mMpInformationHobData->ProcessorInfoBuffer[Index].ProcessorId,
> +            mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Package,
> +            mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Core,
> +
> + mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Thread));
> +  }
> +
> +  //
> +  // Allocate memory for a table to hold pointers to a
> +  // EFI_MM_COMMUNICATE_HEADER for each CPU
> +  //
> +  ArraySize = sizeof (EFI_MM_COMMUNICATE_HEADER *) *
> +              mMpInformationHobData->NumberOfEnabledProcessors;
> +  Status = mMmst->MmAllocatePool (EfiRuntimeServicesData,
> +                                  ArraySize,
> +                                  (VOID **)
> +&PerCpuGuidedEventContext);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_INFO, "PerCpuGuidedEventContext mem alloc failed - 0x%x\n", Status));
> +    return Status;
> +  }
> +  return Status;
> +}
> diff --git
> a/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
> b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
> new file mode 100644
> index 0000000000..17cefd171c
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.h
> @@ -0,0 +1,89 @@
> +/** @file
> +  Private header with declarations and definitions specific to the MM
> +Standalone
> +  CPU driver
> +
> +  Copyright (c) 2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _ARM_TF_CPU_DRIVER_H_
> +#define _ARM_TF_CPU_DRIVER_H_
> +
> +#include <Protocol/MmCommunication.h> #include
> +<Protocol/MmConfiguration.h> #include <Protocol/MmCpu.h> #include
> +<Guid/MpInformation.h>
> +
> +//
> +// Common declarations and definitions //
> +#define EVENT_ID_MM_COMMUNICATE_SMC     0x10
> +
> +//
> +// CPU driver initialization specific declarations // extern
> +EFI_MM_SYSTEM_TABLE *mMmst;
> +
> +//
> +// CPU State Save protocol specific declarations // extern
> +EFI_MM_CPU_PROTOCOL mMmCpuState;
> +
> +EFI_STATUS
> +EFIAPI
> +MmReadSaveState (
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  OUT VOID                        *Buffer
> +  );
> +
> +EFI_STATUS
> +EFIAPI
> +MmWriteSaveState (
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  IN CONST VOID                   *Buffer
> +  );
> +
> +//
> +// MM event handling specific declarations //
> +extern EFI_MM_COMMUNICATE_HEADER    **PerCpuGuidedEventContext;
> +extern EFI_MMRAM_DESCRIPTOR          mNsCommBuffer;
> +extern MP_INFORMATION_HOB_DATA       *mMpInformationHobData;
> +extern EFI_MM_CONFIGURATION_PROTOCOL mMmConfig;
> +
> +EFI_STATUS
> +PiMmStandloneArmTfCpuDriverEntry (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  );
> +
> +EFI_STATUS
> +EFIAPI
> +PiMmCpuTpFwRootMmiHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +EFI_STATUS _PiMmStandloneArmTfCpuDriverEntry (
> +  IN UINTN EventId,
> +  IN UINTN CpuNumber,
> +  IN UINTN NsCommBufferAddr
> +  );
> +
> +#endif
> diff --git
> a/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> new file mode 100644
> index 0000000000..baf6d957bb
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.in
> +++ f
> @@ -0,0 +1,60 @@
> +#/** @file
> +#
> +#  Standalone MM CPU driver for ARM Standard Platforms # #  Copyright
> +(c) 2009, Apple Inc. All rights reserved.<BR> #  Copyright (c) 2016
> +HP Development Company, L.P.
> +#  Copyright (c) 2017, ARM Limited. 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
> +#
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, #  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#**/
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = PiMmStandloneArmTfCpuDriver
> +  FILE_GUID                      = 58F7A62B-6280-42A7-BC38-10535A64A92C
> +  MODULE_TYPE                    = MM_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  ENTRY_POINT                    = PiMmStandloneArmTfCpuDriverInitialize
> +
> +[Sources]
> +  Init.c
> +  EventHandle.c
> +  StateSave.c
> +
> +[Packages]
> +  ArmPkg/ArmPkg.dec
> +  MdePkg/MdePkg.dec
> +  MdeModulePkg/MdeModulePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[LibraryClasses]
> +  ArmLib
> +  ArmSvcLib
> +  BaseMemoryLib
> +  DebugLib
> +  HobLib
> +  StandaloneMmDriverEntryPoint
> +
> +[Protocols]
> +  gEfiMmConfigurationProtocolGuid                        # PROTOCOL ALWAYS_PRODUCED
> +  gEfiMmCpuProtocolGuid                                  # PROTOCOL ALWAYS_PRODUCED
> +
> +[Guids]
> +  gEfiHobListGuid
> +  gEfiMmPeiMmramMemoryReserveGuid
> +  gZeroGuid
> +  gMpInformationHobGuid
> +  gEfiStandaloneMmNonSecureBufferGuid
> +  gEfiArmTfCpuDriverEpDescriptorGuid
> +
> +[Depex]
> +  TRUE
> diff --git a/StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
> b/StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
> new file mode 100644
> index 0000000000..c5155e1b31
> --- /dev/null
> +++ b/StandaloneMmPkg/Drivers/CpuMm/Arm/StateSave.c
> @@ -0,0 +1,51 @@
> +/** @file
> +
> +  Copyright (c) 2016 HP Development Company, L.P.
> +  Copyright (c) 2016-2017, ARM Limited. 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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Base.h>
> +#include <Pi/PiSmmCis.h>
> +#include <Library/DebugLib.h>
> +
> +#include "PiMmStandloneArmTfCpuDriver.h"
> +
> +EFI_MM_CPU_PROTOCOL mMmCpuState = {
> +  MmReadSaveState,
> +  MmWriteSaveState
> +};
> +
> +EFI_STATUS
> +EFIAPI
> +MmReadSaveState(
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  OUT VOID                        *Buffer
> +  ) {
> +  // todo: implement
> +  return EFI_UNSUPPORTED;
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +MmWriteSaveState(
> +  IN CONST EFI_MM_CPU_PROTOCOL   *This,
> +  IN UINTN                        Width,
> +  IN EFI_MM_SAVE_STATE_REGISTER  Register,
> +  IN UINTN                        CpuIndex,
> +  IN CONST VOID                   *Buffer
> +  ) {
> +  // todo: implement
> +  return EFI_UNSUPPORTED;
> +}
> diff --git a/StandaloneMmPkg/Include/Guid/MpInformation.h
> b/StandaloneMmPkg/Include/Guid/MpInformation.h
> new file mode 100644
> index 0000000000..4e9a3c04ec
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Guid/MpInformation.h
> @@ -0,0 +1,41 @@
> +/** @file
> +  EFI MP information protocol provides a lightweight MP_SERVICES_PROTOCOL.
> +
> +  MP information protocol only provides static information of MP processor.
> +
> +  Copyright (c) 2009, Intel Corporation. All rights reserved.<BR>
> + Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _MP_INFORMATION_H_
> +#define _MP_INFORMATION_H_
> +
> +#include <Protocol/MpService.h>
> +#include <PiPei.h>
> +#include <Ppi/SecPlatformInformation.h>
> +
> +#define MP_INFORMATION_GUID \
> +  { \
> +    0xba33f15d, 0x4000, 0x45c1, {0x8e, 0x88, 0xf9, 0x16, 0x92, 0xd4,
> +0x57, 0xe3}  \
> +  }
> +
> +#pragma pack(1)
> +typedef struct {
> +  UINT64                     NumberOfProcessors;
> +  UINT64                     NumberOfEnabledProcessors;
> +  EFI_PROCESSOR_INFORMATION  ProcessorInfoBuffer[]; }
> +MP_INFORMATION_HOB_DATA; #pragma pack()
> +
> +extern EFI_GUID gMpInformationHobGuid;
> +
> +#endif
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 13/18] StandaloneMmPkg/Core: Implementation of Standalone MM Core Module.
  2018-04-30 19:19   ` Achin Gupta
  2018-04-30 19:28     ` Ard Biesheuvel
@ 2018-05-04 23:28     ` Supreeth Venkatesh
  1 sibling, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:28 UTC (permalink / raw)
  To: Achin Gupta
  Cc: edk2-devel@lists.01.org, michael.d.kinney@intel.com,
	liming.gao@intel.com, jiewen.yao@intel.com,
	leif.lindholm@linaro.org, ard.biesheuvel@linaro.org, nd

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Monday, April 30, 2018 2:20 PM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 13/18] StandaloneMmPkg/Core: Implementation of Standalone MM Core Module.

Hi Supreeth,

I think it is worth adding a signed off by Jiewen since he originally
contributed the code and it has not changed much since. Please update the
correct year in the copyright headers too.

[Supreeth] Indeed. Cover letter includes contribution. Since, he is reviewing it, added  Reviewed-by.

Acked-by: Achin Gupta <achin.gupta@arm.com>

cheers,
Achin

On Fri, Apr 06, 2018 at 03:42:18PM +0100, Supreeth Venkatesh wrote:
> Management Mode (MM) is a generic term used to describe a secure
> execution environment provided by the CPU and related silicon that is
> entered when the CPU detects a MMI. For x86 systems, this can be
> implemented with System Management Mode (SMM). For ARM systems, this can
> be implemented with TrustZone (TZ).
> A MMI can be a CPU instruction or interrupt. Upon detection of a MMI, a
> CPU will jump to the MM Entry Point and save some portion of its state
> (the "save state") such that execution can be resumed.
> The MMI can be generated synchronously by software or asynchronously by
> a hardware event. Each MMI source can be detected, cleared and disabled.
> Some systems provide for special memory (Management Mode RAM or MMRAM)
> which is set aside for software running in MM. Usually the MMRAM is
> hidden during normal CPU execution, but this is not required. Usually,
> after MMRAM is hidden it cannot be exposed until the next system reset.
>
> The MM Core Interface Specification describes three pieces of the PI
> Management Mode architecture:
> 1. MM Dispatch
>    During DXE, the DXE Foundation works with the MM Foundation to
>    schedule MM drivers for execution in the discovered firmware volumes.
> 2. MM Initialization
>    MM related code opens MMRAM, creates the MMRAM memory map, and
>    launches the MM Foundation, which provides the necessary services to
>    launch MM-related drivers. Then, sometime before boot, MMRAM is
>    closed and locked. This piece may be completed during the
>    SEC, PEI or DXE phases.
> 3. MMI Management
>    When an MMI generated, the MM environment is created and then the MMI
>
>    sources are detected and MMI handlers called.
>
> This patch implements the MM Core.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  StandaloneMmPkg/Core/Dependency.c                  |  389 +++++++
>  StandaloneMmPkg/Core/Dispatcher.c                  | 1071 ++++++++++++++++++++
>  StandaloneMmPkg/Core/FwVol.c                       |  104 ++
>  StandaloneMmPkg/Core/Handle.c                      |  533 ++++++++++
>  StandaloneMmPkg/Core/InstallConfigurationTable.c   |  178 ++++
>  StandaloneMmPkg/Core/Locate.c                      |  496 +++++++++
>  StandaloneMmPkg/Core/Mmi.c                         |  337 ++++++
>  StandaloneMmPkg/Core/Notify.c                      |  203 ++++
>  StandaloneMmPkg/Core/Page.c                        |  384 +++++++
>  StandaloneMmPkg/Core/Pool.c                        |  287 ++++++
>  StandaloneMmPkg/Core/StandaloneMmCore.c            |  708 +++++++++++++
>  StandaloneMmPkg/Core/StandaloneMmCore.h            |  903 +++++++++++++++++
>  StandaloneMmPkg/Core/StandaloneMmCore.inf          |   80 ++
>  StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h |   66 ++
>  StandaloneMmPkg/Include/Guid/MmFvDispatch.h        |   38 +
>  StandaloneMmPkg/Include/StandaloneMm.h             |   36 +
>  16 files changed, 5813 insertions(+)
>  create mode 100644 StandaloneMmPkg/Core/Dependency.c
>  create mode 100644 StandaloneMmPkg/Core/Dispatcher.c
>  create mode 100644 StandaloneMmPkg/Core/FwVol.c
>  create mode 100644 StandaloneMmPkg/Core/Handle.c
>  create mode 100644 StandaloneMmPkg/Core/InstallConfigurationTable.c
>  create mode 100644 StandaloneMmPkg/Core/Locate.c
>  create mode 100644 StandaloneMmPkg/Core/Mmi.c
>  create mode 100644 StandaloneMmPkg/Core/Notify.c
>  create mode 100644 StandaloneMmPkg/Core/Page.c
>  create mode 100644 StandaloneMmPkg/Core/Pool.c
>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.c
>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.h
>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCore.inf
>  create mode 100644 StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
>  create mode 100644 StandaloneMmPkg/Include/Guid/MmFvDispatch.h
>  create mode 100644 StandaloneMmPkg/Include/StandaloneMm.h
>
> diff --git a/StandaloneMmPkg/Core/Dependency.c b/StandaloneMmPkg/Core/Dependency.c
> new file mode 100644
> index 0000000000..e501369130
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Dependency.c
> @@ -0,0 +1,389 @@
> +/** @file
> +  MM Driver Dispatcher Dependency Evaluator
> +
> +  This routine evaluates a dependency expression (DEPENDENCY_EXPRESSION) to determine
> +  if a driver can be scheduled for execution.  The criteria for
> +  schedulability is that the dependency expression is satisfied.
> +
> +  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +///
> +/// EFI_DEP_REPLACE_TRUE - Used to dynamically patch the dependency expression
> +///                        to save time.  A EFI_DEP_PUSH is evaluated one an
> +///                        replaced with EFI_DEP_REPLACE_TRUE. If PI spec's Vol 2
> +///                        Driver Execution Environment Core Interface use 0xff
> +///                        as new DEPEX opcode. EFI_DEP_REPLACE_TRUE should be
> +///                        defined to a new value that is not conflicting with PI spec.
> +///
> +#define EFI_DEP_REPLACE_TRUE  0xff
> +
> +///
> +/// Define the initial size of the dependency expression evaluation stack
> +///
> +#define DEPEX_STACK_SIZE_INCREMENT  0x1000
> +
> +//
> +// Global stack used to evaluate dependency expressions
> +//
> +BOOLEAN  *mDepexEvaluationStack        = NULL;
> +BOOLEAN  *mDepexEvaluationStackEnd     = NULL;
> +BOOLEAN  *mDepexEvaluationStackPointer = NULL;
> +
> +/**
> +  Grow size of the Depex stack
> +
> +  @retval EFI_SUCCESS           Stack successfully growed.
> +  @retval EFI_OUT_OF_RESOURCES  There is not enough system memory to grow the stack.
> +
> +**/
> +EFI_STATUS
> +GrowDepexStack (
> +  VOID
> +  )
> +{
> +  BOOLEAN     *NewStack;
> +  UINTN       Size;
> +
> +  Size = DEPEX_STACK_SIZE_INCREMENT;
> +  if (mDepexEvaluationStack != NULL) {
> +    Size = Size + (mDepexEvaluationStackEnd - mDepexEvaluationStack);
> +  }
> +
> +  NewStack = AllocatePool (Size * sizeof (BOOLEAN));
> +  if (NewStack == NULL) {
> +    return EFI_OUT_OF_RESOURCES;
> +  }
> +
> +  if (mDepexEvaluationStack != NULL) {
> +    //
> +    // Copy to Old Stack to the New Stack
> +    //
> +    CopyMem (
> +      NewStack,
> +      mDepexEvaluationStack,
> +      (mDepexEvaluationStackEnd - mDepexEvaluationStack) * sizeof (BOOLEAN)
> +      );
> +
> +    //
> +    // Free The Old Stack
> +    //
> +    FreePool (mDepexEvaluationStack);
> +  }
> +
> +  //
> +  // Make the Stack pointer point to the old data in the new stack
> +  //
> +  mDepexEvaluationStackPointer = NewStack + (mDepexEvaluationStackPointer - mDepexEvaluationStack);
> +  mDepexEvaluationStack        = NewStack;
> +  mDepexEvaluationStackEnd     = NewStack + Size;
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Push an element onto the Boolean Stack.
> +
> +  @param  Value                 BOOLEAN to push.
> +
> +  @retval EFI_SUCCESS           The value was pushed onto the stack.
> +  @retval EFI_OUT_OF_RESOURCES  There is not enough system memory to grow the stack.
> +
> +**/
> +EFI_STATUS
> +PushBool (
> +  IN BOOLEAN  Value
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  //
> +  // Check for a stack overflow condition
> +  //
> +  if (mDepexEvaluationStackPointer == mDepexEvaluationStackEnd) {
> +    //
> +    // Grow the stack
> +    //
> +    Status = GrowDepexStack ();
> +    if (EFI_ERROR (Status)) {
> +      return Status;
> +    }
> +  }
> +
> +  //
> +  // Push the item onto the stack
> +  //
> +  *mDepexEvaluationStackPointer = Value;
> +  mDepexEvaluationStackPointer++;
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Pop an element from the Boolean stack.
> +
> +  @param  Value                 BOOLEAN to pop.
> +
> +  @retval EFI_SUCCESS           The value was popped onto the stack.
> +  @retval EFI_ACCESS_DENIED     The pop operation underflowed the stack.
> +
> +**/
> +EFI_STATUS
> +PopBool (
> +  OUT BOOLEAN  *Value
> +  )
> +{
> +  //
> +  // Check for a stack underflow condition
> +  //
> +  if (mDepexEvaluationStackPointer == mDepexEvaluationStack) {
> +    return EFI_ACCESS_DENIED;
> +  }
> +
> +  //
> +  // Pop the item off the stack
> +  //
> +  mDepexEvaluationStackPointer--;
> +  *Value = *mDepexEvaluationStackPointer;
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  This is the POSTFIX version of the dependency evaluator.  This code does
> +  not need to handle Before or After, as it is not valid to call this
> +  routine in this case. POSTFIX means all the math is done on top of the stack.
> +
> +  @param  DriverEntry           DriverEntry element to update.
> +
> +  @retval TRUE                  If driver is ready to run.
> +  @retval FALSE                 If driver is not ready to run or some fatal error
> +                                was found.
> +
> +**/
> +BOOLEAN
> +MmIsSchedulable (
> +  IN  EFI_MM_DRIVER_ENTRY   *DriverEntry
> +  )
> +{
> +  EFI_STATUS  Status;
> +  UINT8       *Iterator;
> +  BOOLEAN     Operator;
> +  BOOLEAN     Operator2;
> +  EFI_GUID    DriverGuid;
> +  VOID        *Interface;
> +
> +  Operator = FALSE;
> +  Operator2 = FALSE;
> +
> +  if (DriverEntry->After || DriverEntry->Before) {
> +    //
> +    // If Before or After Depex skip as MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
> +    // processes them.
> +    //
> +    return FALSE;
> +  }
> +
> +  DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
> +
> +  if (DriverEntry->Depex == NULL) {
> +    //
> +    // A NULL Depex means that the MM driver is not built correctly.
> +    // All MM drivers must have a valid depex expressiion.
> +    //
> +    DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Depex is empty)\n"));
> +    ASSERT (FALSE);
> +    return FALSE;
> +  }
> +
> +  //
> +  // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
> +  // incorrectly formed DEPEX expressions
> +  //
> +  mDepexEvaluationStackPointer = mDepexEvaluationStack;
> +
> +
> +  Iterator = DriverEntry->Depex;
> +
> +  while (TRUE) {
> +    //
> +    // Check to see if we are attempting to fetch dependency expression instructions
> +    // past the end of the dependency expression.
> +    //
> +    if (((UINTN)Iterator - (UINTN)DriverEntry->Depex) >= DriverEntry->DepexSize) {
> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Attempt to fetch past end of depex)\n"));
> +      return FALSE;
> +    }
> +
> +    //
> +    // Look at the opcode of the dependency expression instruction.
> +    //
> +    switch (*Iterator) {
> +    case EFI_DEP_BEFORE:
> +    case EFI_DEP_AFTER:
> +      //
> +      // For a well-formed Dependency Expression, the code should never get here.
> +      // The BEFORE and AFTER are processed prior to this routine's invocation.
> +      // If the code flow arrives at this point, there was a BEFORE or AFTER
> +      // that were not the first opcodes.
> +      //
> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));
> +      ASSERT (FALSE);
> +
> +    case EFI_DEP_PUSH:
> +      //
> +      // Push operator is followed by a GUID. Test to see if the GUID protocol
> +      // is installed and push the boolean result on the stack.
> +      //
> +      CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));
> +
> +      Status = MmLocateProtocol (&DriverGuid, NULL, &Interface);
> +      if (EFI_ERROR (Status) && (mEfiSystemTable != NULL)) {
> +        //
> +        // For MM Driver, it may depend on uefi protocols
> +        //
> +        Status = mEfiSystemTable->BootServices->LocateProtocol (&DriverGuid, NULL, &Interface);
> +      }
> +
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  PUSH GUID(%g) = FALSE\n", &DriverGuid));
> +        Status = PushBool (FALSE);
> +      } else {
> +        DEBUG ((DEBUG_DISPATCH, "  PUSH GUID(%g) = TRUE\n", &DriverGuid));
> +        *Iterator = EFI_DEP_REPLACE_TRUE;
> +        Status = PushBool (TRUE);
> +      }
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +
> +      Iterator += sizeof (EFI_GUID);
> +      break;
> +
> +    case EFI_DEP_AND:
> +      DEBUG ((DEBUG_DISPATCH, "  AND\n"));
> +      Status = PopBool (&Operator);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +
> +      Status = PopBool (&Operator2);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +
> +      Status = PushBool ((BOOLEAN)(Operator && Operator2));
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +      break;
> +
> +    case EFI_DEP_OR:
> +      DEBUG ((DEBUG_DISPATCH, "  OR\n"));
> +      Status = PopBool (&Operator);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +
> +      Status = PopBool (&Operator2);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +
> +      Status = PushBool ((BOOLEAN)(Operator || Operator2));
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +      break;
> +
> +    case EFI_DEP_NOT:
> +      DEBUG ((DEBUG_DISPATCH, "  NOT\n"));
> +      Status = PopBool (&Operator);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +
> +      Status = PushBool ((BOOLEAN)(!Operator));
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +      break;
> +
> +    case EFI_DEP_TRUE:
> +      DEBUG ((DEBUG_DISPATCH, "  TRUE\n"));
> +      Status = PushBool (TRUE);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +      break;
> +
> +    case EFI_DEP_FALSE:
> +      DEBUG ((DEBUG_DISPATCH, "  FALSE\n"));
> +      Status = PushBool (FALSE);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +      break;
> +
> +    case EFI_DEP_END:
> +      DEBUG ((DEBUG_DISPATCH, "  END\n"));
> +      Status = PopBool (&Operator);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = %a\n", Operator ? "TRUE" : "FALSE"));
> +      return Operator;
> +
> +    case EFI_DEP_REPLACE_TRUE:
> +      CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));
> +      DEBUG ((DEBUG_DISPATCH, "  PUSH GUID(%g) = TRUE\n", &DriverGuid));
> +      Status = PushBool (TRUE);
> +      if (EFI_ERROR (Status)) {
> +        DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unexpected error)\n"));
> +        return FALSE;
> +      }
> +
> +      Iterator += sizeof (EFI_GUID);
> +      break;
> +
> +    default:
> +      DEBUG ((DEBUG_DISPATCH, "  RESULT = FALSE (Unknown opcode)\n"));
> +      goto Done;
> +    }
> +
> +    //
> +    // Skip over the Dependency Op Code we just processed in the switch.
> +    // The math is done out of order, but it should not matter. That is
> +    // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
> +    // This is not an issue, since we just need the correct end result. You
> +    // need to be careful using Iterator in the loop as it's intermediate value
> +    // may be strange.
> +    //
> +    Iterator++;
> +  }
> +
> +Done:
> +  return FALSE;
> +}
> diff --git a/StandaloneMmPkg/Core/Dispatcher.c b/StandaloneMmPkg/Core/Dispatcher.c
> new file mode 100644
> index 0000000000..af18fa7eaa
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Dispatcher.c
> @@ -0,0 +1,1071 @@
> +/** @file
> +  MM Driver Dispatcher.
> +
> +  Step #1 - When a FV protocol is added to the system every driver in the FV
> +            is added to the mDiscoveredList. The Before, and After Depex are
> +            pre-processed as drivers are added to the mDiscoveredList. If an Apriori
> +            file exists in the FV those drivers are addeded to the
> +            mScheduledQueue. The mFvHandleList is used to make sure a
> +            FV is only processed once.
> +
> +  Step #2 - Dispatch. Remove driver from the mScheduledQueue and load and
> +            start it. After mScheduledQueue is drained check the
> +            mDiscoveredList to see if any item has a Depex that is ready to
> +            be placed on the mScheduledQueue.
> +
> +  Step #3 - Adding to the mScheduledQueue requires that you process Before
> +            and After dependencies. This is done recursively as the call to add
> +            to the mScheduledQueue checks for Before and recursively adds
> +            all Befores. It then addes the item that was passed in and then
> +            processess the After dependecies by recursively calling the routine.
> +
> +  Dispatcher Rules:
> +  The rules for the dispatcher are similar to the DXE dispatcher.
> +
> +  The rules for DXE dispatcher are in chapter 10 of the DXE CIS. Figure 10-3
> +  is the state diagram for the DXE dispatcher
> +
> +  Depex - Dependency Expresion.
> +
> +  Copyright (c) 2014, Hewlett-Packard Development Company, L.P.
> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +//
> +// MM Dispatcher Data structures
> +//
> +#define KNOWN_HANDLE_SIGNATURE  SIGNATURE_32('k','n','o','w')
> +typedef struct {
> +  UINTN           Signature;
> +  LIST_ENTRY      Link;         // mFvHandleList
> +  EFI_HANDLE      Handle;
> +} KNOWN_HANDLE;
> +
> +//
> +// Function Prototypes
> +//
> +
> +EFI_STATUS
> +MmCoreFfsFindMmDriver (
> +  IN  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader
> +  );
> +
> +/**
> +  Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
> +  must add any driver with a before dependency on InsertedDriverEntry first.
> +  You do this by recursively calling this routine. After all the Befores are
> +  processed you can add InsertedDriverEntry to the mScheduledQueue.
> +  Then you can add any driver with an After dependency on InsertedDriverEntry
> +  by recursively calling this routine.
> +
> +  @param  InsertedDriverEntry   The driver to insert on the ScheduledLink Queue
> +
> +**/
> +VOID
> +MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
> +  IN  EFI_MM_DRIVER_ENTRY   *InsertedDriverEntry
> +  );
> +
> +//
> +// The Driver List contains one copy of every driver that has been discovered.
> +// Items are never removed from the driver list. List of EFI_MM_DRIVER_ENTRY
> +//
> +LIST_ENTRY  mDiscoveredList = INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList);
> +
> +//
> +// Queue of drivers that are ready to dispatch. This queue is a subset of the
> +// mDiscoveredList.list of EFI_MM_DRIVER_ENTRY.
> +//
> +LIST_ENTRY  mScheduledQueue = INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue);
> +
> +//
> +// List of handles who's Fv's have been parsed and added to the mFwDriverList.
> +//
> +LIST_ENTRY  mFvHandleList = INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList);
> +
> +//
> +// Flag for the MM Dispacher.  TRUE if dispatcher is execuing.
> +//
> +BOOLEAN  gDispatcherRunning = FALSE;
> +
> +//
> +// Flag for the MM Dispacher.  TRUE if there is one or more MM drivers ready to be dispatched
> +//
> +BOOLEAN  gRequestDispatch = FALSE;
> +
> +//
> +// The global variable is defined for Loading modules at fixed address feature to track the MM code
> +// memory range usage. It is a bit mapped array in which every bit indicates the correspoding
> +// memory page available or not.
> +//
> +GLOBAL_REMOVE_IF_UNREFERENCED    UINT64                *mMmCodeMemoryRangeUsageBitMap=NULL;
> +
> +/**
> +  To check memory usage bit map array to figure out if the memory range in which the image will be loaded is available or not. If
> +  memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.
> +  The function is only invoked when load modules at fixed address feature is enabled.
> +
> +  @param  ImageBase                The base addres the image will be loaded at.
> +  @param  ImageSize                The size of the image
> +
> +  @retval EFI_SUCCESS              The memory range the image will be loaded in is available
> +  @retval EFI_NOT_FOUND            The memory range the image will be loaded in is not available
> +**/
> +EFI_STATUS
> +CheckAndMarkFixLoadingMemoryUsageBitMap (
> +  IN  EFI_PHYSICAL_ADDRESS          ImageBase,
> +  IN  UINTN                         ImageSize
> +  )
> +{
> +   UINT32                             MmCodePageNumber;
> +   UINT64                             MmCodeSize;
> +   EFI_PHYSICAL_ADDRESS               MmCodeBase;
> +   UINTN                              BaseOffsetPageNumber;
> +   UINTN                              TopOffsetPageNumber;
> +   UINTN                              Index;
> +   //
> +   // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressMmCodePageNumber
> +   //
> +   MmCodePageNumber = 0;
> +   MmCodeSize = EFI_PAGES_TO_SIZE (MmCodePageNumber);
> +   MmCodeBase = gLoadModuleAtFixAddressMmramBase;
> +
> +   //
> +   // If the memory usage bit map is not initialized,  do it. Every bit in the array
> +   // indicate the status of the corresponding memory page, available or not
> +   //
> +   if (mMmCodeMemoryRangeUsageBitMap == NULL) {
> +     mMmCodeMemoryRangeUsageBitMap = AllocateZeroPool(((MmCodePageNumber / 64) + 1)*sizeof(UINT64));
> +   }
> +   //
> +   // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
> +   //
> +   if (mMmCodeMemoryRangeUsageBitMap == NULL) {
> +     return EFI_NOT_FOUND;
> +   }
> +   //
> +   // see if the memory range for loading the image is in the MM code range.
> +   //
> +   if (MmCodeBase + MmCodeSize <  ImageBase + ImageSize || MmCodeBase >  ImageBase) {
> +     return EFI_NOT_FOUND;
> +   }
> +   //
> +   // Test if the memory is avalaible or not.
> +   //
> +   BaseOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase - MmCodeBase));
> +   TopOffsetPageNumber  = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - MmCodeBase));
> +   for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
> +     if ((mMmCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {
> +       //
> +       // This page is already used.
> +       //
> +       return EFI_NOT_FOUND;
> +     }
> +   }
> +
> +   //
> +   // Being here means the memory range is available.  So mark the bits for the memory range
> +   //
> +   for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
> +     mMmCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));
> +   }
> +   return  EFI_SUCCESS;
> +}
> +/**
> +  Get the fixed loading address from image header assigned by build tool. This function only be called
> +  when Loading module at Fixed address feature enabled.
> +
> +  @param  ImageContext              Pointer to the image context structure that describes the PE/COFF
> +                                    image that needs to be examined by this function.
> +  @retval EFI_SUCCESS               An fixed loading address is assigned to this image by build tools .
> +  @retval EFI_NOT_FOUND             The image has no assigned fixed loadding address.
> +
> +**/
> +EFI_STATUS
> +GetPeCoffImageFixLoadingAssignedAddress(
> +  IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext
> +  )
> +{
> +  UINTN                              SectionHeaderOffset;
> +  EFI_STATUS                         Status;
> +  EFI_IMAGE_SECTION_HEADER           SectionHeader;
> +  EFI_IMAGE_OPTIONAL_HEADER_UNION    *ImgHdr;
> +  EFI_PHYSICAL_ADDRESS               FixLoadingAddress;
> +  UINT16                             Index;
> +  UINTN                              Size;
> +  UINT16                             NumberOfSections;
> +  UINT64                             ValueInSectionHeader;
> +
> +  FixLoadingAddress = 0;
> +  Status = EFI_NOT_FOUND;
> +
> +  //
> +  // Get PeHeader pointer
> +  //
> +  ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
> +  SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +
> +                        sizeof (UINT32) +
> +                        sizeof (EFI_IMAGE_FILE_HEADER) +
> +                        ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;
> +  NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
> +
> +  //
> +  // Get base address from the first section header that doesn't point to code section.
> +  //
> +  for (Index = 0; Index < NumberOfSections; Index++) {
> +    //
> +    // Read section header from file
> +    //
> +    Size = sizeof (EFI_IMAGE_SECTION_HEADER);
> +    Status = ImageContext->ImageRead (
> +                              ImageContext->Handle,
> +                              SectionHeaderOffset,
> +                              &Size,
> +                              &SectionHeader
> +                              );
> +    if (EFI_ERROR (Status)) {
> +      return Status;
> +    }
> +
> +    Status = EFI_NOT_FOUND;
> +
> +    if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
> +      //
> +      // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
> +      // that doesn't point to code section in image header.So there is an assumption that when the feature is enabled,
> +      // if a module with a loading address assigned by tools, the PointerToRelocations & PointerToLineNumbers fields
> +      // should not be Zero, or else, these 2 fields should be set to Zero
> +      //
> +      ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);
> +      if (ValueInSectionHeader != 0) {
> +        //
> +        // Found first section header that doesn't point to code section in which build tool saves the
> +        // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
> +        //
> +        FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressMmramBase + (INT64)ValueInSectionHeader);
> +        //
> +        // Check if the memory range is available.
> +        //
> +        Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));
> +        if (!EFI_ERROR(Status)) {
> +          //
> +          // The assigned address is valid. Return the specified loading address
> +          //
> +          ImageContext->ImageAddress = FixLoadingAddress;
> +        }
> +      }
> +      break;
> +    }
> +    SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
> +  }
> +  DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n", FixLoadingAddress, Status));
> +  return Status;
> +}
> +/**
> +  Loads an EFI image into SMRAM.
> +
> +  @param  DriverEntry             EFI_MM_DRIVER_ENTRY instance
> +
> +  @return EFI_STATUS
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLoadImage (
> +  IN OUT EFI_MM_DRIVER_ENTRY  *DriverEntry
> +  )
> +{
> +  VOID                           *Buffer;
> +  UINTN                          PageCount;
> +  EFI_STATUS                     Status;
> +  EFI_PHYSICAL_ADDRESS           DstBuffer;
> +  PE_COFF_LOADER_IMAGE_CONTEXT   ImageContext;
> +
> +  DEBUG ((DEBUG_INFO, "MmLoadImage - %g\n", &DriverEntry->FileName));
> +
> +  Buffer = AllocateCopyPool (DriverEntry->Pe32DataSize, DriverEntry->Pe32Data);
> +  if (Buffer == NULL) {
> +    return EFI_OUT_OF_RESOURCES;
> +  }
> +
> +  Status               = EFI_SUCCESS;
> +
> +  //
> +  // Initialize ImageContext
> +  //
> +  ImageContext.Handle = Buffer;
> +  ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
> +
> +  //
> +  // Get information about the image being loaded
> +  //
> +  Status = PeCoffLoaderGetImageInfo (&ImageContext);
> +  if (EFI_ERROR (Status)) {
> +    if (Buffer != NULL) {
> +      MmFreePool (Buffer);
> +    }
> +    return Status;
> +  }
> +
> +  PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
> +  DstBuffer = (UINTN)(-1);
> +
> +  Status = MmAllocatePages (
> +              AllocateMaxAddress,
> +              EfiRuntimeServicesCode,
> +              PageCount,
> +              &DstBuffer
> +              );
> +  if (EFI_ERROR (Status)) {
> +    if (Buffer != NULL) {
> +      MmFreePool (Buffer);
> +    }
> +    return Status;
> +  }
> +
> +  ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;
> +
> +  //
> +  // Align buffer on section boundry
> +  //
> +  ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
> +  ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)(ImageContext.SectionAlignment - 1));
> +
> +  //
> +  // Load the image to our new buffer
> +  //
> +  Status = PeCoffLoaderLoadImage (&ImageContext);
> +  if (EFI_ERROR (Status)) {
> +    if (Buffer != NULL) {
> +      MmFreePool (Buffer);
> +    }
> +    MmFreePages (DstBuffer, PageCount);
> +    return Status;
> +  }
> +
> +  //
> +  // Relocate the image in our new buffer
> +  //
> +  Status = PeCoffLoaderRelocateImage (&ImageContext);
> +  if (EFI_ERROR (Status)) {
> +    if (Buffer != NULL) {
> +      MmFreePool (Buffer);
> +    }
> +    MmFreePages (DstBuffer, PageCount);
> +    return Status;
> +  }
> +
> +  //
> +  // Flush the instruction cache so the image data are written before we execute it
> +  //
> +  InvalidateInstructionCacheRange ((VOID *)(UINTN) ImageContext.ImageAddress, (UINTN) ImageContext.ImageSize);
> +
> +  //
> +  // Save Image EntryPoint in DriverEntry
> +  //
> +  DriverEntry->ImageEntryPoint  = ImageContext.EntryPoint;
> +  DriverEntry->ImageBuffer      = DstBuffer;
> +  DriverEntry->NumberOfPage     = PageCount;
> +
> +  if (mEfiSystemTable != NULL) {
> +    Status = mEfiSystemTable->BootServices->AllocatePool (EfiBootServicesData, sizeof (EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&DriverEntry->LoadedImage);
> +    if (EFI_ERROR (Status)) {
> +      if (Buffer != NULL) {
> +        MmFreePool (Buffer);
> +      }
> +      MmFreePages (DstBuffer, PageCount);
> +      return Status;
> +    }
> +
> +    ZeroMem (DriverEntry->LoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));
> +    //
> +    // Fill in the remaining fields of the Loaded Image Protocol instance.
> +    // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
> +    //
> +    DriverEntry->LoadedImage->Revision      = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
> +    DriverEntry->LoadedImage->ParentHandle  = NULL;
> +    DriverEntry->LoadedImage->SystemTable   = mEfiSystemTable;
> +    DriverEntry->LoadedImage->DeviceHandle  = NULL;
> +    DriverEntry->LoadedImage->FilePath      = NULL;
> +
> +    DriverEntry->LoadedImage->ImageBase     = (VOID *)(UINTN)DriverEntry->ImageBuffer;
> +    DriverEntry->LoadedImage->ImageSize     = ImageContext.ImageSize;
> +    DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;
> +    DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;
> +
> +    //
> +    // Create a new image handle in the UEFI handle database for the MM Driver
> +    //
> +    DriverEntry->ImageHandle = NULL;
> +    Status = mEfiSystemTable->BootServices->InstallMultipleProtocolInterfaces (
> +                    &DriverEntry->ImageHandle,
> +                    &gEfiLoadedImageProtocolGuid, DriverEntry->LoadedImage,
> +                    NULL
> +                    );
> +  }
> +
> +  //
> +  // Print the load address and the PDB file name if it is available
> +  //
> +
> +  DEBUG_CODE_BEGIN ();
> +
> +    UINTN Index;
> +    UINTN StartIndex;
> +    CHAR8 EfiFileName[256];
> +
> +
> +    DEBUG ((DEBUG_INFO | DEBUG_LOAD,
> +           "Loading MM driver at 0x%11p EntryPoint=0x%11p ",
> +           (VOID *)(UINTN) ImageContext.ImageAddress,
> +           FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));
> +
> +
> +    //
> +    // Print Module Name by Pdb file path.
> +    // Windows and Unix style file path are all trimmed correctly.
> +    //
> +    if (ImageContext.PdbPointer != NULL) {
> +      StartIndex = 0;
> +      for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {
> +        if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {
> +          StartIndex = Index + 1;
> +        }
> +      }
> +      //
> +      // Copy the PDB file name to our temporary string, and replace .pdb with .efi
> +      // The PDB file name is limited in the range of 0~255.
> +      // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
> +      //
> +      for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {
> +        EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];
> +        if (EfiFileName[Index] == 0) {
> +          EfiFileName[Index] = '.';
> +        }
> +        if (EfiFileName[Index] == '.') {
> +          EfiFileName[Index + 1] = 'e';
> +          EfiFileName[Index + 2] = 'f';
> +          EfiFileName[Index + 3] = 'i';
> +          EfiFileName[Index + 4] = 0;
> +          break;
> +        }
> +      }
> +
> +      if (Index == sizeof (EfiFileName) - 4) {
> +        EfiFileName[Index] = 0;
> +      }
> +      DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));
> +    }
> +    DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));
> +
> +  DEBUG_CODE_END ();
> +
> +  //
> +  // Free buffer allocated by Fv->ReadSection.
> +  //
> +  // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
> +  // used the UEFI Boot Services AllocatePool() function
> +  //
> +  MmFreePool(Buffer);
> +  return Status;
> +}
> +
> +/**
> +  Preprocess dependency expression and update DriverEntry to reflect the
> +  state of  Before and After dependencies. If DriverEntry->Before
> +  or DriverEntry->After is set it will never be cleared.
> +
> +  @param  DriverEntry           DriverEntry element to update .
> +
> +  @retval EFI_SUCCESS           It always works.
> +
> +**/
> +EFI_STATUS
> +MmPreProcessDepex (
> +  IN EFI_MM_DRIVER_ENTRY  *DriverEntry
> +  )
> +{
> +  UINT8  *Iterator;
> +
> +  Iterator = DriverEntry->Depex;
> +  DriverEntry->Dependent = TRUE;
> +
> +  if (*Iterator == EFI_DEP_BEFORE) {
> +    DriverEntry->Before = TRUE;
> +  } else if (*Iterator == EFI_DEP_AFTER) {
> +    DriverEntry->After = TRUE;
> +  }
> +
> +  if (DriverEntry->Before || DriverEntry->After) {
> +    CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));
> +  }
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Read Depex and pre-process the Depex for Before and After. If Section Extraction
> +  protocol returns an error via ReadSection defer the reading of the Depex.
> +
> +  @param  DriverEntry           Driver to work on.
> +
> +  @retval EFI_SUCCESS           Depex read and preprossesed
> +  @retval EFI_PROTOCOL_ERROR    The section extraction protocol returned an error
> +                                and  Depex reading needs to be retried.
> +  @retval Error                 DEPEX not found.
> +
> +**/
> +EFI_STATUS
> +MmGetDepexSectionAndPreProccess (
> +  IN EFI_MM_DRIVER_ENTRY  *DriverEntry
> +  )
> +{
> +  EFI_STATUS                     Status;
> +
> +  //
> +  // Data already read
> +  //
> +  if (DriverEntry->Depex == NULL) {
> +    Status = EFI_NOT_FOUND;
> +  } else {
> +    Status = EFI_SUCCESS;
> +  }
> +  if (EFI_ERROR (Status)) {
> +    if (Status == EFI_PROTOCOL_ERROR) {
> +      //
> +      // The section extraction protocol failed so set protocol error flag
> +      //
> +      DriverEntry->DepexProtocolError = TRUE;
> +    } else {
> +      //
> +      // If no Depex assume depend on all architectural protocols
> +      //
> +      DriverEntry->Depex = NULL;
> +      DriverEntry->Dependent = TRUE;
> +      DriverEntry->DepexProtocolError = FALSE;
> +    }
> +  } else {
> +    //
> +    // Set Before and After state information based on Depex
> +    // Driver will be put in Dependent state
> +    //
> +    MmPreProcessDepex (DriverEntry);
> +    DriverEntry->DepexProtocolError = FALSE;
> +  }
> +
> +  return Status;
> +}
> +
> +/**
> +  This is the main Dispatcher for MM and it exits when there are no more
> +  drivers to run. Drain the mScheduledQueue and load and start a PE
> +  image for each driver. Search the mDiscoveredList to see if any driver can
> +  be placed on the mScheduledQueue. If no drivers are placed on the
> +  mScheduledQueue exit the function.
> +
> +  @retval EFI_SUCCESS           All of the MM Drivers that could be dispatched
> +                                have been run and the MM Entry Point has been
> +                                registered.
> +  @retval EFI_NOT_READY         The MM Driver that registered the MM Entry Point
> +                                was just dispatched.
> +  @retval EFI_NOT_FOUND         There are no MM Drivers available to be dispatched.
> +  @retval EFI_ALREADY_STARTED   The MM Dispatcher is already running
> +
> +**/
> +EFI_STATUS
> +MmDispatcher (
> +  VOID
> +  )
> +{
> +  EFI_STATUS            Status;
> +  LIST_ENTRY            *Link;
> +  EFI_MM_DRIVER_ENTRY  *DriverEntry;
> +  BOOLEAN               ReadyToRun;
> +  BOOLEAN               PreviousMmEntryPointRegistered;
> +
> +  DEBUG ((DEBUG_INFO, "MmDispatcher\n"));
> +
> +  if (!gRequestDispatch) {
> +    DEBUG ((DEBUG_INFO, "  !gRequestDispatch\n"));
> +    return EFI_NOT_FOUND;
> +  }
> +
> +  if (gDispatcherRunning) {
> +    DEBUG ((DEBUG_INFO, "  gDispatcherRunning\n"));
> +    //
> +    // If the dispatcher is running don't let it be restarted.
> +    //
> +    return EFI_ALREADY_STARTED;
> +  }
> +
> +  gDispatcherRunning = TRUE;
> +
> +  do {
> +    //
> +    // Drain the Scheduled Queue
> +    //
> +    DEBUG ((DEBUG_INFO, "  Drain the Scheduled Queue\n"));
> +    while (!IsListEmpty (&mScheduledQueue)) {
> +      DriverEntry = CR (
> +                      mScheduledQueue.ForwardLink,
> +                      EFI_MM_DRIVER_ENTRY,
> +                      ScheduledLink,
> +                      EFI_MM_DRIVER_ENTRY_SIGNATURE
> +                      );
> +      DEBUG ((DEBUG_INFO, "  DriverEntry (Scheduled) - %g\n", &DriverEntry->FileName));
> +
> +      //
> +      // Load the MM Driver image into memory. If the Driver was transitioned from
> +      // Untrused to Scheduled it would have already been loaded so we may need to
> +      // skip the LoadImage
> +      //
> +      if (DriverEntry->ImageHandle == NULL) {
> +        Status = MmLoadImage (DriverEntry);
> +
> +        //
> +        // Update the driver state to reflect that it's been loaded
> +        //
> +        if (EFI_ERROR (Status)) {
> +          //
> +          // The MM Driver could not be loaded, and do not attempt to load or start it again.
> +          // Take driver from Scheduled to Initialized.
> +          //
> +          DriverEntry->Initialized  = TRUE;
> +          DriverEntry->Scheduled = FALSE;
> +          RemoveEntryList (&DriverEntry->ScheduledLink);
> +
> +          //
> +          // If it's an error don't try the StartImage
> +          //
> +          continue;
> +        }
> +      }
> +
> +      DriverEntry->Scheduled    = FALSE;
> +      DriverEntry->Initialized  = TRUE;
> +      RemoveEntryList (&DriverEntry->ScheduledLink);
> +
> +      /*REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
> +        EFI_PROGRESS_CODE,
> +        EFI_SOFTWARE_MM_DRIVER | EFI_SW_PC_INIT_BEGIN,
> +        &DriverEntry->ImageHandle,
> +        sizeof (DriverEntry->ImageHandle)
> +        );*/
> +
> +      //
> +      // Cache state of MmEntryPointRegistered before calling entry point
> +      //
> +      PreviousMmEntryPointRegistered = gMmCorePrivate->MmEntryPointRegistered;
> +
> +      //
> +      // For each MM driver, pass NULL as ImageHandle
> +      //
> +      if (mEfiSystemTable == NULL) {
> +        DEBUG ((DEBUG_INFO, "StartImage - 0x%x (Standalone Mode)\n", DriverEntry->ImageEntryPoint));
> +        Status = ((MM_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, &gMmCoreMmst);
> +      } else {
> +        DEBUG ((DEBUG_INFO, "StartImage - 0x%x (Tradition Mode)\n", DriverEntry->ImageEntryPoint));
> +        Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, mEfiSystemTable);
> +      }
> +      if (EFI_ERROR(Status)){
> +        DEBUG ((DEBUG_INFO, "StartImage Status - %r\n", Status));
> +        MmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);
> +      }
> +
> +      /*REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
> +        EFI_PROGRESS_CODE,
> +        EFI_SOFTWARE_MM_DRIVER | EFI_SW_PC_INIT_END,
> +        &DriverEntry->ImageHandle,
> +        sizeof (DriverEntry->ImageHandle)
> +        );*/
> +
> +      if (!PreviousMmEntryPointRegistered && gMmCorePrivate->MmEntryPointRegistered) {
> +        //
> +        // Return immediately if the MM Entry Point was registered by the MM
> +        // Driver that was just dispatched.  The MM IPL will reinvoke the MM
> +        // Core Dispatcher.  This is required so MM Mode may be enabled as soon
> +        // as all the dependent MM Drivers for MM Mode have been dispatched.
> +        // Once the MM Entry Point has been registered, then MM Mode will be
> +        // used.
> +        //
> +        gRequestDispatch = TRUE;
> +        gDispatcherRunning = FALSE;
> +        return EFI_NOT_READY;
> +      }
> +    }
> +
> +    //
> +    // Search DriverList for items to place on Scheduled Queue
> +    //
> +    DEBUG ((DEBUG_INFO, "  Search DriverList for items to place on Scheduled Queue\n"));
> +    ReadyToRun = FALSE;
> +    for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
> +      DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
> +      DEBUG ((DEBUG_INFO, "  DriverEntry (Discovered) - %g\n", &DriverEntry->FileName));
> +
> +      if (DriverEntry->DepexProtocolError){
> +        //
> +        // If Section Extraction Protocol did not let the Depex be read before retry the read
> +        //
> +        Status = MmGetDepexSectionAndPreProccess (DriverEntry);
> +      }
> +
> +      if (DriverEntry->Dependent) {
> +        if (MmIsSchedulable (DriverEntry)) {
> +          MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
> +          ReadyToRun = TRUE;
> +        }
> +      }
> +    }
> +  } while (ReadyToRun);
> +
> +  //
> +  // If there is no more MM driver to dispatch, stop the dispatch request
> +  //
> +  DEBUG ((DEBUG_INFO, "  no more MM driver to dispatch, stop the dispatch request\n"));
> +  gRequestDispatch = FALSE;
> +  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
> +    DriverEntry = CR (Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
> +    DEBUG ((DEBUG_INFO, "  DriverEntry (Discovered) - %g\n", &DriverEntry->FileName));
> +
> +    if (!DriverEntry->Initialized){
> +      //
> +      // We have MM driver pending to dispatch
> +      //
> +      gRequestDispatch = TRUE;
> +      break;
> +    }
> +  }
> +
> +  gDispatcherRunning = FALSE;
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
> +  must add any driver with a before dependency on InsertedDriverEntry first.
> +  You do this by recursively calling this routine. After all the Befores are
> +  processed you can add InsertedDriverEntry to the mScheduledQueue.
> +  Then you can add any driver with an After dependency on InsertedDriverEntry
> +  by recursively calling this routine.
> +
> +  @param  InsertedDriverEntry   The driver to insert on the ScheduledLink Queue
> +
> +**/
> +VOID
> +MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
> +  IN  EFI_MM_DRIVER_ENTRY   *InsertedDriverEntry
> +  )
> +{
> +  LIST_ENTRY            *Link;
> +  EFI_MM_DRIVER_ENTRY *DriverEntry;
> +
> +  //
> +  // Process Before Dependency
> +  //
> +  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
> +    DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
> +    if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
> +      DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
> +      DEBUG ((DEBUG_DISPATCH, "  BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
> +      if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
> +        //
> +        // Recursively process BEFORE
> +        //
> +        DEBUG ((DEBUG_DISPATCH, "TRUE\n  END\n  RESULT = TRUE\n"));
> +        MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
> +      } else {
> +        DEBUG ((DEBUG_DISPATCH, "FALSE\n  END\n  RESULT = FALSE\n"));
> +      }
> +    }
> +  }
> +
> +  //
> +  // Convert driver from Dependent to Scheduled state
> +  //
> +
> +  InsertedDriverEntry->Dependent = FALSE;
> +  InsertedDriverEntry->Scheduled = TRUE;
> +  InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);
> +
> +
> +  //
> +  // Process After Dependency
> +  //
> +  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
> +    DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
> +    if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
> +      DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
> +      DEBUG ((DEBUG_DISPATCH, "  AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
> +      if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
> +        //
> +        // Recursively process AFTER
> +        //
> +        DEBUG ((DEBUG_DISPATCH, "TRUE\n  END\n  RESULT = TRUE\n"));
> +        MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
> +      } else {
> +        DEBUG ((DEBUG_DISPATCH, "FALSE\n  END\n  RESULT = FALSE\n"));
> +      }
> +    }
> +  }
> +}
> +
> +/**
> +  Return TRUE if the Fv has been processed, FALSE if not.
> +
> +  @param  FvHandle              The handle of a FV that's being tested
> +
> +  @retval TRUE                  Fv protocol on FvHandle has been processed
> +  @retval FALSE                 Fv protocol on FvHandle has not yet been
> +                                processed
> +
> +**/
> +BOOLEAN
> +FvHasBeenProcessed (
> +  IN EFI_HANDLE  FvHandle
> +  )
> +{
> +  LIST_ENTRY    *Link;
> +  KNOWN_HANDLE  *KnownHandle;
> +
> +  for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {
> +    KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);
> +    if (KnownHandle->Handle == FvHandle) {
> +      return TRUE;
> +    }
> +  }
> +  return FALSE;
> +}
> +
> +/**
> +  Remember that Fv protocol on FvHandle has had it's drivers placed on the
> +  mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are
> +  never removed/freed from the mFvHandleList.
> +
> +  @param  FvHandle              The handle of a FV that has been processed
> +
> +**/
> +VOID
> +FvIsBeingProcesssed (
> +  IN EFI_HANDLE  FvHandle
> +  )
> +{
> +  KNOWN_HANDLE  *KnownHandle;
> +
> +  DEBUG ((DEBUG_INFO, "FvIsBeingProcesssed - 0x%08x\n", FvHandle));
> +
> +  KnownHandle = AllocatePool (sizeof (KNOWN_HANDLE));
> +  ASSERT (KnownHandle != NULL);
> +
> +  KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;
> +  KnownHandle->Handle = FvHandle;
> +  InsertTailList (&mFvHandleList, &KnownHandle->Link);
> +}
> +
> +/**
> +  Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
> +  and initilize any state variables. Read the Depex from the FV and store it
> +  in DriverEntry. Pre-process the Depex to set the Before and After state.
> +  The Discovered list is never free'ed and contains booleans that represent the
> +  other possible MM driver states.
> +
> +  @param  Fv                    Fv protocol, needed to read Depex info out of
> +                                FLASH.
> +  @param  FvHandle              Handle for Fv, needed in the
> +                                EFI_MM_DRIVER_ENTRY so that the PE image can be
> +                                read out of the FV at a later time.
> +  @param  DriverName            Name of driver to add to mDiscoveredList.
> +
> +  @retval EFI_SUCCESS           If driver was added to the mDiscoveredList.
> +  @retval EFI_ALREADY_STARTED   The driver has already been started. Only one
> +                                DriverName may be active in the system at any one
> +                                time.
> +
> +**/
> +EFI_STATUS
> +MmAddToDriverList (
> +  IN EFI_HANDLE   FvHandle,
> +  IN VOID         *Pe32Data,
> +  IN UINTN        Pe32DataSize,
> +  IN VOID         *Depex,
> +  IN UINTN        DepexSize,
> +  IN EFI_GUID     *DriverName
> +  )
> +{
> +  EFI_MM_DRIVER_ENTRY  *DriverEntry;
> +
> +  DEBUG ((DEBUG_INFO, "MmAddToDriverList - %g (0x%08x)\n", DriverName, Pe32Data));
> +
> +  //
> +  // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
> +  // NULL or FALSE.
> +  //
> +  DriverEntry = AllocateZeroPool (sizeof (EFI_MM_DRIVER_ENTRY));
> +  ASSERT (DriverEntry != NULL);
> +
> +  DriverEntry->Signature        = EFI_MM_DRIVER_ENTRY_SIGNATURE;
> +  CopyGuid (&DriverEntry->FileName, DriverName);
> +  DriverEntry->FvHandle         = FvHandle;
> +  DriverEntry->Pe32Data         = Pe32Data;
> +  DriverEntry->Pe32DataSize     = Pe32DataSize;
> +  DriverEntry->Depex            = Depex;
> +  DriverEntry->DepexSize        = DepexSize;
> +
> +  MmGetDepexSectionAndPreProccess (DriverEntry);
> +
> +  InsertTailList (&mDiscoveredList, &DriverEntry->Link);
> +  gRequestDispatch = TRUE;
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  Event notification that is fired every time a FV dispatch protocol is added.
> +  More than one protocol may have been added when this event is fired, so you
> +  must loop on MmLocateHandle () to see how many protocols were added and
> +  do the following to each FV:
> +  If the Fv has already been processed, skip it. If the Fv has not been
> +  processed then mark it as being processed, as we are about to process it.
> +  Read the Fv and add any driver in the Fv to the mDiscoveredList.The
> +  mDiscoveredList is never free'ed and contains variables that define
> +  the other states the MM driver transitions to..
> +  While you are at it read the A Priori file into memory.
> +  Place drivers in the A Priori list onto the mScheduledQueue.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmDriverDispatchHandler (
> +  IN     EFI_HANDLE  DispatchHandle,
> +  IN     CONST VOID  *Context,        OPTIONAL
> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_STATUS                            Status;
> +
> +  DEBUG ((DEBUG_INFO, "MmDriverDispatchHandler\n"));
> +
> +  //
> +  // Execute the MM Dispatcher on any newly discovered FVs and previously
> +  // discovered MM drivers that have been discovered but not dispatched.
> +  //
> +  Status = MmDispatcher ();
> +
> +  //
> +  // Check to see if CommBuffer and CommBufferSize are valid
> +  //
> +  if (CommBuffer != NULL && CommBufferSize != NULL) {
> +    if (*CommBufferSize > 0) {
> +      if (Status == EFI_NOT_READY) {
> +        //
> +        // If a the MM Core Entry Point was just registered, then set flag to
> +        // request the MM Dispatcher to be restarted.
> +        //
> +        *(UINT8 *)CommBuffer = COMM_BUFFER_MM_DISPATCH_RESTART;
> +      } else if (!EFI_ERROR (Status)) {
> +        //
> +        // Set the flag to show that the MM Dispatcher executed without errors
> +        //
> +        *(UINT8 *)CommBuffer = COMM_BUFFER_MM_DISPATCH_SUCCESS;
> +      } else {
> +        //
> +        // Set the flag to show that the MM Dispatcher encountered an error
> +        //
> +        *(UINT8 *)CommBuffer = COMM_BUFFER_MM_DISPATCH_ERROR;
> +      }
> +    }
> +  }
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmFvDispatchHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_STATUS                            Status;
> +  EFI_MM_COMMUNICATE_FV_DISPATCH_DATA  *CommunicationFvDispatchData;
> +  EFI_FIRMWARE_VOLUME_HEADER            *FwVolHeader;
> +
> +  DEBUG ((DEBUG_INFO, "MmFvDispatchHandler\n"));
> +
> +  CommunicationFvDispatchData = CommBuffer;
> +
> +  DEBUG ((DEBUG_INFO, "  Dispatch - 0x%016lx - 0x%016lx\n", CommunicationFvDispatchData->Address, CommunicationFvDispatchData->Size));
> +
> +  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)CommunicationFvDispatchData->Address;
> +
> +  MmCoreFfsFindMmDriver (FwVolHeader);
> +
> +  //
> +  // Execute the MM Dispatcher on any newly discovered FVs and previously
> +  // discovered MM drivers that have been discovered but not dispatched.
> +  //
> +  Status = MmDispatcher ();
> +
> +  return Status;
> +}
> +
> +/**
> +  Traverse the discovered list for any drivers that were discovered but not loaded
> +  because the dependency experessions evaluated to false.
> +
> +**/
> +VOID
> +MmDisplayDiscoveredNotDispatched (
> +  VOID
> +  )
> +{
> +  LIST_ENTRY                   *Link;
> +  EFI_MM_DRIVER_ENTRY         *DriverEntry;
> +
> +  for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {
> +    DriverEntry = CR(Link, EFI_MM_DRIVER_ENTRY, Link, EFI_MM_DRIVER_ENTRY_SIGNATURE);
> +    if (DriverEntry->Dependent) {
> +      DEBUG ((DEBUG_LOAD, "MM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));
> +    }
> +  }
> +}
> diff --git a/StandaloneMmPkg/Core/FwVol.c b/StandaloneMmPkg/Core/FwVol.c
> new file mode 100644
> index 0000000000..901c58bc53
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/FwVol.c
> @@ -0,0 +1,104 @@
> +/**@file
> +
> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +#include <Library/FvLib.h>
> +
> +//
> +// List of file types supported by dispatcher
> +//
> +EFI_FV_FILETYPE mMmFileTypes[] = {
> +  EFI_FV_FILETYPE_MM,
> +  0xE, //EFI_FV_FILETYPE_MM_STANDALONE,
> +       //
> +       // Note: DXE core will process the FV image file, so skip it in MM core
> +       // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
> +       //
> +};
> +
> +EFI_STATUS
> +MmAddToDriverList (
> +  IN EFI_HANDLE   FvHandle,
> +  IN VOID         *Pe32Data,
> +  IN UINTN        Pe32DataSize,
> +  IN VOID         *Depex,
> +  IN UINTN        DepexSize,
> +  IN EFI_GUID     *DriverName
> +  );
> +
> +BOOLEAN
> +FvHasBeenProcessed (
> +  IN EFI_HANDLE  FvHandle
> +  );
> +
> +VOID
> +FvIsBeingProcesssed (
> +  IN EFI_HANDLE  FvHandle
> +  );
> +
> +EFI_STATUS
> +MmCoreFfsFindMmDriver (
> +  IN  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader
> +  )
> +/*++
> +
> +Routine Description:
> +  Given the pointer to the Firmware Volume Header find the
> +  MM driver and return it's PE32 image.
> +
> +Arguments:
> +  FwVolHeader - Pointer to memory mapped FV
> +
> +Returns:
> +  other       - Failure
> +
> +--*/
> +{
> +  EFI_STATUS          Status;
> +  EFI_STATUS          DepexStatus;
> +  EFI_FFS_FILE_HEADER *FileHeader;
> +  EFI_FV_FILETYPE     FileType;
> +  VOID                *Pe32Data;
> +  UINTN               Pe32DataSize;
> +  VOID                *Depex;
> +  UINTN               DepexSize;
> +  UINTN               Index;
> +
> +  DEBUG((DEBUG_INFO, "MmCoreFfsFindMmDriver - 0x%x\n", FwVolHeader));
> +
> +  if (FvHasBeenProcessed (FwVolHeader)) {
> +    return EFI_SUCCESS;
> +  }
> +
> +  FvIsBeingProcesssed (FwVolHeader);
> +
> +  for (Index = 0; Index < sizeof(mMmFileTypes) / sizeof(mMmFileTypes[0]); Index++) {
> +    DEBUG((DEBUG_INFO, "Check MmFileTypes - 0x%x\n", mMmFileTypes[Index]));
> +    FileType = mMmFileTypes[Index];
> +    FileHeader = NULL;
> +    do {
> +      Status = FfsFindNextFile(FileType, FwVolHeader, &FileHeader);
> +      if (!EFI_ERROR(Status)) {
> +        Status = FfsFindSectionData(EFI_SECTION_PE32, FileHeader, &Pe32Data, &Pe32DataSize);
> +        DEBUG((DEBUG_INFO, "Find PE data - 0x%x\n", Pe32Data));
> +        DepexStatus = FfsFindSectionData(EFI_SECTION_MM_DEPEX, FileHeader, &Depex, &DepexSize);
> +        if (!EFI_ERROR(DepexStatus)) {
> +          MmAddToDriverList(FwVolHeader, Pe32Data, Pe32DataSize, Depex, DepexSize, &FileHeader->Name);
> +        }
> +      }
> +    } while (!EFI_ERROR(Status));
> +  }
> +
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Core/Handle.c b/StandaloneMmPkg/Core/Handle.c
> new file mode 100644
> index 0000000000..01832f4bbe
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Handle.c
> @@ -0,0 +1,533 @@
> +/** @file
> +  SMM handle & protocol handling.
> +
> +  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +//
> +// mProtocolDatabase     - A list of all protocols in the system.  (simple list for now)
> +// gHandleList           - A list of all the handles in the system
> +//
> +LIST_ENTRY  mProtocolDatabase  = INITIALIZE_LIST_HEAD_VARIABLE (mProtocolDatabase);
> +LIST_ENTRY  gHandleList        = INITIALIZE_LIST_HEAD_VARIABLE (gHandleList);
> +
> +/**
> +  Check whether a handle is a valid EFI_HANDLE
> +
> +  @param  UserHandle             The handle to check
> +
> +  @retval EFI_INVALID_PARAMETER  The handle is NULL or not a valid EFI_HANDLE.
> +  @retval EFI_SUCCESS            The handle is valid EFI_HANDLE.
> +
> +**/
> +EFI_STATUS
> +MmValidateHandle (
> +  IN EFI_HANDLE  UserHandle
> +  )
> +{
> +  IHANDLE  *Handle;
> +
> +  Handle = (IHANDLE *)UserHandle;
> +  if (Handle == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +  if (Handle->Signature != EFI_HANDLE_SIGNATURE) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Finds the protocol entry for the requested protocol.
> +
> +  @param  Protocol               The ID of the protocol
> +  @param  Create                 Create a new entry if not found
> +
> +  @return Protocol entry
> +
> +**/
> +PROTOCOL_ENTRY  *
> +MmFindProtocolEntry (
> +  IN EFI_GUID   *Protocol,
> +  IN BOOLEAN    Create
> +  )
> +{
> +  LIST_ENTRY          *Link;
> +  PROTOCOL_ENTRY      *Item;
> +  PROTOCOL_ENTRY      *ProtEntry;
> +
> +  //
> +  // Search the database for the matching GUID
> +  //
> +
> +  ProtEntry = NULL;
> +  for (Link = mProtocolDatabase.ForwardLink;
> +       Link != &mProtocolDatabase;
> +       Link = Link->ForwardLink) {
> +
> +    Item = CR(Link, PROTOCOL_ENTRY, AllEntries, PROTOCOL_ENTRY_SIGNATURE);
> +    if (CompareGuid (&Item->ProtocolID, Protocol)) {
> +      //
> +      // This is the protocol entry
> +      //
> +      ProtEntry = Item;
> +      break;
> +    }
> +  }
> +
> +  //
> +  // If the protocol entry was not found and Create is TRUE, then
> +  // allocate a new entry
> +  //
> +  if ((ProtEntry == NULL) && Create) {
> +    ProtEntry = AllocatePool (sizeof(PROTOCOL_ENTRY));
> +    if (ProtEntry != NULL) {
> +      //
> +      // Initialize new protocol entry structure
> +      //
> +      ProtEntry->Signature = PROTOCOL_ENTRY_SIGNATURE;
> +      CopyGuid ((VOID *)&ProtEntry->ProtocolID, Protocol);
> +      InitializeListHead (&ProtEntry->Protocols);
> +      InitializeListHead (&ProtEntry->Notify);
> +
> +      //
> +      // Add it to protocol database
> +      //
> +      InsertTailList (&mProtocolDatabase, &ProtEntry->AllEntries);
> +    }
> +  }
> +  return ProtEntry;
> +}
> +
> +/**
> +  Finds the protocol instance for the requested handle and protocol.
> +  Note: This function doesn't do parameters checking, it's caller's responsibility
> +  to pass in valid parameters.
> +
> +  @param  Handle                 The handle to search the protocol on
> +  @param  Protocol               GUID of the protocol
> +  @param  Interface              The interface for the protocol being searched
> +
> +  @return Protocol instance (NULL: Not found)
> +
> +**/
> +PROTOCOL_INTERFACE *
> +MmFindProtocolInterface (
> +  IN IHANDLE   *Handle,
> +  IN EFI_GUID  *Protocol,
> +  IN VOID      *Interface
> +  )
> +{
> +  PROTOCOL_INTERFACE  *Prot;
> +  PROTOCOL_ENTRY      *ProtEntry;
> +  LIST_ENTRY          *Link;
> +
> +  Prot = NULL;
> +
> +  //
> +  // Lookup the protocol entry for this protocol ID
> +  //
> +  ProtEntry = MmFindProtocolEntry (Protocol, FALSE);
> +  if (ProtEntry != NULL) {
> +    //
> +    // Look at each protocol interface for any matches
> +    //
> +    for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link=Link->ForwardLink) {
> +      //
> +      // If this protocol interface matches, remove it
> +      //
> +      Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);
> +      if (Prot->Interface == Interface && Prot->Protocol == ProtEntry) {
> +        break;
> +      }
> +      Prot = NULL;
> +    }
> +  }
> +  return Prot;
> +}
> +
> +/**
> +  Wrapper function to MmInstallProtocolInterfaceNotify.  This is the public API which
> +  Calls the private one which contains a BOOLEAN parameter for notifications
> +
> +  @param  UserHandle             The handle to install the protocol handler on,
> +                                 or NULL if a new handle is to be allocated
> +  @param  Protocol               The protocol to add to the handle
> +  @param  InterfaceType          Indicates whether Interface is supplied in
> +                                 native form.
> +  @param  Interface              The interface for the protocol being added
> +
> +  @return Status code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInstallProtocolInterface (
> +  IN OUT EFI_HANDLE      *UserHandle,
> +  IN EFI_GUID            *Protocol,
> +  IN EFI_INTERFACE_TYPE  InterfaceType,
> +  IN VOID                *Interface
> +  )
> +{
> +  return MmInstallProtocolInterfaceNotify (
> +           UserHandle,
> +           Protocol,
> +           InterfaceType,
> +           Interface,
> +           TRUE
> +           );
> +}
> +
> +/**
> +  Installs a protocol interface into the boot services environment.
> +
> +  @param  UserHandle             The handle to install the protocol handler on,
> +                                 or NULL if a new handle is to be allocated
> +  @param  Protocol               The protocol to add to the handle
> +  @param  InterfaceType          Indicates whether Interface is supplied in
> +                                 native form.
> +  @param  Interface              The interface for the protocol being added
> +  @param  Notify                 indicates whether notify the notification list
> +                                 for this protocol
> +
> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> +  @retval EFI_OUT_OF_RESOURCES   No enough buffer to allocate
> +  @retval EFI_SUCCESS            Protocol interface successfully installed
> +
> +**/
> +EFI_STATUS
> +MmInstallProtocolInterfaceNotify (
> +  IN OUT EFI_HANDLE          *UserHandle,
> +  IN     EFI_GUID            *Protocol,
> +  IN     EFI_INTERFACE_TYPE  InterfaceType,
> +  IN     VOID                *Interface,
> +  IN     BOOLEAN             Notify
> +  )
> +{
> +  PROTOCOL_INTERFACE  *Prot;
> +  PROTOCOL_ENTRY      *ProtEntry;
> +  IHANDLE             *Handle;
> +  EFI_STATUS          Status;
> +  VOID                *ExistingInterface;
> +
> +  //
> +  // returns EFI_INVALID_PARAMETER if InterfaceType is invalid.
> +  // Also added check for invalid UserHandle and Protocol pointers.
> +  //
> +  if (UserHandle == NULL || Protocol == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  if (InterfaceType != EFI_NATIVE_INTERFACE) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  //
> +  // Print debug message
> +  //
> +  DEBUG((DEBUG_LOAD | DEBUG_INFO, "MmInstallProtocolInterface: %g %p\n", Protocol, Interface));
> +
> +  Status = EFI_OUT_OF_RESOURCES;
> +  Prot = NULL;
> +  Handle = NULL;
> +
> +  if (*UserHandle != NULL) {
> +    Status = MmHandleProtocol (*UserHandle, Protocol, (VOID **)&ExistingInterface);
> +    if (!EFI_ERROR (Status)) {
> +      return EFI_INVALID_PARAMETER;
> +    }
> +  }
> +
> +  //
> +  // Lookup the Protocol Entry for the requested protocol
> +  //
> +  ProtEntry = MmFindProtocolEntry (Protocol, TRUE);
> +  if (ProtEntry == NULL) {
> +    goto Done;
> +  }
> +
> +  //
> +  // Allocate a new protocol interface structure
> +  //
> +  Prot = AllocateZeroPool (sizeof(PROTOCOL_INTERFACE));
> +  if (Prot == NULL) {
> +    Status = EFI_OUT_OF_RESOURCES;
> +    goto Done;
> +  }
> +
> +  //
> +  // If caller didn't supply a handle, allocate a new one
> +  //
> +  Handle = (IHANDLE *)*UserHandle;
> +  if (Handle == NULL) {
> +    Handle = AllocateZeroPool (sizeof(IHANDLE));
> +    if (Handle == NULL) {
> +      Status = EFI_OUT_OF_RESOURCES;
> +      goto Done;
> +    }
> +
> +    //
> +    // Initialize new handler structure
> +    //
> +    Handle->Signature = EFI_HANDLE_SIGNATURE;
> +    InitializeListHead (&Handle->Protocols);
> +
> +    //
> +    // Add this handle to the list global list of all handles
> +    // in the system
> +    //
> +    InsertTailList (&gHandleList, &Handle->AllHandles);
> +  }
> +
> +  Status = MmValidateHandle (Handle);
> +  if (EFI_ERROR (Status)) {
> +    goto Done;
> +  }
> +
> +  //
> +  // Each interface that is added must be unique
> +  //
> +  ASSERT (MmFindProtocolInterface (Handle, Protocol, Interface) == NULL);
> +
> +  //
> +  // Initialize the protocol interface structure
> +  //
> +  Prot->Signature = PROTOCOL_INTERFACE_SIGNATURE;
> +  Prot->Handle = Handle;
> +  Prot->Protocol = ProtEntry;
> +  Prot->Interface = Interface;
> +
> +  //
> +  // Add this protocol interface to the head of the supported
> +  // protocol list for this handle
> +  //
> +  InsertHeadList (&Handle->Protocols, &Prot->Link);
> +
> +  //
> +  // Add this protocol interface to the tail of the
> +  // protocol entry
> +  //
> +  InsertTailList (&ProtEntry->Protocols, &Prot->ByProtocol);
> +
> +  //
> +  // Notify the notification list for this protocol
> +  //
> +  if (Notify) {
> +    MmNotifyProtocol (Prot);
> +  }
> +  Status = EFI_SUCCESS;
> +
> +Done:
> +  if (!EFI_ERROR (Status)) {
> +    //
> +    // Return the new handle back to the caller
> +    //
> +    *UserHandle = Handle;
> +  } else {
> +    //
> +    // There was an error, clean up
> +    //
> +    if (Prot != NULL) {
> +      FreePool (Prot);
> +    }
> +  }
> +  return Status;
> +}
> +
> +/**
> +  Uninstalls all instances of a protocol:interfacer from a handle.
> +  If the last protocol interface is remove from the handle, the
> +  handle is freed.
> +
> +  @param  UserHandle             The handle to remove the protocol handler from
> +  @param  Protocol               The protocol, of protocol:interface, to remove
> +  @param  Interface              The interface, of protocol:interface, to remove
> +
> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL.
> +  @retval EFI_SUCCESS            Protocol interface successfully uninstalled.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmUninstallProtocolInterface (
> +  IN EFI_HANDLE  UserHandle,
> +  IN EFI_GUID    *Protocol,
> +  IN VOID        *Interface
> +  )
> +{
> +  EFI_STATUS          Status;
> +  IHANDLE             *Handle;
> +  PROTOCOL_INTERFACE  *Prot;
> +
> +  //
> +  // Check that Protocol is valid
> +  //
> +  if (Protocol == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  //
> +  // Check that UserHandle is a valid handle
> +  //
> +  Status = MmValidateHandle (UserHandle);
> +  if (EFI_ERROR (Status)) {
> +    return Status;
> +  }
> +
> +  //
> +  // Check that Protocol exists on UserHandle, and Interface matches the interface in the database
> +  //
> +  Prot = MmFindProtocolInterface (UserHandle, Protocol, Interface);
> +  if (Prot == NULL) {
> +    return EFI_NOT_FOUND;
> +  }
> +
> +  //
> +  // Remove the protocol interface from the protocol
> +  //
> +  Status = EFI_NOT_FOUND;
> +  Handle = (IHANDLE *)UserHandle;
> +  Prot   = MmRemoveInterfaceFromProtocol (Handle, Protocol, Interface);
> +
> +  if (Prot != NULL) {
> +    //
> +    // Remove the protocol interface from the handle
> +    //
> +    RemoveEntryList (&Prot->Link);
> +
> +    //
> +    // Free the memory
> +    //
> +    Prot->Signature = 0;
> +    FreePool (Prot);
> +    Status = EFI_SUCCESS;
> +  }
> +
> +  //
> +  // If there are no more handlers for the handle, free the handle
> +  //
> +  if (IsListEmpty (&Handle->Protocols)) {
> +    Handle->Signature = 0;
> +    RemoveEntryList (&Handle->AllHandles);
> +    FreePool (Handle);
> +  }
> +  return Status;
> +}
> +
> +/**
> +  Locate a certain GUID protocol interface in a Handle's protocols.
> +
> +  @param  UserHandle             The handle to obtain the protocol interface on
> +  @param  Protocol               The GUID of the protocol
> +
> +  @return The requested protocol interface for the handle
> +
> +**/
> +PROTOCOL_INTERFACE  *
> +MmGetProtocolInterface (
> +  IN EFI_HANDLE  UserHandle,
> +  IN EFI_GUID    *Protocol
> +  )
> +{
> +  EFI_STATUS          Status;
> +  PROTOCOL_ENTRY      *ProtEntry;
> +  PROTOCOL_INTERFACE  *Prot;
> +  IHANDLE             *Handle;
> +  LIST_ENTRY          *Link;
> +
> +  Status = MmValidateHandle (UserHandle);
> +  if (EFI_ERROR (Status)) {
> +    return NULL;
> +  }
> +
> +  Handle = (IHANDLE *)UserHandle;
> +
> +  //
> +  // Look at each protocol interface for a match
> +  //
> +  for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link = Link->ForwardLink) {
> +    Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);
> +    ProtEntry = Prot->Protocol;
> +    if (CompareGuid (&ProtEntry->ProtocolID, Protocol)) {
> +      return Prot;
> +    }
> +  }
> +  return NULL;
> +}
> +
> +/**
> +  Queries a handle to determine if it supports a specified protocol.
> +
> +  @param  UserHandle             The handle being queried.
> +  @param  Protocol               The published unique identifier of the protocol.
> +  @param  Interface              Supplies the address where a pointer to the
> +                                 corresponding Protocol Interface is returned.
> +
> +  @retval EFI_SUCCESS            The interface information for the specified protocol was returned.
> +  @retval EFI_UNSUPPORTED        The device does not support the specified protocol.
> +  @retval EFI_INVALID_PARAMETER  Handle is not a valid EFI_HANDLE..
> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL.
> +  @retval EFI_INVALID_PARAMETER  Interface is NULL.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmHandleProtocol (
> +  IN  EFI_HANDLE  UserHandle,
> +  IN  EFI_GUID    *Protocol,
> +  OUT VOID        **Interface
> +  )
> +{
> +  EFI_STATUS          Status;
> +  PROTOCOL_INTERFACE  *Prot;
> +
> +  //
> +  // Check for invalid Protocol
> +  //
> +  if (Protocol == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  //
> +  // Check for invalid Interface
> +  //
> +  if (Interface == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  } else {
> +    *Interface = NULL;
> +  }
> +
> +  //
> +  // Check for invalid UserHandle
> +  //
> +  Status = MmValidateHandle (UserHandle);
> +  if (EFI_ERROR (Status)) {
> +    return Status;
> +  }
> +
> +  //
> +  // Look at each protocol interface for a match
> +  //
> +  Prot = MmGetProtocolInterface (UserHandle, Protocol);
> +  if (Prot == NULL) {
> +    return EFI_UNSUPPORTED;
> +  }
> +
> +  //
> +  // This is the protocol interface entry for this protocol
> +  //
> +  *Interface = Prot->Interface;
> +
> +  return EFI_SUCCESS;
> +}
> diff --git a/StandaloneMmPkg/Core/InstallConfigurationTable.c b/StandaloneMmPkg/Core/InstallConfigurationTable.c
> new file mode 100644
> index 0000000000..3a31c63f94
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/InstallConfigurationTable.c
> @@ -0,0 +1,178 @@
> +/** @file
> +  System Management System Table Services MmInstallConfigurationTable service
> +
> +  Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +#define CONFIG_TABLE_SIZE_INCREASED 0x10
> +
> +UINTN  mMmSystemTableAllocateSize = 0;
> +
> +/**
> +  The MmInstallConfigurationTable() function is used to maintain the list
> +  of configuration tables that are stored in the System Management System
> +  Table.  The list is stored as an array of (GUID, Pointer) pairs.  The list
> +  must be allocated from pool memory with PoolType set to EfiRuntimeServicesData.
> +
> +  @param  SystemTable      A pointer to the SMM System Table (SMST).
> +  @param  Guid             A pointer to the GUID for the entry to add, update, or remove.
> +  @param  Table            A pointer to the buffer of the table to add.
> +  @param  TableSize        The size of the table to install.
> +
> +  @retval EFI_SUCCESS           The (Guid, Table) pair was added, updated, or removed.
> +  @retval EFI_INVALID_PARAMETER Guid is not valid.
> +  @retval EFI_NOT_FOUND         An attempt was made to delete a non-existent entry.
> +  @retval EFI_OUT_OF_RESOURCES  There is not enough memory available to complete the operation.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInstallConfigurationTable (
> +  IN  CONST EFI_MM_SYSTEM_TABLE    *SystemTable,
> +  IN  CONST EFI_GUID               *Guid,
> +  IN  VOID                         *Table,
> +  IN  UINTN                        TableSize
> +  )
> +{
> +  UINTN                    Index;
> +  EFI_CONFIGURATION_TABLE  *ConfigurationTable;
> +  EFI_CONFIGURATION_TABLE  *OldTable;
> +
> +  //
> +  // If Guid is NULL, then this operation cannot be performed
> +  //
> +  if (Guid == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  ConfigurationTable = gMmCoreMmst.MmConfigurationTable;
> +
> +  //
> +  // Search all the table for an entry that matches Guid
> +  //
> +  for (Index = 0; Index < gMmCoreMmst.NumberOfTableEntries; Index++) {
> +    if (CompareGuid (Guid, &(ConfigurationTable[Index].VendorGuid))) {
> +      break;
> +    }
> +  }
> +
> +  if (Index < gMmCoreMmst.NumberOfTableEntries) {
> +    //
> +    // A match was found, so this is either a modify or a delete operation
> +    //
> +    if (Table != NULL) {
> +      //
> +      // If Table is not NULL, then this is a modify operation.
> +      // Modify the table entry and return.
> +      //
> +      ConfigurationTable[Index].VendorTable = Table;
> +      return EFI_SUCCESS;
> +    }
> +
> +    //
> +    // A match was found and Table is NULL, so this is a delete operation.
> +    //
> +    gMmCoreMmst.NumberOfTableEntries--;
> +
> +    //
> +    // Copy over deleted entry
> +    //
> +    CopyMem (
> +      &(ConfigurationTable[Index]),
> +      &(ConfigurationTable[Index + 1]),
> +      (gMmCoreMmst.NumberOfTableEntries - Index) * sizeof (EFI_CONFIGURATION_TABLE)
> +      );
> +
> +  } else {
> +    //
> +    // No matching GUIDs were found, so this is an add operation.
> +    //
> +    if (Table == NULL) {
> +      //
> +      // If Table is NULL on an add operation, then return an error.
> +      //
> +      return EFI_NOT_FOUND;
> +    }
> +
> +    //
> +    // Assume that Index == gMmCoreMmst.NumberOfTableEntries
> +    //
> +    if ((Index * sizeof (EFI_CONFIGURATION_TABLE)) >= mMmSystemTableAllocateSize) {
> +      //
> +      // Allocate a table with one additional entry.
> +      //
> +      mMmSystemTableAllocateSize += (CONFIG_TABLE_SIZE_INCREASED * sizeof (EFI_CONFIGURATION_TABLE));
> +      ConfigurationTable = AllocatePool (mMmSystemTableAllocateSize);
> +      if (ConfigurationTable == NULL) {
> +        //
> +        // If a new table could not be allocated, then return an error.
> +        //
> +        return EFI_OUT_OF_RESOURCES;
> +      }
> +
> +      if (gMmCoreMmst.MmConfigurationTable != NULL) {
> +        //
> +        // Copy the old table to the new table.
> +        //
> +        CopyMem (
> +          ConfigurationTable,
> +          gMmCoreMmst.MmConfigurationTable,
> +          Index * sizeof (EFI_CONFIGURATION_TABLE)
> +          );
> +
> +        //
> +        // Record the old table pointer.
> +        //
> +        OldTable = gMmCoreMmst.MmConfigurationTable;
> +
> +        //
> +        // As the MmInstallConfigurationTable() may be re-entered by FreePool() in
> +        // its calling stack, updating System table to the new table pointer must
> +        // be done before calling FreePool() to free the old table.
> +        // It can make sure the gMmCoreMmst.MmConfigurationTable point to the new
> +        // table and avoid the errors of use-after-free to the old table by the
> +        // reenter of MmInstallConfigurationTable() in FreePool()'s calling stack.
> +        //
> +        gMmCoreMmst.MmConfigurationTable = ConfigurationTable;
> +
> +        //
> +        // Free the old table after updating System Table to the new table pointer.
> +        //
> +        FreePool (OldTable);
> +      } else {
> +        //
> +        // Update System Table
> +        //
> +        gMmCoreMmst.MmConfigurationTable = ConfigurationTable;
> +      }
> +    }
> +
> +    //
> +    // Fill in the new entry
> +    //
> +    CopyGuid ((VOID *)&ConfigurationTable[Index].VendorGuid, Guid);
> +    ConfigurationTable[Index].VendorTable = Table;
> +
> +    //
> +    // This is an add operation, so increment the number of table entries
> +    //
> +    gMmCoreMmst.NumberOfTableEntries++;
> +  }
> +
> +  //
> +  // CRC-32 field is ignorable for SMM System Table and should be set to zero
> +  //
> +
> +  return EFI_SUCCESS;
> +}
> diff --git a/StandaloneMmPkg/Core/Locate.c b/StandaloneMmPkg/Core/Locate.c
> new file mode 100644
> index 0000000000..6a90575f99
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Locate.c
> @@ -0,0 +1,496 @@
> +/** @file
> +  Locate handle functions
> +
> +  Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +//
> +// ProtocolRequest - Last LocateHandle request ID
> +//
> +UINTN mEfiLocateHandleRequest = 0;
> +
> +//
> +// Internal prototypes
> +//
> +
> +typedef struct {
> +  EFI_GUID        *Protocol;
> +  VOID            *SearchKey;
> +  LIST_ENTRY      *Position;
> +  PROTOCOL_ENTRY  *ProtEntry;
> +} LOCATE_POSITION;
> +
> +typedef
> +IHANDLE *
> +(* CORE_GET_NEXT) (
> +  IN OUT LOCATE_POSITION    *Position,
> +  OUT VOID                  **Interface
> +  );
> +
> +/**
> +  Routine to get the next Handle, when you are searching for all handles.
> +
> +  @param  Position               Information about which Handle to seach for.
> +  @param  Interface              Return the interface structure for the matching
> +                                 protocol.
> +
> +  @return An pointer to IHANDLE if the next Position is not the end of the list.
> +          Otherwise,NULL is returned.
> +
> +**/
> +IHANDLE *
> +MmGetNextLocateAllHandles (
> +  IN OUT LOCATE_POSITION  *Position,
> +  OUT    VOID             **Interface
> +  )
> +{
> +  IHANDLE     *Handle;
> +
> +  //
> +  // Next handle
> +  //
> +  Position->Position = Position->Position->ForwardLink;
> +
> +  //
> +  // If not at the end of the list, get the handle
> +  //
> +  Handle      = NULL;
> +  *Interface  = NULL;
> +  if (Position->Position != &gHandleList) {
> +    Handle = CR (Position->Position, IHANDLE, AllHandles, EFI_HANDLE_SIGNATURE);
> +  }
> +  return Handle;
> +}
> +
> +/**
> +  Routine to get the next Handle, when you are searching for register protocol
> +  notifies.
> +
> +  @param  Position               Information about which Handle to seach for.
> +  @param  Interface              Return the interface structure for the matching
> +                                 protocol.
> +
> +  @return An pointer to IHANDLE if the next Position is not the end of the list.
> +          Otherwise,NULL is returned.
> +
> +**/
> +IHANDLE *
> +MmGetNextLocateByRegisterNotify (
> +  IN OUT LOCATE_POSITION  *Position,
> +  OUT    VOID             **Interface
> +  )
> +{
> +  IHANDLE             *Handle;
> +  PROTOCOL_NOTIFY     *ProtNotify;
> +  PROTOCOL_INTERFACE  *Prot;
> +  LIST_ENTRY          *Link;
> +
> +  Handle      = NULL;
> +  *Interface  = NULL;
> +  ProtNotify = Position->SearchKey;
> +
> +  //
> +  // If this is the first request, get the next handle
> +  //
> +  if (ProtNotify != NULL) {
> +    ASSERT(ProtNotify->Signature == PROTOCOL_NOTIFY_SIGNATURE);
> +    Position->SearchKey = NULL;
> +
> +    //
> +    // If not at the end of the list, get the next handle
> +    //
> +    Link = ProtNotify->Position->ForwardLink;
> +    if (Link != &ProtNotify->Protocol->Protocols) {
> +      Prot = CR (Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_INTERFACE_SIGNATURE);
> +      Handle = Prot->Handle;
> +      *Interface = Prot->Interface;
> +    }
> +  }
> +  return Handle;
> +}
> +
> +/**
> +  Routine to get the next Handle, when you are searching for a given protocol.
> +
> +  @param  Position               Information about which Handle to seach for.
> +  @param  Interface              Return the interface structure for the matching
> +                                 protocol.
> +
> +  @return An pointer to IHANDLE if the next Position is not the end of the list.
> +          Otherwise,NULL is returned.
> +
> +**/
> +IHANDLE *
> +MmGetNextLocateByProtocol (
> +  IN OUT LOCATE_POSITION  *Position,
> +  OUT    VOID             **Interface
> +  )
> +{
> +  IHANDLE             *Handle;
> +  LIST_ENTRY          *Link;
> +  PROTOCOL_INTERFACE  *Prot;
> +
> +  Handle      = NULL;
> +  *Interface  = NULL;
> +  for (; ;) {
> +    //
> +    // Next entry
> +    //
> +    Link = Position->Position->ForwardLink;
> +    Position->Position = Link;
> +
> +    //
> +    // If not at the end, return the handle
> +    //
> +    if (Link == &Position->ProtEntry->Protocols) {
> +      Handle = NULL;
> +      break;
> +    }
> +
> +    //
> +    // Get the handle
> +    //
> +    Prot = CR(Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_INTERFACE_SIGNATURE);
> +    Handle = Prot->Handle;
> +    *Interface = Prot->Interface;
> +
> +    //
> +    // If this handle has not been returned this request, then
> +    // return it now
> +    //
> +    if (Handle->LocateRequest != mEfiLocateHandleRequest) {
> +      Handle->LocateRequest = mEfiLocateHandleRequest;
> +      break;
> +    }
> +  }
> +  return Handle;
> +}
> +
> +/**
> +  Return the first Protocol Interface that matches the Protocol GUID. If
> +  Registration is pasased in return a Protocol Instance that was just add
> +  to the system. If Retistration is NULL return the first Protocol Interface
> +  you find.
> +
> +  @param  Protocol               The protocol to search for
> +  @param  Registration           Optional Registration Key returned from
> +                                 RegisterProtocolNotify()
> +  @param  Interface              Return the Protocol interface (instance).
> +
> +  @retval EFI_SUCCESS            If a valid Interface is returned
> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> +  @retval EFI_NOT_FOUND          Protocol interface not found
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLocateProtocol (
> +  IN  EFI_GUID  *Protocol,
> +  IN  VOID      *Registration OPTIONAL,
> +  OUT VOID      **Interface
> +  )
> +{
> +  EFI_STATUS              Status;
> +  LOCATE_POSITION         Position;
> +  PROTOCOL_NOTIFY         *ProtNotify;
> +  IHANDLE                 *Handle;
> +
> +  if ((Interface == NULL) || (Protocol == NULL)) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  *Interface = NULL;
> +  Status = EFI_SUCCESS;
> +
> +  //
> +  // Set initial position
> +  //
> +  Position.Protocol  = Protocol;
> +  Position.SearchKey = Registration;
> +  Position.Position  = &gHandleList;
> +
> +  mEfiLocateHandleRequest += 1;
> +
> +  if (Registration == NULL) {
> +    //
> +    // Look up the protocol entry and set the head pointer
> +    //
> +    Position.ProtEntry = MmFindProtocolEntry (Protocol, FALSE);
> +    if (Position.ProtEntry == NULL) {
> +      return EFI_NOT_FOUND;
> +    }
> +    Position.Position = &Position.ProtEntry->Protocols;
> +
> +    Handle = MmGetNextLocateByProtocol (&Position, Interface);
> +  } else {
> +    Handle = MmGetNextLocateByRegisterNotify (&Position, Interface);
> +  }
> +
> +  if (Handle == NULL) {
> +    Status = EFI_NOT_FOUND;
> +  } else if (Registration != NULL) {
> +    //
> +    // If this is a search by register notify and a handle was
> +    // returned, update the register notification position
> +    //
> +    ProtNotify = Registration;
> +    ProtNotify->Position = ProtNotify->Position->ForwardLink;
> +  }
> +
> +  return Status;
> +}
> +
> +/**
> +  Locates the requested handle(s) and returns them in Buffer.
> +
> +  @param  SearchType             The type of search to perform to locate the
> +                                 handles
> +  @param  Protocol               The protocol to search for
> +  @param  SearchKey              Dependant on SearchType
> +  @param  BufferSize             On input the size of Buffer.  On output the
> +                                 size of data returned.
> +  @param  Buffer                 The buffer to return the results in
> +
> +  @retval EFI_BUFFER_TOO_SMALL   Buffer too small, required buffer size is
> +                                 returned in BufferSize.
> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> +  @retval EFI_SUCCESS            Successfully found the requested handle(s) and
> +                                 returns them in Buffer.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLocateHandle (
> +  IN     EFI_LOCATE_SEARCH_TYPE  SearchType,
> +  IN     EFI_GUID                *Protocol   OPTIONAL,
> +  IN     VOID                    *SearchKey  OPTIONAL,
> +  IN OUT UINTN                   *BufferSize,
> +  OUT    EFI_HANDLE              *Buffer
> +  )
> +{
> +  EFI_STATUS       Status;
> +  LOCATE_POSITION  Position;
> +  PROTOCOL_NOTIFY  *ProtNotify;
> +  CORE_GET_NEXT    GetNext;
> +  UINTN            ResultSize;
> +  IHANDLE          *Handle;
> +  IHANDLE          **ResultBuffer;
> +  VOID             *Interface;
> +
> +  if (BufferSize == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  if ((*BufferSize > 0) && (Buffer == NULL)) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  GetNext = NULL;
> +
> +  //
> +  // Set initial position
> +  //
> +  Position.Protocol  = Protocol;
> +  Position.SearchKey = SearchKey;
> +  Position.Position  = &gHandleList;
> +
> +  ResultSize = 0;
> +  ResultBuffer = (IHANDLE **) Buffer;
> +  Status = EFI_SUCCESS;
> +
> +  //
> +  // Get the search function based on type
> +  //
> +  switch (SearchType) {
> +  case AllHandles:
> +    GetNext = MmGetNextLocateAllHandles;
> +    break;
> +
> +  case ByRegisterNotify:
> +    GetNext = MmGetNextLocateByRegisterNotify;
> +    //
> +    // Must have SearchKey for locate ByRegisterNotify
> +    //
> +    if (SearchKey == NULL) {
> +      Status = EFI_INVALID_PARAMETER;
> +    }
> +    break;
> +
> +  case ByProtocol:
> +    GetNext = MmGetNextLocateByProtocol;
> +    if (Protocol == NULL) {
> +      Status = EFI_INVALID_PARAMETER;
> +      break;
> +    }
> +    //
> +    // Look up the protocol entry and set the head pointer
> +    //
> +    Position.ProtEntry = MmFindProtocolEntry (Protocol, FALSE);
> +    if (Position.ProtEntry == NULL) {
> +      Status = EFI_NOT_FOUND;
> +      break;
> +    }
> +    Position.Position = &Position.ProtEntry->Protocols;
> +    break;
> +
> +  default:
> +    Status = EFI_INVALID_PARAMETER;
> +    break;
> +  }
> +
> +  if (EFI_ERROR(Status)) {
> +    return Status;
> +  }
> +
> +  //
> +  // Enumerate out the matching handles
> +  //
> +  mEfiLocateHandleRequest += 1;
> +  for (; ;) {
> +    //
> +    // Get the next handle.  If no more handles, stop
> +    //
> +    Handle = GetNext (&Position, &Interface);
> +    if (NULL == Handle) {
> +      break;
> +    }
> +
> +    //
> +    // Increase the resulting buffer size, and if this handle
> +    // fits return it
> +    //
> +    ResultSize += sizeof(Handle);
> +    if (ResultSize <= *BufferSize) {
> +        *ResultBuffer = Handle;
> +        ResultBuffer += 1;
> +    }
> +  }
> +
> +  //
> +  // If the result is a zero length buffer, then there were no
> +  // matching handles
> +  //
> +  if (ResultSize == 0) {
> +    Status = EFI_NOT_FOUND;
> +  } else {
> +    //
> +    // Return the resulting buffer size.  If it's larger than what
> +    // was passed, then set the error code
> +    //
> +    if (ResultSize > *BufferSize) {
> +      Status = EFI_BUFFER_TOO_SMALL;
> +    }
> +
> +    *BufferSize = ResultSize;
> +
> +    if (SearchType == ByRegisterNotify && !EFI_ERROR(Status)) {
> +      ASSERT (SearchKey != NULL);
> +      //
> +      // If this is a search by register notify and a handle was
> +      // returned, update the register notification position
> +      //
> +      ProtNotify = SearchKey;
> +      ProtNotify->Position = ProtNotify->Position->ForwardLink;
> +    }
> +  }
> +
> +  return Status;
> +}
> +
> +/**
> +  Function returns an array of handles that support the requested protocol
> +  in a buffer allocated from pool. This is a version of MmLocateHandle()
> +  that allocates a buffer for the caller.
> +
> +  @param  SearchType             Specifies which handle(s) are to be returned.
> +  @param  Protocol               Provides the protocol to search by.    This
> +                                 parameter is only valid for SearchType
> +                                 ByProtocol.
> +  @param  SearchKey              Supplies the search key depending on the
> +                                 SearchType.
> +  @param  NumberHandles          The number of handles returned in Buffer.
> +  @param  Buffer                 A pointer to the buffer to return the requested
> +                                 array of  handles that support Protocol.
> +
> +  @retval EFI_SUCCESS            The result array of handles was returned.
> +  @retval EFI_NOT_FOUND          No handles match the search.
> +  @retval EFI_OUT_OF_RESOURCES   There is not enough pool memory to store the
> +                                 matching results.
> +  @retval EFI_INVALID_PARAMETER  One or more parameters are not valid.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLocateHandleBuffer (
> +  IN     EFI_LOCATE_SEARCH_TYPE  SearchType,
> +  IN     EFI_GUID                *Protocol OPTIONAL,
> +  IN     VOID                    *SearchKey OPTIONAL,
> +  IN OUT UINTN                   *NumberHandles,
> +  OUT    EFI_HANDLE              **Buffer
> +  )
> +{
> +  EFI_STATUS  Status;
> +  UINTN       BufferSize;
> +
> +  if (NumberHandles == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  if (Buffer == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  BufferSize = 0;
> +  *NumberHandles = 0;
> +  *Buffer = NULL;
> +  Status = MmLocateHandle (
> +             SearchType,
> +             Protocol,
> +             SearchKey,
> +             &BufferSize,
> +             *Buffer
> +             );
> +  //
> +  // LocateHandleBuffer() returns incorrect status code if SearchType is
> +  // invalid.
> +  //
> +  // Add code to correctly handle expected errors from MmLocateHandle().
> +  //
> +  if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {
> +    if (Status != EFI_INVALID_PARAMETER) {
> +      Status = EFI_NOT_FOUND;
> +    }
> +    return Status;
> +  }
> +
> +  *Buffer = AllocatePool (BufferSize);
> +  if (*Buffer == NULL) {
> +    return EFI_OUT_OF_RESOURCES;
> +  }
> +
> +  Status = MmLocateHandle (
> +             SearchType,
> +             Protocol,
> +             SearchKey,
> +             &BufferSize,
> +             *Buffer
> +             );
> +
> +  *NumberHandles = BufferSize / sizeof(EFI_HANDLE);
> +  if (EFI_ERROR(Status)) {
> +    *NumberHandles = 0;
> +  }
> +
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Core/Mmi.c b/StandaloneMmPkg/Core/Mmi.c
> new file mode 100644
> index 0000000000..29aba7b53d
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Mmi.c
> @@ -0,0 +1,337 @@
> +/** @file
> +  MMI management.
> +
> +  Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +//
> +// MM_HANDLER_STATE_NOTIFIER
> +//
> +
> +//
> +// MM_HANDLER - used for each MM handler
> +//
> +
> +#define MMI_ENTRY_SIGNATURE  SIGNATURE_32('m','m','i','e')
> +
> + typedef struct {
> +  UINTN       Signature;
> +  LIST_ENTRY  AllEntries;  // All entries
> +
> +  EFI_GUID    HandlerType; // Type of interrupt
> +  LIST_ENTRY  MmiHandlers; // All handlers
> +} MMI_ENTRY;
> +
> +#define MMI_HANDLER_SIGNATURE  SIGNATURE_32('m','m','i','h')
> +
> + typedef struct {
> +  UINTN                         Signature;
> +  LIST_ENTRY                    Link;        // Link on MMI_ENTRY.MmiHandlers
> +  EFI_MM_HANDLER_ENTRY_POINT    Handler;     // The mm handler's entry point
> +  MMI_ENTRY                     *MmiEntry;
> +} MMI_HANDLER;
> +
> +LIST_ENTRY  mRootMmiHandlerList = INITIALIZE_LIST_HEAD_VARIABLE (mRootMmiHandlerList);
> +LIST_ENTRY  mMmiEntryList       = INITIALIZE_LIST_HEAD_VARIABLE (mMmiEntryList);
> +
> +/**
> +  Finds the MMI entry for the requested handler type.
> +
> +  @param  HandlerType            The type of the interrupt
> +  @param  Create                 Create a new entry if not found
> +
> +  @return MMI entry
> +
> +**/
> +MMI_ENTRY  *
> +EFIAPI
> +MmCoreFindMmiEntry (
> +  IN EFI_GUID  *HandlerType,
> +  IN BOOLEAN   Create
> +  )
> +{
> +  LIST_ENTRY  *Link;
> +  MMI_ENTRY   *Item;
> +  MMI_ENTRY   *MmiEntry;
> +
> +  //
> +  // Search the MMI entry list for the matching GUID
> +  //
> +  MmiEntry = NULL;
> +  for (Link = mMmiEntryList.ForwardLink;
> +       Link != &mMmiEntryList;
> +       Link = Link->ForwardLink) {
> +
> +    Item = CR (Link, MMI_ENTRY, AllEntries, MMI_ENTRY_SIGNATURE);
> +    if (CompareGuid (&Item->HandlerType, HandlerType)) {
> +      //
> +      // This is the MMI entry
> +      //
> +      MmiEntry = Item;
> +      break;
> +    }
> +  }
> +
> +  //
> +  // If the protocol entry was not found and Create is TRUE, then
> +  // allocate a new entry
> +  //
> +  if ((MmiEntry == NULL) && Create) {
> +    MmiEntry = AllocatePool (sizeof(MMI_ENTRY));
> +    if (MmiEntry != NULL) {
> +      //
> +      // Initialize new MMI entry structure
> +      //
> +      MmiEntry->Signature = MMI_ENTRY_SIGNATURE;
> +      CopyGuid ((VOID *)&MmiEntry->HandlerType, HandlerType);
> +      InitializeListHead (&MmiEntry->MmiHandlers);
> +
> +      //
> +      // Add it to MMI entry list
> +      //
> +      InsertTailList (&mMmiEntryList, &MmiEntry->AllEntries);
> +    }
> +  }
> +  return MmiEntry;
> +}
> +
> +/**
> +  Manage MMI of a particular type.
> +
> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
> +  @param  Context        Points to an optional context buffer.
> +  @param  CommBuffer     Points to the optional communication buffer.
> +  @param  CommBufferSize Points to the size of the optional communication buffer.
> +
> +  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING  Interrupt source was processed successfully but not quiesced.
> +  @retval EFI_INTERRUPT_PENDING              One or more MMI sources could not be quiesced.
> +  @retval EFI_NOT_FOUND                      Interrupt source was not handled or quiesced.
> +  @retval EFI_SUCCESS                        Interrupt source was handled and quiesced.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmiManage (
> +  IN     CONST EFI_GUID  *HandlerType,
> +  IN     CONST VOID      *Context         OPTIONAL,
> +  IN OUT VOID            *CommBuffer      OPTIONAL,
> +  IN OUT UINTN           *CommBufferSize  OPTIONAL
> +  )
> +{
> +  LIST_ENTRY   *Link;
> +  LIST_ENTRY   *Head;
> +  MMI_ENTRY    *MmiEntry;
> +  MMI_HANDLER  *MmiHandler;
> +  BOOLEAN      SuccessReturn;
> +  EFI_STATUS   Status;
> +
> +  Status = EFI_NOT_FOUND;
> +  SuccessReturn = FALSE;
> +  if (HandlerType == NULL) {
> +    //
> +    // Root MMI handler
> +    //
> +
> +    Head = &mRootMmiHandlerList;
> +  } else {
> +    //
> +    // Non-root MMI handler
> +    //
> +    MmiEntry = MmCoreFindMmiEntry ((EFI_GUID *) HandlerType, FALSE);
> +    if (MmiEntry == NULL) {
> +      //
> +      // There is no handler registered for this interrupt source
> +      //
> +      return Status;
> +    }
> +
> +    Head = &MmiEntry->MmiHandlers;
> +  }
> +
> +  for (Link = Head->ForwardLink; Link != Head; Link = Link->ForwardLink) {
> +    MmiHandler = CR (Link, MMI_HANDLER, Link, MMI_HANDLER_SIGNATURE);
> +
> +    Status = MmiHandler->Handler (
> +               (EFI_HANDLE) MmiHandler,
> +               Context,
> +               CommBuffer,
> +               CommBufferSize
> +               );
> +
> +    switch (Status) {
> +    case EFI_INTERRUPT_PENDING:
> +      //
> +      // If a handler returns EFI_INTERRUPT_PENDING and HandlerType is not NULL then
> +      // no additional handlers will be processed and EFI_INTERRUPT_PENDING will be returned.
> +      //
> +      if (HandlerType != NULL) {
> +        return EFI_INTERRUPT_PENDING;
> +      }
> +      break;
> +
> +    case EFI_SUCCESS:
> +      //
> +      // If at least one of the handlers returns EFI_SUCCESS then the function will return
> +      // EFI_SUCCESS. If a handler returns EFI_SUCCESS and HandlerType is not NULL then no
> +      // additional handlers will be processed.
> +      //
> +      if (HandlerType != NULL) {
> +        return EFI_SUCCESS;
> +      }
> +      SuccessReturn = TRUE;
> +      break;
> +
> +    case EFI_WARN_INTERRUPT_SOURCE_QUIESCED:
> +      //
> +      // If at least one of the handlers returns EFI_WARN_INTERRUPT_SOURCE_QUIESCED
> +      // then the function will return EFI_SUCCESS.
> +      //
> +      SuccessReturn = TRUE;
> +      break;
> +
> +    case EFI_WARN_INTERRUPT_SOURCE_PENDING:
> +      //
> +      // If all the handlers returned EFI_WARN_INTERRUPT_SOURCE_PENDING
> +      // then EFI_WARN_INTERRUPT_SOURCE_PENDING will be returned.
> +      //
> +      break;
> +
> +    default:
> +      //
> +      // Unexpected status code returned.
> +      //
> +      ASSERT (FALSE);
> +      break;
> +    }
> +  }
> +
> +  if (SuccessReturn) {
> +    Status = EFI_SUCCESS;
> +  }
> +
> +  return Status;
> +}
> +
> +/**
> +  Registers a handler to execute within MM.
> +
> +  @param  Handler        Handler service funtion pointer.
> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
> +  @param  DispatchHandle On return, contains a unique handle which can be used to later unregister the handler function.
> +
> +  @retval EFI_SUCCESS           Handler register success.
> +  @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmiHandlerRegister (
> +  IN  EFI_MM_HANDLER_ENTRY_POINT    Handler,
> +  IN  CONST EFI_GUID                *HandlerType  OPTIONAL,
> +  OUT EFI_HANDLE                    *DispatchHandle
> +  )
> +{
> +  MMI_HANDLER  *MmiHandler;
> +  MMI_ENTRY    *MmiEntry;
> +  LIST_ENTRY   *List;
> +
> +  if (Handler == NULL || DispatchHandle == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  MmiHandler = AllocateZeroPool (sizeof (MMI_HANDLER));
> +  if (MmiHandler == NULL) {
> +    return EFI_OUT_OF_RESOURCES;
> +  }
> +
> +  MmiHandler->Signature = MMI_HANDLER_SIGNATURE;
> +  MmiHandler->Handler = Handler;
> +
> +  if (HandlerType == NULL) {
> +    //
> +    // This is root MMI handler
> +    //
> +    MmiEntry = NULL;
> +    List = &mRootMmiHandlerList;
> +  } else {
> +    //
> +    // None root MMI handler
> +    //
> +    MmiEntry = MmCoreFindMmiEntry ((EFI_GUID *) HandlerType, TRUE);
> +    if (MmiEntry == NULL) {
> +      return EFI_OUT_OF_RESOURCES;
> +    }
> +
> +    List = &MmiEntry->MmiHandlers;
> +  }
> +
> +  MmiHandler->MmiEntry = MmiEntry;
> +  InsertTailList (List, &MmiHandler->Link);
> +
> +  *DispatchHandle = (EFI_HANDLE) MmiHandler;
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Unregister a handler in MM.
> +
> +  @param  DispatchHandle  The handle that was specified when the handler was registered.
> +
> +  @retval EFI_SUCCESS           Handler function was successfully unregistered.
> +  @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid handle.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmiHandlerUnRegister (
> +  IN EFI_HANDLE  DispatchHandle
> +  )
> +{
> +  MMI_HANDLER  *MmiHandler;
> +  MMI_ENTRY    *MmiEntry;
> +
> +  MmiHandler = (MMI_HANDLER *) DispatchHandle;
> +
> +  if (MmiHandler == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  if (MmiHandler->Signature != MMI_HANDLER_SIGNATURE) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  MmiEntry = MmiHandler->MmiEntry;
> +
> +  RemoveEntryList (&MmiHandler->Link);
> +  FreePool (MmiHandler);
> +
> +  if (MmiEntry == NULL) {
> +    //
> +    // This is root MMI handler
> +    //
> +    return EFI_SUCCESS;
> +  }
> +
> +  if (IsListEmpty (&MmiEntry->MmiHandlers)) {
> +    //
> +    // No handler registered for this interrupt now, remove the MMI_ENTRY
> +    //
> +    RemoveEntryList (&MmiEntry->AllEntries);
> +
> +    FreePool (MmiEntry);
> +  }
> +
> +  return EFI_SUCCESS;
> +}
> diff --git a/StandaloneMmPkg/Core/Notify.c b/StandaloneMmPkg/Core/Notify.c
> new file mode 100644
> index 0000000000..d5fc8f50d1
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Notify.c
> @@ -0,0 +1,203 @@
> +/** @file
> +  Support functions for UEFI protocol notification infrastructure.
> +
> +  Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +/**
> +  Signal event for every protocol in protocol entry.
> +
> +  @param  Prot                   Protocol interface
> +
> +**/
> +VOID
> +MmNotifyProtocol (
> +  IN PROTOCOL_INTERFACE  *Prot
> +  )
> +{
> +  PROTOCOL_ENTRY   *ProtEntry;
> +  PROTOCOL_NOTIFY  *ProtNotify;
> +  LIST_ENTRY       *Link;
> +
> +  ProtEntry = Prot->Protocol;
> +  for (Link=ProtEntry->Notify.ForwardLink; Link != &ProtEntry->Notify; Link=Link->ForwardLink) {
> +    ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
> +    ProtNotify->Function (&ProtEntry->ProtocolID, Prot->Interface, Prot->Handle);
> +  }
> +}
> +
> +/**
> +  Removes Protocol from the protocol list (but not the handle list).
> +
> +  @param  Handle                 The handle to remove protocol on.
> +  @param  Protocol               GUID of the protocol to be moved
> +  @param  Interface              The interface of the protocol
> +
> +  @return Protocol Entry
> +
> +**/
> +PROTOCOL_INTERFACE *
> +MmRemoveInterfaceFromProtocol (
> +  IN IHANDLE   *Handle,
> +  IN EFI_GUID  *Protocol,
> +  IN VOID      *Interface
> +  )
> +{
> +  PROTOCOL_INTERFACE  *Prot;
> +  PROTOCOL_NOTIFY     *ProtNotify;
> +  PROTOCOL_ENTRY      *ProtEntry;
> +  LIST_ENTRY          *Link;
> +
> +  Prot = MmFindProtocolInterface (Handle, Protocol, Interface);
> +  if (Prot != NULL) {
> +
> +    ProtEntry = Prot->Protocol;
> +
> +    //
> +    // If there's a protocol notify location pointing to this entry, back it up one
> +    //
> +    for(Link = ProtEntry->Notify.ForwardLink; Link != &ProtEntry->Notify; Link=Link->ForwardLink) {
> +      ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
> +
> +      if (ProtNotify->Position == &Prot->ByProtocol) {
> +        ProtNotify->Position = Prot->ByProtocol.BackLink;
> +      }
> +    }
> +
> +    //
> +    // Remove the protocol interface entry
> +    //
> +    RemoveEntryList (&Prot->ByProtocol);
> +  }
> +
> +  return Prot;
> +}
> +
> +/**
> +  Add a new protocol notification record for the request protocol.
> +
> +  @param  Protocol               The requested protocol to add the notify
> +                                 registration
> +  @param  Function               Points to the notification function
> +  @param  Registration           Returns the registration record
> +
> +  @retval EFI_SUCCESS            Successfully returned the registration record
> +                                 that has been added or unhooked
> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL or Registration is NULL
> +  @retval EFI_OUT_OF_RESOURCES   Not enough memory resource to finish the request
> +  @retval EFI_NOT_FOUND          If the registration is not found when Function == NULL
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmRegisterProtocolNotify (
> +  IN  CONST EFI_GUID     *Protocol,
> +  IN  EFI_MM_NOTIFY_FN  Function,
> +  OUT VOID               **Registration
> +  )
> +{
> +  PROTOCOL_ENTRY   *ProtEntry;
> +  PROTOCOL_NOTIFY  *ProtNotify;
> +  LIST_ENTRY       *Link;
> +  EFI_STATUS       Status;
> +
> +  if (Protocol == NULL || Registration == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  if (Function == NULL) {
> +    //
> +    // Get the protocol entry per Protocol
> +    //
> +    ProtEntry = MmFindProtocolEntry ((EFI_GUID *) Protocol, FALSE);
> +    if (ProtEntry != NULL) {
> +      ProtNotify = (PROTOCOL_NOTIFY * )*Registration;
> +      for (Link = ProtEntry->Notify.ForwardLink;
> +           Link != &ProtEntry->Notify;
> +           Link = Link->ForwardLink) {
> +        //
> +        // Compare the notification record
> +        //
> +        if (ProtNotify == (CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE))){
> +          //
> +          // If Registration is an existing registration, then unhook it
> +          //
> +          ProtNotify->Signature = 0;
> +          RemoveEntryList (&ProtNotify->Link);
> +          FreePool (ProtNotify);
> +          return EFI_SUCCESS;
> +        }
> +      }
> +    }
> +    //
> +    // If the registration is not found
> +    //
> +    return EFI_NOT_FOUND;
> +  }
> +
> +  ProtNotify = NULL;
> +
> +  //
> +  // Get the protocol entry to add the notification too
> +  //
> +  ProtEntry = MmFindProtocolEntry ((EFI_GUID *) Protocol, TRUE);
> +  if (ProtEntry != NULL) {
> +    //
> +    // Find whether notification already exist
> +    //
> +    for (Link = ProtEntry->Notify.ForwardLink;
> +         Link != &ProtEntry->Notify;
> +         Link = Link->ForwardLink) {
> +
> +      ProtNotify = CR(Link, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
> +      if (CompareGuid (&ProtNotify->Protocol->ProtocolID, Protocol) &&
> +          (ProtNotify->Function == Function)) {
> +
> +        //
> +        // Notification already exist
> +        //
> +        *Registration = ProtNotify;
> +
> +        return EFI_SUCCESS;
> +      }
> +    }
> +
> +    //
> +    // Allocate a new notification record
> +    //
> +    ProtNotify = AllocatePool (sizeof(PROTOCOL_NOTIFY));
> +    if (ProtNotify != NULL) {
> +      ProtNotify->Signature = PROTOCOL_NOTIFY_SIGNATURE;
> +      ProtNotify->Protocol = ProtEntry;
> +      ProtNotify->Function = Function;
> +      //
> +      // Start at the ending
> +      //
> +      ProtNotify->Position = ProtEntry->Protocols.BackLink;
> +
> +      InsertTailList (&ProtEntry->Notify, &ProtNotify->Link);
> +    }
> +  }
> +
> +  //
> +  // Done.  If we have a protocol notify entry, then return it.
> +  // Otherwise, we must have run out of resources trying to add one
> +  //
> +  Status = EFI_OUT_OF_RESOURCES;
> +  if (ProtNotify != NULL) {
> +    *Registration = ProtNotify;
> +    Status = EFI_SUCCESS;
> +  }
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Core/Page.c b/StandaloneMmPkg/Core/Page.c
> new file mode 100644
> index 0000000000..ba3e7cea74
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Page.c
> @@ -0,0 +1,384 @@
> +/** @file
> +  MM Memory page management functions.
> +
> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +#define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \
> +  ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size)))
> +
> +#define TRUNCATE_TO_PAGES(a)  ((a) >> EFI_PAGE_SHIFT)
> +
> +LIST_ENTRY  mMmMemoryMap = INITIALIZE_LIST_HEAD_VARIABLE (mMmMemoryMap);
> +
> +UINTN mMapKey;
> +
> +/**
> +  Internal Function. Allocate n pages from given free page node.
> +
> +  @param  Pages                  The free page node.
> +  @param  NumberOfPages          Number of pages to be allocated.
> +  @param  MaxAddress             Request to allocate memory below this address.
> +
> +  @return Memory address of allocated pages.
> +
> +**/
> +UINTN
> +InternalAllocPagesOnOneNode (
> +  IN OUT FREE_PAGE_LIST  *Pages,
> +  IN     UINTN           NumberOfPages,
> +  IN     UINTN           MaxAddress
> +  )
> +{
> +  UINTN           Top;
> +  UINTN           Bottom;
> +  FREE_PAGE_LIST  *Node;
> +
> +  Top = TRUNCATE_TO_PAGES (MaxAddress + 1 - (UINTN)Pages);
> +  if (Top > Pages->NumberOfPages) {
> +    Top = Pages->NumberOfPages;
> +  }
> +  Bottom = Top - NumberOfPages;
> +
> +  if (Top < Pages->NumberOfPages) {
> +    Node = (FREE_PAGE_LIST*)((UINTN)Pages + EFI_PAGES_TO_SIZE (Top));
> +    Node->NumberOfPages = Pages->NumberOfPages - Top;
> +    InsertHeadList (&Pages->Link, &Node->Link);
> +  }
> +
> +  if (Bottom > 0) {
> +    Pages->NumberOfPages = Bottom;
> +  } else {
> +    RemoveEntryList (&Pages->Link);
> +  }
> +
> +  return (UINTN)Pages + EFI_PAGES_TO_SIZE (Bottom);
> +}
> +
> +/**
> +  Internal Function. Allocate n pages from free page list below MaxAddress.
> +
> +  @param  FreePageList           The free page node.
> +  @param  NumberOfPages          Number of pages to be allocated.
> +  @param  MaxAddress             Request to allocate memory below this address.
> +
> +  @return Memory address of allocated pages.
> +
> +**/
> +UINTN
> +InternalAllocMaxAddress (
> +  IN OUT LIST_ENTRY  *FreePageList,
> +  IN     UINTN       NumberOfPages,
> +  IN     UINTN       MaxAddress
> +  )
> +{
> +  LIST_ENTRY      *Node;
> +  FREE_PAGE_LIST  *Pages;
> +
> +  for (Node = FreePageList->BackLink; Node != FreePageList; Node = Node->BackLink) {
> +    Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
> +    if (Pages->NumberOfPages >= NumberOfPages &&
> +        (UINTN)Pages + EFI_PAGES_TO_SIZE (NumberOfPages) - 1 <= MaxAddress) {
> +      return InternalAllocPagesOnOneNode (Pages, NumberOfPages, MaxAddress);
> +    }
> +  }
> +  return (UINTN)(-1);
> +}
> +
> +/**
> +  Internal Function. Allocate n pages from free page list at given address.
> +
> +  @param  FreePageList           The free page node.
> +  @param  NumberOfPages          Number of pages to be allocated.
> +  @param  MaxAddress             Request to allocate memory below this address.
> +
> +  @return Memory address of allocated pages.
> +
> +**/
> +UINTN
> +InternalAllocAddress (
> +  IN OUT LIST_ENTRY  *FreePageList,
> +  IN     UINTN       NumberOfPages,
> +  IN     UINTN       Address
> +  )
> +{
> +  UINTN           EndAddress;
> +  LIST_ENTRY      *Node;
> +  FREE_PAGE_LIST  *Pages;
> +
> +  if ((Address & EFI_PAGE_MASK) != 0) {
> +    return ~Address;
> +  }
> +
> +  EndAddress = Address + EFI_PAGES_TO_SIZE (NumberOfPages);
> +  for (Node = FreePageList->BackLink; Node!= FreePageList; Node = Node->BackLink) {
> +    Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
> +    if ((UINTN)Pages <= Address) {
> +      if ((UINTN)Pages + EFI_PAGES_TO_SIZE (Pages->NumberOfPages) < EndAddress) {
> +        break;
> +      }
> +      return InternalAllocPagesOnOneNode (Pages, NumberOfPages, EndAddress);
> +    }
> +  }
> +  return ~Address;
> +}
> +
> +/**
> +  Allocates pages from the memory map.
> +
> +  @param  Type                   The type of allocation to perform.
> +  @param  MemoryType             The type of memory to turn the allocated pages
> +                                 into.
> +  @param  NumberOfPages          The number of pages to allocate.
> +  @param  Memory                 A pointer to receive the base allocated memory
> +                                 address.
> +
> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
> +  @retval EFI_SUCCESS            Pages successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalAllocatePages (
> +  IN  EFI_ALLOCATE_TYPE     Type,
> +  IN  EFI_MEMORY_TYPE       MemoryType,
> +  IN  UINTN                 NumberOfPages,
> +  OUT EFI_PHYSICAL_ADDRESS  *Memory
> +  )
> +{
> +  UINTN  RequestedAddress;
> +
> +  if (MemoryType != EfiRuntimeServicesCode &&
> +      MemoryType != EfiRuntimeServicesData) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  if (NumberOfPages > TRUNCATE_TO_PAGES ((UINTN)-1) + 1) {
> +    return EFI_OUT_OF_RESOURCES;
> +  }
> +
> +  //
> +  // We don't track memory type in MM
> +  //
> +  RequestedAddress = (UINTN)*Memory;
> +  switch (Type) {
> +    case AllocateAnyPages:
> +      RequestedAddress = (UINTN)(-1);
> +    case AllocateMaxAddress:
> +      *Memory = InternalAllocMaxAddress (
> +                  &mMmMemoryMap,
> +                  NumberOfPages,
> +                  RequestedAddress
> +                  );
> +      if (*Memory == (UINTN)-1) {
> +        return EFI_OUT_OF_RESOURCES;
> +      }
> +      break;
> +    case AllocateAddress:
> +      *Memory = InternalAllocAddress (
> +                  &mMmMemoryMap,
> +                  NumberOfPages,
> +                  RequestedAddress
> +                  );
> +      if (*Memory != RequestedAddress) {
> +        return EFI_NOT_FOUND;
> +      }
> +      break;
> +    default:
> +      return EFI_INVALID_PARAMETER;
> +  }
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Allocates pages from the memory map.
> +
> +  @param  Type                   The type of allocation to perform.
> +  @param  MemoryType             The type of memory to turn the allocated pages
> +                                 into.
> +  @param  NumberOfPages          The number of pages to allocate.
> +  @param  Memory                 A pointer to receive the base allocated memory
> +                                 address.
> +
> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
> +  @retval EFI_SUCCESS            Pages successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmAllocatePages (
> +  IN  EFI_ALLOCATE_TYPE     Type,
> +  IN  EFI_MEMORY_TYPE       MemoryType,
> +  IN  UINTN                 NumberOfPages,
> +  OUT EFI_PHYSICAL_ADDRESS  *Memory
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  Status = MmInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory);
> +  return Status;
> +}
> +
> +/**
> +  Internal Function. Merge two adjacent nodes.
> +
> +  @param  First             The first of two nodes to merge.
> +
> +  @return Pointer to node after merge (if success) or pointer to next node (if fail).
> +
> +**/
> +FREE_PAGE_LIST *
> +InternalMergeNodes (
> +  IN FREE_PAGE_LIST  *First
> +  )
> +{
> +  FREE_PAGE_LIST  *Next;
> +
> +  Next = BASE_CR (First->Link.ForwardLink, FREE_PAGE_LIST, Link);
> +  ASSERT (
> +    TRUNCATE_TO_PAGES ((UINTN)Next - (UINTN)First) >= First->NumberOfPages);
> +
> +  if (TRUNCATE_TO_PAGES ((UINTN)Next - (UINTN)First) == First->NumberOfPages) {
> +    First->NumberOfPages += Next->NumberOfPages;
> +    RemoveEntryList (&Next->Link);
> +    Next = First;
> +  }
> +  return Next;
> +}
> +
> +/**
> +  Frees previous allocated pages.
> +
> +  @param  Memory                 Base address of memory being freed.
> +  @param  NumberOfPages          The number of pages to free.
> +
> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range.
> +  @retval EFI_INVALID_PARAMETER  Address not aligned.
> +  @return EFI_SUCCESS            Pages successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalFreePages (
> +  IN EFI_PHYSICAL_ADDRESS  Memory,
> +  IN UINTN                 NumberOfPages
> +  )
> +{
> +  LIST_ENTRY      *Node;
> +  FREE_PAGE_LIST  *Pages;
> +
> +  if ((Memory & EFI_PAGE_MASK) != 0) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  Pages = NULL;
> +  Node = mMmMemoryMap.ForwardLink;
> +  while (Node != &mMmMemoryMap) {
> +    Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
> +    if (Memory < (UINTN)Pages) {
> +      break;
> +    }
> +    Node = Node->ForwardLink;
> +  }
> +
> +  if (Node != &mMmMemoryMap &&
> +      Memory + EFI_PAGES_TO_SIZE (NumberOfPages) > (UINTN)Pages) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  if (Node->BackLink != &mMmMemoryMap) {
> +    Pages = BASE_CR (Node->BackLink, FREE_PAGE_LIST, Link);
> +    if ((UINTN)Pages + EFI_PAGES_TO_SIZE (Pages->NumberOfPages) > Memory) {
> +      return EFI_INVALID_PARAMETER;
> +    }
> +  }
> +
> +  Pages = (FREE_PAGE_LIST*)(UINTN)Memory;
> +  Pages->NumberOfPages = NumberOfPages;
> +  InsertTailList (Node, &Pages->Link);
> +
> +  if (Pages->Link.BackLink != &mMmMemoryMap) {
> +    Pages = InternalMergeNodes (
> +              BASE_CR (Pages->Link.BackLink, FREE_PAGE_LIST, Link)
> +              );
> +  }
> +
> +  if (Node != &mMmMemoryMap) {
> +    InternalMergeNodes (Pages);
> +  }
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Frees previous allocated pages.
> +
> +  @param  Memory                 Base address of memory being freed.
> +  @param  NumberOfPages          The number of pages to free.
> +
> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range.
> +  @retval EFI_INVALID_PARAMETER  Address not aligned.
> +  @return EFI_SUCCESS            Pages successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmFreePages (
> +  IN EFI_PHYSICAL_ADDRESS  Memory,
> +  IN UINTN                 NumberOfPages
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  Status = MmInternalFreePages (Memory, NumberOfPages);
> +  return Status;
> +}
> +
> +/**
> +  Add free MMRAM region for use by memory service.
> +
> +  @param  MemBase                Base address of memory region.
> +  @param  MemLength              Length of the memory region.
> +  @param  Type                   Memory type.
> +  @param  Attributes             Memory region state.
> +
> +**/
> +VOID
> +MmAddMemoryRegion (
> +  IN  EFI_PHYSICAL_ADDRESS  MemBase,
> +  IN  UINT64                MemLength,
> +  IN  EFI_MEMORY_TYPE       Type,
> +  IN  UINT64                Attributes
> +  )
> +{
> +  UINTN  AlignedMemBase;
> +
> +  //
> +  // Do not add memory regions that is already allocated, needs testing, or needs ECC initialization
> +  //
> +  if ((Attributes & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {
> +    return;
> +  }
> +
> +  //
> +  // Align range on an EFI_PAGE_SIZE boundary
> +  //
> +  AlignedMemBase = (UINTN)(MemBase + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
> +  MemLength -= AlignedMemBase - MemBase;
> +  MmFreePages (AlignedMemBase, TRUNCATE_TO_PAGES ((UINTN)MemLength));
> +}
> diff --git a/StandaloneMmPkg/Core/Pool.c b/StandaloneMmPkg/Core/Pool.c
> new file mode 100644
> index 0000000000..bdf1258381
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/Pool.c
> @@ -0,0 +1,287 @@
> +/** @file
> +  SMM Memory pool management functions.
> +
> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +LIST_ENTRY  mMmPoolLists[MAX_POOL_INDEX];
> +//
> +// To cache the MMRAM base since when Loading modules At fixed address feature is enabled,
> +// all module is assigned an offset relative the MMRAM base in build time.
> +//
> +GLOBAL_REMOVE_IF_UNREFERENCED  EFI_PHYSICAL_ADDRESS       gLoadModuleAtFixAddressMmramBase = 0;
> +
> +/**
> +  Called to initialize the memory service.
> +
> +  @param   MmramRangeCount       Number of MMRAM Regions
> +  @param   MmramRanges           Pointer to MMRAM Descriptors
> +
> +**/
> +VOID
> +MmInitializeMemoryServices (
> +  IN UINTN                 MmramRangeCount,
> +  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
> +  )
> +{
> +  UINTN                  Index;
> +
> +  //
> +  // Initialize Pool list
> +  //
> +  for (Index = sizeof (mMmPoolLists) / sizeof (*mMmPoolLists); Index > 0;) {
> +    InitializeListHead (&mMmPoolLists[--Index]);
> +  }
> +
> +
> +  //
> +  // Initialize free MMRAM regions
> +  //
> +  for (Index = 0; Index < MmramRangeCount; Index++) {
> +    //
> +    // BUGBUG: Add legacy MMRAM region is buggy.
> +    //
> +    if (MmramRanges[Index].CpuStart < BASE_1MB) {
> +      continue;
> +    }
> +    DEBUG ((DEBUG_INFO, "MmAddMemoryRegion %d : 0x%016lx - 0x%016lx\n", Index, MmramRanges[Index].CpuStart, MmramRanges[Index].PhysicalSize));
> +    MmAddMemoryRegion (
> +      MmramRanges[Index].CpuStart,
> +      MmramRanges[Index].PhysicalSize,
> +      EfiConventionalMemory,
> +      MmramRanges[Index].RegionState
> +      );
> +  }
> +
> +}
> +
> +/**
> +  Internal Function. Allocate a pool by specified PoolIndex.
> +
> +  @param  PoolIndex             Index which indicate the Pool size.
> +  @param  FreePoolHdr           The returned Free pool.
> +
> +  @retval EFI_OUT_OF_RESOURCES   Allocation failed.
> +  @retval EFI_SUCCESS            Pool successfully allocated.
> +
> +**/
> +EFI_STATUS
> +InternalAllocPoolByIndex (
> +  IN  UINTN             PoolIndex,
> +  OUT FREE_POOL_HEADER  **FreePoolHdr
> +  )
> +{
> +  EFI_STATUS            Status;
> +  FREE_POOL_HEADER      *Hdr;
> +  EFI_PHYSICAL_ADDRESS  Address;
> +
> +  ASSERT (PoolIndex <= MAX_POOL_INDEX);
> +  Status = EFI_SUCCESS;
> +  Hdr = NULL;
> +  if (PoolIndex == MAX_POOL_INDEX) {
> +    Status = MmInternalAllocatePages (AllocateAnyPages, EfiRuntimeServicesData, EFI_SIZE_TO_PAGES (MAX_POOL_SIZE << 1), &Address);
> +    if (EFI_ERROR (Status)) {
> +      return EFI_OUT_OF_RESOURCES;
> +    }
> +    Hdr = (FREE_POOL_HEADER *) (UINTN) Address;
> +  } else if (!IsListEmpty (&mMmPoolLists[PoolIndex])) {
> +    Hdr = BASE_CR (GetFirstNode (&mMmPoolLists[PoolIndex]), FREE_POOL_HEADER, Link);
> +    RemoveEntryList (&Hdr->Link);
> +  } else {
> +    Status = InternalAllocPoolByIndex (PoolIndex + 1, &Hdr);
> +    if (!EFI_ERROR (Status)) {
> +      Hdr->Header.Size >>= 1;
> +      Hdr->Header.Available = TRUE;
> +      InsertHeadList (&mMmPoolLists[PoolIndex], &Hdr->Link);
> +      Hdr = (FREE_POOL_HEADER*)((UINT8*)Hdr + Hdr->Header.Size);
> +    }
> +  }
> +
> +  if (!EFI_ERROR (Status)) {
> +    Hdr->Header.Size = MIN_POOL_SIZE << PoolIndex;
> +    Hdr->Header.Available = FALSE;
> +  }
> +
> +  *FreePoolHdr = Hdr;
> +  return Status;
> +}
> +
> +/**
> +  Internal Function. Free a pool by specified PoolIndex.
> +
> +  @param  FreePoolHdr           The pool to free.
> +
> +  @retval EFI_SUCCESS           Pool successfully freed.
> +
> +**/
> +EFI_STATUS
> +InternalFreePoolByIndex (
> +  IN FREE_POOL_HEADER  *FreePoolHdr
> +  )
> +{
> +  UINTN  PoolIndex;
> +
> +  ASSERT ((FreePoolHdr->Header.Size & (FreePoolHdr->Header.Size - 1)) == 0);
> +  ASSERT (((UINTN)FreePoolHdr & (FreePoolHdr->Header.Size - 1)) == 0);
> +  ASSERT (FreePoolHdr->Header.Size >= MIN_POOL_SIZE);
> +
> +  PoolIndex = (UINTN) (HighBitSet32 ((UINT32)FreePoolHdr->Header.Size) - MIN_POOL_SHIFT);
> +  FreePoolHdr->Header.Available = TRUE;
> +  ASSERT (PoolIndex < MAX_POOL_INDEX);
> +  InsertHeadList (&mMmPoolLists[PoolIndex], &FreePoolHdr->Link);
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Allocate pool of a particular type.
> +
> +  @param  PoolType               Type of pool to allocate.
> +  @param  Size                   The amount of pool to allocate.
> +  @param  Buffer                 The address to return a pointer to the allocated
> +                                 pool.
> +
> +  @retval EFI_INVALID_PARAMETER  PoolType not valid.
> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
> +  @retval EFI_SUCCESS            Pool successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalAllocatePool (
> +  IN   EFI_MEMORY_TYPE  PoolType,
> +  IN   UINTN            Size,
> +  OUT  VOID             **Buffer
> +  )
> +{
> +  POOL_HEADER           *PoolHdr;
> +  FREE_POOL_HEADER      *FreePoolHdr;
> +  EFI_STATUS            Status;
> +  EFI_PHYSICAL_ADDRESS  Address;
> +  UINTN                 PoolIndex;
> +
> +  if (PoolType != EfiRuntimeServicesCode &&
> +      PoolType != EfiRuntimeServicesData) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  Size += sizeof (*PoolHdr);
> +  if (Size > MAX_POOL_SIZE) {
> +    Size = EFI_SIZE_TO_PAGES (Size);
> +    Status = MmInternalAllocatePages (AllocateAnyPages, PoolType, Size, &Address);
> +    if (EFI_ERROR (Status)) {
> +      return Status;
> +    }
> +
> +    PoolHdr = (POOL_HEADER*)(UINTN)Address;
> +    PoolHdr->Size = EFI_PAGES_TO_SIZE (Size);
> +    PoolHdr->Available = FALSE;
> +    *Buffer = PoolHdr + 1;
> +    return Status;
> +  }
> +
> +  Size = (Size + MIN_POOL_SIZE - 1) >> MIN_POOL_SHIFT;
> +  PoolIndex = (UINTN) HighBitSet32 ((UINT32)Size);
> +  if ((Size & (Size - 1)) != 0) {
> +    PoolIndex++;
> +  }
> +
> +  Status = InternalAllocPoolByIndex (PoolIndex, &FreePoolHdr);
> +  if (!EFI_ERROR(Status)) {
> +    *Buffer = &FreePoolHdr->Header + 1;
> +  }
> +  return Status;
> +}
> +
> +/**
> +  Allocate pool of a particular type.
> +
> +  @param  PoolType               Type of pool to allocate.
> +  @param  Size                   The amount of pool to allocate.
> +  @param  Buffer                 The address to return a pointer to the allocated
> +                                 pool.
> +
> +  @retval EFI_INVALID_PARAMETER  PoolType not valid.
> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
> +  @retval EFI_SUCCESS            Pool successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmAllocatePool (
> +  IN   EFI_MEMORY_TYPE  PoolType,
> +  IN   UINTN            Size,
> +  OUT  VOID             **Buffer
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  Status = MmInternalAllocatePool (PoolType, Size, Buffer);
> +  return Status;
> +}
> +
> +/**
> +  Frees pool.
> +
> +  @param  Buffer                 The allocated pool entry to free.
> +
> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
> +  @retval EFI_SUCCESS            Pool successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalFreePool (
> +  IN VOID  *Buffer
> +  )
> +{
> +  FREE_POOL_HEADER  *FreePoolHdr;
> +
> +  if (Buffer == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  FreePoolHdr = (FREE_POOL_HEADER*)((POOL_HEADER*)Buffer - 1);
> +  ASSERT (!FreePoolHdr->Header.Available);
> +
> +  if (FreePoolHdr->Header.Size > MAX_POOL_SIZE) {
> +    ASSERT (((UINTN)FreePoolHdr & EFI_PAGE_MASK) == 0);
> +    ASSERT ((FreePoolHdr->Header.Size & EFI_PAGE_MASK) == 0);
> +    return MmInternalFreePages (
> +             (EFI_PHYSICAL_ADDRESS)(UINTN)FreePoolHdr,
> +             EFI_SIZE_TO_PAGES (FreePoolHdr->Header.Size)
> +             );
> +  }
> +  return InternalFreePoolByIndex (FreePoolHdr);
> +}
> +
> +/**
> +  Frees pool.
> +
> +  @param  Buffer                 The allocated pool entry to free.
> +
> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
> +  @retval EFI_SUCCESS            Pool successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmFreePool (
> +  IN VOID  *Buffer
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  Status = MmInternalFreePool (Buffer);
> +  return Status;
> +}
> diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.c b/StandaloneMmPkg/Core/StandaloneMmCore.c
> new file mode 100644
> index 0000000000..0bb99b9710
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/StandaloneMmCore.c
> @@ -0,0 +1,708 @@
> +/** @file
> +  MM Core Main Entry Point
> +
> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "StandaloneMmCore.h"
> +
> +EFI_STATUS
> +MmCoreFfsFindMmDriver (
> +  IN  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader
> +  );
> +
> +EFI_STATUS
> +MmDispatcher (
> +  VOID
> +  );
> +
> +//
> +// Globals used to initialize the protocol
> +//
> +EFI_HANDLE            mMmCpuHandle = NULL;
> +
> +//
> +// Physical pointer to private structure shared between MM IPL and the MM Core
> +//
> +MM_CORE_PRIVATE_DATA  *gMmCorePrivate;
> +
> +//
> +// MM Core global variable for MM System Table.  Only accessed as a physical structure in MMRAM.
> +//
> +EFI_MM_SYSTEM_TABLE  gMmCoreMmst = {
> +
> +  // The table header for the MMST.
> +  {
> +    MM_MMST_SIGNATURE,
> +    EFI_MM_SYSTEM_TABLE_REVISION,
> +    sizeof (gMmCoreMmst.Hdr)
> +  },
> +  // MmFirmwareVendor
> +  NULL,
> +  // MmFirmwareRevision
> +  0,
> +  // MmInstallConfigurationTable
> +  MmInstallConfigurationTable,
> +  // I/O Service
> +  {
> +    {
> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5,       // MmMemRead
> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5        // MmMemWrite
> +    },
> +    {
> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5,       // MmIoRead
> +      (EFI_MM_CPU_IO) MmEfiNotAvailableYetArg5        // MmIoWrite
> +    }
> +  },
> +  // Runtime memory services
> +  MmAllocatePool,
> +  MmFreePool,
> +  MmAllocatePages,
> +  MmFreePages,
> +  // MP service
> +  NULL,                          // MmStartupThisAp
> +  0,                             // CurrentlyExecutingCpu
> +  0,                             // NumberOfCpus
> +  NULL,                          // CpuSaveStateSize
> +  NULL,                          // CpuSaveState
> +  0,                             // NumberOfTableEntries
> +  NULL,                          // MmConfigurationTable
> +  MmInstallProtocolInterface,
> +  MmUninstallProtocolInterface,
> +  MmHandleProtocol,
> +  MmRegisterProtocolNotify,
> +  MmLocateHandle,
> +  MmLocateProtocol,
> +  MmiManage,
> +  MmiHandlerRegister,
> +  MmiHandlerUnRegister
> +};
> +
> +//
> +// Flag to determine if the platform has performed a legacy boot.
> +// If this flag is TRUE, then the runtime code and runtime data associated with the
> +// MM IPL are converted to free memory, so the MM Core must guarantee that is
> +// does not touch of the code/data associated with the MM IPL if this flag is TRUE.
> +//
> +BOOLEAN  mInLegacyBoot = FALSE;
> +
> +//
> +// Table of MMI Handlers that are registered by the MM Core when it is initialized
> +//
> +MM_CORE_MMI_HANDLERS  mMmCoreMmiHandlers[] = {
> +  { MmFvDispatchHandler,     &gMmFvDispatchGuid,                 NULL, TRUE  },
> +  { MmDriverDispatchHandler, &gEfiEventDxeDispatchGuid,          NULL, TRUE  },
> +  { MmReadyToLockHandler,    &gEfiDxeMmReadyToLockProtocolGuid,  NULL, TRUE  },
> +  { MmEndOfDxeHandler,       &gEfiEndOfDxeEventGroupGuid,        NULL, FALSE },
> +  { MmLegacyBootHandler,     &gEfiEventLegacyBootGuid,           NULL, FALSE },
> +  { MmExitBootServiceHandler,&gEfiEventExitBootServicesGuid,     NULL, FALSE },
> +  { MmReadyToBootHandler,    &gEfiEventReadyToBootGuid,          NULL, FALSE },
> +  { NULL,                    NULL,                               NULL, FALSE },
> +};
> +
> +EFI_SYSTEM_TABLE                *mEfiSystemTable;
> +UINTN                           mMmramRangeCount;
> +EFI_MMRAM_DESCRIPTOR            *mMmramRanges;
> +
> +/**
> +  Place holder function until all the MM System Table Service are available.
> +
> +  Note: This function is only used by MMRAM invocation.  It is never used by DXE invocation.
> +
> +  @param  Arg1                   Undefined
> +  @param  Arg2                   Undefined
> +  @param  Arg3                   Undefined
> +  @param  Arg4                   Undefined
> +  @param  Arg5                   Undefined
> +
> +  @return EFI_NOT_AVAILABLE_YET
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmEfiNotAvailableYetArg5 (
> +  UINTN Arg1,
> +  UINTN Arg2,
> +  UINTN Arg3,
> +  UINTN Arg4,
> +  UINTN Arg5
> +  )
> +{
> +  //
> +  // This function should never be executed.  If it does, then the architectural protocols
> +  // have not been designed correctly.
> +  //
> +  return EFI_NOT_AVAILABLE_YET;
> +}
> +
> +/**
> +  Software MMI handler that is called when a Legacy Boot event is signaled.  The MM
> +  Core uses this signal to know that a Legacy Boot has been performed and that
> +  gMmCorePrivate that is shared between the UEFI and MM execution environments can
> +  not be accessed from MM anymore since that structure is considered free memory by
> +  a legacy OS.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLegacyBootHandler (
> +  IN     EFI_HANDLE  DispatchHandle,
> +  IN     CONST VOID  *Context,        OPTIONAL
> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_HANDLE  MmHandle;
> +  EFI_STATUS  Status = EFI_SUCCESS;
> +
> +  if (!mInLegacyBoot) {
> +    MmHandle = NULL;
> +    Status = MmInstallProtocolInterface (
> +               &MmHandle,
> +               &gEfiEventLegacyBootGuid,
> +               EFI_NATIVE_INTERFACE,
> +               NULL
> +               );
> +  }
> +  mInLegacyBoot = TRUE;
> +  return Status;
> +}
> +
> +/**
> +  Software MMI handler that is called when a ExitBoot Service event is signaled.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmExitBootServiceHandler (
> +  IN     EFI_HANDLE  DispatchHandle,
> +  IN     CONST VOID  *Context,        OPTIONAL
> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_HANDLE  MmHandle;
> +  EFI_STATUS  Status = EFI_SUCCESS;
> +  STATIC BOOLEAN mInExitBootServices = FALSE;
> +
> +  if (!mInExitBootServices) {
> +    MmHandle = NULL;
> +    Status = MmInstallProtocolInterface (
> +               &MmHandle,
> +               &gEfiEventExitBootServicesGuid,
> +               EFI_NATIVE_INTERFACE,
> +               NULL
> +               );
> +  }
> +  mInExitBootServices = TRUE;
> +  return Status;
> +}
> +
> +/**
> +  Software MMI handler that is called when a ExitBoot Service event is signaled.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmReadyToBootHandler (
> +  IN     EFI_HANDLE  DispatchHandle,
> +  IN     CONST VOID  *Context,        OPTIONAL
> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_HANDLE  MmHandle;
> +  EFI_STATUS  Status = EFI_SUCCESS;
> +  STATIC BOOLEAN mInReadyToBoot = FALSE;
> +
> +  if (!mInReadyToBoot) {
> +    MmHandle = NULL;
> +    Status = MmInstallProtocolInterface (
> +               &MmHandle,
> +               &gEfiEventReadyToBootGuid,
> +               EFI_NATIVE_INTERFACE,
> +               NULL
> +               );
> +  }
> +  mInReadyToBoot = TRUE;
> +  return Status;
> +}
> +
> +/**
> +  Software MMI handler that is called when the DxeMmReadyToLock protocol is added
> +  or if gEfiEventReadyToBootGuid is signaled.  This function unregisters the
> +  Software SMIs that are nor required after MMRAM is locked and installs the
> +  MM Ready To Lock Protocol so MM Drivers are informed that MMRAM is about
> +  to be locked.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmReadyToLockHandler (
> +  IN     EFI_HANDLE  DispatchHandle,
> +  IN     CONST VOID  *Context,        OPTIONAL
> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_STATUS  Status;
> +  UINTN       Index;
> +  EFI_HANDLE  MmHandle;
> +
> +  DEBUG ((DEBUG_INFO, "MmReadyToLockHandler\n"));
> +
> +  //
> +  // Unregister MMI Handlers that are no longer required after the MM driver dispatch is stopped
> +  //
> +  for (Index = 0; mMmCoreMmiHandlers[Index].HandlerType != NULL; Index++) {
> +    if (mMmCoreMmiHandlers[Index].UnRegister) {
> +      MmiHandlerUnRegister (mMmCoreMmiHandlers[Index].DispatchHandle);
> +    }
> +  }
> +
> +  //
> +  // Install MM Ready to lock protocol
> +  //
> +  MmHandle = NULL;
> +  Status = MmInstallProtocolInterface (
> +             &MmHandle,
> +             &gEfiMmReadyToLockProtocolGuid,
> +             EFI_NATIVE_INTERFACE,
> +             NULL
> +             );
> +
> +  //
> +  // Make sure MM CPU I/O 2 Protocol has been installed into the handle database
> +  //
> +  //Status = MmLocateProtocol (&EFI_MM_CPU_IO_PROTOCOL_GUID, NULL, &Interface);
> +
> +  //
> +  // Print a message on a debug build if the MM CPU I/O 2 Protocol is not installed
> +  //
> +  //if (EFI_ERROR (Status)) {
> +      //DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));
> +  //}
> +
> +
> +  //
> +  // Assert if the CPU I/O 2 Protocol is not installed
> +  //
> +  //ASSERT_EFI_ERROR (Status);
> +
> +  //
> +  // Display any drivers that were not dispatched because dependency expression
> +  // evaluated to false if this is a debug build
> +  //
> +  //MmDisplayDiscoveredNotDispatched ();
> +
> +  return Status;
> +}
> +
> +/**
> +  Software MMI handler that is called when the EndOfDxe event is signaled.
> +  This function installs the MM EndOfDxe Protocol so MM Drivers are informed that
> +  platform code will invoke 3rd part code.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmEndOfDxeHandler (
> +  IN     EFI_HANDLE  DispatchHandle,
> +  IN     CONST VOID  *Context,        OPTIONAL
> +  IN OUT VOID        *CommBuffer,     OPTIONAL
> +  IN OUT UINTN       *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_STATUS  Status;
> +  EFI_HANDLE  MmHandle;
> +
> +  DEBUG ((DEBUG_INFO, "MmEndOfDxeHandler\n"));
> +  //
> +  // Install MM EndOfDxe protocol
> +  //
> +  MmHandle = NULL;
> +  Status = MmInstallProtocolInterface (
> +             &MmHandle,
> +             &gEfiMmEndOfDxeProtocolGuid,
> +             EFI_NATIVE_INTERFACE,
> +             NULL
> +             );
> +  return Status;
> +}
> +
> +
> +
> +/**
> +  The main entry point to MM Foundation.
> +
> +  Note: This function is only used by MMRAM invocation.  It is never used by DXE invocation.
> +
> +  @param  MmEntryContext           Processor information and functionality
> +                                    needed by MM Foundation.
> +
> +**/
> +VOID
> +EFIAPI
> +MmEntryPoint (
> +  IN CONST EFI_MM_ENTRY_CONTEXT  *MmEntryContext
> +)
> +{
> +  EFI_STATUS                  Status;
> +  EFI_MM_COMMUNICATE_HEADER  *CommunicateHeader;
> +  BOOLEAN                     InLegacyBoot;
> +
> +  DEBUG ((DEBUG_INFO, "MmEntryPoint ...\n"));
> +
> +  //
> +  // Update MMST using the context
> +  //
> +  CopyMem (&gMmCoreMmst.MmStartupThisAp, MmEntryContext, sizeof (EFI_MM_ENTRY_CONTEXT));
> +
> +  //
> +  // Call platform hook before Mm Dispatch
> +  //
> +  //PlatformHookBeforeMmDispatch ();
> +
> +  //
> +  // If a legacy boot has occured, then make sure gMmCorePrivate is not accessed
> +  //
> +  InLegacyBoot = mInLegacyBoot;
> +  if (!InLegacyBoot) {
> +    //
> +    // TBD: Mark the InMm flag as TRUE
> +    //
> +    gMmCorePrivate->InMm = TRUE;
> +
> +    //
> +    // Check to see if this is a Synchronous MMI sent through the MM Communication
> +    // Protocol or an Asynchronous MMI
> +    //
> +    if (gMmCorePrivate->CommunicationBuffer != 0) {
> +      //
> +      // Synchronous MMI for MM Core or request from Communicate protocol
> +      //
> +      if (!MmIsBufferOutsideMmValid ((UINTN)gMmCorePrivate->CommunicationBuffer, gMmCorePrivate->BufferSize)) {
> +        //
> +        // If CommunicationBuffer is not in valid address scope, return EFI_INVALID_PARAMETER
> +        //
> +        gMmCorePrivate->CommunicationBuffer = 0;
> +        gMmCorePrivate->ReturnStatus = EFI_INVALID_PARAMETER;
> +      } else {
> +        CommunicateHeader = (EFI_MM_COMMUNICATE_HEADER *)(UINTN)gMmCorePrivate->CommunicationBuffer;
> +        gMmCorePrivate->BufferSize -= OFFSET_OF (EFI_MM_COMMUNICATE_HEADER, Data);
> +        //DEBUG ((DEBUG_INFO, "CommunicateHeader->HeaderGuid - %g\n", &CommunicateHeader->HeaderGuid));
> +        Status = MmiManage (
> +                   &CommunicateHeader->HeaderGuid,
> +                   NULL,
> +                   CommunicateHeader->Data,
> +                   (UINTN *)&gMmCorePrivate->BufferSize
> +                   );
> +        //
> +        // Update CommunicationBuffer, BufferSize and ReturnStatus
> +        // Communicate service finished, reset the pointer to CommBuffer to NULL
> +        //
> +        gMmCorePrivate->BufferSize += OFFSET_OF (EFI_MM_COMMUNICATE_HEADER, Data);
> +        gMmCorePrivate->CommunicationBuffer = 0;
> +        gMmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
> +      }
> +    }
> +  }
> +
> +  //
> +  // Process Asynchronous MMI sources
> +  //
> +  MmiManage (NULL, NULL, NULL, NULL);
> +
> +  //
> +  // TBD: Do not use private data structure ?
> +  //
> +
> +  //
> +  // If a legacy boot has occured, then make sure gMmCorePrivate is not accessed
> +  //
> +  if (!InLegacyBoot) {
> +    //
> +    // Clear the InMm flag as we are going to leave MM
> +    //
> +    gMmCorePrivate->InMm = FALSE;
> +  }
> +
> +  DEBUG ((DEBUG_INFO, "MmEntryPoint Done\n"));
> +}
> +
> +EFI_STATUS
> +EFIAPI
> +MmConfigurationMmNotify (
> +  IN CONST EFI_GUID *Protocol,
> +  IN VOID           *Interface,
> +  IN EFI_HANDLE      Handle
> +  )
> +{
> +  EFI_STATUS                      Status;
> +  EFI_MM_CONFIGURATION_PROTOCOL  *MmConfiguration;
> +
> +  DEBUG ((DEBUG_INFO, "MmConfigurationMmNotify(%g) - %x\n", Protocol, Interface));
> +
> +  MmConfiguration = Interface;
> +
> +  //
> +  // Register the MM Entry Point provided by the MM Core with the MM COnfiguration protocol
> +  //
> +  Status = MmConfiguration->RegisterMmEntry (MmConfiguration, (EFI_MM_ENTRY_POINT)(UINTN)gMmCorePrivate->MmEntryPoint);
> +  ASSERT_EFI_ERROR (Status);
> +
> +  //
> +  // Set flag to indicate that the MM Entry Point has been registered which
> +  // means that MMIs are now fully operational.
> +  //
> +  gMmCorePrivate->MmEntryPointRegistered = TRUE;
> +
> +  //
> +  // Print debug message showing MM Core entry point address.
> +  //
> +  DEBUG ((DEBUG_INFO, "MM Core registered MM Entry Point address %p\n", (VOID *)(UINTN)gMmCorePrivate->MmEntryPoint));
> +  return EFI_SUCCESS;
> +}
> +
> +UINTN
> +GetHobListSize (
> +  IN VOID *HobStart
> +  )
> +{
> +  EFI_PEI_HOB_POINTERS  Hob;
> +
> +  ASSERT (HobStart != NULL);
> +
> +  Hob.Raw = (UINT8 *) HobStart;
> +  while (!END_OF_HOB_LIST (Hob)) {
> +    Hob.Raw = GET_NEXT_HOB (Hob);
> +  }
> +  //
> +  // Need plus END_OF_HOB_LIST
> +  //
> +  return (UINTN)Hob.Raw - (UINTN)HobStart + sizeof(EFI_HOB_GENERIC_HEADER);
> +}
> +
> +/**
> +  The Entry Point for MM Core
> +
> +  Install DXE Protocols and reload MM Core into MMRAM and register MM Core
> +  EntryPoint on the MMI vector.
> +
> +  Note: This function is called for both DXE invocation and MMRAM invocation.
> +
> +  @param  ImageHandle    The firmware allocated handle for the EFI image.
> +  @param  SystemTable    A pointer to the EFI System Table.
> +
> +  @retval EFI_SUCCESS    The entry point is executed successfully.
> +  @retval Other          Some error occurred when executing this entry point.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +StandaloneMmMain (
> +  IN VOID  *HobStart
> +  )
> +{
> +  EFI_STATUS                      Status;
> +  UINTN                           Index;
> +  VOID                            *MmHobStart;
> +  UINTN                           HobSize;
> +  VOID                            *Registration;
> +  EFI_HOB_GUID_TYPE               *GuidHob;
> +  MM_CORE_DATA_HOB_DATA           *DataInHob;
> +  EFI_HOB_GUID_TYPE               *MmramRangesHob;
> +  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
> +  EFI_MMRAM_DESCRIPTOR            *MmramRanges;
> +  UINT32                          MmramRangeCount;
> +  EFI_HOB_FIRMWARE_VOLUME         *BfvHob;
> +
> +  ProcessLibraryConstructorList (HobStart, &gMmCoreMmst);
> +
> +  DEBUG ((DEBUG_INFO, "MmMain - 0x%x\n", HobStart));
> +
> +  //
> +  // Determine if the caller has passed a reference to a MM_CORE_PRIVATE_DATA
> +  // structure in the Hoblist. This choice will govern how boot information is
> +  // extracted later.
> +  //
> +  GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);
> +  if (GuidHob == NULL) {
> +    //
> +    // Allocate and zero memory for a MM_CORE_PRIVATE_DATA table and then
> +    // initialise it
> +    //
> +    gMmCorePrivate = (MM_CORE_PRIVATE_DATA *) AllocateRuntimePages(EFI_SIZE_TO_PAGES(sizeof(MM_CORE_PRIVATE_DATA)));
> +    SetMem ((VOID *)(UINTN)gMmCorePrivate, sizeof(MM_CORE_PRIVATE_DATA), 0);
> +    gMmCorePrivate->Signature = MM_CORE_PRIVATE_DATA_SIGNATURE;
> +    gMmCorePrivate->MmEntryPointRegistered = FALSE;
> +    gMmCorePrivate->InMm = FALSE;
> +    gMmCorePrivate->ReturnStatus = EFI_SUCCESS;
> +
> +    //
> +    // Extract the MMRAM ranges from the MMRAM descriptor HOB
> +    //
> +    MmramRangesHob = GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid, HobStart);
> +    if (MmramRangesHob == NULL)
> +      return EFI_UNSUPPORTED;
> +
> +    MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
> +    ASSERT (MmramRangesHobData != NULL);
> +    MmramRanges = MmramRangesHobData->Descriptor;
> +    MmramRangeCount = MmramRangesHobData->NumberOfMmReservedRegions;
> +    ASSERT (MmramRanges);
> +    ASSERT (MmramRangeCount);
> +
> +    //
> +    // Copy the MMRAM ranges into MM_CORE_PRIVATE_DATA table just in case any
> +    // code relies on them being present there
> +    //
> +    gMmCorePrivate->MmramRangeCount = MmramRangeCount;
> +    gMmCorePrivate->MmramRanges = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (MmramRangeCount * sizeof(EFI_MMRAM_DESCRIPTOR));
> +    ASSERT (gMmCorePrivate->MmramRanges != 0);
> +    CopyMem ((VOID *)(UINTN)gMmCorePrivate->MmramRanges,
> +             MmramRanges,
> +             MmramRangeCount * sizeof(EFI_MMRAM_DESCRIPTOR));
> +  } else {
> +    DataInHob       = GET_GUID_HOB_DATA (GuidHob);
> +    gMmCorePrivate = (MM_CORE_PRIVATE_DATA *)(UINTN)DataInHob->Address;
> +    MmramRanges     = (EFI_MMRAM_DESCRIPTOR *)(UINTN)gMmCorePrivate->MmramRanges;
> +    MmramRangeCount = gMmCorePrivate->MmramRangeCount;
> +  }
> +
> +  //
> +  // Print the MMRAM ranges passed by the caller
> +  //
> +  DEBUG ((DEBUG_INFO, "MmramRangeCount - 0x%x\n", MmramRangeCount));
> +  for (Index = 0; Index < MmramRangeCount; Index++) {
> +          DEBUG ((DEBUG_INFO, "MmramRanges[%d]: 0x%016lx - 0x%lx\n", Index,
> +                  MmramRanges[Index].CpuStart,
> +                  MmramRanges[Index].PhysicalSize));
> +  }
> +
> +  //
> +  // Copy the MMRAM ranges into private MMRAM
> +  //
> +  mMmramRangeCount = MmramRangeCount;
> +  DEBUG ((DEBUG_INFO, "mMmramRangeCount - 0x%x\n", mMmramRangeCount));
> +  mMmramRanges = AllocatePool (mMmramRangeCount * sizeof (EFI_MMRAM_DESCRIPTOR));
> +  DEBUG ((DEBUG_INFO, "mMmramRanges - 0x%x\n", mMmramRanges));
> +  ASSERT (mMmramRanges != NULL);
> +  CopyMem (mMmramRanges, (VOID *)(UINTN)MmramRanges, mMmramRangeCount * sizeof (EFI_MMRAM_DESCRIPTOR));
> +
> +  //
> +  // Get Boot Firmware Volume address from the BFV Hob
> +  //
> +  BfvHob = GetFirstHob (EFI_HOB_TYPE_FV);
> +  if (BfvHob != NULL) {
> +    DEBUG ((DEBUG_INFO, "BFV address - 0x%x\n", BfvHob->BaseAddress));
> +    DEBUG ((DEBUG_INFO, "BFV size    - 0x%x\n", BfvHob->Length));
> +    gMmCorePrivate->StandaloneBfvAddress = BfvHob->BaseAddress;
> +  }
> +
> +  gMmCorePrivate->Mmst          = (EFI_PHYSICAL_ADDRESS)(UINTN)&gMmCoreMmst;
> +  gMmCorePrivate->MmEntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)MmEntryPoint;
> +
> +  //
> +  // No need to initialize memory service.
> +  // It is done in constructor of StandaloneMmCoreMemoryAllocationLib(),
> +  // so that the library linked with StandaloneMmCore can use AllocatePool() in constuctor.
> +  //
> +
> +  DEBUG ((DEBUG_INFO, "MmInstallConfigurationTable For HobList\n"));
> +  //
> +  // Install HobList
> +  //
> +  HobSize = GetHobListSize (HobStart);
> +  DEBUG ((DEBUG_INFO, "HobSize - 0x%x\n", HobSize));
> +  MmHobStart = AllocatePool (HobSize);
> +  DEBUG ((DEBUG_INFO, "MmHobStart - 0x%x\n", MmHobStart));
> +  ASSERT (MmHobStart != NULL);
> +  CopyMem (MmHobStart, HobStart, HobSize);
> +  Status = MmInstallConfigurationTable (&gMmCoreMmst, &gEfiHobListGuid, MmHobStart, HobSize);
> +  ASSERT_EFI_ERROR (Status);
> +
> +  //
> +  // Register notification for EFI_MM_CONFIGURATION_PROTOCOL registration and
> +  // use it to register the MM Foundation entrypoint
> +  //
> +  DEBUG ((DEBUG_INFO, "MmRegisterProtocolNotify - MmConfigurationMmProtocol\n"));
> +  Status = MmRegisterProtocolNotify (
> +             &gEfiMmConfigurationProtocolGuid,
> +             MmConfigurationMmNotify,
> +             &Registration
> +             );
> +  ASSERT_EFI_ERROR (Status);
> +
> +  //
> +  // Dispatch standalone BFV
> +  //
> +  DEBUG ((DEBUG_INFO, "Mm Dispatch StandaloneBfvAddress - 0x%08x\n", gMmCorePrivate->StandaloneBfvAddress));
> +  if (gMmCorePrivate->StandaloneBfvAddress != 0) {
> +    MmCoreFfsFindMmDriver ((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)gMmCorePrivate->StandaloneBfvAddress);
> +    MmDispatcher ();
> +  }
> +
> +  //
> +  // Register all handlers in the core table
> +  //
> +  for (Index = 0; mMmCoreMmiHandlers[Index].HandlerType != NULL; Index++) {
> +    Status = MmiHandlerRegister(mMmCoreMmiHandlers[Index].Handler,
> +                                mMmCoreMmiHandlers[Index].HandlerType,
> +                                &mMmCoreMmiHandlers[Index].DispatchHandle);
> +    DEBUG ((DEBUG_INFO, "MmiHandlerRegister - GUID %g - Status %d\n", mMmCoreMmiHandlers[Index].HandlerType, Status));
> +  }
> +
> +  DEBUG ((DEBUG_INFO, "MmMain Done!\n"));
> +
> +  return EFI_SUCCESS;
> +}
> diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.h b/StandaloneMmPkg/Core/StandaloneMmCore.h
> new file mode 100644
> index 0000000000..53921b7844
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/StandaloneMmCore.h
> @@ -0,0 +1,903 @@
> +/** @file
> +  The internal header file includes the common header files, defines
> +  internal structure and functions used by MmCore module.
> +
> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _MM_CORE_H_
> +#define _MM_CORE_H_
> +
> +#include <PiMm.h>
> +#include <StandaloneMm.h>
> +
> +#include <Protocol/DxeMmReadyToLock.h>
> +#include <Protocol/MmReadyToLock.h>
> +#include <Protocol/MmEndOfDxe.h>
> +#include <Protocol/MmCommunication.h>
> +#include <Protocol/LoadedImage.h>
> +#include <Protocol/MmConfiguration.h>
> +
> +#include <Guid/Apriori.h>
> +#include <Guid/EventGroup.h>
> +#include <Guid/EventLegacyBios.h>
> +#include <Guid/ZeroGuid.h>
> +#include <Guid/MemoryProfile.h>
> +#include <Guid/HobList.h>
> +#include <Guid/MmFvDispatch.h>
> +#include <Guid/MmramMemoryReserve.h>
> +
> +#include <Library/MmCoreStandaloneEntryPoint.h>
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/PeCoffLib.h>
> +#include <Library/CacheMaintenanceLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/ReportStatusCodeLib.h>
> +#include <Library/MemoryAllocationLib.h>
> +#include <Library/PcdLib.h>
> +//#include <Library/MmCorePlatformHookLib.h>
> +#include <Library/MemLib.h>
> +#include <Library/HobLib.h>
> +
> +#include "StandaloneMmCorePrivateData.h"
> +
> +//
> +// Used to build a table of MMI Handlers that the MM Core registers
> +//
> +typedef struct {
> +  EFI_MM_HANDLER_ENTRY_POINT    Handler;
> +  EFI_GUID                      *HandlerType;
> +  EFI_HANDLE                    DispatchHandle;
> +  BOOLEAN                       UnRegister;
> +} MM_CORE_MMI_HANDLERS;
> +
> +//
> +// Structure for recording the state of an MM Driver
> +//
> +#define EFI_MM_DRIVER_ENTRY_SIGNATURE SIGNATURE_32('s', 'd','r','v')
> +
> +typedef struct {
> +  UINTN                           Signature;
> +  LIST_ENTRY                      Link;             // mDriverList
> +
> +  LIST_ENTRY                      ScheduledLink;    // mScheduledQueue
> +
> +  EFI_HANDLE                      FvHandle;
> +  EFI_GUID                        FileName;
> +  VOID                            *Pe32Data;
> +  UINTN                           Pe32DataSize;
> +
> +  VOID                            *Depex;
> +  UINTN                           DepexSize;
> +
> +  BOOLEAN                         Before;
> +  BOOLEAN                         After;
> +  EFI_GUID                        BeforeAfterGuid;
> +
> +  BOOLEAN                         Dependent;
> +  BOOLEAN                         Scheduled;
> +  BOOLEAN                         Initialized;
> +  BOOLEAN                         DepexProtocolError;
> +
> +  EFI_HANDLE                      ImageHandle;
> +  EFI_LOADED_IMAGE_PROTOCOL       *LoadedImage;
> +  //
> +  // Image EntryPoint in MMRAM
> +  //
> +  PHYSICAL_ADDRESS                ImageEntryPoint;
> +  //
> +  // Image Buffer in MMRAM
> +  //
> +  PHYSICAL_ADDRESS                ImageBuffer;
> +  //
> +  // Image Page Number
> +  //
> +  UINTN                           NumberOfPage;
> +} EFI_MM_DRIVER_ENTRY;
> +
> +#define EFI_HANDLE_SIGNATURE            SIGNATURE_32('h','n','d','l')
> +
> +///
> +/// IHANDLE - contains a list of protocol handles
> +///
> +typedef struct {
> +  UINTN               Signature;
> +  /// All handles list of IHANDLE
> +  LIST_ENTRY          AllHandles;
> +  /// List of PROTOCOL_INTERFACE's for this handle
> +  LIST_ENTRY          Protocols;
> +  UINTN               LocateRequest;
> +} IHANDLE;
> +
> +#define ASSERT_IS_HANDLE(a)  ASSERT((a)->Signature == EFI_HANDLE_SIGNATURE)
> +
> +#define PROTOCOL_ENTRY_SIGNATURE        SIGNATURE_32('p','r','t','e')
> +
> +///
> +/// PROTOCOL_ENTRY - each different protocol has 1 entry in the protocol
> +/// database.  Each handler that supports this protocol is listed, along
> +/// with a list of registered notifies.
> +///
> +typedef struct {
> +  UINTN               Signature;
> +  /// Link Entry inserted to mProtocolDatabase
> +  LIST_ENTRY          AllEntries;
> +  /// ID of the protocol
> +  EFI_GUID            ProtocolID;
> +  /// All protocol interfaces
> +  LIST_ENTRY          Protocols;
> +  /// Registerd notification handlers
> +  LIST_ENTRY          Notify;
> +} PROTOCOL_ENTRY;
> +
> +#define PROTOCOL_INTERFACE_SIGNATURE  SIGNATURE_32('p','i','f','c')
> +
> +///
> +/// PROTOCOL_INTERFACE - each protocol installed on a handle is tracked
> +/// with a protocol interface structure
> +///
> +typedef struct {
> +  UINTN                       Signature;
> +  /// Link on IHANDLE.Protocols
> +  LIST_ENTRY                  Link;
> +  /// Back pointer
> +  IHANDLE                     *Handle;
> +  /// Link on PROTOCOL_ENTRY.Protocols
> +  LIST_ENTRY                  ByProtocol;
> +  /// The protocol ID
> +  PROTOCOL_ENTRY              *Protocol;
> +  /// The interface value
> +  VOID                        *Interface;
> +} PROTOCOL_INTERFACE;
> +
> +#define PROTOCOL_NOTIFY_SIGNATURE       SIGNATURE_32('p','r','t','n')
> +
> +///
> +/// PROTOCOL_NOTIFY - used for each register notification for a protocol
> +///
> +typedef struct {
> +  UINTN               Signature;
> +  PROTOCOL_ENTRY      *Protocol;
> +  /// All notifications for this protocol
> +  LIST_ENTRY          Link;
> +  /// Notification function
> +  EFI_MM_NOTIFY_FN   Function;
> +  /// Last position notified
> +  LIST_ENTRY          *Position;
> +} PROTOCOL_NOTIFY;
> +
> +//
> +// MM Core Global Variables
> +//
> +extern MM_CORE_PRIVATE_DATA  *gMmCorePrivate;
> +extern EFI_MM_SYSTEM_TABLE   gMmCoreMmst;
> +extern LIST_ENTRY            gHandleList;
> +extern EFI_PHYSICAL_ADDRESS  gLoadModuleAtFixAddressMmramBase;
> +
> +/**
> +  Called to initialize the memory service.
> +
> +  @param   MmramRangeCount       Number of MMRAM Regions
> +  @param   MmramRanges           Pointer to MMRAM Descriptors
> +
> +**/
> +VOID
> +MmInitializeMemoryServices (
> +  IN UINTN                 MmramRangeCount,
> +  IN EFI_MMRAM_DESCRIPTOR  *MmramRanges
> +  );
> +
> +/**
> +  The MmInstallConfigurationTable() function is used to maintain the list
> +  of configuration tables that are stored in the System Management System
> +  Table.  The list is stored as an array of (GUID, Pointer) pairs.  The list
> +  must be allocated from pool memory with PoolType set to EfiRuntimeServicesData.
> +
> +  @param  SystemTable      A pointer to the MM System Table (SMST).
> +  @param  Guid             A pointer to the GUID for the entry to add, update, or remove.
> +  @param  Table            A pointer to the buffer of the table to add.
> +  @param  TableSize        The size of the table to install.
> +
> +  @retval EFI_SUCCESS           The (Guid, Table) pair was added, updated, or removed.
> +  @retval EFI_INVALID_PARAMETER Guid is not valid.
> +  @retval EFI_NOT_FOUND         An attempt was made to delete a non-existent entry.
> +  @retval EFI_OUT_OF_RESOURCES  There is not enough memory available to complete the operation.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInstallConfigurationTable (
> +  IN  CONST EFI_MM_SYSTEM_TABLE  *SystemTable,
> +  IN  CONST EFI_GUID              *Guid,
> +  IN  VOID                        *Table,
> +  IN  UINTN                       TableSize
> +  );
> +
> +/**
> +  Wrapper function to MmInstallProtocolInterfaceNotify.  This is the public API which
> +  Calls the private one which contains a BOOLEAN parameter for notifications
> +
> +  @param  UserHandle             The handle to install the protocol handler on,
> +                                 or NULL if a new handle is to be allocated
> +  @param  Protocol               The protocol to add to the handle
> +  @param  InterfaceType          Indicates whether Interface is supplied in
> +                                 native form.
> +  @param  Interface              The interface for the protocol being added
> +
> +  @return Status code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInstallProtocolInterface (
> +  IN OUT EFI_HANDLE     *UserHandle,
> +  IN EFI_GUID           *Protocol,
> +  IN EFI_INTERFACE_TYPE InterfaceType,
> +  IN VOID               *Interface
> +  );
> +
> +/**
> +  Allocates pages from the memory map.
> +
> +  @param  Type                   The type of allocation to perform
> +  @param  MemoryType             The type of memory to turn the allocated pages
> +                                 into
> +  @param  NumberOfPages          The number of pages to allocate
> +  @param  Memory                 A pointer to receive the base allocated memory
> +                                 address
> +
> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
> +  @retval EFI_SUCCESS            Pages successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmAllocatePages (
> +  IN      EFI_ALLOCATE_TYPE         Type,
> +  IN      EFI_MEMORY_TYPE           MemoryType,
> +  IN      UINTN                     NumberOfPages,
> +  OUT     EFI_PHYSICAL_ADDRESS      *Memory
> +  );
> +
> +/**
> +  Allocates pages from the memory map.
> +
> +  @param  Type                   The type of allocation to perform
> +  @param  MemoryType             The type of memory to turn the allocated pages
> +                                 into
> +  @param  NumberOfPages          The number of pages to allocate
> +  @param  Memory                 A pointer to receive the base allocated memory
> +                                 address
> +
> +  @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in spec.
> +  @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
> +  @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
> +  @retval EFI_SUCCESS            Pages successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalAllocatePages (
> +  IN      EFI_ALLOCATE_TYPE         Type,
> +  IN      EFI_MEMORY_TYPE           MemoryType,
> +  IN      UINTN                     NumberOfPages,
> +  OUT     EFI_PHYSICAL_ADDRESS      *Memory
> +  );
> +
> +/**
> +  Frees previous allocated pages.
> +
> +  @param  Memory                 Base address of memory being freed
> +  @param  NumberOfPages          The number of pages to free
> +
> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range
> +  @retval EFI_INVALID_PARAMETER  Address not aligned, Address is zero or NumberOfPages is zero.
> +  @return EFI_SUCCESS            Pages successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmFreePages (
> +  IN      EFI_PHYSICAL_ADDRESS      Memory,
> +  IN      UINTN                     NumberOfPages
> +  );
> +
> +/**
> +  Frees previous allocated pages.
> +
> +  @param  Memory                 Base address of memory being freed
> +  @param  NumberOfPages          The number of pages to free
> +
> +  @retval EFI_NOT_FOUND          Could not find the entry that covers the range
> +  @retval EFI_INVALID_PARAMETER  Address not aligned, Address is zero or NumberOfPages is zero.
> +  @return EFI_SUCCESS            Pages successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalFreePages (
> +  IN      EFI_PHYSICAL_ADDRESS      Memory,
> +  IN      UINTN                     NumberOfPages
> +  );
> +
> +/**
> +  Allocate pool of a particular type.
> +
> +  @param  PoolType               Type of pool to allocate
> +  @param  Size                   The amount of pool to allocate
> +  @param  Buffer                 The address to return a pointer to the allocated
> +                                 pool
> +
> +  @retval EFI_INVALID_PARAMETER  PoolType not valid
> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
> +  @retval EFI_SUCCESS            Pool successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmAllocatePool (
> +  IN      EFI_MEMORY_TYPE           PoolType,
> +  IN      UINTN                     Size,
> +  OUT     VOID                      **Buffer
> +  );
> +
> +/**
> +  Allocate pool of a particular type.
> +
> +  @param  PoolType               Type of pool to allocate
> +  @param  Size                   The amount of pool to allocate
> +  @param  Buffer                 The address to return a pointer to the allocated
> +                                 pool
> +
> +  @retval EFI_INVALID_PARAMETER  PoolType not valid
> +  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.
> +  @retval EFI_SUCCESS            Pool successfully allocated.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalAllocatePool (
> +  IN      EFI_MEMORY_TYPE           PoolType,
> +  IN      UINTN                     Size,
> +  OUT     VOID                      **Buffer
> +  );
> +
> +/**
> +  Frees pool.
> +
> +  @param  Buffer                 The allocated pool entry to free
> +
> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
> +  @retval EFI_SUCCESS            Pool successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmFreePool (
> +  IN      VOID                      *Buffer
> +  );
> +
> +/**
> +  Frees pool.
> +
> +  @param  Buffer                 The allocated pool entry to free
> +
> +  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.
> +  @retval EFI_SUCCESS            Pool successfully freed.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmInternalFreePool (
> +  IN      VOID                      *Buffer
> +  );
> +
> +/**
> +  Installs a protocol interface into the boot services environment.
> +
> +  @param  UserHandle             The handle to install the protocol handler on,
> +                                 or NULL if a new handle is to be allocated
> +  @param  Protocol               The protocol to add to the handle
> +  @param  InterfaceType          Indicates whether Interface is supplied in
> +                                 native form.
> +  @param  Interface              The interface for the protocol being added
> +  @param  Notify                 indicates whether notify the notification list
> +                                 for this protocol
> +
> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> +  @retval EFI_OUT_OF_RESOURCES   No enough buffer to allocate
> +  @retval EFI_SUCCESS            Protocol interface successfully installed
> +
> +**/
> +EFI_STATUS
> +MmInstallProtocolInterfaceNotify (
> +  IN OUT EFI_HANDLE     *UserHandle,
> +  IN EFI_GUID           *Protocol,
> +  IN EFI_INTERFACE_TYPE InterfaceType,
> +  IN VOID               *Interface,
> +  IN BOOLEAN            Notify
> +  );
> +
> +/**
> +  Uninstalls all instances of a protocol:interfacer from a handle.
> +  If the last protocol interface is remove from the handle, the
> +  handle is freed.
> +
> +  @param  UserHandle             The handle to remove the protocol handler from
> +  @param  Protocol               The protocol, of protocol:interface, to remove
> +  @param  Interface              The interface, of protocol:interface, to remove
> +
> +  @retval EFI_INVALID_PARAMETER  Protocol is NULL.
> +  @retval EFI_SUCCESS            Protocol interface successfully uninstalled.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmUninstallProtocolInterface (
> +  IN EFI_HANDLE       UserHandle,
> +  IN EFI_GUID         *Protocol,
> +  IN VOID             *Interface
> +  );
> +
> +/**
> +  Queries a handle to determine if it supports a specified protocol.
> +
> +  @param  UserHandle             The handle being queried.
> +  @param  Protocol               The published unique identifier of the protocol.
> +  @param  Interface              Supplies the address where a pointer to the
> +                                 corresponding Protocol Interface is returned.
> +
> +  @return The requested protocol interface for the handle
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmHandleProtocol (
> +  IN EFI_HANDLE       UserHandle,
> +  IN EFI_GUID         *Protocol,
> +  OUT VOID            **Interface
> +  );
> +
> +/**
> +  Add a new protocol notification record for the request protocol.
> +
> +  @param  Protocol               The requested protocol to add the notify
> +                                 registration
> +  @param  Function               Points to the notification function
> +  @param  Registration           Returns the registration record
> +
> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> +  @retval EFI_SUCCESS            Successfully returned the registration record
> +                                 that has been added
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmRegisterProtocolNotify (
> +  IN  CONST EFI_GUID              *Protocol,
> +  IN  EFI_MM_NOTIFY_FN           Function,
> +  OUT VOID                        **Registration
> +  );
> +
> +/**
> +  Locates the requested handle(s) and returns them in Buffer.
> +
> +  @param  SearchType             The type of search to perform to locate the
> +                                 handles
> +  @param  Protocol               The protocol to search for
> +  @param  SearchKey              Dependant on SearchType
> +  @param  BufferSize             On input the size of Buffer.  On output the
> +                                 size of data returned.
> +  @param  Buffer                 The buffer to return the results in
> +
> +  @retval EFI_BUFFER_TOO_SMALL   Buffer too small, required buffer size is
> +                                 returned in BufferSize.
> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> +  @retval EFI_SUCCESS            Successfully found the requested handle(s) and
> +                                 returns them in Buffer.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLocateHandle (
> +  IN EFI_LOCATE_SEARCH_TYPE   SearchType,
> +  IN EFI_GUID                 *Protocol   OPTIONAL,
> +  IN VOID                     *SearchKey  OPTIONAL,
> +  IN OUT UINTN                *BufferSize,
> +  OUT EFI_HANDLE              *Buffer
> +  );
> +
> +/**
> +  Return the first Protocol Interface that matches the Protocol GUID. If
> +  Registration is pasased in return a Protocol Instance that was just add
> +  to the system. If Retistration is NULL return the first Protocol Interface
> +  you find.
> +
> +  @param  Protocol               The protocol to search for
> +  @param  Registration           Optional Registration Key returned from
> +                                 RegisterProtocolNotify()
> +  @param  Interface              Return the Protocol interface (instance).
> +
> +  @retval EFI_SUCCESS            If a valid Interface is returned
> +  @retval EFI_INVALID_PARAMETER  Invalid parameter
> +  @retval EFI_NOT_FOUND          Protocol interface not found
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLocateProtocol (
> +  IN  EFI_GUID  *Protocol,
> +  IN  VOID      *Registration OPTIONAL,
> +  OUT VOID      **Interface
> +  );
> +
> +/**
> +  Manage MMI of a particular type.
> +
> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
> +  @param  Context        Points to an optional context buffer.
> +  @param  CommBuffer     Points to the optional communication buffer.
> +  @param  CommBufferSize Points to the size of the optional communication buffer.
> +
> +  @retval EFI_SUCCESS                        Interrupt source was processed successfully but not quiesced.
> +  @retval EFI_INTERRUPT_PENDING              One or more MMI sources could not be quiesced.
> +  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING  Interrupt source was not handled or quiesced.
> +  @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED Interrupt source was handled and quiesced.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmiManage (
> +  IN     CONST EFI_GUID           *HandlerType,
> +  IN     CONST VOID               *Context         OPTIONAL,
> +  IN OUT VOID                     *CommBuffer      OPTIONAL,
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  Registers a handler to execute within MM.
> +
> +  @param  Handler        Handler service funtion pointer.
> +  @param  HandlerType    Points to the handler type or NULL for root MMI handlers.
> +  @param  DispatchHandle On return, contains a unique handle which can be used to later unregister the handler function.
> +
> +  @retval EFI_SUCCESS           Handler register success.
> +  @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmiHandlerRegister (
> +  IN   EFI_MM_HANDLER_ENTRY_POINT     Handler,
> +  IN   CONST EFI_GUID                 *HandlerType  OPTIONAL,
> +  OUT  EFI_HANDLE                     *DispatchHandle
> +  );
> +
> +/**
> +  Unregister a handler in MM.
> +
> +  @param  DispatchHandle  The handle that was specified when the handler was registered.
> +
> +  @retval EFI_SUCCESS           Handler function was successfully unregistered.
> +  @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid handle.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmiHandlerUnRegister (
> +  IN  EFI_HANDLE                      DispatchHandle
> +  );
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmDriverDispatchHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmFvDispatchHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmLegacyBootHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmExitBootServiceHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmReadyToBootHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmReadyToLockHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  This function is the main entry point for an MM handler dispatch
> +  or communicate-based callback.
> +
> +  @param  DispatchHandle  The unique handle assigned to this handler by MmiHandlerRegister().
> +  @param  Context         Points to an optional handler context which was specified when the handler was registered.
> +  @param  CommBuffer      A pointer to a collection of data in memory that will
> +                          be conveyed from a non-MM environment into an MM environment.
> +  @param  CommBufferSize  The size of the CommBuffer.
> +
> +  @return Status Code
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmEndOfDxeHandler (
> +  IN     EFI_HANDLE               DispatchHandle,
> +  IN     CONST VOID               *Context,        OPTIONAL
> +  IN OUT VOID                     *CommBuffer,     OPTIONAL
> +  IN OUT UINTN                    *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  Place holder function until all the MM System Table Service are available.
> +
> +  @param  Arg1                   Undefined
> +  @param  Arg2                   Undefined
> +  @param  Arg3                   Undefined
> +  @param  Arg4                   Undefined
> +  @param  Arg5                   Undefined
> +
> +  @return EFI_NOT_AVAILABLE_YET
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmEfiNotAvailableYetArg5 (
> +  UINTN Arg1,
> +  UINTN Arg2,
> +  UINTN Arg3,
> +  UINTN Arg4,
> +  UINTN Arg5
> +  );
> +
> +//
> +//Functions used during debug buils
> +//
> +
> +/**
> +  Traverse the discovered list for any drivers that were discovered but not loaded
> +  because the dependency expressions evaluated to false.
> +
> +**/
> +VOID
> +MmDisplayDiscoveredNotDispatched (
> +  VOID
> +  );
> +
> +/**
> +  Add free MMRAM region for use by memory service.
> +
> +  @param  MemBase                Base address of memory region.
> +  @param  MemLength              Length of the memory region.
> +  @param  Type                   Memory type.
> +  @param  Attributes             Memory region state.
> +
> +**/
> +VOID
> +MmAddMemoryRegion (
> +  IN      EFI_PHYSICAL_ADDRESS      MemBase,
> +  IN      UINT64                    MemLength,
> +  IN      EFI_MEMORY_TYPE           Type,
> +  IN      UINT64                    Attributes
> +  );
> +
> +/**
> +  Finds the protocol entry for the requested protocol.
> +
> +  @param  Protocol               The ID of the protocol
> +  @param  Create                 Create a new entry if not found
> +
> +  @return Protocol entry
> +
> +**/
> +PROTOCOL_ENTRY  *
> +MmFindProtocolEntry (
> +  IN EFI_GUID   *Protocol,
> +  IN BOOLEAN    Create
> +  );
> +
> +/**
> +  Signal event for every protocol in protocol entry.
> +
> +  @param  Prot                   Protocol interface
> +
> +**/
> +VOID
> +MmNotifyProtocol (
> +  IN PROTOCOL_INTERFACE   *Prot
> +  );
> +
> +/**
> +  Finds the protocol instance for the requested handle and protocol.
> +  Note: This function doesn't do parameters checking, it's caller's responsibility
> +  to pass in valid parameters.
> +
> +  @param  Handle                 The handle to search the protocol on
> +  @param  Protocol               GUID of the protocol
> +  @param  Interface              The interface for the protocol being searched
> +
> +  @return Protocol instance (NULL: Not found)
> +
> +**/
> +PROTOCOL_INTERFACE *
> +MmFindProtocolInterface (
> +  IN IHANDLE        *Handle,
> +  IN EFI_GUID       *Protocol,
> +  IN VOID           *Interface
> +  );
> +
> +/**
> +  Removes Protocol from the protocol list (but not the handle list).
> +
> +  @param  Handle                 The handle to remove protocol on.
> +  @param  Protocol               GUID of the protocol to be moved
> +  @param  Interface              The interface of the protocol
> +
> +  @return Protocol Entry
> +
> +**/
> +PROTOCOL_INTERFACE *
> +MmRemoveInterfaceFromProtocol (
> +  IN IHANDLE        *Handle,
> +  IN EFI_GUID       *Protocol,
> +  IN VOID           *Interface
> +  );
> +
> +/**
> +  This is the POSTFIX version of the dependency evaluator.  This code does
> +  not need to handle Before or After, as it is not valid to call this
> +  routine in this case. POSTFIX means all the math is done on top of the stack.
> +
> +  @param  DriverEntry           DriverEntry element to update.
> +
> +  @retval TRUE                  If driver is ready to run.
> +  @retval FALSE                 If driver is not ready to run or some fatal error
> +                                was found.
> +
> +**/
> +BOOLEAN
> +MmIsSchedulable (
> +  IN  EFI_MM_DRIVER_ENTRY   *DriverEntry
> +  );
> +
> +/**
> +  Dump MMRAM information.
> +
> +**/
> +VOID
> +DumpMmramInfo (
> +  VOID
> +  );
> +
> +extern UINTN                    mMmramRangeCount;
> +extern EFI_MMRAM_DESCRIPTOR     *mMmramRanges;
> +extern EFI_SYSTEM_TABLE         *mEfiSystemTable;
> +
> +#endif
> diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.inf b/StandaloneMmPkg/Core/StandaloneMmCore.inf
> new file mode 100644
> index 0000000000..c5eaa14ba3
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/StandaloneMmCore.inf
> @@ -0,0 +1,80 @@
> +## @file
> +# This module provide an SMM CIS compliant implementation of SMM Core.
> +#
> +# Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
> +# Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +#
> +# 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
> +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = StandaloneMmCore
> +  FILE_GUID                      = 6E14B6FD-3600-4DD6-A17A-206B3B6DCE16
> +  MODULE_TYPE                    = MM_CORE_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  ENTRY_POINT                    = StandaloneMmMain
> +
> +#  VALID_ARCHITECTURES           = IA32 X64 AARCH64
> +
> +[Sources]
> +  StandaloneMmCore.c
> +  StandaloneMmCore.h
> +  StandaloneMmCorePrivateData.h
> +  Page.c
> +  Pool.c
> +  Handle.c
> +  Locate.c
> +  Notify.c
> +  Dependency.c
> +  Dispatcher.c
> +  Mmi.c
> +  InstallConfigurationTable.c
> +  FwVol.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +  MdeModulePkg/MdeModulePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[LibraryClasses]
> +  BaseLib
> +  BaseMemoryLib
> +  CacheMaintenanceLib
> +  DebugLib
> +  FvLib
> +  HobLib
> +  MemoryAllocationLib
> +  MemLib
> +  PeCoffLib
> +  ReportStatusCodeLib
> +  StandaloneMmCoreEntryPoint
> +
> +[Protocols]
> +  gEfiDxeMmReadyToLockProtocolGuid             ## UNDEFINED # SmiHandlerRegister
> +  gEfiMmReadyToLockProtocolGuid                ## PRODUCES
> +  gEfiMmEndOfDxeProtocolGuid                   ## PRODUCES
> +  gEfiLoadedImageProtocolGuid                   ## PRODUCES
> +  gEfiMmConfigurationProtocolGuid               ## CONSUMES
> +
> +[Guids]
> +  gAprioriGuid                                  ## SOMETIMES_CONSUMES   ## File
> +  gEfiEventDxeDispatchGuid                      ## PRODUCES             ## GUID # SmiHandlerRegister
> +  gEfiEndOfDxeEventGroupGuid                    ## PRODUCES             ## GUID # SmiHandlerRegister
> +  ## SOMETIMES_CONSUMES   ## GUID # Locate protocol
> +  ## SOMETIMES_PRODUCES   ## GUID # SmiHandlerRegister
> +  gEdkiiMemoryProfileGuid
> +  gZeroGuid                                     ## SOMETIMES_CONSUMES   ## GUID
> +  gEfiHobListGuid
> +  gMmCoreDataHobGuid
> +  gMmFvDispatchGuid
> +  gEfiEventLegacyBootGuid
> +  gEfiEventExitBootServicesGuid
> +  gEfiEventReadyToBootGuid
> diff --git a/StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h b/StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
> new file mode 100644
> index 0000000000..faedf3ff2d
> --- /dev/null
> +++ b/StandaloneMmPkg/Core/StandaloneMmCorePrivateData.h
> @@ -0,0 +1,66 @@
> +/** @file
> +  The internal header file that declared a data structure that is shared
> +  between the MM IPL and the MM Core.
> +
> +  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _STANDALONE_MM_CORE_PRIVATE_DATA_H_
> +#define _STANDALONE_MM_CORE_PRIVATE_DATA_H_
> +
> +#include <Guid/MmCoreData.h>
> +
> +//
> +// Page management
> +//
> +
> +typedef struct {
> +  LIST_ENTRY  Link;
> +  UINTN       NumberOfPages;
> +} FREE_PAGE_LIST;
> +
> +extern LIST_ENTRY  mMmMemoryMap;
> +
> +//
> +// Pool management
> +//
> +
> +//
> +// MIN_POOL_SHIFT must not be less than 5
> +//
> +#define MIN_POOL_SHIFT  6
> +#define MIN_POOL_SIZE   (1 << MIN_POOL_SHIFT)
> +
> +//
> +// MAX_POOL_SHIFT must not be less than EFI_PAGE_SHIFT - 1
> +//
> +#define MAX_POOL_SHIFT  (EFI_PAGE_SHIFT - 1)
> +#define MAX_POOL_SIZE   (1 << MAX_POOL_SHIFT)
> +
> +//
> +// MAX_POOL_INDEX are calculated by maximum and minimum pool sizes
> +//
> +#define MAX_POOL_INDEX  (MAX_POOL_SHIFT - MIN_POOL_SHIFT + 1)
> +
> +typedef struct {
> +  UINTN        Size;
> +  BOOLEAN      Available;
> +} POOL_HEADER;
> +
> +typedef struct {
> +  POOL_HEADER  Header;
> +  LIST_ENTRY   Link;
> +} FREE_POOL_HEADER;
> +
> +extern LIST_ENTRY  mMmPoolLists[MAX_POOL_INDEX];
> +
> +#endif
> diff --git a/StandaloneMmPkg/Include/Guid/MmFvDispatch.h b/StandaloneMmPkg/Include/Guid/MmFvDispatch.h
> new file mode 100644
> index 0000000000..fb194d3474
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/Guid/MmFvDispatch.h
> @@ -0,0 +1,38 @@
> +/** @file
> +  GUIDs for MM Event.
> +
> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +This program and the accompanying materials are licensed and made available under
> +the terms and conditions of the BSD License that accompanies this distribution.
> +The full text of the license may be found at
> +http://opensource.org/licenses/bsd-license.php.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef __MM_FV_DISPATCH_H__
> +#define __MM_FV_DISPATCH_H__
> +
> +#define MM_FV_DISPATCH_GUID \
> +  { 0xb65694cc, 0x9e3, 0x4c3b, { 0xb5, 0xcd, 0x5, 0xf4, 0x4d, 0x3c, 0xdb, 0xff }}
> +
> +extern EFI_GUID gMmFvDispatchGuid;
> +
> +#pragma pack(1)
> +typedef struct {
> +  EFI_PHYSICAL_ADDRESS  Address;
> +  UINT64                Size;
> +} EFI_MM_COMMUNICATE_FV_DISPATCH_DATA;
> +
> +typedef struct {
> +  EFI_GUID                              HeaderGuid;
> +  UINTN                                 MessageLength;
> +  EFI_MM_COMMUNICATE_FV_DISPATCH_DATA   Data;
> +} EFI_MM_COMMUNICATE_FV_DISPATCH;
> +#pragma pack()
> +
> +#endif
> diff --git a/StandaloneMmPkg/Include/StandaloneMm.h b/StandaloneMmPkg/Include/StandaloneMm.h
> new file mode 100644
> index 0000000000..0e420315bb
> --- /dev/null
> +++ b/StandaloneMmPkg/Include/StandaloneMm.h
> @@ -0,0 +1,36 @@
> +/** @file
> +  Standalone MM.
> +
> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR>
> +
> +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
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef _STANDALONE_MM_H_
> +#define _STANDALONE_MM_H_
> +
> +#include <PiMm.h>
> +
> +typedef
> +EFI_STATUS
> +(EFIAPI *MM_IMAGE_ENTRY_POINT) (
> +  IN EFI_HANDLE            ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
> +  );
> +
> +typedef
> +EFI_STATUS
> +(EFIAPI *STANDALONE_MM_FOUNDATION_ENTRY_POINT) (
> +  IN VOID  *HobStart
> +  );
> +
> +#endif
> --
> 2.16.2
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 14/18] StandaloneMmPkg: Describe the declaration, definition and fdf files.
  2018-04-30 19:32   ` Achin Gupta
@ 2018-05-04 23:28     ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:28 UTC (permalink / raw)
  To: Achin Gupta; +Cc: edk2-devel@lists.01.org

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Monday, April 30, 2018 2:32 PM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 14/18] StandaloneMmPkg: Describe the declaration, definition and fdf files.

Hi Supreeth,

One CIL.

On Fri, Apr 06, 2018 at 03:42:19PM +0100, Supreeth Venkatesh wrote:
> This patch describes the package declarations, definitions and
> firmware device files for creating standalone management mode image
> with core/foundation and drivers.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  StandaloneMmPkg/StandaloneMmPkg.dec |  47 +++++++++
> StandaloneMmPkg/StandaloneMmPkg.dsc | 132 ++++++++++++++++++++++++++
> StandaloneMmPkg/StandaloneMmPkg.fdf | 184
> ++++++++++++++++++++++++++++++++++++
>  3 files changed, 363 insertions(+)
>  create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dec
>  create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dsc
>  create mode 100644 StandaloneMmPkg/StandaloneMmPkg.fdf
>
> diff --git a/StandaloneMmPkg/StandaloneMmPkg.dec
> b/StandaloneMmPkg/StandaloneMmPkg.dec
> new file mode 100644
> index 0000000000..36521bb039
> --- /dev/null
> +++ b/StandaloneMmPkg/StandaloneMmPkg.dec
> @@ -0,0 +1,47 @@
> +## @file
> +# This package is a platform package that provide platform
> +module/library # required by Standalone MM platform.
> +#
> +# Copyright (c) 2016-2017, ARM Ltd. All rights reserved.<BR> # # 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
> +#
> +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#
> +
> +[Defines]
> +  DEC_SPECIFICATION              = 0x0001001A
> +  PACKAGE_NAME                   = StandaloneMmPkg
> +  PACKAGE_GUID                   = 2AE82968-7769-4A85-A5BC-A0954CE54A5C
> +  PACKAGE_VERSION                = 1.0
> +
> +[Includes]
> +  Include
> +
> +[LibraryClasses]
> +
> +[Guids]
> +  gStandaloneMmPkgTokenSpaceGuid           = { 0x18fe7632, 0xf5c8, 0x4e63, { 0x8d, 0xe8, 0x17, 0xa5, 0x5c, 0x59, 0x13, 0xbd }}
> +  gMpInformationHobGuid                    = { 0xba33f15d, 0x4000, 0x45c1, { 0x8e, 0x88, 0xf9, 0x16, 0x92, 0xd4, 0x57, 0xe3 }}
> +  gMmFvDispatchGuid                        = { 0xb65694cc, 0x09e3, 0x4c3b, { 0xb5, 0xcd, 0x05, 0xf4, 0x4d, 0x3c, 0xdb, 0xff }}
> +
> +  ## Include/Guid/MmCoreData.h
> +  gMmCoreDataHobGuid                       = { 0xa160bf99, 0x2aa4, 0x4d7d, { 0x99, 0x93, 0x89, 0x9c, 0xb1, 0x2d, 0xf3, 0x76 }}
> +
> +  ## Include/Guid/MmramMemoryReserve.h
> +  gEfiMmPeiMmramMemoryReserveGuid          = { 0x0703f912, 0xbf8d, 0x4e2a, { 0xbe, 0x07, 0xab, 0x27, 0x25, 0x25, 0xc5, 0x92 }}
> +
> +  gEfiStandaloneMmNonSecureBufferGuid      = { 0xf00497e3, 0xbfa2, 0x41a1, { 0x9d, 0x29, 0x54, 0xc2, 0xe9, 0x37, 0x21, 0xc5 }}
> +  gEfiArmTfCpuDriverEpDescriptorGuid       = { 0x6ecbd5a1, 0xc0f8, 0x4702, { 0x83, 0x01, 0x4f, 0xc2, 0xc5, 0x47, 0x0a, 0x51 }}
> +
> +[PcdsFeatureFlag]
> +
> +gStandaloneMmPkgTokenSpaceGuid.PcdStandaloneMmEnable|FALSE|BOOLEAN|0x
> +00000001
> +
> +[Protocols]
> +  gEfiMmConfigurationProtocolGuid          = { 0xc109319, 0xc149, 0x450e,  { 0xa3, 0xe3, 0xb9, 0xba, 0xdd, 0x9d, 0xc3, 0xa4 }}
> +
> diff --git a/StandaloneMmPkg/StandaloneMmPkg.dsc
> b/StandaloneMmPkg/StandaloneMmPkg.dsc
> new file mode 100644
> index 0000000000..8cc996f6b0
> --- /dev/null
> +++ b/StandaloneMmPkg/StandaloneMmPkg.dsc
> @@ -0,0 +1,132 @@
> +## @file
> +# Standalone MM Platform.
> +#
> +# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR> #
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR> #
> +#    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
> +#
> +#    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +##
> +
> +#####################################################################
> +###########
> +#
> +# Defines Section - statements that will be processed to create a Makefile.
> +#
> +#####################################################################
> +###########
> +[Defines]
> +  PLATFORM_NAME                  = StandaloneMm
> +  PLATFORM_GUID                  = 9A4BBA60-B4F9-47C7-9258-3BD77CAE9322
> +  PLATFORM_VERSION               = 1.0
> +  DSC_SPECIFICATION              = 0x00010011
> +  OUTPUT_DIRECTORY               = Build/StandaloneMmPkg
> +  SUPPORTED_ARCHITECTURES        = IA32|X64|AARCH64
> +  BUILD_TARGETS                  = DEBUG|RELEASE
> +  SKUID_IDENTIFIER               = DEFAULT
> +  FLASH_DEFINITION               = StandaloneMmPkg/StandaloneMmPkg.fdf
> +  DEFINE DEBUG_MESSAGE           = TRUE
> +
> +  # LzmaF86
> +  DEFINE COMPRESSION_TOOL_GUID   = D42AE6BD-1352-4bfb-909A-CA72A6EAE889
> +
> +#####################################################################
> +###########
> +#
> +# Library Class section - list of all Library Classes needed by this Platform.
> +#
> +#####################################################################
> +###########
> +[LibraryClasses]
> +  #
> +  # Basic
> +  #
> +  BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
> +  BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
> +
> +DebugLib|MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort
> +.inf
> +
> +DebugPrintErrorLevelLib|MdePkg/Library/BaseDebugPrintErrorLevelLib/Ba
> +seDebugPrintErrorLevelLib.inf
> +  FvLib|StandaloneMmPkg/Library/FvLib/FvLib.inf
> +  IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
> +
> +MemoryAllocationLib|StandaloneMmPkg/Library/MemoryAllocationLib/Memor
> +yAllocationLib.inf
> +  PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
> +  PeCoffLib|MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf
> +  PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf
> +
> +ReportStatusCodeLib|MdePkg/Library/BaseReportStatusCodeLibNull/BaseRe
> +portStatusCodeLibNull.inf
> +
> +  #
> +  # Entry point
> +  #
> +
> + StandaloneMmDriverEntryPoint|StandaloneMmPkg/Library/StandaloneMmDri
> + verEntryPoint/StandaloneMmDriverEntryPoint.inf
> +
> +[LibraryClasses.AARCH64]
> +  ArmLib|ArmPkg/Library/ArmLib/ArmBaseLib.inf
> +  ArmMmuLib|ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
> +  ArmSvcLib|ArmPkg/Library/ArmSvcLib/ArmSvcLib.inf
> +
> +CacheMaintenanceLib|ArmPkg/Library/ArmCacheMaintenanceLib/ArmCacheMai
> +ntenanceLib.inf
> +  HobLib|StandaloneMmPkg/Library/HobLib/HobLib.inf
> +  MemLib|StandaloneMmPkg/Library/MemLib/MemLib.inf
> +
> +PeCoffExtraActionLib|ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPe
> +CoffExtraActionLib.inf
> +  PL011UartLib|ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.inf
> +  # ARM PL011 UART Driver
> +
> +SerialPortLib|ArmPlatformPkg/Library/PL011SerialPortLib/PL011SerialPo
> +rtLib.inf
> +
> +
> + StandaloneMmCoreEntryPoint|StandaloneMmPkg/Library/StandaloneMmCoreE
> + ntryPoint/StandaloneMmCoreEntryPoint.inf
> +
> +#####################################################################
> +###########
> +#
> +# Pcd Section - list of all EDK II PCD Entries defined by this
> +Platform #
> +#####################################################################
> +###########
> +[PcdsFeatureFlag]
> +  gStandaloneMmPkgTokenSpaceGuid.PcdStandaloneMmEnable|TRUE
> +
> +[PcdsFixedAtBuild]
> +  gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x800000CF
> +  gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0xff
> +  gEfiMdePkgTokenSpaceGuid.PcdReportStatusCodePropertyMask|0x0f
> +
> +[PcdsFixedAtBuild.AARCH64]
> +  ## PL011 - Serial Terminal
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialRegisterBase|0x1c0b0000
> +  gEfiMdePkgTokenSpaceGuid.PcdUartDefaultBaudRate|115200
> +
> +#####################################################################
> +##############################
> +#
> +# Components Section - list of the modules and components that will be processed by compilation
> +#                      tools and the EDK II tools to generate PE32/PE32+/Coff image files.
> +#
> +# Note: The EDK II DSC file is not used to specify how compiled binary images get placed
> +#       into firmware volume images. This section is just a list of modules to compile from
> +#       source into UEFI-compliant binaries.
> +#       It is the FDF file that contains information on combining binary files into firmware
> +#       volume images, whose concept is beyond UEFI and is described in PI specification.
> +#       Binary modules do not need to be listed in this section, as they should be
> +#       specified in the FDF file. For example: Shell binary (Shell_Full.efi), FAT binary (Fat.efi),
> +#       Logo (Logo.bmp), and etc.
> +#       There may also be modules listed in this section that are not required in the FDF file,
> +#       When a module listed here is excluded from FDF file, then UEFI-compliant binary will be
> +#       generated for it, but the binary will not be put into any firmware volume.
> +#
> +#####################################################################
> +##############################
> +[Components.common]
> +  #
> +  # MM Core
> +  #
> +  StandaloneMmPkg/Core/StandaloneMmCore.inf
> +
> +[Components.AARCH64]
> +  StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> +
> +#####################################################################
> +##############################
> +#
> +# BuildOptions Section - Define the module specific tool chain flags that should be used as
> +#                        the default flags for a module. These flags are appended to any
> +#                        standard flags that are defined by the build process. They can be
> +#                        applied for any modules or only those modules with the specific
> +#                        module style (EDK or EDKII) specified in [Components] section.
> +#
> +#####################################################################
> +##############################
> +[BuildOptions.Common]
> +GCC:*_*_*_DLINK_FLAGS = -z common-page-size=0x1000

I think these must be AArch64 specific build options. Also as discussed, use of FP/SIMD must be disabled here.
[Supreeth] Ok.

cheers,
Achin

> diff --git a/StandaloneMmPkg/StandaloneMmPkg.fdf
> b/StandaloneMmPkg/StandaloneMmPkg.fdf
> new file mode 100644
> index 0000000000..7a22a51b4c
> --- /dev/null
> +++ b/StandaloneMmPkg/StandaloneMmPkg.fdf
> @@ -0,0 +1,184 @@
> +#
> +#  Copyright (c) 2011 - 2017, ARM Limited. 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
> +#
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, #  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +
> +#####################################################################
> +###########
> +#
> +# FD Section
> +# The [FD] Section is made up of the definition statements and a #
> +description of what goes into  the Flash Device Image.  Each FD
> +section # defines one flash "device" image.  A flash device image may
> +be one of # the following: Removable media bootable image (like a
> +boot floppy # image,) an Option ROM image (that would be "flashed"
> +into an add-in # card,) a System "Flash"  image (that would be burned
> +into a system's # flash) or an Update ("Capsule") image that will be
> +used to update and # existing system flash.
> +#
> +#####################################################################
> +###########
> +
> +[FD.]
> +!ifdef ARM_FVP_RUN_NORFLASH
> +BaseAddress   = 0x08000000|gArmTokenSpaceGuid.PcdFdBaseAddress  # The base address of the Firmware in Flash0.
> +!else
> +BaseAddress   = 0xff200000|gArmTokenSpaceGuid.PcdFdBaseAddress  # UEFI in DRAM + 128MB.
> +!endif
> +Size          = 0x00e00000|gArmTokenSpaceGuid.PcdFdSize         # The size in bytes of the device (64MiB).
> +ErasePolarity = 1
> +
> +# This one is tricky, it must be: BlockSize * NumBlocks = Size
> +BlockSize     = 0x00001000
> +NumBlocks     = 0x0e00
> +
> +0x00000000|0x00280000
> +gArmTokenSpaceGuid.PcdFvBaseAddress|gArmTokenSpaceGuid.PcdFvSize
> +FV = FVMAIN_COMPACT
> +
> +[FV.FVMAIN_COMPACT]
> +FvAlignment        = 16
> +ERASE_POLARITY     = 1
> +MEMORY_MAPPED      = TRUE
> +STICKY_WRITE       = TRUE
> +LOCK_CAP           = TRUE
> +LOCK_STATUS        = TRUE
> +WRITE_DISABLED_CAP = TRUE
> +WRITE_ENABLED_CAP  = TRUE
> +WRITE_STATUS       = TRUE
> +WRITE_LOCK_CAP     = TRUE
> +WRITE_LOCK_STATUS  = TRUE
> +READ_DISABLED_CAP  = TRUE
> +READ_ENABLED_CAP   = TRUE
> +READ_STATUS        = TRUE
> +READ_LOCK_CAP      = TRUE
> +READ_LOCK_STATUS   = TRUE
> +
> +INF StandaloneMmPkg/Core/StandaloneMmCore.inf
> +INF StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> +
> +#####################################################################
> +###########
> +#
> +# Rules are use with the [FV] section's module INF type to define #
> +how an FFS file is created for a given INF file. The following Rule
> +are the default # rules for the different module type. User can add
> +the customized rules to define the # content of the FFS file.
> +#
> +#####################################################################
> +###########
> +
> +
> +############################################################################
> +# Example of a DXE_DRIVER FFS file with a Checksum encapsulation section   #
> +#####################################################################
> +#######
> +#
> +#[Rule.Common.DXE_DRIVER]
> +#  FILE DRIVER = $(NAMED_GUID) {
> +#    DXE_DEPEX    DXE_DEPEX               Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +#    COMPRESS PI_STD {
> +#      GUIDED {
> +#        PE32     PE32                    $(INF_OUTPUT)/$(MODULE_NAME).efi
> +#        UI       STRING="$(MODULE_NAME)" Optional
> +#        VERSION  STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +#      }
> +#    }
> +#  }
> +#
> +#####################################################################
> +#######
> +
> +[Rule.Common.SEC]
> +  FILE SEC = $(NAMED_GUID) RELOCS_STRIPPED FIXED {
> +    TE  TE Align = Auto                 $(INF_OUTPUT)/$(MODULE_NAME).efi
> +  }
> +
> +[Rule.Common.MM_CORE_STANDALONE]
> +  FILE SEC = $(NAMED_GUID) RELOCS_STRIPPED FIXED {
> +    PE32  PE32 Align = Auto             $(INF_OUTPUT)/$(MODULE_NAME).efi
> +  }
> +
> +[Rule.Common.MM_STANDALONE]
> +  FILE MM_STANDALONE = $(NAMED_GUID) {
> +    SMM_DEPEX SMM_DEPEX Optional       $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32      PE32                     $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI        STRING="$(MODULE_NAME)" Optional
> +    VERSION   STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +  }
> +
> +[Rule.Common.PEI_CORE]
> +  FILE PEI_CORE = $(NAMED_GUID) FIXED {
> +    TE     TE Align = Auto              $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI     STRING ="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.PEIM]
> +  FILE PEIM = $(NAMED_GUID) FIXED {
> +     PEI_DEPEX PEI_DEPEX Optional       $(INF_OUTPUT)/$(MODULE_NAME).depex
> +     TE       TE Align = Auto           $(INF_OUTPUT)/$(MODULE_NAME).efi
> +     UI       STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.PEIM.TIANOCOMPRESSED]
> +  FILE PEIM = $(NAMED_GUID) DEBUG_MYTOOLS_IA32 {
> +    PEI_DEPEX PEI_DEPEX Optional        $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    GUIDED A31280AD-481E-41B6-95E8-127F4C984779 PROCESSING_REQUIRED = TRUE {
> +      PE32      PE32                    $(INF_OUTPUT)/$(MODULE_NAME).efi
> +      UI        STRING="$(MODULE_NAME)" Optional
> +    }
> +  }
> +
> +[Rule.Common.DXE_CORE]
> +  FILE DXE_CORE = $(NAMED_GUID) {
> +    PE32     PE32                       $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI       STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.UEFI_DRIVER]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32         PE32                   $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI           STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.DXE_DRIVER]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32         PE32                   $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI           STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.DXE_RUNTIME_DRIVER]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32         PE32                   $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI           STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.UEFI_APPLICATION]
> +  FILE APPLICATION = $(NAMED_GUID) {
> +    UI     STRING ="$(MODULE_NAME)"     Optional
> +    PE32   PE32                         $(INF_OUTPUT)/$(MODULE_NAME).efi
> +  }
> +
> +[Rule.Common.UEFI_DRIVER.BINARY]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX DXE_DEPEX Optional      |.depex
> +    PE32      PE32                    |.efi
> +    UI        STRING="$(MODULE_NAME)" Optional
> +    VERSION   STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +  }
> +
> +[Rule.Common.UEFI_APPLICATION.BINARY]
> +  FILE APPLICATION = $(NAMED_GUID) {
> +    PE32      PE32                    |.efi
> +    UI        STRING="$(MODULE_NAME)" Optional
> +    VERSION   STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +  }
> +
> +[Rule.Common.USER_DEFINED.ACPITABLE]
> +  FILE FREEFORM = $(NAMED_GUID) {
> +    RAW ASL                |.aml
> +  }
> --
> 2.16.2
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 14/18] StandaloneMmPkg: Describe the declaration, definition and fdf files.
  2018-04-18 19:50   ` Daniil Egranov
@ 2018-05-04 23:29     ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:29 UTC (permalink / raw)
  To: Daniil Egranov, edk2-devel@lists.01.org

My response inline.

-----Original Message-----
From: Daniil Egranov <daniil.egranov@arm.com>
Sent: Wednesday, April 18, 2018 2:51 PM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>; edk2-devel@lists.01.org
Cc: ard.biesheuvel@linaro.org; leif.lindholm@linaro.org; jiewen.yao@intel.com; liming.gao@intel.com; michael.d.kinney@intel.com
Subject: Re: [edk2] [PATCH v1 14/18] StandaloneMmPkg: Describe the declaration, definition and fdf files.

Hi Supreeth,

Having .inc with the common configuration and data can be useful for including it to platform specific configurations. It will help to avoid patching platform specific builds in case of any changes in StandaloneMM structure or configuration.
[Supreeth]  I have moved/added dsc and fdf files as a reference for AArch64 FVP in edk2-platforms where it belongs rightfully. Please see version 2.
However, if you think it can be refactored in edk2-platforms. Please contribute.

Thanks,
Daniil

On 04/06/2018 09:42 AM, Supreeth Venkatesh wrote:
> This patch describes the package declarations, definitions and
> firmware device files for creating standalone management mode image
> with core/foundation and drivers.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>   StandaloneMmPkg/StandaloneMmPkg.dec |  47 +++++++++
>   StandaloneMmPkg/StandaloneMmPkg.dsc | 132 ++++++++++++++++++++++++++
>   StandaloneMmPkg/StandaloneMmPkg.fdf | 184 ++++++++++++++++++++++++++++++++++++
>   3 files changed, 363 insertions(+)
>   create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dec
>   create mode 100644 StandaloneMmPkg/StandaloneMmPkg.dsc
>   create mode 100644 StandaloneMmPkg/StandaloneMmPkg.fdf
>
> diff --git a/StandaloneMmPkg/StandaloneMmPkg.dec
> b/StandaloneMmPkg/StandaloneMmPkg.dec
> new file mode 100644
> index 0000000000..36521bb039
> --- /dev/null
> +++ b/StandaloneMmPkg/StandaloneMmPkg.dec
> @@ -0,0 +1,47 @@
> +## @file
> +# This package is a platform package that provide platform
> +module/library # required by Standalone MM platform.
> +#
> +# Copyright (c) 2016-2017, ARM Ltd. All rights reserved.<BR> # # 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
> +#
> +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#
> +
> +[Defines]
> +  DEC_SPECIFICATION              = 0x0001001A
> +  PACKAGE_NAME                   = StandaloneMmPkg
> +  PACKAGE_GUID                   = 2AE82968-7769-4A85-A5BC-A0954CE54A5C
> +  PACKAGE_VERSION                = 1.0
> +
> +[Includes]
> +  Include
> +
> +[LibraryClasses]
> +
> +[Guids]
> +  gStandaloneMmPkgTokenSpaceGuid           = { 0x18fe7632, 0xf5c8, 0x4e63, { 0x8d, 0xe8, 0x17, 0xa5, 0x5c, 0x59, 0x13, 0xbd }}
> +  gMpInformationHobGuid                    = { 0xba33f15d, 0x4000, 0x45c1, { 0x8e, 0x88, 0xf9, 0x16, 0x92, 0xd4, 0x57, 0xe3 }}
> +  gMmFvDispatchGuid                        = { 0xb65694cc, 0x09e3, 0x4c3b, { 0xb5, 0xcd, 0x05, 0xf4, 0x4d, 0x3c, 0xdb, 0xff }}
> +
> +  ## Include/Guid/MmCoreData.h
> +  gMmCoreDataHobGuid                       = { 0xa160bf99, 0x2aa4, 0x4d7d, { 0x99, 0x93, 0x89, 0x9c, 0xb1, 0x2d, 0xf3, 0x76 }}
> +
> +  ## Include/Guid/MmramMemoryReserve.h
> +  gEfiMmPeiMmramMemoryReserveGuid          = { 0x0703f912, 0xbf8d, 0x4e2a, { 0xbe, 0x07, 0xab, 0x27, 0x25, 0x25, 0xc5, 0x92 }}
> +
> +  gEfiStandaloneMmNonSecureBufferGuid      = { 0xf00497e3, 0xbfa2, 0x41a1, { 0x9d, 0x29, 0x54, 0xc2, 0xe9, 0x37, 0x21, 0xc5 }}
> +  gEfiArmTfCpuDriverEpDescriptorGuid       = { 0x6ecbd5a1, 0xc0f8, 0x4702, { 0x83, 0x01, 0x4f, 0xc2, 0xc5, 0x47, 0x0a, 0x51 }}
> +
> +[PcdsFeatureFlag]
> +
> +gStandaloneMmPkgTokenSpaceGuid.PcdStandaloneMmEnable|FALSE|BOOLEAN|0x
> +00000001
> +
> +[Protocols]
> +  gEfiMmConfigurationProtocolGuid          = { 0xc109319, 0xc149, 0x450e,  { 0xa3, 0xe3, 0xb9, 0xba, 0xdd, 0x9d, 0xc3, 0xa4 }}
> +
> diff --git a/StandaloneMmPkg/StandaloneMmPkg.dsc
> b/StandaloneMmPkg/StandaloneMmPkg.dsc
> new file mode 100644
> index 0000000000..8cc996f6b0
> --- /dev/null
> +++ b/StandaloneMmPkg/StandaloneMmPkg.dsc
> @@ -0,0 +1,132 @@
> +## @file
> +# Standalone MM Platform.
> +#
> +# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR> #
> +Copyright (c) 2016 - 2017, ARM Limited. All rights reserved.<BR> #
> +#    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
> +#
> +#    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +#    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +##
> +
> +#####################################################################
> +###########
> +#
> +# Defines Section - statements that will be processed to create a Makefile.
> +#
> +#####################################################################
> +###########
> +[Defines]
> +  PLATFORM_NAME                  = StandaloneMm
> +  PLATFORM_GUID                  = 9A4BBA60-B4F9-47C7-9258-3BD77CAE9322
> +  PLATFORM_VERSION               = 1.0
> +  DSC_SPECIFICATION              = 0x00010011
> +  OUTPUT_DIRECTORY               = Build/StandaloneMmPkg
> +  SUPPORTED_ARCHITECTURES        = IA32|X64|AARCH64
> +  BUILD_TARGETS                  = DEBUG|RELEASE
> +  SKUID_IDENTIFIER               = DEFAULT
> +  FLASH_DEFINITION               = StandaloneMmPkg/StandaloneMmPkg.fdf
> +  DEFINE DEBUG_MESSAGE           = TRUE
> +
> +  # LzmaF86
> +  DEFINE COMPRESSION_TOOL_GUID   = D42AE6BD-1352-4bfb-909A-CA72A6EAE889
> +
> +#####################################################################
> +###########
> +#
> +# Library Class section - list of all Library Classes needed by this Platform.
> +#
> +#####################################################################
> +###########
> +[LibraryClasses]
> +  #
> +  # Basic
> +  #
> +  BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
> +  BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
> +
> +DebugLib|MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort
> +.inf
> +
> +DebugPrintErrorLevelLib|MdePkg/Library/BaseDebugPrintErrorLevelLib/Ba
> +seDebugPrintErrorLevelLib.inf
> +  FvLib|StandaloneMmPkg/Library/FvLib/FvLib.inf
> +  IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
> +
> +MemoryAllocationLib|StandaloneMmPkg/Library/MemoryAllocationLib/Memor
> +yAllocationLib.inf
> +  PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
> +  PeCoffLib|MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf
> +  PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf
> +
> +ReportStatusCodeLib|MdePkg/Library/BaseReportStatusCodeLibNull/BaseRe
> +portStatusCodeLibNull.inf
> +
> +  #
> +  # Entry point
> +  #
> +
> + StandaloneMmDriverEntryPoint|StandaloneMmPkg/Library/StandaloneMmDri
> + verEntryPoint/StandaloneMmDriverEntryPoint.inf
> +
> +[LibraryClasses.AARCH64]
> +  ArmLib|ArmPkg/Library/ArmLib/ArmBaseLib.inf
> +  ArmMmuLib|ArmPkg/Library/ArmMmuLib/ArmMmuSecLib.inf
> +  ArmSvcLib|ArmPkg/Library/ArmSvcLib/ArmSvcLib.inf
> +
> +CacheMaintenanceLib|ArmPkg/Library/ArmCacheMaintenanceLib/ArmCacheMai
> +ntenanceLib.inf
> +  HobLib|StandaloneMmPkg/Library/HobLib/HobLib.inf
> +  MemLib|StandaloneMmPkg/Library/MemLib/MemLib.inf
> +
> +PeCoffExtraActionLib|ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPe
> +CoffExtraActionLib.inf
> +  PL011UartLib|ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.inf
> +  # ARM PL011 UART Driver
> +
> +SerialPortLib|ArmPlatformPkg/Library/PL011SerialPortLib/PL011SerialPo
> +rtLib.inf
> +
> +
> + StandaloneMmCoreEntryPoint|StandaloneMmPkg/Library/StandaloneMmCoreE
> + ntryPoint/StandaloneMmCoreEntryPoint.inf
> +
> +#####################################################################
> +###########
> +#
> +# Pcd Section - list of all EDK II PCD Entries defined by this
> +Platform #
> +#####################################################################
> +###########
> +[PcdsFeatureFlag]
> +  gStandaloneMmPkgTokenSpaceGuid.PcdStandaloneMmEnable|TRUE
> +
> +[PcdsFixedAtBuild]
> +  gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x800000CF
> +  gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0xff
> +  gEfiMdePkgTokenSpaceGuid.PcdReportStatusCodePropertyMask|0x0f
> +
> +[PcdsFixedAtBuild.AARCH64]
> +  ## PL011 - Serial Terminal
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialRegisterBase|0x1c0b0000
> +  gEfiMdePkgTokenSpaceGuid.PcdUartDefaultBaudRate|115200
> +
> +#####################################################################
> +##############################
> +#
> +# Components Section - list of the modules and components that will be processed by compilation
> +#                      tools and the EDK II tools to generate PE32/PE32+/Coff image files.
> +#
> +# Note: The EDK II DSC file is not used to specify how compiled binary images get placed
> +#       into firmware volume images. This section is just a list of modules to compile from
> +#       source into UEFI-compliant binaries.
> +#       It is the FDF file that contains information on combining binary files into firmware
> +#       volume images, whose concept is beyond UEFI and is described in PI specification.
> +#       Binary modules do not need to be listed in this section, as they should be
> +#       specified in the FDF file. For example: Shell binary (Shell_Full.efi), FAT binary (Fat.efi),
> +#       Logo (Logo.bmp), and etc.
> +#       There may also be modules listed in this section that are not required in the FDF file,
> +#       When a module listed here is excluded from FDF file, then UEFI-compliant binary will be
> +#       generated for it, but the binary will not be put into any firmware volume.
> +#
> +#####################################################################
> +##############################
> +[Components.common]
> +  #
> +  # MM Core
> +  #
> +  StandaloneMmPkg/Core/StandaloneMmCore.inf
> +
> +[Components.AARCH64]
> +  StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> +
> +#####################################################################
> +##############################
> +#
> +# BuildOptions Section - Define the module specific tool chain flags that should be used as
> +#                        the default flags for a module. These flags are appended to any
> +#                        standard flags that are defined by the build process. They can be
> +#                        applied for any modules or only those modules with the specific
> +#                        module style (EDK or EDKII) specified in [Components] section.
> +#
> +#####################################################################
> +##############################
> +[BuildOptions.Common]
> +GCC:*_*_*_DLINK_FLAGS = -z common-page-size=0x1000
> diff --git a/StandaloneMmPkg/StandaloneMmPkg.fdf
> b/StandaloneMmPkg/StandaloneMmPkg.fdf
> new file mode 100644
> index 0000000000..7a22a51b4c
> --- /dev/null
> +++ b/StandaloneMmPkg/StandaloneMmPkg.fdf
> @@ -0,0 +1,184 @@
> +#
> +#  Copyright (c) 2011 - 2017, ARM Limited. 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
> +#
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, #  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +
> +#####################################################################
> +###########
> +#
> +# FD Section
> +# The [FD] Section is made up of the definition statements and a #
> +description of what goes into  the Flash Device Image.  Each FD
> +section # defines one flash "device" image.  A flash device image may
> +be one of # the following: Removable media bootable image (like a
> +boot floppy # image,) an Option ROM image (that would be "flashed"
> +into an add-in # card,) a System "Flash"  image (that would be burned
> +into a system's # flash) or an Update ("Capsule") image that will be
> +used to update and # existing system flash.
> +#
> +#####################################################################
> +###########
> +
> +[FD.]
> +!ifdef ARM_FVP_RUN_NORFLASH
> +BaseAddress   = 0x08000000|gArmTokenSpaceGuid.PcdFdBaseAddress  # The base address of the Firmware in Flash0.
> +!else
> +BaseAddress   = 0xff200000|gArmTokenSpaceGuid.PcdFdBaseAddress  # UEFI in DRAM + 128MB.
> +!endif
> +Size          = 0x00e00000|gArmTokenSpaceGuid.PcdFdSize         # The size in bytes of the device (64MiB).
> +ErasePolarity = 1
> +
> +# This one is tricky, it must be: BlockSize * NumBlocks = Size
> +BlockSize     = 0x00001000
> +NumBlocks     = 0x0e00
> +
> +0x00000000|0x00280000
> +gArmTokenSpaceGuid.PcdFvBaseAddress|gArmTokenSpaceGuid.PcdFvSize
> +FV = FVMAIN_COMPACT
> +
> +[FV.FVMAIN_COMPACT]
> +FvAlignment        = 16
> +ERASE_POLARITY     = 1
> +MEMORY_MAPPED      = TRUE
> +STICKY_WRITE       = TRUE
> +LOCK_CAP           = TRUE
> +LOCK_STATUS        = TRUE
> +WRITE_DISABLED_CAP = TRUE
> +WRITE_ENABLED_CAP  = TRUE
> +WRITE_STATUS       = TRUE
> +WRITE_LOCK_CAP     = TRUE
> +WRITE_LOCK_STATUS  = TRUE
> +READ_DISABLED_CAP  = TRUE
> +READ_ENABLED_CAP   = TRUE
> +READ_STATUS        = TRUE
> +READ_LOCK_CAP      = TRUE
> +READ_LOCK_STATUS   = TRUE
> +
> +INF StandaloneMmPkg/Core/StandaloneMmCore.inf
> +INF StandaloneMmPkg/Drivers/CpuMm/Arm/PiMmStandloneArmTfCpuDriver.inf
> +
> +#####################################################################
> +###########
> +#
> +# Rules are use with the [FV] section's module INF type to define #
> +how an FFS file is created for a given INF file. The following Rule
> +are the default # rules for the different module type. User can add
> +the customized rules to define the # content of the FFS file.
> +#
> +#####################################################################
> +###########
> +
> +
> +############################################################################
> +# Example of a DXE_DRIVER FFS file with a Checksum encapsulation section   #
> +#####################################################################
> +#######
> +#
> +#[Rule.Common.DXE_DRIVER]
> +#  FILE DRIVER = $(NAMED_GUID) {
> +#    DXE_DEPEX    DXE_DEPEX               Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +#    COMPRESS PI_STD {
> +#      GUIDED {
> +#        PE32     PE32                    $(INF_OUTPUT)/$(MODULE_NAME).efi
> +#        UI       STRING="$(MODULE_NAME)" Optional
> +#        VERSION  STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +#      }
> +#    }
> +#  }
> +#
> +#####################################################################
> +#######
> +
> +[Rule.Common.SEC]
> +  FILE SEC = $(NAMED_GUID) RELOCS_STRIPPED FIXED {
> +    TE  TE Align = Auto                 $(INF_OUTPUT)/$(MODULE_NAME).efi
> +  }
> +
> +[Rule.Common.MM_CORE_STANDALONE]
> +  FILE SEC = $(NAMED_GUID) RELOCS_STRIPPED FIXED {
> +    PE32  PE32 Align = Auto             $(INF_OUTPUT)/$(MODULE_NAME).efi
> +  }
> +
> +[Rule.Common.MM_STANDALONE]
> +  FILE MM_STANDALONE = $(NAMED_GUID) {
> +    SMM_DEPEX SMM_DEPEX Optional       $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32      PE32                     $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI        STRING="$(MODULE_NAME)" Optional
> +    VERSION   STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +  }
> +
> +[Rule.Common.PEI_CORE]
> +  FILE PEI_CORE = $(NAMED_GUID) FIXED {
> +    TE     TE Align = Auto              $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI     STRING ="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.PEIM]
> +  FILE PEIM = $(NAMED_GUID) FIXED {
> +     PEI_DEPEX PEI_DEPEX Optional       $(INF_OUTPUT)/$(MODULE_NAME).depex
> +     TE       TE Align = Auto           $(INF_OUTPUT)/$(MODULE_NAME).efi
> +     UI       STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.PEIM.TIANOCOMPRESSED]
> +  FILE PEIM = $(NAMED_GUID) DEBUG_MYTOOLS_IA32 {
> +    PEI_DEPEX PEI_DEPEX Optional        $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    GUIDED A31280AD-481E-41B6-95E8-127F4C984779 PROCESSING_REQUIRED = TRUE {
> +      PE32      PE32                    $(INF_OUTPUT)/$(MODULE_NAME).efi
> +      UI        STRING="$(MODULE_NAME)" Optional
> +    }
> +  }
> +
> +[Rule.Common.DXE_CORE]
> +  FILE DXE_CORE = $(NAMED_GUID) {
> +    PE32     PE32                       $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI       STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.UEFI_DRIVER]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32         PE32                   $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI           STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.DXE_DRIVER]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32         PE32                   $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI           STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.DXE_RUNTIME_DRIVER]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
> +    PE32         PE32                   $(INF_OUTPUT)/$(MODULE_NAME).efi
> +    UI           STRING="$(MODULE_NAME)" Optional
> +  }
> +
> +[Rule.Common.UEFI_APPLICATION]
> +  FILE APPLICATION = $(NAMED_GUID) {
> +    UI     STRING ="$(MODULE_NAME)"     Optional
> +    PE32   PE32                         $(INF_OUTPUT)/$(MODULE_NAME).efi
> +  }
> +
> +[Rule.Common.UEFI_DRIVER.BINARY]
> +  FILE DRIVER = $(NAMED_GUID) {
> +    DXE_DEPEX DXE_DEPEX Optional      |.depex
> +    PE32      PE32                    |.efi
> +    UI        STRING="$(MODULE_NAME)" Optional
> +    VERSION   STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +  }
> +
> +[Rule.Common.UEFI_APPLICATION.BINARY]
> +  FILE APPLICATION = $(NAMED_GUID) {
> +    PE32      PE32                    |.efi
> +    UI        STRING="$(MODULE_NAME)" Optional
> +    VERSION   STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
> +  }
> +
> +[Rule.Common.USER_DEFINED.ACPITABLE]
> +  FILE FREEFORM = $(NAMED_GUID) {
> +    RAW ASL                |.aml
> +  }
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 15/18] ArmPkg: Extra action to update permissions for S-ELO MM Image.
  2018-04-30 19:49   ` Achin Gupta
@ 2018-05-04 23:30     ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:30 UTC (permalink / raw)
  To: Achin Gupta
  Cc: edk2-devel@lists.01.org, leif.lindholm@linaro.org,
	ard.biesheuvel@linaro.org

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Monday, April 30, 2018 2:49 PM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 15/18] ArmPkg: Extra action to update permissions for S-ELO MM Image.

Hi Supreeth,

This file was originally contributed by Ard a while back so worth poking him and Leif for review. If MM is expected to be the only use case of this library then it might make sense to pull in under the StandaloneMmPkg instead of relying on PcdStandaloneMmEnable.
[Supreeth] I will let Ard and Leif comment on it.
It's basically a debate between code duplication of the entire library in StandaloneMmPkg Vs addition of one PCD check and one function in existing ArmPkg library.

Cheers,
Achin

On Fri, Apr 06, 2018 at 03:42:20PM +0100, Supreeth Venkatesh wrote:
> The Standalone MM drivers runs in S-EL0 in AArch64 on ARM Standard
> Platforms and is deployed during SEC phase. The memory allocated to
> the Standalone MM drivers should be marked as RO+X.
>
> During PE/COFF Image section parsing, this patch implements extra
> action "UpdatePeCoffPermissions" to request the privileged firmware in
> EL3 to update the permissions.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  .../DebugPeCoffExtraActionLib.c                    | 185 +++++++++++++++++++--
>  .../DebugPeCoffExtraActionLib.inf                  |   7 +
>  2 files changed, 181 insertions(+), 11 deletions(-)
>
> diff --git
> a/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.c
> b/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.c
> index f298e58cdf..c87aaf05c7 100644
> ---
> a/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.c
> +++ b/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionL
> +++ ib.c
> @@ -15,14 +15,165 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>  **/
>
>  #include <PiDxe.h>
> -#include <Library/PeCoffLib.h>
>
> +#include <Library/ArmMmuLib.h>
>  #include <Library/BaseLib.h>
> -#include <Library/DebugLib.h>
>  #include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/PcdLib.h>
> +#include <Library/PeCoffLib.h>
>  #include <Library/PeCoffExtraActionLib.h>  #include
> <Library/PrintLib.h>
>
> +typedef RETURN_STATUS (*REGION_PERMISSION_UPDATE_FUNC) (
> +  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
> +  IN  UINT64                    Length
> +  );
> +
> +STATIC
> +RETURN_STATUS
> +UpdatePeCoffPermissions (
> +  IN  CONST PE_COFF_LOADER_IMAGE_CONTEXT      *ImageContext,
> +  IN  REGION_PERMISSION_UPDATE_FUNC           NoExecUpdater,
> +  IN  REGION_PERMISSION_UPDATE_FUNC           ReadOnlyUpdater
> +  )
> +{
> +  RETURN_STATUS                         Status;
> +  EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION   Hdr;
> +  EFI_IMAGE_OPTIONAL_HEADER_UNION       HdrData;
> +  UINTN                                 Size;
> +  UINTN                                 ReadSize;
> +  UINT32                                SectionHeaderOffset;
> +  UINTN                                 NumberOfSections;
> +  UINTN                                 Index;
> +  EFI_IMAGE_SECTION_HEADER              SectionHeader;
> +  PE_COFF_LOADER_IMAGE_CONTEXT          TmpContext;
> +  EFI_PHYSICAL_ADDRESS                  Base;
> +
> +  //
> +  // We need to copy ImageContext since PeCoffLoaderGetImageInfo ()
> + // will mangle the ImageAddress field  //  CopyMem (&TmpContext,
> + ImageContext, sizeof (TmpContext));
> +
> +  if (TmpContext.PeCoffHeaderOffset == 0) {
> +    Status = PeCoffLoaderGetImageInfo (&TmpContext);
> +    if (RETURN_ERROR (Status)) {
> +      DEBUG ((DEBUG_ERROR,
> +        "%a: PeCoffLoaderGetImageInfo () failed (Status = %r)\n",
> +        __FUNCTION__, Status));
> +      return Status;
> +    }
> +  }
> +
> +  if (TmpContext.IsTeImage &&
> +      TmpContext.ImageAddress == ImageContext->ImageAddress) {
> +    DEBUG ((DEBUG_INFO, "%a: ignoring XIP TE image at 0x%lx\n", __FUNCTION__,
> +      ImageContext->ImageAddress));
> +    return RETURN_SUCCESS;
> +  }
> +
> +  if (TmpContext.SectionAlignment < EFI_PAGE_SIZE) {
> +    //
> +    // The sections need to be at least 4 KB aligned, since that is the
> +    // granularity at which we can tighten permissions. So just clear the
> +    // noexec permissions on the entire region.
> +    //
> +    if (!TmpContext.IsTeImage) {
> +      DEBUG ((DEBUG_WARN,
> +        "%a: non-TE Image at 0x%lx has SectionAlignment < 4 KB (%lu)\n",
> +        __FUNCTION__, ImageContext->ImageAddress, TmpContext.SectionAlignment));
> +    }
> +    Base = ImageContext->ImageAddress & ~(EFI_PAGE_SIZE - 1);
> +    Size = ImageContext->ImageAddress - Base + ImageContext->ImageSize;
> +    return NoExecUpdater (Base, ALIGN_VALUE (Size, EFI_PAGE_SIZE));
> + }
> +
> +  //
> +  // Read the PE/COFF Header. For PE32 (32-bit) this will read in too
> + much  // data, but that should not hurt anything.
> + Hdr.Pe32->OptionalHeader.Magic  // determines if this is a PE32 or
> + PE32+ image. The magic is in the same  // location in both images.
> +  //
> +  Hdr.Union = &HdrData;
> +  Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);
> +  ReadSize = Size;
> +  Status = TmpContext.ImageRead (TmpContext.Handle,
> +                         TmpContext.PeCoffHeaderOffset, &Size,
> + Hdr.Pe32);  if (RETURN_ERROR (Status) || (Size != ReadSize)) {
> +    DEBUG ((DEBUG_ERROR,
> +      "%a: TmpContext.ImageRead () failed (Status = %r)\n",
> +      __FUNCTION__, Status));
> +    return Status;
> +  }
> +
> +  ASSERT (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE);
> +
> +  SectionHeaderOffset = TmpContext.PeCoffHeaderOffset + sizeof (UINT32) +
> +                        sizeof (EFI_IMAGE_FILE_HEADER);
> +  NumberOfSections    = (UINTN)(Hdr.Pe32->FileHeader.NumberOfSections);
> +
> +  switch (Hdr.Pe32->OptionalHeader.Magic) {
> +    case EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC:
> +      SectionHeaderOffset += Hdr.Pe32->FileHeader.SizeOfOptionalHeader;
> +      break;
> +    case EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC:
> +      SectionHeaderOffset += Hdr.Pe32Plus->FileHeader.SizeOfOptionalHeader;
> +      break;
> +    default:
> +      ASSERT (FALSE);
> +  }
> +
> +  //
> +  // Iterate over the sections
> +  //
> +  for (Index = 0; Index < NumberOfSections; Index++) {
> +    //
> +    // Read section header from file
> +    //
> +    Size = sizeof (EFI_IMAGE_SECTION_HEADER);
> +    ReadSize = Size;
> +    Status = TmpContext.ImageRead (TmpContext.Handle, SectionHeaderOffset,
> +                                   &Size, &SectionHeader);
> +    if (RETURN_ERROR (Status) || (Size != ReadSize)) {
> +      DEBUG ((DEBUG_ERROR,
> +        "%a: TmpContext.ImageRead () failed (Status = %r)\n",
> +        __FUNCTION__, Status));
> +      return Status;
> +    }
> +
> +    Base = TmpContext.ImageAddress + SectionHeader.VirtualAddress;
> +
> +    if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_MEM_EXECUTE)
> + == 0) {
> +
> +      if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_MEM_WRITE) == 0 &&
> +          TmpContext.ImageType !=
> + EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
> +
> +        DEBUG ((DEBUG_INFO,
> +          "%a: Mapping section %d of image at 0x%lx with RO-XN permissions and size 0x%x\n",
> +          __FUNCTION__, Index, Base, SectionHeader.Misc.VirtualSize));
> +        ReadOnlyUpdater (Base, SectionHeader.Misc.VirtualSize);
> +      } else {
> +        DEBUG ((DEBUG_WARN,
> +          "%a: Mapping section %d of image at 0x%lx with RW-XN permissions and size 0x%x\n",
> +          __FUNCTION__, Index, Base, SectionHeader.Misc.VirtualSize));
> +      }
> +    } else {
> +        DEBUG ((DEBUG_INFO,
> +          "%a: Mapping section %d of image at 0x%lx with RO-XN permissions and size 0x%x\n",
> +           __FUNCTION__, Index, Base, SectionHeader.Misc.VirtualSize));
> +        ReadOnlyUpdater (Base, SectionHeader.Misc.VirtualSize);
> +
> +        DEBUG ((DEBUG_INFO,
> +          "%a: Mapping section %d of image at 0x%lx with RO-X permissions and size 0x%x\n",
> +          __FUNCTION__, Index, Base, SectionHeader.Misc.VirtualSize));
> +        NoExecUpdater (Base, SectionHeader.Misc.VirtualSize);
> +    }
> +
> +    SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
> +  }
> +  return RETURN_SUCCESS;
> +}
>
>  /**
>    If the build is done on cygwin the paths are cygpaths.
> @@ -83,23 +234,29 @@ PeCoffLoaderRelocateImageExtraAction (
>    CHAR8 Temp[512];
>  #endif
>
> +  if (PcdGetBool(PcdStandaloneMmEnable) == TRUE)  {
> +     UpdatePeCoffPermissions (ImageContext, ArmClearMemoryRegionNoExec,
> +                              ArmSetMemoryRegionReadOnly);  }
> +
>    if (ImageContext->PdbPointer) {
>  #ifdef __CC_ARM
>  #if (__ARMCC_VERSION < 500000)
>      // Print out the command for the RVD debugger to load symbols for this image
> -    DEBUG ((EFI_D_LOAD | EFI_D_INFO, "load /a /ni /np %a &0x%p\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
> +    DEBUG ((DEBUG_LOAD | DEBUG_INFO, "load /a /ni /np %a &0x%p\n",
> + DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof
> + (Temp)), (UINTN)(ImageContext->ImageAddress +
> + ImageContext->SizeOfHeaders)));
>  #else
>      // Print out the command for the DS-5 to load symbols for this image
> -    DEBUG ((EFI_D_LOAD | EFI_D_INFO, "add-symbol-file %a 0x%p\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
> +    DEBUG ((DEBUG_LOAD | DEBUG_INFO, "add-symbol-file %a 0x%p\n",
> + DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof
> + (Temp)), (UINTN)(ImageContext->ImageAddress +
> + ImageContext->SizeOfHeaders)));
>  #endif
>  #elif __GNUC__
>      // This may not work correctly if you generate PE/COFF directlyas then the Offset would not be required
> -    DEBUG ((EFI_D_LOAD | EFI_D_INFO, "add-symbol-file %a 0x%p\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
> +    DEBUG ((DEBUG_LOAD | DEBUG_INFO, "add-symbol-file %a 0x%p\n",
> + DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof
> + (Temp)), (UINTN)(ImageContext->ImageAddress +
> + ImageContext->SizeOfHeaders)));
>  #else
> -    DEBUG ((EFI_D_LOAD | EFI_D_INFO, "Loading driver at 0x%11p EntryPoint=0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress, FUNCTION_ENTRY_POINT (ImageContext->EntryPoint)));
> +    DEBUG ((DEBUG_LOAD | DEBUG_INFO, "Loading driver at 0x%11p
> + EntryPoint=0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress,
> + FUNCTION_ENTRY_POINT (ImageContext->EntryPoint)));
>  #endif
>    } else {
> -    DEBUG ((EFI_D_LOAD | EFI_D_INFO, "Loading driver at 0x%11p EntryPoint=0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress, FUNCTION_ENTRY_POINT (ImageContext->EntryPoint)));
> +    DEBUG ((DEBUG_LOAD | DEBUG_INFO, "Loading driver at 0x%11p
> + EntryPoint=0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress,
> + FUNCTION_ENTRY_POINT (ImageContext->EntryPoint)));
>    }
>  }
>
> @@ -125,17 +282,23 @@ PeCoffLoaderUnloadImageExtraAction (
>    CHAR8 Temp[512];
>  #endif
>
> +  if (PcdGetBool(PcdStandaloneMmEnable) == TRUE)  {
> +     UpdatePeCoffPermissions (ImageContext, ArmSetMemoryRegionNoExec,
> +                              ArmClearMemoryRegionReadOnly);  }
> +
>    if (ImageContext->PdbPointer) {
>  #ifdef __CC_ARM
>      // Print out the command for the RVD debugger to load symbols for this image
> -    DEBUG ((EFI_D_ERROR, "unload symbols_only %a\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp))));
> +    DEBUG ((DEBUG_ERROR, "unload symbols_only %a\n",
> + DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof
> + (Temp))));
>  #elif __GNUC__
>      // This may not work correctly if you generate PE/COFF directlyas then the Offset would not be required
> -    DEBUG ((EFI_D_ERROR, "remove-symbol-file %a 0x%08x\n", DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof (Temp)), (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)));
> +    DEBUG ((DEBUG_ERROR, "remove-symbol-file %a 0x%08x\n",
> + DeCygwinPathIfNeeded (ImageContext->PdbPointer, Temp, sizeof
> + (Temp)), (UINTN)(ImageContext->ImageAddress +
> + ImageContext->SizeOfHeaders)));
>  #else
> -    DEBUG ((EFI_D_ERROR, "Unloading %a\n", ImageContext->PdbPointer));
> +    DEBUG ((DEBUG_ERROR, "Unloading %a\n",
> + ImageContext->PdbPointer));
>  #endif
>    } else {
> -    DEBUG ((EFI_D_ERROR, "Unloading driver at 0x%11p\n", (VOID *)(UINTN) ImageContext->ImageAddress));
> +    DEBUG ((DEBUG_ERROR, "Unloading driver at 0x%11p\n", (VOID
> + *)(UINTN) ImageContext->ImageAddress));
>    }
>  }
> diff --git
> a/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.i
> nf
> b/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.i
> nf
> index c1f717e5bd..38bf3993ae 100644
> ---
> a/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionLib.i
> nf
> +++ b/ArmPkg/Library/DebugPeCoffExtraActionLib/DebugPeCoffExtraActionL
> +++ ib.inf
> @@ -33,7 +33,14 @@
>    DebugPeCoffExtraActionLib.c
>
>  [Packages]
> +  ArmPkg/ArmPkg.dec
>    MdePkg/MdePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[FeaturePcd]
> +  gStandaloneMmPkgTokenSpaceGuid.PcdStandaloneMmEnable
>
>  [LibraryClasses]
> +  ArmMmuLib
>    DebugLib
> +  PcdLib
> --
> 2.16.2
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 16/18] BaseTools/AutoGen: Update header file for MM modules.
  2018-04-30 19:52   ` Achin Gupta
@ 2018-05-04 23:30     ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:30 UTC (permalink / raw)
  To: Achin Gupta
  Cc: edk2-devel@lists.01.org, michael.d.kinney@intel.com,
	liming.gao@intel.com, jiewen.yao@intel.com,
	leif.lindholm@linaro.org, ard.biesheuvel@linaro.org, nd

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Monday, April 30, 2018 2:53 PM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 16/18] BaseTools/AutoGen: Update header file for MM modules.

Hi Supreeth,

CIL.

On Fri, Apr 06, 2018 at 03:42:21PM +0100, Supreeth Venkatesh wrote:
> This patch corrects the Module Type Header file for Management
> Mode(MM) as specified in PI v1.6 Specification. Also, it updates
> parameter for auto generated template functions from
> EFI_SMM_SYSTEM_TABLE2 to EFI_MM_SYSTEM_TABLE.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>

-Achin & +Jiewen if possible!
[Supreeth] Ok.

Acked-by: Achin Gupta <achin.gupta@arm.com>

cheers,
Achin

> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  BaseTools/Source/Python/AutoGen/GenC.py | 16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/BaseTools/Source/Python/AutoGen/GenC.py
> b/BaseTools/Source/Python/AutoGen/GenC.py
> index 4d9ea1b2a8..8601e4ee70 100644
> --- a/BaseTools/Source/Python/AutoGen/GenC.py
> +++ b/BaseTools/Source/Python/AutoGen/GenC.py
> @@ -270,7 +270,7 @@ EFI_STATUS
>  EFIAPI
>  ${Function} (
>    IN EFI_HANDLE            ImageHandle,
> -  IN EFI_SMM_SYSTEM_TABLE2 *MmSystemTable
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
>    );
>  ${END}
>  """)
> @@ -283,7 +283,7 @@ EFI_STATUS
>  EFIAPI
>  ProcessModuleEntryPointList (
>    IN EFI_HANDLE            ImageHandle,
> -  IN EFI_SMM_SYSTEM_TABLE2 *MmSystemTable
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
>    )
>
>  {
> @@ -297,7 +297,7 @@ EFI_STATUS
>  EFIAPI
>  ProcessModuleEntryPointList (
>    IN EFI_HANDLE            ImageHandle,
> -  IN EFI_SMM_SYSTEM_TABLE2 *MmSystemTable
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
>    )
>
>  {
> @@ -312,7 +312,7 @@ EFI_STATUS
>  EFIAPI
>  ProcessModuleEntryPointList (
>    IN EFI_HANDLE            ImageHandle,
> -  IN EFI_SMM_SYSTEM_TABLE2 *MmSystemTable
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
>    )
>
>  {
> @@ -680,7 +680,7 @@ EFI_STATUS
>  EFIAPI
>  ${Function} (
>    IN EFI_HANDLE            ImageHandle,
> -  IN EFI_SMM_SYSTEM_TABLE2  *MmSystemTable
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
>    );${END}
>  """),
>  }
> @@ -760,7 +760,7 @@ VOID
>  EFIAPI
>  ProcessLibrary${Type}List (
>    IN EFI_HANDLE            ImageHandle,
> -  IN EFI_SMM_SYSTEM_TABLE2  *MmSystemTable
> +  IN EFI_MM_SYSTEM_TABLE   *MmSystemTable
>    )
>  {
>  ${BEGIN}  EFI_STATUS  Status;
> @@ -784,8 +784,8 @@ gModuleTypeHeaderFile = {
>      "UEFI_DRIVER"       :   ["Uefi.h",  "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiDriverEntryPoint.h"],
>      "UEFI_APPLICATION"  :   ["Uefi.h",  "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiApplicationEntryPoint.h"],
>      "SMM_CORE"          :   ["PiDxe.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiDriverEntryPoint.h"],
> -    "MM_STANDALONE"     :   ["PiSmm.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/SmmDriverStandaloneEntryPoint.h"],
> -    "MM_CORE_STANDALONE" :  ["PiSmm.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/SmmCoreStandaloneEntryPoint.h"],
> +    "MM_STANDALONE"     :   ["PiMm.h",  "Library/BaseLib.h", "Library/DebugLib.h", "Library/MmDriverStandaloneEntryPoint.h"],
> +    "MM_CORE_STANDALONE":   ["PiMm.h",  "Library/BaseLib.h", "Library/DebugLib.h", "Library/MmCoreStandaloneEntryPoint.h"],
>      "USER_DEFINED"      :   [gBasicHeaderFile]
>  }
>
> --
> 2.16.2
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v1 17/18] StandaloneMmPkg: Add application to test MM communication protocol.
  2018-04-30 20:02   ` Achin Gupta
@ 2018-05-04 23:31     ` Supreeth Venkatesh
  0 siblings, 0 replies; 70+ messages in thread
From: Supreeth Venkatesh @ 2018-05-04 23:31 UTC (permalink / raw)
  To: Achin Gupta, jiewen.yao@intel.com; +Cc: edk2-devel@lists.01.org

My response inline.

-----Original Message-----
From: Achin Gupta
Sent: Monday, April 30, 2018 3:02 PM
To: Supreeth Venkatesh <Supreeth.Venkatesh@arm.com>
Cc: edk2-devel@lists.01.org; michael.d.kinney@intel.com; liming.gao@intel.com; jiewen.yao@intel.com; leif.lindholm@linaro.org; ard.biesheuvel@linaro.org; nd <nd@arm.com>
Subject: Re: [PATCH v1 17/18] StandaloneMmPkg: Add application to test MM communication protocol.

Hi Supreeth,

I agree with Jiewen that these should not be a part of this series. Can you push these in a separate series?

[Supreeth] Jiewen's comment was "they are only for unit test. I do not think we should check in them". I will not send these patches as of now, if you do need them, Let me know.

cheers,
Achin

On Fri, Apr 06, 2018 at 03:42:22PM +0100, Supreeth Venkatesh wrote:
> This patch adds a simple application that uses the MM communication
> protocol to pass a copy of the UEFI system table to the MM environment
> in the secure world.
>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Achin Gupta <achin.gupta@arm.com>
> Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> ---
>  .../Application/MmCommTestApp/MmCommTest.c         | 81 ++++++++++++++++++++++
>  .../Application/MmCommTestApp/MmCommTest.h         | 37 ++++++++++
>  .../Application/MmCommTestApp/MmCommTest.inf       | 57 +++++++++++++++
>  3 files changed, 175 insertions(+)
>  create mode 100644
> StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.c
>  create mode 100644
> StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.h
>  create mode 100644
> StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.inf
>
> diff --git a/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.c
> b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.c
> new file mode 100644
> index 0000000000..efbafdde62
> --- /dev/null
> +++ b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.c
> @@ -0,0 +1,81 @@
> +/** @file
> +  This sample application demos how to communicate
> +  with secure partition using MM communication protocol
> +
> +  Copyright (c) 2006 - 2008, Intel Corporation. All rights
> + reserved.<BR>  Copyright (c) 2016 - 2018, ARM Limited. All rights
> + reserved.<BR>
> +
> +  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
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> + BASIS,  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include <Uefi.h>
> +#include <Library/PcdLib.h>
> +#include <Library/UefiLib.h>
> +#include <Library/UefiApplicationEntryPoint.h>
> +#include <Library/DebugLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/UefiBootServicesTableLib.h>
> +
> +#include "MmCommTest.h"
> +
> +#include <Library/ArmSmcLib.h>
> +
> +#include <Protocol/MmCommunication.h>
> +
> +EFI_MM_COMMUNICATION_PROTOCOL  *mMmCommunication = NULL;
> +
> +EFI_STATUS
> +MmIplNotifyCommTest (
> +  VOID
> +  )
> +{
> +  EFI_MM_COMMUNICATE_TEST    MmCommTest;
> +  UINTN                      Size;
> +
> +  DEBUG ((DEBUG_INFO, "MmIplNotifyCommTest\n"));
> +
> +  CopyGuid (&MmCommTest.HeaderGuid, &gMmCommTestGuid);  CopyMem
> + (&MmCommTest.Data.EfiSystemTable, gST, sizeof (EFI_SYSTEM_TABLE));
> + MmCommTest.MessageLength = sizeof (EFI_MM_COMMUNICATE_TEST_DATA);
> +
> +  //
> +  // Generate the MM_COMMUNICATE SMC and return the result
> +  //
> +  Size = sizeof (MmCommTest);
> +  return mMmCommunication->Communicate (NULL, &MmCommTest, &Size); }
> +
> +/**
> +  The user Entry Point for Application. The user code starts with
> +this function
> +  as the real entry point for the application.
> +
> +  @param[in] ImageHandle    The firmware allocated handle for the EFI image.
> +  @param[in] SystemTable    A pointer to the EFI System Table.
> +
> +  @retval EFI_SUCCESS       The entry point is executed successfully.
> +  @retval other             Some error occurs when executing this entry point.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MmCommTestEntryPoint (
> +  IN EFI_HANDLE        ImageHandle,
> +  IN EFI_SYSTEM_TABLE  *SystemTable
> +  )
> +{
> +  EFI_STATUS Status;
> +
> +  Status = gBS->LocateProtocol (&gEfiMmCommunicationProtocolGuid,
> + NULL, (VOID **) &mMmCommunication);  if (EFI_ERROR (Status)) {
> +    return Status;
> +  }
> +
> +  return MmIplNotifyCommTest ();
> +}
> diff --git a/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.h
> b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.h
> new file mode 100644
> index 0000000000..8e8305a060
> --- /dev/null
> +++ b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.h
> @@ -0,0 +1,37 @@
> +/** @file
> +  GUIDs for MM Event.
> +
> +Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
> +
> +This program and the accompanying materials are licensed and made
> +available under the terms and conditions of the BSD License that accompanies this distribution.
> +The full text of the license may be found at
> +http://opensource.org/licenses/bsd-license.php.
> +
> +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
> +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#ifndef __MM_COMM_TEST_H__
> +#define __MM_COMM_TEST_H__
> +
> +#define MM_COMM_TEST_GUID \
> +  { 0xa37721e4, 0x8c0b, 0x4bca, { 0xb5, 0xe8, 0xe9, 0x2, 0xa0, 0x25,
> +0x51, 0x4e }}
> +
> +extern EFI_GUID gMmCommTestGuid;
> +
> +#pragma pack(1)
> +typedef struct {
> +  EFI_SYSTEM_TABLE      EfiSystemTable;
> +} EFI_MM_COMMUNICATE_TEST_DATA;
> +
> +typedef struct {
> +  EFI_GUID                         HeaderGuid;
> +  UINTN                            MessageLength;
> +  EFI_MM_COMMUNICATE_TEST_DATA     Data;
> +} EFI_MM_COMMUNICATE_TEST;
> +#pragma pack()
> +
> +#endif
> diff --git a/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.inf
> b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.inf
> new file mode 100644
> index 0000000000..1828cd7e13
> --- /dev/null
> +++ b/StandaloneMmPkg/Application/MmCommTestApp/MmCommTest.inf
> @@ -0,0 +1,57 @@
> +## @file
> +#  Sample UEFI Application Reference EDKII Module.
> +#
> +#  This is a sample shell application that will print "UEFI firmware
> +version Info!" to the #  UEFI Console.
> +#
> +#  It demos how to communicate with secure partition using MM #
> +communication protocol.
> +#
> +#  Copyright (c) 2008 - 2014, Intel Corporation. All rights
> +reserved.<BR> #  Copyright (c) 2016 - 2018, ARM Limited. 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
> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> +BASIS, #  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
> +#
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001A
> +  BASE_NAME                      = MmCommTest
> +  FILE_GUID                      = 6987936E-ED34-44db-AE97-1FA5E4ED2116
> +  MODULE_TYPE                    = UEFI_APPLICATION
> +  VERSION_STRING                 = 1.0
> +  ENTRY_POINT                    = MmCommTestEntryPoint
> +
> +[Sources]
> +  MmCommTest.c
> +
> +[Packages]
> +  ArmPkg/ArmPkg.dec
> +  ArmPlatformPkg/ArmPlatformPkg.dec
> +  MdePkg/MdePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[LibraryClasses]
> +  ArmSmcLib
> +  BaseMemoryLib
> +  DebugLib
> +  PrintLib
> +  UefiApplicationEntryPoint
> +  UefiBootServicesTableLib
> +  UefiLib
> +
> +[FeaturePcd]
> +
> +[Pcd]
> +
> +[Guids]
> +  gMmCommTestGuid
> +
> +[Protocols]
> +  gEfiMmCommunicationProtocolGuid
> --
> 2.16.2
>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


^ permalink raw reply	[flat|nested] 70+ messages in thread

end of thread, other threads:[~2018-05-04 23:31 UTC | newest]

Thread overview: 70+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-04-06 14:42 [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 01/18] ArmPkg: Add PCDs needed for MM communication driver Supreeth Venkatesh
2018-04-11 14:43   ` Achin Gupta
     [not found]     ` <AM4PR0802MB23063743A3B2F5A552BE320580870@AM4PR0802MB2306.eurprd08.prod.outlook.com>
2018-05-04 23:13       ` Supreeth Venkatesh
2018-05-04 23:17     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 02/18] ArmPkg/Drivers: Add EFI_MM_COMMUNICATION_PROTOCOL DXE driver Supreeth Venkatesh
2018-04-11 14:00   ` Achin Gupta
2018-05-04 23:18     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 03/18] ArmPkg/Include: Add MM interface SVC return codes Supreeth Venkatesh
2018-04-11 14:38   ` Achin Gupta
2018-05-04 23:19     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 04/18] ArmPkg/ArmMmuLib: Add MMU Library suitable for use in S-EL0 Supreeth Venkatesh
2018-04-11 19:21   ` Achin Gupta
2018-05-04 23:19     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 05/18] ArmPkg/ArmMmuLib: Add MMU library inf file " Supreeth Venkatesh
2018-04-11 19:24   ` Achin Gupta
2018-05-04 23:19     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 06/18] StandaloneMmPkg: Add an AArch64 specific entry point library Supreeth Venkatesh
2018-04-16 14:04   ` Achin Gupta
2018-05-04 23:20     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 07/18] StandaloneMmPkg/FvLib: Add a common FV Library for management mode Supreeth Venkatesh
2018-04-16 14:44   ` Achin Gupta
2018-05-04 23:21     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 08/18] StandaloneMmPkg/MemLib: AARCH64 Specific instance of memory check library Supreeth Venkatesh
2018-04-16 15:12   ` Achin Gupta
2018-04-16 22:30     ` Yao, Jiewen
2018-04-25 10:35       ` Achin Gupta
2018-04-26 13:02         ` Yao, Jiewen
2018-05-04 23:21     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 09/18] StandaloneMmPkg/MemoryAllocationLib: Add MM memory allocation library Supreeth Venkatesh
2018-04-25 14:33   ` Achin Gupta
2018-04-26 13:05     ` Yao, Jiewen
2018-05-04 23:23       ` Supreeth Venkatesh
2018-05-04 23:21     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 10/18] StandaloneMmPkg/HobLib: Add AARCH64 Specific HOB Library for management mode Supreeth Venkatesh
2018-04-25 14:50   ` Achin Gupta
2018-04-26 13:04     ` Yao, Jiewen
2018-05-04 23:22       ` Supreeth Venkatesh
2018-05-04 23:25     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 11/18] StandaloneMmPkg: MM driver entry point library Supreeth Venkatesh
2018-04-30 14:29   ` Achin Gupta
2018-05-04 23:24     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 12/18] StandaloneMmPkg/CpuMm: Add CPU driver suitable for ARM Platforms Supreeth Venkatesh
2018-04-18 22:09   ` Daniil Egranov
2018-05-04 23:25     ` Supreeth Venkatesh
2018-04-30 15:50   ` Achin Gupta
2018-05-04 23:24     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 13/18] StandaloneMmPkg/Core: Implementation of Standalone MM Core Module Supreeth Venkatesh
2018-04-30 19:19   ` Achin Gupta
2018-04-30 19:28     ` Ard Biesheuvel
2018-04-30 20:17       ` Achin Gupta
2018-05-01  8:18       ` Laszlo Ersek
2018-05-04 23:28     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 14/18] StandaloneMmPkg: Describe the declaration, definition and fdf files Supreeth Venkatesh
2018-04-18 19:50   ` Daniil Egranov
2018-05-04 23:29     ` Supreeth Venkatesh
2018-04-30 19:32   ` Achin Gupta
2018-05-04 23:28     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 15/18] ArmPkg: Extra action to update permissions for S-ELO MM Image Supreeth Venkatesh
2018-04-30 19:49   ` Achin Gupta
2018-05-04 23:30     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 16/18] BaseTools/AutoGen: Update header file for MM modules Supreeth Venkatesh
2018-04-30 19:52   ` Achin Gupta
2018-05-04 23:30     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 17/18] StandaloneMmPkg: Add application to test MM communication protocol Supreeth Venkatesh
2018-04-30 20:02   ` Achin Gupta
2018-05-04 23:31     ` Supreeth Venkatesh
2018-04-06 14:42 ` [PATCH v1 18/18] StandaloneMmPkg: Add handler to handle event received from Normal World Supreeth Venkatesh
2018-04-08  6:01 ` [PATCH v1 00/18] *** Standalone Management Mode Core Interface for AARCH64 Platforms *** Yao, Jiewen
2018-05-04 23:15   ` Supreeth Venkatesh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox