1 | /* $Id: PGMSharedPage.cpp 29411 2010-05-12 12:18:51Z 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_PHYS
|
---|
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 | */
|
---|
47 | static 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 | */
|
---|
60 | static 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, &fFlags, &GCPhys);
|
---|
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 = RT_OFFSETOF(GMMREGISTERSHAREDMODULEREQ, aRegions[pReq->cRegions]);
|
---|
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 | */
|
---|
135 | VMMR3DECL(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 | Log(("PGMR3SharedModuleRegister family=%d name=%s version=%s base=%RGv size=%x cRegions=%d\n", enmGuestOS, pszModuleName, pszVersion, GCBaseAddr, cbModule, cRegions));
|
---|
142 |
|
---|
143 | /* Sanity check. */
|
---|
144 | AssertReturn(cRegions < VMMDEVSHAREDREGIONDESC_MAX, VERR_INVALID_PARAMETER);
|
---|
145 |
|
---|
146 | pReq = (PGMMREGISTERSHAREDMODULEREQ)RTMemAllocZ(RT_OFFSETOF(GMMREGISTERSHAREDMODULEREQ, aRegions[cRegions]));
|
---|
147 | AssertReturn(pReq, VERR_NO_MEMORY);
|
---|
148 |
|
---|
149 | pReq->enmGuestOS = enmGuestOS;
|
---|
150 | pReq->GCBaseAddr = GCBaseAddr;
|
---|
151 | pReq->cbModule = cbModule;
|
---|
152 | pReq->cRegions = cRegions;
|
---|
153 | for (unsigned i = 0; i < cRegions; i++)
|
---|
154 | pReq->aRegions[i] = pRegions[i];
|
---|
155 |
|
---|
156 | if ( RTStrCopy(pReq->szName, sizeof(pReq->szName), pszModuleName) != VINF_SUCCESS
|
---|
157 | || RTStrCopy(pReq->szVersion, sizeof(pReq->szVersion), pszVersion) != VINF_SUCCESS)
|
---|
158 | {
|
---|
159 | RTMemFree(pReq);
|
---|
160 | return VERR_BUFFER_OVERFLOW;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /* Queue the actual registration as we are under the IOM lock right now. Perform this operation on the way out. */
|
---|
164 | return VMR3ReqCallNoWait(pVM, VMMGetCpuId(pVM), (PFNRT)pgmR3SharedModuleRegisterHelper, 2, pVM, pReq);
|
---|
165 | #else
|
---|
166 | return VERR_NOT_IMPLEMENTED;
|
---|
167 | #endif
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | #ifdef VBOX_WITH_PAGE_SHARING
|
---|
172 | /**
|
---|
173 | * Shared module unregistration helper (called on the way out).
|
---|
174 | *
|
---|
175 | * @param pVM The VM handle.
|
---|
176 | * @param pReq Unregistration request info
|
---|
177 | */
|
---|
178 | static DECLCALLBACK(void) pgmR3SharedModuleUnregisterHelper(PVM pVM, PGMMREGISTERSHAREDMODULEREQ pReq)
|
---|
179 | {
|
---|
180 | int rc;
|
---|
181 |
|
---|
182 | rc = GMMR3UnregisterSharedModule(pVM, pReq);
|
---|
183 | RTMemFree(pReq);
|
---|
184 | return;
|
---|
185 | }
|
---|
186 | #endif
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Unregisters a shared module for the VM
|
---|
190 | *
|
---|
191 | * @returns VBox status code.
|
---|
192 | * @param pVM VM handle
|
---|
193 | * @param pszModuleName Module name
|
---|
194 | * @param pszVersion Module version
|
---|
195 | * @param GCBaseAddr Module base address
|
---|
196 | * @param cbModule Module size
|
---|
197 | */
|
---|
198 | VMMR3DECL(int) PGMR3SharedModuleUnregister(PVM pVM, char *pszModuleName, char *pszVersion, RTGCPTR GCBaseAddr, uint32_t cbModule)
|
---|
199 | {
|
---|
200 | #ifdef VBOX_WITH_PAGE_SHARING
|
---|
201 | PGMMUNREGISTERSHAREDMODULEREQ pReq;
|
---|
202 |
|
---|
203 | Log(("PGMR3SharedModuleUnregister name=%s version=%s base=%RGv size=%x\n", pszModuleName, pszVersion, GCBaseAddr, cbModule));
|
---|
204 |
|
---|
205 | pReq = (PGMMUNREGISTERSHAREDMODULEREQ)RTMemAllocZ(sizeof(*pReq));
|
---|
206 | AssertReturn(pReq, VERR_NO_MEMORY);
|
---|
207 |
|
---|
208 | pReq->GCBaseAddr = GCBaseAddr;
|
---|
209 | pReq->cbModule = cbModule;
|
---|
210 |
|
---|
211 | if ( RTStrCopy(pReq->szName, sizeof(pReq->szName), pszModuleName) != VINF_SUCCESS
|
---|
212 | || RTStrCopy(pReq->szVersion, sizeof(pReq->szVersion), pszVersion) != VINF_SUCCESS)
|
---|
213 | {
|
---|
214 | return VERR_BUFFER_OVERFLOW;
|
---|
215 | }
|
---|
216 | /* Queue the actual registration as we are under the IOM lock right now. Perform this operation on the way out. */
|
---|
217 | return VMR3ReqCallNoWait(pVM, VMMGetCpuId(pVM), (PFNRT)pgmR3SharedModuleUnregisterHelper, 2, pVM, pReq);
|
---|
218 | #else
|
---|
219 | return VERR_NOT_IMPLEMENTED;
|
---|
220 | #endif
|
---|
221 | }
|
---|