VirtualBox

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

Last change on this file since 53517 was 49287, checked in by vboxsync, 11 years ago

thread-r0drv-nt.cpp: Changed RTThreadPreemptIsPendingTrusty to always return false as it appears that RTThreadPreemptIsPending isn't good enough for at least W7+.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.7 KB
Line 
1/* $Id: thread-r0drv-nt.cpp 49287 2013-10-25 12:27:58Z vboxsync $ */
2/** @file
3 * IPRT - Threads, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2011 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* Header Files *
29*******************************************************************************/
30#include "the-nt-kernel.h"
31#include "internal/iprt.h"
32#include <iprt/thread.h>
33
34#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
35# include <iprt/asm-amd64-x86.h>
36#endif
37#include <iprt/assert.h>
38#include <iprt/err.h>
39#include <iprt/mp.h>
40#include "internal-r0drv-nt.h"
41
42
43RT_C_DECLS_BEGIN
44NTSTATUS NTAPI ZwYieldExecution(void);
45RT_C_DECLS_END
46
47
48RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
49{
50 return (RTNATIVETHREAD)PsGetCurrentThread();
51}
52
53
54static int rtR0ThreadNtSleepCommon(RTMSINTERVAL cMillies)
55{
56 LARGE_INTEGER Interval;
57 Interval.QuadPart = -(int64_t)cMillies * 10000;
58 NTSTATUS rcNt = KeDelayExecutionThread(KernelMode, TRUE, &Interval);
59 switch (rcNt)
60 {
61 case STATUS_SUCCESS:
62 return VINF_SUCCESS;
63 case STATUS_ALERTED:
64 case STATUS_USER_APC:
65 return VERR_INTERRUPTED;
66 default:
67 return RTErrConvertFromNtStatus(rcNt);
68 }
69}
70
71
72RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
73{
74 return rtR0ThreadNtSleepCommon(cMillies);
75}
76
77
78RTDECL(int) RTThreadSleepCommon(RTMSINTERVAL cMillies)
79{
80 return rtR0ThreadNtSleepCommon(cMillies);
81}
82
83
84RTDECL(bool) RTThreadYield(void)
85{
86 return ZwYieldExecution() != STATUS_NO_YIELD_PERFORMED;
87}
88
89
90RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
91{
92 Assert(hThread == NIL_RTTHREAD);
93 KIRQL Irql = KeGetCurrentIrql();
94 if (Irql > APC_LEVEL)
95 return false;
96 if (!ASMIntAreEnabled())
97 return false;
98 return true;
99}
100
101
102RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
103{
104 Assert(hThread == NIL_RTTHREAD);
105
106 /*
107 * Read the globals and check if they are useful.
108 */
109/** @todo Should we check KPRCB.InterruptRequest and KPRCB.DpcInterruptRequested (older kernels). */
110 uint32_t const offQuantumEnd = g_offrtNtPbQuantumEnd;
111 uint32_t const cbQuantumEnd = g_cbrtNtPbQuantumEnd;
112 uint32_t const offDpcQueueDepth = g_offrtNtPbDpcQueueDepth;
113 if (!offQuantumEnd && !cbQuantumEnd && !offDpcQueueDepth)
114 return false;
115 Assert((offQuantumEnd && cbQuantumEnd) || (!offQuantumEnd && !cbQuantumEnd));
116
117 /*
118 * Disable interrupts so we won't be messed around.
119 */
120 bool fPending;
121 RTCCUINTREG fSavedFlags = ASMIntDisableFlags();
122
123#ifdef RT_ARCH_X86
124 PKPCR pPcr = (PKPCR)__readfsdword(RT_OFFSETOF(KPCR,SelfPcr));
125 uint8_t *pbPrcb = (uint8_t *)pPcr->Prcb;
126
127#elif defined(RT_ARCH_AMD64)
128 /* HACK ALERT! The offset is from windbg/vista64. */
129 PKPCR pPcr = (PKPCR)__readgsqword(RT_OFFSETOF(KPCR,Self));
130 uint8_t *pbPrcb = (uint8_t *)pPcr->CurrentPrcb;
131
132#else
133# error "port me"
134#endif
135
136 /* Check QuantumEnd. */
137 if (cbQuantumEnd == 1)
138 {
139 uint8_t volatile *pbQuantumEnd = (uint8_t volatile *)(pbPrcb + offQuantumEnd);
140 fPending = *pbQuantumEnd == TRUE;
141 }
142 else if (cbQuantumEnd == sizeof(uint32_t))
143 {
144 uint32_t volatile *pu32QuantumEnd = (uint32_t volatile *)(pbPrcb + offQuantumEnd);
145 fPending = *pu32QuantumEnd != 0;
146 }
147
148 /* Check DpcQueueDepth. */
149 if ( !fPending
150 && offDpcQueueDepth)
151 {
152 uint32_t volatile *pu32DpcQueueDepth = (uint32_t volatile *)(pbPrcb + offDpcQueueDepth);
153 fPending = *pu32DpcQueueDepth > 0;
154 }
155
156 ASMSetFlags(fSavedFlags);
157 return fPending;
158}
159
160
161RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
162{
163#if 0 /** @todo RTThreadPreemptIsPending isn't good enough on w7 and possibly elsewhere. */
164 /* RTThreadPreemptIsPending is only reliable if we've got both offsets and size. */
165 return g_offrtNtPbQuantumEnd != 0
166 && g_cbrtNtPbQuantumEnd != 0
167 && g_offrtNtPbDpcQueueDepth != 0;
168#else
169 return false;
170#endif
171}
172
173
174RTDECL(bool) RTThreadPreemptIsPossible(void)
175{
176 /* yes, kernel preemption is possible. */
177 return true;
178}
179
180
181RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
182{
183 AssertPtr(pState);
184 Assert(pState->uchOldIrql == 255);
185 Assert(KeGetCurrentIrql() <= DISPATCH_LEVEL);
186
187 KeRaiseIrql(DISPATCH_LEVEL, &pState->uchOldIrql);
188 RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
189}
190
191
192RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
193{
194 AssertPtr(pState);
195
196 RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
197 KeLowerIrql(pState->uchOldIrql);
198 pState->uchOldIrql = 255;
199}
200
201
202RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
203{
204 Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
205
206 KIRQL CurIrql = KeGetCurrentIrql();
207 return CurIrql > PASSIVE_LEVEL; /** @todo Is there a more correct way? */
208}
209
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