File: debugger_l0_windows.cpp

package info (click to toggle)
intel-compute-runtime 25.44.36015.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,632 kB
  • sloc: cpp: 931,547; lisp: 2,074; sh: 719; makefile: 162; python: 21
file content (258 lines) | stat: -rw-r--r-- 10,467 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * Copyright (C) 2020-2023 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/debugger/debugger_l0.h"
#include "shared/source/device/device.h"
#include "shared/source/execution_environment/root_device_environment.h"
#include "shared/source/helpers/gfx_core_helper.h"
#include "shared/source/kernel/debug_data.h"
#include "shared/source/os_interface/windows/wddm/wddm.h"
#include "shared/source/os_interface/windows/wddm_allocation.h"
#include "shared/source/os_interface/windows/wddm_debug.h"

#include "KmEscape.h"

namespace NEO {

bool DebuggerL0::initDebuggingInOs(NEO::OSInterface *osInterface) {

    if (osInterface == nullptr) {
        return false;
    }
    if (osInterface->isDebugAttachAvailable() == false) {
        return false;
    }

    return true;
}

void DebuggerL0::initSbaTrackingMode() {
    singleAddressSpaceSbaTracking = true;
}

void DebuggerL0::registerAllocationType(GraphicsAllocation *allocation) {
    if (!device->getRootDeviceEnvironment().osInterface) {
        return;
    }

    auto wddm = device->getRootDeviceEnvironment().osInterface->getDriverModel()->as<NEO::Wddm>();
    auto dataType = MAX_GFX_ALLOCATION_TYPE;

    auto wddmAllocation = static_cast<WddmAllocation *>(allocation);
    switch (wddmAllocation->getAllocationType()) {
    case AllocationType::debugContextSaveArea:
        dataType = SIP_CONTEXT_SAVE_AREA;
        break;
    case AllocationType::debugSbaTrackingBuffer:
        dataType = SBA_BUFFER_AREA;
        break;
    case AllocationType::debugModuleArea:
        dataType = MODULE_HEAP_DEBUG_AREA;
        break;
    case AllocationType::kernelIsa:
        dataType = ISA;
    default:
        break;
    }

    if (dataType == MAX_GFX_ALLOCATION_TYPE) {
        return;
    }

    WddmAllocation::RegistrationData registrationData = {};
    registrationData.gpuVirtualAddress = wddmAllocation->getGpuAddress();
    registrationData.size = wddmAllocation->getUnderlyingBufferSize();

    PRINT_DEBUGGER_INFO_LOG("REGISTER_ALLOCATION_TYPE: gpuVA=0x%llX Size=%X DataType=%d DataSize=%d DataPointer=%p\n",
                            registrationData.gpuVirtualAddress, registrationData.size, dataType, sizeof(registrationData), &registrationData);

    GFX_ALLOCATION_DEBUG_DATA_INFO allocationDebugDataInfo;
    allocationDebugDataInfo.DataType = dataType;
    allocationDebugDataInfo.DataSize = sizeof(registrationData);
    allocationDebugDataInfo.DataPointer = reinterpret_cast<uint64_t>(&registrationData);

    KM_ESCAPE_INFO escapeInfo = {};
    escapeInfo.Header.EscapeCode = GFX_ESCAPE_KMD;
    escapeInfo.Header.Size = sizeof(escapeInfo) - sizeof(escapeInfo.Header);
    escapeInfo.EscapeOperation = KM_ESCAPE_EUDBG_UMD_REGISTER_ALLOCATION_TYPE;
    escapeInfo.KmEuDbgUmdRegisterAllocationData.hAllocation = wddmAllocation->getDefaultHandle();
    escapeInfo.KmEuDbgUmdRegisterAllocationData.hElfAddressPtr = uint64_t(-1);
    escapeInfo.KmEuDbgUmdRegisterAllocationData.NumOfDebugDataInfo = 1;
    escapeInfo.KmEuDbgUmdRegisterAllocationData.DebugDataBufferPtr = reinterpret_cast<uint64_t>(&allocationDebugDataInfo);

    D3DKMT_ESCAPE escapeCommand = {};
    escapeCommand.Flags.HardwareAccess = 0;
    escapeCommand.Flags.Reserved = 0;
    escapeCommand.hAdapter = wddm->getAdapter();
    escapeCommand.hContext = (D3DKMT_HANDLE)0;
    escapeCommand.hDevice = wddm->getDeviceHandle();
    escapeCommand.pPrivateDriverData = &escapeInfo;
    escapeCommand.PrivateDriverDataSize = sizeof(escapeInfo);
    escapeCommand.Type = D3DKMT_ESCAPE_DRIVERPRIVATE;

    [[maybe_unused]] auto status = wddm->escape(escapeCommand);
    DEBUG_BREAK_IF(STATUS_SUCCESS != status);
}

void DebuggerL0::registerElfAndLinkWithAllocation(NEO::DebugData *debugData, NEO::GraphicsAllocation *allocation) {
}

uint32_t DebuggerL0::registerElf(NEO::DebugData *debugData) {
    return 0;
}

bool DebuggerL0::attachZebinModuleToSegmentAllocations(const StackVec<NEO::GraphicsAllocation *, 32> &kernelAllocs, uint32_t &moduleHandle, uint32_t elfHandle) {
    return false;
}

void DebuggerL0::notifyModuleCreate(void *module, uint32_t moduleSize, uint64_t moduleLoadAddress) {
    if (device->getRootDeviceEnvironment().osInterface == nullptr) {
        return;
    }

    // Register ELF
    KM_ESCAPE_INFO escapeInfo = {};
    escapeInfo.Header.EscapeCode = GFX_ESCAPE_KMD;
    escapeInfo.Header.Size = sizeof(escapeInfo) - sizeof(escapeInfo.Header);
    escapeInfo.EscapeOperation = KM_ESCAPE_EUDBG_UMD_CREATE_DEBUG_DATA;
    escapeInfo.KmEuDbgUmdCreateDebugData.DebugDataType = ELF_BINARY;
    escapeInfo.KmEuDbgUmdCreateDebugData.DataSize = moduleSize;
    escapeInfo.KmEuDbgUmdCreateDebugData.hElfAddressPtr = reinterpret_cast<uint64_t>(module);

    auto wddm = device->getRootDeviceEnvironment().osInterface->getDriverModel()->as<NEO::Wddm>();

    D3DKMT_ESCAPE escapeCommand = {};
    escapeCommand.Flags.HardwareAccess = 0;
    escapeCommand.Flags.Reserved = 0;
    escapeCommand.hAdapter = wddm->getAdapter();
    escapeCommand.hContext = (D3DKMT_HANDLE)0;
    escapeCommand.hDevice = wddm->getDeviceHandle();
    escapeCommand.pPrivateDriverData = &escapeInfo;
    escapeCommand.PrivateDriverDataSize = sizeof(escapeInfo);
    escapeCommand.Type = D3DKMT_ESCAPE_DRIVERPRIVATE;

    auto status = wddm->escape(escapeCommand);

    if (STATUS_SUCCESS != status) {
        PRINT_DEBUGGER_ERROR_LOG("KM_ESCAPE_EUDBG_UMD_CREATE_DEBUG_DATA: Failed - Status: 0x%llX\n", status);
        return;
    }

    PRINT_DEBUGGER_INFO_LOG("KM_ESCAPE_EUDBG_UMD_CREATE_DEBUG_DATA - Success\n");

    // Fire MODULE_CREATE event
    escapeInfo = {};
    escapeInfo.Header.EscapeCode = GFX_ESCAPE_KMD;
    escapeInfo.Header.Size = sizeof(escapeInfo) - sizeof(escapeInfo.Header);
    escapeInfo.EscapeOperation = KM_ESCAPE_EUDBG_UMD_MODULE_CREATE_NOTIFY;
    escapeInfo.KmEuDbgUmdCreateModuleNotification.IsCreate = true;
    escapeInfo.KmEuDbgUmdCreateModuleNotification.Modulesize = moduleSize;
    escapeInfo.KmEuDbgUmdCreateModuleNotification.hElfAddressPtr = reinterpret_cast<uint64_t>(module);
    escapeInfo.KmEuDbgUmdCreateModuleNotification.LoadAddress = moduleLoadAddress;

    status = wddm->escape(escapeCommand);

    if (STATUS_SUCCESS != status) {
        PRINT_DEBUGGER_ERROR_LOG("KM_ESCAPE_EUDBG_UMD_MODULE_CREATE_NOTIFY: Failed - Status: 0x%llX\n", status);
        return;
    }

    PRINT_DEBUGGER_INFO_LOG("KM_ESCAPE_EUDBG_UMD_MODULE_CREATE_NOTIFY - Success\n");
}

void DebuggerL0::notifyModuleDestroy(uint64_t moduleLoadAddress) {
    if (device->getRootDeviceEnvironment().osInterface == nullptr) {
        return;
    }

    KM_ESCAPE_INFO escapeInfo = {};
    escapeInfo.Header.EscapeCode = GFX_ESCAPE_KMD;
    escapeInfo.Header.Size = sizeof(escapeInfo) - sizeof(escapeInfo.Header);
    escapeInfo.EscapeOperation = KM_ESCAPE_EUDBG_UMD_MODULE_CREATE_NOTIFY;
    escapeInfo.KmEuDbgUmdCreateModuleNotification.IsCreate = false;
    escapeInfo.KmEuDbgUmdCreateModuleNotification.Modulesize = 0;
    escapeInfo.KmEuDbgUmdCreateModuleNotification.hElfAddressPtr = 0;
    escapeInfo.KmEuDbgUmdCreateModuleNotification.LoadAddress = moduleLoadAddress;

    auto wddm = device->getRootDeviceEnvironment().osInterface->getDriverModel()->as<NEO::Wddm>();

    D3DKMT_ESCAPE escapeCommand = {};
    escapeCommand.Flags.HardwareAccess = 0;
    escapeCommand.Flags.Reserved = 0;
    escapeCommand.hAdapter = wddm->getAdapter();
    escapeCommand.hContext = (D3DKMT_HANDLE)0;
    escapeCommand.hDevice = wddm->getDeviceHandle();
    escapeCommand.pPrivateDriverData = &escapeInfo;
    escapeCommand.PrivateDriverDataSize = sizeof(escapeInfo);
    escapeCommand.Type = D3DKMT_ESCAPE_DRIVERPRIVATE;

    auto status = wddm->escape(escapeCommand);

    if (STATUS_SUCCESS != status) {
        PRINT_DEBUGGER_ERROR_LOG("KM_ESCAPE_EUDBG_UMD_MODULE_DESTROY_NOTIFY: Failed - Status: 0x%llX\n", status);
        return;
    }

    PRINT_DEBUGGER_INFO_LOG("KM_ESCAPE_EUDBG_UMD_MODULE_DESTROY_NOTIFY - Success\n");
}

bool DebuggerL0::removeZebinModule(uint32_t moduleHandle) {
    return false;
}

static NTSTATUS runEscape(NEO::Wddm *wddm, KM_ESCAPE_INFO &escapeInfo) {
    D3DKMT_ESCAPE escapeCommand = {};

    escapeInfo.Header.EscapeCode = GFX_ESCAPE_KMD;
    escapeInfo.Header.Size = sizeof(escapeInfo) - sizeof(escapeInfo.Header);
    escapeInfo.EscapeOperation = KM_ESCAPE_EUDBG_UMD_CREATE_DEBUG_DATA;

    escapeCommand.Flags.HardwareAccess = 0;
    escapeCommand.Flags.Reserved = 0;
    escapeCommand.hAdapter = wddm->getAdapter();
    escapeCommand.hContext = (D3DKMT_HANDLE)0;
    escapeCommand.hDevice = wddm->getDeviceHandle();
    escapeCommand.pPrivateDriverData = &escapeInfo;
    escapeCommand.PrivateDriverDataSize = sizeof(escapeInfo);
    escapeCommand.Type = D3DKMT_ESCAPE_DRIVERPRIVATE;

    return wddm->escape(escapeCommand);
}

void DebuggerL0::notifyCommandQueueCreated(NEO::Device *deviceIn) {
    if (device->getRootDeviceEnvironment().osInterface.get() != nullptr) {
        std::unique_lock<std::mutex> commandQueueCountLock(debuggerL0Mutex);
        if (++commandQueueCount[0] == 1) {
            auto pWddm = device->getRootDeviceEnvironment().osInterface->getDriverModel()->as<NEO::Wddm>();
            int val = 0;
            KM_ESCAPE_INFO escapeInfo = {};
            escapeInfo.KmEuDbgUmdCreateDebugData.DataSize = sizeof(val);
            escapeInfo.KmEuDbgUmdCreateDebugData.DebugDataType = static_cast<uint32_t>(NEO::DebugDataType::CMD_QUEUE_CREATED);
            escapeInfo.KmEuDbgUmdCreateDebugData.hElfAddressPtr = reinterpret_cast<uint64_t>(&val);

            runEscape(pWddm, escapeInfo);
        }
    }
}

void DebuggerL0::notifyCommandQueueDestroyed(NEO::Device *deviceIn) {
    if (device->getRootDeviceEnvironment().osInterface.get() != nullptr) {
        std::unique_lock<std::mutex> commandQueueCountLock(debuggerL0Mutex);
        if (--commandQueueCount[0] == 0) {
            auto pWddm = device->getRootDeviceEnvironment().osInterface->getDriverModel()->as<NEO::Wddm>();
            int val = 0;
            KM_ESCAPE_INFO escapeInfo = {};
            escapeInfo.KmEuDbgUmdCreateDebugData.DataSize = sizeof(val);
            escapeInfo.KmEuDbgUmdCreateDebugData.DebugDataType = static_cast<uint32_t>(NEO::DebugDataType::CMD_QUEUE_DESTROYED);
            escapeInfo.KmEuDbgUmdCreateDebugData.hElfAddressPtr = reinterpret_cast<uint64_t>(&val);

            runEscape(pWddm, escapeInfo);
        }
    }
}

} // namespace NEO