VirtualBox

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

最後變更 在這個檔案從52764是 52764,由 vboxsync 提交於 11 年 前

@cfgm adjustments.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette