VirtualBox

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

Last change on this file since 100651 was 100108, checked in by vboxsync, 18 months ago

*: Fix build issues when setting VBOX_WITH_WARNINGS_AS_ERRORS=1 on darwin.arm64 and make it a default, bugref:10469

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