1 | /* $Id: PGMPhys.cpp 36911 2011-05-02 14:21:18Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PGM - Page Manager and Monitor, Physical Memory Addressing.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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/vmm/pgm.h>
|
---|
24 | #include <VBox/vmm/iom.h>
|
---|
25 | #include <VBox/vmm/mm.h>
|
---|
26 | #include <VBox/vmm/stam.h>
|
---|
27 | #include <VBox/vmm/rem.h>
|
---|
28 | #include <VBox/vmm/pdmdev.h>
|
---|
29 | #include "PGMInternal.h"
|
---|
30 | #include <VBox/vmm/vm.h>
|
---|
31 | #include "PGMInline.h"
|
---|
32 | #include <VBox/sup.h>
|
---|
33 | #include <VBox/param.h>
|
---|
34 | #include <VBox/err.h>
|
---|
35 | #include <VBox/log.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/alloc.h>
|
---|
38 | #include <iprt/asm.h>
|
---|
39 | #include <iprt/thread.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <iprt/system.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | /*******************************************************************************
|
---|
45 | * Defined Constants And Macros *
|
---|
46 | *******************************************************************************/
|
---|
47 | /** The number of pages to free in one batch. */
|
---|
48 | #define PGMPHYS_FREE_PAGE_BATCH_SIZE 128
|
---|
49 |
|
---|
50 |
|
---|
51 | /*******************************************************************************
|
---|
52 | * Internal Functions *
|
---|
53 | *******************************************************************************/
|
---|
54 | static DECLCALLBACK(int) pgmR3PhysRomWriteHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser);
|
---|
55 |
|
---|
56 |
|
---|
57 | /*
|
---|
58 | * PGMR3PhysReadU8-64
|
---|
59 | * PGMR3PhysWriteU8-64
|
---|
60 | */
|
---|
61 | #define PGMPHYSFN_READNAME PGMR3PhysReadU8
|
---|
62 | #define PGMPHYSFN_WRITENAME PGMR3PhysWriteU8
|
---|
63 | #define PGMPHYS_DATASIZE 1
|
---|
64 | #define PGMPHYS_DATATYPE uint8_t
|
---|
65 | #include "PGMPhysRWTmpl.h"
|
---|
66 |
|
---|
67 | #define PGMPHYSFN_READNAME PGMR3PhysReadU16
|
---|
68 | #define PGMPHYSFN_WRITENAME PGMR3PhysWriteU16
|
---|
69 | #define PGMPHYS_DATASIZE 2
|
---|
70 | #define PGMPHYS_DATATYPE uint16_t
|
---|
71 | #include "PGMPhysRWTmpl.h"
|
---|
72 |
|
---|
73 | #define PGMPHYSFN_READNAME PGMR3PhysReadU32
|
---|
74 | #define PGMPHYSFN_WRITENAME PGMR3PhysWriteU32
|
---|
75 | #define PGMPHYS_DATASIZE 4
|
---|
76 | #define PGMPHYS_DATATYPE uint32_t
|
---|
77 | #include "PGMPhysRWTmpl.h"
|
---|
78 |
|
---|
79 | #define PGMPHYSFN_READNAME PGMR3PhysReadU64
|
---|
80 | #define PGMPHYSFN_WRITENAME PGMR3PhysWriteU64
|
---|
81 | #define PGMPHYS_DATASIZE 8
|
---|
82 | #define PGMPHYS_DATATYPE uint64_t
|
---|
83 | #include "PGMPhysRWTmpl.h"
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * EMT worker for PGMR3PhysReadExternal.
|
---|
88 | */
|
---|
89 | static DECLCALLBACK(int) pgmR3PhysReadExternalEMT(PVM pVM, PRTGCPHYS pGCPhys, void *pvBuf, size_t cbRead)
|
---|
90 | {
|
---|
91 | PGMPhysRead(pVM, *pGCPhys, pvBuf, cbRead);
|
---|
92 | return VINF_SUCCESS;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Read from physical memory, external users.
|
---|
98 | *
|
---|
99 | * @returns VBox status code.
|
---|
100 | * @retval VINF_SUCCESS.
|
---|
101 | *
|
---|
102 | * @param pVM VM Handle.
|
---|
103 | * @param GCPhys Physical address to read from.
|
---|
104 | * @param pvBuf Where to read into.
|
---|
105 | * @param cbRead How many bytes to read.
|
---|
106 | *
|
---|
107 | * @thread Any but EMTs.
|
---|
108 | */
|
---|
109 | VMMR3DECL(int) PGMR3PhysReadExternal(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
|
---|
110 | {
|
---|
111 | VM_ASSERT_OTHER_THREAD(pVM);
|
---|
112 |
|
---|
113 | AssertMsgReturn(cbRead > 0, ("don't even think about reading zero bytes!\n"), VINF_SUCCESS);
|
---|
114 | LogFlow(("PGMR3PhysReadExternal: %RGp %d\n", GCPhys, cbRead));
|
---|
115 |
|
---|
116 | pgmLock(pVM);
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Copy loop on ram ranges.
|
---|
120 | */
|
---|
121 | PPGMRAMRANGE pRam = pgmPhysGetRangeAtOrAbove(pVM, GCPhys);
|
---|
122 | for (;;)
|
---|
123 | {
|
---|
124 | /* Inside range or not? */
|
---|
125 | if (pRam && GCPhys >= pRam->GCPhys)
|
---|
126 | {
|
---|
127 | /*
|
---|
128 | * Must work our way thru this page by page.
|
---|
129 | */
|
---|
130 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
131 | while (off < pRam->cb)
|
---|
132 | {
|
---|
133 | unsigned iPage = off >> PAGE_SHIFT;
|
---|
134 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * If the page has an ALL access handler, we'll have to
|
---|
138 | * delegate the job to EMT.
|
---|
139 | */
|
---|
140 | if (PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage))
|
---|
141 | {
|
---|
142 | pgmUnlock(pVM);
|
---|
143 |
|
---|
144 | return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)pgmR3PhysReadExternalEMT, 4,
|
---|
145 | pVM, &GCPhys, pvBuf, cbRead);
|
---|
146 | }
|
---|
147 | Assert(!PGM_PAGE_IS_MMIO(pPage));
|
---|
148 |
|
---|
149 | /*
|
---|
150 | * Simple stuff, go ahead.
|
---|
151 | */
|
---|
152 | size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
153 | if (cb > cbRead)
|
---|
154 | cb = cbRead;
|
---|
155 | const void *pvSrc;
|
---|
156 | int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, pRam->GCPhys + off, &pvSrc);
|
---|
157 | if (RT_SUCCESS(rc))
|
---|
158 | memcpy(pvBuf, pvSrc, cb);
|
---|
159 | else
|
---|
160 | {
|
---|
161 | AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
162 | pRam->GCPhys + off, pPage, rc));
|
---|
163 | memset(pvBuf, 0xff, cb);
|
---|
164 | }
|
---|
165 |
|
---|
166 | /* next page */
|
---|
167 | if (cb >= cbRead)
|
---|
168 | {
|
---|
169 | pgmUnlock(pVM);
|
---|
170 | return VINF_SUCCESS;
|
---|
171 | }
|
---|
172 | cbRead -= cb;
|
---|
173 | off += cb;
|
---|
174 | GCPhys += cb;
|
---|
175 | pvBuf = (char *)pvBuf + cb;
|
---|
176 | } /* walk pages in ram range. */
|
---|
177 | }
|
---|
178 | else
|
---|
179 | {
|
---|
180 | LogFlow(("PGMPhysRead: Unassigned %RGp size=%u\n", GCPhys, cbRead));
|
---|
181 |
|
---|
182 | /*
|
---|
183 | * Unassigned address space.
|
---|
184 | */
|
---|
185 | size_t cb = pRam ? pRam->GCPhys - GCPhys : ~(size_t)0;
|
---|
186 | if (cb >= cbRead)
|
---|
187 | {
|
---|
188 | memset(pvBuf, 0xff, cbRead);
|
---|
189 | break;
|
---|
190 | }
|
---|
191 | memset(pvBuf, 0xff, cb);
|
---|
192 |
|
---|
193 | cbRead -= cb;
|
---|
194 | pvBuf = (char *)pvBuf + cb;
|
---|
195 | GCPhys += cb;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /* Advance range if necessary. */
|
---|
199 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
200 | pRam = pRam->CTX_SUFF(pNext);
|
---|
201 | } /* Ram range walk */
|
---|
202 |
|
---|
203 | pgmUnlock(pVM);
|
---|
204 |
|
---|
205 | return VINF_SUCCESS;
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * EMT worker for PGMR3PhysWriteExternal.
|
---|
211 | */
|
---|
212 | static DECLCALLBACK(int) pgmR3PhysWriteExternalEMT(PVM pVM, PRTGCPHYS pGCPhys, const void *pvBuf, size_t cbWrite)
|
---|
213 | {
|
---|
214 | /** @todo VERR_EM_NO_MEMORY */
|
---|
215 | PGMPhysWrite(pVM, *pGCPhys, pvBuf, cbWrite);
|
---|
216 | return VINF_SUCCESS;
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Write to physical memory, external users.
|
---|
222 | *
|
---|
223 | * @returns VBox status code.
|
---|
224 | * @retval VINF_SUCCESS.
|
---|
225 | * @retval VERR_EM_NO_MEMORY.
|
---|
226 | *
|
---|
227 | * @param pVM VM Handle.
|
---|
228 | * @param GCPhys Physical address to write to.
|
---|
229 | * @param pvBuf What to write.
|
---|
230 | * @param cbWrite How many bytes to write.
|
---|
231 | * @param pszWho Who is writing. For tracking down who is writing
|
---|
232 | * after we've saved the state.
|
---|
233 | *
|
---|
234 | * @thread Any but EMTs.
|
---|
235 | */
|
---|
236 | VMMDECL(int) PGMR3PhysWriteExternal(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite, const char *pszWho)
|
---|
237 | {
|
---|
238 | VM_ASSERT_OTHER_THREAD(pVM);
|
---|
239 |
|
---|
240 | AssertMsg(!pVM->pgm.s.fNoMorePhysWrites,
|
---|
241 | ("Calling PGMR3PhysWriteExternal after pgmR3Save()! GCPhys=%RGp cbWrite=%#x pszWho=%s\n",
|
---|
242 | GCPhys, cbWrite, pszWho));
|
---|
243 | AssertMsgReturn(cbWrite > 0, ("don't even think about writing zero bytes!\n"), VINF_SUCCESS);
|
---|
244 | LogFlow(("PGMR3PhysWriteExternal: %RGp %d\n", GCPhys, cbWrite));
|
---|
245 |
|
---|
246 | pgmLock(pVM);
|
---|
247 |
|
---|
248 | /*
|
---|
249 | * Copy loop on ram ranges, stop when we hit something difficult.
|
---|
250 | */
|
---|
251 | PPGMRAMRANGE pRam = pgmPhysGetRangeAtOrAbove(pVM, GCPhys);
|
---|
252 | for (;;)
|
---|
253 | {
|
---|
254 | /* Inside range or not? */
|
---|
255 | if (pRam && GCPhys >= pRam->GCPhys)
|
---|
256 | {
|
---|
257 | /*
|
---|
258 | * Must work our way thru this page by page.
|
---|
259 | */
|
---|
260 | RTGCPTR off = GCPhys - pRam->GCPhys;
|
---|
261 | while (off < pRam->cb)
|
---|
262 | {
|
---|
263 | RTGCPTR iPage = off >> PAGE_SHIFT;
|
---|
264 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
265 |
|
---|
266 | /*
|
---|
267 | * Is the page problematic, we have to do the work on the EMT.
|
---|
268 | *
|
---|
269 | * Allocating writable pages and access handlers are
|
---|
270 | * problematic, write monitored pages are simple and can be
|
---|
271 | * dealt with here.
|
---|
272 | */
|
---|
273 | if ( PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage)
|
---|
274 | || PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED)
|
---|
275 | {
|
---|
276 | if ( PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_WRITE_MONITORED
|
---|
277 | && !PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage))
|
---|
278 | pgmPhysPageMakeWriteMonitoredWritable(pVM, pPage);
|
---|
279 | else
|
---|
280 | {
|
---|
281 | pgmUnlock(pVM);
|
---|
282 |
|
---|
283 | return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)pgmR3PhysWriteExternalEMT, 4,
|
---|
284 | pVM, &GCPhys, pvBuf, cbWrite);
|
---|
285 | }
|
---|
286 | }
|
---|
287 | Assert(!PGM_PAGE_IS_MMIO(pPage));
|
---|
288 |
|
---|
289 | /*
|
---|
290 | * Simple stuff, go ahead.
|
---|
291 | */
|
---|
292 | size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
293 | if (cb > cbWrite)
|
---|
294 | cb = cbWrite;
|
---|
295 | void *pvDst;
|
---|
296 | int rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, pRam->GCPhys + off, &pvDst);
|
---|
297 | if (RT_SUCCESS(rc))
|
---|
298 | memcpy(pvDst, pvBuf, cb);
|
---|
299 | else
|
---|
300 | AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
301 | pRam->GCPhys + off, pPage, rc));
|
---|
302 |
|
---|
303 | /* next page */
|
---|
304 | if (cb >= cbWrite)
|
---|
305 | {
|
---|
306 | pgmUnlock(pVM);
|
---|
307 | return VINF_SUCCESS;
|
---|
308 | }
|
---|
309 |
|
---|
310 | cbWrite -= cb;
|
---|
311 | off += cb;
|
---|
312 | GCPhys += cb;
|
---|
313 | pvBuf = (const char *)pvBuf + cb;
|
---|
314 | } /* walk pages in ram range */
|
---|
315 | }
|
---|
316 | else
|
---|
317 | {
|
---|
318 | /*
|
---|
319 | * Unassigned address space, skip it.
|
---|
320 | */
|
---|
321 | if (!pRam)
|
---|
322 | break;
|
---|
323 | size_t cb = pRam->GCPhys - GCPhys;
|
---|
324 | if (cb >= cbWrite)
|
---|
325 | break;
|
---|
326 | cbWrite -= cb;
|
---|
327 | pvBuf = (const char *)pvBuf + cb;
|
---|
328 | GCPhys += cb;
|
---|
329 | }
|
---|
330 |
|
---|
331 | /* Advance range if necessary. */
|
---|
332 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
333 | pRam = pRam->CTX_SUFF(pNext);
|
---|
334 | } /* Ram range walk */
|
---|
335 |
|
---|
336 | pgmUnlock(pVM);
|
---|
337 | return VINF_SUCCESS;
|
---|
338 | }
|
---|
339 |
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * VMR3ReqCall worker for PGMR3PhysGCPhys2CCPtrExternal to make pages writable.
|
---|
343 | *
|
---|
344 | * @returns see PGMR3PhysGCPhys2CCPtrExternal
|
---|
345 | * @param pVM The VM handle.
|
---|
346 | * @param pGCPhys Pointer to the guest physical address.
|
---|
347 | * @param ppv Where to store the mapping address.
|
---|
348 | * @param pLock Where to store the lock.
|
---|
349 | */
|
---|
350 | static DECLCALLBACK(int) pgmR3PhysGCPhys2CCPtrDelegated(PVM pVM, PRTGCPHYS pGCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
351 | {
|
---|
352 | /*
|
---|
353 | * Just hand it to PGMPhysGCPhys2CCPtr and check that it's not a page with
|
---|
354 | * an access handler after it succeeds.
|
---|
355 | */
|
---|
356 | int rc = pgmLock(pVM);
|
---|
357 | AssertRCReturn(rc, rc);
|
---|
358 |
|
---|
359 | rc = PGMPhysGCPhys2CCPtr(pVM, *pGCPhys, ppv, pLock);
|
---|
360 | if (RT_SUCCESS(rc))
|
---|
361 | {
|
---|
362 | PPGMPAGEMAPTLBE pTlbe;
|
---|
363 | int rc2 = pgmPhysPageQueryTlbe(pVM, *pGCPhys, &pTlbe);
|
---|
364 | AssertFatalRC(rc2);
|
---|
365 | PPGMPAGE pPage = pTlbe->pPage;
|
---|
366 | if (PGM_PAGE_IS_MMIO(pPage))
|
---|
367 | {
|
---|
368 | PGMPhysReleasePageMappingLock(pVM, pLock);
|
---|
369 | rc = VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
370 | }
|
---|
371 | else if ( PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage)
|
---|
372 | #ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
|
---|
373 | || pgmPoolIsDirtyPage(pVM, *pGCPhys)
|
---|
374 | #endif
|
---|
375 | )
|
---|
376 | {
|
---|
377 | /* We *must* flush any corresponding pgm pool page here, otherwise we'll
|
---|
378 | * not be informed about writes and keep bogus gst->shw mappings around.
|
---|
379 | */
|
---|
380 | pgmPoolFlushPageByGCPhys(pVM, *pGCPhys);
|
---|
381 | Assert(!PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage));
|
---|
382 | /** @todo r=bird: return VERR_PGM_PHYS_PAGE_RESERVED here if it still has
|
---|
383 | * active handlers, see the PGMR3PhysGCPhys2CCPtrExternal docs. */
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | pgmUnlock(pVM);
|
---|
388 | return rc;
|
---|
389 | }
|
---|
390 |
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Requests the mapping of a guest page into ring-3, external threads.
|
---|
394 | *
|
---|
395 | * When you're done with the page, call PGMPhysReleasePageMappingLock() ASAP to
|
---|
396 | * release it.
|
---|
397 | *
|
---|
398 | * This API will assume your intention is to write to the page, and will
|
---|
399 | * therefore replace shared and zero pages. If you do not intend to modify the
|
---|
400 | * page, use the PGMR3PhysGCPhys2CCPtrReadOnlyExternal() API.
|
---|
401 | *
|
---|
402 | * @returns VBox status code.
|
---|
403 | * @retval VINF_SUCCESS on success.
|
---|
404 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical
|
---|
405 | * backing or if the page has any active access handlers. The caller
|
---|
406 | * must fall back on using PGMR3PhysWriteExternal.
|
---|
407 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
408 | *
|
---|
409 | * @param pVM The VM handle.
|
---|
410 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
411 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
412 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
413 | *
|
---|
414 | * @remark Avoid calling this API from within critical sections (other than the
|
---|
415 | * PGM one) because of the deadlock risk when we have to delegating the
|
---|
416 | * task to an EMT.
|
---|
417 | * @thread Any.
|
---|
418 | */
|
---|
419 | VMMR3DECL(int) PGMR3PhysGCPhys2CCPtrExternal(PVM pVM, RTGCPHYS GCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
420 | {
|
---|
421 | AssertPtr(ppv);
|
---|
422 | AssertPtr(pLock);
|
---|
423 |
|
---|
424 | Assert(VM_IS_EMT(pVM) || !PGMIsLockOwner(pVM));
|
---|
425 |
|
---|
426 | int rc = pgmLock(pVM);
|
---|
427 | AssertRCReturn(rc, rc);
|
---|
428 |
|
---|
429 | /*
|
---|
430 | * Query the Physical TLB entry for the page (may fail).
|
---|
431 | */
|
---|
432 | PPGMPAGEMAPTLBE pTlbe;
|
---|
433 | rc = pgmPhysPageQueryTlbe(pVM, GCPhys, &pTlbe);
|
---|
434 | if (RT_SUCCESS(rc))
|
---|
435 | {
|
---|
436 | PPGMPAGE pPage = pTlbe->pPage;
|
---|
437 | if (PGM_PAGE_IS_MMIO(pPage))
|
---|
438 | rc = VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
439 | else
|
---|
440 | {
|
---|
441 | /*
|
---|
442 | * If the page is shared, the zero page, or being write monitored
|
---|
443 | * it must be converted to an page that's writable if possible.
|
---|
444 | * We can only deal with write monitored pages here, the rest have
|
---|
445 | * to be on an EMT.
|
---|
446 | */
|
---|
447 | if ( PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage)
|
---|
448 | || PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED
|
---|
449 | #ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
|
---|
450 | || pgmPoolIsDirtyPage(pVM, GCPhys)
|
---|
451 | #endif
|
---|
452 | )
|
---|
453 | {
|
---|
454 | if ( PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_WRITE_MONITORED
|
---|
455 | && !PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage)
|
---|
456 | #ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
|
---|
457 | && !pgmPoolIsDirtyPage(pVM, GCPhys)
|
---|
458 | #endif
|
---|
459 | )
|
---|
460 | pgmPhysPageMakeWriteMonitoredWritable(pVM, pPage);
|
---|
461 | else
|
---|
462 | {
|
---|
463 | pgmUnlock(pVM);
|
---|
464 |
|
---|
465 | return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)pgmR3PhysGCPhys2CCPtrDelegated, 4,
|
---|
466 | pVM, &GCPhys, ppv, pLock);
|
---|
467 | }
|
---|
468 | }
|
---|
469 |
|
---|
470 | /*
|
---|
471 | * Now, just perform the locking and calculate the return address.
|
---|
472 | */
|
---|
473 | PPGMPAGEMAP pMap = pTlbe->pMap;
|
---|
474 | if (pMap)
|
---|
475 | pMap->cRefs++;
|
---|
476 |
|
---|
477 | unsigned cLocks = PGM_PAGE_GET_WRITE_LOCKS(pPage);
|
---|
478 | if (RT_LIKELY(cLocks < PGM_PAGE_MAX_LOCKS - 1))
|
---|
479 | {
|
---|
480 | if (cLocks == 0)
|
---|
481 | pVM->pgm.s.cWriteLockedPages++;
|
---|
482 | PGM_PAGE_INC_WRITE_LOCKS(pPage);
|
---|
483 | }
|
---|
484 | else if (cLocks != PGM_PAGE_GET_WRITE_LOCKS(pPage))
|
---|
485 | {
|
---|
486 | PGM_PAGE_INC_WRITE_LOCKS(pPage);
|
---|
487 | AssertMsgFailed(("%RGp / %R[pgmpage] is entering permanent write locked state!\n", GCPhys, pPage));
|
---|
488 | if (pMap)
|
---|
489 | pMap->cRefs++; /* Extra ref to prevent it from going away. */
|
---|
490 | }
|
---|
491 |
|
---|
492 | *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
|
---|
493 | pLock->uPageAndType = (uintptr_t)pPage | PGMPAGEMAPLOCK_TYPE_WRITE;
|
---|
494 | pLock->pvMap = pMap;
|
---|
495 | }
|
---|
496 | }
|
---|
497 |
|
---|
498 | pgmUnlock(pVM);
|
---|
499 | return rc;
|
---|
500 | }
|
---|
501 |
|
---|
502 |
|
---|
503 | /**
|
---|
504 | * Requests the mapping of a guest page into ring-3, external threads.
|
---|
505 | *
|
---|
506 | * When you're done with the page, call PGMPhysReleasePageMappingLock() ASAP to
|
---|
507 | * release it.
|
---|
508 | *
|
---|
509 | * @returns VBox status code.
|
---|
510 | * @retval VINF_SUCCESS on success.
|
---|
511 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical
|
---|
512 | * backing or if the page as an active ALL access handler. The caller
|
---|
513 | * must fall back on using PGMPhysRead.
|
---|
514 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
515 | *
|
---|
516 | * @param pVM The VM handle.
|
---|
517 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
518 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
519 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
520 | *
|
---|
521 | * @remark Avoid calling this API from within critical sections (other than
|
---|
522 | * the PGM one) because of the deadlock risk.
|
---|
523 | * @thread Any.
|
---|
524 | */
|
---|
525 | VMMR3DECL(int) PGMR3PhysGCPhys2CCPtrReadOnlyExternal(PVM pVM, RTGCPHYS GCPhys, void const **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
526 | {
|
---|
527 | int rc = pgmLock(pVM);
|
---|
528 | AssertRCReturn(rc, rc);
|
---|
529 |
|
---|
530 | /*
|
---|
531 | * Query the Physical TLB entry for the page (may fail).
|
---|
532 | */
|
---|
533 | PPGMPAGEMAPTLBE pTlbe;
|
---|
534 | rc = pgmPhysPageQueryTlbe(pVM, GCPhys, &pTlbe);
|
---|
535 | if (RT_SUCCESS(rc))
|
---|
536 | {
|
---|
537 | PPGMPAGE pPage = pTlbe->pPage;
|
---|
538 | #if 1
|
---|
539 | /* MMIO pages doesn't have any readable backing. */
|
---|
540 | if (PGM_PAGE_IS_MMIO(pPage))
|
---|
541 | rc = VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
542 | #else
|
---|
543 | if (PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage))
|
---|
544 | rc = VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
545 | #endif
|
---|
546 | else
|
---|
547 | {
|
---|
548 | /*
|
---|
549 | * Now, just perform the locking and calculate the return address.
|
---|
550 | */
|
---|
551 | PPGMPAGEMAP pMap = pTlbe->pMap;
|
---|
552 | if (pMap)
|
---|
553 | pMap->cRefs++;
|
---|
554 |
|
---|
555 | unsigned cLocks = PGM_PAGE_GET_READ_LOCKS(pPage);
|
---|
556 | if (RT_LIKELY(cLocks < PGM_PAGE_MAX_LOCKS - 1))
|
---|
557 | {
|
---|
558 | if (cLocks == 0)
|
---|
559 | pVM->pgm.s.cReadLockedPages++;
|
---|
560 | PGM_PAGE_INC_READ_LOCKS(pPage);
|
---|
561 | }
|
---|
562 | else if (cLocks != PGM_PAGE_GET_READ_LOCKS(pPage))
|
---|
563 | {
|
---|
564 | PGM_PAGE_INC_READ_LOCKS(pPage);
|
---|
565 | AssertMsgFailed(("%RGp / %R[pgmpage] is entering permanent readonly locked state!\n", GCPhys, pPage));
|
---|
566 | if (pMap)
|
---|
567 | pMap->cRefs++; /* Extra ref to prevent it from going away. */
|
---|
568 | }
|
---|
569 |
|
---|
570 | *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
|
---|
571 | pLock->uPageAndType = (uintptr_t)pPage | PGMPAGEMAPLOCK_TYPE_READ;
|
---|
572 | pLock->pvMap = pMap;
|
---|
573 | }
|
---|
574 | }
|
---|
575 |
|
---|
576 | pgmUnlock(pVM);
|
---|
577 | return rc;
|
---|
578 | }
|
---|
579 |
|
---|
580 | #ifdef PGM_USE_RAMRANGE_SEARCH_TREES
|
---|
581 |
|
---|
582 | #define MAKE_LEAF(a_pNode) \
|
---|
583 | do { \
|
---|
584 | (a_pNode)->pLeftR3 = NIL_RTR3PTR; \
|
---|
585 | (a_pNode)->pRightR3 = NIL_RTR3PTR; \
|
---|
586 | (a_pNode)->pLeftR0 = NIL_RTR0PTR; \
|
---|
587 | (a_pNode)->pRightR0 = NIL_RTR0PTR; \
|
---|
588 | (a_pNode)->pLeftRC = NIL_RTRCPTR; \
|
---|
589 | (a_pNode)->pRightRC = NIL_RTRCPTR; \
|
---|
590 | } while (0)
|
---|
591 |
|
---|
592 | #define INSERT_LEFT(a_pParent, a_pNode) \
|
---|
593 | do { \
|
---|
594 | (a_pParent)->pLeftR3 = (a_pNode); \
|
---|
595 | (a_pParent)->pLeftR0 = (a_pNode)->pSelfR0; \
|
---|
596 | (a_pParent)->pLeftRC = (a_pNode)->pSelfRC; \
|
---|
597 | } while (0)
|
---|
598 | #define INSERT_RIGHT(a_pParent, a_pNode) \
|
---|
599 | do { \
|
---|
600 | (a_pParent)->pRightR3 = (a_pNode); \
|
---|
601 | (a_pParent)->pRightR0 = (a_pNode)->pSelfR0; \
|
---|
602 | (a_pParent)->pRightRC = (a_pNode)->pSelfRC; \
|
---|
603 | } while (0)
|
---|
604 |
|
---|
605 |
|
---|
606 | /**
|
---|
607 | * Recursive tree builder.
|
---|
608 | *
|
---|
609 | * @param ppRam Pointer to the iterator variable.
|
---|
610 | * @param iHeight The hight about normal leaf nodes. Inserts a leaf
|
---|
611 | * node if 0.
|
---|
612 | */
|
---|
613 | static PPGMRAMRANGE pgmR3PhysRebuildRamRangeSearchTreesRecursively(PPGMRAMRANGE *ppRam, int iDepth)
|
---|
614 | {
|
---|
615 | PPGMRAMRANGE pRam;
|
---|
616 | if (iDepth <= 0)
|
---|
617 | {
|
---|
618 | /*
|
---|
619 | * Leaf node.
|
---|
620 | */
|
---|
621 | pRam = *ppRam;
|
---|
622 | if (pRam)
|
---|
623 | {
|
---|
624 | *ppRam = pRam->pNextR3;
|
---|
625 | MAKE_LEAF(pRam);
|
---|
626 | }
|
---|
627 | }
|
---|
628 | else
|
---|
629 | {
|
---|
630 |
|
---|
631 | /*
|
---|
632 | * Intermediate node.
|
---|
633 | */
|
---|
634 | PPGMRAMRANGE pLeft = pgmR3PhysRebuildRamRangeSearchTreesRecursively(ppRam, iDepth - 1);
|
---|
635 |
|
---|
636 | pRam = *ppRam;
|
---|
637 | if (!pRam)
|
---|
638 | return pLeft;
|
---|
639 | *ppRam = pRam->pNextR3;
|
---|
640 | MAKE_LEAF(pRam);
|
---|
641 | INSERT_LEFT(pRam, pLeft);
|
---|
642 |
|
---|
643 | PPGMRAMRANGE pRight = pgmR3PhysRebuildRamRangeSearchTreesRecursively(ppRam, iDepth - 1);
|
---|
644 | if (pRight)
|
---|
645 | INSERT_RIGHT(pRam, pRight);
|
---|
646 | }
|
---|
647 | return pRam;
|
---|
648 | }
|
---|
649 |
|
---|
650 |
|
---|
651 | /**
|
---|
652 | * Rebuilds the RAM range search trees.
|
---|
653 | *
|
---|
654 | * @param pVM The VM handle.
|
---|
655 | */
|
---|
656 | static void pgmR3PhysRebuildRamRangeSearchTrees(PVM pVM)
|
---|
657 | {
|
---|
658 |
|
---|
659 | /*
|
---|
660 | * Create the reasonably balanced tree in a sequential fashion.
|
---|
661 | * For simplicity (laziness) we use standard recursion here.
|
---|
662 | */
|
---|
663 | int iDepth = 0;
|
---|
664 | PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesXR3;
|
---|
665 | PPGMRAMRANGE pRoot = pgmR3PhysRebuildRamRangeSearchTreesRecursively(&pRam, 0);
|
---|
666 | while (pRam)
|
---|
667 | {
|
---|
668 | PPGMRAMRANGE pLeft = pRoot;
|
---|
669 |
|
---|
670 | pRoot = pRam;
|
---|
671 | pRam = pRam->pNextR3;
|
---|
672 | MAKE_LEAF(pRoot);
|
---|
673 | INSERT_LEFT(pRoot, pLeft);
|
---|
674 |
|
---|
675 | PPGMRAMRANGE pRight = pgmR3PhysRebuildRamRangeSearchTreesRecursively(&pRam, iDepth);
|
---|
676 | if (pRight)
|
---|
677 | INSERT_RIGHT(pRoot, pRight);
|
---|
678 | /** @todo else: rotate the tree. */
|
---|
679 |
|
---|
680 | iDepth++;
|
---|
681 | }
|
---|
682 |
|
---|
683 | pVM->pgm.s.pRamRangeTreeR3 = pRoot;
|
---|
684 | pVM->pgm.s.pRamRangeTreeR0 = pRoot ? pRoot->pSelfR0 : NIL_RTR0PTR;
|
---|
685 | pVM->pgm.s.pRamRangeTreeRC = pRoot ? pRoot->pSelfRC : NIL_RTRCPTR;
|
---|
686 |
|
---|
687 | #ifdef VBOX_STRICT
|
---|
688 | /*
|
---|
689 | * Verify that the above code works.
|
---|
690 | */
|
---|
691 | unsigned cRanges = 0;
|
---|
692 | for (pRam = pVM->pgm.s.pRamRangesXR3; pRam; pRam = pRam->pNextR3)
|
---|
693 | cRanges++;
|
---|
694 | Assert(cRanges > 0);
|
---|
695 |
|
---|
696 | unsigned cMaxDepth = ASMBitLastSetU32(cRanges);
|
---|
697 | if ((1U << cMaxDepth) < cRanges)
|
---|
698 | cMaxDepth++;
|
---|
699 |
|
---|
700 | for (pRam = pVM->pgm.s.pRamRangesXR3; pRam; pRam = pRam->pNextR3)
|
---|
701 | {
|
---|
702 | unsigned cDepth = 0;
|
---|
703 | PPGMRAMRANGE pRam2 = pVM->pgm.s.pRamRangeTreeR3;
|
---|
704 | for (;;)
|
---|
705 | {
|
---|
706 | if (pRam == pRam2)
|
---|
707 | break;
|
---|
708 | Assert(pRam2);
|
---|
709 | if (pRam->GCPhys < pRam2->GCPhys)
|
---|
710 | pRam2 = pRam2->pLeftR3;
|
---|
711 | else
|
---|
712 | pRam2 = pRam2->pRightR3;
|
---|
713 | }
|
---|
714 | AssertMsg(cDepth <= cMaxDepth, ("cDepth=%d cMaxDepth=%d\n", cDepth, cMaxDepth));
|
---|
715 | }
|
---|
716 | #endif /* VBOX_STRICT */
|
---|
717 | }
|
---|
718 |
|
---|
719 | #undef MAKE_LEAF
|
---|
720 | #undef INSERT_LEFT
|
---|
721 | #undef INSERT_RIGHT
|
---|
722 | #endif /* PGM_USE_RAMRANGE_SEARCH_TREES */
|
---|
723 |
|
---|
724 | /**
|
---|
725 | * Relinks the RAM ranges using the pSelfRC and pSelfR0 pointers.
|
---|
726 | *
|
---|
727 | * Called when anything was relocated.
|
---|
728 | *
|
---|
729 | * @param pVM Pointer to the shared VM structure.
|
---|
730 | */
|
---|
731 | void pgmR3PhysRelinkRamRanges(PVM pVM)
|
---|
732 | {
|
---|
733 | PPGMRAMRANGE pCur;
|
---|
734 |
|
---|
735 | #ifdef VBOX_STRICT
|
---|
736 | for (pCur = pVM->pgm.s.pRamRangesXR3; pCur; pCur = pCur->pNextR3)
|
---|
737 | {
|
---|
738 | Assert((pCur->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pCur->pSelfR0 == MMHyperCCToR0(pVM, pCur));
|
---|
739 | Assert((pCur->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pCur->pSelfRC == MMHyperCCToRC(pVM, pCur));
|
---|
740 | Assert((pCur->GCPhys & PAGE_OFFSET_MASK) == 0);
|
---|
741 | Assert((pCur->GCPhysLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
|
---|
742 | Assert((pCur->cb & PAGE_OFFSET_MASK) == 0);
|
---|
743 | Assert(pCur->cb == pCur->GCPhysLast - pCur->GCPhys + 1);
|
---|
744 | for (PPGMRAMRANGE pCur2 = pVM->pgm.s.pRamRangesXR3; pCur2; pCur2 = pCur2->pNextR3)
|
---|
745 | Assert( pCur2 == pCur
|
---|
746 | || strcmp(pCur2->pszDesc, pCur->pszDesc)); /** @todo fix MMIO ranges!! */
|
---|
747 | }
|
---|
748 | #endif
|
---|
749 |
|
---|
750 | pCur = pVM->pgm.s.pRamRangesXR3;
|
---|
751 | if (pCur)
|
---|
752 | {
|
---|
753 | pVM->pgm.s.pRamRangesXR0 = pCur->pSelfR0;
|
---|
754 | pVM->pgm.s.pRamRangesXRC = pCur->pSelfRC;
|
---|
755 |
|
---|
756 | for (; pCur->pNextR3; pCur = pCur->pNextR3)
|
---|
757 | {
|
---|
758 | pCur->pNextR0 = pCur->pNextR3->pSelfR0;
|
---|
759 | pCur->pNextRC = pCur->pNextR3->pSelfRC;
|
---|
760 | }
|
---|
761 |
|
---|
762 | Assert(pCur->pNextR0 == NIL_RTR0PTR);
|
---|
763 | Assert(pCur->pNextRC == NIL_RTRCPTR);
|
---|
764 | }
|
---|
765 | else
|
---|
766 | {
|
---|
767 | Assert(pVM->pgm.s.pRamRangesXR0 == NIL_RTR0PTR);
|
---|
768 | Assert(pVM->pgm.s.pRamRangesXRC == NIL_RTRCPTR);
|
---|
769 | }
|
---|
770 | ASMAtomicIncU32(&pVM->pgm.s.idRamRangesGen);
|
---|
771 |
|
---|
772 | #ifdef PGM_USE_RAMRANGE_SEARCH_TREES
|
---|
773 | pgmR3PhysRebuildRamRangeSearchTrees(pVM);
|
---|
774 | #endif
|
---|
775 | }
|
---|
776 |
|
---|
777 |
|
---|
778 | /**
|
---|
779 | * Links a new RAM range into the list.
|
---|
780 | *
|
---|
781 | * @param pVM Pointer to the shared VM structure.
|
---|
782 | * @param pNew Pointer to the new list entry.
|
---|
783 | * @param pPrev Pointer to the previous list entry. If NULL, insert as head.
|
---|
784 | */
|
---|
785 | static void pgmR3PhysLinkRamRange(PVM pVM, PPGMRAMRANGE pNew, PPGMRAMRANGE pPrev)
|
---|
786 | {
|
---|
787 | AssertMsg(pNew->pszDesc, ("%RGp-%RGp\n", pNew->GCPhys, pNew->GCPhysLast));
|
---|
788 | Assert((pNew->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pNew->pSelfR0 == MMHyperCCToR0(pVM, pNew));
|
---|
789 | Assert((pNew->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pNew->pSelfRC == MMHyperCCToRC(pVM, pNew));
|
---|
790 |
|
---|
791 | pgmLock(pVM);
|
---|
792 |
|
---|
793 | PPGMRAMRANGE pRam = pPrev ? pPrev->pNextR3 : pVM->pgm.s.pRamRangesXR3;
|
---|
794 | pNew->pNextR3 = pRam;
|
---|
795 | pNew->pNextR0 = pRam ? pRam->pSelfR0 : NIL_RTR0PTR;
|
---|
796 | pNew->pNextRC = pRam ? pRam->pSelfRC : NIL_RTRCPTR;
|
---|
797 |
|
---|
798 | if (pPrev)
|
---|
799 | {
|
---|
800 | pPrev->pNextR3 = pNew;
|
---|
801 | pPrev->pNextR0 = pNew->pSelfR0;
|
---|
802 | pPrev->pNextRC = pNew->pSelfRC;
|
---|
803 | }
|
---|
804 | else
|
---|
805 | {
|
---|
806 | pVM->pgm.s.pRamRangesXR3 = pNew;
|
---|
807 | pVM->pgm.s.pRamRangesXR0 = pNew->pSelfR0;
|
---|
808 | pVM->pgm.s.pRamRangesXRC = pNew->pSelfRC;
|
---|
809 | }
|
---|
810 | ASMAtomicIncU32(&pVM->pgm.s.idRamRangesGen);
|
---|
811 |
|
---|
812 | #ifdef PGM_USE_RAMRANGE_SEARCH_TREES
|
---|
813 | pgmR3PhysRebuildRamRangeSearchTrees(pVM);
|
---|
814 | #endif
|
---|
815 | pgmUnlock(pVM);
|
---|
816 | }
|
---|
817 |
|
---|
818 |
|
---|
819 | /**
|
---|
820 | * Unlink an existing RAM range from the list.
|
---|
821 | *
|
---|
822 | * @param pVM Pointer to the shared VM structure.
|
---|
823 | * @param pRam Pointer to the new list entry.
|
---|
824 | * @param pPrev Pointer to the previous list entry. If NULL, insert as head.
|
---|
825 | */
|
---|
826 | static void pgmR3PhysUnlinkRamRange2(PVM pVM, PPGMRAMRANGE pRam, PPGMRAMRANGE pPrev)
|
---|
827 | {
|
---|
828 | Assert(pPrev ? pPrev->pNextR3 == pRam : pVM->pgm.s.pRamRangesXR3 == pRam);
|
---|
829 | Assert((pRam->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pRam->pSelfR0 == MMHyperCCToR0(pVM, pRam));
|
---|
830 | Assert((pRam->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pRam->pSelfRC == MMHyperCCToRC(pVM, pRam));
|
---|
831 |
|
---|
832 | pgmLock(pVM);
|
---|
833 |
|
---|
834 | PPGMRAMRANGE pNext = pRam->pNextR3;
|
---|
835 | if (pPrev)
|
---|
836 | {
|
---|
837 | pPrev->pNextR3 = pNext;
|
---|
838 | pPrev->pNextR0 = pNext ? pNext->pSelfR0 : NIL_RTR0PTR;
|
---|
839 | pPrev->pNextRC = pNext ? pNext->pSelfRC : NIL_RTRCPTR;
|
---|
840 | }
|
---|
841 | else
|
---|
842 | {
|
---|
843 | Assert(pVM->pgm.s.pRamRangesXR3 == pRam);
|
---|
844 | pVM->pgm.s.pRamRangesXR3 = pNext;
|
---|
845 | pVM->pgm.s.pRamRangesXR0 = pNext ? pNext->pSelfR0 : NIL_RTR0PTR;
|
---|
846 | pVM->pgm.s.pRamRangesXRC = pNext ? pNext->pSelfRC : NIL_RTRCPTR;
|
---|
847 | }
|
---|
848 | ASMAtomicIncU32(&pVM->pgm.s.idRamRangesGen);
|
---|
849 |
|
---|
850 | #ifdef PGM_USE_RAMRANGE_SEARCH_TREES
|
---|
851 | pgmR3PhysRebuildRamRangeSearchTrees(pVM);
|
---|
852 | #endif
|
---|
853 | pgmUnlock(pVM);
|
---|
854 | }
|
---|
855 |
|
---|
856 |
|
---|
857 | /**
|
---|
858 | * Unlink an existing RAM range from the list.
|
---|
859 | *
|
---|
860 | * @param pVM Pointer to the shared VM structure.
|
---|
861 | * @param pRam Pointer to the new list entry.
|
---|
862 | */
|
---|
863 | static void pgmR3PhysUnlinkRamRange(PVM pVM, PPGMRAMRANGE pRam)
|
---|
864 | {
|
---|
865 | pgmLock(pVM);
|
---|
866 |
|
---|
867 | /* find prev. */
|
---|
868 | PPGMRAMRANGE pPrev = NULL;
|
---|
869 | PPGMRAMRANGE pCur = pVM->pgm.s.pRamRangesXR3;
|
---|
870 | while (pCur != pRam)
|
---|
871 | {
|
---|
872 | pPrev = pCur;
|
---|
873 | pCur = pCur->pNextR3;
|
---|
874 | }
|
---|
875 | AssertFatal(pCur);
|
---|
876 |
|
---|
877 | pgmR3PhysUnlinkRamRange2(pVM, pRam, pPrev);
|
---|
878 | pgmUnlock(pVM);
|
---|
879 | }
|
---|
880 |
|
---|
881 |
|
---|
882 | /**
|
---|
883 | * Frees a range of pages, replacing them with ZERO pages of the specified type.
|
---|
884 | *
|
---|
885 | * @returns VBox status code.
|
---|
886 | * @param pVM The VM handle.
|
---|
887 | * @param pRam The RAM range in which the pages resides.
|
---|
888 | * @param GCPhys The address of the first page.
|
---|
889 | * @param GCPhysLast The address of the last page.
|
---|
890 | * @param uType The page type to replace then with.
|
---|
891 | */
|
---|
892 | static int pgmR3PhysFreePageRange(PVM pVM, PPGMRAMRANGE pRam, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast, uint8_t uType)
|
---|
893 | {
|
---|
894 | Assert(PGMIsLockOwner(pVM));
|
---|
895 | uint32_t cPendingPages = 0;
|
---|
896 | PGMMFREEPAGESREQ pReq;
|
---|
897 | int rc = GMMR3FreePagesPrepare(pVM, &pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE, GMMACCOUNT_BASE);
|
---|
898 | AssertLogRelRCReturn(rc, rc);
|
---|
899 |
|
---|
900 | /* Iterate the pages. */
|
---|
901 | PPGMPAGE pPageDst = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
902 | uint32_t cPagesLeft = ((GCPhysLast - GCPhys) >> PAGE_SHIFT) + 1;
|
---|
903 | while (cPagesLeft-- > 0)
|
---|
904 | {
|
---|
905 | rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, pPageDst, GCPhys);
|
---|
906 | AssertLogRelRCReturn(rc, rc); /* We're done for if this goes wrong. */
|
---|
907 |
|
---|
908 | PGM_PAGE_SET_TYPE(pPageDst, uType);
|
---|
909 |
|
---|
910 | GCPhys += PAGE_SIZE;
|
---|
911 | pPageDst++;
|
---|
912 | }
|
---|
913 |
|
---|
914 | if (cPendingPages)
|
---|
915 | {
|
---|
916 | rc = GMMR3FreePagesPerform(pVM, pReq, cPendingPages);
|
---|
917 | AssertLogRelRCReturn(rc, rc);
|
---|
918 | }
|
---|
919 | GMMR3FreePagesCleanup(pReq);
|
---|
920 |
|
---|
921 | return rc;
|
---|
922 | }
|
---|
923 |
|
---|
924 | #if HC_ARCH_BITS == 64 && (defined(RT_OS_WINDOWS) || defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD))
|
---|
925 | /**
|
---|
926 | * Rendezvous callback used by PGMR3ChangeMemBalloon that changes the memory balloon size
|
---|
927 | *
|
---|
928 | * This is only called on one of the EMTs while the other ones are waiting for
|
---|
929 | * it to complete this function.
|
---|
930 | *
|
---|
931 | * @returns VINF_SUCCESS (VBox strict status code).
|
---|
932 | * @param pVM The VM handle.
|
---|
933 | * @param pVCpu The VMCPU for the EMT we're being called on. Unused.
|
---|
934 | * @param pvUser User parameter
|
---|
935 | */
|
---|
936 | static DECLCALLBACK(VBOXSTRICTRC) pgmR3PhysChangeMemBalloonRendezvous(PVM pVM, PVMCPU pVCpu, void *pvUser)
|
---|
937 | {
|
---|
938 | uintptr_t *paUser = (uintptr_t *)pvUser;
|
---|
939 | bool fInflate = !!paUser[0];
|
---|
940 | unsigned cPages = paUser[1];
|
---|
941 | RTGCPHYS *paPhysPage = (RTGCPHYS *)paUser[2];
|
---|
942 | uint32_t cPendingPages = 0;
|
---|
943 | PGMMFREEPAGESREQ pReq;
|
---|
944 | int rc;
|
---|
945 |
|
---|
946 | Log(("pgmR3PhysChangeMemBalloonRendezvous: %s %x pages\n", (fInflate) ? "inflate" : "deflate", cPages));
|
---|
947 | pgmLock(pVM);
|
---|
948 |
|
---|
949 | if (fInflate)
|
---|
950 | {
|
---|
951 | /* Flush the PGM pool cache as we might have stale references to pages that we just freed. */
|
---|
952 | pgmR3PoolClearAllRendezvous(pVM, pVCpu, NULL);
|
---|
953 |
|
---|
954 | /* Replace pages with ZERO pages. */
|
---|
955 | rc = GMMR3FreePagesPrepare(pVM, &pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE, GMMACCOUNT_BASE);
|
---|
956 | if (RT_FAILURE(rc))
|
---|
957 | {
|
---|
958 | pgmUnlock(pVM);
|
---|
959 | AssertLogRelRC(rc);
|
---|
960 | return rc;
|
---|
961 | }
|
---|
962 |
|
---|
963 | /* Iterate the pages. */
|
---|
964 | for (unsigned i = 0; i < cPages; i++)
|
---|
965 | {
|
---|
966 | PPGMPAGE pPage = pgmPhysGetPage(pVM, paPhysPage[i]);
|
---|
967 | if ( pPage == NULL
|
---|
968 | || PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_RAM)
|
---|
969 | {
|
---|
970 | Log(("pgmR3PhysChangeMemBalloonRendezvous: invalid physical page %RGp pPage->u3Type=%d\n", paPhysPage[i], pPage ? PGM_PAGE_GET_TYPE(pPage) : 0));
|
---|
971 | break;
|
---|
972 | }
|
---|
973 |
|
---|
974 | LogFlow(("balloon page: %RGp\n", paPhysPage[i]));
|
---|
975 |
|
---|
976 | /* Flush the shadow PT if this page was previously used as a guest page table. */
|
---|
977 | pgmPoolFlushPageByGCPhys(pVM, paPhysPage[i]);
|
---|
978 |
|
---|
979 | rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, pPage, paPhysPage[i]);
|
---|
980 | if (RT_FAILURE(rc))
|
---|
981 | {
|
---|
982 | pgmUnlock(pVM);
|
---|
983 | AssertLogRelRC(rc);
|
---|
984 | return rc;
|
---|
985 | }
|
---|
986 | Assert(PGM_PAGE_IS_ZERO(pPage));
|
---|
987 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_BALLOONED);
|
---|
988 | }
|
---|
989 |
|
---|
990 | if (cPendingPages)
|
---|
991 | {
|
---|
992 | rc = GMMR3FreePagesPerform(pVM, pReq, cPendingPages);
|
---|
993 | if (RT_FAILURE(rc))
|
---|
994 | {
|
---|
995 | pgmUnlock(pVM);
|
---|
996 | AssertLogRelRC(rc);
|
---|
997 | return rc;
|
---|
998 | }
|
---|
999 | }
|
---|
1000 | GMMR3FreePagesCleanup(pReq);
|
---|
1001 | }
|
---|
1002 | else
|
---|
1003 | {
|
---|
1004 | /* Iterate the pages. */
|
---|
1005 | for (unsigned i = 0; i < cPages; i++)
|
---|
1006 | {
|
---|
1007 | PPGMPAGE pPage = pgmPhysGetPage(pVM, paPhysPage[i]);
|
---|
1008 | AssertBreak(pPage && PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM);
|
---|
1009 |
|
---|
1010 | LogFlow(("Free ballooned page: %RGp\n", paPhysPage[i]));
|
---|
1011 |
|
---|
1012 | Assert(PGM_PAGE_IS_BALLOONED(pPage));
|
---|
1013 |
|
---|
1014 | /* Change back to zero page. */
|
---|
1015 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ZERO);
|
---|
1016 | }
|
---|
1017 |
|
---|
1018 | /* Note that we currently do not map any ballooned pages in our shadow page tables, so no need to flush the pgm pool. */
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | /* Notify GMM about the balloon change. */
|
---|
1022 | rc = GMMR3BalloonedPages(pVM, (fInflate) ? GMMBALLOONACTION_INFLATE : GMMBALLOONACTION_DEFLATE, cPages);
|
---|
1023 | if (RT_SUCCESS(rc))
|
---|
1024 | {
|
---|
1025 | if (!fInflate)
|
---|
1026 | {
|
---|
1027 | Assert(pVM->pgm.s.cBalloonedPages >= cPages);
|
---|
1028 | pVM->pgm.s.cBalloonedPages -= cPages;
|
---|
1029 | }
|
---|
1030 | else
|
---|
1031 | pVM->pgm.s.cBalloonedPages += cPages;
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | pgmUnlock(pVM);
|
---|
1035 |
|
---|
1036 | /* Flush the recompiler's TLB as well. */
|
---|
1037 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
1038 | CPUMSetChangedFlags(&pVM->aCpus[i], CPUM_CHANGED_GLOBAL_TLB_FLUSH);
|
---|
1039 |
|
---|
1040 | AssertLogRelRC(rc);
|
---|
1041 | return rc;
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | /**
|
---|
1045 | * Frees a range of ram pages, replacing them with ZERO pages; helper for PGMR3PhysFreeRamPages
|
---|
1046 | *
|
---|
1047 | * @returns VBox status code.
|
---|
1048 | * @param pVM The VM handle.
|
---|
1049 | * @param fInflate Inflate or deflate memory balloon
|
---|
1050 | * @param cPages Number of pages to free
|
---|
1051 | * @param paPhysPage Array of guest physical addresses
|
---|
1052 | */
|
---|
1053 | static DECLCALLBACK(void) pgmR3PhysChangeMemBalloonHelper(PVM pVM, bool fInflate, unsigned cPages, RTGCPHYS *paPhysPage)
|
---|
1054 | {
|
---|
1055 | uintptr_t paUser[3];
|
---|
1056 |
|
---|
1057 | paUser[0] = fInflate;
|
---|
1058 | paUser[1] = cPages;
|
---|
1059 | paUser[2] = (uintptr_t)paPhysPage;
|
---|
1060 | int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, pgmR3PhysChangeMemBalloonRendezvous, (void *)paUser);
|
---|
1061 | AssertRC(rc);
|
---|
1062 |
|
---|
1063 | /* Made a copy in PGMR3PhysFreeRamPages; free it here. */
|
---|
1064 | RTMemFree(paPhysPage);
|
---|
1065 | }
|
---|
1066 | #endif
|
---|
1067 |
|
---|
1068 | /**
|
---|
1069 | * Inflate or deflate a memory balloon
|
---|
1070 | *
|
---|
1071 | * @returns VBox status code.
|
---|
1072 | * @param pVM The VM handle.
|
---|
1073 | * @param fInflate Inflate or deflate memory balloon
|
---|
1074 | * @param cPages Number of pages to free
|
---|
1075 | * @param paPhysPage Array of guest physical addresses
|
---|
1076 | */
|
---|
1077 | VMMR3DECL(int) PGMR3PhysChangeMemBalloon(PVM pVM, bool fInflate, unsigned cPages, RTGCPHYS *paPhysPage)
|
---|
1078 | {
|
---|
1079 | /* This must match GMMR0Init; currently we only support memory ballooning on all 64-bit hosts except Mac OS X */
|
---|
1080 | #if HC_ARCH_BITS == 64 && (defined(RT_OS_WINDOWS) || defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD))
|
---|
1081 | int rc;
|
---|
1082 |
|
---|
1083 | /* Older additions (ancient non-functioning balloon code) pass wrong physical addresses. */
|
---|
1084 | AssertReturn(!(paPhysPage[0] & 0xfff), VERR_INVALID_PARAMETER);
|
---|
1085 |
|
---|
1086 | /* We own the IOM lock here and could cause a deadlock by waiting for another VCPU that is blocking on the IOM lock.
|
---|
1087 | * In the SMP case we post a request packet to postpone the job.
|
---|
1088 | */
|
---|
1089 | if (pVM->cCpus > 1)
|
---|
1090 | {
|
---|
1091 | unsigned cbPhysPage = cPages * sizeof(paPhysPage[0]);
|
---|
1092 | RTGCPHYS *paPhysPageCopy = (RTGCPHYS *)RTMemAlloc(cbPhysPage);
|
---|
1093 | AssertReturn(paPhysPageCopy, VERR_NO_MEMORY);
|
---|
1094 |
|
---|
1095 | memcpy(paPhysPageCopy, paPhysPage, cbPhysPage);
|
---|
1096 |
|
---|
1097 | rc = VMR3ReqCallNoWait(pVM, VMCPUID_ANY_QUEUE, (PFNRT)pgmR3PhysChangeMemBalloonHelper, 4, pVM, fInflate, cPages, paPhysPageCopy);
|
---|
1098 | AssertRC(rc);
|
---|
1099 | }
|
---|
1100 | else
|
---|
1101 | {
|
---|
1102 | uintptr_t paUser[3];
|
---|
1103 |
|
---|
1104 | paUser[0] = fInflate;
|
---|
1105 | paUser[1] = cPages;
|
---|
1106 | paUser[2] = (uintptr_t)paPhysPage;
|
---|
1107 | rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, pgmR3PhysChangeMemBalloonRendezvous, (void *)paUser);
|
---|
1108 | AssertRC(rc);
|
---|
1109 | }
|
---|
1110 | return rc;
|
---|
1111 | #else
|
---|
1112 | return VERR_NOT_IMPLEMENTED;
|
---|
1113 | #endif
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | /**
|
---|
1117 | * Rendezvous callback used by PGMR3WriteProtectRAM that write protects all
|
---|
1118 | * physical RAM.
|
---|
1119 | *
|
---|
1120 | * This is only called on one of the EMTs while the other ones are waiting for
|
---|
1121 | * it to complete this function.
|
---|
1122 | *
|
---|
1123 | * @returns VINF_SUCCESS (VBox strict status code).
|
---|
1124 | * @param pVM The VM handle.
|
---|
1125 | * @param pVCpu The VMCPU for the EMT we're being called on. Unused.
|
---|
1126 | * @param pvUser User parameter, unused.
|
---|
1127 | */
|
---|
1128 | static DECLCALLBACK(VBOXSTRICTRC) pgmR3PhysWriteProtectRAMRendezvous(PVM pVM, PVMCPU pVCpu, void *pvUser)
|
---|
1129 | {
|
---|
1130 | int rc = VINF_SUCCESS;
|
---|
1131 | NOREF(pvUser);
|
---|
1132 |
|
---|
1133 | pgmLock(pVM);
|
---|
1134 | #ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
|
---|
1135 | pgmPoolResetDirtyPages(pVM);
|
---|
1136 | #endif
|
---|
1137 |
|
---|
1138 | /** @todo pointless to write protect the physical page pointed to by RSP. */
|
---|
1139 |
|
---|
1140 | for (PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRangesX);
|
---|
1141 | pRam;
|
---|
1142 | pRam = pRam->CTX_SUFF(pNext))
|
---|
1143 | {
|
---|
1144 | uint32_t cPages = pRam->cb >> PAGE_SHIFT;
|
---|
1145 | for (uint32_t iPage = 0; iPage < cPages; iPage++)
|
---|
1146 | {
|
---|
1147 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
1148 | PGMPAGETYPE enmPageType = (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage);
|
---|
1149 |
|
---|
1150 | if ( RT_LIKELY(enmPageType == PGMPAGETYPE_RAM)
|
---|
1151 | || enmPageType == PGMPAGETYPE_MMIO2)
|
---|
1152 | {
|
---|
1153 | /*
|
---|
1154 | * A RAM page.
|
---|
1155 | */
|
---|
1156 | switch (PGM_PAGE_GET_STATE(pPage))
|
---|
1157 | {
|
---|
1158 | case PGM_PAGE_STATE_ALLOCATED:
|
---|
1159 | /** @todo Optimize this: Don't always re-enable write
|
---|
1160 | * monitoring if the page is known to be very busy. */
|
---|
1161 | if (PGM_PAGE_IS_WRITTEN_TO(pPage))
|
---|
1162 | {
|
---|
1163 | PGM_PAGE_CLEAR_WRITTEN_TO(pPage);
|
---|
1164 | /* Remember this dirty page for the next (memory) sync. */
|
---|
1165 | PGM_PAGE_SET_FT_DIRTY(pPage);
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 | pgmPhysPageWriteMonitor(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT));
|
---|
1169 | break;
|
---|
1170 |
|
---|
1171 | case PGM_PAGE_STATE_SHARED:
|
---|
1172 | AssertFailed();
|
---|
1173 | break;
|
---|
1174 |
|
---|
1175 | case PGM_PAGE_STATE_WRITE_MONITORED: /* nothing to change. */
|
---|
1176 | default:
|
---|
1177 | break;
|
---|
1178 | }
|
---|
1179 | }
|
---|
1180 | }
|
---|
1181 | }
|
---|
1182 | pgmR3PoolWriteProtectPages(pVM);
|
---|
1183 | PGM_INVL_ALL_VCPU_TLBS(pVM);
|
---|
1184 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
1185 | CPUMSetChangedFlags(&pVM->aCpus[idCpu], CPUM_CHANGED_GLOBAL_TLB_FLUSH);
|
---|
1186 |
|
---|
1187 | pgmUnlock(pVM);
|
---|
1188 | return rc;
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 | /**
|
---|
1192 | * Protect all physical RAM to monitor writes
|
---|
1193 | *
|
---|
1194 | * @returns VBox status code.
|
---|
1195 | * @param pVM The VM handle.
|
---|
1196 | */
|
---|
1197 | VMMR3DECL(int) PGMR3PhysWriteProtectRAM(PVM pVM)
|
---|
1198 | {
|
---|
1199 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
1200 |
|
---|
1201 | int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, pgmR3PhysWriteProtectRAMRendezvous, NULL);
|
---|
1202 | AssertRC(rc);
|
---|
1203 | return rc;
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 | /**
|
---|
1207 | * Enumerate all dirty FT pages.
|
---|
1208 | *
|
---|
1209 | * @returns VBox status code.
|
---|
1210 | * @param pVM The VM handle.
|
---|
1211 | * @param pfnEnum Enumerate callback handler.
|
---|
1212 | * @param pvUser Enumerate callback handler parameter.
|
---|
1213 | */
|
---|
1214 | VMMR3DECL(int) PGMR3PhysEnumDirtyFTPages(PVM pVM, PFNPGMENUMDIRTYFTPAGES pfnEnum, void *pvUser)
|
---|
1215 | {
|
---|
1216 | int rc = VINF_SUCCESS;
|
---|
1217 |
|
---|
1218 | pgmLock(pVM);
|
---|
1219 | for (PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRangesX);
|
---|
1220 | pRam;
|
---|
1221 | pRam = pRam->CTX_SUFF(pNext))
|
---|
1222 | {
|
---|
1223 | uint32_t cPages = pRam->cb >> PAGE_SHIFT;
|
---|
1224 | for (uint32_t iPage = 0; iPage < cPages; iPage++)
|
---|
1225 | {
|
---|
1226 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
1227 | PGMPAGETYPE enmPageType = (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage);
|
---|
1228 |
|
---|
1229 | if ( RT_LIKELY(enmPageType == PGMPAGETYPE_RAM)
|
---|
1230 | || enmPageType == PGMPAGETYPE_MMIO2)
|
---|
1231 | {
|
---|
1232 | /*
|
---|
1233 | * A RAM page.
|
---|
1234 | */
|
---|
1235 | switch (PGM_PAGE_GET_STATE(pPage))
|
---|
1236 | {
|
---|
1237 | case PGM_PAGE_STATE_ALLOCATED:
|
---|
1238 | case PGM_PAGE_STATE_WRITE_MONITORED:
|
---|
1239 | if ( !PGM_PAGE_IS_WRITTEN_TO(pPage) /* not very recently updated? */
|
---|
1240 | && PGM_PAGE_IS_FT_DIRTY(pPage))
|
---|
1241 | {
|
---|
1242 | unsigned cbPageRange = PAGE_SIZE;
|
---|
1243 | unsigned iPageClean = iPage + 1;
|
---|
1244 | RTGCPHYS GCPhysPage = pRam->GCPhys + iPage * PAGE_SIZE;
|
---|
1245 | uint8_t *pu8Page = NULL;
|
---|
1246 | PGMPAGEMAPLOCK Lock;
|
---|
1247 |
|
---|
1248 | /* Find the next clean page, so we can merge adjacent dirty pages. */
|
---|
1249 | for (; iPageClean < cPages; iPageClean++)
|
---|
1250 | {
|
---|
1251 | PPGMPAGE pPageNext = &pRam->aPages[iPageClean];
|
---|
1252 | if ( RT_UNLIKELY(PGM_PAGE_GET_TYPE(pPageNext) != PGMPAGETYPE_RAM)
|
---|
1253 | || PGM_PAGE_GET_STATE(pPageNext) != PGM_PAGE_STATE_ALLOCATED
|
---|
1254 | || PGM_PAGE_IS_WRITTEN_TO(pPageNext)
|
---|
1255 | || !PGM_PAGE_IS_FT_DIRTY(pPageNext)
|
---|
1256 | /* Crossing a chunk boundary? */
|
---|
1257 | || (GCPhysPage & GMM_PAGEID_IDX_MASK) != ((GCPhysPage + cbPageRange) & GMM_PAGEID_IDX_MASK)
|
---|
1258 | )
|
---|
1259 | break;
|
---|
1260 |
|
---|
1261 | cbPageRange += PAGE_SIZE;
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 | rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhysPage, (const void **)&pu8Page, &Lock);
|
---|
1265 | if (RT_SUCCESS(rc))
|
---|
1266 | {
|
---|
1267 | /** @todo this is risky; the range might be changed, but little choice as the sync
|
---|
1268 | * costs a lot of time. */
|
---|
1269 | pgmUnlock(pVM);
|
---|
1270 | pfnEnum(pVM, GCPhysPage, pu8Page, cbPageRange, pvUser);
|
---|
1271 | pgmLock(pVM);
|
---|
1272 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
1273 | }
|
---|
1274 |
|
---|
1275 | for (iPage; iPage < iPageClean; iPage++)
|
---|
1276 | PGM_PAGE_CLEAR_FT_DIRTY(&pRam->aPages[iPage]);
|
---|
1277 |
|
---|
1278 | iPage = iPageClean - 1;
|
---|
1279 | }
|
---|
1280 | break;
|
---|
1281 | }
|
---|
1282 | }
|
---|
1283 | }
|
---|
1284 | }
|
---|
1285 | pgmUnlock(pVM);
|
---|
1286 | return rc;
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 |
|
---|
1290 | /**
|
---|
1291 | * Gets the number of ram ranges.
|
---|
1292 | *
|
---|
1293 | * @returns Number of ram ranges. Returns UINT32_MAX if @a pVM is invalid.
|
---|
1294 | * @param pVM The VM handle.
|
---|
1295 | */
|
---|
1296 | VMMR3DECL(uint32_t) PGMR3PhysGetRamRangeCount(PVM pVM)
|
---|
1297 | {
|
---|
1298 | VM_ASSERT_VALID_EXT_RETURN(pVM, UINT32_MAX);
|
---|
1299 |
|
---|
1300 | pgmLock(pVM);
|
---|
1301 | uint32_t cRamRanges = 0;
|
---|
1302 | for (PPGMRAMRANGE pCur = pVM->pgm.s.CTX_SUFF(pRamRangesX); pCur; pCur = pCur->CTX_SUFF(pNext))
|
---|
1303 | cRamRanges++;
|
---|
1304 | pgmUnlock(pVM);
|
---|
1305 | return cRamRanges;
|
---|
1306 | }
|
---|
1307 |
|
---|
1308 |
|
---|
1309 | /**
|
---|
1310 | * Get information about a range.
|
---|
1311 | *
|
---|
1312 | * @returns VINF_SUCCESS or VERR_OUT_OF_RANGE.
|
---|
1313 | * @param pVM The VM handle
|
---|
1314 | * @param iRange The ordinal of the range.
|
---|
1315 | * @param pGCPhysStart Where to return the start of the range. Optional.
|
---|
1316 | * @param pGCPhysLast Where to return the address of the last byte in the
|
---|
1317 | * range. Optional.
|
---|
1318 | * @param pfIsMmio Where to indicate that this is a pure MMIO range.
|
---|
1319 | * Optional.
|
---|
1320 | */
|
---|
1321 | VMMR3DECL(int) PGMR3PhysGetRange(PVM pVM, uint32_t iRange, PRTGCPHYS pGCPhysStart, PRTGCPHYS pGCPhysLast,
|
---|
1322 | const char **ppszDesc, bool *pfIsMmio)
|
---|
1323 | {
|
---|
1324 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
1325 |
|
---|
1326 | pgmLock(pVM);
|
---|
1327 | uint32_t iCurRange = 0;
|
---|
1328 | for (PPGMRAMRANGE pCur = pVM->pgm.s.CTX_SUFF(pRamRangesX); pCur; pCur = pCur->CTX_SUFF(pNext), iCurRange++)
|
---|
1329 | if (iCurRange == iRange)
|
---|
1330 | {
|
---|
1331 | if (pGCPhysStart)
|
---|
1332 | *pGCPhysStart = pCur->GCPhys;
|
---|
1333 | if (pGCPhysLast)
|
---|
1334 | *pGCPhysLast = pCur->GCPhysLast;
|
---|
1335 | if (pfIsMmio)
|
---|
1336 | *pfIsMmio = !!(pCur->fFlags & PGM_RAM_RANGE_FLAGS_AD_HOC_MMIO);
|
---|
1337 |
|
---|
1338 | pgmUnlock(pVM);
|
---|
1339 | return VINF_SUCCESS;
|
---|
1340 | }
|
---|
1341 | pgmUnlock(pVM);
|
---|
1342 | return VERR_OUT_OF_RANGE;
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 |
|
---|
1346 | /**
|
---|
1347 | * Query the amount of free memory inside VMMR0
|
---|
1348 | *
|
---|
1349 | * @returns VBox status code.
|
---|
1350 | * @param pVM The VM handle.
|
---|
1351 | * @param pcbAllocMem Where to return the amount of memory allocated
|
---|
1352 | * by VMs.
|
---|
1353 | * @param pcbFreeMem Where to return the amount of memory that is
|
---|
1354 | * allocated from the host but not currently used
|
---|
1355 | * by any VMs.
|
---|
1356 | * @param pcbBallonedMem Where to return the sum of memory that is
|
---|
1357 | * currently ballooned by the VMs.
|
---|
1358 | * @param pcbSharedMem Where to return the amount of memory that is
|
---|
1359 | * currently shared.
|
---|
1360 | */
|
---|
1361 | VMMR3DECL(int) PGMR3QueryGlobalMemoryStats(PVM pVM, uint64_t *pcbAllocMem, uint64_t *pcbFreeMem,
|
---|
1362 | uint64_t *pcbBallonedMem, uint64_t *pcbSharedMem)
|
---|
1363 | {
|
---|
1364 | uint64_t cAllocPages = 0;
|
---|
1365 | uint64_t cFreePages = 0;
|
---|
1366 | uint64_t cBalloonPages = 0;
|
---|
1367 | uint64_t cSharedPages = 0;
|
---|
1368 | int rc = GMMR3QueryHypervisorMemoryStats(pVM, &cAllocPages, &cFreePages, &cBalloonPages, &cSharedPages);
|
---|
1369 | AssertRCReturn(rc, rc);
|
---|
1370 |
|
---|
1371 | if (pcbAllocMem)
|
---|
1372 | *pcbAllocMem = cAllocPages * _4K;
|
---|
1373 |
|
---|
1374 | if (pcbFreeMem)
|
---|
1375 | *pcbFreeMem = cFreePages * _4K;
|
---|
1376 |
|
---|
1377 | if (pcbBallonedMem)
|
---|
1378 | *pcbBallonedMem = cBalloonPages * _4K;
|
---|
1379 |
|
---|
1380 | if (pcbSharedMem)
|
---|
1381 | *pcbSharedMem = cSharedPages * _4K;
|
---|
1382 |
|
---|
1383 | Log(("PGMR3QueryVMMMemoryStats: all=%llx free=%llx ballooned=%llx shared=%llx\n",
|
---|
1384 | cAllocPages, cFreePages, cBalloonPages, cSharedPages));
|
---|
1385 | return VINF_SUCCESS;
|
---|
1386 | }
|
---|
1387 |
|
---|
1388 |
|
---|
1389 | /**
|
---|
1390 | * Query memory stats for the VM.
|
---|
1391 | *
|
---|
1392 | * @returns VBox status code.
|
---|
1393 | * @param pVM The VM handle.
|
---|
1394 | * @param pcbTotalMem Where to return total amount memory the VM may
|
---|
1395 | * possibly use.
|
---|
1396 | * @param pcbPrivateMem Where to return the amount of private memory
|
---|
1397 | * currently allocated.
|
---|
1398 | * @param pcbSharedMem Where to return the amount of actually shared
|
---|
1399 | * memory currently used by the VM.
|
---|
1400 | * @param pcbZeroMem Where to return the amount of memory backed by
|
---|
1401 | * zero pages.
|
---|
1402 | *
|
---|
1403 | * @remarks The total mem is normally larger than the sum of the three
|
---|
1404 | * components. There are two reasons for this, first the amount of
|
---|
1405 | * shared memory is what we're sure is shared instead of what could
|
---|
1406 | * possibly be shared with someone. Secondly, because the total may
|
---|
1407 | * include some pure MMIO pages that doesn't go into any of the three
|
---|
1408 | * sub-counts.
|
---|
1409 | *
|
---|
1410 | * @todo Why do we return reused shared pages instead of anything that could
|
---|
1411 | * potentially be shared? Doesn't this mean the first VM gets a much
|
---|
1412 | * lower number of shared pages?
|
---|
1413 | */
|
---|
1414 | VMMR3DECL(int) PGMR3QueryMemoryStats(PVM pVM, uint64_t *pcbTotalMem, uint64_t *pcbPrivateMem,
|
---|
1415 | uint64_t *pcbSharedMem, uint64_t *pcbZeroMem)
|
---|
1416 | {
|
---|
1417 | VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
|
---|
1418 |
|
---|
1419 | if (pcbTotalMem)
|
---|
1420 | *pcbTotalMem = (uint64_t)pVM->pgm.s.cAllPages * PAGE_SIZE;
|
---|
1421 |
|
---|
1422 | if (pcbPrivateMem)
|
---|
1423 | *pcbPrivateMem = (uint64_t)pVM->pgm.s.cPrivatePages * PAGE_SIZE;
|
---|
1424 |
|
---|
1425 | if (pcbSharedMem)
|
---|
1426 | *pcbSharedMem = (uint64_t)pVM->pgm.s.cReusedSharedPages * PAGE_SIZE;
|
---|
1427 |
|
---|
1428 | if (pcbZeroMem)
|
---|
1429 | *pcbZeroMem = (uint64_t)pVM->pgm.s.cZeroPages * PAGE_SIZE;
|
---|
1430 |
|
---|
1431 | Log(("PGMR3QueryMemoryStats: all=%x private=%x reused=%x zero=%x\n", pVM->pgm.s.cAllPages, pVM->pgm.s.cPrivatePages, pVM->pgm.s.cReusedSharedPages, pVM->pgm.s.cZeroPages));
|
---|
1432 | return VINF_SUCCESS;
|
---|
1433 | }
|
---|
1434 |
|
---|
1435 |
|
---|
1436 | /**
|
---|
1437 | * PGMR3PhysRegisterRam worker that initializes and links a RAM range.
|
---|
1438 | *
|
---|
1439 | * @param pVM The VM handle.
|
---|
1440 | * @param pNew The new RAM range.
|
---|
1441 | * @param GCPhys The address of the RAM range.
|
---|
1442 | * @param GCPhysLast The last address of the RAM range.
|
---|
1443 | * @param RCPtrNew The RC address if the range is floating. NIL_RTRCPTR
|
---|
1444 | * if in HMA.
|
---|
1445 | * @param R0PtrNew Ditto for R0.
|
---|
1446 | * @param pszDesc The description.
|
---|
1447 | * @param pPrev The previous RAM range (for linking).
|
---|
1448 | */
|
---|
1449 | static void pgmR3PhysInitAndLinkRamRange(PVM pVM, PPGMRAMRANGE pNew, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast,
|
---|
1450 | RTRCPTR RCPtrNew, RTR0PTR R0PtrNew, const char *pszDesc, PPGMRAMRANGE pPrev)
|
---|
1451 | {
|
---|
1452 | /*
|
---|
1453 | * Initialize the range.
|
---|
1454 | */
|
---|
1455 | pNew->pSelfR0 = R0PtrNew != NIL_RTR0PTR ? R0PtrNew : MMHyperCCToR0(pVM, pNew);
|
---|
1456 | pNew->pSelfRC = RCPtrNew != NIL_RTRCPTR ? RCPtrNew : MMHyperCCToRC(pVM, pNew);
|
---|
1457 | pNew->GCPhys = GCPhys;
|
---|
1458 | pNew->GCPhysLast = GCPhysLast;
|
---|
1459 | pNew->cb = GCPhysLast - GCPhys + 1;
|
---|
1460 | pNew->pszDesc = pszDesc;
|
---|
1461 | pNew->fFlags = RCPtrNew != NIL_RTRCPTR ? PGM_RAM_RANGE_FLAGS_FLOATING : 0;
|
---|
1462 | pNew->pvR3 = NULL;
|
---|
1463 | pNew->paLSPages = NULL;
|
---|
1464 |
|
---|
1465 | uint32_t const cPages = pNew->cb >> PAGE_SHIFT;
|
---|
1466 | RTGCPHYS iPage = cPages;
|
---|
1467 | while (iPage-- > 0)
|
---|
1468 | PGM_PAGE_INIT_ZERO(&pNew->aPages[iPage], pVM, PGMPAGETYPE_RAM);
|
---|
1469 |
|
---|
1470 | /* Update the page count stats. */
|
---|
1471 | pVM->pgm.s.cZeroPages += cPages;
|
---|
1472 | pVM->pgm.s.cAllPages += cPages;
|
---|
1473 |
|
---|
1474 | /*
|
---|
1475 | * Link it.
|
---|
1476 | */
|
---|
1477 | pgmR3PhysLinkRamRange(pVM, pNew, pPrev);
|
---|
1478 | }
|
---|
1479 |
|
---|
1480 |
|
---|
1481 | /**
|
---|
1482 | * Relocate a floating RAM range.
|
---|
1483 | *
|
---|
1484 | * @copydoc FNPGMRELOCATE.
|
---|
1485 | */
|
---|
1486 | static DECLCALLBACK(bool) pgmR3PhysRamRangeRelocate(PVM pVM, RTGCPTR GCPtrOld, RTGCPTR GCPtrNew, PGMRELOCATECALL enmMode, void *pvUser)
|
---|
1487 | {
|
---|
1488 | PPGMRAMRANGE pRam = (PPGMRAMRANGE)pvUser;
|
---|
1489 | Assert(pRam->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING);
|
---|
1490 | Assert(pRam->pSelfRC == GCPtrOld + PAGE_SIZE);
|
---|
1491 |
|
---|
1492 | switch (enmMode)
|
---|
1493 | {
|
---|
1494 | case PGMRELOCATECALL_SUGGEST:
|
---|
1495 | return true;
|
---|
1496 |
|
---|
1497 | case PGMRELOCATECALL_RELOCATE:
|
---|
1498 | {
|
---|
1499 | /*
|
---|
1500 | * Update myself, then relink all the ranges and flush the RC TLB.
|
---|
1501 | */
|
---|
1502 | pgmLock(pVM);
|
---|
1503 |
|
---|
1504 | pRam->pSelfRC = (RTRCPTR)(GCPtrNew + PAGE_SIZE);
|
---|
1505 |
|
---|
1506 | pgmR3PhysRelinkRamRanges(pVM);
|
---|
1507 | for (unsigned i = 0; i < PGM_RAMRANGE_TLB_ENTRIES; i++)
|
---|
1508 | pVM->pgm.s.apRamRangesTlbRC[i] = NIL_RTRCPTR;
|
---|
1509 |
|
---|
1510 | pgmUnlock(pVM);
|
---|
1511 | return true;
|
---|
1512 | }
|
---|
1513 |
|
---|
1514 | default:
|
---|
1515 | AssertFailedReturn(false);
|
---|
1516 | }
|
---|
1517 | }
|
---|
1518 |
|
---|
1519 |
|
---|
1520 | /**
|
---|
1521 | * PGMR3PhysRegisterRam worker that registers a high chunk.
|
---|
1522 | *
|
---|
1523 | * @returns VBox status code.
|
---|
1524 | * @param pVM The VM handle.
|
---|
1525 | * @param GCPhys The address of the RAM.
|
---|
1526 | * @param cRamPages The number of RAM pages to register.
|
---|
1527 | * @param cbChunk The size of the PGMRAMRANGE guest mapping.
|
---|
1528 | * @param iChunk The chunk number.
|
---|
1529 | * @param pszDesc The RAM range description.
|
---|
1530 | * @param ppPrev Previous RAM range pointer. In/Out.
|
---|
1531 | */
|
---|
1532 | static int pgmR3PhysRegisterHighRamChunk(PVM pVM, RTGCPHYS GCPhys, uint32_t cRamPages,
|
---|
1533 | uint32_t cbChunk, uint32_t iChunk, const char *pszDesc,
|
---|
1534 | PPGMRAMRANGE *ppPrev)
|
---|
1535 | {
|
---|
1536 | const char *pszDescChunk = iChunk == 0
|
---|
1537 | ? pszDesc
|
---|
1538 | : MMR3HeapAPrintf(pVM, MM_TAG_PGM_PHYS, "%s (#%u)", pszDesc, iChunk + 1);
|
---|
1539 | AssertReturn(pszDescChunk, VERR_NO_MEMORY);
|
---|
1540 |
|
---|
1541 | /*
|
---|
1542 | * Allocate memory for the new chunk.
|
---|
1543 | */
|
---|
1544 | size_t const cChunkPages = RT_ALIGN_Z(RT_UOFFSETOF(PGMRAMRANGE, aPages[cRamPages]), PAGE_SIZE) >> PAGE_SHIFT;
|
---|
1545 | PSUPPAGE paChunkPages = (PSUPPAGE)RTMemTmpAllocZ(sizeof(SUPPAGE) * cChunkPages);
|
---|
1546 | AssertReturn(paChunkPages, VERR_NO_TMP_MEMORY);
|
---|
1547 | RTR0PTR R0PtrChunk = NIL_RTR0PTR;
|
---|
1548 | void *pvChunk = NULL;
|
---|
1549 | int rc = SUPR3PageAllocEx(cChunkPages, 0 /*fFlags*/, &pvChunk,
|
---|
1550 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
1551 | VMMIsHwVirtExtForced(pVM) ? &R0PtrChunk : NULL,
|
---|
1552 | #else
|
---|
1553 | NULL,
|
---|
1554 | #endif
|
---|
1555 | paChunkPages);
|
---|
1556 | if (RT_SUCCESS(rc))
|
---|
1557 | {
|
---|
1558 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
1559 | if (!VMMIsHwVirtExtForced(pVM))
|
---|
1560 | R0PtrChunk = NIL_RTR0PTR;
|
---|
1561 | #else
|
---|
1562 | R0PtrChunk = (uintptr_t)pvChunk;
|
---|
1563 | #endif
|
---|
1564 | memset(pvChunk, 0, cChunkPages << PAGE_SHIFT);
|
---|
1565 |
|
---|
1566 | PPGMRAMRANGE pNew = (PPGMRAMRANGE)pvChunk;
|
---|
1567 |
|
---|
1568 | /*
|
---|
1569 | * Create a mapping and map the pages into it.
|
---|
1570 | * We push these in below the HMA.
|
---|
1571 | */
|
---|
1572 | RTGCPTR GCPtrChunkMap = pVM->pgm.s.GCPtrPrevRamRangeMapping - cbChunk;
|
---|
1573 | rc = PGMR3MapPT(pVM, GCPtrChunkMap, cbChunk, 0 /*fFlags*/, pgmR3PhysRamRangeRelocate, pNew, pszDescChunk);
|
---|
1574 | if (RT_SUCCESS(rc))
|
---|
1575 | {
|
---|
1576 | pVM->pgm.s.GCPtrPrevRamRangeMapping = GCPtrChunkMap;
|
---|
1577 |
|
---|
1578 | RTGCPTR const GCPtrChunk = GCPtrChunkMap + PAGE_SIZE;
|
---|
1579 | RTGCPTR GCPtrPage = GCPtrChunk;
|
---|
1580 | for (uint32_t iPage = 0; iPage < cChunkPages && RT_SUCCESS(rc); iPage++, GCPtrPage += PAGE_SIZE)
|
---|
1581 | rc = PGMMap(pVM, GCPtrPage, paChunkPages[iPage].Phys, PAGE_SIZE, 0);
|
---|
1582 | if (RT_SUCCESS(rc))
|
---|
1583 | {
|
---|
1584 | /*
|
---|
1585 | * Ok, init and link the range.
|
---|
1586 | */
|
---|
1587 | pgmR3PhysInitAndLinkRamRange(pVM, pNew, GCPhys, GCPhys + ((RTGCPHYS)cRamPages << PAGE_SHIFT) - 1,
|
---|
1588 | (RTRCPTR)GCPtrChunk, R0PtrChunk, pszDescChunk, *ppPrev);
|
---|
1589 | *ppPrev = pNew;
|
---|
1590 | }
|
---|
1591 | }
|
---|
1592 |
|
---|
1593 | if (RT_FAILURE(rc))
|
---|
1594 | SUPR3PageFreeEx(pvChunk, cChunkPages);
|
---|
1595 | }
|
---|
1596 |
|
---|
1597 | RTMemTmpFree(paChunkPages);
|
---|
1598 | return rc;
|
---|
1599 | }
|
---|
1600 |
|
---|
1601 |
|
---|
1602 | /**
|
---|
1603 | * Sets up a range RAM.
|
---|
1604 | *
|
---|
1605 | * This will check for conflicting registrations, make a resource
|
---|
1606 | * reservation for the memory (with GMM), and setup the per-page
|
---|
1607 | * tracking structures (PGMPAGE).
|
---|
1608 | *
|
---|
1609 | * @returns VBox status code.
|
---|
1610 | * @param pVM Pointer to the shared VM structure.
|
---|
1611 | * @param GCPhys The physical address of the RAM.
|
---|
1612 | * @param cb The size of the RAM.
|
---|
1613 | * @param pszDesc The description - not copied, so, don't free or change it.
|
---|
1614 | */
|
---|
1615 | VMMR3DECL(int) PGMR3PhysRegisterRam(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, const char *pszDesc)
|
---|
1616 | {
|
---|
1617 | /*
|
---|
1618 | * Validate input.
|
---|
1619 | */
|
---|
1620 | Log(("PGMR3PhysRegisterRam: GCPhys=%RGp cb=%RGp pszDesc=%s\n", GCPhys, cb, pszDesc));
|
---|
1621 | AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
|
---|
1622 | AssertReturn(RT_ALIGN_T(cb, PAGE_SIZE, RTGCPHYS) == cb, VERR_INVALID_PARAMETER);
|
---|
1623 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
1624 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
1625 | AssertMsgReturn(GCPhysLast > GCPhys, ("The range wraps! GCPhys=%RGp cb=%RGp\n", GCPhys, cb), VERR_INVALID_PARAMETER);
|
---|
1626 | AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
|
---|
1627 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
1628 |
|
---|
1629 | pgmLock(pVM);
|
---|
1630 |
|
---|
1631 | /*
|
---|
1632 | * Find range location and check for conflicts.
|
---|
1633 | * (We don't lock here because the locking by EMT is only required on update.)
|
---|
1634 | */
|
---|
1635 | PPGMRAMRANGE pPrev = NULL;
|
---|
1636 | PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesXR3;
|
---|
1637 | while (pRam && GCPhysLast >= pRam->GCPhys)
|
---|
1638 | {
|
---|
1639 | if ( GCPhysLast >= pRam->GCPhys
|
---|
1640 | && GCPhys <= pRam->GCPhysLast)
|
---|
1641 | AssertLogRelMsgFailedReturn(("%RGp-%RGp (%s) conflicts with existing %RGp-%RGp (%s)\n",
|
---|
1642 | GCPhys, GCPhysLast, pszDesc,
|
---|
1643 | pRam->GCPhys, pRam->GCPhysLast, pRam->pszDesc),
|
---|
1644 | VERR_PGM_RAM_CONFLICT);
|
---|
1645 |
|
---|
1646 | /* next */
|
---|
1647 | pPrev = pRam;
|
---|
1648 | pRam = pRam->pNextR3;
|
---|
1649 | }
|
---|
1650 |
|
---|
1651 | /*
|
---|
1652 | * Register it with GMM (the API bitches).
|
---|
1653 | */
|
---|
1654 | const RTGCPHYS cPages = cb >> PAGE_SHIFT;
|
---|
1655 | int rc = MMR3IncreaseBaseReservation(pVM, cPages);
|
---|
1656 | if (RT_FAILURE(rc))
|
---|
1657 | {
|
---|
1658 | pgmUnlock(pVM);
|
---|
1659 | return rc;
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | if ( GCPhys >= _4G
|
---|
1663 | && cPages > 256)
|
---|
1664 | {
|
---|
1665 | /*
|
---|
1666 | * The PGMRAMRANGE structures for the high memory can get very big.
|
---|
1667 | * In order to avoid SUPR3PageAllocEx allocation failures due to the
|
---|
1668 | * allocation size limit there and also to avoid being unable to find
|
---|
1669 | * guest mapping space for them, we split this memory up into 4MB in
|
---|
1670 | * (potential) raw-mode configs and 16MB chunks in forced AMD-V/VT-x
|
---|
1671 | * mode.
|
---|
1672 | *
|
---|
1673 | * The first and last page of each mapping are guard pages and marked
|
---|
1674 | * not-present. So, we've got 4186112 and 16769024 bytes available for
|
---|
1675 | * the PGMRAMRANGE structure.
|
---|
1676 | *
|
---|
1677 | * Note! The sizes used here will influence the saved state.
|
---|
1678 | */
|
---|
1679 | uint32_t cbChunk;
|
---|
1680 | uint32_t cPagesPerChunk;
|
---|
1681 | if (VMMIsHwVirtExtForced(pVM))
|
---|
1682 | {
|
---|
1683 | cbChunk = 16U*_1M;
|
---|
1684 | cPagesPerChunk = 1048048; /* max ~1048059 */
|
---|
1685 | AssertCompile(sizeof(PGMRAMRANGE) + sizeof(PGMPAGE) * 1048048 < 16U*_1M - PAGE_SIZE * 2);
|
---|
1686 | }
|
---|
1687 | else
|
---|
1688 | {
|
---|
1689 | cbChunk = 4U*_1M;
|
---|
1690 | cPagesPerChunk = 261616; /* max ~261627 */
|
---|
1691 | AssertCompile(sizeof(PGMRAMRANGE) + sizeof(PGMPAGE) * 261616 < 4U*_1M - PAGE_SIZE * 2);
|
---|
1692 | }
|
---|
1693 | AssertRelease(RT_UOFFSETOF(PGMRAMRANGE, aPages[cPagesPerChunk]) + PAGE_SIZE * 2 <= cbChunk);
|
---|
1694 |
|
---|
1695 | RTGCPHYS cPagesLeft = cPages;
|
---|
1696 | RTGCPHYS GCPhysChunk = GCPhys;
|
---|
1697 | uint32_t iChunk = 0;
|
---|
1698 | while (cPagesLeft > 0)
|
---|
1699 | {
|
---|
1700 | uint32_t cPagesInChunk = cPagesLeft;
|
---|
1701 | if (cPagesInChunk > cPagesPerChunk)
|
---|
1702 | cPagesInChunk = cPagesPerChunk;
|
---|
1703 |
|
---|
1704 | rc = pgmR3PhysRegisterHighRamChunk(pVM, GCPhysChunk, cPagesInChunk, cbChunk, iChunk, pszDesc, &pPrev);
|
---|
1705 | AssertRCReturn(rc, rc);
|
---|
1706 |
|
---|
1707 | /* advance */
|
---|
1708 | GCPhysChunk += (RTGCPHYS)cPagesInChunk << PAGE_SHIFT;
|
---|
1709 | cPagesLeft -= cPagesInChunk;
|
---|
1710 | iChunk++;
|
---|
1711 | }
|
---|
1712 | }
|
---|
1713 | else
|
---|
1714 | {
|
---|
1715 | /*
|
---|
1716 | * Allocate, initialize and link the new RAM range.
|
---|
1717 | */
|
---|
1718 | const size_t cbRamRange = RT_OFFSETOF(PGMRAMRANGE, aPages[cPages]);
|
---|
1719 | PPGMRAMRANGE pNew;
|
---|
1720 | rc = MMR3HyperAllocOnceNoRel(pVM, cbRamRange, 0, MM_TAG_PGM_PHYS, (void **)&pNew);
|
---|
1721 | AssertLogRelMsgRCReturn(rc, ("cbRamRange=%zu\n", cbRamRange), rc);
|
---|
1722 |
|
---|
1723 | pgmR3PhysInitAndLinkRamRange(pVM, pNew, GCPhys, GCPhysLast, NIL_RTRCPTR, NIL_RTR0PTR, pszDesc, pPrev);
|
---|
1724 | }
|
---|
1725 | PGMPhysInvalidatePageMapTLB(pVM);
|
---|
1726 | pgmUnlock(pVM);
|
---|
1727 |
|
---|
1728 | /*
|
---|
1729 | * Notify REM.
|
---|
1730 | */
|
---|
1731 | REMR3NotifyPhysRamRegister(pVM, GCPhys, cb, REM_NOTIFY_PHYS_RAM_FLAGS_RAM);
|
---|
1732 |
|
---|
1733 | return VINF_SUCCESS;
|
---|
1734 | }
|
---|
1735 |
|
---|
1736 |
|
---|
1737 | /**
|
---|
1738 | * Worker called by PGMR3InitFinalize if we're configured to pre-allocate RAM.
|
---|
1739 | *
|
---|
1740 | * We do this late in the init process so that all the ROM and MMIO ranges have
|
---|
1741 | * been registered already and we don't go wasting memory on them.
|
---|
1742 | *
|
---|
1743 | * @returns VBox status code.
|
---|
1744 | *
|
---|
1745 | * @param pVM Pointer to the shared VM structure.
|
---|
1746 | */
|
---|
1747 | int pgmR3PhysRamPreAllocate(PVM pVM)
|
---|
1748 | {
|
---|
1749 | Assert(pVM->pgm.s.fRamPreAlloc);
|
---|
1750 | Log(("pgmR3PhysRamPreAllocate: enter\n"));
|
---|
1751 |
|
---|
1752 | /*
|
---|
1753 | * Walk the RAM ranges and allocate all RAM pages, halt at
|
---|
1754 | * the first allocation error.
|
---|
1755 | */
|
---|
1756 | uint64_t cPages = 0;
|
---|
1757 | uint64_t NanoTS = RTTimeNanoTS();
|
---|
1758 | pgmLock(pVM);
|
---|
1759 | for (PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesXR3; pRam; pRam = pRam->pNextR3)
|
---|
1760 | {
|
---|
1761 | PPGMPAGE pPage = &pRam->aPages[0];
|
---|
1762 | RTGCPHYS GCPhys = pRam->GCPhys;
|
---|
1763 | uint32_t cLeft = pRam->cb >> PAGE_SHIFT;
|
---|
1764 | while (cLeft-- > 0)
|
---|
1765 | {
|
---|
1766 | if (PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM)
|
---|
1767 | {
|
---|
1768 | switch (PGM_PAGE_GET_STATE(pPage))
|
---|
1769 | {
|
---|
1770 | case PGM_PAGE_STATE_ZERO:
|
---|
1771 | {
|
---|
1772 | int rc = pgmPhysAllocPage(pVM, pPage, GCPhys);
|
---|
1773 | if (RT_FAILURE(rc))
|
---|
1774 | {
|
---|
1775 | LogRel(("PGM: RAM Pre-allocation failed at %RGp (in %s) with rc=%Rrc\n", GCPhys, pRam->pszDesc, rc));
|
---|
1776 | pgmUnlock(pVM);
|
---|
1777 | return rc;
|
---|
1778 | }
|
---|
1779 | cPages++;
|
---|
1780 | break;
|
---|
1781 | }
|
---|
1782 |
|
---|
1783 | case PGM_PAGE_STATE_BALLOONED:
|
---|
1784 | case PGM_PAGE_STATE_ALLOCATED:
|
---|
1785 | case PGM_PAGE_STATE_WRITE_MONITORED:
|
---|
1786 | case PGM_PAGE_STATE_SHARED:
|
---|
1787 | /* nothing to do here. */
|
---|
1788 | break;
|
---|
1789 | }
|
---|
1790 | }
|
---|
1791 |
|
---|
1792 | /* next */
|
---|
1793 | pPage++;
|
---|
1794 | GCPhys += PAGE_SIZE;
|
---|
1795 | }
|
---|
1796 | }
|
---|
1797 | pgmUnlock(pVM);
|
---|
1798 | NanoTS = RTTimeNanoTS() - NanoTS;
|
---|
1799 |
|
---|
1800 | LogRel(("PGM: Pre-allocated %llu pages in %llu ms\n", cPages, NanoTS / 1000000));
|
---|
1801 | Log(("pgmR3PhysRamPreAllocate: returns VINF_SUCCESS\n"));
|
---|
1802 | return VINF_SUCCESS;
|
---|
1803 | }
|
---|
1804 |
|
---|
1805 |
|
---|
1806 | /**
|
---|
1807 | * Resets (zeros) the RAM.
|
---|
1808 | *
|
---|
1809 | * ASSUMES that the caller owns the PGM lock.
|
---|
1810 | *
|
---|
1811 | * @returns VBox status code.
|
---|
1812 | * @param pVM Pointer to the shared VM structure.
|
---|
1813 | */
|
---|
1814 | int pgmR3PhysRamReset(PVM pVM)
|
---|
1815 | {
|
---|
1816 | Assert(PGMIsLockOwner(pVM));
|
---|
1817 |
|
---|
1818 | /* Reset the memory balloon. */
|
---|
1819 | int rc = GMMR3BalloonedPages(pVM, GMMBALLOONACTION_RESET, 0);
|
---|
1820 | AssertRC(rc);
|
---|
1821 |
|
---|
1822 | #ifdef VBOX_WITH_PAGE_SHARING
|
---|
1823 | /* Clear all registered shared modules. */
|
---|
1824 | rc = GMMR3ResetSharedModules(pVM);
|
---|
1825 | AssertRC(rc);
|
---|
1826 | #endif
|
---|
1827 | /* Reset counters. */
|
---|
1828 | pVM->pgm.s.cReusedSharedPages = 0;
|
---|
1829 | pVM->pgm.s.cBalloonedPages = 0;
|
---|
1830 |
|
---|
1831 | /*
|
---|
1832 | * We batch up pages that should be freed instead of calling GMM for
|
---|
1833 | * each and every one of them.
|
---|
1834 | */
|
---|
1835 | uint32_t cPendingPages = 0;
|
---|
1836 | PGMMFREEPAGESREQ pReq;
|
---|
1837 | rc = GMMR3FreePagesPrepare(pVM, &pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE, GMMACCOUNT_BASE);
|
---|
1838 | AssertLogRelRCReturn(rc, rc);
|
---|
1839 |
|
---|
1840 | /*
|
---|
1841 | * Walk the ram ranges.
|
---|
1842 | */
|
---|
1843 | for (PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesXR3; pRam; pRam = pRam->pNextR3)
|
---|
1844 | {
|
---|
1845 | uint32_t iPage = pRam->cb >> PAGE_SHIFT;
|
---|
1846 | AssertMsg(((RTGCPHYS)iPage << PAGE_SHIFT) == pRam->cb, ("%RGp %RGp\n", (RTGCPHYS)iPage << PAGE_SHIFT, pRam->cb));
|
---|
1847 |
|
---|
1848 | if (!pVM->pgm.s.fRamPreAlloc)
|
---|
1849 | {
|
---|
1850 | /* Replace all RAM pages by ZERO pages. */
|
---|
1851 | while (iPage-- > 0)
|
---|
1852 | {
|
---|
1853 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
1854 | switch (PGM_PAGE_GET_TYPE(pPage))
|
---|
1855 | {
|
---|
1856 | case PGMPAGETYPE_RAM:
|
---|
1857 | /* Do not replace pages part of a 2 MB continuous range
|
---|
1858 | with zero pages, but zero them instead. */
|
---|
1859 | if ( PGM_PAGE_GET_PDE_TYPE(pPage) == PGM_PAGE_PDE_TYPE_PDE
|
---|
1860 | || PGM_PAGE_GET_PDE_TYPE(pPage) == PGM_PAGE_PDE_TYPE_PDE_DISABLED)
|
---|
1861 | {
|
---|
1862 | void *pvPage;
|
---|
1863 | rc = pgmPhysPageMap(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), &pvPage);
|
---|
1864 | AssertLogRelRCReturn(rc, rc);
|
---|
1865 | ASMMemZeroPage(pvPage);
|
---|
1866 | }
|
---|
1867 | else if (PGM_PAGE_IS_BALLOONED(pPage))
|
---|
1868 | {
|
---|
1869 | /* Turn into a zero page; the balloon status is lost when the VM reboots. */
|
---|
1870 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ZERO);
|
---|
1871 | }
|
---|
1872 | else if (!PGM_PAGE_IS_ZERO(pPage))
|
---|
1873 | {
|
---|
1874 | rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, pPage, pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT));
|
---|
1875 | AssertLogRelRCReturn(rc, rc);
|
---|
1876 | }
|
---|
1877 | break;
|
---|
1878 |
|
---|
1879 | case PGMPAGETYPE_MMIO2_ALIAS_MMIO:
|
---|
1880 | pgmHandlerPhysicalResetAliasedPage(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT),
|
---|
1881 | true /*fDoAccounting*/);
|
---|
1882 | break;
|
---|
1883 |
|
---|
1884 | case PGMPAGETYPE_MMIO2:
|
---|
1885 | case PGMPAGETYPE_ROM_SHADOW: /* handled by pgmR3PhysRomReset. */
|
---|
1886 | case PGMPAGETYPE_ROM:
|
---|
1887 | case PGMPAGETYPE_MMIO:
|
---|
1888 | break;
|
---|
1889 | default:
|
---|
1890 | AssertFailed();
|
---|
1891 | }
|
---|
1892 | } /* for each page */
|
---|
1893 | }
|
---|
1894 | else
|
---|
1895 | {
|
---|
1896 | /* Zero the memory. */
|
---|
1897 | while (iPage-- > 0)
|
---|
1898 | {
|
---|
1899 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
1900 | switch (PGM_PAGE_GET_TYPE(pPage))
|
---|
1901 | {
|
---|
1902 | case PGMPAGETYPE_RAM:
|
---|
1903 | switch (PGM_PAGE_GET_STATE(pPage))
|
---|
1904 | {
|
---|
1905 | case PGM_PAGE_STATE_ZERO:
|
---|
1906 | break;
|
---|
1907 |
|
---|
1908 | case PGM_PAGE_STATE_BALLOONED:
|
---|
1909 | /* Turn into a zero page; the balloon status is lost when the VM reboots. */
|
---|
1910 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ZERO);
|
---|
1911 | break;
|
---|
1912 |
|
---|
1913 | case PGM_PAGE_STATE_SHARED:
|
---|
1914 | case PGM_PAGE_STATE_WRITE_MONITORED:
|
---|
1915 | rc = pgmPhysPageMakeWritable(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT));
|
---|
1916 | AssertLogRelRCReturn(rc, rc);
|
---|
1917 | /* no break */
|
---|
1918 |
|
---|
1919 | case PGM_PAGE_STATE_ALLOCATED:
|
---|
1920 | {
|
---|
1921 | void *pvPage;
|
---|
1922 | rc = pgmPhysPageMap(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), &pvPage);
|
---|
1923 | AssertLogRelRCReturn(rc, rc);
|
---|
1924 | ASMMemZeroPage(pvPage);
|
---|
1925 | break;
|
---|
1926 | }
|
---|
1927 | }
|
---|
1928 | break;
|
---|
1929 |
|
---|
1930 | case PGMPAGETYPE_MMIO2_ALIAS_MMIO:
|
---|
1931 | pgmHandlerPhysicalResetAliasedPage(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT),
|
---|
1932 | true /*fDoAccounting*/);
|
---|
1933 | break;
|
---|
1934 |
|
---|
1935 | case PGMPAGETYPE_MMIO2:
|
---|
1936 | case PGMPAGETYPE_ROM_SHADOW:
|
---|
1937 | case PGMPAGETYPE_ROM:
|
---|
1938 | case PGMPAGETYPE_MMIO:
|
---|
1939 | break;
|
---|
1940 | default:
|
---|
1941 | AssertFailed();
|
---|
1942 |
|
---|
1943 | }
|
---|
1944 | } /* for each page */
|
---|
1945 | }
|
---|
1946 |
|
---|
1947 | }
|
---|
1948 |
|
---|
1949 | /*
|
---|
1950 | * Finish off any pages pending freeing.
|
---|
1951 | */
|
---|
1952 | if (cPendingPages)
|
---|
1953 | {
|
---|
1954 | rc = GMMR3FreePagesPerform(pVM, pReq, cPendingPages);
|
---|
1955 | AssertLogRelRCReturn(rc, rc);
|
---|
1956 | }
|
---|
1957 | GMMR3FreePagesCleanup(pReq);
|
---|
1958 |
|
---|
1959 | return VINF_SUCCESS;
|
---|
1960 | }
|
---|
1961 |
|
---|
1962 | /**
|
---|
1963 | * Frees all RAM during VM termination
|
---|
1964 | *
|
---|
1965 | * ASSUMES that the caller owns the PGM lock.
|
---|
1966 | *
|
---|
1967 | * @returns VBox status code.
|
---|
1968 | * @param pVM Pointer to the shared VM structure.
|
---|
1969 | */
|
---|
1970 | int pgmR3PhysRamTerm(PVM pVM)
|
---|
1971 | {
|
---|
1972 | Assert(PGMIsLockOwner(pVM));
|
---|
1973 |
|
---|
1974 | /* Reset the memory balloon. */
|
---|
1975 | int rc = GMMR3BalloonedPages(pVM, GMMBALLOONACTION_RESET, 0);
|
---|
1976 | AssertRC(rc);
|
---|
1977 |
|
---|
1978 | #ifdef VBOX_WITH_PAGE_SHARING
|
---|
1979 | /* Clear all registered shared modules. */
|
---|
1980 | rc = GMMR3ResetSharedModules(pVM);
|
---|
1981 | AssertRC(rc);
|
---|
1982 | #endif
|
---|
1983 |
|
---|
1984 | /*
|
---|
1985 | * We batch up pages that should be freed instead of calling GMM for
|
---|
1986 | * each and every one of them.
|
---|
1987 | */
|
---|
1988 | uint32_t cPendingPages = 0;
|
---|
1989 | PGMMFREEPAGESREQ pReq;
|
---|
1990 | rc = GMMR3FreePagesPrepare(pVM, &pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE, GMMACCOUNT_BASE);
|
---|
1991 | AssertLogRelRCReturn(rc, rc);
|
---|
1992 |
|
---|
1993 | /*
|
---|
1994 | * Walk the ram ranges.
|
---|
1995 | */
|
---|
1996 | for (PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesXR3; pRam; pRam = pRam->pNextR3)
|
---|
1997 | {
|
---|
1998 | uint32_t iPage = pRam->cb >> PAGE_SHIFT;
|
---|
1999 | AssertMsg(((RTGCPHYS)iPage << PAGE_SHIFT) == pRam->cb, ("%RGp %RGp\n", (RTGCPHYS)iPage << PAGE_SHIFT, pRam->cb));
|
---|
2000 |
|
---|
2001 | /* Replace all RAM pages by ZERO pages. */
|
---|
2002 | while (iPage-- > 0)
|
---|
2003 | {
|
---|
2004 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
2005 | switch (PGM_PAGE_GET_TYPE(pPage))
|
---|
2006 | {
|
---|
2007 | case PGMPAGETYPE_RAM:
|
---|
2008 | /* Free all shared pages. Private pages are automatically freed during GMM VM cleanup. */
|
---|
2009 | if (PGM_PAGE_IS_SHARED(pPage))
|
---|
2010 | {
|
---|
2011 | rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, pPage, pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT));
|
---|
2012 | AssertLogRelRCReturn(rc, rc);
|
---|
2013 | }
|
---|
2014 | break;
|
---|
2015 |
|
---|
2016 | case PGMPAGETYPE_MMIO2_ALIAS_MMIO:
|
---|
2017 | case PGMPAGETYPE_MMIO2:
|
---|
2018 | case PGMPAGETYPE_ROM_SHADOW: /* handled by pgmR3PhysRomReset. */
|
---|
2019 | case PGMPAGETYPE_ROM:
|
---|
2020 | case PGMPAGETYPE_MMIO:
|
---|
2021 | break;
|
---|
2022 | default:
|
---|
2023 | AssertFailed();
|
---|
2024 | }
|
---|
2025 | } /* for each page */
|
---|
2026 | }
|
---|
2027 |
|
---|
2028 | /*
|
---|
2029 | * Finish off any pages pending freeing.
|
---|
2030 | */
|
---|
2031 | if (cPendingPages)
|
---|
2032 | {
|
---|
2033 | rc = GMMR3FreePagesPerform(pVM, pReq, cPendingPages);
|
---|
2034 | AssertLogRelRCReturn(rc, rc);
|
---|
2035 | }
|
---|
2036 | GMMR3FreePagesCleanup(pReq);
|
---|
2037 | return VINF_SUCCESS;
|
---|
2038 | }
|
---|
2039 |
|
---|
2040 | /**
|
---|
2041 | * This is the interface IOM is using to register an MMIO region.
|
---|
2042 | *
|
---|
2043 | * It will check for conflicts and ensure that a RAM range structure
|
---|
2044 | * is present before calling the PGMR3HandlerPhysicalRegister API to
|
---|
2045 | * register the callbacks.
|
---|
2046 | *
|
---|
2047 | * @returns VBox status code.
|
---|
2048 | *
|
---|
2049 | * @param pVM Pointer to the shared VM structure.
|
---|
2050 | * @param GCPhys The start of the MMIO region.
|
---|
2051 | * @param cb The size of the MMIO region.
|
---|
2052 | * @param pfnHandlerR3 The address of the ring-3 handler. (IOMR3MMIOHandler)
|
---|
2053 | * @param pvUserR3 The user argument for R3.
|
---|
2054 | * @param pfnHandlerR0 The address of the ring-0 handler. (IOMMMIOHandler)
|
---|
2055 | * @param pvUserR0 The user argument for R0.
|
---|
2056 | * @param pfnHandlerRC The address of the RC handler. (IOMMMIOHandler)
|
---|
2057 | * @param pvUserRC The user argument for RC.
|
---|
2058 | * @param pszDesc The description of the MMIO region.
|
---|
2059 | */
|
---|
2060 | VMMR3DECL(int) PGMR3PhysMMIORegister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb,
|
---|
2061 | R3PTRTYPE(PFNPGMR3PHYSHANDLER) pfnHandlerR3, RTR3PTR pvUserR3,
|
---|
2062 | R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnHandlerR0, RTR0PTR pvUserR0,
|
---|
2063 | RCPTRTYPE(PFNPGMRCPHYSHANDLER) pfnHandlerRC, RTRCPTR pvUserRC,
|
---|
2064 | R3PTRTYPE(const char *) pszDesc)
|
---|
2065 | {
|
---|
2066 | /*
|
---|
2067 | * Assert on some assumption.
|
---|
2068 | */
|
---|
2069 | VM_ASSERT_EMT(pVM);
|
---|
2070 | AssertReturn(!(cb & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
2071 | AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
2072 | AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
|
---|
2073 | AssertReturn(*pszDesc, VERR_INVALID_PARAMETER);
|
---|
2074 |
|
---|
2075 | /*
|
---|
2076 | * Make sure there's a RAM range structure for the region.
|
---|
2077 | */
|
---|
2078 | int rc;
|
---|
2079 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
2080 | bool fRamExists = false;
|
---|
2081 | PPGMRAMRANGE pRamPrev = NULL;
|
---|
2082 | PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesXR3;
|
---|
2083 | while (pRam && GCPhysLast >= pRam->GCPhys)
|
---|
2084 | {
|
---|
2085 | if ( GCPhysLast >= pRam->GCPhys
|
---|
2086 | && GCPhys <= pRam->GCPhysLast)
|
---|
2087 | {
|
---|
2088 | /* Simplification: all within the same range. */
|
---|
2089 | AssertLogRelMsgReturn( GCPhys >= pRam->GCPhys
|
---|
2090 | && GCPhysLast <= pRam->GCPhysLast,
|
---|
2091 | ("%RGp-%RGp (MMIO/%s) falls partly outside %RGp-%RGp (%s)\n",
|
---|
2092 | GCPhys, GCPhysLast, pszDesc,
|
---|
2093 | pRam->GCPhys, pRam->GCPhysLast, pRam->pszDesc),
|
---|
2094 | VERR_PGM_RAM_CONFLICT);
|
---|
2095 |
|
---|
2096 | /* Check that it's all RAM or MMIO pages. */
|
---|
2097 | PCPGMPAGE pPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
2098 | uint32_t cLeft = cb >> PAGE_SHIFT;
|
---|
2099 | while (cLeft-- > 0)
|
---|
2100 | {
|
---|
2101 | AssertLogRelMsgReturn( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM
|
---|
2102 | || PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO,
|
---|
2103 | ("%RGp-%RGp (MMIO/%s): %RGp is not a RAM or MMIO page - type=%d desc=%s\n",
|
---|
2104 | GCPhys, GCPhysLast, pszDesc, PGM_PAGE_GET_TYPE(pPage), pRam->pszDesc),
|
---|
2105 | VERR_PGM_RAM_CONFLICT);
|
---|
2106 | pPage++;
|
---|
2107 | }
|
---|
2108 |
|
---|
2109 | /* Looks good. */
|
---|
2110 | fRamExists = true;
|
---|
2111 | break;
|
---|
2112 | }
|
---|
2113 |
|
---|
2114 | /* next */
|
---|
2115 | pRamPrev = pRam;
|
---|
2116 | pRam = pRam->pNextR3;
|
---|
2117 | }
|
---|
2118 | PPGMRAMRANGE pNew;
|
---|
2119 | if (fRamExists)
|
---|
2120 | {
|
---|
2121 | pNew = NULL;
|
---|
2122 |
|
---|
2123 | /*
|
---|
2124 | * Make all the pages in the range MMIO/ZERO pages, freeing any
|
---|
2125 | * RAM pages currently mapped here. This might not be 100% correct
|
---|
2126 | * for PCI memory, but we're doing the same thing for MMIO2 pages.
|
---|
2127 | */
|
---|
2128 | rc = pgmLock(pVM);
|
---|
2129 | if (RT_SUCCESS(rc))
|
---|
2130 | {
|
---|
2131 | rc = pgmR3PhysFreePageRange(pVM, pRam, GCPhys, GCPhysLast, PGMPAGETYPE_MMIO);
|
---|
2132 | pgmUnlock(pVM);
|
---|
2133 | }
|
---|
2134 | AssertRCReturn(rc, rc);
|
---|
2135 |
|
---|
2136 | /* Force a PGM pool flush as guest ram references have been changed. */
|
---|
2137 | /** todo; not entirely SMP safe; assuming for now the guest takes care of this internally (not touch mapped mmio while changing the mapping). */
|
---|
2138 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
2139 | pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
|
---|
2140 | VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
|
---|
2141 | }
|
---|
2142 | else
|
---|
2143 | {
|
---|
2144 | pgmLock(pVM);
|
---|
2145 |
|
---|
2146 | /*
|
---|
2147 | * No RAM range, insert an ad hoc one.
|
---|
2148 | *
|
---|
2149 | * Note that we don't have to tell REM about this range because
|
---|
2150 | * PGMHandlerPhysicalRegisterEx will do that for us.
|
---|
2151 | */
|
---|
2152 | Log(("PGMR3PhysMMIORegister: Adding ad hoc MMIO range for %RGp-%RGp %s\n", GCPhys, GCPhysLast, pszDesc));
|
---|
2153 |
|
---|
2154 | const uint32_t cPages = cb >> PAGE_SHIFT;
|
---|
2155 | const size_t cbRamRange = RT_OFFSETOF(PGMRAMRANGE, aPages[cPages]);
|
---|
2156 | rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMRAMRANGE, aPages[cPages]), 16, MM_TAG_PGM_PHYS, (void **)&pNew);
|
---|
2157 | AssertLogRelMsgRCReturn(rc, ("cbRamRange=%zu\n", cbRamRange), rc);
|
---|
2158 |
|
---|
2159 | /* Initialize the range. */
|
---|
2160 | pNew->pSelfR0 = MMHyperCCToR0(pVM, pNew);
|
---|
2161 | pNew->pSelfRC = MMHyperCCToRC(pVM, pNew);
|
---|
2162 | pNew->GCPhys = GCPhys;
|
---|
2163 | pNew->GCPhysLast = GCPhysLast;
|
---|
2164 | pNew->cb = cb;
|
---|
2165 | pNew->pszDesc = pszDesc;
|
---|
2166 | pNew->fFlags = PGM_RAM_RANGE_FLAGS_AD_HOC_MMIO;
|
---|
2167 | pNew->pvR3 = NULL;
|
---|
2168 | pNew->paLSPages = NULL;
|
---|
2169 |
|
---|
2170 | uint32_t iPage = cPages;
|
---|
2171 | while (iPage-- > 0)
|
---|
2172 | PGM_PAGE_INIT_ZERO(&pNew->aPages[iPage], pVM, PGMPAGETYPE_MMIO);
|
---|
2173 | Assert(PGM_PAGE_GET_TYPE(&pNew->aPages[0]) == PGMPAGETYPE_MMIO);
|
---|
2174 |
|
---|
2175 | /* update the page count stats. */
|
---|
2176 | pVM->pgm.s.cPureMmioPages += cPages;
|
---|
2177 | pVM->pgm.s.cAllPages += cPages;
|
---|
2178 |
|
---|
2179 | /* link it */
|
---|
2180 | pgmR3PhysLinkRamRange(pVM, pNew, pRamPrev);
|
---|
2181 |
|
---|
2182 | pgmUnlock(pVM);
|
---|
2183 | }
|
---|
2184 |
|
---|
2185 | /*
|
---|
2186 | * Register the access handler.
|
---|
2187 | */
|
---|
2188 | rc = PGMHandlerPhysicalRegisterEx(pVM, PGMPHYSHANDLERTYPE_MMIO, GCPhys, GCPhysLast,
|
---|
2189 | pfnHandlerR3, pvUserR3,
|
---|
2190 | pfnHandlerR0, pvUserR0,
|
---|
2191 | pfnHandlerRC, pvUserRC, pszDesc);
|
---|
2192 | if ( RT_FAILURE(rc)
|
---|
2193 | && !fRamExists)
|
---|
2194 | {
|
---|
2195 | pVM->pgm.s.cPureMmioPages -= cb >> PAGE_SHIFT;
|
---|
2196 | pVM->pgm.s.cAllPages -= cb >> PAGE_SHIFT;
|
---|
2197 |
|
---|
2198 | /* remove the ad hoc range. */
|
---|
2199 | pgmR3PhysUnlinkRamRange2(pVM, pNew, pRamPrev);
|
---|
2200 | pNew->cb = pNew->GCPhys = pNew->GCPhysLast = NIL_RTGCPHYS;
|
---|
2201 | MMHyperFree(pVM, pRam);
|
---|
2202 | }
|
---|
2203 | PGMPhysInvalidatePageMapTLB(pVM);
|
---|
2204 |
|
---|
2205 | return rc;
|
---|
2206 | }
|
---|
2207 |
|
---|
2208 |
|
---|
2209 | /**
|
---|
2210 | * This is the interface IOM is using to register an MMIO region.
|
---|
2211 | *
|
---|
2212 | * It will take care of calling PGMHandlerPhysicalDeregister and clean up
|
---|
2213 | * any ad hoc PGMRAMRANGE left behind.
|
---|
2214 | *
|
---|
2215 | * @returns VBox status code.
|
---|
2216 | * @param pVM Pointer to the shared VM structure.
|
---|
2217 | * @param GCPhys The start of the MMIO region.
|
---|
2218 | * @param cb The size of the MMIO region.
|
---|
2219 | */
|
---|
2220 | VMMR3DECL(int) PGMR3PhysMMIODeregister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb)
|
---|
2221 | {
|
---|
2222 | VM_ASSERT_EMT(pVM);
|
---|
2223 |
|
---|
2224 | /** @todo this needs to own the PGM lock! */
|
---|
2225 | /*
|
---|
2226 | * First deregister the handler, then check if we should remove the ram range.
|
---|
2227 | */
|
---|
2228 | int rc = PGMHandlerPhysicalDeregister(pVM, GCPhys);
|
---|
2229 | if (RT_SUCCESS(rc))
|
---|
2230 | {
|
---|
2231 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
2232 | PPGMRAMRANGE pRamPrev = NULL;
|
---|
2233 | PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesXR3;
|
---|
2234 | while (pRam && GCPhysLast >= pRam->GCPhys)
|
---|
2235 | {
|
---|
2236 | /** @todo We're being a bit too careful here. rewrite. */
|
---|
2237 | if ( GCPhysLast == pRam->GCPhysLast
|
---|
2238 | && GCPhys == pRam->GCPhys)
|
---|
2239 | {
|
---|
2240 | Assert(pRam->cb == cb);
|
---|
2241 |
|
---|
2242 | /*
|
---|
2243 | * See if all the pages are dead MMIO pages.
|
---|
2244 | */
|
---|
2245 | uint32_t const cPages = cb >> PAGE_SHIFT;
|
---|
2246 | bool fAllMMIO = true;
|
---|
2247 | uint32_t iPage = 0;
|
---|
2248 | uint32_t cLeft = cPages;
|
---|
2249 | while (cLeft-- > 0)
|
---|
2250 | {
|
---|
2251 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
2252 | if ( PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO
|
---|
2253 | /*|| not-out-of-action later */)
|
---|
2254 | {
|
---|
2255 | fAllMMIO = false;
|
---|
2256 | Assert(PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO2_ALIAS_MMIO);
|
---|
2257 | AssertMsgFailed(("%RGp %R[pgmpage]\n", pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), pPage));
|
---|
2258 | break;
|
---|
2259 | }
|
---|
2260 | Assert(PGM_PAGE_IS_ZERO(pPage));
|
---|
2261 | pPage++;
|
---|
2262 | }
|
---|
2263 | if (fAllMMIO)
|
---|
2264 | {
|
---|
2265 | /*
|
---|
2266 | * Ad-hoc range, unlink and free it.
|
---|
2267 | */
|
---|
2268 | Log(("PGMR3PhysMMIODeregister: Freeing ad hoc MMIO range for %RGp-%RGp %s\n",
|
---|
2269 | GCPhys, GCPhysLast, pRam->pszDesc));
|
---|
2270 |
|
---|
2271 | pVM->pgm.s.cAllPages -= cPages;
|
---|
2272 | pVM->pgm.s.cPureMmioPages -= cPages;
|
---|
2273 |
|
---|
2274 | pgmR3PhysUnlinkRamRange2(pVM, pRam, pRamPrev);
|
---|
2275 | pRam->cb = pRam->GCPhys = pRam->GCPhysLast = NIL_RTGCPHYS;
|
---|
2276 | MMHyperFree(pVM, pRam);
|
---|
2277 | break;
|
---|
2278 | }
|
---|
2279 | }
|
---|
2280 |
|
---|
2281 | /*
|
---|
2282 | * Range match? It will all be within one range (see PGMAllHandler.cpp).
|
---|
2283 | */
|
---|
2284 | if ( GCPhysLast >= pRam->GCPhys
|
---|
2285 | && GCPhys <= pRam->GCPhysLast)
|
---|
2286 | {
|
---|
2287 | Assert(GCPhys >= pRam->GCPhys);
|
---|
2288 | Assert(GCPhysLast <= pRam->GCPhysLast);
|
---|
2289 |
|
---|
2290 | /*
|
---|
2291 | * Turn the pages back into RAM pages.
|
---|
2292 | */
|
---|
2293 | uint32_t iPage = (GCPhys - pRam->GCPhys) >> PAGE_SHIFT;
|
---|
2294 | uint32_t cLeft = cb >> PAGE_SHIFT;
|
---|
2295 | while (cLeft--)
|
---|
2296 | {
|
---|
2297 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
2298 | AssertMsg(PGM_PAGE_IS_MMIO(pPage), ("%RGp %R[pgmpage]\n", pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), pPage));
|
---|
2299 | AssertMsg(PGM_PAGE_IS_ZERO(pPage), ("%RGp %R[pgmpage]\n", pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), pPage));
|
---|
2300 | if (PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO)
|
---|
2301 | PGM_PAGE_SET_TYPE(pPage, PGMPAGETYPE_RAM);
|
---|
2302 | }
|
---|
2303 | break;
|
---|
2304 | }
|
---|
2305 |
|
---|
2306 | /* next */
|
---|
2307 | pRamPrev = pRam;
|
---|
2308 | pRam = pRam->pNextR3;
|
---|
2309 | }
|
---|
2310 | }
|
---|
2311 |
|
---|
2312 | /* Force a PGM pool flush as guest ram references have been changed. */
|
---|
2313 | /** todo; not entirely SMP safe; assuming for now the guest takes care of this internally (not touch mapped mmio while changing the mapping). */
|
---|
2314 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
2315 | pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
|
---|
2316 | VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
|
---|
2317 |
|
---|
2318 | PGMPhysInvalidatePageMapTLB(pVM);
|
---|
2319 | pgmPhysInvalidRamRangeTlbs(pVM);
|
---|
2320 | return rc;
|
---|
2321 | }
|
---|
2322 |
|
---|
2323 |
|
---|
2324 | /**
|
---|
2325 | * Locate a MMIO2 range.
|
---|
2326 | *
|
---|
2327 | * @returns Pointer to the MMIO2 range.
|
---|
2328 | * @param pVM Pointer to the shared VM structure.
|
---|
2329 | * @param pDevIns The device instance owning the region.
|
---|
2330 | * @param iRegion The region.
|
---|
2331 | */
|
---|
2332 | DECLINLINE(PPGMMMIO2RANGE) pgmR3PhysMMIO2Find(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion)
|
---|
2333 | {
|
---|
2334 | /*
|
---|
2335 | * Search the list.
|
---|
2336 | */
|
---|
2337 | for (PPGMMMIO2RANGE pCur = pVM->pgm.s.pMmio2RangesR3; pCur; pCur = pCur->pNextR3)
|
---|
2338 | if ( pCur->pDevInsR3 == pDevIns
|
---|
2339 | && pCur->iRegion == iRegion)
|
---|
2340 | return pCur;
|
---|
2341 | return NULL;
|
---|
2342 | }
|
---|
2343 |
|
---|
2344 |
|
---|
2345 | /**
|
---|
2346 | * Allocate and register an MMIO2 region.
|
---|
2347 | *
|
---|
2348 | * As mentioned elsewhere, MMIO2 is just RAM spelled differently. It's RAM
|
---|
2349 | * associated with a device. It is also non-shared memory with a permanent
|
---|
2350 | * ring-3 mapping and page backing (presently).
|
---|
2351 | *
|
---|
2352 | * A MMIO2 range may overlap with base memory if a lot of RAM is configured for
|
---|
2353 | * the VM, in which case we'll drop the base memory pages. Presently we will
|
---|
2354 | * make no attempt to preserve anything that happens to be present in the base
|
---|
2355 | * memory that is replaced, this is of course incorrectly but it's too much
|
---|
2356 | * effort.
|
---|
2357 | *
|
---|
2358 | * @returns VBox status code.
|
---|
2359 | * @retval VINF_SUCCESS on success, *ppv pointing to the R3 mapping of the
|
---|
2360 | * memory.
|
---|
2361 | * @retval VERR_ALREADY_EXISTS if the region already exists.
|
---|
2362 | *
|
---|
2363 | * @param pVM Pointer to the shared VM structure.
|
---|
2364 | * @param pDevIns The device instance owning the region.
|
---|
2365 | * @param iRegion The region number. If the MMIO2 memory is a PCI
|
---|
2366 | * I/O region this number has to be the number of that
|
---|
2367 | * region. Otherwise it can be any number safe
|
---|
2368 | * UINT8_MAX.
|
---|
2369 | * @param cb The size of the region. Must be page aligned.
|
---|
2370 | * @param fFlags Reserved for future use, must be zero.
|
---|
2371 | * @param ppv Where to store the pointer to the ring-3 mapping of
|
---|
2372 | * the memory.
|
---|
2373 | * @param pszDesc The description.
|
---|
2374 | */
|
---|
2375 | VMMR3DECL(int) PGMR3PhysMMIO2Register(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS cb, uint32_t fFlags, void **ppv, const char *pszDesc)
|
---|
2376 | {
|
---|
2377 | /*
|
---|
2378 | * Validate input.
|
---|
2379 | */
|
---|
2380 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
2381 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
2382 | AssertReturn(iRegion <= UINT8_MAX, VERR_INVALID_PARAMETER);
|
---|
2383 | AssertPtrReturn(ppv, VERR_INVALID_POINTER);
|
---|
2384 | AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
|
---|
2385 | AssertReturn(*pszDesc, VERR_INVALID_PARAMETER);
|
---|
2386 | AssertReturn(pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion) == NULL, VERR_ALREADY_EXISTS);
|
---|
2387 | AssertReturn(!(cb & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
2388 | AssertReturn(cb, VERR_INVALID_PARAMETER);
|
---|
2389 | AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
|
---|
2390 |
|
---|
2391 | const uint32_t cPages = cb >> PAGE_SHIFT;
|
---|
2392 | AssertLogRelReturn(((RTGCPHYS)cPages << PAGE_SHIFT) == cb, VERR_INVALID_PARAMETER);
|
---|
2393 | AssertLogRelReturn(cPages <= INT32_MAX / 2, VERR_NO_MEMORY);
|
---|
2394 |
|
---|
2395 | /*
|
---|
2396 | * For the 2nd+ instance, mangle the description string so it's unique.
|
---|
2397 | */
|
---|
2398 | if (pDevIns->iInstance > 0) /** @todo Move to PDMDevHlp.cpp and use a real string cache. */
|
---|
2399 | {
|
---|
2400 | pszDesc = MMR3HeapAPrintf(pVM, MM_TAG_PGM_PHYS, "%s [%u]", pszDesc, pDevIns->iInstance);
|
---|
2401 | if (!pszDesc)
|
---|
2402 | return VERR_NO_MEMORY;
|
---|
2403 | }
|
---|
2404 |
|
---|
2405 | /*
|
---|
2406 | * Try reserve and allocate the backing memory first as this is what is
|
---|
2407 | * most likely to fail.
|
---|
2408 | */
|
---|
2409 | int rc = MMR3AdjustFixedReservation(pVM, cPages, pszDesc);
|
---|
2410 | if (RT_SUCCESS(rc))
|
---|
2411 | {
|
---|
2412 | void *pvPages;
|
---|
2413 | PSUPPAGE paPages = (PSUPPAGE)RTMemTmpAlloc(cPages * sizeof(SUPPAGE));
|
---|
2414 | if (RT_SUCCESS(rc))
|
---|
2415 | rc = SUPR3PageAllocEx(cPages, 0 /*fFlags*/, &pvPages, NULL /*pR0Ptr*/, paPages);
|
---|
2416 | if (RT_SUCCESS(rc))
|
---|
2417 | {
|
---|
2418 | memset(pvPages, 0, cPages * PAGE_SIZE);
|
---|
2419 |
|
---|
2420 | /*
|
---|
2421 | * Create the MMIO2 range record for it.
|
---|
2422 | */
|
---|
2423 | const size_t cbRange = RT_OFFSETOF(PGMMMIO2RANGE, RamRange.aPages[cPages]);
|
---|
2424 | PPGMMMIO2RANGE pNew;
|
---|
2425 | rc = MMR3HyperAllocOnceNoRel(pVM, cbRange, 0, MM_TAG_PGM_PHYS, (void **)&pNew);
|
---|
2426 | AssertLogRelMsgRC(rc, ("cbRamRange=%zu\n", cbRange));
|
---|
2427 | if (RT_SUCCESS(rc))
|
---|
2428 | {
|
---|
2429 | pNew->pDevInsR3 = pDevIns;
|
---|
2430 | pNew->pvR3 = pvPages;
|
---|
2431 | //pNew->pNext = NULL;
|
---|
2432 | //pNew->fMapped = false;
|
---|
2433 | //pNew->fOverlapping = false;
|
---|
2434 | pNew->iRegion = iRegion;
|
---|
2435 | pNew->idSavedState = UINT8_MAX;
|
---|
2436 | pNew->RamRange.pSelfR0 = MMHyperCCToR0(pVM, &pNew->RamRange);
|
---|
2437 | pNew->RamRange.pSelfRC = MMHyperCCToRC(pVM, &pNew->RamRange);
|
---|
2438 | pNew->RamRange.GCPhys = NIL_RTGCPHYS;
|
---|
2439 | pNew->RamRange.GCPhysLast = NIL_RTGCPHYS;
|
---|
2440 | pNew->RamRange.pszDesc = pszDesc;
|
---|
2441 | pNew->RamRange.cb = cb;
|
---|
2442 | pNew->RamRange.fFlags = PGM_RAM_RANGE_FLAGS_AD_HOC_MMIO2;
|
---|
2443 | pNew->RamRange.pvR3 = pvPages;
|
---|
2444 | //pNew->RamRange.paLSPages = NULL;
|
---|
2445 |
|
---|
2446 | uint32_t iPage = cPages;
|
---|
2447 | while (iPage-- > 0)
|
---|
2448 | {
|
---|
2449 | PGM_PAGE_INIT(&pNew->RamRange.aPages[iPage],
|
---|
2450 | paPages[iPage].Phys, NIL_GMM_PAGEID,
|
---|
2451 | PGMPAGETYPE_MMIO2, PGM_PAGE_STATE_ALLOCATED);
|
---|
2452 | }
|
---|
2453 |
|
---|
2454 | /* update page count stats */
|
---|
2455 | pVM->pgm.s.cAllPages += cPages;
|
---|
2456 | pVM->pgm.s.cPrivatePages += cPages;
|
---|
2457 |
|
---|
2458 | /*
|
---|
2459 | * Link it into the list.
|
---|
2460 | * Since there is no particular order, just push it.
|
---|
2461 | */
|
---|
2462 | pgmLock(pVM);
|
---|
2463 | pNew->pNextR3 = pVM->pgm.s.pMmio2RangesR3;
|
---|
2464 | pVM->pgm.s.pMmio2RangesR3 = pNew;
|
---|
2465 | pgmUnlock(pVM);
|
---|
2466 |
|
---|
2467 | *ppv = pvPages;
|
---|
2468 | RTMemTmpFree(paPages);
|
---|
2469 | PGMPhysInvalidatePageMapTLB(pVM);
|
---|
2470 | return VINF_SUCCESS;
|
---|
2471 | }
|
---|
2472 |
|
---|
2473 | SUPR3PageFreeEx(pvPages, cPages);
|
---|
2474 | }
|
---|
2475 | RTMemTmpFree(paPages);
|
---|
2476 | MMR3AdjustFixedReservation(pVM, -(int32_t)cPages, pszDesc);
|
---|
2477 | }
|
---|
2478 | if (pDevIns->iInstance > 0)
|
---|
2479 | MMR3HeapFree((void *)pszDesc);
|
---|
2480 | return rc;
|
---|
2481 | }
|
---|
2482 |
|
---|
2483 |
|
---|
2484 | /**
|
---|
2485 | * Deregisters and frees an MMIO2 region.
|
---|
2486 | *
|
---|
2487 | * Any physical (and virtual) access handlers registered for the region must
|
---|
2488 | * be deregistered before calling this function.
|
---|
2489 | *
|
---|
2490 | * @returns VBox status code.
|
---|
2491 | * @param pVM Pointer to the shared VM structure.
|
---|
2492 | * @param pDevIns The device instance owning the region.
|
---|
2493 | * @param iRegion The region. If it's UINT32_MAX it'll be a wildcard match.
|
---|
2494 | */
|
---|
2495 | VMMR3DECL(int) PGMR3PhysMMIO2Deregister(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion)
|
---|
2496 | {
|
---|
2497 | /*
|
---|
2498 | * Validate input.
|
---|
2499 | */
|
---|
2500 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
2501 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
2502 | AssertReturn(iRegion <= UINT8_MAX || iRegion == UINT32_MAX, VERR_INVALID_PARAMETER);
|
---|
2503 |
|
---|
2504 | pgmLock(pVM);
|
---|
2505 | int rc = VINF_SUCCESS;
|
---|
2506 | unsigned cFound = 0;
|
---|
2507 | PPGMMMIO2RANGE pPrev = NULL;
|
---|
2508 | PPGMMMIO2RANGE pCur = pVM->pgm.s.pMmio2RangesR3;
|
---|
2509 | while (pCur)
|
---|
2510 | {
|
---|
2511 | if ( pCur->pDevInsR3 == pDevIns
|
---|
2512 | && ( iRegion == UINT32_MAX
|
---|
2513 | || pCur->iRegion == iRegion))
|
---|
2514 | {
|
---|
2515 | cFound++;
|
---|
2516 |
|
---|
2517 | /*
|
---|
2518 | * Unmap it if it's mapped.
|
---|
2519 | */
|
---|
2520 | if (pCur->fMapped)
|
---|
2521 | {
|
---|
2522 | int rc2 = PGMR3PhysMMIO2Unmap(pVM, pCur->pDevInsR3, pCur->iRegion, pCur->RamRange.GCPhys);
|
---|
2523 | AssertRC(rc2);
|
---|
2524 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
2525 | rc = rc2;
|
---|
2526 | }
|
---|
2527 |
|
---|
2528 | /*
|
---|
2529 | * Unlink it
|
---|
2530 | */
|
---|
2531 | PPGMMMIO2RANGE pNext = pCur->pNextR3;
|
---|
2532 | if (pPrev)
|
---|
2533 | pPrev->pNextR3 = pNext;
|
---|
2534 | else
|
---|
2535 | pVM->pgm.s.pMmio2RangesR3 = pNext;
|
---|
2536 | pCur->pNextR3 = NULL;
|
---|
2537 |
|
---|
2538 | /*
|
---|
2539 | * Free the memory.
|
---|
2540 | */
|
---|
2541 | int rc2 = SUPR3PageFreeEx(pCur->pvR3, pCur->RamRange.cb >> PAGE_SHIFT);
|
---|
2542 | AssertRC(rc2);
|
---|
2543 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
2544 | rc = rc2;
|
---|
2545 |
|
---|
2546 | uint32_t const cPages = pCur->RamRange.cb >> PAGE_SHIFT;
|
---|
2547 | rc2 = MMR3AdjustFixedReservation(pVM, -(int32_t)cPages, pCur->RamRange.pszDesc);
|
---|
2548 | AssertRC(rc2);
|
---|
2549 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
2550 | rc = rc2;
|
---|
2551 |
|
---|
2552 | /* we're leaking hyper memory here if done at runtime. */
|
---|
2553 | #ifdef VBOX_STRICT
|
---|
2554 | VMSTATE const enmState = VMR3GetState(pVM);
|
---|
2555 | AssertMsg( enmState == VMSTATE_POWERING_OFF
|
---|
2556 | || enmState == VMSTATE_POWERING_OFF_LS
|
---|
2557 | || enmState == VMSTATE_OFF
|
---|
2558 | || enmState == VMSTATE_OFF_LS
|
---|
2559 | || enmState == VMSTATE_DESTROYING
|
---|
2560 | || enmState == VMSTATE_TERMINATED
|
---|
2561 | || enmState == VMSTATE_CREATING
|
---|
2562 | , ("%s\n", VMR3GetStateName(enmState)));
|
---|
2563 | #endif
|
---|
2564 | /*rc = MMHyperFree(pVM, pCur);
|
---|
2565 | AssertRCReturn(rc, rc); - not safe, see the alloc call. */
|
---|
2566 |
|
---|
2567 |
|
---|
2568 | /* update page count stats */
|
---|
2569 | pVM->pgm.s.cAllPages -= cPages;
|
---|
2570 | pVM->pgm.s.cPrivatePages -= cPages;
|
---|
2571 |
|
---|
2572 | /* next */
|
---|
2573 | pCur = pNext;
|
---|
2574 | }
|
---|
2575 | else
|
---|
2576 | {
|
---|
2577 | pPrev = pCur;
|
---|
2578 | pCur = pCur->pNextR3;
|
---|
2579 | }
|
---|
2580 | }
|
---|
2581 | PGMPhysInvalidatePageMapTLB(pVM);
|
---|
2582 | pgmUnlock(pVM);
|
---|
2583 | return !cFound && iRegion != UINT32_MAX ? VERR_NOT_FOUND : rc;
|
---|
2584 | }
|
---|
2585 |
|
---|
2586 |
|
---|
2587 | /**
|
---|
2588 | * Maps a MMIO2 region.
|
---|
2589 | *
|
---|
2590 | * This is done when a guest / the bios / state loading changes the
|
---|
2591 | * PCI config. The replacing of base memory has the same restrictions
|
---|
2592 | * as during registration, of course.
|
---|
2593 | *
|
---|
2594 | * @returns VBox status code.
|
---|
2595 | *
|
---|
2596 | * @param pVM Pointer to the shared VM structure.
|
---|
2597 | * @param pDevIns The
|
---|
2598 | */
|
---|
2599 | VMMR3DECL(int) PGMR3PhysMMIO2Map(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS GCPhys)
|
---|
2600 | {
|
---|
2601 | /*
|
---|
2602 | * Validate input
|
---|
2603 | */
|
---|
2604 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
2605 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
2606 | AssertReturn(iRegion <= UINT8_MAX, VERR_INVALID_PARAMETER);
|
---|
2607 | AssertReturn(GCPhys != NIL_RTGCPHYS, VERR_INVALID_PARAMETER);
|
---|
2608 | AssertReturn(GCPhys != 0, VERR_INVALID_PARAMETER);
|
---|
2609 | AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
2610 |
|
---|
2611 | PPGMMMIO2RANGE pCur = pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion);
|
---|
2612 | AssertReturn(pCur, VERR_NOT_FOUND);
|
---|
2613 | AssertReturn(!pCur->fMapped, VERR_WRONG_ORDER);
|
---|
2614 | Assert(pCur->RamRange.GCPhys == NIL_RTGCPHYS);
|
---|
2615 | Assert(pCur->RamRange.GCPhysLast == NIL_RTGCPHYS);
|
---|
2616 |
|
---|
2617 | const RTGCPHYS GCPhysLast = GCPhys + pCur->RamRange.cb - 1;
|
---|
2618 | AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
|
---|
2619 |
|
---|
2620 | /*
|
---|
2621 | * Find our location in the ram range list, checking for
|
---|
2622 | * restriction we don't bother implementing yet (partially overlapping).
|
---|
2623 | */
|
---|
2624 | bool fRamExists = false;
|
---|
2625 | PPGMRAMRANGE pRamPrev = NULL;
|
---|
2626 | PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesXR3;
|
---|
2627 | while (pRam && GCPhysLast >= pRam->GCPhys)
|
---|
2628 | {
|
---|
2629 | if ( GCPhys <= pRam->GCPhysLast
|
---|
2630 | && GCPhysLast >= pRam->GCPhys)
|
---|
2631 | {
|
---|
2632 | /* completely within? */
|
---|
2633 | AssertLogRelMsgReturn( GCPhys >= pRam->GCPhys
|
---|
2634 | && GCPhysLast <= pRam->GCPhysLast,
|
---|
2635 | ("%RGp-%RGp (MMIO2/%s) falls partly outside %RGp-%RGp (%s)\n",
|
---|
2636 | GCPhys, GCPhysLast, pCur->RamRange.pszDesc,
|
---|
2637 | pRam->GCPhys, pRam->GCPhysLast, pRam->pszDesc),
|
---|
2638 | VERR_PGM_RAM_CONFLICT);
|
---|
2639 | fRamExists = true;
|
---|
2640 | break;
|
---|
2641 | }
|
---|
2642 |
|
---|
2643 | /* next */
|
---|
2644 | pRamPrev = pRam;
|
---|
2645 | pRam = pRam->pNextR3;
|
---|
2646 | }
|
---|
2647 | if (fRamExists)
|
---|
2648 | {
|
---|
2649 | PPGMPAGE pPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
2650 | uint32_t cPagesLeft = pCur->RamRange.cb >> PAGE_SHIFT;
|
---|
2651 | while (cPagesLeft-- > 0)
|
---|
2652 | {
|
---|
2653 | AssertLogRelMsgReturn(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM,
|
---|
2654 | ("%RGp isn't a RAM page (%d) - mapping %RGp-%RGp (MMIO2/%s).\n",
|
---|
2655 | GCPhys, PGM_PAGE_GET_TYPE(pPage), GCPhys, GCPhysLast, pCur->RamRange.pszDesc),
|
---|
2656 | VERR_PGM_RAM_CONFLICT);
|
---|
2657 | pPage++;
|
---|
2658 | }
|
---|
2659 | }
|
---|
2660 | Log(("PGMR3PhysMMIO2Map: %RGp-%RGp fRamExists=%RTbool %s\n",
|
---|
2661 | GCPhys, GCPhysLast, fRamExists, pCur->RamRange.pszDesc));
|
---|
2662 |
|
---|
2663 | /*
|
---|
2664 | * Make the changes.
|
---|
2665 | */
|
---|
2666 | pgmLock(pVM);
|
---|
2667 |
|
---|
2668 | pCur->RamRange.GCPhys = GCPhys;
|
---|
2669 | pCur->RamRange.GCPhysLast = GCPhysLast;
|
---|
2670 | pCur->fMapped = true;
|
---|
2671 | pCur->fOverlapping = fRamExists;
|
---|
2672 |
|
---|
2673 | if (fRamExists)
|
---|
2674 | {
|
---|
2675 | /** @todo use pgmR3PhysFreePageRange here. */
|
---|
2676 | uint32_t cPendingPages = 0;
|
---|
2677 | PGMMFREEPAGESREQ pReq;
|
---|
2678 | int rc = GMMR3FreePagesPrepare(pVM, &pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE, GMMACCOUNT_BASE);
|
---|
2679 | AssertLogRelRCReturn(rc, rc);
|
---|
2680 |
|
---|
2681 | /* replace the pages, freeing all present RAM pages. */
|
---|
2682 | PPGMPAGE pPageSrc = &pCur->RamRange.aPages[0];
|
---|
2683 | PPGMPAGE pPageDst = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
2684 | uint32_t cPagesLeft = pCur->RamRange.cb >> PAGE_SHIFT;
|
---|
2685 | while (cPagesLeft-- > 0)
|
---|
2686 | {
|
---|
2687 | rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, pPageDst, GCPhys);
|
---|
2688 | AssertLogRelRCReturn(rc, rc); /* We're done for if this goes wrong. */
|
---|
2689 |
|
---|
2690 | RTHCPHYS const HCPhys = PGM_PAGE_GET_HCPHYS(pPageSrc);
|
---|
2691 | PGM_PAGE_SET_HCPHYS(pPageDst, HCPhys);
|
---|
2692 | PGM_PAGE_SET_TYPE(pPageDst, PGMPAGETYPE_MMIO2);
|
---|
2693 | PGM_PAGE_SET_STATE(pPageDst, PGM_PAGE_STATE_ALLOCATED);
|
---|
2694 | PGM_PAGE_SET_PDE_TYPE(pPageDst, PGM_PAGE_PDE_TYPE_DONTCARE);
|
---|
2695 | PGM_PAGE_SET_PTE_INDEX(pPageDst, 0);
|
---|
2696 | PGM_PAGE_SET_TRACKING(pPageDst, 0);
|
---|
2697 |
|
---|
2698 | pVM->pgm.s.cZeroPages--;
|
---|
2699 | GCPhys += PAGE_SIZE;
|
---|
2700 | pPageSrc++;
|
---|
2701 | pPageDst++;
|
---|
2702 | }
|
---|
2703 |
|
---|
2704 | /* Flush physical page map TLB. */
|
---|
2705 | PGMPhysInvalidatePageMapTLB(pVM);
|
---|
2706 |
|
---|
2707 | if (cPendingPages)
|
---|
2708 | {
|
---|
2709 | rc = GMMR3FreePagesPerform(pVM, pReq, cPendingPages);
|
---|
2710 | AssertLogRelRCReturn(rc, rc);
|
---|
2711 | }
|
---|
2712 | GMMR3FreePagesCleanup(pReq);
|
---|
2713 |
|
---|
2714 | /* Force a PGM pool flush as guest ram references have been changed. */
|
---|
2715 | /** todo; not entirely SMP safe; assuming for now the guest takes care of this internally (not touch mapped mmio while changing the mapping). */
|
---|
2716 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
2717 | pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
|
---|
2718 | VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
|
---|
2719 |
|
---|
2720 | pgmUnlock(pVM);
|
---|
2721 | }
|
---|
2722 | else
|
---|
2723 | {
|
---|
2724 | RTGCPHYS cb = pCur->RamRange.cb;
|
---|
2725 |
|
---|
2726 | /* Clear the tracking data of pages we're going to reactivate. */
|
---|
2727 | PPGMPAGE pPageSrc = &pCur->RamRange.aPages[0];
|
---|
2728 | uint32_t cPagesLeft = pCur->RamRange.cb >> PAGE_SHIFT;
|
---|
2729 | while (cPagesLeft-- > 0)
|
---|
2730 | {
|
---|
2731 | PGM_PAGE_SET_TRACKING(pPageSrc, 0);
|
---|
2732 | PGM_PAGE_SET_PTE_INDEX(pPageSrc, 0);
|
---|
2733 | pPageSrc++;
|
---|
2734 | }
|
---|
2735 |
|
---|
2736 | /* link in the ram range */
|
---|
2737 | pgmR3PhysLinkRamRange(pVM, &pCur->RamRange, pRamPrev);
|
---|
2738 | pgmUnlock(pVM);
|
---|
2739 |
|
---|
2740 | REMR3NotifyPhysRamRegister(pVM, GCPhys, cb, REM_NOTIFY_PHYS_RAM_FLAGS_MMIO2);
|
---|
2741 | }
|
---|
2742 |
|
---|
2743 | PGMPhysInvalidatePageMapTLB(pVM);
|
---|
2744 | return VINF_SUCCESS;
|
---|
2745 | }
|
---|
2746 |
|
---|
2747 |
|
---|
2748 | /**
|
---|
2749 | * Unmaps a MMIO2 region.
|
---|
2750 | *
|
---|
2751 | * This is done when a guest / the bios / state loading changes the
|
---|
2752 | * PCI config. The replacing of base memory has the same restrictions
|
---|
2753 | * as during registration, of course.
|
---|
2754 | */
|
---|
2755 | VMMR3DECL(int) PGMR3PhysMMIO2Unmap(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS GCPhys)
|
---|
2756 | {
|
---|
2757 | /*
|
---|
2758 | * Validate input
|
---|
2759 | */
|
---|
2760 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
2761 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
2762 | AssertReturn(iRegion <= UINT8_MAX, VERR_INVALID_PARAMETER);
|
---|
2763 | AssertReturn(GCPhys != NIL_RTGCPHYS, VERR_INVALID_PARAMETER);
|
---|
2764 | AssertReturn(GCPhys != 0, VERR_INVALID_PARAMETER);
|
---|
2765 | AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
2766 |
|
---|
2767 | PPGMMMIO2RANGE pCur = pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion);
|
---|
2768 | AssertReturn(pCur, VERR_NOT_FOUND);
|
---|
2769 | AssertReturn(pCur->fMapped, VERR_WRONG_ORDER);
|
---|
2770 | AssertReturn(pCur->RamRange.GCPhys == GCPhys, VERR_INVALID_PARAMETER);
|
---|
2771 | Assert(pCur->RamRange.GCPhysLast != NIL_RTGCPHYS);
|
---|
2772 |
|
---|
2773 | Log(("PGMR3PhysMMIO2Unmap: %RGp-%RGp %s\n",
|
---|
2774 | pCur->RamRange.GCPhys, pCur->RamRange.GCPhysLast, pCur->RamRange.pszDesc));
|
---|
2775 |
|
---|
2776 | /*
|
---|
2777 | * Unmap it.
|
---|
2778 | */
|
---|
2779 | pgmLock(pVM);
|
---|
2780 |
|
---|
2781 | RTGCPHYS GCPhysRangeREM;
|
---|
2782 | RTGCPHYS cbRangeREM;
|
---|
2783 | bool fInformREM;
|
---|
2784 | if (pCur->fOverlapping)
|
---|
2785 | {
|
---|
2786 | /* Restore the RAM pages we've replaced. */
|
---|
2787 | PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesXR3;
|
---|
2788 | while (pRam->GCPhys > pCur->RamRange.GCPhysLast)
|
---|
2789 | pRam = pRam->pNextR3;
|
---|
2790 |
|
---|
2791 | PPGMPAGE pPageDst = &pRam->aPages[(pCur->RamRange.GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
2792 | uint32_t cPagesLeft = pCur->RamRange.cb >> PAGE_SHIFT;
|
---|
2793 | while (cPagesLeft-- > 0)
|
---|
2794 | {
|
---|
2795 | PGM_PAGE_INIT_ZERO(pPageDst, pVM, PGMPAGETYPE_RAM);
|
---|
2796 | pVM->pgm.s.cZeroPages++;
|
---|
2797 | pPageDst++;
|
---|
2798 | }
|
---|
2799 |
|
---|
2800 | /* Flush physical page map TLB. */
|
---|
2801 | PGMPhysInvalidatePageMapTLB(pVM);
|
---|
2802 |
|
---|
2803 | GCPhysRangeREM = NIL_RTGCPHYS; /* shuts up gcc */
|
---|
2804 | cbRangeREM = RTGCPHYS_MAX; /* ditto */
|
---|
2805 | fInformREM = false;
|
---|
2806 | }
|
---|
2807 | else
|
---|
2808 | {
|
---|
2809 | GCPhysRangeREM = pCur->RamRange.GCPhys;
|
---|
2810 | cbRangeREM = pCur->RamRange.cb;
|
---|
2811 | fInformREM = true;
|
---|
2812 |
|
---|
2813 | pgmR3PhysUnlinkRamRange(pVM, &pCur->RamRange);
|
---|
2814 | }
|
---|
2815 |
|
---|
2816 | pCur->RamRange.GCPhys = NIL_RTGCPHYS;
|
---|
2817 | pCur->RamRange.GCPhysLast = NIL_RTGCPHYS;
|
---|
2818 | pCur->fOverlapping = false;
|
---|
2819 | pCur->fMapped = false;
|
---|
2820 |
|
---|
2821 | /* Force a PGM pool flush as guest ram references have been changed. */
|
---|
2822 | /** todo; not entirely SMP safe; assuming for now the guest takes care of this internally (not touch mapped mmio while changing the mapping). */
|
---|
2823 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
2824 | pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
|
---|
2825 | VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
|
---|
2826 |
|
---|
2827 | PGMPhysInvalidatePageMapTLB(pVM);
|
---|
2828 | pgmPhysInvalidRamRangeTlbs(pVM);
|
---|
2829 | pgmUnlock(pVM);
|
---|
2830 |
|
---|
2831 | if (fInformREM)
|
---|
2832 | REMR3NotifyPhysRamDeregister(pVM, GCPhysRangeREM, cbRangeREM);
|
---|
2833 |
|
---|
2834 | return VINF_SUCCESS;
|
---|
2835 | }
|
---|
2836 |
|
---|
2837 |
|
---|
2838 | /**
|
---|
2839 | * Checks if the given address is an MMIO2 base address or not.
|
---|
2840 | *
|
---|
2841 | * @returns true/false accordingly.
|
---|
2842 | * @param pVM Pointer to the shared VM structure.
|
---|
2843 | * @param pDevIns The owner of the memory, optional.
|
---|
2844 | * @param GCPhys The address to check.
|
---|
2845 | */
|
---|
2846 | VMMR3DECL(bool) PGMR3PhysMMIO2IsBase(PVM pVM, PPDMDEVINS pDevIns, RTGCPHYS GCPhys)
|
---|
2847 | {
|
---|
2848 | /*
|
---|
2849 | * Validate input
|
---|
2850 | */
|
---|
2851 | VM_ASSERT_EMT_RETURN(pVM, false);
|
---|
2852 | AssertPtrReturn(pDevIns, false);
|
---|
2853 | AssertReturn(GCPhys != NIL_RTGCPHYS, false);
|
---|
2854 | AssertReturn(GCPhys != 0, false);
|
---|
2855 | AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), false);
|
---|
2856 |
|
---|
2857 | /*
|
---|
2858 | * Search the list.
|
---|
2859 | */
|
---|
2860 | pgmLock(pVM);
|
---|
2861 | for (PPGMMMIO2RANGE pCur = pVM->pgm.s.pMmio2RangesR3; pCur; pCur = pCur->pNextR3)
|
---|
2862 | if (pCur->RamRange.GCPhys == GCPhys)
|
---|
2863 | {
|
---|
2864 | Assert(pCur->fMapped);
|
---|
2865 | pgmUnlock(pVM);
|
---|
2866 | return true;
|
---|
2867 | }
|
---|
2868 | pgmUnlock(pVM);
|
---|
2869 | return false;
|
---|
2870 | }
|
---|
2871 |
|
---|
2872 |
|
---|
2873 | /**
|
---|
2874 | * Gets the HC physical address of a page in the MMIO2 region.
|
---|
2875 | *
|
---|
2876 | * This is API is intended for MMHyper and shouldn't be called
|
---|
2877 | * by anyone else...
|
---|
2878 | *
|
---|
2879 | * @returns VBox status code.
|
---|
2880 | * @param pVM Pointer to the shared VM structure.
|
---|
2881 | * @param pDevIns The owner of the memory, optional.
|
---|
2882 | * @param iRegion The region.
|
---|
2883 | * @param off The page expressed an offset into the MMIO2 region.
|
---|
2884 | * @param pHCPhys Where to store the result.
|
---|
2885 | */
|
---|
2886 | VMMR3DECL(int) PGMR3PhysMMIO2GetHCPhys(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS off, PRTHCPHYS pHCPhys)
|
---|
2887 | {
|
---|
2888 | /*
|
---|
2889 | * Validate input
|
---|
2890 | */
|
---|
2891 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
2892 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
2893 | AssertReturn(iRegion <= UINT8_MAX, VERR_INVALID_PARAMETER);
|
---|
2894 |
|
---|
2895 | pgmLock(pVM);
|
---|
2896 | PPGMMMIO2RANGE pCur = pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion);
|
---|
2897 | AssertReturn(pCur, VERR_NOT_FOUND);
|
---|
2898 | AssertReturn(off < pCur->RamRange.cb, VERR_INVALID_PARAMETER);
|
---|
2899 |
|
---|
2900 | PCPGMPAGE pPage = &pCur->RamRange.aPages[off >> PAGE_SHIFT];
|
---|
2901 | *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage);
|
---|
2902 | pgmUnlock(pVM);
|
---|
2903 | return VINF_SUCCESS;
|
---|
2904 | }
|
---|
2905 |
|
---|
2906 |
|
---|
2907 | /**
|
---|
2908 | * Maps a portion of an MMIO2 region into kernel space (host).
|
---|
2909 | *
|
---|
2910 | * The kernel mapping will become invalid when the MMIO2 memory is deregistered
|
---|
2911 | * or the VM is terminated.
|
---|
2912 | *
|
---|
2913 | * @return VBox status code.
|
---|
2914 | *
|
---|
2915 | * @param pVM Pointer to the shared VM structure.
|
---|
2916 | * @param pDevIns The device owning the MMIO2 memory.
|
---|
2917 | * @param iRegion The region.
|
---|
2918 | * @param off The offset into the region. Must be page aligned.
|
---|
2919 | * @param cb The number of bytes to map. Must be page aligned.
|
---|
2920 | * @param pszDesc Mapping description.
|
---|
2921 | * @param pR0Ptr Where to store the R0 address.
|
---|
2922 | */
|
---|
2923 | VMMR3DECL(int) PGMR3PhysMMIO2MapKernel(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS off, RTGCPHYS cb,
|
---|
2924 | const char *pszDesc, PRTR0PTR pR0Ptr)
|
---|
2925 | {
|
---|
2926 | /*
|
---|
2927 | * Validate input.
|
---|
2928 | */
|
---|
2929 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
2930 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
2931 | AssertReturn(iRegion <= UINT8_MAX, VERR_INVALID_PARAMETER);
|
---|
2932 |
|
---|
2933 | PPGMMMIO2RANGE pCur = pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion);
|
---|
2934 | AssertReturn(pCur, VERR_NOT_FOUND);
|
---|
2935 | AssertReturn(off < pCur->RamRange.cb, VERR_INVALID_PARAMETER);
|
---|
2936 | AssertReturn(cb <= pCur->RamRange.cb, VERR_INVALID_PARAMETER);
|
---|
2937 | AssertReturn(off + cb <= pCur->RamRange.cb, VERR_INVALID_PARAMETER);
|
---|
2938 |
|
---|
2939 | /*
|
---|
2940 | * Pass the request on to the support library/driver.
|
---|
2941 | */
|
---|
2942 | int rc = SUPR3PageMapKernel(pCur->pvR3, off, cb, 0, pR0Ptr);
|
---|
2943 |
|
---|
2944 | return rc;
|
---|
2945 | }
|
---|
2946 |
|
---|
2947 |
|
---|
2948 | /**
|
---|
2949 | * Registers a ROM image.
|
---|
2950 | *
|
---|
2951 | * Shadowed ROM images requires double the amount of backing memory, so,
|
---|
2952 | * don't use that unless you have to. Shadowing of ROM images is process
|
---|
2953 | * where we can select where the reads go and where the writes go. On real
|
---|
2954 | * hardware the chipset provides means to configure this. We provide
|
---|
2955 | * PGMR3PhysProtectROM() for this purpose.
|
---|
2956 | *
|
---|
2957 | * A read-only copy of the ROM image will always be kept around while we
|
---|
2958 | * will allocate RAM pages for the changes on demand (unless all memory
|
---|
2959 | * is configured to be preallocated).
|
---|
2960 | *
|
---|
2961 | * @returns VBox status.
|
---|
2962 | * @param pVM VM Handle.
|
---|
2963 | * @param pDevIns The device instance owning the ROM.
|
---|
2964 | * @param GCPhys First physical address in the range.
|
---|
2965 | * Must be page aligned!
|
---|
2966 | * @param cb The size of the range (in bytes).
|
---|
2967 | * Must be page aligned!
|
---|
2968 | * @param pvBinary Pointer to the binary data backing the ROM image.
|
---|
2969 | * @param cbBinary The size of the binary data pvBinary points to.
|
---|
2970 | * This must be less or equal to @a cb.
|
---|
2971 | * @param fFlags Mask of flags. PGMPHYS_ROM_FLAGS_SHADOWED
|
---|
2972 | * and/or PGMPHYS_ROM_FLAGS_PERMANENT_BINARY.
|
---|
2973 | * @param pszDesc Pointer to description string. This must not be freed.
|
---|
2974 | *
|
---|
2975 | * @remark There is no way to remove the rom, automatically on device cleanup or
|
---|
2976 | * manually from the device yet. This isn't difficult in any way, it's
|
---|
2977 | * just not something we expect to be necessary for a while.
|
---|
2978 | */
|
---|
2979 | VMMR3DECL(int) PGMR3PhysRomRegister(PVM pVM, PPDMDEVINS pDevIns, RTGCPHYS GCPhys, RTGCPHYS cb,
|
---|
2980 | const void *pvBinary, uint32_t cbBinary, uint32_t fFlags, const char *pszDesc)
|
---|
2981 | {
|
---|
2982 | Log(("PGMR3PhysRomRegister: pDevIns=%p GCPhys=%RGp(-%RGp) cb=%RGp pvBinary=%p cbBinary=%#x fFlags=%#x pszDesc=%s\n",
|
---|
2983 | pDevIns, GCPhys, GCPhys + cb, cb, pvBinary, cbBinary, fFlags, pszDesc));
|
---|
2984 |
|
---|
2985 | /*
|
---|
2986 | * Validate input.
|
---|
2987 | */
|
---|
2988 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
2989 | AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
|
---|
2990 | AssertReturn(RT_ALIGN_T(cb, PAGE_SIZE, RTGCPHYS) == cb, VERR_INVALID_PARAMETER);
|
---|
2991 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
2992 | AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
|
---|
2993 | AssertPtrReturn(pvBinary, VERR_INVALID_PARAMETER);
|
---|
2994 | AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
|
---|
2995 | AssertReturn(!(fFlags & ~(PGMPHYS_ROM_FLAGS_SHADOWED | PGMPHYS_ROM_FLAGS_PERMANENT_BINARY)), VERR_INVALID_PARAMETER);
|
---|
2996 | VM_ASSERT_STATE_RETURN(pVM, VMSTATE_CREATING, VERR_VM_INVALID_VM_STATE);
|
---|
2997 |
|
---|
2998 | const uint32_t cPages = cb >> PAGE_SHIFT;
|
---|
2999 |
|
---|
3000 | /*
|
---|
3001 | * Find the ROM location in the ROM list first.
|
---|
3002 | */
|
---|
3003 | PPGMROMRANGE pRomPrev = NULL;
|
---|
3004 | PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3;
|
---|
3005 | while (pRom && GCPhysLast >= pRom->GCPhys)
|
---|
3006 | {
|
---|
3007 | if ( GCPhys <= pRom->GCPhysLast
|
---|
3008 | && GCPhysLast >= pRom->GCPhys)
|
---|
3009 | AssertLogRelMsgFailedReturn(("%RGp-%RGp (%s) conflicts with existing %RGp-%RGp (%s)\n",
|
---|
3010 | GCPhys, GCPhysLast, pszDesc,
|
---|
3011 | pRom->GCPhys, pRom->GCPhysLast, pRom->pszDesc),
|
---|
3012 | VERR_PGM_RAM_CONFLICT);
|
---|
3013 | /* next */
|
---|
3014 | pRomPrev = pRom;
|
---|
3015 | pRom = pRom->pNextR3;
|
---|
3016 | }
|
---|
3017 |
|
---|
3018 | /*
|
---|
3019 | * Find the RAM location and check for conflicts.
|
---|
3020 | *
|
---|
3021 | * Conflict detection is a bit different than for RAM
|
---|
3022 | * registration since a ROM can be located within a RAM
|
---|
3023 | * range. So, what we have to check for is other memory
|
---|
3024 | * types (other than RAM that is) and that we don't span
|
---|
3025 | * more than one RAM range (layz).
|
---|
3026 | */
|
---|
3027 | bool fRamExists = false;
|
---|
3028 | PPGMRAMRANGE pRamPrev = NULL;
|
---|
3029 | PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesXR3;
|
---|
3030 | while (pRam && GCPhysLast >= pRam->GCPhys)
|
---|
3031 | {
|
---|
3032 | if ( GCPhys <= pRam->GCPhysLast
|
---|
3033 | && GCPhysLast >= pRam->GCPhys)
|
---|
3034 | {
|
---|
3035 | /* completely within? */
|
---|
3036 | AssertLogRelMsgReturn( GCPhys >= pRam->GCPhys
|
---|
3037 | && GCPhysLast <= pRam->GCPhysLast,
|
---|
3038 | ("%RGp-%RGp (%s) falls partly outside %RGp-%RGp (%s)\n",
|
---|
3039 | GCPhys, GCPhysLast, pszDesc,
|
---|
3040 | pRam->GCPhys, pRam->GCPhysLast, pRam->pszDesc),
|
---|
3041 | VERR_PGM_RAM_CONFLICT);
|
---|
3042 | fRamExists = true;
|
---|
3043 | break;
|
---|
3044 | }
|
---|
3045 |
|
---|
3046 | /* next */
|
---|
3047 | pRamPrev = pRam;
|
---|
3048 | pRam = pRam->pNextR3;
|
---|
3049 | }
|
---|
3050 | if (fRamExists)
|
---|
3051 | {
|
---|
3052 | PPGMPAGE pPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
3053 | uint32_t cPagesLeft = cPages;
|
---|
3054 | while (cPagesLeft-- > 0)
|
---|
3055 | {
|
---|
3056 | AssertLogRelMsgReturn(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM,
|
---|
3057 | ("%RGp (%R[pgmpage]) isn't a RAM page - registering %RGp-%RGp (%s).\n",
|
---|
3058 | pRam->GCPhys + ((RTGCPHYS)(uintptr_t)(pPage - &pRam->aPages[0]) << PAGE_SHIFT),
|
---|
3059 | pPage, GCPhys, GCPhysLast, pszDesc), VERR_PGM_RAM_CONFLICT);
|
---|
3060 | Assert(PGM_PAGE_IS_ZERO(pPage));
|
---|
3061 | pPage++;
|
---|
3062 | }
|
---|
3063 | }
|
---|
3064 |
|
---|
3065 | /*
|
---|
3066 | * Update the base memory reservation if necessary.
|
---|
3067 | */
|
---|
3068 | uint32_t cExtraBaseCost = fRamExists ? 0 : cPages;
|
---|
3069 | if (fFlags & PGMPHYS_ROM_FLAGS_SHADOWED)
|
---|
3070 | cExtraBaseCost += cPages;
|
---|
3071 | if (cExtraBaseCost)
|
---|
3072 | {
|
---|
3073 | int rc = MMR3IncreaseBaseReservation(pVM, cExtraBaseCost);
|
---|
3074 | if (RT_FAILURE(rc))
|
---|
3075 | return rc;
|
---|
3076 | }
|
---|
3077 |
|
---|
3078 | /*
|
---|
3079 | * Allocate memory for the virgin copy of the RAM.
|
---|
3080 | */
|
---|
3081 | PGMMALLOCATEPAGESREQ pReq;
|
---|
3082 | int rc = GMMR3AllocatePagesPrepare(pVM, &pReq, cPages, GMMACCOUNT_BASE);
|
---|
3083 | AssertRCReturn(rc, rc);
|
---|
3084 |
|
---|
3085 | for (uint32_t iPage = 0; iPage < cPages; iPage++)
|
---|
3086 | {
|
---|
3087 | pReq->aPages[iPage].HCPhysGCPhys = GCPhys + (iPage << PAGE_SHIFT);
|
---|
3088 | pReq->aPages[iPage].idPage = NIL_GMM_PAGEID;
|
---|
3089 | pReq->aPages[iPage].idSharedPage = NIL_GMM_PAGEID;
|
---|
3090 | }
|
---|
3091 |
|
---|
3092 | pgmLock(pVM);
|
---|
3093 | rc = GMMR3AllocatePagesPerform(pVM, pReq);
|
---|
3094 | pgmUnlock(pVM);
|
---|
3095 | if (RT_FAILURE(rc))
|
---|
3096 | {
|
---|
3097 | GMMR3AllocatePagesCleanup(pReq);
|
---|
3098 | return rc;
|
---|
3099 | }
|
---|
3100 |
|
---|
3101 | /*
|
---|
3102 | * Allocate the new ROM range and RAM range (if necessary).
|
---|
3103 | */
|
---|
3104 | PPGMROMRANGE pRomNew;
|
---|
3105 | rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMROMRANGE, aPages[cPages]), 0, MM_TAG_PGM_PHYS, (void **)&pRomNew);
|
---|
3106 | if (RT_SUCCESS(rc))
|
---|
3107 | {
|
---|
3108 | PPGMRAMRANGE pRamNew = NULL;
|
---|
3109 | if (!fRamExists)
|
---|
3110 | rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMRAMRANGE, aPages[cPages]), sizeof(PGMPAGE), MM_TAG_PGM_PHYS, (void **)&pRamNew);
|
---|
3111 | if (RT_SUCCESS(rc))
|
---|
3112 | {
|
---|
3113 | pgmLock(pVM);
|
---|
3114 |
|
---|
3115 | /*
|
---|
3116 | * Initialize and insert the RAM range (if required).
|
---|
3117 | */
|
---|
3118 | PPGMROMPAGE pRomPage = &pRomNew->aPages[0];
|
---|
3119 | if (!fRamExists)
|
---|
3120 | {
|
---|
3121 | pRamNew->pSelfR0 = MMHyperCCToR0(pVM, pRamNew);
|
---|
3122 | pRamNew->pSelfRC = MMHyperCCToRC(pVM, pRamNew);
|
---|
3123 | pRamNew->GCPhys = GCPhys;
|
---|
3124 | pRamNew->GCPhysLast = GCPhysLast;
|
---|
3125 | pRamNew->cb = cb;
|
---|
3126 | pRamNew->pszDesc = pszDesc;
|
---|
3127 | pRamNew->fFlags = PGM_RAM_RANGE_FLAGS_AD_HOC_ROM;
|
---|
3128 | pRamNew->pvR3 = NULL;
|
---|
3129 | pRamNew->paLSPages = NULL;
|
---|
3130 |
|
---|
3131 | PPGMPAGE pPage = &pRamNew->aPages[0];
|
---|
3132 | for (uint32_t iPage = 0; iPage < cPages; iPage++, pPage++, pRomPage++)
|
---|
3133 | {
|
---|
3134 | PGM_PAGE_INIT(pPage,
|
---|
3135 | pReq->aPages[iPage].HCPhysGCPhys,
|
---|
3136 | pReq->aPages[iPage].idPage,
|
---|
3137 | PGMPAGETYPE_ROM,
|
---|
3138 | PGM_PAGE_STATE_ALLOCATED);
|
---|
3139 |
|
---|
3140 | pRomPage->Virgin = *pPage;
|
---|
3141 | }
|
---|
3142 |
|
---|
3143 | pVM->pgm.s.cAllPages += cPages;
|
---|
3144 | pgmR3PhysLinkRamRange(pVM, pRamNew, pRamPrev);
|
---|
3145 | }
|
---|
3146 | else
|
---|
3147 | {
|
---|
3148 | PPGMPAGE pPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
3149 | for (uint32_t iPage = 0; iPage < cPages; iPage++, pPage++, pRomPage++)
|
---|
3150 | {
|
---|
3151 | PGM_PAGE_SET_TYPE(pPage, PGMPAGETYPE_ROM);
|
---|
3152 | PGM_PAGE_SET_HCPHYS(pPage, pReq->aPages[iPage].HCPhysGCPhys);
|
---|
3153 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
|
---|
3154 | PGM_PAGE_SET_PAGEID(pPage, pReq->aPages[iPage].idPage);
|
---|
3155 | PGM_PAGE_SET_PDE_TYPE(pPage, PGM_PAGE_PDE_TYPE_DONTCARE);
|
---|
3156 | PGM_PAGE_SET_PTE_INDEX(pPage, 0);
|
---|
3157 | PGM_PAGE_SET_TRACKING(pPage, 0);
|
---|
3158 |
|
---|
3159 | pRomPage->Virgin = *pPage;
|
---|
3160 | }
|
---|
3161 |
|
---|
3162 | pRamNew = pRam;
|
---|
3163 |
|
---|
3164 | pVM->pgm.s.cZeroPages -= cPages;
|
---|
3165 | }
|
---|
3166 | pVM->pgm.s.cPrivatePages += cPages;
|
---|
3167 |
|
---|
3168 | /* Flush physical page map TLB. */
|
---|
3169 | PGMPhysInvalidatePageMapTLB(pVM);
|
---|
3170 |
|
---|
3171 | pgmUnlock(pVM);
|
---|
3172 |
|
---|
3173 |
|
---|
3174 | /*
|
---|
3175 | * !HACK ALERT! REM + (Shadowed) ROM ==> mess.
|
---|
3176 | *
|
---|
3177 | * If it's shadowed we'll register the handler after the ROM notification
|
---|
3178 | * so we get the access handler callbacks that we should. If it isn't
|
---|
3179 | * shadowed we'll do it the other way around to make REM use the built-in
|
---|
3180 | * ROM behavior and not the handler behavior (which is to route all access
|
---|
3181 | * to PGM atm).
|
---|
3182 | */
|
---|
3183 | if (fFlags & PGMPHYS_ROM_FLAGS_SHADOWED)
|
---|
3184 | {
|
---|
3185 | REMR3NotifyPhysRomRegister(pVM, GCPhys, cb, NULL, true /* fShadowed */);
|
---|
3186 | rc = PGMR3HandlerPhysicalRegister(pVM,
|
---|
3187 | fFlags & PGMPHYS_ROM_FLAGS_SHADOWED
|
---|
3188 | ? PGMPHYSHANDLERTYPE_PHYSICAL_ALL
|
---|
3189 | : PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
|
---|
3190 | GCPhys, GCPhysLast,
|
---|
3191 | pgmR3PhysRomWriteHandler, pRomNew,
|
---|
3192 | NULL, "pgmPhysRomWriteHandler", MMHyperCCToR0(pVM, pRomNew),
|
---|
3193 | NULL, "pgmPhysRomWriteHandler", MMHyperCCToRC(pVM, pRomNew), pszDesc);
|
---|
3194 | }
|
---|
3195 | else
|
---|
3196 | {
|
---|
3197 | rc = PGMR3HandlerPhysicalRegister(pVM,
|
---|
3198 | fFlags & PGMPHYS_ROM_FLAGS_SHADOWED
|
---|
3199 | ? PGMPHYSHANDLERTYPE_PHYSICAL_ALL
|
---|
3200 | : PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
|
---|
3201 | GCPhys, GCPhysLast,
|
---|
3202 | pgmR3PhysRomWriteHandler, pRomNew,
|
---|
3203 | NULL, "pgmPhysRomWriteHandler", MMHyperCCToR0(pVM, pRomNew),
|
---|
3204 | NULL, "pgmPhysRomWriteHandler", MMHyperCCToRC(pVM, pRomNew), pszDesc);
|
---|
3205 | REMR3NotifyPhysRomRegister(pVM, GCPhys, cb, NULL, false /* fShadowed */);
|
---|
3206 | }
|
---|
3207 | if (RT_SUCCESS(rc))
|
---|
3208 | {
|
---|
3209 | pgmLock(pVM);
|
---|
3210 |
|
---|
3211 | /*
|
---|
3212 | * Copy the image over to the virgin pages.
|
---|
3213 | * This must be done after linking in the RAM range.
|
---|
3214 | */
|
---|
3215 | size_t cbBinaryLeft = cbBinary;
|
---|
3216 | PPGMPAGE pRamPage = &pRamNew->aPages[(GCPhys - pRamNew->GCPhys) >> PAGE_SHIFT];
|
---|
3217 | for (uint32_t iPage = 0; iPage < cPages; iPage++, pRamPage++)
|
---|
3218 | {
|
---|
3219 | void *pvDstPage;
|
---|
3220 | rc = pgmPhysPageMap(pVM, pRamPage, GCPhys + (iPage << PAGE_SHIFT), &pvDstPage);
|
---|
3221 | if (RT_FAILURE(rc))
|
---|
3222 | {
|
---|
3223 | VMSetError(pVM, rc, RT_SRC_POS, "Failed to map virgin ROM page at %RGp", GCPhys);
|
---|
3224 | break;
|
---|
3225 | }
|
---|
3226 | if (cbBinaryLeft >= PAGE_SIZE)
|
---|
3227 | {
|
---|
3228 | memcpy(pvDstPage, (uint8_t const *)pvBinary + ((size_t)iPage << PAGE_SHIFT), PAGE_SIZE);
|
---|
3229 | cbBinaryLeft -= PAGE_SIZE;
|
---|
3230 | }
|
---|
3231 | else
|
---|
3232 | {
|
---|
3233 | ASMMemZeroPage(pvDstPage); /* (shouldn't be necessary, but can't hurt either) */
|
---|
3234 | if (cbBinaryLeft > 0)
|
---|
3235 | {
|
---|
3236 | memcpy(pvDstPage, (uint8_t const *)pvBinary + ((size_t)iPage << PAGE_SHIFT), cbBinaryLeft);
|
---|
3237 | cbBinaryLeft = 0;
|
---|
3238 | }
|
---|
3239 | }
|
---|
3240 | }
|
---|
3241 | if (RT_SUCCESS(rc))
|
---|
3242 | {
|
---|
3243 | /*
|
---|
3244 | * Initialize the ROM range.
|
---|
3245 | * Note that the Virgin member of the pages has already been initialized above.
|
---|
3246 | */
|
---|
3247 | pRomNew->GCPhys = GCPhys;
|
---|
3248 | pRomNew->GCPhysLast = GCPhysLast;
|
---|
3249 | pRomNew->cb = cb;
|
---|
3250 | pRomNew->fFlags = fFlags;
|
---|
3251 | pRomNew->idSavedState = UINT8_MAX;
|
---|
3252 | pRomNew->cbOriginal = cbBinary;
|
---|
3253 | #ifdef VBOX_STRICT
|
---|
3254 | pRomNew->pvOriginal = fFlags & PGMPHYS_ROM_FLAGS_PERMANENT_BINARY
|
---|
3255 | ? pvBinary : RTMemDup(pvBinary, cbBinary);
|
---|
3256 | #else
|
---|
3257 | pRomNew->pvOriginal = fFlags & PGMPHYS_ROM_FLAGS_PERMANENT_BINARY ? pvBinary : NULL;
|
---|
3258 | #endif
|
---|
3259 | pRomNew->pszDesc = pszDesc;
|
---|
3260 |
|
---|
3261 | for (unsigned iPage = 0; iPage < cPages; iPage++)
|
---|
3262 | {
|
---|
3263 | PPGMROMPAGE pPage = &pRomNew->aPages[iPage];
|
---|
3264 | pPage->enmProt = PGMROMPROT_READ_ROM_WRITE_IGNORE;
|
---|
3265 | PGM_PAGE_INIT_ZERO(&pPage->Shadow, pVM, PGMPAGETYPE_ROM_SHADOW);
|
---|
3266 | }
|
---|
3267 |
|
---|
3268 | /* update the page count stats for the shadow pages. */
|
---|
3269 | if (fFlags & PGMPHYS_ROM_FLAGS_SHADOWED)
|
---|
3270 | {
|
---|
3271 | pVM->pgm.s.cZeroPages += cPages;
|
---|
3272 | pVM->pgm.s.cAllPages += cPages;
|
---|
3273 | }
|
---|
3274 |
|
---|
3275 | /*
|
---|
3276 | * Insert the ROM range, tell REM and return successfully.
|
---|
3277 | */
|
---|
3278 | pRomNew->pNextR3 = pRom;
|
---|
3279 | pRomNew->pNextR0 = pRom ? MMHyperCCToR0(pVM, pRom) : NIL_RTR0PTR;
|
---|
3280 | pRomNew->pNextRC = pRom ? MMHyperCCToRC(pVM, pRom) : NIL_RTRCPTR;
|
---|
3281 |
|
---|
3282 | if (pRomPrev)
|
---|
3283 | {
|
---|
3284 | pRomPrev->pNextR3 = pRomNew;
|
---|
3285 | pRomPrev->pNextR0 = MMHyperCCToR0(pVM, pRomNew);
|
---|
3286 | pRomPrev->pNextRC = MMHyperCCToRC(pVM, pRomNew);
|
---|
3287 | }
|
---|
3288 | else
|
---|
3289 | {
|
---|
3290 | pVM->pgm.s.pRomRangesR3 = pRomNew;
|
---|
3291 | pVM->pgm.s.pRomRangesR0 = MMHyperCCToR0(pVM, pRomNew);
|
---|
3292 | pVM->pgm.s.pRomRangesRC = MMHyperCCToRC(pVM, pRomNew);
|
---|
3293 | }
|
---|
3294 |
|
---|
3295 | PGMPhysInvalidatePageMapTLB(pVM);
|
---|
3296 | GMMR3AllocatePagesCleanup(pReq);
|
---|
3297 | pgmUnlock(pVM);
|
---|
3298 | return VINF_SUCCESS;
|
---|
3299 | }
|
---|
3300 |
|
---|
3301 | /* bail out */
|
---|
3302 |
|
---|
3303 | pgmUnlock(pVM);
|
---|
3304 | int rc2 = PGMHandlerPhysicalDeregister(pVM, GCPhys);
|
---|
3305 | AssertRC(rc2);
|
---|
3306 | pgmLock(pVM);
|
---|
3307 | }
|
---|
3308 |
|
---|
3309 | if (!fRamExists)
|
---|
3310 | {
|
---|
3311 | pgmR3PhysUnlinkRamRange2(pVM, pRamNew, pRamPrev);
|
---|
3312 | MMHyperFree(pVM, pRamNew);
|
---|
3313 | }
|
---|
3314 | }
|
---|
3315 | MMHyperFree(pVM, pRomNew);
|
---|
3316 | }
|
---|
3317 |
|
---|
3318 | /** @todo Purge the mapping cache or something... */
|
---|
3319 | GMMR3FreeAllocatedPages(pVM, pReq);
|
---|
3320 | GMMR3AllocatePagesCleanup(pReq);
|
---|
3321 | pgmUnlock(pVM);
|
---|
3322 | return rc;
|
---|
3323 | }
|
---|
3324 |
|
---|
3325 |
|
---|
3326 | /**
|
---|
3327 | * \#PF Handler callback for ROM write accesses.
|
---|
3328 | *
|
---|
3329 | * @returns VINF_SUCCESS if the handler have carried out the operation.
|
---|
3330 | * @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
|
---|
3331 | * @param pVM VM Handle.
|
---|
3332 | * @param GCPhys The physical address the guest is writing to.
|
---|
3333 | * @param pvPhys The HC mapping of that address.
|
---|
3334 | * @param pvBuf What the guest is reading/writing.
|
---|
3335 | * @param cbBuf How much it's reading/writing.
|
---|
3336 | * @param enmAccessType The access type.
|
---|
3337 | * @param pvUser User argument.
|
---|
3338 | */
|
---|
3339 | static DECLCALLBACK(int) pgmR3PhysRomWriteHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf,
|
---|
3340 | PGMACCESSTYPE enmAccessType, void *pvUser)
|
---|
3341 | {
|
---|
3342 | PPGMROMRANGE pRom = (PPGMROMRANGE)pvUser;
|
---|
3343 | const uint32_t iPage = (GCPhys - pRom->GCPhys) >> PAGE_SHIFT;
|
---|
3344 | Assert(iPage < (pRom->cb >> PAGE_SHIFT));
|
---|
3345 | PPGMROMPAGE pRomPage = &pRom->aPages[iPage];
|
---|
3346 | Log5(("pgmR3PhysRomWriteHandler: %d %c %#08RGp %#04zx\n", pRomPage->enmProt, enmAccessType == PGMACCESSTYPE_READ ? 'R' : 'W', GCPhys, cbBuf));
|
---|
3347 |
|
---|
3348 | if (enmAccessType == PGMACCESSTYPE_READ)
|
---|
3349 | {
|
---|
3350 | switch (pRomPage->enmProt)
|
---|
3351 | {
|
---|
3352 | /*
|
---|
3353 | * Take the default action.
|
---|
3354 | */
|
---|
3355 | case PGMROMPROT_READ_ROM_WRITE_IGNORE:
|
---|
3356 | case PGMROMPROT_READ_RAM_WRITE_IGNORE:
|
---|
3357 | case PGMROMPROT_READ_ROM_WRITE_RAM:
|
---|
3358 | case PGMROMPROT_READ_RAM_WRITE_RAM:
|
---|
3359 | return VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
3360 |
|
---|
3361 | default:
|
---|
3362 | AssertMsgFailedReturn(("enmProt=%d iPage=%d GCPhys=%RGp\n",
|
---|
3363 | pRom->aPages[iPage].enmProt, iPage, GCPhys),
|
---|
3364 | VERR_INTERNAL_ERROR);
|
---|
3365 | }
|
---|
3366 | }
|
---|
3367 | else
|
---|
3368 | {
|
---|
3369 | Assert(enmAccessType == PGMACCESSTYPE_WRITE);
|
---|
3370 | switch (pRomPage->enmProt)
|
---|
3371 | {
|
---|
3372 | /*
|
---|
3373 | * Ignore writes.
|
---|
3374 | */
|
---|
3375 | case PGMROMPROT_READ_ROM_WRITE_IGNORE:
|
---|
3376 | case PGMROMPROT_READ_RAM_WRITE_IGNORE:
|
---|
3377 | return VINF_SUCCESS;
|
---|
3378 |
|
---|
3379 | /*
|
---|
3380 | * Write to the RAM page.
|
---|
3381 | */
|
---|
3382 | case PGMROMPROT_READ_ROM_WRITE_RAM:
|
---|
3383 | case PGMROMPROT_READ_RAM_WRITE_RAM: /* yes this will get here too, it's *way* simpler that way. */
|
---|
3384 | {
|
---|
3385 | /* This should be impossible now, pvPhys doesn't work cross page anylonger. */
|
---|
3386 | Assert(((GCPhys - pRom->GCPhys + cbBuf - 1) >> PAGE_SHIFT) == iPage);
|
---|
3387 |
|
---|
3388 | /*
|
---|
3389 | * Take the lock, do lazy allocation, map the page and copy the data.
|
---|
3390 | *
|
---|
3391 | * Note that we have to bypass the mapping TLB since it works on
|
---|
3392 | * guest physical addresses and entering the shadow page would
|
---|
3393 | * kind of screw things up...
|
---|
3394 | */
|
---|
3395 | int rc = pgmLock(pVM);
|
---|
3396 | AssertRC(rc);
|
---|
3397 |
|
---|
3398 | PPGMPAGE pShadowPage = &pRomPage->Shadow;
|
---|
3399 | if (!PGMROMPROT_IS_ROM(pRomPage->enmProt))
|
---|
3400 | {
|
---|
3401 | pShadowPage = pgmPhysGetPage(pVM, GCPhys);
|
---|
3402 | AssertLogRelReturn(pShadowPage, VERR_INTERNAL_ERROR);
|
---|
3403 | }
|
---|
3404 |
|
---|
3405 | void *pvDstPage;
|
---|
3406 | rc = pgmPhysPageMakeWritableAndMap(pVM, pShadowPage, GCPhys & X86_PTE_PG_MASK, &pvDstPage);
|
---|
3407 | if (RT_SUCCESS(rc))
|
---|
3408 | {
|
---|
3409 | memcpy((uint8_t *)pvDstPage + (GCPhys & PAGE_OFFSET_MASK), pvBuf, cbBuf);
|
---|
3410 | pRomPage->LiveSave.fWrittenTo = true;
|
---|
3411 | }
|
---|
3412 |
|
---|
3413 | pgmUnlock(pVM);
|
---|
3414 | return rc;
|
---|
3415 | }
|
---|
3416 |
|
---|
3417 | default:
|
---|
3418 | AssertMsgFailedReturn(("enmProt=%d iPage=%d GCPhys=%RGp\n",
|
---|
3419 | pRom->aPages[iPage].enmProt, iPage, GCPhys),
|
---|
3420 | VERR_INTERNAL_ERROR);
|
---|
3421 | }
|
---|
3422 | }
|
---|
3423 | }
|
---|
3424 |
|
---|
3425 |
|
---|
3426 | /**
|
---|
3427 | * Called by PGMR3Reset to reset the shadow, switch to the virgin,
|
---|
3428 | * and verify that the virgin part is untouched.
|
---|
3429 | *
|
---|
3430 | * This is done after the normal memory has been cleared.
|
---|
3431 | *
|
---|
3432 | * ASSUMES that the caller owns the PGM lock.
|
---|
3433 | *
|
---|
3434 | * @param pVM The VM handle.
|
---|
3435 | */
|
---|
3436 | int pgmR3PhysRomReset(PVM pVM)
|
---|
3437 | {
|
---|
3438 | Assert(PGMIsLockOwner(pVM));
|
---|
3439 | for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
|
---|
3440 | {
|
---|
3441 | const uint32_t cPages = pRom->cb >> PAGE_SHIFT;
|
---|
3442 |
|
---|
3443 | if (pRom->fFlags & PGMPHYS_ROM_FLAGS_SHADOWED)
|
---|
3444 | {
|
---|
3445 | /*
|
---|
3446 | * Reset the physical handler.
|
---|
3447 | */
|
---|
3448 | int rc = PGMR3PhysRomProtect(pVM, pRom->GCPhys, pRom->cb, PGMROMPROT_READ_ROM_WRITE_IGNORE);
|
---|
3449 | AssertRCReturn(rc, rc);
|
---|
3450 |
|
---|
3451 | /*
|
---|
3452 | * What we do with the shadow pages depends on the memory
|
---|
3453 | * preallocation option. If not enabled, we'll just throw
|
---|
3454 | * out all the dirty pages and replace them by the zero page.
|
---|
3455 | */
|
---|
3456 | if (!pVM->pgm.s.fRamPreAlloc)
|
---|
3457 | {
|
---|
3458 | /* Free the dirty pages. */
|
---|
3459 | uint32_t cPendingPages = 0;
|
---|
3460 | PGMMFREEPAGESREQ pReq;
|
---|
3461 | rc = GMMR3FreePagesPrepare(pVM, &pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE, GMMACCOUNT_BASE);
|
---|
3462 | AssertRCReturn(rc, rc);
|
---|
3463 |
|
---|
3464 | for (uint32_t iPage = 0; iPage < cPages; iPage++)
|
---|
3465 | if ( !PGM_PAGE_IS_ZERO(&pRom->aPages[iPage].Shadow)
|
---|
3466 | && !PGM_PAGE_IS_BALLOONED(&pRom->aPages[iPage].Shadow))
|
---|
3467 | {
|
---|
3468 | Assert(PGM_PAGE_GET_STATE(&pRom->aPages[iPage].Shadow) == PGM_PAGE_STATE_ALLOCATED);
|
---|
3469 | rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, &pRom->aPages[iPage].Shadow,
|
---|
3470 | pRom->GCPhys + (iPage << PAGE_SHIFT));
|
---|
3471 | AssertLogRelRCReturn(rc, rc);
|
---|
3472 | }
|
---|
3473 |
|
---|
3474 | if (cPendingPages)
|
---|
3475 | {
|
---|
3476 | rc = GMMR3FreePagesPerform(pVM, pReq, cPendingPages);
|
---|
3477 | AssertLogRelRCReturn(rc, rc);
|
---|
3478 | }
|
---|
3479 | GMMR3FreePagesCleanup(pReq);
|
---|
3480 | }
|
---|
3481 | else
|
---|
3482 | {
|
---|
3483 | /* clear all the shadow pages. */
|
---|
3484 | for (uint32_t iPage = 0; iPage < cPages; iPage++)
|
---|
3485 | {
|
---|
3486 | if (PGM_PAGE_IS_ZERO(&pRom->aPages[iPage].Shadow))
|
---|
3487 | continue;
|
---|
3488 | Assert(!PGM_PAGE_IS_BALLOONED(&pRom->aPages[iPage].Shadow));
|
---|
3489 | void *pvDstPage;
|
---|
3490 | const RTGCPHYS GCPhys = pRom->GCPhys + (iPage << PAGE_SHIFT);
|
---|
3491 | rc = pgmPhysPageMakeWritableAndMap(pVM, &pRom->aPages[iPage].Shadow, GCPhys, &pvDstPage);
|
---|
3492 | if (RT_FAILURE(rc))
|
---|
3493 | break;
|
---|
3494 | ASMMemZeroPage(pvDstPage);
|
---|
3495 | }
|
---|
3496 | AssertRCReturn(rc, rc);
|
---|
3497 | }
|
---|
3498 | }
|
---|
3499 |
|
---|
3500 | #ifdef VBOX_STRICT
|
---|
3501 | /*
|
---|
3502 | * Verify that the virgin page is unchanged if possible.
|
---|
3503 | */
|
---|
3504 | if (pRom->pvOriginal)
|
---|
3505 | {
|
---|
3506 | size_t cbSrcLeft = pRom->cbOriginal;
|
---|
3507 | uint8_t const *pbSrcPage = (uint8_t const *)pRom->pvOriginal;
|
---|
3508 | for (uint32_t iPage = 0; iPage < cPages && cbSrcLeft > 0; iPage++, pbSrcPage += PAGE_SIZE)
|
---|
3509 | {
|
---|
3510 | const RTGCPHYS GCPhys = pRom->GCPhys + (iPage << PAGE_SHIFT);
|
---|
3511 | void const *pvDstPage;
|
---|
3512 | int rc = pgmPhysPageMapReadOnly(pVM, &pRom->aPages[iPage].Virgin, GCPhys, &pvDstPage);
|
---|
3513 | if (RT_FAILURE(rc))
|
---|
3514 | break;
|
---|
3515 |
|
---|
3516 | if (memcmp(pvDstPage, pbSrcPage, RT_MIN(cbSrcLeft, PAGE_SIZE)))
|
---|
3517 | LogRel(("pgmR3PhysRomReset: %RGp rom page changed (%s) - loaded saved state?\n",
|
---|
3518 | GCPhys, pRom->pszDesc));
|
---|
3519 | cbSrcLeft -= RT_MIN(cbSrcLeft, PAGE_SIZE);
|
---|
3520 | }
|
---|
3521 | }
|
---|
3522 | #endif
|
---|
3523 | }
|
---|
3524 |
|
---|
3525 | return VINF_SUCCESS;
|
---|
3526 | }
|
---|
3527 |
|
---|
3528 |
|
---|
3529 | /**
|
---|
3530 | * Called by PGMR3Term to free resources.
|
---|
3531 | *
|
---|
3532 | * ASSUMES that the caller owns the PGM lock.
|
---|
3533 | *
|
---|
3534 | * @param pVM The VM handle.
|
---|
3535 | */
|
---|
3536 | void pgmR3PhysRomTerm(PVM pVM)
|
---|
3537 | {
|
---|
3538 | #ifdef RT_STRICT
|
---|
3539 | /*
|
---|
3540 | * Free the heap copy of the original bits.
|
---|
3541 | */
|
---|
3542 | for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
|
---|
3543 | {
|
---|
3544 | if ( pRom->pvOriginal
|
---|
3545 | && !(pRom->fFlags & PGMPHYS_ROM_FLAGS_PERMANENT_BINARY))
|
---|
3546 | {
|
---|
3547 | RTMemFree((void *)pRom->pvOriginal);
|
---|
3548 | pRom->pvOriginal = NULL;
|
---|
3549 | }
|
---|
3550 | }
|
---|
3551 | #endif
|
---|
3552 | }
|
---|
3553 |
|
---|
3554 |
|
---|
3555 | /**
|
---|
3556 | * Change the shadowing of a range of ROM pages.
|
---|
3557 | *
|
---|
3558 | * This is intended for implementing chipset specific memory registers
|
---|
3559 | * and will not be very strict about the input. It will silently ignore
|
---|
3560 | * any pages that are not the part of a shadowed ROM.
|
---|
3561 | *
|
---|
3562 | * @returns VBox status code.
|
---|
3563 | * @retval VINF_PGM_SYNC_CR3
|
---|
3564 | *
|
---|
3565 | * @param pVM Pointer to the shared VM structure.
|
---|
3566 | * @param GCPhys Where to start. Page aligned.
|
---|
3567 | * @param cb How much to change. Page aligned.
|
---|
3568 | * @param enmProt The new ROM protection.
|
---|
3569 | */
|
---|
3570 | VMMR3DECL(int) PGMR3PhysRomProtect(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, PGMROMPROT enmProt)
|
---|
3571 | {
|
---|
3572 | /*
|
---|
3573 | * Check input
|
---|
3574 | */
|
---|
3575 | if (!cb)
|
---|
3576 | return VINF_SUCCESS;
|
---|
3577 | AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
3578 | AssertReturn(!(cb & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
3579 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
3580 | AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
|
---|
3581 | AssertReturn(enmProt >= PGMROMPROT_INVALID && enmProt <= PGMROMPROT_END, VERR_INVALID_PARAMETER);
|
---|
3582 |
|
---|
3583 | /*
|
---|
3584 | * Process the request.
|
---|
3585 | */
|
---|
3586 | pgmLock(pVM);
|
---|
3587 | int rc = VINF_SUCCESS;
|
---|
3588 | bool fFlushTLB = false;
|
---|
3589 | for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
|
---|
3590 | {
|
---|
3591 | if ( GCPhys <= pRom->GCPhysLast
|
---|
3592 | && GCPhysLast >= pRom->GCPhys
|
---|
3593 | && (pRom->fFlags & PGMPHYS_ROM_FLAGS_SHADOWED))
|
---|
3594 | {
|
---|
3595 | /*
|
---|
3596 | * Iterate the relevant pages and make necessary the changes.
|
---|
3597 | */
|
---|
3598 | bool fChanges = false;
|
---|
3599 | uint32_t const cPages = pRom->GCPhysLast <= GCPhysLast
|
---|
3600 | ? pRom->cb >> PAGE_SHIFT
|
---|
3601 | : (GCPhysLast - pRom->GCPhys + 1) >> PAGE_SHIFT;
|
---|
3602 | for (uint32_t iPage = (GCPhys - pRom->GCPhys) >> PAGE_SHIFT;
|
---|
3603 | iPage < cPages;
|
---|
3604 | iPage++)
|
---|
3605 | {
|
---|
3606 | PPGMROMPAGE pRomPage = &pRom->aPages[iPage];
|
---|
3607 | if (PGMROMPROT_IS_ROM(pRomPage->enmProt) != PGMROMPROT_IS_ROM(enmProt))
|
---|
3608 | {
|
---|
3609 | fChanges = true;
|
---|
3610 |
|
---|
3611 | /* flush references to the page. */
|
---|
3612 | PPGMPAGE pRamPage = pgmPhysGetPage(pVM, pRom->GCPhys + (iPage << PAGE_SHIFT));
|
---|
3613 | int rc2 = pgmPoolTrackUpdateGCPhys(pVM, pRom->GCPhys + (iPage << PAGE_SHIFT), pRamPage,
|
---|
3614 | true /*fFlushPTEs*/, &fFlushTLB);
|
---|
3615 | if (rc2 != VINF_SUCCESS && (rc == VINF_SUCCESS || RT_FAILURE(rc2)))
|
---|
3616 | rc = rc2;
|
---|
3617 |
|
---|
3618 | PPGMPAGE pOld = PGMROMPROT_IS_ROM(pRomPage->enmProt) ? &pRomPage->Virgin : &pRomPage->Shadow;
|
---|
3619 | PPGMPAGE pNew = PGMROMPROT_IS_ROM(pRomPage->enmProt) ? &pRomPage->Shadow : &pRomPage->Virgin;
|
---|
3620 |
|
---|
3621 | *pOld = *pRamPage;
|
---|
3622 | *pRamPage = *pNew;
|
---|
3623 | /** @todo preserve the volatile flags (handlers) when these have been moved out of HCPhys! */
|
---|
3624 | }
|
---|
3625 | pRomPage->enmProt = enmProt;
|
---|
3626 | }
|
---|
3627 |
|
---|
3628 | /*
|
---|
3629 | * Reset the access handler if we made changes, no need
|
---|
3630 | * to optimize this.
|
---|
3631 | */
|
---|
3632 | if (fChanges)
|
---|
3633 | {
|
---|
3634 | int rc2 = PGMHandlerPhysicalReset(pVM, pRom->GCPhys);
|
---|
3635 | if (RT_FAILURE(rc2))
|
---|
3636 | {
|
---|
3637 | pgmUnlock(pVM);
|
---|
3638 | AssertRC(rc);
|
---|
3639 | return rc2;
|
---|
3640 | }
|
---|
3641 | }
|
---|
3642 |
|
---|
3643 | /* Advance - cb isn't updated. */
|
---|
3644 | GCPhys = pRom->GCPhys + (cPages << PAGE_SHIFT);
|
---|
3645 | }
|
---|
3646 | }
|
---|
3647 | pgmUnlock(pVM);
|
---|
3648 | if (fFlushTLB)
|
---|
3649 | PGM_INVL_ALL_VCPU_TLBS(pVM);
|
---|
3650 |
|
---|
3651 | return rc;
|
---|
3652 | }
|
---|
3653 |
|
---|
3654 |
|
---|
3655 | /**
|
---|
3656 | * Sets the Address Gate 20 state.
|
---|
3657 | *
|
---|
3658 | * @param pVCpu The VCPU to operate on.
|
---|
3659 | * @param fEnable True if the gate should be enabled.
|
---|
3660 | * False if the gate should be disabled.
|
---|
3661 | */
|
---|
3662 | VMMDECL(void) PGMR3PhysSetA20(PVMCPU pVCpu, bool fEnable)
|
---|
3663 | {
|
---|
3664 | LogFlow(("PGMR3PhysSetA20 %d (was %d)\n", fEnable, pVCpu->pgm.s.fA20Enabled));
|
---|
3665 | if (pVCpu->pgm.s.fA20Enabled != fEnable)
|
---|
3666 | {
|
---|
3667 | pVCpu->pgm.s.fA20Enabled = fEnable;
|
---|
3668 | pVCpu->pgm.s.GCPhysA20Mask = ~(RTGCPHYS)(!fEnable << 20);
|
---|
3669 | REMR3A20Set(pVCpu->pVMR3, pVCpu, fEnable);
|
---|
3670 | /** @todo we're not handling this correctly for VT-x / AMD-V. See #2911 */
|
---|
3671 | }
|
---|
3672 | }
|
---|
3673 |
|
---|
3674 | #ifdef PGM_WITH_LARGE_ADDRESS_SPACE_ON_32_BIT_HOST
|
---|
3675 | /**
|
---|
3676 | * Tree enumeration callback for dealing with age rollover.
|
---|
3677 | * It will perform a simple compression of the current age.
|
---|
3678 | */
|
---|
3679 | static DECLCALLBACK(int) pgmR3PhysChunkAgeingRolloverCallback(PAVLU32NODECORE pNode, void *pvUser)
|
---|
3680 | {
|
---|
3681 | Assert(PGMIsLockOwner((PVM)pvUser));
|
---|
3682 | /* Age compression - ASSUMES iNow == 4. */
|
---|
3683 | PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)pNode;
|
---|
3684 | if (pChunk->iAge >= UINT32_C(0xffffff00))
|
---|
3685 | pChunk->iAge = 3;
|
---|
3686 | else if (pChunk->iAge >= UINT32_C(0xfffff000))
|
---|
3687 | pChunk->iAge = 2;
|
---|
3688 | else if (pChunk->iAge)
|
---|
3689 | pChunk->iAge = 1;
|
---|
3690 | else /* iAge = 0 */
|
---|
3691 | pChunk->iAge = 4;
|
---|
3692 | return 0;
|
---|
3693 | }
|
---|
3694 |
|
---|
3695 |
|
---|
3696 | /**
|
---|
3697 | * Tree enumeration callback that updates the chunks that have
|
---|
3698 | * been used since the last
|
---|
3699 | */
|
---|
3700 | static DECLCALLBACK(int) pgmR3PhysChunkAgeingCallback(PAVLU32NODECORE pNode, void *pvUser)
|
---|
3701 | {
|
---|
3702 | PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)pNode;
|
---|
3703 | if (!pChunk->iAge)
|
---|
3704 | {
|
---|
3705 | PVM pVM = (PVM)pvUser;
|
---|
3706 | pChunk->iAge = pVM->pgm.s.ChunkR3Map.iNow;
|
---|
3707 | }
|
---|
3708 | return 0;
|
---|
3709 | }
|
---|
3710 |
|
---|
3711 |
|
---|
3712 | /**
|
---|
3713 | * Performs ageing of the ring-3 chunk mappings.
|
---|
3714 | *
|
---|
3715 | * @param pVM The VM handle.
|
---|
3716 | */
|
---|
3717 | VMMR3DECL(void) PGMR3PhysChunkAgeing(PVM pVM)
|
---|
3718 | {
|
---|
3719 | pgmLock(pVM);
|
---|
3720 | pVM->pgm.s.ChunkR3Map.AgeingCountdown = RT_MIN(pVM->pgm.s.ChunkR3Map.cMax / 4, 1024);
|
---|
3721 | pVM->pgm.s.ChunkR3Map.iNow++;
|
---|
3722 | if (pVM->pgm.s.ChunkR3Map.iNow == 0)
|
---|
3723 | {
|
---|
3724 | pVM->pgm.s.ChunkR3Map.iNow = 4;
|
---|
3725 | RTAvlU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pTree, true /*fFromLeft*/, pgmR3PhysChunkAgeingRolloverCallback, pVM);
|
---|
3726 | }
|
---|
3727 | else
|
---|
3728 | RTAvlU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pTree, true /*fFromLeft*/, pgmR3PhysChunkAgeingCallback, pVM);
|
---|
3729 | pgmUnlock(pVM);
|
---|
3730 | }
|
---|
3731 |
|
---|
3732 |
|
---|
3733 | /**
|
---|
3734 | * The structure passed in the pvUser argument of pgmR3PhysChunkUnmapCandidateCallback().
|
---|
3735 | */
|
---|
3736 | typedef struct PGMR3PHYSCHUNKUNMAPCB
|
---|
3737 | {
|
---|
3738 | PVM pVM; /**< The VM handle. */
|
---|
3739 | PPGMCHUNKR3MAP pChunk; /**< The chunk to unmap. */
|
---|
3740 | uint32_t iLastAge; /**< Highest age found so far. */
|
---|
3741 | } PGMR3PHYSCHUNKUNMAPCB, *PPGMR3PHYSCHUNKUNMAPCB;
|
---|
3742 |
|
---|
3743 |
|
---|
3744 | /**
|
---|
3745 | * Callback used to find the mapping that's been unused for
|
---|
3746 | * the longest time.
|
---|
3747 | */
|
---|
3748 | static DECLCALLBACK(int) pgmR3PhysChunkUnmapCandidateCallback(PAVLU32NODECORE pNode, void *pvUser)
|
---|
3749 | {
|
---|
3750 | PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)pNode;
|
---|
3751 | PPGMR3PHYSCHUNKUNMAPCB pArg = (PPGMR3PHYSCHUNKUNMAPCB)pvUser;
|
---|
3752 |
|
---|
3753 | if ( pChunk->iAge
|
---|
3754 | && !pChunk->cRefs
|
---|
3755 | && pArg->iLastAge < pChunk->iAge)
|
---|
3756 | {
|
---|
3757 | /*
|
---|
3758 | * Check that it's not in any of the TLBs.
|
---|
3759 | */
|
---|
3760 | PVM pVM = pArg->pVM;
|
---|
3761 | for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.ChunkR3Map.Tlb.aEntries); i++)
|
---|
3762 | if (pVM->pgm.s.ChunkR3Map.Tlb.aEntries[i].pChunk == pChunk)
|
---|
3763 | {
|
---|
3764 | pChunk = NULL;
|
---|
3765 | break;
|
---|
3766 | }
|
---|
3767 | if (pChunk)
|
---|
3768 | for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.PhysTlbHC.aEntries); i++)
|
---|
3769 | if (pVM->pgm.s.PhysTlbHC.aEntries[i].pMap == pChunk)
|
---|
3770 | {
|
---|
3771 | pChunk = NULL;
|
---|
3772 | break;
|
---|
3773 | }
|
---|
3774 | if (pChunk)
|
---|
3775 | {
|
---|
3776 | pArg->pChunk = pChunk;
|
---|
3777 | pArg->iLastAge = pChunk->iAge;
|
---|
3778 | }
|
---|
3779 | }
|
---|
3780 | return 0;
|
---|
3781 | }
|
---|
3782 |
|
---|
3783 |
|
---|
3784 | /**
|
---|
3785 | * Finds a good candidate for unmapping when the ring-3 mapping cache is full.
|
---|
3786 | *
|
---|
3787 | * The candidate will not be part of any TLBs, so no need to flush
|
---|
3788 | * anything afterwards.
|
---|
3789 | *
|
---|
3790 | * @returns Chunk id.
|
---|
3791 | * @param pVM The VM handle.
|
---|
3792 | */
|
---|
3793 | static int32_t pgmR3PhysChunkFindUnmapCandidate(PVM pVM)
|
---|
3794 | {
|
---|
3795 | Assert(PGMIsLockOwner(pVM));
|
---|
3796 |
|
---|
3797 | /*
|
---|
3798 | * Do tree ageing first?
|
---|
3799 | */
|
---|
3800 | if (pVM->pgm.s.ChunkR3Map.AgeingCountdown-- == 0)
|
---|
3801 | {
|
---|
3802 | STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pStats)->StatChunkAging, a);
|
---|
3803 | PGMR3PhysChunkAgeing(pVM);
|
---|
3804 | STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->StatChunkAging, a);
|
---|
3805 | }
|
---|
3806 |
|
---|
3807 | /*
|
---|
3808 | * Enumerate the age tree starting with the left most node.
|
---|
3809 | */
|
---|
3810 | STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pStats)->StatChunkFindCandidate, a);
|
---|
3811 | PGMR3PHYSCHUNKUNMAPCB Args;
|
---|
3812 | Args.pVM = pVM;
|
---|
3813 | Args.pChunk = NULL;
|
---|
3814 | Args.iLastAge = 0;
|
---|
3815 | RTAvlU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pTree, true /*fFromLeft*/, pgmR3PhysChunkUnmapCandidateCallback, &Args);
|
---|
3816 | Assert(Args.pChunk);
|
---|
3817 | if (Args.pChunk)
|
---|
3818 | {
|
---|
3819 | STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->StatChunkFindCandidate, a);
|
---|
3820 | return Args.pChunk->Core.Key;
|
---|
3821 | }
|
---|
3822 |
|
---|
3823 | STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->StatChunkFindCandidate, a);
|
---|
3824 | return INT32_MAX;
|
---|
3825 | }
|
---|
3826 |
|
---|
3827 | /**
|
---|
3828 | * Rendezvous callback used by pgmR3PhysUnmapChunk that unmaps a chunk
|
---|
3829 | *
|
---|
3830 | * This is only called on one of the EMTs while the other ones are waiting for
|
---|
3831 | * it to complete this function.
|
---|
3832 | *
|
---|
3833 | * @returns VINF_SUCCESS (VBox strict status code).
|
---|
3834 | * @param pVM The VM handle.
|
---|
3835 | * @param pVCpu The VMCPU for the EMT we're being called on. Unused.
|
---|
3836 | * @param pvUser User pointer. Unused
|
---|
3837 | *
|
---|
3838 | */
|
---|
3839 | DECLCALLBACK(VBOXSTRICTRC) pgmR3PhysUnmapChunkRendezvous(PVM pVM, PVMCPU pVCpu, void *pvUser)
|
---|
3840 | {
|
---|
3841 | int rc = VINF_SUCCESS;
|
---|
3842 | pgmLock(pVM);
|
---|
3843 |
|
---|
3844 | if (pVM->pgm.s.ChunkR3Map.c >= pVM->pgm.s.ChunkR3Map.cMax)
|
---|
3845 | {
|
---|
3846 | /* Flush the pgm pool cache; call the internal rendezvous handler as we're already in a rendezvous handler here. */
|
---|
3847 | /* todo: also not really efficient to unmap a chunk that contains PD or PT pages. */
|
---|
3848 | pgmR3PoolClearAllRendezvous(pVM, &pVM->aCpus[0], NULL /* no need to flush the REM TLB as we already did that above */);
|
---|
3849 |
|
---|
3850 | /*
|
---|
3851 | * Request the ring-0 part to unmap a chunk to make space in the mapping cache.
|
---|
3852 | */
|
---|
3853 | GMMMAPUNMAPCHUNKREQ Req;
|
---|
3854 | Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
3855 | Req.Hdr.cbReq = sizeof(Req);
|
---|
3856 | Req.pvR3 = NULL;
|
---|
3857 | Req.idChunkMap = NIL_GMM_CHUNKID;
|
---|
3858 | Req.idChunkUnmap = pgmR3PhysChunkFindUnmapCandidate(pVM);
|
---|
3859 |
|
---|
3860 | if (Req.idChunkUnmap != INT32_MAX)
|
---|
3861 | {
|
---|
3862 | STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pStats)->StatChunkUnmap, a);
|
---|
3863 | rc = VMMR3CallR0(pVM, VMMR0_DO_GMM_MAP_UNMAP_CHUNK, 0, &Req.Hdr);
|
---|
3864 | STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->StatChunkUnmap, a);
|
---|
3865 | if (RT_SUCCESS(rc))
|
---|
3866 | {
|
---|
3867 | /* remove the unmapped one. */
|
---|
3868 | PPGMCHUNKR3MAP pUnmappedChunk = (PPGMCHUNKR3MAP)RTAvlU32Remove(&pVM->pgm.s.ChunkR3Map.pTree, Req.idChunkUnmap);
|
---|
3869 | AssertRelease(pUnmappedChunk);
|
---|
3870 | pUnmappedChunk->pv = NULL;
|
---|
3871 | pUnmappedChunk->Core.Key = UINT32_MAX;
|
---|
3872 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
3873 | MMR3HeapFree(pUnmappedChunk);
|
---|
3874 | #else
|
---|
3875 | MMR3UkHeapFree(pVM, pUnmappedChunk, MM_TAG_PGM_CHUNK_MAPPING);
|
---|
3876 | #endif
|
---|
3877 | pVM->pgm.s.ChunkR3Map.c--;
|
---|
3878 | pVM->pgm.s.cUnmappedChunks++;
|
---|
3879 |
|
---|
3880 | /* Flush dangling PGM pointers (R3 & R0 ptrs to GC physical addresses) */
|
---|
3881 | /* todo: we should not flush chunks which include cr3 mappings. */
|
---|
3882 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
3883 | {
|
---|
3884 | PPGMCPU pPGM = &pVM->aCpus[idCpu].pgm.s;
|
---|
3885 |
|
---|
3886 | pPGM->pGst32BitPdR3 = NULL;
|
---|
3887 | pPGM->pGstPaePdptR3 = NULL;
|
---|
3888 | pPGM->pGstAmd64Pml4R3 = NULL;
|
---|
3889 | #ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
3890 | pPGM->pGst32BitPdR0 = NIL_RTR0PTR;
|
---|
3891 | pPGM->pGstPaePdptR0 = NIL_RTR0PTR;
|
---|
3892 | pPGM->pGstAmd64Pml4R0 = NIL_RTR0PTR;
|
---|
3893 | #endif
|
---|
3894 | for (unsigned i = 0; i < RT_ELEMENTS(pPGM->apGstPaePDsR3); i++)
|
---|
3895 | {
|
---|
3896 | pPGM->apGstPaePDsR3[i] = NULL;
|
---|
3897 | #ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
3898 | pPGM->apGstPaePDsR0[i] = NIL_RTR0PTR;
|
---|
3899 | #endif
|
---|
3900 | }
|
---|
3901 |
|
---|
3902 | /* Flush REM TLBs. */
|
---|
3903 | CPUMSetChangedFlags(&pVM->aCpus[idCpu], CPUM_CHANGED_GLOBAL_TLB_FLUSH);
|
---|
3904 | }
|
---|
3905 |
|
---|
3906 | /* Flush REM translation blocks. */
|
---|
3907 | REMFlushTBs(pVM);
|
---|
3908 | }
|
---|
3909 | }
|
---|
3910 | }
|
---|
3911 | pgmUnlock(pVM);
|
---|
3912 | return rc;
|
---|
3913 | }
|
---|
3914 |
|
---|
3915 | /**
|
---|
3916 | * Unmap a chunk to free up virtual address space (request packet handler for pgmR3PhysChunkMap)
|
---|
3917 | *
|
---|
3918 | * @returns VBox status code.
|
---|
3919 | * @param pVM The VM to operate on.
|
---|
3920 | */
|
---|
3921 | void pgmR3PhysUnmapChunk(PVM pVM)
|
---|
3922 | {
|
---|
3923 | int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, pgmR3PhysUnmapChunkRendezvous, NULL);
|
---|
3924 | AssertRC(rc);
|
---|
3925 | }
|
---|
3926 | #endif /* PGM_WITH_LARGE_ADDRESS_SPACE_ON_32_BIT_HOST */
|
---|
3927 |
|
---|
3928 | /**
|
---|
3929 | * Maps the given chunk into the ring-3 mapping cache.
|
---|
3930 | *
|
---|
3931 | * This will call ring-0.
|
---|
3932 | *
|
---|
3933 | * @returns VBox status code.
|
---|
3934 | * @param pVM The VM handle.
|
---|
3935 | * @param idChunk The chunk in question.
|
---|
3936 | * @param ppChunk Where to store the chunk tracking structure.
|
---|
3937 | *
|
---|
3938 | * @remarks Called from within the PGM critical section.
|
---|
3939 | * @remarks Can be called from any thread!
|
---|
3940 | */
|
---|
3941 | int pgmR3PhysChunkMap(PVM pVM, uint32_t idChunk, PPPGMCHUNKR3MAP ppChunk)
|
---|
3942 | {
|
---|
3943 | int rc;
|
---|
3944 |
|
---|
3945 | Assert(PGMIsLockOwner(pVM));
|
---|
3946 | /*
|
---|
3947 | * Allocate a new tracking structure first.
|
---|
3948 | */
|
---|
3949 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
3950 | PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)MMR3HeapAllocZ(pVM, MM_TAG_PGM_CHUNK_MAPPING, sizeof(*pChunk));
|
---|
3951 | #else
|
---|
3952 | PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)MMR3UkHeapAllocZ(pVM, MM_TAG_PGM_CHUNK_MAPPING, sizeof(*pChunk), NULL);
|
---|
3953 | #endif
|
---|
3954 | AssertReturn(pChunk, VERR_NO_MEMORY);
|
---|
3955 | pChunk->Core.Key = idChunk;
|
---|
3956 |
|
---|
3957 | /*
|
---|
3958 | * Request the ring-0 part to map the chunk in question.
|
---|
3959 | */
|
---|
3960 | GMMMAPUNMAPCHUNKREQ Req;
|
---|
3961 | Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
3962 | Req.Hdr.cbReq = sizeof(Req);
|
---|
3963 | Req.pvR3 = NULL;
|
---|
3964 | Req.idChunkMap = idChunk;
|
---|
3965 | Req.idChunkUnmap = NIL_GMM_CHUNKID;
|
---|
3966 |
|
---|
3967 | /* Must be callable from any thread, so can't use VMMR3CallR0. */
|
---|
3968 | STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pStats)->StatChunkMap, a);
|
---|
3969 | rc = SUPR3CallVMMR0Ex(pVM->pVMR0, NIL_VMCPUID, VMMR0_DO_GMM_MAP_UNMAP_CHUNK, 0, &Req.Hdr);
|
---|
3970 | STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->StatChunkMap, a);
|
---|
3971 | if (RT_SUCCESS(rc))
|
---|
3972 | {
|
---|
3973 | /*
|
---|
3974 | * Update the tree.
|
---|
3975 | */
|
---|
3976 | /* insert the new one. */
|
---|
3977 | AssertPtr(Req.pvR3);
|
---|
3978 | pChunk->pv = Req.pvR3;
|
---|
3979 | bool fRc = RTAvlU32Insert(&pVM->pgm.s.ChunkR3Map.pTree, &pChunk->Core);
|
---|
3980 | AssertRelease(fRc);
|
---|
3981 | pVM->pgm.s.ChunkR3Map.c++;
|
---|
3982 | pVM->pgm.s.cMappedChunks++;
|
---|
3983 |
|
---|
3984 | /* If we're running out of virtual address space, then we should unmap another chunk. */
|
---|
3985 | if (pVM->pgm.s.ChunkR3Map.c >= pVM->pgm.s.ChunkR3Map.cMax)
|
---|
3986 | {
|
---|
3987 | #ifdef PGM_WITH_LARGE_ADDRESS_SPACE_ON_32_BIT_HOST
|
---|
3988 | /* Postpone the unmap operation (which requires a rendezvous operation) as we own the PGM lock here. */
|
---|
3989 | rc = VMR3ReqCallNoWaitU(pVM->pUVM, VMCPUID_ANY_QUEUE, (PFNRT)pgmR3PhysUnmapChunk, 1, pVM);
|
---|
3990 | AssertRC(rc);
|
---|
3991 | #else
|
---|
3992 | AssertFatalFailed(); /* can't happen */
|
---|
3993 | #endif
|
---|
3994 | }
|
---|
3995 | }
|
---|
3996 | else
|
---|
3997 | {
|
---|
3998 | AssertRC(rc);
|
---|
3999 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
4000 | MMR3HeapFree(pChunk);
|
---|
4001 | #else
|
---|
4002 | MMR3UkHeapFree(pVM, pChunk, MM_TAG_PGM_CHUNK_MAPPING);
|
---|
4003 | #endif
|
---|
4004 | pChunk = NULL;
|
---|
4005 | }
|
---|
4006 |
|
---|
4007 | *ppChunk = pChunk;
|
---|
4008 | return rc;
|
---|
4009 | }
|
---|
4010 |
|
---|
4011 |
|
---|
4012 | /**
|
---|
4013 | * For VMMCALLRING3_PGM_MAP_CHUNK, considered internal.
|
---|
4014 | *
|
---|
4015 | * @returns see pgmR3PhysChunkMap.
|
---|
4016 | * @param pVM The VM handle.
|
---|
4017 | * @param idChunk The chunk to map.
|
---|
4018 | */
|
---|
4019 | VMMR3DECL(int) PGMR3PhysChunkMap(PVM pVM, uint32_t idChunk)
|
---|
4020 | {
|
---|
4021 | PPGMCHUNKR3MAP pChunk;
|
---|
4022 | int rc;
|
---|
4023 |
|
---|
4024 | pgmLock(pVM);
|
---|
4025 | rc = pgmR3PhysChunkMap(pVM, idChunk, &pChunk);
|
---|
4026 | pgmUnlock(pVM);
|
---|
4027 | return rc;
|
---|
4028 | }
|
---|
4029 |
|
---|
4030 |
|
---|
4031 | /**
|
---|
4032 | * Invalidates the TLB for the ring-3 mapping cache.
|
---|
4033 | *
|
---|
4034 | * @param pVM The VM handle.
|
---|
4035 | */
|
---|
4036 | VMMR3DECL(void) PGMR3PhysChunkInvalidateTLB(PVM pVM)
|
---|
4037 | {
|
---|
4038 | pgmLock(pVM);
|
---|
4039 | for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.ChunkR3Map.Tlb.aEntries); i++)
|
---|
4040 | {
|
---|
4041 | pVM->pgm.s.ChunkR3Map.Tlb.aEntries[i].idChunk = NIL_GMM_CHUNKID;
|
---|
4042 | pVM->pgm.s.ChunkR3Map.Tlb.aEntries[i].pChunk = NULL;
|
---|
4043 | }
|
---|
4044 | /* The page map TLB references chunks, so invalidate that one too. */
|
---|
4045 | PGMPhysInvalidatePageMapTLB(pVM);
|
---|
4046 | pgmUnlock(pVM);
|
---|
4047 | }
|
---|
4048 |
|
---|
4049 |
|
---|
4050 | /**
|
---|
4051 | * Response to VMMCALLRING3_PGM_ALLOCATE_LARGE_PAGE to allocate a large (2MB) page
|
---|
4052 | * for use with a nested paging PDE.
|
---|
4053 | *
|
---|
4054 | * @returns The following VBox status codes.
|
---|
4055 | * @retval VINF_SUCCESS on success.
|
---|
4056 | * @retval VINF_EM_NO_MEMORY if we're out of memory.
|
---|
4057 | *
|
---|
4058 | * @param pVM The VM handle.
|
---|
4059 | * @param GCPhys GC physical start address of the 2 MB range
|
---|
4060 | */
|
---|
4061 | VMMR3DECL(int) PGMR3PhysAllocateLargeHandyPage(PVM pVM, RTGCPHYS GCPhys)
|
---|
4062 | {
|
---|
4063 | #ifdef PGM_WITH_LARGE_PAGES
|
---|
4064 | uint64_t u64TimeStamp1, u64TimeStamp2;
|
---|
4065 |
|
---|
4066 | pgmLock(pVM);
|
---|
4067 |
|
---|
4068 | STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pStats)->StatAllocLargePage, a);
|
---|
4069 | u64TimeStamp1 = RTTimeMilliTS();
|
---|
4070 | int rc = VMMR3CallR0(pVM, VMMR0_DO_PGM_ALLOCATE_LARGE_HANDY_PAGE, 0, NULL);
|
---|
4071 | u64TimeStamp2 = RTTimeMilliTS();
|
---|
4072 | STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->StatAllocLargePage, a);
|
---|
4073 | if (RT_SUCCESS(rc))
|
---|
4074 | {
|
---|
4075 | Assert(pVM->pgm.s.cLargeHandyPages == 1);
|
---|
4076 |
|
---|
4077 | uint32_t idPage = pVM->pgm.s.aLargeHandyPage[0].idPage;
|
---|
4078 | RTHCPHYS HCPhys = pVM->pgm.s.aLargeHandyPage[0].HCPhysGCPhys;
|
---|
4079 |
|
---|
4080 | void *pv;
|
---|
4081 |
|
---|
4082 | /* Map the large page into our address space.
|
---|
4083 | *
|
---|
4084 | * Note: assuming that within the 2 MB range:
|
---|
4085 | * - GCPhys + PAGE_SIZE = HCPhys + PAGE_SIZE (whole point of this exercise)
|
---|
4086 | * - user space mapping is continuous as well
|
---|
4087 | * - page id (GCPhys) + 1 = page id (GCPhys + PAGE_SIZE)
|
---|
4088 | */
|
---|
4089 | rc = pgmPhysPageMapByPageID(pVM, idPage, HCPhys, &pv);
|
---|
4090 | AssertLogRelMsg(RT_SUCCESS(rc), ("idPage=%#x HCPhysGCPhys=%RHp rc=%Rrc\n", idPage, HCPhys, rc));
|
---|
4091 |
|
---|
4092 | if (RT_SUCCESS(rc))
|
---|
4093 | {
|
---|
4094 | /*
|
---|
4095 | * Clear the pages.
|
---|
4096 | */
|
---|
4097 | STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pStats)->StatClearLargePage, b);
|
---|
4098 | for (unsigned i = 0; i < _2M/PAGE_SIZE; i++)
|
---|
4099 | {
|
---|
4100 | ASMMemZeroPage(pv);
|
---|
4101 |
|
---|
4102 | PPGMPAGE pPage;
|
---|
4103 | rc = pgmPhysGetPageEx(pVM, GCPhys, &pPage);
|
---|
4104 | AssertRC(rc);
|
---|
4105 |
|
---|
4106 | Assert(PGM_PAGE_IS_ZERO(pPage));
|
---|
4107 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->StatRZPageReplaceZero);
|
---|
4108 | pVM->pgm.s.cZeroPages--;
|
---|
4109 |
|
---|
4110 | /*
|
---|
4111 | * Do the PGMPAGE modifications.
|
---|
4112 | */
|
---|
4113 | pVM->pgm.s.cPrivatePages++;
|
---|
4114 | PGM_PAGE_SET_HCPHYS(pPage, HCPhys);
|
---|
4115 | PGM_PAGE_SET_PAGEID(pPage, idPage);
|
---|
4116 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
|
---|
4117 | PGM_PAGE_SET_PDE_TYPE(pPage, PGM_PAGE_PDE_TYPE_PDE);
|
---|
4118 | PGM_PAGE_SET_PTE_INDEX(pPage, 0);
|
---|
4119 | PGM_PAGE_SET_TRACKING(pPage, 0);
|
---|
4120 |
|
---|
4121 | /* Somewhat dirty assumption that page ids are increasing. */
|
---|
4122 | idPage++;
|
---|
4123 |
|
---|
4124 | HCPhys += PAGE_SIZE;
|
---|
4125 | GCPhys += PAGE_SIZE;
|
---|
4126 |
|
---|
4127 | pv = (void *)((uintptr_t)pv + PAGE_SIZE);
|
---|
4128 |
|
---|
4129 | Log3(("PGMR3PhysAllocateLargePage: idPage=%#x HCPhys=%RGp\n", idPage, HCPhys));
|
---|
4130 | }
|
---|
4131 | STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->StatClearLargePage, b);
|
---|
4132 |
|
---|
4133 | /* Flush all TLBs. */
|
---|
4134 | PGM_INVL_ALL_VCPU_TLBS(pVM);
|
---|
4135 | PGMPhysInvalidatePageMapTLB(pVM);
|
---|
4136 | }
|
---|
4137 | pVM->pgm.s.cLargeHandyPages = 0;
|
---|
4138 | }
|
---|
4139 |
|
---|
4140 | if (RT_SUCCESS(rc))
|
---|
4141 | {
|
---|
4142 | static uint32_t cTimeOut = 0;
|
---|
4143 | uint64_t u64TimeStampDelta = u64TimeStamp2 - u64TimeStamp1;
|
---|
4144 |
|
---|
4145 | if (u64TimeStampDelta > 100)
|
---|
4146 | {
|
---|
4147 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->StatLargePageOverflow);
|
---|
4148 | if ( ++cTimeOut > 10
|
---|
4149 | || u64TimeStampDelta > 1000 /* more than one second forces an early retirement from allocating large pages. */)
|
---|
4150 | {
|
---|
4151 | /* If repeated attempts to allocate a large page takes more than 100 ms, then we fall back to normal 4k pages.
|
---|
4152 | * E.g. Vista 64 tries to move memory around, which takes a huge amount of time.
|
---|
4153 | */
|
---|
4154 | LogRel(("PGMR3PhysAllocateLargePage: allocating large pages takes too long (last attempt %d ms; nr of timeouts %d); DISABLE\n", u64TimeStampDelta, cTimeOut));
|
---|
4155 | PGMSetLargePageUsage(pVM, false);
|
---|
4156 | }
|
---|
4157 | }
|
---|
4158 | else
|
---|
4159 | if (cTimeOut > 0)
|
---|
4160 | cTimeOut--;
|
---|
4161 | }
|
---|
4162 |
|
---|
4163 | pgmUnlock(pVM);
|
---|
4164 | return rc;
|
---|
4165 | #else
|
---|
4166 | return VERR_NOT_IMPLEMENTED;
|
---|
4167 | #endif /* PGM_WITH_LARGE_PAGES */
|
---|
4168 | }
|
---|
4169 |
|
---|
4170 |
|
---|
4171 | /**
|
---|
4172 | * Response to VM_FF_PGM_NEED_HANDY_PAGES and VMMCALLRING3_PGM_ALLOCATE_HANDY_PAGES.
|
---|
4173 | *
|
---|
4174 | * This function will also work the VM_FF_PGM_NO_MEMORY force action flag, to
|
---|
4175 | * signal and clear the out of memory condition. When contracted, this API is
|
---|
4176 | * used to try clear the condition when the user wants to resume.
|
---|
4177 | *
|
---|
4178 | * @returns The following VBox status codes.
|
---|
4179 | * @retval VINF_SUCCESS on success. FFs cleared.
|
---|
4180 | * @retval VINF_EM_NO_MEMORY if we're out of memory. The FF is not cleared in
|
---|
4181 | * this case and it gets accompanied by VM_FF_PGM_NO_MEMORY.
|
---|
4182 | *
|
---|
4183 | * @param pVM The VM handle.
|
---|
4184 | *
|
---|
4185 | * @remarks The VINF_EM_NO_MEMORY status is for the benefit of the FF processing
|
---|
4186 | * in EM.cpp and shouldn't be propagated outside TRPM, HWACCM, EM and
|
---|
4187 | * pgmPhysEnsureHandyPage. There is one exception to this in the \#PF
|
---|
4188 | * handler.
|
---|
4189 | */
|
---|
4190 | VMMR3DECL(int) PGMR3PhysAllocateHandyPages(PVM pVM)
|
---|
4191 | {
|
---|
4192 | pgmLock(pVM);
|
---|
4193 |
|
---|
4194 | /*
|
---|
4195 | * Allocate more pages, noting down the index of the first new page.
|
---|
4196 | */
|
---|
4197 | uint32_t iClear = pVM->pgm.s.cHandyPages;
|
---|
4198 | AssertMsgReturn(iClear <= RT_ELEMENTS(pVM->pgm.s.aHandyPages), ("%d", iClear), VERR_INTERNAL_ERROR);
|
---|
4199 | Log(("PGMR3PhysAllocateHandyPages: %d -> %d\n", iClear, RT_ELEMENTS(pVM->pgm.s.aHandyPages)));
|
---|
4200 | int rcAlloc = VINF_SUCCESS;
|
---|
4201 | int rcSeed = VINF_SUCCESS;
|
---|
4202 | int rc = VMMR3CallR0(pVM, VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES, 0, NULL);
|
---|
4203 | while (rc == VERR_GMM_SEED_ME)
|
---|
4204 | {
|
---|
4205 | void *pvChunk;
|
---|
4206 | rcAlloc = rc = SUPR3PageAlloc(GMM_CHUNK_SIZE >> PAGE_SHIFT, &pvChunk);
|
---|
4207 | if (RT_SUCCESS(rc))
|
---|
4208 | {
|
---|
4209 | rcSeed = rc = VMMR3CallR0(pVM, VMMR0_DO_GMM_SEED_CHUNK, (uintptr_t)pvChunk, NULL);
|
---|
4210 | if (RT_FAILURE(rc))
|
---|
4211 | SUPR3PageFree(pvChunk, GMM_CHUNK_SIZE >> PAGE_SHIFT);
|
---|
4212 | }
|
---|
4213 | if (RT_SUCCESS(rc))
|
---|
4214 | rc = VMMR3CallR0(pVM, VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES, 0, NULL);
|
---|
4215 | }
|
---|
4216 |
|
---|
4217 | /* todo: we should split this up into an allocate and flush operation. sometimes you want to flush and not allocate more (which will trigger the vm account limit error) */
|
---|
4218 | if ( rc == VERR_GMM_HIT_VM_ACCOUNT_LIMIT
|
---|
4219 | && pVM->pgm.s.cHandyPages > 0)
|
---|
4220 | {
|
---|
4221 | /* Still handy pages left, so don't panic. */
|
---|
4222 | rc = VINF_SUCCESS;
|
---|
4223 | }
|
---|
4224 |
|
---|
4225 | if (RT_SUCCESS(rc))
|
---|
4226 | {
|
---|
4227 | AssertMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
|
---|
4228 | Assert(pVM->pgm.s.cHandyPages > 0);
|
---|
4229 | VM_FF_CLEAR(pVM, VM_FF_PGM_NEED_HANDY_PAGES);
|
---|
4230 | VM_FF_CLEAR(pVM, VM_FF_PGM_NO_MEMORY);
|
---|
4231 |
|
---|
4232 | /*
|
---|
4233 | * Clear the pages.
|
---|
4234 | */
|
---|
4235 | while (iClear < pVM->pgm.s.cHandyPages)
|
---|
4236 | {
|
---|
4237 | PGMMPAGEDESC pPage = &pVM->pgm.s.aHandyPages[iClear];
|
---|
4238 | void *pv;
|
---|
4239 | rc = pgmPhysPageMapByPageID(pVM, pPage->idPage, pPage->HCPhysGCPhys, &pv);
|
---|
4240 | AssertLogRelMsgBreak(RT_SUCCESS(rc), ("idPage=%#x HCPhysGCPhys=%RHp rc=%Rrc\n", pPage->idPage, pPage->HCPhysGCPhys, rc));
|
---|
4241 | ASMMemZeroPage(pv);
|
---|
4242 | iClear++;
|
---|
4243 | Log3(("PGMR3PhysAllocateHandyPages: idPage=%#x HCPhys=%RGp\n", pPage->idPage, pPage->HCPhysGCPhys));
|
---|
4244 | }
|
---|
4245 | }
|
---|
4246 | else
|
---|
4247 | {
|
---|
4248 | uint64_t cAllocPages, cMaxPages, cBalloonPages;
|
---|
4249 |
|
---|
4250 | /*
|
---|
4251 | * We should never get here unless there is a genuine shortage of
|
---|
4252 | * memory (or some internal error). Flag the error so the VM can be
|
---|
4253 | * suspended ASAP and the user informed. If we're totally out of
|
---|
4254 | * handy pages we will return failure.
|
---|
4255 | */
|
---|
4256 | /* Report the failure. */
|
---|
4257 | LogRel(("PGM: Failed to procure handy pages; rc=%Rrc rcAlloc=%Rrc rcSeed=%Rrc cHandyPages=%#x\n"
|
---|
4258 | " cAllPages=%#x cPrivatePages=%#x cSharedPages=%#x cZeroPages=%#x\n",
|
---|
4259 | rc, rcAlloc, rcSeed,
|
---|
4260 | pVM->pgm.s.cHandyPages,
|
---|
4261 | pVM->pgm.s.cAllPages,
|
---|
4262 | pVM->pgm.s.cPrivatePages,
|
---|
4263 | pVM->pgm.s.cSharedPages,
|
---|
4264 | pVM->pgm.s.cZeroPages));
|
---|
4265 |
|
---|
4266 | if (GMMR3QueryMemoryStats(pVM, &cAllocPages, &cMaxPages, &cBalloonPages) == VINF_SUCCESS)
|
---|
4267 | {
|
---|
4268 | LogRel(("GMM: Statistics:\n"
|
---|
4269 | " Allocated pages: %RX64\n"
|
---|
4270 | " Maximum pages: %RX64\n"
|
---|
4271 | " Ballooned pages: %RX64\n", cAllocPages, cMaxPages, cBalloonPages));
|
---|
4272 | }
|
---|
4273 |
|
---|
4274 | if ( rc != VERR_NO_MEMORY
|
---|
4275 | && rc != VERR_LOCK_FAILED)
|
---|
4276 | {
|
---|
4277 | for (uint32_t i = 0; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
|
---|
4278 | {
|
---|
4279 | LogRel(("PGM: aHandyPages[#%#04x] = {.HCPhysGCPhys=%RHp, .idPage=%#08x, .idSharedPage=%#08x}\n",
|
---|
4280 | i, pVM->pgm.s.aHandyPages[i].HCPhysGCPhys, pVM->pgm.s.aHandyPages[i].idPage,
|
---|
4281 | pVM->pgm.s.aHandyPages[i].idSharedPage));
|
---|
4282 | uint32_t const idPage = pVM->pgm.s.aHandyPages[i].idPage;
|
---|
4283 | if (idPage != NIL_GMM_PAGEID)
|
---|
4284 | {
|
---|
4285 | for (PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesXR3;
|
---|
4286 | pRam;
|
---|
4287 | pRam = pRam->pNextR3)
|
---|
4288 | {
|
---|
4289 | uint32_t const cPages = pRam->cb >> PAGE_SHIFT;
|
---|
4290 | for (uint32_t iPage = 0; iPage < cPages; iPage++)
|
---|
4291 | if (PGM_PAGE_GET_PAGEID(&pRam->aPages[iPage]) == idPage)
|
---|
4292 | LogRel(("PGM: Used by %RGp %R[pgmpage] (%s)\n",
|
---|
4293 | pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), &pRam->aPages[iPage], pRam->pszDesc));
|
---|
4294 | }
|
---|
4295 | }
|
---|
4296 | }
|
---|
4297 | }
|
---|
4298 |
|
---|
4299 | /* Set the FFs and adjust rc. */
|
---|
4300 | VM_FF_SET(pVM, VM_FF_PGM_NEED_HANDY_PAGES);
|
---|
4301 | VM_FF_SET(pVM, VM_FF_PGM_NO_MEMORY);
|
---|
4302 | if ( rc == VERR_NO_MEMORY
|
---|
4303 | || rc == VERR_LOCK_FAILED)
|
---|
4304 | rc = VINF_EM_NO_MEMORY;
|
---|
4305 | }
|
---|
4306 |
|
---|
4307 | pgmUnlock(pVM);
|
---|
4308 | return rc;
|
---|
4309 | }
|
---|
4310 |
|
---|
4311 |
|
---|
4312 | /**
|
---|
4313 | * Frees the specified RAM page and replaces it with the ZERO page.
|
---|
4314 | *
|
---|
4315 | * This is used by ballooning, remapping MMIO2, RAM reset and state loading.
|
---|
4316 | *
|
---|
4317 | * @param pVM Pointer to the shared VM structure.
|
---|
4318 | * @param pReq Pointer to the request.
|
---|
4319 | * @param pcPendingPages Where the number of pages waiting to be freed are
|
---|
4320 | * kept. This will normally be incremented.
|
---|
4321 | * @param pPage Pointer to the page structure.
|
---|
4322 | * @param GCPhys The guest physical address of the page, if applicable.
|
---|
4323 | *
|
---|
4324 | * @remarks The caller must own the PGM lock.
|
---|
4325 | */
|
---|
4326 | int pgmPhysFreePage(PVM pVM, PGMMFREEPAGESREQ pReq, uint32_t *pcPendingPages, PPGMPAGE pPage, RTGCPHYS GCPhys)
|
---|
4327 | {
|
---|
4328 | /*
|
---|
4329 | * Assert sanity.
|
---|
4330 | */
|
---|
4331 | Assert(PGMIsLockOwner(pVM));
|
---|
4332 | if (RT_UNLIKELY( PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_RAM
|
---|
4333 | && PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_ROM_SHADOW))
|
---|
4334 | {
|
---|
4335 | AssertMsgFailed(("GCPhys=%RGp pPage=%R[pgmpage]\n", GCPhys, pPage));
|
---|
4336 | return VMSetError(pVM, VERR_PGM_PHYS_NOT_RAM, RT_SRC_POS, "GCPhys=%RGp type=%d", GCPhys, PGM_PAGE_GET_TYPE(pPage));
|
---|
4337 | }
|
---|
4338 |
|
---|
4339 | /** @todo What about ballooning of large pages??! */
|
---|
4340 | Assert( PGM_PAGE_GET_PDE_TYPE(pPage) != PGM_PAGE_PDE_TYPE_PDE
|
---|
4341 | && PGM_PAGE_GET_PDE_TYPE(pPage) != PGM_PAGE_PDE_TYPE_PDE_DISABLED);
|
---|
4342 |
|
---|
4343 | if ( PGM_PAGE_IS_ZERO(pPage)
|
---|
4344 | || PGM_PAGE_IS_BALLOONED(pPage))
|
---|
4345 | return VINF_SUCCESS;
|
---|
4346 |
|
---|
4347 | const uint32_t idPage = PGM_PAGE_GET_PAGEID(pPage);
|
---|
4348 | Log3(("pgmPhysFreePage: idPage=%#x GCPhys=%RGp pPage=%R[pgmpage]\n", idPage, GCPhys, pPage));
|
---|
4349 | if (RT_UNLIKELY( idPage == NIL_GMM_PAGEID
|
---|
4350 | || idPage > GMM_PAGEID_LAST
|
---|
4351 | || PGM_PAGE_GET_CHUNKID(pPage) == NIL_GMM_CHUNKID))
|
---|
4352 | {
|
---|
4353 | AssertMsgFailed(("GCPhys=%RGp pPage=%R[pgmpage]\n", GCPhys, pPage));
|
---|
4354 | return VMSetError(pVM, VERR_PGM_PHYS_INVALID_PAGE_ID, RT_SRC_POS, "GCPhys=%RGp idPage=%#x", GCPhys, pPage);
|
---|
4355 | }
|
---|
4356 |
|
---|
4357 | /* update page count stats. */
|
---|
4358 | if (PGM_PAGE_IS_SHARED(pPage))
|
---|
4359 | pVM->pgm.s.cSharedPages--;
|
---|
4360 | else
|
---|
4361 | pVM->pgm.s.cPrivatePages--;
|
---|
4362 | pVM->pgm.s.cZeroPages++;
|
---|
4363 |
|
---|
4364 | /* Deal with write monitored pages. */
|
---|
4365 | if (PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_WRITE_MONITORED)
|
---|
4366 | {
|
---|
4367 | PGM_PAGE_SET_WRITTEN_TO(pPage);
|
---|
4368 | pVM->pgm.s.cWrittenToPages++;
|
---|
4369 | }
|
---|
4370 |
|
---|
4371 | /*
|
---|
4372 | * pPage = ZERO page.
|
---|
4373 | */
|
---|
4374 | PGM_PAGE_SET_HCPHYS(pPage, pVM->pgm.s.HCPhysZeroPg);
|
---|
4375 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ZERO);
|
---|
4376 | PGM_PAGE_SET_PAGEID(pPage, NIL_GMM_PAGEID);
|
---|
4377 | PGM_PAGE_SET_PDE_TYPE(pPage, PGM_PAGE_PDE_TYPE_DONTCARE);
|
---|
4378 | PGM_PAGE_SET_PTE_INDEX(pPage, 0);
|
---|
4379 | PGM_PAGE_SET_TRACKING(pPage, 0);
|
---|
4380 |
|
---|
4381 | /* Flush physical page map TLB entry. */
|
---|
4382 | PGMPhysInvalidatePageMapTLBEntry(pVM, GCPhys);
|
---|
4383 |
|
---|
4384 | /*
|
---|
4385 | * Make sure it's not in the handy page array.
|
---|
4386 | */
|
---|
4387 | for (uint32_t i = pVM->pgm.s.cHandyPages; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
|
---|
4388 | {
|
---|
4389 | if (pVM->pgm.s.aHandyPages[i].idPage == idPage)
|
---|
4390 | {
|
---|
4391 | pVM->pgm.s.aHandyPages[i].idPage = NIL_GMM_PAGEID;
|
---|
4392 | break;
|
---|
4393 | }
|
---|
4394 | if (pVM->pgm.s.aHandyPages[i].idSharedPage == idPage)
|
---|
4395 | {
|
---|
4396 | pVM->pgm.s.aHandyPages[i].idSharedPage = NIL_GMM_PAGEID;
|
---|
4397 | break;
|
---|
4398 | }
|
---|
4399 | }
|
---|
4400 |
|
---|
4401 | /*
|
---|
4402 | * Push it onto the page array.
|
---|
4403 | */
|
---|
4404 | uint32_t iPage = *pcPendingPages;
|
---|
4405 | Assert(iPage < PGMPHYS_FREE_PAGE_BATCH_SIZE);
|
---|
4406 | *pcPendingPages += 1;
|
---|
4407 |
|
---|
4408 | pReq->aPages[iPage].idPage = idPage;
|
---|
4409 |
|
---|
4410 | if (iPage + 1 < PGMPHYS_FREE_PAGE_BATCH_SIZE)
|
---|
4411 | return VINF_SUCCESS;
|
---|
4412 |
|
---|
4413 | /*
|
---|
4414 | * Flush the pages.
|
---|
4415 | */
|
---|
4416 | int rc = GMMR3FreePagesPerform(pVM, pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE);
|
---|
4417 | if (RT_SUCCESS(rc))
|
---|
4418 | {
|
---|
4419 | GMMR3FreePagesRePrep(pVM, pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE, GMMACCOUNT_BASE);
|
---|
4420 | *pcPendingPages = 0;
|
---|
4421 | }
|
---|
4422 | return rc;
|
---|
4423 | }
|
---|
4424 |
|
---|
4425 |
|
---|
4426 | /**
|
---|
4427 | * Converts a GC physical address to a HC ring-3 pointer, with some
|
---|
4428 | * additional checks.
|
---|
4429 | *
|
---|
4430 | * @returns VBox status code.
|
---|
4431 | * @retval VINF_SUCCESS on success.
|
---|
4432 | * @retval VINF_PGM_PHYS_TLB_CATCH_WRITE and *ppv set if the page has a write
|
---|
4433 | * access handler of some kind.
|
---|
4434 | * @retval VERR_PGM_PHYS_TLB_CATCH_ALL if the page has a handler catching all
|
---|
4435 | * accesses or is odd in any way.
|
---|
4436 | * @retval VERR_PGM_PHYS_TLB_UNASSIGNED if the page doesn't exist.
|
---|
4437 | *
|
---|
4438 | * @param pVM The VM handle.
|
---|
4439 | * @param GCPhys The GC physical address to convert.
|
---|
4440 | * @param fWritable Whether write access is required.
|
---|
4441 | * @param ppv Where to store the pointer corresponding to GCPhys on
|
---|
4442 | * success.
|
---|
4443 | */
|
---|
4444 | VMMR3DECL(int) PGMR3PhysTlbGCPhys2Ptr(PVM pVM, RTGCPHYS GCPhys, bool fWritable, void **ppv)
|
---|
4445 | {
|
---|
4446 | pgmLock(pVM);
|
---|
4447 |
|
---|
4448 | PPGMRAMRANGE pRam;
|
---|
4449 | PPGMPAGE pPage;
|
---|
4450 | int rc = pgmPhysGetPageAndRangeEx(pVM, GCPhys, &pPage, &pRam);
|
---|
4451 | if (RT_SUCCESS(rc))
|
---|
4452 | {
|
---|
4453 | if (PGM_PAGE_IS_BALLOONED(pPage))
|
---|
4454 | rc = VINF_PGM_PHYS_TLB_CATCH_WRITE;
|
---|
4455 | else if (!PGM_PAGE_HAS_ANY_HANDLERS(pPage))
|
---|
4456 | rc = VINF_SUCCESS;
|
---|
4457 | else
|
---|
4458 | {
|
---|
4459 | if (PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage)) /* catches MMIO */
|
---|
4460 | rc = VERR_PGM_PHYS_TLB_CATCH_ALL;
|
---|
4461 | else if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage))
|
---|
4462 | {
|
---|
4463 | /** @todo Handle TLB loads of virtual handlers so ./test.sh can be made to work
|
---|
4464 | * in -norawr0 mode. */
|
---|
4465 | if (fWritable)
|
---|
4466 | rc = VINF_PGM_PHYS_TLB_CATCH_WRITE;
|
---|
4467 | }
|
---|
4468 | else
|
---|
4469 | {
|
---|
4470 | /* Temporarily disabled physical handler(s), since the recompiler
|
---|
4471 | doesn't get notified when it's reset we'll have to pretend it's
|
---|
4472 | operating normally. */
|
---|
4473 | if (pgmHandlerPhysicalIsAll(pVM, GCPhys))
|
---|
4474 | rc = VERR_PGM_PHYS_TLB_CATCH_ALL;
|
---|
4475 | else
|
---|
4476 | rc = VINF_PGM_PHYS_TLB_CATCH_WRITE;
|
---|
4477 | }
|
---|
4478 | }
|
---|
4479 | if (RT_SUCCESS(rc))
|
---|
4480 | {
|
---|
4481 | int rc2;
|
---|
4482 |
|
---|
4483 | /* Make sure what we return is writable. */
|
---|
4484 | if (fWritable)
|
---|
4485 | switch (PGM_PAGE_GET_STATE(pPage))
|
---|
4486 | {
|
---|
4487 | case PGM_PAGE_STATE_ALLOCATED:
|
---|
4488 | break;
|
---|
4489 | case PGM_PAGE_STATE_BALLOONED:
|
---|
4490 | AssertFailed();
|
---|
4491 | break;
|
---|
4492 | case PGM_PAGE_STATE_ZERO:
|
---|
4493 | case PGM_PAGE_STATE_SHARED:
|
---|
4494 | if (rc == VINF_PGM_PHYS_TLB_CATCH_WRITE)
|
---|
4495 | break;
|
---|
4496 | case PGM_PAGE_STATE_WRITE_MONITORED:
|
---|
4497 | rc2 = pgmPhysPageMakeWritable(pVM, pPage, GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK);
|
---|
4498 | AssertLogRelRCReturn(rc2, rc2);
|
---|
4499 | break;
|
---|
4500 | }
|
---|
4501 |
|
---|
4502 | /* Get a ring-3 mapping of the address. */
|
---|
4503 | PPGMPAGER3MAPTLBE pTlbe;
|
---|
4504 | rc2 = pgmPhysPageQueryTlbe(pVM, GCPhys, &pTlbe);
|
---|
4505 | AssertLogRelRCReturn(rc2, rc2);
|
---|
4506 | *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
|
---|
4507 | /** @todo mapping/locking hell; this isn't horribly efficient since
|
---|
4508 | * pgmPhysPageLoadIntoTlb will repeat the lookup we've done here. */
|
---|
4509 |
|
---|
4510 | Log6(("PGMR3PhysTlbGCPhys2Ptr: GCPhys=%RGp rc=%Rrc pPage=%R[pgmpage] *ppv=%p\n", GCPhys, rc, pPage, *ppv));
|
---|
4511 | }
|
---|
4512 | else
|
---|
4513 | Log6(("PGMR3PhysTlbGCPhys2Ptr: GCPhys=%RGp rc=%Rrc pPage=%R[pgmpage]\n", GCPhys, rc, pPage));
|
---|
4514 |
|
---|
4515 | /* else: handler catching all access, no pointer returned. */
|
---|
4516 | }
|
---|
4517 | else
|
---|
4518 | rc = VERR_PGM_PHYS_TLB_UNASSIGNED;
|
---|
4519 |
|
---|
4520 | pgmUnlock(pVM);
|
---|
4521 | return rc;
|
---|
4522 | }
|
---|
4523 |
|
---|