MiAddMappedPtes

NTSTATUS __stdcall MiAddMappedPtes(
        _MMPTE *FirstPte,
        UINT64 NumberOfPtes,
        _CONTROL_AREA *ControlArea,
        _LARGE_INTEGER *SectionOffset,
        UINT64 SessionId){
  int v5; 
  _CONTROL_AREA *v6; 
  _MMPTE *v7; 
  _MMPTE *v8; 
  __int64 DemandZeroPte; 
  _SUBSECTION *v10; 
  _SUBSECTION *v11; 
  unsigned __int16 v12; 
  _MMPTE *SubsectionBase; 
  unsigned __int64 v14; 
  unsigned __int64 v15; 
  __int64 v16; 
  __int64 v17; 
  int v18; 
  __int16 bf_2; 
  union {unsigned int LongFlags;_MMSECTION_FLAGS Flags;} v21; 
  _MI_PER_SESSION_PROTOS *SubsectionDriverProtos; 
  unsigned int bf_0; 
  unsigned int v24; 
  unsigned __int64 i; 
  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;} v26; 
  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;} v27; 
  int v28; 
  UINT64 PteOffset; 
  unsigned __int64 v30; 
  _CONTROL_AREA *v31; 
  int v32; 

  v31 = ControlArea;
  v5 = 0;
  v6 = ControlArea;
  v7 = FirstPte;
  PteOffset = 0i64;
  if( (MiFlags & 0x10000) == 0 || (MiFlags & 0x8000) == 0 || (ControlArea->u.LongFlags & 0x20) == 0 )
    v32 = 0;
  v8 = &FirstPte[NumberOfPtes];
  v30 = 0i64;
  DemandZeroPte = 0i64;
  v10 = MiOffsetToProtos(ControlArea, SectionOffset->QuadPart, &PteOffset);
  v11 = v10;
  if( v10 )
  {
    v12 = 1;
    if( (v10->u.SubsectionFlags._bf_2 & 2) != 0 && (v6->u.LongFlags & 0x4000000) != 0 )
      goto LABEL_22;
    if( (v6->u.LongFlags & 0x20) != 0 )
    {
      SubsectionDriverProtos = MiGetSubsectionDriverProtos(v10);
      if( SubsectionDriverProtos )
        goto LABEL_23;
    }
    SubsectionBase = v11->SubsectionBase;
    v14 = (unsigned __int64)&SubsectionBase[PteOffset];
    v15 = (unsigned __int64)&SubsectionBase[v11->PtesInSubsection];
    while( 1 )
    {
      if( v7 >= v8 )
        return 0;
      if( v14 < v15 )
        goto LABEL_9;
      v11 = v11->NextSubsection;
      if( !v11 )
      {
        for( i = *(_QWORD *)v6[1].gap8
                + 8
                * (v6->Segment->TotalNumberOfPtes | ((unsigned __int64)(v6->Segment->SegmentFlags.Short0 & 0x3FF) << 32));
              v14 < i;
              v14 += 8i64 )
        {
          LODWORD(v26.Long) = MiMakePrototypePteDirect(v14);
          v27.Long = v26.Long;
          LOBYTE(v28) = MiPteInShadowRange((UINT64)v7);
          if( v28 && (KeGetCurrentThread()->ApcState.Process->Flags3 & 0x1000) != 0 && (v27.Long & 1) != 0 )
            v27.Long |= 0x8000000000000000ui64;
          v7->u.Long = v27.Long;
          if( ++v7 >= v8 )
            break;
        }
        return 0;
      }
      bf_2 = v11->u.SubsectionFlags._bf_2;
      v21.LongFlags = (unsigned int)v6->u;
      v14 = (unsigned __int64)v11->SubsectionBase;
      if( (((v21.LongFlags & 0x4000000) != 0) & _bittest16(&bf_2, v12)) != 0 )
      {
LABEL_22:
        SubsectionDriverProtos = (_MI_PER_SESSION_PROTOS *)MiGetSharedProtos(v6, (unsigned int)SessionId, v11);
      }
      else
      {
        if( (v21.LongFlags & 0x20) == 0 )
          goto LABEL_18;
        SubsectionDriverProtos = MiGetSubsectionDriverProtos(v11);
        if( !SubsectionDriverProtos )
          goto LABEL_18;
      }
LABEL_23:
      v14 = (unsigned __int64)SubsectionDriverProtos->SubsectionBase;
LABEL_18:
      v5 = 0;
      v15 = v14 + 8i64 * v11->PtesInSubsection;
      if( !v32 )
        goto LABEL_10;
      bf_0 = (unsigned __int16)v11->u.SubsectionFlags._bf_0;
      if( (bf_0 & 4) == 0 )
        goto LABEL_10;
      v24 = ((unsigned __int16)v11->u.SubsectionFlags._bf_2 >> 4) + (v11->NumberOfFullSectors << 9);
      v30 = v14 + 8i64 * ((v24 >> 12) + ((v24 & 0xFFF) != 0));
      v5 = ((__int64)(v15 - v30) >> 3) - (v11->_bf_34 & 0x3FFFFFFF);
      DemandZeroPte = MiMakeDemandZeroPte((bf_0 >> 1) & 0x1F);
LABEL_9:
      if( v5 && v14 >= v30 )
      {
        v17 = DemandZeroPte;
        --v5;
        goto LABEL_11;
      }
LABEL_10:
      LODWORD(v16) = MiMakePrototypePteDirect(v14);
      v17 = v16;
LABEL_11:
      LOBYTE(v18) = MiPteInShadowRange((UINT64)v7);
      if( v18 && (KeGetCurrentThread()->ApcState.Process->Flags3 & 0x1000) != 0 && (v17 & 1) != 0 )
        v17 |= 0x8000000000000000ui64;
      v7->u.Long = v17;
      v6 = v31;
      ++v7;
      v14 += 8i64;
      v12 = 1;
    }
  }
  return -1073741793;
}

Referenced by:

MiInsertInSystemSpace
MiMapSystemImage