VirtualBox

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

Last change on this file since 61572 was 57409, checked in by vboxsync, 9 years ago

thread-r0drv-nt.cpp: Remove unused function RTThreadSleepCommon.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.8 KB
Line 
1/* $Id: thread-r0drv-nt.cpp 57409 2015-08-18 09:58:24Z vboxsync $ */
2/** @file
3 * IPRT - Threads, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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
44RT_C_DECLS_BEGIN
45NTSTATUS NTAPI ZwYieldExecution(void);
46RT_C_DECLS_END
47
48
49RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
50{
51 return (RTNATIVETHREAD)PsGetCurrentThread();
52}
53
54
55static int rtR0ThreadNtSleepCommon(RTMSINTERVAL cMillies)
56{
57 LARGE_INTEGER Interval;
58 Interval.QuadPart = -(int64_t)cMillies * 10000;
59 NTSTATUS rcNt = KeDelayExecutionThread(KernelMode, TRUE, &Interval);
60 switch (rcNt)
61 {
62 case STATUS_SUCCESS:
63 return VINF_SUCCESS;
64 case STATUS_ALERTED:
65 case STATUS_USER_APC:
66 return VERR_INTERRUPTED;
67 default:
68 return RTErrConvertFromNtStatus(rcNt);
69 }
70}
71
72
73RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
74{
75 return rtR0ThreadNtSleepCommon(cMillies);
76}
77
78
79RTDECL(bool) RTThreadYield(void)
80{
81 return ZwYieldExecution() != STATUS_NO_YIELD_PERFORMED;
82}
83
84
85RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
86{
87 Assert(hThread == NIL_RTTHREAD);
88 KIRQL Irql = KeGetCurrentIrql();
89 if (Irql > APC_LEVEL)
90 return false;
91 if (!ASMIntAreEnabled())
92 return false;
93 return true;
94}
95
96
97RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
98{
99 Assert(hThread == NIL_RTTHREAD);
100
101 /*
102 * Read the globals and check if they are useful.
103 */
104/** @todo Should we check KPRCB.InterruptRequest and KPRCB.DpcInterruptRequested (older kernels). */
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#if 0 /** @todo RTThreadPreemptIsPending isn't good enough on w7 and possibly elsewhere. */
159 /* RTThreadPreemptIsPending is only reliable if we've got both offsets and size. */
160 return g_offrtNtPbQuantumEnd != 0
161 && g_cbrtNtPbQuantumEnd != 0
162 && g_offrtNtPbDpcQueueDepth != 0;
163#else
164 return false;
165#endif
166}
167
168
169RTDECL(bool) RTThreadPreemptIsPossible(void)
170{
171 /* yes, kernel preemption is possible. */
172 return true;
173}
174
175
176RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
177{
178 AssertPtr(pState);
179 Assert(pState->uchOldIrql == 255);
180 Assert(KeGetCurrentIrql() <= DISPATCH_LEVEL);
181
182 KeRaiseIrql(DISPATCH_LEVEL, &pState->uchOldIrql);
183 RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
184}
185
186
187RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
188{
189 AssertPtr(pState);
190
191 RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
192 KeLowerIrql(pState->uchOldIrql);
193 pState->uchOldIrql = 255;
194}
195
196
197RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
198{
199 Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
200
201 KIRQL CurIrql = KeGetCurrentIrql();
202 return CurIrql > PASSIVE_LEVEL; /** @todo Is there a more correct way? */
203}
204
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