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