VirtualBox

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

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

VMM: Added VMMGC_MAIN_MODULE_NAME -> VMMRC_MAIN_MODULE_NAME and extended physical handler type registration with non-#PF ring-0 and raw-mode handlers (not specified or called yet).

  • 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 56051 2015-05-24 14:11:46Z 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);
72static FNPGMPHYSHANDLER gimR3Mmio2WriteHandler;
73
74
75/**
76 * Initializes the GIM.
77 *
78 * @returns VBox status code.
79 * @param pVM Pointer to the VM.
80 */
81VMMR3_INT_DECL(int) GIMR3Init(PVM pVM)
82{
83 LogFlow(("GIMR3Init\n"));
84
85 /*
86 * Assert alignment and sizes.
87 */
88 AssertCompile(sizeof(pVM->gim.s) <= sizeof(pVM->gim.padding));
89
90 /*
91 * Initialize members.
92 */
93 pVM->gim.s.hSemiReadOnlyMmio2Handler = NIL_PGMPHYSHANDLERTYPE;
94
95 /*
96 * Register the saved state data unit.
97 */
98 int rc = SSMR3RegisterInternal(pVM, "GIM", 0 /* uInstance */, GIM_SAVED_STATE_VERSION, sizeof(GIM),
99 NULL /* pfnLivePrep */, NULL /* pfnLiveExec */, NULL /* pfnLiveVote*/,
100 NULL /* pfnSavePrep */, gimR3Save, NULL /* pfnSaveDone */,
101 NULL /* pfnLoadPrep */, gimR3Load, NULL /* pfnLoadDone */);
102 if (RT_FAILURE(rc))
103 return rc;
104
105 /*
106 * Read configuration.
107 */
108 PCFGMNODE pCfgNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "GIM/");
109
110 /** @cfgm{/GIM/Provider, string}
111 * The name of the GIM provider. The default is "none". */
112 char szProvider[64];
113 rc = CFGMR3QueryStringDef(pCfgNode, "Provider", szProvider, sizeof(szProvider), "None");
114 AssertLogRelRCReturn(rc, rc);
115
116 /** @cfgm{/GIM/Version, uint32_t}
117 * The interface version. The default is 0, which means "provide the most
118 * up-to-date implementation". */
119 uint32_t uVersion;
120 rc = CFGMR3QueryU32Def(pCfgNode, "Version", &uVersion, 0 /* default */);
121 AssertLogRelRCReturn(rc, rc);
122
123 /*
124 * Setup the GIM provider for this VM.
125 */
126 LogRel(("GIM: Using provider '%s' (Implementation version: %u)\n", szProvider, uVersion));
127 if (!RTStrCmp(szProvider, "None"))
128 pVM->gim.s.enmProviderId = GIMPROVIDERID_NONE;
129 else
130 {
131 pVM->gim.s.u32Version = uVersion;
132 /** @todo r=bird: Because u32Version is saved, it should be translated to the
133 * 'most up-to-date implementation' version number when 0. Otherwise,
134 * we'll have abiguities when loading the state of older VMs. */
135 if (!RTStrCmp(szProvider, "Minimal"))
136 {
137 pVM->gim.s.enmProviderId = GIMPROVIDERID_MINIMAL;
138 rc = gimR3MinimalInit(pVM);
139 }
140 else if (!RTStrCmp(szProvider, "HyperV"))
141 {
142 pVM->gim.s.enmProviderId = GIMPROVIDERID_HYPERV;
143 rc = gimR3HvInit(pVM);
144 }
145 else if (!RTStrCmp(szProvider, "KVM"))
146 {
147 pVM->gim.s.enmProviderId = GIMPROVIDERID_KVM;
148 rc = gimR3KvmInit(pVM);
149 }
150 else
151 rc = VMR3SetError(pVM->pUVM, VERR_GIM_INVALID_PROVIDER, RT_SRC_POS, "Provider '%s' unknown.", szProvider);
152 }
153 return rc;
154}
155
156
157/**
158 * Initializes the remaining bits of the GIM provider.
159 *
160 * This is called after initializing HM and most other VMM components.
161 *
162 * @returns VBox status code.
163 * @param pVM Pointer to the VM.
164 * @param enmWhat What has been completed.
165 * @thread EMT(0)
166 */
167VMMR3_INT_DECL(int) GIMR3InitCompleted(PVM pVM)
168{
169 switch (pVM->gim.s.enmProviderId)
170 {
171 case GIMPROVIDERID_MINIMAL:
172 return gimR3MinimalInitCompleted(pVM);
173
174 case GIMPROVIDERID_HYPERV:
175 return gimR3HvInitCompleted(pVM);
176
177 case GIMPROVIDERID_KVM:
178 return gimR3KvmInitCompleted(pVM);
179
180 default:
181 break;
182 }
183
184 if (!TMR3CpuTickIsFixedRateMonotonic(pVM, true /* fWithParavirtEnabled */))
185 LogRel(("GIM: Warning!!! Host TSC is unstable. The guest may behave unpredictably with a paravirtualized clock.\n"));
186
187 return VINF_SUCCESS;
188}
189
190
191/**
192 * Applies relocations to data and code managed by this component.
193 *
194 * This function will be called at init and whenever the VMM need to relocate
195 * itself inside the GC.
196 *
197 * @param pVM Pointer to the VM.
198 * @param offDelta Relocation delta relative to old location.
199 */
200VMM_INT_DECL(void) GIMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
201{
202 LogFlow(("GIMR3Relocate\n"));
203
204 if ( pVM->gim.s.enmProviderId == GIMPROVIDERID_NONE
205 || HMIsEnabled(pVM))
206 return;
207
208 switch (pVM->gim.s.enmProviderId)
209 {
210 case GIMPROVIDERID_MINIMAL:
211 {
212 gimR3MinimalRelocate(pVM, offDelta);
213 break;
214 }
215
216 case GIMPROVIDERID_HYPERV:
217 {
218 gimR3HvRelocate(pVM, offDelta);
219 break;
220 }
221
222 case GIMPROVIDERID_KVM:
223 {
224 gimR3KvmRelocate(pVM, offDelta);
225 break;
226 }
227
228 default:
229 {
230 AssertMsgFailed(("Invalid provider Id %#x\n", pVM->gim.s.enmProviderId));
231 break;
232 }
233 }
234}
235
236
237/**
238 * Executes state-save operation.
239 *
240 * @returns VBox status code.
241 * @param pVM Pointer to the VM.
242 * @param pSSM SSM operation handle.
243 */
244DECLCALLBACK(int) gimR3Save(PVM pVM, PSSMHANDLE pSSM)
245{
246 AssertReturn(pVM, VERR_INVALID_PARAMETER);
247 AssertReturn(pSSM, VERR_SSM_INVALID_STATE);
248
249 /** @todo Save per-CPU data. */
250 int rc = VINF_SUCCESS;
251#if 0
252 SSMR3PutU32(pSSM, pVM->cCpus);
253 for (VMCPUID i = 0; i < pVM->cCpus; i++)
254 {
255 rc = SSMR3PutXYZ(pSSM, pVM->aCpus[i].gim.s.XYZ);
256 }
257#endif
258
259 /*
260 * Save per-VM data.
261 */
262 SSMR3PutU32(pSSM, pVM->gim.s.enmProviderId);
263 SSMR3PutU32(pSSM, pVM->gim.s.u32Version);
264
265 /*
266 * Save provider-specific data.
267 */
268 switch (pVM->gim.s.enmProviderId)
269 {
270 case GIMPROVIDERID_HYPERV:
271 rc = gimR3HvSave(pVM, pSSM);
272 AssertRCReturn(rc, rc);
273 break;
274
275 case GIMPROVIDERID_KVM:
276 rc = gimR3KvmSave(pVM, pSSM);
277 AssertRCReturn(rc, rc);
278 break;
279
280 default:
281 break;
282 }
283
284 return rc;
285}
286
287
288/**
289 * Execute state load operation.
290 *
291 * @returns VBox status code.
292 * @param pVM Pointer to the VM.
293 * @param pSSM SSM operation handle.
294 * @param uVersion Data layout version.
295 * @param uPass The data pass.
296 */
297DECLCALLBACK(int) gimR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uSSMVersion, uint32_t uPass)
298{
299 if (uPass != SSM_PASS_FINAL)
300 return VINF_SUCCESS;
301 if (uSSMVersion != GIM_SAVED_STATE_VERSION)
302 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
303
304 /** @todo Load per-CPU data. */
305 int rc;
306#if 0
307 for (VMCPUID i = 0; i < pVM->cCpus; i++)
308 {
309 rc = SSMR3PutXYZ(pSSM, pVM->aCpus[i].gim.s.XYZ);
310 }
311#endif
312
313 /*
314 * Load per-VM data.
315 */
316 uint32_t uProviderId;
317 uint32_t uProviderVersion;
318
319 rc = SSMR3GetU32(pSSM, &uProviderId); AssertRCReturn(rc, rc);
320 rc = SSMR3GetU32(pSSM, &uProviderVersion); AssertRCReturn(rc, rc);
321
322 if ((GIMPROVIDERID)uProviderId != pVM->gim.s.enmProviderId)
323 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Saved GIM provider %u differs from the configured one (%u)."),
324 uProviderId, pVM->gim.s.enmProviderId);
325#if 0 /** @todo r=bird: Figure out what you mean to do here with the version. */
326 if (uProviderVersion != pVM->gim.s.u32Version)
327 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Saved GIM provider version %u differs from the configured one (%u)."),
328 uProviderVersion, pVM->gim.s.u32Version);
329#else
330 pVM->gim.s.u32Version = uProviderVersion;
331#endif
332
333 /*
334 * Load provider-specific data.
335 */
336 switch (pVM->gim.s.enmProviderId)
337 {
338 case GIMPROVIDERID_HYPERV:
339 rc = gimR3HvLoad(pVM, pSSM, uSSMVersion);
340 AssertRCReturn(rc, rc);
341 break;
342
343 case GIMPROVIDERID_KVM:
344 rc = gimR3KvmLoad(pVM, pSSM, uSSMVersion);
345 AssertRCReturn(rc, rc);
346 break;
347
348 default:
349 break;
350 }
351
352 return VINF_SUCCESS;
353}
354
355
356/**
357 * Terminates the GIM.
358 *
359 * Termination means cleaning up and freeing all resources,
360 * the VM itself is, at this point, powered off or suspended.
361 *
362 * @returns VBox status code.
363 * @param pVM Pointer to the VM.
364 */
365VMMR3_INT_DECL(int) GIMR3Term(PVM pVM)
366{
367 switch (pVM->gim.s.enmProviderId)
368 {
369 case GIMPROVIDERID_HYPERV:
370 return gimR3HvTerm(pVM);
371
372 case GIMPROVIDERID_KVM:
373 return gimR3KvmTerm(pVM);
374
375 default:
376 break;
377 }
378 return VINF_SUCCESS;
379}
380
381
382/**
383 * The VM is being reset.
384 *
385 * For the GIM component this means unmapping and unregistering MMIO2 regions
386 * and other provider-specific resets.
387 *
388 * @returns VBox status code.
389 * @param pVM Pointer to the VM.
390 */
391VMMR3_INT_DECL(void) GIMR3Reset(PVM pVM)
392{
393 switch (pVM->gim.s.enmProviderId)
394 {
395 case GIMPROVIDERID_HYPERV:
396 return gimR3HvReset(pVM);
397
398 case GIMPROVIDERID_KVM:
399 return gimR3KvmReset(pVM);
400
401 default:
402 break;
403 }
404}
405
406
407/**
408 * Registers the GIM device with VMM.
409 *
410 * @param pVM Pointer to the VM.
411 * @param pDevIns Pointer to the GIM device instance.
412 */
413VMMR3DECL(void) GIMR3GimDeviceRegister(PVM pVM, PPDMDEVINS pDevIns)
414{
415 pVM->gim.s.pDevInsR3 = pDevIns;
416}
417
418
419/**
420 * Returns the array of MMIO2 regions that are expected to be registered and
421 * later mapped into the guest-physical address space for the GIM provider
422 * configured for the VM.
423 *
424 * @returns Pointer to an array of GIM MMIO2 regions, may return NULL.
425 * @param pVM Pointer to the VM.
426 * @param pcRegions Where to store the number of items in the array.
427 *
428 * @remarks The caller does not own and therefore must -NOT- try to free the
429 * returned pointer.
430 */
431VMMR3DECL(PGIMMMIO2REGION) GIMR3GetMmio2Regions(PVM pVM, uint32_t *pcRegions)
432{
433 Assert(pVM);
434 Assert(pcRegions);
435
436 *pcRegions = 0;
437 switch (pVM->gim.s.enmProviderId)
438 {
439 case GIMPROVIDERID_HYPERV:
440 return gimR3HvGetMmio2Regions(pVM, pcRegions);
441
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 * @callback_method_impl{FNPGMPHYSHANDLER,
483 * Write access handler for mapped MMIO2 pages. Currently ignores writes.}
484 *
485 * @todo In the future we might want to let the GIM provider decide what the
486 * handler should do (like throwing #GP faults).
487 */
488static DECLCALLBACK(VBOXSTRICTRC)
489gimR3Mmio2WriteHandler(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf,
490 PGMACCESSTYPE enmAccessType, PGMACCESSORIGIN enmOrigin, void *pvUser)
491{
492 /*
493 * Ignore writes to the mapped MMIO2 page.
494 */
495 Assert(enmAccessType == PGMACCESSTYPE_WRITE);
496 return VINF_SUCCESS; /** @todo Hyper-V says we should #GP(0) fault for writes to the Hypercall and TSC page. */
497}
498
499
500/**
501 * Maps a registered MMIO2 region in the guest address space. The region will be
502 * made read-only and writes from the guest will be ignored.
503 *
504 * @returns VBox status code.
505 * @param pVM Pointer to the VM.
506 * @param pRegion Pointer to the GIM MMIO2 region.
507 * @param GCPhysRegion Where in the guest address space to map the region.
508 */
509VMMR3_INT_DECL(int) GIMR3Mmio2Map(PVM pVM, PGIMMMIO2REGION pRegion, RTGCPHYS GCPhysRegion)
510{
511 PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
512 AssertPtr(pDevIns);
513
514 /* The guest-physical address must be page-aligned. */
515 if (GCPhysRegion & PAGE_OFFSET_MASK)
516 {
517 LogFunc(("%s: %#RGp not paging aligned\n", pRegion->szDescription, GCPhysRegion));
518 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
519 }
520
521 /* Allow only normal pages to be overlaid using our MMIO2 pages (disallow MMIO, ROM, reserved pages). */
522 /** @todo Hyper-V doesn't seem to be very strict about this, may be relax
523 * later if some guest really requires it. */
524 if (!PGMPhysIsGCPhysNormal(pVM, GCPhysRegion))
525 {
526 LogFunc(("%s: %#RGp is not normal memory\n", pRegion->szDescription, GCPhysRegion));
527 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
528 }
529
530 if (!pRegion->fRegistered)
531 {
532 LogFunc(("%s: Region has not been registered.\n"));
533 return VERR_GIM_IPE_1;
534 }
535
536 /*
537 * Map the MMIO2 region over the specified guest-physical address.
538 */
539 int rc = PDMDevHlpMMIO2Map(pDevIns, pRegion->iRegion, GCPhysRegion);
540 if (RT_SUCCESS(rc))
541 {
542 /*
543 * Install access-handlers for the mapped page to prevent (ignore) writes to it
544 * from the guest.
545 */
546 if (pVM->gim.s.hSemiReadOnlyMmio2Handler == NIL_PGMPHYSHANDLERTYPE)
547 rc = PGMR3HandlerPhysicalTypeRegister(pVM, PGMPHYSHANDLERKIND_WRITE,
548 gimR3Mmio2WriteHandler,
549 NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NULL /* pszPfHandlerR0 */,
550 NULL /* pszModRC */, NULL /* pszHandlerRC */, NULL /* pszPfHandlerRC */,
551 "GIM read-only MMIO2 handler",
552 &pVM->gim.s.hSemiReadOnlyMmio2Handler);
553 if (RT_SUCCESS(rc))
554 {
555 rc = PGMHandlerPhysicalRegister(pVM, GCPhysRegion, GCPhysRegion + (pRegion->cbRegion - 1),
556 pVM->gim.s.hSemiReadOnlyMmio2Handler,
557 NULL /* pvUserR3 */, NIL_RTR0PTR /* pvUserR0 */, NIL_RTRCPTR /* pvUserRC */,
558 pRegion->szDescription);
559 if (RT_SUCCESS(rc))
560 {
561 pRegion->fMapped = true;
562 pRegion->GCPhysPage = GCPhysRegion;
563 return rc;
564 }
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 PGMPHYSHANDLERKIND_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
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