1 | /* $Id: TMAllCpu.cpp 54065 2015-02-03 10:45:39Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * TM - Timeout Manager, CPU Time, All Contexts.
|
---|
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.alldomusa.eu.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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_TM
|
---|
23 | #include <VBox/vmm/tm.h>
|
---|
24 | #include <iprt/asm-amd64-x86.h> /* for SUPGetCpuHzFromGIP */
|
---|
25 | #include "TMInternal.h"
|
---|
26 | #include <VBox/vmm/vm.h>
|
---|
27 | #include <VBox/vmm/gim.h>
|
---|
28 | #include <VBox/sup.h>
|
---|
29 |
|
---|
30 | #include <VBox/param.h>
|
---|
31 | #include <VBox/err.h>
|
---|
32 | #include <iprt/asm-math.h>
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <VBox/log.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Gets the raw cpu tick from current virtual time.
|
---|
39 | */
|
---|
40 | DECLINLINE(uint64_t) tmCpuTickGetRawVirtual(PVM pVM, bool fCheckTimers)
|
---|
41 | {
|
---|
42 | uint64_t u64;
|
---|
43 | if (fCheckTimers)
|
---|
44 | u64 = TMVirtualSyncGet(pVM);
|
---|
45 | else
|
---|
46 | u64 = TMVirtualSyncGetNoCheck(pVM);
|
---|
47 | if (u64 != TMCLOCK_FREQ_VIRTUAL) /* what's the use of this test, document! */
|
---|
48 | u64 = ASMMultU64ByU32DivByU32(u64, pVM->tm.s.cTSCTicksPerSecond, TMCLOCK_FREQ_VIRTUAL);
|
---|
49 | return u64;
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Resumes the CPU timestamp counter ticking.
|
---|
55 | *
|
---|
56 | * @returns VBox status code.
|
---|
57 | * @param pVM Pointer to the VM.
|
---|
58 | * @param pVCpu Pointer to the VMCPU.
|
---|
59 | * @internal
|
---|
60 | */
|
---|
61 | int tmCpuTickResume(PVM pVM, PVMCPU pVCpu)
|
---|
62 | {
|
---|
63 | if (!pVCpu->tm.s.fTSCTicking)
|
---|
64 | {
|
---|
65 | pVCpu->tm.s.fTSCTicking = true;
|
---|
66 |
|
---|
67 | /** @todo Test that pausing and resuming doesn't cause lag! (I.e. that we're
|
---|
68 | * unpaused before the virtual time and stopped after it. */
|
---|
69 | if (pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET)
|
---|
70 | pVCpu->tm.s.offTSCRawSrc = ASMReadTSC() - pVCpu->tm.s.u64TSC;
|
---|
71 | else
|
---|
72 | pVCpu->tm.s.offTSCRawSrc = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
|
---|
73 | - pVCpu->tm.s.u64TSC;
|
---|
74 | return VINF_SUCCESS;
|
---|
75 | }
|
---|
76 | AssertFailed();
|
---|
77 | return VERR_TM_TSC_ALREADY_TICKING;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Resumes the CPU timestamp counter ticking.
|
---|
83 | *
|
---|
84 | * @returns VINF_SUCCESS or VERR_TM_VIRTUAL_TICKING_IPE (asserted).
|
---|
85 | * @param pVM Pointer to the VM.
|
---|
86 | * @param pVCpu Pointer to the VCPU.
|
---|
87 | */
|
---|
88 | int tmCpuTickResumeLocked(PVM pVM, PVMCPU pVCpu)
|
---|
89 | {
|
---|
90 | if (!pVCpu->tm.s.fTSCTicking)
|
---|
91 | {
|
---|
92 | /* TSC must be ticking before calling tmCpuTickGetRawVirtual()! */
|
---|
93 | pVCpu->tm.s.fTSCTicking = true;
|
---|
94 | uint32_t c = ASMAtomicIncU32(&pVM->tm.s.cTSCsTicking);
|
---|
95 | AssertMsgReturn(c <= pVM->cCpus, ("%u vs %u\n", c, pVM->cCpus), VERR_TM_VIRTUAL_TICKING_IPE);
|
---|
96 | if (c == 1)
|
---|
97 | {
|
---|
98 | /* The first VCPU to resume. */
|
---|
99 | uint64_t offTSCRawSrcOld = pVCpu->tm.s.offTSCRawSrc;
|
---|
100 |
|
---|
101 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCResume);
|
---|
102 |
|
---|
103 | /* When resuming, use the TSC value of the last stopped VCPU to avoid the TSC going back. */
|
---|
104 | if (pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET)
|
---|
105 | pVCpu->tm.s.offTSCRawSrc = ASMReadTSC() - pVM->tm.s.u64LastPausedTSC;
|
---|
106 | else
|
---|
107 | pVCpu->tm.s.offTSCRawSrc = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
|
---|
108 | - pVM->tm.s.u64LastPausedTSC;
|
---|
109 |
|
---|
110 | /* Calculate the offset for other VCPUs to use. */
|
---|
111 | pVM->tm.s.offTSCPause = pVCpu->tm.s.offTSCRawSrc - offTSCRawSrcOld;
|
---|
112 | }
|
---|
113 | else
|
---|
114 | {
|
---|
115 | /* All other VCPUs (if any). */
|
---|
116 | pVCpu->tm.s.offTSCRawSrc += pVM->tm.s.offTSCPause;
|
---|
117 | }
|
---|
118 | }
|
---|
119 | return VINF_SUCCESS;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Pauses the CPU timestamp counter ticking.
|
---|
125 | *
|
---|
126 | * @returns VBox status code.
|
---|
127 | * @param pVCpu Pointer to the VMCPU.
|
---|
128 | * @internal
|
---|
129 | */
|
---|
130 | int tmCpuTickPause(PVMCPU pVCpu)
|
---|
131 | {
|
---|
132 | if (pVCpu->tm.s.fTSCTicking)
|
---|
133 | {
|
---|
134 | pVCpu->tm.s.u64TSC = TMCpuTickGetNoCheck(pVCpu);
|
---|
135 | pVCpu->tm.s.fTSCTicking = false;
|
---|
136 | return VINF_SUCCESS;
|
---|
137 | }
|
---|
138 | AssertFailed();
|
---|
139 | return VERR_TM_TSC_ALREADY_PAUSED;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Pauses the CPU timestamp counter ticking.
|
---|
145 | *
|
---|
146 | * @returns VBox status code.
|
---|
147 | * @param pVM Pointer to the VM.
|
---|
148 | * @param pVCpu Pointer to the VMCPU.
|
---|
149 | * @internal
|
---|
150 | */
|
---|
151 | int tmCpuTickPauseLocked(PVM pVM, PVMCPU pVCpu)
|
---|
152 | {
|
---|
153 | if (pVCpu->tm.s.fTSCTicking)
|
---|
154 | {
|
---|
155 | pVCpu->tm.s.u64TSC = TMCpuTickGetNoCheck(pVCpu);
|
---|
156 | pVCpu->tm.s.fTSCTicking = false;
|
---|
157 |
|
---|
158 | uint32_t c = ASMAtomicDecU32(&pVM->tm.s.cTSCsTicking);
|
---|
159 | AssertMsgReturn(c < pVM->cCpus, ("%u vs %u\n", c, pVM->cCpus), VERR_TM_VIRTUAL_TICKING_IPE);
|
---|
160 | if (c == 0)
|
---|
161 | {
|
---|
162 | /* When the last TSC stops, remember the value. */
|
---|
163 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCPause);
|
---|
164 | pVM->tm.s.u64LastPausedTSC = pVCpu->tm.s.u64TSC;
|
---|
165 | }
|
---|
166 | return VINF_SUCCESS;
|
---|
167 | }
|
---|
168 | AssertFailed();
|
---|
169 | return VERR_TM_TSC_ALREADY_PAUSED;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Record why we refused to use offsetted TSC.
|
---|
175 | *
|
---|
176 | * Used by TMCpuTickCanUseRealTSC() and TMCpuTickGetDeadlineAndTscOffset().
|
---|
177 | *
|
---|
178 | * @param pVM Pointer to the VM.
|
---|
179 | * @param pVCpu The current CPU.
|
---|
180 | */
|
---|
181 | DECLINLINE(void) tmCpuTickRecordOffsettedTscRefusal(PVM pVM, PVMCPU pVCpu)
|
---|
182 | {
|
---|
183 | /* Sample the reason for refusing. */
|
---|
184 | if (pVM->tm.s.enmTSCMode != TMTSCMODE_DYNAMIC)
|
---|
185 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCNotFixed);
|
---|
186 | else if (!pVCpu->tm.s.fTSCTicking)
|
---|
187 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCNotTicking);
|
---|
188 | else if (pVM->tm.s.enmTSCMode != TMTSCMODE_REAL_TSC_OFFSET)
|
---|
189 | {
|
---|
190 | if (pVM->tm.s.fVirtualSyncCatchUp)
|
---|
191 | {
|
---|
192 | if (pVM->tm.s.u32VirtualSyncCatchUpPercentage <= 10)
|
---|
193 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupLE010);
|
---|
194 | else if (pVM->tm.s.u32VirtualSyncCatchUpPercentage <= 25)
|
---|
195 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupLE025);
|
---|
196 | else if (pVM->tm.s.u32VirtualSyncCatchUpPercentage <= 100)
|
---|
197 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupLE100);
|
---|
198 | else
|
---|
199 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupOther);
|
---|
200 | }
|
---|
201 | else if (!pVM->tm.s.fVirtualSyncTicking)
|
---|
202 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCSyncNotTicking);
|
---|
203 | else if (pVM->tm.s.fVirtualWarpDrive)
|
---|
204 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCWarp);
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Checks if AMD-V / VT-x can use an offsetted hardware TSC or not.
|
---|
211 | *
|
---|
212 | * @returns true/false accordingly.
|
---|
213 | * @param pVCpu Pointer to the VMCPU.
|
---|
214 | * @param poffRealTSC The offset against the TSC of the current CPU.
|
---|
215 | * @param pfParavirtTsc Where to store whether paravirt. TSC is enabled.
|
---|
216 | *
|
---|
217 | * @thread EMT(pVCpu).
|
---|
218 | * @see TMCpuTickGetDeadlineAndTscOffset().
|
---|
219 | */
|
---|
220 | VMM_INT_DECL(bool) TMCpuTickCanUseRealTSC(PVMCPU pVCpu, uint64_t *poffRealTSC, bool *pfParavirtTsc)
|
---|
221 | {
|
---|
222 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
223 | bool fOffsettedTsc = false;
|
---|
224 |
|
---|
225 | /*
|
---|
226 | * We require:
|
---|
227 | * 1. A fixed TSC, this is checked at init time.
|
---|
228 | * 2. That the TSC is ticking (we shouldn't be here if it isn't)
|
---|
229 | * 3. Either that we're using the real TSC as time source or
|
---|
230 | * a) we don't have any lag to catch up, and
|
---|
231 | * b) the virtual sync clock hasn't been halted by an expired timer, and
|
---|
232 | * c) we're not using warp drive (accelerated virtual guest time).
|
---|
233 | */
|
---|
234 | Assert(pVCpu->tm.s.fTSCTicking);
|
---|
235 | *pfParavirtTsc = pVM->tm.s.fParavirtTscEnabled;
|
---|
236 |
|
---|
237 | if (pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET)
|
---|
238 | {
|
---|
239 | /* The source is the real TSC. */
|
---|
240 | *poffRealTSC = 0 - pVCpu->tm.s.offTSCRawSrc;
|
---|
241 | return true; /** @todo count this? */
|
---|
242 | }
|
---|
243 |
|
---|
244 | if ( pVM->tm.s.enmTSCMode == TMTSCMODE_DYNAMIC
|
---|
245 | && !pVM->tm.s.fVirtualSyncCatchUp
|
---|
246 | && RT_LIKELY(pVM->tm.s.fVirtualSyncTicking)
|
---|
247 | && !pVM->tm.s.fVirtualWarpDrive)
|
---|
248 | {
|
---|
249 | /* The source is the timer synchronous virtual clock. */
|
---|
250 | uint64_t u64Now = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
|
---|
251 | - pVCpu->tm.s.offTSCRawSrc;
|
---|
252 | /** @todo When we start collecting statistics on how much time we spend executing
|
---|
253 | * guest code before exiting, we should check this against the next virtual sync
|
---|
254 | * timer timeout. If it's lower than the avg. length, we should trap rdtsc to increase
|
---|
255 | * the chance that we'll get interrupted right after the timer expired. */
|
---|
256 | uint64_t u64TSC = ASMReadTSC(); /** @todo should be replaced with SUPReadTSC() eventually. */
|
---|
257 | *poffRealTSC = u64Now - u64TSC;
|
---|
258 | fOffsettedTsc = u64Now >= pVCpu->tm.s.u64TSCLastSeen;
|
---|
259 | return true; /** @todo count this? */
|
---|
260 | }
|
---|
261 |
|
---|
262 | #ifdef VBOX_WITH_STATISTICS
|
---|
263 | tmCpuTickRecordOffsettedTscRefusal(pVM, pVCpu);
|
---|
264 | #endif
|
---|
265 | return false;
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Calculates the number of host CPU ticks till the next virtual sync deadline.
|
---|
271 | *
|
---|
272 | * @note To save work, this function will not bother calculating the accurate
|
---|
273 | * tick count for deadlines that are more than a second ahead.
|
---|
274 | *
|
---|
275 | * @returns The number of host cpu ticks to the next deadline. Max one second.
|
---|
276 | * @param cNsToDeadline The number of nano seconds to the next virtual
|
---|
277 | * sync deadline.
|
---|
278 | */
|
---|
279 | DECLINLINE(uint64_t) tmCpuCalcTicksToDeadline(uint64_t cNsToDeadline)
|
---|
280 | {
|
---|
281 | AssertCompile(TMCLOCK_FREQ_VIRTUAL <= _4G);
|
---|
282 | if (RT_UNLIKELY(cNsToDeadline >= TMCLOCK_FREQ_VIRTUAL))
|
---|
283 | return SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
|
---|
284 | uint64_t cTicks = ASMMultU64ByU32DivByU32(SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage),
|
---|
285 | cNsToDeadline,
|
---|
286 | TMCLOCK_FREQ_VIRTUAL);
|
---|
287 | if (cTicks > 4000)
|
---|
288 | cTicks -= 4000; /* fudge to account for overhead */
|
---|
289 | else
|
---|
290 | cTicks >>= 1;
|
---|
291 | return cTicks;
|
---|
292 | }
|
---|
293 |
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * Gets the next deadline in host CPU clock ticks and the TSC offset if we can
|
---|
297 | * use the raw TSC.
|
---|
298 | *
|
---|
299 | * @returns The number of host CPU clock ticks to the next timer deadline.
|
---|
300 | * @param pVCpu The current CPU.
|
---|
301 | * @param poffRealTSC The offset against the TSC of the current CPU.
|
---|
302 | * @param pfOffsettedTsc Where to store whether TSC offsetting can be used.
|
---|
303 | * @param pfParavirtTsc Where to store whether paravirt. TSC is enabled.
|
---|
304 | *
|
---|
305 | * @thread EMT(pVCpu).
|
---|
306 | * @see TMCpuTickCanUseRealTSC().
|
---|
307 | */
|
---|
308 | VMM_INT_DECL(uint64_t) TMCpuTickGetDeadlineAndTscOffset(PVMCPU pVCpu, uint64_t *poffRealTSC, bool *pfOffsettedTsc,
|
---|
309 | bool *pfParavirtTsc)
|
---|
310 | {
|
---|
311 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
312 | uint64_t cTicksToDeadline;
|
---|
313 |
|
---|
314 | /*
|
---|
315 | * We require:
|
---|
316 | * 1. A fixed TSC, this is checked at init time.
|
---|
317 | * 2. That the TSC is ticking (we shouldn't be here if it isn't)
|
---|
318 | * 3. Either that we're using the real TSC as time source or
|
---|
319 | * a) we don't have any lag to catch up, and
|
---|
320 | * b) the virtual sync clock hasn't been halted by an expired timer, and
|
---|
321 | * c) we're not using warp drive (accelerated virtual guest time).
|
---|
322 | */
|
---|
323 | Assert(pVCpu->tm.s.fTSCTicking);
|
---|
324 | *pfParavirtTsc = pVM->tm.s.fParavirtTscEnabled;
|
---|
325 |
|
---|
326 | if (pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET)
|
---|
327 | {
|
---|
328 | /* The source is the real TSC. */
|
---|
329 | *poffRealTSC = 0 - pVCpu->tm.s.offTSCRawSrc;
|
---|
330 | *pfOffsettedTsc = true;
|
---|
331 | cTicksToDeadline = tmCpuCalcTicksToDeadline(TMVirtualSyncGetNsToDeadline(pVM));
|
---|
332 | return cTicksToDeadline;
|
---|
333 | }
|
---|
334 |
|
---|
335 | if ( pVM->tm.s.enmTSCMode == TMTSCMODE_DYNAMIC
|
---|
336 | && !pVM->tm.s.fVirtualSyncCatchUp
|
---|
337 | && RT_LIKELY(pVM->tm.s.fVirtualSyncTicking)
|
---|
338 | && !pVM->tm.s.fVirtualWarpDrive)
|
---|
339 | {
|
---|
340 | /* The source is the timer synchronous virtual clock. */
|
---|
341 | uint64_t cNsToDeadline;
|
---|
342 | uint64_t u64NowVirtSync = TMVirtualSyncGetWithDeadlineNoCheck(pVM, &cNsToDeadline);
|
---|
343 | uint64_t u64Now = u64NowVirtSync != TMCLOCK_FREQ_VIRTUAL /* what's the use of this? */
|
---|
344 | ? ASMMultU64ByU32DivByU32(u64NowVirtSync, pVM->tm.s.cTSCTicksPerSecond, TMCLOCK_FREQ_VIRTUAL)
|
---|
345 | : u64NowVirtSync;
|
---|
346 | u64Now -= pVCpu->tm.s.offTSCRawSrc;
|
---|
347 | *poffRealTSC = u64Now - ASMReadTSC(); /** @todo replace with SUPReadTSC() eventually. */
|
---|
348 | *pfOffsettedTsc = u64Now >= pVCpu->tm.s.u64TSCLastSeen;
|
---|
349 | cTicksToDeadline = tmCpuCalcTicksToDeadline(cNsToDeadline);
|
---|
350 | return cTicksToDeadline;
|
---|
351 | }
|
---|
352 |
|
---|
353 | #ifdef VBOX_WITH_STATISTICS
|
---|
354 | tmCpuTickRecordOffsettedTscRefusal(pVM, pVCpu);
|
---|
355 | #endif
|
---|
356 | *pfOffsettedTsc = false;
|
---|
357 | *poffRealTSC = 0;
|
---|
358 | cTicksToDeadline = tmCpuCalcTicksToDeadline(TMVirtualSyncGetNsToDeadline(pVM));
|
---|
359 | return cTicksToDeadline;
|
---|
360 | }
|
---|
361 |
|
---|
362 |
|
---|
363 | /**
|
---|
364 | * Read the current CPU timestamp counter.
|
---|
365 | *
|
---|
366 | * @returns Gets the CPU tsc.
|
---|
367 | * @param pVCpu Pointer to the VMCPU.
|
---|
368 | */
|
---|
369 | DECLINLINE(uint64_t) tmCpuTickGetInternal(PVMCPU pVCpu, bool fCheckTimers)
|
---|
370 | {
|
---|
371 | uint64_t u64;
|
---|
372 |
|
---|
373 | if (RT_LIKELY(pVCpu->tm.s.fTSCTicking))
|
---|
374 | {
|
---|
375 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
376 | if (pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET)
|
---|
377 | u64 = ASMReadTSC();
|
---|
378 | else
|
---|
379 | u64 = tmCpuTickGetRawVirtual(pVM, fCheckTimers);
|
---|
380 | u64 -= pVCpu->tm.s.offTSCRawSrc;
|
---|
381 |
|
---|
382 | /* Always return a value higher than what the guest has already seen. */
|
---|
383 | if (RT_LIKELY(u64 > pVCpu->tm.s.u64TSCLastSeen))
|
---|
384 | pVCpu->tm.s.u64TSCLastSeen = u64;
|
---|
385 | else
|
---|
386 | {
|
---|
387 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCUnderflow);
|
---|
388 | pVCpu->tm.s.u64TSCLastSeen += 64; /** @todo choose a good increment here */
|
---|
389 | u64 = pVCpu->tm.s.u64TSCLastSeen;
|
---|
390 | }
|
---|
391 | }
|
---|
392 | else
|
---|
393 | u64 = pVCpu->tm.s.u64TSC;
|
---|
394 | return u64;
|
---|
395 | }
|
---|
396 |
|
---|
397 |
|
---|
398 | /**
|
---|
399 | * Read the current CPU timestamp counter.
|
---|
400 | *
|
---|
401 | * @returns Gets the CPU tsc.
|
---|
402 | * @param pVCpu Pointer to the VMCPU.
|
---|
403 | */
|
---|
404 | VMMDECL(uint64_t) TMCpuTickGet(PVMCPU pVCpu)
|
---|
405 | {
|
---|
406 | return tmCpuTickGetInternal(pVCpu, true /* fCheckTimers */);
|
---|
407 | }
|
---|
408 |
|
---|
409 |
|
---|
410 | /**
|
---|
411 | * Read the current CPU timestamp counter, don't check for expired timers.
|
---|
412 | *
|
---|
413 | * @returns Gets the CPU tsc.
|
---|
414 | * @param pVCpu Pointer to the VMCPU.
|
---|
415 | */
|
---|
416 | VMM_INT_DECL(uint64_t) TMCpuTickGetNoCheck(PVMCPU pVCpu)
|
---|
417 | {
|
---|
418 | return tmCpuTickGetInternal(pVCpu, false /* fCheckTimers */);
|
---|
419 | }
|
---|
420 |
|
---|
421 |
|
---|
422 | /**
|
---|
423 | * Sets the current CPU timestamp counter.
|
---|
424 | *
|
---|
425 | * @returns VBox status code.
|
---|
426 | * @param pVM Pointer to the VM.
|
---|
427 | * @param pVCpu Pointer to the VMCPU.
|
---|
428 | * @param u64Tick The new timestamp value.
|
---|
429 | *
|
---|
430 | * @thread EMT which TSC is to be set.
|
---|
431 | */
|
---|
432 | VMM_INT_DECL(int) TMCpuTickSet(PVM pVM, PVMCPU pVCpu, uint64_t u64Tick)
|
---|
433 | {
|
---|
434 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
435 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCSet);
|
---|
436 |
|
---|
437 | /*
|
---|
438 | * This is easier to do when the TSC is paused since resume will
|
---|
439 | * do all the calculations for us. Actually, we don't need to
|
---|
440 | * call tmCpuTickPause here since we overwrite u64TSC anyway.
|
---|
441 | */
|
---|
442 | bool fTSCTicking = pVCpu->tm.s.fTSCTicking;
|
---|
443 | pVCpu->tm.s.fTSCTicking = false;
|
---|
444 | pVCpu->tm.s.u64TSC = u64Tick;
|
---|
445 | pVCpu->tm.s.u64TSCLastSeen = u64Tick;
|
---|
446 | if (fTSCTicking)
|
---|
447 | tmCpuTickResume(pVM, pVCpu);
|
---|
448 | /** @todo Try help synchronizing it better among the virtual CPUs? */
|
---|
449 |
|
---|
450 | return VINF_SUCCESS;
|
---|
451 | }
|
---|
452 |
|
---|
453 | /**
|
---|
454 | * Sets the last seen CPU timestamp counter.
|
---|
455 | *
|
---|
456 | * @returns VBox status code.
|
---|
457 | * @param pVCpu Pointer to the VMCPU.
|
---|
458 | * @param u64LastSeenTick The last seen timestamp value.
|
---|
459 | *
|
---|
460 | * @thread EMT which TSC is to be set.
|
---|
461 | */
|
---|
462 | VMM_INT_DECL(int) TMCpuTickSetLastSeen(PVMCPU pVCpu, uint64_t u64LastSeenTick)
|
---|
463 | {
|
---|
464 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
465 |
|
---|
466 | LogFlow(("TMCpuTickSetLastSeen %RX64\n", u64LastSeenTick));
|
---|
467 | if (pVCpu->tm.s.u64TSCLastSeen < u64LastSeenTick)
|
---|
468 | pVCpu->tm.s.u64TSCLastSeen = u64LastSeenTick;
|
---|
469 | return VINF_SUCCESS;
|
---|
470 | }
|
---|
471 |
|
---|
472 | /**
|
---|
473 | * Gets the last seen CPU timestamp counter of the guest.
|
---|
474 | *
|
---|
475 | * @returns the last seen TSC.
|
---|
476 | * @param pVCpu Pointer to the VMCPU.
|
---|
477 | *
|
---|
478 | * @thread EMT(pVCpu).
|
---|
479 | */
|
---|
480 | VMM_INT_DECL(uint64_t) TMCpuTickGetLastSeen(PVMCPU pVCpu)
|
---|
481 | {
|
---|
482 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
483 |
|
---|
484 | return pVCpu->tm.s.u64TSCLastSeen;
|
---|
485 | }
|
---|
486 |
|
---|
487 |
|
---|
488 | /**
|
---|
489 | * Get the timestamp frequency.
|
---|
490 | *
|
---|
491 | * @returns Number of ticks per second.
|
---|
492 | * @param pVM The VM.
|
---|
493 | */
|
---|
494 | VMMDECL(uint64_t) TMCpuTicksPerSecond(PVM pVM)
|
---|
495 | {
|
---|
496 | if ( pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET
|
---|
497 | && g_pSUPGlobalInfoPage->u32Mode != SUPGIPMODE_INVARIANT_TSC)
|
---|
498 | {
|
---|
499 | uint64_t cTSCTicksPerSecond = SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
|
---|
500 | if (RT_LIKELY(cTSCTicksPerSecond != ~(uint64_t)0))
|
---|
501 | return cTSCTicksPerSecond;
|
---|
502 | }
|
---|
503 | return pVM->tm.s.cTSCTicksPerSecond;
|
---|
504 | }
|
---|
505 |
|
---|