HvpViewMapPromoteRangeToMapping

NTSTATUS __stdcall HvpViewMapPromoteRangeToMapping(_XSTATE_CONFIGURATION *a1, INT64 a2, UINT64 a3, INT64 *a4){
  _XSTATE_FEATURE *v4; 
  UINT64 v5; 
  __int64 v6; 
  INT64 v7; 
  bool v8; 
  _XSTATE_CONFIGURATION *v9; 
  int v10; 
  UINT64 v11; 
  NTSTATUS ViewRangeValid; 
  __int64 MemoryAllocationGranularity; 
  NTSTATUS View; 
  INT64 v16; 
  _RTL_BALANCED_NODE *v17; 
  __int64 v18; 
  UINT64 v19; 
  UINT64 v20; 
  INT64 v21; 
  UINT64 v22; 
  UINT64 v23; 
  __int64 i; 
  _QWORD *v25; 
  __int64 v26; 
  _XSTATE_CONFIGURATION *v27; 
  _QWORD *v28; 
  UINT64 v29; 
  BOOL v30; 
  UINT64 v31; 
  PPRIVILEGE_SET v32; 
  __int64 v33; 
  VOID *v34; 
  UINT64 v35; 
  UINT64 v36; 
  _QWORD *v37; 
  _QWORD *v38; 
  _RTL_BALANCED_NODE *v39; 
  _RTL_BALANCED_NODE *Node; 
  PPRIVILEGE_SET Privileges; 
  PPRIVILEGE_SET *p_Privileges; 

  Node = 0i64;
  v4 = &a1->Features[2];
  v5 = (UINT64)a1->Features[2];
  v6 = (unsigned int)(a2 + 4096);
  v7 = v6 + (unsigned int)a3;
  p_Privileges = &Privileges;
  v8 = (a1->Features[3].Offset & 1) == 0;
  Privileges = (PPRIVILEGE_SET)&Privileges;
  v9 = a1;
  if( !v8 && v5 )
    v5 ^= (unsigned __int64)v4;
  v10 = a1->Features[3].Offset & 1;
  while( v5 )
  {
    if( v6 >= *(_QWORD *)(v5 + 40) )
    {
      if( v6 < *(_QWORD *)(v5 + 48) )
        break;
      v11 = *(_QWORD *)(v5 + 8);
    }
    else
    {
      v11 = *(_QWORD *)v5;
    }
    if( v10 && v11 )
      v5 ^= v11;
    else
      v5 = v11;
  }
  if( *(_QWORD *)(v5 + 48) >= v7 )
  {
    *a4 = v6 + *(_QWORD *)(v5 + 56) - *(_QWORD *)(v5 + 24);
    return 0;
  }
  MemoryAllocationGranularity = CmSiGetMemoryAllocationGranularity();
  View = HvpViewMapCreateView((INT64)v9, v6 & -MemoryAllocationGranularity, v7, (CHAR **)&Node);
  v17 = Node;
  ViewRangeValid = View;
  if( View >= 0 )
  {
    ViewRangeValid = HvpViewMapMakeViewRangeValid((UINT64)v9, Node, v6, v7, 0);
    if( ViewRangeValid >= 0 )
    {
      v18 = v6;
      if( v6 < (unsigned __int64)v7 )
      {
        do
        {
          v19 = (UINT64)*v4;
          if( (v4[1].Offset & 1) != 0 && v19 )
            v19 ^= (unsigned __int64)v4;
          while( v19 )
          {
            if( v18 >= *(_QWORD *)(v19 + 40) )
            {
              if( v18 < *(_QWORD *)(v19 + 48) )
                break;
              v20 = *(_QWORD *)(v19 + 8);
            }
            else
            {
              v20 = *(_QWORD *)v19;
            }
            if( (v4[1].Offset & 1) != 0 && v20 )
              v19 ^= v20;
            else
              v19 = v20;
          }
          ViewRangeValid = HvpViewMapMigrateCOWData(a1, v17, v19);
          if( ViewRangeValid < 0 )
            goto LABEL_75;
          v18 = *(_QWORD *)(v19 + 48);
        }
        while( v18 < v7 );
      }
      v21 = v7 - 1;
      v22 = (UINT64)*v4;
      if( (v4[1].Offset & 1) != 0 && v22 )
        v22 ^= (unsigned __int64)v4;
      while( v22 )
      {
        if( v21 >= *(_QWORD *)(v22 + 40) )
        {
          if( v21 < *(_QWORD *)(v22 + 48) )
            break;
          v23 = *(_QWORD *)(v22 + 8);
        }
        else
        {
          v23 = *(_QWORD *)v22;
        }
        if( (v4[1].Offset & 1) != 0 && v23 )
          v22 ^= v23;
        else
          v22 = v23;
      }
      for( i = *(_QWORD *)(v5 + 48); i < *(_QWORD *)(v22 + 40); i = *(_QWORD *)(v35 + 48) )
      {
        v35 = (UINT64)*v4;
        if( (v4[1].Offset & 1) != 0 && v35 )
          v35 ^= (unsigned __int64)v4;
        while( v35 )
        {
          if( i < *(_QWORD *)(v35 + 40) )
          {
            v36 = *(_QWORD *)v35;
          }
          else
          {
            if( i < *(_QWORD *)(v35 + 48) )
              break;
            v36 = *(_QWORD *)(v35 + 8);
          }
          if( (v4[1].Offset & 1) != 0 && v36 )
            v35 ^= v36;
          else
            v35 = v36;
        }
        RtlRbRemoveNode((UINT64 *)v4, v35);
        v37 = p_Privileges;
        if( *p_Privileges != (PPRIVILEGE_SET)&Privileges )
          goto LABEL_104;
        *(_QWORD *)(v35 + 8) = p_Privileges;
        *(_QWORD *)v35 = &Privileges;
        *v37 = v35;
        p_Privileges = (PPRIVILEGE_SET *)v35;
      }
      v25 = (_QWORD *)v5;
      if( *(_QWORD *)(v5 + 40) == v6 )
      {
        RtlRbRemoveNode((UINT64 *)v4, v5);
        v38 = p_Privileges;
        if( *p_Privileges != (PPRIVILEGE_SET)&Privileges )
          goto LABEL_104;
        *(_QWORD *)(v5 + 8) = p_Privileges;
        *(_QWORD *)v5 = &Privileges;
        *v38 = v5;
        p_Privileges = (PPRIVILEGE_SET *)v5;
        v27 = a1;
      }
      else
      {
        v26 = *(_QWORD *)(v5 + 48);
        v27 = a1;
        HvpViewMapMakeViewRangeInvalid((UINT64)a1, v25, v6, v26);
      }
      if( *(_QWORD *)(v22 + 48) == v7 )
      {
        RtlRbRemoveNode((UINT64 *)v4, v22);
        v28 = p_Privileges;
        if( *p_Privileges != (PPRIVILEGE_SET)&Privileges )
          goto LABEL_104;
        *(_QWORD *)(v22 + 8) = p_Privileges;
        *(_QWORD *)v22 = &Privileges;
        *v28 = v22;
        p_Privileges = (PPRIVILEGE_SET *)v22;
      }
      else
      {
        HvpViewMapMakeViewRangeInvalid((UINT64)v27, (_QWORD *)v22, *(_QWORD *)(v22 + 40), v7);
      }
      v29 = (UINT64)*v4;
      if( (v4[1].Offset & 1) != 0 && v29 )
        v29 ^= (unsigned __int64)v4;
      v30 = 0;
      if( v29 )
      {
        while( 1 )
        {
          if( (signed __int64)v17[1].ParentValue < *(_QWORD *)(v29 + 40) )
          {
            v31 = *(_QWORD *)v29;
            if( (v4[1].Offset & 1) != 0 )
            {
              if( !v31 )
                goto LABEL_103;
              v31 ^= v29;
            }
            if( !v31 )
            {
LABEL_103:
              v30 = 0;
              break;
            }
          }
          else
          {
            v31 = *(_QWORD *)(v29 + 8);
            if( (v4[1].Offset & 1) != 0 )
            {
              if( !v31 )
                goto LABEL_102;
              v31 ^= v29;
            }
            if( !v31 )
            {
LABEL_102:
              v30 = 1;
              break;
            }
          }
          v29 = v31;
        }
      }
      RtlRbInsertNodeEx((UINT64 *)v4, v29, v30, (UINT64)v17);
      v32 = Privileges;
      v33 = *(_QWORD *)&Privileges->PrivilegeCount;
      if( (PPRIVILEGE_SET *)Privileges->Privilege[0].Luid == &Privileges )
      {
        while( 1 )
        {
          if( *(PPRIVILEGE_SET *)(v33 + 8) != v32 )
            goto LABEL_104;
          Privileges = (PPRIVILEGE_SET)v33;
          *(_QWORD *)(v33 + 8) = &Privileges;
          if( v32 == (PPRIVILEGE_SET)&Privileges )
            break;
          v34 = *(VOID **)&v32[2].Privilege[0].Attributes;
          if( v34 )
            CmSiUnmapViewOfSection((__int64)&Privileges, *(VOID ***)v27->Features, v34);
          CmSiFreeMemory(v32);
          v32 = Privileges;
          if( (PPRIVILEGE_SET *)Privileges->Privilege[0].Luid != &Privileges )
            goto LABEL_104;
          v33 = *(_QWORD *)&Privileges->PrivilegeCount;
        }
        ViewRangeValid = 0;
        v16 = v6 + (char *)v17[2].Children[1] - (char *)v17[1].Children[0];
        v17 = 0i64;
        *a4 = v16;
LABEL_75:
        v9 = a1;
        goto LABEL_76;
      }
LABEL_104:
      __fastfail(3u);
    }
  }
LABEL_76:
  if( v17 )
  {
    v39 = v17[2].Children[1];
    if( v39 )
      CmSiUnmapViewOfSection(v16, *(VOID ***)v9->Features, v39);
    CmSiFreeMemory((PPRIVILEGE_SET)v17);
  }
  return ViewRangeValid;
}

Referenced by:

HvpAddBin
HvpRemapAndEnlistHiveBins