1 | /* $Id: PGMR0SharedPage.cpp 100966 2023-08-24 23:23:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PGM - Page Manager and Monitor, Page Sharing, Ring-0.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-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 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_PGM_SHARED
|
---|
33 | #define VBOX_WITHOUT_PAGING_BIT_FIELDS /* 64-bit bitfields are just asking for trouble. See @bugref{9841} and others. */
|
---|
34 | #include <VBox/vmm/pgm.h>
|
---|
35 | #include <VBox/vmm/iem.h>
|
---|
36 | #include <VBox/vmm/gmm.h>
|
---|
37 | #include "PGMInternal.h"
|
---|
38 | #include <VBox/vmm/vmcc.h>
|
---|
39 | #include <VBox/vmm/gvm.h>
|
---|
40 | #include "PGMInline.h"
|
---|
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 | /**
|
---|
49 | * Check a registered module for shared page changes.
|
---|
50 | *
|
---|
51 | * The PGM lock shall be taken prior to calling this method.
|
---|
52 | *
|
---|
53 | * @returns The following VBox status codes.
|
---|
54 | *
|
---|
55 | * @param pVM The cross context VM structure.
|
---|
56 | * @param pGVM Pointer to the GVM instance data.
|
---|
57 | * @param idCpu The ID of the calling virtual CPU.
|
---|
58 | * @param pModule Global module description.
|
---|
59 | * @param paRegionsGCPtrs Array parallel to pModules->aRegions with the
|
---|
60 | * addresses of the regions in the calling
|
---|
61 | * process.
|
---|
62 | */
|
---|
63 | VMMR0DECL(int) PGMR0SharedModuleCheck(PVMCC pVM, PGVM pGVM, VMCPUID idCpu, PGMMSHAREDMODULE pModule, PCRTGCPTR64 paRegionsGCPtrs)
|
---|
64 | {
|
---|
65 | PVMCPUCC pVCpu = &pGVM->aCpus[idCpu];
|
---|
66 | int rc = VINF_SUCCESS;
|
---|
67 | bool fFlushTLBs = false;
|
---|
68 | bool fFlushRemTLBs = false;
|
---|
69 | GMMSHAREDPAGEDESC PageDesc;
|
---|
70 |
|
---|
71 | Log(("PGMR0SharedModuleCheck: check %s %s base=%RGv size=%x\n", pModule->szName, pModule->szVersion, pModule->Core.Key, pModule->cbModule));
|
---|
72 |
|
---|
73 | PGM_LOCK_ASSERT_OWNER(pVM); /* This cannot fail as we grab the lock in pgmR3SharedModuleRegRendezvous before calling into ring-0. */
|
---|
74 |
|
---|
75 | /*
|
---|
76 | * Check every region of the shared module.
|
---|
77 | */
|
---|
78 | for (uint32_t idxRegion = 0; idxRegion < pModule->cRegions; idxRegion++)
|
---|
79 | {
|
---|
80 | RTGCPTR GCPtrPage = paRegionsGCPtrs[idxRegion] & ~(RTGCPTR)GUEST_PAGE_OFFSET_MASK;
|
---|
81 | uint32_t cbLeft = pModule->aRegions[idxRegion].cb; Assert(!(cbLeft & GUEST_PAGE_OFFSET_MASK));
|
---|
82 | uint32_t idxPage = 0;
|
---|
83 |
|
---|
84 | while (cbLeft)
|
---|
85 | {
|
---|
86 | /** @todo inefficient to fetch each guest page like this... */
|
---|
87 | PGMPTWALK Walk;
|
---|
88 | rc = PGMGstGetPage(pVCpu, GCPtrPage, &Walk);
|
---|
89 | if ( rc == VINF_SUCCESS
|
---|
90 | && !(Walk.fEffective & X86_PTE_RW)) /* important as we make assumptions about this below! */
|
---|
91 | {
|
---|
92 | PPGMPAGE pPage = pgmPhysGetPage(pVM, Walk.GCPhys);
|
---|
93 | Assert(!pPage || !PGM_PAGE_IS_BALLOONED(pPage));
|
---|
94 | if ( pPage
|
---|
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 )
|
---|
98 | {
|
---|
99 | PageDesc.idPage = PGM_PAGE_GET_PAGEID(pPage);
|
---|
100 | PageDesc.HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
|
---|
101 | PageDesc.GCPhys = Walk.GCPhys;
|
---|
102 |
|
---|
103 | rc = GMMR0SharedModuleCheckPage(pGVM, pModule, idxRegion, idxPage, &PageDesc);
|
---|
104 | if (RT_FAILURE(rc))
|
---|
105 | break;
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * Any change for this page?
|
---|
109 | */
|
---|
110 | if (PageDesc.idPage != NIL_GMM_PAGEID)
|
---|
111 | {
|
---|
112 | Assert(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED);
|
---|
113 |
|
---|
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));
|
---|
116 |
|
---|
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
|
---|
123 | || ( VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3)
|
---|
124 | && (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)));
|
---|
125 | if (rc == VINF_SUCCESS)
|
---|
126 | fFlushTLBs |= fFlush;
|
---|
127 | fFlushRemTLBs = true;
|
---|
128 |
|
---|
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);
|
---|
134 |
|
---|
135 | /* Invalidate page map TLB entry for this page too. */
|
---|
136 | pgmPhysInvalidatePageMapTLBEntry(pVM, PageDesc.GCPhys);
|
---|
137 | IEMTlbInvalidateAllPhysicalAllCpus(pVM, NIL_VMCPUID, IEMTLBPHYSFLUSHREASON_SHARED);
|
---|
138 | pVM->pgm.s.cReusedSharedPages++;
|
---|
139 | }
|
---|
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);
|
---|
146 |
|
---|
147 | # ifdef VBOX_STRICT /* check sum hack */
|
---|
148 | pPage->s.u2Unused0 = PageDesc.u32StrictChecksum & 3;
|
---|
149 | //pPage->s.u2Unused1 = (PageDesc.u32StrictChecksum >> 8) & 3;
|
---|
150 | # endif
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | else
|
---|
155 | {
|
---|
156 | Assert( rc == VINF_SUCCESS
|
---|
157 | || rc == VERR_PAGE_NOT_PRESENT
|
---|
158 | || rc == VERR_PAGE_MAP_LEVEL4_NOT_PRESENT
|
---|
159 | || rc == VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT
|
---|
160 | || rc == VERR_PAGE_TABLE_NOT_PRESENT);
|
---|
161 | rc = VINF_SUCCESS; /* ignore error */
|
---|
162 | }
|
---|
163 |
|
---|
164 | idxPage++;
|
---|
165 | GCPtrPage += HOST_PAGE_SIZE;
|
---|
166 | cbLeft -= HOST_PAGE_SIZE;
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | /*
|
---|
171 | * Do TLB flushing if necessary.
|
---|
172 | */
|
---|
173 | if (fFlushTLBs)
|
---|
174 | PGM_INVL_ALL_VCPU_TLBS(pVM);
|
---|
175 |
|
---|
176 | if (fFlushRemTLBs)
|
---|
177 | for (VMCPUID idCurCpu = 0; idCurCpu < pGVM->cCpus; idCurCpu++)
|
---|
178 | CPUMSetChangedFlags(&pGVM->aCpus[idCurCpu], CPUM_CHANGED_GLOBAL_TLB_FLUSH);
|
---|
179 |
|
---|
180 | return rc;
|
---|
181 | }
|
---|
182 | #endif /* VBOX_WITH_PAGE_SHARING */
|
---|
183 |
|
---|