MiReservePtes
_MMPTE *__stdcall MiReservePtes(_MI_SYSTEM_PTE_TYPE *SystemPteInfo, UINT64 NumberOfPtes){
unsigned int Flags;
unsigned int v3;
UINT64 v4;
UINT64 v5;
unsigned __int64 v6;
_MMPTE *v8;
union {unsigned __int64 Long;volatile unsigned __int64 VolatileLong;_MMPTE_HARDWARE Hard;_MMPTE_PROTOTYPE Proto;_MMPTE_SOFTWARE Soft;_MMPTE_TIMESTAMP TimeStamp;_MMPTE_TRANSITION Trans;_MMPTE_SUBSECTION Subsect;_MMPTE_LIST List;} v9;
int v11;
unsigned __int64 LowestBitEverAllocated;
_MI_SYSTEM_PTE_TYPE *v13;
unsigned __int64 Hint;
__int128 v15;
unsigned __int64 SizeOfBitMap;
unsigned __int64 v17;
unsigned __int64 *Buffer;
unsigned __int64 v19;
unsigned __int64 v20;
unsigned __int64 v21;
__int64 v22;
unsigned __int64 *v23;
unsigned __int64 v24;
unsigned __int64 v25;
unsigned __int64 v26;
__int64 v27;
__int64 v28;
_KTBFLUSH_TYPE v29;
unsigned __int64 v30;
int v31;
unsigned int v32;
unsigned __int64 *v33;
bool v34;
__int64 v35;
unsigned int v36;
unsigned __int64 v37;
unsigned __int64 v38;
__int64 v39;
__int64 v40;
int v41;
_MMPTE *v42;
unsigned __int64 v43;
unsigned __int64 *v44;
__int64 v45;
unsigned int v46;
unsigned __int64 *v47;
unsigned int v48;
__int64 v49;
__int64 v50;
bool v51;
__int64 v52;
unsigned int v53;
unsigned __int64 v54;
unsigned __int64 v55;
unsigned __int64 v56;
unsigned __int64 v57;
unsigned __int64 *v58;
unsigned __int64 v59;
_MI_SYSTEM_PTE_TYPE *BitMapHeader;
__int128 v61;
unsigned __int64 v62;
int v63;
Flags = SystemPteInfo->Flags;
v3 = 0;
v4 = (unsigned int)NumberOfPtes;
v5 = (unsigned int)NumberOfPtes;
v6 = (unsigned int)NumberOfPtes;
v61 = 0i64;
if( (Flags & 4) != 0 )
{
v6 = (unsigned __int64)(unsigned int)NumberOfPtes >> 4;
}
else if( SystemPteInfo == (_MI_SYSTEM_PTE_TYPE *)((char *)&stru_140C4DB30 + 4432)
&& (unsigned int)NumberOfPtes <= 0x40ui64 )
{
v8 = MiCheckProcessorPteCache((unsigned int)NumberOfPtes);
if( v8 )
goto LABEL_5;
}
if( (unsigned int)v4 < 0x200 )
goto LABEL_20;
v42 = MiExpandPtes(SystemPteInfo, v4);
v8 = v42;
if( v42 )
{
if( SystemPteInfo == (_MI_SYSTEM_PTE_TYPE *)((char *)&stru_140C4DB30 + 4432) && (dword_140CFA17C & 2) != 0 )
MiCheckPteReserve(v42, v4);
_InterlockedExchangeAdd64((volatile signed __int64 *)&SystemPteInfo->TotalFreeSystemPtes, -(__int64)v6);
return v8;
}
if( SystemPteInfo != (_MI_SYSTEM_PTE_TYPE *)((char *)&stru_140C4DB30 + 4432) )
{
LABEL_20:
v11 = 0;
v63 = 0;
LowestBitEverAllocated = 0i64;
while( 1 )
{
while( 1 )
{
v13 = SystemPteInfo;
Hint = SystemPteInfo->Hint;
v56 = Hint;
BitMapHeader = SystemPteInfo;
if( SystemPteInfo->Bitmap.SizeOfBitMap )
break;
LABEL_57:
if( !LowestBitEverAllocated || LowestBitEverAllocated == SystemPteInfo->LowestBitEverAllocated )
{
if( (SystemPteInfo->Flags & 1) == 0
|| ((v11 & 1) != 0 || (v11 |= 1u, v63 = v11, MiEmptyPteBins(SystemPteInfo, 0i64), v31 != 1))
&& ((v11 & 2) != 0
|| (v11 |= 2u, v63 = v11, MiPteBinsNeedTrimming(SystemPteInfo) != 1)
|| (MiEmptyPteBins(SystemPteInfo, (CHAR *)1), v41 != 1)) )
{
v8 = MiExpandPtes(SystemPteInfo, v4);
if( v8 )
goto LABEL_44;
if( (SystemPteInfo->Flags & 1) == 0 || (v63 & 4) != 0 )
goto LABEL_66;
v11 = v63 | 4;
v63 |= 4u;
MiEmptyPteBins(SystemPteInfo, (CHAR *)1);
}
}
}
LowestBitEverAllocated = SystemPteInfo->LowestBitEverAllocated;
v59 = LowestBitEverAllocated;
if( LowestBitEverAllocated )
{
if( SystemPteInfo->Bitmap.SizeOfBitMap < LowestBitEverAllocated )
{
LowestBitEverAllocated = 0i64;
v59 = 0i64;
}
else
{
*(_QWORD *)&v15 = SystemPteInfo->Bitmap.SizeOfBitMap - LowestBitEverAllocated;
*((_QWORD *)&v15 + 1) = &SystemPteInfo->Bitmap.Buffer[LowestBitEverAllocated >> 6];
v61 = v15;
v13 = (_MI_SYSTEM_PTE_TYPE *)&v61;
BitMapHeader = (_MI_SYSTEM_PTE_TYPE *)&v61;
if( Hint )
{
Hint -= LowestBitEverAllocated;
v56 = Hint;
}
}
}
SizeOfBitMap = v13->Bitmap.SizeOfBitMap;
v17 = Hint;
Buffer = v13->Bitmap.Buffer;
v57 = v13->Bitmap.SizeOfBitMap;
if( Hint >= v13->Bitmap.SizeOfBitMap )
v17 = 0i64;
v58 = v13->Bitmap.Buffer;
v19 = SizeOfBitMap - 1;
if( v6 )
{
while( v19 - v17 + 1 < v6 )
{
v25 = -1i64;
LABEL_52:
if( !v17 )
{
LABEL_36:
LowestBitEverAllocated = v59;
v26 = v25;
v13 = BitMapHeader;
goto LABEL_37;
}
v30 = v6 + Hint;
if( v6 + Hint > SizeOfBitMap )
v30 = SizeOfBitMap;
Buffer = v58;
v19 = v30 - 1;
v17 = 0i64;
}
v20 = v19 - v6 + 1;
v62 = v20;
v21 = (unsigned __int64)&Buffer[v20 >> 6];
v22 = Buffer[v17 >> 6] | ((1i64 << (v17 & 0x3F)) - 1);
v23 = &Buffer[v17 >> 6];
if( v6 > 0x7F )
{
v43 = v21 + 8;
if( (v20 & 0x3F) == 0 )
v43 = v21;
if( v22 )
{
if( *++v23 )
{
v44 = v58;
goto LABEL_101;
}
v34 = !_BitScanReverse64((unsigned __int64 *)&v50, v22);
if( v34 )
v46 = 64;
else
v46 = 63 - v50;
v44 = v58;
}
else
{
v44 = v58;
v46 = 0;
}
while( 1 )
{
v25 = ((v23 - v44) << 6) - v46;
if( v25 > v20 )
break;
v47 = &v23[(v6 - v46) >> 6];
if( ++v23 == v47 )
{
LABEL_109:
v48 = ((_BYTE)v6 - (_BYTE)v46) & 0x3F;
if( (((_BYTE)v6 - (_BYTE)v46) & 0x3F) != 0 )
{
v34 = !_BitScanForward64((unsigned __int64 *)&v49, *v23);
if( v34 )
LODWORD(v49) = 64;
if( (unsigned int)v49 < v48 )
goto LABEL_101;
}
goto LABEL_35;
}
while( !*v23 )
{
if( ++v23 == v47 )
goto LABEL_109;
}
do
{
LABEL_101:
if( (unsigned __int64)v23 > v43 )
{
LABEL_85:
v25 = -1i64;
goto LABEL_51;
}
++v23;
}
while( *v23 );
v34 = !_BitScanReverse64((unsigned __int64 *)&v45, *(v23 - 1));
if( v34 )
v46 = 64;
else
v46 = 63 - v45;
}
}
else
{
if( v6 >= 0x40 )
{
while( 2 )
{
v51 = v22 < 0;
while( 1 )
{
if( v51 )
{
do
{
if( (unsigned __int64)++v23 > v21 )
goto LABEL_50;
v22 = *v23;
}
while( (*v23 & 0x8000000000000000ui64) != 0i64 );
}
v34 = !_BitScanReverse64((unsigned __int64 *)&v52, v22);
if( v34 )
v53 = 64;
else
v53 = 63 - v52;
v25 = ((v23 - Buffer + 1) << 6) - v53;
if( v25 > v20 )
goto LABEL_50;
v54 = v6 - v53;
if( v6 == v53 )
goto LABEL_35;
v22 = v23[1];
++v23;
if( v54 < 0x40 )
break;
v51 = v22 < 0;
if( !v22 )
{
v54 -= 64i64;
if( !v54 )
goto LABEL_35;
v22 = v23[1];
++v23;
break;
}
}
v34 = !_BitScanForward64(&v55, v22);
if( v34 )
v55 = 64i64;
if( v55 < v54 )
continue;
goto LABEL_35;
}
}
if( v6 > 1 )
{
v32 = 0;
v33 = &v58[v19 >> 6];
while( 1 )
{
if( v22 == -1 )
{
while( 1 )
{
if( (unsigned __int64)++v23 > v21 )
goto LABEL_85;
v22 = *v23;
if( *v23 != -1i64 )
{
v32 = 0;
break;
}
}
}
v34 = !_BitScanForward64((unsigned __int64 *)&v35, v22);
if( v34 )
LODWORD(v35) = 64;
if( v32 + (unsigned int)v35 >= v6 )
break;
v36 = v6;
v37 = ~v22;
while( 1 )
{
v37 &= v37 >> (v36 >> 1);
if( !v37 )
break;
v36 -= v36 >> 1;
if( v36 <= 1 )
{
_BitScanForward64(&v38, v37);
v39 = (unsigned int)v38;
goto LABEL_76;
}
}
if( v23 == v33 )
goto LABEL_85;
v34 = !_BitScanReverse64((unsigned __int64 *)&v40, v22);
if( v34 )
v32 = 64;
else
v32 = 63 - v40;
v22 = v23[1];
++v23;
}
v39 = -(__int64)v32;
LABEL_76:
v25 = ((v23 - v58) << 6) + v39;
if( v25 <= v62 )
goto LABEL_35;
}
else
{
if( v22 != -1 )
{
LABEL_34:
_BitScanForward64(&v24, ~v22);
v25 = v24 + ((v23 - Buffer) << 6);
if( v25 > v20 )
{
v25 = -1i64;
}
else
{
LABEL_35:
if( v25 != -1i64 )
goto LABEL_36;
}
LABEL_51:
SizeOfBitMap = v57;
Hint = v56;
goto LABEL_52;
}
while( (unsigned __int64)++v23 <= v21 )
{
v22 = *v23;
if( *v23 != -1i64 )
goto LABEL_34;
}
}
}
LABEL_50:
v25 = -1i64;
goto LABEL_51;
}
v26 = v17 & 0xFFFFFFFFFFFFFFF8ui64;
LABEL_37:
if( v26 == -1i64 )
{
v11 = v63;
goto LABEL_57;
}
if( RtlInterlockedSetClearRunEx(&v13->Bitmap, v26, v6) )
{
v27 = v26 + LowestBitEverAllocated;
if( v13 != (_MI_SYSTEM_PTE_TYPE *)&v61 )
v27 = v26;
v28 = 16 * v27;
SystemPteInfo->Hint = v27 + v6;
if( (SystemPteInfo->Flags & 4) == 0 )
v28 = v27;
v8 = &SystemPteInfo->BasePte[v28];
LABEL_44:
_InterlockedExchangeAdd64((volatile signed __int64 *)&SystemPteInfo->TotalFreeSystemPtes, -(__int64)v6);
v5 = v4;
LABEL_5:
if( (SystemPteInfo->Flags & 2) != 0 )
{
if( SystemPteInfo == (_MI_SYSTEM_PTE_TYPE *)((char *)&stru_140C4DB30 + 4432) && (dword_140CFA17C & 2) != 0 )
MiCheckPteReserve(v8, v5);
if( (unsigned int)v4 <= 0x10 )
{
if( (_DWORD)v4 )
{
do
{
v9.Long = (unsigned __int64)v8[v3].u;
if( *(&stru_140C4DB30 + 42) && (v9.Long & 0x10) == 0 )
v9.Long &= ~*(&stru_140C4DB30 + 42);
if( (v9.Long & 0xFFFFFFFF00000000ui64) != 0 )
break;
++v3;
}
while( v3 < (unsigned int)v4 );
}
if( v3 == (_DWORD)v4 )
return v8;
}
v29 = FlushKernel;
}
else
{
v29 = FlushSession;
}
MiFlushTbAsNeeded(v8, v5, v29);
return v8;
}
v11 = v63;
}
}
LABEL_66:
++*SystemPteInfo->FailureCount;
return 0i64;
}Referenced by:
MiAddPagesToEnclave
MiAllocateEnclaveVad
MiApplyImportOptimizationToRuntimeDriver
MiBuildDynamicRegion
MiBuildReservationCluster
MiCopyDataPageToImagePage
MiCopyToUserVa
MiCreatePfnBitMaps
MiCreatePteCopyList
MiCreateRetpolineBitmap
MiExpandPartitionIds
MiGetPageChain
MiInitNucleus
MiInitSystem
MiInitializeCrashDumpPtes
MiInitializeEnclaveMetadataPage
MiInitializeGapFrames
MiInitializeKernelCfg
MiInitializeMirroring
MiInitializePageFaultResources
MiInitializePteInfo
MiInitializeRetpoline
MiInitializeSystemPtes
MiInitializeTbFlush
MiInsertInSystemSpace
MiLockAndMapEntireDriver
MiMakePageAvoidRead
MiMapContiguousMemory
MiMapDummyPages
MiMapHotPatchImageInSystemSpace
MiMapSinglePage
MiPrivateFixup
MiRelocateImageAgain
MiRelocateImagePfn
MiReservePageHash
MiScrubNodeLargePageList
MiSessionCreateInternal
MiUpdateImagePfnImportRelocations
MiValidateImagePfn
MiZeroInParallelWorker
MiZeroLargePage
MiZeroPhysicalPage
MmAllocateDumpHibernateResources
MmAllocateIndependentPagesEx
MmAllocateMappingAddressEx
MmAllocateNonCachedMemory
MmCopyMemory
MmCopyToCachedPage
MmCreateKernelStack
MmCreateProcessAddressSpace
MmInitializeProcessor
MmMapLockedPagesSpecifyCache
MmMapMdl