File: external_semaphore_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 (234 lines) | stat: -rw-r--r-- 8,570 bytes parent folder | download
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
/*
 * Copyright (C) 2025 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/os_interface/windows/external_semaphore_windows.h"

#include "shared/source/os_interface/os_library.h"
#include "shared/source/os_interface/windows/d3dkmthk_wrapper.h"
#include "shared/source/os_interface/windows/gdi_interface.h"
#include "shared/source/os_interface/windows/wddm/wddm.h"
#include "shared/source/os_interface/windows/windows_wrapper.h"

#include <memory>

typedef VOID(NTAPI *_RtlInitUnicodeString)(PUNICODE_STRING destinationString, PCWSTR sourceString);
typedef VOID(NTAPI *_NtOpenDirectoryObject)(PHANDLE directoryHandle, ACCESS_MASK desiredAccess, POBJECT_ATTRIBUTES objectAttributes);

namespace NEO {

std::unique_ptr<ExternalSemaphore> ExternalSemaphore::create(OSInterface *osInterface, ExternalSemaphore::Type type, void *handle, int fd, const char *name) {
    if (osInterface) {
        auto externalSemaphore = ExternalSemaphoreWindows::create(osInterface);

        bool result = externalSemaphore->importSemaphore(handle, fd, 0, name, type, false);
        if (result == false) {
            return nullptr;
        }

        return externalSemaphore;
    }
    return nullptr;
}

std::unique_ptr<ExternalSemaphoreWindows> ExternalSemaphoreWindows::create(OSInterface *osInterface) {
    auto extSemWindows = std::make_unique<ExternalSemaphoreWindows>();
    extSemWindows->osInterface = osInterface;
    extSemWindows->state = SemaphoreState::Initial;

    return extSemWindows;
}

bool ExternalSemaphoreWindows::importSemaphore(void *extHandle, int fd, uint32_t flags, const char *name, Type type, bool isNative) {
    switch (type) {
    case ExternalSemaphore::D3d12Fence:
    case ExternalSemaphore::OpaqueWin32:
    case ExternalSemaphore::TimelineSemaphoreWin32:
        break;
    default:
        return false;
    }
    HANDLE syncNtHandle = nullptr;

    this->type = type;

    auto wddm = this->osInterface->getDriverModel()->as<Wddm>();

    syncNtHandle = reinterpret_cast<HANDLE>(extHandle);

    if (type == ExternalSemaphore::OpaqueWin32) {
        auto moduleHandle = GetModuleHandleA("ntdll.dll");
        _RtlInitUnicodeString rtlInitUnicodeString = (_RtlInitUnicodeString)GetProcAddress(moduleHandle, "RtlInitUnicodeString");
        _NtOpenDirectoryObject ntOpenDirectoryObject = (_NtOpenDirectoryObject)GetProcAddress(moduleHandle, "NtOpenDirectoryObject");

        HANDLE rootDirectory;
        OBJECT_ATTRIBUTES objectAttributesRootDirectory;
        UNICODE_STRING unicodeNameRootDirectory;
        PUNICODE_STRING pUnicodeNameRootDirectory = NULL;

        wchar_t baseName[] = L"\\BaseNamedObjects";
        rtlInitUnicodeString(&unicodeNameRootDirectory, baseName);
        pUnicodeNameRootDirectory = &unicodeNameRootDirectory;
        InitializeObjectAttributes(&objectAttributesRootDirectory, pUnicodeNameRootDirectory, 0, nullptr, nullptr);
        ntOpenDirectoryObject(&rootDirectory, 0x0004 /* DIRECTORY_CREATE_OBJECT */, &objectAttributesRootDirectory);

        this->pCpuAddress = MapViewOfFile(syncNtHandle, FILE_MAP_WRITE | FILE_MAP_READ, 0, 0, 0);

        auto sharedMemoryContentHeader = this->getSharedMemoryContentHeader();
        auto access = sharedMemoryContentHeader->access;
        auto pSyncName = &sharedMemoryContentHeader->sharedSyncName[0];
        this->pLastSignaledValue = &sharedMemoryContentHeader->lastSignaledValue;
        syncNtHandle = nullptr;

        OBJECT_ATTRIBUTES objectAttributes;
        UNICODE_STRING unicodeName;

        PUNICODE_STRING pUnicodeName = NULL;
        const wchar_t *pName = reinterpret_cast<wchar_t *>(pSyncName);

        if (pName) {
            rtlInitUnicodeString(&unicodeName, pName);
            pUnicodeName = &unicodeName;
        }

        InitializeObjectAttributes(&objectAttributes, pUnicodeName, 0, rootDirectory, nullptr);

        D3DKMT_OPENSYNCOBJECTNTHANDLEFROMNAME openName = {};
        openName.dwDesiredAccess = access;
        openName.pObjAttrib = &objectAttributes;
        auto status = wddm->getGdi()->openSyncObjectNtHandleFromName(&openName);
        if (status != STATUS_SUCCESS) {
            return false;
        }

        syncNtHandle = openName.hNtHandle;
    }

    if (type == ExternalSemaphore::TimelineSemaphoreWin32 && name != nullptr) {
        auto moduleHandle = GetModuleHandleA("ntdll.dll");
        _RtlInitUnicodeString rtlInitUnicodeString = (_RtlInitUnicodeString)GetProcAddress(moduleHandle, "RtlInitUnicodeString");
        _NtOpenDirectoryObject ntOpenDirectoryObject = (_NtOpenDirectoryObject)GetProcAddress(moduleHandle, "NtOpenDirectoryObject");

        HANDLE rootDirectory;
        OBJECT_ATTRIBUTES objectAttributesRootDirectory;
        UNICODE_STRING unicodeNameRootDirectory;
        PUNICODE_STRING pUnicodeNameRootDirectory = NULL;

        wchar_t baseName[] = L"\\BaseNamedObjects";
        rtlInitUnicodeString(&unicodeNameRootDirectory, baseName);
        pUnicodeNameRootDirectory = &unicodeNameRootDirectory;
        InitializeObjectAttributes(&objectAttributesRootDirectory, pUnicodeNameRootDirectory, 0, nullptr, nullptr);
        ntOpenDirectoryObject(&rootDirectory, 0x0004 /* DIRECTORY_CREATE_OBJECT */, &objectAttributesRootDirectory);

        OBJECT_ATTRIBUTES objectAttributes;
        UNICODE_STRING unicodeName;

        PUNICODE_STRING pUnicodeName = NULL;
        std::wstring wideName;
        size_t length = strlen(name) + 1;
        wideName.resize(length);
        mbstowcs(&wideName[0], name, length);

        const wchar_t *pName = wideName.c_str();

        rtlInitUnicodeString(&unicodeName, pName);
        pUnicodeName = &unicodeName;

        InitializeObjectAttributes(&objectAttributes, pUnicodeName, 0, rootDirectory, nullptr);

        D3DKMT_OPENSYNCOBJECTNTHANDLEFROMNAME openName = {};
        openName.dwDesiredAccess = D3DDDI_SYNC_OBJECT_ALL_ACCESS;
        openName.pObjAttrib = &objectAttributes;
        auto status = wddm->getGdi()->openSyncObjectNtHandleFromName(&openName);
        if (status != STATUS_SUCCESS) {
            return false;
        }

        syncNtHandle = openName.hNtHandle;
    }

    D3DKMT_OPENSYNCOBJECTFROMNTHANDLE2 open = {};
    open.hNtHandle = syncNtHandle;
    open.hDevice = wddm->getDeviceHandle();
    open.Flags.NoGPUAccess = (type == ExternalSemaphore::D3d12Fence);

    auto status = wddm->getGdi()->openSyncObjectFromNtHandle2(&open);
    if (status != STATUS_SUCCESS) {
        return false;
    }

    this->syncHandle = open.hSyncObject;

    return true;
}

bool ExternalSemaphoreWindows::enqueueWait(uint64_t *fenceValue) {
    auto wddm = this->osInterface->getDriverModel()->as<Wddm>();

    D3DDDI_WAITFORSYNCHRONIZATIONOBJECTFROMCPU_FLAGS waitFlags = {};
    waitFlags.WaitAny = false;

    D3DKMT_WAITFORSYNCHRONIZATIONOBJECTFROMCPU wait = {};
    wait.hDevice = wddm->getDeviceHandle();
    wait.ObjectCount = 1;
    wait.ObjectHandleArray = &this->syncHandle;

    uint64_t lastSignaledValue = 0;
    if (this->type == ExternalSemaphore::OpaqueWin32) {
        lastSignaledValue = *this->pLastSignaledValue;
        wait.FenceValueArray = &lastSignaledValue;
    } else {
        wait.FenceValueArray = fenceValue;
    }

    wait.FenceValueArray = fenceValue;
    wait.hAsyncEvent = nullptr;
    wait.Flags = waitFlags;

    auto status = wddm->getGdi()->waitForSynchronizationObjectFromCpu(&wait);
    if (status != STATUS_SUCCESS) {
        return false;
    }

    this->state = SemaphoreState::Signaled;

    return true;
}

bool ExternalSemaphoreWindows::enqueueSignal(uint64_t *fenceValue) {
    auto wddm = this->osInterface->getDriverModel()->as<Wddm>();

    D3DKMT_SIGNALSYNCHRONIZATIONOBJECTFROMCPU signal = {};
    signal.hDevice = wddm->getDeviceHandle();
    signal.ObjectCount = 1;
    signal.ObjectHandleArray = &this->syncHandle;

    if (this->type == ExternalSemaphore::TimelineSemaphoreWin32) {
        signal.Flags.AllowFenceRewind = true;
    }

    uint64_t lastSignaledValue = 0;
    if (this->type == ExternalSemaphore::OpaqueWin32) {
        lastSignaledValue = *this->pLastSignaledValue + 2;
        signal.FenceValueArray = &lastSignaledValue;
    } else {
        signal.FenceValueArray = fenceValue;
    }

    auto status = wddm->getGdi()->signalSynchronizationObjectFromCpu(&signal);
    if (status != STATUS_SUCCESS) {
        return false;
    }

    if (this->type == ExternalSemaphore::OpaqueWin32) {
        *this->pLastSignaledValue += 2;
    }
    this->state = SemaphoreState::Signaled;

    return true;
}

} // namespace NEO