ReactOS 0.4.15-dev-7924-g5949c20
descriptor.c File Reference
#include "bl.h"
Include dependency graph for descriptor.c:

Go to the source code of this file.

Functions

LONG MmMdpLookupTypePrecedenceIndex (_In_ BL_MEMORY_TYPE Type)
 
BOOLEAN MmMdpHasPrecedence (_In_ BL_MEMORY_TYPE Type1, _In_ BL_MEMORY_TYPE Type2)
 
VOID MmMdpSwitchToDynamicDescriptors (_In_ ULONG Count)
 
NTSTATUS MmMdFreeDescriptor (_In_ PBL_MEMORY_DESCRIPTOR MemoryDescriptor)
 
VOID MmMdpSaveCurrentListPointer (_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ PLIST_ENTRY Current)
 
ULONG MmMdCountList (_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList)
 
VOID MmMdInitializeList (_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ ULONG Type, _In_ PLIST_ENTRY ListHead)
 
NTSTATUS MmMdCopyList (_In_ PBL_MEMORY_DESCRIPTOR_LIST DestinationList, _In_ PBL_MEMORY_DESCRIPTOR_LIST SourceList, _In_opt_ PBL_MEMORY_DESCRIPTOR ListDescriptor, _Out_ PULONG ActualCount, _In_ ULONG Count, _In_ ULONG Flags)
 
VOID MmMdRemoveDescriptorFromList (_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ PBL_MEMORY_DESCRIPTOR Entry)
 
VOID MmMdFreeList (_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList)
 
PBL_MEMORY_DESCRIPTOR MmMdInitByteGranularDescriptor (_In_ ULONG Flags, _In_ BL_MEMORY_TYPE Type, _In_ ULONGLONG BasePage, _In_ ULONGLONG VirtualPage, _In_ ULONGLONG PageCount)
 
NTSTATUS MmMdTruncateDescriptors (_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ PBL_MEMORY_DESCRIPTOR_LIST NewList, _In_ ULONGLONG BasePage)
 
BOOLEAN MmMdpTruncateDescriptor (_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ PBL_MEMORY_DESCRIPTOR MemoryDescriptor, _In_ ULONG Flags)
 
BOOLEAN MmMdpCoalesceDescriptor (_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ PBL_MEMORY_DESCRIPTOR MemoryDescriptor, _In_ ULONG Flags)
 
NTSTATUS MmMdAddDescriptorToList (_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ PBL_MEMORY_DESCRIPTOR MemoryDescriptor, _In_ ULONG Flags)
 
NTSTATUS MmMdRemoveRegionFromMdlEx (_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ ULONG Flags, _In_ ULONGLONG BasePage, _In_ ULONGLONG PageCount, _Out_opt_ PBL_MEMORY_DESCRIPTOR_LIST NewMdList)
 
PBL_MEMORY_DESCRIPTOR MmMdFindDescriptorFromMdl (_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ ULONG Flags, _In_ ULONGLONG Page)
 
PBL_MEMORY_DESCRIPTOR MmMdFindDescriptor (_In_ ULONG WhichList, _In_ ULONG Flags, _In_ ULONGLONG Page)
 
BOOLEAN MmMdFindSatisfyingRegion (_In_ PBL_MEMORY_DESCRIPTOR Descriptor, _Out_ PBL_MEMORY_DESCRIPTOR NewDescriptor, _In_ ULONGLONG Pages, _In_ PBL_ADDRESS_RANGE BaseRange, _In_ PBL_ADDRESS_RANGE VirtualRange, _In_ BOOLEAN TopDown, _In_ BL_MEMORY_TYPE MemoryType, _In_ ULONG Flags, _In_ ULONG Alignment)
 
VOID MmMdFreeGlobalDescriptors (VOID)
 
VOID MmMdInitialize (_In_ ULONG Phase, _In_ PBL_LIBRARY_PARAMETERS LibraryParameters)
 

Variables

BL_MEMORY_DESCRIPTOR MmStaticMemoryDescriptors [512]
 
ULONG MmGlobalMemoryDescriptorCount
 
PBL_MEMORY_DESCRIPTOR MmGlobalMemoryDescriptors
 
ULONG MmGlobalMemoryDescriptorsUsed
 
PBL_MEMORY_DESCRIPTOR MmDynamicMemoryDescriptors
 
ULONG MmDynamicMemoryDescriptorCount
 
BL_MEMORY_TYPE MmPlatformMemoryTypePrecedence []
 

Function Documentation

◆ MmMdAddDescriptorToList()

NTSTATUS MmMdAddDescriptorToList ( _In_ PBL_MEMORY_DESCRIPTOR_LIST  MdList,
_In_ PBL_MEMORY_DESCRIPTOR  MemoryDescriptor,
_In_ ULONG  Flags 
)

Definition at line 582 of file descriptor.c.

587{
588 PLIST_ENTRY ThisEntry, FirstEntry;
589 PBL_MEMORY_DESCRIPTOR ThisDescriptor;
590
591 /* Arguments must be present */
592 if (!(MdList) || !(MemoryDescriptor))
593 {
595 }
596
597 /* Check if coalescing is forcefully disabled */
599 {
600 /* Then we won't be coalescing */
601 Flags &= ~BL_MM_ADD_DESCRIPTOR_COALESCE_FLAG;
602 }
603 else if (MemoryDescriptor->Flags & BlMemoryCoalesced)
604 {
605 /* Coalesce if the descriptor requires it */
607 }
608
609 /* Check if truncation is forcefully disabled */
611 {
612 Flags &= ~BL_MM_ADD_DESCRIPTOR_TRUNCATE_FLAG;
613 }
614
615 /* Update the current list pointer if the descriptor requires it */
616 if (MemoryDescriptor->Flags & BlMemoryUpdate)
617 {
619 }
620
621 /* Get the current descriptor */
622 ThisEntry = MdList->This;
623 ThisDescriptor = CONTAINING_RECORD(ThisEntry, BL_MEMORY_DESCRIPTOR, ListEntry);
624
625 /* Also get the first descriptor */
626 FirstEntry = MdList->First;
627
628 /* Check if there's no current pointer, or if it's higher than the new one */
629 if (!(ThisEntry) ||
630 (MemoryDescriptor->BasePage <= ThisDescriptor->BasePage))
631 {
632 /* Start at the first descriptor instead, since current is past us */
633 ThisEntry = FirstEntry->Flink;
634 }
635
636 /* Loop until we find the right location to insert */
637 while (ThisEntry != FirstEntry)
638 {
639 /* Get the descriptor part of this entry */
640 ThisDescriptor = CONTAINING_RECORD(ThisEntry, BL_MEMORY_DESCRIPTOR, ListEntry);
641
642 /* Is the address smaller, or equal but more important? */
643 if ((MemoryDescriptor->BasePage < ThisDescriptor->BasePage) ||
644 ((MemoryDescriptor->BasePage == ThisDescriptor->BasePage) &&
645 (MmMdpHasPrecedence(MemoryDescriptor->Type, ThisDescriptor->Type))))
646 {
647 /* Then insert right here */
648 InsertTailList(ThisEntry, &MemoryDescriptor->ListEntry);
649 goto Quickie;
650 }
651
652 /* Try the next entry */
653 ThisEntry = ThisEntry->Flink;
654 }
655
656 /* Then we didn't find a good match, so insert it right here */
657 InsertTailList(FirstEntry, &MemoryDescriptor->ListEntry);
658
659Quickie:
660 /* Do we have to truncate? */
662 {
663 /* Do it and then exit */
665 {
666 return STATUS_SUCCESS;
667 }
668 }
669
670 /* Do we have to coalesce? */
672 {
673 /* Do it and then exit */
675 {
676 return STATUS_SUCCESS;
677 }
678 }
679
680 /* Do we have to update the current pointer? */
682 {
683 /* Do it */
685 }
686
687 /* We're done */
688 return STATUS_SUCCESS;
689}
#define BL_MM_ADD_DESCRIPTOR_NEVER_COALESCE_FLAG
Definition: bl.h:91
#define BL_MM_ADD_DESCRIPTOR_COALESCE_FLAG
Definition: bl.h:89
#define BL_MM_ADD_DESCRIPTOR_TRUNCATE_FLAG
Definition: bl.h:90
#define BL_MM_ADD_DESCRIPTOR_NEVER_TRUNCATE_FLAG
Definition: bl.h:92
@ BlMemoryUpdate
Definition: bl.h:378
@ BlMemoryCoalesced
Definition: bl.h:377
#define BL_MM_ADD_DESCRIPTOR_UPDATE_LIST_POINTER_FLAG
Definition: bl.h:94
BOOLEAN MmMdpTruncateDescriptor(_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ PBL_MEMORY_DESCRIPTOR MemoryDescriptor, _In_ ULONG Flags)
Definition: descriptor.c:486
BOOLEAN MmMdpCoalesceDescriptor(_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ PBL_MEMORY_DESCRIPTOR MemoryDescriptor, _In_ ULONG Flags)
Definition: descriptor.c:528
BOOLEAN MmMdpHasPrecedence(_In_ BL_MEMORY_TYPE Type1, _In_ BL_MEMORY_TYPE Type2)
Definition: descriptor.c:64
VOID MmMdpSaveCurrentListPointer(_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ PLIST_ENTRY Current)
Definition: descriptor.c:185
#define InsertTailList(ListHead, Entry)
#define STATUS_SUCCESS
Definition: shellext.h:65
ULONGLONG BasePage
Definition: bl.h:830
BL_MEMORY_TYPE Type
Definition: bl.h:841
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
_Must_inspect_result_ _In_ WDFUSBDEVICE _In_opt_ WDFREQUEST _In_opt_ PWDF_REQUEST_SEND_OPTIONS _In_ PWDF_USB_CONTROL_SETUP_PACKET _In_opt_ PWDF_MEMORY_DESCRIPTOR MemoryDescriptor
Definition: wdfusb.h:1339
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170

Referenced by BlMmMapPhysicalAddressEx(), MmFwGetMemoryMap(), MmMdCopyList(), MmMdRemoveRegionFromMdlEx(), MmMdTruncateDescriptors(), MmPaInitialize(), MmPapAllocateRegionFromMdl(), MmPapFreePhysicalPages(), MmPapPageAllocatorExtend(), MmPaReleaseSelfMapPages(), and MmTrInitialize().

◆ MmMdCopyList()

NTSTATUS MmMdCopyList ( _In_ PBL_MEMORY_DESCRIPTOR_LIST  DestinationList,
_In_ PBL_MEMORY_DESCRIPTOR_LIST  SourceList,
_In_opt_ PBL_MEMORY_DESCRIPTOR  ListDescriptor,
_Out_ PULONG  ActualCount,
_In_ ULONG  Count,
_In_ ULONG  Flags 
)

Definition at line 249 of file descriptor.c.

257{
259 PULONG Used;
260 PLIST_ENTRY First, NextEntry;
262
263 /* Both parameters must be present */
264 if (!(DestinationList) || !(SourceList))
265 {
267 }
268
269 /* Assume success */
271
272 /* Check if a descriptor is being used to store the list */
273 if (ListDescriptor)
274 {
275 /* See how big it is */
277 Used = ActualCount;
278 }
279 else
280 {
281 /* We are using our internal descriptors instead */
284
285 /* Use as many as are available */
287 ListDescriptor = MmGlobalMemoryDescriptors;
288 }
289
290 /* Never truncate descriptors during a list copy */
292
293 /* Iterate through the list */
294 First = SourceList->First;
295 NextEntry = First->Flink;
296 while ((NextEntry != First) && (NT_SUCCESS(Status)))
297 {
298 /* Make sure there's still space */
299 if (Count <= *Used)
300 {
302 break;
303 }
304
305 /* Get the current descriptor */
306 Descriptor = CONTAINING_RECORD(NextEntry,
308 ListEntry);
309
310 /* Copy it into one of the descriptors we have */
311 RtlCopyMemory(&ListDescriptor[*Used],
313 sizeof(*Descriptor));
314
315 /* Add it to the list we have */
316 Status = MmMdAddDescriptorToList(DestinationList,
317 &ListDescriptor[*Used],
318 Flags);
319 ++*Used;
320
321 /* Move to the next entry */
322 NextEntry = NextEntry->Flink;
323 }
324
325 /* Check if the global descriptors were used */
326 if (ListDescriptor == MmGlobalMemoryDescriptors)
327 {
328 /* Unwind our usage */
331 }
332
333 /* Return back to caller */
334 return Status;
335}
WCHAR First[]
Definition: FormatMessage.c:11
LONG NTSTATUS
Definition: precomp.h:26
ULONG MmDescriptorCallTreeCount
Definition: mm.c:19
NTSTATUS MmMdAddDescriptorToList(_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ PBL_MEMORY_DESCRIPTOR MemoryDescriptor, _In_ ULONG Flags)
Definition: descriptor.c:582
ULONG MmGlobalMemoryDescriptorsUsed
Definition: descriptor.c:18
ULONG MmGlobalMemoryDescriptorCount
Definition: descriptor.c:16
PBL_MEMORY_DESCRIPTOR MmGlobalMemoryDescriptors
Definition: descriptor.c:17
VOID MmMdFreeGlobalDescriptors(VOID)
Definition: descriptor.c:1332
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
Status
Definition: gdiplustypes.h:25
int Count
Definition: noreturn.cpp:7
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
uint32_t * PULONG
Definition: typedefs.h:59
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
_Must_inspect_result_ _In_ WDFIORESLIST _In_ PIO_RESOURCE_DESCRIPTOR Descriptor
Definition: wdfresource.h:342
_In_ IN_ADDR _In_ IN_ADDR _Out_ MULTICAST_MODE_TYPE _Inout_ ULONG _Out_writes_ SourceCount IN_ADDR * SourceList
Definition: ws2tcpip.h:622

Referenced by BlMmGetMemoryMap().

◆ MmMdCountList()

ULONG MmMdCountList ( _In_ PBL_MEMORY_DESCRIPTOR_LIST  MdList)

Definition at line 205 of file descriptor.c.

208{
209 PLIST_ENTRY First, NextEntry;
210 ULONG Count;
211
212 /* Iterate the list */
213 for (Count = 0, First = MdList->First, NextEntry = First->Flink;
214 NextEntry != First;
215 NextEntry = NextEntry->Flink, Count++);
216
217 /* Return the count */
218 return Count;
219}
uint32_t ULONG
Definition: typedefs.h:59

Referenced by BlMmGetMemoryMap().

◆ MmMdFindDescriptor()

PBL_MEMORY_DESCRIPTOR MmMdFindDescriptor ( _In_ ULONG  WhichList,
_In_ ULONG  Flags,
_In_ ULONGLONG  Page 
)

Definition at line 1049 of file descriptor.c.

1054{
1055 PBL_MEMORY_DESCRIPTOR FoundDescriptor;
1056
1057 /* Check if the caller is looking for mapped, allocated memory */
1058 if (WhichList & BL_MM_INCLUDE_MAPPED_ALLOCATED)
1059 {
1060 /* Find a descriptor in that list */
1062 if (FoundDescriptor)
1063 {
1064 /* Got it */
1065 return FoundDescriptor;
1066 }
1067 }
1068
1069 /* Check if the caller is looking for mapped, unallocated memory */
1070 if (WhichList & BL_MM_INCLUDE_MAPPED_UNALLOCATED)
1071 {
1072 /* Find a descriptor in that list */
1074 if (FoundDescriptor)
1075 {
1076 /* Got it */
1077 return FoundDescriptor;
1078 }
1079 }
1080
1081 /* Check if the caller is looking for unmapped, allocated memory */
1082 if (WhichList & BL_MM_INCLUDE_UNMAPPED_ALLOCATED)
1083 {
1084 /* Find a descriptor in that list */
1086 if (FoundDescriptor)
1087 {
1088 /* Got it */
1089 return FoundDescriptor;
1090 }
1091 }
1092
1093 /* Check if the caller is looking for unmapped, unallocated memory */
1094 if (WhichList & BL_MM_INCLUDE_UNMAPPED_UNALLOCATED)
1095 {
1096 /* Find a descriptor in that list */
1098 if (FoundDescriptor)
1099 {
1100 /* Got it */
1101 return FoundDescriptor;
1102 }
1103 }
1104
1105 /* Check if the caller is looking for reserved, allocated memory */
1106 if (WhichList & BL_MM_INCLUDE_RESERVED_ALLOCATED)
1107 {
1108 /* Find a descriptor in that list */
1110 if (FoundDescriptor)
1111 {
1112 /* Got it */
1113 return FoundDescriptor;
1114 }
1115 }
1116
1117 /* Check if the caller is looking for bad memory */
1118 if (WhichList & BL_MM_INCLUDE_BAD_MEMORY)
1119 {
1120 /* Find a descriptor in that list */
1121 FoundDescriptor = MmMdFindDescriptorFromMdl(&MmMdlBadMemory, Flags, Page);
1122 if (FoundDescriptor)
1123 {
1124 /* Got it */
1125 return FoundDescriptor;
1126 }
1127 }
1128
1129 /* Check if the caller is looking for truncated memory */
1130 if (WhichList & BL_MM_INCLUDE_TRUNCATED_MEMORY)
1131 {
1132 /* Find a descriptor in that list */
1134 if (FoundDescriptor)
1135 {
1136 /* Got it */
1137 return FoundDescriptor;
1138 }
1139 }
1140
1141 /* Check if the caller is looking for persistent memory */
1142 if (WhichList & BL_MM_INCLUDE_PERSISTENT_MEMORY)
1143 {
1144 /* Find a descriptor in that list */
1146 if (FoundDescriptor)
1147 {
1148 /* Got it */
1149 return FoundDescriptor;
1150 }
1151 }
1152
1153 /* Nothing if we got here */
1154 return NULL;
1155}
BL_MEMORY_DESCRIPTOR_LIST MmMdlMappedUnallocated
Definition: pagealloc.c:36
#define BL_MM_INCLUDE_TRUNCATED_MEMORY
Definition: bl.h:103
BL_MEMORY_DESCRIPTOR_LIST MmMdlReservedAllocated
Definition: pagealloc.c:40
#define BL_MM_INCLUDE_MAPPED_ALLOCATED
Definition: bl.h:96
BL_MEMORY_DESCRIPTOR_LIST MmMdlBadMemory
Definition: pagealloc.c:41
#define BL_MM_INCLUDE_UNMAPPED_UNALLOCATED
Definition: bl.h:99
BL_MEMORY_DESCRIPTOR_LIST MmMdlUnmappedAllocated
Definition: pagealloc.c:38
#define BL_MM_INCLUDE_MAPPED_UNALLOCATED
Definition: bl.h:97
#define BL_MM_INCLUDE_BAD_MEMORY
Definition: bl.h:101
BL_MEMORY_DESCRIPTOR_LIST MmMdlTruncatedMemory
Definition: pagealloc.c:42
BL_MEMORY_DESCRIPTOR_LIST MmMdlMappedAllocated
Definition: pagealloc.c:35
BL_MEMORY_DESCRIPTOR_LIST MmMdlUnmappedUnallocated
Definition: pagealloc.c:39
#define BL_MM_INCLUDE_PERSISTENT_MEMORY
Definition: bl.h:104
#define BL_MM_INCLUDE_UNMAPPED_ALLOCATED
Definition: bl.h:98
#define BL_MM_INCLUDE_RESERVED_ALLOCATED
Definition: bl.h:100
BL_MEMORY_DESCRIPTOR_LIST MmMdlPersistentMemory
Definition: pagealloc.c:43
PBL_MEMORY_DESCRIPTOR MmMdFindDescriptorFromMdl(_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ ULONG Flags, _In_ ULONGLONG Page)
Definition: descriptor.c:960
#define NULL
Definition: types.h:112
_In_ PVOID _Out_opt_ BOOLEAN _Out_opt_ PPFN_NUMBER Page
Definition: mm.h:1306

Referenced by BlMmMapPhysicalAddressEx(), MmArchTranslateVirtualAddress(), MmPapFreePhysicalPages(), and MmPaReleaseSelfMapPages().

◆ MmMdFindDescriptorFromMdl()

PBL_MEMORY_DESCRIPTOR MmMdFindDescriptorFromMdl ( _In_ PBL_MEMORY_DESCRIPTOR_LIST  MdList,
_In_ ULONG  Flags,
_In_ ULONGLONG  Page 
)

Definition at line 960 of file descriptor.c.

965{
966 BOOLEAN IsVirtual;
967 PLIST_ENTRY NextEntry, ListHead;
968 PBL_MEMORY_DESCRIPTOR Current;
969 ULONGLONG BasePage;
970
971 /* Assume physical */
972 IsVirtual = FALSE;
973
974 /* Check if the caller wants physical memory */
976 {
977 /* Check if this is a virtual memory list */
978 if (MdList->Type == BlMdVirtual)
979 {
980 /* We won't find anything */
981 return NULL;
982 }
983 }
984 else if (MdList->Type == BlMdPhysical)
985 {
986 /* Otherwise, caller wants virtual, but this is a physical list */
987 IsVirtual = TRUE;
988 NextEntry = MdList->First->Flink;
989 }
990
991 /* Check if this is a physical search */
992 if (!IsVirtual)
993 {
994 /* Check if we can use the current pointer */
995 NextEntry = MdList->This;
996 if (!NextEntry)
997 {
998 /* We can't -- start at the beginning */
999 NextEntry = MdList->First->Flink;
1000 }
1001 else
1002 {
1003 /* If the page is below the current pointer, restart */
1004 Current = CONTAINING_RECORD(NextEntry, BL_MEMORY_DESCRIPTOR, ListEntry);
1005 if (Page < Current->BasePage)
1006 {
1007 NextEntry = MdList->First->Flink;
1008 }
1009 }
1010 }
1011
1012 /* Loop the list of descriptors */
1013 ListHead = MdList->First;
1014 while (NextEntry != ListHead)
1015 {
1016 /* Get the current one */
1017 Current = CONTAINING_RECORD(NextEntry, BL_MEMORY_DESCRIPTOR, ListEntry);
1018
1019 /* Check if we are looking for virtual memory */
1020 if (IsVirtual)
1021 {
1022 /* Use the base address */
1023 BasePage = Current->VirtualPage;
1024 }
1025 else
1026 {
1027 /* Use the page */
1028 BasePage = Current->BasePage;
1029 }
1030
1031 /* If this is a virtual descriptor, make sure it has a base address */
1032 if ((!(IsVirtual) || (BasePage)) &&
1033 (BasePage <= Page) &&
1034 (Page < (BasePage + Current->PageCount)))
1035 {
1036 /* The descriptor fits the page being requested */
1037 return Current;
1038 }
1039
1040 /* Try the next one */
1041 NextEntry = NextEntry->Flink;
1042 }
1043
1044 /* Nothing found if we're here */
1045 return NULL;
1046}
unsigned char BOOLEAN
@ BlMdVirtual
Definition: bl.h:224
@ BlMdPhysical
Definition: bl.h:223
#define BL_MM_REMOVE_VIRTUAL_REGION_FLAG
Definition: bl.h:125
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
ULONGLONG VirtualPage
Definition: bl.h:831
ULONGLONG PageCount
Definition: bl.h:839
uint64_t ULONGLONG
Definition: typedefs.h:67

Referenced by MmFwGetMemoryMap(), and MmMdFindDescriptor().

◆ MmMdFindSatisfyingRegion()

BOOLEAN MmMdFindSatisfyingRegion ( _In_ PBL_MEMORY_DESCRIPTOR  Descriptor,
_Out_ PBL_MEMORY_DESCRIPTOR  NewDescriptor,
_In_ ULONGLONG  Pages,
_In_ PBL_ADDRESS_RANGE  BaseRange,
_In_ PBL_ADDRESS_RANGE  VirtualRange,
_In_ BOOLEAN  TopDown,
_In_ BL_MEMORY_TYPE  MemoryType,
_In_ ULONG  Flags,
_In_ ULONG  Alignment 
)

Definition at line 1158 of file descriptor.c.

1169{
1170 ULONGLONG BaseMin, BaseMax, AlignedMin;
1171 ULONGLONG VirtualPage, BasePage;
1172 ULONGLONG BaseDelta, AlignedBase;
1173 ULONGLONG VirtualMin, VirtualMax;
1174
1175 /* Extract the minimum and maximum range */
1176 BaseMin = BaseRange->Minimum;
1177 BaseMax = BaseRange->Maximum;
1178
1179 /* Don't go below where the descriptor starts */
1180 if (BaseMin < Descriptor->BasePage)
1181 {
1182 BaseMin = Descriptor->BasePage;
1183 }
1184
1185 /* Don't go beyond where the descriptor ends */
1186 if (BaseMax > (Descriptor->BasePage + Descriptor->PageCount - 1))
1187 {
1188 BaseMax = (Descriptor->BasePage + Descriptor->PageCount - 1);
1189 }
1190
1191 /* Check for start overflow */
1192 if (BaseMin > BaseMax)
1193 {
1194 return FALSE;
1195 }
1196
1197 /* Align the base as required */
1198 if (Alignment != 1)
1199 {
1200 AlignedMin = ALIGN_UP_BY(BaseMin, Alignment);
1201 }
1202 else
1203 {
1204 AlignedMin = BaseMin;
1205 }
1206
1207 /* Check for range overflow */
1208 if (((AlignedMin + Pages - 1) < AlignedMin) || ((AlignedMin + Pages - 1) > BaseMax))
1209 {
1210 return FALSE;
1211 }
1212
1213 /* Check if this was a top-down request */
1214 if (TopDown)
1215 {
1216 /* Then get the highest page possible */
1217 BasePage = BaseMax - Pages + 1;
1218 if (Alignment != 1)
1219 {
1220 /* Align it as needed */
1221 AlignedBase = ALIGN_DOWN_BY(BasePage, Alignment);
1222 }
1223 else
1224 {
1225 AlignedBase = BasePage;
1226 }
1227
1228 /* Calculate the delta between max address and our aligned base */
1229 BaseDelta = BasePage - AlignedBase;
1230 BasePage -= BaseDelta;
1231 }
1232 else
1233 {
1234 /* Otherwise, get the lowest page possible */
1235 BasePage = AlignedMin;
1236 BaseDelta = 0;
1237 }
1238
1239 /* If a virtual address range was passed in, this must be a virtual descriptor */
1240 if (((VirtualRange->Minimum) || (VirtualRange->Maximum)) &&
1241 !(Descriptor->VirtualPage))
1242 {
1243 return FALSE;
1244 }
1245
1246 /* Any mapped page already? */
1247 if (Descriptor->VirtualPage)
1248 {
1249 /* Get virtual min/max */
1250 VirtualMin = VirtualRange->Minimum;
1251 VirtualMax = VirtualRange->Maximum;
1252
1253 /* Don't go below where the descriptor maps */
1254 if (VirtualMin <= Descriptor->VirtualPage)
1255 {
1256 VirtualMin = Descriptor->VirtualPage;
1257 }
1258
1259 /* Don't go above where the descriptor maps */
1260 if (VirtualMax >= (Descriptor->VirtualPage + Descriptor->PageCount - 1))
1261 {
1262 VirtualMax = Descriptor->VirtualPage + Descriptor->PageCount - 1;
1263 }
1264
1265 /* Don't let the base overflow */
1266 if (VirtualMin > VirtualMax)
1267 {
1268 return FALSE;
1269 }
1270
1271 /* Adjust the base by the alignment delta */
1272 VirtualMin += AlignedMin - BaseMin;
1273
1274 /* Check that the bounds don't overflow or underflow */
1275 if (((VirtualMin + Pages - 1) < VirtualMin) ||
1276 ((VirtualMin + Pages - 1) > VirtualMax))
1277 {
1278 return FALSE;
1279 }
1280
1281 /* Finally, pick the correct address based on direction */
1282 if (TopDown)
1283 {
1284 /* Highest possible base address, aligned */
1285 VirtualPage = VirtualMax - Pages + 1 - BaseDelta;
1286 }
1287 else
1288 {
1289 /* Lowest possible base address, aligned */
1290 VirtualPage = VirtualMin;
1291 }
1292 }
1293 else
1294 {
1295 /* Nothing to worry about */
1296 VirtualPage = 0;
1297 }
1298
1299 /* Bail out if the memory type attributes don't match */
1300 if ((((Flags & 0xFF) & (Descriptor->Flags & 0xFF)) != (Flags & 0xFF)) ||
1301 (((Flags & 0xFF00) & (Descriptor->Flags & 0xFF00)) != (Flags & 0xFF00)))
1302 {
1303 //EfiPrintf(L"Incorrect memory attributes\r\n");
1304 return FALSE;
1305 }
1306
1307 /* Bail out if the allocation flags don't match */
1309 {
1310 //EfiPrintf(L"Incorrect memory allocation flags\r\n");
1311 return FALSE;
1312 }
1313
1314 /* Bail out if the type doesn't match */
1315 if (Descriptor->Type != MemoryType)
1316 {
1317 //EfiPrintf(L"Incorrect descriptor type: %lx %lx\r\n", Descriptor->Type, MemoryType);
1318 return FALSE;
1319 }
1320
1321 /* We have a matching region, fill out the descriptor for it */
1322 NewDescriptor->BasePage = BasePage;
1323 NewDescriptor->PageCount = Pages;
1324 NewDescriptor->Type = Descriptor->Type;
1325 NewDescriptor->VirtualPage = VirtualPage;
1326 NewDescriptor->Flags = Descriptor->Flags;
1327 //EfiPrintf(L"Found a matching descriptor: %08I64X with %08I64X pages\r\n", BasePage, Pages);
1328 return TRUE;
1329}
#define ALIGN_DOWN_BY(size, align)
#define ALIGN_UP_BY(size, align)
@ BlMemoryLargePages
Definition: bl.h:366
@ BlMemoryRuntime
Definition: bl.h:376
@ BlMemoryBelow1MB
Definition: bl.h:369
_In_opt_ PSECURITY_DESCRIPTOR _Out_ PSECURITY_DESCRIPTOR * NewDescriptor
Definition: sefuncs.h:30

Referenced by MmPapAllocateRegionFromMdl().

◆ MmMdFreeDescriptor()

NTSTATUS MmMdFreeDescriptor ( _In_ PBL_MEMORY_DESCRIPTOR  MemoryDescriptor)

Definition at line 157 of file descriptor.c.

160{
162
163 /* Check if this is a valid static descriptor */
169 {
170 /* It's a global/static descriptor, so just zero it */
173 }
174 else
175 {
176 /* It's a dynamic descriptor, so free it */
178 }
179
180 /* Done */
181 return Status;
182}
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
NTSTATUS BlMmFreeHeap(_In_ PVOID Buffer)
Definition: heapalloc.c:663
ULONG MmDynamicMemoryDescriptorCount
Definition: descriptor.c:20
PBL_MEMORY_DESCRIPTOR MmDynamicMemoryDescriptors
Definition: descriptor.c:19
BL_MEMORY_DESCRIPTOR MmStaticMemoryDescriptors[512]
Definition: descriptor.c:15
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262

Referenced by MmFwGetMemoryMap(), MmMdFreeList(), MmMdRemoveRegionFromMdlEx(), MmPapAllocateRegionFromMdl(), and Mmx86pMapMemoryRegions().

◆ MmMdFreeGlobalDescriptors()

VOID MmMdFreeGlobalDescriptors ( VOID  )

Definition at line 1332 of file descriptor.c.

1335{
1336 PBL_MEMORY_DESCRIPTOR Descriptor, OldDescriptor;
1337 ULONG Index = 0;
1338 PLIST_ENTRY OldFlink, OldBlink;
1339
1340 /* Make sure we're not int middle of a call using a descriptor */
1342 {
1343 return;
1344 }
1345
1346 /* Loop every current global descriptor */
1348 {
1349 /* Does it have any valid pages? */
1350 OldDescriptor = &MmGlobalMemoryDescriptors[Index];
1351 if (OldDescriptor->PageCount)
1352 {
1353 /* Allocate a copy of it */
1355 if (!Descriptor)
1356 {
1357 return;
1358 }
1359
1360 /* Save the links */
1361 OldBlink = OldDescriptor->ListEntry.Blink;
1362 OldFlink = OldDescriptor->ListEntry.Flink;
1363
1364 /* Make the copy */
1365 *Descriptor = *OldDescriptor;
1366
1367 /* Fix the links */
1368 OldBlink->Flink = &Descriptor->ListEntry;
1369 OldFlink->Blink = &Descriptor->ListEntry;
1370
1371 /* Zero the descriptor */
1372 RtlZeroMemory(OldDescriptor, sizeof(*OldDescriptor));
1373 }
1374
1375 /* Keep going */
1376 Index++;
1377 }
1378
1379 /* All global descriptors freed */
1381}
PVOID BlMmAllocateHeap(_In_ SIZE_T Size)
Definition: heapalloc.c:569
LIST_ENTRY ListEntry
Definition: bl.h:825
struct _LIST_ENTRY * Blink
Definition: typedefs.h:122
_In_ WDFCOLLECTION _In_ ULONG Index

Referenced by BlMmMapPhysicalAddressEx(), BlMmUnmapVirtualAddressEx(), BlpMmInitialize(), MmMdCopyList(), MmPapAllocatePagesInRange(), MmPapAllocatePhysicalPagesInRange(), MmPaReserveSelfMapPages(), MmPaTruncateMemory(), and TrpGenerateMappingTracker().

◆ MmMdFreeList()

VOID MmMdFreeList ( _In_ PBL_MEMORY_DESCRIPTOR_LIST  MdList)

Definition at line 356 of file descriptor.c.

359{
360 PLIST_ENTRY FirstEntry, NextEntry;
362
363 /* Go over every descriptor from the top */
364 FirstEntry = MdList->First;
365 NextEntry = FirstEntry->Flink;
366 while (NextEntry != FirstEntry)
367 {
368 /* Remove and free each one */
369 Entry = CONTAINING_RECORD(NextEntry, BL_MEMORY_DESCRIPTOR, ListEntry);
370 NextEntry = NextEntry->Flink;
373 }
374}
NTSTATUS MmMdFreeDescriptor(_In_ PBL_MEMORY_DESCRIPTOR MemoryDescriptor)
Definition: descriptor.c:157
VOID MmMdRemoveDescriptorFromList(_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ PBL_MEMORY_DESCRIPTOR Entry)
Definition: descriptor.c:338
base of all file and directory entries
Definition: entries.h:83

Referenced by BlMmGetMemoryMap(), MmFwGetMemoryMap(), and MmMdRemoveRegionFromMdlEx().

◆ MmMdInitByteGranularDescriptor()

PBL_MEMORY_DESCRIPTOR MmMdInitByteGranularDescriptor ( _In_ ULONG  Flags,
_In_ BL_MEMORY_TYPE  Type,
_In_ ULONGLONG  BasePage,
_In_ ULONGLONG  VirtualPage,
_In_ ULONGLONG  PageCount 
)

Definition at line 377 of file descriptor.c.

384{
386
387 /* If we're out of descriptors, bail out */
389 {
390 EfiPrintf(L"Out of descriptors!\r\n");
391 EfiStall(1000000);
392 return NULL;
393 }
394
395 /* Take one of the available descriptors and fill it out */
397 MemoryDescriptor->BasePage = BasePage;
398 MemoryDescriptor->VirtualPage = VirtualPage;
399 MemoryDescriptor->PageCount = PageCount;
400 MemoryDescriptor->Flags = Flags;
401 MemoryDescriptor->Type = Type;
403
404 /* Increment the count and return the descriptor */
406 return MemoryDescriptor;
407}
Type
Definition: Type.h:7
VOID EfiPrintf(_In_ PWCHAR Format,...)
Definition: firmware.c:126
NTSTATUS EfiStall(_In_ ULONG StallTime)
Definition: firmware.c:1003
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
#define L(x)
Definition: ntvdm.h:50

Referenced by BlMmMapPhysicalAddressEx(), MmFwGetMemoryMap(), MmMdRemoveRegionFromMdlEx(), MmMdTruncateDescriptors(), MmPaInitialize(), MmPapAllocateRegionFromMdl(), MmPapFreePhysicalPages(), MmPapPageAllocatorExtend(), MmPaReleaseSelfMapPages(), MmTrInitialize(), and TrpGenerateMappingTracker().

◆ MmMdInitialize()

VOID MmMdInitialize ( _In_ ULONG  Phase,
_In_ PBL_LIBRARY_PARAMETERS  LibraryParameters 
)

Definition at line 1384 of file descriptor.c.

1388{
1389 /* Are we in phase 1? */
1390 if (Phase != 0)
1391 {
1392 /* Switch to dynamic descriptors if we have too many */
1393 if (LibraryParameters->DescriptorCount > RTL_NUMBER_OF(MmStaticMemoryDescriptors))
1394 {
1395 MmMdpSwitchToDynamicDescriptors(LibraryParameters->DescriptorCount);
1396 }
1397 }
1398 else
1399 {
1400 /* In phase 0, start with a pool of 512 static descriptors */
1405 }
1406}
VOID MmMdpSwitchToDynamicDescriptors(_In_ ULONG Count)
Definition: descriptor.c:148

Referenced by BlpMmInitialize().

◆ MmMdInitializeList()

VOID MmMdInitializeList ( _In_ PBL_MEMORY_DESCRIPTOR_LIST  MdList,
_In_ ULONG  Type,
_In_ PLIST_ENTRY  ListHead 
)

Definition at line 222 of file descriptor.c.

227{
228 /* Check if a list was specified */
229 if (ListHead)
230 {
231 /* Use it */
232 MdList->First = ListHead;
233 }
234 else
235 {
236 /* Otherwise, use the internal, built-in list */
237 InitializeListHead(&MdList->ListHead);
238 MdList->First = &MdList->ListHead;
239 }
240
241 /* Set the type */
242 MdList->Type = Type;
243
244 /* Initialize current iterator to nothing */
245 MdList->This = NULL;
246}

Referenced by BlMmGetMemoryMap(), and MmMdRemoveRegionFromMdlEx().

◆ MmMdpCoalesceDescriptor()

BOOLEAN MmMdpCoalesceDescriptor ( _In_ PBL_MEMORY_DESCRIPTOR_LIST  MdList,
_In_ PBL_MEMORY_DESCRIPTOR  MemoryDescriptor,
_In_ ULONG  Flags 
)

Definition at line 528 of file descriptor.c.

533{
534 PBL_MEMORY_DESCRIPTOR NextDescriptor, PreviousDescriptor;
535 PLIST_ENTRY NextEntry, PreviousEntry;
536 ULONGLONG EndPage, PreviousEndPage, PreviousMappedEndPage, MappedEndPage;
537
538 /* Get the next descriptor */
539 NextEntry = MemoryDescriptor->ListEntry.Flink;
540 NextDescriptor = CONTAINING_RECORD(NextEntry, BL_MEMORY_DESCRIPTOR, ListEntry);
541
542 /* Get the previous descriptor */
543 PreviousEntry = MemoryDescriptor->ListEntry.Blink;
544 PreviousDescriptor = CONTAINING_RECORD(PreviousEntry, BL_MEMORY_DESCRIPTOR, ListEntry);
545
546 /* Calculate end pages */
547 EndPage = MemoryDescriptor->BasePage + MemoryDescriptor->PageCount;
548 MappedEndPage = MemoryDescriptor->BasePage + MemoryDescriptor->PageCount;
549 PreviousMappedEndPage = PreviousDescriptor->VirtualPage + PreviousDescriptor->PageCount;
550 PreviousEndPage = PreviousDescriptor->BasePage + PreviousDescriptor->PageCount;
551 PreviousMappedEndPage = PreviousDescriptor->VirtualPage + PreviousDescriptor->PageCount;
552
553 /* Check if the previous entry touches the current entry, and is compatible */
554 if ((PreviousEntry != MdList->First) &&
555 (PreviousDescriptor->Type == MemoryDescriptor->Type) &&
556 ((PreviousDescriptor->Flags ^ MemoryDescriptor->Flags) & 0x1B19FFFF) &&
557 (PreviousEndPage == MemoryDescriptor->BasePage) &&
558 ((!(MemoryDescriptor->VirtualPage) && !(PreviousDescriptor->VirtualPage)) ||
559 ((MemoryDescriptor->VirtualPage) && (PreviousDescriptor->VirtualPage) &&
560 (PreviousMappedEndPage == MemoryDescriptor->VirtualPage))))
561 {
562 EfiPrintf(L"Previous descriptor coalescable!\r\n");
563 }
564
565 /* CHeck if the current entry touches the next entry, and is compatible */
566 if ((NextEntry != MdList->First) &&
567 (NextDescriptor->Type == MemoryDescriptor->Type) &&
568 ((NextDescriptor->Flags ^ MemoryDescriptor->Flags) & 0x1B19FFFF) &&
569 (EndPage == NextDescriptor->BasePage) &&
570 ((!(MemoryDescriptor->VirtualPage) && !(NextDescriptor->VirtualPage)) ||
571 ((MemoryDescriptor->VirtualPage) && (NextDescriptor->VirtualPage) &&
572 (MappedEndPage == NextDescriptor->VirtualPage))))
573 {
574 EfiPrintf(L"Next descriptor coalescable!\r\n");
575 }
576
577 /* Nothing to do */
578 return FALSE;
579}
int WINAPI EndPage(_In_ HDC)

Referenced by MmMdAddDescriptorToList().

◆ MmMdpHasPrecedence()

BOOLEAN MmMdpHasPrecedence ( _In_ BL_MEMORY_TYPE  Type1,
_In_ BL_MEMORY_TYPE  Type2 
)

Definition at line 64 of file descriptor.c.

68{
69 BL_MEMORY_CLASS Class1, Class2;
70 ULONG i, j;
71
72 /* It isn't free RAM, but the comparator is -- it succeeds it */
73 if (Type2 == BlConventionalMemory)
74 {
75 return TRUE;
76 }
77
78 /* Descriptor is free RAM -- it precedes */
79 if (Type1 == BlConventionalMemory)
80 {
81 return FALSE;
82 }
83
84 /* Descriptor is not system, application, or loader class -- it precedes */
85 Class1 = Type1 >> BL_MEMORY_CLASS_SHIFT;
86 if ((Class1 != BlSystemClass) &&
87 (Class1 != BlApplicationClass) &&
88 (Class1 != BlLoaderClass))
89 {
90 return TRUE;
91 }
92
93 /* It isn't one of those classes, but the comparator it -- it succeeds it */
94 Class2 = Type2 >> BL_MEMORY_CLASS_SHIFT;
95 if ((Class2 != BlSystemClass) &&
96 (Class2 != BlApplicationClass) &&
97 (Class2 != BlLoaderClass))
98 {
99 return FALSE;
100 }
101
102 /* Descriptor is system class */
103 if (Class1 == BlSystemClass)
104 {
105 /* If the other guy isn't, system wins */
106 if (Class2 != BlSystemClass)
107 {
108 return TRUE;
109 }
110
111 /* Scan for the descriptor's system precedence index */
114
115 /* Does the current have a valid index? */
116 if (i == 0xFFFFFFFF)
117 {
118 return TRUE;
119 }
120
121 /* Yes, what about the comparator? */
122 if (j == 0xFFFFFFFF)
123 {
124 return FALSE;
125 }
126
127 /* Let the indexes fight! */
128 return i <= j;
129 }
130
131 /* Descriptor is not system class, but comparator is -- it succeeds it */
132 if (Class2 == BlSystemClass)
133 {
134 return FALSE;
135 }
136
137 /* Descriptor is loader class -- it preceedes */
138 if (Class1 == BlLoaderClass)
139 {
140 return TRUE;
141 }
142
143 /* It isn't loader class -- if the other guy is, succeed it */
144 return Class2 != BlLoaderClass;
145}
@ BlConventionalMemory
Definition: bl.h:327
@ BlSystemClass
Definition: bl.h:295
@ BlLoaderClass
Definition: bl.h:293
@ BlApplicationClass
Definition: bl.h:294
#define BL_MEMORY_CLASS_SHIFT
Definition: bl.h:145
enum _BL_MEMORY_CLASS BL_MEMORY_CLASS
LONG MmMdpLookupTypePrecedenceIndex(_In_ BL_MEMORY_TYPE Type)
Definition: descriptor.c:41
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250

Referenced by MmMdAddDescriptorToList().

◆ MmMdpLookupTypePrecedenceIndex()

LONG MmMdpLookupTypePrecedenceIndex ( _In_ BL_MEMORY_TYPE  Type)

Definition at line 41 of file descriptor.c.

44{
45 ULONG i;
46
47 /* Check the precedence array */
49 {
50 /* Check for a match */
52 {
53 /* Return the index */
54 return i;
55 }
56 }
57
58 /* Invalid index type */
59 return -1;
60}
BL_MEMORY_TYPE MmPlatformMemoryTypePrecedence[]
Definition: descriptor.c:22

Referenced by MmMdpHasPrecedence().

◆ MmMdpSaveCurrentListPointer()

VOID MmMdpSaveCurrentListPointer ( _In_ PBL_MEMORY_DESCRIPTOR_LIST  MdList,
_In_ PLIST_ENTRY  Current 
)

Definition at line 185 of file descriptor.c.

189{
190 PBL_MEMORY_DESCRIPTOR FirstEntry, LastEntry;
191
192 /* Make sure that this is not a global descriptor and not the first one */
193 FirstEntry = &MmGlobalMemoryDescriptors[0];
195 if ((((ULONG_PTR)Current < (ULONG_PTR)FirstEntry) ||
196 ((ULONG_PTR)Current >= (ULONG_PTR)LastEntry)) &&
197 (Current != MdList->First))
198 {
199 /* Save this as the current pointer */
200 MdList->This = Current;
201 }
202}
uint32_t ULONG_PTR
Definition: typedefs.h:65

Referenced by MmMdAddDescriptorToList(), and MmMdRemoveDescriptorFromList().

◆ MmMdpSwitchToDynamicDescriptors()

VOID MmMdpSwitchToDynamicDescriptors ( _In_ ULONG  Count)

Definition at line 148 of file descriptor.c.

151{
152 EfiPrintf(L"Dynamic switch NOT SUPPORTED!!!\r\n");
153 EfiStall(10000000);
154}

Referenced by MmMdInitialize().

◆ MmMdpTruncateDescriptor()

BOOLEAN MmMdpTruncateDescriptor ( _In_ PBL_MEMORY_DESCRIPTOR_LIST  MdList,
_In_ PBL_MEMORY_DESCRIPTOR  MemoryDescriptor,
_In_ ULONG  Flags 
)

Definition at line 486 of file descriptor.c.

491{
492 PBL_MEMORY_DESCRIPTOR NextDescriptor, PreviousDescriptor;
493 PLIST_ENTRY NextEntry, PreviousEntry;
494 ULONGLONG EndPage, PreviousEndPage;// , NextEndPage;
495
496 /* Get the next descriptor */
497 NextEntry = MemoryDescriptor->ListEntry.Flink;
498 NextDescriptor = CONTAINING_RECORD(NextEntry, BL_MEMORY_DESCRIPTOR, ListEntry);
499
500 /* Get the previous descriptor */
501 PreviousEntry = MemoryDescriptor->ListEntry.Blink;
502 PreviousDescriptor = CONTAINING_RECORD(PreviousEntry, BL_MEMORY_DESCRIPTOR, ListEntry);
503
504 /* Calculate end pages */
505 EndPage = MemoryDescriptor->BasePage + MemoryDescriptor->PageCount;
506 //NextEndPage = NextDescriptor->BasePage + NextDescriptor->PageCount;
507 PreviousEndPage = PreviousDescriptor->BasePage + PreviousDescriptor->PageCount;
508
509 /* Check for backward overlap */
510 if ((PreviousEntry != MdList->First) && (MemoryDescriptor->BasePage < PreviousEndPage))
511 {
512 EfiPrintf(L"Overlap detected -- this is unexpected on x86/x64 platforms\r\n");
513 EfiStall(1000000);
514 }
515
516 /* Check for forward overlap */
517 if ((NextEntry != MdList->First) && (NextDescriptor->BasePage < EndPage))
518 {
519 EfiPrintf(L"Overlap detected -- this is unexpected on x86/x64 platforms\r\n");
520 EfiStall(1000000);
521 }
522
523 /* Nothing to do */
524 return FALSE;
525}

Referenced by MmMdAddDescriptorToList().

◆ MmMdRemoveDescriptorFromList()

VOID MmMdRemoveDescriptorFromList ( _In_ PBL_MEMORY_DESCRIPTOR_LIST  MdList,
_In_ PBL_MEMORY_DESCRIPTOR  Entry 
)

Definition at line 338 of file descriptor.c.

342{
343 /* Remove the entry */
344 RemoveEntryList(&Entry->ListEntry);
345
346 /* Check if this was the current link */
347 if (MdList->This == &Entry->ListEntry)
348 {
349 /* Remove the current link and set the next one */
350 MdList->This = NULL;
351 MmMdpSaveCurrentListPointer(MdList, Entry->ListEntry.Blink);
352 }
353}
#define RemoveEntryList(Entry)
Definition: env_spec_w32.h:986

Referenced by BlMmMapPhysicalAddressEx(), MmMdFreeList(), MmMdRemoveRegionFromMdlEx(), MmMdTruncateDescriptors(), MmPapAllocateRegionFromMdl(), MmPapFreePhysicalPages(), and Mmx86pMapMemoryRegions().

◆ MmMdRemoveRegionFromMdlEx()

NTSTATUS MmMdRemoveRegionFromMdlEx ( _In_ PBL_MEMORY_DESCRIPTOR_LIST  MdList,
_In_ ULONG  Flags,
_In_ ULONGLONG  BasePage,
_In_ ULONGLONG  PageCount,
_Out_opt_ PBL_MEMORY_DESCRIPTOR_LIST  NewMdList 
)

Definition at line 692 of file descriptor.c.

699{
700 BOOLEAN HaveNewList, UseVirtualPage;
702 PLIST_ENTRY ListHead, NextEntry;
704 BL_MEMORY_DESCRIPTOR OldDescriptor;
706 ULONGLONG FoundBasePage, FoundEndPage, FoundPageCount, EndPage, VirtualPage;
707
708 /* Set initial status */
710 ListDescriptor = NULL;
712 HaveNewList = FALSE;
713
714 /* Check if removed descriptors should go into a new list */
715 if (NewMdList != NULL)
716 {
717 /* Initialize it */
718 MmMdInitializeList(NewMdList, MdList->Type, NULL);
719
720 /* Remember for later */
721 HaveNewList = TRUE;
722 }
723
724 /* Is the region being removed physical? */
725 UseVirtualPage = FALSE;
727 {
728 /* Is this a list of virtual descriptors? */
729 if (MdList->Type == BlMdVirtual)
730 {
731 /* Request is nonsensical, fail */
733 goto Quickie;
734 }
735 }
736 else
737 {
738 /* Is this a list of physical descriptors? */
739 if (MdList->Type == BlMdPhysical)
740 {
741 /* We'll have to use the virtual page instead */
742 UseVirtualPage = TRUE;
743 }
744 }
745
746 /* Loop the list*/
747 ListHead = MdList->First;
748 NextEntry = ListHead->Flink;
749 while (NextEntry != ListHead)
750 {
751 /* Get the descriptor */
752 Descriptor = CONTAINING_RECORD(NextEntry, BL_MEMORY_DESCRIPTOR, ListEntry);
753
754 /* Extract range details */
755 FoundBasePage = UseVirtualPage ? Descriptor->VirtualPage : Descriptor->BasePage;
756 FoundPageCount = Descriptor->PageCount;
757 FoundEndPage = FoundBasePage + FoundPageCount;
758 EndPage = PageCount + BasePage;
759
760 /* Make a copy of the original descriptor */
761 OldDescriptor = *Descriptor;
762
763 /* Check if the region to be removed starts after the found region starts */
764 if ((BasePage > FoundBasePage) || (FoundBasePage >= EndPage))
765 {
766 /* Check if the region ends after the found region */
767 if ((BasePage >= FoundEndPage) || (FoundEndPage > EndPage))
768 {
769 /* Check if the found region starts after the region or ends before the region */
770 if ((FoundBasePage >= BasePage) || (EndPage >= FoundEndPage))
771 {
772 /* This is a fully-mapped descriptor -- change nothing */
773 OldDescriptor.PageCount = 0;
774 }
775 else
776 {
777 /* This descriptor fully covers the entire allocation */
778 FoundBasePage = Descriptor->BasePage;
779 VirtualPage = Descriptor->VirtualPage;
780 FoundPageCount = BasePage - FoundBasePage;
781
782 /* This is how many pages we will eat away from the descriptor */
783 RegionSize = FoundPageCount + PageCount;
784
785 /* Update the descriptor to account for the consumed pages */
786 Descriptor->BasePage += RegionSize;
787 Descriptor->PageCount -= RegionSize;
788 if (VirtualPage)
789 {
790 Descriptor->VirtualPage += RegionSize;
791 }
792
793 /* Initialize a descriptor for the start of the region */
795 Descriptor->Type,
796 FoundBasePage,
797 VirtualPage,
798 FoundPageCount);
799 if (!NewDescriptor)
800 {
802 goto Quickie;
803 }
804
805 /* Add it into the list */
807 if (!NT_SUCCESS(Status))
808 {
810 goto Quickie;
811 }
812
813 /* Don't free it on exit path */
815
816 /* Adjust the leftover descriptor */
817 OldDescriptor.BasePage += FoundPageCount;
818 OldDescriptor.PageCount = PageCount;
819 if (OldDescriptor.VirtualPage)
820 {
821 OldDescriptor.VirtualPage += FoundPageCount;
822 }
823 }
824 }
825 else
826 {
827 /* This descriptor contains the entire allocation */
828 RegionSize = FoundEndPage - BasePage;
829 Descriptor->PageCount -= RegionSize;
830
831 /* Adjust the leftover descriptor */
832 OldDescriptor.BasePage += Descriptor->PageCount;
833 OldDescriptor.PageCount = RegionSize;
834 if (OldDescriptor.VirtualPage)
835 {
836 OldDescriptor.VirtualPage += FoundPageCount;
837 }
838 }
839
840 /* Go to the next entry */
841 NextEntry = NextEntry->Flink;
842 }
843 else
844 {
845 /*
846 * This descriptor contains the end of the allocation. It may:
847 *
848 * - Contain the full allocation (i.e.: the start is aligned)
849 * - Contain parts of the end of the allocation (i.e.: the end is beyond)
850 * - Contain the entire tail end of the allocation (i..e:the end is within)
851 *
852 * So first, figure out if we cover the entire end or not
853 */
854 if (EndPage < FoundEndPage)
855 {
856 /* The allocation goes past the end of this descriptor */
857 FoundEndPage = EndPage;
858 }
859
860 /* This is how many pages we will eat away from the descriptor */
861 FoundPageCount = FoundEndPage - FoundBasePage;
862
863 /* Update the descriptor to account for the consumed pages */
864 Descriptor->BasePage += FoundPageCount;
865 Descriptor->PageCount -= FoundPageCount;
866 if (Descriptor->VirtualPage)
867 {
868 Descriptor->VirtualPage += FoundPageCount;
869 }
870
871 /* Go to the next entry */
872 NextEntry = NextEntry->Flink;
873
874 /* Check if the descriptor is now empty */
875 if (!Descriptor->PageCount)
876 {
877 /* Remove it */
879
880 /* Check if we're supposed to insert it into a new list */
881 if (HaveNewList)
882 {
883 /* This is the one to add */
884 ListDescriptor = Descriptor;
885 }
886 else
887 {
888 /* Nope -- just get rid of it */
890 }
891 }
892 }
893
894 /* Is there a remainder descriptor, and do we have a list for it */
895 if ((OldDescriptor.PageCount) && (HaveNewList))
896 {
897 /* Did we already chop off the descriptor? */
898 if (ListDescriptor)
899 {
900 /* Use what we previously chopped */
901 *ListDescriptor = OldDescriptor;
902 }
903 else
904 {
905 /* First time, so build a descriptor to describe the leftover */
906 ListDescriptor = MmMdInitByteGranularDescriptor(OldDescriptor.Flags,
907 OldDescriptor.Type,
908 OldDescriptor.BasePage,
909 OldDescriptor.VirtualPage,
910 OldDescriptor.PageCount);
911 if (!ListDescriptor)
912 {
914 goto Quickie;
915 }
916
917 /* Add it into the list */
918 Status = MmMdAddDescriptorToList(NewMdList, ListDescriptor, 0);
919 if (!NT_SUCCESS(Status))
920 {
921 goto Quickie;
922 }
923
924 /* Don't free on exit path */
925 ListDescriptor = NULL;
926 }
927 }
928 }
929
930Quickie:
931 /* Check for failure cleanup */
932 if (!NT_SUCCESS(Status))
933 {
934 /* Did we have to build a new list? */
935 if (HaveNewList)
936 {
937 /* Free and re-initialize it */
938 MmMdFreeList(NewMdList);
939 MmMdInitializeList(NewMdList, MdList->Type, NULL);
940 }
941
942 /* Check if we had a list descriptor, and free it */
943 if (ListDescriptor)
944 {
945 MmMdFreeDescriptor(ListDescriptor);
946 }
947
948 /* Check if we had a new descriptor, and free it */
949 if (NewDescriptor)
950 {
952 }
953 }
954
955 /* All done */
956 return Status;
957}
VOID MmMdInitializeList(_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList, _In_ ULONG Type, _In_ PLIST_ENTRY ListHead)
Definition: descriptor.c:222
PBL_MEMORY_DESCRIPTOR MmMdInitByteGranularDescriptor(_In_ ULONG Flags, _In_ BL_MEMORY_TYPE Type, _In_ ULONGLONG BasePage, _In_ ULONGLONG VirtualPage, _In_ ULONGLONG PageCount)
Definition: descriptor.c:377
VOID MmMdFreeList(_In_ PBL_MEMORY_DESCRIPTOR_LIST MdList)
Definition: descriptor.c:356
__kernel_entry _Inout_ _Inout_ PSIZE_T RegionSize
Definition: mmfuncs.h:172

◆ MmMdTruncateDescriptors()

NTSTATUS MmMdTruncateDescriptors ( _In_ PBL_MEMORY_DESCRIPTOR_LIST  MdList,
_In_ PBL_MEMORY_DESCRIPTOR_LIST  NewList,
_In_ ULONGLONG  BasePage 
)

Definition at line 410 of file descriptor.c.

415{
416 PLIST_ENTRY ListHead, NextEntry;
418 ULONGLONG FoundEndPage;
419
420 /* Search the descriptor list */
421 ListHead = MdList->First;
422 NextEntry = ListHead->Flink;
423 while (NextEntry != ListHead)
424 {
425 /* Get the current descriptor */
426 Descriptor = CONTAINING_RECORD(NextEntry,
428 ListEntry);
429
430 /* Go to the next entry in case we have to remove */
431 NextEntry = NextEntry->Flink;
432
433 /* Don't touch anything else but free RAM */
434 if (((Descriptor->Type >> BL_MEMORY_CLASS_SHIFT) == BlSystemClass) &&
436 {
437 continue;
438 }
439
440 /* Check if this page is within the descriptor's region */
441 FoundEndPage = Descriptor->BasePage + Descriptor->PageCount;
442 if (BasePage > Descriptor->BasePage)
443 {
444 /* Check if it doesn't go beyond the descriptor */
445 if (BasePage < FoundEndPage)
446 {
447 /* Create a new descriptor to describe this region */
448 EfiPrintf(L"Truncating descriptor type %lx base: %llx end: %llx\r\n",
449 Descriptor->Type, Descriptor->BasePage, FoundEndPage);
451 Descriptor->Type,
452 BasePage,
453 0,
454 FoundEndPage - BasePage);
455 if (!NewDescriptor)
456 {
457 return STATUS_NO_MEMORY;
458 }
459
460 /* Cut off this descriptor to make it shorter */
461 Descriptor->PageCount = BasePage - Descriptor->BasePage;
462
463 /* Add the region to the new list */
467 }
468 }
469 else
470 {
471 /* This whole descriptor covers the truncated area */
472 EfiPrintf(L"Truncating descriptor type %lx base: %llx end: %llx\r\n",
473 Descriptor->Type, Descriptor->BasePage, FoundEndPage);
478 }
479 }
480
481 /* All good if we got here */
482 return STATUS_SUCCESS;
483}

Referenced by MmPaTruncateMemory().

Variable Documentation

◆ MmDynamicMemoryDescriptorCount

ULONG MmDynamicMemoryDescriptorCount

Definition at line 20 of file descriptor.c.

Referenced by MmMdFreeDescriptor().

◆ MmDynamicMemoryDescriptors

PBL_MEMORY_DESCRIPTOR MmDynamicMemoryDescriptors

Definition at line 19 of file descriptor.c.

Referenced by MmMdFreeDescriptor().

◆ MmGlobalMemoryDescriptorCount

ULONG MmGlobalMemoryDescriptorCount

◆ MmGlobalMemoryDescriptors

◆ MmGlobalMemoryDescriptorsUsed

ULONG MmGlobalMemoryDescriptorsUsed

◆ MmPlatformMemoryTypePrecedence

BL_MEMORY_TYPE MmPlatformMemoryTypePrecedence[]
Initial value:
=
{
}
@ BlReservedMemory
Definition: bl.h:329
@ BlUnusableMemory
Definition: bl.h:328
@ BlEfiRuntimeDataMemory
Definition: bl.h:338
@ BlDeviceIoMemory
Definition: bl.h:335
@ BlEfiBootMemory
Definition: bl.h:330
@ BlPalMemory
Definition: bl.h:337
@ BlEfiRuntimeCodeMemory
Definition: bl.h:332
@ BlConventionalZeroedMemory
Definition: bl.h:331
@ BlAcpiNvsMemory
Definition: bl.h:334
@ BlAcpiReclaimMemory
Definition: bl.h:333
@ BlDevicePortMemory
Definition: bl.h:336

Definition at line 22 of file descriptor.c.

Referenced by MmMdpLookupTypePrecedenceIndex().

◆ MmStaticMemoryDescriptors

BL_MEMORY_DESCRIPTOR MmStaticMemoryDescriptors[512]

Definition at line 15 of file descriptor.c.

Referenced by MmMdFreeDescriptor(), and MmMdInitialize().