MiAllocatePartitionPhysicalPages

NTSTATUS __stdcall MiAllocatePartitionPhysicalPages(
        _MI_PARTITION *Partition,
        _MI_PARTITION *ToPartition,
        INT64 NumberOfPages,
        INT64 RequiredNode,
        INT64 Flags,
        INT64 a6){
  union {unsigned int LongFlags;_MI_PARTITION_FLAGS Flags;} v6; 
  _MI_PARTITION *v7; 
  unsigned int IdealNode; 
  INT64 v9; 
  INT64 HugeRangeFromNode; 
  __int64 v11; 
  int v12; 
  int v13; 
  int v14; 
  int v15; 
  int v16; 
  unsigned __int64 v18; 
  _BOOL4 v19; 
  unsigned int v20; 
  unsigned int v21; 
  unsigned int v22; 
  unsigned int v23; 
  __int64 v24; 
  int v25; 
  unsigned int v26; 
  _MI_ZERO_THREAD_CONTEXT *LargeNodePage; 
  UINT64 v28; 
  int v29; 
  __int64 v30; 
  _MI_PARTITION *v31; 
  int updated; 
  __int64 v33; 
  char v34; 
  _LARGE_INTEGER v35; 
  int v36; 
  unsigned int v37; 
  unsigned __int64 v38; 
  unsigned __int64 v39; 
  _MDL *PagesForMdl; 
  unsigned int *v41; 
  UINT64 v42; 
  int v43; 
  _MI_PARTITION *v44; 
  __int32 v45; 
  NTSTATUS v46; 
  UINT64 PfnState; 
  UINT64 v48; 
  _MI_PARTITION *Partitiona; 
  int v50; 
  int v51; 
  _RTL_AVL_TREE PageRoot; 
  __int64 v53; 
  unsigned __int64 v54; 
  unsigned int v55; 
  unsigned __int64 v56; 
  __m256i InsertInfo; 
  UINT64 LargePageIndex; 
  _MI_PARTITION *TargetPartition; 
  INT64 v60; 
  int v61; 

  v60 = NumberOfPages;
  TargetPartition = ToPartition;
  v6.LongFlags = (unsigned int)ToPartition->Core.u;
  v7 = &Irp;
  IdealNode = RequiredNode;
  v55 = RequiredNode;
  v9 = NumberOfPages;
  PageRoot.Root = 0i64;
  HugeRangeFromNode = 0i64;
  v53 = 0i64;
  if( Partition )
    v7 = Partition;
  v11 = 0i64;
  Partitiona = v7;
  v51 = Flags & 4;
  v12 = ((v6.Flags._bf_0 & 0x40) == 0) | 0x100000;
  v13 = v12 | 0x8000;
  *(_OWORD *)InsertInfo.m256i_i8 = 0i64;
  if( (Flags & 4) == 0 )
    v13 = v12;
  v14 = v13 | 0x4000;
  *(_OWORD *)&InsertInfo.m256i_u64[2] = 0i64;
  if( (Flags & 0x12) != 0 )
    v14 = v13;
  v15 = Flags & 0x200;
  v61 = v15;
  v50 = v14;
  if( (Flags & 0x200) == 0 )
  {
    MiAcquireNonPagedResources(v7, NumberOfPages);
    if( v16 < 0 )
      return -1073741670;
    v7 = Partitiona;
    v9 = v60;
  }
  v54 = 0x40000i64;
  while( 1 )
  {
    v18 = v9 - v11;
    v56 = v9 - v11;
    if( (unsigned __int64)(v9 - v11) < 0x200 )
      goto LABEL_39;
    v19 = 1;
    if( (Flags & 0x60) == 0 )
      v19 = v18 < 0x40000;
    HugeRangeFromNode &= 0xFFFFFFFFFFFC0000ui64;
    LODWORD(LargePageIndex) = v19;
    v20 = 0;
    if( v15 )
    {
      v21 = 0;
      if( KeNumberNodes )
      {
        do
        {
          HugeRangeFromNode = MiGetHugeRangeFromNode(Partitiona, v21, (v14 & 1) == 0);
          if( (HugeRangeFromNode & 0x3FFFF) != 0 || (Flags & 1) == 0 )
            break;
          v22 = v21 + 1;
          v23 = 0;
          if( v22 != (unsigned __int16)KeNumberNodes )
            v23 = v22;
          v21 = v23 + 1;
        }
        while( v21 < (unsigned __int16)KeNumberNodes );
        IdealNode = v55;
        v18 = v56;
        v9 = v60;
      }
      if( (HugeRangeFromNode & 0x3FFFF) == 0 )
        goto LABEL_38;
      v24 = (unsigned __int64)(HugeRangeFromNode & 0x3FFFF) << 18;
      LODWORD(LargePageIndex) = 0;
      LOBYTE(v25) = MiHugeRangeIsZeroed(HugeRangeFromNode);
      v20 = v26;
      LOBYTE(v20) = v25 != 0;
    }
    else
    {
      LODWORD(PfnState) = v14;
      LargeNodePage = (_MI_ZERO_THREAD_CONTEXT *)MiFindLargeNodePage(v7, IdealNode, &LargePageIndex, 1ui64, PfnState);
      if( !LargeNodePage )
      {
        v9 = v60;
        goto LABEL_39;
      }
      v24 = ((char *)LargeNodePage - (char *)MmGetPfnDb()) / 48;
      if( (LargeNodePage[4] & 0x3E0i64) != 0 )
      {
        if( (v14 & 1) != 0 )
          goto LABEL_30;
        MiZeroLargePage(LargeNodePage);
      }
      v20 = 1;
    }
LABEL_30:
    v28 = MiLargePageSizes[(unsigned int)LargePageIndex];
    MiAddRangeToPartitionTree(&PageRoot, v24, v28, v20);
    if( !v29 )
      break;
    v7 = Partitiona;
    v15 = v61;
    if( Partitiona == &Irp && !v61 )
      _InterlockedExchangeAdd64((_QWORD *)&stru_140C4DB30 + 569, v28);
    v9 = v60;
    v11 += v28;
    if( v11 == v60 )
      goto LABEL_48;
    v14 = v50;
  }
  if( v61 )
  {
    MiInsertHugeRangeInList(HugeRangeFromNode, v20, 0i64);
    v9 = v60;
LABEL_38:
    v15 = v61;
LABEL_39:
    v30 = v53;
    goto LABEL_40;
  }
  LODWORD(v33) = MiFreeMdlPageRun(v24, v28, v20);
  v9 = v60;
  v30 = v33;
  v15 = v61;
LABEL_40:
  if( v11 == v9 )
  {
    v7 = Partitiona;
  }
  else
  {
    v31 = Partitiona;
    if( v15 || (MiReleaseNonPagedResources(Partitiona, v18 - v30), (Flags & 0xA2) != 0) || v30 )
    {
      updated = -1073741670;
      goto LABEL_74;
    }
    v9 = v60;
    v7 = Partitiona;
  }
LABEL_48:
  v34 = Flags;
  v35.QuadPart = 0i64;
  v36 = v50 & 1 | 2;
  if( (Flags & 1) != 0 )
    v36 = v50 & 1;
  v37 = v36 | 0x10;
  if( (Flags & 0x10) != 0 )
  {
    v37 |= 0x40u;
    v35.QuadPart = 0x200000i64;
    v38 = 0x40000i64;
  }
  else
  {
    if( (Flags & 0x40) != 0 )
    {
      v37 |= 0x40u;
      v38 = 512i64;
      v35.QuadPart = 0x200000i64;
    }
    else if( (Flags & 0x100) != 0 )
    {
      v37 |= 0x40u;
      v38 = 0x40000i64;
      v35.QuadPart = 0x40000000i64;
    }
    else
    {
      v38 = 1048574i64;
    }
    v54 = v38;
  }
  if( v11 == v9 )
  {
LABEL_71:
    v43 = v61;
    v44 = TargetPartition;
    if( !v61 )
    {
      updated = MiUpdatePartitionLargePfnBitMap(TargetPartition, &PageRoot);
      if( updated < 0 )
        goto LABEL_73;
    }
    InsertInfo.m256i_i64[0] = (__int64)&PageRoot;
    v45 = 3;
    *(_OWORD *)&InsertInfo.m256i_u64[1] = 0i64;
    if( (v34 & 8) != 0 )
      v45 = 7;
    InsertInfo.m256i_i32[6] = v45;
    if( v43 )
      InsertInfo.m256i_i32[6] = v45 | 0x10;
    MiInsertPartitionPages(Partitiona, v44, InsertInfo.m256i_i32);
    return v46;
  }
  while( 2 )
  {
    v39 = v38;
    LODWORD(v48) = v37;
    if( v9 - v11 <= v38 )
      v39 = v9 - v11;
    PagesForMdl = MiAllocatePagesForMdl(
                    v7,
                    (_LARGE_INTEGER)(-(__int64)(v51 != 0) & 0x100000000i64),
                    (_LARGE_INTEGER)-1i64,
                    v35,
                    v39 << 12,
                    MiCached,
                    IdealNode,
                    v48);
    v41 = (unsigned int *)PagesForMdl;
    if( !PagesForMdl )
    {
      if( (v37 & 0x40) == 0 )
        goto LABEL_70;
      v37 = v37 & 0xFFFFFF9F | 0x20;
      goto LABEL_67;
    }
    if( MiAddMdlToPartitionTree(&PageRoot.Root, (INT64)PagesForMdl, v37) )
    {
      v11 += (unsigned __int64)v41[10] >> 12;
      ExFreePoolWithTag(v41, 0);
LABEL_67:
      v9 = v60;
      if( v11 == v60 )
        goto LABEL_71;
      v7 = Partitiona;
      v38 = v54;
      continue;
    }
    break;
  }
  MiFreePagesFromMdl(v41, 0i64, v42);
  ExFreePoolWithTag(v41, 0);
LABEL_70:
  updated = -1073741670;
LABEL_73:
  v31 = Partitiona;
LABEL_74:
  MiFreePartitionTree(v31, &PageRoot, 1ui64);
  return updated;
}

Referenced by:

MiReleasePartitionHugeIoSpace
MmManagePartitionMoveMemory