1 | /* $Id: GIMR0Hv.cpp 51954 2014-07-09 12:50:52Z 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/vm.h>
|
---|
28 |
|
---|
29 | #include <iprt/spinlock.h>
|
---|
30 |
|
---|
31 |
|
---|
32 | #if 0
|
---|
33 | /**
|
---|
34 | * Allocates and maps one physically contiguous page. The allocated page is
|
---|
35 | * zero'd out.
|
---|
36 | *
|
---|
37 | * @returns IPRT status code.
|
---|
38 | * @param pMemObj Pointer to the ring-0 memory object.
|
---|
39 | * @param ppVirt Where to store the virtual address of the
|
---|
40 | * allocation.
|
---|
41 | * @param pPhys Where to store the physical address of the
|
---|
42 | * allocation.
|
---|
43 | */
|
---|
44 | static int gimR0HvPageAllocZ(PRTR0MEMOBJ pMemObj, PRTR0PTR ppVirt, PRTHCPHYS pHCPhys)
|
---|
45 | {
|
---|
46 | AssertPtr(pMemObj);
|
---|
47 | AssertPtr(ppVirt);
|
---|
48 | AssertPtr(pHCPhys);
|
---|
49 |
|
---|
50 | int rc = RTR0MemObjAllocCont(pMemObj, PAGE_SIZE, false /* fExecutable */);
|
---|
51 | if (RT_FAILURE(rc))
|
---|
52 | return rc;
|
---|
53 | *ppVirt = RTR0MemObjAddress(*pMemObj);
|
---|
54 | *pHCPhys = RTR0MemObjGetPagePhysAddr(*pMemObj, 0 /* iPage */);
|
---|
55 | ASMMemZero32(*ppVirt, PAGE_SIZE);
|
---|
56 | return VINF_SUCCESS;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Frees and unmaps an allocated physical page.
|
---|
62 | *
|
---|
63 | * @param pMemObj Pointer to the ring-0 memory object.
|
---|
64 | * @param ppVirt Where to re-initialize the virtual address of
|
---|
65 | * allocation as 0.
|
---|
66 | * @param pHCPhys Where to re-initialize the physical address of the
|
---|
67 | * allocation as 0.
|
---|
68 | */
|
---|
69 | static void gimR0HvPageFree(PRTR0MEMOBJ pMemObj, PRTR0PTR ppVirt, PRTHCPHYS pHCPhys)
|
---|
70 | {
|
---|
71 | AssertPtr(pMemObj);
|
---|
72 | AssertPtr(ppVirt);
|
---|
73 | AssertPtr(pHCPhys);
|
---|
74 | if (*pMemObj != NIL_RTR0MEMOBJ)
|
---|
75 | {
|
---|
76 | int rc = RTR0MemObjFree(*pMemObj, true /* fFreeMappings */);
|
---|
77 | AssertRC(rc);
|
---|
78 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
79 | *ppVirt = 0;
|
---|
80 | *pHCPhys = 0;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Updates Hyper-V's reference TSC page.
|
---|
87 | *
|
---|
88 | * @returns VBox status code.
|
---|
89 | * @param pVM Pointer to the VM.
|
---|
90 | * @param u64Offset The computed TSC offset.
|
---|
91 | * @thread EMT.
|
---|
92 | */
|
---|
93 | VMM_INT_DECL(int) GIMR0HvUpdateParavirtTsc(PVM pVM, uint64_t u64Offset)
|
---|
94 | {
|
---|
95 | Assert(GIMIsEnabled(pVM));
|
---|
96 | bool fHvTscEnabled = MSR_GIM_HV_REF_TSC_IS_ENABLED(pVM->gim.s.u.Hv.u64TscPageMsr);
|
---|
97 | if (RT_UNLIKELY(!fHvTscEnabled))
|
---|
98 | return VERR_GIM_PVTSC_NOT_ENABLED;
|
---|
99 |
|
---|
100 | PCGIMHV pcHv = &pVM->gim.s.u.Hv;
|
---|
101 | PCGIMMMIO2REGION pcRegion = &pcHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
|
---|
102 | PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)pcRegion->CTX_SUFF(pvPage);
|
---|
103 | Assert(pRefTsc);
|
---|
104 |
|
---|
105 | RTSpinlockAcquire(pcHv->hSpinlockR0);
|
---|
106 | pRefTsc->i64TscOffset = u64Offset;
|
---|
107 | if (pRefTsc->u32TscSequence < UINT32_C(0xfffffffe))
|
---|
108 | ASMAtomicIncU32(&pRefTsc->u32TscSequence);
|
---|
109 | else
|
---|
110 | ASMAtomicWriteU32(&pRefTsc->u32TscSequence, 1);
|
---|
111 | RTSpinlockRelease(pcHv->hSpinlockR0);
|
---|
112 |
|
---|
113 | Assert(pRefTsc->u32TscSequence != 0);
|
---|
114 | Assert(pRefTsc->u32TscSequence != UINT32_C(0xffffffff));
|
---|
115 | return VINF_SUCCESS;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Does ring-0 per-VM GIM Hyper-V initialization.
|
---|
121 | *
|
---|
122 | * @returns VBox status code.
|
---|
123 | * @param pVM Pointer to the VM.
|
---|
124 | */
|
---|
125 | VMMR0_INT_DECL(int) GIMR0HvInitVM(PVM pVM)
|
---|
126 | {
|
---|
127 | AssertPtr(pVM);
|
---|
128 | Assert(GIMIsEnabled(pVM));
|
---|
129 |
|
---|
130 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
131 | Assert(pHv->hSpinlockR0 == NIL_RTSPINLOCK);
|
---|
132 |
|
---|
133 | int rc = RTSpinlockCreate(&pHv->hSpinlockR0, RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, "Hyper-V");
|
---|
134 | return rc;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Does ring-0 per-VM GIM Hyper-V termination.
|
---|
140 | *
|
---|
141 | * @returns VBox status code.
|
---|
142 | * @param pVM Pointer to the VM.
|
---|
143 | */
|
---|
144 | VMMR0_INT_DECL(int) GIMR0HvTermVM(PVM pVM)
|
---|
145 | {
|
---|
146 | AssertPtr(pVM);
|
---|
147 | Assert(GIMIsEnabled(pVM));
|
---|
148 |
|
---|
149 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
150 | RTSpinlockDestroy(pHv->hSpinlockR0);
|
---|
151 | pHv->hSpinlockR0 = NIL_RTSPINLOCK;
|
---|
152 |
|
---|
153 | return VINF_SUCCESS;
|
---|
154 | }
|
---|
155 |
|
---|