VirtualBox

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

Last change on this file since 69111 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • 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 69111 2017-10-17 14:26:02Z 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
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); RT_NOREF1(hThread);
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); RT_NOREF1(hThread);
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 else
143 fPending = false;
144
145 /* Check DpcQueueDepth. */
146 if ( !fPending
147 && offDpcQueueDepth)
148 {
149 uint32_t volatile *pu32DpcQueueDepth = (uint32_t volatile *)(pbPrcb + offDpcQueueDepth);
150 fPending = *pu32DpcQueueDepth > 0;
151 }
152
153 ASMSetFlags(fSavedFlags);
154 return fPending;
155}
156
157
158RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
159{
160#if 0 /** @todo RTThreadPreemptIsPending isn't good enough on w7 and possibly elsewhere. */
161 /* RTThreadPreemptIsPending is only reliable if we've got both offsets and size. */
162 return g_offrtNtPbQuantumEnd != 0
163 && g_cbrtNtPbQuantumEnd != 0
164 && g_offrtNtPbDpcQueueDepth != 0;
165#else
166 return false;
167#endif
168}
169
170
171RTDECL(bool) RTThreadPreemptIsPossible(void)
172{
173 /* yes, kernel preemption is possible. */
174 return true;
175}
176
177
178RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
179{
180 AssertPtr(pState);
181 Assert(pState->uchOldIrql == 255);
182 Assert(KeGetCurrentIrql() <= DISPATCH_LEVEL);
183
184 KeRaiseIrql(DISPATCH_LEVEL, &pState->uchOldIrql);
185 RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
186}
187
188
189RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
190{
191 AssertPtr(pState);
192
193 RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
194 KeLowerIrql(pState->uchOldIrql);
195 pState->uchOldIrql = 255;
196}
197
198
199RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
200{
201 Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
202
203 KIRQL CurIrql = KeGetCurrentIrql();
204 return CurIrql > PASSIVE_LEVEL; /** @todo Is there a more correct way? */
205}
206
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