VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/thread-r0drv-nt.cpp

Last change on this file was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.4 KB
Line 
1/* $Id: thread-r0drv-nt.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Threads, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "the-nt-kernel.h"
42#include "internal/iprt.h"
43#include <iprt/thread.h>
44
45#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
46# include <iprt/asm-amd64-x86.h>
47#endif
48#include <iprt/assert.h>
49#include <iprt/err.h>
50#include <iprt/mp.h>
51#include "internal-r0drv-nt.h"
52
53
54
55RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
56{
57 return (RTNATIVETHREAD)PsGetCurrentThread();
58}
59
60
61static int rtR0ThreadNtSleepCommon(RTMSINTERVAL cMillies)
62{
63 LARGE_INTEGER Interval;
64 Interval.QuadPart = -(int64_t)cMillies * 10000;
65 NTSTATUS rcNt = KeDelayExecutionThread(KernelMode, TRUE, &Interval);
66 switch (rcNt)
67 {
68 case STATUS_SUCCESS:
69 return VINF_SUCCESS;
70 case STATUS_ALERTED:
71 case STATUS_USER_APC:
72 return VERR_INTERRUPTED;
73 default:
74 return RTErrConvertFromNtStatus(rcNt);
75 }
76}
77
78
79RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
80{
81 return rtR0ThreadNtSleepCommon(cMillies);
82}
83
84
85RTDECL(bool) RTThreadYield(void)
86{
87 return ZwYieldExecution() != STATUS_NO_YIELD_PERFORMED;
88}
89
90
91RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
92{
93 Assert(hThread == NIL_RTTHREAD); RT_NOREF1(hThread);
94 KIRQL Irql = KeGetCurrentIrql();
95 if (Irql > APC_LEVEL)
96 return false;
97 if (!ASMIntAreEnabled())
98 return false;
99 return true;
100}
101
102
103RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
104{
105 Assert(hThread == NIL_RTTHREAD); RT_NOREF1(hThread);
106
107 /*
108 * The KeShouldYieldProcessor API introduced in Windows 10 looks like exactly
109 * what we want. But of course there is a snag. It may return with interrupts
110 * enabled when called with them disabled. Let's just hope it doesn't get upset
111 * by disabled interrupts in other ways...
112 */
113 if (g_pfnrtKeShouldYieldProcessor)
114 {
115 RTCCUINTREG fSavedFlags = ASMGetFlags();
116 bool fReturn = g_pfnrtKeShouldYieldProcessor() != FALSE;
117 ASMSetFlags(fSavedFlags);
118 return fReturn;
119 }
120
121 /*
122 * Fallback approach for pre W10 kernels.
123 *
124 * If W10 is anything to go by, we should also check and yield when:
125 * - pPrcb->NextThread != NULL && pPrcb->NextThread != pPrcb->CurrentThread
126 * when QuantumEnd is zero.
127 * - pPrcb->DpcRequestSummary & 1
128 * - pPrcb->DpcRequestSummary & 0x1e
129 */
130
131 /*
132 * Read the globals and check if they are useful.
133 */
134/** @todo Should we check KPRCB.InterruptRequest and KPRCB.DpcInterruptRequested (older kernels). */
135 uint32_t const offQuantumEnd = g_offrtNtPbQuantumEnd;
136 uint32_t const cbQuantumEnd = g_cbrtNtPbQuantumEnd;
137 uint32_t const offDpcQueueDepth = g_offrtNtPbDpcQueueDepth;
138 if (!offQuantumEnd && !cbQuantumEnd && !offDpcQueueDepth)
139 return false;
140 Assert((offQuantumEnd && cbQuantumEnd) || (!offQuantumEnd && !cbQuantumEnd));
141
142 /*
143 * Disable interrupts so we won't be messed around.
144 */
145 bool fPending;
146 RTCCUINTREG fSavedFlags = ASMIntDisableFlags();
147
148#ifdef RT_ARCH_X86
149 PKPCR pPcr = (PKPCR)__readfsdword(RT_UOFFSETOF(KPCR,SelfPcr));
150 uint8_t *pbPrcb = (uint8_t *)pPcr->Prcb;
151
152#elif defined(RT_ARCH_AMD64)
153 /* HACK ALERT! The offset is from windbg/vista64. */
154 PKPCR pPcr = (PKPCR)__readgsqword(RT_UOFFSETOF(KPCR,Self));
155 uint8_t *pbPrcb = (uint8_t *)pPcr->CurrentPrcb;
156
157#else
158# error "port me"
159#endif
160
161 /* Check QuantumEnd. */
162 if (cbQuantumEnd == 1)
163 {
164 uint8_t volatile *pbQuantumEnd = (uint8_t volatile *)(pbPrcb + offQuantumEnd);
165 fPending = *pbQuantumEnd == TRUE;
166 }
167 else if (cbQuantumEnd == sizeof(uint32_t))
168 {
169 uint32_t volatile *pu32QuantumEnd = (uint32_t volatile *)(pbPrcb + offQuantumEnd);
170 fPending = *pu32QuantumEnd != 0;
171 }
172 else
173 fPending = false;
174
175 /* Check DpcQueueDepth. */
176 if ( !fPending
177 && offDpcQueueDepth)
178 {
179 uint32_t volatile *pu32DpcQueueDepth = (uint32_t volatile *)(pbPrcb + offDpcQueueDepth);
180 fPending = *pu32DpcQueueDepth > 0;
181 }
182
183 ASMSetFlags(fSavedFlags);
184 return fPending;
185}
186
187
188RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
189{
190 if (g_pfnrtKeShouldYieldProcessor)
191 return true;
192#if 0 /** @todo RTThreadPreemptIsPending isn't good enough on w7 and possibly elsewhere. */
193 /* RTThreadPreemptIsPending is only reliable if we've got both offsets and size. */
194 return g_offrtNtPbQuantumEnd != 0
195 && g_cbrtNtPbQuantumEnd != 0
196 && g_offrtNtPbDpcQueueDepth != 0;
197#else
198 return false;
199#endif
200}
201
202
203RTDECL(bool) RTThreadPreemptIsPossible(void)
204{
205 /* yes, kernel preemption is possible. */
206 return true;
207}
208
209
210RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
211{
212 AssertPtr(pState);
213 Assert(pState->uchOldIrql == 255);
214 Assert(KeGetCurrentIrql() <= DISPATCH_LEVEL);
215
216 KeRaiseIrql(DISPATCH_LEVEL, &pState->uchOldIrql);
217 RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
218}
219
220
221RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
222{
223 AssertPtr(pState);
224
225 RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
226 KeLowerIrql(pState->uchOldIrql);
227 pState->uchOldIrql = 255;
228}
229
230
231RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
232{
233 Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
234
235 KIRQL CurIrql = KeGetCurrentIrql();
236 return CurIrql > PASSIVE_LEVEL; /** @todo Is there a more correct way? */
237}
238
239
240RTDECL(int) RTThreadQueryTerminationStatus(RTTHREAD hThread)
241{
242 AssertReturn(hThread == NIL_RTTHREAD, VERR_INVALID_HANDLE);
243 if (RT_LIKELY(g_pfnrtPsIsThreadTerminating))
244 {
245 BOOLEAN fRc = g_pfnrtPsIsThreadTerminating(PsGetCurrentThread());
246 return !fRc ? VINF_SUCCESS : VINF_THREAD_IS_TERMINATING;
247 }
248 return VERR_NOT_SUPPORTED;
249}
250
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use