VirtualBox

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

Last change on this file since 80239 was 80191, checked in by vboxsync, 5 years ago

VMM/r3: Refactored VMCPU enumeration in preparation that aCpus will be replaced with a pointer array. Removed two raw-mode offset members from the CPUM and CPUMCPU sub-structures. bugref:9217 bugref:9517

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.3 KB
Line 
1/* $Id: GIM.cpp 80191 2019-08-08 00:36:57Z vboxsync $ */
2/** @file
3 * GIM - Guest Interface Manager.
4 */
5
6/*
7 * Copyright (C) 2014-2019 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 be 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/*********************************************************************************************************************************
50* Header Files *
51*********************************************************************************************************************************/
52#define VBOX_BUGREF_9217_PART_I
53#define LOG_GROUP LOG_GROUP_GIM
54#include <VBox/vmm/gim.h>
55#include <VBox/vmm/hm.h>
56#include <VBox/vmm/ssm.h>
57#include <VBox/vmm/pdmdev.h>
58#include "GIMInternal.h"
59#include <VBox/vmm/vm.h>
60
61#include <VBox/log.h>
62
63#include <iprt/err.h>
64#include <iprt/semaphore.h>
65#include <iprt/string.h>
66
67/* Include all GIM providers. */
68#include "GIMMinimalInternal.h"
69#include "GIMHvInternal.h"
70#include "GIMKvmInternal.h"
71
72
73/*********************************************************************************************************************************
74* Internal Functions *
75*********************************************************************************************************************************/
76static FNSSMINTSAVEEXEC gimR3Save;
77static FNSSMINTLOADEXEC gimR3Load;
78static FNSSMINTLOADDONE gimR3LoadDone;
79
80
81/**
82 * Initializes the GIM.
83 *
84 * @returns VBox status code.
85 * @param pVM The cross context VM structure.
86 */
87VMMR3_INT_DECL(int) GIMR3Init(PVM pVM)
88{
89 LogFlow(("GIMR3Init\n"));
90
91 /*
92 * Assert alignment and sizes.
93 */
94 AssertCompile(sizeof(pVM->gim.s) <= sizeof(pVM->gim.padding));
95 AssertCompile(sizeof(pVM->apCpusR3[0]->gim.s) <= sizeof(pVM->apCpusR3[0]->gim.padding));
96
97 /*
98 * Initialize members.
99 */
100 pVM->gim.s.hSemiReadOnlyMmio2Handler = NIL_PGMPHYSHANDLERTYPE;
101
102 /*
103 * Register the saved state data unit.
104 */
105 int rc = SSMR3RegisterInternal(pVM, "GIM", 0 /* uInstance */, GIM_SAVED_STATE_VERSION, sizeof(GIM),
106 NULL /* pfnLivePrep */, NULL /* pfnLiveExec */, NULL /* pfnLiveVote*/,
107 NULL /* pfnSavePrep */, gimR3Save, NULL /* pfnSaveDone */,
108 NULL /* pfnLoadPrep */, gimR3Load, gimR3LoadDone);
109 if (RT_FAILURE(rc))
110 return rc;
111
112 /*
113 * Read configuration.
114 */
115 PCFGMNODE pCfgNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "GIM/");
116
117 /*
118 * Validate the GIM settings.
119 */
120 rc = CFGMR3ValidateConfig(pCfgNode, "/GIM/", /* pszNode */
121 "Provider" /* pszValidValues */
122 "|Version",
123 "HyperV", /* pszValidNodes */
124 "GIM", /* pszWho */
125 0); /* uInstance */
126 if (RT_FAILURE(rc))
127 return rc;
128
129 /** @cfgm{/GIM/Provider, string}
130 * The name of the GIM provider. The default is "none". */
131 char szProvider[64];
132 rc = CFGMR3QueryStringDef(pCfgNode, "Provider", szProvider, sizeof(szProvider), "None");
133 AssertLogRelRCReturn(rc, rc);
134
135 /** @cfgm{/GIM/Version, uint32_t}
136 * The interface version. The default is 0, which means "provide the most
137 * up-to-date implementation". */
138 uint32_t uVersion;
139 rc = CFGMR3QueryU32Def(pCfgNode, "Version", &uVersion, 0 /* default */);
140 AssertLogRelRCReturn(rc, rc);
141
142 /*
143 * Setup the GIM provider for this VM.
144 */
145 LogRel(("GIM: Using provider '%s' (Implementation version: %u)\n", szProvider, uVersion));
146 if (!RTStrCmp(szProvider, "None"))
147 pVM->gim.s.enmProviderId = GIMPROVIDERID_NONE;
148 else
149 {
150 pVM->gim.s.u32Version = uVersion;
151 /** @todo r=bird: Because u32Version is saved, it should be translated to the
152 * 'most up-to-date implementation' version number when 0. Otherwise,
153 * we'll have abiguities when loading the state of older VMs. */
154 if (!RTStrCmp(szProvider, "Minimal"))
155 {
156 pVM->gim.s.enmProviderId = GIMPROVIDERID_MINIMAL;
157 rc = gimR3MinimalInit(pVM);
158 }
159 else if (!RTStrCmp(szProvider, "HyperV"))
160 {
161 pVM->gim.s.enmProviderId = GIMPROVIDERID_HYPERV;
162 rc = gimR3HvInit(pVM, pCfgNode);
163 }
164 else if (!RTStrCmp(szProvider, "KVM"))
165 {
166 pVM->gim.s.enmProviderId = GIMPROVIDERID_KVM;
167 rc = gimR3KvmInit(pVM);
168 }
169 else
170 rc = VMR3SetError(pVM->pUVM, VERR_GIM_INVALID_PROVIDER, RT_SRC_POS, "Provider '%s' unknown.", szProvider);
171 }
172
173 /*
174 * Statistics.
175 */
176 STAM_REL_REG_USED(pVM, &pVM->gim.s.StatDbgXmit, STAMTYPE_COUNTER, "/GIM/Debug/Transmit", STAMUNIT_OCCURENCES, "Debug packets sent.");
177 STAM_REL_REG_USED(pVM, &pVM->gim.s.StatDbgXmitBytes, STAMTYPE_COUNTER, "/GIM/Debug/TransmitBytes", STAMUNIT_OCCURENCES, "Debug bytes sent.");
178 STAM_REL_REG_USED(pVM, &pVM->gim.s.StatDbgRecv, STAMTYPE_COUNTER, "/GIM/Debug/Receive", STAMUNIT_OCCURENCES, "Debug packets received.");
179 STAM_REL_REG_USED(pVM, &pVM->gim.s.StatDbgRecvBytes, STAMTYPE_COUNTER, "/GIM/Debug/ReceiveBytes", STAMUNIT_OCCURENCES, "Debug bytes received.");
180
181 STAM_REL_REG_USED(pVM, &pVM->gim.s.StatHypercalls, STAMTYPE_COUNTER, "/GIM/Hypercalls", STAMUNIT_OCCURENCES, "Number of hypercalls initiated.");
182 return rc;
183}
184
185
186/**
187 * Initializes the remaining bits of the GIM provider.
188 *
189 * This is called after initializing HM and most other VMM components.
190 *
191 * @returns VBox status code.
192 * @param pVM The cross context VM structure.
193 * @thread EMT(0)
194 */
195VMMR3_INT_DECL(int) GIMR3InitCompleted(PVM pVM)
196{
197 switch (pVM->gim.s.enmProviderId)
198 {
199 case GIMPROVIDERID_MINIMAL:
200 return gimR3MinimalInitCompleted(pVM);
201
202 case GIMPROVIDERID_HYPERV:
203 return gimR3HvInitCompleted(pVM);
204
205 case GIMPROVIDERID_KVM:
206 return gimR3KvmInitCompleted(pVM);
207
208 default:
209 break;
210 }
211
212 if (!TMR3CpuTickIsFixedRateMonotonic(pVM, true /* fWithParavirtEnabled */))
213 LogRel(("GIM: Warning!!! Host TSC is unstable. The guest may behave unpredictably with a paravirtualized clock.\n"));
214
215 return VINF_SUCCESS;
216}
217
218
219/**
220 * @callback_method_impl{FNSSMINTSAVEEXEC}
221 */
222static DECLCALLBACK(int) gimR3Save(PVM pVM, PSSMHANDLE pSSM)
223{
224 AssertReturn(pVM, VERR_INVALID_PARAMETER);
225 AssertReturn(pSSM, VERR_SSM_INVALID_STATE);
226
227 int rc = VINF_SUCCESS;
228#if 0
229 /* Save per-CPU data. */
230 SSMR3PutU32(pSSM, pVM->cCpus);
231 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
232 {
233 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
234 rc = SSMR3PutXYZ(pSSM, pVCpu->gim.s.XYZ);
235 }
236#endif
237
238 /*
239 * Save per-VM data.
240 */
241 SSMR3PutU32(pSSM, pVM->gim.s.enmProviderId);
242 SSMR3PutU32(pSSM, pVM->gim.s.u32Version);
243
244 /*
245 * Save provider-specific data.
246 */
247 switch (pVM->gim.s.enmProviderId)
248 {
249 case GIMPROVIDERID_HYPERV:
250 rc = gimR3HvSave(pVM, pSSM);
251 AssertRCReturn(rc, rc);
252 break;
253
254 case GIMPROVIDERID_KVM:
255 rc = gimR3KvmSave(pVM, pSSM);
256 AssertRCReturn(rc, rc);
257 break;
258
259 default:
260 break;
261 }
262
263 return rc;
264}
265
266
267/**
268 * @callback_method_impl{FNSSMINTLOADEXEC}
269 */
270static DECLCALLBACK(int) gimR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
271{
272 if (uPass != SSM_PASS_FINAL)
273 return VINF_SUCCESS;
274 if (uVersion != GIM_SAVED_STATE_VERSION)
275 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
276
277 int rc;
278#if 0
279 /* Load per-CPU data. */
280 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
281 {
282 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
283 rc = SSMR3PutXYZ(pSSM, pVCpu->gim.s.XYZ);
284 }
285#endif
286
287 /*
288 * Load per-VM data.
289 */
290 uint32_t uProviderId;
291 uint32_t uProviderVersion;
292
293 rc = SSMR3GetU32(pSSM, &uProviderId); AssertRCReturn(rc, rc);
294 rc = SSMR3GetU32(pSSM, &uProviderVersion); AssertRCReturn(rc, rc);
295
296 if ((GIMPROVIDERID)uProviderId != pVM->gim.s.enmProviderId)
297 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Saved GIM provider %u differs from the configured one (%u)."),
298 uProviderId, pVM->gim.s.enmProviderId);
299#if 0 /** @todo r=bird: Figure out what you mean to do here with the version. */
300 if (uProviderVersion != pVM->gim.s.u32Version)
301 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Saved GIM provider version %u differs from the configured one (%u)."),
302 uProviderVersion, pVM->gim.s.u32Version);
303#else
304 pVM->gim.s.u32Version = uProviderVersion;
305#endif
306
307 /*
308 * Load provider-specific data.
309 */
310 switch (pVM->gim.s.enmProviderId)
311 {
312 case GIMPROVIDERID_HYPERV:
313 rc = gimR3HvLoad(pVM, pSSM);
314 AssertRCReturn(rc, rc);
315 break;
316
317 case GIMPROVIDERID_KVM:
318 rc = gimR3KvmLoad(pVM, pSSM);
319 AssertRCReturn(rc, rc);
320 break;
321
322 default:
323 break;
324 }
325
326 return VINF_SUCCESS;
327}
328
329
330/**
331 * @callback_method_impl{FNSSMINTLOADDONE}
332 */
333static DECLCALLBACK(int) gimR3LoadDone(PVM pVM, PSSMHANDLE pSSM)
334{
335 switch (pVM->gim.s.enmProviderId)
336 {
337 case GIMPROVIDERID_HYPERV:
338 return gimR3HvLoadDone(pVM, pSSM);
339
340 default:
341 return VINF_SUCCESS;
342 }
343}
344
345
346/**
347 * Terminates the GIM.
348 *
349 * Termination means cleaning up and freeing all resources,
350 * the VM itself is, at this point, powered off or suspended.
351 *
352 * @returns VBox status code.
353 * @param pVM The cross context VM structure.
354 */
355VMMR3_INT_DECL(int) GIMR3Term(PVM pVM)
356{
357 switch (pVM->gim.s.enmProviderId)
358 {
359 case GIMPROVIDERID_HYPERV:
360 return gimR3HvTerm(pVM);
361
362 case GIMPROVIDERID_KVM:
363 return gimR3KvmTerm(pVM);
364
365 default:
366 break;
367 }
368 return VINF_SUCCESS;
369}
370
371
372/**
373 * Applies relocations to data and code managed by this
374 * component. This function will be called at init and
375 * whenever the VMM need to relocate it self inside the GC.
376 *
377 * @param pVM The cross context VM structure.
378 * @param offDelta Relocation delta relative to old location.
379 */
380VMMR3_INT_DECL(void) GIMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
381{
382 switch (pVM->gim.s.enmProviderId)
383 {
384 case GIMPROVIDERID_HYPERV:
385 gimR3HvRelocate(pVM, offDelta);
386 break;
387
388 default:
389 break;
390 }
391}
392
393
394/**
395 * The VM is being reset.
396 *
397 * For the GIM component this means unmapping and unregistering MMIO2 regions
398 * and other provider-specific resets.
399 *
400 * @returns VBox status code.
401 * @param pVM The cross context VM structure.
402 */
403VMMR3_INT_DECL(void) GIMR3Reset(PVM pVM)
404{
405 switch (pVM->gim.s.enmProviderId)
406 {
407 case GIMPROVIDERID_HYPERV:
408 return gimR3HvReset(pVM);
409
410 case GIMPROVIDERID_KVM:
411 return gimR3KvmReset(pVM);
412
413 default:
414 break;
415 }
416}
417
418
419/**
420 * Registers the GIM device with VMM.
421 *
422 * @param pVM The cross context VM structure.
423 * @param pDevIns Pointer to the GIM device instance.
424 * @param pDbg Pointer to the GIM device debug structure, can be
425 * NULL.
426 */
427VMMR3DECL(void) GIMR3GimDeviceRegister(PVM pVM, PPDMDEVINS pDevIns, PGIMDEBUG pDbg)
428{
429 pVM->gim.s.pDevInsR3 = pDevIns;
430 pVM->gim.s.pDbgR3 = pDbg;
431}
432
433
434/**
435 * Gets debug setup specified by the provider.
436 *
437 * @returns VBox status code.
438 * @param pVM The cross context VM structure.
439 * @param pDbgSetup Where to store the debug setup details.
440 */
441VMMR3DECL(int) GIMR3GetDebugSetup(PVM pVM, PGIMDEBUGSETUP pDbgSetup)
442{
443 AssertReturn(pVM, VERR_INVALID_PARAMETER);
444 AssertReturn(pDbgSetup, VERR_INVALID_PARAMETER);
445
446 switch (pVM->gim.s.enmProviderId)
447 {
448 case GIMPROVIDERID_HYPERV:
449 return gimR3HvGetDebugSetup(pVM, pDbgSetup);
450 default:
451 break;
452 }
453 return VERR_GIM_NO_DEBUG_CONNECTION;
454}
455
456
457/**
458 * Read data from a host debug session.
459 *
460 * @returns VBox status code.
461 *
462 * @param pVM The cross context VM structure.
463 * @param pvRead The read buffer.
464 * @param pcbRead The size of the read buffer as well as where to store
465 * the number of bytes read.
466 * @param pfnReadComplete Callback when the buffer has been read and
467 * before signalling reading of the next buffer.
468 * Optional, can be NULL.
469 * @thread EMT.
470 */
471VMMR3_INT_DECL(int) gimR3DebugRead(PVM pVM, void *pvRead, size_t *pcbRead, PFNGIMDEBUGBUFREADCOMPLETED pfnReadComplete)
472{
473 PGIMDEBUG pDbg = pVM->gim.s.pDbgR3;
474 if (pDbg)
475 {
476 if (ASMAtomicReadBool(&pDbg->fDbgRecvBufRead) == true)
477 {
478 STAM_REL_COUNTER_INC(&pVM->gim.s.StatDbgRecv);
479 STAM_REL_COUNTER_ADD(&pVM->gim.s.StatDbgRecvBytes, pDbg->cbDbgRecvBufRead);
480
481 memcpy(pvRead, pDbg->pvDbgRecvBuf, pDbg->cbDbgRecvBufRead);
482 *pcbRead = pDbg->cbDbgRecvBufRead;
483 if (pfnReadComplete)
484 pfnReadComplete(pVM);
485 RTSemEventMultiSignal(pDbg->hDbgRecvThreadSem);
486 ASMAtomicWriteBool(&pDbg->fDbgRecvBufRead, false);
487 return VINF_SUCCESS;
488 }
489 else
490 *pcbRead = 0;
491 return VERR_NO_DATA;
492 }
493 return VERR_GIM_NO_DEBUG_CONNECTION;
494}
495
496
497/**
498 * Write data to a host debug session.
499 *
500 * @returns VBox status code.
501 *
502 * @param pVM The cross context VM structure.
503 * @param pvWrite The write buffer.
504 * @param pcbWrite The size of the write buffer as well as where to store
505 * the number of bytes written.
506 * @thread EMT.
507 */
508VMMR3_INT_DECL(int) gimR3DebugWrite(PVM pVM, void *pvWrite, size_t *pcbWrite)
509{
510 PGIMDEBUG pDbg = pVM->gim.s.pDbgR3;
511 if (pDbg)
512 {
513 PPDMISTREAM pDbgStream = pDbg->pDbgDrvStream;
514 if (pDbgStream)
515 {
516 size_t cbWrite = *pcbWrite;
517 int rc = pDbgStream->pfnWrite(pDbgStream, pvWrite, pcbWrite);
518 if ( RT_SUCCESS(rc)
519 && *pcbWrite == cbWrite)
520 {
521 STAM_REL_COUNTER_INC(&pVM->gim.s.StatDbgXmit);
522 STAM_REL_COUNTER_ADD(&pVM->gim.s.StatDbgXmitBytes, *pcbWrite);
523 }
524 return rc;
525 }
526 }
527 return VERR_GIM_NO_DEBUG_CONNECTION;
528}
529
530
531/**
532 * Returns the array of MMIO2 regions that are expected to be registered and
533 * later mapped into the guest-physical address space for the GIM provider
534 * configured for the VM.
535 *
536 * @returns Pointer to an array of GIM MMIO2 regions, may return NULL.
537 * @param pVM The cross context VM structure.
538 * @param pcRegions Where to store the number of items in the array.
539 *
540 * @remarks The caller does not own and therefore must -NOT- try to free the
541 * returned pointer.
542 */
543VMMR3DECL(PGIMMMIO2REGION) GIMR3GetMmio2Regions(PVM pVM, uint32_t *pcRegions)
544{
545 Assert(pVM);
546 Assert(pcRegions);
547
548 *pcRegions = 0;
549 switch (pVM->gim.s.enmProviderId)
550 {
551 case GIMPROVIDERID_HYPERV:
552 return gimR3HvGetMmio2Regions(pVM, pcRegions);
553
554 default:
555 break;
556 }
557
558 return NULL;
559}
560
561#if 0 /* ??? */
562
563/**
564 * @callback_method_impl{FNPGMPHYSHANDLER,
565 * Write access handler for mapped MMIO2 pages. Currently ignores writes.}
566 *
567 * @todo In the future we might want to let the GIM provider decide what the
568 * handler should do (like throwing \#GP faults).
569 */
570static DECLCALLBACK(VBOXSTRICTRC) gimR3Mmio2WriteHandler(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf,
571 size_t cbBuf, PGMACCESSTYPE enmAccessType, PGMACCESSORIGIN enmOrigin,
572 void *pvUser)
573{
574 RT_NOREF6(pVM, pVCpu, GCPhys, pvPhys, pvBuf, cbBuf);
575 RT_NOREF3(enmAccessType, enmOrigin, pvUser);
576
577 /*
578 * Ignore writes to the mapped MMIO2 page.
579 */
580 Assert(enmAccessType == PGMACCESSTYPE_WRITE);
581 return VINF_SUCCESS; /** @todo Hyper-V says we should \#GP(0) fault for writes to the Hypercall and TSC page. */
582}
583
584
585/**
586 * Unmaps a registered MMIO2 region in the guest address space and removes any
587 * access handlers for it.
588 *
589 * @returns VBox status code.
590 * @param pVM The cross context VM structure.
591 * @param pRegion Pointer to the GIM MMIO2 region.
592 */
593VMMR3_INT_DECL(int) gimR3Mmio2Unmap(PVM pVM, PGIMMMIO2REGION pRegion)
594{
595 AssertPtr(pVM);
596 AssertPtr(pRegion);
597
598 PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
599 AssertPtr(pDevIns);
600 if (pRegion->fMapped)
601 {
602 int rc = PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
603 AssertRC(rc);
604
605 rc = PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, pRegion->GCPhysPage);
606 if (RT_SUCCESS(rc))
607 {
608 pRegion->fMapped = false;
609 pRegion->GCPhysPage = NIL_RTGCPHYS;
610 }
611 }
612 return VINF_SUCCESS;
613}
614
615
616/**
617 * Maps a registered MMIO2 region in the guest address space.
618 *
619 * The region will be made read-only and writes from the guest will be ignored.
620 *
621 * @returns VBox status code.
622 * @param pVM The cross context VM structure.
623 * @param pRegion Pointer to the GIM MMIO2 region.
624 * @param GCPhysRegion Where in the guest address space to map the region.
625 */
626VMMR3_INT_DECL(int) GIMR3Mmio2Map(PVM pVM, PGIMMMIO2REGION pRegion, RTGCPHYS GCPhysRegion)
627{
628 PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
629 AssertPtr(pDevIns);
630
631 /* The guest-physical address must be page-aligned. */
632 if (GCPhysRegion & PAGE_OFFSET_MASK)
633 {
634 LogFunc(("%s: %#RGp not paging aligned\n", pRegion->szDescription, GCPhysRegion));
635 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
636 }
637
638 /* Allow only normal pages to be overlaid using our MMIO2 pages (disallow MMIO, ROM, reserved pages). */
639 /** @todo Hyper-V doesn't seem to be very strict about this, may be relax
640 * later if some guest really requires it. */
641 if (!PGMPhysIsGCPhysNormal(pVM, GCPhysRegion))
642 {
643 LogFunc(("%s: %#RGp is not normal memory\n", pRegion->szDescription, GCPhysRegion));
644 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
645 }
646
647 if (!pRegion->fRegistered)
648 {
649 LogFunc(("%s: Region has not been registered.\n", pRegion->szDescription));
650 return VERR_GIM_IPE_1;
651 }
652
653 /*
654 * Map the MMIO2 region over the specified guest-physical address.
655 */
656 int rc = PDMDevHlpMMIOExMap(pDevIns, NULL, pRegion->iRegion, GCPhysRegion);
657 if (RT_SUCCESS(rc))
658 {
659 /*
660 * Install access-handlers for the mapped page to prevent (ignore) writes to it
661 * from the guest.
662 */
663 if (pVM->gim.s.hSemiReadOnlyMmio2Handler == NIL_PGMPHYSHANDLERTYPE)
664 rc = PGMR3HandlerPhysicalTypeRegister(pVM, PGMPHYSHANDLERKIND_WRITE,
665 gimR3Mmio2WriteHandler,
666 NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NULL /* pszPfHandlerR0 */,
667 NULL /* pszModRC */, NULL /* pszHandlerRC */, NULL /* pszPfHandlerRC */,
668 "GIM read-only MMIO2 handler",
669 &pVM->gim.s.hSemiReadOnlyMmio2Handler);
670 if (RT_SUCCESS(rc))
671 {
672 rc = PGMHandlerPhysicalRegister(pVM, GCPhysRegion, GCPhysRegion + (pRegion->cbRegion - 1),
673 pVM->gim.s.hSemiReadOnlyMmio2Handler,
674 NULL /* pvUserR3 */, NIL_RTR0PTR /* pvUserR0 */, NIL_RTRCPTR /* pvUserRC */,
675 pRegion->szDescription);
676 if (RT_SUCCESS(rc))
677 {
678 pRegion->fMapped = true;
679 pRegion->GCPhysPage = GCPhysRegion;
680 return rc;
681 }
682 }
683
684 PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, GCPhysRegion);
685 }
686
687 return rc;
688}
689
690
691/**
692 * Registers the physical handler for the registered and mapped MMIO2 region.
693 *
694 * @returns VBox status code.
695 * @param pVM The cross context VM structure.
696 * @param pRegion Pointer to the GIM MMIO2 region.
697 */
698VMMR3_INT_DECL(int) gimR3Mmio2HandlerPhysicalRegister(PVM pVM, PGIMMMIO2REGION pRegion)
699{
700 AssertPtr(pRegion);
701 AssertReturn(pRegion->fRegistered, VERR_GIM_IPE_2);
702 AssertReturn(pRegion->fMapped, VERR_GIM_IPE_3);
703
704 return PGMR3HandlerPhysicalRegister(pVM,
705 PGMPHYSHANDLERKIND_WRITE,
706 pRegion->GCPhysPage, pRegion->GCPhysPage + (pRegion->cbRegion - 1),
707 gimR3Mmio2WriteHandler, NULL /* pvUserR3 */,
708 NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NIL_RTR0PTR /* pvUserR0 */,
709 NULL /* pszModRC */, NULL /* pszHandlerRC */, NIL_RTRCPTR /* pvUserRC */,
710 pRegion->szDescription);
711}
712
713
714/**
715 * Deregisters the physical handler for the MMIO2 region.
716 *
717 * @returns VBox status code.
718 * @param pVM The cross context VM structure.
719 * @param pRegion Pointer to the GIM MMIO2 region.
720 */
721VMMR3_INT_DECL(int) gimR3Mmio2HandlerPhysicalDeregister(PVM pVM, PGIMMMIO2REGION pRegion)
722{
723 return PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
724}
725
726#endif
727
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