VirtualBox

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

Last change on this file since 107227 was 107227, checked in by vboxsync, 6 weeks ago

VMM: Cleaning up ARMv8 / x86 split. jiraref:VBP-1470

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.1 KB
Line 
1/* $Id: GIM.cpp 107227 2024-12-04 15:20:14Z vboxsync $ */
2/** @file
3 * GIM - Guest Interface Manager.
4 */
5
6/*
7 * Copyright (C) 2014-2024 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_X86)
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_X86)
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_X86)
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_X86)
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_X86)
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 RT_NOREF(pSSM);
353
354 switch (pVM->gim.s.enmProviderId)
355 {
356#if defined(VBOX_VMM_TARGET_X86)
357 case GIMPROVIDERID_HYPERV:
358 return gimR3HvLoadDone(pVM, pSSM);
359#endif
360 default:
361 return VINF_SUCCESS;
362 }
363}
364
365
366/**
367 * Terminates the GIM.
368 *
369 * Termination means cleaning up and freeing all resources,
370 * the VM itself is, at this point, powered off or suspended.
371 *
372 * @returns VBox status code.
373 * @param pVM The cross context VM structure.
374 */
375VMMR3_INT_DECL(int) GIMR3Term(PVM pVM)
376{
377 switch (pVM->gim.s.enmProviderId)
378 {
379#if defined(VBOX_VMM_TARGET_X86)
380 case GIMPROVIDERID_HYPERV:
381 return gimR3HvTerm(pVM);
382
383 case GIMPROVIDERID_KVM:
384 return gimR3KvmTerm(pVM);
385#endif
386 default:
387 return VINF_SUCCESS;
388 }
389}
390
391
392/**
393 * Applies relocations to data and code managed by this
394 * component. This function will be called at init and
395 * whenever the VMM need to relocate it self inside the GC.
396 *
397 * @param pVM The cross context VM structure.
398 * @param offDelta Relocation delta relative to old location.
399 */
400VMMR3_INT_DECL(void) GIMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
401{
402 RT_NOREF(offDelta);
403
404 switch (pVM->gim.s.enmProviderId)
405 {
406#if defined(VBOX_VMM_TARGET_X86)
407 case GIMPROVIDERID_HYPERV:
408 gimR3HvRelocate(pVM, offDelta);
409 break;
410#endif
411 default:
412 break;
413 }
414}
415
416
417/**
418 * The VM is being reset.
419 *
420 * For the GIM component this means unmapping and unregistering MMIO2 regions
421 * and other provider-specific resets.
422 *
423 * @param pVM The cross context VM structure.
424 */
425VMMR3_INT_DECL(void) GIMR3Reset(PVM pVM)
426{
427 switch (pVM->gim.s.enmProviderId)
428 {
429#if defined(VBOX_VMM_TARGET_X86)
430 case GIMPROVIDERID_HYPERV:
431 gimR3HvReset(pVM);
432 break;
433
434 case GIMPROVIDERID_KVM:
435 gimR3KvmReset(pVM);
436 break;
437#endif
438 default:
439 break;
440 }
441}
442
443
444/**
445 * Registers the GIM device with VMM.
446 *
447 * @param pVM The cross context VM structure.
448 * @param pDevIns Pointer to the GIM device instance.
449 * @param pDbg Pointer to the GIM device debug structure, can be
450 * NULL.
451 */
452VMMR3DECL(void) GIMR3GimDeviceRegister(PVM pVM, PPDMDEVINS pDevIns, PGIMDEBUG pDbg)
453{
454 pVM->gim.s.pDevInsR3 = pDevIns;
455 pVM->gim.s.pDbgR3 = pDbg;
456}
457
458
459/**
460 * Gets debug setup specified by the provider.
461 *
462 * @returns VBox status code.
463 * @param pVM The cross context VM structure.
464 * @param pDbgSetup Where to store the debug setup details.
465 */
466VMMR3DECL(int) GIMR3GetDebugSetup(PVM pVM, PGIMDEBUGSETUP pDbgSetup)
467{
468 AssertReturn(pVM, VERR_INVALID_PARAMETER);
469 AssertReturn(pDbgSetup, VERR_INVALID_PARAMETER);
470
471 switch (pVM->gim.s.enmProviderId)
472 {
473#if defined(VBOX_VMM_TARGET_X86)
474 case GIMPROVIDERID_HYPERV:
475 return gimR3HvGetDebugSetup(pVM, pDbgSetup);
476#endif
477 default:
478 return VERR_GIM_NO_DEBUG_CONNECTION;
479 }
480}
481
482
483/**
484 * Read data from a host debug session.
485 *
486 * @returns VBox status code.
487 *
488 * @param pVM The cross context VM structure.
489 * @param pvRead The read buffer.
490 * @param pcbRead The size of the read buffer as well as where to store
491 * the number of bytes read.
492 * @param pfnReadComplete Callback when the buffer has been read and
493 * before signalling reading of the next buffer.
494 * Optional, can be NULL.
495 * @thread EMT.
496 */
497VMMR3_INT_DECL(int) gimR3DebugRead(PVM pVM, void *pvRead, size_t *pcbRead, PFNGIMDEBUGBUFREADCOMPLETED pfnReadComplete)
498{
499 PGIMDEBUG pDbg = pVM->gim.s.pDbgR3;
500 if (pDbg)
501 {
502 if (ASMAtomicReadBool(&pDbg->fDbgRecvBufRead) == true)
503 {
504 STAM_REL_COUNTER_INC(&pVM->gim.s.StatDbgRecv);
505 STAM_REL_COUNTER_ADD(&pVM->gim.s.StatDbgRecvBytes, pDbg->cbDbgRecvBufRead);
506
507 memcpy(pvRead, pDbg->pvDbgRecvBuf, pDbg->cbDbgRecvBufRead);
508 *pcbRead = pDbg->cbDbgRecvBufRead;
509 if (pfnReadComplete)
510 pfnReadComplete(pVM);
511 RTSemEventMultiSignal(pDbg->hDbgRecvThreadSem);
512 ASMAtomicWriteBool(&pDbg->fDbgRecvBufRead, false);
513 return VINF_SUCCESS;
514 }
515 else
516 *pcbRead = 0;
517 return VERR_NO_DATA;
518 }
519 return VERR_GIM_NO_DEBUG_CONNECTION;
520}
521
522
523/**
524 * Write data to a host debug session.
525 *
526 * @returns VBox status code.
527 *
528 * @param pVM The cross context VM structure.
529 * @param pvWrite The write buffer.
530 * @param pcbWrite The size of the write buffer as well as where to store
531 * the number of bytes written.
532 * @thread EMT.
533 */
534VMMR3_INT_DECL(int) gimR3DebugWrite(PVM pVM, void *pvWrite, size_t *pcbWrite)
535{
536 PGIMDEBUG pDbg = pVM->gim.s.pDbgR3;
537 if (pDbg)
538 {
539 PPDMISTREAM pDbgStream = pDbg->pDbgDrvStream;
540 if (pDbgStream)
541 {
542 size_t cbWrite = *pcbWrite;
543 int rc = pDbgStream->pfnWrite(pDbgStream, pvWrite, pcbWrite);
544 if ( RT_SUCCESS(rc)
545 && *pcbWrite == cbWrite)
546 {
547 STAM_REL_COUNTER_INC(&pVM->gim.s.StatDbgXmit);
548 STAM_REL_COUNTER_ADD(&pVM->gim.s.StatDbgXmitBytes, *pcbWrite);
549 }
550 return rc;
551 }
552 }
553 return VERR_GIM_NO_DEBUG_CONNECTION;
554}
555
556#if 0 /* ??? */
557
558/**
559 * @callback_method_impl{FNPGMPHYSHANDLER,
560 * Write access handler for mapped MMIO2 pages. Currently ignores writes.}
561 *
562 * @todo In the future we might want to let the GIM provider decide what the
563 * handler should do (like throwing \#GP faults).
564 */
565static DECLCALLBACK(VBOXSTRICTRC) gimR3Mmio2WriteHandler(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf,
566 size_t cbBuf, PGMACCESSTYPE enmAccessType, PGMACCESSORIGIN enmOrigin,
567 void *pvUser)
568{
569 RT_NOREF6(pVM, pVCpu, GCPhys, pvPhys, pvBuf, cbBuf);
570 RT_NOREF3(enmAccessType, enmOrigin, pvUser);
571
572 /*
573 * Ignore writes to the mapped MMIO2 page.
574 */
575 Assert(enmAccessType == PGMACCESSTYPE_WRITE);
576 return VINF_SUCCESS; /** @todo Hyper-V says we should \#GP(0) fault for writes to the Hypercall and TSC page. */
577}
578
579
580/**
581 * Unmaps a registered MMIO2 region in the guest address space and removes any
582 * access handlers for it.
583 *
584 * @returns VBox status code.
585 * @param pVM The cross context VM structure.
586 * @param pRegion Pointer to the GIM MMIO2 region.
587 */
588VMMR3_INT_DECL(int) gimR3Mmio2Unmap(PVM pVM, PGIMMMIO2REGION pRegion)
589{
590 AssertPtr(pVM);
591 AssertPtr(pRegion);
592
593 PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
594 AssertPtr(pDevIns);
595 if (pRegion->fMapped)
596 {
597 int rc = PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
598 AssertRC(rc);
599
600 rc = PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, pRegion->GCPhysPage);
601 if (RT_SUCCESS(rc))
602 {
603 pRegion->fMapped = false;
604 pRegion->GCPhysPage = NIL_RTGCPHYS;
605 }
606 }
607 return VINF_SUCCESS;
608}
609
610
611/**
612 * Maps a registered MMIO2 region in the guest address space.
613 *
614 * The region will be made read-only and writes from the guest will be ignored.
615 *
616 * @returns VBox status code.
617 * @param pVM The cross context VM structure.
618 * @param pRegion Pointer to the GIM MMIO2 region.
619 * @param GCPhysRegion Where in the guest address space to map the region.
620 */
621VMMR3_INT_DECL(int) GIMR3Mmio2Map(PVM pVM, PGIMMMIO2REGION pRegion, RTGCPHYS GCPhysRegion)
622{
623 PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
624 AssertPtr(pDevIns);
625
626 /* The guest-physical address must be page-aligned. */
627 if (GCPhysRegion & GUEST_PAGE_OFFSET_MASK)
628 {
629 LogFunc(("%s: %#RGp not paging aligned\n", pRegion->szDescription, GCPhysRegion));
630 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
631 }
632
633 /* Allow only normal pages to be overlaid using our MMIO2 pages (disallow MMIO, ROM, reserved pages). */
634 /** @todo Hyper-V doesn't seem to be very strict about this, may be relax
635 * later if some guest really requires it. */
636 if (!PGMPhysIsGCPhysNormal(pVM, GCPhysRegion))
637 {
638 LogFunc(("%s: %#RGp is not normal memory\n", pRegion->szDescription, GCPhysRegion));
639 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
640 }
641
642 if (!pRegion->fRegistered)
643 {
644 LogFunc(("%s: Region has not been registered.\n", pRegion->szDescription));
645 return VERR_GIM_IPE_1;
646 }
647
648 /*
649 * Map the MMIO2 region over the specified guest-physical address.
650 */
651 int rc = PDMDevHlpMMIOExMap(pDevIns, NULL, pRegion->iRegion, GCPhysRegion);
652 if (RT_SUCCESS(rc))
653 {
654 /*
655 * Install access-handlers for the mapped page to prevent (ignore) writes to it
656 * from the guest.
657 */
658 if (pVM->gim.s.hSemiReadOnlyMmio2Handler == NIL_PGMPHYSHANDLERTYPE)
659 rc = PGMR3HandlerPhysicalTypeRegister(pVM, PGMPHYSHANDLERKIND_WRITE,
660 gimR3Mmio2WriteHandler,
661 NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NULL /* pszPfHandlerR0 */,
662 NULL /* pszModRC */, NULL /* pszHandlerRC */, NULL /* pszPfHandlerRC */,
663 "GIM read-only MMIO2 handler",
664 &pVM->gim.s.hSemiReadOnlyMmio2Handler);
665 if (RT_SUCCESS(rc))
666 {
667 rc = PGMHandlerPhysicalRegister(pVM, GCPhysRegion, GCPhysRegion + (pRegion->cbRegion - 1),
668 pVM->gim.s.hSemiReadOnlyMmio2Handler,
669 NULL /* pvUserR3 */, NIL_RTR0PTR /* pvUserR0 */, NIL_RTRCPTR /* pvUserRC */,
670 pRegion->szDescription);
671 if (RT_SUCCESS(rc))
672 {
673 pRegion->fMapped = true;
674 pRegion->GCPhysPage = GCPhysRegion;
675 return rc;
676 }
677 }
678
679 PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, GCPhysRegion);
680 }
681
682 return rc;
683}
684
685
686/**
687 * Registers the physical handler for the registered and mapped MMIO2 region.
688 *
689 * @returns VBox status code.
690 * @param pVM The cross context VM structure.
691 * @param pRegion Pointer to the GIM MMIO2 region.
692 */
693VMMR3_INT_DECL(int) gimR3Mmio2HandlerPhysicalRegister(PVM pVM, PGIMMMIO2REGION pRegion)
694{
695 AssertPtr(pRegion);
696 AssertReturn(pRegion->fRegistered, VERR_GIM_IPE_2);
697 AssertReturn(pRegion->fMapped, VERR_GIM_IPE_3);
698
699 return PGMR3HandlerPhysicalRegister(pVM,
700 PGMPHYSHANDLERKIND_WRITE,
701 pRegion->GCPhysPage, pRegion->GCPhysPage + (pRegion->cbRegion - 1),
702 gimR3Mmio2WriteHandler, NULL /* pvUserR3 */,
703 NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NIL_RTR0PTR /* pvUserR0 */,
704 NULL /* pszModRC */, NULL /* pszHandlerRC */, NIL_RTRCPTR /* pvUserRC */,
705 pRegion->szDescription);
706}
707
708
709/**
710 * Deregisters the physical handler for the MMIO2 region.
711 *
712 * @returns VBox status code.
713 * @param pVM The cross context VM structure.
714 * @param pRegion Pointer to the GIM MMIO2 region.
715 */
716VMMR3_INT_DECL(int) gimR3Mmio2HandlerPhysicalDeregister(PVM pVM, PGIMMMIO2REGION pRegion)
717{
718 return PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
719}
720
721#endif
722
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