MiRemovePhysicalMemory

VOID __stdcall MiRemovePhysicalMemory(UINT64 StartPage, UINT64 NumberOfPages, UINT64 Flags){
  UINT64 DanglingExtent; 
  _ETHREAD *CurrentThread; 
  int v5; 
  char v6; 
  _PHYSICAL_MEMORY_DESCRIPTOR *v7; 
  int v8; 
  UINT64 v9; 
  _PHYSICAL_MEMORY_DESCRIPTOR *i; 
  _QWORD *v11; 
  __int64 v12; 
  unsigned int v13; 
  char v14; 
  _BYTE *v15; 
  INT64 v16; 
  _PHYSICAL_MEMORY_RUN *Run; 
  __int64 v18; 
  UINT64 v19; 
  _PHYSICAL_MEMORY_DESCRIPTOR *NewPageRunBlock; 
  _PHYSICAL_MEMORY_DESCRIPTOR *NewPhysicalMemoryBlock; 
  _ETHREAD *Thread; 
  PVOID P[3]; 
  __int64 v24[12]; 
  UINT64 NumberOfPagesa; 
  UINT64 Flagsa; 
  int v27; 
  LODWORD(Flagsa) = Flags;
  NumberOfPagesa = NumberOfPages;
  DanglingExtent = StartPage;
  P[1] = P;
  P[2] = 0i64;
  P[0] = P;
  CurrentThread = (_ETHREAD *)KeGetCurrentThread();
  v5 = Flags & 2;
  Thread = CurrentThread;
  v24[1] = 0i64;
  v6 = Flags;
  v24[4] = 0i64;
  NewPageRunBlock = 0i64;
  v7 = 0i64;
  NewPhysicalMemoryBlock = 0i64;
  v27 = v5;
  if( (Flags & 2) != 0 )
  {
    v8 = 0;
    MiLockDynamicMemoryExclusive((__int64)&MiSystemPartition, (__int64)CurrentThread);
    if( (v6 & 0x20) != 0 )
    {
      DanglingExtent = MiGetDanglingExtent(&NumberOfPagesa);
      if( DanglingExtent == -1i64 )
      {
        MiUnlockDynamicMemoryExclusive((_MI_PARTITION *)&MiSystemPartition, CurrentThread);
        return;
      }
    }
    v9 = NumberOfPagesa;
    goto LABEL_20;
  }
  v9 = NumberOfPagesa;
  v24[0] = 0i64;
  v24[3] = NumberOfPagesa;
  v24[2] = StartPage;
  MiLockDynamicMemoryExclusive((__int64)&MiSystemPartition, (__int64)CurrentThread);
  v8 = MiConfigureMemoryRemoval(&NewPhysicalMemoryBlock, (unsigned int *)MmPhysicalMemoryBlock, v24);
  if( v8 < 0 )
  {
LABEL_30:
    v14 = Flagsa;
    goto LABEL_31;
  }
  v7 = MiReferencePageRuns((_MI_PARTITION *)&MiSystemPartition, 1ui64);
  if( !(unsigned int)MiDescribePageRun(P, DanglingExtent, v9) )
  {
    v8 = -1073741670;
    goto LABEL_30;
  }
  for( i = v7; ; i = NewPageRunBlock )
  {
    v11 = P[0];
    if( P[0] == P )
      break;
    if( *((PVOID **)P[0] + 1) != P || (v12 = *(_QWORD *)P[0], *(PVOID *)(*(_QWORD *)P[0] + 8i64) != P[0]) )
LABEL_40:
      __fastfail(3u);
    P[0] = *(PVOID *)P[0];
    *(_QWORD *)(v12 + 8) = P;
    v8 = MiConfigureMemoryRemoval(&NewPageRunBlock, &i->NumberOfRuns, v11);
    ExFreePoolWithTag(v11, 0);
    if( i != v7 )
      ExFreePoolWithTag(i[-1].Run, 0);
    if( v8 < 0 )
    {
      v5 = v27;
      goto LABEL_30;
    }
  }
  MiReduceCommitLimits((_MI_PARTITION *)&MiSystemPartition, v9, v9);
  MiReturnCommit((_MI_PARTITION *)&MiSystemPartition, v9);
  LODWORD(v19) = 0;
  MiPerformMemoryChange(DanglingExtent, v9, &NewPhysicalMemoryBlock, &NewPageRunBlock, v19);
  MiComputeNodeMemory((_MI_PARTITION *)&MiSystemPartition);
  v13 = -1;
  if( qword_140C525D0 <= 0xFFFFFFFF )
    v13 = qword_140C525D0;
  KUSER_SHARED_DATA.NumberOfPhysicalPages = v13;
  KeConfigureDynamicMemory(DanglingExtent, DanglingExtent + v9 - 1, 2ui64);
  v5 = v27;
LABEL_20:
  v14 = Flagsa;
  MiInitializeDynamicPfns(DanglingExtent, v9, (UINT16)&MiSystemPartition, (unsigned int)Flagsa);
  qword_140C50B10 += v9;
  if( (unsigned __int64)qword_140C50B10 >= 0x5555 && !byte_140C50B8C )
  {
    stru_140C50AF0.List.Flink = 0i64;
    stru_140C50AF0.WorkerRoutine = (void(__fastcall *)(void *))MiFreeUnusedPfnPages;
    stru_140C50AF0.Parameter = &MiSystemPartition;
    ExQueueWorkItem(&stru_140C50AF0, DelayedWorkQueue);
    byte_140C50B8C = 1;
  }
  if( !v5 )
  {
    if( (MiFlags & 0x10000000) != 0 && (MiFlags & 0x30u) >= 0x20 )
      ZwUpdateWnfStateData();
    KePulseEvent(qword_140C50C08, 0);
    MiFlushEntireTbDueToAttributeChange(v16, v15);
    MiFlushCacheRange(DanglingExtent, v9);
  }
LABEL_31:
  MiUnlockDynamicMemoryExclusive((_MI_PARTITION *)&MiSystemPartition, Thread);
  if( NewPhysicalMemoryBlock )
    ExFreePoolWithTag(NewPhysicalMemoryBlock[-1].Run, 0);
  if( !NewPageRunBlock )
    goto LABEL_36;
  Run = NewPageRunBlock[-1].Run;
  while( 1 )
  {
    ExFreePoolWithTag(Run, 0);
LABEL_36:
    Run = (_PHYSICAL_MEMORY_RUN *)P[0];
    if( P[0] == P )
      break;
    if( *((PVOID **)P[0] + 1) != P )
      goto LABEL_40;
    v18 = *(_QWORD *)P[0];
    if( *(PVOID *)(*(_QWORD *)P[0] + 8i64) != P[0] )
      goto LABEL_40;
    P[0] = *(PVOID *)P[0];
    *(_QWORD *)(v18 + 8) = P;
  }
  if( v7 )
    MiDereferencePageRuns(v7);
  if( v8 >= 0 && (v14 & 8) == 0 && !v5 )
    IoUpdateDumpPhysicalRanges();
}

Referenced by:

MiAllocateFileExtents
MiDeleteExtentPfns
MiHotRemovePartitionPageRun
MiRemoveMdlPages
MmRemovePhysicalMemory