VirtualBox

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

Last change on this file since 22015 was 20689, checked in by vboxsync, 15 years ago

TMCPU::u64TSCOffset -> offTSCRawSrc

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.8 KB
Line 
1/* $Id: TMAllCpu.cpp 20689 2009-06-18 12:13:14Z 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 */
111VMMDECL(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 else
214 u64 = pVCpu->tm.s.u64TSC;
215 return u64;
216}
217
218
219/**
220 * Read the current CPU timstamp counter.
221 *
222 * @returns Gets the CPU tsc.
223 * @param pVCpu The VMCPU to operate on.
224 */
225VMMDECL(uint64_t) TMCpuTickGet(PVMCPU pVCpu)
226{
227 return tmCpuTickGetInternal(pVCpu, true /* fCheckTimers */);
228}
229
230
231/**
232 * Read the current CPU timstamp counter, don't check for expired timers.
233 *
234 * @returns Gets the CPU tsc.
235 * @param pVCpu The VMCPU to operate on.
236 */
237VMMDECL(uint64_t) TMCpuTickGetNoCheck(PVMCPU pVCpu)
238{
239 return tmCpuTickGetInternal(pVCpu, false /* fCheckTimers */);
240}
241
242
243/**
244 * Sets the current CPU timestamp counter.
245 *
246 * @returns VBox status code.
247 * @param pVM The VM handle.
248 * @param pVCpu The virtual CPU to operate on.
249 * @param u64Tick The new timestamp value.
250 *
251 * @thread EMT which TSC is to be set.
252 */
253VMMDECL(int) TMCpuTickSet(PVM pVM, PVMCPU pVCpu, uint64_t u64Tick)
254{
255 VMCPU_ASSERT_EMT(pVCpu);
256 STAM_COUNTER_INC(&pVM->tm.s.StatTSCSet);
257
258 /*
259 * This is easier to do when the TSC is paused since resume will
260 * do all the calcuations for us. Actually, we don't need to
261 * call tmCpuTickPause here since we overwrite u64TSC anyway.
262 */
263 bool fTSCTicking = pVCpu->tm.s.fTSCTicking;
264 pVCpu->tm.s.fTSCTicking = false;
265 pVCpu->tm.s.u64TSC = u64Tick;
266 if (fTSCTicking)
267 tmCpuTickResume(pVM, pVCpu);
268 /** @todo Try help synchronizing it better among the virtual CPUs? */
269
270 return VINF_SUCCESS;
271}
272
273
274/**
275 * Get the timestamp frequency.
276 *
277 * @returns Number of ticks per second.
278 * @param pVM The VM.
279 */
280VMMDECL(uint64_t) TMCpuTicksPerSecond(PVM pVM)
281{
282 if (pVM->tm.s.fTSCUseRealTSC)
283 {
284 uint64_t cTSCTicksPerSecond = SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
285 if (RT_LIKELY(cTSCTicksPerSecond != ~(uint64_t)0))
286 return cTSCTicksPerSecond;
287 }
288 return pVM->tm.s.cTSCTicksPerSecond;
289}
290
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