VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/GIM.cpp@ 51560

Last change on this file since 51560 was 51560, checked in by vboxsync, 11 years ago

VMM/GIM: Mapping of GIM MMIO2 regions and Hyper-V provider work.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.6 KB
Line 
1/* $Id: GIM.cpp 51560 2014-06-06 05:17:02Z vboxsync $ */
2/** @file
3 * GIM - Guest Interface Manager.
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/** @page pg_gim GIM - The Guest Interface Manager
19 *
20 * The Guest Interface Manager abstracts an interface provider through which
21 * guests may interact with the hypervisor.
22 *
23 * @see grp_gim
24 *
25 *
26 * @section sec_gim_provider Providers
27 *
28 * A GIM provider implements a particular hypervisor interface such as Microsoft
29 * Hyper-V, Linux KVM and so on. It hooks into various components in the VMM to
30 * ease the guest in running under a recognized, virtualized environment.
31 *
32 * If the GIM provider configured for the VM needs to be recognized by the guest
33 * OS inorder to make use of features supported by the interface. Since it
34 * requires co-operation from the guest OS, a GIM provider is also referred to
35 * as a paravirtualization interface.
36 *
37 * One of the ideas behind this, is primarily for making guests more accurate
38 * and efficient when operating in a virtualized environment. For instance, a
39 * guest OS which interfaces to VirtualBox through a GIM provider may rely on
40 * the provider (and VirtualBox ultimately) for providing the correct TSC
41 * frequency of the host processor and may therefore not have to caliberate the
42 * TSC itself, resulting in higher accuracy and saving several CPU cycles.
43 *
44 * At most, only one GIM provider can be active for a running VM and cannot be
45 * changed during the lifetime of the VM.
46 */
47
48/*******************************************************************************
49* Header Files *
50*******************************************************************************/
51#define LOG_GROUP LOG_GROUP_GIM
52#include <VBox/log.h>
53#include "GIMInternal.h"
54#include <VBox/vmm/vm.h>
55#include <VBox/vmm/hm.h>
56#include <VBox/vmm/ssm.h>
57#include <VBox/vmm/pdmdev.h>
58
59#include <iprt/err.h>
60#include <iprt/string.h>
61
62/* Include all GIM providers. */
63#include "GIMMinimalInternal.h"
64#include "GIMHvInternal.h"
65
66/*******************************************************************************
67* Internal Functions *
68*******************************************************************************/
69static DECLCALLBACK(int) gimR3Save(PVM pVM, PSSMHANDLE pSSM);
70static DECLCALLBACK(int) gimR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
71
72
73/**
74 * Initializes the GIM.
75 *
76 * @returns VBox status code.
77 * @param pVM Pointer to the VM.
78 */
79VMMR3_INT_DECL(int) GIMR3Init(PVM pVM)
80{
81 LogFlow(("GIMR3Init\n"));
82
83 /*
84 * Assert alignment and sizes.
85 */
86 AssertCompile(sizeof(pVM->gim.s) <= sizeof(pVM->gim.padding));
87
88 /*
89 * Register the saved state data unit.
90 */
91 int rc;
92#if 0
93 rc = SSMR3RegisterInternal(pVM, "GIM", 0, GIM_SSM_VERSION, sizeof(GIM),
94 NULL, NULL, NULL,
95 NULL, gimR3Save, NULL,
96 NULL, gimR3Load, NULL);
97 if (RT_FAILURE(rc))
98 return rc;
99#endif
100
101 /*
102 * Read configuration.
103 */
104 PCFGMNODE pCfgNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "GIM/");
105
106 /** @cfgm{GIM/Provider, string}
107 * The name of the GIM provider. The default is "none". */
108 char szProvider[64];
109 rc = CFGMR3QueryStringDef(pCfgNode, "Provider", szProvider, sizeof(szProvider), "None");
110 AssertLogRelRCReturn(rc, rc);
111
112 /** @cfgm{GIM/Version, uint32_t}
113 * The interface version. The default is 0, which means "provide the most
114 * up-to-date implementation". */
115 uint32_t uVersion;
116 rc = CFGMR3QueryU32Def(pCfgNode, "Version", &uVersion, 0 /* default */);
117 AssertLogRelRCReturn(rc, rc);
118
119 /*
120 * Setup the GIM provider for this VM.
121 */
122 LogRel(("GIM: Using provider \"%s\" (Implementation version: %u)\n", szProvider, uVersion));
123 if (!RTStrCmp(szProvider, "None"))
124 {
125 Assert(!pVM->gim.s.fEnabled);
126 pVM->gim.s.enmProviderId = GIMPROVIDERID_NONE;
127 }
128 else
129 {
130 pVM->gim.s.fEnabled = true;
131 pVM->gim.s.u32Version = uVersion;
132 if (!RTStrCmp(szProvider, "Minimal"))
133 {
134 pVM->gim.s.enmProviderId = GIMPROVIDERID_MINIMAL;
135 rc = GIMR3MinimalInit(pVM);
136 }
137 else if (!RTStrCmp(szProvider, "HyperV"))
138 {
139 pVM->gim.s.enmProviderId = GIMPROVIDERID_HYPERV;
140 rc = GIMR3HvInit(pVM);
141 }
142 /** @todo KVM and others. */
143 else
144 {
145 LogRel(("GIM: Provider \"%s\" unknown.\n", szProvider));
146 rc = VERR_GIM_INVALID_PROVIDER;
147 }
148 }
149 return rc;
150}
151
152#if 0
153VMM_INT_DECL(int) GIMR3InitFinalize(PVM pVM)
154{
155 LogFlow(("GIMR3InitFinalize\n"));
156
157 if (!pVM->gim.s.fEnabled)
158 return VINF_SUCCESS;
159
160 switch (pVM->gim.s.enmProviderId)
161 {
162 case GIMPROVIDERID_MINIMAL:
163 {
164 GIMR3MinimalInitFinalize(pVM);
165 break;
166 }
167
168 case GIMPROVIDERID_HYPERV:
169 {
170 GIMR3HvInitFinalize(pVM);
171 break;
172 }
173
174 case GIMPROVIDERID_KVM: /** @todo KVM. */
175 default:
176 {
177 AssertMsgFailed(("Invalid provider Id %#x\n", pVM->gim.s.enmProviderId));
178 break;
179 }
180 }
181}
182#endif
183
184
185/**
186 * Applies relocations to data and code managed by this component. This function
187 * will be called at init and whenever the VMM need to relocate itself inside
188 * the GC.
189 *
190 * @param pVM Pointer to the VM.
191 * @param offDelta Relocation delta relative to old location.
192 */
193VMM_INT_DECL(void) GIMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
194{
195 LogFlow(("GIMR3Relocate\n"));
196
197 if ( !pVM->gim.s.fEnabled
198 || HMIsEnabled(pVM))
199 {
200 return;
201 }
202
203 switch (pVM->gim.s.enmProviderId)
204 {
205 case GIMPROVIDERID_MINIMAL:
206 {
207 GIMR3MinimalRelocate(pVM, offDelta);
208 break;
209 }
210
211 case GIMPROVIDERID_HYPERV:
212 {
213 GIMR3HvRelocate(pVM, offDelta);
214 break;
215 }
216
217 case GIMPROVIDERID_KVM: /** @todo KVM. */
218 default:
219 {
220 AssertMsgFailed(("Invalid provider Id %#x\n", pVM->gim.s.enmProviderId));
221 break;
222 }
223 }
224}
225
226
227/**
228 * Execute state save operation.
229 *
230 * @returns VBox status code.
231 * @param pVM Pointer to the VM.
232 * @param pSSM SSM operation handle.
233 */
234DECLCALLBACK(int) gimR3Save(PVM pVM, PSSMHANDLE pSSM)
235{
236 /** @todo save state. */
237 return VINF_SUCCESS;
238}
239
240
241/**
242 * Execute state load operation.
243 *
244 * @returns VBox status code.
245 * @param pVM Pointer to the VM.
246 * @param pSSM SSM operation handle.
247 * @param uVersion Data layout version.
248 * @param uPass The data pass.
249 */
250DECLCALLBACK(int) gimR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
251{
252 /** @todo load state. */
253 return VINF_SUCCESS;
254}
255
256
257/**
258 * Terminates the GIM.
259 *
260 * Termination means cleaning up and freeing all resources,
261 * the VM itself is, at this point, powered off or suspended.
262 *
263 * @returns VBox status code.
264 * @param pVM Pointer to the VM.
265 */
266VMMR3_INT_DECL(int) GIMR3Term(PVM pVM)
267{
268 return VINF_SUCCESS;
269}
270
271
272/**
273 * The VM is being reset.
274 *
275 * For the GIM component this means unmapping and unregistering MMIO2 regions
276 * and other provider-specific resets.
277 *
278 * @returns VBox status code.
279 * @param pVM Pointer to the VM.
280 */
281VMMR3_INT_DECL(void) GIMR3Reset(PVM pVM)
282{
283 if (!pVM->gim.s.fEnabled)
284 return;
285
286 switch (pVM->gim.s.enmProviderId)
287 {
288 case GIMPROVIDERID_HYPERV:
289 return GIMR3HvReset(pVM);
290
291 default:
292 break;
293 }
294}
295
296
297/**
298 * Registers the GIM device with VMM.
299 *
300 * @param pVM Pointer to the VM.
301 * @param pDevIns Pointer to the GIM device instance.
302 */
303VMMR3DECL(void) GIMR3GimDeviceRegister(PVM pVM, PPDMDEVINS pDevIns)
304{
305 pVM->gim.s.pDevInsR3 = pDevIns;
306}
307
308
309/**
310 * Returns the array of MMIO2 regions that are expected to be registered and
311 * later mapped into the guest-physical address space for the GIM provider
312 * configured for the VM.
313 *
314 * @returns Pointer to an array of GIM MMIO2 regions, may return NULL.
315 * @param pVM Pointer to the VM.
316 * @param pcRegions Where to store the number of items in the array.
317 *
318 * @remarks The caller does not own and therefore must -NOT- try to free the
319 * returned pointer.
320 */
321VMMR3DECL(PGIMMMIO2REGION) GIMR3GetMmio2Regions(PVM pVM, uint32_t *pcRegions)
322{
323 Assert(pVM);
324 Assert(pcRegions);
325
326 *pcRegions = 0;
327 if (!pVM->gim.s.fEnabled)
328 return NULL;
329
330 switch (pVM->gim.s.enmProviderId)
331 {
332 case GIMPROVIDERID_HYPERV:
333 return GIMR3HvGetMmio2Regions(pVM, pcRegions);
334
335 case GIMPROVIDERID_KVM: /** @todo KVM. */
336 default:
337 break;
338 }
339
340 return NULL;
341}
342
343
344/**
345 * Unmaps a registered MMIO2 region in the guest address space and removes any
346 * access handlers for it.
347 *
348 * @returns VBox status code.
349 * @param pVM Pointer to the VM.
350 * @param pRegion Pointer to the GIM MMIO2 region.
351 */
352VMM_INT_DECL(int) GIMR3Mmio2Unmap(PVM pVM, PGIMMMIO2REGION pRegion)
353{
354 AssertPtr(pVM);
355 AssertPtr(pRegion);
356
357 PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
358 AssertPtr(pDevIns);
359 if (pRegion->fMapped)
360 {
361 PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
362 int rc = PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, pRegion->GCPhysPage);
363 if (RT_SUCCESS(rc))
364 {
365 pRegion->fMapped = false;
366 pRegion->GCPhysPage = NIL_RTGCPHYS;
367 }
368 }
369 return VINF_SUCCESS;
370}
371
372
373/**
374 * Write access handler for a mapped MMIO2 region that presently ignores writes.
375 *
376 * @returns VBox status code.
377 * @param pVM Pointer to the VM.
378 * @param GCPhys The guest-physical address of the region.
379 * @param pvPhys Pointer to the region in the guest address space.
380 * @param pvBuf Pointer to the data being read/written.
381 * @param cbBuf The size of the buffer in @a pvBuf.
382 * @param enmAccessType The type of access.
383 * @param pvUser User argument (NULL, not used).
384 */
385static DECLCALLBACK(int) gimR3Mmio2PageWriteHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf,
386 PGMACCESSTYPE enmAccessType, void *pvUser)
387{
388 /*
389 * Ignore writes to the mapped MMIO2 page.
390 */
391 Assert(enmAccessType == PGMACCESSTYPE_WRITE);
392 return VINF_SUCCESS; /** @todo Hyper-V says we should #GP(0) fault for writes to the Hypercall and TSC page. */
393}
394
395
396/**
397 * Maps a registered MMIO2 region in the guest address space. The region will be
398 * made read-only and writes from the guest will be ignored.
399 *
400 * @returns VBox status code.
401 * @param pVM Pointer to the VM.
402 * @param pRegion Pointer to the GIM MMIO2 region.
403 * @param GCPhysRegion Where in the guest address space to map the region.
404 * @param pszDesc Description of the region being mapped.
405 */
406VMM_INT_DECL(int) GIMR3Mmio2Map(PVM pVM, PGIMMMIO2REGION pRegion, RTGCPHYS GCPhysRegion, const char *pszDesc)
407{
408 PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
409 AssertPtr(pDevIns);
410
411 /* The guest-physical address must be page-aligned. */
412 if (GCPhysRegion & PAGE_OFFSET_MASK)
413 {
414 LogFunc(("%s: %#RGp not paging aligned\n", pszDesc, GCPhysRegion));
415 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
416 }
417
418 /* Allow only normal pages to be overlaid using our MMIO2 pages (disallow MMIO, ROM, reserved pages). */
419 /** @todo Hyper-V doesn't seem to be very strict about this, may be relax
420 * later if some guest really requires it. */
421 if (!PGMPhysIsGCPhysNormal(pVM, GCPhysRegion))
422 {
423 LogFunc(("%s: %#RGp is not normal memory\n", pszDesc, GCPhysRegion));
424 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
425 }
426
427 if (pRegion->fMapped)
428 {
429 LogFunc(("%s: A mapping for %#RGp already exists.\n", pszDesc, GCPhysRegion));
430 return VERR_PGM_MAPPING_CONFLICT;
431 }
432
433 /*
434 * Map the MMIO2 region over the guest-physical address.
435 */
436 int rc = PDMDevHlpMMIO2Map(pDevIns, pRegion->iRegion, GCPhysRegion);
437 if (RT_SUCCESS(rc))
438 {
439 /*
440 * Install access-handlers for the mapped page to prevent (ignore) writes to it from the guest.
441 */
442 rc = PGMR3HandlerPhysicalRegister(pVM,
443 PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
444 GCPhysRegion, GCPhysRegion + (pRegion->cbRegion - 1),
445 gimR3Mmio2PageWriteHandler, NULL /* pvUserR3 */,
446 NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NIL_RTR0PTR /* pvUserR0 */,
447 NULL /* pszModRC */, NULL /* pszHandlerRC */, NIL_RTRCPTR /* pvUserRC */,
448 pszDesc);
449 if (RT_SUCCESS(rc))
450 {
451 pRegion->fMapped = true;
452 pRegion->GCPhysPage = GCPhysRegion;
453 return VINF_SUCCESS;
454 }
455
456 PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, GCPhysRegion);
457 }
458
459 return rc;
460}
461
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette