MiExtendSection

NTSTATUS __stdcall MiExtendSection(
        _SUBSECTION *LastSubsection,
        UINT64 *NewSectionSize,
        UINT64 PtesUsed,
        UINT64 RequiredPtes){
  _CONTROL_AREA *v4; 
  _MSUBSECTION *p_ExtendedSubsectionHead; 
  UINT64 v7; 
  unsigned int v8; 
  __int128 v9; 
  union {unsigned int LongFlags;_MMSECTION_FLAGS Flags;} v10; 
  __int128 v11; 
  __int128 v12; 
  UINT64 v13; 
  unsigned __int64 v14; 
  UINT64 v15; 
  __int128 v16; 
  unsigned int v17; 
  _LIST_ENTRY v18; 
  __int128 v19; 
  __int128 v20; 
  unsigned __int64 v21; 
  unsigned int v22; 
  _SUBSECTION *Pool; 
  _MSUBSECTION *v24; 
  unsigned int v25; 
  _SEGMENT *v26; 
  __int16 bf_0; 
  unsigned int PtesInSubsection; 
  __int64 StartingSector; 
  unsigned __int64 v30; 
  __int64 v31; 
  NTSTATUS appended; 
  unsigned __int16 Short0; 
  unsigned __int64 v34; 
  _MSUBSECTION *NextSubsection; 
  _MSUBSECTION *v37; 
  UINT64 v38; 
  UINT64 v39; 
  UINT64 v40; 
  unsigned int v41; 
  unsigned __int64 v42; 
  unsigned __int64 v43; 
  _MMPTE *v44; 
  int v45; 
  int v46; 
  __int64 v47; 
  int SubsectionCharges; 
  _MI_PARTITION *ControlAreaPartition; 
  _MI_CROSS_PARTITION_CHARGES *v50; 
  _MSUBSECTION *v51; 
  __int64 v52; 
  _CONTROL_AREA *ControlArea; 
  UINT64 v54; 
  unsigned __int64 v55; 
  _SEGMENT *Segment; 
  _MSUBSECTION ExtendedSubsectionHead; 
  int AllocationFlags; 
  unsigned int v60; 
  _MI_PARTITION *v62; 
  UINT64 DanglingPageCount; 

  v60 = PtesUsed;
  p_ExtendedSubsectionHead = &ExtendedSubsectionHead;
  ControlArea = LastSubsection->ControlArea;
  v4 = LastSubsection->ControlArea;
  v7 = (8 * RequiredPtes + 4095) & 0xFFFFFFFFFFFFF000ui64;
  v8 = PtesUsed;
  *(_QWORD *)DanglingPageCount = 0i64;
  Segment = v4->Segment;
  v9 = *(_OWORD *)&LastSubsection->NextSubsection;
  v10.LongFlags = (unsigned int)v4->u;
  *(_OWORD *)&ExtendedSubsectionHead.Core.ControlArea = *(_OWORD *)&LastSubsection->ControlArea;
  v54 = v7;
  v11 = *(_OWORD *)&LastSubsection->u.LongFlags;
  *(_OWORD *)&ExtendedSubsectionHead.Core.NextSubsection = v9;
  v12 = *(_OWORD *)&LastSubsection->u1.e1._bf_0;
  v13 = 0i64;
  *(_OWORD *)&ExtendedSubsectionHead.Core.u.LongFlags = v11;
  v14 = (-(__int64)((v10.LongFlags & 0x40000000) != 0) & 0x100000) + 0x100000;
  v15 = 0i64;
  v16 = *(_OWORD *)&LastSubsection[1].SubsectionBase;
  v17 = 0;
  v55 = v14;
  *(_OWORD *)&ExtendedSubsectionHead.Core.u1.e1._bf_0 = v12;
  v18 = *(_LIST_ENTRY *)&LastSubsection[1].GlobalPerSessionHead.Root;
  *(_OWORD *)&ExtendedSubsectionHead.SubsectionNode.Right = v16;
  v19 = *(_OWORD *)&LastSubsection[1].NumberOfFullSectors;
  ExtendedSubsectionHead.DereferenceList = v18;
  v20 = *(_OWORD *)&LastSubsection[2].NextSubsection;
  *(_OWORD *)&ExtendedSubsectionHead.NumberOfMappedViews = v19;
  ExtendedSubsectionHead.ProtosNode.Node.0 = *($B2AD7D77B0CFA0A0765E11E131618F47 *)&LastSubsection[2].ControlArea;
  *(_OWORD *)&ExtendedSubsectionHead.ProtosNode.Node._bf_0 = v20;
  if( (_DWORD)PtesUsed )
    ExtendedSubsectionHead.Core._bf_34 ^= (ExtendedSubsectionHead.Core._bf_34 ^ (ExtendedSubsectionHead.Core._bf_34
                                                                               - PtesUsed)) & 0x3FFFFFFF;
  v21 = 0i64;
  do
  {
    if( v7 - v13 > v14 )
      v22 = v14;
    else
      v22 = v7 - v13;
    Pool = (_SUBSECTION *)MiAllocatePool(64i64, 0x90ui64, 0x64536D4Dui64);
    v24 = (_MSUBSECTION *)Pool;
    if( !Pool )
    {
      NextSubsection = (_MSUBSECTION *)ExtendedSubsectionHead.Core.NextSubsection;
LABEL_49:
      SubsectionCharges = -1073741670;
      goto LABEL_50;
    }
    v25 = v22 >> 3;
    *(_QWORD *)&Pool[1].u.LongFlags = (char *)Pool + 80;
    Pool[1].GlobalPerSessionHead.Root = (_RTL_BALANCED_NODE *)&Pool[1].24;
    p_ExtendedSubsectionHead->Core.NextSubsection = Pool;
    Pool->ControlArea = ControlArea;
    Pool->PtesInSubsection = v22 >> 3;
    v13 += v22;
    if( v13 > 8 * RequiredPtes )
      Pool->_bf_34 ^= (Pool->_bf_34 ^ ((v13 >> 3) - RequiredPtes)) & 0x3FFFFFFF;
    v26 = Segment;
    bf_0 = Pool->u.SubsectionFlags._bf_0 & 0xFFC1 | (2 * ((Segment->SegmentFlags.UChar1 >> 1) & 0x1F));
    Pool->u.SubsectionFlags._bf_0 = bf_0;
    if( p_ExtendedSubsectionHead == &ExtendedSubsectionHead )
    {
      p_ExtendedSubsectionHead->Core.u.SubsectionFlags._bf_2 &= 0xFu;
      PtesInSubsection = p_ExtendedSubsectionHead->Core.PtesInSubsection;
      StartingSector = p_ExtendedSubsectionHead->Core.StartingSector;
      v30 = (unsigned __int64)(p_ExtendedSubsectionHead->Core.u.SubsectionFlags._bf_0 & 0xFFC0) << 26;
      p_ExtendedSubsectionHead->Core.NumberOfFullSectors = PtesInSubsection;
      v21 = StartingSector | v30;
      bf_0 = v24->Core.u.SubsectionFlags._bf_0;
    }
    else
    {
      PtesInSubsection = p_ExtendedSubsectionHead->Core.NumberOfFullSectors;
    }
    v31 = PtesInSubsection;
    v7 = v54;
    v21 += v31;
    v24->Core.StartingSector = v21;
    v24->Core.u.SubsectionFlags._bf_0 = bf_0 & 0x3F | (WORD2(v21) << 6);
    if( v13 < v54 )
    {
      v24->Core.NumberOfFullSectors = v25;
    }
    else
    {
      v24->Core.NumberOfFullSectors = (*NewSectionSize >> 12) - v21;
      v24->Core.u.SubsectionFlags._bf_2 = v24->Core.u.SubsectionFlags._bf_2 & 0xF | (16 * *(_WORD *)NewSectionSize);
    }
    p_ExtendedSubsectionHead = v24;
    v14 = v55;
  }
  while( v13 < v54 );
  if( v8 )
  {
    LOBYTE(v46) = MiControlAreaUsingExtents((INT64)ControlArea);
    if( v46 )
    {
      if( LastSubsection->SubsectionBase )
      {
        MiSubsectionNeedsExtents(LastSubsection);
        *(_QWORD *)DanglingPageCount = LastSubsection;
      }
    }
  }
  appended = MiAppendSubsectionChain((_MSUBSECTION *)LastSubsection, &ExtendedSubsectionHead, 0i64);
  if( !appended )
  {
LABEL_16:
    Short0 = v26->SegmentFlags.Short0;
    v34 = (v26->TotalNumberOfPtes | ((unsigned __int64)(Short0 & 0x3FF) << 32)) + RequiredPtes + v8;
    v26->TotalNumberOfPtes += RequiredPtes + v8;
    v26->SegmentFlags.Short0 = Short0 ^ (Short0 ^ WORD2(v34)) & 0x3FF;
    return 0;
  }
  NextSubsection = (_MSUBSECTION *)ExtendedSubsectionHead.Core.NextSubsection;
LABEL_18:
  v37 = NextSubsection;
  if( (appended & 1) != 0 )
  {
    v38 = v54;
    AllocationFlags = v17 | 1;
    v39 = 0i64;
    while( 1 )
    {
      v40 = v38;
      v41 = v55;
      v42 = v40 - v39;
      if( v42 <= v55 )
        v41 = v42;
      v43 = v41;
      v39 += v41;
      v44 = (_MMPTE *)MiAllocatePool(274i64, v41, 0x74536D4Dui64);
      if( !v44 )
        break;
      v45 = ControlArea->u.LongFlags & 0x40000000;
      MiInitializePrototypePtes(v44, v43 >> 3, &v37->Core, v45 == 0);
      if( v45 )
      {
        if( !*(_QWORD *)DanglingPageCount )
          *(_QWORD *)DanglingPageCount = v37;
      }
      else if( (AllocationFlags & 2) != 0 )
      {
        MiDecrementSubsectionViewCount(v37);
      }
      MiSetSubsectionBase(&v37->Core, v44);
      MiUpdateSystemProtoPtesTree(&v37->ProtosNode.Node, 1i64);
      v38 = v54;
      v37 = (_MSUBSECTION *)v37->Core.NextSubsection;
      if( v39 >= v54 )
      {
        v17 = AllocationFlags;
        v8 = v60;
        goto LABEL_28;
      }
    }
    LOBYTE(v17) = AllocationFlags;
    goto LABEL_49;
  }
  v17 |= 2u;
  while( 1 )
  {
    v47 = v37->Core.PtesInSubsection - (v37->Core._bf_34 & 0x3FFFFFFF);
    SubsectionCharges = MiGetSubsectionCharges((UINT8)v37);
    if( SubsectionCharges < 0 )
      break;
    if( MiIncrementSubsectionViewCount(v37, 0x38ui64) <= 1 )
    {
      v15 += v47;
      break;
    }
    v37 = (_MSUBSECTION *)v37->Core.NextSubsection;
    if( !v37 )
    {
      if( v8 )
      {
        SubsectionCharges = MiGetSubsectionCharges((UINT8)LastSubsection);
        if( SubsectionCharges < 0 )
          break;
        v15 += v8;
      }
LABEL_28:
      appended = MiAppendSubsectionChain((_MSUBSECTION *)LastSubsection, &ExtendedSubsectionHead, v17);
      if( appended )
        goto LABEL_18;
      v26 = Segment;
      goto LABEL_16;
    }
  }
LABEL_50:
  ControlAreaPartition = MiGetControlAreaPartition(ControlArea, (_EPROCESS *)v24);
  v62 = ControlAreaPartition;
  v50 = (_MI_CROSS_PARTITION_CHARGES *)ControlAreaPartition;
  if( v15 )
  {
    MiReturnCrossPartitionSectionCharges((_MI_CROSS_PARTITION_CHARGES *)ControlAreaPartition, 1i64, v15);
    v15 = 0i64;
  }
  if( NextSubsection )
  {
    do
    {
      v51 = (_MSUBSECTION *)NextSubsection->Core.NextSubsection;
      if( (ControlArea->u.LongFlags & 0x40000000) != 0 )
      {
        if( NextSubsection->Core.SubsectionBase )
        {
          DanglingPageCount = 0i64;
          MiDeleteSubsectionPages(&NextSubsection->Core, &DanglingPageCount);
        }
      }
      else
      {
        if( (v17 & 2) != 0 && (NextSubsection->Core.u1.e1._bf_0 & 0x3FFFFFFF) != 0 )
        {
          LODWORD(v52) = MiDecrementSubsectionViewCount(NextSubsection);
          v15 += v52;
        }
        if( NextSubsection->Core.SubsectionBase )
        {
          MiUpdateSystemProtoPtesTree(&NextSubsection->ProtosNode.Node, 0i64);
          ExFreePoolWithTag(NextSubsection->Core.SubsectionBase, 0);
        }
      }
      CmSiFreeMemory((PPRIVILEGE_SET)NextSubsection);
      NextSubsection = v51;
    }
    while( v51 );
    v50 = (_MI_CROSS_PARTITION_CHARGES *)v62;
  }
  if( v15 )
    MiReturnCrossPartitionSectionCharges(v50, 1i64, v15);
  return SubsectionCharges;
}

Referenced by:

MmExtendSection