diff --git a/projects/rocr-runtime/include/hsakmt.h b/projects/rocr-runtime/include/hsakmt.h index 1c3cd84a78..9d5ef5897f 100644 --- a/projects/rocr-runtime/include/hsakmt.h +++ b/projects/rocr-runtime/include/hsakmt.h @@ -774,6 +774,15 @@ hsaKmtDbgGetQueueData( bool suspend_queues //In ); +/** + Check whether gpu firmware and kernel support debugging +*/ +HSAKMT_STATUS +HSAKMTAPI +hsaKmtCheckRuntimeDebugSupport( + void + ); + /** Debug ops call primarily used for KFD testing */ diff --git a/projects/rocr-runtime/src/debug.c b/projects/rocr-runtime/src/debug.c index d12dc4a260..b3e312cbc1 100644 --- a/projects/rocr-runtime/src/debug.c +++ b/projects/rocr-runtime/src/debug.c @@ -268,7 +268,7 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtDbgAddressWatch(HSAuint32 NodeId, #define HSA_RUNTIME_ENABLE_MAX_MAJOR 1 #define HSA_RUNTIME_ENABLE_MIN_MINOR 13 -static HSAKMT_STATUS checkRuntimeDebugSupport(void) { +HSAKMT_STATUS hsaKmtCheckRuntimeDebugSupport(void) { HsaNodeProperties node = {0}; HsaSystemProperties props = {0}; HsaVersionInfo versionInfo = {0}; @@ -306,7 +306,7 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtRuntimeEnable(void *rDebug, bool setupTtmp) { struct kfd_ioctl_runtime_enable_args args = {0}; - HSAKMT_STATUS result = checkRuntimeDebugSupport(); + HSAKMT_STATUS result = hsaKmtCheckRuntimeDebugSupport(); if (result) return result; @@ -332,7 +332,7 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtRuntimeEnable(void *rDebug, HSAKMT_STATUS HSAKMTAPI hsaKmtRuntimeDisable(void) { struct kfd_ioctl_runtime_enable_args args = {0}; - HSAKMT_STATUS result = checkRuntimeDebugSupport(); + HSAKMT_STATUS result = hsaKmtCheckRuntimeDebugSupport(); if (result) return result; diff --git a/projects/rocr-runtime/src/libhsakmt.ver b/projects/rocr-runtime/src/libhsakmt.ver index c720476beb..5d1a673ffb 100644 --- a/projects/rocr-runtime/src/libhsakmt.ver +++ b/projects/rocr-runtime/src/libhsakmt.ver @@ -65,6 +65,7 @@ hsaKmtGetQueueInfo; hsaKmtAllocQueueGWS; hsaKmtRuntimeEnable; hsaKmtRuntimeDisable; +hsaKmtCheckRuntimeDebugSupport; hsaKmtGetRuntimeCapabilities; hsaKmtDebugTrapIoctl; hsaKmtSPMAcquire; diff --git a/projects/rocr-runtime/tests/kfdtest/CMakeLists.txt b/projects/rocr-runtime/tests/kfdtest/CMakeLists.txt index 1ec430da20..4e4ef6ad03 100644 --- a/projects/rocr-runtime/tests/kfdtest/CMakeLists.txt +++ b/projects/rocr-runtime/tests/kfdtest/CMakeLists.txt @@ -167,6 +167,7 @@ set (SRC_FILES gtest-1.6.0/gtest-all.cpp src/AqlQueue.cpp src/BasePacket.cpp + src/BaseDebug.cpp src/BaseQueue.cpp src/Dispatch.cpp src/GoogleTestExtension.cpp diff --git a/projects/rocr-runtime/tests/kfdtest/src/BaseDebug.cpp b/projects/rocr-runtime/tests/kfdtest/src/BaseDebug.cpp new file mode 100644 index 0000000000..50f243cc39 --- /dev/null +++ b/projects/rocr-runtime/tests/kfdtest/src/BaseDebug.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2023 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. + * + */ + +#include "BaseDebug.hpp" +#include +#include +#include +#include +#include +#include +#include "unistd.h" + +BaseDebug::BaseDebug(void) { +} + +BaseDebug::~BaseDebug(void) { + /* + * If the process is still attached, close and destroy the polling file + * descriptor. Note that on process termination, the KFD automatically + * disables processes that are still runtime enabled and debug enabled + * so we don't do it here. + */ + if (m_Pid) { + close(m_Fd.fd); + unlink(m_Fd_Name); + } +} + +// Creates temp file descriptor and debug attaches. +HSAKMT_STATUS BaseDebug::Attach(struct kfd_runtime_info *rInfo, + int rInfoSize, + unsigned int pid, + uint64_t exceptionEnable) { + struct kfd_ioctl_dbg_trap_args args = {0}; + char fd_name[32]; + + memset(&args, 0x00, sizeof(args)); + + mkfifo(m_Fd_Name, 0666); + m_Fd.fd = open(m_Fd_Name, O_CLOEXEC | O_NONBLOCK | O_RDWR); + m_Fd.events = POLLIN | POLLRDNORM; + + args.pid = pid; + args.op = KFD_IOC_DBG_TRAP_ENABLE; + args.enable.rinfo_ptr = (uint64_t)rInfo; + args.enable.rinfo_size = rInfoSize; + args.enable.dbg_fd = m_Fd.fd; + args.enable.exception_mask = exceptionEnable; + + if (hsaKmtDebugTrapIoctl(&args, NULL)) { + close(m_Fd.fd); + unlink(m_Fd_Name); + return HSAKMT_STATUS_ERROR; + } + + m_Pid = pid; + + return HSAKMT_STATUS_SUCCESS; +} + + +void BaseDebug::Detach(void) { + struct kfd_ioctl_dbg_trap_args args = {0}; + + memset(&args, 0x00, sizeof(args)); + + args.pid = m_Pid; + args.op = KFD_IOC_DBG_TRAP_DISABLE; + + hsaKmtDebugTrapIoctl(&args, NULL); + + close(m_Fd.fd); + unlink(m_Fd_Name); + + m_Pid = 0; + m_Fd.fd = 0; + m_Fd.events = 0; +} diff --git a/projects/rocr-runtime/tests/kfdtest/src/BaseDebug.hpp b/projects/rocr-runtime/tests/kfdtest/src/BaseDebug.hpp new file mode 100644 index 0000000000..ac53c6c696 --- /dev/null +++ b/projects/rocr-runtime/tests/kfdtest/src/BaseDebug.hpp @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2023 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 __KFD_BASE_DEBUG__H__ +#define __KFD_BASE_DEBUG__H__ + +#include "hsakmt.h" +#include +#include + +// @class BaseDebug +class BaseDebug { + public: + BaseDebug(void); + virtual ~BaseDebug(void); + + HSAKMT_STATUS Attach(struct kfd_runtime_info *rInfo, + int rInfoSize, + unsigned int pid, + uint64_t exceptionEnable); + + void Detach(void); + + private: + unsigned int m_Pid; + struct pollfd m_Fd; + const char *m_Fd_Name = "/tmp/dbg_fifo"; +}; + +#endif // __KFD_BASE_DEBUG__H__