VirtualBox

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

Last change on this file since 10934 was 10838, checked in by vboxsync, 16 years ago

Fixed assertion (creation failure).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 84.5 KB
Line 
1/* $Id: PGMPhys.cpp 10838 2008-07-23 19:43:31Z 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
27#include <VBox/pgm.h>
28#include <VBox/cpum.h>
29#include <VBox/iom.h>
30#include <VBox/sup.h>
31#include <VBox/mm.h>
32#include <VBox/stam.h>
33#include <VBox/rem.h>
34#include <VBox/csam.h>
35#include "PGMInternal.h"
36#include <VBox/vm.h>
37#include <VBox/dbg.h>
38#include <VBox/param.h>
39#include <VBox/err.h>
40#include <iprt/assert.h>
41#include <iprt/alloc.h>
42#include <iprt/asm.h>
43#include <VBox/log.h>
44#include <iprt/thread.h>
45#include <iprt/string.h>
46
47
48/*******************************************************************************
49* Internal Functions *
50*******************************************************************************/
51/*static - shut up warning */
52DECLCALLBACK(int) pgmR3PhysRomWriteHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser);
53
54
55
56/*
57 * PGMR3PhysReadU8-64
58 * PGMR3PhysWriteU8-64
59 */
60#define PGMPHYSFN_READNAME PGMR3PhysReadU8
61#define PGMPHYSFN_WRITENAME PGMR3PhysWriteU8
62#define PGMPHYS_DATASIZE 1
63#define PGMPHYS_DATATYPE uint8_t
64#include "PGMPhysRWTmpl.h"
65
66#define PGMPHYSFN_READNAME PGMR3PhysReadU16
67#define PGMPHYSFN_WRITENAME PGMR3PhysWriteU16
68#define PGMPHYS_DATASIZE 2
69#define PGMPHYS_DATATYPE uint16_t
70#include "PGMPhysRWTmpl.h"
71
72#define PGMPHYSFN_READNAME PGMR3PhysReadU32
73#define PGMPHYSFN_WRITENAME PGMR3PhysWriteU32
74#define PGMPHYS_DATASIZE 4
75#define PGMPHYS_DATATYPE uint32_t
76#include "PGMPhysRWTmpl.h"
77
78#define PGMPHYSFN_READNAME PGMR3PhysReadU64
79#define PGMPHYSFN_WRITENAME PGMR3PhysWriteU64
80#define PGMPHYS_DATASIZE 8
81#define PGMPHYS_DATATYPE uint64_t
82#include "PGMPhysRWTmpl.h"
83
84
85
86/**
87 * Links a new RAM range into the list.
88 *
89 * @param pVM Pointer to the shared VM structure.
90 * @param pNew Pointer to the new list entry.
91 * @param pPrev Pointer to the previous list entry. If NULL, insert as head.
92 */
93static void pgmR3PhysLinkRamRange(PVM pVM, PPGMRAMRANGE pNew, PPGMRAMRANGE pPrev)
94{
95 pgmLock(pVM);
96
97 PPGMRAMRANGE pRam = pPrev ? pPrev->pNextR3 : pVM->pgm.s.pRamRangesR3;
98 pNew->pNextR3 = pRam;
99 pNew->pNextR0 = pRam ? MMHyperCCToR0(pVM, pRam) : NIL_RTR0PTR;
100 pNew->pNextGC = pRam ? MMHyperCCToGC(pVM, pRam) : NIL_RTGCPTR;
101
102 if (pPrev)
103 {
104 pPrev->pNextR3 = pNew;
105 pPrev->pNextR0 = MMHyperCCToR0(pVM, pNew);
106 pPrev->pNextGC = MMHyperCCToGC(pVM, pNew);
107 }
108 else
109 {
110 pVM->pgm.s.pRamRangesR3 = pNew;
111 pVM->pgm.s.pRamRangesR0 = MMHyperCCToR0(pVM, pNew);
112 pVM->pgm.s.pRamRangesGC = MMHyperCCToGC(pVM, pNew);
113 }
114
115 pgmUnlock(pVM);
116}
117
118
119/**
120 * Unlink an existing RAM range from the list.
121 *
122 * @param pVM Pointer to the shared VM structure.
123 * @param pRam Pointer to the new list entry.
124 * @param pPrev Pointer to the previous list entry. If NULL, insert as head.
125 */
126static void pgmR3PhysUnlinkRamRange2(PVM pVM, PPGMRAMRANGE pRam, PPGMRAMRANGE pPrev)
127{
128 Assert(pPrev ? pPrev->pNextR3 == pRam : pVM->pgm.s.pRamRangesR3 == pRam);
129
130 pgmLock(pVM);
131
132 PPGMRAMRANGE pNext = pRam->pNextR3;
133 if (pPrev)
134 {
135 pPrev->pNextR3 = pNext;
136 pPrev->pNextR0 = pNext ? MMHyperCCToR0(pVM, pNext) : NIL_RTR0PTR;
137 pPrev->pNextGC = pNext ? MMHyperCCToGC(pVM, pNext) : NIL_RTGCPTR;
138 }
139 else
140 {
141 Assert(pVM->pgm.s.pRamRangesR3 == pRam);
142 pVM->pgm.s.pRamRangesR3 = pNext;
143 pVM->pgm.s.pRamRangesR0 = pNext ? MMHyperCCToR0(pVM, pNext) : NIL_RTR0PTR;
144 pVM->pgm.s.pRamRangesGC = pNext ? MMHyperCCToGC(pVM, pNext) : NIL_RTGCPTR;
145 }
146
147 pgmUnlock(pVM);
148}
149
150
151/**
152 * Unlink an existing RAM range from the list.
153 *
154 * @param pVM Pointer to the shared VM structure.
155 * @param pRam Pointer to the new list entry.
156 */
157static void pgmR3PhysUnlinkRamRange(PVM pVM, PPGMRAMRANGE pRam)
158{
159 /* find prev. */
160 PPGMRAMRANGE pPrev = NULL;
161 PPGMRAMRANGE pCur = pVM->pgm.s.pRamRangesR3;
162 while (pCur != pRam)
163 {
164 pPrev = pCur;
165 pCur = pCur->pNextR3;
166 }
167 AssertFatal(pCur);
168
169 pgmR3PhysUnlinkRamRange2(pVM, pRam, pPrev);
170}
171
172
173
174/**
175 * Sets up a range RAM.
176 *
177 * This will check for conflicting registrations, make a resource
178 * reservation for the memory (with GMM), and setup the per-page
179 * tracking structures (PGMPAGE).
180 *
181 * @returns VBox stutus code.
182 * @param pVM Pointer to the shared VM structure.
183 * @param GCPhys The physical address of the RAM.
184 * @param cb The size of the RAM.
185 * @param pszDesc The description - not copied, so, don't free or change it.
186 */
187PGMR3DECL(int) PGMR3PhysRegisterRam(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, const char *pszDesc)
188{
189 /*
190 * Validate input.
191 */
192 Log(("PGMR3PhysRegisterRam: GCPhys=%RGp cb=%RGp pszDesc=%s\n", GCPhys, cb, pszDesc));
193 AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
194 AssertReturn(RT_ALIGN_T(cb, PAGE_SIZE, RTGCPHYS) == cb, VERR_INVALID_PARAMETER);
195 AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
196 RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
197 AssertMsgReturn(GCPhysLast > GCPhys, ("The range wraps! GCPhys=%RGp cb=%RGp\n", GCPhys, cb), VERR_INVALID_PARAMETER);
198 AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
199 VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
200
201 /*
202 * Find range location and check for conflicts.
203 * (We don't lock here because the locking by EMT is only required on update.)
204 */
205 PPGMRAMRANGE pPrev = NULL;
206 PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3;
207 while (pRam && GCPhysLast >= pRam->GCPhys)
208 {
209 if ( GCPhysLast >= pRam->GCPhys
210 && GCPhys <= pRam->GCPhysLast)
211 AssertLogRelMsgFailedReturn(("%RGp-%RGp (%s) conflicts with existing %RGp-%RGp (%s)\n",
212 GCPhys, GCPhysLast, pszDesc,
213 pRam->GCPhys, pRam->GCPhysLast, pRam->pszDesc),
214 VERR_PGM_RAM_CONFLICT);
215
216 /* next */
217 pPrev = pRam;
218 pRam = pRam->pNextR3;
219 }
220
221 /*
222 * Register it with GMM (the API bitches).
223 */
224 const RTGCPHYS cPages = cb >> PAGE_SHIFT;
225 int rc = MMR3IncreaseBaseReservation(pVM, cPages);
226 if (RT_FAILURE(rc))
227 return rc;
228
229 /*
230 * Allocate RAM range.
231 */
232 const size_t cbRamRange = RT_OFFSETOF(PGMRAMRANGE, aPages[cPages]);
233 PPGMRAMRANGE pNew;
234 rc = MMR3HyperAllocOnceNoRel(pVM, cbRamRange, 0, MM_TAG_PGM_PHYS, (void **)&pNew);
235 AssertLogRelMsgRCReturn(rc, ("cbRamRange=%zu\n", cbRamRange), rc);
236
237 /*
238 * Initialize the range.
239 */
240 pNew->GCPhys = GCPhys;
241 pNew->GCPhysLast = GCPhysLast;
242 pNew->pszDesc = pszDesc;
243 pNew->cb = cb;
244 pNew->fFlags = 0;
245
246 pNew->pvHC = NULL;
247 pNew->pavHCChunkHC = NULL;
248 pNew->pavHCChunkGC = 0;
249
250#ifndef VBOX_WITH_NEW_PHYS_CODE
251 /* Allocate memory for chunk to HC ptr lookup array. */
252 rc = MMHyperAlloc(pVM, (cb >> PGM_DYNAMIC_CHUNK_SHIFT) * sizeof(void *), 16, MM_TAG_PGM, (void **)&pNew->pavHCChunkHC);
253 AssertRCReturn(rc, rc);
254 pNew->pavHCChunkGC = MMHyperCCToGC(pVM, pNew->pavHCChunkHC);
255 pNew->fFlags |= MM_RAM_FLAGS_DYNAMIC_ALLOC;
256
257#endif
258 RTGCPHYS iPage = cPages;
259 while (iPage-- > 0)
260 PGM_PAGE_INIT_ZERO(&pNew->aPages[iPage], pVM, PGMPAGETYPE_RAM);
261
262 /*
263 * Insert the new RAM range.
264 */
265 pgmR3PhysLinkRamRange(pVM, pNew, pPrev);
266
267 /*
268 * Notify REM.
269 */
270#ifdef VBOX_WITH_NEW_PHYS_CODE
271 REMR3NotifyPhysRamRegister(pVM, GCPhys, cb, 0);
272#else
273 REMR3NotifyPhysRamRegister(pVM, GCPhys, cb, MM_RAM_FLAGS_DYNAMIC_ALLOC);
274#endif
275
276 return VINF_SUCCESS;
277}
278
279
280/**
281 * Resets (zeros) the RAM.
282 *
283 * ASSUMES that the caller owns the PGM lock.
284 *
285 * @returns VBox status code.
286 * @param pVM Pointer to the shared VM structure.
287 */
288int pgmR3PhysRamReset(PVM pVM)
289{
290 /*
291 * Walk the ram ranges.
292 */
293 for (PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3; pRam; pRam = pRam->pNextR3)
294 {
295 uint32_t iPage = pRam->cb >> PAGE_SHIFT; Assert((RTGCPHYS)iPage << PAGE_SHIFT == pRam->cb);
296#ifdef VBOX_WITH_NEW_PHYS_CODE
297 if (!pVM->pgm.f.fRamPreAlloc)
298 {
299 /* Replace all RAM pages by ZERO pages. */
300 while (iPage-- > 0)
301 {
302 PPGMPAGE pPage = &pRam->aPages[iPage];
303 switch (PGM_PAGE_GET_TYPE(pPage))
304 {
305 case PGMPAGETYPE_RAM:
306 if (!PGM_PAGE_IS_ZERO(pPage))
307 pgmPhysFreePage(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)i << PAGE_SHIFT));
308 break;
309
310 case PGMPAGETYPE_MMIO2:
311 case PGMPAGETYPE_ROM_SHADOW: /* handled by pgmR3PhysRomReset. */
312 case PGMPAGETYPE_ROM:
313 case PGMPAGETYPE_MMIO:
314 break;
315 default:
316 AssertFailed();
317 }
318 } /* for each page */
319 }
320 else
321#endif
322 {
323 /* Zero the memory. */
324 while (iPage-- > 0)
325 {
326 PPGMPAGE pPage = &pRam->aPages[iPage];
327 switch (PGM_PAGE_GET_TYPE(pPage))
328 {
329#ifndef VBOX_WITH_NEW_PHYS_CODE
330 case PGMPAGETYPE_INVALID:
331 case PGMPAGETYPE_RAM:
332 if (pRam->aPages[iPage].HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)) /** @todo PAGE FLAGS */
333 {
334 /* shadow ram is reloaded elsewhere. */
335 Log4(("PGMR3Reset: not clearing phys page %RGp due to flags %RHp\n", pRam->GCPhys + (iPage << PAGE_SHIFT), pRam->aPages[iPage].HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO))); /** @todo PAGE FLAGS */
336 continue;
337 }
338 if (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
339 {
340 unsigned iChunk = iPage >> (PGM_DYNAMIC_CHUNK_SHIFT - PAGE_SHIFT);
341 if (pRam->pavHCChunkHC[iChunk])
342 ASMMemZero32((char *)pRam->pavHCChunkHC[iChunk] + ((iPage << PAGE_SHIFT) & PGM_DYNAMIC_CHUNK_OFFSET_MASK), PAGE_SIZE);
343 }
344 else
345 ASMMemZero32((char *)pRam->pvHC + (iPage << PAGE_SHIFT), PAGE_SIZE);
346 break;
347#else /* VBOX_WITH_NEW_PHYS_CODE */
348 case PGMPAGETYPE_RAM:
349 switch (PGM_PAGE_GET_STATE(pPage))
350 {
351 case PGM_PAGE_STATE_ZERO:
352 break;
353 case PGM_PAGE_STATE_SHARED:
354 case PGM_PAGE_STATE_WRITE_MONITORED:
355 rc = pgmPhysPageMakeWritable(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)i << PAGE_SHIFT));
356 AssertLogRelRCReturn(rc, rc);
357 case PGM_PAGE_STATE_ALLOCATED:
358 {
359 void *pvPage;
360 PPGMPAGEMAP pMapIgnored;
361 rc = pgmPhysPageMap(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)i << PAGE_SHIFT), &pMapIgnored, &pvPage);
362 AssertLogRelRCReturn(rc, rc);
363 ASMMemZeroPage(pvPage);
364 break;
365 }
366 }
367 break;
368#endif /* VBOX_WITH_NEW_PHYS_CODE */
369
370 case PGMPAGETYPE_MMIO2:
371 case PGMPAGETYPE_ROM_SHADOW:
372 case PGMPAGETYPE_ROM:
373 case PGMPAGETYPE_MMIO:
374 break;
375 default:
376 AssertFailed();
377
378 }
379 } /* for each page */
380 }
381
382 }
383
384 return VINF_SUCCESS;
385}
386
387
388/**
389 * This is the interface IOM is using to register an MMIO region.
390 *
391 * It will check for conflicts and ensure that a RAM range structure
392 * is present before calling the PGMR3HandlerPhysicalRegister API to
393 * register the callbacks.
394 *
395 * @returns VBox status code.
396 *
397 * @param pVM Pointer to the shared VM structure.
398 * @param GCPhys The start of the MMIO region.
399 * @param cb The size of the MMIO region.
400 * @param pfnHandlerR3 The address of the ring-3 handler. (IOMR3MMIOHandler)
401 * @param pvUserR3 The user argument for R3.
402 * @param pfnHandlerR0 The address of the ring-0 handler. (IOMMMIOHandler)
403 * @param pvUserR0 The user argument for R0.
404 * @param pfnHandlerGC The address of the GC handler. (IOMMMIOHandler)
405 * @param pvUserGC The user argument for GC.
406 * @param pszDesc The description of the MMIO region.
407 */
408PDMR3DECL(int) PGMR3PhysMMIORegister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb,
409 R3PTRTYPE(PFNPGMR3PHYSHANDLER) pfnHandlerR3, RTR3PTR pvUserR3,
410 R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnHandlerR0, RTR0PTR pvUserR0,
411 RCPTRTYPE(PFNPGMGCPHYSHANDLER) pfnHandlerGC, RTGCPTR pvUserGC,
412 R3PTRTYPE(const char *) pszDesc)
413{
414 /*
415 * Assert on some assumption.
416 */
417 VM_ASSERT_EMT(pVM);
418 AssertReturn(!(cb & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
419 AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
420 AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
421 AssertReturn(*pszDesc, VERR_INVALID_PARAMETER);
422
423 /*
424 * Make sure there's a RAM range structure for the region.
425 */
426 int rc;
427 RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
428 bool fRamExists = false;
429 PPGMRAMRANGE pRamPrev = NULL;
430 PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3;
431 while (pRam && GCPhysLast >= pRam->GCPhys)
432 {
433 if ( GCPhysLast >= pRam->GCPhys
434 && GCPhys <= pRam->GCPhysLast)
435 {
436 /* Simplification: all within the same range. */
437 AssertLogRelMsgReturn( GCPhys >= pRam->GCPhys
438 && GCPhysLast <= pRam->GCPhysLast,
439 ("%RGp-%RGp (MMIO/%s) falls partly outside %RGp-%RGp (%s)\n",
440 GCPhys, GCPhysLast, pszDesc,
441 pRam->GCPhys, pRam->GCPhysLast, pRam->pszDesc),
442 VERR_PGM_RAM_CONFLICT);
443
444 /* Check that it's all RAM or MMIO pages. */
445 PCPGMPAGE pPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
446 uint32_t cLeft = cb >> PAGE_SHIFT;
447 while (cLeft-- > 0)
448 {
449 AssertLogRelMsgReturn( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM
450 || PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO,
451 ("%RGp-%RGp (MMIO/%s): %RGp is not a RAM or MMIO page - type=%d desc=%s\n",
452 GCPhys, GCPhysLast, pszDesc, PGM_PAGE_GET_TYPE(pPage), pRam->pszDesc),
453 VERR_PGM_RAM_CONFLICT);
454 pPage++;
455 }
456
457 /* Looks good. */
458 fRamExists = true;
459 break;
460 }
461
462 /* next */
463 pRamPrev = pRam;
464 pRam = pRam->pNextR3;
465 }
466 PPGMRAMRANGE pNew;
467 if (fRamExists)
468 pNew = NULL;
469 else
470 {
471 /*
472 * No RAM range, insert an ad-hoc one.
473 *
474 * Note that we don't have to tell REM about this range because
475 * PGMHandlerPhysicalRegisterEx will do that for us.
476 */
477 Log(("PGMR3PhysMMIORegister: Adding ad-hoc MMIO range for %RGp-%RGp %s\n", GCPhys, GCPhysLast, pszDesc));
478
479 const uint32_t cPages = cb >> PAGE_SHIFT;
480 const size_t cbRamRange = RT_OFFSETOF(PGMRAMRANGE, aPages[cPages]);
481 rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMRAMRANGE, aPages[cPages]), 16, MM_TAG_PGM_PHYS, (void **)&pNew);
482 AssertLogRelMsgRCReturn(rc, ("cbRamRange=%zu\n", cbRamRange), rc);
483
484 /* Initialize the range. */
485 pNew->GCPhys = GCPhys;
486 pNew->GCPhysLast = GCPhysLast;
487 pNew->pszDesc = pszDesc;
488 pNew->cb = cb;
489 pNew->fFlags = 0; /* Some MMIO flag here? */
490
491 pNew->pvHC = NULL;
492 pNew->pavHCChunkHC = NULL;
493 pNew->pavHCChunkGC = 0;
494
495 uint32_t iPage = cPages;
496 while (iPage-- > 0)
497 PGM_PAGE_INIT_ZERO_REAL(&pNew->aPages[iPage], pVM, PGMPAGETYPE_MMIO);
498 Assert(PGM_PAGE_GET_TYPE(&pNew->aPages[0]) == PGMPAGETYPE_MMIO);
499
500 /* link it */
501 pgmR3PhysLinkRamRange(pVM, pNew, pRamPrev);
502 }
503
504 /*
505 * Register the access handler.
506 */
507 rc = PGMHandlerPhysicalRegisterEx(pVM, PGMPHYSHANDLERTYPE_MMIO, GCPhys, GCPhysLast,
508 pfnHandlerR3, pvUserR3,
509 pfnHandlerR0, pvUserR0,
510 pfnHandlerGC, pvUserGC, pszDesc);
511 if ( RT_FAILURE(rc)
512 && !fRamExists)
513 {
514 /* remove the ad-hoc range. */
515 pgmR3PhysUnlinkRamRange2(pVM, pNew, pRamPrev);
516 pNew->cb = pNew->GCPhys = pNew->GCPhysLast = NIL_RTGCPHYS;
517 MMHyperFree(pVM, pRam);
518 }
519
520 return rc;
521}
522
523
524/**
525 * This is the interface IOM is using to register an MMIO region.
526 *
527 * It will take care of calling PGMHandlerPhysicalDeregister and clean up
528 * any ad-hoc PGMRAMRANGE left behind.
529 *
530 * @returns VBox status code.
531 * @param pVM Pointer to the shared VM structure.
532 * @param GCPhys The start of the MMIO region.
533 * @param cb The size of the MMIO region.
534 */
535PDMR3DECL(int) PGMR3PhysMMIODeregister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb)
536{
537 VM_ASSERT_EMT(pVM);
538
539 /*
540 * First deregister the handler, then check if we should remove the ram range.
541 */
542 int rc = PGMHandlerPhysicalDeregister(pVM, GCPhys);
543 if (RT_SUCCESS(rc))
544 {
545 RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
546 PPGMRAMRANGE pRamPrev = NULL;
547 PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3;
548 while (pRam && GCPhysLast >= pRam->GCPhys)
549 {
550 /*if ( GCPhysLast >= pRam->GCPhys
551 && GCPhys <= pRam->GCPhysLast) - later */
552 if ( GCPhysLast == pRam->GCPhysLast
553 && GCPhys == pRam->GCPhys)
554 {
555 Assert(pRam->cb == cb);
556
557 /*
558 * See if all the pages are dead MMIO pages.
559 */
560 bool fAllMMIO = true;
561 PPGMPAGE pPage = &pRam->aPages[0];
562 uint32_t cLeft = cb >> PAGE_SHIFT;
563 while (cLeft-- > 0)
564 {
565 if ( PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO
566 /*|| not-out-of-action later */)
567 {
568 fAllMMIO = false;
569 break;
570 }
571 pPage++;
572 }
573
574 /*
575 * Unlink it and free if it's all MMIO.
576 */
577 if (fAllMMIO)
578 {
579 Log(("PGMR3PhysMMIODeregister: Freeing ad-hoc MMIO range for %RGp-%RGp %s\n",
580 GCPhys, GCPhysLast, pRam->pszDesc));
581
582 pgmR3PhysUnlinkRamRange2(pVM, pRam, pRamPrev);
583 pRam->cb = pRam->GCPhys = pRam->GCPhysLast = NIL_RTGCPHYS;
584 MMHyperFree(pVM, pRam);
585 }
586 break;
587 }
588
589 /* next */
590 pRamPrev = pRam;
591 pRam = pRam->pNextR3;
592 }
593 }
594
595 return rc;
596}
597
598
599/**
600 * Locate a MMIO2 range.
601 *
602 * @returns Pointer to the MMIO2 range.
603 * @param pVM Pointer to the shared VM structure.
604 * @param pDevIns The device instance owning the region.
605 * @param iRegion The region.
606 */
607DECLINLINE(PPGMMMIO2RANGE) pgmR3PhysMMIO2Find(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion)
608{
609 /*
610 * Search the list.
611 */
612 for (PPGMMMIO2RANGE pCur = pVM->pgm.s.pMmio2RangesR3; pCur; pCur = pCur->pNextR3)
613 if (pCur->pDevInsR3 == pDevIns)
614 return pCur;
615 return NULL;
616}
617
618
619/**
620 * Allocate and register a MMIO2 region.
621 *
622 * As mentioned elsewhere, MMIO2 is just RAM spelled differently. It's
623 * RAM associated with a device. It is also non-shared memory with a
624 * permanent ring-3 mapping and page backing (presently).
625 *
626 * A MMIO2 range may overlap with base memory if a lot of RAM
627 * is configured for the VM, in which case we'll drop the base
628 * memory pages. Presently we will make no attempt to preserve
629 * anything that happens to be present in the base memory that
630 * is replaced, this is of course incorrectly but it's too much
631 * effort.
632 *
633 * @returns VBox status code.
634 * @retval VINF_SUCCESS on success, *ppv pointing to the R3 mapping of the memory.
635 * @retval VERR_ALREADY_EXISTS if the region already exists.
636 *
637 * @param pVM Pointer to the shared VM structure.
638 * @param pDevIns The device instance owning the region.
639 * @param iRegion The region number. If the MMIO2 memory is a PCI I/O region
640 * this number has to be the number of that region. Otherwise
641 * it can be any number safe UINT8_MAX.
642 * @param cb The size of the region. Must be page aligned.
643 * @param fFlags Reserved for future use, must be zero.
644 * @param ppv Where to store the pointer to the ring-3 mapping of the memory.
645 * @param pszDesc The description.
646 */
647PDMR3DECL(int) PGMR3PhysMMIO2Register(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS cb, uint32_t fFlags, void **ppv, const char *pszDesc)
648{
649 /*
650 * Validate input.
651 */
652 VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
653 AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
654 AssertReturn(iRegion <= UINT8_MAX, VERR_INVALID_PARAMETER);
655 AssertPtrReturn(ppv, VERR_INVALID_POINTER);
656 AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
657 AssertReturn(*pszDesc, VERR_INVALID_PARAMETER);
658 AssertReturn(pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion) == NULL, VERR_ALREADY_EXISTS);
659 AssertReturn(!(cb & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
660 AssertReturn(cb, VERR_INVALID_PARAMETER);
661 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
662
663 const uint32_t cPages = cb >> PAGE_SHIFT;
664 AssertLogRelReturn((RTGCPHYS)cPages << PAGE_SHIFT == cb, VERR_INVALID_PARAMETER);
665 AssertLogRelReturn(cPages <= INT32_MAX / 2, VERR_NO_MEMORY);
666
667 /*
668 * Try reserve and allocate the backing memory first as this is what is
669 * most likely to fail.
670 */
671 int rc = MMR3AdjustFixedReservation(pVM, cPages, pszDesc);
672 if (RT_FAILURE(rc))
673 return rc;
674
675 void *pvPages;
676 PSUPPAGE paPages = (PSUPPAGE)RTMemTmpAlloc(cPages * sizeof(SUPPAGE));
677 if (RT_SUCCESS(rc))
678 rc = SUPPageAllocLockedEx(cPages, &pvPages, paPages);
679 if (RT_SUCCESS(rc))
680 {
681 /*
682 * Create the MMIO2 range record for it.
683 */
684 const size_t cbRange = RT_OFFSETOF(PGMMMIO2RANGE, RamRange.aPages[cPages]);
685 PPGMMMIO2RANGE pNew;
686 rc = MMR3HyperAllocOnceNoRel(pVM, cbRange, 0, MM_TAG_PGM_PHYS, (void **)&pNew);
687 AssertLogRelMsgRC(rc, ("cbRamRange=%zu\n", cbRange));
688 if (RT_SUCCESS(rc))
689 {
690 pNew->pDevInsR3 = pDevIns;
691 pNew->pvR3 = pvPages;
692 //pNew->pNext = NULL;
693 //pNew->fMapped = false;
694 //pNew->fOverlapping = false;
695 pNew->iRegion = iRegion;
696 pNew->RamRange.GCPhys = NIL_RTGCPHYS;
697 pNew->RamRange.GCPhysLast = NIL_RTGCPHYS;
698 pNew->RamRange.pszDesc = pszDesc;
699 pNew->RamRange.cb = cb;
700 //pNew->RamRange.fFlags = 0;
701
702 pNew->RamRange.pvHC = pvPages; ///@todo remove this
703 pNew->RamRange.pavHCChunkHC = NULL; ///@todo remove this
704 pNew->RamRange.pavHCChunkGC = 0; ///@todo remove this
705
706 uint32_t iPage = cPages;
707 while (iPage-- > 0)
708 {
709 PGM_PAGE_INIT(&pNew->RamRange.aPages[iPage],
710 paPages[iPage].Phys & X86_PTE_PAE_PG_MASK, NIL_GMM_PAGEID,
711 PGMPAGETYPE_MMIO2, PGM_PAGE_STATE_ALLOCATED);
712 }
713
714 /*
715 * Link it into the list.
716 * Since there is no particular order, just push it.
717 */
718 pNew->pNextR3 = pVM->pgm.s.pMmio2RangesR3;
719 pVM->pgm.s.pMmio2RangesR3 = pNew;
720
721 *ppv = pvPages;
722 RTMemTmpFree(paPages);
723 return VINF_SUCCESS;
724 }
725
726 SUPPageFreeLocked(pvPages, cPages);
727 }
728 RTMemTmpFree(paPages);
729 MMR3AdjustFixedReservation(pVM, -cPages, pszDesc);
730 return rc;
731}
732
733
734/**
735 * Deregisters and frees a MMIO2 region.
736 *
737 * Any physical (and virtual) access handlers registered for the region must
738 * be deregistered before calling this function.
739 *
740 * @returns VBox status code.
741 * @param pVM Pointer to the shared VM structure.
742 * @param pDevIns The device instance owning the region.
743 * @param iRegion The region. If it's UINT32_MAX it'll be a wildcard match.
744 */
745PDMR3DECL(int) PGMR3PhysMMIO2Deregister(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion)
746{
747 /*
748 * Validate input.
749 */
750 VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
751 AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
752 AssertReturn(iRegion <= UINT8_MAX || iRegion == UINT32_MAX, VERR_INVALID_PARAMETER);
753
754 int rc = VINF_SUCCESS;
755 unsigned cFound = 0;
756 PPGMMMIO2RANGE pPrev = NULL;
757 PPGMMMIO2RANGE pCur = pVM->pgm.s.pMmio2RangesR3;
758 while (pCur)
759 {
760 if ( pCur->pDevInsR3 == pDevIns
761 && ( iRegion == UINT32_MAX
762 || pCur->iRegion == iRegion))
763 {
764 cFound++;
765
766 /*
767 * Unmap it if it's mapped.
768 */
769 if (pCur->fMapped)
770 {
771 int rc2 = PGMR3PhysMMIO2Unmap(pVM, pCur->pDevInsR3, pCur->iRegion, pCur->RamRange.GCPhys);
772 AssertRC(rc2);
773 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
774 rc = rc2;
775 }
776
777 /*
778 * Unlink it
779 */
780 PPGMMMIO2RANGE pNext = pCur->pNextR3;
781 if (pPrev)
782 pPrev->pNextR3 = pNext;
783 else
784 pVM->pgm.s.pMmio2RangesR3 = pNext;
785 pCur->pNextR3 = NULL;
786
787 /*
788 * Free the memory.
789 */
790 int rc2 = SUPPageFreeLocked(pCur->pvR3, pCur->RamRange.cb >> PAGE_SHIFT);
791 AssertRC(rc2);
792 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
793 rc = rc2;
794
795 rc2 = MMR3AdjustFixedReservation(pVM, -(pCur->RamRange.cb >> PAGE_SHIFT), pCur->RamRange.pszDesc);
796 AssertRC(rc2);
797 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
798 rc = rc2;
799
800 /* we're leaking hyper memory here if done at runtime. */
801 Assert( VMR3GetState(pVM) == VMSTATE_OFF
802 || VMR3GetState(pVM) == VMSTATE_DESTROYING
803 || VMR3GetState(pVM) == VMSTATE_TERMINATED
804 || VMR3GetState(pVM) == VMSTATE_CREATING);
805 /*rc = MMHyperFree(pVM, pCur);
806 AssertRCReturn(rc, rc); - not safe, see the alloc call. */
807
808 /* next */
809 pCur = pNext;
810 }
811 else
812 {
813 pPrev = pCur;
814 pCur = pCur->pNextR3;
815 }
816 }
817
818 return !cFound && iRegion != UINT32_MAX ? VERR_NOT_FOUND : rc;
819}
820
821
822/**
823 * Maps a MMIO2 region.
824 *
825 * This is done when a guest / the bios / state loading changes the
826 * PCI config. The replacing of base memory has the same restrictions
827 * as during registration, of course.
828 *
829 * @returns VBox status code.
830 *
831 * @param pVM Pointer to the shared VM structure.
832 * @param pDevIns The
833 */
834PDMR3DECL(int) PGMR3PhysMMIO2Map(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS GCPhys)
835{
836 /*
837 * Validate input
838 */
839 VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
840 AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
841 AssertReturn(iRegion <= UINT8_MAX, VERR_INVALID_PARAMETER);
842 AssertReturn(GCPhys != NIL_RTGCPHYS, VERR_INVALID_PARAMETER);
843 AssertReturn(GCPhys != 0, VERR_INVALID_PARAMETER);
844 AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
845
846 PPGMMMIO2RANGE pCur = pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion);
847 AssertReturn(pCur, VERR_NOT_FOUND);
848 AssertReturn(!pCur->fMapped, VERR_WRONG_ORDER);
849 Assert(pCur->RamRange.GCPhys == NIL_RTGCPHYS);
850 Assert(pCur->RamRange.GCPhysLast == NIL_RTGCPHYS);
851
852 const RTGCPHYS GCPhysLast = GCPhys + pCur->RamRange.cb - 1;
853 AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
854
855 /*
856 * Find our location in the ram range list, checking for
857 * restriction we don't bother implementing yet (partially overlapping).
858 */
859 bool fRamExists = false;
860 PPGMRAMRANGE pRamPrev = NULL;
861 PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3;
862 while (pRam && GCPhysLast >= pRam->GCPhys)
863 {
864 if ( GCPhys <= pRam->GCPhysLast
865 && GCPhysLast >= pRam->GCPhys)
866 {
867 /* completely within? */
868 AssertLogRelMsgReturn( GCPhys >= pRam->GCPhys
869 && GCPhysLast <= pRam->GCPhysLast,
870 ("%RGp-%RGp (MMIO2/%s) falls partly outside %RGp-%RGp (%s)\n",
871 GCPhys, GCPhysLast, pCur->RamRange.pszDesc,
872 pRam->GCPhys, pRam->GCPhysLast, pRam->pszDesc),
873 VERR_PGM_RAM_CONFLICT);
874 fRamExists = true;
875 break;
876 }
877
878 /* next */
879 pRamPrev = pRam;
880 pRam = pRam->pNextR3;
881 }
882 if (fRamExists)
883 {
884 PPGMPAGE pPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
885 uint32_t cPagesLeft = pCur->RamRange.cb >> PAGE_SHIFT;
886 while (cPagesLeft-- > 0)
887 {
888 AssertLogRelMsgReturn(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM,
889 ("%RGp isn't a RAM page (%d) - mapping %RGp-%RGp (MMIO2/%s).\n",
890 GCPhys, PGM_PAGE_GET_TYPE(pPage), GCPhys, GCPhysLast, pCur->RamRange.pszDesc),
891 VERR_PGM_RAM_CONFLICT);
892 pPage++;
893 }
894 }
895 Log(("PGMR3PhysMMIO2Map: %RGp-%RGp fRamExists=%RTbool %s\n",
896 GCPhys, GCPhysLast, fRamExists, pCur->RamRange.pszDesc));
897
898 /*
899 * Make the changes.
900 */
901 pgmLock(pVM);
902
903 pCur->RamRange.GCPhys = GCPhys;
904 pCur->RamRange.GCPhysLast = GCPhysLast;
905 pCur->fMapped = true;
906 pCur->fOverlapping = fRamExists;
907
908 if (fRamExists)
909 {
910 /* replace the pages, freeing all present RAM pages. */
911 PPGMPAGE pPageSrc = &pCur->RamRange.aPages[0];
912 PPGMPAGE pPageDst = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
913 uint32_t cPagesLeft = pCur->RamRange.cb >> PAGE_SHIFT;
914 while (cPagesLeft-- > 0)
915 {
916 pgmPhysFreePage(pVM, pPageDst, GCPhys);
917
918 RTHCPHYS const HCPhys = PGM_PAGE_GET_HCPHYS(pPageSrc);
919 PGM_PAGE_SET_HCPHYS(pPageDst, HCPhys);
920 PGM_PAGE_SET_TYPE(pPageDst, PGMPAGETYPE_MMIO2);
921 PGM_PAGE_SET_STATE(pPageDst, PGM_PAGE_STATE_ALLOCATED);
922
923 GCPhys += PAGE_SIZE;
924 pPageSrc++;
925 pPageDst++;
926 }
927 }
928 else
929 {
930 /* link in the ram range */
931 pgmR3PhysLinkRamRange(pVM, &pCur->RamRange, pRamPrev);
932 REMR3NotifyPhysRamRegister(pVM, GCPhys, pCur->RamRange.cb, 0);
933 }
934
935 pgmUnlock(pVM);
936
937 return VINF_SUCCESS;
938}
939
940
941/**
942 * Unmaps a MMIO2 region.
943 *
944 * This is done when a guest / the bios / state loading changes the
945 * PCI config. The replacing of base memory has the same restrictions
946 * as during registration, of course.
947 */
948PDMR3DECL(int) PGMR3PhysMMIO2Unmap(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS GCPhys)
949{
950 /*
951 * Validate input
952 */
953 VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
954 AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
955 AssertReturn(iRegion <= UINT8_MAX, VERR_INVALID_PARAMETER);
956 AssertReturn(GCPhys != NIL_RTGCPHYS, VERR_INVALID_PARAMETER);
957 AssertReturn(GCPhys != 0, VERR_INVALID_PARAMETER);
958 AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
959
960 PPGMMMIO2RANGE pCur = pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion);
961 AssertReturn(pCur, VERR_NOT_FOUND);
962 AssertReturn(pCur->fMapped, VERR_WRONG_ORDER);
963 AssertReturn(pCur->RamRange.GCPhys == GCPhys, VERR_INVALID_PARAMETER);
964 Assert(pCur->RamRange.GCPhysLast != NIL_RTGCPHYS);
965
966 Log(("PGMR3PhysMMIO2Unmap: %RGp-%RGp %s\n",
967 pCur->RamRange.GCPhys, pCur->RamRange.GCPhysLast, pCur->RamRange.pszDesc));
968
969 /*
970 * Unmap it.
971 */
972 pgmLock(pVM);
973
974 if (pCur->fOverlapping)
975 {
976 /* Restore the RAM pages we've replaced. */
977 PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3;
978 while (pRam->GCPhys > pCur->RamRange.GCPhysLast)
979 pRam = pRam->pNextR3;
980
981 RTHCPHYS const HCPhysZeroPg = pVM->pgm.s.HCPhysZeroPg;
982 Assert(HCPhysZeroPg != 0 && HCPhysZeroPg != NIL_RTHCPHYS);
983 PPGMPAGE pPageDst = &pRam->aPages[(pCur->RamRange.GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
984 uint32_t cPagesLeft = pCur->RamRange.cb >> PAGE_SHIFT;
985 while (cPagesLeft-- > 0)
986 {
987 PGM_PAGE_SET_HCPHYS(pPageDst, pVM->pgm.s.HCPhysZeroPg);
988 PGM_PAGE_SET_TYPE(pPageDst, PGMPAGETYPE_RAM);
989 PGM_PAGE_SET_STATE(pPageDst, PGM_PAGE_STATE_ZERO);
990
991 pPageDst++;
992 }
993 }
994 else
995 {
996 REMR3NotifyPhysReserve(pVM, pCur->RamRange.GCPhys, pCur->RamRange.cb);
997 pgmR3PhysUnlinkRamRange(pVM, &pCur->RamRange);
998 }
999
1000 pCur->RamRange.GCPhys = NIL_RTGCPHYS;
1001 pCur->RamRange.GCPhysLast = NIL_RTGCPHYS;
1002 pCur->fOverlapping = false;
1003 pCur->fMapped = false;
1004
1005 pgmUnlock(pVM);
1006
1007 return VINF_SUCCESS;
1008}
1009
1010
1011/**
1012 * Checks if the given address is an MMIO2 base address or not.
1013 *
1014 * @returns true/false accordingly.
1015 * @param pVM Pointer to the shared VM structure.
1016 * @param pDevIns The owner of the memory, optional.
1017 * @param GCPhys The address to check.
1018 */
1019PDMR3DECL(bool) PGMR3PhysMMIO2IsBase(PVM pVM, PPDMDEVINS pDevIns, RTGCPHYS GCPhys)
1020{
1021 /*
1022 * Validate input
1023 */
1024 VM_ASSERT_EMT_RETURN(pVM, false);
1025 AssertPtrReturn(pDevIns, false);
1026 AssertReturn(GCPhys != NIL_RTGCPHYS, false);
1027 AssertReturn(GCPhys != 0, false);
1028 AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), false);
1029
1030 /*
1031 * Search the list.
1032 */
1033 for (PPGMMMIO2RANGE pCur = pVM->pgm.s.pMmio2RangesR3; pCur; pCur = pCur->pNextR3)
1034 if (pCur->RamRange.GCPhys == GCPhys)
1035 {
1036 Assert(pCur->fMapped);
1037 return true;
1038 }
1039 return false;
1040}
1041
1042
1043/**
1044 * Gets the HC physical address of a page in the MMIO2 region.
1045 *
1046 * This is API is intended for MMHyper and shouldn't be called
1047 * by anyone else...
1048 *
1049 * @returns VBox status code.
1050 * @param pVM Pointer to the shared VM structure.
1051 * @param pDevIns The owner of the memory, optional.
1052 * @param iRegion The region.
1053 * @param off The page expressed an offset into the MMIO2 region.
1054 * @param pHCPhys Where to store the result.
1055 */
1056PDMR3DECL(int) PGMR3PhysMMIO2GetHCPhys(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS off, PRTHCPHYS pHCPhys)
1057{
1058 /*
1059 * Validate input
1060 */
1061 VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
1062 AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
1063 AssertReturn(iRegion <= UINT8_MAX, VERR_INVALID_PARAMETER);
1064
1065 PPGMMMIO2RANGE pCur = pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion);
1066 AssertReturn(pCur, VERR_NOT_FOUND);
1067 AssertReturn(off < pCur->RamRange.cb, VERR_INVALID_PARAMETER);
1068
1069 PCPGMPAGE pPage = &pCur->RamRange.aPages[off >> PAGE_SHIFT];
1070 *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage);
1071 return VINF_SUCCESS;
1072}
1073
1074
1075/**
1076 * Registers a ROM image.
1077 *
1078 * Shadowed ROM images requires double the amount of backing memory, so,
1079 * don't use that unless you have to. Shadowing of ROM images is process
1080 * where we can select where the reads go and where the writes go. On real
1081 * hardware the chipset provides means to configure this. We provide
1082 * PGMR3PhysProtectROM() for this purpose.
1083 *
1084 * A read-only copy of the ROM image will always be kept around while we
1085 * will allocate RAM pages for the changes on demand (unless all memory
1086 * is configured to be preallocated).
1087 *
1088 * @returns VBox status.
1089 * @param pVM VM Handle.
1090 * @param pDevIns The device instance owning the ROM.
1091 * @param GCPhys First physical address in the range.
1092 * Must be page aligned!
1093 * @param cbRange The size of the range (in bytes).
1094 * Must be page aligned!
1095 * @param pvBinary Pointer to the binary data backing the ROM image.
1096 * This must be exactly \a cbRange in size.
1097 * @param fFlags Mask of flags. PGMPHYS_ROM_FLAG_SHADOWED
1098 * and/or PGMPHYS_ROM_FLAG_PERMANENT_BINARY.
1099 * @param pszDesc Pointer to description string. This must not be freed.
1100 *
1101 * @remark There is no way to remove the rom, automatically on device cleanup or
1102 * manually from the device yet. This isn't difficult in any way, it's
1103 * just not something we expect to be necessary for a while.
1104 */
1105PGMR3DECL(int) PGMR3PhysRomRegister(PVM pVM, PPDMDEVINS pDevIns, RTGCPHYS GCPhys, RTGCPHYS cb,
1106 const void *pvBinary, uint32_t fFlags, const char *pszDesc)
1107{
1108 Log(("PGMR3PhysRomRegister: pDevIns=%p GCPhys=%RGp(-%RGp) cb=%RGp pvBinary=%p fFlags=%#x pszDesc=%s\n",
1109 pDevIns, GCPhys, GCPhys + cb, cb, pvBinary, fFlags, pszDesc));
1110
1111 /*
1112 * Validate input.
1113 */
1114 AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
1115 AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
1116 AssertReturn(RT_ALIGN_T(cb, PAGE_SIZE, RTGCPHYS) == cb, VERR_INVALID_PARAMETER);
1117 RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
1118 AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
1119 AssertPtrReturn(pvBinary, VERR_INVALID_PARAMETER);
1120 AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
1121 AssertReturn(!(fFlags & ~(PGMPHYS_ROM_FLAG_SHADOWED | PGMPHYS_ROM_FLAG_PERMANENT_BINARY)), VERR_INVALID_PARAMETER);
1122 VM_ASSERT_STATE_RETURN(pVM, VMSTATE_CREATING, VERR_VM_INVALID_VM_STATE);
1123
1124 const uint32_t cPages = cb >> PAGE_SHIFT;
1125
1126 /*
1127 * Find the ROM location in the ROM list first.
1128 */
1129 PPGMROMRANGE pRomPrev = NULL;
1130 PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3;
1131 while (pRom && GCPhysLast >= pRom->GCPhys)
1132 {
1133 if ( GCPhys <= pRom->GCPhysLast
1134 && GCPhysLast >= pRom->GCPhys)
1135 AssertLogRelMsgFailedReturn(("%RGp-%RGp (%s) conflicts with existing %RGp-%RGp (%s)\n",
1136 GCPhys, GCPhysLast, pszDesc,
1137 pRom->GCPhys, pRom->GCPhysLast, pRom->pszDesc),
1138 VERR_PGM_RAM_CONFLICT);
1139 /* next */
1140 pRomPrev = pRom;
1141 pRom = pRom->pNextR3;
1142 }
1143
1144 /*
1145 * Find the RAM location and check for conflicts.
1146 *
1147 * Conflict detection is a bit different than for RAM
1148 * registration since a ROM can be located within a RAM
1149 * range. So, what we have to check for is other memory
1150 * types (other than RAM that is) and that we don't span
1151 * more than one RAM range (layz).
1152 */
1153 bool fRamExists = false;
1154 PPGMRAMRANGE pRamPrev = NULL;
1155 PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3;
1156 while (pRam && GCPhysLast >= pRam->GCPhys)
1157 {
1158 if ( GCPhys <= pRam->GCPhysLast
1159 && GCPhysLast >= pRam->GCPhys)
1160 {
1161 /* completely within? */
1162 AssertLogRelMsgReturn( GCPhys >= pRam->GCPhys
1163 && GCPhysLast <= pRam->GCPhysLast,
1164 ("%RGp-%RGp (%s) falls partly outside %RGp-%RGp (%s)\n",
1165 GCPhys, GCPhysLast, pszDesc,
1166 pRam->GCPhys, pRam->GCPhysLast, pRam->pszDesc),
1167 VERR_PGM_RAM_CONFLICT);
1168 fRamExists = true;
1169 break;
1170 }
1171
1172 /* next */
1173 pRamPrev = pRam;
1174 pRam = pRam->pNextR3;
1175 }
1176 if (fRamExists)
1177 {
1178 PPGMPAGE pPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
1179 uint32_t cPagesLeft = cPages;
1180 while (cPagesLeft-- > 0)
1181 {
1182 AssertLogRelMsgReturn(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM,
1183 ("%RGp isn't a RAM page (%d) - registering %RGp-%RGp (%s).\n",
1184 GCPhys, PGM_PAGE_GET_TYPE(pPage), GCPhys, GCPhysLast, pszDesc),
1185 VERR_PGM_RAM_CONFLICT);
1186 Assert(PGM_PAGE_IS_ZERO(pPage));
1187 pPage++;
1188 }
1189 }
1190
1191 /*
1192 * Update the base memory reservation if necessary.
1193 */
1194 uint32_t cExtraBaseCost = fRamExists ? cPages : 0;
1195 if (fFlags & PGMPHYS_ROM_FLAG_SHADOWED)
1196 cExtraBaseCost += cPages;
1197 if (cExtraBaseCost)
1198 {
1199 int rc = MMR3IncreaseBaseReservation(pVM, cExtraBaseCost);
1200 if (RT_FAILURE(rc))
1201 return rc;
1202 }
1203
1204 /*
1205 * Allocate memory for the virgin copy of the RAM.
1206 */
1207 PGMMALLOCATEPAGESREQ pReq;
1208 int rc = GMMR3AllocatePagesPrepare(pVM, &pReq, cPages, GMMACCOUNT_BASE);
1209 AssertRCReturn(rc, rc);
1210
1211 for (uint32_t iPage = 0; iPage < cPages; iPage++)
1212 {
1213 pReq->aPages[iPage].HCPhysGCPhys = GCPhys + (iPage << PAGE_SHIFT);
1214 pReq->aPages[iPage].idPage = NIL_GMM_PAGEID;
1215 pReq->aPages[iPage].idSharedPage = NIL_GMM_PAGEID;
1216 }
1217
1218 pgmLock(pVM);
1219 rc = GMMR3AllocatePagesPerform(pVM, pReq);
1220 pgmUnlock(pVM);
1221 if (RT_FAILURE(rc))
1222 {
1223 GMMR3AllocatePagesCleanup(pReq);
1224 return rc;
1225 }
1226
1227 /*
1228 * Allocate the new ROM range and RAM range (if necessary).
1229 */
1230 PPGMROMRANGE pRomNew;
1231 rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMROMRANGE, aPages[cPages]), 0, MM_TAG_PGM_PHYS, (void **)&pRomNew);
1232 if (RT_SUCCESS(rc))
1233 {
1234 PPGMRAMRANGE pRamNew = NULL;
1235 if (!fRamExists)
1236 rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMRAMRANGE, aPages[cPages]), sizeof(PGMPAGE), MM_TAG_PGM_PHYS, (void **)&pRamNew);
1237 if (RT_SUCCESS(rc))
1238 {
1239 pgmLock(pVM);
1240
1241 /*
1242 * Initialize and insert the RAM range (if required).
1243 */
1244 PPGMROMPAGE pRomPage = &pRomNew->aPages[0];
1245 if (!fRamExists)
1246 {
1247 pRamNew->GCPhys = GCPhys;
1248 pRamNew->GCPhysLast = GCPhysLast;
1249 pRamNew->pszDesc = pszDesc;
1250 pRamNew->cb = cb;
1251 pRamNew->fFlags = 0;
1252 pRamNew->pvHC = NULL;
1253
1254 PPGMPAGE pPage = &pRamNew->aPages[0];
1255 for (uint32_t iPage = 0; iPage < cPages; iPage++, pPage++, pRomPage++)
1256 {
1257 PGM_PAGE_INIT(pPage,
1258 pReq->aPages[iPage].HCPhysGCPhys,
1259 pReq->aPages[iPage].idPage,
1260 PGMPAGETYPE_ROM,
1261 PGM_PAGE_STATE_ALLOCATED);
1262
1263 pRomPage->Virgin = *pPage;
1264 }
1265
1266 pgmR3PhysLinkRamRange(pVM, pRamNew, pRamPrev);
1267 }
1268 else
1269 {
1270 PPGMPAGE pPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
1271 for (uint32_t iPage = 0; iPage < cPages; iPage++, pPage++, pRomPage++)
1272 {
1273 PGM_PAGE_SET_TYPE(pPage, PGMPAGETYPE_ROM);
1274 PGM_PAGE_SET_HCPHYS(pPage, pReq->aPages[iPage].HCPhysGCPhys);
1275 PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
1276 PGM_PAGE_SET_PAGEID(pPage, pReq->aPages[iPage].idPage);
1277
1278 pRomPage->Virgin = *pPage;
1279 }
1280
1281 pRamNew = pRam;
1282 }
1283 pgmUnlock(pVM);
1284
1285
1286 /*
1287 * Register the write access handler for the range (PGMROMPROT_READ_ROM_WRITE_IGNORE).
1288 */
1289 rc = PGMR3HandlerPhysicalRegister(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE, GCPhys, GCPhysLast,
1290#if 0 /** @todo we actually need a ring-3 write handler here for shadowed ROMs, so hack REM! */
1291 pgmR3PhysRomWriteHandler, pRomNew,
1292#else
1293 NULL, NULL,
1294#endif
1295 NULL, "pgmPhysRomWriteHandler", MMHyperCCToR0(pVM, pRomNew),
1296 NULL, "pgmPhysRomWriteHandler", MMHyperCCToGC(pVM, pRomNew), pszDesc);
1297 if (RT_SUCCESS(rc))
1298 {
1299 pgmLock(pVM);
1300
1301 /*
1302 * Copy the image over to the virgin pages.
1303 * This must be done after linking in the RAM range.
1304 */
1305 PPGMPAGE pRamPage = &pRamNew->aPages[(GCPhys - pRamNew->GCPhys) >> PAGE_SHIFT];
1306 for (uint32_t iPage = 0; iPage < cPages; iPage++, pRamPage++)
1307 {
1308 void *pvDstPage;
1309 PPGMPAGEMAP pMapIgnored;
1310 rc = pgmPhysPageMap(pVM, pRamPage, GCPhys + (iPage << PAGE_SHIFT), &pMapIgnored, &pvDstPage);
1311 if (RT_FAILURE(rc))
1312 {
1313 VMSetError(pVM, rc, RT_SRC_POS, "Failed to map virgin ROM page at %RGp", GCPhys);
1314 break;
1315 }
1316 memcpy(pvDstPage, (const uint8_t *)pvBinary + (iPage << PAGE_SHIFT), PAGE_SIZE);
1317 }
1318 if (RT_SUCCESS(rc))
1319 {
1320 /*
1321 * Initialize the ROM range.
1322 * Note that the Virgin member of the pages has already been initialized above.
1323 */
1324 pRomNew->GCPhys = GCPhys;
1325 pRomNew->GCPhysLast = GCPhysLast;
1326 pRomNew->cb = cb;
1327 pRomNew->fFlags = fFlags;
1328 pRomNew->pvOriginal = fFlags & PGMPHYS_ROM_FLAG_PERMANENT_BINARY ? pvBinary : NULL;
1329 pRomNew->pszDesc = pszDesc;
1330
1331 for (unsigned iPage = 0; iPage < cPages; iPage++)
1332 {
1333 PPGMROMPAGE pPage = &pRomNew->aPages[iPage];
1334 pPage->enmProt = PGMROMPROT_READ_ROM_WRITE_IGNORE;
1335 PGM_PAGE_INIT_ZERO_REAL(&pPage->Shadow, pVM, PGMPAGETYPE_ROM_SHADOW);
1336 }
1337
1338 /*
1339 * Insert the ROM range, tell REM and return successfully.
1340 */
1341 pRomNew->pNextR3 = pRom;
1342 pRomNew->pNextR0 = pRom ? MMHyperCCToR0(pVM, pRom) : NIL_RTR0PTR;
1343 pRomNew->pNextGC = pRom ? MMHyperCCToGC(pVM, pRom) : NIL_RTGCPTR;
1344
1345 if (pRomPrev)
1346 {
1347 pRomPrev->pNextR3 = pRomNew;
1348 pRomPrev->pNextR0 = MMHyperCCToR0(pVM, pRomNew);
1349 pRomPrev->pNextGC = MMHyperCCToGC(pVM, pRomNew);
1350 }
1351 else
1352 {
1353 pVM->pgm.s.pRomRangesR3 = pRomNew;
1354 pVM->pgm.s.pRomRangesR0 = MMHyperCCToR0(pVM, pRomNew);
1355 pVM->pgm.s.pRomRangesGC = MMHyperCCToGC(pVM, pRomNew);
1356 }
1357
1358 REMR3NotifyPhysRomRegister(pVM, GCPhys, cb, NULL, false); /** @todo fix shadowing and REM. */
1359
1360 GMMR3AllocatePagesCleanup(pReq);
1361 pgmUnlock(pVM);
1362 return VINF_SUCCESS;
1363 }
1364
1365 /* bail out */
1366
1367 pgmUnlock(pVM);
1368 int rc2 = PGMHandlerPhysicalDeregister(pVM, GCPhys);
1369 AssertRC(rc2);
1370 pgmLock(pVM);
1371 }
1372
1373 pgmR3PhysUnlinkRamRange2(pVM, pRamNew, pRamPrev);
1374 if (pRamNew)
1375 MMHyperFree(pVM, pRamNew);
1376 }
1377 MMHyperFree(pVM, pRomNew);
1378 }
1379
1380 /** @todo Purge the mapping cache or something... */
1381 GMMR3FreeAllocatedPages(pVM, pReq);
1382 GMMR3AllocatePagesCleanup(pReq);
1383 pgmUnlock(pVM);
1384 return rc;
1385}
1386
1387
1388/**
1389 * \#PF Handler callback for ROM write accesses.
1390 *
1391 * @returns VINF_SUCCESS if the handler have carried out the operation.
1392 * @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
1393 * @param pVM VM Handle.
1394 * @param GCPhys The physical address the guest is writing to.
1395 * @param pvPhys The HC mapping of that address.
1396 * @param pvBuf What the guest is reading/writing.
1397 * @param cbBuf How much it's reading/writing.
1398 * @param enmAccessType The access type.
1399 * @param pvUser User argument.
1400 */
1401/*static - shut up warning */
1402 DECLCALLBACK(int) pgmR3PhysRomWriteHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser)
1403{
1404 PPGMROMRANGE pRom = (PPGMROMRANGE)pvUser;
1405 const uint32_t iPage = GCPhys - pRom->GCPhys;
1406 Assert(iPage < (pRom->cb >> PAGE_SHIFT));
1407 PPGMROMPAGE pRomPage = &pRom->aPages[iPage];
1408 switch (pRomPage->enmProt)
1409 {
1410 /*
1411 * Ignore.
1412 */
1413 case PGMROMPROT_READ_ROM_WRITE_IGNORE:
1414 case PGMROMPROT_READ_RAM_WRITE_IGNORE:
1415 return VINF_SUCCESS;
1416
1417 /*
1418 * Write to the ram page.
1419 */
1420 case PGMROMPROT_READ_ROM_WRITE_RAM:
1421 case PGMROMPROT_READ_RAM_WRITE_RAM: /* yes this will get here too, it's *way* simpler that way. */
1422 {
1423 /* This should be impossible now, pvPhys doesn't work cross page anylonger. */
1424 Assert(((GCPhys - pRom->GCPhys + cbBuf - 1) >> PAGE_SHIFT) == iPage);
1425
1426 /*
1427 * Take the lock, do lazy allocation, map the page and copy the data.
1428 *
1429 * Note that we have to bypass the mapping TLB since it works on
1430 * guest physical addresses and entering the shadow page would
1431 * kind of screw things up...
1432 */
1433 int rc = pgmLock(pVM);
1434 AssertRC(rc);
1435
1436 if (RT_UNLIKELY(PGM_PAGE_GET_STATE(&pRomPage->Shadow) != PGM_PAGE_STATE_ALLOCATED))
1437 {
1438 rc = pgmPhysPageMakeWritable(pVM, &pRomPage->Shadow, GCPhys);
1439 if (RT_FAILURE(rc))
1440 {
1441 pgmUnlock(pVM);
1442 return rc;
1443 }
1444 }
1445
1446 void *pvDstPage;
1447 PPGMPAGEMAP pMapIgnored;
1448 rc = pgmPhysPageMap(pVM, &pRomPage->Shadow, GCPhys & X86_PTE_PG_MASK, &pMapIgnored, &pvDstPage);
1449 if (RT_SUCCESS(rc))
1450 memcpy((uint8_t *)pvDstPage + (GCPhys & PAGE_OFFSET_MASK), pvBuf, cbBuf);
1451
1452 pgmUnlock(pVM);
1453 return rc;
1454 }
1455
1456 default:
1457 AssertMsgFailedReturn(("enmProt=%d iPage=%d GCPhys=%RGp\n",
1458 pRom->aPages[iPage].enmProt, iPage, GCPhys),
1459 VERR_INTERNAL_ERROR);
1460 }
1461}
1462
1463
1464
1465/**
1466 * Called by PGMR3Reset to reset the shadow, switch to the virgin,
1467 * and verify that the virgin part is untouched.
1468 *
1469 * This is done after the normal memory has been cleared.
1470 *
1471 * ASSUMES that the caller owns the PGM lock.
1472 *
1473 * @param pVM The VM handle.
1474 */
1475int pgmR3PhysRomReset(PVM pVM)
1476{
1477 for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
1478 {
1479 const uint32_t cPages = pRom->cb >> PAGE_SHIFT;
1480
1481 if (pRom->fFlags & PGMPHYS_ROM_FLAG_SHADOWED)
1482 {
1483 /*
1484 * Reset the physical handler.
1485 */
1486 int rc = PGMR3PhysRomProtect(pVM, pRom->GCPhys, pRom->cb, PGMROMPROT_READ_ROM_WRITE_IGNORE);
1487 AssertRCReturn(rc, rc);
1488
1489 /*
1490 * What we do with the shadow pages depends on the memory
1491 * preallocation option. If not enabled, we'll just throw
1492 * out all the dirty pages and replace them by the zero page.
1493 */
1494 if (1)///@todo !pVM->pgm.f.fRamPreAlloc)
1495 {
1496 /* Count dirty shadow pages. */
1497 uint32_t cDirty = 0;
1498 uint32_t iPage = cPages;
1499 while (iPage-- > 0)
1500 if (PGM_PAGE_GET_STATE(&pRom->aPages[iPage].Shadow) != PGM_PAGE_STATE_ZERO)
1501 cDirty++;
1502 if (cDirty)
1503 {
1504 /* Free the dirty pages. */
1505 PGMMFREEPAGESREQ pReq;
1506 rc = GMMR3FreePagesPrepare(pVM, &pReq, cDirty, GMMACCOUNT_BASE);
1507 AssertRCReturn(rc, rc);
1508
1509 uint32_t iReqPage = 0;
1510 for (iPage = 0; iPage < cPages; iPage++)
1511 if (PGM_PAGE_GET_STATE(&pRom->aPages[iPage].Shadow) != PGM_PAGE_STATE_ZERO)
1512 {
1513 pReq->aPages[iReqPage].idPage = PGM_PAGE_GET_PAGEID(&pRom->aPages[iPage].Shadow);
1514 iReqPage++;
1515 }
1516
1517 rc = GMMR3FreePagesPerform(pVM, pReq);
1518 GMMR3FreePagesCleanup(pReq);
1519 AssertRCReturn(rc, rc);
1520
1521 /* setup the zero page. */
1522 for (iPage = 0; iPage < cPages; iPage++)
1523 if (PGM_PAGE_GET_STATE(&pRom->aPages[iPage].Shadow) != PGM_PAGE_STATE_ZERO)
1524 PGM_PAGE_INIT_ZERO_REAL(&pRom->aPages[iPage].Shadow, pVM, PGMPAGETYPE_ROM_SHADOW);
1525 }
1526 }
1527 else
1528 {
1529 /* clear all the pages. */
1530 for (uint32_t iPage = 0; iPage < cPages; iPage++)
1531 {
1532 const RTGCPHYS GCPhys = pRom->GCPhys + (iPage << PAGE_SHIFT);
1533 rc = pgmPhysPageMakeWritable(pVM, &pRom->aPages[iPage].Shadow, GCPhys);
1534 if (RT_FAILURE(rc))
1535 break;
1536
1537 void *pvDstPage;
1538 PPGMPAGEMAP pMapIgnored;
1539 rc = pgmPhysPageMap(pVM, &pRom->aPages[iPage].Shadow, GCPhys, &pMapIgnored, &pvDstPage);
1540 if (RT_FAILURE(rc))
1541 break;
1542 ASMMemZeroPage(pvDstPage);
1543 }
1544 AssertRCReturn(rc, rc);
1545 }
1546 }
1547
1548#ifdef VBOX_STRICT
1549 /*
1550 * Verify that the virgin page is unchanged if possible.
1551 */
1552 if (pRom->pvOriginal)
1553 {
1554 uint8_t const *pbSrcPage = (uint8_t const *)pRom->pvOriginal;
1555 for (uint32_t iPage = 0; iPage < cPages; iPage++, pbSrcPage += PAGE_SIZE)
1556 {
1557 const RTGCPHYS GCPhys = pRom->GCPhys + (iPage << PAGE_SHIFT);
1558 PPGMPAGEMAP pMapIgnored;
1559 void *pvDstPage;
1560 int rc = pgmPhysPageMap(pVM, &pRom->aPages[iPage].Virgin, GCPhys, &pMapIgnored, &pvDstPage);
1561 if (RT_FAILURE(rc))
1562 break;
1563 if (memcmp(pvDstPage, pbSrcPage, PAGE_SIZE))
1564 LogRel(("pgmR3PhysRomReset: %RGp rom page changed (%s) - loaded saved state?\n",
1565 GCPhys, pRom->pszDesc));
1566 }
1567 }
1568#endif
1569 }
1570
1571 return VINF_SUCCESS;
1572}
1573
1574
1575/**
1576 * Change the shadowing of a range of ROM pages.
1577 *
1578 * This is intended for implementing chipset specific memory registers
1579 * and will not be very strict about the input. It will silently ignore
1580 * any pages that are not the part of a shadowed ROM.
1581 *
1582 * @returns VBox status code.
1583 * @param pVM Pointer to the shared VM structure.
1584 * @param GCPhys Where to start. Page aligned.
1585 * @param cb How much to change. Page aligned.
1586 * @param enmProt The new ROM protection.
1587 */
1588PGMR3DECL(int) PGMR3PhysRomProtect(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, PGMROMPROT enmProt)
1589{
1590 /*
1591 * Check input
1592 */
1593 if (!cb)
1594 return VINF_SUCCESS;
1595 AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
1596 AssertReturn(!(cb & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
1597 RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
1598 AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
1599 AssertReturn(enmProt >= PGMROMPROT_INVALID && enmProt <= PGMROMPROT_END, VERR_INVALID_PARAMETER);
1600
1601 /*
1602 * Process the request.
1603 */
1604 bool fFlushedPool = false;
1605 for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
1606 if ( GCPhys <= pRom->GCPhysLast
1607 && GCPhysLast >= pRom->GCPhys)
1608 {
1609 /*
1610 * Iterate the relevant pages and the ncessary make changes.
1611 */
1612 bool fChanges = false;
1613 uint32_t const cPages = pRom->GCPhysLast > GCPhysLast
1614 ? pRom->cb >> PAGE_SHIFT
1615 : (GCPhysLast - pRom->GCPhys) >> PAGE_SHIFT;
1616 for (uint32_t iPage = (GCPhys - pRom->GCPhys) >> PAGE_SHIFT;
1617 iPage < cPages;
1618 iPage++)
1619 {
1620 PPGMROMPAGE pRomPage = &pRom->aPages[iPage];
1621 if (PGMROMPROT_IS_ROM(pRomPage->enmProt) != PGMROMPROT_IS_ROM(enmProt))
1622 {
1623 fChanges = true;
1624
1625 /* flush the page pool first so we don't leave any usage references dangling. */
1626 if (!fFlushedPool)
1627 {
1628 pgmPoolFlushAll(pVM);
1629 fFlushedPool = true;
1630 }
1631
1632 PPGMPAGE pOld = PGMROMPROT_IS_ROM(pRomPage->enmProt) ? &pRomPage->Virgin : &pRomPage->Shadow;
1633 PPGMPAGE pNew = PGMROMPROT_IS_ROM(pRomPage->enmProt) ? &pRomPage->Shadow : &pRomPage->Virgin;
1634 PPGMPAGE pRamPage = pgmPhysGetPage(&pVM->pgm.s, pRom->GCPhys + (iPage << PAGE_SHIFT));
1635
1636 *pOld = *pRamPage;
1637 *pRamPage = *pNew;
1638 /** @todo preserve the volatile flags (handlers) when these have been moved out of HCPhys! */
1639 }
1640 }
1641
1642 /*
1643 * Reset the access handler if we made changes, no need
1644 * to optimize this.
1645 */
1646 if (fChanges)
1647 {
1648 int rc = PGMHandlerPhysicalReset(pVM, pRom->GCPhys);
1649 AssertRCReturn(rc, rc);
1650 }
1651
1652 /* Advance - cb isn't updated. */
1653 GCPhys = pRom->GCPhys + (cPages << PAGE_SHIFT);
1654 }
1655
1656 return VINF_SUCCESS;
1657}
1658
1659
1660/**
1661 * Interface that the MMR3RamRegister(), MMR3RomRegister() and MMIO handler
1662 * registration APIs calls to inform PGM about memory registrations.
1663 *
1664 * It registers the physical memory range with PGM. MM is responsible
1665 * for the toplevel things - allocation and locking - while PGM is taking
1666 * care of all the details and implements the physical address space virtualization.
1667 *
1668 * @returns VBox status.
1669 * @param pVM The VM handle.
1670 * @param pvRam HC virtual address of the RAM range. (page aligned)
1671 * @param GCPhys GC physical address of the RAM range. (page aligned)
1672 * @param cb Size of the RAM range. (page aligned)
1673 * @param fFlags Flags, MM_RAM_*.
1674 * @param paPages Pointer an array of physical page descriptors.
1675 * @param pszDesc Description string.
1676 */
1677PGMR3DECL(int) PGMR3PhysRegister(PVM pVM, void *pvRam, RTGCPHYS GCPhys, size_t cb, unsigned fFlags, const SUPPAGE *paPages, const char *pszDesc)
1678{
1679 /*
1680 * Validate input.
1681 * (Not so important because callers are only MMR3PhysRegister()
1682 * and PGMR3HandlerPhysicalRegisterEx(), but anyway...)
1683 */
1684 Log(("PGMR3PhysRegister %08X %x bytes flags %x %s\n", GCPhys, cb, fFlags, pszDesc));
1685
1686 Assert((fFlags & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_DYNAMIC_ALLOC)) || paPages);
1687 /*Assert(!(fFlags & MM_RAM_FLAGS_RESERVED) || !paPages);*/
1688 Assert((fFlags == (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO)) || (fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC) || pvRam);
1689 /*Assert(!(fFlags & MM_RAM_FLAGS_RESERVED) || !pvRam);*/
1690 Assert(!(fFlags & ~0xfff));
1691 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb && cb);
1692 Assert(RT_ALIGN_P(pvRam, PAGE_SIZE) == pvRam);
1693 Assert(!(fFlags & ~(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2 | MM_RAM_FLAGS_DYNAMIC_ALLOC)));
1694 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
1695 RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
1696 if (GCPhysLast < GCPhys)
1697 {
1698 AssertMsgFailed(("The range wraps! GCPhys=%VGp cb=%#x\n", GCPhys, cb));
1699 return VERR_INVALID_PARAMETER;
1700 }
1701
1702 /*
1703 * Find range location and check for conflicts.
1704 */
1705 PPGMRAMRANGE pPrev = NULL;
1706 PPGMRAMRANGE pCur = pVM->pgm.s.pRamRangesR3;
1707 while (pCur)
1708 {
1709 if (GCPhys <= pCur->GCPhysLast && GCPhysLast >= pCur->GCPhys)
1710 {
1711 AssertMsgFailed(("Conflict! This cannot happen!\n"));
1712 return VERR_PGM_RAM_CONFLICT;
1713 }
1714 if (GCPhysLast < pCur->GCPhys)
1715 break;
1716
1717 /* next */
1718 pPrev = pCur;
1719 pCur = pCur->pNextR3;
1720 }
1721
1722 /*
1723 * Allocate RAM range.
1724 * Small ranges are allocated from the heap, big ones have separate mappings.
1725 */
1726 size_t cbRam = RT_OFFSETOF(PGMRAMRANGE, aPages[cb >> PAGE_SHIFT]);
1727 PPGMRAMRANGE pNew;
1728 RTGCPTR GCPtrNew;
1729 int rc = VERR_NO_MEMORY;
1730 if (cbRam > PAGE_SIZE / 2)
1731 { /* large */
1732 cbRam = RT_ALIGN_Z(cbRam, PAGE_SIZE);
1733 rc = SUPPageAlloc(cbRam >> PAGE_SHIFT, (void **)&pNew);
1734 if (VBOX_SUCCESS(rc))
1735 {
1736 rc = MMR3HyperMapHCRam(pVM, pNew, cbRam, true, pszDesc, &GCPtrNew);
1737 if (VBOX_SUCCESS(rc))
1738 {
1739 Assert(MMHyperHC2GC(pVM, pNew) == GCPtrNew);
1740 rc = MMR3HyperReserve(pVM, PAGE_SIZE, "fence", NULL);
1741 }
1742 else
1743 {
1744 AssertMsgFailed(("MMR3HyperMapHCRam(,,%#x,,,) -> %Vrc\n", cbRam, rc));
1745 SUPPageFree(pNew, cbRam >> PAGE_SHIFT);
1746 }
1747 }
1748 else
1749 AssertMsgFailed(("SUPPageAlloc(%#x,,) -> %Vrc\n", cbRam >> PAGE_SHIFT, rc));
1750
1751 }
1752/** @todo Make VGA and VMMDev register their memory at init time before the hma size is fixated. */
1753 if (RT_FAILURE(rc))
1754 { /* small + fallback (vga) */
1755 rc = MMHyperAlloc(pVM, cbRam, 16, MM_TAG_PGM, (void **)&pNew);
1756 if (VBOX_SUCCESS(rc))
1757 GCPtrNew = MMHyperHC2GC(pVM, pNew);
1758 else
1759 AssertMsgFailed(("MMHyperAlloc(,%#x,,,) -> %Vrc\n", cbRam, cb));
1760 }
1761 if (VBOX_SUCCESS(rc))
1762 {
1763 /*
1764 * Initialize the range.
1765 */
1766 pNew->pvHC = pvRam;
1767 pNew->GCPhys = GCPhys;
1768 pNew->GCPhysLast = GCPhysLast;
1769 pNew->cb = cb;
1770 pNew->fFlags = fFlags;
1771 pNew->pavHCChunkHC = NULL;
1772 pNew->pavHCChunkGC = 0;
1773
1774 unsigned iPage = cb >> PAGE_SHIFT;
1775 if (paPages)
1776 {
1777 while (iPage-- > 0)
1778 {
1779 PGM_PAGE_INIT(&pNew->aPages[iPage], paPages[iPage].Phys & X86_PTE_PAE_PG_MASK, NIL_GMM_PAGEID,
1780 fFlags & MM_RAM_FLAGS_MMIO2 ? PGMPAGETYPE_MMIO2 : PGMPAGETYPE_RAM,
1781 PGM_PAGE_STATE_ALLOCATED);
1782 pNew->aPages[iPage].HCPhys |= fFlags; /** @todo PAGE FLAGS*/
1783 }
1784 }
1785 else if (fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
1786 {
1787 /* Allocate memory for chunk to HC ptr lookup array. */
1788 rc = MMHyperAlloc(pVM, (cb >> PGM_DYNAMIC_CHUNK_SHIFT) * sizeof(void *), 16, MM_TAG_PGM, (void **)&pNew->pavHCChunkHC);
1789 AssertMsgReturn(rc == VINF_SUCCESS, ("MMHyperAlloc(,%#x,,,) -> %Vrc\n", cbRam, cb), rc);
1790
1791 pNew->pavHCChunkGC = MMHyperHC2GC(pVM, pNew->pavHCChunkHC);
1792 Assert(pNew->pavHCChunkGC);
1793
1794 /* Physical memory will be allocated on demand. */
1795 while (iPage-- > 0)
1796 {
1797 PGM_PAGE_INIT(&pNew->aPages[iPage], 0, NIL_GMM_PAGEID, PGMPAGETYPE_RAM, PGM_PAGE_STATE_ZERO);
1798 pNew->aPages[iPage].HCPhys = fFlags; /** @todo PAGE FLAGS */
1799 }
1800 }
1801 else
1802 {
1803 Assert(fFlags == (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO));
1804 RTHCPHYS HCPhysDummyPage = MMR3PageDummyHCPhys(pVM);
1805 while (iPage-- > 0)
1806 {
1807 PGM_PAGE_INIT(&pNew->aPages[iPage], HCPhysDummyPage, NIL_GMM_PAGEID, PGMPAGETYPE_MMIO, PGM_PAGE_STATE_ZERO);
1808 pNew->aPages[iPage].HCPhys |= fFlags; /** @todo PAGE FLAGS*/
1809 }
1810 }
1811
1812 /*
1813 * Insert the new RAM range.
1814 */
1815 pgmLock(pVM);
1816 pNew->pNextR3 = pCur;
1817 pNew->pNextR0 = pCur ? MMHyperCCToR0(pVM, pCur) : NIL_RTR0PTR;
1818 pNew->pNextGC = pCur ? MMHyperCCToGC(pVM, pCur) : NIL_RTGCPTR;
1819 if (pPrev)
1820 {
1821 pPrev->pNextR3 = pNew;
1822 pPrev->pNextR0 = MMHyperCCToR0(pVM, pNew);
1823 pPrev->pNextGC = GCPtrNew;
1824 }
1825 else
1826 {
1827 pVM->pgm.s.pRamRangesR3 = pNew;
1828 pVM->pgm.s.pRamRangesR0 = MMHyperCCToR0(pVM, pNew);
1829 pVM->pgm.s.pRamRangesGC = GCPtrNew;
1830 }
1831 pgmUnlock(pVM);
1832 }
1833 return rc;
1834}
1835
1836#ifndef VBOX_WITH_NEW_PHYS_CODE
1837
1838/**
1839 * Register a chunk of a the physical memory range with PGM. MM is responsible
1840 * for the toplevel things - allocation and locking - while PGM is taking
1841 * care of all the details and implements the physical address space virtualization.
1842 *
1843 *
1844 * @returns VBox status.
1845 * @param pVM The VM handle.
1846 * @param pvRam HC virtual address of the RAM range. (page aligned)
1847 * @param GCPhys GC physical address of the RAM range. (page aligned)
1848 * @param cb Size of the RAM range. (page aligned)
1849 * @param fFlags Flags, MM_RAM_*.
1850 * @param paPages Pointer an array of physical page descriptors.
1851 * @param pszDesc Description string.
1852 */
1853PGMR3DECL(int) PGMR3PhysRegisterChunk(PVM pVM, void *pvRam, RTGCPHYS GCPhys, size_t cb, unsigned fFlags, const SUPPAGE *paPages, const char *pszDesc)
1854{
1855 NOREF(pszDesc);
1856
1857 /*
1858 * Validate input.
1859 * (Not so important because callers are only MMR3PhysRegister()
1860 * and PGMR3HandlerPhysicalRegisterEx(), but anyway...)
1861 */
1862 Log(("PGMR3PhysRegisterChunk %08X %x bytes flags %x %s\n", GCPhys, cb, fFlags, pszDesc));
1863
1864 Assert(paPages);
1865 Assert(pvRam);
1866 Assert(!(fFlags & ~0xfff));
1867 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb && cb);
1868 Assert(RT_ALIGN_P(pvRam, PAGE_SIZE) == pvRam);
1869 Assert(!(fFlags & ~(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2 | MM_RAM_FLAGS_DYNAMIC_ALLOC)));
1870 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
1871 Assert(VM_IS_EMT(pVM));
1872 Assert(!(GCPhys & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
1873 Assert(cb == PGM_DYNAMIC_CHUNK_SIZE);
1874
1875 RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
1876 if (GCPhysLast < GCPhys)
1877 {
1878 AssertMsgFailed(("The range wraps! GCPhys=%VGp cb=%#x\n", GCPhys, cb));
1879 return VERR_INVALID_PARAMETER;
1880 }
1881
1882 /*
1883 * Find existing range location.
1884 */
1885 PPGMRAMRANGE pRam = CTXALLSUFF(pVM->pgm.s.pRamRanges);
1886 while (pRam)
1887 {
1888 RTGCPHYS off = GCPhys - pRam->GCPhys;
1889 if ( off < pRam->cb
1890 && (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC))
1891 break;
1892
1893 pRam = CTXALLSUFF(pRam->pNext);
1894 }
1895 AssertReturn(pRam, VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS);
1896
1897 unsigned off = (GCPhys - pRam->GCPhys) >> PAGE_SHIFT;
1898 unsigned iPage = cb >> PAGE_SHIFT;
1899 if (paPages)
1900 {
1901 while (iPage-- > 0)
1902 pRam->aPages[off + iPage].HCPhys = (paPages[iPage].Phys & X86_PTE_PAE_PG_MASK) | fFlags; /** @todo PAGE FLAGS */
1903 }
1904 off >>= (PGM_DYNAMIC_CHUNK_SHIFT - PAGE_SHIFT);
1905 pRam->pavHCChunkHC[off] = pvRam;
1906
1907 /* Notify the recompiler. */
1908 REMR3NotifyPhysRamChunkRegister(pVM, GCPhys, PGM_DYNAMIC_CHUNK_SIZE, (RTHCUINTPTR)pvRam, fFlags);
1909
1910 return VINF_SUCCESS;
1911}
1912
1913
1914/**
1915 * Allocate missing physical pages for an existing guest RAM range.
1916 *
1917 * @returns VBox status.
1918 * @param pVM The VM handle.
1919 * @param GCPhys GC physical address of the RAM range. (page aligned)
1920 */
1921PGMR3DECL(int) PGM3PhysGrowRange(PVM pVM, PCRTGCPHYS pGCPhys)
1922{
1923 RTGCPHYS GCPhys = *pGCPhys;
1924
1925 /*
1926 * Walk range list.
1927 */
1928 pgmLock(pVM);
1929
1930 PPGMRAMRANGE pRam = CTXALLSUFF(pVM->pgm.s.pRamRanges);
1931 while (pRam)
1932 {
1933 RTGCPHYS off = GCPhys - pRam->GCPhys;
1934 if ( off < pRam->cb
1935 && (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC))
1936 {
1937 bool fRangeExists = false;
1938 unsigned off = (GCPhys - pRam->GCPhys) >> PGM_DYNAMIC_CHUNK_SHIFT;
1939
1940 /** @note A request made from another thread may end up in EMT after somebody else has already allocated the range. */
1941 if (pRam->pavHCChunkHC[off])
1942 fRangeExists = true;
1943
1944 pgmUnlock(pVM);
1945 if (fRangeExists)
1946 return VINF_SUCCESS;
1947 return pgmr3PhysGrowRange(pVM, GCPhys);
1948 }
1949
1950 pRam = CTXALLSUFF(pRam->pNext);
1951 }
1952 pgmUnlock(pVM);
1953 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
1954}
1955
1956
1957/**
1958 * Allocate missing physical pages for an existing guest RAM range.
1959 *
1960 * @returns VBox status.
1961 * @param pVM The VM handle.
1962 * @param pRamRange RAM range
1963 * @param GCPhys GC physical address of the RAM range. (page aligned)
1964 */
1965int pgmr3PhysGrowRange(PVM pVM, RTGCPHYS GCPhys)
1966{
1967 void *pvRam;
1968 int rc;
1969
1970 /* We must execute this function in the EMT thread, otherwise we'll run into problems. */
1971 if (!VM_IS_EMT(pVM))
1972 {
1973 PVMREQ pReq;
1974 const RTGCPHYS GCPhysParam = GCPhys;
1975
1976 AssertMsg(!PDMCritSectIsOwner(&pVM->pgm.s.CritSect), ("We own the PGM lock -> deadlock danger!!\n"));
1977
1978 rc = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)PGM3PhysGrowRange, 2, pVM, &GCPhysParam);
1979 if (VBOX_SUCCESS(rc))
1980 {
1981 rc = pReq->iStatus;
1982 VMR3ReqFree(pReq);
1983 }
1984 return rc;
1985 }
1986
1987 /* Round down to chunk boundary */
1988 GCPhys = GCPhys & PGM_DYNAMIC_CHUNK_BASE_MASK;
1989
1990 STAM_COUNTER_INC(&pVM->pgm.s.StatDynRamGrow);
1991 STAM_COUNTER_ADD(&pVM->pgm.s.StatDynRamTotal, PGM_DYNAMIC_CHUNK_SIZE/(1024*1024));
1992
1993 Log(("pgmr3PhysGrowRange: allocate chunk of size 0x%X at %VGp\n", PGM_DYNAMIC_CHUNK_SIZE, GCPhys));
1994
1995 unsigned cPages = PGM_DYNAMIC_CHUNK_SIZE >> PAGE_SHIFT;
1996
1997 for (;;)
1998 {
1999 rc = SUPPageAlloc(cPages, &pvRam);
2000 if (VBOX_SUCCESS(rc))
2001 {
2002
2003 rc = MMR3PhysRegisterEx(pVM, pvRam, GCPhys, PGM_DYNAMIC_CHUNK_SIZE, 0, MM_PHYS_TYPE_DYNALLOC_CHUNK, "Main Memory");
2004 if (VBOX_SUCCESS(rc))
2005 return rc;
2006
2007 SUPPageFree(pvRam, cPages);
2008 }
2009
2010 VMSTATE enmVMState = VMR3GetState(pVM);
2011 if (enmVMState != VMSTATE_RUNNING)
2012 {
2013 AssertMsgFailed(("Out of memory while trying to allocate a guest RAM chunk at %VGp!\n", GCPhys));
2014 LogRel(("PGM: Out of memory while trying to allocate a guest RAM chunk at %VGp (VMstate=%s)!\n", GCPhys, VMR3GetStateName(enmVMState)));
2015 return rc;
2016 }
2017
2018 LogRel(("pgmr3PhysGrowRange: out of memory. pause until the user resumes execution.\n"));
2019
2020 /* Pause first, then inform Main. */
2021 rc = VMR3SuspendNoSave(pVM);
2022 AssertRC(rc);
2023
2024 VMSetRuntimeError(pVM, false, "HostMemoryLow", "Unable to allocate and lock memory. The virtual machine will be paused. Please close applications to free up memory or close the VM.");
2025
2026 /* Wait for resume event; will only return in that case. If the VM is stopped, the EMT thread will be destroyed. */
2027 rc = VMR3WaitForResume(pVM);
2028
2029 /* Retry */
2030 LogRel(("pgmr3PhysGrowRange: VM execution resumed -> retry.\n"));
2031 }
2032}
2033
2034#endif /* !VBOX_WITH_NEW_PHYS_CODE */
2035
2036
2037/**
2038 * Interface MMR3RomRegister() and MMR3PhysReserve calls to update the
2039 * flags of existing RAM ranges.
2040 *
2041 * @returns VBox status.
2042 * @param pVM The VM handle.
2043 * @param GCPhys GC physical address of the RAM range. (page aligned)
2044 * @param cb Size of the RAM range. (page aligned)
2045 * @param fFlags The Or flags, MM_RAM_* \#defines.
2046 * @param fMask The and mask for the flags.
2047 */
2048PGMR3DECL(int) PGMR3PhysSetFlags(PVM pVM, RTGCPHYS GCPhys, size_t cb, unsigned fFlags, unsigned fMask)
2049{
2050 Log(("PGMR3PhysSetFlags %08X %x %x %x\n", GCPhys, cb, fFlags, fMask));
2051
2052 /*
2053 * Validate input.
2054 * (Not so important because caller is always MMR3RomRegister() and MMR3PhysReserve(), but anyway...)
2055 */
2056 Assert(!(fFlags & ~(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)));
2057 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb && cb);
2058 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
2059 RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
2060 AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
2061
2062 /*
2063 * Lookup the range.
2064 */
2065 PPGMRAMRANGE pRam = CTXALLSUFF(pVM->pgm.s.pRamRanges);
2066 while (pRam && GCPhys > pRam->GCPhysLast)
2067 pRam = CTXALLSUFF(pRam->pNext);
2068 if ( !pRam
2069 || GCPhys > pRam->GCPhysLast
2070 || GCPhysLast < pRam->GCPhys)
2071 {
2072 AssertMsgFailed(("No RAM range for %VGp-%VGp\n", GCPhys, GCPhysLast));
2073 return VERR_INVALID_PARAMETER;
2074 }
2075
2076 /*
2077 * Update the requested flags.
2078 */
2079 RTHCPHYS fFullMask = ~(RTHCPHYS)(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)
2080 | fMask;
2081 unsigned iPageEnd = (GCPhysLast - pRam->GCPhys + 1) >> PAGE_SHIFT;
2082 unsigned iPage = (GCPhys - pRam->GCPhys) >> PAGE_SHIFT;
2083 for ( ; iPage < iPageEnd; iPage++)
2084 pRam->aPages[iPage].HCPhys = (pRam->aPages[iPage].HCPhys & fFullMask) | fFlags; /** @todo PAGE FLAGS */
2085
2086 return VINF_SUCCESS;
2087}
2088
2089
2090/**
2091 * Sets the Address Gate 20 state.
2092 *
2093 * @param pVM VM handle.
2094 * @param fEnable True if the gate should be enabled.
2095 * False if the gate should be disabled.
2096 */
2097PGMDECL(void) PGMR3PhysSetA20(PVM pVM, bool fEnable)
2098{
2099 LogFlow(("PGMR3PhysSetA20 %d (was %d)\n", fEnable, pVM->pgm.s.fA20Enabled));
2100 if (pVM->pgm.s.fA20Enabled != (RTUINT)fEnable)
2101 {
2102 pVM->pgm.s.fA20Enabled = fEnable;
2103 pVM->pgm.s.GCPhysA20Mask = ~(RTGCPHYS)(!fEnable << 20);
2104 REMR3A20Set(pVM, fEnable);
2105 /** @todo we're not handling this correctly for VT-x / AMD-V. See #2911 */
2106 }
2107}
2108
2109
2110/**
2111 * Tree enumeration callback for dealing with age rollover.
2112 * It will perform a simple compression of the current age.
2113 */
2114static DECLCALLBACK(int) pgmR3PhysChunkAgeingRolloverCallback(PAVLU32NODECORE pNode, void *pvUser)
2115{
2116 /* Age compression - ASSUMES iNow == 4. */
2117 PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)pNode;
2118 if (pChunk->iAge >= UINT32_C(0xffffff00))
2119 pChunk->iAge = 3;
2120 else if (pChunk->iAge >= UINT32_C(0xfffff000))
2121 pChunk->iAge = 2;
2122 else if (pChunk->iAge)
2123 pChunk->iAge = 1;
2124 else /* iAge = 0 */
2125 pChunk->iAge = 4;
2126
2127 /* reinsert */
2128 PVM pVM = (PVM)pvUser;
2129 RTAvllU32Remove(&pVM->pgm.s.ChunkR3Map.pAgeTree, pChunk->AgeCore.Key);
2130 pChunk->AgeCore.Key = pChunk->iAge;
2131 RTAvllU32Insert(&pVM->pgm.s.ChunkR3Map.pAgeTree, &pChunk->AgeCore);
2132 return 0;
2133}
2134
2135
2136/**
2137 * Tree enumeration callback that updates the chunks that have
2138 * been used since the last
2139 */
2140static DECLCALLBACK(int) pgmR3PhysChunkAgeingCallback(PAVLU32NODECORE pNode, void *pvUser)
2141{
2142 PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)pNode;
2143 if (!pChunk->iAge)
2144 {
2145 PVM pVM = (PVM)pvUser;
2146 RTAvllU32Remove(&pVM->pgm.s.ChunkR3Map.pAgeTree, pChunk->AgeCore.Key);
2147 pChunk->AgeCore.Key = pChunk->iAge = pVM->pgm.s.ChunkR3Map.iNow;
2148 RTAvllU32Insert(&pVM->pgm.s.ChunkR3Map.pAgeTree, &pChunk->AgeCore);
2149 }
2150
2151 return 0;
2152}
2153
2154
2155/**
2156 * Performs ageing of the ring-3 chunk mappings.
2157 *
2158 * @param pVM The VM handle.
2159 */
2160PGMR3DECL(void) PGMR3PhysChunkAgeing(PVM pVM)
2161{
2162 pVM->pgm.s.ChunkR3Map.AgeingCountdown = RT_MIN(pVM->pgm.s.ChunkR3Map.cMax / 4, 1024);
2163 pVM->pgm.s.ChunkR3Map.iNow++;
2164 if (pVM->pgm.s.ChunkR3Map.iNow == 0)
2165 {
2166 pVM->pgm.s.ChunkR3Map.iNow = 4;
2167 RTAvlU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pTree, true /*fFromLeft*/, pgmR3PhysChunkAgeingRolloverCallback, pVM);
2168 }
2169 else
2170 RTAvlU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pTree, true /*fFromLeft*/, pgmR3PhysChunkAgeingCallback, pVM);
2171}
2172
2173
2174/**
2175 * The structure passed in the pvUser argument of pgmR3PhysChunkUnmapCandidateCallback().
2176 */
2177typedef struct PGMR3PHYSCHUNKUNMAPCB
2178{
2179 PVM pVM; /**< The VM handle. */
2180 PPGMCHUNKR3MAP pChunk; /**< The chunk to unmap. */
2181} PGMR3PHYSCHUNKUNMAPCB, *PPGMR3PHYSCHUNKUNMAPCB;
2182
2183
2184/**
2185 * Callback used to find the mapping that's been unused for
2186 * the longest time.
2187 */
2188static DECLCALLBACK(int) pgmR3PhysChunkUnmapCandidateCallback(PAVLLU32NODECORE pNode, void *pvUser)
2189{
2190 do
2191 {
2192 PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)((uint8_t *)pNode - RT_OFFSETOF(PGMCHUNKR3MAP, AgeCore));
2193 if ( pChunk->iAge
2194 && !pChunk->cRefs)
2195 {
2196 /*
2197 * Check that it's not in any of the TLBs.
2198 */
2199 PVM pVM = ((PPGMR3PHYSCHUNKUNMAPCB)pvUser)->pVM;
2200 for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.ChunkR3Map.Tlb.aEntries); i++)
2201 if (pVM->pgm.s.ChunkR3Map.Tlb.aEntries[i].pChunk == pChunk)
2202 {
2203 pChunk = NULL;
2204 break;
2205 }
2206 if (pChunk)
2207 for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.PhysTlbHC.aEntries); i++)
2208 if (pVM->pgm.s.PhysTlbHC.aEntries[i].pMap == pChunk)
2209 {
2210 pChunk = NULL;
2211 break;
2212 }
2213 if (pChunk)
2214 {
2215 ((PPGMR3PHYSCHUNKUNMAPCB)pvUser)->pChunk = pChunk;
2216 return 1; /* done */
2217 }
2218 }
2219
2220 /* next with the same age - this version of the AVL API doesn't enumerate the list, so we have to do it. */
2221 pNode = pNode->pList;
2222 } while (pNode);
2223 return 0;
2224}
2225
2226
2227/**
2228 * Finds a good candidate for unmapping when the ring-3 mapping cache is full.
2229 *
2230 * The candidate will not be part of any TLBs, so no need to flush
2231 * anything afterwards.
2232 *
2233 * @returns Chunk id.
2234 * @param pVM The VM handle.
2235 */
2236static int32_t pgmR3PhysChunkFindUnmapCandidate(PVM pVM)
2237{
2238 /*
2239 * Do tree ageing first?
2240 */
2241 if (pVM->pgm.s.ChunkR3Map.AgeingCountdown-- == 0)
2242 PGMR3PhysChunkAgeing(pVM);
2243
2244 /*
2245 * Enumerate the age tree starting with the left most node.
2246 */
2247 PGMR3PHYSCHUNKUNMAPCB Args;
2248 Args.pVM = pVM;
2249 Args.pChunk = NULL;
2250 if (RTAvllU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pAgeTree, true /*fFromLeft*/, pgmR3PhysChunkUnmapCandidateCallback, pVM))
2251 return Args.pChunk->Core.Key;
2252 return INT32_MAX;
2253}
2254
2255
2256/**
2257 * Maps the given chunk into the ring-3 mapping cache.
2258 *
2259 * This will call ring-0.
2260 *
2261 * @returns VBox status code.
2262 * @param pVM The VM handle.
2263 * @param idChunk The chunk in question.
2264 * @param ppChunk Where to store the chunk tracking structure.
2265 *
2266 * @remarks Called from within the PGM critical section.
2267 */
2268int pgmR3PhysChunkMap(PVM pVM, uint32_t idChunk, PPPGMCHUNKR3MAP ppChunk)
2269{
2270 int rc;
2271 /*
2272 * Allocate a new tracking structure first.
2273 */
2274#if 0 /* for later when we've got a separate mapping method for ring-0. */
2275 PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)MMR3HeapAlloc(pVM, MM_TAG_PGM_CHUNK_MAPPING, sizeof(*pChunk));
2276 AssertReturn(pChunk, VERR_NO_MEMORY);
2277#else
2278 PPGMCHUNKR3MAP pChunk;
2279 rc = MMHyperAlloc(pVM, sizeof(*pChunk), 0, MM_TAG_PGM_CHUNK_MAPPING, (void **)&pChunk);
2280 AssertRCReturn(rc, rc);
2281#endif
2282 pChunk->Core.Key = idChunk;
2283 pChunk->AgeCore.Key = pVM->pgm.s.ChunkR3Map.iNow;
2284 pChunk->iAge = 0;
2285 pChunk->cRefs = 0;
2286 pChunk->cPermRefs = 0;
2287 pChunk->pv = NULL;
2288
2289 /*
2290 * Request the ring-0 part to map the chunk in question and if
2291 * necessary unmap another one to make space in the mapping cache.
2292 */
2293 GMMMAPUNMAPCHUNKREQ Req;
2294 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
2295 Req.Hdr.cbReq = sizeof(Req);
2296 Req.pvR3 = NULL;
2297 Req.idChunkMap = idChunk;
2298 Req.idChunkUnmap = INT32_MAX;
2299 if (pVM->pgm.s.ChunkR3Map.c >= pVM->pgm.s.ChunkR3Map.cMax)
2300 Req.idChunkUnmap = pgmR3PhysChunkFindUnmapCandidate(pVM);
2301 rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_MAP_UNMAP_CHUNK, 0, &Req.Hdr);
2302 if (VBOX_SUCCESS(rc))
2303 {
2304 /*
2305 * Update the tree.
2306 */
2307 /* insert the new one. */
2308 AssertPtr(Req.pvR3);
2309 pChunk->pv = Req.pvR3;
2310 bool fRc = RTAvlU32Insert(&pVM->pgm.s.ChunkR3Map.pTree, &pChunk->Core);
2311 AssertRelease(fRc);
2312 pVM->pgm.s.ChunkR3Map.c++;
2313
2314 fRc = RTAvllU32Insert(&pVM->pgm.s.ChunkR3Map.pAgeTree, &pChunk->AgeCore);
2315 AssertRelease(fRc);
2316
2317 /* remove the unmapped one. */
2318 if (Req.idChunkUnmap != INT32_MAX)
2319 {
2320 PPGMCHUNKR3MAP pUnmappedChunk = (PPGMCHUNKR3MAP)RTAvlU32Remove(&pVM->pgm.s.ChunkR3Map.pTree, Req.idChunkUnmap);
2321 AssertRelease(pUnmappedChunk);
2322 pUnmappedChunk->pv = NULL;
2323 pUnmappedChunk->Core.Key = UINT32_MAX;
2324#if 0 /* for later when we've got a separate mapping method for ring-0. */
2325 MMR3HeapFree(pUnmappedChunk);
2326#else
2327 MMHyperFree(pVM, pUnmappedChunk);
2328#endif
2329 pVM->pgm.s.ChunkR3Map.c--;
2330 }
2331 }
2332 else
2333 {
2334 AssertRC(rc);
2335#if 0 /* for later when we've got a separate mapping method for ring-0. */
2336 MMR3HeapFree(pChunk);
2337#else
2338 MMHyperFree(pVM, pChunk);
2339#endif
2340 pChunk = NULL;
2341 }
2342
2343 *ppChunk = pChunk;
2344 return rc;
2345}
2346
2347
2348/**
2349 * For VMMCALLHOST_PGM_MAP_CHUNK, considered internal.
2350 *
2351 * @returns see pgmR3PhysChunkMap.
2352 * @param pVM The VM handle.
2353 * @param idChunk The chunk to map.
2354 */
2355PDMR3DECL(int) PGMR3PhysChunkMap(PVM pVM, uint32_t idChunk)
2356{
2357 PPGMCHUNKR3MAP pChunk;
2358 return pgmR3PhysChunkMap(pVM, idChunk, &pChunk);
2359}
2360
2361
2362/**
2363 * Invalidates the TLB for the ring-3 mapping cache.
2364 *
2365 * @param pVM The VM handle.
2366 */
2367PGMR3DECL(void) PGMR3PhysChunkInvalidateTLB(PVM pVM)
2368{
2369 pgmLock(pVM);
2370 for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.ChunkR3Map.Tlb.aEntries); i++)
2371 {
2372 pVM->pgm.s.ChunkR3Map.Tlb.aEntries[i].idChunk = NIL_GMM_CHUNKID;
2373 pVM->pgm.s.ChunkR3Map.Tlb.aEntries[i].pChunk = NULL;
2374 }
2375 pgmUnlock(pVM);
2376}
2377
2378
2379/**
2380 * Response to VM_FF_PGM_NEED_HANDY_PAGES and VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES.
2381 *
2382 * @returns The following VBox status codes.
2383 * @retval VINF_SUCCESS on success. FF cleared.
2384 * @retval VINF_EM_NO_MEMORY if we're out of memory. The FF is not cleared in this case.
2385 *
2386 * @param pVM The VM handle.
2387 */
2388PDMR3DECL(int) PGMR3PhysAllocateHandyPages(PVM pVM)
2389{
2390 pgmLock(pVM);
2391 int rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES, 0, NULL);
2392 if (rc == VERR_GMM_SEED_ME)
2393 {
2394 void *pvChunk;
2395 rc = SUPPageAlloc(GMM_CHUNK_SIZE >> PAGE_SHIFT, &pvChunk);
2396 if (VBOX_SUCCESS(rc))
2397 rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_SEED_CHUNK, (uintptr_t)pvChunk, NULL);
2398 if (VBOX_FAILURE(rc))
2399 {
2400 LogRel(("PGM: GMM Seeding failed, rc=%Vrc\n", rc));
2401 rc = VINF_EM_NO_MEMORY;
2402 }
2403 }
2404 pgmUnlock(pVM);
2405 Assert(rc == VINF_SUCCESS || rc == VINF_EM_NO_MEMORY);
2406 return rc;
2407}
2408
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