VirtualBox

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

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

VMM/GIM: More bits for Hyper-V implementation.

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