VirtualBox

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

最後變更 在這個檔案從53442是 53441,由 vboxsync 提交於 10 年 前

VMM/TM: Rename TMMODE to TMTSCMODE and the corresponding enum and CFGM key. Fixed an endian issue in the assembly code.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 17.0 KB
 
1/* $Id: TMAllCpu.cpp 53441 2014-12-04 11:06:26Z vboxsync $ */
2/** @file
3 * TM - Timeout Manager, CPU Time, All Contexts.
4 */
5
6/*
7 * Copyright (C) 2006-2014 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 */
40DECLINLINE(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 */
61int 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 */
88int 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 */
130int 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 */
151int 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 */
181DECLINLINE(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 * Can be NULL.
216 * @param pfParavirtTsc Where to store whether paravirt. TSC can be used or
217 * not.
218 * @thread EMT(pVCpu).
219 */
220VMM_INT_DECL(bool) TMCpuTickCanUseRealTSC(PVMCPU pVCpu, uint64_t *poffRealTSC, bool *pfParavirtTsc)
221{
222 PVM pVM = pVCpu->CTX_SUFF(pVM);
223 bool fParavirtTsc = false;
224
225 /*
226 * We require:
227 * 1. Use of a paravirtualized TSC is enabled by the guest.
228 * (OR)
229 * 1. A fixed TSC, this is checked at init time.
230 * 2. That the TSC is ticking (we shouldn't be here if it isn't)
231 * 3. Either that we're using the real TSC as time source or
232 * a) we don't have any lag to catch up, and
233 * b) the virtual sync clock hasn't been halted by an expired timer, and
234 * c) we're not using warp drive (accelerated virtual guest time).
235 */
236 *pfParavirtTsc = GIMIsParavirtTscEnabled(pVM);
237 if ( pVM->tm.s.enmTSCMode == TMTSCMODE_DYNAMIC
238 && RT_LIKELY(pVCpu->tm.s.fTSCTicking)
239 && ( pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET
240 || ( !pVM->tm.s.fVirtualSyncCatchUp
241 && RT_LIKELY(pVM->tm.s.fVirtualSyncTicking)
242 && !pVM->tm.s.fVirtualWarpDrive)))
243 {
244 if (pVM->tm.s.enmTSCMode != TMTSCMODE_REAL_TSC_OFFSET)
245 {
246 /* The source is the timer synchronous virtual clock. */
247 if (poffRealTSC)
248 {
249 uint64_t u64Now = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
250 - pVCpu->tm.s.offTSCRawSrc;
251 /** @todo When we start collecting statistics on how much time we spend executing
252 * guest code before exiting, we should check this against the next virtual sync
253 * timer timeout. If it's lower than the avg. length, we should trap rdtsc to increase
254 * the chance that we'll get interrupted right after the timer expired. */
255 *poffRealTSC = u64Now - ASMReadTSC();
256 }
257 }
258 else if (poffRealTSC)
259 {
260 /* The source is the real TSC. */
261 *poffRealTSC = 0 - pVCpu->tm.s.offTSCRawSrc;
262 }
263 /** @todo count this? */
264 return true;
265 }
266
267#ifdef VBOX_WITH_STATISTICS
268 tmCpuTickRecordOffsettedTscRefusal(pVM, pVCpu);
269#endif
270 return false;
271}
272
273
274/**
275 * Calculates the number of host CPU ticks till the next virtual sync deadline.
276 *
277 * @note To save work, this function will not bother calculating the accurate
278 * tick count for deadlines that are more than a second ahead.
279 *
280 * @returns The number of host cpu ticks to the next deadline. Max one second.
281 * @param cNsToDeadline The number of nano seconds to the next virtual
282 * sync deadline.
283 */
284DECLINLINE(uint64_t) tmCpuCalcTicksToDeadline(uint64_t cNsToDeadline)
285{
286 AssertCompile(TMCLOCK_FREQ_VIRTUAL <= _4G);
287 if (RT_UNLIKELY(cNsToDeadline >= TMCLOCK_FREQ_VIRTUAL))
288 return SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
289 uint64_t cTicks = ASMMultU64ByU32DivByU32(SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage),
290 cNsToDeadline,
291 TMCLOCK_FREQ_VIRTUAL);
292 if (cTicks > 4000)
293 cTicks -= 4000; /* fudge to account for overhead */
294 else
295 cTicks >>= 1;
296 return cTicks;
297}
298
299
300/**
301 * Gets the next deadline in host CPU clock ticks and the TSC offset if we can
302 * use the raw TSC.
303 *
304 * @returns The number of host CPU clock ticks to the next timer deadline.
305 * @param pVCpu The current CPU.
306 * @param pfParavirtTsc Where to store whether paravirt. TSC can be used or
307 * not.
308 * @param poffRealTSC The offset against the TSC of the current CPU.
309 *
310 * @thread EMT(pVCpu).
311 * @remarks Superset of TMCpuTickCanUseRealTSC().
312 */
313VMM_INT_DECL(uint64_t) TMCpuTickGetDeadlineAndTscOffset(PVMCPU pVCpu, bool *pfOffsettedTsc, bool *pfParavirtTsc,
314 uint64_t *poffRealTSC)
315{
316 PVM pVM = pVCpu->CTX_SUFF(pVM);
317 uint64_t cTicksToDeadline;
318
319 /*
320 * We require:
321 * 1. Use of a paravirtualized TSC is enabled by the guest.
322 * (OR)
323 * 1. A fixed TSC, this is checked at init time.
324 * 2. That the TSC is ticking (we shouldn't be here if it isn't)
325 * 3. Either that we're using the real TSC as time source or
326 * a) we don't have any lag to catch up, and
327 * b) the virtual sync clock hasn't been halted by an expired timer, and
328 * c) we're not using warp drive (accelerated virtual guest time).
329 */
330 *pfParavirtTsc = GIMIsParavirtTscEnabled(pVM);
331 if ( pVM->tm.s.enmTSCMode == TMTSCMODE_DYNAMIC
332 && RT_LIKELY(pVCpu->tm.s.fTSCTicking)
333 && ( pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET
334 || ( !pVM->tm.s.fVirtualSyncCatchUp
335 && RT_LIKELY(pVM->tm.s.fVirtualSyncTicking)
336 && !pVM->tm.s.fVirtualWarpDrive)))
337 {
338 *pfOffsettedTsc = true;
339 if (pVM->tm.s.enmTSCMode != TMTSCMODE_REAL_TSC_OFFSET)
340 {
341 /* The source is the timer synchronous virtual clock. */
342 uint64_t cNsToDeadline;
343 uint64_t u64NowVirtSync = TMVirtualSyncGetWithDeadlineNoCheck(pVM, &cNsToDeadline);
344 uint64_t u64Now = u64NowVirtSync != TMCLOCK_FREQ_VIRTUAL /* what's the use of this? */
345 ? ASMMultU64ByU32DivByU32(u64NowVirtSync, pVM->tm.s.cTSCTicksPerSecond, TMCLOCK_FREQ_VIRTUAL)
346 : u64NowVirtSync;
347 u64Now -= pVCpu->tm.s.offTSCRawSrc;
348 *poffRealTSC = u64Now - ASMReadTSC();
349 cTicksToDeadline = tmCpuCalcTicksToDeadline(cNsToDeadline);
350 }
351 else
352 {
353 /* The source is the real TSC. */
354 *poffRealTSC = 0 - pVCpu->tm.s.offTSCRawSrc;
355 cTicksToDeadline = tmCpuCalcTicksToDeadline(TMVirtualSyncGetNsToDeadline(pVM));
356 }
357 }
358 else
359 {
360#ifdef VBOX_WITH_STATISTICS
361 tmCpuTickRecordOffsettedTscRefusal(pVM, pVCpu);
362#endif
363 *pfOffsettedTsc = false;
364 *poffRealTSC = 0;
365 cTicksToDeadline = tmCpuCalcTicksToDeadline(TMVirtualSyncGetNsToDeadline(pVM));
366 }
367
368 return cTicksToDeadline;
369}
370
371
372/**
373 * Read the current CPU timestamp counter.
374 *
375 * @returns Gets the CPU tsc.
376 * @param pVCpu Pointer to the VMCPU.
377 */
378DECLINLINE(uint64_t) tmCpuTickGetInternal(PVMCPU pVCpu, bool fCheckTimers)
379{
380 uint64_t u64;
381
382 if (RT_LIKELY(pVCpu->tm.s.fTSCTicking))
383 {
384 PVM pVM = pVCpu->CTX_SUFF(pVM);
385 if (pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET)
386 u64 = ASMReadTSC();
387 else
388 u64 = tmCpuTickGetRawVirtual(pVM, fCheckTimers);
389 u64 -= pVCpu->tm.s.offTSCRawSrc;
390
391 /* Always return a value higher than what the guest has already seen. */
392 if (RT_LIKELY(u64 > pVCpu->tm.s.u64TSCLastSeen))
393 pVCpu->tm.s.u64TSCLastSeen = u64;
394 else
395 {
396 STAM_COUNTER_INC(&pVM->tm.s.StatTSCUnderflow);
397 pVCpu->tm.s.u64TSCLastSeen += 64; /* @todo choose a good increment here */
398 u64 = pVCpu->tm.s.u64TSCLastSeen;
399 }
400 }
401 else
402 u64 = pVCpu->tm.s.u64TSC;
403 return u64;
404}
405
406
407/**
408 * Read the current CPU timestamp counter.
409 *
410 * @returns Gets the CPU tsc.
411 * @param pVCpu Pointer to the VMCPU.
412 */
413VMMDECL(uint64_t) TMCpuTickGet(PVMCPU pVCpu)
414{
415 return tmCpuTickGetInternal(pVCpu, true /* fCheckTimers */);
416}
417
418
419/**
420 * Read the current CPU timestamp counter, don't check for expired timers.
421 *
422 * @returns Gets the CPU tsc.
423 * @param pVCpu Pointer to the VMCPU.
424 */
425VMM_INT_DECL(uint64_t) TMCpuTickGetNoCheck(PVMCPU pVCpu)
426{
427 return tmCpuTickGetInternal(pVCpu, false /* fCheckTimers */);
428}
429
430
431/**
432 * Sets the current CPU timestamp counter.
433 *
434 * @returns VBox status code.
435 * @param pVM Pointer to the VM.
436 * @param pVCpu Pointer to the VMCPU.
437 * @param u64Tick The new timestamp value.
438 *
439 * @thread EMT which TSC is to be set.
440 */
441VMM_INT_DECL(int) TMCpuTickSet(PVM pVM, PVMCPU pVCpu, uint64_t u64Tick)
442{
443 VMCPU_ASSERT_EMT(pVCpu);
444 STAM_COUNTER_INC(&pVM->tm.s.StatTSCSet);
445
446 /*
447 * This is easier to do when the TSC is paused since resume will
448 * do all the calculations for us. Actually, we don't need to
449 * call tmCpuTickPause here since we overwrite u64TSC anyway.
450 */
451 bool fTSCTicking = pVCpu->tm.s.fTSCTicking;
452 pVCpu->tm.s.fTSCTicking = false;
453 pVCpu->tm.s.u64TSC = u64Tick;
454 pVCpu->tm.s.u64TSCLastSeen = u64Tick;
455 if (fTSCTicking)
456 tmCpuTickResume(pVM, pVCpu);
457 /** @todo Try help synchronizing it better among the virtual CPUs? */
458
459 return VINF_SUCCESS;
460}
461
462/**
463 * Sets the last seen CPU timestamp counter.
464 *
465 * @returns VBox status code.
466 * @param pVCpu Pointer to the VMCPU.
467 * @param u64LastSeenTick The last seen timestamp value.
468 *
469 * @thread EMT which TSC is to be set.
470 */
471VMM_INT_DECL(int) TMCpuTickSetLastSeen(PVMCPU pVCpu, uint64_t u64LastSeenTick)
472{
473 VMCPU_ASSERT_EMT(pVCpu);
474
475 LogFlow(("TMCpuTickSetLastSeen %RX64\n", u64LastSeenTick));
476 if (pVCpu->tm.s.u64TSCLastSeen < u64LastSeenTick)
477 pVCpu->tm.s.u64TSCLastSeen = u64LastSeenTick;
478 return VINF_SUCCESS;
479}
480
481/**
482 * Gets the last seen CPU timestamp counter of the guest.
483 *
484 * @returns the last seen TSC.
485 * @param pVCpu Pointer to the VMCPU.
486 *
487 * @thread EMT(pVCpu).
488 */
489VMM_INT_DECL(uint64_t) TMCpuTickGetLastSeen(PVMCPU pVCpu)
490{
491 VMCPU_ASSERT_EMT(pVCpu);
492
493 return pVCpu->tm.s.u64TSCLastSeen;
494}
495
496
497/**
498 * Get the timestamp frequency.
499 *
500 * @returns Number of ticks per second.
501 * @param pVM The VM.
502 */
503VMMDECL(uint64_t) TMCpuTicksPerSecond(PVM pVM)
504{
505 /** @todo revisit this, not sure why we need to get the rate from GIP for
506 * real-tsc-offset. */
507 if (pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET)
508 {
509 uint64_t cTSCTicksPerSecond = SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
510 if (RT_LIKELY(cTSCTicksPerSecond != ~(uint64_t)0))
511 return cTSCTicksPerSecond;
512 }
513 return pVM->tm.s.cTSCTicksPerSecond;
514}
515
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette