VirtualBox

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

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

VMM/GIM: cleanup.

  • 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 54654 2015-03-05 15:43:41Z vboxsync $ */
2/** @file
3 * GIM - Guest Interface Manager.
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/** @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 /** @todo Load per-CPU data. */
279 int rc;
280#if 0
281 for (VMCPUID i = 0; i < pVM->cCpus; i++)
282 {
283 rc = SSMR3PutXYZ(pSSM, pVM->aCpus[i].gim.s.XYZ);
284 }
285#endif
286
287 /*
288 * Load per-VM data.
289 */
290 uint32_t uProviderId;
291 uint32_t uProviderVersion;
292
293 rc = SSMR3GetU32(pSSM, &uProviderId); AssertRCReturn(rc, rc);
294 rc = SSMR3GetU32(pSSM, &uProviderVersion); AssertRCReturn(rc, rc);
295
296 if ((GIMPROVIDERID)uProviderId != pVM->gim.s.enmProviderId)
297 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Saved GIM provider %u differs from the configured one (%u)."),
298 uProviderId, pVM->gim.s.enmProviderId);
299#if 0 /** @todo r=bird: Figure out what you mean to do here with the version. */
300 if (uProviderVersion != pVM->gim.s.u32Version)
301 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Saved GIM provider version %u differs from the configured one (%u)."),
302 uProviderVersion, pVM->gim.s.u32Version);
303#else
304 pVM->gim.s.u32Version = uProviderVersion;
305#endif
306
307 /*
308 * Load provider-specific data.
309 */
310 switch (pVM->gim.s.enmProviderId)
311 {
312 case GIMPROVIDERID_HYPERV:
313 rc = gimR3HvLoad(pVM, pSSM, uVersion);
314 AssertRCReturn(rc, rc);
315 break;
316
317 default:
318 break;
319 }
320
321 return VINF_SUCCESS;
322}
323
324
325/**
326 * Terminates the GIM.
327 *
328 * Termination means cleaning up and freeing all resources,
329 * the VM itself is, at this point, powered off or suspended.
330 *
331 * @returns VBox status code.
332 * @param pVM Pointer to the VM.
333 */
334VMMR3_INT_DECL(int) GIMR3Term(PVM pVM)
335{
336 switch (pVM->gim.s.enmProviderId)
337 {
338 case GIMPROVIDERID_HYPERV:
339 return gimR3HvTerm(pVM);
340
341 default:
342 break;
343 }
344 return VINF_SUCCESS;
345}
346
347
348/**
349 * The VM is being reset.
350 *
351 * For the GIM component this means unmapping and unregistering MMIO2 regions
352 * and other provider-specific resets.
353 *
354 * @returns VBox status code.
355 * @param pVM Pointer to the VM.
356 */
357VMMR3_INT_DECL(void) GIMR3Reset(PVM pVM)
358{
359 switch (pVM->gim.s.enmProviderId)
360 {
361 case GIMPROVIDERID_HYPERV:
362 return gimR3HvReset(pVM);
363
364 default:
365 break;
366 }
367}
368
369
370/**
371 * Registers the GIM device with VMM.
372 *
373 * @param pVM Pointer to the VM.
374 * @param pDevIns Pointer to the GIM device instance.
375 */
376VMMR3DECL(void) GIMR3GimDeviceRegister(PVM pVM, PPDMDEVINS pDevIns)
377{
378 pVM->gim.s.pDevInsR3 = pDevIns;
379}
380
381
382/**
383 * Returns the array of MMIO2 regions that are expected to be registered and
384 * later mapped into the guest-physical address space for the GIM provider
385 * configured for the VM.
386 *
387 * @returns Pointer to an array of GIM MMIO2 regions, may return NULL.
388 * @param pVM Pointer to the VM.
389 * @param pcRegions Where to store the number of items in the array.
390 *
391 * @remarks The caller does not own and therefore must -NOT- try to free the
392 * returned pointer.
393 */
394VMMR3DECL(PGIMMMIO2REGION) GIMR3GetMmio2Regions(PVM pVM, uint32_t *pcRegions)
395{
396 Assert(pVM);
397 Assert(pcRegions);
398
399 *pcRegions = 0;
400 switch (pVM->gim.s.enmProviderId)
401 {
402 case GIMPROVIDERID_HYPERV:
403 return gimR3HvGetMmio2Regions(pVM, pcRegions);
404
405 case GIMPROVIDERID_KVM: /** @todo KVM. */
406 default:
407 break;
408 }
409
410 return NULL;
411}
412
413
414/**
415 * Unmaps a registered MMIO2 region in the guest address space and removes any
416 * access handlers for it.
417 *
418 * @returns VBox status code.
419 * @param pVM Pointer to the VM.
420 * @param pRegion Pointer to the GIM MMIO2 region.
421 */
422VMMR3_INT_DECL(int) GIMR3Mmio2Unmap(PVM pVM, PGIMMMIO2REGION pRegion)
423{
424 AssertPtr(pVM);
425 AssertPtr(pRegion);
426
427 PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
428 AssertPtr(pDevIns);
429 if (pRegion->fMapped)
430 {
431 int rc = PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
432 AssertRC(rc);
433
434 rc = PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, pRegion->GCPhysPage);
435 if (RT_SUCCESS(rc))
436 {
437 pRegion->fMapped = false;
438 pRegion->GCPhysPage = NIL_RTGCPHYS;
439 }
440 }
441 return VINF_SUCCESS;
442}
443
444
445/**
446 * Write access handler for a mapped MMIO2 region. At present, this handler
447 * simply ignores writes.
448 *
449 * In the future we might want to let the GIM provider decide what the handler
450 * should do (like throwing #GP faults).
451 *
452 * @returns VBox status code.
453 * @param pVM Pointer to the VM.
454 * @param GCPhys The guest-physical address of the region.
455 * @param pvPhys Pointer to the region in the guest address space.
456 * @param pvBuf Pointer to the data being read/written.
457 * @param cbBuf The size of the buffer in @a pvBuf.
458 * @param enmAccessType The type of access.
459 * @param pvUser User argument (NULL, not used).
460 */
461static DECLCALLBACK(int) gimR3Mmio2WriteHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf,
462 PGMACCESSTYPE enmAccessType, void *pvUser)
463{
464 /*
465 * Ignore writes to the mapped MMIO2 page.
466 */
467 Assert(enmAccessType == PGMACCESSTYPE_WRITE);
468 return VINF_SUCCESS; /** @todo Hyper-V says we should #GP(0) fault for writes to the Hypercall and TSC page. */
469}
470
471
472/**
473 * Maps a registered MMIO2 region in the guest address space. The region will be
474 * made read-only and writes from the guest will be ignored.
475 *
476 * @returns VBox status code.
477 * @param pVM Pointer to the VM.
478 * @param pRegion Pointer to the GIM MMIO2 region.
479 * @param GCPhysRegion Where in the guest address space to map the region.
480 */
481VMMR3_INT_DECL(int) GIMR3Mmio2Map(PVM pVM, PGIMMMIO2REGION pRegion, RTGCPHYS GCPhysRegion)
482{
483 PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
484 AssertPtr(pDevIns);
485
486 /* The guest-physical address must be page-aligned. */
487 if (GCPhysRegion & PAGE_OFFSET_MASK)
488 {
489 LogFunc(("%s: %#RGp not paging aligned\n", pRegion->szDescription, GCPhysRegion));
490 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
491 }
492
493 /* Allow only normal pages to be overlaid using our MMIO2 pages (disallow MMIO, ROM, reserved pages). */
494 /** @todo Hyper-V doesn't seem to be very strict about this, may be relax
495 * later if some guest really requires it. */
496 if (!PGMPhysIsGCPhysNormal(pVM, GCPhysRegion))
497 {
498 LogFunc(("%s: %#RGp is not normal memory\n", pRegion->szDescription, GCPhysRegion));
499 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
500 }
501
502 if (!pRegion->fRegistered)
503 {
504 LogFunc(("%s: Region has not been registered.\n"));
505 return VERR_GIM_IPE_1;
506 }
507
508 /*
509 * Map the MMIO2 region over the specified guest-physical address.
510 */
511 int rc = PDMDevHlpMMIO2Map(pDevIns, pRegion->iRegion, GCPhysRegion);
512 if (RT_SUCCESS(rc))
513 {
514 /*
515 * Install access-handlers for the mapped page to prevent (ignore) writes to it from the guest.
516 */
517 rc = PGMR3HandlerPhysicalRegister(pVM,
518 PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
519 GCPhysRegion, GCPhysRegion + (pRegion->cbRegion - 1),
520 gimR3Mmio2WriteHandler, NULL /* pvUserR3 */,
521 NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NIL_RTR0PTR /* pvUserR0 */,
522 NULL /* pszModRC */, NULL /* pszHandlerRC */, NIL_RTRCPTR /* pvUserRC */,
523 pRegion->szDescription);
524 if (RT_SUCCESS(rc))
525 {
526 pRegion->fMapped = true;
527 pRegion->GCPhysPage = GCPhysRegion;
528 return rc;
529 }
530
531 PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, GCPhysRegion);
532 }
533
534 return rc;
535}
536
537#if 0
538/**
539 * Registers the physical handler for the registered and mapped MMIO2 region.
540 *
541 * @returns VBox status code.
542 * @param pVM Pointer to the VM.
543 * @param pRegion Pointer to the GIM MMIO2 region.
544 */
545VMMR3_INT_DECL(int) GIMR3Mmio2HandlerPhysicalRegister(PVM pVM, PGIMMMIO2REGION pRegion)
546{
547 AssertPtr(pRegion);
548 AssertReturn(pRegion->fRegistered, VERR_GIM_IPE_2);
549 AssertReturn(pRegion->fMapped, VERR_GIM_IPE_3);
550
551 return PGMR3HandlerPhysicalRegister(pVM,
552 PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
553 pRegion->GCPhysPage, pRegion->GCPhysPage + (pRegion->cbRegion - 1),
554 gimR3Mmio2WriteHandler, NULL /* pvUserR3 */,
555 NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NIL_RTR0PTR /* pvUserR0 */,
556 NULL /* pszModRC */, NULL /* pszHandlerRC */, NIL_RTRCPTR /* pvUserRC */,
557 pRegion->szDescription);
558}
559
560
561/**
562 * Deregisters the physical handler for the MMIO2 region.
563 *
564 * @returns VBox status code.
565 * @param pVM Pointer to the VM.
566 * @param pRegion Pointer to the GIM MMIO2 region.
567 */
568VMMR3_INT_DECL(int) GIMR3Mmio2HandlerPhysicalDeregister(PVM pVM, PGIMMMIO2REGION pRegion)
569{
570 return PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
571}
572#endif
573
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