RDFU  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
MemoryMap.c File Reference

Functions related to memory map operations. More...

Include dependency graph for MemoryMap.c:

Go to the source code of this file.

Macros

#define NUM_OF_MAP_ENTRIES   ((MemoryMapSize / DescriptorSize)+1)
 

Functions

VOID GetMemAttributesStringAscii (CHAR8 *MemAttributesBuff, UINTN MemAttributesBuffSize, UINT64 Attributes)
 
EFI_STATUS GetMemoryEntryGcdInformation (EFI_PHYSICAL_ADDRESS BaseAddress, memory_map_entry *MemEntry)
 
memory_map_entry_list * GetMemoryMapList (VOID)
 
VOID FreeMemMap (memory_map_entry_list *MemMap)
 
EFI_STATUS DumpGcdMemoryMap (VOID)
 
memory_map_entry * FindMapEntryByAddr (memory_map_entry_list *MemMap, EFI_PHYSICAL_ADDRESS Address)
 
EFI_STATUS DumpMemoryMap (memory_map_entry_list *MemMap, BOOLEAN DumpToDisk, UINT32 SkipType)
 

Variables

CHAR8 * memory_types []
 

Detailed Description

Functions related to memory map operations.

Author
Piotr Bania
Date
Feb 2013 Functions related to memory map operations

Definition in file MemoryMap.c.

Macro Definition Documentation

#define NUM_OF_MAP_ENTRIES   ((MemoryMapSize / DescriptorSize)+1)

Referenced by GetMemoryMapList().

Function Documentation

EFI_STATUS DumpGcdMemoryMap ( VOID  )

Function displays the GCD memory map obtained through GetMemorySpaceMap. This function is only available in DXE drivers due to DxeServicesTableLib requirements.

Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 294 of file MemoryMap.c.

References EFI_STATUS(), and LogFullAscii().

Referenced by DumpMemoryMap().

295 {
296 
297 #if USE_DXE_SERVICES == 1
298  EFI_STATUS Status;
299  EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemSpaceMap;
300  UINTN i, NumOfDescriptors;
301 
302 
303  Status = gDS->GetMemorySpaceMap(&NumOfDescriptors, &MemSpaceMap);
304 
305  if (EFI_ERROR(Status))
306  {
307 #if DEBUG_ON == 1
308  LogFullAscii("%a: GetMemorySpaceMap returned %08x\r\n",
309  __FUNCTION__, Status);
310 #endif
311 
312  return Status;
313  }
314 
315 
316  for (i = 0; i < NumOfDescriptors; i++)
317  {
318  LogFullAscii("%08d: BaseAddr=%016lx Length=%016lx Attributes=%016lx\r\n",
319  i, MemSpaceMap[i].BaseAddress, MemSpaceMap[i].Length, MemSpaceMap[i].Attributes);
320  }
321 #endif
322 
323  return EFI_SUCCESS;
324 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS DumpMemoryMap ( memory_map_entry_list *  MemMap,
BOOLEAN  DumpToDisk,
UINT32  SkipType 
)

Function displays and dump the memory map.

Parameters
MemMapPointer to memory map obtained from GetMemoryMapList
DumpToDiskInidicates whether the data will be dumped to disk
SkipTypeTypes of memory that will be not included in the disk dump
Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 381 of file MemoryMap.c.

References _memory_map_entry::Attributes, DumpGcdMemoryMap(), DumpMemoryToFile(), GetMemAttributesStringAscii(), LogFullAscii(), _memory_map_entry_list::MapKey, memory_types, _memory_map_entry_list::MemoryMapEntry, _memory_map_entry::NumberOfPages, _memory_map_entry_list::NumOfEntries, _memory_map_entry::PhysicalStart, _memory_map_entry::Type, and _memory_map_entry::VirtualStart.

Referenced by CommandMemInfo(), and DrawBoxAndMenu().

384 {
385  UINTN i;
386  CHAR8 Attributes[256];
387  CHAR16 DmpFilePath[512];
388 
389  memory_map_entry *MapEntry;
390 
391  if (!MemMap)
392  return EFI_NOT_FOUND;
393 
394  LogFullAscii("--- BEGINING MEMORY MAP DUMP ---\r\n");
395  LogFullAscii("MapKey: %08x NumOfEntries=%08x\r\n", MemMap->MapKey, MemMap->NumOfEntries);
396 
397  for (i = 0; i < MemMap->NumOfEntries; i++)
398  {
399  MapEntry = &MemMap->MemoryMapEntry[i];
400 
401  GetMemAttributesStringAscii(Attributes, sizeof(Attributes), MapEntry->Attributes);
402 
403  LogFullAscii("%08d: PhysStart=%016lx PhysEnd=%016lx NumOfPages=%08llx VirtualStart=%016lx Attributes=%016lx (%a) Type=%08x (%a)\r\n",
404  i,
405  MapEntry->PhysicalStart,
406  MapEntry->PhysicalStart + EFI_PAGES_TO_SIZE(MapEntry->NumberOfPages),
407  MapEntry->NumberOfPages,
408  MapEntry->VirtualStart,
409  MapEntry->Attributes,
410  Attributes,
411  MapEntry->Type,
412  memory_types[MapEntry->Type]);
413 
414 
415 #if USE_DXE_SERVICES == 1
416  LogFullAscii("%08d: GcdLength=%08llx GcdMemoryType=%08x GcdBaseAddr=%016lx ImageHandle=%08x\r\n",
417  i,
418  MapEntry->Length,
419  MapEntry->GcdMemoryType,
420  MapEntry->BaseAddress,
421  MapEntry->ImageHandle);
422 #endif
423 
424  if (DumpToDisk)
425  {
426 
427  //"mdump\\%08x.dmp"
428  UnicodeSPrint(DmpFilePath, sizeof(DmpFilePath)-1, L"mdump\\%p.dmp",
429  MapEntry->PhysicalStart);
430 
431  LogFullAscii("%a: dumping to: %s \r\n", __FUNCTION__, DmpFilePath);
432 
433  // remember we always skip ReservedType (value = 0) if SkipType!=0
434  if (SkipType && ((MapEntry->Type == SkipType) || (MapEntry->Type == 0)))
435  {
436  LogFullAscii("%a: not dumping this entry to disk (type skipped)\r\n",
437  __FUNCTION__);
438  continue;
439  }
440 
441 
442  // dump it to file
444  (UINTN)EFI_PAGES_TO_SIZE(MapEntry->NumberOfPages),
445  DmpFilePath,
446  FALSE);
447 
448  } // if dump to disk
449  } // for map entry
450 
451 
452 #if USE_DXE_SERVICES == 1
454 #endif
455 
456  return EFI_SUCCESS;
457 }

Here is the call graph for this function:

Here is the caller graph for this function:

memory_map_entry* FindMapEntryByAddr ( memory_map_entry_list *  MemMap,
EFI_PHYSICAL_ADDRESS  Address 
)

Function finds memory map entry in the memory map list by address. Function checks if the selected address points somewhere inside the map memory entry range.

Parameters
MemMapPointer to the memory map
AddressMemory address
Return values
PointerOn success function returns pointer to the located image_entry
NULLOn error

Definition at line 343 of file MemoryMap.c.

References _memory_map_entry_list::MemoryMapEntry, _memory_map_entry::NumberOfPages, _memory_map_entry_list::NumOfEntries, and _memory_map_entry::PhysicalStart.

Referenced by CommandMemInfo().

345 {
346  UINTN i;
347  memory_map_entry *MapEntry;
348  EFI_PHYSICAL_ADDRESS PhysEnd;
349 
350 
351  if (!MemMap)
352  return 0;
353 
354  for (i = 0; i < MemMap->NumOfEntries; i++)
355  {
356  MapEntry = &MemMap->MemoryMapEntry[i];
357  PhysEnd = MapEntry->PhysicalStart + EFI_PAGES_TO_SIZE(MapEntry->NumberOfPages);
358 
359  if ((Address >= MapEntry->PhysicalStart) && (Address < PhysEnd))
360  return MapEntry;
361 
362  }
363 
364  return 0;
365 }

Here is the caller graph for this function:

VOID FreeMemMap ( memory_map_entry_list *  MemMap)

Function displays the GCD memory map obtained through GetMemorySpaceMap. This function is only available in DXE drivers due to DxeServicesTableLib requirements.

Parameters
MemMapPointer to memory map obtained from GetMemoryMapList

Definition at line 270 of file MemoryMap.c.

References MemFree(), and _memory_map_entry_list::MemoryMapEntry.

Referenced by CommandMemInfo(), and DrawBoxAndMenu().

271 {
272  MemFree(MemMap->MemoryMapEntry);
273  MemFree(MemMap);
274 }

Here is the call graph for this function:

Here is the caller graph for this function:

VOID GetMemAttributesStringAscii ( CHAR8 *  MemAttributesBuff,
UINTN  MemAttributesBuffSize,
UINT64  Attributes 
)

Function converts memory Attributes to text representation.

Parameters
MemAttributesBuffPointer to output buffer
MemAttributesBuffSizeSize of the output buffer
AttributesValue of memory attributes
Return values
NONE

Definition at line 42 of file MemoryMap.c.

References isMEM_ATTR_MEMORY_RP(), isMEM_ATTR_MEMORY_RUNTIME(), isMEM_ATTR_MEMORY_UC(), isMEM_ATTR_MEMORY_UCE(), isMEM_ATTR_MEMORY_WB(), isMEM_ATTR_MEMORY_WC(), isMEM_ATTR_MEMORY_WP(), isMEM_ATTR_MEMORY_WT(), and isMEM_ATTR_MEMORY_XP().

Referenced by CommandMemInfo(), and DumpMemoryMap().

45 {
46  gBS->SetMem(MemAttributesBuff, MemAttributesBuffSize, 0);
47 
48  if (isMEM_ATTR_MEMORY_UC(Attributes))
49  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_UC |", MemAttributesBuff);
50  if (isMEM_ATTR_MEMORY_WC(Attributes))
51  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_WC |", MemAttributesBuff);
52  if (isMEM_ATTR_MEMORY_WT(Attributes))
53  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_WT |", MemAttributesBuff);
54  if (isMEM_ATTR_MEMORY_WB(Attributes))
55  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_WB |", MemAttributesBuff);
56  if (isMEM_ATTR_MEMORY_UCE(Attributes))
57  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_UCE |", MemAttributesBuff);
58  if (isMEM_ATTR_MEMORY_WP(Attributes))
59  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_WP |", MemAttributesBuff);
60  if (isMEM_ATTR_MEMORY_RP(Attributes))
61  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_RP |", MemAttributesBuff);
62  if (isMEM_ATTR_MEMORY_XP(Attributes))
63  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_XP |", MemAttributesBuff);
64  if (isMEM_ATTR_MEMORY_RUNTIME(Attributes))
65  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_RUNTIME |", MemAttributesBuff);
66 
67 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS GetMemoryEntryGcdInformation ( EFI_PHYSICAL_ADDRESS  BaseAddress,
memory_map_entry *  MemEntry 
)

Definition at line 71 of file MemoryMap.c.

References EFI_STATUS(), and LogFullAscii().

Referenced by GetMemoryMapList().

73 {
74 
75 #if USE_DXE_SERVICES == 1
76  EFI_STATUS Status;
77  EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
78 
79  Status = gDS->GetMemorySpaceDescriptor(BaseAddress, &GcdDescriptor);
80 
81  if (EFI_ERROR(Status))
82  {
83 #if DEBUG_ON == 1
84  LogFullAscii("%a: error GetMemorySpaceDescriptor returned %08x\r\n",
85  __FUNCTION__, Status);
86 #endif
87 
88  return Status;
89  }
90 
91  MemEntry->BaseAddress = GcdDescriptor.BaseAddress;
92  MemEntry->Capabilities = GcdDescriptor.Capabilities;
93  MemEntry->GcdMemoryType = GcdDescriptor.GcdMemoryType;
94  MemEntry->Length = GcdDescriptor.Length;
95  MemEntry->ImageHandle = GcdDescriptor.ImageHandle;
96  MemEntry->DeviceHandle = GcdDescriptor.DeviceHandle;
97 #endif
98 
99  return EFI_SUCCESS;
100 }

Here is the call graph for this function:

Here is the caller graph for this function:

memory_map_entry_list* GetMemoryMapList ( VOID  )

Function generates current memory map. Caller is responsible for deallocating the memory.

Return values
PointerOn success pointer to memory_map_entry_list
NULLOn error

Definition at line 114 of file MemoryMap.c.

References _memory_map_entry::Attributes, EFI_STATUS(), GetMemoryEntryGcdInformation(), LogFullAscii(), _memory_map_entry_list::MapKey, MemAllocate(), MemFree(), _memory_map_entry_list::MemoryMapEntry, NUM_OF_MAP_ENTRIES, _memory_map_entry::NumberOfPages, _memory_map_entry_list::NumOfEntries, PAGE_SIZE, _memory_map_entry::PhysicalStart, _memory_map_entry::Type, and _memory_map_entry::VirtualStart.

Referenced by CommandMemInfo(), and DrawBoxAndMenu().

115 {
116  EFI_STATUS Status;
117  UINTN MemoryMapSize;
118  EFI_MEMORY_DESCRIPTOR *MemoryMap;
119  UINTN MapKey;
120  UINTN DescriptorSize;
121  UINT32 DescriptorVersion;
122  UINTN Pages;
123  UINTN i;
124 
125  EFI_MEMORY_DESCRIPTOR *EfiMemoryMapEnd;
126  EFI_MEMORY_DESCRIPTOR *EfiEntry;
127  EFI_MEMORY_DESCRIPTOR *NextEfiEntry;
128 
129  memory_map_entry_list *MemMap;
130  memory_map_entry *MemMapEntry;
131 
132 
133  MemoryMap = NULL;
134  MemoryMapSize = 0;
135  Pages = 0;
136 
137 
138  Status = gBS->GetMemoryMap(&MemoryMapSize,
139  MemoryMap,
140  &MapKey,
141  &DescriptorSize,
142  &DescriptorVersion);
143 
144  if (Status == EFI_BUFFER_TOO_SMALL)
145  {
146 
147 #if DEBUG_ON == 1
148  LogFullAscii("%a: Buffer to small, need = 0x%08x(%d) bytes\r\n",
149  __FUNCTION__, MemoryMapSize, MemoryMapSize);
150 #endif
151 
152  MemoryMapSize += PAGE_SIZE;
153  Pages = EFI_SIZE_TO_PAGES(MemoryMapSize);
154 
155  // do not use AllocatePages here, mac apple firmware is buggy
156  // and it will fault when GetMemoryMap will be executed
157  // with memory from AllocatePages as a parameter
158 
159  //MemoryMap = AllocatePages (Pages);
160  MemoryMap = MemAllocate(MemoryMapSize);
161 
162 
163  // get memory map again
164  Status = gBS->GetMemoryMap(
165  &MemoryMapSize,
166  MemoryMap,
167  &MapKey,
168  &DescriptorSize,
169  &DescriptorVersion
170  );
171 
172  if (EFI_ERROR(Status))
173  {
174 #if DEBUG_ON == 1
175  LogFullAscii("%a: GetMemoryMap returned unknown error (2nd time), status = %08d\r\n",
176  __FUNCTION__, Status);
177 #endif
178  return 0;
179  }
180  }
181  else
182  {
183 #if DEBUG_ON == 1
184  LogFullAscii("%a: GetMemoryMap returned unknown error, status = %08x \n",
185  __FUNCTION__, Status);
186 #endif
187 
188  return 0;
189  }
190 
191 
192 #if DEBUG_ON == 1
193  LogFullAscii("%a: Memory map recevied, processing!\r\n", __FUNCTION__);
194 #endif
195 
196 
197  // initialize variables
198  EfiEntry = MemoryMap;
199  NextEfiEntry = NEXT_MEMORY_DESCRIPTOR(EfiEntry, DescriptorSize);
200  EfiMemoryMapEnd = (EFI_MEMORY_DESCRIPTOR*)((UINT8*)MemoryMap + MemoryMapSize);
201 
202 #define NUM_OF_MAP_ENTRIES ((MemoryMapSize / DescriptorSize)+1)
203 
204 
205  // allocate the memory for our structures
207  if (!MemMap)
208  {
209 #if DEBUG_ON == 1
210  LogFullAscii("%a: unable to allocate memory for MemMap\r\n",
211  __FUNCTION__);
212 #endif
213 
214  MemFree(MemoryMap);
215  return 0;
216  }
217 
219 
220  if (!MemMapEntry)
221  {
222 #if DEBUG_ON == 1
223  LogFullAscii("%a: unable to allocate memory for MemMapEntry\r\n",
224  __FUNCTION__);
225 #endif
226 
227  MemFree(MemMap);
228  MemFree(MemoryMap);
229  return 0;
230  }
231 
232 
233 
234  for (i = 0; i < (NUM_OF_MAP_ENTRIES - 1); i++)
235  {
236 
237  MemMapEntry[i].Type = EfiEntry->Type;
238  MemMapEntry[i].PhysicalStart = EfiEntry->PhysicalStart;
239  MemMapEntry[i].VirtualStart = EfiEntry->VirtualStart;
240  MemMapEntry[i].NumberOfPages = EfiEntry->NumberOfPages;
241  MemMapEntry[i].Attributes = EfiEntry->Attribute;
242  GetMemoryEntryGcdInformation(EfiEntry->PhysicalStart, &MemMapEntry[i]);
243 
244  EfiEntry = NEXT_MEMORY_DESCRIPTOR(EfiEntry, DescriptorSize);
245  }
246 
247 
248  MemMap->NumOfEntries = NUM_OF_MAP_ENTRIES - 1;
249  MemMap->MemoryMapEntry = MemMapEntry;
250  MemMap->MapKey = MapKey;
251 
252 
253 #undef NUM_OF_MAP_ENTRIES
254 
255  MemFree(MemoryMap);
256  return MemMap;
257 }

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

CHAR8* memory_types[]
Initial value:
= {
"Reserved",
"LoaderCode",
"LoaderData",
"BootServicesCode (BS_code)",
"BootServicesData (BS_data)",
"RuntimeServicesCode (RT_code)",
"RuntimeServicesData (RT_Data)",
"ConventionalMemory (available)",
"UnusableMemory",
"ACPIReclaimMemory (ACPI_recl)",
"ACPIMemoryNVS (ACPI_NVS)",
"MemoryMappedIO",
"MemoryMappedIOPortSpace",
"PalCode"}

Definition at line 14 of file MemoryMap.c.

Referenced by DumpMemoryMap().