VirtualBox

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

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

iprt/asm*.h: split out asm-math.h, don't include asm-*.h from asm.h, don't include asm.h from sup.h. Fixed a couple file headers.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.0 KB
Line 
1/* $Id: TMAllCpu.cpp 29250 2010-05-09 17:53:58Z 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 = TMVirtualSyncGetEx(pVM, fCheckTimers);
42 if (u64 != TMCLOCK_FREQ_VIRTUAL)
43 u64 = ASMMultU64ByU32DivByU32(u64, pVM->tm.s.cTSCTicksPerSecond, TMCLOCK_FREQ_VIRTUAL);
44 return u64;
45}
46
47
48/**
49 * Resumes the CPU timestamp counter ticking.
50 *
51 * @returns VBox status code.
52 * @param pVM The VM to operate on.
53 * @param pVCpu The VMCPU to operate on.
54 * @internal
55 */
56int tmCpuTickResume(PVM pVM, PVMCPU pVCpu)
57{
58 if (!pVCpu->tm.s.fTSCTicking)
59 {
60 pVCpu->tm.s.fTSCTicking = true;
61 if (pVM->tm.s.fTSCVirtualized)
62 {
63 /** @todo Test that pausing and resuming doesn't cause lag! (I.e. that we're
64 * unpaused before the virtual time and stopped after it. */
65 if (pVM->tm.s.fTSCUseRealTSC)
66 pVCpu->tm.s.offTSCRawSrc = ASMReadTSC() - pVCpu->tm.s.u64TSC;
67 else
68 pVCpu->tm.s.offTSCRawSrc = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
69 - pVCpu->tm.s.u64TSC;
70 }
71 return VINF_SUCCESS;
72 }
73 AssertFailed();
74 return VERR_INTERNAL_ERROR;
75}
76
77
78/**
79 * Pauses the CPU timestamp counter ticking.
80 *
81 * @returns VBox status code.
82 * @param pVM The VM to operate on.
83 * @param pVCpu The VMCPU to operate on.
84 * @internal
85 */
86int tmCpuTickPause(PVM pVM, PVMCPU pVCpu)
87{
88 if (pVCpu->tm.s.fTSCTicking)
89 {
90 pVCpu->tm.s.u64TSC = TMCpuTickGetNoCheck(pVCpu);
91 pVCpu->tm.s.fTSCTicking = false;
92 return VINF_SUCCESS;
93 }
94 AssertFailed();
95 return VERR_INTERNAL_ERROR;
96}
97
98
99/**
100 * Checks if AMD-V / VT-x can use an offsetted hardware TSC or not.
101 *
102 * @returns true/false accordingly.
103 * @param pVCpu The VMCPU to operate on.
104 * @param poffRealTSC The offset against the TSC of the current CPU.
105 * Can be NULL.
106 * @thread EMT.
107 */
108VMM_INT_DECL(bool) TMCpuTickCanUseRealTSC(PVMCPU pVCpu, uint64_t *poffRealTSC)
109{
110 PVM pVM = pVCpu->CTX_SUFF(pVM);
111
112 /*
113 * We require:
114 * 1. A fixed TSC, this is checked at init time.
115 * 2. That the TSC is ticking (we shouldn't be here if it isn't)
116 * 3. Either that we're using the real TSC as time source or
117 * a) we don't have any lag to catch up, and
118 * b) the virtual sync clock hasn't been halted by an expired timer, and
119 * c) we're not using warp drive (accelerated virtual guest time).
120 */
121 if ( pVM->tm.s.fMaybeUseOffsettedHostTSC
122 && RT_LIKELY(pVCpu->tm.s.fTSCTicking)
123 && ( pVM->tm.s.fTSCUseRealTSC
124 || ( !pVM->tm.s.fVirtualSyncCatchUp
125 && RT_LIKELY(pVM->tm.s.fVirtualSyncTicking)
126 && !pVM->tm.s.fVirtualWarpDrive))
127 )
128 {
129 if (!pVM->tm.s.fTSCUseRealTSC)
130 {
131 /* The source is the timer synchronous virtual clock. */
132 Assert(pVM->tm.s.fTSCVirtualized);
133
134 if (poffRealTSC)
135 {
136 uint64_t u64Now = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
137 - pVCpu->tm.s.offTSCRawSrc;
138 /** @todo When we start collecting statistics on how much time we spend executing
139 * guest code before exiting, we should check this against the next virtual sync
140 * timer timeout. If it's lower than the avg. length, we should trap rdtsc to increase
141 * the chance that we'll get interrupted right after the timer expired. */
142 *poffRealTSC = u64Now - ASMReadTSC();
143 }
144 }
145 else if (poffRealTSC)
146 {
147 /* The source is the real TSC. */
148 if (pVM->tm.s.fTSCVirtualized)
149 *poffRealTSC = pVCpu->tm.s.offTSCRawSrc;
150 else
151 *poffRealTSC = 0;
152 }
153 /** @todo count this? */
154 return true;
155 }
156
157#ifdef VBOX_WITH_STATISTICS
158 /* Sample the reason for refusing. */
159 if (!pVM->tm.s.fMaybeUseOffsettedHostTSC)
160 STAM_COUNTER_INC(&pVM->tm.s.StatTSCNotFixed);
161 else if (!pVCpu->tm.s.fTSCTicking)
162 STAM_COUNTER_INC(&pVM->tm.s.StatTSCNotTicking);
163 else if (!pVM->tm.s.fTSCUseRealTSC)
164 {
165 if (pVM->tm.s.fVirtualSyncCatchUp)
166 {
167 if (pVM->tm.s.u32VirtualSyncCatchUpPercentage <= 10)
168 STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupLE010);
169 else if (pVM->tm.s.u32VirtualSyncCatchUpPercentage <= 25)
170 STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupLE025);
171 else if (pVM->tm.s.u32VirtualSyncCatchUpPercentage <= 100)
172 STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupLE100);
173 else
174 STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupOther);
175 }
176 else if (!pVM->tm.s.fVirtualSyncTicking)
177 STAM_COUNTER_INC(&pVM->tm.s.StatTSCSyncNotTicking);
178 else if (pVM->tm.s.fVirtualWarpDrive)
179 STAM_COUNTER_INC(&pVM->tm.s.StatTSCWarp);
180 }
181#endif
182 return false;
183}
184
185
186/**
187 * Read the current CPU timstamp counter.
188 *
189 * @returns Gets the CPU tsc.
190 * @param pVCpu The VMCPU to operate on.
191 */
192DECLINLINE(uint64_t) tmCpuTickGetInternal(PVMCPU pVCpu, bool fCheckTimers)
193{
194 uint64_t u64;
195
196 if (RT_LIKELY(pVCpu->tm.s.fTSCTicking))
197 {
198 PVM pVM = pVCpu->CTX_SUFF(pVM);
199 if (pVM->tm.s.fTSCVirtualized)
200 {
201 if (pVM->tm.s.fTSCUseRealTSC)
202 u64 = ASMReadTSC();
203 else
204 u64 = tmCpuTickGetRawVirtual(pVM, fCheckTimers);
205 u64 -= pVCpu->tm.s.offTSCRawSrc;
206 }
207 else
208 u64 = ASMReadTSC();
209
210 /* Never return a value lower than what the guest has already seen. */
211 if (u64 < pVCpu->tm.s.u64TSCLastSeen)
212 {
213 STAM_COUNTER_INC(&pVM->tm.s.StatTSCUnderflow);
214 pVCpu->tm.s.u64TSCLastSeen += 64; /* @todo choose a good increment here */
215 u64 = pVCpu->tm.s.u64TSCLastSeen;
216 }
217 }
218 else
219 u64 = pVCpu->tm.s.u64TSC;
220 return u64;
221}
222
223
224/**
225 * Read the current CPU timstamp counter.
226 *
227 * @returns Gets the CPU tsc.
228 * @param pVCpu The VMCPU to operate on.
229 */
230VMMDECL(uint64_t) TMCpuTickGet(PVMCPU pVCpu)
231{
232 return tmCpuTickGetInternal(pVCpu, true /* fCheckTimers */);
233}
234
235
236/**
237 * Read the current CPU timstamp counter, don't check for expired timers.
238 *
239 * @returns Gets the CPU tsc.
240 * @param pVCpu The VMCPU to operate on.
241 */
242VMM_INT_DECL(uint64_t) TMCpuTickGetNoCheck(PVMCPU pVCpu)
243{
244 return tmCpuTickGetInternal(pVCpu, false /* fCheckTimers */);
245}
246
247
248/**
249 * Sets the current CPU timestamp counter.
250 *
251 * @returns VBox status code.
252 * @param pVM The VM handle.
253 * @param pVCpu The virtual CPU to operate on.
254 * @param u64Tick The new timestamp value.
255 *
256 * @thread EMT which TSC is to be set.
257 */
258VMM_INT_DECL(int) TMCpuTickSet(PVM pVM, PVMCPU pVCpu, uint64_t u64Tick)
259{
260 VMCPU_ASSERT_EMT(pVCpu);
261 STAM_COUNTER_INC(&pVM->tm.s.StatTSCSet);
262
263 /*
264 * This is easier to do when the TSC is paused since resume will
265 * do all the calcuations for us. Actually, we don't need to
266 * call tmCpuTickPause here since we overwrite u64TSC anyway.
267 */
268 bool fTSCTicking = pVCpu->tm.s.fTSCTicking;
269 pVCpu->tm.s.fTSCTicking = false;
270 pVCpu->tm.s.u64TSC = u64Tick;
271 pVCpu->tm.s.u64TSCLastSeen = u64Tick;
272 if (fTSCTicking)
273 tmCpuTickResume(pVM, pVCpu);
274 /** @todo Try help synchronizing it better among the virtual CPUs? */
275
276 return VINF_SUCCESS;
277}
278
279/**
280 * Sets the last seen CPU timestamp counter.
281 *
282 * @returns VBox status code.
283 * @param pVCpu The virtual CPU to operate on.
284 * @param u64LastSeenTick The last seen timestamp value.
285 *
286 * @thread EMT which TSC is to be set.
287 */
288VMM_INT_DECL(int) TMCpuTickSetLastSeen(PVMCPU pVCpu, uint64_t u64LastSeenTick)
289{
290 VMCPU_ASSERT_EMT(pVCpu);
291
292 LogFlow(("TMCpuTickSetLastSeen %RX64\n", u64LastSeenTick));
293 if (pVCpu->tm.s.u64TSCLastSeen < u64LastSeenTick)
294 pVCpu->tm.s.u64TSCLastSeen = u64LastSeenTick;
295 return VINF_SUCCESS;
296}
297
298/**
299 * Gets the last seen CPU timestamp counter.
300 *
301 * @returns last seen TSC
302 * @param pVCpu The virtual CPU to operate on.
303 *
304 * @thread EMT which TSC is to be set.
305 */
306VMM_INT_DECL(uint64_t) TMCpuTickGetLastSeen(PVMCPU pVCpu)
307{
308 VMCPU_ASSERT_EMT(pVCpu);
309
310 return pVCpu->tm.s.u64TSCLastSeen;
311}
312
313
314/**
315 * Get the timestamp frequency.
316 *
317 * @returns Number of ticks per second.
318 * @param pVM The VM.
319 */
320VMMDECL(uint64_t) TMCpuTicksPerSecond(PVM pVM)
321{
322 if (pVM->tm.s.fTSCUseRealTSC)
323 {
324 uint64_t cTSCTicksPerSecond = SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
325 if (RT_LIKELY(cTSCTicksPerSecond != ~(uint64_t)0))
326 return cTSCTicksPerSecond;
327 }
328 return pVM->tm.s.cTSCTicksPerSecond;
329}
330
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