1 | /* $Id: GIMKvmInternal.h 98980 2023-03-15 11:46:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * GIM - KVM, Internal header file.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2015-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef VMM_INCLUDED_SRC_include_GIMKvmInternal_h
|
---|
29 | #define VMM_INCLUDED_SRC_include_GIMKvmInternal_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <VBox/vmm/gim.h>
|
---|
35 | #include <VBox/vmm/cpum.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /** @name KVM base features.
|
---|
39 | * @{
|
---|
40 | */
|
---|
41 | /** Old, deprecated clock source available. */
|
---|
42 | #define GIM_KVM_BASE_FEAT_CLOCK_OLD RT_BIT(0)
|
---|
43 | /** No need for artifical delays on IO operations. */
|
---|
44 | #define GIM_KVM_BASE_FEAT_NOP_IO_DELAY RT_BIT(1)
|
---|
45 | /** MMU op supported (deprecated, unused). */
|
---|
46 | #define GIM_KVM_BASE_FEAT_MMU_OP RT_BIT(2)
|
---|
47 | /** Clock source available. */
|
---|
48 | #define GIM_KVM_BASE_FEAT_CLOCK RT_BIT(3)
|
---|
49 | /** Asynchronous page faults supported. */
|
---|
50 | #define GIM_KVM_BASE_FEAT_ASYNC_PF RT_BIT(4)
|
---|
51 | /** Steal time (VCPU not executing guest code time in ns) available. */
|
---|
52 | #define GIM_KVM_BASE_FEAT_STEAL_TIME RT_BIT(5)
|
---|
53 | /** Paravirtualized EOI (end-of-interrupt) supported. */
|
---|
54 | #define GIM_KVM_BASE_FEAT_PV_EOI RT_BIT(6)
|
---|
55 | /** Paravirtualized spinlock (unhalting VCPU) supported. */
|
---|
56 | #define GIM_KVM_BASE_FEAT_PV_UNHALT RT_BIT(7)
|
---|
57 | /** The TSC is stable (fixed rate, monotonic). */
|
---|
58 | #define GIM_KVM_BASE_FEAT_TSC_STABLE RT_BIT(24)
|
---|
59 | /** @} */
|
---|
60 |
|
---|
61 |
|
---|
62 | /** @name KVM MSRs.
|
---|
63 | * @{
|
---|
64 | */
|
---|
65 | /** Start of range 0. */
|
---|
66 | #define MSR_GIM_KVM_RANGE0_FIRST UINT32_C(0x11)
|
---|
67 | /** Old, deprecated wall clock. */
|
---|
68 | #define MSR_GIM_KVM_WALL_CLOCK_OLD UINT32_C(0x11)
|
---|
69 | /** Old, deprecated System time. */
|
---|
70 | #define MSR_GIM_KVM_SYSTEM_TIME_OLD UINT32_C(0x12)
|
---|
71 | /** End of range 0. */
|
---|
72 | #define MSR_GIM_KVM_RANGE0_LAST MSR_GIM_KVM_SYSTEM_TIME_OLD
|
---|
73 |
|
---|
74 | /** Start of range 1. */
|
---|
75 | #define MSR_GIM_KVM_RANGE1_FIRST UINT32_C(0x4b564d00)
|
---|
76 | /** Wall clock. */
|
---|
77 | #define MSR_GIM_KVM_WALL_CLOCK UINT32_C(0x4b564d00)
|
---|
78 | /** System time. */
|
---|
79 | #define MSR_GIM_KVM_SYSTEM_TIME UINT32_C(0x4b564d01)
|
---|
80 | /** Asynchronous page fault. */
|
---|
81 | #define MSR_GIM_KVM_ASYNC_PF UINT32_C(0x4b564d02)
|
---|
82 | /** Steal time. */
|
---|
83 | #define MSR_GIM_KVM_STEAL_TIME UINT32_C(0x4b564d03)
|
---|
84 | /** Paravirtualized EOI (end-of-interrupt). */
|
---|
85 | #define MSR_GIM_KVM_EOI UINT32_C(0x4b564d04)
|
---|
86 | /** End of range 1. */
|
---|
87 | #define MSR_GIM_KVM_RANGE1_LAST MSR_GIM_KVM_EOI
|
---|
88 |
|
---|
89 | AssertCompile(MSR_GIM_KVM_RANGE0_FIRST <= MSR_GIM_KVM_RANGE0_LAST);
|
---|
90 | AssertCompile(MSR_GIM_KVM_RANGE1_FIRST <= MSR_GIM_KVM_RANGE1_LAST);
|
---|
91 | /** @} */
|
---|
92 |
|
---|
93 | /** KVM page size. */
|
---|
94 | #define GIM_KVM_PAGE_SIZE 0x1000
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * MMIO2 region indices.
|
---|
98 | */
|
---|
99 | /** The system time page(s) region. */
|
---|
100 | #define GIM_KVM_SYSTEM_TIME_PAGE_REGION_IDX UINT8_C(0)
|
---|
101 | /** The steal time page(s) region. */
|
---|
102 | #define GIM_KVM_STEAL_TIME_PAGE_REGION_IDX UINT8_C(1)
|
---|
103 | /** The maximum region index (must be <= UINT8_MAX). */
|
---|
104 | #define GIM_KVM_REGION_IDX_MAX GIM_KVM_STEAL_TIME_PAGE_REGION_IDX
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * KVM system-time structure (GIM_KVM_SYSTEM_TIME_FLAGS_XXX) flags.
|
---|
108 | * See "Documentation/virtual/kvm/api.txt".
|
---|
109 | */
|
---|
110 | /** The TSC is stable (monotonic). */
|
---|
111 | #define GIM_KVM_SYSTEM_TIME_FLAGS_TSC_STABLE RT_BIT(0)
|
---|
112 | /** The guest VCPU has been paused by the hypervisor. */
|
---|
113 | #define GIM_KVM_SYSTEM_TIME_FLAGS_GUEST_PAUSED RT_BIT(1)
|
---|
114 | /** */
|
---|
115 |
|
---|
116 | /** @name KVM MSR - System time (MSR_GIM_KVM_SYSTEM_TIME and
|
---|
117 | * MSR_GIM_KVM_SYSTEM_TIME_OLD).
|
---|
118 | * @{
|
---|
119 | */
|
---|
120 | /** The system-time enable bit. */
|
---|
121 | #define MSR_GIM_KVM_SYSTEM_TIME_ENABLE_BIT RT_BIT_64(0)
|
---|
122 | /** Whether the system-time struct. is enabled or not. */
|
---|
123 | #define MSR_GIM_KVM_SYSTEM_TIME_IS_ENABLED(a) RT_BOOL((a) & MSR_GIM_KVM_SYSTEM_TIME_ENABLE_BIT)
|
---|
124 | /** Guest-physical address of the system-time struct. */
|
---|
125 | #define MSR_GIM_KVM_SYSTEM_TIME_GUEST_GPA(a) ((a) & ~MSR_GIM_KVM_SYSTEM_TIME_ENABLE_BIT)
|
---|
126 | /** @} */
|
---|
127 |
|
---|
128 | /** @name KVM MSR - Wall clock (MSR_GIM_KVM_WALL_CLOCK and
|
---|
129 | * MSR_GIM_KVM_WALL_CLOCK_OLD).
|
---|
130 | * @{
|
---|
131 | */
|
---|
132 | /** Guest-physical address of the wall-clock struct. */
|
---|
133 | #define MSR_GIM_KVM_WALL_CLOCK_GUEST_GPA(a) (a)
|
---|
134 | /** @} */
|
---|
135 |
|
---|
136 |
|
---|
137 | /** @name KVM Hypercall operations.
|
---|
138 | * @{ */
|
---|
139 | #define KVM_HYPERCALL_OP_VAPIC_POLL_IRQ 1
|
---|
140 | #define KVM_HYPERCALL_OP_MMU 2
|
---|
141 | #define KVM_HYPERCALL_OP_FEATURES 3
|
---|
142 | #define KVM_HYPERCALL_OP_KICK_CPU 5
|
---|
143 | /** @} */
|
---|
144 |
|
---|
145 | /** @name KVM Hypercall return values.
|
---|
146 | * @{ */
|
---|
147 | /* Return values for hypercalls */
|
---|
148 | #define KVM_HYPERCALL_RET_SUCCESS 0
|
---|
149 | #define KVM_HYPERCALL_RET_ENOSYS (uint64_t)(-1000)
|
---|
150 | #define KVM_HYPERCALL_RET_EFAULT (uint64_t)(-14)
|
---|
151 | #define KVM_HYPERCALL_RET_E2BIG (uint64_t)(-7)
|
---|
152 | #define KVM_HYPERCALL_RET_EPERM (uint64_t)(-1)
|
---|
153 | /** @} */
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * KVM per-VCPU system-time structure.
|
---|
157 | */
|
---|
158 | typedef struct GIMKVMSYSTEMTIME
|
---|
159 | {
|
---|
160 | /** Version (sequence number). */
|
---|
161 | uint32_t u32Version;
|
---|
162 | /** Alignment padding. */
|
---|
163 | uint32_t u32Padding0;
|
---|
164 | /** TSC time stamp. */
|
---|
165 | uint64_t u64Tsc;
|
---|
166 | /** System time in nanoseconds. */
|
---|
167 | uint64_t u64NanoTS;
|
---|
168 | /** TSC to system time scale factor. */
|
---|
169 | uint32_t u32TscScale;
|
---|
170 | /** TSC frequency shift. */
|
---|
171 | int8_t i8TscShift;
|
---|
172 | /** Clock source (GIM_KVM_SYSTEM_TIME_FLAGS_XXX) flags. */
|
---|
173 | uint8_t fFlags;
|
---|
174 | /** Alignment padding. */
|
---|
175 | uint8_t abPadding0[2];
|
---|
176 | } GIMKVMSYSTEMTIME;
|
---|
177 | /** Pointer to KVM system-time struct. */
|
---|
178 | typedef GIMKVMSYSTEMTIME *PGIMKVMSYSTEMTIME;
|
---|
179 | /** Pointer to a const KVM system-time struct. */
|
---|
180 | typedef GIMKVMSYSTEMTIME const *PCGIMKVMSYSTEMTIME;
|
---|
181 | AssertCompileSize(GIMKVMSYSTEMTIME, 32);
|
---|
182 |
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * KVM per-VM wall-clock structure.
|
---|
186 | */
|
---|
187 | typedef struct GIMKVMWALLCLOCK
|
---|
188 | {
|
---|
189 | /** Version (sequence number). */
|
---|
190 | uint32_t u32Version;
|
---|
191 | /** Number of seconds since boot. */
|
---|
192 | uint32_t u32Sec;
|
---|
193 | /** Number of nanoseconds since boot. */
|
---|
194 | uint32_t u32Nano;
|
---|
195 | } GIMKVMWALLCLOCK;
|
---|
196 | /** Pointer to KVM wall-clock struct. */
|
---|
197 | typedef GIMKVMWALLCLOCK *PGIMKVMWALLCLOCK;
|
---|
198 | /** Pointer to a const KVM wall-clock struct. */
|
---|
199 | typedef GIMKVMWALLCLOCK const *PCGIMKVMWALLCLOCK;
|
---|
200 | AssertCompileSize(GIMKVMWALLCLOCK, 12);
|
---|
201 |
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * GIM KVM VM instance data.
|
---|
205 | * Changes to this must checked against the padding of the gim union in VM!
|
---|
206 | */
|
---|
207 | typedef struct GIMKVM
|
---|
208 | {
|
---|
209 | /** Wall-clock MSR. */
|
---|
210 | uint64_t u64WallClockMsr;
|
---|
211 | /** CPUID features: Basic. */
|
---|
212 | uint32_t uBaseFeat;
|
---|
213 | /** Whether GIM needs to trap \#UD exceptions. */
|
---|
214 | bool fTrapXcptUD;
|
---|
215 | /** Disassembler opcode of hypercall instruction native for this host CPU. */
|
---|
216 | uint16_t uOpcodeNative;
|
---|
217 | /** Native hypercall opcode bytes. Use for replacing. */
|
---|
218 | uint8_t abOpcodeNative[3];
|
---|
219 | /** Alignment padding. */
|
---|
220 | uint8_t abPadding[5];
|
---|
221 | /** The TSC frequency (in HZ) reported to the guest. */
|
---|
222 | uint64_t cTscTicksPerSecond;
|
---|
223 | } GIMKVM;
|
---|
224 | /** Pointer to per-VM GIM KVM instance data. */
|
---|
225 | typedef GIMKVM *PGIMKVM;
|
---|
226 | /** Pointer to const per-VM GIM KVM instance data. */
|
---|
227 | typedef GIMKVM const *PCGIMKVM;
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * GIM KVMV VCPU instance data.
|
---|
231 | * Changes to this must checked against the padding of the gim union in VMCPU!
|
---|
232 | */
|
---|
233 | typedef struct GIMKVMCPU
|
---|
234 | {
|
---|
235 | /** System-time MSR. */
|
---|
236 | uint64_t u64SystemTimeMsr;
|
---|
237 | /** The guest-physical address of the system-time struct. */
|
---|
238 | RTGCPHYS GCPhysSystemTime;
|
---|
239 | /** The version (sequence number) of the system-time struct. */
|
---|
240 | uint32_t u32SystemTimeVersion;
|
---|
241 | /** The guest TSC value while enabling the system-time MSR. */
|
---|
242 | uint64_t uTsc;
|
---|
243 | /** The guest virtual time while enabling the system-time MSR. */
|
---|
244 | uint64_t uVirtNanoTS;
|
---|
245 | /** The flags of the system-time struct. */
|
---|
246 | uint8_t fSystemTimeFlags;
|
---|
247 | } GIMKVMCPU;
|
---|
248 | /** Pointer to per-VCPU GIM KVM instance data. */
|
---|
249 | typedef GIMKVMCPU *PGIMKVMCPU;
|
---|
250 | /** Pointer to const per-VCPU GIM KVM instance data. */
|
---|
251 | typedef GIMKVMCPU const *PCGIMKVMCPU;
|
---|
252 |
|
---|
253 |
|
---|
254 | RT_C_DECLS_BEGIN
|
---|
255 |
|
---|
256 | #ifdef IN_RING3
|
---|
257 | VMMR3_INT_DECL(int) gimR3KvmInit(PVM pVM);
|
---|
258 | VMMR3_INT_DECL(int) gimR3KvmInitCompleted(PVM pVM);
|
---|
259 | VMMR3_INT_DECL(int) gimR3KvmTerm(PVM pVM);
|
---|
260 | VMMR3_INT_DECL(void) gimR3KvmRelocate(PVM pVM, RTGCINTPTR offDelta);
|
---|
261 | VMMR3_INT_DECL(void) gimR3KvmReset(PVM pVM);
|
---|
262 | VMMR3_INT_DECL(int) gimR3KvmSave(PVM pVM, PSSMHANDLE pSSM);
|
---|
263 | VMMR3_INT_DECL(int) gimR3KvmLoad(PVM pVM, PSSMHANDLE pSSM);
|
---|
264 |
|
---|
265 | VMMR3_INT_DECL(int) gimR3KvmDisableSystemTime(PVM pVM);
|
---|
266 | VMMR3_INT_DECL(int) gimR3KvmEnableSystemTime(PVM pVM, PVMCPU pVCpu, uint64_t uMsrSystemTime);
|
---|
267 | VMMR3_INT_DECL(int) gimR3KvmEnableWallClock(PVM pVM, RTGCPHYS GCPhysSysTime);
|
---|
268 | #endif /* IN_RING3 */
|
---|
269 |
|
---|
270 | VMM_INT_DECL(bool) gimKvmIsParavirtTscEnabled(PVMCC pVM);
|
---|
271 | VMM_INT_DECL(bool) gimKvmAreHypercallsEnabled(PVMCPU pVCpu);
|
---|
272 | VMM_INT_DECL(VBOXSTRICTRC) gimKvmHypercall(PVMCPUCC pVCpu, PCPUMCTX pCtx);
|
---|
273 | #if !defined(VBOX_VMM_TARGET_ARMV8)
|
---|
274 | VMM_INT_DECL(VBOXSTRICTRC) gimKvmReadMsr(PVMCPUCC pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t *puValue);
|
---|
275 | VMM_INT_DECL(VBOXSTRICTRC) gimKvmWriteMsr(PVMCPUCC pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t uRawValue);
|
---|
276 | #endif
|
---|
277 | VMM_INT_DECL(bool) gimKvmShouldTrapXcptUD(PVM pVM);
|
---|
278 | VMM_INT_DECL(VBOXSTRICTRC) gimKvmXcptUD(PVMCC pVM, PVMCPUCC pVCpu, PCPUMCTX pCtx, PDISCPUSTATE pDis, uint8_t *pcbInstr);
|
---|
279 | VMM_INT_DECL(VBOXSTRICTRC) gimKvmHypercallEx(PVMCPUCC pVCpu, PCPUMCTX pCtx, unsigned uDisOpcode, uint8_t cbInstr);
|
---|
280 |
|
---|
281 | RT_C_DECLS_END
|
---|
282 |
|
---|
283 | #endif /* !VMM_INCLUDED_SRC_include_GIMKvmInternal_h */
|
---|
284 |
|
---|