VirtualBox

source: vbox/trunk/src/VBox/VMM/include/GIMKvmInternal.h@ 92780

Last change on this file since 92780 was 86121, checked in by vboxsync, 4 years ago

VMM/GIM: Fix handling KVM system-time struct. to work from both ring-0 and ring-3 (earlier it relied on MSR writes being handled in ring-0).

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