diff --git a/dxcore_loader.cpp b/dxcore_loader.cpp new file mode 100644 index 0000000000..ec6a4c8e87 --- /dev/null +++ b/dxcore_loader.cpp @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved. + */ + +#include "dxcore_loader.h" +#include "librocdxg.h" +#include +#include +#include +#include + +namespace wsl { +namespace thunk { +namespace dxcore { + +DxcoreLoader::DxcoreLoader() + : dxcore_handle_(nullptr) + , init_flag_() + , pfn_D3DKMTCreateAllocation2(nullptr) + , pfn_D3DKMTDestroyAllocation2(nullptr) + , pfn_D3DKMTMapGpuVirtualAddress(nullptr) + , pfn_D3DKMTReserveGpuVirtualAddress(nullptr) + , pfn_D3DKMTFreeGpuVirtualAddress(nullptr) + , pfn_D3DKMTCreateDevice(nullptr) + , pfn_D3DKMTDestroyDevice(nullptr) + , pfn_D3DKMTEnumAdapters2(nullptr) + , pfn_D3DKMTQueryAdapterInfo(nullptr) + , pfn_D3DKMTCreateContextVirtual(nullptr) + , pfn_D3DKMTDestroyContext(nullptr) + , pfn_D3DKMTSubmitCommand(nullptr) + , pfn_D3DKMTCreateSynchronizationObject2(nullptr) + , pfn_D3DKMTDestroySynchronizationObject(nullptr) + , pfn_D3DKMTQueryStatistics(nullptr) + , pfn_D3DKMTEscape(nullptr) + , pfn_D3DKMTLock2(nullptr) + , pfn_D3DKMTUnlock2(nullptr) + , pfn_D3DKMTCreatePagingQueue(nullptr) + , pfn_D3DKMTDestroyPagingQueue(nullptr) + , pfn_D3DKMTWaitForSynchronizationObjectFromGpu(nullptr) + , pfn_D3DKMTSignalSynchronizationObjectFromGpu(nullptr) + , pfn_D3DKMTWaitForSynchronizationObjectFromCpu(nullptr) + , pfn_D3DKMTQueryClockCalibration(nullptr) + , pfn_D3DKMTMakeResident(nullptr) + , pfn_D3DKMTEvict(nullptr) + , pfn_D3DKMTShareObjects(nullptr) + , pfn_D3DKMTQueryResourceInfoFromNtHandle(nullptr) + , pfn_D3DKMTOpenResourceFromNtHandle(nullptr) + , pfn_D3DKMTCreateHwQueue(nullptr) + , pfn_D3DKMTDestroyHwQueue(nullptr) + , pfn_D3DKMTSubmitCommandToHwQueue(nullptr) { +} + +DxcoreLoader::~DxcoreLoader() { + Shutdown(); +} + +bool DxcoreLoader::Initialize() { + std::call_once(init_flag_, [this]() { + // Attempt to load libdxcore.so + dlerror(); // Clear error + dxcore_handle_ = dlopen("libdxcore.so", RTLD_LAZY); + + if (!dxcore_handle_) { + pr_err("[DxcoreLoader] Cannot load libdxcore.so: %s\n", dlerror()); + return; + } + + pr_info("[DxcoreLoader] libdxcore.so loaded successfully\n"); + if (!LoadDxcoreApis()) { + // If API loading failed, close the handle to indicate failure + dlclose(dxcore_handle_); + dxcore_handle_ = nullptr; + return; + } + }); + + return IsLoaded(); +} + +void DxcoreLoader::Shutdown() { + if (dxcore_handle_) { + if (dlclose(dxcore_handle_) != 0) { + pr_err("[DxcoreLoader] Cannot unload libdxcore.so: %s\n", dlerror()); + } else { + pr_info("[DxcoreLoader] libdxcore.so unloaded successfully\n"); + } + dxcore_handle_ = nullptr; + } +} + +bool DxcoreLoader::LoadDxcoreApis() { + if (!dxcore_handle_) { + pr_err("[DxcoreLoader] Error: dxcore_handle_ is null\n"); + return false; + } + + dlerror(); // Clear error + + // Load all D3DKMT functions + #define LOAD_DXCORE_API(func_name) \ + DXCORE_PFN(func_name) = (DXCORE_DEF(func_name)*)dlsym(dxcore_handle_, #func_name); \ + if (!DXCORE_PFN(func_name)) { \ + pr_err("[DxcoreLoader] Failed to load " #func_name ": %s\n", dlerror()); \ + goto ERROR; \ + } + + LOAD_DXCORE_API(D3DKMTCreateAllocation2); + LOAD_DXCORE_API(D3DKMTDestroyAllocation2); + LOAD_DXCORE_API(D3DKMTMapGpuVirtualAddress); + LOAD_DXCORE_API(D3DKMTReserveGpuVirtualAddress); + LOAD_DXCORE_API(D3DKMTFreeGpuVirtualAddress); + LOAD_DXCORE_API(D3DKMTCreateDevice); + LOAD_DXCORE_API(D3DKMTDestroyDevice); + LOAD_DXCORE_API(D3DKMTEnumAdapters2); + LOAD_DXCORE_API(D3DKMTQueryAdapterInfo); + LOAD_DXCORE_API(D3DKMTCreateContextVirtual); + LOAD_DXCORE_API(D3DKMTDestroyContext); + LOAD_DXCORE_API(D3DKMTSubmitCommand); + LOAD_DXCORE_API(D3DKMTCreateSynchronizationObject2); + LOAD_DXCORE_API(D3DKMTDestroySynchronizationObject); + LOAD_DXCORE_API(D3DKMTQueryStatistics); + LOAD_DXCORE_API(D3DKMTEscape); + LOAD_DXCORE_API(D3DKMTLock2); + LOAD_DXCORE_API(D3DKMTUnlock2); + LOAD_DXCORE_API(D3DKMTCreatePagingQueue); + LOAD_DXCORE_API(D3DKMTDestroyPagingQueue); + LOAD_DXCORE_API(D3DKMTWaitForSynchronizationObjectFromGpu); + LOAD_DXCORE_API(D3DKMTSignalSynchronizationObjectFromGpu); + LOAD_DXCORE_API(D3DKMTWaitForSynchronizationObjectFromCpu); + LOAD_DXCORE_API(D3DKMTQueryClockCalibration); + LOAD_DXCORE_API(D3DKMTMakeResident); + LOAD_DXCORE_API(D3DKMTEvict); + LOAD_DXCORE_API(D3DKMTShareObjects); + LOAD_DXCORE_API(D3DKMTQueryResourceInfoFromNtHandle); + LOAD_DXCORE_API(D3DKMTOpenResourceFromNtHandle); + LOAD_DXCORE_API(D3DKMTCreateHwQueue); + LOAD_DXCORE_API(D3DKMTDestroyHwQueue); + LOAD_DXCORE_API(D3DKMTSubmitCommandToHwQueue); + + #undef LOAD_DXCORE_API + + pr_info("[DxcoreLoader] All DXCore APIs loaded successfully\n"); + return true; +ERROR: + pr_err("[DxcoreLoader] Failed to load DXCore APIs\n"); + return false; +} + +} // namespace dxcore +} // namespace thunk +} // namespace wsl diff --git a/dxcore_loader.h b/dxcore_loader.h new file mode 100644 index 0000000000..3f649a4da0 --- /dev/null +++ b/dxcore_loader.h @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef LIBROCDXG_DXCORE_LOADER_H +#define LIBROCDXG_DXCORE_LOADER_H + +#include "impl/wddm/types.h" +#include +#include + +#define DXCORE_CALL(function_name) wsl::thunk::dxcore::DxcoreLoader::Instance().pfn_##function_name + +namespace wsl { +namespace thunk { +namespace dxcore { + +/** + * @brief DxcoreLoader class for dynamic loading of libdxcore.so + * + * This class provides a singleton loader for the DXCore library, allowing + * optional loading based on environment variable LIBROCDXG_ENABLE_DXCORE. + * Supported values: "1", "true", "yes" (case-sensitive). + * If not set or invalid, fallback to stub implementations. + * + * Thread-safe initialization using std::call_once. + */ + +// Macro definitions mimicking HSAKMT design +#define DXCORE_DEF(function_name) PFN##function_name +#define DXCORE_PFN(function_name) pfn_##function_name + +class DxcoreLoader { +public: + // D3DKMT function type definitions + typedef NTSTATUS (DXCORE_DEF(D3DKMTCreateAllocation2))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTDestroyAllocation2))(void *args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTMapGpuVirtualAddress))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTReserveGpuVirtualAddress))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTFreeGpuVirtualAddress))(void *args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTCreateDevice))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTDestroyDevice))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTEnumAdapters2))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTQueryAdapterInfo))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTCreateContextVirtual))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTDestroyContext))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTSubmitCommand))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTCreateSynchronizationObject2))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTDestroySynchronizationObject))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTQueryStatistics))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTEscape))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTLock2))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTUnlock2))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTCreatePagingQueue))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTDestroyPagingQueue))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTWaitForSynchronizationObjectFromGpu))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTSignalSynchronizationObjectFromGpu))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTWaitForSynchronizationObjectFromCpu))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTQueryClockCalibration))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTMakeResident))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTEvict))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTShareObjects))(size_t num_allocations, WinResourceHandle* resource, OBJECT_ATTRIBUTES* obj_attr, uint32_t flags, void** nt_handle); + typedef NTSTATUS (DXCORE_DEF(D3DKMTQueryResourceInfoFromNtHandle))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTOpenResourceFromNtHandle))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTCreateHwQueue))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTDestroyHwQueue))(void* args); + typedef NTSTATUS (DXCORE_DEF(D3DKMTSubmitCommandToHwQueue))(void* args); + + static DxcoreLoader& Instance() { + static DxcoreLoader* instance = new DxcoreLoader(); + return (*instance); + } + + bool Initialize(); + void Shutdown(); + bool IsLoaded() const { return dxcore_handle_ != nullptr; } + + // Function pointer declarations + DXCORE_DEF(D3DKMTCreateAllocation2)* DXCORE_PFN(D3DKMTCreateAllocation2); + DXCORE_DEF(D3DKMTDestroyAllocation2)* DXCORE_PFN(D3DKMTDestroyAllocation2); + DXCORE_DEF(D3DKMTMapGpuVirtualAddress)* DXCORE_PFN(D3DKMTMapGpuVirtualAddress); + DXCORE_DEF(D3DKMTReserveGpuVirtualAddress)* DXCORE_PFN(D3DKMTReserveGpuVirtualAddress); + DXCORE_DEF(D3DKMTFreeGpuVirtualAddress)* DXCORE_PFN(D3DKMTFreeGpuVirtualAddress); + DXCORE_DEF(D3DKMTCreateDevice)* DXCORE_PFN(D3DKMTCreateDevice); + DXCORE_DEF(D3DKMTDestroyDevice)* DXCORE_PFN(D3DKMTDestroyDevice); + DXCORE_DEF(D3DKMTEnumAdapters2)* DXCORE_PFN(D3DKMTEnumAdapters2); + DXCORE_DEF(D3DKMTQueryAdapterInfo)* DXCORE_PFN(D3DKMTQueryAdapterInfo); + DXCORE_DEF(D3DKMTCreateContextVirtual)* DXCORE_PFN(D3DKMTCreateContextVirtual); + DXCORE_DEF(D3DKMTDestroyContext)* DXCORE_PFN(D3DKMTDestroyContext); + DXCORE_DEF(D3DKMTSubmitCommand)* DXCORE_PFN(D3DKMTSubmitCommand); + DXCORE_DEF(D3DKMTCreateSynchronizationObject2)* DXCORE_PFN(D3DKMTCreateSynchronizationObject2); + DXCORE_DEF(D3DKMTDestroySynchronizationObject)* DXCORE_PFN(D3DKMTDestroySynchronizationObject); + DXCORE_DEF(D3DKMTQueryStatistics)* DXCORE_PFN(D3DKMTQueryStatistics); + DXCORE_DEF(D3DKMTEscape)* DXCORE_PFN(D3DKMTEscape); + DXCORE_DEF(D3DKMTLock2)* DXCORE_PFN(D3DKMTLock2); + DXCORE_DEF(D3DKMTUnlock2)* DXCORE_PFN(D3DKMTUnlock2); + DXCORE_DEF(D3DKMTCreatePagingQueue)* DXCORE_PFN(D3DKMTCreatePagingQueue); + DXCORE_DEF(D3DKMTDestroyPagingQueue)* DXCORE_PFN(D3DKMTDestroyPagingQueue); + DXCORE_DEF(D3DKMTWaitForSynchronizationObjectFromGpu)* DXCORE_PFN(D3DKMTWaitForSynchronizationObjectFromGpu); + DXCORE_DEF(D3DKMTSignalSynchronizationObjectFromGpu)* DXCORE_PFN(D3DKMTSignalSynchronizationObjectFromGpu); + DXCORE_DEF(D3DKMTWaitForSynchronizationObjectFromCpu)* DXCORE_PFN(D3DKMTWaitForSynchronizationObjectFromCpu); + DXCORE_DEF(D3DKMTQueryClockCalibration)* DXCORE_PFN(D3DKMTQueryClockCalibration); + DXCORE_DEF(D3DKMTMakeResident)* DXCORE_PFN(D3DKMTMakeResident); + DXCORE_DEF(D3DKMTEvict)* DXCORE_PFN(D3DKMTEvict); + DXCORE_DEF(D3DKMTShareObjects)* DXCORE_PFN(D3DKMTShareObjects); + DXCORE_DEF(D3DKMTQueryResourceInfoFromNtHandle)* DXCORE_PFN(D3DKMTQueryResourceInfoFromNtHandle); + DXCORE_DEF(D3DKMTOpenResourceFromNtHandle)* DXCORE_PFN(D3DKMTOpenResourceFromNtHandle); + DXCORE_DEF(D3DKMTCreateHwQueue)* DXCORE_PFN(D3DKMTCreateHwQueue); + DXCORE_DEF(D3DKMTDestroyHwQueue)* DXCORE_PFN(D3DKMTDestroyHwQueue); + DXCORE_DEF(D3DKMTSubmitCommandToHwQueue)* DXCORE_PFN(D3DKMTSubmitCommandToHwQueue); + +private: + DxcoreLoader(); + ~DxcoreLoader(); + + bool LoadDxcoreApis(); + + void* dxcore_handle_; + std::once_flag init_flag_; // For thread-safe initialization + + // Disable copy + DxcoreLoader(const DxcoreLoader&) = delete; + DxcoreLoader& operator=(const DxcoreLoader&) = delete; +}; + +} // namespace dxcore +} // namespace thunk +} // namespace wsl + +#endif // LIBROCDXG_DXCORE_LOADER_H diff --git a/openclose.cpp b/openclose.cpp index 6be1b24b86..d903e2aa44 100644 --- a/openclose.cpp +++ b/openclose.cpp @@ -556,6 +556,11 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtOpenKFD(void) { dxg_runtime->dxg_fd = fd; } + if (!wsl::thunk::dxcore::DxcoreLoader::Instance().Initialize()) { + pr_err("Failed to load libdxcore.so\n"); + result = HSAKMT_STATUS_ERROR; + goto dxcore_loader_failed; + } hsakmt_hsa_loader_init(); init_page_size(); @@ -563,10 +568,6 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtOpenKFD(void) { char *useSvmStr = getenv("HSA_USE_SVM"); dxg_runtime->is_svm_api_supported = !(useSvmStr && !strcmp(useSvmStr, "0")) && false; - // result = topology_sysfs_get_system_props(&sys_props); - if (result != HSAKMT_STATUS_SUCCESS) - goto topology_sysfs_failed; - dxg_runtime->dxg_open_count = 1; if (!atfork_installed) { @@ -587,7 +588,7 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtOpenKFD(void) { reset_suballocator(); pthread_mutex_unlock(&dxg_runtime->hsakmt_mutex); return result; -topology_sysfs_failed: +dxcore_loader_failed: close(fd); open_failed: pthread_mutex_unlock(&dxg_runtime->hsakmt_mutex); @@ -604,6 +605,7 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtCloseKFD(void) { if (--dxg_runtime->dxg_open_count == 0) { close(dxg_runtime->dxg_fd); dxg_runtime->dxg_fd = -1; + wsl::thunk::dxcore::DxcoreLoader::Instance().Shutdown(); } result = HSAKMT_STATUS_SUCCESS;