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