MiGetPage

NTSTATUS __stdcall MiGetPage(ULONG_PTR *Partition, UINT64 Color, UINT64 Flags){
  unsigned int v3; 
  const _MMLISTS *v4; 
  _MI_PARTITION *v5; 
  unsigned int v6; 
  int v7; 
  char *v8; 
  _MI_PFN_CACHE_ATTRIBUTE v9; 
  unsigned int v10; 
  unsigned int v11; 
  unsigned int v12; 
  __int64 v13; 
  int v14; 
  unsigned int v15; 
  __int64 *v16; 
  __int64 v17; 
  _MMLISTS i; 
  _SLIST_HEADER *v19; 
  INT64 PerfectColorHeadPage; 
  _MI_PARTITION *v21; 
  unsigned int v22; 
  int v23; 
  unsigned __int64 v24; 
  __int64 v25; 
  NTSTATUS v26; 
  int v28; 
  int v29; 
  _MMLISTS v30; 
  _MMPFNLIST *v31; 
  unsigned __int64 Flink; 
  _MMPFNLIST *v33; 
  __int64 v34; 
  __int64 *v35; 
  unsigned int v36; 
  _MMLISTS v37; 
  _SLIST_HEADER *v38; 
  INT64 v39; 
  INT64 v40; 
  __int64 v41; 
  unsigned __int64 v42; 
  __int64 v43; 
  unsigned __int64 v44; 
  int v45; 
  unsigned __int8 CurrentIrql; 
  volatile signed __int64 *v47; 
  signed __int8 v48; 
  UINT64 *p_Lock; 
  UINT64 v50; 
  bool v51; 
  char *v53; 
  char *v54; 
  int v55; 
  __int64 v56; 
  int v57; 
  volatile signed __int32 *v58; 
  __int64 v59; 
  _BOOL4 v60; 
  unsigned __int64 v61; 
  __int64 v62; 
  _MMPFN *PfnDb; 
  __int64 v64; 
  UINT8 *BugCheckParameter4; 
  unsigned int v66; 
  unsigned int v67; 
  volatile signed __int64 *v68; 
  unsigned __int8 v69; 
  char *v70; 
  _BOOL4 v71; 
  char v72; 
  char v73; 
  int v74; 
  UINT64 SpinCount; 
  _MMPFNLIST *v76; 
  _KLOCK_QUEUE_HANDLE LockQueue; 
  __int64 v78; 
  __int64 v79; 
  char *v80; 
  unsigned int Colora; 
  _MMLISTS PageType; 
  _MMLISTS PageTypea; 

  Colora = Color;
  v3 = MmNumberOfChannels;
  v4 = &MiFreeThenFree;
  v5 = (_MI_PARTITION *)Partition;
  HIDWORD(SpinCount) = MmNumberOfChannels;
  v6 = (unsigned int)Color >> *(&stru_140C4DB30 + 156);
  v7 = 0;
  v8 = 0i64;
  v9 = MiCached;
  v71 = 1;
  v66 = 0;
  v10 = Flags;
  v11 = Color;
  v12 = (unsigned int)Color >> *(&stru_140C4DB30 + 156);
  while( 2 )
  {
    v70 = 0i64;
LABEL_3:
    v13 = (__int64)&v5->Core.NodeInformation[v12];
    if( !*(_QWORD *)(v13 + 4176) && (_DWORD)InitializationPhase )
    {
      if( MiPageAvailable(v5, v10) )
      {
        v8 = v70;
        goto LABEL_146;
      }
      return -1;
    }
    if( v3 > 1 )
    {
      v8 = (char *)(v13 + 4313);
      v70 = (char *)(v13 + 4313);
      if( v12 == v6 )
      {
        v53 = (char *)(v13 + ((v10 & 8 | 0x21B2ui64) >> 1));
        v71 = (*(_DWORD *)(v13 + 4280) & 1) == 0;
        v70 = v53;
        v8 = v53;
        if( (v10 & 0x20) != 0 )
        {
          v70 = (char *)(v13 + ((v10 & 8 | 0x21B2ui64) >> 1));
          if( (*(_DWORD *)(v13 + 4280) & 1) != 0 )
          {
            v54 = &v73;
            v55 = (unsigned __int16)(unsigned __int8)MiChannelMaximumPowerOf2Mask & (unsigned __int16)(v11 >> *(&stru_140C4DB30 + 157));
            v72 = MiChannelMaximumPowerOf2Mask & (v11 >> *(&stru_140C4DB30 + 157));
            v56 = v3;
            do
            {
              v57 = (unsigned __int8)*v53;
              if( v57 != v55 )
                *v54++ = v57;
              ++v53;
              --v56;
            }
            while( v56 );
            v8 = &v72;
            v70 = &v72;
          }
        }
        v4 = &MiFreeThenFree;
        v66 = (unsigned __int8)*v8;
      }
    }
    v14 = v10 & 2;
    v80 = &v8[v3];
    v74 = v14;
    do
    {
      if( v8 )
      {
        v15 = v11 & *(&stru_140C4DB30 + 66) | (v12 << *(&stru_140C4DB30 + 156)) | ((unsigned __int8)*v8 << *(&stru_140C4DB30 + 157));
        Colora = v15;
      }
      else
      {
        v15 = Colora;
      }
      v16 = &MiZeroThenZero;
      if( !v14 )
        v16 = (__int64 *)v4;
      v79 = v15;
      v17 = 16i64 * v15;
      v78 = v17;
      for( i = *(_DWORD *)v16; ; i = *((_DWORD *)v16 + 1) )
      {
        v19 = (_SLIST_HEADER *)((char *)v5->PageLists.FreePageSlist[i] + v17);
        if( LOWORD(v19->Alignment) )
        {
          PerfectColorHeadPage = (INT64)RtlpInterlockedPopEntrySList(v19);
          if( PerfectColorHeadPage )
          {
            *(_MMPTE *)(PerfectColorHeadPage + 16) = ZeroPte;
            if( i == ZeroedPageList )
            {
              if( (MiFlags & 0x80u) != 0 && (++*(&stru_140C4DB30 + 631) & MmPageValidationFrequency) == 0 )
                MiArePageContentsZero((PVOID)((PerfectColorHeadPage - (__int64)MmGetPfnDb()) / 48));
              goto LABEL_15;
            }
            if( ZeroPte.u.Long )
            {
              v42 = ZeroPte.u.Long & 0xFFFFFFFFFFFFFC9Fui64;
            }
            else
            {
              v41 = 128i64;
              if( !*(&stru_140C4DB30 + 42) )
              {
LABEL_68:
                *(_QWORD *)(PerfectColorHeadPage + 16) = v41;
LABEL_15:
                *(_QWORD *)PerfectColorHeadPage = 0i64;
                goto LABEL_16;
              }
              if( (*(&stru_140C4DB30 + 336) & 0x80) != 0 )
              {
                v41 = 144i64;
                goto LABEL_68;
              }
              v42 = *(&stru_140C4DB30 + 42);
            }
            v41 = v42 | 0x80;
            goto LABEL_68;
          }
          v17 = v78;
          v5 = (_MI_PARTITION *)Partition;
        }
        if( i == *((_DWORD *)v16 + 1) )
          break;
      }
      PageType = *(_DWORD *)v16;
      v30 = PageType;
      memset(&LockQueue, 0, sizeof(LockQueue));
      v31 = v5->PageLists.FreePagesByColor[PageType];
      Flink = v31[v79].Flink;
      v33 = &v31[v79];
      v76 = v33;
      if( Flink == 0xFFFFFFFFFi64 )
      {
LABEL_45:
        PerfectColorHeadPage = 0i64;
        goto LABEL_16;
      }
      v45 = v10 & 1;
      while( 2 )
      {
        PerfectColorHeadPage = (INT64)&MmGetPfnDb()[Flink];
        if( v45 )
        {
          v47 = (volatile signed __int64 *)(PerfectColorHeadPage + 24);
          v68 = (volatile signed __int64 *)(PerfectColorHeadPage + 24);
          if( _interlockedbittestandset64((volatile signed __int32 *)(PerfectColorHeadPage + 24), 0x3Fui64) )
            goto LABEL_45;
          CurrentIrql = 17;
          v69 = 17;
        }
        else
        {
          CurrentIrql = KeGetCurrentIrql();
          v69 = CurrentIrql;
          __writecr8(2ui64);
          v47 = (volatile signed __int64 *)(PerfectColorHeadPage + 24);
          v48 = _interlockedbittestandset64((volatile signed __int32 *)(PerfectColorHeadPage + 24), 0x3Fui64);
          v68 = (volatile signed __int64 *)(PerfectColorHeadPage + 24);
          if( (v10 & 0x4000) != 0 )
          {
            LODWORD(SpinCount) = 0;
            if( v48 )
            {
              v58 = (volatile signed __int32 *)(PerfectColorHeadPage + 24);
              do
              {
                do
                  KeYieldProcessorEx(&SpinCount);
                while( *(__int64 *)v58 < 0 );
              }
              while( _interlockedbittestandset64(v58, 0x3Fui64) );
              v33 = v76;
              v30 = PageType;
              v47 = (volatile signed __int64 *)(PerfectColorHeadPage + 24);
              CurrentIrql = v69;
            }
          }
          else
          {
            v68 = (volatile signed __int64 *)(PerfectColorHeadPage + 24);
            if( v48 )
            {
              __writecr8(CurrentIrql);
              goto LABEL_45;
            }
          }
        }
        if( (*(_BYTE *)(PerfectColorHeadPage + 34) & 7) != v30 )
        {
          _InterlockedAnd64(v47, 0x7FFFFFFFFFFFFFFFui64);
          if( CurrentIrql != 17 )
            __writecr8(CurrentIrql);
          goto LABEL_104;
        }
        LockQueue.LockQueue.Next = 0i64;
        p_Lock = &v33->Lock;
        LockQueue.LockQueue.Lock = &v33->Lock;
        if( (v10 & 0x4000) != 0 )
        {
          KxAcquireQueuedSpinLock(&LockQueue.LockQueue, p_Lock);
        }
        else if( !KxTryToAcquireQueuedSpinLock(&LockQueue.LockQueue, p_Lock) )
        {
          _InterlockedAnd64(v68, 0x7FFFFFFFFFFFFFFFui64);
          if( v69 != 17 )
            __writecr8(v69);
          goto LABEL_45;
        }
        if( Flink != v33->Flink )
        {
          KeReleaseInStackQueuedSpinLockFromDpcLevel(&LockQueue);
          _InterlockedAnd64(v68, 0x7FFFFFFFFFFFFFFFui64);
          if( v69 != 17 )
            __writecr8(v69);
          v30 = PageType;
LABEL_104:
          Flink = v33->Flink;
          if( Flink == 0xFFFFFFFFFi64 )
            goto LABEL_45;
          v45 = v10 & 1;
          continue;
        }
        break;
      }
      if( MiUnlinkFreeOrZeroedPage(Flink, v33, (UINT8 *)v10) )
      {
        _InterlockedAnd64(v68, 0x7FFFFFFFFFFFFFFFui64);
        if( (v10 & 1) != 0 )
        {
          v21 = (_MI_PARTITION *)Partition;
        }
        else
        {
          v51 = v33->Flink == 0xFFFFFFFFFi64;
          v21 = (_MI_PARTITION *)Partition;
          if( !v51 )
            MiReplenishPageSlist((_MI_PARTITION *)Partition, PageType, Colora, v50);
        }
        KeReleaseInStackQueuedSpinLockFromDpcLevel(&LockQueue);
        if( v69 != 17 )
          __writecr8(v69);
        goto LABEL_17;
      }
      KeReleaseInStackQueuedSpinLockFromDpcLevel(&LockQueue);
      MiReturnFreeZeroPage(PerfectColorHeadPage);
      _InterlockedAnd64(v68, 0x7FFFFFFFFFFFFFFFui64);
      if( v69 != 17 )
        __writecr8(v69);
      PerfectColorHeadPage = 1i64;
LABEL_16:
      v21 = (_MI_PARTITION *)Partition;
LABEL_17:
      if( PerfectColorHeadPage == 1 )
        return -1;
      if( PerfectColorHeadPage )
      {
LABEL_19:
        v22 = v66;
LABEL_20:
        *(_QWORD *)PerfectColorHeadPage = 0i64;
        v23 = *(&stru_140C4DB30 + 38);
        v24 = (PerfectColorHeadPage - (__int64)MmGetPfnDb()) / 48;
        if( *(&stru_140C4DB30 + 37) > *(&stru_140C4DB30 + 38)
          || (v25 = *(&stru_140C4DB30 + 27) + 16i64 * *(&stru_140C4DB30 + 37), v24 < *(_QWORD *)v25)
          || *(&stru_140C4DB30 + 37) != *(&stru_140C4DB30 + 38) && v24 >= *(_QWORD *)(v25 + 16) )
        {
          v28 = 0;
          if( *(&stru_140C4DB30 + 38) < 0 )
LABEL_174:
            KeBugCheckEx(0x1Au, (PVOID)0x6201, (PVOID)v24, 0i64, 0i64);
          while( 1 )
          {
            v29 = (v28 + v23) >> 1;
            v25 = *(&stru_140C4DB30 + 27) + 16i64 * v29;
            if( v24 < *(_QWORD *)v25 )
            {
              if( !v29 )
                KeBugCheckEx(0x1Au, (PVOID)0x6200, (PVOID)v24, *(&stru_140C4DB30 + 27), 0i64);
              v23 = v29 - 1;
            }
            else
            {
              if( v29 == *(&stru_140C4DB30 + 38) || v24 < *(_QWORD *)(v25 + 16) )
              {
                *(&stru_140C4DB30 + 37) = (v28 + v23) >> 1;
                break;
              }
              v28 = v29 + 1;
            }
            if( v23 < v28 )
              goto LABEL_174;
          }
        }
        if( *(_DWORD *)(v25 + 8) == v6 )
        {
          if( *(&stru_140C4DB30 + 28) )
            v26 = MiPageToChannel(v24);
          else
            v26 = 0;
          if( v26 != v22 )
          {
            v60 = v71;
            if( !v71 )
              goto LABEL_163;
          }
        }
        else
        {
          v60 = v71;
LABEL_163:
          if( (v10 & 1) == 0 )
          {
            if( (v10 & 0x80u) != 0 )
            {
              PfnDb = MmGetPfnDb();
            }
            else
            {
              LODWORD(v61) = MiSwapNumaStandbyPage(v24, v6, v22, v10 | 0x10);
              v24 = v61;
              v62 = v61;
              PfnDb = MmGetPfnDb();
              PerfectColorHeadPage = (INT64)&PfnDb[v62];
            }
            if( (v10 & 0x10) != 0
              && (LODWORD(v64) = MiSearchNumaNodeTable((PVOID)((PerfectColorHeadPage - (__int64)PfnDb) / 48)),
                  *(_DWORD *)(v64 + 8) != v6)
              || (v10 & 0x20) != 0 && MiGetPfnChannel(PerfectColorHeadPage) != v22 && !v60 )
            {
              MiReleaseFreshPage((_MMPFN *)PerfectColorHeadPage);
              return -1;
            }
          }
        }
        if( (v10 & 0x200) == 0 )
        {
          if( (v10 & 0x400) != 0 )
          {
            v9 = MiNonCached;
          }
          else
          {
            v9 = MiNotMapped;
            if( (v10 & 0x800) != 0 )
              v9 = MiWriteCombined;
          }
        }
        if( (v10 & 0x100) != 0 && MiPfnZeroingNeeded((_MMPFN *)PerfectColorHeadPage, v9) )
        {
          MiZeroPhysicalPage(v24, (v10 & 0x8000) == 0, v9);
          *(_QWORD *)(PerfectColorHeadPage + 16) &= 0xFFFFFFFFFFFFFC1Fui64;
        }
        if( v9 != MiNotMapped && *(unsigned __int8 *)(PerfectColorHeadPage + 34) >> 6 != v9 )
          MiChangePageAttribute((_MMPFN *)PerfectColorHeadPage, v9, 0i64);
        return v24;
      }
      v34 = v78;
      v35 = &MiZeroThenZero;
      v36 = v10 & 0xFFFFFFFD;
      if( !v74 )
        v36 = v10 | 2;
      v67 = v36;
      if( (v36 & 2) == 0 )
        v35 = (__int64 *)&MiFreeThenFree;
      v76 = (_MMPFNLIST *)v35;
      v37 = *(_DWORD *)v35;
      while( 2 )
      {
        PageTypea = v37;
        v38 = (_SLIST_HEADER *)((char *)v21->PageLists.FreePageSlist[v37] + v34);
        if( LOWORD(v38->Alignment) )
        {
          PerfectColorHeadPage = (INT64)RtlpInterlockedPopEntrySList(v38);
          if( PerfectColorHeadPage )
          {
            *(_MMPTE *)(PerfectColorHeadPage + 16) = ZeroPte;
            if( PageTypea == ZeroedPageList )
            {
              if( (MiFlags & 0x80u) != 0 && (++*(&stru_140C4DB30 + 631) & MmPageValidationFrequency) == 0 )
                MiArePageContentsZero((PVOID)((PerfectColorHeadPage - (__int64)MmGetPfnDb()) / 48));
              goto LABEL_55;
            }
            if( ZeroPte.u.Long )
            {
              v44 = ZeroPte.u.Long & 0xFFFFFFFFFFFFFC9Fui64;
LABEL_73:
              v43 = v44 | 0x80;
            }
            else
            {
              v43 = 128i64;
              if( *(&stru_140C4DB30 + 42) )
              {
                if( *(&stru_140C4DB30 + 336) >= 0 )
                {
                  v44 = *(&stru_140C4DB30 + 42);
                  goto LABEL_73;
                }
                v43 = 144i64;
              }
            }
            *(_QWORD *)(PerfectColorHeadPage + 16) = v43;
LABEL_55:
            v11 = Colora;
            *(_QWORD *)PerfectColorHeadPage = 0i64;
            goto LABEL_56;
          }
          v35 = (__int64 *)v76;
        }
        v37 = *((_DWORD *)v35 + 1);
        if( PageTypea != v37 )
          continue;
        break;
      }
      v11 = Colora;
      LODWORD(BugCheckParameter4) = v67;
      PerfectColorHeadPage = MiGetPerfectColorHeadPage(
                               v21,
                               &v21->PageLists.FreePagesByColor[*(int *)v35][v79],
                               Colora,
                               (_MMLISTS)*(_DWORD *)v35,
                               BugCheckParameter4);
LABEL_56:
      if( PerfectColorHeadPage == 1 )
        return -1;
      if( PerfectColorHeadPage )
        goto LABEL_19;
      MiCheckZeroFreeRebalance((INT64)v21, v11);
      LODWORD(v39) = MiGetFreeOrZeroPageAnyColor(v21, v11, v10);
      PerfectColorHeadPage = v39;
      if( v39 == 1 )
        return -1;
      if( v39 )
        goto LABEL_19;
      LODWORD(v40) = MiGetFreeOrZeroPageAnyColor(v21, v11, v67);
      PerfectColorHeadPage = v40;
      if( v40 == 1 )
        return -1;
      if( v40 )
        goto LABEL_19;
      v5 = (_MI_PARTITION *)Partition;
      if( !MiPageAvailable((_MI_PARTITION *)Partition, v10) )
        return -1;
      v4 = &MiFreeThenFree;
      v14 = v74;
      v8 = v70 + 1;
      v70 = v8;
    }
    while( v8 != v80 );
    v3 = HIDWORD(SpinCount);
LABEL_146:
    if( (v10 & 0x4000) == 0 )
    {
      if( ++v7 != (unsigned __int16)KeNumberNodes && (v10 & 0x10) == 0 )
      {
        v12 = *(_DWORD *)(*(&stru_140C4DB30 + 21) + 4i64 * (v7 + v6 * (unsigned __int16)KeNumberNodes));
        if( v3 == 1 )
        {
          v8 = 0i64;
          v11 = v11 & *(&stru_140C4DB30 + 66) | (v12 << *(&stru_140C4DB30 + 156));
          Colora = v11;
          continue;
        }
        goto LABEL_3;
      }
      if( (v10 & 1) == 0 )
      {
        v22 = v66;
        if( (v10 & 0x30) == 0 || MiCheckNodeChannelStandbyCount((INT64)v5, v6, v66) )
        {
          LODWORD(v59) = MiRemoveLowestPriorityStandbyPage(v5, 8ui64, v10);
          if( v59 != -1 )
          {
            PerfectColorHeadPage = (INT64)&MmGetPfnDb()[v59];
            goto LABEL_20;
          }
        }
      }
    }
    return -1;
  }
}

Referenced by:

MiAllocateDriverPage
MiAllocateDummyPage
MiAllocateKernelStackPages
MiAllocateMdlPagesByLists
MiAllocateTopLevelPage
MiBuildForkPte
MiComputeOptimalZeroPath
MiCopyOnWrite
MiCreateUltraThreadContextHelper
MiDoneWithThisPageGetAnother
MiFillPerSessionProtos
MiGetFileHashPage
MiGetPageChain
MiGetPageTablePages
MiGetPoolPages
MiGetSystemPage
MiHandleForkTransitionPte
MiInitializeCacheFlushing
MiInitializeGapFrames
MiMakeOutswappedPageResident
MiMakePageAvoidRead
MiMigratePfn
MiPfPrepareReadList
MiPfPutPagesInTransition
MiPrivateFixup
MiPruneStandbyPages
MiReplaceLockedPage
MiResolveMappedFileFault
MiSplitDirectMapPage
MiSwapStackPage
MiSwitchToTransition
MiTradeTransitionPage
MmAllocateIndependentPagesEx
MmAllocateNonChargedSecurePages
MmCheckCachedPageStates
MxSwapPages