VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PGMSharedPage.cpp@ 44410

Last change on this file since 44410 was 44410, checked in by vboxsync, 12 years ago

VMM: dbg build fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.3 KB
Line 
1/* $Id: PGMSharedPage.cpp 44410 2013-01-28 09:58:16Z vboxsync $ */
2/** @file
3 * PGM - Page Manager and Monitor, Shared page handling
4 */
5
6/*
7 * Copyright (C) 2006-2011 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/stam.h>
25#include <VBox/vmm/uvm.h>
26#include "PGMInternal.h"
27#include <VBox/vmm/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/asm.h>
33#include <iprt/assert.h>
34#include <iprt/mem.h>
35#include <iprt/string.h>
36
37#include "PGMInline.h"
38
39
40#ifdef VBOX_WITH_PAGE_SHARING
41
42/*******************************************************************************
43* Global Variables *
44*******************************************************************************/
45# ifdef VBOX_STRICT
46/** Keep a copy of all registered shared modules for the .pgmcheckduppages debugger command. */
47static PGMMREGISTERSHAREDMODULEREQ g_apSharedModules[512] = {0};
48static unsigned g_cSharedModules = 0;
49# endif /* VBOX_STRICT */
50
51
52/**
53 * Registers a new shared module for the VM
54 *
55 * @returns VBox status code.
56 * @param pVM Pointer to the VM.
57 * @param enmGuestOS Guest OS type.
58 * @param pszModuleName Module name.
59 * @param pszVersion Module version.
60 * @param GCBaseAddr Module base address.
61 * @param cbModule Module size.
62 * @param cRegions Number of shared region descriptors.
63 * @param paRegions Shared region(s).
64 *
65 * @todo This should be a GMMR3 call. No need to involve GMM here.
66 */
67VMMR3DECL(int) PGMR3SharedModuleRegister(PVM pVM, VBOXOSFAMILY enmGuestOS, char *pszModuleName, char *pszVersion,
68 RTGCPTR GCBaseAddr, uint32_t cbModule, uint32_t cRegions,
69 VMMDEVSHAREDREGIONDESC const *paRegions)
70{
71 Log(("PGMR3SharedModuleRegister family=%d name=%s version=%s base=%RGv size=%x cRegions=%d\n",
72 enmGuestOS, pszModuleName, pszVersion, GCBaseAddr, cbModule, cRegions));
73
74 /*
75 * Sanity check.
76 */
77 AssertReturn(cRegions <= VMMDEVSHAREDREGIONDESC_MAX, VERR_INVALID_PARAMETER);
78
79 /*
80 * Allocate and initialize a GMM request.
81 */
82 PGMMREGISTERSHAREDMODULEREQ pReq;
83 pReq = (PGMMREGISTERSHAREDMODULEREQ)RTMemAllocZ(RT_OFFSETOF(GMMREGISTERSHAREDMODULEREQ, aRegions[cRegions]));
84 AssertReturn(pReq, VERR_NO_MEMORY);
85
86 pReq->enmGuestOS = enmGuestOS;
87 pReq->GCBaseAddr = GCBaseAddr;
88 pReq->cbModule = cbModule;
89 pReq->cRegions = cRegions;
90 for (uint32_t i = 0; i < cRegions; i++)
91 pReq->aRegions[i] = paRegions[i];
92
93 int rc = RTStrCopy(pReq->szName, sizeof(pReq->szName), pszModuleName);
94 if (RT_SUCCESS(rc))
95 {
96 rc = RTStrCopy(pReq->szVersion, sizeof(pReq->szVersion), pszVersion);
97 if (RT_SUCCESS(rc))
98 {
99 /*
100 * Issue the request. In strict builds, do some local tracking.
101 */
102 pgmR3PhysAssertSharedPageChecksums(pVM);
103 rc = GMMR3RegisterSharedModule(pVM, pReq);
104 if (RT_SUCCESS(rc))
105 rc = pReq->rc;
106 AssertMsg(rc == VINF_SUCCESS || rc == VINF_GMM_SHARED_MODULE_ALREADY_REGISTERED, ("%Rrc\n", rc));
107
108# ifdef VBOX_STRICT
109 if ( rc == VINF_SUCCESS
110 && g_cSharedModules < RT_ELEMENTS(g_apSharedModules))
111 {
112 unsigned i;
113 for (i = 0; i < RT_ELEMENTS(g_apSharedModules); i++)
114 if (g_apSharedModules[i] == NULL)
115 {
116
117 size_t const cbSharedModule = RT_OFFSETOF(GMMREGISTERSHAREDMODULEREQ, aRegions[cRegions]);
118 g_apSharedModules[i] = (PGMMREGISTERSHAREDMODULEREQ)RTMemDup(pReq, cbSharedModule);
119 g_cSharedModules++;
120 break;
121 }
122 Assert(i < RT_ELEMENTS(g_apSharedModules));
123 }
124# endif /* VBOX_STRICT */
125 if (RT_SUCCESS(rc))
126 rc = VINF_SUCCESS;
127 }
128 }
129
130 RTMemFree(pReq);
131 return rc;
132}
133
134
135/**
136 * Unregisters a shared module for the VM
137 *
138 * @returns VBox status code.
139 * @param pVM Pointer to the VM.
140 * @param pszModuleName Module name.
141 * @param pszVersion Module version.
142 * @param GCBaseAddr Module base address.
143 * @param cbModule Module size.
144 *
145 * @todo This should be a GMMR3 call. No need to involve GMM here.
146 */
147VMMR3DECL(int) PGMR3SharedModuleUnregister(PVM pVM, char *pszModuleName, char *pszVersion, RTGCPTR GCBaseAddr, uint32_t cbModule)
148{
149 Log(("PGMR3SharedModuleUnregister name=%s version=%s base=%RGv size=%x\n", pszModuleName, pszVersion, GCBaseAddr, cbModule));
150 AssertMsgReturn(cbModule > 0 && cbModule < _1G, ("%u\n", cbModule), VERR_OUT_OF_RANGE);
151
152
153 /*
154 * Forward the request to GMM (ring-0).
155 */
156 PGMMUNREGISTERSHAREDMODULEREQ pReq = (PGMMUNREGISTERSHAREDMODULEREQ)RTMemAlloc(sizeof(*pReq));
157 AssertReturn(pReq, VERR_NO_MEMORY);
158
159 pReq->GCBaseAddr = GCBaseAddr;
160 pReq->u32Alignment = 0;
161 pReq->cbModule = cbModule;
162
163 int rc = RTStrCopy(pReq->szName, sizeof(pReq->szName), pszModuleName);
164 if (RT_SUCCESS(rc))
165 {
166 rc = RTStrCopy(pReq->szVersion, sizeof(pReq->szVersion), pszVersion);
167 if (RT_SUCCESS(rc))
168 {
169 pgmR3PhysAssertSharedPageChecksums(pVM);
170 rc = GMMR3UnregisterSharedModule(pVM, pReq);
171 pgmR3PhysAssertSharedPageChecksums(pVM);
172
173# ifdef VBOX_STRICT
174 /*
175 * Update our local tracking.
176 */
177 for (unsigned i = 0; i < g_cSharedModules; i++)
178 {
179 if ( g_apSharedModules[i]
180 && !strcmp(g_apSharedModules[i]->szName, pszModuleName)
181 && !strcmp(g_apSharedModules[i]->szVersion, pszVersion))
182 {
183 RTMemFree(g_apSharedModules[i]);
184 g_apSharedModules[i] = NULL;
185 g_cSharedModules--;
186 break;
187 }
188 }
189# endif /* VBOX_STRICT */
190 }
191 }
192
193 RTMemFree(pReq);
194 return rc;
195}
196
197
198/**
199 * Rendezvous callback that will be called once.
200 *
201 * @returns VBox strict status code.
202 * @param pVM Pointer to the VM.
203 * @param pVCpu Pointer to the VMCPU of the calling EMT.
204 * @param pvUser Pointer to a VMCPUID with the requester's ID.
205 */
206static DECLCALLBACK(VBOXSTRICTRC) pgmR3SharedModuleRegRendezvous(PVM pVM, PVMCPU pVCpu, void *pvUser)
207{
208 VMCPUID idCpu = *(VMCPUID *)pvUser;
209
210 /* Execute on the VCPU that issued the original request to make sure we're in the right cr3 context. */
211 if (pVCpu->idCpu != idCpu)
212 {
213 Assert(pVM->cCpus > 1);
214 return VINF_SUCCESS;
215 }
216
217
218 /* Flush all pending handy page operations before changing any shared page assignments. */
219 int rc = PGMR3PhysAllocateHandyPages(pVM);
220 AssertRC(rc);
221
222 /*
223 * Lock it here as we can't deal with busy locks in this ring-0 path.
224 */
225 LogFlow(("pgmR3SharedModuleRegRendezvous: start (%d)\n", pVM->pgm.s.cSharedPages));
226
227 pgmLock(pVM);
228 pgmR3PhysAssertSharedPageChecksums(pVM);
229 rc = GMMR3CheckSharedModules(pVM);
230 pgmR3PhysAssertSharedPageChecksums(pVM);
231 pgmUnlock(pVM);
232 AssertLogRelRC(rc);
233
234 LogFlow(("pgmR3SharedModuleRegRendezvous: done (%d)\n", pVM->pgm.s.cSharedPages));
235 return rc;
236}
237
238/**
239 * Shared module check helper (called on the way out).
240 *
241 * @param pVM Pointer to the VM.
242 * @param VMCPUID VCPU id
243 */
244static DECLCALLBACK(void) pgmR3CheckSharedModulesHelper(PVM pVM, VMCPUID idCpu)
245{
246 /* We must stall other VCPUs as we'd otherwise have to send IPI flush commands for every single change we make. */
247 STAM_REL_PROFILE_START(&pVM->pgm.s.StatShModCheck, a);
248 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ALL_AT_ONCE, pgmR3SharedModuleRegRendezvous, &idCpu);
249 AssertRCSuccess(rc);
250 STAM_REL_PROFILE_STOP(&pVM->pgm.s.StatShModCheck, a);
251}
252
253
254/**
255 * Check all registered modules for changes.
256 *
257 * @returns VBox status code.
258 * @param pVM Pointer to the VM
259 */
260VMMR3DECL(int) PGMR3SharedModuleCheckAll(PVM pVM)
261{
262 /* Queue the actual registration as we are under the IOM lock right now. Perform this operation on the way out. */
263 return VMR3ReqCallNoWait(pVM, VMCPUID_ANY_QUEUE, (PFNRT)pgmR3CheckSharedModulesHelper, 2, pVM, VMMGetCpuId(pVM));
264}
265
266
267# ifdef DEBUG
268/**
269 * Query the state of a page in a shared module
270 *
271 * @returns VBox status code.
272 * @param pVM Pointer to the VM.
273 * @param GCPtrPage Page address.
274 * @param pfShared Shared status (out).
275 * @param pfPageFlags Page flags (out).
276 */
277VMMR3DECL(int) PGMR3SharedModuleGetPageState(PVM pVM, RTGCPTR GCPtrPage, bool *pfShared, uint64_t *pfPageFlags)
278{
279 /* Debug only API for the page fusion testcase. */
280 RTGCPHYS GCPhys;
281 uint64_t fFlags;
282
283 pgmLock(pVM);
284
285 int rc = PGMGstGetPage(VMMGetCpu(pVM), GCPtrPage, &fFlags, &GCPhys);
286 switch (rc)
287 {
288 case VINF_SUCCESS:
289 {
290 PPGMPAGE pPage = pgmPhysGetPage(pVM, GCPhys);
291 if (pPage)
292 {
293 *pfShared = PGM_PAGE_IS_SHARED(pPage);
294 *pfPageFlags = fFlags;
295 }
296 else
297 rc = VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
298 break;
299 }
300
301 case VERR_PAGE_NOT_PRESENT:
302 case VERR_PAGE_TABLE_NOT_PRESENT:
303 case VERR_PAGE_MAP_LEVEL4_NOT_PRESENT:
304 case VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT:
305 *pfShared = false;
306 *pfPageFlags = 0;
307 rc = VINF_SUCCESS;
308 break;
309
310 default:
311 break;
312 }
313
314 pgmUnlock(pVM);
315 return rc;
316}
317# endif /* DEBUG */
318
319# ifdef VBOX_STRICT
320
321/**
322 * The '.pgmcheckduppages' command.
323 *
324 * @returns VBox status.
325 * @param pCmd Pointer to the command descriptor (as registered).
326 * @param pCmdHlp Pointer to command helper functions.
327 * @param pVM Pointer to the current VM (if any).
328 * @param paArgs Pointer to (readonly) array of arguments.
329 * @param cArgs Number of arguments in the array.
330 */
331DECLCALLBACK(int) pgmR3CmdCheckDuplicatePages(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
332{
333 unsigned cBallooned = 0;
334 unsigned cShared = 0;
335 unsigned cZero = 0;
336 unsigned cUnique = 0;
337 unsigned cDuplicate = 0;
338 unsigned cAllocZero = 0;
339 unsigned cPages = 0;
340 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs);
341 PVM pVM = pUVM->pVM;
342
343 pgmLock(pVM);
344
345 for (PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesXR3; pRam; pRam = pRam->pNextR3)
346 {
347 PPGMPAGE pPage = &pRam->aPages[0];
348 RTGCPHYS GCPhys = pRam->GCPhys;
349 uint32_t cLeft = pRam->cb >> PAGE_SHIFT;
350 while (cLeft-- > 0)
351 {
352 if (PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM)
353 {
354 switch (PGM_PAGE_GET_STATE(pPage))
355 {
356 case PGM_PAGE_STATE_ZERO:
357 cZero++;
358 break;
359
360 case PGM_PAGE_STATE_BALLOONED:
361 cBallooned++;
362 break;
363
364 case PGM_PAGE_STATE_SHARED:
365 cShared++;
366 break;
367
368 case PGM_PAGE_STATE_ALLOCATED:
369 case PGM_PAGE_STATE_WRITE_MONITORED:
370 {
371 /* Check if the page was allocated, but completely zero. */
372 PGMPAGEMAPLOCK PgMpLck;
373 const void *pvPage;
374 int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, GCPhys, &pvPage, &PgMpLck);
375 if ( RT_SUCCESS(rc)
376 && ASMMemIsZeroPage(pvPage))
377 cAllocZero++;
378 else if (GMMR3IsDuplicatePage(pVM, PGM_PAGE_GET_PAGEID(pPage)))
379 cDuplicate++;
380 else
381 cUnique++;
382 if (RT_SUCCESS(rc))
383 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
384 break;
385 }
386
387 default:
388 AssertFailed();
389 break;
390 }
391 }
392
393 /* next */
394 pPage++;
395 GCPhys += PAGE_SIZE;
396 cPages++;
397 /* Give some feedback for every processed megabyte. */
398 if ((cPages & 0x7f) == 0)
399 pCmdHlp->pfnPrintf(pCmdHlp, NULL, ".");
400 }
401 }
402 pgmUnlock(pVM);
403
404 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "\nNumber of zero pages %08x (%d MB)\n", cZero, cZero / 256);
405 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Number of alloczero pages %08x (%d MB)\n", cAllocZero, cAllocZero / 256);
406 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Number of ballooned pages %08x (%d MB)\n", cBallooned, cBallooned / 256);
407 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Number of shared pages %08x (%d MB)\n", cShared, cShared / 256);
408 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Number of unique pages %08x (%d MB)\n", cUnique, cUnique / 256);
409 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Number of duplicate pages %08x (%d MB)\n", cDuplicate, cDuplicate / 256);
410 return VINF_SUCCESS;
411}
412
413
414/**
415 * The '.pgmsharedmodules' command.
416 *
417 * @returns VBox status.
418 * @param pCmd Pointer to the command descriptor (as registered).
419 * @param pCmdHlp Pointer to command helper functions.
420 * @param pVM Pointer to the current VM (if any).
421 * @param paArgs Pointer to (readonly) array of arguments.
422 * @param cArgs Number of arguments in the array.
423 */
424DECLCALLBACK(int) pgmR3CmdShowSharedModules(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
425{
426 NOREF(pCmd); NOREF(paArgs); NOREF(cArgs);
427 PVM pVM = pUVM->pVM;
428
429 pgmLock(pVM);
430 for (unsigned i = 0; i < RT_ELEMENTS(g_apSharedModules); i++)
431 {
432 if (g_apSharedModules[i])
433 {
434 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Shared module %s (%s):\n", g_apSharedModules[i]->szName, g_apSharedModules[i]->szVersion);
435 for (unsigned j = 0; j < g_apSharedModules[i]->cRegions; j++)
436 pCmdHlp->pfnPrintf(pCmdHlp, NULL, "--- Region %d: base %RGv size %x\n", j, g_apSharedModules[i]->aRegions[j].GCRegionAddr, g_apSharedModules[i]->aRegions[j].cbRegion);
437 }
438 }
439 pgmUnlock(pVM);
440
441 return VINF_SUCCESS;
442}
443
444# endif /* VBOX_STRICT*/
445#endif /* VBOX_WITH_PAGE_SHARING */
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