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