VirtualBox

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

Last change on this file since 26638 was 26616, checked in by vboxsync, 15 years ago

More large page work (disabled)

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