VirtualBox

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

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

VMM/GIM: Get rid of separate fEnabled field, just use the provider to figure out whether it's enabled or not.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.6 KB
Line 
1/* $Id: GIM.cpp 52767 2014-09-16 16:51:51Z 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 = SSMR3RegisterInternal(pVM, "GIM", 0 /* uInstance */, GIM_SAVED_STATE_VERSION, sizeof(GIM),
92 NULL /* pfnLivePrep */, NULL /* pfnLiveExec */, NULL /* pfnLiveVote*/,
93 NULL /* pfnSavePrep */, gimR3Save, NULL /* pfnSaveDone */,
94 NULL /* pfnLoadPrep */, gimR3Load, NULL /* pfnLoadDone */);
95 if (RT_FAILURE(rc))
96 return rc;
97
98 /*
99 * Read configuration.
100 */
101 PCFGMNODE pCfgNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "GIM/");
102
103 /** @cfgm{/GIM/Provider, string}
104 * The name of the GIM provider. The default is "none". */
105 char szProvider[64];
106 rc = CFGMR3QueryStringDef(pCfgNode, "Provider", szProvider, sizeof(szProvider), "None");
107 AssertLogRelRCReturn(rc, rc);
108
109 /** @cfgm{/GIM/Version, uint32_t}
110 * The interface version. The default is 0, which means "provide the most
111 * up-to-date implementation". */
112 uint32_t uVersion;
113 rc = CFGMR3QueryU32Def(pCfgNode, "Version", &uVersion, 0 /* default */);
114 AssertLogRelRCReturn(rc, rc);
115
116 /*
117 * Setup the GIM provider for this VM.
118 */
119 LogRel(("GIM: Using provider \"%s\" (Implementation version: %u)\n", szProvider, uVersion));
120 if (!RTStrCmp(szProvider, "None"))
121 pVM->gim.s.enmProviderId = GIMPROVIDERID_NONE;
122 else
123 {
124 pVM->gim.s.u32Version = uVersion;
125 /** @todo r=bird: Because u32Version is saved, it should be translated to the
126 * 'most up-to-date implementation' version number when 0. Otherwise,
127 * we'll have abiguities when loading the state of older VMs. */
128 if (!RTStrCmp(szProvider, "Minimal"))
129 {
130 pVM->gim.s.enmProviderId = GIMPROVIDERID_MINIMAL;
131 rc = GIMR3MinimalInit(pVM);
132 }
133 else if (!RTStrCmp(szProvider, "HyperV"))
134 {
135 pVM->gim.s.enmProviderId = GIMPROVIDERID_HYPERV;
136 rc = GIMR3HvInit(pVM);
137 }
138 /** @todo KVM and others. */
139 else
140 rc = VMR3SetError(pVM->pUVM, VERR_GIM_INVALID_PROVIDER, RT_SRC_POS, "Provider \"%s\" unknown.", szProvider);
141 }
142 return rc;
143}
144
145
146/**
147 * Initializes the remaining bits of the GIM provider.
148 *
149 * This is called after initializing HM and most other VMM components.
150 *
151 * @returns VBox status code.
152 * @param pVM Pointer to the VM.
153 * @param enmWhat What has been completed.
154 * @thread EMT(0)
155 */
156VMMR3_INT_DECL(int) GIMR3InitCompleted(PVM pVM)
157{
158 switch (pVM->gim.s.enmProviderId)
159 {
160 case GIMPROVIDERID_MINIMAL:
161 return GIMR3MinimalInitCompleted(pVM);
162
163 case GIMPROVIDERID_HYPERV:
164 return GIMR3HvInitCompleted(pVM);
165
166 default:
167 break;
168 }
169 return VINF_SUCCESS;
170}
171
172
173/**
174 * Applies relocations to data and code managed by this component.
175 *
176 * This function will be called at init and whenever the VMM need to relocate
177 * itself inside the GC.
178 *
179 * @param pVM Pointer to the VM.
180 * @param offDelta Relocation delta relative to old location.
181 */
182VMM_INT_DECL(void) GIMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
183{
184 LogFlow(("GIMR3Relocate\n"));
185
186 if ( pVM->gim.s.enmProviderId == GIMPROVIDERID_NONE
187 || HMIsEnabled(pVM))
188 {
189 return;
190 }
191
192 switch (pVM->gim.s.enmProviderId)
193 {
194 case GIMPROVIDERID_MINIMAL:
195 {
196 GIMR3MinimalRelocate(pVM, offDelta);
197 break;
198 }
199
200 case GIMPROVIDERID_HYPERV:
201 {
202 GIMR3HvRelocate(pVM, offDelta);
203 break;
204 }
205
206 case GIMPROVIDERID_KVM: /** @todo KVM. */
207 default:
208 {
209 AssertMsgFailed(("Invalid provider Id %#x\n", pVM->gim.s.enmProviderId));
210 break;
211 }
212 }
213}
214
215
216/**
217 * Executes state-save operation.
218 *
219 * @returns VBox status code.
220 * @param pVM Pointer to the VM.
221 * @param pSSM SSM operation handle.
222 */
223DECLCALLBACK(int) gimR3Save(PVM pVM, PSSMHANDLE pSSM)
224{
225 AssertReturn(pVM, VERR_INVALID_PARAMETER);
226 AssertReturn(pSSM, VERR_SSM_INVALID_STATE);
227
228 /** @todo Save per-CPU data. */
229 int rc = VINF_SUCCESS;
230#if 0
231 SSMR3PutU32(pSSM, pVM->cCpus);
232 for (VMCPUID i = 0; i < pVM->cCpus; i++)
233 {
234 rc = SSMR3PutXYZ(pSSM, pVM->aCpus[i].gim.s.XYZ);
235 }
236#endif
237
238 /*
239 * Save per-VM data.
240 */
241 SSMR3PutU32(pSSM, pVM->gim.s.enmProviderId);
242 SSMR3PutU32(pSSM, pVM->gim.s.u32Version);
243
244 /*
245 * Save provider-specific data.
246 */
247 switch (pVM->gim.s.enmProviderId)
248 {
249 case GIMPROVIDERID_HYPERV:
250 rc = GIMR3HvSave(pVM, pSSM);
251 AssertRCReturn(rc, rc);
252 break;
253
254 default:
255 break;
256 }
257
258 return rc;
259}
260
261
262/**
263 * Execute state load operation.
264 *
265 * @returns VBox status code.
266 * @param pVM Pointer to the VM.
267 * @param pSSM SSM operation handle.
268 * @param uVersion Data layout version.
269 * @param uPass The data pass.
270 */
271DECLCALLBACK(int) gimR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
272{
273 if (uPass != SSM_PASS_FINAL)
274 return VINF_SUCCESS;
275 if (uVersion != GIM_SAVED_STATE_VERSION)
276 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
277
278
279 /** @todo Load per-CPU data. */
280 int rc;
281#if 0
282 for (VMCPUID i = 0; i < pVM->cCpus; i++)
283 {
284 rc = SSMR3PutXYZ(pSSM, pVM->aCpus[i].gim.s.XYZ);
285 }
286#endif
287
288 /*
289 * Load per-VM data.
290 */
291 uint32_t uProviderId;
292 uint32_t uProviderVersion;
293
294 rc = SSMR3GetU32(pSSM, &uProviderId); AssertRCReturn(rc, rc);
295 rc = SSMR3GetU32(pSSM, &uProviderVersion); AssertRCReturn(rc, rc);
296
297 if ((GIMPROVIDERID)uProviderId != pVM->gim.s.enmProviderId)
298 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Saved GIM provider %u differs from the configured one (%u)."),
299 uProviderId, pVM->gim.s.enmProviderId);
300#if 0 /** @todo r=bird: Figure out what you mean to do here with the version. */
301 if (uProviderVersion != pVM->gim.s.u32Version)
302 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Saved GIM provider version %u differs from the configured one (%u)."),
303 uProviderVersion, pVM->gim.s.u32Version);
304#else
305 pVM->gim.s.u32Version = uProviderVersion;
306#endif
307
308 /*
309 * Load provider-specific data.
310 */
311 switch (pVM->gim.s.enmProviderId)
312 {
313 case GIMPROVIDERID_HYPERV:
314 rc = GIMR3HvLoad(pVM, pSSM, uVersion);
315 AssertRCReturn(rc, rc);
316 break;
317
318 default:
319 break;
320 }
321
322 return VINF_SUCCESS;
323}
324
325
326/**
327 * Terminates the GIM.
328 *
329 * Termination means cleaning up and freeing all resources,
330 * the VM itself is, at this point, powered off or suspended.
331 *
332 * @returns VBox status code.
333 * @param pVM Pointer to the VM.
334 */
335VMMR3_INT_DECL(int) GIMR3Term(PVM pVM)
336{
337 switch (pVM->gim.s.enmProviderId)
338 {
339 case GIMPROVIDERID_HYPERV:
340 return GIMR3HvTerm(pVM);
341
342 default:
343 break;
344 }
345 return VINF_SUCCESS;
346}
347
348
349/**
350 * The VM is being reset.
351 *
352 * For the GIM component this means unmapping and unregistering MMIO2 regions
353 * and other provider-specific resets.
354 *
355 * @returns VBox status code.
356 * @param pVM Pointer to the VM.
357 */
358VMMR3_INT_DECL(void) GIMR3Reset(PVM pVM)
359{
360 switch (pVM->gim.s.enmProviderId)
361 {
362 case GIMPROVIDERID_HYPERV:
363 return GIMR3HvReset(pVM);
364
365 default:
366 break;
367 }
368}
369
370
371/**
372 * Registers the GIM device with VMM.
373 *
374 * @param pVM Pointer to the VM.
375 * @param pDevIns Pointer to the GIM device instance.
376 */
377VMMR3DECL(void) GIMR3GimDeviceRegister(PVM pVM, PPDMDEVINS pDevIns)
378{
379 pVM->gim.s.pDevInsR3 = pDevIns;
380}
381
382
383/**
384 * Returns the array of MMIO2 regions that are expected to be registered and
385 * later mapped into the guest-physical address space for the GIM provider
386 * configured for the VM.
387 *
388 * @returns Pointer to an array of GIM MMIO2 regions, may return NULL.
389 * @param pVM Pointer to the VM.
390 * @param pcRegions Where to store the number of items in the array.
391 *
392 * @remarks The caller does not own and therefore must -NOT- try to free the
393 * returned pointer.
394 */
395VMMR3DECL(PGIMMMIO2REGION) GIMR3GetMmio2Regions(PVM pVM, uint32_t *pcRegions)
396{
397 Assert(pVM);
398 Assert(pcRegions);
399
400 *pcRegions = 0;
401 switch (pVM->gim.s.enmProviderId)
402 {
403 case GIMPROVIDERID_HYPERV:
404 return GIMR3HvGetMmio2Regions(pVM, pcRegions);
405
406 case GIMPROVIDERID_KVM: /** @todo KVM. */
407 default:
408 break;
409 }
410
411 return NULL;
412}
413
414
415/**
416 * Unmaps a registered MMIO2 region in the guest address space and removes any
417 * access handlers for it.
418 *
419 * @returns VBox status code.
420 * @param pVM Pointer to the VM.
421 * @param pRegion Pointer to the GIM MMIO2 region.
422 */
423VMMR3_INT_DECL(int) GIMR3Mmio2Unmap(PVM pVM, PGIMMMIO2REGION pRegion)
424{
425 AssertPtr(pVM);
426 AssertPtr(pRegion);
427
428 PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
429 AssertPtr(pDevIns);
430 if (pRegion->fMapped)
431 {
432 int rc = PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
433 AssertRC(rc);
434
435 rc = PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, pRegion->GCPhysPage);
436 if (RT_SUCCESS(rc))
437 {
438 pRegion->fMapped = false;
439 pRegion->GCPhysPage = NIL_RTGCPHYS;
440 }
441 }
442 return VINF_SUCCESS;
443}
444
445
446/**
447 * Write access handler for a mapped MMIO2 region. At present, this handler
448 * simply ignores writes.
449 *
450 * In the future we might want to let the GIM provider decide what the handler
451 * should do (like throwing #GP faults).
452 *
453 * @returns VBox status code.
454 * @param pVM Pointer to the VM.
455 * @param GCPhys The guest-physical address of the region.
456 * @param pvPhys Pointer to the region in the guest address space.
457 * @param pvBuf Pointer to the data being read/written.
458 * @param cbBuf The size of the buffer in @a pvBuf.
459 * @param enmAccessType The type of access.
460 * @param pvUser User argument (NULL, not used).
461 */
462static DECLCALLBACK(int) gimR3Mmio2WriteHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf,
463 PGMACCESSTYPE enmAccessType, void *pvUser)
464{
465 /*
466 * Ignore writes to the mapped MMIO2 page.
467 */
468 Assert(enmAccessType == PGMACCESSTYPE_WRITE);
469 return VINF_SUCCESS; /** @todo Hyper-V says we should #GP(0) fault for writes to the Hypercall and TSC page. */
470}
471
472
473/**
474 * Maps a registered MMIO2 region in the guest address space. The region will be
475 * made read-only and writes from the guest will be ignored.
476 *
477 * @returns VBox status code.
478 * @param pVM Pointer to the VM.
479 * @param pRegion Pointer to the GIM MMIO2 region.
480 * @param GCPhysRegion Where in the guest address space to map the region.
481 */
482VMMR3_INT_DECL(int) GIMR3Mmio2Map(PVM pVM, PGIMMMIO2REGION pRegion, RTGCPHYS GCPhysRegion)
483{
484 PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
485 AssertPtr(pDevIns);
486
487 /* The guest-physical address must be page-aligned. */
488 if (GCPhysRegion & PAGE_OFFSET_MASK)
489 {
490 LogFunc(("%s: %#RGp not paging aligned\n", pRegion->szDescription, GCPhysRegion));
491 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
492 }
493
494 /* Allow only normal pages to be overlaid using our MMIO2 pages (disallow MMIO, ROM, reserved pages). */
495 /** @todo Hyper-V doesn't seem to be very strict about this, may be relax
496 * later if some guest really requires it. */
497 if (!PGMPhysIsGCPhysNormal(pVM, GCPhysRegion))
498 {
499 LogFunc(("%s: %#RGp is not normal memory\n", pRegion->szDescription, GCPhysRegion));
500 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
501 }
502
503 if (!pRegion->fRegistered)
504 {
505 LogFunc(("%s: Region has not been registered.\n"));
506 return VERR_GIM_IPE_1;
507 }
508
509 /*
510 * Map the MMIO2 region over the specified guest-physical address.
511 */
512 int rc = PDMDevHlpMMIO2Map(pDevIns, pRegion->iRegion, GCPhysRegion);
513 if (RT_SUCCESS(rc))
514 {
515 /*
516 * Install access-handlers for the mapped page to prevent (ignore) writes to it from the guest.
517 */
518 rc = PGMR3HandlerPhysicalRegister(pVM,
519 PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
520 GCPhysRegion, GCPhysRegion + (pRegion->cbRegion - 1),
521 gimR3Mmio2WriteHandler, NULL /* pvUserR3 */,
522 NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NIL_RTR0PTR /* pvUserR0 */,
523 NULL /* pszModRC */, NULL /* pszHandlerRC */, NIL_RTRCPTR /* pvUserRC */,
524 pRegion->szDescription);
525 if (RT_SUCCESS(rc))
526 {
527 pRegion->fMapped = true;
528 pRegion->GCPhysPage = GCPhysRegion;
529 return rc;
530 }
531
532 PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, GCPhysRegion);
533 }
534
535 return rc;
536}
537
538#if 0
539/**
540 * Registers the physical handler for the registered and mapped MMIO2 region.
541 *
542 * @returns VBox status code.
543 * @param pVM Pointer to the VM.
544 * @param pRegion Pointer to the GIM MMIO2 region.
545 */
546VMMR3_INT_DECL(int) GIMR3Mmio2HandlerPhysicalRegister(PVM pVM, PGIMMMIO2REGION pRegion)
547{
548 AssertPtr(pRegion);
549 AssertReturn(pRegion->fRegistered, VERR_GIM_IPE_2);
550 AssertReturn(pRegion->fMapped, VERR_GIM_IPE_3);
551
552 return PGMR3HandlerPhysicalRegister(pVM,
553 PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
554 pRegion->GCPhysPage, pRegion->GCPhysPage + (pRegion->cbRegion - 1),
555 gimR3Mmio2WriteHandler, NULL /* pvUserR3 */,
556 NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NIL_RTR0PTR /* pvUserR0 */,
557 NULL /* pszModRC */, NULL /* pszHandlerRC */, NIL_RTRCPTR /* pvUserRC */,
558 pRegion->szDescription);
559}
560
561
562/**
563 * Deregisters the physical handler for the MMIO2 region.
564 *
565 * @returns VBox status code.
566 * @param pVM Pointer to the VM.
567 * @param pRegion Pointer to the GIM MMIO2 region.
568 */
569VMMR3_INT_DECL(int) GIMR3Mmio2HandlerPhysicalDeregister(PVM pVM, PGIMMMIO2REGION pRegion)
570{
571 return PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
572}
573#endif
574
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