1 | /* $Id: GIMAll.cpp 54819 2015-03-17 17:58:30Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * GIM - Guest Interface Manager - 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 "GIMInternal.h"
|
---|
24 | #include <VBox/err.h>
|
---|
25 | #include <VBox/vmm/vm.h>
|
---|
26 |
|
---|
27 | /* Include all the providers. */
|
---|
28 | #include "GIMHvInternal.h"
|
---|
29 | #include "GIMMinimalInternal.h"
|
---|
30 |
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Checks whether GIM is being used by this VM.
|
---|
34 | *
|
---|
35 | * @retval @c true if used.
|
---|
36 | * @retval @c false if no GIM provider ("none") is used.
|
---|
37 | *
|
---|
38 | * @param pVM Pointer to the VM.
|
---|
39 | */
|
---|
40 | VMMDECL(bool) GIMIsEnabled(PVM pVM)
|
---|
41 | {
|
---|
42 | return pVM->gim.s.enmProviderId != GIMPROVIDERID_NONE;
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Gets the GIM provider configured for this VM.
|
---|
48 | *
|
---|
49 | * @returns The GIM provider Id.
|
---|
50 | * @param pVM Pointer to the VM.
|
---|
51 | */
|
---|
52 | VMMDECL(GIMPROVIDERID) GIMGetProvider(PVM pVM)
|
---|
53 | {
|
---|
54 | return pVM->gim.s.enmProviderId;
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Returns whether the guest has configured and enabled calls to the hypervisor.
|
---|
60 | *
|
---|
61 | * @returns true if hypercalls are enabled and usable, false otherwise.
|
---|
62 | * @param pVCpu Pointer to the VMCPU.
|
---|
63 | */
|
---|
64 | VMM_INT_DECL(bool) GIMAreHypercallsEnabled(PVMCPU pVCpu)
|
---|
65 | {
|
---|
66 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
67 | if (!GIMIsEnabled(pVM))
|
---|
68 | return false;
|
---|
69 |
|
---|
70 | switch (pVM->gim.s.enmProviderId)
|
---|
71 | {
|
---|
72 | case GIMPROVIDERID_HYPERV:
|
---|
73 | return gimHvAreHypercallsEnabled(pVCpu);
|
---|
74 |
|
---|
75 | default:
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Implements a GIM hypercall with the provider configured for the VM.
|
---|
83 | *
|
---|
84 | * @returns VBox status code.
|
---|
85 | * @param pVCpu Pointer to the VMCPU.
|
---|
86 | * @param pCtx Pointer to the guest-CPU context.
|
---|
87 | */
|
---|
88 | VMM_INT_DECL(int) GIMHypercall(PVMCPU pVCpu, PCPUMCTX pCtx)
|
---|
89 | {
|
---|
90 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
91 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
92 |
|
---|
93 | if (RT_UNLIKELY(!GIMIsEnabled(pVM)))
|
---|
94 | return VERR_GIM_NOT_ENABLED;
|
---|
95 |
|
---|
96 | switch (pVM->gim.s.enmProviderId)
|
---|
97 | {
|
---|
98 | case GIMPROVIDERID_HYPERV:
|
---|
99 | return gimHvHypercall(pVCpu, pCtx);
|
---|
100 |
|
---|
101 | default:
|
---|
102 | AssertMsgFailed(("GIMHypercall: for unknown provider %u\n", pVM->gim.s.enmProviderId));
|
---|
103 | return VERR_GIM_IPE_3;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Returns whether the guest has configured and setup the use of paravirtualized
|
---|
110 | * TSC.
|
---|
111 | *
|
---|
112 | * Paravirtualized TSCs are per-VM and the rest of the execution engine logic
|
---|
113 | * relies on that.
|
---|
114 | *
|
---|
115 | * @returns true if enabled and usable, false otherwise.
|
---|
116 | * @param pVM Pointer to the VM.
|
---|
117 | */
|
---|
118 | VMM_INT_DECL(bool) GIMIsParavirtTscEnabled(PVM pVM)
|
---|
119 | {
|
---|
120 | switch (pVM->gim.s.enmProviderId)
|
---|
121 | {
|
---|
122 | case GIMPROVIDERID_HYPERV:
|
---|
123 | return gimHvIsParavirtTscEnabled(pVM);
|
---|
124 |
|
---|
125 | default:
|
---|
126 | break;
|
---|
127 | }
|
---|
128 | return false;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Invokes the read-MSR handler for the GIM provider configured for the VM.
|
---|
134 | *
|
---|
135 | * @returns Strict VBox status code like CPUMQueryGuestMsr.
|
---|
136 | * @retval VINF_CPUM_R3_MSR_READ
|
---|
137 | * @retval VERR_CPUM_RAISE_GP_0
|
---|
138 | *
|
---|
139 | * @param pVCpu Pointer to the VMCPU.
|
---|
140 | * @param idMsr The MSR to read.
|
---|
141 | * @param pRange The range this MSR belongs to.
|
---|
142 | * @param puValue Where to store the MSR value read.
|
---|
143 | */
|
---|
144 | VMM_INT_DECL(VBOXSTRICTRC) GIMReadMsr(PVMCPU pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t *puValue)
|
---|
145 | {
|
---|
146 | Assert(pVCpu);
|
---|
147 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
148 | Assert(GIMIsEnabled(pVM));
|
---|
149 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
150 |
|
---|
151 | switch (pVM->gim.s.enmProviderId)
|
---|
152 | {
|
---|
153 | case GIMPROVIDERID_HYPERV:
|
---|
154 | return gimHvReadMsr(pVCpu, idMsr, pRange, puValue);
|
---|
155 |
|
---|
156 | case GIMPROVIDERID_KVM:
|
---|
157 | return gimKvmReadMsr(pVCpu, idMsr, pRange, puValue);
|
---|
158 |
|
---|
159 | default:
|
---|
160 | AssertMsgFailed(("GIMReadMsr: for unknown provider %u idMsr=%#RX32 -> #GP(0)", pVM->gim.s.enmProviderId, idMsr));
|
---|
161 | return VERR_CPUM_RAISE_GP_0;
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Invokes the write-MSR handler for the GIM provider configured for the VM.
|
---|
168 | *
|
---|
169 | * @returns Strict VBox status code like CPUMSetGuestMsr.
|
---|
170 | * @retval VINF_CPUM_R3_MSR_WRITE
|
---|
171 | * @retval VERR_CPUM_RAISE_GP_0
|
---|
172 | *
|
---|
173 | * @param pVCpu Pointer to the VMCPU.
|
---|
174 | * @param idMsr The MSR to write.
|
---|
175 | * @param pRange The range this MSR belongs to.
|
---|
176 | * @param uValue The value to set, ignored bits masked.
|
---|
177 | * @param uRawValue The raw value with the ignored bits not masked.
|
---|
178 | */
|
---|
179 | VMM_INT_DECL(VBOXSTRICTRC) GIMWriteMsr(PVMCPU pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t uValue, uint64_t uRawValue)
|
---|
180 | {
|
---|
181 | AssertPtr(pVCpu);
|
---|
182 | NOREF(uValue);
|
---|
183 |
|
---|
184 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
185 | Assert(GIMIsEnabled(pVM));
|
---|
186 | VMCPU_ASSERT_EMT(pVCpu);
|
---|
187 |
|
---|
188 | switch (pVM->gim.s.enmProviderId)
|
---|
189 | {
|
---|
190 | case GIMPROVIDERID_HYPERV:
|
---|
191 | return gimHvWriteMsr(pVCpu, idMsr, pRange, uRawValue);
|
---|
192 |
|
---|
193 | case GIMPROVIDERID_KVM:
|
---|
194 | return gimKvmWriteMsr(pVCpu, idMsr, pRange, uRawValue);
|
---|
195 |
|
---|
196 | default:
|
---|
197 | AssertMsgFailed(("GIMWriteMsr: for unknown provider %u idMsr=%#RX32 -> #GP(0)", pVM->gim.s.enmProviderId, idMsr));
|
---|
198 | return VERR_CPUM_RAISE_GP_0;
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|