VirtualBox

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

Last change on this file since 14806 was 14592, checked in by vboxsync, 16 years ago

PGMR3PhysRegister: Use MMR3HyperAllocOnceNoRel instead of muching about with SUPPageAlloc and doing it own its own. Made MMR3HyperAllocOnceNoRel install a fence page after each mapping.

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