VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/GIMAll.cpp@ 55118

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

VMM: GIM raw-mode support.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1/* $Id: GIMAll.cpp 55118 2015-04-07 15:21:45Z 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 */
40VMMDECL(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 */
52VMMDECL(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 */
64VMM_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 case GIMPROVIDERID_KVM:
76 return gimKvmAreHypercallsEnabled(pVCpu);
77
78 default:
79 return false;
80 }
81}
82
83
84/**
85 * Implements a GIM hypercall with the provider configured for the VM.
86 *
87 * @returns VBox status code.
88 * @param pVCpu Pointer to the VMCPU.
89 * @param pCtx Pointer to the guest-CPU context.
90 */
91VMM_INT_DECL(int) GIMHypercall(PVMCPU pVCpu, PCPUMCTX pCtx)
92{
93 PVM pVM = pVCpu->CTX_SUFF(pVM);
94 VMCPU_ASSERT_EMT(pVCpu);
95
96 if (RT_UNLIKELY(!GIMIsEnabled(pVM)))
97 return VERR_GIM_NOT_ENABLED;
98
99 switch (pVM->gim.s.enmProviderId)
100 {
101 case GIMPROVIDERID_HYPERV:
102 return gimHvHypercall(pVCpu, pCtx);
103
104 case GIMPROVIDERID_KVM:
105 return gimKvmHypercall(pVCpu, pCtx);
106
107 default:
108 AssertMsgFailed(("GIMHypercall: for provider %u not available/implemented\n", pVM->gim.s.enmProviderId));
109 return VERR_GIM_HYPERCALLS_NOT_AVAILABLE;
110 }
111}
112
113
114/**
115 * Returns whether the guest has configured and setup the use of paravirtualized
116 * TSC.
117 *
118 * Paravirtualized TSCs are per-VM and the rest of the execution engine logic
119 * relies on that.
120 *
121 * @returns true if enabled and usable, false otherwise.
122 * @param pVM Pointer to the VM.
123 */
124VMM_INT_DECL(bool) GIMIsParavirtTscEnabled(PVM pVM)
125{
126 switch (pVM->gim.s.enmProviderId)
127 {
128 case GIMPROVIDERID_HYPERV:
129 return gimHvIsParavirtTscEnabled(pVM);
130
131 case GIMPROVIDERID_KVM:
132 return gimKvmIsParavirtTscEnabled(pVM);
133
134 default:
135 break;
136 }
137 return false;
138}
139
140
141/**
142 * Whether #UD exceptions in the guest needs to be intercepted by the GIM
143 * provider.
144 *
145 * At the moment, the reason why this isn't a more generic interface wrt to
146 * exceptions is because of performance (each VM-exit would have to manually
147 * check whether or not GIM needs to be notified). Left as a todo for later if
148 * really required.
149 *
150 * @returns true if needed, false otherwise.
151 * @param pVM Pointer to the VM.
152 */
153VMM_INT_DECL(bool) GIMShouldTrapXcptUD(PVM pVM)
154{
155 if (!GIMIsEnabled(pVM))
156 return false;
157
158 switch (pVM->gim.s.enmProviderId)
159 {
160 case GIMPROVIDERID_KVM:
161 return gimKvmShouldTrapXcptUD(pVM);
162
163 default:
164 return false;
165 }
166}
167
168
169/**
170 * Exception handler for #UD when requested by the GIM provider.
171 *
172 * @param pVCpu Pointer to the VMCPU.
173 * @param pCtx Pointer to the guest-CPU context.
174 * @param pDis Pointer to the disassembled instruction state at RIP.
175 * Optional, can be NULL.
176 */
177VMM_INT_DECL(int) GIMXcptUD(PVMCPU pVCpu, PCPUMCTX pCtx, PDISCPUSTATE pDis)
178{
179 PVM pVM = pVCpu->CTX_SUFF(pVM);
180 Assert(GIMIsEnabled(pVM));
181
182 switch (pVM->gim.s.enmProviderId)
183 {
184 case GIMPROVIDERID_KVM:
185 return gimKvmXcptUD(pVCpu, pCtx, pDis);
186
187 default:
188 return VERR_GIM_OPERATION_FAILED;
189 }
190}
191
192
193/**
194 * Invokes the read-MSR handler for the GIM provider configured for the VM.
195 *
196 * @returns Strict VBox status code like CPUMQueryGuestMsr.
197 * @retval VINF_CPUM_R3_MSR_READ
198 * @retval VERR_CPUM_RAISE_GP_0
199 *
200 * @param pVCpu Pointer to the VMCPU.
201 * @param idMsr The MSR to read.
202 * @param pRange The range this MSR belongs to.
203 * @param puValue Where to store the MSR value read.
204 */
205VMM_INT_DECL(VBOXSTRICTRC) GIMReadMsr(PVMCPU pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t *puValue)
206{
207 Assert(pVCpu);
208 PVM pVM = pVCpu->CTX_SUFF(pVM);
209 Assert(GIMIsEnabled(pVM));
210 VMCPU_ASSERT_EMT(pVCpu);
211
212 switch (pVM->gim.s.enmProviderId)
213 {
214 case GIMPROVIDERID_HYPERV:
215 return gimHvReadMsr(pVCpu, idMsr, pRange, puValue);
216
217 case GIMPROVIDERID_KVM:
218 return gimKvmReadMsr(pVCpu, idMsr, pRange, puValue);
219
220 default:
221 AssertMsgFailed(("GIMReadMsr: for unknown provider %u idMsr=%#RX32 -> #GP(0)", pVM->gim.s.enmProviderId, idMsr));
222 return VERR_CPUM_RAISE_GP_0;
223 }
224}
225
226
227/**
228 * Invokes the write-MSR handler for the GIM provider configured for the VM.
229 *
230 * @returns Strict VBox status code like CPUMSetGuestMsr.
231 * @retval VINF_CPUM_R3_MSR_WRITE
232 * @retval VERR_CPUM_RAISE_GP_0
233 *
234 * @param pVCpu Pointer to the VMCPU.
235 * @param idMsr The MSR to write.
236 * @param pRange The range this MSR belongs to.
237 * @param uValue The value to set, ignored bits masked.
238 * @param uRawValue The raw value with the ignored bits not masked.
239 */
240VMM_INT_DECL(VBOXSTRICTRC) GIMWriteMsr(PVMCPU pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t uValue, uint64_t uRawValue)
241{
242 AssertPtr(pVCpu);
243 NOREF(uValue);
244
245 PVM pVM = pVCpu->CTX_SUFF(pVM);
246 Assert(GIMIsEnabled(pVM));
247 VMCPU_ASSERT_EMT(pVCpu);
248
249 switch (pVM->gim.s.enmProviderId)
250 {
251 case GIMPROVIDERID_HYPERV:
252 return gimHvWriteMsr(pVCpu, idMsr, pRange, uRawValue);
253
254 case GIMPROVIDERID_KVM:
255 return gimKvmWriteMsr(pVCpu, idMsr, pRange, uRawValue);
256
257 default:
258 AssertMsgFailed(("GIMWriteMsr: for unknown provider %u idMsr=%#RX32 -> #GP(0)", pVM->gim.s.enmProviderId, idMsr));
259 return VERR_CPUM_RAISE_GP_0;
260 }
261}
262
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