summaryrefslogtreecommitdiff
path: root/src/VBox/VMM/VMMR3/GIM.cpp
blob: 145765ba21250bce8e1ce4e2ff03860c69e082f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/* $Id$ */
/** @file
 * GIM - Guest Interface Manager.
 */

/*
 * Copyright (C) 2014-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

/** @page pg_gim        GIM - The Guest Interface Manager
 *
 * The Guest Interface Manager abstracts an interface provider through which
 * guests may interact with the hypervisor.
 *
 * @see grp_gim
 *
 *
 * @section sec_gim_provider   Providers
 *
 * A GIM provider implements a particular hypervisor interface such as Microsoft
 * Hyper-V, Linux KVM and so on. It hooks into various components in the VMM to
 * ease the guest in running under a recognized, virtualized environment.
 *
 * The GIM provider configured for the VM needs to be recognized by the guest OS
 * in order to make use of features supported by the interface. Since it
 * requires co-operation from the guest OS, a GIM provider may also be referred to
 * as a paravirtualization interface.
 *
 * One of the goals of having a paravirtualized interface is for enabling guests
 * to be more accurate and efficient when operating in a virtualized
 * environment. For instance, a guest OS which interfaces to VirtualBox through
 * a GIM provider may rely on the provider for supplying the correct TSC
 * frequency of the host processor. The guest can then avoid caliberating the
 * TSC itself, resulting in higher accuracy and better performance.
 *
 * At most, only one GIM provider can be active for a running VM and cannot be
 * changed during the lifetime of the VM.
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_GIM
#include <VBox/vmm/gim.h>
#include <VBox/vmm/hm.h>
#include <VBox/vmm/ssm.h>
#include <VBox/vmm/pdmdev.h>
#include "GIMInternal.h"
#include <VBox/vmm/vm.h>

#include <VBox/log.h>

#include <iprt/err.h>
#include <iprt/semaphore.h>
#include <iprt/string.h>

#if !defined(VBOX_VMM_TARGET_ARMV8)
/* Include all GIM providers. */
# include "GIMMinimalInternal.h"
# include "GIMHvInternal.h"
# include "GIMKvmInternal.h"
#endif


/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/
static FNSSMINTSAVEEXEC  gimR3Save;
static FNSSMINTLOADEXEC  gimR3Load;
static FNSSMINTLOADDONE  gimR3LoadDone;


/**
 * Initializes the GIM.
 *
 * @returns VBox status code.
 * @param   pVM         The cross context VM structure.
 */
VMMR3_INT_DECL(int) GIMR3Init(PVM pVM)
{
    LogFlow(("GIMR3Init\n"));

    /*
     * Assert alignment and sizes.
     */
    AssertCompile(sizeof(pVM->gim.s) <= sizeof(pVM->gim.padding));
    AssertCompile(sizeof(pVM->apCpusR3[0]->gim.s) <= sizeof(pVM->apCpusR3[0]->gim.padding));

    /*
     * Initialize members.
     */
    pVM->gim.s.hSemiReadOnlyMmio2Handler = NIL_PGMPHYSHANDLERTYPE;

    /*
     * Register the saved state data unit.
     */
    int rc = SSMR3RegisterInternal(pVM, "GIM", 0 /* uInstance */, GIM_SAVED_STATE_VERSION, sizeof(GIM),
                                   NULL /* pfnLivePrep */, NULL /* pfnLiveExec */, NULL /* pfnLiveVote*/,
                                   NULL /* pfnSavePrep */, gimR3Save,              NULL /* pfnSaveDone */,
                                   NULL /* pfnLoadPrep */, gimR3Load,              gimR3LoadDone);
    if (RT_FAILURE(rc))
        return rc;

    /*
     * Read configuration.
     */
    PCFGMNODE pCfgNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "GIM/");

    /*
     * Validate the GIM settings.
     */
    rc = CFGMR3ValidateConfig(pCfgNode, "/GIM/", /* pszNode */
                              "Provider"         /* pszValidValues */
                              "|Version",
                              "HyperV",          /* pszValidNodes */
                              "GIM",             /* pszWho */
                              0);                /* uInstance */
    if (RT_FAILURE(rc))
        return rc;

    /** @cfgm{/GIM/Provider, string}
     * The name of the GIM provider. The default is "none". */
    char szProvider[64];
    rc = CFGMR3QueryStringDef(pCfgNode, "Provider", szProvider, sizeof(szProvider), "None");
    AssertLogRelRCReturn(rc, rc);

    /** @cfgm{/GIM/Version, uint32_t}
     * The interface version. The default is 0, which means "provide the most
     * up-to-date implementation". */
    uint32_t uVersion;
    rc = CFGMR3QueryU32Def(pCfgNode, "Version", &uVersion, 0 /* default */);
    AssertLogRelRCReturn(rc, rc);

    /*
     * Setup the GIM provider for this VM.
     */
    LogRel(("GIM: Using provider '%s' (Implementation version: %u)\n", szProvider, uVersion));
    if (!RTStrCmp(szProvider, "None"))
        pVM->gim.s.enmProviderId = GIMPROVIDERID_NONE;
    else
    {
        pVM->gim.s.u32Version = uVersion;
        /** @todo r=bird: Because u32Version is saved, it should be translated to the
         *        'most up-to-date implementation' version number when 0. Otherwise,
         *        we'll have abiguities when loading the state of older VMs. */
#if !defined(VBOX_VMM_TARGET_ARMV8)
        if (!RTStrCmp(szProvider, "Minimal"))
        {
            pVM->gim.s.enmProviderId = GIMPROVIDERID_MINIMAL;
            rc = gimR3MinimalInit(pVM);
        }
        else if (!RTStrCmp(szProvider, "HyperV"))
        {
            pVM->gim.s.enmProviderId = GIMPROVIDERID_HYPERV;
            rc = gimR3HvInit(pVM, pCfgNode);
        }
        else if (!RTStrCmp(szProvider, "KVM"))
        {
            pVM->gim.s.enmProviderId = GIMPROVIDERID_KVM;
            rc = gimR3KvmInit(pVM);
        }
        else
#endif
            rc = VMR3SetError(pVM->pUVM, VERR_GIM_INVALID_PROVIDER, RT_SRC_POS, "Provider '%s' unknown.", szProvider);
    }

    /*
     * Statistics.
     */
    STAM_REL_REG_USED(pVM, &pVM->gim.s.StatDbgXmit,      STAMTYPE_COUNTER, "/GIM/Debug/Transmit",      STAMUNIT_OCCURENCES, "Debug packets sent.");
    STAM_REL_REG_USED(pVM, &pVM->gim.s.StatDbgXmitBytes, STAMTYPE_COUNTER, "/GIM/Debug/TransmitBytes", STAMUNIT_OCCURENCES, "Debug bytes sent.");
    STAM_REL_REG_USED(pVM, &pVM->gim.s.StatDbgRecv,      STAMTYPE_COUNTER, "/GIM/Debug/Receive",       STAMUNIT_OCCURENCES, "Debug packets received.");
    STAM_REL_REG_USED(pVM, &pVM->gim.s.StatDbgRecvBytes, STAMTYPE_COUNTER, "/GIM/Debug/ReceiveBytes",  STAMUNIT_OCCURENCES, "Debug bytes received.");

    STAM_REL_REG_USED(pVM, &pVM->gim.s.StatHypercalls,   STAMTYPE_COUNTER, "/GIM/Hypercalls",          STAMUNIT_OCCURENCES, "Number of hypercalls initiated.");
    return rc;
}


/**
 * Initializes the remaining bits of the GIM provider.
 *
 * This is called after initializing HM and most other VMM components.
 *
 * @returns VBox status code.
 * @param   pVM                 The cross context VM structure.
 * @thread  EMT(0)
 */
VMMR3_INT_DECL(int) GIMR3InitCompleted(PVM pVM)
{
    switch (pVM->gim.s.enmProviderId)
    {
#if !defined(VBOX_VMM_TARGET_ARMV8)
        case GIMPROVIDERID_MINIMAL:
            return gimR3MinimalInitCompleted(pVM);

        case GIMPROVIDERID_HYPERV:
            return gimR3HvInitCompleted(pVM);

        case GIMPROVIDERID_KVM:
            return gimR3KvmInitCompleted(pVM);
#endif
        default:
            break;
    }

    if (!TMR3CpuTickIsFixedRateMonotonic(pVM, true /* fWithParavirtEnabled */))
        LogRel(("GIM: Warning!!! Host TSC is unstable. The guest may behave unpredictably with a paravirtualized clock.\n"));

    return VINF_SUCCESS;
}


/**
 * @callback_method_impl{FNSSMINTSAVEEXEC}
 */
static DECLCALLBACK(int) gimR3Save(PVM pVM, PSSMHANDLE pSSM)
{
    AssertReturn(pVM,  VERR_INVALID_PARAMETER);
    AssertReturn(pSSM, VERR_SSM_INVALID_STATE);

    int rc = VINF_SUCCESS;
#if 0
    /* Save per-CPU data. */
    SSMR3PutU32(pSSM, pVM->cCpus);
    for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
    {
        PVMCPU pVCpu = pVM->apCpusR3[idCpu];
        rc = SSMR3PutXYZ(pSSM, pVCpu->gim.s.XYZ);
    }
#endif

    /*
     * Save per-VM data.
     */
    SSMR3PutU32(pSSM, pVM->gim.s.enmProviderId);
    SSMR3PutU32(pSSM, pVM->gim.s.u32Version);

    /*
     * Save provider-specific data.
     */
    switch (pVM->gim.s.enmProviderId)
    {
#if !defined(VBOX_VMM_TARGET_ARMV8)
        case GIMPROVIDERID_HYPERV:
            rc = gimR3HvSave(pVM, pSSM);
            AssertRCReturn(rc, rc);
            break;

        case GIMPROVIDERID_KVM:
            rc = gimR3KvmSave(pVM, pSSM);
            AssertRCReturn(rc, rc);
            break;
#endif
        default:
            break;
    }

    return rc;
}


/**
 * @callback_method_impl{FNSSMINTLOADEXEC}
 */
static DECLCALLBACK(int) gimR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
{
    if (uPass != SSM_PASS_FINAL)
        return VINF_SUCCESS;
    if (uVersion != GIM_SAVED_STATE_VERSION)
        return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;

    int rc;
#if 0
    /* Load per-CPU data. */
    for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
    {
        PVMCPU pVCpu = pVM->apCpusR3[idCpu];
        rc = SSMR3PutXYZ(pSSM, pVCpu->gim.s.XYZ);
    }
#endif

    /*
     * Load per-VM data.
     */
    uint32_t uProviderId;
    uint32_t uProviderVersion;

    SSMR3GetU32(pSSM, &uProviderId);
    rc = SSMR3GetU32(pSSM, &uProviderVersion);
    AssertRCReturn(rc, rc);

    if ((GIMPROVIDERID)uProviderId != pVM->gim.s.enmProviderId)
        return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Saved GIM provider %u differs from the configured one (%u)."),
                                uProviderId, pVM->gim.s.enmProviderId);
#if 0 /** @todo r=bird: Figure out what you mean to do here with the version. */
    if (uProviderVersion != pVM->gim.s.u32Version)
        return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Saved GIM provider version %u differs from the configured one (%u)."),
                                uProviderVersion, pVM->gim.s.u32Version);
#else
    pVM->gim.s.u32Version = uProviderVersion;
#endif

    /*
     * Load provider-specific data.
     */
    switch (pVM->gim.s.enmProviderId)
    {
#if !defined(VBOX_VMM_TARGET_ARMV8)
        case GIMPROVIDERID_HYPERV:
            rc = gimR3HvLoad(pVM, pSSM);
            AssertRCReturn(rc, rc);
            break;

        case GIMPROVIDERID_KVM:
            rc = gimR3KvmLoad(pVM, pSSM);
            AssertRCReturn(rc, rc);
            break;
#endif
        default:
            break;
    }

    return VINF_SUCCESS;
}


/**
 * @callback_method_impl{FNSSMINTLOADDONE}
 */
static DECLCALLBACK(int) gimR3LoadDone(PVM pVM, PSSMHANDLE pSSM)
{
    switch (pVM->gim.s.enmProviderId)
    {
#if !defined(VBOX_VMM_TARGET_ARMV8)
        case GIMPROVIDERID_HYPERV:
            return gimR3HvLoadDone(pVM, pSSM);
#endif
        default:
            return VINF_SUCCESS;
    }
}


/**
 * Terminates the GIM.
 *
 * Termination means cleaning up and freeing all resources,
 * the VM itself is, at this point, powered off or suspended.
 *
 * @returns VBox status code.
 * @param   pVM         The cross context VM structure.
 */
VMMR3_INT_DECL(int) GIMR3Term(PVM pVM)
{
    switch (pVM->gim.s.enmProviderId)
    {
#if !defined(VBOX_VMM_TARGET_ARMV8)
        case GIMPROVIDERID_HYPERV:
            return gimR3HvTerm(pVM);

        case GIMPROVIDERID_KVM:
            return gimR3KvmTerm(pVM);
#endif
        default:
            break;
    }
    return VINF_SUCCESS;
}


/**
 * Applies relocations to data and code managed by this
 * component. This function will be called at init and
 * whenever the VMM need to relocate it self inside the GC.
 *
 * @param   pVM         The cross context VM structure.
 * @param   offDelta    Relocation delta relative to old location.
 */
VMMR3_INT_DECL(void) GIMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
{
    switch (pVM->gim.s.enmProviderId)
    {
#if !defined(VBOX_VMM_TARGET_ARMV8)
        case GIMPROVIDERID_HYPERV:
            gimR3HvRelocate(pVM, offDelta);
            break;
#endif
        default:
            break;
    }
}


/**
 * The VM is being reset.
 *
 * For the GIM component this means unmapping and unregistering MMIO2 regions
 * and other provider-specific resets.
 *
 * @param   pVM     The cross context VM structure.
 */
VMMR3_INT_DECL(void) GIMR3Reset(PVM pVM)
{
    switch (pVM->gim.s.enmProviderId)
    {
#if !defined(VBOX_VMM_TARGET_ARMV8)
        case GIMPROVIDERID_HYPERV:
            return gimR3HvReset(pVM);

        case GIMPROVIDERID_KVM:
            return gimR3KvmReset(pVM);
#endif
        default:
            break;
    }
}


/**
 * Registers the GIM device with VMM.
 *
 * @param   pVM             The cross context VM structure.
 * @param   pDevIns         Pointer to the GIM device instance.
 * @param   pDbg            Pointer to the GIM device debug structure, can be
 *                          NULL.
 */
VMMR3DECL(void) GIMR3GimDeviceRegister(PVM pVM, PPDMDEVINS pDevIns, PGIMDEBUG pDbg)
{
    pVM->gim.s.pDevInsR3 = pDevIns;
    pVM->gim.s.pDbgR3    = pDbg;
}


/**
 * Gets debug setup specified by the provider.
 *
 * @returns VBox status code.
 * @param   pVM             The cross context VM structure.
 * @param   pDbgSetup       Where to store the debug setup details.
 */
VMMR3DECL(int) GIMR3GetDebugSetup(PVM pVM, PGIMDEBUGSETUP pDbgSetup)
{
    AssertReturn(pVM, VERR_INVALID_PARAMETER);
    AssertReturn(pDbgSetup, VERR_INVALID_PARAMETER);

    switch (pVM->gim.s.enmProviderId)
    {
#if !defined(VBOX_VMM_TARGET_ARMV8)
        case GIMPROVIDERID_HYPERV:
            return gimR3HvGetDebugSetup(pVM, pDbgSetup);
#endif
        default:
            break;
    }
    return VERR_GIM_NO_DEBUG_CONNECTION;
}


/**
 * Read data from a host debug session.
 *
 * @returns VBox status code.
 *
 * @param   pVM                 The cross context VM structure.
 * @param   pvRead              The read buffer.
 * @param   pcbRead             The size of the read buffer as well as where to store
 *                              the number of bytes read.
 * @param   pfnReadComplete     Callback when the buffer has been read and
 *                              before signalling reading of the next buffer.
 *                              Optional, can be NULL.
 * @thread  EMT.
 */
VMMR3_INT_DECL(int) gimR3DebugRead(PVM pVM, void *pvRead, size_t *pcbRead, PFNGIMDEBUGBUFREADCOMPLETED pfnReadComplete)
{
    PGIMDEBUG pDbg = pVM->gim.s.pDbgR3;
    if (pDbg)
    {
        if (ASMAtomicReadBool(&pDbg->fDbgRecvBufRead) == true)
        {
            STAM_REL_COUNTER_INC(&pVM->gim.s.StatDbgRecv);
            STAM_REL_COUNTER_ADD(&pVM->gim.s.StatDbgRecvBytes, pDbg->cbDbgRecvBufRead);

            memcpy(pvRead, pDbg->pvDbgRecvBuf, pDbg->cbDbgRecvBufRead);
            *pcbRead = pDbg->cbDbgRecvBufRead;
            if (pfnReadComplete)
                pfnReadComplete(pVM);
            RTSemEventMultiSignal(pDbg->hDbgRecvThreadSem);
            ASMAtomicWriteBool(&pDbg->fDbgRecvBufRead, false);
            return VINF_SUCCESS;
        }
        else
            *pcbRead = 0;
        return VERR_NO_DATA;
    }
    return VERR_GIM_NO_DEBUG_CONNECTION;
}


/**
 * Write data to a host debug session.
 *
 * @returns VBox status code.
 *
 * @param   pVM         The cross context VM structure.
 * @param   pvWrite     The write buffer.
 * @param   pcbWrite    The size of the write buffer as well as where to store
 *                      the number of bytes written.
 * @thread  EMT.
 */
VMMR3_INT_DECL(int) gimR3DebugWrite(PVM pVM, void *pvWrite, size_t *pcbWrite)
{
    PGIMDEBUG pDbg = pVM->gim.s.pDbgR3;
    if (pDbg)
    {
        PPDMISTREAM pDbgStream = pDbg->pDbgDrvStream;
        if (pDbgStream)
        {
            size_t cbWrite = *pcbWrite;
            int rc = pDbgStream->pfnWrite(pDbgStream, pvWrite, pcbWrite);
            if (   RT_SUCCESS(rc)
                && *pcbWrite == cbWrite)
            {
                STAM_REL_COUNTER_INC(&pVM->gim.s.StatDbgXmit);
                STAM_REL_COUNTER_ADD(&pVM->gim.s.StatDbgXmitBytes, *pcbWrite);
            }
            return rc;
        }
    }
    return VERR_GIM_NO_DEBUG_CONNECTION;
}

#if 0 /* ??? */

/**
 * @callback_method_impl{FNPGMPHYSHANDLER,
 *      Write access handler for mapped MMIO2 pages.  Currently ignores writes.}
 *
 * @todo In the future we might want to let the GIM provider decide what the
 *       handler should do (like throwing \#GP faults).
 */
static DECLCALLBACK(VBOXSTRICTRC) gimR3Mmio2WriteHandler(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf,
                                                         size_t cbBuf, PGMACCESSTYPE enmAccessType, PGMACCESSORIGIN enmOrigin,
                                                         void *pvUser)
{
    RT_NOREF6(pVM, pVCpu, GCPhys, pvPhys, pvBuf, cbBuf);
    RT_NOREF3(enmAccessType, enmOrigin, pvUser);

    /*
     * Ignore writes to the mapped MMIO2 page.
     */
    Assert(enmAccessType == PGMACCESSTYPE_WRITE);
    return VINF_SUCCESS;        /** @todo Hyper-V says we should \#GP(0) fault for writes to the Hypercall and TSC page. */
}


/**
 * Unmaps a registered MMIO2 region in the guest address space and removes any
 * access handlers for it.
 *
 * @returns VBox status code.
 * @param   pVM         The cross context VM structure.
 * @param   pRegion     Pointer to the GIM MMIO2 region.
 */
VMMR3_INT_DECL(int) gimR3Mmio2Unmap(PVM pVM, PGIMMMIO2REGION pRegion)
{
    AssertPtr(pVM);
    AssertPtr(pRegion);

    PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
    AssertPtr(pDevIns);
    if (pRegion->fMapped)
    {
        int rc = PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
        AssertRC(rc);

        rc = PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, pRegion->GCPhysPage);
        if (RT_SUCCESS(rc))
        {
            pRegion->fMapped    = false;
            pRegion->GCPhysPage = NIL_RTGCPHYS;
        }
    }
    return VINF_SUCCESS;
}


/**
 * Maps a registered MMIO2 region in the guest address space.
 *
 * The region will be made read-only and writes from the guest will be ignored.
 *
 * @returns VBox status code.
 * @param   pVM             The cross context VM structure.
 * @param   pRegion         Pointer to the GIM MMIO2 region.
 * @param   GCPhysRegion    Where in the guest address space to map the region.
 */
VMMR3_INT_DECL(int) GIMR3Mmio2Map(PVM pVM, PGIMMMIO2REGION pRegion, RTGCPHYS GCPhysRegion)
{
    PPDMDEVINS pDevIns = pVM->gim.s.pDevInsR3;
    AssertPtr(pDevIns);

    /* The guest-physical address must be page-aligned. */
    if (GCPhysRegion & GUEST_PAGE_OFFSET_MASK)
    {
        LogFunc(("%s: %#RGp not paging aligned\n", pRegion->szDescription, GCPhysRegion));
        return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
    }

    /* Allow only normal pages to be overlaid using our MMIO2 pages (disallow MMIO, ROM, reserved pages). */
    /** @todo Hyper-V doesn't seem to be very strict about this, may be relax
     *        later if some guest really requires it. */
    if (!PGMPhysIsGCPhysNormal(pVM, GCPhysRegion))
    {
        LogFunc(("%s: %#RGp is not normal memory\n", pRegion->szDescription, GCPhysRegion));
        return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
    }

    if (!pRegion->fRegistered)
    {
        LogFunc(("%s: Region has not been registered.\n", pRegion->szDescription));
        return VERR_GIM_IPE_1;
    }

    /*
     * Map the MMIO2 region over the specified guest-physical address.
     */
    int rc = PDMDevHlpMMIOExMap(pDevIns, NULL, pRegion->iRegion, GCPhysRegion);
    if (RT_SUCCESS(rc))
    {
        /*
         * Install access-handlers for the mapped page to prevent (ignore) writes to it
         * from the guest.
         */
        if (pVM->gim.s.hSemiReadOnlyMmio2Handler == NIL_PGMPHYSHANDLERTYPE)
            rc = PGMR3HandlerPhysicalTypeRegister(pVM, PGMPHYSHANDLERKIND_WRITE,
                                                  gimR3Mmio2WriteHandler,
                                                  NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NULL /* pszPfHandlerR0 */,
                                                  NULL /* pszModRC */, NULL /* pszHandlerRC */, NULL /* pszPfHandlerRC */,
                                                  "GIM read-only MMIO2 handler",
                                                  &pVM->gim.s.hSemiReadOnlyMmio2Handler);
        if (RT_SUCCESS(rc))
        {
            rc = PGMHandlerPhysicalRegister(pVM,  GCPhysRegion, GCPhysRegion + (pRegion->cbRegion - 1),
                                            pVM->gim.s.hSemiReadOnlyMmio2Handler,
                                            NULL /* pvUserR3 */, NIL_RTR0PTR /* pvUserR0 */, NIL_RTRCPTR /* pvUserRC */,
                                            pRegion->szDescription);
            if (RT_SUCCESS(rc))
            {
                pRegion->fMapped    = true;
                pRegion->GCPhysPage = GCPhysRegion;
                return rc;
            }
        }

        PDMDevHlpMMIO2Unmap(pDevIns, pRegion->iRegion, GCPhysRegion);
    }

    return rc;
}


/**
 * Registers the physical handler for the registered and mapped MMIO2 region.
 *
 * @returns VBox status code.
 * @param   pVM         The cross context VM structure.
 * @param   pRegion     Pointer to the GIM MMIO2 region.
 */
VMMR3_INT_DECL(int) gimR3Mmio2HandlerPhysicalRegister(PVM pVM, PGIMMMIO2REGION pRegion)
{
    AssertPtr(pRegion);
    AssertReturn(pRegion->fRegistered, VERR_GIM_IPE_2);
    AssertReturn(pRegion->fMapped, VERR_GIM_IPE_3);

    return PGMR3HandlerPhysicalRegister(pVM,
                                        PGMPHYSHANDLERKIND_WRITE,
                                        pRegion->GCPhysPage, pRegion->GCPhysPage + (pRegion->cbRegion - 1),
                                        gimR3Mmio2WriteHandler,  NULL /* pvUserR3 */,
                                        NULL /* pszModR0 */, NULL /* pszHandlerR0 */, NIL_RTR0PTR /* pvUserR0 */,
                                        NULL /* pszModRC */, NULL /* pszHandlerRC */, NIL_RTRCPTR /* pvUserRC */,
                                        pRegion->szDescription);
}


/**
 * Deregisters the physical handler for the MMIO2 region.
 *
 * @returns VBox status code.
 * @param   pVM         The cross context VM structure.
 * @param   pRegion     Pointer to the GIM MMIO2 region.
 */
VMMR3_INT_DECL(int) gimR3Mmio2HandlerPhysicalDeregister(PVM pVM, PGIMMMIO2REGION pRegion)
{
    return PGMHandlerPhysicalDeregister(pVM, pRegion->GCPhysPage);
}

#endif