TianoCore EDK2 master
Loading...
Searching...
No Matches
MmUnblockMemoryLib.c
Go to the documentation of this file.
1
13#include <Uefi.h>
15#include <Ppi/MmCommunication.h>
16#include <Library/HobLib.h>
17#include <Library/DebugLib.h>
20#include <Base.h>
21#include <PiPei.h>
22
37 IN VOID *HobStart,
38 IN EFI_PHYSICAL_ADDRESS UnblockBase,
39 IN EFI_PHYSICAL_ADDRESS UnblockEnd
40 )
41{
44 EFI_STATUS Status;
45 EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHob;
46
47 while ((MemoryAllocationHob = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, HobStart)) != NULL) {
48 HobBase = MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress;
49 HobEnd = MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress +
50 MemoryAllocationHob->AllocDescriptor.MemoryLength;
51 if ((UnblockBase < HobEnd) && (UnblockEnd > HobBase)) {
52 //
53 // The overlapped memory allocation HOB type must be one of the three specific types.
54 //
55 if ((MemoryAllocationHob->AllocDescriptor.MemoryType != EfiRuntimeServicesData) &&
56 (MemoryAllocationHob->AllocDescriptor.MemoryType != EfiACPIMemoryNVS) &&
57 (MemoryAllocationHob->AllocDescriptor.MemoryType != EfiReservedMemoryType))
58 {
59 DEBUG ((DEBUG_ERROR, "Error: range [0x%lx, 0x%lx] to unblock contains invalid type memory\n", UnblockBase, UnblockEnd));
61 }
62
63 if (UnblockBase < HobBase) {
64 //
65 // Recursively call to check [UnblockBase, HobBase]
66 //
68 GET_NEXT_HOB (MemoryAllocationHob),
69 UnblockBase,
70 HobBase
71 );
72 if (EFI_ERROR (Status)) {
73 return Status;
74 }
75 }
76
77 if (UnblockEnd > HobEnd) {
78 //
79 // Recursively call to check [HobEnd, UnblockEnd]
80 //
82 GET_NEXT_HOB (MemoryAllocationHob),
83 HobEnd,
84 UnblockEnd
85 );
86 if (EFI_ERROR (Status)) {
87 return Status;
88 }
89 }
90
91 return EFI_SUCCESS;
92 }
93
94 HobStart = GET_NEXT_HOB (MemoryAllocationHob);
95 }
96
97 DEBUG ((DEBUG_ERROR, "Error: range [0x%lx, 0x%lx] to unblock doesn't belong to any memory allocation HOB\n", UnblockBase, UnblockEnd));
99}
100
122EFIAPI
124 IN EFI_PHYSICAL_ADDRESS UnblockAddress,
125 IN UINT64 NumberOfPages
126 )
127{
128 EFI_STATUS Status;
129 MM_UNBLOCK_REGION *MmUnblockMemoryHob;
130 EFI_PEI_MM_COMMUNICATION_PPI *MmCommunicationPpi;
131 VOID *HobList;
132
133 if (!IS_ALIGNED (UnblockAddress, SIZE_4KB)) {
134 DEBUG ((DEBUG_ERROR, "Error: UnblockAddress is not 4KB aligned: %p\n", UnblockAddress));
135 return EFI_INVALID_PARAMETER;
136 }
137
138 HobList = GetHobList ();
139 Status = MmUnblockMemoryLibIsUnblockableRegion (HobList, UnblockAddress, UnblockAddress + EFI_PAGES_TO_SIZE (NumberOfPages));
140 if (EFI_ERROR (Status)) {
141 return Status;
142 }
143
144 //
145 // Unblock requests are rejected when MmIpl finishes execution.
146 //
147 Status = PeiServicesLocatePpi (&gEfiPeiMmCommunicationPpiGuid, 0, NULL, (VOID **)&MmCommunicationPpi);
148 if (!EFI_ERROR (Status)) {
149 DEBUG ((DEBUG_ERROR, "Unblock requests are rejected since the MmIpl finishes execution\n"));
151 }
152
153 //
154 // Build the GUID'd HOB for MmCore
155 //
156 MmUnblockMemoryHob = BuildGuidHob (&gMmUnblockRegionHobGuid, sizeof (MM_UNBLOCK_REGION));
157 if (MmUnblockMemoryHob == NULL) {
158 DEBUG ((DEBUG_ERROR, "MmUnblockMemoryRequest: Failed to allocate hob for unblocked data parameter!!\n"));
159 return Status;
160 }
161
162 ZeroMem (MmUnblockMemoryHob, sizeof (MM_UNBLOCK_REGION));
163
164 //
165 // Caller ID is filled in.
166 //
167 CopyGuid (&MmUnblockMemoryHob->IdentifierGuid, &gEfiCallerIdGuid);
168 MmUnblockMemoryHob->PhysicalStart = UnblockAddress;
169 MmUnblockMemoryHob->NumberOfPages = NumberOfPages;
170 return EFI_SUCCESS;
171}
VOID *EFIAPI BuildGuidHob(IN CONST EFI_GUID *Guid, IN UINTN DataLength)
Definition: HobLib.c:336
VOID *EFIAPI GetNextHob(IN UINT16 Type, IN CONST VOID *HobStart)
Definition: HobLib.c:103
VOID *EFIAPI GetHobList(VOID)
Definition: HobLib.c:76
GUID *EFIAPI CopyGuid(OUT GUID *DestinationGuid, IN CONST GUID *SourceGuid)
Definition: MemLibGuid.c:39
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
EFI_STATUS EFIAPI PeiServicesLocatePpi(IN CONST EFI_GUID *Guid, IN UINTN Instance, IN OUT EFI_PEI_PPI_DESCRIPTOR **PpiDescriptor, IN OUT VOID **Ppi)
#define NULL
Definition: Base.h:319
#define RETURN_ACCESS_DENIED
Definition: Base.h:1147
#define IS_ALIGNED(Value, Alignment)
Definition: Base.h:912
#define IN
Definition: Base.h:279
#define RETURN_INVALID_PARAMETER
Definition: Base.h:1076
#define DEBUG(Expression)
Definition: DebugLib.h:434
EFI_STATUS EFIAPI MmUnblockMemoryRequest(IN EFI_PHYSICAL_ADDRESS UnblockAddress, IN UINT64 NumberOfPages)
EFI_STATUS MmUnblockMemoryLibIsUnblockableRegion(IN VOID *HobStart, IN EFI_PHYSICAL_ADDRESS UnblockBase, IN EFI_PHYSICAL_ADDRESS UnblockEnd)
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
#define EFI_PAGES_TO_SIZE(Pages)
Definition: UefiBaseType.h:213
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
@ EfiReservedMemoryType
@ EfiACPIMemoryNVS
@ EfiRuntimeServicesData
EFI_PHYSICAL_ADDRESS MemoryBaseAddress
Definition: PiHob.h:119
EFI_MEMORY_TYPE MemoryType
Definition: PiHob.h:131
EFI_HOB_MEMORY_ALLOCATION_HEADER AllocDescriptor
Definition: PiHob.h:153
EFI_PHYSICAL_ADDRESS PhysicalStart