VirtualBox

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

Last change on this file since 2018 was 1956, checked in by vboxsync, 18 years ago

Fixed a couple of issues with virtualized TSC related to pausing and state loading. Also implemented support for TSC frequencies up to 4GHz and changed the default to the host CPU (virtualized TSC again).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1/* $Id: TMAllCpu.cpp 1956 2007-04-05 15:26:03Z 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
37
38/**
39 * Gets the raw cpu tick from current virtual time.
40 */
41DECLINLINE(uint64_t) tmCpuTickGetRawVirtual(PVM pVM)
42{
43 uint64_t u64 = TMVirtualGet(pVM);
44 if (u64 != TMCLOCK_FREQ_VIRTUAL)
45 u64 = ASMMultU64ByU32DivByU32(u64, pVM->tm.s.cTSCTicksPerSecond, TMCLOCK_FREQ_VIRTUAL);
46 return u64;
47}
48
49
50/**
51 * Resumes the CPU timestamp counter ticking.
52 *
53 * @returns VBox status code.
54 * @param pVM The VM to operate on.
55 */
56TMDECL(int) TMCpuTickResume(PVM pVM)
57{
58 if (!pVM->tm.s.fTSCTicking)
59 {
60 pVM->tm.s.fTSCTicking = true;
61 if (pVM->tm.s.fTSCVirtualized)
62 {
63 if (pVM->tm.s.fTSCUseRealTSC)
64 pVM->tm.s.u64TSCOffset = ASMReadTSC() - pVM->tm.s.u64TSC;
65 else
66 pVM->tm.s.u64TSCOffset = tmCpuTickGetRawVirtual(pVM) - pVM->tm.s.u64TSC;
67 }
68 return VINF_SUCCESS;
69 }
70 AssertFailed();
71 return VERR_INTERNAL_ERROR;
72}
73
74
75/**
76 * Pauses the CPU timestamp counter ticking.
77 *
78 * @returns VBox status code.
79 * @param pVM The VM to operate on.
80 */
81TMDECL(int) TMCpuTickPause(PVM pVM)
82{
83 if (pVM->tm.s.fTSCTicking)
84 {
85 pVM->tm.s.u64TSC = TMCpuTickGet(pVM);
86 pVM->tm.s.fTSCTicking = false;
87 return VINF_SUCCESS;
88 }
89 AssertFailed();
90 return VERR_INTERNAL_ERROR;
91}
92
93
94
95/**
96 * Read the current CPU timstamp counter.
97 *
98 * @returns Gets the CPU tsc.
99 * @param pVM The VM to operate on.
100 */
101TMDECL(uint64_t) TMCpuTickGet(PVM pVM)
102{
103 uint64_t u64;
104 if (RT_LIKELY(pVM->tm.s.fTSCTicking))
105 {
106 if (pVM->tm.s.fTSCVirtualized)
107 {
108 if (pVM->tm.s.fTSCUseRealTSC)
109 u64 = ASMReadTSC();
110 else
111 u64 = tmCpuTickGetRawVirtual(pVM);
112 u64 -= pVM->tm.s.u64TSCOffset;
113 }
114 else
115 u64 = ASMReadTSC();
116 }
117 else
118 u64 = pVM->tm.s.u64TSC;
119 return u64;
120}
121
122
123/**
124 * Sets the current CPU timestamp counter.
125 *
126 * @returns VBox status code.
127 * @param pVM The VM to operate on.
128 * @param u64Tick The new timestamp value.
129 */
130TMDECL(int) TMCpuTickSet(PVM pVM, uint64_t u64Tick)
131{
132 Assert(!pVM->tm.s.fTSCTicking);
133 pVM->tm.s.u64TSC = u64Tick;
134 return VINF_SUCCESS;
135}
136
137
138/**
139 * Get the timestamp frequency.
140 *
141 * @returns Number of ticks per second.
142 * @param pVM The VM.
143 */
144TMDECL(uint64_t) TMCpuTicksPerSecond(PVM pVM)
145{
146 if (pVM->tm.s.fTSCUseRealTSC)
147 {
148 uint64_t cTSCTicksPerSecond = SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
149 if (RT_LIKELY(cTSCTicksPerSecond != ~(uint64_t)0))
150 return cTSCTicksPerSecond;
151 }
152 return pVM->tm.s.cTSCTicksPerSecond;
153}
154
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