1 | /* $Id: TMAllCpu.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * TM - Timeout Manager, CPU Time, All Contexts.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 Oracle Corporation
|
---|
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 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_TM
|
---|
23 | #include <VBox/vmm/tm.h>
|
---|
24 | #include <iprt/asm-amd64-x86.h> /* for SUPGetCpuHzFromGIP */
|
---|
25 | #include "TMInternal.h"
|
---|
26 | #include <VBox/vmm/vm.h>
|
---|
27 | #include <VBox/vmm/gim.h>
|
---|
28 | #include <VBox/vmm/dbgf.h>
|
---|
29 | #include <VBox/sup.h>
|
---|
30 |
|
---|
31 | #include <VBox/param.h>
|
---|
32 | #include <VBox/err.h>
|
---|
33 | #include <iprt/asm-math.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <VBox/log.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Gets the raw cpu tick from current virtual time.
|
---|
40 | *
|
---|
41 | * @param pVM The cross context VM structure.
|
---|
42 | * @param fCheckTimers Whether to check timers.
|
---|
43 | */
|
---|
44 | DECLINLINE(uint64_t) tmCpuTickGetRawVirtual(PVM pVM, bool fCheckTimers)
|
---|
45 | {
|
---|
46 | uint64_t u64;
|
---|
47 | if (fCheckTimers)
|
---|
48 | u64 = TMVirtualSyncGet(pVM);
|
---|
49 | else
|
---|
50 | u64 = TMVirtualSyncGetNoCheck(pVM);
|
---|
51 | return ASMMultU64ByU32DivByU32(u64, pVM->tm.s.cTSCTicksPerSecond, TMCLOCK_FREQ_VIRTUAL);
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | #ifdef IN_RING3
|
---|
56 | /**
|
---|
57 | * Used by tmR3CpuTickParavirtEnable and tmR3CpuTickParavirtDisable.
|
---|
58 | *
|
---|
59 | * @param pVM The cross context VM structure.
|
---|
60 | */
|
---|
61 | uint64_t tmR3CpuTickGetRawVirtualNoCheck(PVM pVM)
|
---|
62 | {
|
---|
63 | return tmCpuTickGetRawVirtual(pVM, false /*fCheckTimers*/);
|
---|
64 | }
|
---|
65 | #endif
|
---|
66 |
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Resumes the CPU timestamp counter ticking.
|
---|
70 | *
|
---|
71 | * @returns VBox status code.
|
---|
72 | * @param pVM The cross context VM structure.
|
---|
73 | * @param pVCpu The cross context virtual CPU structure.
|
---|
74 | * @internal
|
---|
75 | */
|
---|
76 | int tmCpuTickResume(PVM pVM, PVMCPU pVCpu)
|
---|
77 | {
|
---|
78 | if (!pVCpu->tm.s.fTSCTicking)
|
---|
79 | {
|
---|
80 | pVCpu->tm.s.fTSCTicking = true;
|
---|
81 |
|
---|
82 | /** @todo Test that pausing and resuming doesn't cause lag! (I.e. that we're
|
---|
83 | * unpaused before the virtual time and stopped after it. */
|
---|
84 | if (pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET)
|
---|
85 | pVCpu->tm.s.offTSCRawSrc = SUPReadTsc() - pVCpu->tm.s.u64TSC;
|
---|
86 | else
|
---|
87 | pVCpu->tm.s.offTSCRawSrc = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
|
---|
88 | - pVCpu->tm.s.u64TSC;
|
---|
89 | return VINF_SUCCESS;
|
---|
90 | }
|
---|
91 | AssertFailed();
|
---|
92 | return VERR_TM_TSC_ALREADY_TICKING;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Resumes the CPU timestamp counter ticking.
|
---|
98 | *
|
---|
99 | * @returns VINF_SUCCESS or VERR_TM_VIRTUAL_TICKING_IPE (asserted).
|
---|
100 | * @param pVM The cross context VM structure.
|
---|
101 | * @param pVCpu The cross context virtual CPU structure.
|
---|
102 | */
|
---|
103 | int tmCpuTickResumeLocked(PVM pVM, PVMCPU pVCpu)
|
---|
104 | {
|
---|
105 | if (!pVCpu->tm.s.fTSCTicking)
|
---|
106 | {
|
---|
107 | /* TSC must be ticking before calling tmCpuTickGetRawVirtual()! */
|
---|
108 | pVCpu->tm.s.fTSCTicking = true;
|
---|
109 | uint32_t c = ASMAtomicIncU32(&pVM->tm.s.cTSCsTicking);
|
---|
110 | AssertMsgReturn(c <= pVM->cCpus, ("%u vs %u\n", c, pVM->cCpus), VERR_TM_VIRTUAL_TICKING_IPE);
|
---|
111 | if (c == 1)
|
---|
112 | {
|
---|
113 | /* The first VCPU to resume. */
|
---|
114 | uint64_t offTSCRawSrcOld = pVCpu->tm.s.offTSCRawSrc;
|
---|
115 |
|
---|
116 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCResume);
|
---|
117 |
|
---|
118 | /* When resuming, use the TSC value of the last stopped VCPU to avoid the TSC going back. */
|
---|
119 | if (pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET)
|
---|
120 | pVCpu->tm.s.offTSCRawSrc = SUPReadTsc() - pVM->tm.s.u64LastPausedTSC;
|
---|
121 | else
|
---|
122 | pVCpu->tm.s.offTSCRawSrc = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
|
---|
123 | - pVM->tm.s.u64LastPausedTSC;
|
---|
124 |
|
---|
125 | /* Calculate the offset for other VCPUs to use. */
|
---|
126 | pVM->tm.s.offTSCPause = pVCpu->tm.s.offTSCRawSrc - offTSCRawSrcOld;
|
---|
127 | }
|
---|
128 | else
|
---|
129 | {
|
---|
130 | /* All other VCPUs (if any). */
|
---|
131 | pVCpu->tm.s.offTSCRawSrc += pVM->tm.s.offTSCPause;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | return VINF_SUCCESS;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Pauses the CPU timestamp counter ticking.
|
---|
140 | *
|
---|
141 | * @returns VBox status code.
|
---|
142 | * @param pVCpu The cross context virtual CPU structure.
|
---|
143 | * @internal
|
---|
144 | */
|
---|
145 | int tmCpuTickPause(PVMCPU pVCpu)
|
---|
146 | {
|
---|
147 | if (pVCpu->tm.s.fTSCTicking)
|
---|
148 | {
|
---|
149 | pVCpu->tm.s.u64TSC = TMCpuTickGetNoCheck(pVCpu);
|
---|
150 | pVCpu->tm.s.fTSCTicking = false;
|
---|
151 | return VINF_SUCCESS;
|
---|
152 | }
|
---|
153 | AssertFailed();
|
---|
154 | return VERR_TM_TSC_ALREADY_PAUSED;
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Pauses the CPU timestamp counter ticking.
|
---|
160 | *
|
---|
161 | * @returns VBox status code.
|
---|
162 | * @param pVM The cross context VM structure.
|
---|
163 | * @param pVCpu The cross context virtual CPU structure.
|
---|
164 | * @internal
|
---|
165 | */
|
---|
166 | int tmCpuTickPauseLocked(PVM pVM, PVMCPU pVCpu)
|
---|
167 | {
|
---|
168 | if (pVCpu->tm.s.fTSCTicking)
|
---|
169 | {
|
---|
170 | pVCpu->tm.s.u64TSC = TMCpuTickGetNoCheck(pVCpu);
|
---|
171 | pVCpu->tm.s.fTSCTicking = false;
|
---|
172 |
|
---|
173 | uint32_t c = ASMAtomicDecU32(&pVM->tm.s.cTSCsTicking);
|
---|
174 | AssertMsgReturn(c < pVM->cCpus, ("%u vs %u\n", c, pVM->cCpus), VERR_TM_VIRTUAL_TICKING_IPE);
|
---|
175 | if (c == 0)
|
---|
176 | {
|
---|
177 | /* When the last TSC stops, remember the value. */
|
---|
178 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCPause);
|
---|
179 | pVM->tm.s.u64LastPausedTSC = pVCpu->tm.s.u64TSC;
|
---|
180 | }
|
---|
181 | return VINF_SUCCESS;
|
---|
182 | }
|
---|
183 | AssertFailed();
|
---|
184 | return VERR_TM_TSC_ALREADY_PAUSED;
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | #ifdef VBOX_WITH_STATISTICS
|
---|
189 | /**
|
---|
190 | * Record why we refused to use offsetted TSC.
|
---|
191 | *
|
---|
192 | * Used by TMCpuTickCanUseRealTSC() and TMCpuTickGetDeadlineAndTscOffset().
|
---|
193 | *
|
---|
194 | * @param pVM The cross context VM structure.
|
---|
195 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
196 | */
|
---|
197 | DECLINLINE(void) tmCpuTickRecordOffsettedTscRefusal(PVM pVM, PVMCPU pVCpu)
|
---|
198 | {
|
---|
199 | /* Sample the reason for refusing. */
|
---|
200 | if (pVM->tm.s.enmTSCMode != TMTSCMODE_DYNAMIC)
|
---|
201 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCNotFixed);
|
---|
202 | else if (!pVCpu->tm.s.fTSCTicking)
|
---|
203 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCNotTicking);
|
---|
204 | else if (pVM->tm.s.enmTSCMode != TMTSCMODE_REAL_TSC_OFFSET)
|
---|
205 | {
|
---|
206 | if (pVM->tm.s.fVirtualSyncCatchUp)
|
---|
207 | {
|
---|
208 | if (pVM->tm.s.u32VirtualSyncCatchUpPercentage <= 10)
|
---|
209 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupLE010);
|
---|
210 | else if (pVM->tm.s.u32VirtualSyncCatchUpPercentage <= 25)
|
---|
211 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupLE025);
|
---|
212 | else if (pVM->tm.s.u32VirtualSyncCatchUpPercentage <= 100)
|
---|
213 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupLE100);
|
---|
214 | else
|
---|
215 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupOther);
|
---|
216 | }
|
---|
217 | else if (!pVM->tm.s.fVirtualSyncTicking)
|
---|
218 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCSyncNotTicking);
|
---|
219 | else if (pVM->tm.s.fVirtualWarpDrive)
|
---|
220 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCWarp);
|
---|
221 | }
|
---|
222 | }
|
---|
223 | #endif /* VBOX_WITH_STATISTICS */
|
---|
224 |
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Checks if AMD-V / VT-x can use an offsetted hardware TSC or not.
|
---|
228 | *
|
---|
229 | * @returns true/false accordingly.
|
---|
230 | * @param pVM The cross context VM structure.
|
---|
231 | * @param pVCpu The cross context virtual CPU structure.
|
---|
232 | * @param poffRealTsc The offset against the TSC of the current host CPU,
|
---|
233 | * if pfOffsettedTsc is set to true.
|
---|
234 | * @param pfParavirtTsc Where to return whether paravirt TSC is enabled.
|
---|
235 | *
|
---|
236 | * @thread EMT(pVCpu).
|
---|
237 | * @see TMCpuTickGetDeadlineAndTscOffset().
|
---|
238 | */
|
---|
239 | VMM_INT_DECL(bool) TMCpuTickCanUseRealTSC(PVM pVM, PVMCPU pVCpu, uint64_t *poffRealTsc, bool *pfParavirtTsc)
|
---|
240 | {
|
---|
241 | Assert(pVCpu->tm.s.fTSCTicking || DBGFIsStepping(pVCpu));
|
---|
242 |
|
---|
243 | *pfParavirtTsc = pVM->tm.s.fParavirtTscEnabled;
|
---|
244 |
|
---|
245 | /*
|
---|
246 | * In real TSC mode it's easy, we just need the delta & offTscRawSrc and
|
---|
247 | * the CPU will add them to RDTSC and RDTSCP at runtime.
|
---|
248 | *
|
---|
249 | * In tmCpuTickGetInternal we do:
|
---|
250 | * SUPReadTsc() - pVCpu->tm.s.offTSCRawSrc;
|
---|
251 | * Where SUPReadTsc() does:
|
---|
252 | * ASMReadTSC() - pGipCpu->i64TscDelta;
|
---|
253 | * Which means tmCpuTickGetInternal actually does:
|
---|
254 | * ASMReadTSC() - pGipCpu->i64TscDelta - pVCpu->tm.s.offTSCRawSrc;
|
---|
255 | * So, the offset to be ADDED to RDTSC[P] is:
|
---|
256 | * offRealTsc = -(pGipCpu->i64TscDelta + pVCpu->tm.s.offTSCRawSrc)
|
---|
257 | */
|
---|
258 | if (pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET)
|
---|
259 | {
|
---|
260 | /** @todo We should negate both deltas! It's soo weird that we do the
|
---|
261 | * exact opposite of what the hardware implements. */
|
---|
262 | #ifdef IN_RING3
|
---|
263 | *poffRealTsc = 0 - pVCpu->tm.s.offTSCRawSrc - SUPGetTscDelta();
|
---|
264 | #else
|
---|
265 | *poffRealTsc = 0 - pVCpu->tm.s.offTSCRawSrc - SUPGetTscDeltaByCpuSetIndex(pVCpu->iHostCpuSet);
|
---|
266 | #endif
|
---|
267 | return true;
|
---|
268 | }
|
---|
269 |
|
---|
270 | /*
|
---|
271 | * We require:
|
---|
272 | * 1. A fixed TSC, this is checked at init time.
|
---|
273 | * 2. That the TSC is ticking (we shouldn't be here if it isn't)
|
---|
274 | * 3. Either that we're using the real TSC as time source or
|
---|
275 | * a) we don't have any lag to catch up, and
|
---|
276 | * b) the virtual sync clock hasn't been halted by an expired timer, and
|
---|
277 | * c) we're not using warp drive (accelerated virtual guest time).
|
---|
278 | */
|
---|
279 | if ( pVM->tm.s.enmTSCMode == TMTSCMODE_DYNAMIC
|
---|
280 | && !pVM->tm.s.fVirtualSyncCatchUp
|
---|
281 | && RT_LIKELY(pVM->tm.s.fVirtualSyncTicking)
|
---|
282 | && !pVM->tm.s.fVirtualWarpDrive)
|
---|
283 | {
|
---|
284 | /* The source is the timer synchronous virtual clock. */
|
---|
285 | uint64_t u64Now = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
|
---|
286 | - pVCpu->tm.s.offTSCRawSrc;
|
---|
287 | /** @todo When we start collecting statistics on how much time we spend executing
|
---|
288 | * guest code before exiting, we should check this against the next virtual sync
|
---|
289 | * timer timeout. If it's lower than the avg. length, we should trap rdtsc to increase
|
---|
290 | * the chance that we'll get interrupted right after the timer expired. */
|
---|
291 | if (u64Now >= pVCpu->tm.s.u64TSCLastSeen)
|
---|
292 | {
|
---|
293 | *poffRealTsc = u64Now - ASMReadTSC();
|
---|
294 | return true; /** @todo count this? */
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | #ifdef VBOX_WITH_STATISTICS
|
---|
299 | tmCpuTickRecordOffsettedTscRefusal(pVM, pVCpu);
|
---|
300 | #endif
|
---|
301 | return false;
|
---|
302 | }
|
---|
303 |
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Calculates the number of host CPU ticks till the next virtual sync deadline.
|
---|
307 | *
|
---|
308 | * @note To save work, this function will not bother calculating the accurate
|
---|
309 | * tick count for deadlines that are more than a second ahead.
|
---|
310 | *
|
---|
311 | * @returns The number of host cpu ticks to the next deadline. Max one second.
|
---|
312 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
313 | * @param cNsToDeadline The number of nano seconds to the next virtual
|
---|
314 | * sync deadline.
|
---|
315 | */
|
---|
316 | DECLINLINE(uint64_t) tmCpuCalcTicksToDeadline(PVMCPU pVCpu, uint64_t cNsToDeadline)
|
---|
317 | {
|
---|
318 | AssertCompile(TMCLOCK_FREQ_VIRTUAL <= _4G);
|
---|
319 | #ifdef IN_RING3
|
---|
320 | RT_NOREF_PV(pVCpu);
|
---|
321 | uint64_t uCpuHz = SUPGetCpuHzFromGip(g_pSUPGlobalInfoPage);
|
---|
322 | #else
|
---|
323 | uint64_t uCpuHz = SUPGetCpuHzFromGipBySetIndex(g_pSUPGlobalInfoPage, pVCpu->iHostCpuSet);
|
---|
324 | #endif
|
---|
325 | if (RT_UNLIKELY(cNsToDeadline >= TMCLOCK_FREQ_VIRTUAL))
|
---|
326 | return uCpuHz;
|
---|
327 | uint64_t cTicks = ASMMultU64ByU32DivByU32(uCpuHz, cNsToDeadline, TMCLOCK_FREQ_VIRTUAL);
|
---|
328 | if (cTicks > 4000)
|
---|
329 | cTicks -= 4000; /* fudge to account for overhead */
|
---|
330 | else
|
---|
331 | cTicks >>= 1;
|
---|
332 | return cTicks;
|
---|
333 | }
|
---|
334 |
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * Gets the next deadline in host CPU clock ticks and the TSC offset if we can
|
---|
338 | * use the raw TSC.
|
---|
339 | *
|
---|
340 | * @returns The number of host CPU clock ticks to the next timer deadline.
|
---|
341 | * @param pVM The cross context VM structure.
|
---|
342 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
343 | * @param poffRealTsc The offset against the TSC of the current host CPU,
|
---|
344 | * if pfOffsettedTsc is set to true.
|
---|
345 | * @param pfOffsettedTsc Where to return whether TSC offsetting can be used.
|
---|
346 | * @param pfParavirtTsc Where to return whether paravirt TSC is enabled.
|
---|
347 | *
|
---|
348 | * @thread EMT(pVCpu).
|
---|
349 | * @see TMCpuTickCanUseRealTSC().
|
---|
350 | */
|
---|
351 | VMM_INT_DECL(uint64_t) TMCpuTickGetDeadlineAndTscOffset(PVM pVM, PVMCPU pVCpu, uint64_t *poffRealTsc,
|
---|
352 | bool *pfOffsettedTsc, bool *pfParavirtTsc)
|
---|
353 | {
|
---|
354 | Assert(pVCpu->tm.s.fTSCTicking || DBGFIsStepping(pVCpu));
|
---|
355 |
|
---|
356 | *pfParavirtTsc = pVM->tm.s.fParavirtTscEnabled;
|
---|
357 |
|
---|
358 | /*
|
---|
359 | * Same logic as in TMCpuTickCanUseRealTSC.
|
---|
360 | */
|
---|
361 | if (pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET)
|
---|
362 | {
|
---|
363 | /** @todo We should negate both deltas! It's soo weird that we do the
|
---|
364 | * exact opposite of what the hardware implements. */
|
---|
365 | #ifdef IN_RING3
|
---|
366 | *poffRealTsc = 0 - pVCpu->tm.s.offTSCRawSrc - SUPGetTscDelta();
|
---|
367 | #else
|
---|
368 | *poffRealTsc = 0 - pVCpu->tm.s.offTSCRawSrc - SUPGetTscDeltaByCpuSetIndex(pVCpu->iHostCpuSet);
|
---|
369 | #endif
|
---|
370 | *pfOffsettedTsc = true;
|
---|
371 | return tmCpuCalcTicksToDeadline(pVCpu, TMVirtualSyncGetNsToDeadline(pVM));
|
---|
372 | }
|
---|
373 |
|
---|
374 | /*
|
---|
375 | * Same logic as in TMCpuTickCanUseRealTSC.
|
---|
376 | */
|
---|
377 | if ( pVM->tm.s.enmTSCMode == TMTSCMODE_DYNAMIC
|
---|
378 | && !pVM->tm.s.fVirtualSyncCatchUp
|
---|
379 | && RT_LIKELY(pVM->tm.s.fVirtualSyncTicking)
|
---|
380 | && !pVM->tm.s.fVirtualWarpDrive)
|
---|
381 | {
|
---|
382 | /* The source is the timer synchronous virtual clock. */
|
---|
383 | uint64_t cNsToDeadline;
|
---|
384 | uint64_t u64NowVirtSync = TMVirtualSyncGetWithDeadlineNoCheck(pVM, &cNsToDeadline);
|
---|
385 | uint64_t u64Now = ASMMultU64ByU32DivByU32(u64NowVirtSync, pVM->tm.s.cTSCTicksPerSecond, TMCLOCK_FREQ_VIRTUAL);
|
---|
386 | u64Now -= pVCpu->tm.s.offTSCRawSrc;
|
---|
387 | *poffRealTsc = u64Now - ASMReadTSC();
|
---|
388 | *pfOffsettedTsc = u64Now >= pVCpu->tm.s.u64TSCLastSeen;
|
---|
389 | return tmCpuCalcTicksToDeadline(pVCpu, cNsToDeadline);
|
---|
390 | }
|
---|
391 |
|
---|
392 | #ifdef VBOX_WITH_STATISTICS
|
---|
393 | tmCpuTickRecordOffsettedTscRefusal(pVM, pVCpu);
|
---|
394 | #endif
|
---|
395 | *pfOffsettedTsc = false;
|
---|
396 | *poffRealTsc = 0;
|
---|
397 | return tmCpuCalcTicksToDeadline(pVCpu, TMVirtualSyncGetNsToDeadline(pVM));
|
---|
398 | }
|
---|
399 |
|
---|
400 |
|
---|
401 | /**
|
---|
402 | * Read the current CPU timestamp counter.
|
---|
403 | *
|
---|
404 | * @returns Gets the CPU tsc.
|
---|
405 | * @param pVCpu The cross context virtual CPU structure.
|
---|
406 | * @param fCheckTimers Whether to check timers.
|
---|
407 | */
|
---|
408 | DECLINLINE(uint64_t) tmCpuTickGetInternal(PVMCPU pVCpu, bool fCheckTimers)
|
---|
409 | {
|
---|
410 | uint64_t u64;
|
---|
411 |
|
---|
412 | if (RT_LIKELY(pVCpu->tm.s.fTSCTicking))
|
---|
413 | {
|
---|
414 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
415 | if (pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET)
|
---|
416 | u64 = SUPReadTsc();
|
---|
417 | else
|
---|
418 | u64 = tmCpuTickGetRawVirtual(pVM, fCheckTimers);
|
---|
419 | u64 -= pVCpu->tm.s.offTSCRawSrc;
|
---|
420 |
|
---|
421 | /* Always return a value higher than what the guest has already seen. */
|
---|
422 | if (RT_LIKELY(u64 > pVCpu->tm.s.u64TSCLastSeen))
|
---|
423 | pVCpu->tm.s.u64TSCLastSeen = u64;
|
---|
424 | else
|
---|
425 | {
|
---|
426 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCUnderflow);
|
---|
427 | pVCpu->tm.s.u64TSCLastSeen += 64; /** @todo choose a good increment here */
|
---|
428 | u64 = pVCpu->tm.s.u64TSCLastSeen;
|
---|
429 | }
|
---|
430 | }
|
---|
431 | else
|
---|
432 | u64 = pVCpu->tm.s.u64TSC;
|
---|
433 | /** @todo @bugref{7243}: SVM TSC offset. */
|
---|
434 | return u64;
|
---|
435 | }
|
---|
436 |
|
---|
437 |
|
---|
438 | /**
|
---|
439 | * Read the current CPU timestamp counter.
|
---|
440 | *
|
---|
441 | * @returns Gets the CPU tsc.
|
---|
442 | * @param pVCpu The cross context virtual CPU structure.
|
---|
443 | */
|
---|
444 | VMMDECL(uint64_t) TMCpuTickGet(PVMCPU pVCpu)
|
---|
445 | {
|
---|
446 | return tmCpuTickGetInternal(pVCpu, true /* fCheckTimers */);
|
---|
447 | }
|
---|
448 |
|
---|
449 |
|
---|
450 | /**
|
---|
451 | * Read the current CPU timestamp counter, don't check for expired timers.
|
---|
452 | *
|
---|
453 | * @returns Gets the CPU tsc.
|
---|
454 | * @param pVCpu The cross context virtual CPU structure.
|
---|
455 | */
|
---|
456 | VMM_INT_DECL(uint64_t) TMCpuTickGetNoCheck(PVMCPU pVCpu)
|
---|
457 | {
|
---|
458 | return tmCpuTickGetInternal(pVCpu, false /* fCheckTimers */);
|
---|
459 | }
|
---|
460 |
|
---|
461 |
|
---|
462 | /**
|
---|
463 | * Sets the current CPU timestamp counter.
|
---|
464 | *
|
---|
465 | * @returns VBox status code.
|
---|
466 | * @param pVM The cross context VM structure.
|
---|
467 | * @param pVCpu The cross context virtual CPU structure.
|
---|
468 | * @param u64Tick The new timestamp value.
|
---|
469 | *
|
---|
470 | * @thread EMT which TSC is to be set.
|
---|
471 | */
|
---|
472 | VMM_INT_DECL(int) TMCpuTickSet(PVM pVM, PVMCPU pVCpu, uint64_t u64Tick)
|
---|
473 | {
|
---|
474 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
475 | STAM_COUNTER_INC(&pVM->tm.s.StatTSCSet);
|
---|
476 |
|
---|
477 | /*
|
---|
478 | * This is easier to do when the TSC is paused since resume will
|
---|
479 | * do all the calculations for us. Actually, we don't need to
|
---|
480 | * call tmCpuTickPause here since we overwrite u64TSC anyway.
|
---|
481 | */
|
---|
482 | bool fTSCTicking = pVCpu->tm.s.fTSCTicking;
|
---|
483 | pVCpu->tm.s.fTSCTicking = false;
|
---|
484 | pVCpu->tm.s.u64TSC = u64Tick;
|
---|
485 | pVCpu->tm.s.u64TSCLastSeen = u64Tick;
|
---|
486 | if (fTSCTicking)
|
---|
487 | tmCpuTickResume(pVM, pVCpu);
|
---|
488 | /** @todo Try help synchronizing it better among the virtual CPUs? */
|
---|
489 |
|
---|
490 | return VINF_SUCCESS;
|
---|
491 | }
|
---|
492 |
|
---|
493 | /**
|
---|
494 | * Sets the last seen CPU timestamp counter.
|
---|
495 | *
|
---|
496 | * @returns VBox status code.
|
---|
497 | * @param pVCpu The cross context virtual CPU structure.
|
---|
498 | * @param u64LastSeenTick The last seen timestamp value.
|
---|
499 | *
|
---|
500 | * @thread EMT which TSC is to be set.
|
---|
501 | */
|
---|
502 | VMM_INT_DECL(int) TMCpuTickSetLastSeen(PVMCPU pVCpu, uint64_t u64LastSeenTick)
|
---|
503 | {
|
---|
504 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
505 |
|
---|
506 | LogFlow(("TMCpuTickSetLastSeen %RX64\n", u64LastSeenTick));
|
---|
507 | if (pVCpu->tm.s.u64TSCLastSeen < u64LastSeenTick)
|
---|
508 | pVCpu->tm.s.u64TSCLastSeen = u64LastSeenTick;
|
---|
509 | return VINF_SUCCESS;
|
---|
510 | }
|
---|
511 |
|
---|
512 | /**
|
---|
513 | * Gets the last seen CPU timestamp counter of the guest.
|
---|
514 | *
|
---|
515 | * @returns the last seen TSC.
|
---|
516 | * @param pVCpu The cross context virtual CPU structure.
|
---|
517 | *
|
---|
518 | * @thread EMT(pVCpu).
|
---|
519 | */
|
---|
520 | VMM_INT_DECL(uint64_t) TMCpuTickGetLastSeen(PVMCPU pVCpu)
|
---|
521 | {
|
---|
522 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
523 |
|
---|
524 | return pVCpu->tm.s.u64TSCLastSeen;
|
---|
525 | }
|
---|
526 |
|
---|
527 |
|
---|
528 | /**
|
---|
529 | * Get the timestamp frequency.
|
---|
530 | *
|
---|
531 | * @returns Number of ticks per second.
|
---|
532 | * @param pVM The cross context VM structure.
|
---|
533 | */
|
---|
534 | VMMDECL(uint64_t) TMCpuTicksPerSecond(PVM pVM)
|
---|
535 | {
|
---|
536 | if ( pVM->tm.s.enmTSCMode == TMTSCMODE_REAL_TSC_OFFSET
|
---|
537 | && g_pSUPGlobalInfoPage->u32Mode != SUPGIPMODE_INVARIANT_TSC)
|
---|
538 | {
|
---|
539 | #ifdef IN_RING3
|
---|
540 | uint64_t cTSCTicksPerSecond = SUPGetCpuHzFromGip(g_pSUPGlobalInfoPage);
|
---|
541 | #elif defined(IN_RING0)
|
---|
542 | uint64_t cTSCTicksPerSecond = SUPGetCpuHzFromGipBySetIndex(g_pSUPGlobalInfoPage, RTMpCpuIdToSetIndex(RTMpCpuId()));
|
---|
543 | #else
|
---|
544 | uint64_t cTSCTicksPerSecond = SUPGetCpuHzFromGipBySetIndex(g_pSUPGlobalInfoPage, VMMGetCpu(pVM)->iHostCpuSet);
|
---|
545 | #endif
|
---|
546 | if (RT_LIKELY(cTSCTicksPerSecond != ~(uint64_t)0))
|
---|
547 | return cTSCTicksPerSecond;
|
---|
548 | }
|
---|
549 | return pVM->tm.s.cTSCTicksPerSecond;
|
---|
550 | }
|
---|
551 |
|
---|
552 |
|
---|
553 | /**
|
---|
554 | * Whether the TSC is ticking for the VCPU.
|
---|
555 | *
|
---|
556 | * @returns true if ticking, false otherwise.
|
---|
557 | * @param pVCpu The cross context virtual CPU structure.
|
---|
558 | */
|
---|
559 | VMM_INT_DECL(bool) TMCpuTickIsTicking(PVMCPU pVCpu)
|
---|
560 | {
|
---|
561 | return pVCpu->tm.s.fTSCTicking;
|
---|
562 | }
|
---|
563 |
|
---|