MiStoreWriteModifiedPages

NTSTATUS __stdcall MiStoreWriteModifiedPages(_MMMOD_WRITER_MDL_ENTRY *ModWriterEntry){
  _MMPAGING_FILE *v1; 
  _MDL *p_Mdl; 
  unsigned int v3; 
  _MMMOD_WRITER_MDL_ENTRY *v4; 
  _MI_PARTITION *Partition; 
  UINT64 ModifiedWriteDisableCount; 
  _MMPAGING_FILE *v7; 
  unsigned __int64 v8; 
  _MMPFN *v9; 
  INT64 v10; 
  INT64 v11; 
  UINT32 v12; 
  _MDL *p_Size; 
  UINT32 v14; 
  _MDL *Next; 
  int v16; 
  unsigned int v17; 
  unsigned int v18; 
  unsigned int v19; 
  int v20; 
  char *v21; 
  unsigned int v22; 
  unsigned int v23; 
  __int64 *v24; 
  __int64 i; 
  unsigned int v26; 
  NTSTATUS v27; 
  UINT64 v28; 
  UINT64 v29; 
  unsigned int v30; 
  NTSTATUS v31; 
  unsigned __int64 v32; 
  _MMPTE v33; 
  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;} v34; 
  int v35; 
  _RTL_BITMAP *ModifiedWriteFailedBitmap; 
  unsigned int *Buffer; 
  unsigned int v38; 
  unsigned int *v39; 
  bool j; 
  unsigned int v41; 
  unsigned __int64 v42; 
  INT64 v43; 
  INT64 bf_0; 
  _RTL_BITMAP *v45; 
  unsigned int v46; 
  unsigned int v47; 
  unsigned int v48; 
  int v49; 
  int v50; 
  char *v51; 
  unsigned int v52; 
  __int64 v53; 
  __int64 k; 
  char *v55; 
  unsigned __int64 v56; 
  __int64 v57; 
  _MMPTE v58; 
  unsigned int v59; 
  _ETHREAD *v60; 
  NTSTATUS v61; 
  _MI_PAGING_FILE_SPACE_BITMAPS *v62; 
  NTSTATUS result; 
  unsigned __int8 v64; 
  _MI_PAGING_FILE_SPACE_BITMAPS *v65; 
  unsigned int SizeOfBitMap; 
  int v67; 
  _MMPFN *v68; 
  unsigned __int64 v69; 
  VOID **OpaqueContext; 
  _GUID *v71; 
  INT64 v72; 
  INT64 v73; 
  INT64 v74; 
  unsigned __int8 v75; 
  INT64 v76; 
  _MMPAGING_FILE *PagingFile; 
  _MI_STORE_WRITE_SUPPORT *Pool; 
  INT64 v79; 
  INT64 v80; 
  _MDL *v81; 
  _MM_STORE_KEY StoreKeyOut; 
  _ETHREAD *Thread; 
  _MM_STORE_KEY_DESCRIPTOR StoreKeyDescriptorOut[4]; 
  UINT64 UnbiasedInterruptTime; 
  _MDL *v86; 
  _MDL *a3; 
  _MI_PAGING_FILE_SPACE_BITMAPS ReferencedBitmaps; 
  unsigned int v90; 
  unsigned int AllocationBitmapHint; 
  unsigned int RetryCount; 

  v1 = ModWriterEntry->PagingFile;
  p_Mdl = &ModWriterEntry->Mdl;
  v3 = ModWriterEntry->Mdl.ByteCount >> 12;
  v4 = ModWriterEntry;
  StoreKeyOut._bf_0 = 0i64;
  Partition = v1->Partition;
  *(_OWORD *)StoreKeyDescriptorOut = 0i64;
  v80 = 0i64;
  memset(&ReferencedBitmaps.AllocationBitmap, 0, 32);
  ModifiedWriteDisableCount = Partition->Store.ModifiedWriteDisableCount;
  a3 = p_Mdl;
  HIDWORD(v76) = v3;
  if( (_DWORD)ModifiedWriteDisableCount )
  {
    MiStoreLogWriteDisabled(ModifiedWriteDisableCount, v3);
    result = -1073741670;
    --Partition->Store.ModifiedWriteDisableCount;
    return result;
  }
  v7 = Partition->Vp.PagingFile[Partition->Store.VirtualPageFileNumber];
  PagingFile = v7;
  if( !v7->FreeSpace )
  {
    MiStoreLogFullPagefile();
    return -1073741697;
  }
  Thread = (_ETHREAD *)KeGetCurrentThread();
  --Thread->Tcb.KernelApcDisable;
  ReferencedBitmaps.Anchor = 0i64;
  v4->StoreWriteRefCount = 1;
  LOBYTE(v8) = 17;
  v9 = 0i64;
  AllocationBitmapHint = v7->AllocationBitmapHint;
  LODWORD(v76) = 0;
  Pool = 0i64;
  RetryCount = 0;
  v90 = 1;
  UnbiasedInterruptTime = KiQueryUnbiasedInterruptTime(1u);
  p_Size = p_Mdl + 1;
  v14 = 0;
  v86 = p_Mdl + 1;
LABEL_4:
  v81 = p_Size;
  HIDWORD(v74) = v14;
  while( v14 < v12 )
  {
    Next = p_Size->Next;
    v16 = 1;
    LODWORD(v74) = 1;
    if( p_Size->Next == (_MDL *)*(&stru_140C4DB30 + 495) || !v7->FreeSpace )
      goto LABEL_29;
    if( ReferencedBitmaps.Anchor != v7->Bitmaps )
    {
      if( ReferencedBitmaps.Anchor )
      {
        v65 = MiDerefPageFileSpaceBitmaps(v7, &ReferencedBitmaps, 1ui64);
        if( v65 )
          ExFreePoolWithTag(v65, 0);
      }
      MiRefPageFileSpaceBitmaps(
        v7,
        &ReferencedBitmaps,
        v10,
        v11,
        (INT64)OpaqueContext,
        (INT64)v71,
        v72,
        v73,
        v74,
        v76,
        (INT64)PagingFile,
        (INT64)Pool,
        v79,
        v80,
        (INT64)v81,
        StoreKeyOut._bf_0,
        (INT64)Thread,
        *(INT64 *)StoreKeyDescriptorOut);
    }
    v17 = AllocationBitmapHint;
    v18 = ReferencedBitmaps.AllocationBitmap.SizeOfBitMap - 1;
    v19 = AllocationBitmapHint < ReferencedBitmaps.AllocationBitmap.SizeOfBitMap ? AllocationBitmapHint : 0;
    v20 = ((__int64)ReferencedBitmaps.AllocationBitmap.Buffer & 4) != 0i64 ? 0x20 : 0;
    v21 = (char *)ReferencedBitmaps.AllocationBitmap.Buffer
        - (((__int64)ReferencedBitmaps.AllocationBitmap.Buffer & 4) != 0i64 ? 4 : 0);
    while( 1 )
    {
      LODWORD(v79) = 0;
      v22 = v18 + v20;
      v23 = v19 + v20;
      if( v18 - v19 == -1 )
      {
        v26 = -1;
      }
      else
      {
        v24 = (__int64 *)&v21[8 * ((unsigned __int64)v23 >> 6)];
        for( i = ((1i64 << (v23 & 0x3F)) - 1) | *v24; i == -1; i = *v24 )
        {
          if( ++v24 > (__int64 *)&v21[8 * ((unsigned __int64)v22 >> 6)] )
            goto LABEL_35;
        }
        _BitScanForward64((unsigned __int64 *)&i, ~i);
        v26 = i + ((unsigned int)(((char *)v24 - v21) >> 3) << 6);
        LODWORD(v79) = i;
        if( v26 > v22 )
        {
LABEL_35:
          v26 = -1;
          goto LABEL_36;
        }
        if( v26 != -1 )
        {
          v26 -= v20;
          break;
        }
LABEL_36:
        v17 = AllocationBitmapHint;
      }
      if( !v19 )
        break;
      SizeOfBitMap = v17 + 1;
      if( v17 + 1 > ReferencedBitmaps.AllocationBitmap.SizeOfBitMap )
        SizeOfBitMap = ReferencedBitmaps.AllocationBitmap.SizeOfBitMap;
      v18 = SizeOfBitMap - 1;
      v19 = 0;
    }
    v9 = &MmGetPfnDb()[(_QWORD)Next];
    v8 = MiLockPageInline(v9);
    if( (v9->u2._bf_0 & 0x4000000000000000i64) != 0 )
      goto LABEL_80;
    v80 = 0i64;
    v27 = MiStoreCheckCandidatePage(v9, v26, &StoreKeyOut, StoreKeyDescriptorOut, (VOID **)&v80);
    if( v27 < 0 )
    {
      MiStoreLogNotCandidate((unsigned int)v27, v26, v28, v29, (_GUID *)OpaqueContext, v71);
LABEL_80:
      v16 = v74;
LABEL_29:
      if( (_BYTE)v8 != 17 )
      {
        _InterlockedAnd64(&v9->u2.Lock, 0x7FFFFFFFFFFFFFFFui64);
        __writecr8((unsigned __int8)v8);
        LOBYTE(v8) = 17;
      }
      if( v16 )
      {
LABEL_34:
        v35 = HIDWORD(v74);
        _bittestandset((signed __int32 *)Partition->Store.ModifiedWriteFailedBitmap->Buffer, HIDWORD(v74));
      }
      else
      {
        v35 = HIDWORD(v74);
      }
      v14 = v35 + 1;
      v12 = HIDWORD(v76);
      p_Size = (_MDL *)&v81->Size;
      RetryCount = 0;
      v4 = ModWriterEntry;
      v7 = PagingFile;
      goto LABEL_4;
    }
    _InterlockedAnd64(&v9->u2.Lock, 0x7FFFFFFFFFFFFFFFui64);
    __writecr8(v8);
    LOBYTE(v8) = 17;
    if( !Pool )
    {
      Pool = (_MI_STORE_WRITE_SUPPORT *)RtlpInterlockedPopEntrySList(&Partition->Store.WriteSupportSListHead);
      if( !Pool )
      {
        Pool = (_MI_STORE_WRITE_SUPPORT *)MiAllocatePool(64i64, 0x78ui64, 0x57536D4Dui64);
        if( !Pool )
        {
          LODWORD(v76) = 1;
          goto LABEL_34;
        }
      }
    }
    if( PagingFile->FreeSpace - 1 < 8 )
      v30 = LODWORD(PagingFile->FreeSpace) - 1;
    else
      v30 = 8;
    if( v90 )
    {
      if( MiSufficientAvailablePages((INT64)Partition, 0x120ui64) )
      {
        v90 &= -(KiQueryUnbiasedInterruptTime(1u) - UnbiasedInterruptTime < 0x2FAF080);
      }
      else
      {
        v90 = 0;
        if( v30 >= 3 )
          v30 = 3;
      }
    }
    v4 = ModWriterEntry;
    _InterlockedIncrement((volatile signed __int32 *)&ModWriterEntry->StoreWriteRefCount);
    LODWORD(v73) = v90;
    v31 = MiStoreWriteIssue(
            (INT64)Pool,
            (INT64)v9,
            &StoreKeyOut,
            (INT64)StoreKeyDescriptorOut,
            (INT64)Pool,
            v80,
            (INT64)ModWriterEntry,
            v73);
    if( v31 >= 0 )
    {
      v75 = 0;
      Pool = 0i64;
      v8 = (unsigned __int8)ExAcquireSpinLockExclusive((INT64 *)&PagingFile->SpinLock);
      AllocationBitmapHint = v26 + 1;
      _bittestandset((signed __int32 *)PagingFile->Bitmaps->AllocationBitmap.Buffer, v26);
      --PagingFile->FreeSpace;
      PagingFile->AllocationBitmapHint = v26 + 1;
      MiUpdatePagefilePeakUsage(PagingFile);
      ExReleaseSpinLockExclusiveFromDpcLevel((INT64 *)&PagingFile->SpinLock);
      __writecr8(v8);
      v32 = MiLockPageInline(v9);
      v33.u.Long = MiCapturePageFileInfoInline(&v9->OriginalPte, 1ui64, 1ui64).u.Long;
      v9->OriginalPte.u.Long &= ~2ui64;
      v34.Long = v33.u.Long;
      MiUpdatePfnBackingStore(v9, PagingFile, v26, 1ui64);
      _InterlockedAnd64(&v9->u2.Lock, 0x7FFFFFFFFFFFFFFFui64);
      __writecr8(v32);
      LOBYTE(v8) = 17;
      MiReleasePageFileInfo(Partition, (_MMPTE)v34.Long, 2ui64);
      v16 = v75;
      goto LABEL_29;
    }
    _InterlockedDecrement((volatile signed __int32 *)&ModWriterEntry->StoreWriteRefCount);
    if( v31 != -1073740682 )
    {
      v67 = v76;
      if( v31 == -1073741670 )
        v67 = 1;
      LODWORD(v76) = v67;
LABEL_99:
      LODWORD(v71) = ++Partition->Store.WriteIssueFailures;
      LODWORD(OpaqueContext) = v90;
      MiStoreLogWriteIssueFailure(
        (unsigned int)v31,
        (INT64)StoreKeyDescriptorOut,
        v26,
        RetryCount,
        (INT64)OpaqueContext,
        (INT64)v71);
      goto LABEL_34;
    }
    AllocationBitmapHint = v26 + 1;
    if( RetryCount >= v30 )
      goto LABEL_99;
    LODWORD(OpaqueContext) = Partition->Store.WriteIssueFailures;
    MiStoreLogWriteIssueRetry(StoreKeyDescriptorOut, v26, RetryCount, v90, (UINT64)OpaqueContext);
    v12 = HIDWORD(v76);
    v7 = PagingFile;
    v14 = HIDWORD(v74);
    p_Size = v81;
    ++RetryCount;
  }
  if( (_DWORD)v76 )
    Partition->Store.ModifiedWriteDisableCount = 32;
  ModifiedWriteFailedBitmap = Partition->Store.ModifiedWriteFailedBitmap;
  if( ModifiedWriteFailedBitmap->SizeOfBitMap )
  {
    if( v12 <= 1 )
    {
      if( v12 == 1 )
      {
        v64 = _bittest((const signed __int32 *)ModifiedWriteFailedBitmap->Buffer, 0);
        goto LABEL_77;
      }
    }
    else if( ModifiedWriteFailedBitmap->SizeOfBitMap >= v12 )
    {
      Buffer = ModifiedWriteFailedBitmap->Buffer;
      v38 = *Buffer;
      v39 = &Buffer[(unsigned __int64)(v12 - 1) >> 5];
      if( Buffer != v39 )
      {
        for( j = v38 == -1; ; j = *Buffer == -1 )
        {
          if( !j )
            goto LABEL_47;
          if( ++Buffer == v39 )
            break;
        }
        if( ((0xFFFFFFFF >> ~(v12 - 1)) & *Buffer) != 0xFFFFFFFF >> ~(v12 - 1) )
        {
          v64 = 0;
LABEL_77:
          if( v64 )
            goto LABEL_74;
          goto LABEL_47;
        }
LABEL_74:
        RtlClearBits(Partition->Store.ModifiedWriteFailedBitmap, 0, v12);
        v4->StoreWriteRefCount = 0;
        v61 = -1073740759;
        v60 = Thread;
        goto LABEL_67;
      }
      if( (v38 & (0xFFFFFFFF >> (32 - v12))) == 0xFFFFFFFF >> (32 - v12) )
        goto LABEL_74;
    }
  }
LABEL_47:
  v41 = -1;
  v42 = (unsigned __int64)v4->WriteOffset.QuadPart >> 12;
  LODWORD(v43) = MiTransferSoftwarePte(ZeroPte.u.Long, (INT64)v4->PagingFile, (unsigned int)v42);
  bf_0 = v43;
  while( 2 )
  {
    v45 = Partition->Store.ModifiedWriteFailedBitmap;
    v46 = v41 + 1;
    v47 = v45->SizeOfBitMap;
    v48 = v46 < v45->SizeOfBitMap ? v46 : 0;
    v49 = v45->SizeOfBitMap - 1;
    v50 = ((__int64)v45->Buffer & 4) != 0i64 ? 0x20 : 0;
    v51 = (char *)v45->Buffer - (((__int64)v45->Buffer & 4) != 0 ? 4 : 0);
    while( 1 )
    {
      v52 = v49 + v50;
      v53 = 1i64;
      k = v48 + v50;
      if( v52 - (_DWORD)k != -1 )
      {
        v55 = &v51[8 * ((unsigned __int64)(unsigned int)k >> 6)];
        v53 = ~*(_QWORD *)v55 | ((1i64 << ((v48 + v50) & 0x3F)) - 1);
        for( k = -1i64; v53 == -1; v53 = ~*(_QWORD *)v55 )
        {
          v55 += 8;
          if( v55 > &v51[8 * ((unsigned __int64)v52 >> 6)] )
            goto LABEL_59;
        }
        v53 = ~v53;
        _BitScanForward64(&v56, v53);
        v41 = v56 + ((unsigned int)((v55 - v51) >> 3) << 6);
        if( v41 <= v52 )
          break;
      }
LABEL_59:
      v41 = -1;
LABEL_60:
      if( !v48 )
        goto LABEL_56;
      v59 = v46 + 1;
      if( v46 + 1 > v47 )
        v59 = v47;
      v49 = v59 - 1;
      v48 = 0;
    }
    if( v41 == -1 )
      goto LABEL_60;
    v41 -= v50;
LABEL_56:
    if( v41 != -1 )
    {
      _bittestandreset((signed __int32 *)Partition->Store.ModifiedWriteFailedBitmap->Buffer, v41);
      v57 = *((_QWORD *)&v86->Next + v41);
      if( v57 == *(&stru_140C4DB30 + 495) )
      {
        LODWORD(v58.u.Long) = MiUpdatePageFileHighInPte(bf_0, v41 + (unsigned int)v42);
        bf_0 = v58.u.Hard._bf_0;
        MiReleasePageFileInfo(Partition, v58, 2ui64);
      }
      else
      {
        v68 = &MmGetPfnDb()[v57];
        v69 = MiLockPageInline(v68);
        *((_BYTE *)&v68->u3 + 2) |= 0x10u;
        _InterlockedAnd64(&v68->u2.Lock, 0x7FFFFFFFFFFFFFFFui64);
        __writecr8(v69);
      }
      continue;
    }
    break;
  }
  MiStoreUpdatePagefileHash((_MDL *)k, v53, a3, HIDWORD(v76));
  v60 = Thread;
  *(_DWORD *)&ModWriterEntry->u1 |= 0x20u;
  --v60->Tcb.SpecialApcDisable;
  ++Partition->Store.WritesOutstanding;
  KiLeaveGuardedRegionUnsafe((__int64)v60);
  MiStoreModifiedWriteDereference(ModWriterEntry);
  v7 = PagingFile;
  v61 = 259;
LABEL_67:
  KeLeaveCriticalRegionThread(&v60->Tcb);
  if( Pool )
    MiStoreFreeWriteSupport(Pool, Partition);
  if( ReferencedBitmaps.Anchor )
  {
    v62 = MiDerefPageFileSpaceBitmaps(v7, &ReferencedBitmaps, 0i64);
    if( v62 )
      ExFreePoolWithTag(v62, 0);
  }
  return v61;
}

Referenced by:

MiGatherPagefilePages