VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/PGMR0SharedPage.cpp@ 80274

Last change on this file since 80274 was 80274, checked in by vboxsync, 5 years ago

VMM: Refactoring VMMR0/* and VMMRZ/* to use VMCC & VMMCPUCC. bugref:9217

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette