1 | /* $Id: semeventmulti-r0drv-nt.cpp 90488 2021-08-03 09:17:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define RTSEMEVENTMULTI_WITHOUT_REMAPPING
|
---|
32 | #include "the-nt-kernel.h"
|
---|
33 | #include <iprt/semaphore.h>
|
---|
34 |
|
---|
35 | #include <iprt/asm.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/lockvalidator.h>
|
---|
39 | #include <iprt/mem.h>
|
---|
40 | #include <iprt/time.h>
|
---|
41 | #include <iprt/timer.h>
|
---|
42 |
|
---|
43 | #include "internal/magics.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | /*********************************************************************************************************************************
|
---|
47 | * Structures and Typedefs *
|
---|
48 | *********************************************************************************************************************************/
|
---|
49 | /**
|
---|
50 | * NT event semaphore.
|
---|
51 | */
|
---|
52 | typedef struct RTSEMEVENTMULTIINTERNAL
|
---|
53 | {
|
---|
54 | /** Magic value (RTSEMEVENTMULTI_MAGIC). */
|
---|
55 | uint32_t volatile u32Magic;
|
---|
56 | /** Reference counter. */
|
---|
57 | uint32_t volatile cRefs;
|
---|
58 | /** The NT Event object. */
|
---|
59 | KEVENT Event;
|
---|
60 | } RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
|
---|
61 |
|
---|
62 |
|
---|
63 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
|
---|
64 | {
|
---|
65 | return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
|
---|
70 | const char *pszNameFmt, ...)
|
---|
71 | {
|
---|
72 | AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
|
---|
73 | RT_NOREF2(hClass, pszNameFmt);
|
---|
74 |
|
---|
75 | AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
|
---|
76 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
77 | if (pThis)
|
---|
78 | {
|
---|
79 | pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
|
---|
80 | pThis->cRefs = 1;
|
---|
81 | KeInitializeEvent(&pThis->Event, NotificationEvent, FALSE /* not signalled */);
|
---|
82 |
|
---|
83 | *phEventMultiSem = pThis;
|
---|
84 | return VINF_SUCCESS;
|
---|
85 | }
|
---|
86 | return VERR_NO_MEMORY;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Retains a reference to the semaphore.
|
---|
92 | *
|
---|
93 | * @param pThis The semaphore to retain.
|
---|
94 | */
|
---|
95 | DECLINLINE(void) rtR0SemEventMultiNtRetain(PRTSEMEVENTMULTIINTERNAL pThis)
|
---|
96 | {
|
---|
97 | uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
98 | Assert(cRefs < 100000); NOREF(cRefs);
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Releases a reference to the semaphore.
|
---|
104 | *
|
---|
105 | * @param pThis The semaphore to release
|
---|
106 | */
|
---|
107 | DECLINLINE(void) rtR0SemEventMultiNtRelease(PRTSEMEVENTMULTIINTERNAL pThis)
|
---|
108 | {
|
---|
109 | if (ASMAtomicDecU32(&pThis->cRefs) == 0)
|
---|
110 | RTMemFree(pThis);
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
|
---|
115 | {
|
---|
116 | /*
|
---|
117 | * Validate input.
|
---|
118 | */
|
---|
119 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
120 | if (pThis == NIL_RTSEMEVENTMULTI)
|
---|
121 | return VINF_SUCCESS;
|
---|
122 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
123 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Invalidate it and signal the object just in case.
|
---|
127 | */
|
---|
128 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
129 | KeSetEvent(&pThis->Event, 0xfff, FALSE);
|
---|
130 | rtR0SemEventMultiNtRelease(pThis);
|
---|
131 | return VINF_SUCCESS;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
|
---|
136 | {
|
---|
137 | /*
|
---|
138 | * Validate input.
|
---|
139 | */
|
---|
140 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
141 | if (!pThis)
|
---|
142 | return VERR_INVALID_PARAMETER;
|
---|
143 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
144 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
145 | rtR0SemEventMultiNtRetain(pThis);
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * Signal the event object.
|
---|
149 | */
|
---|
150 | KeSetEvent(&pThis->Event, 1, FALSE);
|
---|
151 |
|
---|
152 | rtR0SemEventMultiNtRelease(pThis);
|
---|
153 | return VINF_SUCCESS;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
|
---|
158 | {
|
---|
159 | /*
|
---|
160 | * Validate input.
|
---|
161 | */
|
---|
162 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
163 | if (!pThis)
|
---|
164 | return VERR_INVALID_PARAMETER;
|
---|
165 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
166 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
167 | rtR0SemEventMultiNtRetain(pThis);
|
---|
168 |
|
---|
169 | /*
|
---|
170 | * Reset the event object.
|
---|
171 | */
|
---|
172 | KeResetEvent(&pThis->Event);
|
---|
173 |
|
---|
174 | rtR0SemEventMultiNtRelease(pThis);
|
---|
175 | return VINF_SUCCESS;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Worker for RTSemEventMultiWaitEx and RTSemEventMultiWaitExDebug.
|
---|
181 | *
|
---|
182 | * @returns VBox status code.
|
---|
183 | * @param pThis The event semaphore.
|
---|
184 | * @param fFlags See RTSemEventMultiWaitEx.
|
---|
185 | * @param uTimeout See RTSemEventMultiWaitEx.
|
---|
186 | * @param pSrcPos The source code position of the wait.
|
---|
187 | */
|
---|
188 | DECLINLINE(int) rtR0SemEventMultiNtWait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
|
---|
189 | PCRTLOCKVALSRCPOS pSrcPos)
|
---|
190 | {
|
---|
191 | /*
|
---|
192 | * Validate input.
|
---|
193 | */
|
---|
194 | if (!pThis)
|
---|
195 | return VERR_INVALID_PARAMETER;
|
---|
196 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
197 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
198 | AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
|
---|
199 | RT_NOREF1(pSrcPos);
|
---|
200 |
|
---|
201 | rtR0SemEventMultiNtRetain(pThis);
|
---|
202 |
|
---|
203 | /*
|
---|
204 | * Convert the timeout to a relative one because KeWaitForSingleObject
|
---|
205 | * takes system time instead of interrupt time as input for absolute
|
---|
206 | * timeout specifications. So, we're best of by giving it relative time.
|
---|
207 | *
|
---|
208 | * Lazy bird converts uTimeout to relative nanoseconds and then to Nt time.
|
---|
209 | */
|
---|
210 | if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
|
---|
211 | {
|
---|
212 | if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
|
---|
213 | uTimeout = uTimeout < UINT64_MAX / UINT32_C(1000000) * UINT32_C(1000000)
|
---|
214 | ? uTimeout * UINT32_C(1000000)
|
---|
215 | : UINT64_MAX;
|
---|
216 | if (uTimeout == UINT64_MAX)
|
---|
217 | fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
|
---|
218 | else
|
---|
219 | {
|
---|
220 | if (fFlags & RTSEMWAIT_FLAGS_ABSOLUTE)
|
---|
221 | {
|
---|
222 | uint64_t u64Now = RTTimeSystemNanoTS();
|
---|
223 | uTimeout = u64Now < uTimeout
|
---|
224 | ? uTimeout - u64Now
|
---|
225 | : 0;
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | /*
|
---|
231 | * Wait for it.
|
---|
232 | * We're assuming interruptible waits should happen at UserMode level.
|
---|
233 | */
|
---|
234 | NTSTATUS rcNt;
|
---|
235 | BOOLEAN fInterruptible = !!(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE);
|
---|
236 | KPROCESSOR_MODE WaitMode = fInterruptible ? UserMode : KernelMode;
|
---|
237 | if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
|
---|
238 | rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, NULL);
|
---|
239 | else
|
---|
240 | {
|
---|
241 | LARGE_INTEGER Timeout;
|
---|
242 | Timeout.QuadPart = -(int64_t)(uTimeout / 100);
|
---|
243 | rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, &Timeout);
|
---|
244 | }
|
---|
245 | int rc;
|
---|
246 | if (pThis->u32Magic == RTSEMEVENTMULTI_MAGIC)
|
---|
247 | {
|
---|
248 | switch (rcNt)
|
---|
249 | {
|
---|
250 | case STATUS_SUCCESS:
|
---|
251 | rc = VINF_SUCCESS;
|
---|
252 | break;
|
---|
253 | case STATUS_ALERTED:
|
---|
254 | rc = VERR_INTERRUPTED;
|
---|
255 | break;
|
---|
256 | case STATUS_USER_APC:
|
---|
257 | rc = VERR_INTERRUPTED;
|
---|
258 | break;
|
---|
259 | case STATUS_TIMEOUT:
|
---|
260 | rc = VERR_TIMEOUT;
|
---|
261 | break;
|
---|
262 | default:
|
---|
263 | AssertMsgFailed(("pThis->u32Magic=%RX32 pThis=%p: wait returned %lx!\n",
|
---|
264 | pThis->u32Magic, pThis, (long)rcNt));
|
---|
265 | rc = VERR_INTERNAL_ERROR_4;
|
---|
266 | break;
|
---|
267 | }
|
---|
268 | }
|
---|
269 | else
|
---|
270 | rc = VERR_SEM_DESTROYED;
|
---|
271 |
|
---|
272 | rtR0SemEventMultiNtRelease(pThis);
|
---|
273 | return rc;
|
---|
274 | }
|
---|
275 |
|
---|
276 |
|
---|
277 | RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout)
|
---|
278 | {
|
---|
279 | #ifndef RTSEMEVENT_STRICT
|
---|
280 | return rtR0SemEventMultiNtWait(hEventMultiSem, fFlags, uTimeout, NULL);
|
---|
281 | #else
|
---|
282 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
283 | return rtR0SemEventMultiNtWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
|
---|
284 | #endif
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
|
---|
289 | RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
290 | {
|
---|
291 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
292 | return rtR0SemEventMultiNtWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | RTDECL(uint32_t) RTSemEventMultiGetResolution(void)
|
---|
297 | {
|
---|
298 | return RTTimerGetSystemGranularity();
|
---|
299 | }
|
---|
300 |
|
---|
301 |
|
---|
302 | RTR0DECL(bool) RTSemEventMultiIsSignalSafe(void)
|
---|
303 | {
|
---|
304 | return KeGetCurrentIrql() <= DISPATCH_LEVEL;
|
---|
305 | }
|
---|
306 |
|
---|