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