MiMapUserLargePages

INT64 __fastcall MiMapUserLargePages(__int64 a1){
  unsigned int v2; 
  INT64 v3; 
  unsigned __int64 v4; 
  unsigned __int64 v5; 
  INT64 *v6; 
  CHAR v7; 
  INT64 result; 
  unsigned int v9; 
  unsigned __int64 v10; 
  unsigned __int64 v11; 
  __int64 v12; 
  unsigned int v13; 
  unsigned int LargestPageIndex; 
  UINT8 v15; 
  char *v16; 
  UINT64 v17; 
  unsigned __int64 v18; 
  _MMPTE *PteBase; 
  unsigned int v20; 
  bool i; 
  unsigned __int64 v22; 
  __int64 v23; 
  UINT64 v24; 
  __int64 v25; 
  int v26; 
  int v27; 
  INT64 a5; 
  int v29; 
  unsigned int v30; 
  int v31; 
  unsigned int v32; 
  int v33; 
  ULONG_PTR BugCheckParameter1; 
  unsigned __int64 v35; 
  INT64 v36; 
  char *v37; 
  __int64 a3; 
  __int64 v39; 
  unsigned __int64 v40; 
  _QWORD v41[2]; 
  __int128 v42; 
  int v44; 
  *(_OWORD *)v41 = 0i64;
  v42 = 0i64;
  v2 = MiComputePreferredNode(a1);
  v32 = v2;
  v36 = *((_QWORD *)KeGetCurrentThread() + 23);
  v3 = v36 + 1664;
  v4 = (*(unsigned int *)(a1 + 24) | ((unsigned __int64)*(unsigned __int8 *)(a1 + 32) << 32)) << 12;
  v5 = (((*(unsigned int *)(a1 + 28) | ((unsigned __int64)*(unsigned __int8 *)(a1 + 33) << 32)) << 12) | 0xFFF) + 1;
  v40 = v5;
  result = MiCreateLargePfnList(a1, (v5 - v4) >> 12, v7, v6, (CHAR *)v41);
  if( (int)result < 0 )
    return result;
  v9 = *(_DWORD *)(a1 + 48);
  v10 = 0x140000000ui64;
  v11 = v9;
  v12 = (v9 >> 18) & 3;
  if( LODWORD(MiVadPageSizes[v12]) == 16 )
  {
    v13 = 2;
LABEL_4:
    LargestPageIndex = v13;
    goto LABEL_8;
  }
  if( (v9 & 0x500000) == 5242880 )
  {
    v13 = MiVadPageIndices[v12];
    goto LABEL_4;
  }
  v13 = 1;
  LargestPageIndex = MiGetLargestPageIndex();
LABEL_8:
  v31 = 0;
  v44 = 0;
  v15 = 17;
  v29 = 0;
  v16 = 0i64;
  v30 = LargestPageIndex;
  v17 = 0i64;
  v18 = v4;
  v35 = v4;
  PteBase = MmGetPteBase();
  v33 = *(_DWORD *)(v10 + 4 * ((v11 >> 7) & 0x1F) + 95168);
  BugCheckParameter1 = (ULONG_PTR)PteBase + ((v4 >> 9) & 0x7FFFFFFFF8i64);
  if( v4 >= v5 )
    return 0i64;
  do
  {
    v20 = LargestPageIndex;
    for( i = LargestPageIndex <= v13; i; i = v20 <= v13 )
    {
      v22 = *(_QWORD *)(v10 + 8i64 * v20 + 108432) << 12;
      if( !(v18 % v22) && v5 - v18 >= v22 )
      {
        LODWORD(v23) = v20;
        if( v41[v20] )
        {
LABEL_17:
          if( (unsigned int)v23 <= v13 )
          {
            v20 = v23;
            break;
          }
        }
        else
        {
          while( 1 )
          {
            v23 = (unsigned int)(v23 + 1);
            if( (unsigned int)v23 > v13 )
              break;
            if( v41[v23] )
              goto LABEL_17;
          }
        }
        MiDemotePfnListChain((INT64)v41, v20);
        v10 = 0x140000000ui64;
        PteBase = MmGetPteBase();
        break;
      }
      ++v20;
    }
    v37 = (char *)v41[v20];
    v41[v20] = *(_QWORD *)v37;
    v24 = BugCheckParameter1;
    v39 = *(_QWORD *)(v10 + 8i64 * v20 + 108432);
    a3 = (v37 - (char *)MmGetPfnDb()) / 48;
    if( v20 < 2 )
    {
      v25 = 2 - v20;
      do
      {
        v24 = (UINT64)PteBase + ((v24 >> 9) & 0x7FFFFFFFF8i64);
        --v25;
      }
      while( v25 );
      v2 = v32;
    }
    if( ((v24 ^ v17) & 0xFFFFFFFFFFFFF000ui64) != 0 )
    {
      if( v16 )
      {
        MiUnlockPageTable(v3, (UINT64)v16);
        v16 = 0i64;
      }
      v26 = v29;
      if( v29 == 1 )
      {
        if( v20 )
        {
          v27 = 1;
          v44 = 1;
        }
        else
        {
          v27 = v44;
        }
      }
      else
      {
        v27 = v44;
        if( !v20 )
          v27 = 1;
        v44 = v27;
      }
      if( v27 )
      {
        v44 = 0;
        if( v15 == 17 )
          goto LABEL_41;
        if( v29 == 1 )
          MiUnlockWorkingSetExclusive(v3, v15);
        else
          MiUnlockWorkingSetShared(v3, v15);
        v15 = 17;
      }
      if( v15 != 17 )
      {
LABEL_45:
        if( !v26 )
          v16 = (char *)MmGetPteBase() + ((v24 >> 9) & 0x7FFFFFFFF8i64);
        MiMakeSystemAddressValid(v24, 0i64, v2 + 1, v15, v26);
        goto LABEL_48;
      }
LABEL_41:
      if( !v20 )
      {
        v29 = 1;
        v26 = 1;
        v15 = MiLockWorkingSetForLargeMapping(v3, 1i64, v2);
      }
      if( v15 == 17 )
      {
        v26 = 0;
        v29 = 0;
        v15 = MiLockWorkingSetShared(v3);
      }
      goto LABEL_45;
    }
LABEL_48:
    MiInitializeLargeUserBasePfn((INT64)v37, BugCheckParameter1, v36);
    LODWORD(a5) = v33;
    MiInsertLargeUserMapping(a1, BugCheckParameter1, a3, v20, a5);
    v35 += v39 << 12;
    BugCheckParameter1 += 8 * v39;
    if( (++v31 & 0xF) == 0 && MiWorkingSetIsContended(v3) || MiShouldYieldProcessor() )
    {
      v17 = 0i64;
      v44 = 1;
    }
    else
    {
      v17 = v24;
    }
    v5 = v40;
    v10 = 0x140000000ui64;
    v18 = v35;
    LargestPageIndex = v30;
    PteBase = MmGetPteBase();
  }
  while( v35 < v40 );
  if( v16 )
    MiUnlockPageTable(v3, (UINT64)v16);
  if( v15 != 17 )
  {
    if( v29 == 1 )
      MiUnlockWorkingSetExclusive(v3, v15);
    else
      MiUnlockWorkingSetShared(v3, v15);
  }
  return 0i64;
}

Referenced by:

MiCopyLargeVad
MiMapViewOfImageSection
MiReserveUserMemory