VirtualBox

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

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

VMM/GIM: Fix initialization of Hyper-V bits that rely on HM initialization to be completed.

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

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