MiAllocateContiguousMemory

VOID *__stdcall MiAllocateContiguousMemory(
        UINT64 NumberOfBytes,
        UINT64 LowestPage,
        UINT64 HighestPage,
        UINT64 BoundaryPage,
        UINT64 ProtectionMask,
        UINT64 PreferredNode,
        UINT64 CatchOverruns){
  UINT64 v9; 
  UINT64 PerformanceCounter; 
  unsigned int v11; 
  __int64 v12; 
  INT64 v13; 
  UINT64 ContiguousHeapPool; 
  UINT64 v15; 
  int v16; 
  unsigned int v17; 
  int v18; 
  char v19; 
  _LIST_ENTRY *v20; 
  _MI_PFN_CACHE_ATTRIBUTE CacheAttribute; 
  unsigned int v22; 
  unsigned int v23; 
  _DWORD *v24; 
  __int64 v25; 
  _DWORD *v26; 
  NTSTATUS i; 
  INT8 *v28; 
  UINT64 v29; 
  _MMPFN *v30; 
  _MMPFN *v31; 
  char *v32; 
  unsigned __int64 v33; 
  char *v34; 
  UINT64 v36; 
  __int64 v37; 
  UINT64 SizeInPages; 
  UINT64 SizeInPagesa; 
  UINT64 v40; 
  UINT64 v41; 
  UINT64 Flags; 
  UINT64 Flagsa; 
  UINT64 *StartingPfn; 
  UINT64 *StartingPfna; 
  UINT8 v46; 
  unsigned int v47; 
  unsigned int v48; 
  int v50; 
  _MI_PFN_CACHE_ATTRIBUTE v51; 
  POOL_TYPE PoolType; 
  _BOOL4 v55; 
  _DWORD *v56; 
  INT64 v58; 
  __int64 v59[7]; 
  int v60; 
  unsigned int v61; 
  unsigned int v62; 
  _EVENT_DATA_DESCRIPTOR UserData; 

  v9 = LowestPage;
  v47 = ProtectionMask;
  PerformanceCounter = KeQueryPerformanceCounter(0i64);
  v11 = ProtectionMask;
  v12 = -1i64;
  v58 = PerformanceCounter;
  v13 = PerformanceCounter;
  v55 = MmProtectFreedNonPagedPool != 0;
  ContiguousHeapPool = 0i64;
  v15 = (NumberOfBytes >> 12) + ((NumberOfBytes & 0xFFF) != 0);
  if( (ProtectionMask & 2) != 0 && (MiFlags & 0x10000) != 0 )
  {
    v11 = ProtectionMask & 0xFFFFFFFD;
    v47 = ProtectionMask & 0xFFFFFFFD;
  }
  if( !BoundaryPage || (v16 = 0, v15 <= BoundaryPage) )
  {
    v16 = 0;
    if( v9 <= HighestPage && v9 + v15 > v9 && v9 + v15 - 1 <= HighestPage )
    {
      CacheAttribute = MiProtectionToCacheAttribute(v11);
      v22 = PreferredNode;
      v51 = CacheAttribute;
      if( (unsigned __int16)KeNumberNodes <= 1u )
        v22 = v17;
      v48 = v22;
      if( v22 == 0x80000000 )
      {
        v23 = (*(&KiProcessorBlock + KeGetCurrentThread()->IdealProcessor))->ParentNode->Affinity.Reserved[0];
      }
      else
      {
        v23 = v22;
        if( v22 >= (unsigned __int16)KeNumberNodes )
          goto LABEL_31;
      }
      v24 = (_DWORD *)(*(&stru_140C4DB30 + 21) + 4i64 * v23 * (unsigned __int16)KeNumberNodes);
      if( v22 == 0x80000000 )
        v25 = (unsigned __int16)KeNumberNodes;
      else
        v25 = 1i64;
      v56 = &v24[v25];
      PoolType = NonPagedPoolNx;
      if( (v19 & 2) != 0 )
      {
        PoolType = NonPagedPool;
      }
      else if( !v18 && CacheAttribute == MiCached && !MmProtectFreedNonPagedPool && v15 <= 0x200 )
      {
        while( 1 )
        {
          LODWORD(SizeInPages) = *v24;
          ContiguousHeapPool = (UINT64)ExAllocateContiguousHeapPool(
                                         LowestPage,
                                         v20,
                                         BoundaryPage,
                                         NumberOfBytes,
                                         SizeInPages);
          if( ContiguousHeapPool )
            break;
          v20 = (_LIST_ENTRY *)HighestPage;
          if( ++v24 == v56 )
          {
            CacheAttribute = v51;
            goto LABEL_20;
          }
        }
        v16 = 1;
        goto LABEL_31;
      }
LABEL_20:
      v50 = 135266304;
      v26 = (_DWORD *)(*(&stru_140C4DB30 + 21) + 4i64 * v23 * (unsigned __int16)KeNumberNodes);
      LODWORD(StartingPfn) = 135266304;
      LODWORD(Flags) = 0x80000000;
      LODWORD(v40) = *v26;
      for( i = MiFindContiguousPages(
                  &Irp,
                  LowestPage,
                  (UINT64)v20,
                  BoundaryPage,
                  v15,
                  CacheAttribute,
                  v40,
                  Flags,
                  StartingPfn);
            i < 0;
            i = MiFindContiguousPages(&Irp, LowestPage, HighestPage, BoundaryPage, v15, v51, v41, Flagsa, StartingPfna) )
      {
        if( ++v26 == v56 )
          goto LABEL_49;
        LODWORD(Flagsa) = 0x80000000;
        LODWORD(v41) = *v26;
        v50 &= ~0x8000000u;
        LODWORD(StartingPfna) = v50;
      }
      v28 = MiMapContiguousMemory(0xFFFFFFFFFFFFF000ui64, (_EPROCESS *)(v15 << 12), v47, v55);
      ContiguousHeapPool = (UINT64)v28;
      if( v28 )
      {
        LODWORD(SizeInPagesa) = v55;
        if( !(unsigned int)ExInsertPoolTag(v29, v28, v15 << 12, PoolType, SizeInPagesa) )
        {
          v36 = (v15 << 12) + 4096;
          if( !v55 )
            v36 = v15 << 12;
          MmUnmapIoSpace((VOID *)ContiguousHeapPool, v36);
          ContiguousHeapPool = 0i64;
        }
      }
      v30 = MmGetPfnDb() - 1;
      v31 = &v30[v15];
      if( ContiguousHeapPool )
        v32 = (char *)MmGetPteBase() + ((ContiguousHeapPool >> 9) & 0x7FFFFFFFF8i64);
      else
        v32 = 0i64;
      do
      {
        v46 = MiLockPageInline(v30);
        v33 = v30->u4._bf_0 & 0xFFFFFFF000000000ui64 | 0xFFFFFFFFDi64;
        v30->PteLong = (unsigned __int64)v32;
        v30->u4._bf_0 = v33;
        _InterlockedAnd64(&v30->u2.Lock, 0x7FFFFFFFFFFFFFFFui64);
        __writecr8(v46);
        v34 = v32;
        ++v30;
        v32 += 8;
        if( !v34 )
          v32 = 0i64;
      }
      while( v30 < v31 );
      v13 = v58;
      if( ContiguousHeapPool )
      {
        v22 = v48;
        v16 = 0;
        goto LABEL_31;
      }
      v22 = v48;
LABEL_49:
      v16 = 0;
LABEL_31:
      v9 = LowestPage;
      goto LABEL_32;
    }
  }
  v22 = PreferredNode;
LABEL_32:
  if( EtwEventEnabled(*(&ExBootDevicesRemovedEvent + 251), &KERNEL_MEM_EVENT_CONT_ALLOCATION) )
  {
    if( ContiguousHeapPool )
    {
      LODWORD(v37) = MmGetPhysicalAddress(ContiguousHeapPool);
      v12 = v37;
    }
    v59[0] = EtwpGetDurationSince(v13);
    v59[3] = HighestPage << 12;
    v59[4] = BoundaryPage << 12;
    v61 = v47;
    v59[1] = NumberOfBytes;
    UserData.Ptr = (unsigned __int64)v59;
    v59[6] = ContiguousHeapPool;
    v59[5] = v12;
    v59[2] = v9 << 12;
    v60 = v16;
    v62 = v22;
    *(_QWORD *)&UserData.Size = 68i64;
    EtwWriteEx(*(&ExBootDevicesRemovedEvent + 251), &KERNEL_MEM_EVENT_CONT_ALLOCATION, 0i64, 1u, 0, 0, 1u, &UserData);
  }
  return(VOID *)ContiguousHeapPool;
}

Referenced by:

MmAllocateContiguousMemory
MmAllocateContiguousNodeMemory