1 | /* $Id: thread-r0drv-nt.cpp 29254 2010-05-09 18:11:22Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads, Ring-0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 |
|
---|
43 | RT_C_DECLS_BEGIN
|
---|
44 | NTSTATUS NTAPI ZwYieldExecution(void);
|
---|
45 | RT_C_DECLS_END
|
---|
46 |
|
---|
47 |
|
---|
48 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
49 | {
|
---|
50 | return (RTNATIVETHREAD)PsGetCurrentThread();
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | RTDECL(int) RTThreadSleep(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 |
|
---|
72 | RTDECL(bool) RTThreadYield(void)
|
---|
73 | {
|
---|
74 | return ZwYieldExecution() != STATUS_NO_YIELD_PERFORMED;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
|
---|
79 | {
|
---|
80 | Assert(hThread == NIL_RTTHREAD);
|
---|
81 | KIRQL Irql = KeGetCurrentIrql();
|
---|
82 | if (Irql > APC_LEVEL)
|
---|
83 | return false;
|
---|
84 | if (!ASMIntAreEnabled())
|
---|
85 | return false;
|
---|
86 | return true;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
|
---|
91 | {
|
---|
92 | Assert(hThread == NIL_RTTHREAD);
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * Read the globals and check if they are useful.
|
---|
96 | */
|
---|
97 | uint32_t const offQuantumEnd = g_offrtNtPbQuantumEnd;
|
---|
98 | uint32_t const cbQuantumEnd = g_cbrtNtPbQuantumEnd;
|
---|
99 | uint32_t const offDpcQueueDepth = g_offrtNtPbDpcQueueDepth;
|
---|
100 | if (!offQuantumEnd && !cbQuantumEnd && !offDpcQueueDepth)
|
---|
101 | return false;
|
---|
102 | Assert((offQuantumEnd && cbQuantumEnd) || (!offQuantumEnd && !cbQuantumEnd));
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Disable interrupts so we won't be messed around.
|
---|
106 | */
|
---|
107 | bool fPending;
|
---|
108 | RTCCUINTREG fSavedFlags = ASMIntDisableFlags();
|
---|
109 |
|
---|
110 | #ifdef RT_ARCH_X86
|
---|
111 | PKPCR pPcr = (PKPCR)__readfsdword(RT_OFFSETOF(KPCR,SelfPcr));
|
---|
112 | uint8_t *pbPrcb = (uint8_t *)pPcr->Prcb;
|
---|
113 |
|
---|
114 | #elif defined(RT_ARCH_AMD64)
|
---|
115 | /* HACK ALERT! The offset is from windbg/vista64. */
|
---|
116 | PKPCR pPcr = (PKPCR)__readgsqword(RT_OFFSETOF(KPCR,Self));
|
---|
117 | uint8_t *pbPrcb = (uint8_t *)pPcr->CurrentPrcb;
|
---|
118 |
|
---|
119 | #else
|
---|
120 | # error "port me"
|
---|
121 | #endif
|
---|
122 |
|
---|
123 | /* Check QuantumEnd. */
|
---|
124 | if (cbQuantumEnd == 1)
|
---|
125 | {
|
---|
126 | uint8_t volatile *pbQuantumEnd = (uint8_t volatile *)(pbPrcb + offQuantumEnd);
|
---|
127 | fPending = *pbQuantumEnd == TRUE;
|
---|
128 | }
|
---|
129 | else if (cbQuantumEnd == sizeof(uint32_t))
|
---|
130 | {
|
---|
131 | uint32_t volatile *pu32QuantumEnd = (uint32_t volatile *)(pbPrcb + offQuantumEnd);
|
---|
132 | fPending = *pu32QuantumEnd != 0;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /* Check DpcQueueDepth. */
|
---|
136 | if ( !fPending
|
---|
137 | && offDpcQueueDepth)
|
---|
138 | {
|
---|
139 | uint32_t volatile *pu32DpcQueueDepth = (uint32_t volatile *)(pbPrcb + offDpcQueueDepth);
|
---|
140 | fPending = *pu32DpcQueueDepth > 0;
|
---|
141 | }
|
---|
142 |
|
---|
143 | ASMSetFlags(fSavedFlags);
|
---|
144 | return fPending;
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
|
---|
149 | {
|
---|
150 | /* RTThreadPreemptIsPending is only reliable of we've got both offsets and size. */
|
---|
151 | return g_offrtNtPbQuantumEnd != 0
|
---|
152 | && g_cbrtNtPbQuantumEnd != 0
|
---|
153 | && g_offrtNtPbDpcQueueDepth != 0;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | RTDECL(bool) RTThreadPreemptIsPossible(void)
|
---|
158 | {
|
---|
159 | /* yes, kernel preemption is possible. */
|
---|
160 | return true;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
|
---|
165 | {
|
---|
166 | AssertPtr(pState);
|
---|
167 | Assert(pState->uchOldIrql == 255);
|
---|
168 | Assert(KeGetCurrentIrql() <= DISPATCH_LEVEL);
|
---|
169 |
|
---|
170 | KeRaiseIrql(DISPATCH_LEVEL, &pState->uchOldIrql);
|
---|
171 | RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
|
---|
176 | {
|
---|
177 | AssertPtr(pState);
|
---|
178 |
|
---|
179 | RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
|
---|
180 | KeLowerIrql(pState->uchOldIrql);
|
---|
181 | pState->uchOldIrql = 255;
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
|
---|
186 | {
|
---|
187 | Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
|
---|
188 |
|
---|
189 | KIRQL CurIrql = KeGetCurrentIrql();
|
---|
190 | return CurIrql > PASSIVE_LEVEL; /** @todo Is there a more correct way? */
|
---|
191 | }
|
---|
192 |
|
---|