VirtualBox

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

Last change on this file since 61683 was 61632, checked in by vboxsync, 9 years ago

GIM: Correct header order to match what is use *everywhere* else in the VMM and what is absolutely necessary for some of the tricks we use (like the CPUM and DBGF read-only data!).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.2 KB
Line 
1/* $Id: GIMAllHv.cpp 61632 2016-06-09 18:06:26Z 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/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_GIM
23#include <VBox/vmm/gim.h>
24#include <VBox/vmm/em.h>
25#include <VBox/vmm/hm.h>
26#include <VBox/vmm/tm.h>
27#include <VBox/vmm/dbgf.h>
28#include <VBox/vmm/pdmdev.h>
29#include <VBox/vmm/pdmapi.h>
30#include <VBox/vmm/pgm.h>
31#include "GIMHvInternal.h"
32#include "GIMInternal.h"
33#include <VBox/vmm/vm.h>
34
35#include <VBox/err.h>
36
37#include <iprt/asm-amd64-x86.h>
38#ifdef IN_RING3
39# include <iprt/mem.h>
40#endif
41
42
43#ifdef IN_RING3
44/**
45 * Read and validate slow hypercall parameters.
46 *
47 * @returns VBox status code.
48 * @param pVM The cross context VM structure.
49 * @param pCtx Pointer to the guest-CPU context.
50 * @param fIs64BitMode Whether the guest is currently in 64-bit mode or not.
51 * @param enmParam The hypercall parameter type.
52 * @param prcHv Where to store the Hyper-V status code. Only valid
53 * to the caller when this function returns
54 * VINF_SUCCESS.
55 */
56static int gimHvReadSlowHypercallParam(PVM pVM, PCPUMCTX pCtx, bool fIs64BitMode, GIMHVHYPERCALLPARAM enmParam, int *prcHv)
57{
58 int rc = VINF_SUCCESS;
59 PGIMHV pHv = &pVM->gim.s.u.Hv;
60 RTGCPHYS GCPhysParam;
61 void *pvDst;
62 if (enmParam == GIMHVHYPERCALLPARAM_IN)
63 {
64 GCPhysParam = fIs64BitMode ? pCtx->rdx : (pCtx->rbx << 32) | pCtx->ecx;
65 pvDst = pHv->pbHypercallIn;
66 pHv->GCPhysHypercallIn = GCPhysParam;
67 }
68 else
69 {
70 GCPhysParam = fIs64BitMode ? pCtx->r8 : (pCtx->rdi << 32) | pCtx->esi;
71 pvDst = pHv->pbHypercallOut;
72 pHv->GCPhysHypercallOut = GCPhysParam;
73 Assert(enmParam == GIMHVHYPERCALLPARAM_OUT);
74 }
75
76 const char *pcszParam = enmParam == GIMHVHYPERCALLPARAM_IN ? "input" : "output"; NOREF(pcszParam);
77 if (RT_ALIGN_64(GCPhysParam, 8) == GCPhysParam)
78 {
79 if (PGMPhysIsGCPhysNormal(pVM, GCPhysParam))
80 {
81 rc = PGMPhysSimpleReadGCPhys(pVM, pvDst, GCPhysParam, GIM_HV_PAGE_SIZE);
82 if (RT_SUCCESS(rc))
83 {
84 *prcHv = GIM_HV_STATUS_SUCCESS;
85 return VINF_SUCCESS;
86 }
87 LogRel(("GIM: HyperV: Failed reading %s param at %#RGp. rc=%Rrc\n", pcszParam, GCPhysParam, rc));
88 rc = VERR_GIM_HYPERCALL_MEMORY_READ_FAILED;
89 }
90 else
91 {
92 Log(("GIM: HyperV: Invalid %s param address %#RGp\n", pcszParam, GCPhysParam));
93 *prcHv = GIM_HV_STATUS_INVALID_PARAMETER;
94 }
95 }
96 else
97 {
98 Log(("GIM: HyperV: Misaligned %s param address %#RGp\n", pcszParam, GCPhysParam));
99 *prcHv = GIM_HV_STATUS_INVALID_ALIGNMENT;
100 }
101 return rc;
102}
103
104
105/**
106 * Helper for reading and validating slow hypercall input and output parameters.
107 *
108 * @returns VBox status code.
109 * @param pVM The cross context VM structure.
110 * @param pCtx Pointer to the guest-CPU context.
111 * @param fIs64BitMode Whether the guest is currently in 64-bit mode or not.
112 * @param prcHv Where to store the Hyper-V status code. Only valid
113 * to the caller when this function returns
114 * VINF_SUCCESS.
115 */
116static int gimHvReadSlowHypercallParamsInOut(PVM pVM, PCPUMCTX pCtx, bool fIs64BitMode, int *prcHv)
117{
118 int rc = gimHvReadSlowHypercallParam(pVM, pCtx, fIs64BitMode, GIMHVHYPERCALLPARAM_IN, prcHv);
119 if ( RT_SUCCESS(rc)
120 && *prcHv == GIM_HV_STATUS_SUCCESS)
121 rc = gimHvReadSlowHypercallParam(pVM, pCtx, fIs64BitMode, GIMHVHYPERCALLPARAM_OUT, prcHv);
122 return rc;
123}
124#endif
125
126
127/**
128 * Handles all Hyper-V hypercalls.
129 *
130 * @returns Strict VBox status code.
131 * @retval VINF_SUCCESS if the hypercall succeeded (even if its operation
132 * failed).
133 * @retval VINF_GIM_R3_HYPERCALL re-start the hypercall from ring-3.
134 * @retval VERR_GIM_HYPERCALLS_NOT_ENABLED hypercalls are disabled by the
135 * guest.
136 * @retval VERR_GIM_HYPERCALL_ACCESS_DENIED CPL is insufficient.
137 * @retval VERR_GIM_HYPERCALL_MEMORY_READ_FAILED hypercall failed while reading
138 * memory.
139 * @retval VERR_GIM_HYPERCALL_MEMORY_WRITE_FAILED hypercall failed while
140 * writing memory.
141 *
142 * @param pVCpu The cross context virtual CPU structure.
143 * @param pCtx Pointer to the guest-CPU context.
144 *
145 * @thread EMT(pVCpu).
146 */
147VMM_INT_DECL(VBOXSTRICTRC) gimHvHypercall(PVMCPU pVCpu, PCPUMCTX pCtx)
148{
149 VMCPU_ASSERT_EMT(pVCpu);
150
151#ifndef IN_RING3
152 return VINF_GIM_R3_HYPERCALL;
153#else
154 PVM pVM = pVCpu->CTX_SUFF(pVM);
155 STAM_REL_COUNTER_INC(&pVM->gim.s.StatHypercalls);
156
157 /*
158 * Verify that hypercalls are enabled by the guest.
159 */
160 if (!gimHvAreHypercallsEnabled(pVCpu))
161 return VERR_GIM_HYPERCALLS_NOT_ENABLED;
162
163 /*
164 * Verify guest is in ring-0 protected mode.
165 */
166 uint32_t uCpl = CPUMGetGuestCPL(pVCpu);
167 if ( uCpl
168 || CPUMIsGuestInRealModeEx(pCtx))
169 {
170 return VERR_GIM_HYPERCALL_ACCESS_DENIED;
171 }
172
173 /*
174 * Get the hypercall operation code and modes.
175 */
176 const bool fIs64BitMode = CPUMIsGuestIn64BitCodeEx(pCtx);
177 const uint64_t uHyperIn = fIs64BitMode ? pCtx->rcx : (pCtx->rdx << 32) | pCtx->eax;
178 const uint16_t uHyperOp = GIM_HV_HYPERCALL_IN_CALL_CODE(uHyperIn);
179 const bool fHyperFast = GIM_HV_HYPERCALL_IN_IS_FAST(uHyperIn);
180 const uint16_t cHyperReps = GIM_HV_HYPERCALL_IN_REP_COUNT(uHyperIn);
181 const uint16_t idxHyperRepStart = GIM_HV_HYPERCALL_IN_REP_START_IDX(uHyperIn);
182 uint64_t cHyperRepsDone = 0;
183
184 int rc = VINF_SUCCESS;
185 int rcHv = GIM_HV_STATUS_OPERATION_DENIED;
186 PGIMHV pHv = &pVM->gim.s.u.Hv;
187
188 /*
189 * Validate common hypercall input parameters.
190 */
191 if ( !GIM_HV_HYPERCALL_IN_RSVD_1(uHyperIn)
192 && !GIM_HV_HYPERCALL_IN_RSVD_2(uHyperIn)
193 && !GIM_HV_HYPERCALL_IN_RSVD_3(uHyperIn))
194 {
195 /*
196 * Perform the hypercall.
197 */
198 switch (uHyperOp)
199 {
200 case GIM_HV_HYPERCALL_OP_RETREIVE_DEBUG_DATA: /* Non-rep, memory IO. */
201 {
202 if (pHv->uPartFlags & GIM_HV_PART_FLAGS_DEBUGGING)
203 {
204 rc = gimHvReadSlowHypercallParamsInOut(pVM, pCtx, fIs64BitMode, &rcHv);
205 if ( RT_SUCCESS(rc)
206 && rcHv == GIM_HV_STATUS_SUCCESS)
207 {
208 LogRelMax(1, ("GIM: HyperV: Initiated debug data reception via hypercall\n"));
209 rc = gimR3HvHypercallRetrieveDebugData(pVM, &rcHv);
210 if (RT_FAILURE(rc))
211 LogRelMax(10, ("GIM: HyperV: gimR3HvHypercallRetrieveDebugData failed. rc=%Rrc\n", rc));
212 }
213 }
214 else
215 rcHv = GIM_HV_STATUS_ACCESS_DENIED;
216 break;
217 }
218
219 case GIM_HV_HYPERCALL_OP_POST_DEBUG_DATA: /* Non-rep, memory IO. */
220 {
221 if (pHv->uPartFlags & GIM_HV_PART_FLAGS_DEBUGGING)
222 {
223 rc = gimHvReadSlowHypercallParamsInOut(pVM, pCtx, fIs64BitMode, &rcHv);
224 if ( RT_SUCCESS(rc)
225 && rcHv == GIM_HV_STATUS_SUCCESS)
226 {
227 LogRelMax(1, ("GIM: HyperV: Initiated debug data transmission via hypercall\n"));
228 rc = gimR3HvHypercallPostDebugData(pVM, &rcHv);
229 if (RT_FAILURE(rc))
230 LogRelMax(10, ("GIM: HyperV: gimR3HvHypercallPostDebugData failed. rc=%Rrc\n", rc));
231 }
232 }
233 else
234 rcHv = GIM_HV_STATUS_ACCESS_DENIED;
235 break;
236 }
237
238 case GIM_HV_HYPERCALL_OP_RESET_DEBUG_SESSION: /* Non-rep, fast (register IO). */
239 {
240 if (pHv->uPartFlags & GIM_HV_PART_FLAGS_DEBUGGING)
241 {
242 uint32_t fFlags = 0;
243 if (!fHyperFast)
244 {
245 rc = gimHvReadSlowHypercallParam(pVM, pCtx, fIs64BitMode, GIMHVHYPERCALLPARAM_IN, &rcHv);
246 if ( RT_SUCCESS(rc)
247 && rcHv == GIM_HV_STATUS_SUCCESS)
248 {
249 PGIMHVDEBUGRESETIN pIn = (PGIMHVDEBUGRESETIN)pHv->pbHypercallIn;
250 fFlags = pIn->fFlags;
251 }
252 }
253 else
254 {
255 rcHv = GIM_HV_STATUS_SUCCESS;
256 fFlags = fIs64BitMode ? pCtx->rdx : pCtx->ebx;
257 }
258
259 /*
260 * Nothing to flush on the sending side as we don't maintain our own buffers.
261 */
262 /** @todo We should probably ask the debug receive thread to flush it's buffer. */
263 if (rcHv == GIM_HV_STATUS_SUCCESS)
264 {
265 if (fFlags)
266 LogRel(("GIM: HyperV: Resetting debug session via hypercall\n"));
267 else
268 rcHv = GIM_HV_STATUS_INVALID_PARAMETER;
269 }
270 }
271 else
272 rcHv = GIM_HV_STATUS_ACCESS_DENIED;
273 break;
274 }
275
276 case GIM_HV_HYPERCALL_OP_POST_MESSAGE: /* Non-rep, memory IO. */
277 {
278 if (pHv->fIsInterfaceVs)
279 {
280 rc = gimHvReadSlowHypercallParam(pVM, pCtx, fIs64BitMode, GIMHVHYPERCALLPARAM_IN, &rcHv);
281 if ( RT_SUCCESS(rc)
282 && rcHv == GIM_HV_STATUS_SUCCESS)
283 {
284 PGIMHVPOSTMESSAGEIN pMsgIn = (PGIMHVPOSTMESSAGEIN)pHv->pbHypercallIn;
285 PGIMHVCPU pHvCpu = &pVCpu->gim.s.u.HvCpu;
286 if ( pMsgIn->uConnectionId == GIM_HV_VMBUS_MSG_CONNECTION_ID
287 && pMsgIn->enmMessageType == GIMHVMSGTYPE_VMBUS
288 && !MSR_GIM_HV_SINT_IS_MASKED(pHvCpu->uSint2Msr)
289 && MSR_GIM_HV_SIMP_IS_ENABLED(pHvCpu->uSimpMsr))
290 {
291 RTGCPHYS GCPhysSimp = MSR_GIM_HV_SIMP_GPA(pHvCpu->uSimpMsr);
292 if (PGMPhysIsGCPhysNormal(pVM, GCPhysSimp))
293 {
294 /*
295 * The VMBus client (guest) expects to see 0xf at offsets 4 and 16 and 1 at offset 0.
296 */
297 GIMHVMSG HvMsg;
298 RT_ZERO(HvMsg);
299 HvMsg.MsgHdr.enmMessageType = GIMHVMSGTYPE_VMBUS;
300 HvMsg.MsgHdr.cbPayload = 0xf;
301 HvMsg.aPayload[0] = 0xf;
302 uint16_t const offMsg = GIM_HV_VMBUS_MSG_SINT * sizeof(GIMHVMSG);
303 int rc2 = PGMPhysSimpleWriteGCPhys(pVM, GCPhysSimp + offMsg, &HvMsg, sizeof(HvMsg));
304 if (RT_SUCCESS(rc2))
305 LogRel(("GIM: HyperV: SIMP hypercall faking message at %#RGp:%u\n", GCPhysSimp, offMsg));
306 else
307 {
308 LogRel(("GIM: HyperV: Failed to write SIMP message at %#RGp:%u, rc=%Rrc\n", GCPhysSimp,
309 offMsg, rc));
310 }
311 }
312 }
313
314 /*
315 * Make the call fail after updating the SIMP, so the guest can go back to using
316 * the Hyper-V debug MSR interface. Any error code below GIM_HV_STATUS_NOT_ACKNOWLEDGED
317 * and the guest tries to proceed with initializing VMBus which is totally unnecessary
318 * for what we're trying to accomplish, i.e. convince guest to use Hyper-V debugging. Also,
319 * we don't implement other VMBus/SynIC functionality so the guest would #GP and die.
320 */
321 rcHv = GIM_HV_STATUS_NOT_ACKNOWLEDGED;
322 }
323 else
324 rcHv = GIM_HV_STATUS_INVALID_PARAMETER;
325 }
326 else
327 rcHv = GIM_HV_STATUS_ACCESS_DENIED;
328 break;
329 }
330
331 default:
332 rcHv = GIM_HV_STATUS_INVALID_HYPERCALL_CODE;
333 break;
334 }
335 }
336 else
337 rcHv = GIM_HV_STATUS_INVALID_HYPERCALL_INPUT;
338
339 /*
340 * Update the guest with results of the hypercall.
341 */
342 if (RT_SUCCESS(rc))
343 {
344 if (fIs64BitMode)
345 pCtx->rax = (cHyperRepsDone << 32) | rcHv;
346 else
347 {
348 pCtx->edx = cHyperRepsDone;
349 pCtx->eax = rcHv;
350 }
351 }
352
353 return rc;
354#endif
355}
356
357
358/**
359 * Returns whether the guest has configured and enabled the use of Hyper-V's
360 * hypercall interface.
361 *
362 * @returns true if hypercalls are enabled, false otherwise.
363 * @param pVCpu The cross context virtual CPU structure.
364 */
365VMM_INT_DECL(bool) gimHvAreHypercallsEnabled(PVMCPU pVCpu)
366{
367 return RT_BOOL(pVCpu->CTX_SUFF(pVM)->gim.s.u.Hv.u64GuestOsIdMsr != 0);
368}
369
370
371/**
372 * Returns whether the guest has configured and enabled the use of Hyper-V's
373 * paravirtualized TSC.
374 *
375 * @returns true if paravirt. TSC is enabled, false otherwise.
376 * @param pVM The cross context VM structure.
377 */
378VMM_INT_DECL(bool) gimHvIsParavirtTscEnabled(PVM pVM)
379{
380 return MSR_GIM_HV_REF_TSC_IS_ENABLED(pVM->gim.s.u.Hv.u64TscPageMsr);
381}
382
383
384#ifdef IN_RING3
385/**
386 * Gets the descriptive OS ID variant as identified via the
387 * MSR_GIM_HV_GUEST_OS_ID MSR.
388 *
389 * @returns The name.
390 * @param uGuestOsIdMsr The MSR_GIM_HV_GUEST_OS_ID MSR.
391 */
392static const char *gimHvGetGuestOsIdVariantName(uint64_t uGuestOsIdMsr)
393{
394 /* Refer the Hyper-V spec, section 3.6 "Reporting the Guest OS Identity". */
395 uint32_t uVendor = MSR_GIM_HV_GUEST_OS_ID_VENDOR(uGuestOsIdMsr);
396 if (uVendor == 1 /* Microsoft */)
397 {
398 uint32_t uOsVariant = MSR_GIM_HV_GUEST_OS_ID_OS_VARIANT(uGuestOsIdMsr);
399 switch (uOsVariant)
400 {
401 case 0: return "Undefined";
402 case 1: return "MS-DOS";
403 case 2: return "Windows 3.x";
404 case 3: return "Windows 9x";
405 case 4: return "Windows NT or derivative";
406 case 5: return "Windows CE";
407 default: return "Unknown";
408 }
409 }
410 return "Unknown";
411}
412#endif
413
414
415/**
416 * MSR read handler for Hyper-V.
417 *
418 * @returns Strict VBox status code like CPUMQueryGuestMsr().
419 * @retval VINF_CPUM_R3_MSR_READ
420 * @retval VERR_CPUM_RAISE_GP_0
421 *
422 * @param pVCpu The cross context virtual CPU structure.
423 * @param idMsr The MSR being read.
424 * @param pRange The range this MSR belongs to.
425 * @param puValue Where to store the MSR value read.
426 *
427 * @thread EMT.
428 */
429VMM_INT_DECL(VBOXSTRICTRC) gimHvReadMsr(PVMCPU pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t *puValue)
430{
431 NOREF(pRange);
432 PVM pVM = pVCpu->CTX_SUFF(pVM);
433 PGIMHV pHv = &pVM->gim.s.u.Hv;
434
435 switch (idMsr)
436 {
437 case MSR_GIM_HV_TIME_REF_COUNT:
438 {
439 /* Hyper-V reports the time in 100 ns units (10 MHz). */
440 uint64_t u64Tsc = TMCpuTickGet(pVCpu);
441 uint64_t u64TscHz = pHv->cTscTicksPerSecond;
442 uint64_t u64Tsc100Ns = u64TscHz / UINT64_C(10000000); /* 100 ns */
443 *puValue = (u64Tsc / u64Tsc100Ns);
444 return VINF_SUCCESS;
445 }
446
447 case MSR_GIM_HV_VP_INDEX:
448 *puValue = pVCpu->idCpu;
449 return VINF_SUCCESS;
450
451 case MSR_GIM_HV_TPR:
452 return PDMApicReadMsr(pVCpu, MSR_IA32_X2APIC_TPR, puValue);
453
454 case MSR_GIM_HV_ICR:
455 return PDMApicReadMsr(pVCpu, MSR_IA32_X2APIC_ICR, puValue);
456
457 case MSR_GIM_HV_GUEST_OS_ID:
458 *puValue = pHv->u64GuestOsIdMsr;
459 return VINF_SUCCESS;
460
461 case MSR_GIM_HV_HYPERCALL:
462 *puValue = pHv->u64HypercallMsr;
463 return VINF_SUCCESS;
464
465 case MSR_GIM_HV_REF_TSC:
466 *puValue = pHv->u64TscPageMsr;
467 return VINF_SUCCESS;
468
469 case MSR_GIM_HV_TSC_FREQ:
470 *puValue = TMCpuTicksPerSecond(pVM);
471 return VINF_SUCCESS;
472
473 case MSR_GIM_HV_APIC_FREQ:
474 {
475 int rc = PDMApicGetTimerFreq(pVM, puValue);
476 if (RT_FAILURE(rc))
477 return VERR_CPUM_RAISE_GP_0;
478 return VINF_SUCCESS;
479 }
480
481 case MSR_GIM_HV_SYNTH_DEBUG_STATUS:
482 *puValue = pHv->uDbgStatusMsr;
483 return VINF_SUCCESS;
484
485 case MSR_GIM_HV_SINT2:
486 {
487 PGIMHVCPU pHvCpu = &pVCpu->gim.s.u.HvCpu;
488 *puValue = pHvCpu->uSint2Msr;
489 return VINF_SUCCESS;
490 }
491
492 case MSR_GIM_HV_SIMP:
493 {
494 PGIMHVCPU pHvCpu = &pVCpu->gim.s.u.HvCpu;
495 *puValue = pHvCpu->uSimpMsr;
496 return VINF_SUCCESS;
497 }
498
499 case MSR_GIM_HV_RESET:
500 *puValue = 0;
501 return VINF_SUCCESS;
502
503 case MSR_GIM_HV_CRASH_CTL:
504 *puValue = pHv->uCrashCtlMsr;
505 return VINF_SUCCESS;
506
507 case MSR_GIM_HV_CRASH_P0: *puValue = pHv->uCrashP0Msr; return VINF_SUCCESS;
508 case MSR_GIM_HV_CRASH_P1: *puValue = pHv->uCrashP1Msr; return VINF_SUCCESS;
509 case MSR_GIM_HV_CRASH_P2: *puValue = pHv->uCrashP2Msr; return VINF_SUCCESS;
510 case MSR_GIM_HV_CRASH_P3: *puValue = pHv->uCrashP3Msr; return VINF_SUCCESS;
511 case MSR_GIM_HV_CRASH_P4: *puValue = pHv->uCrashP4Msr; return VINF_SUCCESS;
512
513 case MSR_GIM_HV_DEBUG_OPTIONS_MSR:
514 {
515 if (pHv->fIsVendorMsHv)
516 {
517#ifndef IN_RING3
518 return VINF_CPUM_R3_MSR_READ;
519#else
520 LogRelMax(1, ("GIM: HyperV: Guest querying debug options, suggesting %s interface\n",
521 pHv->fDbgHypercallInterface ? "hypercall" : "MSR"));
522 *puValue = pHv->fDbgHypercallInterface ? GIM_HV_DEBUG_OPTIONS_USE_HYPERCALLS : 0;
523 return VINF_SUCCESS;
524#endif
525 }
526 return VERR_CPUM_RAISE_GP_0;
527 }
528
529 /* Write-only MSRs: */
530 case MSR_GIM_HV_EOI:
531 /* Reserved/unknown MSRs: */
532 default:
533 {
534#ifdef IN_RING3
535 static uint32_t s_cTimes = 0;
536 if (s_cTimes++ < 20)
537 LogRel(("GIM: HyperV: Unknown/invalid RdMsr (%#x) -> #GP(0)\n", idMsr));
538#else
539 return VINF_CPUM_R3_MSR_READ;
540#endif
541 LogFunc(("Unknown/invalid RdMsr (%#RX32) -> #GP(0)\n", idMsr));
542 break;
543 }
544 }
545
546 return VERR_CPUM_RAISE_GP_0;
547}
548
549
550/**
551 * MSR write handler for Hyper-V.
552 *
553 * @returns Strict VBox status code like CPUMSetGuestMsr().
554 * @retval VINF_CPUM_R3_MSR_WRITE
555 * @retval VERR_CPUM_RAISE_GP_0
556 *
557 * @param pVCpu The cross context virtual CPU structure.
558 * @param idMsr The MSR being written.
559 * @param pRange The range this MSR belongs to.
560 * @param uRawValue The raw value with the ignored bits not masked.
561 *
562 * @thread EMT.
563 */
564VMM_INT_DECL(VBOXSTRICTRC) gimHvWriteMsr(PVMCPU pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t uRawValue)
565{
566 NOREF(pRange);
567 PVM pVM = pVCpu->CTX_SUFF(pVM);
568 PGIMHV pHv = &pVM->gim.s.u.Hv;
569
570 switch (idMsr)
571 {
572 case MSR_GIM_HV_TPR:
573 return PDMApicWriteMsr(pVCpu, MSR_IA32_X2APIC_TPR, uRawValue);
574
575 case MSR_GIM_HV_EOI:
576 return PDMApicWriteMsr(pVCpu, MSR_IA32_X2APIC_EOI, uRawValue);
577
578 case MSR_GIM_HV_ICR:
579 return PDMApicWriteMsr(pVCpu, MSR_IA32_X2APIC_ICR, uRawValue);
580
581 case MSR_GIM_HV_GUEST_OS_ID:
582 {
583#ifndef IN_RING3
584 return VINF_CPUM_R3_MSR_WRITE;
585#else
586 /* Disable the hypercall-page and hypercalls if 0 is written to this MSR. */
587 if (!uRawValue)
588 {
589 if (MSR_GIM_HV_HYPERCALL_PAGE_IS_ENABLED(pHv->u64HypercallMsr))
590 {
591 gimR3HvDisableHypercallPage(pVM);
592 pHv->u64HypercallMsr &= ~MSR_GIM_HV_HYPERCALL_PAGE_ENABLE_BIT;
593 LogRel(("GIM: HyperV: Hypercall page disabled via Guest OS ID MSR\n"));
594 }
595 }
596 else
597 {
598 LogRel(("GIM: HyperV: Guest OS reported ID %#RX64\n", uRawValue));
599 LogRel(("GIM: HyperV: Open-source=%RTbool Vendor=%#x OS=%#x (%s) Major=%u Minor=%u ServicePack=%u Build=%u\n",
600 MSR_GIM_HV_GUEST_OS_ID_IS_OPENSOURCE(uRawValue), MSR_GIM_HV_GUEST_OS_ID_VENDOR(uRawValue),
601 MSR_GIM_HV_GUEST_OS_ID_OS_VARIANT(uRawValue), gimHvGetGuestOsIdVariantName(uRawValue),
602 MSR_GIM_HV_GUEST_OS_ID_MAJOR_VERSION(uRawValue), MSR_GIM_HV_GUEST_OS_ID_MINOR_VERSION(uRawValue),
603 MSR_GIM_HV_GUEST_OS_ID_SERVICE_VERSION(uRawValue), MSR_GIM_HV_GUEST_OS_ID_BUILD(uRawValue)));
604
605 /* Update the CPUID leaf, see Hyper-V spec. "Microsoft Hypervisor CPUID Leaves". */
606 CPUMCPUIDLEAF HyperLeaf;
607 RT_ZERO(HyperLeaf);
608 HyperLeaf.uLeaf = UINT32_C(0x40000002);
609 HyperLeaf.uEax = MSR_GIM_HV_GUEST_OS_ID_BUILD(uRawValue);
610 HyperLeaf.uEbx = MSR_GIM_HV_GUEST_OS_ID_MINOR_VERSION(uRawValue)
611 | (MSR_GIM_HV_GUEST_OS_ID_MAJOR_VERSION(uRawValue) << 16);
612 HyperLeaf.uEcx = MSR_GIM_HV_GUEST_OS_ID_SERVICE_VERSION(uRawValue);
613 HyperLeaf.uEdx = MSR_GIM_HV_GUEST_OS_ID_SERVICE_VERSION(uRawValue)
614 | (MSR_GIM_HV_GUEST_OS_ID_BUILD(uRawValue) << 24);
615 int rc2 = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
616 AssertRC(rc2);
617 }
618
619 pHv->u64GuestOsIdMsr = uRawValue;
620
621 /*
622 * Notify VMM that hypercalls are now disabled/enabled.
623 */
624 for (VMCPUID i = 0; i < pVM->cCpus; i++)
625 {
626 if (uRawValue)
627 VMMHypercallsEnable(&pVM->aCpus[i]);
628 else
629 VMMHypercallsDisable(&pVM->aCpus[i]);
630 }
631
632 return VINF_SUCCESS;
633#endif /* IN_RING3 */
634 }
635
636 case MSR_GIM_HV_HYPERCALL:
637 {
638#ifndef IN_RING3
639 return VINF_CPUM_R3_MSR_WRITE;
640#else
641 /** @todo There is/was a problem with hypercalls for FreeBSD 10.1 guests,
642 * see @bugref{7270#c116}. */
643 /* First, update all but the hypercall page enable bit. */
644 pHv->u64HypercallMsr = (uRawValue & ~MSR_GIM_HV_HYPERCALL_PAGE_ENABLE_BIT);
645
646 /* Hypercall page can only be enabled when the guest has enabled hypercalls. */
647 bool fEnable = RT_BOOL(uRawValue & MSR_GIM_HV_HYPERCALL_PAGE_ENABLE_BIT);
648 if ( fEnable
649 && !gimHvAreHypercallsEnabled(pVCpu))
650 {
651 return VINF_SUCCESS;
652 }
653
654 /* Is the guest disabling the hypercall-page? Allow it regardless of the Guest-OS Id Msr. */
655 if (!fEnable)
656 {
657 gimR3HvDisableHypercallPage(pVM);
658 pHv->u64HypercallMsr = uRawValue;
659 return VINF_SUCCESS;
660 }
661
662 /* Enable the hypercall-page. */
663 RTGCPHYS GCPhysHypercallPage = MSR_GIM_HV_HYPERCALL_GUEST_PFN(uRawValue) << PAGE_SHIFT;
664 int rc = gimR3HvEnableHypercallPage(pVM, GCPhysHypercallPage);
665 if (RT_SUCCESS(rc))
666 {
667 pHv->u64HypercallMsr = uRawValue;
668 return VINF_SUCCESS;
669 }
670
671 return VERR_CPUM_RAISE_GP_0;
672#endif
673 }
674
675 case MSR_GIM_HV_REF_TSC:
676 {
677#ifndef IN_RING3
678 return VINF_CPUM_R3_MSR_WRITE;
679#else /* IN_RING3 */
680 /* First, update all but the TSC-page enable bit. */
681 pHv->u64TscPageMsr = (uRawValue & ~MSR_GIM_HV_REF_TSC_ENABLE_BIT);
682
683 /* Is the guest disabling the TSC-page? */
684 bool fEnable = RT_BOOL(uRawValue & MSR_GIM_HV_REF_TSC_ENABLE_BIT);
685 if (!fEnable)
686 {
687 gimR3HvDisableTscPage(pVM);
688 pHv->u64TscPageMsr = uRawValue;
689 return VINF_SUCCESS;
690 }
691
692 /* Enable the TSC-page. */
693 RTGCPHYS GCPhysTscPage = MSR_GIM_HV_REF_TSC_GUEST_PFN(uRawValue) << PAGE_SHIFT;
694 int rc = gimR3HvEnableTscPage(pVM, GCPhysTscPage, false /* fUseThisTscSequence */, 0 /* uTscSequence */);
695 if (RT_SUCCESS(rc))
696 {
697 pHv->u64TscPageMsr = uRawValue;
698 return VINF_SUCCESS;
699 }
700
701 return VERR_CPUM_RAISE_GP_0;
702#endif /* IN_RING3 */
703 }
704
705 case MSR_GIM_HV_RESET:
706 {
707#ifndef IN_RING3
708 return VINF_CPUM_R3_MSR_WRITE;
709#else
710 if (MSR_GIM_HV_RESET_IS_SET(uRawValue))
711 {
712 LogRel(("GIM: HyperV: Reset initiated through MSR\n"));
713 int rc = PDMDevHlpVMReset(pVM->gim.s.pDevInsR3, PDMVMRESET_F_GIM);
714 AssertRC(rc); /* Note! Not allowed to return VINF_EM_RESET / VINF_EM_HALT here, so ignore them. */
715 }
716 /* else: Ignore writes to other bits. */
717 return VINF_SUCCESS;
718#endif /* IN_RING3 */
719 }
720
721 case MSR_GIM_HV_CRASH_CTL:
722 {
723#ifndef IN_RING3
724 return VINF_CPUM_R3_MSR_WRITE;
725#else
726 if (uRawValue & MSR_GIM_HV_CRASH_CTL_NOTIFY_BIT)
727 {
728 LogRel(("GIM: HyperV: Guest indicates a fatal condition! P0=%#RX64 P1=%#RX64 P2=%#RX64 P3=%#RX64 P4=%#RX64\n",
729 pHv->uCrashP0Msr, pHv->uCrashP1Msr, pHv->uCrashP2Msr, pHv->uCrashP3Msr, pHv->uCrashP4Msr));
730
731 if (DBGF_IS_EVENT_ENABLED(pVM, DBGFEVENT_BSOD_MSR))
732 DBGFEventGenericWithArg(pVM, pVCpu, DBGFEVENT_BSOD_MSR, pHv->uCrashP0Msr, DBGFEVENTCTX_OTHER);
733 /* (Do not try pass VINF_EM_DBG_EVENT, doesn't work from here!) */
734 }
735 return VINF_SUCCESS;
736#endif
737 }
738
739 case MSR_GIM_HV_SYNTH_DEBUG_SEND_BUFFER:
740 {
741 if (!pHv->fDbgEnabled)
742 return VERR_CPUM_RAISE_GP_0;
743#ifndef IN_RING3
744 return VINF_CPUM_R3_MSR_WRITE;
745#else
746 RTGCPHYS GCPhysBuffer = (RTGCPHYS)uRawValue;
747 pHv->uDbgSendBufferMsr = GCPhysBuffer;
748 if (PGMPhysIsGCPhysNormal(pVM, GCPhysBuffer))
749 LogRel(("GIM: HyperV: Set up debug send buffer at %#RGp\n", GCPhysBuffer));
750 else
751 LogRel(("GIM: HyperV: Destroyed debug send buffer\n"));
752 pHv->uDbgSendBufferMsr = uRawValue;
753 return VINF_SUCCESS;
754#endif
755 }
756
757 case MSR_GIM_HV_SYNTH_DEBUG_RECEIVE_BUFFER:
758 {
759 if (!pHv->fDbgEnabled)
760 return VERR_CPUM_RAISE_GP_0;
761#ifndef IN_RING3
762 return VINF_CPUM_R3_MSR_WRITE;
763#else
764 RTGCPHYS GCPhysBuffer = (RTGCPHYS)uRawValue;
765 pHv->uDbgRecvBufferMsr = GCPhysBuffer;
766 if (PGMPhysIsGCPhysNormal(pVM, GCPhysBuffer))
767 LogRel(("GIM: HyperV: Set up debug receive buffer at %#RGp\n", GCPhysBuffer));
768 else
769 LogRel(("GIM: HyperV: Destroyed debug receive buffer\n"));
770 return VINF_SUCCESS;
771#endif
772 }
773
774 case MSR_GIM_HV_SYNTH_DEBUG_PENDING_BUFFER:
775 {
776 if (!pHv->fDbgEnabled)
777 return VERR_CPUM_RAISE_GP_0;
778#ifndef IN_RING3
779 return VINF_CPUM_R3_MSR_WRITE;
780#else
781 RTGCPHYS GCPhysBuffer = (RTGCPHYS)uRawValue;
782 pHv->uDbgPendingBufferMsr = GCPhysBuffer;
783 if (PGMPhysIsGCPhysNormal(pVM, GCPhysBuffer))
784 LogRel(("GIM: HyperV: Set up debug pending buffer at %#RGp\n", uRawValue));
785 else
786 LogRel(("GIM: HyperV: Destroyed debug pending buffer\n"));
787 return VINF_SUCCESS;
788#endif
789 }
790
791 case MSR_GIM_HV_SYNTH_DEBUG_CONTROL:
792 {
793 if (!pHv->fDbgEnabled)
794 return VERR_CPUM_RAISE_GP_0;
795#ifndef IN_RING3
796 return VINF_CPUM_R3_MSR_WRITE;
797#else
798 if ( MSR_GIM_HV_SYNTH_DEBUG_CONTROL_IS_WRITE(uRawValue)
799 && MSR_GIM_HV_SYNTH_DEBUG_CONTROL_IS_READ(uRawValue))
800 {
801 LogRel(("GIM: HyperV: Requesting both read and write through debug control MSR -> #GP(0)\n"));
802 return VERR_CPUM_RAISE_GP_0;
803 }
804
805 if (MSR_GIM_HV_SYNTH_DEBUG_CONTROL_IS_WRITE(uRawValue))
806 {
807 uint32_t cbWrite = MSR_GIM_HV_SYNTH_DEBUG_CONTROL_W_LEN(uRawValue);
808 if ( cbWrite > 0
809 && cbWrite < GIM_HV_PAGE_SIZE)
810 {
811 if (PGMPhysIsGCPhysNormal(pVM, (RTGCPHYS)pHv->uDbgSendBufferMsr))
812 {
813 Assert(pHv->pvDbgBuffer);
814 int rc = PGMPhysSimpleReadGCPhys(pVM, pHv->pvDbgBuffer, (RTGCPHYS)pHv->uDbgSendBufferMsr, cbWrite);
815 if (RT_SUCCESS(rc))
816 {
817 LogRelMax(1, ("GIM: HyperV: Initiated debug data transmission via MSR\n"));
818 uint32_t cbWritten = 0;
819 rc = gimR3HvDebugWrite(pVM, pHv->pvDbgBuffer, cbWrite, &cbWritten, false /*fUdpPkt*/);
820 if ( RT_SUCCESS(rc)
821 && cbWrite == cbWritten)
822 pHv->uDbgStatusMsr = MSR_GIM_HV_SYNTH_DEBUG_STATUS_W_SUCCESS_BIT;
823 else
824 pHv->uDbgStatusMsr = 0;
825 }
826 else
827 LogRelMax(5, ("GIM: HyperV: Failed to read debug send buffer at %#RGp, rc=%Rrc\n",
828 (RTGCPHYS)pHv->uDbgSendBufferMsr, rc));
829 }
830 else
831 LogRelMax(5, ("GIM: HyperV: Debug send buffer address %#RGp invalid! Ignoring debug write!\n",
832 (RTGCPHYS)pHv->uDbgSendBufferMsr));
833 }
834 else
835 LogRelMax(5, ("GIM: HyperV: Invalid write size %u specified in MSR, ignoring debug write!\n",
836 MSR_GIM_HV_SYNTH_DEBUG_CONTROL_W_LEN(uRawValue)));
837 }
838 else if (MSR_GIM_HV_SYNTH_DEBUG_CONTROL_IS_READ(uRawValue))
839 {
840 if (PGMPhysIsGCPhysNormal(pVM, (RTGCPHYS)pHv->uDbgRecvBufferMsr))
841 {
842 LogRelMax(1, ("GIM: HyperV: Initiated debug data reception via MSR\n"));
843 uint32_t cbReallyRead;
844 Assert(pHv->pvDbgBuffer);
845 int rc = gimR3HvDebugRead(pVM, pHv->pvDbgBuffer, PAGE_SIZE, PAGE_SIZE, &cbReallyRead, 0, false /*fUdpPkt*/);
846 if ( RT_SUCCESS(rc)
847 && cbReallyRead > 0)
848 {
849 rc = PGMPhysSimpleWriteGCPhys(pVM, (RTGCPHYS)pHv->uDbgRecvBufferMsr, pHv->pvDbgBuffer, cbReallyRead);
850 if (RT_SUCCESS(rc))
851 {
852 pHv->uDbgStatusMsr = ((uint16_t)cbReallyRead) << 16;
853 pHv->uDbgStatusMsr |= MSR_GIM_HV_SYNTH_DEBUG_STATUS_R_SUCCESS_BIT;
854 }
855 else
856 {
857 pHv->uDbgStatusMsr = 0;
858 LogRelMax(5, ("GIM: HyperV: PGMPhysSimpleWriteGCPhys failed. rc=%Rrc\n", rc));
859 }
860 }
861 else
862 pHv->uDbgStatusMsr = 0;
863 }
864 else
865 LogRelMax(5, ("GIM: HyperV: Debug receive buffer address %#RGp invalid! Ignoring debug read!\n", (RTGCPHYS)pHv->uDbgRecvBufferMsr));
866 }
867 return VINF_SUCCESS;
868#endif
869 }
870
871 case MSR_GIM_HV_SINT2:
872 {
873 if (!pHv->fDbgEnabled)
874 return VERR_CPUM_RAISE_GP_0;
875#ifndef IN_RING3
876 return VINF_CPUM_R3_MSR_WRITE;
877#else
878 PGIMHVCPU pHvCpu = &pVCpu->gim.s.u.HvCpu;
879 uint8_t uVector = MSR_GIM_HV_SINT_VECTOR(uRawValue);
880 if ( !MSR_GIM_HV_SINT_IS_MASKED(uRawValue)
881 && uVector < 16)
882 {
883 LogRel(("GIM: HyperV: Programmed an invalid vector in SINT2, uVector=%u -> #GP(0)\n", uVector));
884 return VERR_CPUM_RAISE_GP_0;
885 }
886
887 pHvCpu->uSint2Msr = uRawValue;
888 if (MSR_GIM_HV_SINT_IS_MASKED(uRawValue))
889 LogRel(("GIM: HyperV: Masked SINT2\n"));
890 else
891 LogRel(("GIM: HyperV: Unmasked SINT2, uVector=%u\n", uVector));
892 return VINF_SUCCESS;
893#endif
894 }
895
896 case MSR_GIM_HV_SIMP:
897 {
898 if (!pHv->fDbgEnabled)
899 return VERR_CPUM_RAISE_GP_0;
900#ifndef IN_RING3
901 return VINF_CPUM_R3_MSR_WRITE;
902#else
903 PGIMHVCPU pHvCpu = &pVCpu->gim.s.u.HvCpu;
904 pHvCpu->uSimpMsr = uRawValue;
905 if (MSR_GIM_HV_SIMP_IS_ENABLED(uRawValue))
906 {
907 RTGCPHYS GCPhysSimp = MSR_GIM_HV_SIMP_GPA(uRawValue);
908 if (PGMPhysIsGCPhysNormal(pVM, GCPhysSimp))
909 {
910 uint8_t abSimp[PAGE_SIZE];
911 RT_ZERO(abSimp);
912 int rc2 = PGMPhysSimpleWriteGCPhys(pVM, GCPhysSimp, &abSimp[0], sizeof(abSimp));
913 if (RT_SUCCESS(rc2))
914 LogRel(("GIM: HyperV: Enabled synthetic interrupt message page at %#RGp\n", GCPhysSimp));
915 else
916 {
917 LogRel(("GIM: HyperV: WrMsr on MSR_GIM_HV_SIMP failed to update SIMP at %#RGp rc=%Rrc -> #GP(0)\n",
918 GCPhysSimp, rc2));
919 return VERR_CPUM_RAISE_GP_0;
920 }
921 }
922 else
923 LogRel(("GIM: HyperV: Enabled synthetic interrupt message page at invalid address %#RGp\n",GCPhysSimp));
924 }
925 else
926 LogRel(("GIM: HyperV: Disabled synthetic interrupt message page\n"));
927 return VINF_SUCCESS;
928#endif
929 }
930
931 case MSR_GIM_HV_CRASH_P0: pHv->uCrashP0Msr = uRawValue; return VINF_SUCCESS;
932 case MSR_GIM_HV_CRASH_P1: pHv->uCrashP1Msr = uRawValue; return VINF_SUCCESS;
933 case MSR_GIM_HV_CRASH_P2: pHv->uCrashP2Msr = uRawValue; return VINF_SUCCESS;
934 case MSR_GIM_HV_CRASH_P3: pHv->uCrashP3Msr = uRawValue; return VINF_SUCCESS;
935 case MSR_GIM_HV_CRASH_P4: pHv->uCrashP4Msr = uRawValue; return VINF_SUCCESS;
936
937 case MSR_GIM_HV_TIME_REF_COUNT: /* Read-only MSRs. */
938 case MSR_GIM_HV_VP_INDEX:
939 case MSR_GIM_HV_TSC_FREQ:
940 case MSR_GIM_HV_APIC_FREQ:
941 LogFunc(("WrMsr on read-only MSR %#RX32 -> #GP(0)\n", idMsr));
942 return VERR_CPUM_RAISE_GP_0;
943
944 case MSR_GIM_HV_DEBUG_OPTIONS_MSR:
945 {
946 if (pHv->fIsVendorMsHv)
947 {
948#ifndef IN_RING3
949 return VINF_CPUM_R3_MSR_WRITE;
950#else
951 LogRelMax(5, ("GIM: HyperV: Write debug options MSR with %#RX64 ignored\n", uRawValue));
952 return VINF_SUCCESS;
953#endif
954 }
955 return VERR_CPUM_RAISE_GP_0;
956 }
957
958 default:
959 {
960#ifdef IN_RING3
961 static uint32_t s_cTimes = 0;
962 if (s_cTimes++ < 20)
963 LogRel(("GIM: HyperV: Unknown/invalid WrMsr (%#x,%#x`%08x) -> #GP(0)\n", idMsr,
964 uRawValue & UINT64_C(0xffffffff00000000), uRawValue & UINT64_C(0xffffffff)));
965#else
966 return VINF_CPUM_R3_MSR_WRITE;
967#endif
968 LogFunc(("Unknown/invalid WrMsr (%#RX32,%#RX64) -> #GP(0)\n", idMsr, uRawValue));
969 break;
970 }
971 }
972
973 return VERR_CPUM_RAISE_GP_0;
974}
975
976
977/**
978 * Whether we need to trap \#UD exceptions in the guest.
979 *
980 * We only need to trap \#UD exceptions for raw-mode guests when hypercalls are
981 * enabled. For HM VMs, the hypercall would be handled via the
982 * VMCALL/VMMCALL VM-exit.
983 *
984 * @param pVCpu The cross context virtual CPU structure.
985 */
986VMM_INT_DECL(bool) gimHvShouldTrapXcptUD(PVMCPU pVCpu)
987{
988 PVM pVM = pVCpu->CTX_SUFF(pVM);
989 if ( !HMIsEnabled(pVM)
990 && gimHvAreHypercallsEnabled(pVCpu))
991 return true;
992 return false;
993}
994
995
996/**
997 * Checks the currently disassembled instruction and executes the hypercall if
998 * it's a hypercall instruction.
999 *
1000 * @returns Strict VBox status code.
1001 * @param pVCpu The cross context virtual CPU structure.
1002 * @param pCtx Pointer to the guest-CPU context.
1003 * @param pDis Pointer to the disassembled instruction state at RIP.
1004 *
1005 * @thread EMT(pVCpu).
1006 *
1007 * @todo Make this function static when @bugref{7270#c168} is addressed.
1008 */
1009VMM_INT_DECL(VBOXSTRICTRC) gimHvExecHypercallInstr(PVMCPU pVCpu, PCPUMCTX pCtx, PDISCPUSTATE pDis)
1010{
1011 Assert(pVCpu);
1012 Assert(pCtx);
1013 Assert(pDis);
1014 VMCPU_ASSERT_EMT(pVCpu);
1015
1016 PVM pVM = pVCpu->CTX_SUFF(pVM);
1017 CPUMCPUVENDOR const enmGuestCpuVendor = CPUMGetGuestCpuVendor(pVM);
1018 if ( ( pDis->pCurInstr->uOpcode == OP_VMCALL
1019 && ( enmGuestCpuVendor == CPUMCPUVENDOR_INTEL
1020 || enmGuestCpuVendor == CPUMCPUVENDOR_VIA))
1021 || ( pDis->pCurInstr->uOpcode == OP_VMMCALL
1022 && enmGuestCpuVendor == CPUMCPUVENDOR_AMD))
1023 {
1024 return gimHvHypercall(pVCpu, pCtx);
1025 }
1026
1027 return VERR_GIM_INVALID_HYPERCALL_INSTR;
1028}
1029
1030
1031/**
1032 * Exception handler for \#UD.
1033 *
1034 * @returns Strict VBox status code.
1035 * @retval VINF_SUCCESS if the hypercall succeeded (even if its operation
1036 * failed).
1037 * @retval VINF_GIM_R3_HYPERCALL re-start the hypercall from ring-3.
1038 * @retval VINF_GIM_HYPERCALL_CONTINUING continue hypercall without updating
1039 * RIP.
1040 * @retval VERR_GIM_HYPERCALL_ACCESS_DENIED CPL is insufficient.
1041 * @retval VERR_GIM_INVALID_HYPERCALL_INSTR instruction at RIP is not a valid
1042 * hypercall instruction.
1043 *
1044 * @param pVCpu The cross context virtual CPU structure.
1045 * @param pCtx Pointer to the guest-CPU context.
1046 * @param pDis Pointer to the disassembled instruction state at RIP.
1047 * Optional, can be NULL.
1048 * @param pcbInstr Where to store the instruction length of the hypercall
1049 * instruction. Optional, can be NULL.
1050 *
1051 * @thread EMT(pVCpu).
1052 */
1053VMM_INT_DECL(VBOXSTRICTRC) gimHvXcptUD(PVMCPU pVCpu, PCPUMCTX pCtx, PDISCPUSTATE pDis, uint8_t *pcbInstr)
1054{
1055 VMCPU_ASSERT_EMT(pVCpu);
1056
1057 /*
1058 * If we didn't ask for #UD to be trapped, bail.
1059 */
1060 if (!gimHvShouldTrapXcptUD(pVCpu))
1061 return VERR_GIM_IPE_1;
1062
1063 if (!pDis)
1064 {
1065 /*
1066 * Disassemble the instruction at RIP to figure out if it's the Intel VMCALL instruction
1067 * or the AMD VMMCALL instruction and if so, handle it as a hypercall.
1068 */
1069 unsigned cbInstr;
1070 DISCPUSTATE Dis;
1071 int rc = EMInterpretDisasCurrent(pVCpu->CTX_SUFF(pVM), pVCpu, &Dis, &cbInstr);
1072 if (RT_SUCCESS(rc))
1073 {
1074 if (pcbInstr)
1075 *pcbInstr = (uint8_t)cbInstr;
1076 return gimHvExecHypercallInstr(pVCpu, pCtx, &Dis);
1077 }
1078
1079 Log(("GIM: HyperV: Failed to disassemble instruction at CS:RIP=%04x:%08RX64. rc=%Rrc\n", pCtx->cs.Sel, pCtx->rip, rc));
1080 return rc;
1081 }
1082
1083 return gimHvExecHypercallInstr(pVCpu, pCtx, pDis);
1084}
1085
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