MiMapSystemImage

NTSTATUS __stdcall MiMapSystemImage(_SECTION *Section, PVOID MappedBase, UINT64 LoadFlags){
  INT64 v6; 
  _CONTROL_AREA *v7; 
  UINT64 v8; 
  char v9; 
  int v10; 
  unsigned int v11; 
  NTSTATUS result; 
  _MMPTE *PteAddress; 
  char *v14; 
  NTSTATUS v15; 
  __int64 v16; 
  __int64 v17; 
  unsigned int v18; 
  MMPTE *v19; 
  int v20; 
  MMPTE *v21; 
  UINT64 SessionId; 
  _MMPTE *v23; 
  _LARGE_INTEGER SectionOffset; 
  SectionOffset.QuadPart = 0i64;
  v7 = MiSectionControlArea(Section);
  v8 = *(unsigned int *)(*(_QWORD *)v7 + 8i64);
  if( (v9 & 1) == 0 )
  {
    v10 = 1;
    v11 = -1;
    result = MiChargeSystemImageCommitment(v6);
    if( result < 0 )
      return result;
LABEL_3:
    PteAddress = MiGetPteAddress(MappedBase);
    v14 = 0i64;
    v23 = PteAddress;
    if( LoadFlags )
    {
      v14 = (char *)MappedBase + 4096 * (unsigned __int64)*(unsigned int *)(*(_QWORD *)v7 + 8i64);
      v15 = MiMapPatchTable((UINT64)v14, LoadFlags);
      if( v15 < 0 )
      {
LABEL_23:
        if( v10 == 1 )
        {
          MiChargeSystemImageCommitment((INT64)Section);
        }
        else if( (*((_DWORD *)v7 + 14) & 0x4000000) != 0 )
        {
          MiDereferencePerSessionProtos((__int64 *)v7, v11);
        }
        if( (unsigned int)MiGetSystemRegionType((UINT64)MappedBase) == 1
          && MappedBase != *(PVOID *)(*(_QWORD *)v7 + 32i64) )
        {
          MiDeleteSessionDriverProtos((__int64 *)v7);
        }
        return v15;
      }
      PteAddress = v23;
    }
    LODWORD(SessionId) = v11;
    v15 = MiAddMappedPtes(PteAddress, v8, v7, &SectionOffset, SessionId);
    if( v15 >= 0 )
    {
      if( v10 == 1 )
        _InterlockedExchangeAdd((_DWORD *)&xmmword_140C4CA88 + 3, v8);
      return 0;
    }
    if( v14 )
    {
      v21 = MiGetPteAddress(v14);
      MiUnmapPatchTable((UINT64)v21);
    }
    goto LABEL_23;
  }
  LOBYTE(v16) = MiBytesToMapSystemImage((_FAST_MUTEX *)(v8 << 12));
  v17 = v16;
  if( !v16 )
    return -1073741793;
  v10 = 0;
  v18 = MmGetSessionIdEx(*((_QWORD *)KeGetCurrentThread() + 23));
  v11 = v18;
  if( (*((_DWORD *)v7 + 14) & 0x4000000) == 0 || (result = MiCreatePerSessionProtos(v7, v18), result >= 0) )
  {
    MiGetPteAddress((char *)MappedBase + v17 - 1);
    v19 = MiGetPteAddress(MappedBase);
    LOBYTE(v20) = MiMakeZeroedPageTables(v19);
    if( v20 )
      goto LABEL_3;
    if( (*((_DWORD *)v7 + 14) & 0x4000000) != 0 )
      MiDereferencePerSessionProtos((__int64 *)v7, v11);
    return -1073741801;
  }
  return result;
}

Referenced by:

MiApplyHotPatchToLoadedDriver
MmLoadSystemImageEx