Debian Codegraph @

level_zero/tools/test/unit_tests/sources/debug/mock_debug_session.h

raw
1 /*
2 * Copyright (C) 2021-2026 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #pragma once
9 #include "shared/source/helpers/aligned_memory.h"
10 #include "shared/source/helpers/hw_info.h"
11 #include "shared/source/os_interface/os_interface.h"
12 #include "shared/test/common/mocks/mock_sip.h"
13
14 #include "level_zero/core/source/device/device.h"
15 #include "level_zero/tools/source/debug/debug_session.h"
16 #include "level_zero/tools/source/debug/debug_session_imp.h"
17
18 namespace NEO {
19 class EuDebugInterface;
20 struct EuDebugConnect;
21 } // namespace NEO
22
23 namespace L0 {
24 DebugSession *createDebugSessionHelper(const zet_debug_config_t &config, Device *device, int debugFd, void *params);
25 ze_result_t openConnectionUpstreamHelper(int pid, Device *device, NEO::EuDebugInterface &debugInterface, NEO::EuDebugConnect *open, int &debugFd);
26
27 namespace ult {
28
29 using CreateDebugSessionHelperFunc = decltype(&L0::createDebugSessionHelper);
30 using OpenConnectionUpstreamHelperFunc = std::function<ze_result_t(int, L0::Device *, NEO::EuDebugInterface &, NEO::EuDebugConnect *, int &)>;
31
32 class OsInterfaceWithDebugAttach : public NEO::OSInterface {
33 public:
34 OsInterfaceWithDebugAttach() : OSInterface() {}
35 bool isDebugAttachAvailable() const override {
36 return debugAttachAvailable;
37 }
38
39 bool debugAttachAvailable = true;
40 };
41
42 struct DebugSessionMock : public L0::DebugSession {
43 using L0::DebugSession::allThreads;
44 using L0::DebugSession::debugArea;
45 using L0::DebugSession::fillDevicesFromThread;
46 using L0::DebugSession::getPerThreadScratchOffset;
47 using L0::DebugSession::getSingleThreadsForDevice;
48 using L0::DebugSession::isBindlessSystemRoutine;
49
50 DebugSessionMock(const zet_debug_config_t &config, L0::Device *device) : DebugSession(config, device), config(config) {
51 topologyMap = buildMockTopology(device);
52 }
53
54 bool closeConnection() override {
55 closeConnectionCalled = true;
56 return true;
57 }
58
59 static NEO::TopologyMap buildMockTopology(L0::Device *device) {
60 NEO::TopologyMap topologyMap;
61 auto &hwInfo = device->getHwInfo();
62 uint32_t tileCount;
63 if (hwInfo.gtSystemInfo.MultiTileArchInfo.IsValid) {
64 tileCount = hwInfo.gtSystemInfo.MultiTileArchInfo.TileCount;
65 } else {
66 tileCount = 1;
67 }
68
69 for (uint32_t deviceIdx = 0; deviceIdx < tileCount; deviceIdx++) {
70 NEO::TopologyMapping mapping;
71 for (uint32_t i = 0; i < hwInfo.gtSystemInfo.SliceCount; i++) {
72 mapping.sliceIndices.push_back(static_cast<int>(i));
73 }
74 if (hwInfo.gtSystemInfo.SliceCount == 1) {
75 for (uint32_t i = 0; i < hwInfo.gtSystemInfo.SubSliceCount; i++) {
76 mapping.subsliceIndices.push_back(static_cast<int>(i));
77 }
78 }
79 topologyMap[deviceIdx] = mapping;
80 }
81 return topologyMap;
82 }
83
84 ze_result_t initialize() override {
85 if (config.pid == 0) {
86 return ZE_RESULT_ERROR_UNKNOWN;
87 }
88 createEuThreads();
89 return ZE_RESULT_SUCCESS;
90 }
91 ze_result_t readEvent(uint64_t timeout, zet_debug_event_t *event) override {
92 return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
93 }
94 ze_result_t interrupt(ze_device_thread_t thread) override {
95 return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
96 }
97 ze_result_t resume(ze_device_thread_t thread) override {
98 return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
99 }
100 ze_result_t readMemory(ze_device_thread_t thread, const zet_debug_memory_space_desc_t *desc, size_t size, void *buffer) override {
101 return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
102 }
103 ze_result_t writeMemory(ze_device_thread_t thread, const zet_debug_memory_space_desc_t *desc, size_t size, const void *buffer) override {
104 return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
105 }
106 ze_result_t acknowledgeEvent(const zet_debug_event_t *event) override {
107 return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
108 }
109 ze_result_t readRegisters(ze_device_thread_t thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) override {
110 return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
111 }
112 ze_result_t writeRegisters(ze_device_thread_t thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) override {
113 return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
114 }
115 ze_result_t readSbaBuffer(EuThread::ThreadId threadId, NEO::SbaTrackedAddresses &sbaBuffer) override {
116 return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
117 }
118 void startAsyncThread() override {
119 asyncThreadStarted = true;
120 }
121
122 bool readModuleDebugArea() override {
123 return true;
124 }
125
126 ze_result_t readRegistersImp(EuThread::ThreadId thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) override {
127 return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
128 }
129
130 uint32_t getRegisterSize(uint32_t type) override {
131 return 0;
132 }
133
134 const SIP::regset_desc *typeToRegsetDesc(const NEO::StateSaveAreaHeader *pStateSaveAreaHeader, uint32_t type, L0::Device *device) {
135 return DebugSessionImp::typeToRegsetDesc(pStateSaveAreaHeader, type, device);
136 }
137
138 bool getRegisterAccessProperties(EuThread::ThreadId *threadId, uint32_t *pCount, zet_debug_regset_properties_t *pRegisterSetProperties) override {
139 registerAccessPropertiesCalled = true;
140 return true; // ignore parameters for mock
141 }
142
143 bool registerAccessPropertiesCalled = false;
144
145 void detachTileDebugSession(DebugSession *tileSession) override {}
146 bool areAllTileDebugSessionDetached() override { return true; }
147
148 L0::DebugSession *attachTileDebugSession(L0::Device *device) override {
149 return nullptr;
150 }
151
152 void setAttachMode(bool isRootAttach) override {}
153
154 const NEO::TopologyMap &getTopologyMap() override {
155 return this->topologyMap;
156 }
157
158 bool openSipWrapper(NEO::Device *neoDevice, uint64_t contextHandle, uint64_t gpuVa) override {
159 return true;
160 }
161
162 bool closeSipWrapper(NEO::Device *neoDevice, uint64_t contextHandle) override {
163 return true;
164 }
165
166 void closeExternalSipHandles() override {
167 // Mock implementation - no-op
168 }
169
170 NEO::TopologyMap topologyMap;
171 zet_debug_config_t config;
172 bool asyncThreadStarted = false;
173 bool closeConnectionCalled = false;
174 };
175
176 struct MockDebugSession : public L0::DebugSessionImp {
177
178 using L0::DebugSession::allThreads;
179 using L0::DebugSession::debugArea;
180 using L0::DebugSessionImp::addThreadToNewlyStoppedFromRaisedAttention;
181 using L0::DebugSessionImp::allocateStateSaveAreaMemory;
182 using L0::DebugSessionImp::apiEvents;
183 using L0::DebugSessionImp::applyResumeWa;
184 using L0::DebugSessionImp::attentionEventContext;
185 using L0::DebugSessionImp::calculateSrMagicOffset;
186 using L0::DebugSessionImp::calculateThreadSlotOffset;
187 using L0::DebugSessionImp::checkTriggerEventsForAttention;
188 using L0::DebugSessionImp::cmdRegisterAccessHelper;
189 using L0::DebugSessionImp::dumpDebugSurfaceToFile;
190 using L0::DebugSessionImp::fillResumeAndStoppedThreadsFromNewlyStopped;
191 using L0::DebugSessionImp::generateEventsAndResumeStoppedThreads;
192 using L0::DebugSessionImp::generateEventsForPendingInterrupts;
193 using L0::DebugSessionImp::generateEventsForStoppedThreads;
194 using L0::DebugSessionImp::getRegisterSize;
195 using L0::DebugSessionImp::getSipCommandRegisterValues;
196 using L0::DebugSessionImp::getSlmAccessProtocol;
197 using L0::DebugSessionImp::getStateSaveAreaHeader;
198 using L0::DebugSessionImp::interruptTimeout;
199 using L0::DebugSessionImp::isValidNode;
200 using L0::DebugSessionImp::newAttentionRaised;
201 using L0::DebugSessionImp::pollFifo;
202 using L0::DebugSessionImp::readDebugScratchRegisters;
203 using L0::DebugSessionImp::readFifo;
204 using L0::DebugSessionImp::readModeFlags;
205 using L0::DebugSessionImp::readSbaRegisters;
206 using L0::DebugSessionImp::readThreadScratchRegisters;
207 using L0::DebugSessionImp::registersAccessHelper;
208 using L0::DebugSessionImp::resumeAccidentallyStoppedThreads;
209 using L0::DebugSessionImp::sendInterrupts;
210 using L0::DebugSessionImp::stateSaveAreaMemory;
211 using L0::DebugSessionImp::typeToRegsetDesc;
212 using L0::DebugSessionImp::updateStoppedThreadsAndCheckTriggerEvents;
213 using L0::DebugSessionImp::validateAndSetStateSaveAreaHeader;
214
215 using L0::DebugSessionImp::interruptSent;
216 using L0::DebugSessionImp::stateSaveAreaHeader;
217 using L0::DebugSessionImp::triggerEvents;
218
219 using L0::DebugSessionImp::expectedAttentionEvents;
220 using L0::DebugSessionImp::fifoPollInterval;
221 using L0::DebugSessionImp::interruptMutex;
222 using L0::DebugSessionImp::interruptRequests;
223 using L0::DebugSessionImp::isValidGpuAddress;
224 using L0::DebugSessionImp::lastFifoReadTime;
225 using L0::DebugSessionImp::minSlmSipVersion;
226 using L0::DebugSessionImp::newlyStoppedThreads;
227 using L0::DebugSessionImp::pendingInterrupts;
228 using L0::DebugSessionImp::readBarrierMemory;
229 using L0::DebugSessionImp::readStateSaveAreaHeader;
230 using L0::DebugSessionImp::sipSupportsSlm;
231 using L0::DebugSessionImp::SlmAccessProtocol;
232 using L0::DebugSessionImp::slmMemoryAccess;
233 using L0::DebugSessionImp::slmMemoryReadV2;
234 using L0::DebugSessionImp::slmMemoryWriteV2;
235 using L0::DebugSessionImp::slmSipVersionCheck;
236 using L0::DebugSessionImp::tileAttachEnabled;
237 using L0::DebugSessionImp::tileSessions;
238
239 MockDebugSession(const zet_debug_config_t &config, L0::Device *device) : MockDebugSession(config, device, true) {}
240
241 MockDebugSession(const zet_debug_config_t &config, L0::Device *device, bool rootAttach) : MockDebugSession(config, device, rootAttach, 2) {
242 }
243
244 MockDebugSession(const zet_debug_config_t &config, L0::Device *device, bool rootAttach, uint32_t stateSaveHeaderVersion) : DebugSessionImp(config, device) {
245 if (device) {
246 topologyMap = DebugSessionMock::buildMockTopology(device);
247 }
248 setAttachMode(rootAttach);
249 if (rootAttach) {
250 createEuThreads();
251 }
252 if (stateSaveHeaderVersion == 3) {
253 stateSaveAreaHeader = NEO::MockSipData::createStateSaveAreaHeader(3);
254 } else {
255 stateSaveAreaHeader = NEO::MockSipData::createStateSaveAreaHeader(2);
256 }
257 }
258 ~MockDebugSession() override {
259 for (auto session : tileSessions) {
260 delete session.first;
261 }
262 }
263
264 bool closeConnection() override { return true; }
265 ze_result_t initialize() override {
266
267 auto numTiles = connectedDevice->getNEODevice()->getNumSubDevices();
268 if (numTiles > 0 && tileAttachEnabled) {
269 tileSessions.resize(numTiles);
270 zet_debug_config_t config = {};
271
272 for (uint32_t i = 0; i < numTiles; i++) {
273 auto subDevice = connectedDevice->getNEODevice()->getSubDevice(i)->getSpecializedDevice<Device>();
274 tileSessions[i] = std::pair<DebugSessionImp *, bool>{new MockDebugSession(config, subDevice), false};
275 }
276 tileSessionsEnabled = true;
277 }
278
279 return ZE_RESULT_SUCCESS;
280 }
281
282 void attachTile() override { attachTileCalled = true; };
283 void detachTile() override { detachTileCalled = true; };
284 void cleanRootSessionAfterDetach(uint32_t deviceIndex) override { cleanRootSessionDeviceIndices.push_back(deviceIndex); };
285
286 ze_result_t readMemory(ze_device_thread_t thread, const zet_debug_memory_space_desc_t *desc, size_t size, void *buffer) override {
287 return readMemoryResult;
288 }
289
290 ze_result_t writeMemory(ze_device_thread_t thread, const zet_debug_memory_space_desc_t *desc, size_t size, const void *buffer) override {
291 return ZE_RESULT_SUCCESS;
292 }
293 ze_result_t acknowledgeEvent(const zet_debug_event_t *event) override {
294 return ZE_RESULT_SUCCESS;
295 }
296
297 ze_result_t isValidNode(uint64_t vmHandle, uint64_t gpuVa, SIP::fifo_node &node) override {
298 if (isValidNodeResult != ZE_RESULT_SUCCESS) {
299 return isValidNodeResult;
300 } else {
301 return DebugSessionImp::isValidNode(vmHandle, gpuVa, node);
302 }
303 }
304
305 ze_result_t readRegistersImp(EuThread::ThreadId thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) override {
306 readRegistersCallCount++;
307 readRegistersReg = type;
308
309 if (readRegistersSizeToFill > 0 && readRegistersSizeToFill <= sizeof(regs)) {
310 memcpy_s(pRegisterValues, readRegistersSizeToFill, regs, readRegistersSizeToFill);
311 }
312 if (readRegistersResult != ZE_RESULT_FORCE_UINT32) {
313 return readRegistersResult;
314 }
315 return DebugSessionImp::readRegistersImp(thread, type, start, count, pRegisterValues);
316 }
317
318 ze_result_t writeRegistersImp(EuThread::ThreadId thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) override {
319 writeRegistersCallCount++;
320 writeRegistersReg = type;
321
322 if (writeRegistersResult != ZE_RESULT_FORCE_UINT32) {
323 return writeRegistersResult;
324 }
325
326 return DebugSessionImp::writeRegistersImp(thread, type, start, count, pRegisterValues);
327 }
328
329 ze_result_t readSbaBuffer(EuThread::ThreadId threadId, NEO::SbaTrackedAddresses &sbaBuffer) override {
330 sbaBuffer = sba;
331 return readSbaBufferResult;
332 }
333
334 void readStateSaveAreaHeader() override {
335 readStateSaveAreaHeaderCalled++;
336 DebugSessionImp::readStateSaveAreaHeader();
337 }
338 ze_result_t resumeImp(const std::vector<EuThread::ThreadId> &threads, uint32_t deviceIndex) override {
339 resumeImpCalled++;
340 resumeThreadCount = threads.size();
341 resumedThreads.push_back(std::move(threads));
342 resumedDevices.push_back(deviceIndex);
343 return resumeImpResult;
344 }
345
346 ze_result_t interruptImp(uint32_t deviceIndex) override {
347 interruptImpCalled++;
348 interruptedDevices.push_back(deviceIndex);
349 return interruptImpResult;
350 }
351
352 ze_result_t readGpuMemory(uint64_t memoryHandle, char *output, size_t size, uint64_t gpuVa) override {
353 readGpuMemoryCallCount++;
354 if (forcereadGpuMemoryFailOnCount == readGpuMemoryCallCount) {
355 return ZE_RESULT_ERROR_UNKNOWN;
356 }
357 if (gpuVa != 0 && gpuVa >= reinterpret_cast<uint64_t>(stateSaveAreaHeader.data()) &&
358 ((gpuVa + size) <= reinterpret_cast<uint64_t>(stateSaveAreaHeader.data() + stateSaveAreaHeader.size()))) {
359 [[maybe_unused]] auto offset = ptrDiff(gpuVa, reinterpret_cast<uint64_t>(stateSaveAreaHeader.data()));
360 memcpy_s(output, size, reinterpret_cast<void *>(gpuVa), size);
361 }
362
363 else if (!readMemoryBuffer.empty() &&
364 (gpuVa != 0 && gpuVa >= reinterpret_cast<uint64_t>(readMemoryBuffer.data()) &&
365 gpuVa <= reinterpret_cast<uint64_t>(ptrOffset(readMemoryBuffer.data(), readMemoryBuffer.size())))) {
366 memcpy_s(output, size, reinterpret_cast<void *>(gpuVa), std::min(size, readMemoryBuffer.size()));
367 }
368 return readMemoryResult;
369 }
370 ze_result_t writeGpuMemory(uint64_t memoryHandle, const char *input, size_t size, uint64_t gpuVa) override {
371 writeGpuMemoryCallCount++;
372 if (forceWriteGpuMemoryFailOnCount == writeGpuMemoryCallCount) {
373 return ZE_RESULT_ERROR_UNKNOWN;
374 }
375
376 if (gpuVa != 0 && gpuVa >= reinterpret_cast<uint64_t>(stateSaveAreaHeader.data()) &&
377 ((gpuVa + size) <= reinterpret_cast<uint64_t>(stateSaveAreaHeader.data() + stateSaveAreaHeader.size()))) {
378 [[maybe_unused]] auto offset = ptrDiff(gpuVa, reinterpret_cast<uint64_t>(stateSaveAreaHeader.data()));
379 memcpy_s(reinterpret_cast<void *>(gpuVa), size, input, size);
380 }
381 return writeMemoryResult;
382 }
383
384 ze_result_t readThreadScratchRegisters(EuThread::ThreadId thread, uint32_t start, uint32_t count, void *pRegisterValues) override {
385
386 if (readThreadScratchRegistersResult != ZE_RESULT_FORCE_UINT32) {
387 return readThreadScratchRegistersResult;
388 }
389 return DebugSessionImp::readThreadScratchRegisters(thread, start, count, pRegisterValues);
390 }
391
392 ze_result_t updateStoppedThreadsAndCheckTriggerEvents(const AttentionEventFields &attention, uint32_t tileIndex, std::vector<EuThread::ThreadId> &threadsWithAttention) override { return ZE_RESULT_SUCCESS; }
393 void resumeAccidentallyStoppedThreads(const std::vector<EuThread::ThreadId> &threadIds) override {
394 resumeAccidentallyStoppedCalled++;
395 return DebugSessionImp::resumeAccidentallyStoppedThreads(threadIds);
396 }
397
398 void startAsyncThread() override {}
399 bool readModuleDebugArea() override { return true; }
400
401 bool getRegisterAccessProperties(EuThread::ThreadId *threadId, uint32_t *pCount, zet_debug_regset_properties_t *pRegisterSetProperties) override {
402 registerAccessPropertiesCalled = true;
403 return true; // simulate successful retrieval for tests that validate SIP path usage
404 }
405
406 void enqueueApiEvent(zet_debug_event_t &debugEvent) override {
407 apiEvents.push(debugEvent);
408 apiEventCondition.notify_all();
409 }
410
411 bool isAIPequalToThreadStartIP(uint32_t *cr0, uint32_t *dbg0) override {
412 if (callBaseisAIPequalToThreadStartIP) {
413 return DebugSessionImp::isAIPequalToThreadStartIP(cr0, dbg0);
414 }
415 return forceAIPEqualStartIP;
416 }
417
418 bool isForceExceptionOrForceExternalHaltOnlyExceptionReason(uint32_t *cr0) override {
419 if (callBaseIsForceExceptionOrForceExternalHaltOnlyExceptionReason) {
420 return isForceExceptionOrForceExternalHaltOnlyExceptionReasonBase(cr0);
421 }
422 return onlyForceException;
423 }
424
425 bool isForceExceptionOrForceExternalHaltOnlyExceptionReasonBase(uint32_t *cr0) {
426 return DebugSessionImp::isForceExceptionOrForceExternalHaltOnlyExceptionReason(cr0);
427 }
428
429 bool readSystemRoutineIdent(EuThread *thread, uint64_t vmHandle, SIP::sr_ident &srMagic) override {
430 if (skipReadSystemRoutineIdent) {
431 srMagic.count = threadStopped ? 1 : 0;
432 return readSystemRoutineIdentRetVal;
433 }
434 return DebugSessionImp::readSystemRoutineIdent(thread, vmHandle, srMagic);
435 }
436
437 bool readSystemRoutineIdentFromMemory(EuThread *thread, const void *stateSaveArea, SIP::sr_ident &srMagic) override {
438 if (skipReadSystemRoutineIdent) {
439 srMagic.count = threadStopped ? 1 : 0;
440 return readSystemRoutineIdentRetVal;
441 }
442 return DebugSessionImp::readSystemRoutineIdentFromMemory(thread, stateSaveArea, srMagic);
443 }
444
445 bool areRequestedThreadsStopped(ze_device_thread_t thread) override {
446 if (areRequestedThreadsStoppedReturnValue != -1) {
447 return areRequestedThreadsStoppedReturnValue != 0;
448 }
449 return DebugSessionImp::areRequestedThreadsStopped(thread);
450 }
451
452 bool writeResumeCommand(const std::vector<EuThread::ThreadId> &threadIds) override {
453 writeResumeCommandCalled++;
454 if (skipWriteResumeCommand) {
455 return true;
456 }
457 return L0::DebugSessionImp::writeResumeCommand(threadIds);
458 }
459
460 ze_result_t waitForCmdReady(EuThread::ThreadId threadId, uint16_t retryCount) override {
461 return DebugSessionImp::waitForCmdReady(threadId, 1);
462 }
463
464 ze_result_t cmdRegisterAccessHelper(const EuThread::ThreadId &threadId, NEO::SipCommandRegisterValues &command, bool write) override {
465
466 ze_result_t status = ZE_RESULT_SUCCESS;
467
468 if (slmTesting) {
469 uint32_t size = command.sip_commandValues.size * slmSendBytesSize;
470
471 // initial wait for ready
472 if (!write && slmCmdRegisterCmdvalue == static_cast<uint32_t>(NEO::SipKernel::Command::resume)) {
473 if (!memcmp(slmMemory, "FailWaiting", strlen("FailWaiting"))) {
474 return ZE_RESULT_FORCE_UINT32;
475 }
476 command.sip_commandValues.command = static_cast<uint32_t>(NEO::SipKernel::Command::ready);
477 status = ZE_RESULT_SUCCESS;
478 } else {
479 if (write) { // writing to CMD register
480 if (forceCmdAccessFail) {
481 return ZE_RESULT_FORCE_UINT32;
482 } else if (command.sip_commandValues.command == static_cast<uint32_t>(NEO::SipKernel::Command::slmWrite)) {
483 memcpy_s(slmMemory + command.sip_commandValues.offset, size, command.sip_commandValues.buffer, size);
484 }
485 slmCmdRegisterCmdvalue = command.sip_commandValues.command;
486 }
487
488 if (slmCmdRegisterCmdvalue != static_cast<uint32_t>(NEO::SipKernel::Command::resume)) {
489 slmCmdRegisterAccessCount++;
490 }
491
492 if (slmCmdRegisterAccessCount == slmCmdRegisterAccessReadyCount) { // SIP restores cmd to READY
493
494 command.sip_commandValues.command = static_cast<uint32_t>(NEO::SipKernel::Command::ready);
495
496 if (slmCmdRegisterCmdvalue == static_cast<uint32_t>(NEO::SipKernel::Command::slmWrite)) {
497 slmCmdRegisterAccessCount = 0;
498 slmCmdRegisterCmdvalue = static_cast<uint32_t>(NEO::SipKernel::Command::resume);
499 }
500 } else if (slmCmdRegisterAccessCount == slmCmdRegisterAccessReadyCount + 1) { // Reading the data does an extra call to retrieve the data
501 if (!memcmp(slmMemory, "FailReadingData", strlen("FailReadingData"))) {
502 status = ZE_RESULT_FORCE_UINT32;
503 } else if (slmCmdRegisterCmdvalue == static_cast<uint32_t>(NEO::SipKernel::Command::slmRead)) {
504 memcpy_s(command.sip_commandValues.buffer, size, slmMemory + command.sip_commandValues.offset, size);
505 command.sip_commandValues.offset = 0;
506 command.sip_commandValues.size = 0;
507 }
508
509 slmCmdRegisterAccessCount = 0;
510 slmCmdRegisterCmdvalue = static_cast<uint32_t>(NEO::SipKernel::Command::resume);
511 }
512 }
513 return status;
514 }
515
516 return L0::DebugSessionImp::cmdRegisterAccessHelper(threadId, command, write);
517 }
518
519 bool checkThreadIsResumed(const EuThread::ThreadId &threadID) override {
520 checkThreadIsResumedCalled++;
521 if (skipCheckThreadIsResumed) {
522 return true;
523 }
524 return L0::DebugSessionImp::checkThreadIsResumed(threadID);
525 }
526 bool checkThreadIsResumed(const EuThread::ThreadId &threadID, const void *stateSaveArea) override {
527 checkThreadIsResumedFromPassedSaveAreaCalled++;
528 if (skipCheckThreadIsResumed) {
529 return true;
530 }
531 return L0::DebugSessionImp::checkThreadIsResumed(threadID, stateSaveArea);
532 }
533
534 bool getThreadSipCounter(const void *stateSaveArea, L0::EuThread *thread, const NEO::StateSaveAreaHeader *stateSaveAreaHeader, uint64_t *sipThreadCounter) override {
535 if (skipGetThreadSipCounter) {
536 if (sipThreadCounter) {
537 *sipThreadCounter = mockSipCounter;
538 }
539 return getThreadSipCounterRetVal;
540 }
541 return L0::DebugSessionImp::getThreadSipCounter(stateSaveArea, thread, stateSaveAreaHeader, sipThreadCounter);
542 }
543
544 uint64_t getContextStateSaveAreaGpuVa(uint64_t memoryHandle) override {
545 if (returnStateSaveAreaGpuVa && !this->stateSaveAreaHeader.empty()) {
546 return reinterpret_cast<uint64_t>(this->stateSaveAreaHeader.data());
547 }
548 return 0;
549 };
550
551 size_t getContextStateSaveAreaSize(uint64_t memoryHandle) override {
552 if (forceStateSaveAreaSize.has_value()) {
553 return forceStateSaveAreaSize.value();
554 }
555 if (stateSaveAreaHeader.size()) {
556 auto header = getStateSaveAreaHeader();
557 auto stateSaveAreaSize = header->regHeader.num_slices *
558 header->regHeader.num_subslices_per_slice *
559 header->regHeader.num_eus_per_subslice *
560 header->regHeader.num_threads_per_eu *
561 header->regHeader.state_save_size +
562 header->versionHeader.size * 8 + header->regHeader.state_area_offset;
563
564 return alignUp(stateSaveAreaSize, MemoryConstants::pageSize);
565 }
566
567 return 0;
568 }
569
570 int64_t getTimeDifferenceMilliseconds(std::chrono::high_resolution_clock::time_point time) override {
571 if (returnTimeDiff != -1) {
572 return returnTimeDiff;
573 }
574 return L0::DebugSessionImp::getTimeDifferenceMilliseconds(time);
575 }
576
577 const NEO::TopologyMap &getTopologyMap() override {
578 return this->topologyMap;
579 }
580
581 const SIP::regset_desc *typeToRegsetDesc(const NEO::StateSaveAreaHeader *pStateSaveAreaHeader, uint32_t type, L0::Device *device) {
582 return DebugSessionImp::typeToRegsetDesc(pStateSaveAreaHeader, type, device);
583 }
584
585 void checkStoppedThreadsAndGenerateEvents(const std::vector<EuThread::ThreadId> &threads, uint64_t memoryHandle, uint32_t deviceIndex) override {
586 checkStoppedThreadsAndGenerateEventsCallCount++;
587 return L0::DebugSessionImp::checkStoppedThreadsAndGenerateEvents(threads, memoryHandle, deviceIndex);
588 }
589
590 ze_result_t readFifo(uint64_t vmHandle, std::vector<EuThread::ThreadId> &threadsWithAttention) override {
591 readFifoCallCount++;
592 return L0::DebugSessionImp::readFifo(vmHandle, threadsWithAttention);
593 }
594
595 NEO::TopologyMap topologyMap;
596
597 uint32_t readFifoCallCount = 0;
598 uint32_t interruptImpCalled = 0;
599 uint32_t resumeImpCalled = 0;
600 uint32_t resumeAccidentallyStoppedCalled = 0;
601 size_t resumeThreadCount = 0;
602 ze_result_t interruptImpResult = ZE_RESULT_SUCCESS;
603 ze_result_t resumeImpResult = ZE_RESULT_SUCCESS;
604 bool onlyForceException = true;
605 bool forceAIPEqualStartIP = false;
606 bool callBaseIsForceExceptionOrForceExternalHaltOnlyExceptionReason = false;
607 bool callBaseisAIPequalToThreadStartIP = false;
608 bool threadStopped = true;
609 int areRequestedThreadsStoppedReturnValue = -1;
610 bool readSystemRoutineIdentRetVal = true;
611 size_t readRegistersSizeToFill = 0;
612 ze_result_t readSbaBufferResult = ZE_RESULT_SUCCESS;
613 ze_result_t readRegistersResult = ZE_RESULT_FORCE_UINT32;
614 ze_result_t readMemoryResult = ZE_RESULT_SUCCESS;
615 ze_result_t writeMemoryResult = ZE_RESULT_SUCCESS;
616 ze_result_t writeRegistersResult = ZE_RESULT_FORCE_UINT32;
617 ze_result_t readThreadScratchRegistersResult = ZE_RESULT_FORCE_UINT32;
618 ze_result_t isValidNodeResult = ZE_RESULT_SUCCESS;
619
620 uint32_t readStateSaveAreaHeaderCalled = 0;
621 uint32_t readRegistersCallCount = 0;
622 uint32_t readRegistersReg = 0;
623 uint32_t writeRegistersCallCount = 0;
624 uint32_t writeRegistersReg = 0;
625
626 uint32_t readGpuMemoryCallCount = 0;
627 uint32_t forcereadGpuMemoryFailOnCount = 0;
628
629 uint32_t writeGpuMemoryCallCount = 0;
630 uint32_t forceWriteGpuMemoryFailOnCount = 0;
631
632 // Instrumentation flag: set when getRegisterAccessProperties is invoked through SIP external lib path
633 bool registerAccessPropertiesCalled = false;
634
635 bool skipWriteResumeCommand = true;
636 uint32_t writeResumeCommandCalled = 0;
637
638 bool skipCheckThreadIsResumed = true;
639 uint32_t checkThreadIsResumedCalled = 0;
640 uint32_t checkThreadIsResumedFromPassedSaveAreaCalled = 0;
641 uint32_t checkStoppedThreadsAndGenerateEventsCallCount = 0;
642
643 bool skipReadSystemRoutineIdent = true;
644 bool skipGetThreadSipCounter = false;
645 bool getThreadSipCounterRetVal = true;
646 uint64_t mockSipCounter = 0;
647 std::vector<uint32_t> interruptedDevices;
648 std::vector<uint32_t> resumedDevices;
649 std::vector<std::vector<EuThread::ThreadId>> resumedThreads;
650
651 NEO::SbaTrackedAddresses sba;
652 std::vector<uint8_t> readMemoryBuffer;
653 uint64_t regs[16];
654
655 int64_t returnTimeDiff = -1;
656 bool returnStateSaveAreaGpuVa = true;
657 std::optional<size_t> forceStateSaveAreaSize = std::nullopt;
658
659 bool attachTileCalled = false;
660 bool detachTileCalled = false;
661 std::vector<uint32_t> cleanRootSessionDeviceIndices;
662
663 int slmCmdRegisterAccessCount = 0;
664 uint32_t slmCmdRegisterCmdvalue = static_cast<uint32_t>(NEO::SipKernel::Command::resume);
665 bool forceCmdAccessFail = false;
666 int slmCmdRegisterAccessReadyCount = 2;
667 bool slmTesting = false;
668 static constexpr int slmSize = 256;
669 char slmMemory[slmSize];
670 char originalSlmMemory[slmSize];
671 };
672
673 } // namespace ult
674 } // namespace L0