MiReferenceIncomingPhysicalPages

INT64 __fastcall MiReferenceIncomingPhysicalPages(
        INT64 a1,
        INT64 a2,
        UINT64 a3,
        INT64 a4,
        INT64 *a5,
        INT64 a6,
        CHAR *a7){
  INT64 v8; 
  UINT64 v9; 
  INT64 v10; 
  int v11; 
  unsigned int v12; 
  unsigned __int64 v13; 
  INT64 v14; 
  INT64 AweViewPageSize; 
  unsigned int v16; 
  INT64 v17; 
  CHAR *PteAddress; 
  unsigned __int64 v19; 
  const signed __int64 *v20; 
  unsigned __int64 v21; 
  INT64 v22; 
  unsigned __int64 v23; 
  unsigned __int64 v24; 
  __int64 v25; 
  MMPTE *AweNode; 
  INT64 v27; 
  __int64 v28; 
  unsigned __int64 v29; 
  INT64 v30; 
  unsigned __int64 v31; 
  char *v32; 
  unsigned __int64 v33; 
  __int64 v34; 
  VOID *v35; 
  unsigned __int64 v36; 
  int v38; 
  int v39; 
  UINT64 ReserveSize; 
  unsigned __int64 v41; 
  const signed __int64 *v42; 
  UINT64 v45; 
  unsigned int VadCacheAttribute; 
  v45 = a3;
  v8 = 0i64;
  v9 = a3;
  v38 = 0;
  v10 = *(_QWORD *)(a6 + 24);
  v11 = *(_DWORD *)(v10 + 48) & 0x1100000;
  v39 = v11;
  VadCacheAttribute = MiGetVadCacheAttribute(v10);
  v12 = VadCacheAttribute;
  ReserveSize = 0i64;
  v13 = ExGetCallBackBlockRoutine(a1);
  AweViewPageSize = MiGetAweViewPageSize(v14);
  PteAddress = a7;
  v19 = v13;
  if( AweViewPageSize )
    v19 = AweViewPageSize;
  v41 = *(_QWORD *)(a1 + 16);
  v20 = *(const signed __int64 **)(a1 + 24);
  v21 = 0i64;
  v42 = v20;
  if( v9 )
  {
    v22 = a2;
    while( 1 )
    {
      v23 = *(_QWORD *)(v22 + 8 * v21);
      if( !v17 )
        goto LABEL_18;
      if( v23 )
        break;
      ++v21;
LABEL_41:
      if( v21 >= v9 )
      {
        v8 = ReserveSize;
        goto LABEL_43;
      }
    }
    PteAddress = (CHAR *)MiGetPteAddress(*(PVOID *)(v17 + 8 * v21));
    if( v24 < (*(unsigned int *)(v25 + 24) | ((unsigned __int64)*(unsigned __int8 *)(v25 + 32) << 32)) << 12
      || v24 > (((*(unsigned int *)(v25 + 28) | ((unsigned __int64)*(unsigned __int8 *)(v25 + 33) << 32)) << 12) | 0xFFF) )
    {
      AweNode = MiGetAweNode(v24);
      v27 = MiGetAweViewPageSize((INT64)AweNode);
      v29 = v13;
      if( v27 )
        v29 = v27;
      if( v29 != v19 )
        goto LABEL_39;
      v30 = *(_QWORD *)(v28 + 24);
      if( (*(_DWORD *)(v30 + 48) & 0x1100000) == 17825792 )
      {
        if( v11 != 17825792 )
          goto LABEL_39;
      }
      else if( v11 == 17825792 )
      {
        goto LABEL_39;
      }
      if( v12 != (unsigned int)MiGetVadCacheAttribute(v30) )
        goto LABEL_39;
    }
LABEL_18:
    if( v23 / v13 < v41 && ((v19 - 1) & v23) == 0 )
    {
      v31 = 0i64;
      if( v19 / v13 )
      {
        while( _bittest64(v42, v23 / v13 + v31) )
        {
          if( ++v31 >= v19 / v13 )
            goto LABEL_23;
        }
        goto LABEL_39;
      }
LABEL_23:
      v16 = v38;
      v32 = (char *)MmGetPfnDb() + 48 * v23;
      if( v38 < 0 )
      {
        v36 = 0i64;
        if( v19 )
        {
          do
          {
            MiDecrementAweMapCount((VOID *)v31, (UINT64)v32, (UINT64)&ReserveSize);
            v32 += 48 * v13;
            v36 += v13;
          }
          while( v36 < v19 );
          v16 = v38;
LABEL_28:
          v9 = v45;
        }
      }
      else
      {
        v33 = 0i64;
        if( !v19 )
        {
          v9 = v45;
          goto LABEL_30;
        }
        v34 = 48 * v13;
        while( 1 )
        {
          v38 = MiIncrementAweMapCount(a1, (INT64)v32, VadCacheAttribute, (INT64)PteAddress);
          v16 = v38;
          if( v38 < 0 )
            break;
          v33 += v13;
          v34 = 48 * v13;
          v32 += 48 * v13;
          if( v33 >= v19 )
            goto LABEL_28;
        }
        if( v33 )
        {
          do
          {
            v32 -= v34;
            MiDecrementAweMapCount(v35, (UINT64)v32, (UINT64)&ReserveSize);
            v33 -= v13;
          }
          while( v33 );
          v16 = v38;
        }
        v9 = v21;
        PteAddress = &a7[-8 * v19];
        v45 = v21;
        v21 = -1i64;
      }
      v12 = VadCacheAttribute;
LABEL_30:
      PteAddress += 8 * v19;
      v11 = v39;
      ++v21;
LABEL_40:
      v22 = a2;
      v17 = a4;
      goto LABEL_41;
    }
LABEL_39:
    v16 = -1073741800;
    v45 = v21;
    v9 = v21;
    v38 = -1073741800;
    v21 = 0i64;
    goto LABEL_40;
  }
LABEL_43:
  *a5 = v8;
  return v16;
}

Referenced by:

NtMapUserPhysicalPages
NtMapUserPhysicalPagesScatter