1 /*
2  * Copyright (C) 2018-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #include "shared/test/common/mock_gdi/mock_gdi.h"
9 
10 #include "shared/source/helpers/constants.h"
11 
12 ADAPTER_INFO gAdapterInfo = {0};
13 D3DDDI_MAPGPUVIRTUALADDRESS gLastCallMapGpuVaArg = {0};
14 D3DDDI_RESERVEGPUVIRTUALADDRESS gLastCallReserveGpuVaArg = {0};
15 uint32_t gMapGpuVaFailConfigCount = 0;
16 uint32_t gMapGpuVaFailConfigMax = 0;
17 uint64_t gGpuAddressSpace = 0ull;
18 uint32_t gLastPriority = 0ull;
19 ADAPTER_BDF gAdapterBDF{};
20 
21 #ifdef __cplusplus // If used by C++ code,
22 extern "C" { // we need to export the C interface
23 #endif
DllMain(IN HINSTANCE hDllHandle,IN DWORD nReason,IN LPVOID Reserved)24 BOOLEAN WINAPI DllMain(IN HINSTANCE hDllHandle,
25                        IN DWORD nReason,
26                        IN LPVOID Reserved) {
27     return TRUE;
28 }
29 
D3DKMTEscape(IN CONST D3DKMT_ESCAPE * pData)30 NTSTATUS __stdcall D3DKMTEscape(IN CONST D3DKMT_ESCAPE *pData) {
31     static int PerfTicks = 0;
32     ++PerfTicks;
33     ((NEO::TimeStampDataHeader *)pData->pPrivateDriverData)->m_Data.m_Out.RetCode = NEO::GTDI_RET_OK;
34     ((NEO::TimeStampDataHeader *)pData->pPrivateDriverData)->m_Data.m_Out.gpuPerfTicks = ++PerfTicks;
35     ((NEO::TimeStampDataHeader *)pData->pPrivateDriverData)->m_Data.m_Out.cpuPerfTicks = PerfTicks;
36     ((NEO::TimeStampDataHeader *)pData->pPrivateDriverData)->m_Data.m_Out.gpuPerfFreq = 1;
37     ((NEO::TimeStampDataHeader *)pData->pPrivateDriverData)->m_Data.m_Out.cpuPerfFreq = 1;
38 
39     return STATUS_SUCCESS;
40 }
41 
42 DECL_FUNCTIONS()
43 
44 UINT64 PagingFence = 0;
45 
MockSetAdapterInfo(const void * pGfxPlatform,const void * pGTSystemInfo,uint64_t gpuAddressSpace)46 void __stdcall MockSetAdapterInfo(const void *pGfxPlatform, const void *pGTSystemInfo, uint64_t gpuAddressSpace) {
47     if (pGfxPlatform != NULL) {
48         gAdapterInfo.GfxPlatform = *(PLATFORM *)pGfxPlatform;
49     }
50     if (pGTSystemInfo != NULL) {
51         gAdapterInfo.SystemInfo = *(GT_SYSTEM_INFO *)pGTSystemInfo;
52     }
53     gGpuAddressSpace = gpuAddressSpace;
54     InitGfxPartition();
55 }
56 
D3DKMTOpenAdapterFromLuid(IN OUT CONST D3DKMT_OPENADAPTERFROMLUID * openAdapter)57 NTSTATUS __stdcall D3DKMTOpenAdapterFromLuid(IN OUT CONST D3DKMT_OPENADAPTERFROMLUID *openAdapter) {
58     if (openAdapter == nullptr || (openAdapter->AdapterLuid.HighPart == 0 && openAdapter->AdapterLuid.LowPart == 0)) {
59         return STATUS_INVALID_PARAMETER;
60     }
61     D3DKMT_OPENADAPTERFROMLUID *openAdapterNonConst = const_cast<D3DKMT_OPENADAPTERFROMLUID *>(openAdapter);
62     if (openAdapter->AdapterLuid.HighPart == 0xdd) {
63         openAdapterNonConst->hAdapter = SHADOW_ADAPTER_HANDLE;
64     } else {
65         openAdapterNonConst->hAdapter = ADAPTER_HANDLE;
66     }
67     return STATUS_SUCCESS;
68 }
69 
D3DKMTCreateDevice(IN OUT D3DKMT_CREATEDEVICE * createDevice)70 NTSTATUS __stdcall D3DKMTCreateDevice(IN OUT D3DKMT_CREATEDEVICE *createDevice) {
71     if (createDevice == nullptr || createDevice->hAdapter != ADAPTER_HANDLE) {
72         return STATUS_INVALID_PARAMETER;
73     }
74     createDevice->hDevice = DEVICE_HANDLE;
75     SetMockCreateDeviceParams(*createDevice);
76     return STATUS_SUCCESS;
77 }
78 
D3DKMTDestroyDevice(IN CONST D3DKMT_DESTROYDEVICE * destoryDevice)79 NTSTATUS __stdcall D3DKMTDestroyDevice(IN CONST D3DKMT_DESTROYDEVICE *destoryDevice) {
80     if (destoryDevice == nullptr || destoryDevice->hDevice != DEVICE_HANDLE) {
81         return STATUS_INVALID_PARAMETER;
82     }
83     return STATUS_SUCCESS;
84 }
85 
D3DKMTCreatePagingQueue(IN OUT D3DKMT_CREATEPAGINGQUEUE * createQueue)86 NTSTATUS __stdcall D3DKMTCreatePagingQueue(IN OUT D3DKMT_CREATEPAGINGQUEUE *createQueue) {
87     if (createQueue == nullptr || (createQueue->hDevice != DEVICE_HANDLE)) {
88         return STATUS_INVALID_PARAMETER;
89     }
90     createQueue->hPagingQueue = PAGINGQUEUE_HANDLE;
91     createQueue->hSyncObject = PAGINGQUEUE_SYNCOBJECT_HANDLE;
92     createQueue->FenceValueCPUVirtualAddress = &PagingFence;
93     return STATUS_SUCCESS;
94 }
95 
D3DKMTDestroyPagingQueue(IN OUT D3DDDI_DESTROYPAGINGQUEUE * destoryQueue)96 NTSTATUS __stdcall D3DKMTDestroyPagingQueue(IN OUT D3DDDI_DESTROYPAGINGQUEUE *destoryQueue) {
97     if (destoryQueue == nullptr || destoryQueue->hPagingQueue != PAGINGQUEUE_HANDLE) {
98         return STATUS_INVALID_PARAMETER;
99     }
100     return STATUS_SUCCESS;
101 }
102 
103 static D3DKMT_CREATECONTEXTVIRTUAL createContextData = {0};
104 static CREATECONTEXT_PVTDATA createContextPrivateData = {{0}};
105 
D3DKMTCreateContextVirtual(IN D3DKMT_CREATECONTEXTVIRTUAL * createContext)106 NTSTATUS __stdcall D3DKMTCreateContextVirtual(IN D3DKMT_CREATECONTEXTVIRTUAL *createContext) {
107     if (createContext == nullptr || createContext->hDevice != DEVICE_HANDLE) {
108         return STATUS_INVALID_PARAMETER;
109     }
110 
111     createContextData = *createContext;
112     if (createContext->pPrivateDriverData) {
113         createContextPrivateData = *((CREATECONTEXT_PVTDATA *)createContext->pPrivateDriverData);
114         createContextData.pPrivateDriverData = &createContextPrivateData;
115     }
116 
117     if ((createContext->PrivateDriverDataSize != 0 && createContext->pPrivateDriverData == nullptr) ||
118         (createContext->PrivateDriverDataSize == 0 && createContext->pPrivateDriverData != nullptr)) {
119         return STATUS_INVALID_PARAMETER;
120     }
121     createContext->hContext = CONTEXT_HANDLE;
122     return STATUS_SUCCESS;
123 }
124 
D3DKMTDestroyContext(IN CONST D3DKMT_DESTROYCONTEXT * destroyContext)125 NTSTATUS __stdcall D3DKMTDestroyContext(IN CONST D3DKMT_DESTROYCONTEXT *destroyContext) {
126     if (destroyContext == nullptr || destroyContext->hContext != CONTEXT_HANDLE) {
127         return STATUS_INVALID_PARAMETER;
128     }
129     return STATUS_SUCCESS;
130 }
131 
132 static D3DKMT_CREATEALLOCATION pallocation{};
133 
D3DKMTCreateAllocation(IN OUT D3DKMT_CREATEALLOCATION * allocation)134 NTSTATUS __stdcall D3DKMTCreateAllocation(IN OUT D3DKMT_CREATEALLOCATION *allocation) {
135     return STATUS_INVALID_PARAMETER;
136 }
137 
D3DKMTCreateAllocation2(IN OUT D3DKMT_CREATEALLOCATION * allocation)138 NTSTATUS __stdcall D3DKMTCreateAllocation2(IN OUT D3DKMT_CREATEALLOCATION *allocation) {
139     D3DDDI_ALLOCATIONINFO2 *allocationInfo;
140     int numOfAllocations;
141     bool createResource;
142     bool globalShare;
143     if (allocation == nullptr || allocation->hDevice != DEVICE_HANDLE) {
144         return STATUS_INVALID_PARAMETER;
145     }
146     pallocation = *allocation;
147     allocationInfo = allocation->pAllocationInfo2;
148     if (allocationInfo == NULL) {
149         return STATUS_INVALID_PARAMETER;
150     }
151     numOfAllocations = allocation->NumAllocations;
152     createResource = allocation->Flags.CreateResource;
153     globalShare = allocation->Flags.CreateShared;
154     if (createResource) {
155         allocation->hResource = RESOURCE_HANDLE;
156     }
157     if (globalShare) {
158         allocation->hGlobalShare = RESOURCE_HANDLE;
159     }
160 
161     for (int i = 0; i < numOfAllocations; ++i) {
162         if (allocationInfo != NULL) {
163             allocationInfo->hAllocation = ALLOCATION_HANDLE;
164         }
165         allocationInfo++;
166     }
167 
168     return STATUS_SUCCESS;
169 }
170 
D3DKMTShareObjects(UINT cObjects,const D3DKMT_HANDLE * hObjects,POBJECT_ATTRIBUTES pObjectAttributes,DWORD dwDesiredAccess,HANDLE * phSharedNtHandle)171 NTSTATUS __stdcall D3DKMTShareObjects(UINT cObjects, const D3DKMT_HANDLE *hObjects, POBJECT_ATTRIBUTES pObjectAttributes, DWORD dwDesiredAccess, HANDLE *phSharedNtHandle) {
172     *phSharedNtHandle = reinterpret_cast<HANDLE>(reinterpret_cast<void *>(reinterpret_cast<uintptr_t *>(0x123)));
173     return STATUS_SUCCESS;
174 }
175 
176 static unsigned int DestroyAllocationWithResourceHandleCalled = 0u;
177 static D3DKMT_DESTROYALLOCATION2 destroyalloc2 = {0};
178 static D3DKMT_HANDLE LastDestroyedResourceHandle = 0;
179 static D3DKMT_CREATEDEVICE CreateDeviceParams = {{0}};
180 
D3DKMTDestroyAllocation2(IN CONST D3DKMT_DESTROYALLOCATION2 * destroyAllocation)181 NTSTATUS __stdcall D3DKMTDestroyAllocation2(IN CONST D3DKMT_DESTROYALLOCATION2 *destroyAllocation) {
182     int numOfAllocations;
183     const D3DKMT_HANDLE *allocationList;
184     LastDestroyedResourceHandle = 0;
185     if (destroyAllocation == nullptr || destroyAllocation->hDevice != DEVICE_HANDLE) {
186         return STATUS_INVALID_PARAMETER;
187     }
188     destroyalloc2 = *destroyAllocation;
189     numOfAllocations = destroyAllocation->AllocationCount;
190     allocationList = destroyAllocation->phAllocationList;
191 
192     for (int i = 0; i < numOfAllocations; ++i) {
193         if (allocationList != NULL) {
194             if (*allocationList != ALLOCATION_HANDLE) {
195                 return STATUS_UNSUCCESSFUL;
196             }
197         }
198         allocationList++;
199     }
200     if (numOfAllocations == 0 && destroyAllocation->hResource == 0u) {
201         return STATUS_UNSUCCESSFUL;
202     }
203     if (destroyAllocation->hResource) {
204         DestroyAllocationWithResourceHandleCalled = 1;
205         LastDestroyedResourceHandle = destroyAllocation->hResource;
206     }
207 
208     return STATUS_SUCCESS;
209 }
210 
D3DKMTMapGpuVirtualAddress(IN OUT D3DDDI_MAPGPUVIRTUALADDRESS * mapGpuVA)211 NTSTATUS __stdcall D3DKMTMapGpuVirtualAddress(IN OUT D3DDDI_MAPGPUVIRTUALADDRESS *mapGpuVA) {
212     if (mapGpuVA == nullptr) {
213         memset(&gLastCallMapGpuVaArg, 0, sizeof(gLastCallMapGpuVaArg));
214         return STATUS_INVALID_PARAMETER;
215     }
216 
217     memcpy(&gLastCallMapGpuVaArg, mapGpuVA, sizeof(gLastCallMapGpuVaArg));
218 
219     if (mapGpuVA->hPagingQueue != PAGINGQUEUE_HANDLE) {
220         return STATUS_INVALID_PARAMETER;
221     }
222     if (mapGpuVA->hAllocation != ALLOCATION_HANDLE && mapGpuVA->hAllocation != NT_ALLOCATION_HANDLE) {
223         return STATUS_INVALID_PARAMETER;
224     }
225     if (mapGpuVA->MinimumAddress != 0) {
226         if (mapGpuVA->BaseAddress != 0 && mapGpuVA->BaseAddress < mapGpuVA->MinimumAddress) {
227             return STATUS_INVALID_PARAMETER;
228         }
229     }
230     if (mapGpuVA->MaximumAddress != 0) {
231         if (mapGpuVA->BaseAddress != 0 && mapGpuVA->BaseAddress > mapGpuVA->MaximumAddress) {
232             return STATUS_INVALID_PARAMETER;
233         }
234     }
235     if (mapGpuVA->BaseAddress == 0) {
236         if (mapGpuVA->MinimumAddress) {
237             mapGpuVA->VirtualAddress = mapGpuVA->MinimumAddress;
238         } else {
239             mapGpuVA->VirtualAddress = MemoryConstants::pageSize64k;
240         }
241     } else {
242         mapGpuVA->VirtualAddress = mapGpuVA->BaseAddress;
243     }
244 
245     if (gMapGpuVaFailConfigMax != 0) {
246         if (gMapGpuVaFailConfigMax > gMapGpuVaFailConfigCount) {
247             gMapGpuVaFailConfigCount++;
248             return STATUS_UNSUCCESSFUL;
249         }
250     }
251 
252     mapGpuVA->PagingFenceValue = 1;
253     return STATUS_PENDING;
254 }
255 
D3DKMTReserveGpuVirtualAddress(IN OUT D3DDDI_RESERVEGPUVIRTUALADDRESS * reserveGpuVirtualAddress)256 NTSTATUS __stdcall D3DKMTReserveGpuVirtualAddress(IN OUT D3DDDI_RESERVEGPUVIRTUALADDRESS *reserveGpuVirtualAddress) {
257     gLastCallReserveGpuVaArg = *reserveGpuVirtualAddress;
258     reserveGpuVirtualAddress->VirtualAddress = reserveGpuVirtualAddress->MinimumAddress;
259     return STATUS_SUCCESS;
260 }
261 
D3DKMTQueryAdapterInfo(IN CONST D3DKMT_QUERYADAPTERINFO * queryAdapterInfo)262 NTSTATUS __stdcall D3DKMTQueryAdapterInfo(IN CONST D3DKMT_QUERYADAPTERINFO *queryAdapterInfo) {
263     if (queryAdapterInfo == nullptr) {
264         return STATUS_INVALID_PARAMETER;
265     }
266     if (queryAdapterInfo->Type == KMTQAITYPE_UMDRIVERPRIVATE) {
267         if (queryAdapterInfo->pPrivateDriverData == NULL) {
268             return STATUS_INVALID_PARAMETER;
269         }
270         if (queryAdapterInfo->PrivateDriverDataSize == 0) {
271             return STATUS_INVALID_PARAMETER;
272         }
273     }
274 
275     if (queryAdapterInfo->Type == KMTQAITYPE_ADAPTERTYPE) {
276         D3DKMT_ADAPTERTYPE *adapterType = reinterpret_cast<D3DKMT_ADAPTERTYPE *>(queryAdapterInfo->pPrivateDriverData);
277         if (queryAdapterInfo->hAdapter == ADAPTER_HANDLE) {
278             adapterType->RenderSupported = 1;
279         } else if (queryAdapterInfo->hAdapter == SHADOW_ADAPTER_HANDLE) {
280             adapterType->RenderSupported = 0;
281         } else {
282             return STATUS_INVALID_PARAMETER;
283         }
284 
285         return STATUS_SUCCESS;
286     }
287     if (queryAdapterInfo->Type == KMTQAITYPE_UMDRIVERPRIVATE) {
288         if (queryAdapterInfo->hAdapter != ADAPTER_HANDLE && queryAdapterInfo->hAdapter != SHADOW_ADAPTER_HANDLE) {
289             return STATUS_INVALID_PARAMETER;
290         }
291         ADAPTER_INFO *adapterInfo = reinterpret_cast<ADAPTER_INFO *>(queryAdapterInfo->pPrivateDriverData);
292 
293         adapterInfo->GfxPlatform = gAdapterInfo.GfxPlatform;
294         adapterInfo->SystemInfo = gAdapterInfo.SystemInfo;
295         adapterInfo->SkuTable = gAdapterInfo.SkuTable;
296         adapterInfo->WaTable = gAdapterInfo.WaTable;
297         adapterInfo->CacheLineSize = 64;
298         adapterInfo->MinRenderFreq = 350;
299         adapterInfo->MaxRenderFreq = 1150;
300 
301         adapterInfo->SizeOfDmaBuffer = 32768;
302         adapterInfo->GfxMemorySize = 2181038080;
303         adapterInfo->SystemSharedMemory = 4249540608;
304         adapterInfo->SystemVideoMemory = 0;
305         adapterInfo->GfxTimeStampFreq = 1;
306 
307         adapterInfo->GfxPartition.Standard.Base = gAdapterInfo.GfxPartition.Standard.Base;
308         adapterInfo->GfxPartition.Standard.Limit = gAdapterInfo.GfxPartition.Standard.Limit;
309         adapterInfo->GfxPartition.Standard64KB.Base = gAdapterInfo.GfxPartition.Standard64KB.Base;
310         adapterInfo->GfxPartition.Standard64KB.Limit = gAdapterInfo.GfxPartition.Standard64KB.Limit;
311 
312         adapterInfo->GfxPartition.SVM.Base = gAdapterInfo.GfxPartition.SVM.Base;
313         adapterInfo->GfxPartition.SVM.Limit = gAdapterInfo.GfxPartition.SVM.Limit;
314         adapterInfo->GfxPartition.Heap32[0].Base = gAdapterInfo.GfxPartition.Heap32[0].Base;
315         adapterInfo->GfxPartition.Heap32[0].Limit = gAdapterInfo.GfxPartition.Heap32[0].Limit;
316         adapterInfo->GfxPartition.Heap32[1].Base = gAdapterInfo.GfxPartition.Heap32[1].Base;
317         adapterInfo->GfxPartition.Heap32[1].Limit = gAdapterInfo.GfxPartition.Heap32[1].Limit;
318         adapterInfo->GfxPartition.Heap32[2].Base = gAdapterInfo.GfxPartition.Heap32[2].Base;
319         adapterInfo->GfxPartition.Heap32[2].Limit = gAdapterInfo.GfxPartition.Heap32[2].Limit;
320         adapterInfo->GfxPartition.Heap32[3].Base = gAdapterInfo.GfxPartition.Heap32[3].Base;
321         adapterInfo->GfxPartition.Heap32[3].Limit = gAdapterInfo.GfxPartition.Heap32[3].Limit;
322 
323         adapterInfo->stAdapterBDF.Data = gAdapterBDF.Data;
324         return STATUS_SUCCESS;
325     }
326     return STATUS_INVALID_PARAMETER;
327 }
328 
D3DKMTMakeResident(IN OUT D3DDDI_MAKERESIDENT * makeResident)329 NTSTATUS __stdcall D3DKMTMakeResident(IN OUT D3DDDI_MAKERESIDENT *makeResident) {
330     if (makeResident == nullptr || makeResident->hPagingQueue != PAGINGQUEUE_HANDLE) {
331         return STATUS_INVALID_PARAMETER;
332     }
333     makeResident->PagingFenceValue = 0;
334     return STATUS_PENDING;
335 }
336 
337 static UINT totalPrivateSize = 0u;
338 static UINT gmmSize = 0u;
339 static void *gmmPtr = nullptr;
340 static UINT numberOfAllocsToReturn = 0u;
341 
D3DKMTOpenResource(IN OUT D3DKMT_OPENRESOURCE * openResurce)342 NTSTATUS __stdcall D3DKMTOpenResource(IN OUT D3DKMT_OPENRESOURCE *openResurce) {
343     openResurce->hResource = RESOURCE_HANDLE;
344     openResurce->pOpenAllocationInfo[0].hAllocation = ALLOCATION_HANDLE;
345     openResurce->pOpenAllocationInfo[0].pPrivateDriverData = gmmPtr;
346     return STATUS_SUCCESS;
347 }
348 
D3DKMTOpenResourceFromNtHandle(IN OUT D3DKMT_OPENRESOURCEFROMNTHANDLE * openResurce)349 NTSTATUS __stdcall D3DKMTOpenResourceFromNtHandle(IN OUT D3DKMT_OPENRESOURCEFROMNTHANDLE *openResurce) {
350     openResurce->hResource = NT_RESOURCE_HANDLE;
351     openResurce->pOpenAllocationInfo2[0].hAllocation = NT_ALLOCATION_HANDLE;
352     openResurce->pOpenAllocationInfo2[0].pPrivateDriverData = gmmPtr;
353     return STATUS_SUCCESS;
354 }
355 
D3DKMTQueryResourceInfo(IN OUT D3DKMT_QUERYRESOURCEINFO * queryResourceInfo)356 NTSTATUS __stdcall D3DKMTQueryResourceInfo(IN OUT D3DKMT_QUERYRESOURCEINFO *queryResourceInfo) {
357     if (queryResourceInfo->hDevice != DEVICE_HANDLE || queryResourceInfo->hGlobalShare == INVALID_HANDLE) {
358         return STATUS_INVALID_PARAMETER;
359     }
360     queryResourceInfo->TotalPrivateDriverDataSize = totalPrivateSize;
361     queryResourceInfo->PrivateRuntimeDataSize = gmmSize;
362     queryResourceInfo->NumAllocations = numberOfAllocsToReturn;
363 
364     return STATUS_SUCCESS;
365 }
366 
D3DKMTQueryResourceInfoFromNtHandle(IN OUT D3DKMT_QUERYRESOURCEINFOFROMNTHANDLE * queryResourceInfo)367 NTSTATUS __stdcall D3DKMTQueryResourceInfoFromNtHandle(IN OUT D3DKMT_QUERYRESOURCEINFOFROMNTHANDLE *queryResourceInfo) {
368     if (queryResourceInfo->hDevice != DEVICE_HANDLE || queryResourceInfo->hNtHandle == INVALID_HANDLE) {
369         return STATUS_INVALID_PARAMETER;
370     }
371     queryResourceInfo->TotalPrivateDriverDataSize = totalPrivateSize;
372     queryResourceInfo->PrivateRuntimeDataSize = gmmSize;
373     queryResourceInfo->NumAllocations = numberOfAllocsToReturn;
374 
375     return STATUS_SUCCESS;
376 }
377 
D3DKMTLock2(IN OUT D3DKMT_LOCK2 * lock2)378 NTSTATUS __stdcall D3DKMTLock2(IN OUT D3DKMT_LOCK2 *lock2) {
379     if (lock2->hAllocation == 0 || lock2->hDevice == 0) {
380         return STATUS_INVALID_PARAMETER;
381     }
382     lock2->pData = (void *)65536;
383     return STATUS_SUCCESS;
384 }
385 
D3DKMTUnlock2(IN CONST D3DKMT_UNLOCK2 * unlock2)386 NTSTATUS __stdcall D3DKMTUnlock2(IN CONST D3DKMT_UNLOCK2 *unlock2) {
387     if (unlock2->hAllocation == 0 || unlock2->hDevice == 0) {
388         return STATUS_INVALID_PARAMETER;
389     }
390     return STATUS_SUCCESS;
391 }
392 
393 static size_t cpuFence = 0;
394 
395 static bool createSynchronizationObject2FailCall = false;
396 
D3DKMTCreateSynchronizationObject2(IN OUT D3DKMT_CREATESYNCHRONIZATIONOBJECT2 * synchObject)397 NTSTATUS __stdcall D3DKMTCreateSynchronizationObject2(IN OUT D3DKMT_CREATESYNCHRONIZATIONOBJECT2 *synchObject) {
398     if (synchObject == nullptr) {
399         return STATUS_INVALID_PARAMETER;
400     }
401 
402     if (createSynchronizationObject2FailCall) {
403         return STATUS_INVALID_PARAMETER;
404     }
405 
406     synchObject->Info.MonitoredFence.FenceValueCPUVirtualAddress = &cpuFence;
407     synchObject->Info.MonitoredFence.FenceValueGPUVirtualAddress = 3;
408     synchObject->hSyncObject = 4;
409     return STATUS_SUCCESS;
410 }
411 
D3DKMTSetAllocationPriority(IN CONST D3DKMT_SETALLOCATIONPRIORITY * setAllocationPriority)412 NTSTATUS __stdcall D3DKMTSetAllocationPriority(IN CONST D3DKMT_SETALLOCATIONPRIORITY *setAllocationPriority) {
413     if (setAllocationPriority == nullptr || setAllocationPriority->hDevice != DEVICE_HANDLE) {
414         return STATUS_INVALID_PARAMETER;
415     }
416 
417     if (setAllocationPriority->hResource == NULL && (setAllocationPriority->AllocationCount == 0 || setAllocationPriority->phAllocationList == NULL)) {
418         return STATUS_INVALID_PARAMETER;
419     }
420     if (setAllocationPriority->pPriorities == NULL) {
421         return STATUS_INVALID_PARAMETER;
422     }
423     auto priority = setAllocationPriority->pPriorities[0];
424     gLastPriority = priority;
425     for (auto i = 0u; i < setAllocationPriority->AllocationCount; i++) {
426         if (setAllocationPriority->phAllocationList[i] == 0 || setAllocationPriority->pPriorities[i] != priority) {
427             return STATUS_INVALID_PARAMETER;
428         }
429     }
430 
431     return STATUS_SUCCESS;
432 }
433 
434 static D3DKMT_CREATEHWQUEUE createHwQueueData = {};
435 
D3DKMTCreateHwQueue(IN OUT D3DKMT_CREATEHWQUEUE * createHwQueue)436 NTSTATUS __stdcall D3DKMTCreateHwQueue(IN OUT D3DKMT_CREATEHWQUEUE *createHwQueue) {
437     createHwQueue->hHwQueueProgressFence = 1;
438     createHwQueue->HwQueueProgressFenceCPUVirtualAddress = reinterpret_cast<void *>(2);
439     createHwQueue->HwQueueProgressFenceGPUVirtualAddress = 3;
440     createHwQueue->hHwQueue = 4;
441     createHwQueueData = *createHwQueue;
442     return STATUS_SUCCESS;
443 }
444 
445 static D3DKMT_DESTROYHWQUEUE destroyHwQueueData = {};
446 
D3DKMTDestroyHwQueue(IN CONST D3DKMT_DESTROYHWQUEUE * destroyHwQueue)447 NTSTATUS __stdcall D3DKMTDestroyHwQueue(IN CONST D3DKMT_DESTROYHWQUEUE *destroyHwQueue) {
448     destroyHwQueueData = *destroyHwQueue;
449     return STATUS_SUCCESS;
450 }
451 
452 static D3DKMT_SUBMITCOMMANDTOHWQUEUE submitCommandToHwQueueData = {};
453 
D3DKMTSubmitCommandToHwQueue(IN CONST D3DKMT_SUBMITCOMMANDTOHWQUEUE * submitCommandToHwQueue)454 NTSTATUS __stdcall D3DKMTSubmitCommandToHwQueue(IN CONST D3DKMT_SUBMITCOMMANDTOHWQUEUE *submitCommandToHwQueue) {
455     submitCommandToHwQueueData = *submitCommandToHwQueue;
456     return STATUS_SUCCESS;
457 }
458 
459 static D3DKMT_DESTROYSYNCHRONIZATIONOBJECT destroySynchronizationObjectData = {};
460 
D3DKMTDestroySynchronizationObject(IN CONST D3DKMT_DESTROYSYNCHRONIZATIONOBJECT * destroySynchronizationObject)461 NTSTATUS __stdcall D3DKMTDestroySynchronizationObject(IN CONST D3DKMT_DESTROYSYNCHRONIZATIONOBJECT *destroySynchronizationObject) {
462     destroySynchronizationObjectData = *destroySynchronizationObject;
463     return STATUS_SUCCESS;
464 }
465 
466 static bool registerTrimNotificationFailCall = false;
467 
D3DKMTRegisterTrimNotification(IN D3DKMT_REGISTERTRIMNOTIFICATION * registerTrimNotification)468 NTSTATUS __stdcall D3DKMTRegisterTrimNotification(IN D3DKMT_REGISTERTRIMNOTIFICATION *registerTrimNotification) {
469     if (registerTrimNotificationFailCall) {
470         return STATUS_INVALID_PARAMETER;
471     }
472     registerTrimNotification->Handle = TRIM_CALLBACK_HANDLE;
473     return STATUS_SUCCESS;
474 }
475 
476 #ifdef __cplusplus
477 }
478 #endif
479 
MockSetSizes(void * inGmmPtr,UINT inNumAllocsToReturn,UINT inGmmSize,UINT inTotalPrivateSize)480 NTSTATUS MockSetSizes(void *inGmmPtr, UINT inNumAllocsToReturn, UINT inGmmSize, UINT inTotalPrivateSize) {
481     gmmSize = inGmmSize;
482     gmmPtr = inGmmPtr;
483     totalPrivateSize = inTotalPrivateSize;
484     numberOfAllocsToReturn = inNumAllocsToReturn;
485     return STATUS_SUCCESS;
486 }
487 
GetMockSizes(UINT & destroyAlloactionWithResourceHandleCalled,D3DKMT_DESTROYALLOCATION2 * & ptrDestroyAlloc)488 NTSTATUS GetMockSizes(UINT &destroyAlloactionWithResourceHandleCalled, D3DKMT_DESTROYALLOCATION2 *&ptrDestroyAlloc) {
489     destroyAlloactionWithResourceHandleCalled = DestroyAllocationWithResourceHandleCalled;
490     ptrDestroyAlloc = &destroyalloc2;
491     return NTSTATUS();
492 }
493 
GetMockLastDestroyedResHandle()494 D3DKMT_HANDLE GetMockLastDestroyedResHandle() {
495     return LastDestroyedResourceHandle;
496 }
497 
SetMockLastDestroyedResHandle(D3DKMT_HANDLE handle)498 void SetMockLastDestroyedResHandle(D3DKMT_HANDLE handle) {
499     LastDestroyedResourceHandle = handle;
500 }
501 
GetMockCreateDeviceParams()502 D3DKMT_CREATEDEVICE GetMockCreateDeviceParams() {
503     return CreateDeviceParams;
504 }
505 
SetMockCreateDeviceParams(D3DKMT_CREATEDEVICE params)506 void SetMockCreateDeviceParams(D3DKMT_CREATEDEVICE params) {
507     CreateDeviceParams = params;
508 }
509 
getMockAllocation()510 D3DKMT_CREATEALLOCATION *getMockAllocation() {
511     return &pallocation;
512 }
513 
getAdapterInfoAddress()514 ADAPTER_INFO *getAdapterInfoAddress() {
515     return &gAdapterInfo;
516 }
517 
getLastCallMapGpuVaArg()518 D3DDDI_MAPGPUVIRTUALADDRESS *getLastCallMapGpuVaArg() {
519     return &gLastCallMapGpuVaArg;
520 }
521 
getLastCallReserveGpuVaArg()522 D3DDDI_RESERVEGPUVIRTUALADDRESS *getLastCallReserveGpuVaArg() {
523     return &gLastCallReserveGpuVaArg;
524 }
525 
setMapGpuVaFailConfig(uint32_t count,uint32_t max)526 void setMapGpuVaFailConfig(uint32_t count, uint32_t max) {
527     gMapGpuVaFailConfigCount = count;
528     gMapGpuVaFailConfigMax = max;
529 }
530 
getCreateContextData()531 D3DKMT_CREATECONTEXTVIRTUAL *getCreateContextData() {
532     return &createContextData;
533 }
534 
getCreateHwQueueData()535 D3DKMT_CREATEHWQUEUE *getCreateHwQueueData() {
536     return &createHwQueueData;
537 }
538 
getDestroyHwQueueData()539 D3DKMT_DESTROYHWQUEUE *getDestroyHwQueueData() {
540     return &destroyHwQueueData;
541 }
542 
getSubmitCommandToHwQueueData()543 D3DKMT_SUBMITCOMMANDTOHWQUEUE *getSubmitCommandToHwQueueData() {
544     return &submitCommandToHwQueueData;
545 }
546 
getDestroySynchronizationObjectData()547 D3DKMT_DESTROYSYNCHRONIZATIONOBJECT *getDestroySynchronizationObjectData() {
548     return &destroySynchronizationObjectData;
549 }
550 
getMonitorFenceCpuFenceAddress()551 VOID *getMonitorFenceCpuFenceAddress() {
552     return &cpuFence;
553 }
554 
getCreateSynchronizationObject2FailCall()555 bool *getCreateSynchronizationObject2FailCall() {
556     return &createSynchronizationObject2FailCall;
557 }
558 
getRegisterTrimNotificationFailCall()559 bool *getRegisterTrimNotificationFailCall() {
560     return &registerTrimNotificationFailCall;
561 }
562 
getLastPriority()563 uint32_t getLastPriority() {
564     return gLastPriority;
565 }
566 
setAdapterBDF(ADAPTER_BDF & adapterBDF)567 void setAdapterBDF(ADAPTER_BDF &adapterBDF) {
568     gAdapterBDF = adapterBDF;
569 }
570