MiReferenceIncomingPhysicalPages

NTSTATUS __stdcall MiReferenceIncomingPhysicalPages(
        _EX_CALLBACK_ROUTINE_BLOCK *CallBackBlock,
        INT64 a2,
        UINT64 r8_0,
        INT64 r9_0,
        INT64 a5,
        INT64 a6,
        INT64 a7){
  UINT64 v8; 
  UINT64 v9; 
  INT64 v10; 
  int v11; 
  unsigned int v12; 
  unsigned __int64 v13; 
  unsigned __int64 v14; 
  INT64 v15; 
  unsigned __int64 v16; 
  NTSTATUS v17; 
  INT64 v18; 
  INT64 PteAddress; 
  unsigned __int64 v20; 
  const signed __int64 *v21; 
  unsigned __int64 v22; 
  INT64 v23; 
  unsigned __int64 v24; 
  unsigned __int64 v25; 
  __int64 v26; 
  _MMPTE *AweNode; 
  unsigned __int64 v28; 
  __int64 v29; 
  unsigned __int64 v30; 
  unsigned __int64 v31; 
  INT64 v32; 
  unsigned __int64 v33; 
  _MMPFN *v34; 
  unsigned __int64 v35; 
  __int64 v36; 
  VOID *v37; 
  unsigned __int64 v38; 
  NTSTATUS v40; 
  int v41; 
  UINT64 ReserveSize; 
  unsigned __int64 v43; 
  const signed __int64 *v44; 
  UINT64 v47; 
  unsigned int a6a; 

  v47 = r8_0;
  v8 = 0i64;
  v9 = r8_0;
  v40 = 0;
  v10 = *(_QWORD *)(a6 + 24);
  v11 = *(_DWORD *)(v10 + 48) & 0x1100000;
  v41 = v11;
  a6a = MiGetVadCacheAttribute(v10);
  v12 = a6a;
  ReserveSize = 0i64;
  ExGetCallBackBlockRoutine(CallBackBlock);
  v14 = v13;
  LODWORD(v16) = MiGetAweViewPageSize(v15);
  PteAddress = a7;
  v20 = v14;
  if( v16 )
    v20 = v16;
  v43 = *((_QWORD *)CallBackBlock + 2);
  v21 = (const signed __int64 *)*((_QWORD *)CallBackBlock + 3);
  v22 = 0i64;
  v44 = v21;
  if( v9 )
  {
    v23 = a2;
    while( 1 )
    {
      v24 = *(_QWORD *)(v23 + 8 * v22);
      if( !v18 )
        goto LABEL_18;
      if( v24 )
        break;
      ++v22;
LABEL_41:
      if( v22 >= v9 )
      {
        v8 = ReserveSize;
        goto LABEL_43;
      }
    }
    PteAddress = (INT64)MiGetPteAddress(*(VOID **)(v18 + 8 * v22));
    if( v25 < (*(unsigned int *)(v26 + 24) | ((unsigned __int64)*(unsigned __int8 *)(v26 + 32) << 32)) << 12
      || v25 > (((*(unsigned int *)(v26 + 28) | ((unsigned __int64)*(unsigned __int8 *)(v26 + 33) << 32)) << 12) | 0xFFF) )
    {
      AweNode = MiGetAweNode(v25);
      LODWORD(v28) = MiGetAweViewPageSize((INT64)AweNode);
      v30 = v28;
      v31 = v14;
      if( v30 )
        v31 = v30;
      if( v31 != v20 )
        goto LABEL_39;
      v32 = *(_QWORD *)(v29 + 24);
      if( (*(_DWORD *)(v32 + 48) & 0x1100000) == 17825792 )
      {
        if( v11 != 17825792 )
          goto LABEL_39;
      }
      else if( v11 == 17825792 )
      {
        goto LABEL_39;
      }
      if( v12 != MiGetVadCacheAttribute(v32) )
        goto LABEL_39;
    }
LABEL_18:
    if( v24 / v14 < v43 && ((v20 - 1) & v24) == 0 )
    {
      v33 = 0i64;
      if( v20 / v14 )
      {
        while( _bittest64(v44, v24 / v14 + v33) )
        {
          if( ++v33 >= v20 / v14 )
            goto LABEL_23;
        }
        goto LABEL_39;
      }
LABEL_23:
      v17 = v40;
      v34 = &MmGetPfnDb()[v24];
      if( v40 < 0 )
      {
        v38 = 0i64;
        if( v20 )
        {
          do
          {
            MiDecrementAweMapCount((VOID *)v33, (UINT64)v34, (UINT64)&ReserveSize);
            v34 += v14;
            v38 += v14;
          }
          while( v38 < v20 );
          v17 = v40;
LABEL_28:
          v9 = v47;
        }
      }
      else
      {
        v35 = 0i64;
        if( !v20 )
        {
          v9 = v47;
          goto LABEL_30;
        }
        v36 = 48 * v14;
        while( 1 )
        {
          v40 = MiIncrementAweMapCount((INT64)CallBackBlock, (INT64)v34, a6a, PteAddress);
          v17 = v40;
          if( v40 < 0 )
            break;
          v35 += v14;
          v36 = 48 * v14;
          v34 += v14;
          if( v35 >= v20 )
            goto LABEL_28;
        }
        if( v35 )
        {
          do
          {
            v34 = (_MMPFN *)((char *)v34 - v36);
            MiDecrementAweMapCount(v37, (UINT64)v34, (UINT64)&ReserveSize);
            v35 -= v14;
          }
          while( v35 );
          v17 = v40;
        }
        v9 = v22;
        PteAddress = a7 - 8 * v20;
        v47 = v22;
        v22 = -1i64;
      }
      v12 = a6a;
LABEL_30:
      PteAddress += 8 * v20;
      v11 = v41;
      ++v22;
LABEL_40:
      v23 = a2;
      v18 = r9_0;
      goto LABEL_41;
    }
LABEL_39:
    v17 = -1073741800;
    v47 = v22;
    v9 = v22;
    v40 = -1073741800;
    v22 = 0i64;
    goto LABEL_40;
  }
LABEL_43:
  *(_QWORD *)a5 = v8;
  return v17;
}

Referenced by:

NtMapUserPhysicalPages
NtMapUserPhysicalPagesScatter