VirtualBox

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

Last change on this file since 29287 was 29203, checked in by vboxsync, 15 years ago

build fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1/* $Id: PGMSharedPage.cpp 29203 2010-05-07 12:40:36Z 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 pszModuleName Module name
128 * @param pszVersion Module version
129 * @param GCBaseAddr Module base address
130 * @param cbModule Module size
131 * @param cRegions Number of shared region descriptors
132 * @param pRegions Shared region(s)
133 */
134VMMR3DECL(int) PGMR3SharedModuleRegister(PVM pVM, char *pszModuleName, char *pszVersion, RTGCPTR GCBaseAddr, uint32_t cbModule,
135 unsigned cRegions, VMMDEVSHAREDREGIONDESC *pRegions)
136{
137#ifdef VBOX_WITH_PAGE_SHARING
138 PGMMREGISTERSHAREDMODULEREQ pReq;
139
140 /* Sanity check. */
141 AssertReturn(cRegions < VMMDEVSHAREDREGIONDESC_MAX, VERR_INVALID_PARAMETER);
142
143 pReq = (PGMMREGISTERSHAREDMODULEREQ)RTMemAllocZ(RT_OFFSETOF(GMMREGISTERSHAREDMODULEREQ, aRegions[cRegions]));
144 AssertReturn(pReq, VERR_NO_MEMORY);
145
146 pReq->GCBaseAddr = GCBaseAddr;
147 pReq->cbModule = cbModule;
148 pReq->cRegions = cRegions;
149 for (unsigned i = 0; i < cRegions; i++)
150 pReq->aRegions[i] = pRegions[i];
151
152 if ( RTStrCopy(pReq->szName, sizeof(pReq->szName), pszModuleName) != VINF_SUCCESS
153 || RTStrCopy(pReq->szVersion, sizeof(pReq->szVersion), pszVersion) != VINF_SUCCESS)
154 {
155 RTMemFree(pReq);
156 return VERR_BUFFER_OVERFLOW;
157 }
158
159 /* Queue the actual registration as we are under the IOM lock right now. Perform this operation on the way out. */
160 return VMR3ReqCallNoWait(pVM, VMMGetCpuId(pVM), (PFNRT)pgmR3SharedModuleRegisterHelper, 2, pVM, pReq);
161#else
162 return VERR_NOT_IMPLEMENTED;
163#endif
164}
165
166
167#ifdef VBOX_WITH_PAGE_SHARING
168/**
169 * Shared module unregistration helper (called on the way out).
170 *
171 * @param pVM The VM handle.
172 * @param pReq Unregistration request info
173 */
174static DECLCALLBACK(void) pgmR3SharedModuleUnregisterHelper(PVM pVM, PGMMREGISTERSHAREDMODULEREQ pReq)
175{
176 int rc;
177
178 rc = GMMR3UnregisterSharedModule(pVM, pReq);
179 RTMemFree(pReq);
180 return;
181}
182#endif
183
184/**
185 * Unregisters a shared module for the VM
186 *
187 * @returns VBox status code.
188 * @param pVM VM handle
189 * @param pszModuleName Module name
190 * @param pszVersion Module version
191 * @param GCBaseAddr Module base address
192 * @param cbModule Module size
193 */
194VMMR3DECL(int) PGMR3SharedModuleUnregister(PVM pVM, char *pszModuleName, char *pszVersion, RTGCPTR GCBaseAddr, uint32_t cbModule)
195{
196#ifdef VBOX_WITH_PAGE_SHARING
197 PGMMUNREGISTERSHAREDMODULEREQ pReq;
198
199 pReq = (PGMMUNREGISTERSHAREDMODULEREQ)RTMemAllocZ(sizeof(*pReq));
200 AssertReturn(pReq, VERR_NO_MEMORY);
201
202 pReq->GCBaseAddr = GCBaseAddr;
203 pReq->cbModule = cbModule;
204
205 if ( RTStrCopy(pReq->szName, sizeof(pReq->szName), pszModuleName) != VINF_SUCCESS
206 || RTStrCopy(pReq->szVersion, sizeof(pReq->szVersion), pszVersion) != VINF_SUCCESS)
207 {
208 return VERR_BUFFER_OVERFLOW;
209 }
210 /* Queue the actual registration as we are under the IOM lock right now. Perform this operation on the way out. */
211 return VMR3ReqCallNoWait(pVM, VMMGetCpuId(pVM), (PFNRT)pgmR3SharedModuleUnregisterHelper, 2, pVM, pReq);
212#else
213 return VERR_NOT_IMPLEMENTED;
214#endif
215}
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