[30013] | 1 | /* $Id: PGMR0SharedPage.cpp 100966 2023-08-24 23:23:58Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[31126] | 3 | * PGM - Page Manager and Monitor, Page Sharing, Ring-0.
|
---|
[30013] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
[30013] | 8 | *
|
---|
[96407] | 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
|
---|
[30013] | 26 | */
|
---|
| 27 |
|
---|
[57358] | 28 |
|
---|
| 29 | /*********************************************************************************************************************************
|
---|
| 30 | * Header Files *
|
---|
| 31 | *********************************************************************************************************************************/
|
---|
[30013] | 32 | #define LOG_GROUP LOG_GROUP_PGM_SHARED
|
---|
[86473] | 33 | #define VBOX_WITHOUT_PAGING_BIT_FIELDS /* 64-bit bitfields are just asking for trouble. See @bugref{9841} and others. */
|
---|
[35346] | 34 | #include <VBox/vmm/pgm.h>
|
---|
[94800] | 35 | #include <VBox/vmm/iem.h>
|
---|
[35346] | 36 | #include <VBox/vmm/gmm.h>
|
---|
[35333] | 37 | #include "PGMInternal.h"
|
---|
[80274] | 38 | #include <VBox/vmm/vmcc.h>
|
---|
[78431] | 39 | #include <VBox/vmm/gvm.h>
|
---|
[35333] | 40 | #include "PGMInline.h"
|
---|
[30013] | 41 | #include <VBox/log.h>
|
---|
| 42 | #include <VBox/err.h>
|
---|
| 43 | #include <iprt/assert.h>
|
---|
| 44 | #include <iprt/mem.h>
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 | #ifdef VBOX_WITH_PAGE_SHARING
|
---|
| 48 | /**
|
---|
[40054] | 49 | * Check a registered module for shared page changes.
|
---|
[30013] | 50 | *
|
---|
[40054] | 51 | * The PGM lock shall be taken prior to calling this method.
|
---|
| 52 | *
|
---|
[30013] | 53 | * @returns The following VBox status codes.
|
---|
| 54 | *
|
---|
[58122] | 55 | * @param pVM The cross context VM structure.
|
---|
[40054] | 56 | * @param pGVM Pointer to the GVM instance data.
|
---|
| 57 | * @param idCpu The ID of the calling virtual CPU.
|
---|
[41836] | 58 | * @param pModule Global module description.
|
---|
[40054] | 59 | * @param paRegionsGCPtrs Array parallel to pModules->aRegions with the
|
---|
| 60 | * addresses of the regions in the calling
|
---|
| 61 | * process.
|
---|
[30013] | 62 | */
|
---|
[80274] | 63 | VMMR0DECL(int) PGMR0SharedModuleCheck(PVMCC pVM, PGVM pGVM, VMCPUID idCpu, PGMMSHAREDMODULE pModule, PCRTGCPTR64 paRegionsGCPtrs)
|
---|
[30013] | 64 | {
|
---|
[80334] | 65 | PVMCPUCC pVCpu = &pGVM->aCpus[idCpu];
|
---|
[40054] | 66 | int rc = VINF_SUCCESS;
|
---|
| 67 | bool fFlushTLBs = false;
|
---|
| 68 | bool fFlushRemTLBs = false;
|
---|
| 69 | GMMSHAREDPAGEDESC PageDesc;
|
---|
[30013] | 70 |
|
---|
| 71 | Log(("PGMR0SharedModuleCheck: check %s %s base=%RGv size=%x\n", pModule->szName, pModule->szVersion, pModule->Core.Key, pModule->cbModule));
|
---|
| 72 |
|
---|
[37354] | 73 | PGM_LOCK_ASSERT_OWNER(pVM); /* This cannot fail as we grab the lock in pgmR3SharedModuleRegRendezvous before calling into ring-0. */
|
---|
[30013] | 74 |
|
---|
[40054] | 75 | /*
|
---|
| 76 | * Check every region of the shared module.
|
---|
| 77 | */
|
---|
| 78 | for (uint32_t idxRegion = 0; idxRegion < pModule->cRegions; idxRegion++)
|
---|
[30013] | 79 | {
|
---|
[93554] | 80 | RTGCPTR GCPtrPage = paRegionsGCPtrs[idxRegion] & ~(RTGCPTR)GUEST_PAGE_OFFSET_MASK;
|
---|
| 81 | uint32_t cbLeft = pModule->aRegions[idxRegion].cb; Assert(!(cbLeft & GUEST_PAGE_OFFSET_MASK));
|
---|
[40054] | 82 | uint32_t idxPage = 0;
|
---|
[30013] | 83 |
|
---|
[40054] | 84 | while (cbLeft)
|
---|
[30013] | 85 | {
|
---|
[40054] | 86 | /** @todo inefficient to fetch each guest page like this... */
|
---|
[92426] | 87 | PGMPTWALK Walk;
|
---|
| 88 | rc = PGMGstGetPage(pVCpu, GCPtrPage, &Walk);
|
---|
[30013] | 89 | if ( rc == VINF_SUCCESS
|
---|
[92426] | 90 | && !(Walk.fEffective & X86_PTE_RW)) /* important as we make assumptions about this below! */
|
---|
[30013] | 91 | {
|
---|
[92426] | 92 | PPGMPAGE pPage = pgmPhysGetPage(pVM, Walk.GCPhys);
|
---|
[30202] | 93 | Assert(!pPage || !PGM_PAGE_IS_BALLOONED(pPage));
|
---|
[30013] | 94 | if ( pPage
|
---|
[40054] | 95 | && PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED
|
---|
| 96 | && PGM_PAGE_GET_READ_LOCKS(pPage) == 0
|
---|
| 97 | && PGM_PAGE_GET_WRITE_LOCKS(pPage) == 0 )
|
---|
[30013] | 98 | {
|
---|
[39920] | 99 | PageDesc.idPage = PGM_PAGE_GET_PAGEID(pPage);
|
---|
| 100 | PageDesc.HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
|
---|
[92426] | 101 | PageDesc.GCPhys = Walk.GCPhys;
|
---|
[30013] | 102 |
|
---|
[30783] | 103 | rc = GMMR0SharedModuleCheckPage(pGVM, pModule, idxRegion, idxPage, &PageDesc);
|
---|
[40054] | 104 | if (RT_FAILURE(rc))
|
---|
| 105 | break;
|
---|
| 106 |
|
---|
| 107 | /*
|
---|
| 108 | * Any change for this page?
|
---|
| 109 | */
|
---|
| 110 | if (PageDesc.idPage != NIL_GMM_PAGEID)
|
---|
[30783] | 111 | {
|
---|
[40054] | 112 | Assert(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED);
|
---|
[30013] | 113 |
|
---|
[40054] | 114 | Log(("PGMR0SharedModuleCheck: shared page gst virt=%RGv phys=%RGp host %RHp->%RHp\n",
|
---|
| 115 | GCPtrPage, PageDesc.GCPhys, PGM_PAGE_GET_HCPHYS(pPage), PageDesc.HCPhys));
|
---|
[30013] | 116 |
|
---|
[43042] | 117 | /* Page was either replaced by an existing shared
|
---|
| 118 | version of it or converted into a read-only shared
|
---|
| 119 | page, so, clear all references. */
|
---|
| 120 | bool fFlush = false;
|
---|
| 121 | rc = pgmPoolTrackUpdateGCPhys(pVM, PageDesc.GCPhys, pPage, true /* clear the entries */, &fFlush);
|
---|
| 122 | Assert( rc == VINF_SUCCESS
|
---|
[46420] | 123 | || ( VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3)
|
---|
[43042] | 124 | && (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)));
|
---|
| 125 | if (rc == VINF_SUCCESS)
|
---|
| 126 | fFlushTLBs |= fFlush;
|
---|
| 127 | fFlushRemTLBs = true;
|
---|
| 128 |
|
---|
[40054] | 129 | if (PageDesc.HCPhys != PGM_PAGE_GET_HCPHYS(pPage))
|
---|
| 130 | {
|
---|
| 131 | /* Update the physical address and page id now. */
|
---|
| 132 | PGM_PAGE_SET_HCPHYS(pVM, pPage, PageDesc.HCPhys);
|
---|
| 133 | PGM_PAGE_SET_PAGEID(pVM, pPage, PageDesc.idPage);
|
---|
[30783] | 134 |
|
---|
[40054] | 135 | /* Invalidate page map TLB entry for this page too. */
|
---|
| 136 | pgmPhysInvalidatePageMapTLBEntry(pVM, PageDesc.GCPhys);
|
---|
[100966] | 137 | IEMTlbInvalidateAllPhysicalAllCpus(pVM, NIL_VMCPUID, IEMTLBPHYSFLUSHREASON_SHARED);
|
---|
[40054] | 138 | pVM->pgm.s.cReusedSharedPages++;
|
---|
[30013] | 139 | }
|
---|
[40054] | 140 | /* else: nothing changed (== this page is now a shared
|
---|
| 141 | page), so no need to flush anything. */
|
---|
| 142 |
|
---|
| 143 | pVM->pgm.s.cSharedPages++;
|
---|
| 144 | pVM->pgm.s.cPrivatePages--;
|
---|
| 145 | PGM_PAGE_SET_STATE(pVM, pPage, PGM_PAGE_STATE_SHARED);
|
---|
[43045] | 146 |
|
---|
| 147 | # ifdef VBOX_STRICT /* check sum hack */
|
---|
| 148 | pPage->s.u2Unused0 = PageDesc.u32StrictChecksum & 3;
|
---|
[70977] | 149 | //pPage->s.u2Unused1 = (PageDesc.u32StrictChecksum >> 8) & 3;
|
---|
[43045] | 150 | # endif
|
---|
[30013] | 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
[30793] | 154 | else
|
---|
| 155 | {
|
---|
[30910] | 156 | Assert( rc == VINF_SUCCESS
|
---|
[30793] | 157 | || rc == VERR_PAGE_NOT_PRESENT
|
---|
| 158 | || rc == VERR_PAGE_MAP_LEVEL4_NOT_PRESENT
|
---|
| 159 | || rc == VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT
|
---|
[30910] | 160 | || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
[30793] | 161 | rc = VINF_SUCCESS; /* ignore error */
|
---|
| 162 | }
|
---|
[30783] | 163 |
|
---|
| 164 | idxPage++;
|
---|
[93554] | 165 | GCPtrPage += HOST_PAGE_SIZE;
|
---|
| 166 | cbLeft -= HOST_PAGE_SIZE;
|
---|
[30013] | 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[40054] | 170 | /*
|
---|
| 171 | * Do TLB flushing if necessary.
|
---|
| 172 | */
|
---|
[30013] | 173 | if (fFlushTLBs)
|
---|
| 174 | PGM_INVL_ALL_VCPU_TLBS(pVM);
|
---|
| 175 |
|
---|
[40054] | 176 | if (fFlushRemTLBs)
|
---|
[78431] | 177 | for (VMCPUID idCurCpu = 0; idCurCpu < pGVM->cCpus; idCurCpu++)
|
---|
| 178 | CPUMSetChangedFlags(&pGVM->aCpus[idCurCpu], CPUM_CHANGED_GLOBAL_TLB_FLUSH);
|
---|
[40054] | 179 |
|
---|
[30013] | 180 | return rc;
|
---|
| 181 | }
|
---|
[36983] | 182 | #endif /* VBOX_WITH_PAGE_SHARING */
|
---|
[30013] | 183 |
|
---|