VirtualBox

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

Last change on this file since 13572 was 13572, checked in by vboxsync, 16 years ago

Statistics for rdtsc intercepts

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.5 KB
Line 
1/* $Id: TMAllCpu.cpp 13572 2008-10-27 11:02:33Z 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 * @internal
57 */
58int tmCpuTickResume(PVM pVM)
59{
60 if (!pVM->tm.s.fTSCTicking)
61 {
62 pVM->tm.s.fTSCTicking = true;
63 if (pVM->tm.s.fTSCVirtualized)
64 {
65 if (pVM->tm.s.fTSCUseRealTSC)
66 pVM->tm.s.u64TSCOffset = ASMReadTSC() - pVM->tm.s.u64TSC;
67 else
68 pVM->tm.s.u64TSCOffset = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
69 - pVM->tm.s.u64TSC;
70 }
71 return VINF_SUCCESS;
72 }
73 AssertFailed();
74 return VERR_INTERNAL_ERROR;
75}
76
77
78/**
79 * Resumes the CPU timestamp counter ticking.
80 *
81 * @returns VBox status code.
82 * @param pVM The VM to operate on.
83 * @todo replace this with TMNotifyResume
84 */
85VMMDECL(int) TMCpuTickResume(PVM pVM)
86{
87 if (!pVM->tm.s.fTSCTiedToExecution)
88 return tmCpuTickResume(pVM);
89 /* ignored */
90 return VINF_SUCCESS;
91}
92
93
94/**
95 * Pauses the CPU timestamp counter ticking.
96 *
97 * @returns VBox status code.
98 * @param pVM The VM to operate on.
99 * @internal
100 */
101int tmCpuTickPause(PVM pVM)
102{
103 if (pVM->tm.s.fTSCTicking)
104 {
105 pVM->tm.s.u64TSC = TMCpuTickGet(pVM);
106 pVM->tm.s.fTSCTicking = false;
107 return VINF_SUCCESS;
108 }
109 AssertFailed();
110 return VERR_INTERNAL_ERROR;
111}
112
113
114/**
115 * Pauses the CPU timestamp counter ticking.
116 *
117 * @returns VBox status code.
118 * @param pVM The VM to operate on.
119 * @todo replace this with TMNotifySuspend
120 */
121VMMDECL(int) TMCpuTickPause(PVM pVM)
122{
123 if (!pVM->tm.s.fTSCTiedToExecution)
124 return tmCpuTickPause(pVM);
125 /* ignored */
126 return VINF_SUCCESS;
127}
128
129
130/**
131 * Checks if AMD-V / VT-x can use an offsetted hardware TSC or not.
132 *
133 * @returns true/false accordingly.
134 * @param pVM The VM handle.
135 * @param poffRealTSC The offset against the TSC of the current CPU.
136 * Can be NULL.
137 * @thread EMT.
138 */
139VMMDECL(bool) TMCpuTickCanUseRealTSC(PVM pVM, uint64_t *poffRealTSC)
140{
141 /*
142 * We require:
143 * 1. A fixed TSC, this is checked at init time.
144 * 2. That the TSC is ticking (we shouldn't be here if it isn't)
145 * 3. Either that we're using the real TSC as time source or
146 * a) we don't have any lag to catch up, and
147 * b) the virtual sync clock hasn't been halted by an expired timer, and
148 * c) we're not using warp drive (accelerated virtual guest time).
149 */
150#ifdef VBOX_WITH_STATISTICS
151 if (!pVM->tm.s.fMaybeUseOffsettedHostTSC)
152 STAM_COUNTER_INC(&pVM->tm.s.StatTSCNotFixed);
153 else
154 if (!pVM->tm.s.fTSCTicking)
155 STAM_COUNTER_INC(&pVM->tm.s.StatTSCNotTicking);
156 else
157 if (!pVM->tm.s.fTSCUseRealTSC)
158 {
159 if (pVM->tm.s.fVirtualSyncCatchUp)
160 STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchup);
161 else
162 if (!pVM->tm.s.fVirtualSyncTicking)
163 STAM_COUNTER_INC(&pVM->tm.s.StatTSCSyncNotTicking);
164 else
165 if (!pVM->tm.s.fVirtualWarpDrive)
166 STAM_COUNTER_INC(&pVM->tm.s.StatTSCWarp);
167 }
168#endif
169
170 if ( pVM->tm.s.fMaybeUseOffsettedHostTSC
171 && RT_LIKELY(pVM->tm.s.fTSCTicking)
172 && ( pVM->tm.s.fTSCUseRealTSC
173 || ( !pVM->tm.s.fVirtualSyncCatchUp
174 && RT_LIKELY(pVM->tm.s.fVirtualSyncTicking)
175 && !pVM->tm.s.fVirtualWarpDrive))
176 )
177 {
178 if (!pVM->tm.s.fTSCUseRealTSC)
179 {
180 /* The source is the timer synchronous virtual clock. */
181 Assert(pVM->tm.s.fTSCVirtualized);
182
183 if (poffRealTSC)
184 {
185 uint64_t u64Now = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
186 - pVM->tm.s.u64TSCOffset;
187 /** @todo When we start collecting statistics on how much time we spend executing
188 * guest code before exiting, we should check this against the next virtual sync
189 * timer timeout. If it's lower than the avg. length, we should trap rdtsc to increase
190 * the chance that we'll get interrupted right after the timer expired. */
191 *poffRealTSC = u64Now - ASMReadTSC();
192 }
193 }
194 else if (poffRealTSC)
195 {
196 /* The source is the real TSC. */
197 if (pVM->tm.s.fTSCVirtualized)
198 *poffRealTSC = pVM->tm.s.u64TSCOffset;
199 else
200 *poffRealTSC = 0;
201 }
202 return true;
203 }
204
205 return false;
206}
207
208
209/**
210 * Read the current CPU timstamp counter.
211 *
212 * @returns Gets the CPU tsc.
213 * @param pVM The VM to operate on.
214 */
215VMMDECL(uint64_t) TMCpuTickGet(PVM pVM)
216{
217 uint64_t u64;
218 if (RT_LIKELY(pVM->tm.s.fTSCTicking))
219 {
220 if (pVM->tm.s.fTSCVirtualized)
221 {
222 if (pVM->tm.s.fTSCUseRealTSC)
223 u64 = ASMReadTSC();
224 else
225 u64 = tmCpuTickGetRawVirtual(pVM, true /* check for pending timers */);
226 u64 -= pVM->tm.s.u64TSCOffset;
227 }
228 else
229 u64 = ASMReadTSC();
230 }
231 else
232 u64 = pVM->tm.s.u64TSC;
233 return u64;
234}
235
236
237/**
238 * Sets the current CPU timestamp counter.
239 *
240 * @returns VBox status code.
241 * @param pVM The VM to operate on.
242 * @param u64Tick The new timestamp value.
243 */
244VMMDECL(int) TMCpuTickSet(PVM pVM, uint64_t u64Tick)
245{
246 Assert(!pVM->tm.s.fTSCTicking);
247 pVM->tm.s.u64TSC = u64Tick;
248 return VINF_SUCCESS;
249}
250
251
252/**
253 * Get the timestamp frequency.
254 *
255 * @returns Number of ticks per second.
256 * @param pVM The VM.
257 */
258VMMDECL(uint64_t) TMCpuTicksPerSecond(PVM pVM)
259{
260 if (pVM->tm.s.fTSCUseRealTSC)
261 {
262 uint64_t cTSCTicksPerSecond = SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
263 if (RT_LIKELY(cTSCTicksPerSecond != ~(uint64_t)0))
264 return cTSCTicksPerSecond;
265 }
266 return pVM->tm.s.cTSCTicksPerSecond;
267}
268
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