VirtualBox

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

Last change on this file since 99740 was 99739, checked in by vboxsync, 18 months ago

*: doxygen corrections (mostly about removing @returns from functions returning void).

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