VirtualBox

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

Last change on this file since 55493 was 55493, checked in by vboxsync, 10 years ago

PGM,++: Separated physical access handler callback function pointers from the access handler registrations to reduce footprint and simplify adding a couple of more callbacks.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.9 KB
Line 
1/* $Id: GIM.cpp 55493 2015-04-28 16:51:35Z vboxsync $ */
2/** @file
3 * GIM - Guest Interface Manager.
4 */
5
6/*
7 * Copyright (C) 2014-2015 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 * The GIM provider configured for the VM needs to be recognized by the guest OS
33 * in order to make use of features supported by the interface. Since it
34 * requires co-operation from the guest OS, a GIM provider may also referred to
35 * as a paravirtualization interface.
36 *
37 * One of the goals of having a paravirtualized interface is for enabling guests
38 * to be more accurate and efficient when operating in a virtualized
39 * environment. For instance, a guest OS which interfaces to VirtualBox through
40 * a GIM provider may rely on the provider for supplying the correct TSC
41 * frequency of the host processor. The guest can then avoid caliberating the
42 * TSC itself, resulting in higher accuracy and better performance.
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#include "GIMKvmInternal.h"
66
67/*******************************************************************************
68* Internal Functions *
69*******************************************************************************/
70static DECLCALLBACK(int) gimR3Save(PVM pVM, PSSMHANDLE pSSM);
71static DECLCALLBACK(int) gimR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uSSMVersion, uint32_t uPass);
72
73
74/**
75 * Initializes the GIM.
76 *
77 * @returns VBox status code.
78 * @param pVM Pointer to the VM.
79 */
80VMMR3_INT_DECL(int) GIMR3Init(PVM pVM)
81{
82 LogFlow(("GIMR3Init\n"));
83
84 /*
85 * Assert alignment and sizes.
86 */
87 AssertCompile(sizeof(pVM->gim.s) <= sizeof(pVM->gim.padding));
88
89 /*
90 * Initialize members.
91 */
92 pVM->gim.s.hSemiReadOnlyMmio2Handler = NIL_PGMPHYSHANDLERTYPE;
93
94 /*
95 * Register the saved state data unit.
96 */
97 int rc = SSMR3RegisterInternal(pVM, "GIM", 0 /* uInstance */, GIM_SAVED_STATE_VERSION, sizeof(GIM),
98 NULL /* pfnLivePrep */, NULL /* pfnLiveExec */, NULL /* pfnLiveVote*/,
99 NULL /* pfnSavePrep */, gimR3Save, NULL /* pfnSaveDone */,
100 NULL /* pfnLoadPrep */, gimR3Load, NULL /* pfnLoadDone */);
101 if (RT_FAILURE(rc))
102 return rc;
103
104 /*
105 * Read configuration.
106 */
107 PCFGMNODE pCfgNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "GIM/");
108
109 /** @cfgm{/GIM/Provider, string}
110 * The name of the GIM provider. The default is "none". */
111 char szProvider[64];
112 rc = CFGMR3QueryStringDef(pCfgNode, "Provider", szProvider, sizeof(szProvider), "None");
113 AssertLogRelRCReturn(rc, rc);
114
115 /** @cfgm{/GIM/Version, uint32_t}
116 * The interface version. The default is 0, which means "provide the most
117 * up-to-date implementation". */
118 uint32_t uVersion;
119 rc = CFGMR3QueryU32Def(pCfgNode, "Version", &uVersion, 0 /* default */);
120 AssertLogRelRCReturn(rc, rc);
121
122 /*
123 * Setup the GIM provider for this VM.
124 */
125 LogRel(("GIM: Using provider '%s' (Implementation version: %u)\n", szProvider, uVersion));
126 if (!RTStrCmp(szProvider, "None"))
127 pVM->gim.s.enmProviderId = GIMPROVIDERID_NONE;
128 else
129 {
130 pVM->gim.s.u32Version = uVersion;
131 /** @todo r=bird: Because u32Version is saved, it should be translated to the
132 * 'most up-to-date implementation' version number when 0. Otherwise,
133 * we'll have abiguities when loading the state of older VMs. */
134 if (!RTStrCmp(szProvider, "Minimal"))
135 {
136 pVM->gim.s.enmProviderId = GIMPROVIDERID_MINIMAL;
137 rc = gimR3MinimalInit(pVM);
138 }
139 else if (!RTStrCmp(szProvider, "HyperV"))
140 {
141 pVM->gim.s.enmProviderId = GIMPROVIDERID_HYPERV;
142 rc = gimR3HvInit(pVM);
143 }
144 else if (!RTStrCmp(szProvider, "KVM"))
145 {
146 pVM->gim.s.enmProviderId = GIMPROVIDERID_KVM;
147 rc = gimR3KvmInit(pVM);
148 }
149 else
150 rc = VMR3SetError(pVM->pUVM, VERR_GIM_INVALID_PROVIDER, RT_SRC_POS, "Provider '%s' unknown.", szProvider);
151 }
152 return rc;
153}
154
155
156/**
157 * Initializes the remaining bits of the GIM provider.
158 *
159 * This is called after initializing HM and most other VMM components.
160 *
161 * @returns VBox status code.
162 * @param pVM Pointer to the VM.
163 * @param enmWhat What has been completed.
164 * @thread EMT(0)
165 */
166VMMR3_INT_DECL(int) GIMR3InitCompleted(PVM pVM)
167{
168 switch (pVM->gim.s.enmProviderId)
169 {
170 case GIMPROVIDERID_MINIMAL:
171 return gimR3MinimalInitCompleted(pVM);
172
173 case GIMPROVIDERID_HYPERV:
174 return gimR3HvInitCompleted(pVM);
175
176 case GIMPROVIDERID_KVM:
177 return gimR3KvmInitCompleted(pVM);
178
179 default:
180 break;
181 }
182
183 if (!TMR3CpuTickIsFixedRateMonotonic(pVM, true /* fWithParavirtEnabled */))
184 LogRel(("GIM: Warning!!! Host TSC is unstable. The guest may behave unpredictably with a paravirtualized clock.\n"));
185
186 return VINF_SUCCESS;
187}
188
189
190/**
191 * Applies relocations to data and code managed by this component.
192 *
193 * This function will be called at init and whenever the VMM need to relocate
194 * itself inside the GC.
195 *
196 * @param pVM Pointer to the VM.
197 * @param offDelta Relocation delta relative to old location.
198 */
199VMM_INT_DECL(void) GIMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
200{
201 LogFlow(("GIMR3Relocate\n"));
202
203 if ( pVM->gim.s.enmProviderId == GIMPROVIDERID_NONE
204 || HMIsEnabled(pVM))
205 return;
206
207 switch (pVM->gim.s.enmProviderId)
208 {
209 case GIMPROVIDERID_MINIMAL:
210 {
211 gimR3MinimalRelocate(pVM, offDelta);
212 break;
213 }
214
215 case GIMPROVIDERID_HYPERV:
216 {
217 gimR3HvRelocate(pVM, offDelta);
218 break;
219 }
220
221 case GIMPROVIDERID_KVM:
222 {
223 gimR3KvmRelocate(pVM, offDelta);
224 break;
225 }
226
227 default:
228 {
229 AssertMsgFailed(("Invalid provider Id %#x\n", pVM->gim.s.enmProviderId));
230 break;
231 }
232 }
233}
234
235
236/**
237 * Executes state-save operation.
238 *
239 * @returns VBox status code.
240 * @param pVM Pointer to the VM.
241 * @param pSSM SSM operation handle.
242 */
243DECLCALLBACK(int) gimR3Save(PVM pVM, PSSMHANDLE pSSM)
244{
245 AssertReturn(pVM, VERR_INVALID_PARAMETER);
246 AssertReturn(pSSM, VERR_SSM_INVALID_STATE);
247
248 /** @todo Save per-CPU data. */
249 int rc = VINF_SUCCESS;
250#if 0
251 SSMR3PutU32(pSSM, pVM->cCpus);
252 for (VMCPUID i = 0; i < pVM->cCpus; i++)
253 {
254 rc = SSMR3PutXYZ(pSSM, pVM->aCpus[i].gim.s.XYZ);
255 }
256#endif
257
258 /*
259 * Save per-VM data.
260 */
261 SSMR3PutU32(pSSM, pVM->gim.s.enmProviderId);
262 SSMR3PutU32(pSSM, pVM->gim.s.u32Version);
263
264 /*
265 * Save provider-specific data.
266 */
267 switch (pVM->gim.s.enmProviderId)
268 {
269 case GIMPROVIDERID_HYPERV:
270 rc = gimR3HvSave(pVM, pSSM);
271 AssertRCReturn(rc, rc);
272 break;
273
274 case GIMPROVIDERID_KVM:
275 rc = gimR3KvmSave(pVM, pSSM);
276 AssertRCReturn(rc, rc);
277 break;
278
279 default:
280 break;
281 }
282
283 return rc;
284}
285
286
287/**
288 * Execute state load operation.
289 *
290 * @returns VBox status code.
291 * @param pVM Pointer to the VM.
292 * @param pSSM SSM operation handle.
293 * @param uVersion Data layout version.
294 * @param uPass The data pass.
295 */
296DECLCALLBACK(int) gimR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uSSMVersion, uint32_t uPass)
297{
298 if (uPass != SSM_PASS_FINAL)
299 return VINF_SUCCESS;
300 if (uSSMVersion != GIM_SAVED_STATE_VERSION)
301 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
302
303 /** @todo Load per-CPU data. */
304 int rc;
305#if 0
306 for (VMCPUID i = 0; i < pVM->cCpus; i++)
307 {
308 rc = SSMR3PutXYZ(pSSM, pVM->aCpus[i].gim.s.XYZ);
309 }
310#endif
311
312 /*
313 * Load per-VM data.
314 */
315 uint32_t uProviderId;
316 uint32_t uProviderVersion;
317
318 rc = SSMR3GetU32(pSSM, &uProviderId); AssertRCReturn(rc, rc);
319 rc = SSMR3GetU32(pSSM, &uProviderVersion); AssertRCReturn(rc, rc);
320
321 if ((GIMPROVIDERID)uProviderId != pVM->gim.s.enmProviderId)
322 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Saved GIM provider %u differs from the configured one (%u)."),
323 uProviderId, pVM->gim.s.enmProviderId);
324#if 0 /** @todo r=bird: Figure out what you mean to do here with the version. */
325 if (uProviderVersion != pVM->gim.s.u32Version)
326 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Saved GIM provider version %u differs from the configured one (%u)."),
327 uProviderVersion, pVM->gim.s.u32Version);
328#else
329 pVM->gim.s.u32Version = uProviderVersion;
330#endif
331
332 /*
333 * Load provider-specific data.
334 */
335 switch (pVM->gim.s.enmProviderId)
336 {
337 case GIMPROVIDERID_HYPERV:
338 rc = gimR3HvLoad(pVM, pSSM, uSSMVersion);
339 AssertRCReturn(rc, rc);
340 break;
341
342 case GIMPROVIDERID_KVM:
343 rc = gimR3KvmLoad(pVM, pSSM, uSSMVersion);
344 AssertRCReturn(rc, rc);
345 break;
346
347 default:
348 break;
349 }
350
351 return VINF_SUCCESS;
352}
353
354
355/**
356 * Terminates the GIM.
357 *
358 * Termination means cleaning up and freeing all resources,
359 * the VM itself is, at this point, powered off or suspended.
360 *
361 * @returns VBox status code.
362 * @param pVM Pointer to the VM.
363 */
364VMMR3_INT_DECL(int) GIMR3Term(PVM pVM)
365{
366 switch (pVM->gim.s.enmProviderId)
367 {
368 case GIMPROVIDERID_HYPERV:
369 return gimR3HvTerm(pVM);
370
371 case GIMPROVIDERID_KVM:
372 return gimR3KvmTerm(pVM);
373
374 default:
375 break;
376 }
377 return VINF_SUCCESS;
378}
379
380
381/**
382 * The VM is being reset.
383 *
384 * For the GIM component this means unmapping and unregistering MMIO2 regions
385 * and other provider-specific resets.
386 *
387 * @returns VBox status code.
388 * @param pVM Pointer to the VM.
389 */
390VMMR3_INT_DECL(void) GIMR3Reset(PVM pVM)
391{
392 switch (pVM->gim.s.enmProviderId)
393 {
394 case GIMPROVIDERID_HYPERV:
395 return gimR3HvReset(pVM);
396
397 case GIMPROVIDERID_KVM:
398 return gimR3KvmReset(pVM);
399
400 default:
401 break;
402 }
403}
404
405
406/**
407 * Registers the GIM device with VMM.
408 *
409 * @param pVM Pointer to the VM.
410 * @param pDevIns Pointer to the GIM device instance.
411 */
412VMMR3DECL(void) GIMR3GimDeviceRegister(PVM pVM, PPDMDEVINS pDevIns)
413{
414 pVM->gim.s.pDevInsR3 = pDevIns;
415}
416
417
418/**
419 * Returns the array of MMIO2 regions that are expected to be registered and
420 * later mapped into the guest-physical address space for the GIM provider
421 * configured for the VM.
422 *
423 * @returns Pointer to an array of GIM MMIO2 regions, may return NULL.
424 * @param pVM Pointer to the VM.
425 * @param pcRegions Where to store the number of items in the array.
426 *
427 * @remarks The caller does not own and therefore must -NOT- try to free the
428 * returned pointer.
429 */
430VMMR3DECL(PGIMMMIO2REGION) GIMR3GetMmio2Regions(PVM pVM, uint32_t *pcRegions)
431{
432 Assert(pVM);
433 Assert(pcRegions);
434
435 *pcRegions = 0;
436 switch (pVM->gim.s.enmProviderId)
437 {
438 case GIMPROVIDERID_HYPERV:
439 return gimR3HvGetMmio2Regions(pVM, pcRegions);
440
441 default:
442 break;
443 }
444
445 return NULL;
446}
447
448
449/**
450 * Unmaps a registered MMIO2 region in the guest address space and removes any
451 * access handlers for it.
452 *
453 * @returns VBox status code.
454 * @param pVM Pointer to the VM.
455 * @param pRegion Pointer to the GIM MMIO2 region.
456 */
457VMMR3_INT_DECL(int) GIMR3Mmio2Unmap(PVM pVM, PGIMMMIO2REGION pRegion)
458{
459 AssertPtr(pVM);
460 AssertPtr(pRegion);
461
462 PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
463 AssertPtr(pDevIns);
464 if (pRegion->fMapped)
465 {
466 int rc = PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
467 AssertRC(rc);
468
469 rc = PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, pRegion->GCPhysPage);
470 if (RT_SUCCESS(rc))
471 {
472 pRegion->fMapped = false;
473 pRegion->GCPhysPage = NIL_RTGCPHYS;
474 }
475 }
476 return VINF_SUCCESS;
477}
478
479
480/**
481 * Write access handler for a mapped MMIO2 region. At present, this handler
482 * simply ignores writes.
483 *
484 * In the future we might want to let the GIM provider decide what the handler
485 * should do (like throwing #GP faults).
486 *
487 * @returns VBox status code.
488 * @param pVM Pointer to the VM.
489 * @param GCPhys The guest-physical address of the region.
490 * @param pvPhys Pointer to the region in the guest address space.
491 * @param pvBuf Pointer to the data being read/written.
492 * @param cbBuf The size of the buffer in @a pvBuf.
493 * @param enmAccessType The type of access.
494 * @param pvUser User argument (NULL, not used).
495 */
496static DECLCALLBACK(int) gimR3Mmio2WriteHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf,
497 PGMACCESSTYPE enmAccessType, void *pvUser)
498{
499 /*
500 * Ignore writes to the mapped MMIO2 page.
501 */
502 Assert(enmAccessType == PGMACCESSTYPE_WRITE);
503 return VINF_SUCCESS; /** @todo Hyper-V says we should #GP(0) fault for writes to the Hypercall and TSC page. */
504}
505
506
507/**
508 * Maps a registered MMIO2 region in the guest address space. The region will be
509 * made read-only and writes from the guest will be ignored.
510 *
511 * @returns VBox status code.
512 * @param pVM Pointer to the VM.
513 * @param pRegion Pointer to the GIM MMIO2 region.
514 * @param GCPhysRegion Where in the guest address space to map the region.
515 */
516VMMR3_INT_DECL(int) GIMR3Mmio2Map(PVM pVM, PGIMMMIO2REGION pRegion, RTGCPHYS GCPhysRegion)
517{
518 PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
519 AssertPtr(pDevIns);
520
521 /* The guest-physical address must be page-aligned. */
522 if (GCPhysRegion & PAGE_OFFSET_MASK)
523 {
524 LogFunc(("%s: %#RGp not paging aligned\n", pRegion->szDescription, GCPhysRegion));
525 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
526 }
527
528 /* Allow only normal pages to be overlaid using our MMIO2 pages (disallow MMIO, ROM, reserved pages). */
529 /** @todo Hyper-V doesn't seem to be very strict about this, may be relax
530 * later if some guest really requires it. */
531 if (!PGMPhysIsGCPhysNormal(pVM, GCPhysRegion))
532 {
533 LogFunc(("%s: %#RGp is not normal memory\n", pRegion->szDescription, GCPhysRegion));
534 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
535 }
536
537 if (!pRegion->fRegistered)
538 {
539 LogFunc(("%s: Region has not been registered.\n"));
540 return VERR_GIM_IPE_1;
541 }
542
543 /*
544 * Map the MMIO2 region over the specified guest-physical address.
545 */
546 int rc = PDMDevHlpMMIO2Map(pDevIns, pRegion->iRegion, GCPhysRegion);
547 if (RT_SUCCESS(rc))
548 {
549 /*
550 * Install access-handlers for the mapped page to prevent (ignore) writes to it
551 * from the guest.
552 */
553 if (pVM->gim.s.hSemiReadOnlyMmio2Handler == NIL_PGMPHYSHANDLERTYPE)
554 rc = PGMR3HandlerPhysicalTypeRegister(pVM, PGMPHYSHANDLERKIND_WRITE,
555 gimR3Mmio2WriteHandler,
556 NULL /* pszModR0 */, NULL /* pszHandlerR0 */,
557 NULL /* pszModRC */, NULL /* pszHandlerRC */,
558 "GIM read-only MMIO2 handler",
559 &pVM->gim.s.hSemiReadOnlyMmio2Handler);
560 if (RT_SUCCESS(rc))
561 {
562 rc = PGMHandlerPhysicalRegister(pVM, GCPhysRegion, GCPhysRegion + (pRegion->cbRegion - 1),
563 pVM->gim.s.hSemiReadOnlyMmio2Handler,
564 NULL /* pvUserR3 */, NIL_RTR0PTR /* pvUserR0 */, NIL_RTRCPTR /* pvUserRC */,
565 pRegion->szDescription);
566 if (RT_SUCCESS(rc))
567 {
568 pRegion->fMapped = true;
569 pRegion->GCPhysPage = GCPhysRegion;
570 return rc;
571 }
572 }
573
574 PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, GCPhysRegion);
575 }
576
577 return rc;
578}
579
580#if 0
581/**
582 * Registers the physical handler for the registered and mapped MMIO2 region.
583 *
584 * @returns VBox status code.
585 * @param pVM Pointer to the VM.
586 * @param pRegion Pointer to the GIM MMIO2 region.
587 */
588VMMR3_INT_DECL(int) GIMR3Mmio2HandlerPhysicalRegister(PVM pVM, PGIMMMIO2REGION pRegion)
589{
590 AssertPtr(pRegion);
591 AssertReturn(pRegion->fRegistered, VERR_GIM_IPE_2);
592 AssertReturn(pRegion->fMapped, VERR_GIM_IPE_3);
593
594 return PGMR3HandlerPhysicalRegister(pVM,
595 PGMPHYSHANDLERKIND_WRITE,
596 pRegion->GCPhysPage, pRegion->GCPhysPage + (pRegion->cbRegion - 1),
597 gimR3Mmio2WriteHandler, NULL /* pvUserR3 */,
598 NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NIL_RTR0PTR /* pvUserR0 */,
599 NULL /* pszModRC */, NULL /* pszHandlerRC */, NIL_RTRCPTR /* pvUserRC */,
600 pRegion->szDescription);
601}
602
603
604/**
605 * Deregisters the physical handler for the MMIO2 region.
606 *
607 * @returns VBox status code.
608 * @param pVM Pointer to the VM.
609 * @param pRegion Pointer to the GIM MMIO2 region.
610 */
611VMMR3_INT_DECL(int) GIMR3Mmio2HandlerPhysicalDeregister(PVM pVM, PGIMMMIO2REGION pRegion)
612{
613 return PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
614}
615#endif
616
Note: See TracBrowser for help on using the repository browser.

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