MiReplenishBitMap

NTSTATUS __stdcall MiReplenishBitMap(_MI_SYSTEM_PTE_TYPE *PteInfo, UINT64 StartBit, UINT64 FlushTbNeeded){
  int v3; 
  UINT64 v4; 
  _MI_SYSTEM_PTE_TYPE *v5; 
  NTSTATUS v6; 
  unsigned int v7; 
  int v8; 
  _MMPTE *Pml4eBase; 
  UINT64 v10; 
  unsigned __int64 BugCheckParameter4; 
  _MMPTE *BasePte; 
  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;} v13; 
  _MMPTE *v14; 
  __int64 Long_high; 
  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;} v16; 
  unsigned __int64 v17; 
  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;} v18; 
  _MMPTE *v19; 
  __int64 v20; 
  unsigned __int64 Long; 
  unsigned __int64 *Buffer; 
  unsigned __int64 v23; 
  unsigned __int64 *v24; 
  unsigned __int64 *v25; 
  bool v26; 
  unsigned __int64 *v27; 
  int v28; 
  UINT64 v29; 
  _MMPTE *PxeUserLimit; 
  unsigned __int64 v31; 
  unsigned __int64 v32; 
  unsigned __int64 v33; 
  volatile signed __int32 *v34; 
  int v35; 
  unsigned __int64 v36; 
  signed __int32 v38[8]; 
  int v39; 
  _MI_SYSTEM_PTE_TYPE *v40; 
  UINT64 v41; 
  _MI_TB_FLUSH_LIST TbFlushList[48]; 

  v3 = FlushTbNeeded;
  v41 = StartBit;
  v4 = StartBit;
  v40 = PteInfo;
  v5 = PteInfo;
  memset(TbFlushList, 0i64, 0xB8u);
  v6 = 0;
  v7 = 0;
  if( v3 == 1 )
  {
    TbFlushList[2] = 20;
    *(_QWORD *)&TbFlushList[6] = 0i64;
    _InterlockedOr(v38, 0);
    v8 = KiTbFlushTimeStamp;
  }
  else
  {
    v7 = 1;
    v8 = 0;
  }
  v39 = v8;
  Pml4eBase = MmGetPml4eBase();
  do
  {
    v10 = 0i64;
    BugCheckParameter4 = v4;
    do
    {
      BasePte = v5->BasePte;
      v13.Long = (unsigned __int64)BasePte[BugCheckParameter4].u;
      v14 = &BasePte[BugCheckParameter4];
      if( (v13.Long & 0xF000) == 4096i64 )
      {
        Long_high = 1i64;
      }
      else
      {
        v16.Long = (unsigned __int64)v14[1].u;
        if( *(&stru_140C4DB30 + 42) && (v16.Long & 0x10) == 0 )
          v16.Long &= ~*(&stru_140C4DB30 + 42);
        Long_high = HIDWORD(v16.Long);
      }
      v10 += Long_high;
      if( *(&stru_140C4DB30 + 42) && (v13.Long & 0x10) == 0 )
        v13.Long &= ~*(&stru_140C4DB30 + 42);
      v17 = HIDWORD(v13.Long);
      if( !v17 )
        goto LABEL_23;
      v18.Long = (unsigned __int64)BasePte[v17].u;
      v19 = &BasePte[v17];
      if( (v18.Long & 0xF000) == 4096i64 )
      {
        v20 = 1i64;
      }
      else
      {
        Long = v19[1].u.Long;
        if( *(&stru_140C4DB30 + 42) && (Long & 0x10) == 0 )
          Long &= ~*(&stru_140C4DB30 + 42);
        v20 = HIDWORD(Long);
      }
      if( v17 + v20 != BugCheckParameter4 )
      {
LABEL_23:
        if( BugCheckParameter4 >= v5->Bitmap.SizeOfBitMap )
          goto LABEL_74;
        if( v10 > 1 )
        {
          if( v5->Bitmap.SizeOfBitMap - BugCheckParameter4 < v10 )
            goto LABEL_74;
          Buffer = v5->Bitmap.Buffer;
          v23 = Buffer[BugCheckParameter4 >> 6];
          v24 = &Buffer[BugCheckParameter4 >> 6];
          v25 = &Buffer[(v10 + BugCheckParameter4 - 1) >> 6];
          if( v24 == v25 )
          {
            v26 = ((0xFFFFFFFFFFFFFFFFui64 >> (64 - (unsigned __int8)v10) << BugCheckParameter4) & v23) == 0xFFFFFFFFFFFFFFFFui64 >> (64 - (unsigned __int8)v10) << BugCheckParameter4;
          }
          else
          {
            if( ((-1i64 << BugCheckParameter4) & v23) != -1i64 << BugCheckParameter4 )
              goto LABEL_74;
            v27 = v24 + 1;
            if( v27 != v25 )
            {
              while( *v27 == -1i64 )
              {
                if( ++v27 == v25 )
                  goto LABEL_35;
              }
LABEL_74:
              KeBugCheckEx(0xDAu, (PVOID)0x504, v14, (PVOID)v10, (PVOID)BugCheckParameter4);
            }
LABEL_35:
            v26 = ((0xFFFFFFFFFFFFFFFFui64 >> ~((unsigned __int8)v10 + (unsigned __int8)BugCheckParameter4 - 1)) & *v27) == 0xFFFFFFFFFFFFFFFFui64 >> ~((unsigned __int8)v10 + (unsigned __int8)BugCheckParameter4 - 1);
          }
          if( !v26 )
            goto LABEL_74;
          Pml4eBase = MmGetPml4eBase();
        }
        else if( v10 != 1 || !_bittest64((const signed __int64 *)v5->Bitmap.Buffer, BugCheckParameter4) )
        {
          goto LABEL_74;
        }
        if( !v7 )
        {
          MiInsertTbFlushEntry(
            TbFlushList,
            (VOID *)((__int64)(((_QWORD)v14 << 25) - ((_QWORD)MmGetPteBase() << 25)) >> 16),
            v10,
            0i64);
          MiFlushTbListEarly(TbFlushList, (_BYTE *)1);
          if( v28 == 1 )
          {
            _InterlockedOr(v38, 0);
            if( (unsigned int)(KiTbFlushTimeStamp - v8) > 2
              || (v8 & 1) == 0 && (unsigned int)(KiTbFlushTimeStamp - v8) >= 2 )
            {
              goto LABEL_70;
            }
          }
          goto LABEL_67;
        }
        v29 = v10;
        PxeUserLimit = MmGetPxeUserLimit();
        do
        {
          v31 = ZeroPte.u.Long;
          if( v14 >= Pml4eBase
            && v14 <= PxeUserLimit
            && (KeGetCurrentThread()->ApcState.Process->Flags3 & 0x1000) != 0
            && (ZeroPte.u.Long & 1) != 0 )
          {
            v31 = ZeroPte.u.Long | 0x8000000000000000ui64;
          }
          v14->u.Long = v31;
          ++v14;
          --v29;
        }
        while( v29 );
        v5 = v40;
        v8 = v39;
        v32 = BugCheckParameter4 & 0x1F;
        v33 = v10;
        v34 = (volatile signed __int32 *)v40->Bitmap.Buffer + (BugCheckParameter4 >> 5);
        if( v32 + v10 > 0x20 )
        {
          if( (BugCheckParameter4 & 0x1F) != 0 )
          {
            _InterlockedAnd(v34, ~(((1 << (32 - (BugCheckParameter4 & 0x1F))) - 1) << v32));
            v33 = v10 - (32 - (unsigned int)(BugCheckParameter4 & 0x1F));
            ++v34;
          }
          if( v33 >= 0x20 )
          {
            v36 = v33 >> 5;
            v33 += -32i64 * (v33 >> 5);
            do
            {
              *v34++ = 0;
              --v36;
            }
            while( v36 );
          }
          if( !v33 )
            goto LABEL_63;
          v35 = -1 << v33;
        }
        else
        {
          if( v10 == 32 )
          {
            *v34 = 0;
            goto LABEL_63;
          }
          v35 = ~(((1 << v10) - 1) << v32);
        }
        _InterlockedAnd(v34, v35);
LABEL_63:
        if( !MiIsPteCachingOptimizationEnabled() || v5->TotalFreeSystemPtes > 0x40000 )
          MiAttemptCoalesce(v5, BugCheckParameter4, v10);
        v6 += v10;
LABEL_67:
        v10 = 0i64;
        Pml4eBase = MmGetPml4eBase();
      }
      BugCheckParameter4 = v17;
    }
    while( v17 );
    if( !v7 )
    {
LABEL_70:
      if( TbFlushList[3] )
        MiFlushTbList(TbFlushList);
    }
    v4 = v41;
    ++v7;
    Pml4eBase = MmGetPml4eBase();
  }
  while( v7 < 2 );
  return v6;
}

Referenced by:

MiEmptyPteBins
MiInsertCachedPte