MiFreeInitializationCode

NTSTATUS __stdcall MiFreeInitializationCode(_KLDR_DATA_TABLE_ENTRY *DataTableEntry, VOID *StartVa, VOID *EndVa){
  int v3; 
  int v4; 
  VOID *DllBase; 
  _KLDR_DATA_TABLE_ENTRY *v7; 
  _KLDR_DATA_TABLE_ENTRY *v8; 
  __int64 v9; 
  unsigned __int64 v10; 
  _MMPTE *PteAddress; 
  _MI_PARTITION *ControlAreaPartition; 
  NTSTATUS result; 
  VOID *v14; 
  _SECTION *SectionPointer; 
  _CONTROL_AREA *v16; 
  _EPROCESS *v17; 
  _MMSUPPORT_INSTANCE *AnyMultiplexedVm; 
  _CONTROL_AREA *v19; 
  UINT64 v20; 
  INT64 v21; 
  _MMPFN *v22; 
  UINT64 v23; 
  UINT64 Flags; 
  _MI_DELETE_VA_INFO DeleteInfo; 
  __int128 v26; 
  __int128 v27; 

  v4 = v3;
  DllBase = DataTableEntry;
  DeleteInfo = 0i64;
  v26 = 0i64;
  v27 = 0i64;
  if( v3 )
  {
    v8 = 0i64;
    v7 = 0i64;
  }
  else
  {
    DllBase = DataTableEntry->DllBase;
    v7 = DataTableEntry + 1;
    v8 = DataTableEntry;
  }
  v9 = (__int64)(((_QWORD)StartVa << 25) - ((_QWORD)MmGetPteBase() << 25)) >> 16;
  v10 = ((_BYTE *)EndVa - (_BYTE *)StartVa + 8) >> 3;
  if( !v3 )
  {
    MiLockLoaderEntry((INT64)v7, 0i64);
    PteAddress = MiGetPteAddress(DllBase);
    RtlClearBits((PRTL_BITMAP)v7->SectionPointer, ((_BYTE *)StartVa - (_BYTE *)PteAddress) >> 3, v10);
    MiUnlockLoaderEntry((INT64)v7, 0i64);
  }
  ControlAreaPartition = &Irp;
  result = MI_IS_PHYSICAL_ADDRESS((VOID *)v9);
  if( result )
  {
    for( ; v10; v10 -= v23 )
    {
      LODWORD(v21) = MiVaToPfn((VOID *)v9);
      v22 = &MmGetPfnDb()[v21];
      if( v22->u3.ReferenceCount != 1 )
        MiBadRefCount(v22);
      v23 = v10;
      if( 512 - (unsigned __int64)(v21 & 0x1FF) <= v10 )
        v23 = 512 - (v21 & 0x1FF);
      MiFreeLargeInitializationCodePages((INT64)v22, v21, v23);
      result = (_DWORD)v23 << 12;
      v9 += v23 << 12;
    }
  }
  else if( v4 )
  {
    result = MiFreeBootDriverPages((INT64)DllBase, (INT64)StartVa, v10, 1i64, 0i64);
    if( DllBase == (VOID *)PsNtosImageBase )
      MxKernelFreedGapCharges += v10;
    else
      MxHalFreedGapCharges += v10;
  }
  else
  {
    SectionPointer = (_SECTION *)v8->SectionPointer;
    if( SectionPointer )
    {
      v16 = MiSectionControlArea(SectionPointer);
      ControlAreaPartition = MiGetControlAreaPartition(v16, v17);
    }
    AnyMultiplexedVm = MiGetAnyMultiplexedVm(MiWorkingSetTypeSystemViews, v14);
    LODWORD(Flags) = 1;
    MiDeleteSystemPagableVm(AnyMultiplexedVm, v19, (_MMPTE *)StartVa, v10, Flags, &DeleteInfo);
  }
  v20 = *((_QWORD *)&v26 + 1);
  if( *((_QWORD *)&v26 + 1) )
  {
    v7->NonPagedDebugInfo = (_NON_PAGED_DEBUG_INFO *)((char *)v7->NonPagedDebugInfo - *((_QWORD *)&v26 + 1));
    v7->DllBase = (char *)v7->DllBase - v20;
    if( DllBase == PsHalImageBase || DllBase == (VOID *)PsNtosImageBase )
      *(&stru_140C4DB30 + 568) -= *((_QWORD *)&v26 + 1);
    else
      _InterlockedExchangeAdd((_DWORD *)&stru_140C4DB30 + 1144, -(int)v20);
    MiReturnResident((INT64)ControlAreaPartition, v20);
    MiReturnCommit(ControlAreaPartition, v20 - *(_QWORD *)&DeleteInfo.ReducedCommitClonePages);
  }
  return result;
}

Referenced by:

MiFreeDriverInitialization
MiFreeKernelPadSections
MmDiscardDriverSection