VirtualBox

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

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

VMM/GIM: Make guest-OS Id as part of the saved states as it could be useful in the future to have it in there.

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