VirtualBox

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

Last change on this file since 4050 was 2981, checked in by vboxsync, 17 years ago

InnoTek -> innotek: all the headers and comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1/* $Id: TMAllCpu.cpp 2981 2007-06-01 16:01:28Z vboxsync $ */
2/** @file
3 * TM - Timeout Manager, CPU Time, All Contexts.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
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 <iprt/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 */
57TMDECL(int) TMCpuTickResume(PVM pVM)
58{
59 if (!pVM->tm.s.fTSCTicking)
60 {
61 pVM->tm.s.fTSCTicking = true;
62 if (pVM->tm.s.fTSCVirtualized)
63 {
64 if (pVM->tm.s.fTSCUseRealTSC)
65 pVM->tm.s.u64TSCOffset = ASMReadTSC() - pVM->tm.s.u64TSC;
66 else
67 pVM->tm.s.u64TSCOffset = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
68 - pVM->tm.s.u64TSC;
69 }
70 return VINF_SUCCESS;
71 }
72 AssertFailed();
73 return VERR_INTERNAL_ERROR;
74}
75
76
77/**
78 * Pauses the CPU timestamp counter ticking.
79 *
80 * @returns VBox status code.
81 * @param pVM The VM to operate on.
82 */
83TMDECL(int) TMCpuTickPause(PVM pVM)
84{
85 if (pVM->tm.s.fTSCTicking)
86 {
87 pVM->tm.s.u64TSC = TMCpuTickGet(pVM);
88 pVM->tm.s.fTSCTicking = false;
89 return VINF_SUCCESS;
90 }
91 AssertFailed();
92 return VERR_INTERNAL_ERROR;
93}
94
95
96/**
97 * Returns the TSC offset (virtual TSC - host TSC)
98 *
99 * @returns TSC ofset
100 * @param pVM The VM to operate on.
101 * @todo Remove this when the code has been switched to TMCpuTickCanUseRealTSC.
102 */
103TMDECL(uint64_t) TMCpuTickGetOffset(PVM pVM)
104{
105 uint64_t u64;
106 if (RT_LIKELY(pVM->tm.s.fTSCTicking))
107 {
108 if (pVM->tm.s.fTSCVirtualized)
109 {
110 if (pVM->tm.s.fTSCUseRealTSC)
111 u64 = ASMReadTSC();
112 else
113 u64 = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */);
114 u64 -= pVM->tm.s.u64TSCOffset;
115 }
116 else
117 u64 = ASMReadTSC();
118 }
119 else
120 u64 = pVM->tm.s.u64TSC;
121
122 return u64 - ASMReadTSC();
123}
124
125
126/**
127 * Checks if AMD-V / VT-x can use an offsetted hardware TSC or not.
128 *
129 * @returns true/false accordingly.
130 * @param pVM The VM handle.
131 * @param poffRealTSC The offset against the TSC of the current CPU.
132 * Can be NULL.
133 * @thread EMT.
134 */
135TMDECL(bool) TMCpuTickCanUseRealTSC(PVM pVM, uint64_t *poffRealTSC)
136{
137 /*
138 * We require:
139 * 1. A fixed TSC, this is checked at init time.
140 * 2. That the TSC is ticking (we shouldn't be here if it isn't)
141 * 3. Either that we're using the real TSC as time source or
142 * a) We don't have any lag to catch up.
143 * b) The virtual sync clock hasn't been halted by an expired timer.
144 * c) We're not using warp drive (accelerated virtual guest time).
145 */
146 if ( pVM->tm.s.fMaybeUseOffsettedHostTSC
147 && RT_LIKELY(pVM->tm.s.fTSCTicking)
148 && ( pVM->tm.s.fTSCUseRealTSC
149 || ( !pVM->tm.s.fVirtualSyncCatchUp
150 && RT_LIKELY(pVM->tm.s.fVirtualSyncTicking)
151 && !pVM->tm.s.fVirtualWarpDrive))
152 )
153 {
154 if (!pVM->tm.s.fTSCUseRealTSC)
155 {
156 /* The source is the timer synchronous virtual clock. */
157 Assert(pVM->tm.s.fTSCVirtualized);
158
159 if (poffRealTSC)
160 {
161 uint64_t u64Now = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
162 - pVM->tm.s.u64TSCOffset;
163 /** @todo When we start collecting statistics on how much time we spend executing
164 * guest code before exiting, we should check this against the next virtual sync
165 * timer timeout. If it's lower than the avg. length, we should trap rdtsc to increase
166 * the chance that we'll get interrupted right after the timer expired. */
167 *poffRealTSC = u64Now - ASMReadTSC();
168 }
169 }
170 else if (poffRealTSC)
171 {
172 /* The source is the real TSC. */
173 if (pVM->tm.s.fTSCVirtualized)
174 *poffRealTSC = pVM->tm.s.u64TSCOffset;
175 else
176 *poffRealTSC = 0;
177 }
178 return true;
179 }
180
181 return false;
182}
183
184
185/**
186 * Read the current CPU timstamp counter.
187 *
188 * @returns Gets the CPU tsc.
189 * @param pVM The VM to operate on.
190 */
191TMDECL(uint64_t) TMCpuTickGet(PVM pVM)
192{
193 uint64_t u64;
194 if (RT_LIKELY(pVM->tm.s.fTSCTicking))
195 {
196 if (pVM->tm.s.fTSCVirtualized)
197 {
198 if (pVM->tm.s.fTSCUseRealTSC)
199 u64 = ASMReadTSC();
200 else
201 u64 = tmCpuTickGetRawVirtual(pVM, true /* check for pending timers */);
202 u64 -= pVM->tm.s.u64TSCOffset;
203 }
204 else
205 u64 = ASMReadTSC();
206 }
207 else
208 u64 = pVM->tm.s.u64TSC;
209 return u64;
210}
211
212
213/**
214 * Sets the current CPU timestamp counter.
215 *
216 * @returns VBox status code.
217 * @param pVM The VM to operate on.
218 * @param u64Tick The new timestamp value.
219 */
220TMDECL(int) TMCpuTickSet(PVM pVM, uint64_t u64Tick)
221{
222 Assert(!pVM->tm.s.fTSCTicking);
223 pVM->tm.s.u64TSC = u64Tick;
224 return VINF_SUCCESS;
225}
226
227
228/**
229 * Get the timestamp frequency.
230 *
231 * @returns Number of ticks per second.
232 * @param pVM The VM.
233 */
234TMDECL(uint64_t) TMCpuTicksPerSecond(PVM pVM)
235{
236 if (pVM->tm.s.fTSCUseRealTSC)
237 {
238 uint64_t cTSCTicksPerSecond = SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
239 if (RT_LIKELY(cTSCTicksPerSecond != ~(uint64_t)0))
240 return cTSCTicksPerSecond;
241 }
242 return pVM->tm.s.cTSCTicksPerSecond;
243}
244
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