VirtualBox

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

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

VMM/GIM/Minimal: OS X bits.

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

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