VirtualBox

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

Last change on this file since 2128 was 2081, checked in by vboxsync, 18 years ago

don't check for pending timers on resume either.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1/* $Id: TMAllCpu.cpp 2081 2007-04-13 16:14:48Z vboxsync $ */
2/** @file
3 * TM - Timeout Manager, CPU Time, All Contexts.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung 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 = TMVirtualGetEx(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 */
102TMDECL(uint64_t) TMCpuTickGetOffset(PVM pVM)
103{
104 uint64_t u64;
105 if (RT_LIKELY(pVM->tm.s.fTSCTicking))
106 {
107 if (pVM->tm.s.fTSCVirtualized)
108 {
109 if (pVM->tm.s.fTSCUseRealTSC)
110 u64 = ASMReadTSC();
111 else
112 u64 = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */);
113 u64 -= pVM->tm.s.u64TSCOffset;
114 }
115 else
116 u64 = ASMReadTSC();
117 }
118 else
119 u64 = pVM->tm.s.u64TSC;
120
121 return u64 - ASMReadTSC();
122}
123
124
125/**
126 * Read the current CPU timstamp counter.
127 *
128 * @returns Gets the CPU tsc.
129 * @param pVM The VM to operate on.
130 */
131TMDECL(uint64_t) TMCpuTickGet(PVM pVM)
132{
133 uint64_t u64;
134 if (RT_LIKELY(pVM->tm.s.fTSCTicking))
135 {
136 if (pVM->tm.s.fTSCVirtualized)
137 {
138 if (pVM->tm.s.fTSCUseRealTSC)
139 u64 = ASMReadTSC();
140 else
141 u64 = tmCpuTickGetRawVirtual(pVM, true /* check for pending timers */);
142 u64 -= pVM->tm.s.u64TSCOffset;
143 }
144 else
145 u64 = ASMReadTSC();
146 }
147 else
148 u64 = pVM->tm.s.u64TSC;
149 return u64;
150}
151
152
153/**
154 * Sets the current CPU timestamp counter.
155 *
156 * @returns VBox status code.
157 * @param pVM The VM to operate on.
158 * @param u64Tick The new timestamp value.
159 */
160TMDECL(int) TMCpuTickSet(PVM pVM, uint64_t u64Tick)
161{
162 Assert(!pVM->tm.s.fTSCTicking);
163 pVM->tm.s.u64TSC = u64Tick;
164 return VINF_SUCCESS;
165}
166
167
168/**
169 * Get the timestamp frequency.
170 *
171 * @returns Number of ticks per second.
172 * @param pVM The VM.
173 */
174TMDECL(uint64_t) TMCpuTicksPerSecond(PVM pVM)
175{
176 if (pVM->tm.s.fTSCUseRealTSC)
177 {
178 uint64_t cTSCTicksPerSecond = SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
179 if (RT_LIKELY(cTSCTicksPerSecond != ~(uint64_t)0))
180 return cTSCTicksPerSecond;
181 }
182 return pVM->tm.s.cTSCTicksPerSecond;
183}
184
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