MdeModulePkg DxeCoreMemoryAllocationLib: Extend to support MemoryProfileLib

Cc: Jiewen Yao <jiewen.yao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
diff --git a/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationLib.inf b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationLib.inf
index 0747f6e..caba8cd 100644
--- a/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationLib.inf
+++ b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationLib.inf
@@ -1,10 +1,10 @@
 ## @file

 # Memory Allocation Library instance dedicated to DXE Core.

 # The implementation borrows the DxeCore Memory Allocation services as the primitive

-# for memory allocation instead of using UEFI boot servces in an indirect way.

-# It is assumed that this library instance must be linked with DxeCore in this package. 

+# for memory allocation instead of using UEFI boot services in an indirect way.

+# It is assumed that this library instance must be linked with DxeCore in this package.

 #

-# Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.<BR>

+# Copyright (c) 2008 - 2016, 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

@@ -24,7 +24,7 @@
   MODULE_TYPE                    = DXE_CORE

   VERSION_STRING                 = 1.0

   LIBRARY_CLASS                  = MemoryAllocationLib|DXE_CORE

-  

+

 #

 # The following information is for reference only and not required by the build tools.

 #

@@ -34,13 +34,12 @@
 [Sources]

   MemoryAllocationLib.c

   DxeCoreMemoryAllocationServices.h

+  DxeCoreMemoryProfileLibNull.c

 

 [Packages]

   MdePkg/MdePkg.dec

-

+  MdeModulePkg/MdeModulePkg.dec

 

 [LibraryClasses]

   DebugLib

   BaseMemoryLib

-

-

diff --git a/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationLib.uni b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationLib.uni
index 85f8c7e..aa67c8b 100644
--- a/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationLib.uni
+++ b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationLib.uni
@@ -2,10 +2,10 @@
 // Memory Allocation Library instance dedicated to DXE Core.

 //

 // The implementation borrows the DxeCore Memory Allocation services as the primitive

-// for memory allocation instead of using UEFI boot servces in an indirect way.

+// for memory allocation instead of using UEFI boot services in an indirect way.

 // It is assumed that this library instance must be linked with DxeCore in this package.

 //

-// Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.<BR>

+// Copyright (c) 2008 - 2016, 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

diff --git a/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationProfileLib.inf b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationProfileLib.inf
new file mode 100644
index 0000000..a2b5f8c
--- /dev/null
+++ b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationProfileLib.inf
@@ -0,0 +1,48 @@
+## @file

+# Memory Allocation/Profile Library instance dedicated to DXE Core.

+# The implementation borrows the DxeCore Memory Allocation/profile services as the primitive

+# for memory allocation/profile instead of using UEFI boot services or memory profile protocol in an indirect way.

+# It is assumed that this library instance must be linked with DxeCore in this package.

+#

+# Copyright (c) 2008 - 2016, 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

+#  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                    = 0x00010005

+  BASE_NAME                      = DxeCoreMemoryAllocationProfileLib

+  MODULE_UNI_FILE                = DxeCoreMemoryAllocationProfileLib.uni

+  FILE_GUID                      = 7ADD7147-74E8-4583-BE34-B6BC45353BB5

+  MODULE_TYPE                    = DXE_CORE

+  VERSION_STRING                 = 1.0

+  LIBRARY_CLASS                  = MemoryAllocationLib|DXE_CORE

+  LIBRARY_CLASS                  = MemoryProfileLib|DXE_CORE

+

+#

+# The following information is for reference only and not required by the build tools.

+#

+#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC

+#

+

+[Sources]

+  MemoryAllocationLib.c

+  DxeCoreMemoryAllocationServices.h

+  DxeCoreMemoryProfileLib.c

+  DxeCoreMemoryProfileServices.h

+

+[Packages]

+  MdePkg/MdePkg.dec

+  MdeModulePkg/MdeModulePkg.dec

+

+[LibraryClasses]

+  DebugLib

+  BaseMemoryLib

+

diff --git a/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationProfileLib.uni b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationProfileLib.uni
new file mode 100644
index 0000000..82cdca6
--- /dev/null
+++ b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationProfileLib.uni
@@ -0,0 +1,23 @@
+// /** @file

+// Memory Allocation/Profile Library instance dedicated to DXE Core.

+//

+// The implementation borrows the DxeCore Memory Allocation/Profile services as the primitive

+// for memory allocation/profile instead of using UEFI boot services or memory profile protocol in an indirect way.

+// It is assumed that this library instance must be linked with DxeCore in this package.

+//

+// Copyright (c) 2008 - 2016, 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

+// 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.

+//

+// **/

+

+

+#string STR_MODULE_ABSTRACT             #language en-US "Memory Allocation/Profile Library instance dedicated to DXE Core"

+

+#string STR_MODULE_DESCRIPTION          #language en-US "The implementation borrows the DxeCore Memory Allocation/Profile services as the primitive for memory allocation/profile instead of using UEFI boot services or memory profile protocol in an indirect way. It is assumed that this library instance must be linked with DxeCore in this package."

+

diff --git a/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryProfileLib.c b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryProfileLib.c
new file mode 100644
index 0000000..8f28b98
--- /dev/null
+++ b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryProfileLib.c
@@ -0,0 +1,57 @@
+/** @file

+  Support routines for memory profile for DxeCore.

+

+  Copyright (c) 2016, 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         

+  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 <PiDxe.h>

+

+#include <Guid/MemoryProfile.h>

+

+#include "DxeCoreMemoryProfileServices.h"

+

+/**

+  Record memory profile of multilevel caller.

+

+  @param[in] CallerAddress      Address of caller.

+  @param[in] Action             Memory profile action.

+  @param[in] MemoryType         Memory type.

+                                EfiMaxMemoryType means the MemoryType is unknown.

+  @param[in] Buffer             Buffer address.

+  @param[in] Size               Buffer size.

+  @param[in] ActionString       String for memory profile action.

+                                Only needed for user defined allocate action.

+

+  @return EFI_SUCCESS           Memory profile is updated.

+  @return EFI_UNSUPPORTED       Memory profile is unsupported,

+                                or memory profile for the image is not required,

+                                or memory profile for the memory type is not required.

+  @return EFI_ACCESS_DENIED     It is during memory profile data getting.

+  @return EFI_ABORTED           Memory profile recording is not enabled.

+  @return EFI_OUT_OF_RESOURCES  No enough resource to update memory profile for allocate action.

+  @return EFI_NOT_FOUND         No matched allocate info found for free action.

+

+**/

+EFI_STATUS

+EFIAPI

+MemoryProfileLibRecord (

+  IN PHYSICAL_ADDRESS           CallerAddress,

+  IN MEMORY_PROFILE_ACTION      Action,

+  IN EFI_MEMORY_TYPE            MemoryType,

+  IN VOID                       *Buffer,

+  IN UINTN                      Size,

+  IN CHAR8                      *ActionString OPTIONAL

+  )

+{

+  return CoreUpdateProfile (CallerAddress, Action, MemoryType, Size, Buffer, ActionString);

+}

+

diff --git a/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryProfileLibNull.c b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryProfileLibNull.c
new file mode 100644
index 0000000..9ae0db8
--- /dev/null
+++ b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryProfileLibNull.c
@@ -0,0 +1,55 @@
+/** @file

+  Null routines for memory profile for DxeCore.

+

+  Copyright (c) 2016, 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         

+  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 <PiDxe.h>

+

+#include <Guid/MemoryProfile.h>

+

+/**

+  Record memory profile of multilevel caller.

+

+  @param[in] CallerAddress      Address of caller.

+  @param[in] Action             Memory profile action.

+  @param[in] MemoryType         Memory type.

+                                EfiMaxMemoryType means the MemoryType is unknown.

+  @param[in] Buffer             Buffer address.

+  @param[in] Size               Buffer size.

+  @param[in] ActionString       String for memory profile action.

+                                Only needed for user defined allocate action.

+

+  @return EFI_SUCCESS           Memory profile is updated.

+  @return EFI_UNSUPPORTED       Memory profile is unsupported,

+                                or memory profile for the image is not required,

+                                or memory profile for the memory type is not required.

+  @return EFI_ACCESS_DENIED     It is during memory profile data getting.

+  @return EFI_ABORTED           Memory profile recording is not enabled.

+  @return EFI_OUT_OF_RESOURCES  No enough resource to update memory profile for allocate action.

+  @return EFI_NOT_FOUND         No matched allocate info found for free action.

+

+**/

+EFI_STATUS

+EFIAPI

+MemoryProfileLibRecord (

+  IN PHYSICAL_ADDRESS           CallerAddress,

+  IN MEMORY_PROFILE_ACTION      Action,

+  IN EFI_MEMORY_TYPE            MemoryType,

+  IN VOID                       *Buffer,

+  IN UINTN                      Size,

+  IN CHAR8                      *ActionString OPTIONAL

+  )

+{

+  return EFI_UNSUPPORTED;

+}

+

diff --git a/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryProfileServices.h b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryProfileServices.h
new file mode 100644
index 0000000..619d7ad
--- /dev/null
+++ b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryProfileServices.h
@@ -0,0 +1,54 @@
+/** @file

+  Contains function prototypes for Memory Profile Services in DxeCore.

+

+  This header file borrows the DxeCore Memory Profile services as the primitive

+  for memory profile.

+

+  Copyright (c) 2016, 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         

+  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 _DXE_CORE_MEMORY_PROFILE_SERVICES_H_

+#define _DXE_CORE_MEMORY_PROFILE_SERVICES_H_

+

+/**

+  Update memory profile information.

+

+  @param CallerAddress  Address of caller who call Allocate or Free.

+  @param Action         This Allocate or Free action.

+  @param MemoryType     Memory type.

+                        EfiMaxMemoryType means the MemoryType is unknown.

+  @param Size           Buffer size.

+  @param Buffer         Buffer address.

+  @param ActionString   String for memory profile action.

+                        Only needed for user defined allocate action.

+

+  @return EFI_SUCCESS           Memory profile is updated.

+  @return EFI_UNSUPPORTED       Memory profile is unsupported,

+                                or memory profile for the image is not required,

+                                or memory profile for the memory type is not required.

+  @return EFI_ACCESS_DENIED     It is during memory profile data getting.

+  @return EFI_ABORTED           Memory profile recording is not enabled.

+  @return EFI_OUT_OF_RESOURCES  No enough resource to update memory profile for allocate action.

+  @return EFI_NOT_FOUND         No matched allocate info found for free action.

+

+**/

+EFI_STATUS

+EFIAPI

+CoreUpdateProfile (

+  IN PHYSICAL_ADDRESS       CallerAddress,

+  IN MEMORY_PROFILE_ACTION  Action,

+  IN EFI_MEMORY_TYPE        MemoryType,

+  IN UINTN                  Size,       // Valid for AllocatePages/FreePages/AllocatePool

+  IN VOID                   *Buffer,

+  IN CHAR8                  *ActionString OPTIONAL

+  );

+

+#endif

diff --git a/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/MemoryAllocationLib.c b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/MemoryAllocationLib.c
index d7d9ff3..89c19e7 100644
--- a/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/MemoryAllocationLib.c
+++ b/MdeModulePkg/Library/DxeCoreMemoryAllocationLib/MemoryAllocationLib.c
@@ -1,8 +1,9 @@
 /** @file

   Support routines for memory allocation routines based 

-  on boot services for Dxe phase drivers.

+  on DxeCore Memory Allocation services for DxeCore,

+  with memory profile support.

 

-  Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>

+  Copyright (c) 2006 - 2016, 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         

   which accompanies this distribution.  The full text of the license may be found at        

@@ -22,6 +23,8 @@
 #include <Library/DebugLib.h>

 #include "DxeCoreMemoryAllocationServices.h"

 

+#include <Library/MemoryProfileLib.h>

+

 /**

   Allocates one or more 4KB pages of a certain memory type.

 

@@ -74,7 +77,20 @@
   IN UINTN  Pages

   )

 {

-  return InternalAllocatePages (EfiBootServicesData, Pages);

+  VOID  *Buffer;

+

+  Buffer = InternalAllocatePages (EfiBootServicesData, Pages);

+  if (Buffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_PAGES,

+      EfiBootServicesData,

+      Buffer,

+      EFI_PAGES_TO_SIZE (Pages),

+      NULL

+      );

+  }

+  return Buffer;

 }

 

 /**

@@ -96,7 +112,20 @@
   IN UINTN  Pages

   )

 {

-  return InternalAllocatePages (EfiRuntimeServicesData, Pages);

+  VOID  *Buffer;

+

+  Buffer = InternalAllocatePages (EfiRuntimeServicesData, Pages);

+  if (Buffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_PAGES,

+      EfiRuntimeServicesData,

+      Buffer,

+      EFI_PAGES_TO_SIZE (Pages),

+      NULL

+      );

+  }

+  return Buffer;

 }

 

 /**

@@ -118,7 +147,20 @@
   IN UINTN  Pages

   )

 {

-  return InternalAllocatePages (EfiReservedMemoryType, Pages);

+  VOID  *Buffer;

+

+  Buffer = InternalAllocatePages (EfiReservedMemoryType, Pages);

+  if (Buffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_PAGES,

+      EfiReservedMemoryType,

+      Buffer,

+      EFI_PAGES_TO_SIZE (Pages),

+      NULL

+      );

+  }

+  return Buffer;

 }

 

 /**

@@ -263,7 +305,20 @@
   IN UINTN  Alignment

   )

 {

-  return InternalAllocateAlignedPages (EfiBootServicesData, Pages, Alignment);

+  VOID  *Buffer;

+

+  Buffer = InternalAllocateAlignedPages (EfiBootServicesData, Pages, Alignment);

+  if (Buffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_PAGES,

+      EfiBootServicesData,

+      Buffer,

+      EFI_PAGES_TO_SIZE (Pages),

+      NULL

+      );

+  }

+  return Buffer;

 }

 

 /**

@@ -291,7 +346,20 @@
   IN UINTN  Alignment

   )

 {

-  return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);

+  VOID  *Buffer;

+

+  Buffer = InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);

+  if (Buffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RUNTIME_PAGES,

+      EfiRuntimeServicesData,

+      Buffer,

+      EFI_PAGES_TO_SIZE (Pages),

+      NULL

+      );

+  }

+  return Buffer;

 }

 

 /**

@@ -319,7 +387,20 @@
   IN UINTN  Alignment

   )

 {

-  return InternalAllocateAlignedPages (EfiReservedMemoryType, Pages, Alignment);

+  VOID  *Buffer;

+

+  Buffer = InternalAllocateAlignedPages (EfiReservedMemoryType, Pages, Alignment);

+  if (Buffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RESERVED_PAGES,

+      EfiReservedMemoryType,

+      Buffer,

+      EFI_PAGES_TO_SIZE (Pages),

+      NULL

+      );

+  }

+  return Buffer;

 }

 

 /**

@@ -402,7 +483,20 @@
   IN UINTN  AllocationSize

   )

 {

-  return InternalAllocatePool (EfiBootServicesData, AllocationSize);

+  VOID  *Buffer;

+

+  Buffer = InternalAllocatePool (EfiBootServicesData, AllocationSize);

+  if (Buffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_POOL,

+      EfiBootServicesData,

+      Buffer,

+      AllocationSize,

+      NULL

+      );

+  }

+  return Buffer;

 }

 

 /**

@@ -423,7 +517,20 @@
   IN UINTN  AllocationSize

   )

 {

-  return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);

+  VOID  *Buffer;

+

+  Buffer = InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);

+  if (Buffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_POOL,

+      EfiRuntimeServicesData,

+      Buffer,

+      AllocationSize,

+      NULL

+      );

+  }

+  return Buffer;

 }

 

 /**

@@ -444,7 +551,20 @@
   IN UINTN  AllocationSize

   )

 {

-  return InternalAllocatePool (EfiReservedMemoryType, AllocationSize);

+  VOID  *Buffer;

+

+  Buffer = InternalAllocatePool (EfiReservedMemoryType, AllocationSize);

+  if (Buffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_POOL,

+      EfiReservedMemoryType,

+      Buffer,

+      AllocationSize,

+      NULL

+      );

+  }

+  return Buffer;

 }

 

 /**

@@ -495,7 +615,20 @@
   IN UINTN  AllocationSize

   )

 {

-  return InternalAllocateZeroPool (EfiBootServicesData, AllocationSize);

+  VOID  *Buffer;

+

+  Buffer = InternalAllocateZeroPool (EfiBootServicesData, AllocationSize);

+  if (Buffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ZERO_POOL,

+      EfiBootServicesData,

+      Buffer,

+      AllocationSize,

+      NULL

+      );

+  }

+  return Buffer;

 }

 

 /**

@@ -517,7 +650,20 @@
   IN UINTN  AllocationSize

   )

 {

-  return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);

+  VOID  *Buffer;

+

+  Buffer = InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);

+  if (Buffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_ZERO_POOL,

+      EfiRuntimeServicesData,

+      Buffer,

+      AllocationSize,

+      NULL

+      );

+  }

+  return Buffer;

 }

 

 /**

@@ -539,7 +685,20 @@
   IN UINTN  AllocationSize

   )

 {

-  return InternalAllocateZeroPool (EfiReservedMemoryType, AllocationSize);

+  VOID  *Buffer;

+

+  Buffer = InternalAllocateZeroPool (EfiReservedMemoryType, AllocationSize);

+  if (Buffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_ZERO_POOL,

+      EfiReservedMemoryType,

+      Buffer,

+      AllocationSize,

+      NULL

+      );

+  }

+  return Buffer;

 }

 

 /**

@@ -602,7 +761,20 @@
   IN CONST VOID  *Buffer

   )

 {

-  return InternalAllocateCopyPool (EfiBootServicesData, AllocationSize, Buffer);

+  VOID  *NewBuffer;

+

+  NewBuffer = InternalAllocateCopyPool (EfiBootServicesData, AllocationSize, Buffer);

+  if (NewBuffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_COPY_POOL,

+      EfiBootServicesData,

+      NewBuffer,

+      AllocationSize,

+      NULL

+      );

+  }

+  return NewBuffer;

 }

 

 /**

@@ -629,7 +801,20 @@
   IN CONST VOID  *Buffer

   )

 {

-  return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);

+  VOID  *NewBuffer;

+

+  NewBuffer = InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);

+  if (NewBuffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_COPY_POOL,

+      EfiRuntimeServicesData,

+      NewBuffer,

+      AllocationSize,

+      NULL

+      );

+  }

+  return NewBuffer;

 }

 

 /**

@@ -656,7 +841,20 @@
   IN CONST VOID  *Buffer

   )

 {

-  return InternalAllocateCopyPool (EfiReservedMemoryType, AllocationSize, Buffer);

+  VOID  *NewBuffer;

+

+  NewBuffer = InternalAllocateCopyPool (EfiReservedMemoryType, AllocationSize, Buffer);

+  if (NewBuffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_COPY_POOL,

+      EfiRuntimeServicesData,

+      NewBuffer,

+      AllocationSize,

+      NULL

+      );

+  }

+  return NewBuffer;

 }

 

 /**

@@ -728,7 +926,20 @@
   IN VOID   *OldBuffer  OPTIONAL

   )

 {

-  return InternalReallocatePool (EfiBootServicesData, OldSize, NewSize, OldBuffer);

+  VOID  *Buffer;

+

+  Buffer = InternalReallocatePool (EfiBootServicesData, OldSize, NewSize, OldBuffer);

+  if (Buffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_REALLOCATE_POOL,

+      EfiBootServicesData,

+      Buffer,

+      NewSize,

+      NULL

+      );

+  }

+  return Buffer;

 }

 

 /**

@@ -760,7 +971,20 @@
   IN VOID   *OldBuffer  OPTIONAL

   )

 {

-  return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);

+  VOID  *Buffer;

+

+  Buffer = InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);

+  if (Buffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RUNTIME_POOL,

+      EfiRuntimeServicesData,

+      Buffer,

+      NewSize,

+      NULL

+      );

+  }

+  return Buffer;

 }

 

 /**

@@ -792,7 +1016,20 @@
   IN VOID   *OldBuffer  OPTIONAL

   )

 {

-  return InternalReallocatePool (EfiReservedMemoryType, OldSize, NewSize, OldBuffer);

+  VOID  *Buffer;

+

+  Buffer = InternalReallocatePool (EfiReservedMemoryType, OldSize, NewSize, OldBuffer);

+  if (Buffer != NULL) {

+    MemoryProfileLibRecord (

+      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),

+      MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RESERVED_POOL,

+      EfiReservedMemoryType,

+      Buffer,

+      NewSize,

+      NULL

+      );

+  }

+  return Buffer;

 }

 

 /**

diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc
index ae38a2d..972f96e 100644
--- a/MdeModulePkg/MdeModulePkg.dsc
+++ b/MdeModulePkg/MdeModulePkg.dsc
@@ -261,6 +261,7 @@
   MdeModulePkg/Library/DxeCapsuleLibNull/DxeCapsuleLibNull.inf

   MdeModulePkg/Library/UefiMemoryAllocationProfileLib/UefiMemoryAllocationProfileLib.inf

   MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationLib.inf

+  MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationProfileLib.inf

   MdeModulePkg/Library/DxeCorePerformanceLib/DxeCorePerformanceLib.inf

   MdeModulePkg/Library/DxeCrc32GuidedSectionExtractLib/DxeCrc32GuidedSectionExtractLib.inf

   MdeModulePkg/Library/DxeDpcLib/DxeDpcLib.inf