ExtEnvAllocatePhysicalMemory

INT64 __fastcall ExtEnvAllocatePhysicalMemory(
        INT64 a1,
        UINT64 a2,
        UINT64 a3,
        MEMORY_CACHING_TYPE CacheType,
        LARGE_INTEGER *a5,
        INT64 a6){
  PVOID *v6; 
  _LARGE_INTEGER v8; 
  __int64 v9; 
  unsigned int v10; 
  unsigned __int64 v11; 
  bool v12; 
  __int64 v13; 
  char v14; 
  char v15; 
  __int64 v16; 
  __int64 v17; 
  LONGLONG v18; 
  PVOID v19; 
  void *v21; 
  UINT64 v22; 
  __int64 v23; 
  unsigned __int8 v24; 
  unsigned __int64 v25; 
  __int64 *v26; 
  UINT64 PreferredNode; 
  v6 = (PVOID *)a6;
  v8.QuadPart = (unsigned int)a3;
  v9 = (unsigned int)a2;
  *(_QWORD *)a6 = 0i64;
  if( !(_DWORD)a2 )
    return(unsigned int)-1073741637;
  v11 = (unsigned int)a3 >> 12;
  if( (unsigned int)a3 >> 12 << 12 != (_DWORD)a3 )
    return(unsigned int)-1073741637;
  v12 = !_BitScanReverse64((unsigned __int64 *)&v13, v11);
  v14 = -1;
  v15 = -1;
  if( !v12 )
    v15 = v13;
  v12 = !_BitScanForward64((unsigned __int64 *)&v16, v11);
  if( !v12 )
    v14 = v16;
  if( v15 != v14 || (unsigned int)CacheType > MmCached )
    return(unsigned int)-1073741637;
  v10 = 0;
  v17 = (unsigned int)(v9 + 4095) >> 12 << 12;
  if( !HalpExtEnvAllocationPhase )
  {
    a6 = -1i64;
    v18 = HalpAllocPhysicalMemory(HalpExtEnvLoaderBlock, (LARGE_INTEGER *)&a6);
    a5->QuadPart = v18;
    if( v18 )
    {
      v19 = HalMapIoSpace((LARGE_INTEGER)v18, (unsigned int)v17, CacheType);
      *v6 = v19;
      if( v19 )
        return v10;
    }
    goto LABEL_13;
  }
  if( HalpExtEnvAllocationPhase != 1
    || (LODWORD(PreferredNode) = 0x80000000,
        a6 = -1i64,
        MmAllocateContiguousMemorySpecifyCacheNode(
          (unsigned int)(v9 + 4095) >> 12 << 12,
          0i64,
          (_LARGE_INTEGER)-1i64,
          v8,
          CacheType,
          PreferredNode),
        (*v6 = v21) == 0i64)
    || (*a5 = MmGetPhysicalAddress(v21), (v23 = HalpMmAllocCtxAlloc((__int64)a5, 56i64)) == 0) )
  {
LABEL_13:
    v10 = -1073741670;
    if( *v6 && HalpExtEnvAllocationPhase == 1 )
      MmFreeContiguousMemory(*v6);
    return v10;
  }
  *(_OWORD *)v23 = 0i64;
  *(_OWORD *)(v23 + 16) = 0i64;
  *(_OWORD *)(v23 + 32) = 0i64;
  *(_QWORD *)(v23 + 48) = 0i64;
  *(_QWORD *)(v23 + 16) = *v6;
  *(_QWORD *)(v23 + 32) = v9;
  *(_QWORD *)(v23 + 40) = v17;
  *(_DWORD *)(v23 + 48) = CacheType;
  KeAcquireSpinLockRaiseToDpc(&ExtEnvAllocationLock, v22);
  v25 = v24;
  v26 = (__int64 *)qword_140C48B88;
  if( *(__int64 **)qword_140C48B88 != &ExtEnvAllocationList )
    __fastfail(3u);
  *(_QWORD *)v23 = &ExtEnvAllocationList;
  *(_QWORD *)(v23 + 8) = v26;
  *v26 = v23;
  qword_140C48B88 = v23;
  KxReleaseSpinLock(&ExtEnvAllocationLock);
  __writecr8(v25);
  return v10;
}

Referenced by:

HsaAllocateRemappingTableEntry
IvtAllocateContextTable