diff --git a/level_zero/sysman/source/shared/windows/pmt/sysman_pmt.cpp b/level_zero/sysman/source/shared/windows/pmt/sysman_pmt.cpp index 5c6f322ddf..84a3c7cb08 100644 --- a/level_zero/sysman/source/shared/windows/pmt/sysman_pmt.cpp +++ b/level_zero/sysman/source/shared/windows/pmt/sysman_pmt.cpp @@ -13,7 +13,7 @@ namespace L0 { namespace Sysman { -ze_result_t PlatformMonitoringTech::readValue(const std::string key, uint32_t &value) { +ze_result_t PlatformMonitoringTech::readValue(const std::string &key, uint32_t &value) { auto offset = keyOffsetMap.find(key); if (offset == keyOffsetMap.end()) { @@ -43,7 +43,7 @@ ze_result_t PlatformMonitoringTech::readValue(const std::string key, uint32_t &v return ZE_RESULT_ERROR_UNKNOWN; } -ze_result_t PlatformMonitoringTech::readValue(const std::string key, uint64_t &value) { +ze_result_t PlatformMonitoringTech::readValue(const std::string &key, uint64_t &value) { auto offset = keyOffsetMap.find(key); if (offset == keyOffsetMap.end()) { NEO::printDebugString(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, @@ -73,13 +73,12 @@ ze_result_t PlatformMonitoringTech::readValue(const std::string key, uint64_t &v } ze_result_t PlatformMonitoringTech::getGuid() { - int status; + ze_result_t status; unsigned long sizeNeeded; - PmtSysman::PmtTelemetryDiscovery *telemetryDiscovery; + PmtSysman::PmtTelemetryDiscovery *telemetryDiscovery = nullptr; // Get Telmetry Discovery size status = ioctlReadWriteData(deviceInterface, PmtSysman::IoctlPmtGetTelemetryDiscoverySize, NULL, 0, (void *)&sizeNeeded, sizeof(sizeNeeded), NULL); - if (status != ZE_RESULT_SUCCESS || sizeNeeded == 0) { NEO::printDebugString(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "Ioctl call could not return a valid value for the PMT interface telemetry size needed\n"); @@ -87,23 +86,33 @@ ze_result_t PlatformMonitoringTech::getGuid() { return ZE_RESULT_ERROR_UNKNOWN; } - telemetryDiscovery = (PmtSysman::PmtTelemetryDiscovery *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeNeeded); + telemetryDiscovery = (PmtSysman::PmtTelemetryDiscovery *)heapAllocFunction(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeNeeded); + if (telemetryDiscovery == nullptr) { + return ZE_RESULT_ERROR_UNKNOWN; + } // Get Telmetry Discovery Structure status = ioctlReadWriteData(deviceInterface, PmtSysman::IoctlPmtGetTelemetryDiscovery, NULL, 0, (void *)telemetryDiscovery, sizeNeeded, NULL); - if (status != ZE_RESULT_SUCCESS) { NEO::printDebugString(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "Ioctl call could not return a valid value for the PMT telemetry structure which provides the guids supported.\n"); DEBUG_BREAK_IF(true); + heapFreeFunction(GetProcessHeap(), 0, telemetryDiscovery); return ZE_RESULT_ERROR_UNKNOWN; } - for (uint32_t x = 0; x < telemetryDiscovery->count; x++) { - guidToIndexList[telemetryDiscovery->telemetry[x].index] = telemetryDiscovery->telemetry[x].guid; + for (uint32_t i = 0; i < telemetryDiscovery->count; i++) { + if (telemetryDiscovery->telemetry[i].index < PmtSysman::PmtMaxInterfaces) { + guidToIndexList[telemetryDiscovery->telemetry[i].index] = telemetryDiscovery->telemetry[i].guid; + } else { + NEO::printDebugString(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, + "Telemetry index is out of range.\n"); + DEBUG_BREAK_IF(true); + heapFreeFunction(GetProcessHeap(), 0, telemetryDiscovery); + return ZE_RESULT_ERROR_UNKNOWN; + } } - - return ZE_RESULT_SUCCESS; + return heapFreeFunction(GetProcessHeap(), 0, telemetryDiscovery) ? ZE_RESULT_SUCCESS : ZE_RESULT_ERROR_UNKNOWN; } ze_result_t PlatformMonitoringTech::init() { @@ -173,10 +182,9 @@ ze_result_t PlatformMonitoringTech::enumeratePMTInterface(const GUID *guid, std: return ZE_RESULT_SUCCESS; } -ze_result_t PlatformMonitoringTech::ioctlReadWriteData(std::vector path, uint32_t ioctl, void *bufferIn, uint32_t inSize, void *bufferOut, uint32_t outSize, uint32_t *sizeReturned) { - +ze_result_t PlatformMonitoringTech::ioctlReadWriteData(std::vector &path, uint32_t ioctl, void *bufferIn, uint32_t inSize, void *bufferOut, uint32_t outSize, uint32_t *sizeReturned) { void *handle; - int32_t status = FALSE; + BOOL status = FALSE; if (path.empty()) { NEO::printDebugString(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, @@ -187,13 +195,13 @@ ze_result_t PlatformMonitoringTech::ioctlReadWriteData(std::vector path // Open handle to driver handle = this->pcreateFile(&path[0], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); - if (handle == INVALID_HANDLE_VALUE) { NEO::printDebugString(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "Could not open the pmt interface path %s.\n", &path[0]); DEBUG_BREAK_IF(true); return ZE_RESULT_ERROR_UNKNOWN; } + // Call DeviceIoControl status = this->pdeviceIoControl(handle, ioctl, bufferIn, inSize, bufferOut, outSize, reinterpret_cast(sizeReturned), NULL); diff --git a/level_zero/sysman/source/shared/windows/pmt/sysman_pmt.h b/level_zero/sysman/source/shared/windows/pmt/sysman_pmt.h index 9ef25e9c2b..412b62cc84 100644 --- a/level_zero/sysman/source/shared/windows/pmt/sysman_pmt.h +++ b/level_zero/sysman/source/shared/windows/pmt/sysman_pmt.h @@ -25,11 +25,11 @@ namespace Sysman { class PlatformMonitoringTech : NEO::NonCopyableOrMovableClass { public: PlatformMonitoringTech() = delete; - PlatformMonitoringTech(std::vector deviceInterface) : deviceInterface(deviceInterface) {} + PlatformMonitoringTech(std::vector deviceInterface) : deviceInterface(std::move(deviceInterface)) {} virtual ~PlatformMonitoringTech(); - virtual ze_result_t readValue(const std::string key, uint32_t &value); - virtual ze_result_t readValue(const std::string key, uint64_t &value); + virtual ze_result_t readValue(const std::string &key, uint32_t &value); + virtual ze_result_t readValue(const std::string &key, uint64_t &value); ze_result_t getKeyOffsetMap(std::map> &keyOffsetMap); static std::unique_ptr create(); static ze_result_t enumeratePMTInterface(const GUID *Guid, std::vector &deviceInterface); @@ -37,12 +37,14 @@ class PlatformMonitoringTech : NEO::NonCopyableOrMovableClass { protected: std::map> keyOffsetMap; unsigned long guidToIndexList[PmtSysman::PmtMaxInterfaces] = {0}; - ze_result_t ioctlReadWriteData(std::vector path, uint32_t ioctl, void *bufferIn, uint32_t inSize, void *bufferOut, uint32_t outSize, uint32_t *sizeReturned); + ze_result_t ioctlReadWriteData(std::vector &path, uint32_t ioctl, void *bufferIn, uint32_t inSize, void *bufferOut, uint32_t outSize, uint32_t *sizeReturned); virtual ze_result_t init(); ze_result_t getGuid(); decltype(&NEO::SysCalls::deviceIoControl) pdeviceIoControl = NEO::SysCalls::deviceIoControl; decltype(&NEO::SysCalls::createFile) pcreateFile = NEO::SysCalls::createFile; decltype(&NEO::SysCalls::closeHandle) pcloseHandle = NEO::SysCalls::closeHandle; + decltype(&NEO::SysCalls::heapAlloc) heapAllocFunction = NEO::SysCalls::heapAlloc; + decltype(&NEO::SysCalls::heapFree) heapFreeFunction = NEO::SysCalls::heapFree; private: std::vector deviceInterface; diff --git a/shared/source/os_interface/windows/sys_calls.cpp b/shared/source/os_interface/windows/sys_calls.cpp index f4f6f04b8f..e8ca12efc3 100644 --- a/shared/source/os_interface/windows/sys_calls.cpp +++ b/shared/source/os_interface/windows/sys_calls.cpp @@ -175,6 +175,14 @@ CONFIGRET cmGetDeviceInterfaceList(LPGUID interfaceClassGuid, DEVINSTID_W pDevic return CM_Get_Device_Interface_List(interfaceClassGuid, pDeviceID, buffer, bufferLen, ulFlags); } +LPVOID heapAlloc(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes) { + return HeapAlloc(hHeap, dwFlags, dwBytes); +} + +BOOL heapFree(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem) { + return HeapFree(hHeap, dwFlags, lpMem); +} + SIZE_T virtualQuery(LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength) { return VirtualQuery(lpAddress, lpBuffer, dwLength); } diff --git a/shared/source/os_interface/windows/sys_calls.h b/shared/source/os_interface/windows/sys_calls.h index f49f3b8cac..74e9a3c90f 100644 --- a/shared/source/os_interface/windows/sys_calls.h +++ b/shared/source/os_interface/windows/sys_calls.h @@ -56,6 +56,8 @@ HANDLE createFile(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, BOOL deviceIoControl(HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpInBuffer, DWORD nInBufferSize, LPVOID lpOutBuffer, DWORD nOutBufferSize, LPDWORD lpBytesReturned, LPOVERLAPPED lpOverlapped); CONFIGRET cmGetDeviceInterfaceListSize(PULONG pulLen, LPGUID interfaceClassGuid, DEVINSTID_W pDeviceID, ULONG ulFlags); CONFIGRET cmGetDeviceInterfaceList(LPGUID interfaceClassGuid, DEVINSTID_W pDeviceID, PZZWSTR buffer, ULONG bufferLen, ULONG ulFlags); +LPVOID heapAlloc(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes); +BOOL heapFree(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem); SIZE_T virtualQuery(LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength); } // namespace SysCalls diff --git a/shared/test/common/os_interface/windows/mock_sys_calls.h b/shared/test/common/os_interface/windows/mock_sys_calls.h index 4073f1669f..77772468c5 100644 --- a/shared/test/common/os_interface/windows/mock_sys_calls.h +++ b/shared/test/common/os_interface/windows/mock_sys_calls.h @@ -65,6 +65,7 @@ extern HANDLE (*sysCallsCreateFile)(LPCWSTR lpFileName, DWORD dwDesiredAccess, D extern BOOL (*sysCallsDeviceIoControl)(HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpInBuffer, DWORD nInBufferSize, LPVOID lpOutBuffer, DWORD nOutBufferSize, LPDWORD lpBytesReturned, LPOVERLAPPED lpOverlapped); extern CONFIGRET (*sysCallsCmGetDeviceInterfaceListSize)(PULONG pulLen, LPGUID interfaceClassGuid, DEVINSTID_W pDeviceID, ULONG ulFlags); extern CONFIGRET (*sysCallsCmGetDeviceInterfaceList)(LPGUID interfaceClassGuid, DEVINSTID_W pDeviceID, PZZWSTR buffer, ULONG bufferLen, ULONG ulFlags); - +extern LPVOID (*sysCallsHeapAlloc)(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes); +extern BOOL (*sysCallsHeapFree)(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem); } // namespace SysCalls -} // namespace NEO +} // namespace NEO \ No newline at end of file diff --git a/shared/test/common/os_interface/windows/sys_calls.cpp b/shared/test/common/os_interface/windows/sys_calls.cpp index 162358a420..803414c7d5 100644 --- a/shared/test/common/os_interface/windows/sys_calls.cpp +++ b/shared/test/common/os_interface/windows/sys_calls.cpp @@ -111,6 +111,12 @@ CONFIGRET(*sysCallsCmGetDeviceInterfaceListSize) CONFIGRET(*sysCallsCmGetDeviceInterfaceList) (LPGUID interfaceClassGuid, DEVINSTID_W pDeviceID, PZZWSTR buffer, ULONG bufferLen, ULONG ulFlags) = nullptr; +LPVOID(*sysCallsHeapAlloc) +(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes) = nullptr; + +BOOL(*sysCallsHeapFree) +(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem) = nullptr; + bool pathExists(const std::string &path) { std::string tempP1 = path; if (!path.empty() && path.back() == PATH_SEPARATOR) { @@ -329,6 +335,20 @@ CONFIGRET cmGetDeviceInterfaceList(LPGUID interfaceClassGuid, DEVINSTID_W pDevic return -1; } +LPVOID heapAlloc(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes) { + if (sysCallsHeapAlloc != nullptr) { + return sysCallsHeapAlloc(hHeap, dwFlags, dwBytes); + } + return HeapAlloc(hHeap, dwFlags, dwBytes); +} + +BOOL heapFree(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem) { + if (sysCallsHeapFree != nullptr) { + return sysCallsHeapFree(hHeap, dwFlags, lpMem); + } + return HeapFree(hHeap, dwFlags, lpMem); +} + LSTATUS regOpenKeyExA(HKEY hKey, LPCSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult) { if (regOpenKeySuccessCount > 0) { regOpenKeySuccessCount--; @@ -421,4 +441,4 @@ unsigned int readEnablePreemptionRegKey() { return 1; } -} // namespace NEO +} // namespace NEO \ No newline at end of file