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