VirtualBox

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

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

logging

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
1/* $Id: TMAllCpu.cpp 2069 2007-04-13 10:02:12Z 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)
43{
44 uint64_t u64 = TMVirtualGet(pVM);
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) - pVM->tm.s.u64TSC;
68 }
69 return VINF_SUCCESS;
70 }
71 AssertFailed();
72 return VERR_INTERNAL_ERROR;
73}
74
75
76/**
77 * Pauses the CPU timestamp counter ticking.
78 *
79 * @returns VBox status code.
80 * @param pVM The VM to operate on.
81 */
82TMDECL(int) TMCpuTickPause(PVM pVM)
83{
84 if (pVM->tm.s.fTSCTicking)
85 {
86 pVM->tm.s.u64TSC = TMCpuTickGet(pVM);
87 pVM->tm.s.fTSCTicking = false;
88 return VINF_SUCCESS;
89 }
90 AssertFailed();
91 return VERR_INTERNAL_ERROR;
92}
93
94
95/**
96 * Returns the TSC offset
97 *
98 * @returns TSC ofset
99 * @param pVM The VM to operate on.
100 */
101TMDECL(uint64_t) TMCpuTickGetOffset(PVM pVM)
102{
103 return pVM->tm.s.u64TSCOffset;
104}
105
106
107/**
108 * Read the current CPU timstamp counter.
109 *
110 * @returns Gets the CPU tsc.
111 * @param pVM The VM to operate on.
112 */
113TMDECL(uint64_t) TMCpuTickGet(PVM pVM)
114{
115 uint64_t u64;
116 if (RT_LIKELY(pVM->tm.s.fTSCTicking))
117 {
118 if (pVM->tm.s.fTSCVirtualized)
119 {
120 if (pVM->tm.s.fTSCUseRealTSC)
121 u64 = ASMReadTSC();
122 else
123 u64 = tmCpuTickGetRawVirtual(pVM);
124 u64 -= pVM->tm.s.u64TSCOffset;
125 }
126 else
127 u64 = ASMReadTSC();
128 }
129 else
130 u64 = pVM->tm.s.u64TSC;
131 return u64;
132}
133
134
135/**
136 * Sets the current CPU timestamp counter.
137 *
138 * @returns VBox status code.
139 * @param pVM The VM to operate on.
140 * @param u64Tick The new timestamp value.
141 */
142TMDECL(int) TMCpuTickSet(PVM pVM, uint64_t u64Tick)
143{
144 Assert(!pVM->tm.s.fTSCTicking);
145 pVM->tm.s.u64TSC = u64Tick;
146 return VINF_SUCCESS;
147}
148
149
150/**
151 * Get the timestamp frequency.
152 *
153 * @returns Number of ticks per second.
154 * @param pVM The VM.
155 */
156TMDECL(uint64_t) TMCpuTicksPerSecond(PVM pVM)
157{
158 if (pVM->tm.s.fTSCUseRealTSC)
159 {
160 uint64_t cTSCTicksPerSecond = SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
161 if (RT_LIKELY(cTSCTicksPerSecond != ~(uint64_t)0))
162 return cTSCTicksPerSecond;
163 }
164 return pVM->tm.s.cTSCTicksPerSecond;
165}
166
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