VirtualBox

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

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

VMM/GIM: Implemented KVM paravirt. provider.

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