VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/GIMR0Hv.cpp@ 52934

Last change on this file since 52934 was 51961, checked in by vboxsync, 10 years ago

VMM/GIM: Fix Hyper-V TSC offset scaling to 100ns units.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/* $Id: GIMR0Hv.cpp 51961 2014-07-10 08:55:01Z vboxsync $ */
2/** @file
3 * Guest Interface Manager (GIM), Hyper-V - Host Context Ring-0.
4 */
5
6/*
7 * Copyright (C) 2014 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 "GIMInternal.h"
23#include "GIMHvInternal.h"
24
25#include <VBox/err.h>
26#include <VBox/vmm/gim.h>
27#include <VBox/vmm/tm.h>
28#include <VBox/vmm/vm.h>
29
30#include <iprt/spinlock.h>
31
32
33#if 0
34/**
35 * Allocates and maps one physically contiguous page. The allocated page is
36 * zero'd out.
37 *
38 * @returns IPRT status code.
39 * @param pMemObj Pointer to the ring-0 memory object.
40 * @param ppVirt Where to store the virtual address of the
41 * allocation.
42 * @param pPhys Where to store the physical address of the
43 * allocation.
44 */
45static int gimR0HvPageAllocZ(PRTR0MEMOBJ pMemObj, PRTR0PTR ppVirt, PRTHCPHYS pHCPhys)
46{
47 AssertPtr(pMemObj);
48 AssertPtr(ppVirt);
49 AssertPtr(pHCPhys);
50
51 int rc = RTR0MemObjAllocCont(pMemObj, PAGE_SIZE, false /* fExecutable */);
52 if (RT_FAILURE(rc))
53 return rc;
54 *ppVirt = RTR0MemObjAddress(*pMemObj);
55 *pHCPhys = RTR0MemObjGetPagePhysAddr(*pMemObj, 0 /* iPage */);
56 ASMMemZero32(*ppVirt, PAGE_SIZE);
57 return VINF_SUCCESS;
58}
59
60
61/**
62 * Frees and unmaps an allocated physical page.
63 *
64 * @param pMemObj Pointer to the ring-0 memory object.
65 * @param ppVirt Where to re-initialize the virtual address of
66 * allocation as 0.
67 * @param pHCPhys Where to re-initialize the physical address of the
68 * allocation as 0.
69 */
70static void gimR0HvPageFree(PRTR0MEMOBJ pMemObj, PRTR0PTR ppVirt, PRTHCPHYS pHCPhys)
71{
72 AssertPtr(pMemObj);
73 AssertPtr(ppVirt);
74 AssertPtr(pHCPhys);
75 if (*pMemObj != NIL_RTR0MEMOBJ)
76 {
77 int rc = RTR0MemObjFree(*pMemObj, true /* fFreeMappings */);
78 AssertRC(rc);
79 *pMemObj = NIL_RTR0MEMOBJ;
80 *ppVirt = 0;
81 *pHCPhys = 0;
82 }
83}
84#endif
85
86/**
87 * Updates Hyper-V's reference TSC page.
88 *
89 * @returns VBox status code.
90 * @param pVM Pointer to the VM.
91 * @param u64Offset The computed TSC offset.
92 * @thread EMT.
93 */
94VMM_INT_DECL(int) GIMR0HvUpdateParavirtTsc(PVM pVM, uint64_t u64Offset)
95{
96 Assert(GIMIsEnabled(pVM));
97 bool fHvTscEnabled = MSR_GIM_HV_REF_TSC_IS_ENABLED(pVM->gim.s.u.Hv.u64TscPageMsr);
98 if (RT_UNLIKELY(!fHvTscEnabled))
99 return VERR_GIM_PVTSC_NOT_ENABLED;
100
101 PCGIMHV pcHv = &pVM->gim.s.u.Hv;
102 PCGIMMMIO2REGION pcRegion = &pcHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
103 PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)pcRegion->CTX_SUFF(pvPage);
104 Assert(pRefTsc);
105
106 /*
107 * Hyper-V reports the reference time in 100 nanosecond units.
108 */
109 uint64_t u64Tsc100Ns = TMCpuTicksPerSecond(pVM) / UINT64_C(10000000); /* 100 ns */
110 int64_t i64TscOffset = (int64_t)u64Offset / u64Tsc100Ns;
111
112 /*
113 * The TSC page can be simulatenously read by other VCPUs in the guest. The
114 * spinlock is only for protecting simultaneous hypervisor writes from other
115 * EMTs.
116 */
117 RTSpinlockAcquire(pcHv->hSpinlockR0);
118 if (pRefTsc->i64TscOffset != i64TscOffset)
119 {
120 ASMAtomicWriteS64(&pRefTsc->i64TscOffset, i64TscOffset);
121 if (pRefTsc->u32TscSequence < UINT32_C(0xfffffffe))
122 ASMAtomicIncU32(&pRefTsc->u32TscSequence);
123 else
124 ASMAtomicWriteU32(&pRefTsc->u32TscSequence, 1);
125 }
126 RTSpinlockRelease(pcHv->hSpinlockR0);
127
128 Assert(pRefTsc->u32TscSequence != 0);
129 Assert(pRefTsc->u32TscSequence != UINT32_C(0xffffffff));
130 AssertReturn(pRefTsc->u32TscSequence != 0xfffffffe, VERR_GIM_IPE_3);
131 return VINF_SUCCESS;
132}
133
134
135/**
136 * Does ring-0 per-VM GIM Hyper-V initialization.
137 *
138 * @returns VBox status code.
139 * @param pVM Pointer to the VM.
140 */
141VMMR0_INT_DECL(int) GIMR0HvInitVM(PVM pVM)
142{
143 AssertPtr(pVM);
144 Assert(GIMIsEnabled(pVM));
145
146 PGIMHV pHv = &pVM->gim.s.u.Hv;
147 Assert(pHv->hSpinlockR0 == NIL_RTSPINLOCK);
148
149 int rc = RTSpinlockCreate(&pHv->hSpinlockR0, RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, "Hyper-V");
150 return rc;
151}
152
153
154/**
155 * Does ring-0 per-VM GIM Hyper-V termination.
156 *
157 * @returns VBox status code.
158 * @param pVM Pointer to the VM.
159 */
160VMMR0_INT_DECL(int) GIMR0HvTermVM(PVM pVM)
161{
162 AssertPtr(pVM);
163 Assert(GIMIsEnabled(pVM));
164
165 PGIMHV pHv = &pVM->gim.s.u.Hv;
166 RTSpinlockDestroy(pHv->hSpinlockR0);
167 pHv->hSpinlockR0 = NIL_RTSPINLOCK;
168
169 return VINF_SUCCESS;
170}
171
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