VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/TMAllCpu.cpp@ 47681

Last change on this file since 47681 was 44933, checked in by vboxsync, 12 years ago

TM: Do not return the same value, either.

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