1 | /* $Id: PGMSharedPage.cpp 30041 2010-06-04 16:24:14Z 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 | /* Flush all pending handy page operations before changing any shared page assignments. */
|
---|
139 | int rc = PGMR3PhysAllocateHandyPages(pVM);
|
---|
140 | AssertRC(rc);
|
---|
141 |
|
---|
142 | /* Lock it here as we can't deal with busy locks in this ring-0 path. */
|
---|
143 | pgmLock(pVM);
|
---|
144 | rc = GMMR3CheckSharedModules(pVM);
|
---|
145 | pgmUnlock(pVM);
|
---|
146 |
|
---|
147 | return rc;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Shared module check helper (called on the way out).
|
---|
152 | *
|
---|
153 | * @param pVM The VM handle.
|
---|
154 | */
|
---|
155 | static DECLCALLBACK(void) pgmR3CheckSharedModulesHelper(PVM pVM)
|
---|
156 | {
|
---|
157 | /* We must stall other VCPUs as we'd otherwise have to send IPI flush commands for every single change we make. */
|
---|
158 | int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, pgmR3SharedModuleRegRendezvous, NULL);
|
---|
159 | AssertRC(rc);
|
---|
160 | }
|
---|
161 | #endif
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Check all registered modules for changes.
|
---|
165 | *
|
---|
166 | * @returns VBox status code.
|
---|
167 | * @param pVM VM handle
|
---|
168 | */
|
---|
169 | VMMR3DECL(int) PGMR3SharedModuleCheckAll(PVM pVM)
|
---|
170 | {
|
---|
171 | #ifdef VBOX_WITH_PAGE_SHARING
|
---|
172 | /* Queue the actual registration as we are under the IOM lock right now. Perform this operation on the way out. */
|
---|
173 | return VMR3ReqCallNoWait(pVM, VMMGetCpuId(pVM), (PFNRT)pgmR3CheckSharedModulesHelper, 1, pVM);
|
---|
174 | #else
|
---|
175 | return VERR_NOT_IMPLEMENTED;
|
---|
176 | #endif
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Query the state of a page in a shared module
|
---|
181 | *
|
---|
182 | * @returns VBox status code.
|
---|
183 | * @param pVM VM handle
|
---|
184 | * @param GCPtrPage Page address
|
---|
185 | * @param pfShared Shared status (out)
|
---|
186 | * @param pfReadWrite Read/write status (out)
|
---|
187 | */
|
---|
188 | VMMR3DECL(int) PGMR3SharedModuleGetPageState(PVM pVM, RTGCPTR GCPtrPage, bool *pfShared, bool *pfReadWrite)
|
---|
189 | {
|
---|
190 | #if defined(VBOX_WITH_PAGE_SHARING) && defined(DEBUG)
|
---|
191 | /* Debug only API for the page fusion testcase. */
|
---|
192 | RTGCPHYS GCPhys;
|
---|
193 | uint64_t fFlags;
|
---|
194 |
|
---|
195 | pgmLock(pVM);
|
---|
196 |
|
---|
197 | int rc = PGMGstGetPage(VMMGetCpu(pVM), GCPtrPage, &fFlags, &GCPhys);
|
---|
198 | if (rc == VINF_SUCCESS)
|
---|
199 | {
|
---|
200 | PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
|
---|
201 | if (pPage)
|
---|
202 | {
|
---|
203 | *pfShared = PGM_PAGE_IS_SHARED(pPage);
|
---|
204 | *pfReadWrite = !!(fFlags & X86_PTE_RW);
|
---|
205 | }
|
---|
206 | else
|
---|
207 | rc = VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
|
---|
208 | }
|
---|
209 |
|
---|
210 | pgmUnlock(pVM);
|
---|
211 | return rc;
|
---|
212 | #else
|
---|
213 | return VERR_NOT_IMPLEMENTED;
|
---|
214 | #endif
|
---|
215 | }
|
---|