VirtualBox

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

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

VMM/GIM: obsolete comment.

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