VirtualBox

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

Last change on this file since 23487 was 23176, checked in by vboxsync, 15 years ago

Also unqueue the poke dpc.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1/* $Id: thread-r0drv-nt.cpp 23176 2009-09-21 09:58:32Z vboxsync $ */
2/** @file
3 * IPRT - Threads, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include "the-nt-kernel.h"
35#include "internal/iprt.h"
36#include "internal/mp.h"
37#include <iprt/thread.h>
38
39#include <iprt/asm.h>
40#include <iprt/assert.h>
41#include <iprt/err.h>
42#include <iprt/mp.h>
43#include "internal-r0drv-nt.h"
44
45
46RT_C_DECLS_BEGIN
47NTSTATUS NTAPI ZwYieldExecution(void);
48RT_C_DECLS_END
49
50
51RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
52{
53 return (RTNATIVETHREAD)PsGetCurrentThread();
54}
55
56
57RTDECL(int) RTThreadSleep(unsigned cMillies)
58{
59 LARGE_INTEGER Interval;
60 Interval.QuadPart = -(int64_t)cMillies * 10000;
61 NTSTATUS rcNt = KeDelayExecutionThread(KernelMode, TRUE, &Interval);
62 switch (rcNt)
63 {
64 case STATUS_SUCCESS:
65 return VINF_SUCCESS;
66 case STATUS_ALERTED:
67 case STATUS_USER_APC:
68 return VERR_INTERRUPTED;
69 default:
70 return RTErrConvertFromNtStatus(rcNt);
71 }
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);
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);
96
97 /* Remove any pending poke DPC from the queue, so another call to RTMpPokeCpu will send an IPI
98 * Also do this so we don't exit from ring 0 for the poke DPC (which does nothing).
99 */
100 rtMpPokeCpuClear();
101
102 /*
103 * Read the globals and check if they are useful.
104 */
105 uint32_t const offQuantumEnd = g_offrtNtPbQuantumEnd;
106 uint32_t const cbQuantumEnd = g_cbrtNtPbQuantumEnd;
107 uint32_t const offDpcQueueDepth = g_offrtNtPbDpcQueueDepth;
108 if (!offQuantumEnd && !cbQuantumEnd && !offDpcQueueDepth)
109 return false;
110 Assert((offQuantumEnd && cbQuantumEnd) || (!offQuantumEnd && !cbQuantumEnd));
111
112 /*
113 * Disable interrupts so we won't be messed around.
114 */
115 bool fPending;
116 RTCCUINTREG fSavedFlags = ASMIntDisableFlags();
117
118#ifdef RT_ARCH_X86
119 PKPCR pPcr = (PKPCR)__readfsdword(RT_OFFSETOF(KPCR,SelfPcr));
120 uint8_t *pbPrcb = (uint8_t *)pPcr->Prcb;
121
122#elif defined(RT_ARCH_AMD64)
123 /* HACK ALERT! The offset is from windbg/vista64. */
124 PKPCR pPcr = (PKPCR)__readgsqword(RT_OFFSETOF(KPCR,Self));
125 uint8_t *pbPrcb = (uint8_t *)pPcr->CurrentPrcb;
126
127#else
128# error "port me"
129#endif
130
131 /* Check QuantumEnd. */
132 if (cbQuantumEnd == 1)
133 {
134 uint8_t volatile *pbQuantumEnd = (uint8_t volatile *)(pbPrcb + offQuantumEnd);
135 fPending = *pbQuantumEnd == TRUE;
136 }
137 else if (cbQuantumEnd == sizeof(uint32_t))
138 {
139 uint32_t volatile *pu32QuantumEnd = (uint32_t volatile *)(pbPrcb + offQuantumEnd);
140 fPending = *pu32QuantumEnd != 0;
141 }
142
143 /* Check DpcQueueDepth. */
144 if ( !fPending
145 && offDpcQueueDepth)
146 {
147 uint32_t volatile *pu32DpcQueueDepth = (uint32_t volatile *)(pbPrcb + offDpcQueueDepth);
148 fPending = *pu32DpcQueueDepth > 0;
149 }
150
151 ASMSetFlags(fSavedFlags);
152 return fPending;
153}
154
155
156RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
157{
158 /* RTThreadPreemptIsPending is only reliable of we've got both offsets and size. */
159 return g_offrtNtPbQuantumEnd != 0
160 && g_cbrtNtPbQuantumEnd != 0
161 && g_offrtNtPbDpcQueueDepth != 0;
162}
163
164
165RTDECL(bool) RTThreadPreemptIsPossible(void)
166{
167 /* yes, kernel preemption is possible. */
168 return true;
169}
170
171
172RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
173{
174 AssertPtr(pState);
175 Assert(pState->uchOldIrql == 255);
176 Assert(KeGetCurrentIrql() <= DISPATCH_LEVEL);
177
178 KeRaiseIrql(DISPATCH_LEVEL, &pState->uchOldIrql);
179 RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
180}
181
182
183RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
184{
185 AssertPtr(pState);
186
187 RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
188 KeLowerIrql(pState->uchOldIrql);
189 pState->uchOldIrql = 255;
190}
191
192
193RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
194{
195 Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
196
197 KIRQL CurIrql = KeGetCurrentIrql();
198 return CurIrql > PASSIVE_LEVEL; /** @todo Is there a more correct way? */
199}
200
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