VirtualBox

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

Last change on this file since 32419 was 32419, checked in by vboxsync, 14 years ago

TM,HWVMXR0: Use the preemption timer to make sure we stop executing guest code in time for the next TMCLOCK_VIRTUAL_SYNC timer deadline.

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