1 | /* $Id: GIMR0Hv.cpp 53509 2014-12-11 12:14:54Z 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 | */
|
---|
45 | static 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 | */
|
---|
70 | static 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 | */
|
---|
94 | VMM_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) / RT_NS_10MS;
|
---|
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 | if (pRefTsc->u32TscSequence < UINT32_C(0xfffffffe))
|
---|
121 | ASMAtomicIncU32(&pRefTsc->u32TscSequence);
|
---|
122 | else
|
---|
123 | ASMAtomicWriteU32(&pRefTsc->u32TscSequence, 1);
|
---|
124 | ASMAtomicWriteS64(&pRefTsc->i64TscOffset, i64TscOffset);
|
---|
125 | }
|
---|
126 | RTSpinlockRelease(pcHv->hSpinlockR0);
|
---|
127 |
|
---|
128 | Assert(pRefTsc->u32TscSequence != 0);
|
---|
129 | Assert(pRefTsc->u32TscSequence != UINT32_C(0xffffffff));
|
---|
130 | return VINF_SUCCESS;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Does ring-0 per-VM GIM Hyper-V initialization.
|
---|
136 | *
|
---|
137 | * @returns VBox status code.
|
---|
138 | * @param pVM Pointer to the VM.
|
---|
139 | */
|
---|
140 | VMMR0_INT_DECL(int) GIMR0HvInitVM(PVM pVM)
|
---|
141 | {
|
---|
142 | AssertPtr(pVM);
|
---|
143 | Assert(GIMIsEnabled(pVM));
|
---|
144 |
|
---|
145 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
146 | Assert(pHv->hSpinlockR0 == NIL_RTSPINLOCK);
|
---|
147 |
|
---|
148 | int rc = RTSpinlockCreate(&pHv->hSpinlockR0, RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, "Hyper-V");
|
---|
149 | return rc;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Does ring-0 per-VM GIM Hyper-V termination.
|
---|
155 | *
|
---|
156 | * @returns VBox status code.
|
---|
157 | * @param pVM Pointer to the VM.
|
---|
158 | */
|
---|
159 | VMMR0_INT_DECL(int) GIMR0HvTermVM(PVM pVM)
|
---|
160 | {
|
---|
161 | AssertPtr(pVM);
|
---|
162 | Assert(GIMIsEnabled(pVM));
|
---|
163 |
|
---|
164 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
165 | RTSpinlockDestroy(pHv->hSpinlockR0);
|
---|
166 | pHv->hSpinlockR0 = NIL_RTSPINLOCK;
|
---|
167 |
|
---|
168 | return VINF_SUCCESS;
|
---|
169 | }
|
---|
170 |
|
---|