VirtualBox

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

Last change on this file since 29319 was 29308, checked in by vboxsync, 15 years ago

Shared paging updates

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1/* $Id: PGMSharedPage.cpp 29308 2010-05-10 15:23:09Z 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
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#ifdef VBOX_WITH_PAGE_SHARING
39/**
40 * Rendezvous callback that will be called once.
41 *
42 * @returns VBox strict status code.
43 * @param pVM VM handle.
44 * @param pVCpu The VMCPU handle for the calling EMT.
45 * @param pvUser PGMMREGISTERSHAREDMODULEREQ
46 */
47static DECLCALLBACK(VBOXSTRICTRC) pgmR3SharedModuleRegRendezvous(PVM pVM, PVMCPU pVCpu, void *pvUser)
48{
49 PGMMREGISTERSHAREDMODULEREQ pReq = (PGMMREGISTERSHAREDMODULEREQ)pvUser;
50
51 return VMMR3CallR0(pVM, VMMR0_DO_PGM_CHECK_SHARED_MODULE, 0, &pReq->Hdr);
52}
53
54/**
55 * Shared module registration helper (called on the way out).
56 *
57 * @param pVM The VM handle.
58 * @param pReq Registration request info
59 */
60static DECLCALLBACK(void) pgmR3SharedModuleRegisterHelper(PVM pVM, PGMMREGISTERSHAREDMODULEREQ pReq)
61{
62 int rc;
63
64 rc = GMMR3RegisterSharedModule(pVM, pReq);
65 Assert(rc == VINF_SUCCESS || rc == VINF_PGM_SHARED_MODULE_COLLISION || rc == VINF_PGM_SHARED_MODULE_ALREADY_REGISTERED);
66 if (rc == VINF_PGM_SHARED_MODULE_ALREADY_REGISTERED)
67 {
68 PVMCPU pVCpu = VMMGetCpu(pVM);
69 unsigned cFlushedPages = 0;
70
71 /** todo count copy-on-write actions in the trap handler so we don't have to check everything all the time! */
72
73 /* Count the number of shared pages that were changed (copy-on-write). */
74 for (unsigned i = 0; i < pReq->cRegions; i++)
75 {
76 Assert((pReq->aRegions[i].cbRegion & 0xfff) == 0);
77 Assert((pReq->aRegions[i].GCRegionAddr & 0xfff) == 0);
78
79 RTGCPTR GCRegion = pReq->aRegions[i].GCRegionAddr;
80 uint32_t cbRegion = pReq->aRegions[i].cbRegion & ~0xfff;
81
82 while (cbRegion)
83 {
84 RTGCPHYS GCPhys;
85 uint64_t fFlags;
86
87 rc = PGMGstGetPage(pVCpu, GCRegion, &GCPhys, &fFlags);
88 if ( rc == VINF_SUCCESS
89 && !(fFlags & X86_PTE_RW))
90 {
91 PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
92 if ( pPage
93 && !PGM_PAGE_IS_SHARED(pPage))
94 {
95 cFlushedPages++;
96 }
97 }
98
99 GCRegion += PAGE_SIZE;
100 cbRegion -= PAGE_SIZE;
101 }
102 }
103
104 if (cFlushedPages > 32)
105 rc = VINF_SUCCESS; /* force recheck below */
106 }
107 /* Full (re)check needed? */
108 if (rc == VINF_SUCCESS)
109 {
110 pReq->Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
111 pReq->Hdr.cbReq = sizeof(*pReq);
112
113 /* We must stall other VCPUs as we'd otherwise have to send IPI flush commands for every single change we make. */
114 rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, pgmR3SharedModuleRegRendezvous, pReq);
115 AssertRC(rc);
116 }
117 RTMemFree(pReq);
118 return;
119}
120#endif
121
122/**
123 * Registers a new shared module for the VM
124 *
125 * @returns VBox status code.
126 * @param pVM VM handle
127 * @param enmGuestOS Guest OS type
128 * @param pszModuleName Module name
129 * @param pszVersion Module version
130 * @param GCBaseAddr Module base address
131 * @param cbModule Module size
132 * @param cRegions Number of shared region descriptors
133 * @param pRegions Shared region(s)
134 */
135VMMR3DECL(int) PGMR3SharedModuleRegister(PVM pVM, VBOXOSFAMILY enmGuestOS, char *pszModuleName, char *pszVersion, RTGCPTR GCBaseAddr, uint32_t cbModule,
136 unsigned cRegions, VMMDEVSHAREDREGIONDESC *pRegions)
137{
138#ifdef VBOX_WITH_PAGE_SHARING
139 PGMMREGISTERSHAREDMODULEREQ pReq;
140
141 /* Sanity check. */
142 AssertReturn(cRegions < VMMDEVSHAREDREGIONDESC_MAX, VERR_INVALID_PARAMETER);
143
144 pReq = (PGMMREGISTERSHAREDMODULEREQ)RTMemAllocZ(RT_OFFSETOF(GMMREGISTERSHAREDMODULEREQ, aRegions[cRegions]));
145 AssertReturn(pReq, VERR_NO_MEMORY);
146
147 pReq->enmGuestOS = enmGuestOS;
148 pReq->GCBaseAddr = GCBaseAddr;
149 pReq->cbModule = cbModule;
150 pReq->cRegions = cRegions;
151 for (unsigned i = 0; i < cRegions; i++)
152 pReq->aRegions[i] = pRegions[i];
153
154 if ( RTStrCopy(pReq->szName, sizeof(pReq->szName), pszModuleName) != VINF_SUCCESS
155 || RTStrCopy(pReq->szVersion, sizeof(pReq->szVersion), pszVersion) != VINF_SUCCESS)
156 {
157 RTMemFree(pReq);
158 return VERR_BUFFER_OVERFLOW;
159 }
160
161 /* Queue the actual registration as we are under the IOM lock right now. Perform this operation on the way out. */
162 return VMR3ReqCallNoWait(pVM, VMMGetCpuId(pVM), (PFNRT)pgmR3SharedModuleRegisterHelper, 2, pVM, pReq);
163#else
164 return VERR_NOT_IMPLEMENTED;
165#endif
166}
167
168
169#ifdef VBOX_WITH_PAGE_SHARING
170/**
171 * Shared module unregistration helper (called on the way out).
172 *
173 * @param pVM The VM handle.
174 * @param pReq Unregistration request info
175 */
176static DECLCALLBACK(void) pgmR3SharedModuleUnregisterHelper(PVM pVM, PGMMREGISTERSHAREDMODULEREQ pReq)
177{
178 int rc;
179
180 rc = GMMR3UnregisterSharedModule(pVM, pReq);
181 RTMemFree(pReq);
182 return;
183}
184#endif
185
186/**
187 * Unregisters a shared module for the VM
188 *
189 * @returns VBox status code.
190 * @param pVM VM handle
191 * @param pszModuleName Module name
192 * @param pszVersion Module version
193 * @param GCBaseAddr Module base address
194 * @param cbModule Module size
195 */
196VMMR3DECL(int) PGMR3SharedModuleUnregister(PVM pVM, char *pszModuleName, char *pszVersion, RTGCPTR GCBaseAddr, uint32_t cbModule)
197{
198#ifdef VBOX_WITH_PAGE_SHARING
199 PGMMUNREGISTERSHAREDMODULEREQ pReq;
200
201 pReq = (PGMMUNREGISTERSHAREDMODULEREQ)RTMemAllocZ(sizeof(*pReq));
202 AssertReturn(pReq, VERR_NO_MEMORY);
203
204 pReq->GCBaseAddr = GCBaseAddr;
205 pReq->cbModule = cbModule;
206
207 if ( RTStrCopy(pReq->szName, sizeof(pReq->szName), pszModuleName) != VINF_SUCCESS
208 || RTStrCopy(pReq->szVersion, sizeof(pReq->szVersion), pszVersion) != VINF_SUCCESS)
209 {
210 return VERR_BUFFER_OVERFLOW;
211 }
212 /* Queue the actual registration as we are under the IOM lock right now. Perform this operation on the way out. */
213 return VMR3ReqCallNoWait(pVM, VMMGetCpuId(pVM), (PFNRT)pgmR3SharedModuleUnregisterHelper, 2, pVM, pReq);
214#else
215 return VERR_NOT_IMPLEMENTED;
216#endif
217}
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