1 | /* $Id: PGMSharedPage.cpp 30256 2010-06-16 15:14:10Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PGM - Page Manager and Monitor, Shared page handling
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 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/pgm.h>
|
---|
24 | #include <VBox/stam.h>
|
---|
25 | #include "PGMInternal.h"
|
---|
26 | #include "PGMInline.h"
|
---|
27 | #include <VBox/vm.h>
|
---|
28 | #include <VBox/sup.h>
|
---|
29 | #include <VBox/param.h>
|
---|
30 | #include <VBox/err.h>
|
---|
31 | #include <VBox/log.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/mem.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Registers a new shared module for the VM
|
---|
40 | *
|
---|
41 | * @returns VBox status code.
|
---|
42 | * @param pVM VM handle
|
---|
43 | * @param enmGuestOS Guest OS type
|
---|
44 | * @param pszModuleName Module name
|
---|
45 | * @param pszVersion Module version
|
---|
46 | * @param GCBaseAddr Module base address
|
---|
47 | * @param cbModule Module size
|
---|
48 | * @param cRegions Number of shared region descriptors
|
---|
49 | * @param pRegions Shared region(s)
|
---|
50 | */
|
---|
51 | VMMR3DECL(int) PGMR3SharedModuleRegister(PVM pVM, VBOXOSFAMILY enmGuestOS, char *pszModuleName, char *pszVersion, RTGCPTR GCBaseAddr, uint32_t cbModule,
|
---|
52 | unsigned cRegions, VMMDEVSHAREDREGIONDESC *pRegions)
|
---|
53 | {
|
---|
54 | #ifdef VBOX_WITH_PAGE_SHARING
|
---|
55 | PGMMREGISTERSHAREDMODULEREQ pReq;
|
---|
56 |
|
---|
57 | Log(("PGMR3SharedModuleRegister family=%d name=%s version=%s base=%RGv size=%x cRegions=%d\n", enmGuestOS, pszModuleName, pszVersion, GCBaseAddr, cbModule, cRegions));
|
---|
58 |
|
---|
59 | /* Sanity check. */
|
---|
60 | AssertReturn(cRegions < VMMDEVSHAREDREGIONDESC_MAX, VERR_INVALID_PARAMETER);
|
---|
61 |
|
---|
62 | pReq = (PGMMREGISTERSHAREDMODULEREQ)RTMemAllocZ(RT_OFFSETOF(GMMREGISTERSHAREDMODULEREQ, aRegions[cRegions]));
|
---|
63 | AssertReturn(pReq, VERR_NO_MEMORY);
|
---|
64 |
|
---|
65 | pReq->enmGuestOS = enmGuestOS;
|
---|
66 | pReq->GCBaseAddr = GCBaseAddr;
|
---|
67 | pReq->cbModule = cbModule;
|
---|
68 | pReq->cRegions = cRegions;
|
---|
69 | for (unsigned i = 0; i < cRegions; i++)
|
---|
70 | pReq->aRegions[i] = pRegions[i];
|
---|
71 |
|
---|
72 | if ( RTStrCopy(pReq->szName, sizeof(pReq->szName), pszModuleName) != VINF_SUCCESS
|
---|
73 | || RTStrCopy(pReq->szVersion, sizeof(pReq->szVersion), pszVersion) != VINF_SUCCESS)
|
---|
74 | {
|
---|
75 | RTMemFree(pReq);
|
---|
76 | return VERR_BUFFER_OVERFLOW;
|
---|
77 | }
|
---|
78 |
|
---|
79 | int rc = GMMR3RegisterSharedModule(pVM, pReq);
|
---|
80 | RTMemFree(pReq);
|
---|
81 | Assert(rc == VINF_SUCCESS || rc == VINF_PGM_SHARED_MODULE_COLLISION || rc == VINF_PGM_SHARED_MODULE_ALREADY_REGISTERED);
|
---|
82 | if (RT_FAILURE(rc))
|
---|
83 | return rc;
|
---|
84 | return VINF_SUCCESS;
|
---|
85 | #else
|
---|
86 | return VERR_NOT_IMPLEMENTED;
|
---|
87 | #endif
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Unregisters a shared module for the VM
|
---|
92 | *
|
---|
93 | * @returns VBox status code.
|
---|
94 | * @param pVM VM handle
|
---|
95 | * @param pszModuleName Module name
|
---|
96 | * @param pszVersion Module version
|
---|
97 | * @param GCBaseAddr Module base address
|
---|
98 | * @param cbModule Module size
|
---|
99 | */
|
---|
100 | VMMR3DECL(int) PGMR3SharedModuleUnregister(PVM pVM, char *pszModuleName, char *pszVersion, RTGCPTR GCBaseAddr, uint32_t cbModule)
|
---|
101 | {
|
---|
102 | #ifdef VBOX_WITH_PAGE_SHARING
|
---|
103 | PGMMUNREGISTERSHAREDMODULEREQ pReq;
|
---|
104 |
|
---|
105 | Log(("PGMR3SharedModuleUnregister name=%s version=%s base=%RGv size=%x\n", pszModuleName, pszVersion, GCBaseAddr, cbModule));
|
---|
106 |
|
---|
107 | pReq = (PGMMUNREGISTERSHAREDMODULEREQ)RTMemAllocZ(sizeof(*pReq));
|
---|
108 | AssertReturn(pReq, VERR_NO_MEMORY);
|
---|
109 |
|
---|
110 | pReq->GCBaseAddr = GCBaseAddr;
|
---|
111 | pReq->cbModule = cbModule;
|
---|
112 |
|
---|
113 | if ( RTStrCopy(pReq->szName, sizeof(pReq->szName), pszModuleName) != VINF_SUCCESS
|
---|
114 | || RTStrCopy(pReq->szVersion, sizeof(pReq->szVersion), pszVersion) != VINF_SUCCESS)
|
---|
115 | {
|
---|
116 | RTMemFree(pReq);
|
---|
117 | return VERR_BUFFER_OVERFLOW;
|
---|
118 | }
|
---|
119 | int rc = GMMR3UnregisterSharedModule(pVM, pReq);
|
---|
120 | RTMemFree(pReq);
|
---|
121 | return rc;
|
---|
122 | #else
|
---|
123 | return VERR_NOT_IMPLEMENTED;
|
---|
124 | #endif
|
---|
125 | }
|
---|
126 |
|
---|
127 | #ifdef VBOX_WITH_PAGE_SHARING
|
---|
128 | /**
|
---|
129 | * Rendezvous callback that will be called once.
|
---|
130 | *
|
---|
131 | * @returns VBox strict status code.
|
---|
132 | * @param pVM VM handle.
|
---|
133 | * @param pVCpu The VMCPU handle for the calling EMT.
|
---|
134 | * @param pvUser Not used;
|
---|
135 | */
|
---|
136 | static DECLCALLBACK(VBOXSTRICTRC) pgmR3SharedModuleRegRendezvous(PVM pVM, PVMCPU pVCpu, void *pvUser)
|
---|
137 | {
|
---|
138 | VMCPUID idCpu = *(VMCPUID *)pvUser;
|
---|
139 |
|
---|
140 | /* Execute on the VCPU that issued the original request to make sure we're in the right cr3 context. */
|
---|
141 | if (pVCpu->idCpu != idCpu)
|
---|
142 | {
|
---|
143 | Assert(pVM->cCpus > 1);
|
---|
144 | return VINF_SUCCESS;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /* Flush all pending handy page operations before changing any shared page assignments. */
|
---|
148 | int rc = PGMR3PhysAllocateHandyPages(pVM);
|
---|
149 | AssertRC(rc);
|
---|
150 |
|
---|
151 | /* Lock it here as we can't deal with busy locks in this ring-0 path. */
|
---|
152 | pgmLock(pVM);
|
---|
153 | rc = GMMR3CheckSharedModules(pVM);
|
---|
154 | pgmUnlock(pVM);
|
---|
155 |
|
---|
156 | return rc;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Shared module check helper (called on the way out).
|
---|
161 | *
|
---|
162 | * @param pVM The VM handle.
|
---|
163 | * @param VMCPUID VCPU id
|
---|
164 | */
|
---|
165 | static DECLCALLBACK(void) pgmR3CheckSharedModulesHelper(PVM pVM, VMCPUID idCpu)
|
---|
166 | {
|
---|
167 | /* We must stall other VCPUs as we'd otherwise have to send IPI flush commands for every single change we make. */
|
---|
168 | int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE, pgmR3SharedModuleRegRendezvous, &idCpu);
|
---|
169 | AssertRC(rc);
|
---|
170 | }
|
---|
171 | #endif
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Check all registered modules for changes.
|
---|
175 | *
|
---|
176 | * @returns VBox status code.
|
---|
177 | * @param pVM VM handle
|
---|
178 | */
|
---|
179 | VMMR3DECL(int) PGMR3SharedModuleCheckAll(PVM pVM)
|
---|
180 | {
|
---|
181 | #ifdef VBOX_WITH_PAGE_SHARING
|
---|
182 | /* Queue the actual registration as we are under the IOM lock right now. Perform this operation on the way out. */
|
---|
183 | return VMR3ReqCallNoWait(pVM, VMCPUID_ANY_QUEUE, (PFNRT)pgmR3CheckSharedModulesHelper, 2, pVM, VMMGetCpuId(pVM));
|
---|
184 | #else
|
---|
185 | return VERR_NOT_IMPLEMENTED;
|
---|
186 | #endif
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Query the state of a page in a shared module
|
---|
191 | *
|
---|
192 | * @returns VBox status code.
|
---|
193 | * @param pVM VM handle
|
---|
194 | * @param GCPtrPage Page address
|
---|
195 | * @param pfShared Shared status (out)
|
---|
196 | * @param puPageFlags Page flags (out)
|
---|
197 | */
|
---|
198 | VMMR3DECL(int) PGMR3SharedModuleGetPageState(PVM pVM, RTGCPTR GCPtrPage, bool *pfShared, uint64_t *puPageFlags)
|
---|
199 | {
|
---|
200 | #if defined(VBOX_WITH_PAGE_SHARING) && defined(DEBUG)
|
---|
201 | /* Debug only API for the page fusion testcase. */
|
---|
202 | RTGCPHYS GCPhys;
|
---|
203 | uint64_t fFlags;
|
---|
204 |
|
---|
205 | pgmLock(pVM);
|
---|
206 |
|
---|
207 | int rc = PGMGstGetPage(VMMGetCpu(pVM), GCPtrPage, &fFlags, &GCPhys);
|
---|
208 | switch (rc)
|
---|
209 | {
|
---|
210 | case VINF_SUCCESS:
|
---|
211 | {
|
---|
212 | PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
|
---|
213 | if (pPage)
|
---|
214 | {
|
---|
215 | *pfShared = PGM_PAGE_IS_SHARED(pPage);
|
---|
216 | *puPageFlags = fFlags;
|
---|
217 | }
|
---|
218 | else
|
---|
219 | rc = VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
|
---|
220 | break;
|
---|
221 | }
|
---|
222 | case VERR_PAGE_NOT_PRESENT:
|
---|
223 | case VERR_PAGE_TABLE_NOT_PRESENT:
|
---|
224 | case VERR_PAGE_MAP_LEVEL4_NOT_PRESENT:
|
---|
225 | case VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT:
|
---|
226 | *pfShared = false;
|
---|
227 | *puPageFlags = 0;
|
---|
228 | rc = VINF_SUCCESS;
|
---|
229 | break;
|
---|
230 |
|
---|
231 | default:
|
---|
232 | break;
|
---|
233 | }
|
---|
234 |
|
---|
235 | pgmUnlock(pVM);
|
---|
236 | return rc;
|
---|
237 | #else
|
---|
238 | return VERR_NOT_IMPLEMENTED;
|
---|
239 | #endif
|
---|
240 | }
|
---|