VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/SUPLibAll.cpp@ 54874

Last change on this file since 54874 was 54335, checked in by vboxsync, 10 years ago

space.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.5 KB
Line 
1/* $Id: SUPLibAll.cpp 54335 2015-02-20 15:48:26Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - All Contexts Code.
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* Header Files *
29*******************************************************************************/
30#include <VBox/sup.h>
31#ifdef IN_RC
32# include <VBox/vmm/vm.h>
33# include <VBox/vmm/vmm.h>
34#endif
35#ifdef IN_RING0
36# include <iprt/mp.h>
37#endif
38#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
39# include <iprt/asm-amd64-x86.h>
40#endif
41
42
43#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
44
45/**
46 * The slow case for SUPReadTsc where we need to apply deltas.
47 *
48 * Must only be called when deltas are applicable, so please do not call it
49 * directly.
50 *
51 * @returns TSC with delta applied.
52 * @param pGip Pointer to the GIP.
53 *
54 * @remarks May be called with interrupts disabled in ring-0! This is why the
55 * ring-0 code doesn't attempt to figure the delta.
56 *
57 * @internal
58 */
59SUPDECL(uint64_t) SUPReadTscWithDelta(PSUPGLOBALINFOPAGE pGip)
60{
61 uint64_t uTsc;
62 uint16_t iGipCpu;
63 AssertCompile(RT_IS_POWER_OF_TWO(RTCPUSET_MAX_CPUS));
64 AssertCompile(RT_ELEMENTS(pGip->aiCpuFromCpuSetIdx) >= RTCPUSET_MAX_CPUS);
65 Assert(pGip->enmUseTscDelta > SUPGIPUSETSCDELTA_PRACTICALLY_ZERO);
66
67 /*
68 * Read the TSC and get the corresponding aCPUs index.
69 */
70#ifdef IN_RING3
71 if (pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS)
72 {
73 /* RDTSCP gives us all we need, no loops/cli. */
74 uint32_t iCpuSet;
75 uTsc = ASMReadTscWithAux(&iCpuSet);
76 iCpuSet &= RTCPUSET_MAX_CPUS - 1;
77 iGipCpu = pGip->aiCpuFromCpuSetIdx[iCpuSet];
78 }
79 else if (pGip->fGetGipCpu & SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS)
80 {
81 /* Storing the IDTR is normally very quick, but we need to loop. */
82 uint32_t cTries = 0;
83 for (;;)
84 {
85 uint16_t cbLim = ASMGetIdtrLimit();
86 uTsc = ASMReadTSC();
87 if (RT_LIKELY(ASMGetIdtrLimit() == cbLim))
88 {
89 uint16_t iCpuSet = cbLim - 256 * (ARCH_BITS == 64 ? 16 : 8);
90 iCpuSet &= RTCPUSET_MAX_CPUS - 1;
91 iGipCpu = pGip->aiCpuFromCpuSetIdx[iCpuSet];
92 break;
93 }
94 if (cTries >= 16)
95 {
96 iGipCpu = UINT16_MAX;
97 break;
98 }
99 cTries++;
100 }
101 }
102 else
103 {
104 /* Get APIC ID via the slow CPUID instruction, requires looping. */
105 uint32_t cTries = 0;
106 for (;;)
107 {
108 uint8_t idApic = ASMGetApicId();
109 uTsc = ASMReadTSC();
110 if (RT_LIKELY(ASMGetApicId() == idApic))
111 {
112 iGipCpu = pGip->aiCpuFromApicId[idApic];
113 break;
114 }
115 if (cTries >= 16)
116 {
117 iGipCpu = UINT16_MAX;
118 break;
119 }
120 cTries++;
121 }
122 }
123#elif defined(IN_RING0)
124 /* Ring-0: Use use RTMpCpuId(), no loops. */
125 RTCCUINTREG uFlags = ASMIntDisableFlags();
126 int iCpuSet = RTMpCpuIdToSetIndex(RTMpCpuId());
127 if (RT_LIKELY((unsigned)iCpuSet < RT_ELEMENTS(pGip->aiCpuFromCpuSetIdx)))
128 iGipCpu = pGip->aiCpuFromCpuSetIdx[iCpuSet];
129 else
130 iGipCpu = UINT16_MAX;
131 uTsc = ASMReadTSC();
132 ASMSetFlags(uFlags);
133
134# elif defined(IN_RC)
135 /* Raw-mode context: We can get the host CPU set index via VMCPU, no loops. */
136 RTCCUINTREG uFlags = ASMIntDisableFlags(); /* Are already disable, but play safe. */
137 uint32_t iCpuSet = VMMGetCpu(&g_VM)->iHostCpuSet;
138 if (RT_LIKELY(iCpuSet < RT_ELEMENTS(pGip->aiCpuFromCpuSetIdx)))
139 iGipCpu = pGip->aiCpuFromCpuSetIdx[iCpuSet];
140 else
141 iGipCpu = UINT16_MAX;
142 uTsc = ASMReadTSC();
143 ASMSetFlags(uFlags);
144#else
145# error "IN_RING3, IN_RC or IN_RING0 must be defined!"
146#endif
147
148 /*
149 * If the delta is valid, apply it.
150 */
151 if (RT_LIKELY(iGipCpu < pGip->cCpus))
152 {
153 int64_t iTscDelta = pGip->aCPUs[iGipCpu].i64TSCDelta;
154 if (RT_LIKELY(iTscDelta != INT64_MAX))
155 return uTsc + iTscDelta;
156
157# ifdef IN_RING3
158 /*
159 * The delta needs calculating, call supdrv to get the TSC.
160 */
161 int rc = SUPR3ReadTsc(&uTsc, NULL);
162 if (RT_SUCCESS(rc))
163 return uTsc;
164 AssertMsgFailed(("SUPR3ReadTsc -> %Rrc\n", rc));
165 uTsc = ASMReadTSC();
166# endif /* IN_RING3 */
167 }
168
169 /*
170 * This shouldn't happen, especially not in ring-3 and raw-mode context.
171 * But if it does, return something that's half useful.
172 */
173 AssertMsgFailed(("iGipCpu=%d (%#x) cCpus=%d fGetGipCpu=%#x\n", iGipCpu, iGipCpu, pGip->cCpus, pGip->fGetGipCpu));
174 return uTsc;
175}
176
177
178/**
179 * Internal worker for getting the GIP CPU array index for the calling CPU.
180 *
181 * @returns Index into SUPGLOBALINFOPAGE::aCPUs or UINT16_MAX.
182 * @param pGip The GIP.
183 */
184DECLINLINE(uint16_t) supGetGipCpuIndex(PSUPGLOBALINFOPAGE pGip)
185{
186 uint16_t iGipCpu;
187#ifdef IN_RING3
188 if (pGip->fGetGipCpu & SUPGIPGETCPU_IDTR_LIMIT_MASK_MAX_SET_CPUS)
189 {
190 /* Storing the IDTR is normally very fast. */
191 uint16_t cbLim = ASMGetIdtrLimit();
192 uint16_t iCpuSet = cbLim - 256 * (ARCH_BITS == 64 ? 16 : 8);
193 iCpuSet &= RTCPUSET_MAX_CPUS - 1;
194 iGipCpu = pGip->aiCpuFromCpuSetIdx[iCpuSet];
195 }
196 else if (pGip->fGetGipCpu & SUPGIPGETCPU_RDTSCP_MASK_MAX_SET_CPUS)
197 {
198 /* RDTSCP gives us what need need and more. */
199 uint32_t iCpuSet;
200 ASMReadTscWithAux(&iCpuSet);
201 iCpuSet &= RTCPUSET_MAX_CPUS - 1;
202 iGipCpu = pGip->aiCpuFromCpuSetIdx[iCpuSet];
203 }
204 else
205 {
206 /* Get APIC ID via the slow CPUID instruction. */
207 uint8_t idApic = ASMGetApicId();
208 iGipCpu = pGip->aiCpuFromApicId[idApic];
209 }
210#elif defined(IN_RING0)
211 /* Ring-0: Use use RTMpCpuId() (disables cli to avoid host OS assertions about unsafe CPU number usage). */
212 RTCCUINTREG uFlags = ASMIntDisableFlags();
213 int iCpuSet = RTMpCpuIdToSetIndex(RTMpCpuId());
214 if (RT_LIKELY((unsigned)iCpuSet < RT_ELEMENTS(pGip->aiCpuFromCpuSetIdx)))
215 iGipCpu = pGip->aiCpuFromCpuSetIdx[iCpuSet];
216 else
217 iGipCpu = UINT16_MAX;
218 ASMSetFlags(uFlags);
219
220# elif defined(IN_RC)
221 /* Raw-mode context: We can get the host CPU set index via VMCPU. */
222 uint32_t iCpuSet = VMMGetCpu(&g_VM)->iHostCpuSet;
223 if (RT_LIKELY(iCpuSet < RT_ELEMENTS(pGip->aiCpuFromCpuSetIdx)))
224 iGipCpu = pGip->aiCpuFromCpuSetIdx[iCpuSet];
225 else
226 iGipCpu = UINT16_MAX;
227#else
228# error "IN_RING3, IN_RC or IN_RING0 must be defined!"
229#endif
230 return iGipCpu;
231}
232
233
234/**
235 * Slow path in SUPGetTscDelta, don't call directly.
236 *
237 * @returns See SUPGetTscDelta.
238 * @param pGip The GIP.
239 * @internal
240 */
241SUPDECL(uint64_t) SUPGetTscDeltaSlow(PSUPGLOBALINFOPAGE pGip)
242{
243 uint16_t iGipCpu = supGetGipCpuIndex(pGip);
244 if (RT_LIKELY(iGipCpu < pGip->cCpus))
245 {
246 int64_t iTscDelta = pGip->aCPUs[iGipCpu].i64TSCDelta;
247 if (iTscDelta != INT64_MAX)
248 return iTscDelta;
249 }
250 AssertFailed();
251 return 0;
252}
253
254
255/**
256 * Slow path in SUPGetCpuHzFromGip, don't call directly.
257 *
258 * @returns See SUPGetCpuHzFromGip.
259 * @param pGip The GIP.
260 * @internal
261 */
262SUPDECL(uint64_t) SUPGetCpuHzFromGipForAsyncMode(PSUPGLOBALINFOPAGE pGip)
263{
264 uint16_t iGipCpu = supGetGipCpuIndex(pGip);
265 if (RT_LIKELY(iGipCpu < pGip->cCpus))
266 return pGip->aCPUs[iGipCpu].u64CpuHz;
267 AssertFailed();
268 return pGip->u64CpuHz;
269}
270
271
272#endif /* RT_ARCH_AMD64 || RT_ARCH_X86 */
273
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