VirtualBox

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

Last change on this file since 26278 was 22808, checked in by vboxsync, 15 years ago

TM: hiding VMM internal APIs.

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