VirtualBox

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

Last change on this file since 75578 was 73097, checked in by vboxsync, 6 years ago

*: Made RT_UOFFSETOF, RT_OFFSETOF, RT_UOFFSETOF_ADD and RT_OFFSETOF_ADD work like builtin_offsetof() and require compile time resolvable requests, adding RT_UOFFSETOF_DYN for the dynamic questions that can only be answered at runtime.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.7 KB
Line 
1/* $Id: thread-r0drv-nt.cpp 73097 2018-07-12 21:06:33Z vboxsync $ */
2/** @file
3 * IPRT - Threads, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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#include "the-nt-kernel.h"
32#include "internal/iprt.h"
33#include <iprt/thread.h>
34
35#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
36# include <iprt/asm-amd64-x86.h>
37#endif
38#include <iprt/assert.h>
39#include <iprt/err.h>
40#include <iprt/mp.h>
41#include "internal-r0drv-nt.h"
42
43
44
45RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
46{
47 return (RTNATIVETHREAD)PsGetCurrentThread();
48}
49
50
51static int rtR0ThreadNtSleepCommon(RTMSINTERVAL cMillies)
52{
53 LARGE_INTEGER Interval;
54 Interval.QuadPart = -(int64_t)cMillies * 10000;
55 NTSTATUS rcNt = KeDelayExecutionThread(KernelMode, TRUE, &Interval);
56 switch (rcNt)
57 {
58 case STATUS_SUCCESS:
59 return VINF_SUCCESS;
60 case STATUS_ALERTED:
61 case STATUS_USER_APC:
62 return VERR_INTERRUPTED;
63 default:
64 return RTErrConvertFromNtStatus(rcNt);
65 }
66}
67
68
69RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
70{
71 return rtR0ThreadNtSleepCommon(cMillies);
72}
73
74
75RTDECL(bool) RTThreadYield(void)
76{
77 return ZwYieldExecution() != STATUS_NO_YIELD_PERFORMED;
78}
79
80
81RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
82{
83 Assert(hThread == NIL_RTTHREAD); RT_NOREF1(hThread);
84 KIRQL Irql = KeGetCurrentIrql();
85 if (Irql > APC_LEVEL)
86 return false;
87 if (!ASMIntAreEnabled())
88 return false;
89 return true;
90}
91
92
93RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
94{
95 Assert(hThread == NIL_RTTHREAD); RT_NOREF1(hThread);
96
97 /*
98 * The KeShouldYieldProcessor API introduced in Windows 10 looks like exactly
99 * what we want. But of course there is a snag. It may return with interrupts
100 * enabled when called with them disabled. Let's just hope it doesn't get upset
101 * by disabled interrupts in other ways...
102 */
103 if (g_pfnrtKeShouldYieldProcessor)
104 {
105 RTCCUINTREG fSavedFlags = ASMGetFlags();
106 bool fReturn = g_pfnrtKeShouldYieldProcessor() != FALSE;
107 ASMSetFlags(fSavedFlags);
108 return fReturn;
109 }
110
111 /*
112 * Fallback approach for pre W10 kernels.
113 *
114 * If W10 is anything to go by, we should also check and yield when:
115 * - pPrcb->NextThread != NULL && pPrcb->NextThread != pPrcb->CurrentThread
116 * when QuantumEnd is zero.
117 * - pPrcb->DpcRequestSummary & 1
118 * - pPrcb->DpcRequestSummary & 0x1e
119 */
120
121 /*
122 * Read the globals and check if they are useful.
123 */
124/** @todo Should we check KPRCB.InterruptRequest and KPRCB.DpcInterruptRequested (older kernels). */
125 uint32_t const offQuantumEnd = g_offrtNtPbQuantumEnd;
126 uint32_t const cbQuantumEnd = g_cbrtNtPbQuantumEnd;
127 uint32_t const offDpcQueueDepth = g_offrtNtPbDpcQueueDepth;
128 if (!offQuantumEnd && !cbQuantumEnd && !offDpcQueueDepth)
129 return false;
130 Assert((offQuantumEnd && cbQuantumEnd) || (!offQuantumEnd && !cbQuantumEnd));
131
132 /*
133 * Disable interrupts so we won't be messed around.
134 */
135 bool fPending;
136 RTCCUINTREG fSavedFlags = ASMIntDisableFlags();
137
138#ifdef RT_ARCH_X86
139 PKPCR pPcr = (PKPCR)__readfsdword(RT_UOFFSETOF(KPCR,SelfPcr));
140 uint8_t *pbPrcb = (uint8_t *)pPcr->Prcb;
141
142#elif defined(RT_ARCH_AMD64)
143 /* HACK ALERT! The offset is from windbg/vista64. */
144 PKPCR pPcr = (PKPCR)__readgsqword(RT_UOFFSETOF(KPCR,Self));
145 uint8_t *pbPrcb = (uint8_t *)pPcr->CurrentPrcb;
146
147#else
148# error "port me"
149#endif
150
151 /* Check QuantumEnd. */
152 if (cbQuantumEnd == 1)
153 {
154 uint8_t volatile *pbQuantumEnd = (uint8_t volatile *)(pbPrcb + offQuantumEnd);
155 fPending = *pbQuantumEnd == TRUE;
156 }
157 else if (cbQuantumEnd == sizeof(uint32_t))
158 {
159 uint32_t volatile *pu32QuantumEnd = (uint32_t volatile *)(pbPrcb + offQuantumEnd);
160 fPending = *pu32QuantumEnd != 0;
161 }
162 else
163 fPending = false;
164
165 /* Check DpcQueueDepth. */
166 if ( !fPending
167 && offDpcQueueDepth)
168 {
169 uint32_t volatile *pu32DpcQueueDepth = (uint32_t volatile *)(pbPrcb + offDpcQueueDepth);
170 fPending = *pu32DpcQueueDepth > 0;
171 }
172
173 ASMSetFlags(fSavedFlags);
174 return fPending;
175}
176
177
178RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
179{
180 if (g_pfnrtKeShouldYieldProcessor)
181 return true;
182#if 0 /** @todo RTThreadPreemptIsPending isn't good enough on w7 and possibly elsewhere. */
183 /* RTThreadPreemptIsPending is only reliable if we've got both offsets and size. */
184 return g_offrtNtPbQuantumEnd != 0
185 && g_cbrtNtPbQuantumEnd != 0
186 && g_offrtNtPbDpcQueueDepth != 0;
187#else
188 return false;
189#endif
190}
191
192
193RTDECL(bool) RTThreadPreemptIsPossible(void)
194{
195 /* yes, kernel preemption is possible. */
196 return true;
197}
198
199
200RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
201{
202 AssertPtr(pState);
203 Assert(pState->uchOldIrql == 255);
204 Assert(KeGetCurrentIrql() <= DISPATCH_LEVEL);
205
206 KeRaiseIrql(DISPATCH_LEVEL, &pState->uchOldIrql);
207 RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
208}
209
210
211RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
212{
213 AssertPtr(pState);
214
215 RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
216 KeLowerIrql(pState->uchOldIrql);
217 pState->uchOldIrql = 255;
218}
219
220
221RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
222{
223 Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
224
225 KIRQL CurIrql = KeGetCurrentIrql();
226 return CurIrql > PASSIVE_LEVEL; /** @todo Is there a more correct way? */
227}
228
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette