MiProcessVaContiguityInformation

NTSTATUS __stdcall MiProcessVaContiguityInformation(UINT64 a1, INT64 a2){
  INT64 v4; 
  unsigned __int64 v5; 
  _MMSUPPORT_INSTANCE *p_Instance; 
  unsigned int v7; 
  _MI_PARTITION *v8; 
  int v9; 
  UINT64 v11; 
  unsigned __int64 v12; 
  INT64 v13; 
  int v14; 
  INT64 v15; 
  UINT64 v16; 
  signed __int32 v17; 
  int v18; 
  _KQUEUE *LargePage; 
  INT64 v20; 
  struct _KPRCB *CurrentPrcb; 
  __int64 CachedResidentAvailable; 
  bool v23; 
  signed __int32 v24; 
  unsigned __int8 CurrentIrql; 
  _MMPFN *a4; 
  UINT64 v27; 
  _MI_COLOR_BASE ColorBase[4]; 
  INT64 v29; 
  INT64 v30; 

  LODWORD(v29) = 0;
  v4 = 0i64;
  v5 = 512i64;
  p_Instance = &KeGetCurrentThread()->ApcState.Process->Vm.Instance;
  v30 = (INT64)p_Instance;
  *(_OWORD *)ColorBase = 0i64;
  v7 = 0;
  v8 = *(_MI_PARTITION **)(*(&stru_140C4DB30 + 267) + 8i64 * p_Instance->PartitionId);
  MiAcquireNonPagedResources(v8, 0x200ui64);
  if( v9 < 0 )
    return -1073741670;
  v11 = a1 + 16 * a2;
  if( a1 >= v11 )
  {
LABEL_30:
    LODWORD(v29) = 0;
    goto LABEL_31;
  }
  while( 1 )
  {
    if( (*(_DWORD *)(a1 + 8) & 0x1FFFFF) != 0
      || (v12 = *(_QWORD *)a1, (*(_QWORD *)a1 & 0xFFFFFFFFFFE00000ui64) != *(_QWORD *)a1) )
    {
      LODWORD(v29) = -1073741811;
      goto LABEL_31;
    }
    if( !v4 )
      goto LABEL_11;
    if( v12 < (*(unsigned int *)(v4 + 24) | ((unsigned __int64)*(unsigned __int8 *)(v4 + 32) << 32)) << 12
      || v12 > (((*(unsigned int *)(v4 + 28) | ((unsigned __int64)*(unsigned __int8 *)(v4 + 33) << 32)) << 12) | 0xFFF) )
    {
      MiUnlockAndDereferenceVadShared((PVOID)v4);
      v4 = 0i64;
    }
    if( !v4 )
    {
LABEL_11:
      LODWORD(v13) = MiObtainReferencedVadEx(*(_QWORD *)a1, 2, &v29);
      v4 = v13;
      if( !v13 )
        goto LABEL_33;
      if( *(_QWORD *)(a1 + 8) + *(_QWORD *)a1 - 1i64 > (((*(unsigned int *)(v13 + 28) | ((unsigned __int64)*(unsigned __int8 *)(v13 + 33) << 32)) << 12) | 0xFFF) )
      {
        LODWORD(v29) = -1073741800;
        goto LABEL_31;
      }
      LOBYTE(v14) = MiVadSupportsPhysicalContiguityQuery(v13);
      if( !v14
        || !MiVadPagesTradable(v4)
        || MiGetVadCacheAttribute(v15) != 1
        || (v7 = (*(_DWORD *)(v4 + 48) >> 12) & 0x3F) == 0 )
      {
        LODWORD(v29) = -1073741637;
        goto LABEL_31;
      }
    }
    MiInitializePageColorBase(p_Instance, v7, ColorBase);
    v16 = *(_QWORD *)a1;
    v27 = *(_QWORD *)a1 + *(_QWORD *)(a1 + 8);
    if( *(_QWORD *)a1 < v27 )
      break;
LABEL_24:
    a1 += 16i64;
    if( a1 >= v11 )
      goto LABEL_30;
    p_Instance = (_MMSUPPORT_INSTANCE *)v30;
  }
  while( MiQueryVaPhysicalContiguity(v30, v16, 1ui64, 1i64) )
  {
LABEL_23:
    v16 += 0x200000i64;
    if( v16 >= v27 )
      goto LABEL_24;
  }
  v17 = _InterlockedExchangeAdd(*(volatile signed __int32 **)ColorBase, 1u);
  v18 = ColorBase[2] & v17 | ColorBase[3];
  CurrentIrql = KeGetCurrentIrql();
  __writecr8(2ui64);
  LargePage = (_KQUEUE *)MiGetLargePage((INT64)v8, 1u, MiCached, v18, 4, 0i64);
  a4 = (_MMPFN *)LargePage;
  if( LargePage )
    MiConvertEntireLargePageToSmall(LargePage, 1, 2u, (_LARGE_INTEGER *)1, 0i64, 0i64);
  __writecr8(CurrentIrql);
  if( a4 )
  {
    LODWORD(v29) = MiMakeVaRangePhysicallyContiguous((_MMSUPPORT_INSTANCE *)v30, v16, v20, a4);
    goto LABEL_23;
  }
  LODWORD(v29) = -1073741801;
LABEL_31:
  if( v4 )
    MiUnlockAndDereferenceVadShared((PVOID)v4);
LABEL_33:
  MiReturnCommit(v8, 0x200ui64);
  if( v8 != &Irp
    || (CurrentPrcb = KeGetCurrentPrcb(),
        CachedResidentAvailable = (int)CurrentPrcb->CachedResidentAvailable,
        (_DWORD)CachedResidentAvailable == -1) )
  {
LABEL_44:
    _InterlockedExchangeAdd64((volatile signed __int64 *)&v8->Vp.ResidentAvailablePages, v5);
    return v29;
  }
  if( (unsigned __int64)(CachedResidentAvailable + 512) > 0x100 )
  {
LABEL_39:
    if( (int)CachedResidentAvailable > 192
      && (_DWORD)CachedResidentAvailable != -1
      && (_DWORD)CachedResidentAvailable == _InterlockedCompareExchange(
                                              (volatile signed __int32 *)&CurrentPrcb->CachedResidentAvailable,
                                              192,
                                              CachedResidentAvailable) )
    {
      v5 = (int)CachedResidentAvailable - 192 + 512i64;
    }
    if( !v5 )
      return v29;
    goto LABEL_44;
  }
  while( 1 )
  {
    v24 = _InterlockedCompareExchange(
            (volatile signed __int32 *)&CurrentPrcb->CachedResidentAvailable,
            CachedResidentAvailable + 512,
            CachedResidentAvailable);
    v23 = (_DWORD)CachedResidentAvailable == v24;
    LODWORD(CachedResidentAvailable) = v24;
    if( v23 )
      return v29;
    if( v24 == -1 || (unsigned __int64)(v24 + 512i64) > 0x100 )
      goto LABEL_39;
  }
}

Referenced by:

NtSetInformationVirtualMemory