MiInsertPhysicalPteMapping

INT64 __fastcall MiInsertPhysicalPteMapping(UINT64 a1, UINT64 BugCheckParameter2, UINT64 a3){
  unsigned int ProtectionPfnCompatible; 
  unsigned __int64 v6; 
  int v7; 
  __int64 v8; 
  RTL_BALANCED_NODE *v9; 
  int v10; 
  _MMPTE *PteBase; 
  _MMPFN *UsedPtesHandle; 
  unsigned __int64 *v13; 
  unsigned __int64 v14; 
  volatile signed __int32 *v15; 
  INT64 result; 
  char *v17; 
  unsigned __int8 v18; 
  UINT64 SpinCount; 
  ProtectionPfnCompatible = a3;
  v7 = MiProtectionToCacheAttribute((unsigned int)a3);
  if( v6 <= 0xFFFFFFFFFi64 && (v8 = 6 * v6, ((*((_QWORD *)MmGetPfnDb() + v8 + 5) >> 50) & 1) != 0) )
  {
    v17 = (char *)MmGetPfnDb() + 8 * v8;
    if( !*((_WORD *)v17 + 16) )
      MiShowBadMapper(BugCheckParameter2, 4ui64);
    v18 = v17[34];
    if( (v18 & 0xC0) == 0xC0 )
    {
      MiAssignInitialPageAttribute((INT64)v17, v7);
      v18 = v17[34];
    }
    if( v18 >> 6 != v7 )
      ProtectionPfnCompatible = MiMakeProtectionPfnCompatible(ProtectionPfnCompatible, (_MMPFN *)v17);
  }
  else
  {
    v9 = MiLookupIoPageNode(BugCheckParameter2);
    if( !v9 )
      KeBugCheckEx(0x1Au, 0x61949ui64, BugCheckParameter2, 1ui64, 0i64);
    ProtectionPfnCompatible &= 7u;
    v10 = *((unsigned __int16 *)v9[2].Children[0]->Children + (BugCheckParameter2 & 0xFFFFFFFFFi64) - v9[1].ParentValue) >> 14;
    if( v10 )
    {
      if( v10 == 2 )
        ProtectionPfnCompatible |= 0x18u;
    }
    else
    {
      ProtectionPfnCompatible |= 8u;
    }
  }
  *(_QWORD *)a1 = MiMakeValidPte(a1, BugCheckParameter2, ProtectionPfnCompatible | 0x80000000);
  PteBase = MmGetPteBase();
  UsedPtesHandle = MiGetUsedPtesHandle((__int64)((a1 << 25) - ((_QWORD)PteBase << 25)) >> 16);
  MiIncreaseUsedPtesCount(UsedPtesHandle, 1ui64);
  v13 = (unsigned __int64 *)((char *)PteBase + ((a1 >> 9) & 0x7FFFFFFFF8i64));
  v14 = *v13;
  MiPteInShadowRange((UINT64)v13);
  LODWORD(SpinCount) = 0;
  v15 = (volatile signed __int32 *)((char *)MmGetPfnDb() + 48 * ((v14 >> 12) & 0xFFFFFFFFFi64) + 24);
  while( _interlockedbittestandset64(v15, 0x3Fui64) )
  {
    do
      KeYieldProcessorEx(&SpinCount);
    while( *(__int64 *)v15 < 0 );
  }
  result = 0x7FFFFFFFFFFFFFFFi64;
  *(_QWORD *)v15 ^= (*(_QWORD *)v15 ^ (*(_QWORD *)v15 + 1i64)) & 0x3FFFFFFFFFFFFFFFi64;
  _InterlockedAnd64((volatile signed __int64 *)v15, 0x7FFFFFFFFFFFFFFFui64);
  return result;
}

Referenced by:

MiInsertViewOfPhysicalSection
MiMapLockedPagesInUserSpaceHelper