VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/GIMAllHv.cpp@ 54774

Last change on this file since 54774 was 54701, checked in by vboxsync, 10 years ago

VMM/GIM: nits.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 KB
Line 
1/* $Id: GIMAllHv.cpp 54701 2015-03-09 16:42:11Z vboxsync $ */
2/** @file
3 * GIM - Guest Interface Manager, Microsoft Hyper-V, All Contexts.
4 */
5
6/*
7 * Copyright (C) 2014-2015 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/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_GIM
22#include "GIMHvInternal.h"
23#include "GIMInternal.h"
24
25#include <VBox/err.h>
26#include <VBox/vmm/hm.h>
27#include <VBox/vmm/tm.h>
28#include <VBox/vmm/vm.h>
29#include <VBox/vmm/pgm.h>
30#include <VBox/vmm/pdmdev.h>
31#include <VBox/vmm/pdmapi.h>
32
33#include <iprt/asm-amd64-x86.h>
34#include <iprt/spinlock.h>
35
36
37/**
38 * Handles the Hyper-V hypercall.
39 *
40 * @returns VBox status code.
41 * @param pVCpu Pointer to the VMCPU.
42 * @param pCtx Pointer to the guest-CPU context.
43 */
44VMM_INT_DECL(int) gimHvHypercall(PVMCPU pVCpu, PCPUMCTX pCtx)
45{
46 PVM pVM = pVCpu->CTX_SUFF(pVM);
47 if (!MSR_GIM_HV_HYPERCALL_IS_ENABLED(pVM->gim.s.u.Hv.u64HypercallMsr))
48 return VERR_GIM_HYPERCALLS_NOT_ENABLED;
49
50 /** @todo Handle hypercalls. Fail for now */
51 return VERR_GIM_IPE_3;
52}
53
54
55/**
56 * Returns whether the guest has configured and enabled the use of Hyper-V's
57 * hypercall interface.
58 *
59 * @returns true if hypercalls are enabled, false otherwise.
60 * @param pVCpu Pointer to the VMCPU.
61 */
62VMM_INT_DECL(bool) gimHvAreHypercallsEnabled(PVMCPU pVCpu)
63{
64 return MSR_GIM_HV_HYPERCALL_IS_ENABLED(pVCpu->CTX_SUFF(pVM)->gim.s.u.Hv.u64HypercallMsr);
65}
66
67
68/**
69 * Returns whether the guest has configured and enabled the use of Hyper-V's
70 * paravirtualized TSC.
71 *
72 * @returns true if paravirt. TSC is enabled, false otherwise.
73 * @param pVM Pointer to the VM.
74 */
75VMM_INT_DECL(bool) gimHvIsParavirtTscEnabled(PVM pVM)
76{
77 return MSR_GIM_HV_REF_TSC_IS_ENABLED(pVM->gim.s.u.Hv.u64TscPageMsr);
78}
79
80
81/**
82 * MSR read handler for Hyper-V.
83 *
84 * @returns Strict VBox status code like CPUMQueryGuestMsr().
85 * @retval VINF_CPUM_R3_MSR_READ
86 * @retval VERR_CPUM_RAISE_GP_0
87 *
88 * @param pVCpu Pointer to the VMCPU.
89 * @param idMsr The MSR being read.
90 * @param pRange The range this MSR belongs to.
91 * @param puValue Where to store the MSR value read.
92 */
93VMM_INT_DECL(VBOXSTRICTRC) gimHvReadMsr(PVMCPU pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t *puValue)
94{
95 NOREF(pRange);
96 PVM pVM = pVCpu->CTX_SUFF(pVM);
97 PGIMHV pHv = &pVM->gim.s.u.Hv;
98
99 switch (idMsr)
100 {
101 case MSR_GIM_HV_TIME_REF_COUNT:
102 {
103 /* Hyper-V reports the time in 100 ns units (10 MHz). */
104 uint64_t u64Tsc = TMCpuTickGet(pVCpu);
105 uint64_t u64TscHz = TMCpuTicksPerSecond(pVM);
106 uint64_t u64Tsc100Ns = u64TscHz / UINT64_C(10000000); /* 100 ns */
107 *puValue = (u64Tsc / u64Tsc100Ns);
108 return VINF_SUCCESS;
109 }
110
111 case MSR_GIM_HV_VP_INDEX:
112 *puValue = pVCpu->idCpu;
113 return VINF_SUCCESS;
114
115 case MSR_GIM_HV_TPR:
116 PDMApicReadMSR(pVM, pVCpu->idCpu, 0x80, puValue);
117 return VINF_SUCCESS;
118
119 case MSR_GIM_HV_EOI:
120 PDMApicReadMSR(pVM, pVCpu->idCpu, 0x0B, puValue);
121 return VINF_SUCCESS;
122
123 case MSR_GIM_HV_ICR:
124 PDMApicReadMSR(pVM, pVCpu->idCpu, 0x30, puValue);
125 return VINF_SUCCESS;
126
127 case MSR_GIM_HV_GUEST_OS_ID:
128 *puValue = pHv->u64GuestOsIdMsr;
129 return VINF_SUCCESS;
130
131 case MSR_GIM_HV_HYPERCALL:
132 *puValue = pHv->u64HypercallMsr;
133 return VINF_SUCCESS;
134
135 case MSR_GIM_HV_REF_TSC:
136 *puValue = pHv->u64TscPageMsr;
137 return VINF_SUCCESS;
138
139 case MSR_GIM_HV_TSC_FREQ:
140 *puValue = TMCpuTicksPerSecond(pVM);
141 return VINF_SUCCESS;
142
143 case MSR_GIM_HV_APIC_FREQ:
144 {
145 int rc = PDMApicGetTimerFreq(pVM, puValue);
146 if (RT_FAILURE(rc))
147 return VERR_CPUM_RAISE_GP_0;
148 return VINF_SUCCESS;
149 }
150
151 case MSR_GIM_HV_RESET:
152 *puValue = 0;
153 return VINF_SUCCESS;
154
155 default:
156#ifdef IN_RING3
157 static uint32_t s_cTimes = 0;
158 if (s_cTimes++ < 20)
159 LogRel(("GIM: HyperV: Unknown/invalid RdMsr (%#x) -> #GP(0)\n", idMsr));
160#endif
161 LogFunc(("Unknown/invalid RdMsr (%#RX32) -> #GP(0)\n", idMsr));
162 break;
163 }
164
165 return VERR_CPUM_RAISE_GP_0;
166}
167
168
169/**
170 * MSR write handler for Hyper-V.
171 *
172 * @returns Strict VBox status code like CPUMSetGuestMsr().
173 * @retval VINF_CPUM_R3_MSR_WRITE
174 * @retval VERR_CPUM_RAISE_GP_0
175 *
176 * @param pVCpu Pointer to the VMCPU.
177 * @param idMsr The MSR being written.
178 * @param pRange The range this MSR belongs to.
179 * @param uRawValue The raw value with the ignored bits not masked.
180 */
181VMM_INT_DECL(VBOXSTRICTRC) gimHvWriteMsr(PVMCPU pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t uRawValue)
182{
183 NOREF(pRange);
184 PVM pVM = pVCpu->CTX_SUFF(pVM);
185 PGIMHV pHv = &pVM->gim.s.u.Hv;
186
187 switch (idMsr)
188 {
189 case MSR_GIM_HV_TPR:
190 PDMApicWriteMSR(pVM, pVCpu->idCpu, 0x80, uRawValue);
191 return VINF_SUCCESS;
192
193 case MSR_GIM_HV_EOI:
194 PDMApicWriteMSR(pVM, pVCpu->idCpu, 0x0B, uRawValue);
195 return VINF_SUCCESS;
196
197 case MSR_GIM_HV_ICR:
198 PDMApicWriteMSR(pVM, pVCpu->idCpu, 0x30, uRawValue);
199 return VINF_SUCCESS;
200
201 case MSR_GIM_HV_GUEST_OS_ID:
202 {
203#ifndef IN_RING3
204 return VINF_CPUM_R3_MSR_WRITE;
205#else
206 /* Disable the hypercall-page if 0 is written to this MSR. */
207 if (!uRawValue)
208 {
209 gimR3HvDisableHypercallPage(pVM);
210 pHv->u64HypercallMsr &= ~MSR_GIM_HV_HYPERCALL_ENABLE_BIT;
211 }
212 pHv->u64GuestOsIdMsr = uRawValue;
213 return VINF_SUCCESS;
214#endif /* IN_RING3 */
215 }
216
217 case MSR_GIM_HV_HYPERCALL:
218 {
219#ifndef IN_RING3
220 return VINF_CPUM_R3_MSR_WRITE;
221#else /* IN_RING3 */
222 /*
223 * For now ignore writes to the hypercall MSR (i.e. keeps it disabled).
224 * This is required to boot FreeBSD 10.1 (with Hyper-V enabled ofc),
225 * see @bugref{7270} comment #116.
226 */
227 return VINF_SUCCESS;
228# if 0
229 /* First, update all but the hypercall enable bit. */
230 pHv->u64HypercallMsr = (uRawValue & ~MSR_GIM_HV_HYPERCALL_ENABLE_BIT);
231
232 /* Hypercalls can only be enabled when the guest has set the Guest-OS Id Msr. */
233 bool fEnable = RT_BOOL(uRawValue & MSR_GIM_HV_HYPERCALL_ENABLE_BIT);
234 if ( fEnable
235 && !pHv->u64GuestOsIdMsr)
236 {
237 return VINF_SUCCESS;
238 }
239
240 /* Is the guest disabling the hypercall-page? Allow it regardless of the Guest-OS Id Msr. */
241 if (!fEnable)
242 {
243 gimR3HvDisableHypercallPage(pVM);
244 pHv->u64HypercallMsr = uRawValue;
245 return VINF_SUCCESS;
246 }
247
248 /* Enable the hypercall-page. */
249 RTGCPHYS GCPhysHypercallPage = MSR_GIM_HV_HYPERCALL_GUEST_PFN(uRawValue) << PAGE_SHIFT;
250 int rc = gimR3HvEnableHypercallPage(pVM, GCPhysHypercallPage);
251 if (RT_SUCCESS(rc))
252 {
253 pHv->u64HypercallMsr = uRawValue;
254 return VINF_SUCCESS;
255 }
256
257 return VERR_CPUM_RAISE_GP_0;
258# endif
259#endif /* IN_RING3 */
260 }
261
262 case MSR_GIM_HV_REF_TSC:
263 {
264#ifndef IN_RING3
265 return VINF_CPUM_R3_MSR_WRITE;
266#else /* IN_RING3 */
267 /* First, update all but the TSC-page enable bit. */
268 pHv->u64TscPageMsr = (uRawValue & ~MSR_GIM_HV_REF_TSC_ENABLE_BIT);
269
270 /* Is the guest disabling the TSC-page? */
271 bool fEnable = RT_BOOL(uRawValue & MSR_GIM_HV_REF_TSC_ENABLE_BIT);
272 if (!fEnable)
273 {
274 gimR3HvDisableTscPage(pVM);
275 pHv->u64TscPageMsr = uRawValue;
276 return VINF_SUCCESS;
277 }
278
279 /* Enable the TSC-page. */
280 RTGCPHYS GCPhysTscPage = MSR_GIM_HV_REF_TSC_GUEST_PFN(uRawValue) << PAGE_SHIFT;
281 int rc = gimR3HvEnableTscPage(pVM, GCPhysTscPage, false /* fUseThisTscSequence */, 0 /* uTscSequence */);
282 if (RT_SUCCESS(rc))
283 {
284 pHv->u64TscPageMsr = uRawValue;
285 return VINF_SUCCESS;
286 }
287
288 return VERR_CPUM_RAISE_GP_0;
289#endif /* IN_RING3 */
290 }
291
292 case MSR_GIM_HV_RESET:
293 {
294#ifndef IN_RING3
295 return VINF_CPUM_R3_MSR_WRITE;
296#else
297 if (MSR_GIM_HV_RESET_IS_SET(uRawValue))
298 {
299 LogRel(("GIM: HyperV: Reset initiated through MSR.\n"));
300 int rc = PDMDevHlpVMReset(pVM->gim.s.pDevInsR3);
301 AssertRC(rc);
302 }
303 /* else: Ignore writes to other bits. */
304 return VINF_SUCCESS;
305#endif /* IN_RING3 */
306 }
307
308 case MSR_GIM_HV_TIME_REF_COUNT: /* Read-only MSRs. */
309 case MSR_GIM_HV_VP_INDEX:
310 case MSR_GIM_HV_TSC_FREQ:
311 case MSR_GIM_HV_APIC_FREQ:
312 LogFunc(("WrMsr on read-only MSR %#RX32 -> #GP(0)\n", idMsr));
313 return VERR_CPUM_RAISE_GP_0;
314
315 default:
316#ifdef IN_RING3
317 static uint32_t s_cTimes = 0;
318 if (s_cTimes++ < 20)
319 LogRel(("GIM: HyperV: Unknown/invalid WrMsr (%#x,%#x`%08x) -> #GP(0)\n", idMsr, uRawValue & UINT64_C(0xffffffff00000000),
320 uRawValue & UINT64_C(0xffffffff)));
321#endif
322 LogFunc(("Unknown/invalid WrMsr (%#RX32,%#RX64) -> #GP(0)\n", idMsr, uRawValue));
323 break;
324 }
325
326 return VERR_CPUM_RAISE_GP_0;
327}
328
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette