VirtualBox

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

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

VMM/GIM: Fix circular dependency between PDM and GIM init. routines.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.9 KB
Line 
1/* $Id: GIM.cpp 52675 2014-09-10 13:24:03Z 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 rc = SSMR3RegisterInternal(pVM, "GIM", 0 /* uInstance */, GIM_SSM_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 {
123 Assert(!pVM->gim.s.fEnabled);
124 pVM->gim.s.enmProviderId = GIMPROVIDERID_NONE;
125 }
126 else
127 {
128 pVM->gim.s.fEnabled = true;
129 pVM->gim.s.u32Version = uVersion;
130 if (!RTStrCmp(szProvider, "Minimal"))
131 {
132 pVM->gim.s.enmProviderId = GIMPROVIDERID_MINIMAL;
133 rc = GIMR3MinimalInit(pVM);
134 }
135 else if (!RTStrCmp(szProvider, "HyperV"))
136 {
137 pVM->gim.s.enmProviderId = GIMPROVIDERID_HYPERV;
138 rc = GIMR3HvInit(pVM);
139 }
140 /** @todo KVM and others. */
141 else
142 {
143 LogRel(("GIM: Provider \"%s\" unknown.\n", szProvider));
144 rc = VERR_GIM_INVALID_PROVIDER;
145 }
146 }
147 return rc;
148}
149
150
151VMMR3_INT_DECL(int) GIMR3InitFinalize(PVM pVM)
152{
153 LogFlow(("GIMR3InitFinalize\n"));
154
155 if (!pVM->gim.s.fEnabled)
156 return VINF_SUCCESS;
157
158 int rc = VINF_SUCCESS;
159 switch (pVM->gim.s.enmProviderId)
160 {
161 case GIMPROVIDERID_MINIMAL:
162 {
163 rc = GIMR3MinimalInitFinalize(pVM);
164 break;
165 }
166
167 default:
168 break;
169 }
170 return rc;
171}
172
173
174/**
175 * Applies relocations to data and code managed by this component. This function
176 * will be called at init and whenever the VMM need to relocate itself inside
177 * 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.fEnabled
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;
230#if 0
231 for (VMCPUID i = 0; i < pVM->cCpus; i++)
232 {
233 rc = SSMR3PutXYZ(pSSM, pVM->aCpus[i].gim.s.XYZ);
234 }
235#endif
236
237 /*
238 * Save per-VM data.
239 */
240 rc = SSMR3PutBool(pSSM, pVM->gim.s.fEnabled);
241 AssertRCReturn(rc, rc);
242 rc = SSMR3PutU32(pSSM, pVM->gim.s.enmProviderId);
243 AssertRCReturn(rc, rc);
244 rc = SSMR3PutU32(pSSM, pVM->gim.s.u32Version);
245 AssertRCReturn(rc, rc);
246
247 /*
248 * Save provider-specific data.
249 */
250 if (pVM->gim.s.fEnabled)
251 {
252 switch (pVM->gim.s.enmProviderId)
253 {
254 case GIMPROVIDERID_HYPERV:
255 rc = GIMR3HvSave(pVM, pSSM);
256 AssertRCReturn(rc, rc);
257 break;
258
259 default:
260 break;
261 }
262 }
263
264 return rc;
265}
266
267
268/**
269 * Execute state load operation.
270 *
271 * @returns VBox status code.
272 * @param pVM Pointer to the VM.
273 * @param pSSM SSM operation handle.
274 * @param uVersion Data layout version.
275 * @param uPass The data pass.
276 */
277DECLCALLBACK(int) gimR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
278{
279 if (uPass != SSM_PASS_FINAL)
280 return VINF_SUCCESS;
281
282 /** @todo Load per-CPU data. */
283 int rc;
284#if 0
285 for (VMCPUID i = 0; i < pVM->cCpus; i++)
286 {
287 rc = SSMR3PutXYZ(pSSM, pVM->aCpus[i].gim.s.XYZ);
288 }
289#endif
290
291 /*
292 * Load per-VM data.
293 */
294 rc = SSMR3GetBool(pSSM, &pVM->gim.s.fEnabled);
295 AssertRCReturn(rc, rc);
296 rc = SSMR3GetU32(pSSM, (uint32_t *)&pVM->gim.s.enmProviderId);
297 AssertRCReturn(rc, rc);
298 rc = SSMR3GetU32(pSSM, &pVM->gim.s.u32Version);
299 AssertRCReturn(rc, rc);
300
301 /*
302 * Load provider-specific data.
303 */
304 if (pVM->gim.s.fEnabled)
305 {
306 switch (pVM->gim.s.enmProviderId)
307 {
308 case GIMPROVIDERID_HYPERV:
309 rc = GIMR3HvLoad(pVM, pSSM, uVersion);
310 AssertRCReturn(rc, rc);
311 break;
312
313 default:
314 break;
315 }
316 }
317
318 return rc;
319}
320
321
322/**
323 * Terminates the GIM.
324 *
325 * Termination means cleaning up and freeing all resources,
326 * the VM itself is, at this point, powered off or suspended.
327 *
328 * @returns VBox status code.
329 * @param pVM Pointer to the VM.
330 */
331VMMR3_INT_DECL(int) GIMR3Term(PVM pVM)
332{
333 if (!pVM->gim.s.fEnabled)
334 return VINF_SUCCESS;
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 if (!pVM->gim.s.fEnabled)
360 return;
361
362 switch (pVM->gim.s.enmProviderId)
363 {
364 case GIMPROVIDERID_HYPERV:
365 return GIMR3HvReset(pVM);
366
367 default:
368 break;
369 }
370}
371
372
373/**
374 * Registers the GIM device with VMM.
375 *
376 * @param pVM Pointer to the VM.
377 * @param pDevIns Pointer to the GIM device instance.
378 */
379VMMR3DECL(void) GIMR3GimDeviceRegister(PVM pVM, PPDMDEVINS pDevIns)
380{
381 pVM->gim.s.pDevInsR3 = pDevIns;
382}
383
384
385/**
386 * Returns the array of MMIO2 regions that are expected to be registered and
387 * later mapped into the guest-physical address space for the GIM provider
388 * configured for the VM.
389 *
390 * @returns Pointer to an array of GIM MMIO2 regions, may return NULL.
391 * @param pVM Pointer to the VM.
392 * @param pcRegions Where to store the number of items in the array.
393 *
394 * @remarks The caller does not own and therefore must -NOT- try to free the
395 * returned pointer.
396 */
397VMMR3DECL(PGIMMMIO2REGION) GIMR3GetMmio2Regions(PVM pVM, uint32_t *pcRegions)
398{
399 Assert(pVM);
400 Assert(pcRegions);
401
402 *pcRegions = 0;
403 if (!pVM->gim.s.fEnabled)
404 return NULL;
405
406 switch (pVM->gim.s.enmProviderId)
407 {
408 case GIMPROVIDERID_HYPERV:
409 return GIMR3HvGetMmio2Regions(pVM, pcRegions);
410
411 case GIMPROVIDERID_KVM: /** @todo KVM. */
412 default:
413 break;
414 }
415
416 return NULL;
417}
418
419
420/**
421 * Unmaps a registered MMIO2 region in the guest address space and removes any
422 * access handlers for it.
423 *
424 * @returns VBox status code.
425 * @param pVM Pointer to the VM.
426 * @param pRegion Pointer to the GIM MMIO2 region.
427 */
428VMMR3_INT_DECL(int) GIMR3Mmio2Unmap(PVM pVM, PGIMMMIO2REGION pRegion)
429{
430 AssertPtr(pVM);
431 AssertPtr(pRegion);
432
433 PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
434 AssertPtr(pDevIns);
435 if (pRegion->fMapped)
436 {
437 int rc = PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
438 AssertRC(rc);
439
440 rc = PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, pRegion->GCPhysPage);
441 if (RT_SUCCESS(rc))
442 {
443 pRegion->fMapped = false;
444 pRegion->GCPhysPage = NIL_RTGCPHYS;
445 }
446 }
447 return VINF_SUCCESS;
448}
449
450
451/**
452 * Write access handler for a mapped MMIO2 region. At present, this handler
453 * simply ignores writes.
454 *
455 * In the future we might want to let the GIM provider decide what the handler
456 * should do (like throwing #GP faults).
457 *
458 * @returns VBox status code.
459 * @param pVM Pointer to the VM.
460 * @param GCPhys The guest-physical address of the region.
461 * @param pvPhys Pointer to the region in the guest address space.
462 * @param pvBuf Pointer to the data being read/written.
463 * @param cbBuf The size of the buffer in @a pvBuf.
464 * @param enmAccessType The type of access.
465 * @param pvUser User argument (NULL, not used).
466 */
467static DECLCALLBACK(int) gimR3Mmio2WriteHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf,
468 PGMACCESSTYPE enmAccessType, void *pvUser)
469{
470 /*
471 * Ignore writes to the mapped MMIO2 page.
472 */
473 Assert(enmAccessType == PGMACCESSTYPE_WRITE);
474 return VINF_SUCCESS; /** @todo Hyper-V says we should #GP(0) fault for writes to the Hypercall and TSC page. */
475}
476
477
478/**
479 * Maps a registered MMIO2 region in the guest address space. The region will be
480 * made read-only and writes from the guest will be ignored.
481 *
482 * @returns VBox status code.
483 * @param pVM Pointer to the VM.
484 * @param pRegion Pointer to the GIM MMIO2 region.
485 * @param GCPhysRegion Where in the guest address space to map the region.
486 */
487VMMR3_INT_DECL(int) GIMR3Mmio2Map(PVM pVM, PGIMMMIO2REGION pRegion, RTGCPHYS GCPhysRegion)
488{
489 PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
490 AssertPtr(pDevIns);
491
492 /* The guest-physical address must be page-aligned. */
493 if (GCPhysRegion & PAGE_OFFSET_MASK)
494 {
495 LogFunc(("%s: %#RGp not paging aligned\n", pRegion->szDescription, GCPhysRegion));
496 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
497 }
498
499 /* Allow only normal pages to be overlaid using our MMIO2 pages (disallow MMIO, ROM, reserved pages). */
500 /** @todo Hyper-V doesn't seem to be very strict about this, may be relax
501 * later if some guest really requires it. */
502 if (!PGMPhysIsGCPhysNormal(pVM, GCPhysRegion))
503 {
504 LogFunc(("%s: %#RGp is not normal memory\n", pRegion->szDescription, GCPhysRegion));
505 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
506 }
507
508 if (!pRegion->fRegistered)
509 {
510 LogFunc(("%s: Region has not been registered.\n"));
511 return VERR_GIM_IPE_1;
512 }
513
514 /*
515 * Map the MMIO2 region over the specified guest-physical address.
516 */
517 int rc = PDMDevHlpMMIO2Map(pDevIns, pRegion->iRegion, GCPhysRegion);
518 if (RT_SUCCESS(rc))
519 {
520 /*
521 * Install access-handlers for the mapped page to prevent (ignore) writes to it from the guest.
522 */
523 rc = PGMR3HandlerPhysicalRegister(pVM,
524 PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
525 GCPhysRegion, GCPhysRegion + (pRegion->cbRegion - 1),
526 gimR3Mmio2WriteHandler, NULL /* pvUserR3 */,
527 NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NIL_RTR0PTR /* pvUserR0 */,
528 NULL /* pszModRC */, NULL /* pszHandlerRC */, NIL_RTRCPTR /* pvUserRC */,
529 pRegion->szDescription);
530 if (RT_SUCCESS(rc))
531 {
532 pRegion->fMapped = true;
533 pRegion->GCPhysPage = GCPhysRegion;
534 return rc;
535 }
536
537 PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, GCPhysRegion);
538 }
539
540 return rc;
541}
542
543#if 0
544/**
545 * Registers the physical handler for the registered and mapped MMIO2 region.
546 *
547 * @returns VBox status code.
548 * @param pVM Pointer to the VM.
549 * @param pRegion Pointer to the GIM MMIO2 region.
550 */
551VMMR3_INT_DECL(int) GIMR3Mmio2HandlerPhysicalRegister(PVM pVM, PGIMMMIO2REGION pRegion)
552{
553 AssertPtr(pRegion);
554 AssertReturn(pRegion->fRegistered, VERR_GIM_IPE_2);
555 AssertReturn(pRegion->fMapped, VERR_GIM_IPE_3);
556
557 return PGMR3HandlerPhysicalRegister(pVM,
558 PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
559 pRegion->GCPhysPage, pRegion->GCPhysPage + (pRegion->cbRegion - 1),
560 gimR3Mmio2WriteHandler, NULL /* pvUserR3 */,
561 NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NIL_RTR0PTR /* pvUserR0 */,
562 NULL /* pszModRC */, NULL /* pszHandlerRC */, NIL_RTRCPTR /* pvUserRC */,
563 pRegion->szDescription);
564}
565
566
567/**
568 * Deregisters the physical handler for the MMIO2 region.
569 *
570 * @returns VBox status code.
571 * @param pVM Pointer to the VM.
572 * @param pRegion Pointer to the GIM MMIO2 region.
573 */
574VMMR3_INT_DECL(int) GIMR3Mmio2HandlerPhysicalDeregister(PVM pVM, PGIMMMIO2REGION pRegion)
575{
576 return PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
577}
578#endif
579
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