VirtualBox

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

Last change on this file since 8861 was 8155, checked in by vboxsync, 17 years ago

The Big Sun Rebranding Header Change

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1/* $Id: TMAllCpu.cpp 8155 2008-04-18 15:16:47Z 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 <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 * Checks if AMD-V / VT-x can use an offsetted hardware TSC or not.
98 *
99 * @returns true/false accordingly.
100 * @param pVM The VM handle.
101 * @param poffRealTSC The offset against the TSC of the current CPU.
102 * Can be NULL.
103 * @thread EMT.
104 */
105TMDECL(bool) TMCpuTickCanUseRealTSC(PVM pVM, uint64_t *poffRealTSC)
106{
107 /*
108 * We require:
109 * 1. A fixed TSC, this is checked at init time.
110 * 2. That the TSC is ticking (we shouldn't be here if it isn't)
111 * 3. Either that we're using the real TSC as time source or
112 * a) We don't have any lag to catch up.
113 * b) The virtual sync clock hasn't been halted by an expired timer.
114 * c) We're not using warp drive (accelerated virtual guest time).
115 */
116 if ( pVM->tm.s.fMaybeUseOffsettedHostTSC
117 && RT_LIKELY(pVM->tm.s.fTSCTicking)
118 && ( pVM->tm.s.fTSCUseRealTSC
119 || ( !pVM->tm.s.fVirtualSyncCatchUp
120 && RT_LIKELY(pVM->tm.s.fVirtualSyncTicking)
121 && !pVM->tm.s.fVirtualWarpDrive))
122 )
123 {
124 if (!pVM->tm.s.fTSCUseRealTSC)
125 {
126 /* The source is the timer synchronous virtual clock. */
127 Assert(pVM->tm.s.fTSCVirtualized);
128
129 if (poffRealTSC)
130 {
131 uint64_t u64Now = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
132 - pVM->tm.s.u64TSCOffset;
133 /** @todo When we start collecting statistics on how much time we spend executing
134 * guest code before exiting, we should check this against the next virtual sync
135 * timer timeout. If it's lower than the avg. length, we should trap rdtsc to increase
136 * the chance that we'll get interrupted right after the timer expired. */
137 *poffRealTSC = u64Now - ASMReadTSC();
138 }
139 }
140 else if (poffRealTSC)
141 {
142 /* The source is the real TSC. */
143 if (pVM->tm.s.fTSCVirtualized)
144 *poffRealTSC = pVM->tm.s.u64TSCOffset;
145 else
146 *poffRealTSC = 0;
147 }
148 return true;
149 }
150
151 return false;
152}
153
154
155/**
156 * Read the current CPU timstamp counter.
157 *
158 * @returns Gets the CPU tsc.
159 * @param pVM The VM to operate on.
160 */
161TMDECL(uint64_t) TMCpuTickGet(PVM pVM)
162{
163 uint64_t u64;
164 if (RT_LIKELY(pVM->tm.s.fTSCTicking))
165 {
166 if (pVM->tm.s.fTSCVirtualized)
167 {
168 if (pVM->tm.s.fTSCUseRealTSC)
169 u64 = ASMReadTSC();
170 else
171 u64 = tmCpuTickGetRawVirtual(pVM, true /* check for pending timers */);
172 u64 -= pVM->tm.s.u64TSCOffset;
173 }
174 else
175 u64 = ASMReadTSC();
176 }
177 else
178 u64 = pVM->tm.s.u64TSC;
179 return u64;
180}
181
182
183/**
184 * Sets the current CPU timestamp counter.
185 *
186 * @returns VBox status code.
187 * @param pVM The VM to operate on.
188 * @param u64Tick The new timestamp value.
189 */
190TMDECL(int) TMCpuTickSet(PVM pVM, uint64_t u64Tick)
191{
192 Assert(!pVM->tm.s.fTSCTicking);
193 pVM->tm.s.u64TSC = u64Tick;
194 return VINF_SUCCESS;
195}
196
197
198/**
199 * Get the timestamp frequency.
200 *
201 * @returns Number of ticks per second.
202 * @param pVM The VM.
203 */
204TMDECL(uint64_t) TMCpuTicksPerSecond(PVM pVM)
205{
206 if (pVM->tm.s.fTSCUseRealTSC)
207 {
208 uint64_t cTSCTicksPerSecond = SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
209 if (RT_LIKELY(cTSCTicksPerSecond != ~(uint64_t)0))
210 return cTSCTicksPerSecond;
211 }
212 return pVM->tm.s.cTSCTicksPerSecond;
213}
214
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