VirtualBox

source: vbox/trunk/src/VBox/VMM/PGMPhys.cpp@ 30821

Last change on this file since 30821 was 30821, checked in by vboxsync, 14 years ago

Sigh

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette