VirtualBox

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

Last change on this file since 6537 was 6534, checked in by vboxsync, 17 years ago

Quick hack for out of hma address space when registering the VRAM (TODO: fix properly).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 29.7 KB
Line 
1/* $Id: PGMPhys.cpp 6534 2008-01-28 18:44:36Z vboxsync $ */
2/** @file
3 * PGM - Page Manager and Monitor, Physical Memory Addressing.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_PGM
23#include <VBox/pgm.h>
24#include <VBox/cpum.h>
25#include <VBox/iom.h>
26#include <VBox/sup.h>
27#include <VBox/mm.h>
28#include <VBox/stam.h>
29#include <VBox/rem.h>
30#include <VBox/csam.h>
31#include "PGMInternal.h"
32#include <VBox/vm.h>
33#include <VBox/dbg.h>
34#include <VBox/param.h>
35#include <VBox/err.h>
36#include <iprt/assert.h>
37#include <iprt/alloc.h>
38#include <iprt/asm.h>
39#include <VBox/log.h>
40#include <iprt/thread.h>
41#include <iprt/string.h>
42
43
44
45/*
46 * PGMR3PhysReadByte/Word/Dword
47 * PGMR3PhysWriteByte/Word/Dword
48 */
49
50#define PGMPHYSFN_READNAME PGMR3PhysReadByte
51#define PGMPHYSFN_WRITENAME PGMR3PhysWriteByte
52#define PGMPHYS_DATASIZE 1
53#define PGMPHYS_DATATYPE uint8_t
54#include "PGMPhys.h"
55
56#define PGMPHYSFN_READNAME PGMR3PhysReadWord
57#define PGMPHYSFN_WRITENAME PGMR3PhysWriteWord
58#define PGMPHYS_DATASIZE 2
59#define PGMPHYS_DATATYPE uint16_t
60#include "PGMPhys.h"
61
62#define PGMPHYSFN_READNAME PGMR3PhysReadDword
63#define PGMPHYSFN_WRITENAME PGMR3PhysWriteDword
64#define PGMPHYS_DATASIZE 4
65#define PGMPHYS_DATATYPE uint32_t
66#include "PGMPhys.h"
67
68
69
70
71/**
72 * Interface that the MMR3RamRegister(), MMR3RomRegister() and MMIO handler
73 * registration APIs calls to inform PGM about memory registrations.
74 *
75 * It registers the physical memory range with PGM. MM is responsible
76 * for the toplevel things - allocation and locking - while PGM is taking
77 * care of all the details and implements the physical address space virtualization.
78 *
79 * @returns VBox status.
80 * @param pVM The VM handle.
81 * @param pvRam HC virtual address of the RAM range. (page aligned)
82 * @param GCPhys GC physical address of the RAM range. (page aligned)
83 * @param cb Size of the RAM range. (page aligned)
84 * @param fFlags Flags, MM_RAM_*.
85 * @param paPages Pointer an array of physical page descriptors.
86 * @param pszDesc Description string.
87 */
88PGMR3DECL(int) PGMR3PhysRegister(PVM pVM, void *pvRam, RTGCPHYS GCPhys, size_t cb, unsigned fFlags, const SUPPAGE *paPages, const char *pszDesc)
89{
90 /*
91 * Validate input.
92 * (Not so important because callers are only MMR3PhysRegister()
93 * and PGMR3HandlerPhysicalRegisterEx(), but anyway...)
94 */
95 Log(("PGMR3PhysRegister %08X %x bytes flags %x %s\n", GCPhys, cb, fFlags, pszDesc));
96
97 Assert((fFlags & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_DYNAMIC_ALLOC)) || paPages);
98 /*Assert(!(fFlags & MM_RAM_FLAGS_RESERVED) || !paPages);*/
99 Assert((fFlags == (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO)) || (fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC) || pvRam);
100 /*Assert(!(fFlags & MM_RAM_FLAGS_RESERVED) || !pvRam);*/
101 Assert(!(fFlags & ~0xfff));
102 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb && cb);
103 Assert(RT_ALIGN_P(pvRam, PAGE_SIZE) == pvRam);
104 Assert(!(fFlags & ~(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2 | MM_RAM_FLAGS_DYNAMIC_ALLOC)));
105 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
106 RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
107 if (GCPhysLast < GCPhys)
108 {
109 AssertMsgFailed(("The range wraps! GCPhys=%VGp cb=%#x\n", GCPhys, cb));
110 return VERR_INVALID_PARAMETER;
111 }
112
113 /*
114 * Find range location and check for conflicts.
115 */
116 PPGMRAMRANGE pPrev = NULL;
117 PPGMRAMRANGE pCur = pVM->pgm.s.pRamRangesHC;
118 while (pCur)
119 {
120 if (GCPhys <= pCur->GCPhysLast && GCPhysLast >= pCur->GCPhys)
121 {
122 AssertMsgFailed(("Conflict! This cannot happen!\n"));
123 return VERR_PGM_RAM_CONFLICT;
124 }
125 if (GCPhysLast < pCur->GCPhys)
126 break;
127
128 /* next */
129 pPrev = pCur;
130 pCur = pCur->pNextHC;
131 }
132
133 /*
134 * Allocate RAM range.
135 * Small ranges are allocated from the heap, big ones have separate mappings.
136 */
137 size_t cbRam = RT_OFFSETOF(PGMRAMRANGE, aPages[cb >> PAGE_SHIFT]);
138 PPGMRAMRANGE pNew;
139 RTGCPTR GCPtrNew;
140 int rc = VERR_NO_MEMORY;
141 if (cbRam > PAGE_SIZE / 2)
142 { /* large */
143 cbRam = RT_ALIGN_Z(cbRam, PAGE_SIZE);
144 rc = SUPPageAlloc(cbRam >> PAGE_SHIFT, (void **)&pNew);
145 if (VBOX_SUCCESS(rc))
146 {
147 rc = MMR3HyperMapHCRam(pVM, pNew, cbRam, true, pszDesc, &GCPtrNew);
148 if (VBOX_SUCCESS(rc))
149 {
150 Assert(MMHyperHC2GC(pVM, pNew) == GCPtrNew);
151 rc = MMR3HyperReserve(pVM, PAGE_SIZE, "fence", NULL);
152 }
153 else
154 {
155 AssertMsgFailed(("MMR3HyperMapHCRam(,,%#x,,,) -> %Vrc\n", cbRam, rc));
156 SUPPageFree(pNew, cbRam >> PAGE_SHIFT);
157 }
158 }
159 else
160 AssertMsgFailed(("SUPPageAlloc(%#x,,) -> %Vrc\n", cbRam >> PAGE_SHIFT, rc));
161
162 }
163 if (RT_FAILURE(rc))
164 { /* small + fallback (vga) */
165 rc = MMHyperAlloc(pVM, cbRam, 16, MM_TAG_PGM, (void **)&pNew);
166 if (VBOX_SUCCESS(rc))
167 GCPtrNew = MMHyperHC2GC(pVM, pNew);
168 else
169 AssertMsgFailed(("MMHyperAlloc(,%#x,,,) -> %Vrc\n", cbRam, cb));
170 }
171 if (VBOX_SUCCESS(rc))
172 {
173 /*
174 * Initialize the range.
175 */
176 pNew->pvHC = pvRam;
177 pNew->GCPhys = GCPhys;
178 pNew->GCPhysLast = GCPhysLast;
179 pNew->cb = cb;
180 pNew->fFlags = fFlags;
181 pNew->pavHCChunkHC = NULL;
182 pNew->pavHCChunkGC = 0;
183
184 unsigned iPage = cb >> PAGE_SHIFT;
185 if (paPages)
186 {
187 while (iPage-- > 0)
188 {
189 pNew->aPages[iPage].HCPhys = (paPages[iPage].Phys & X86_PTE_PAE_PG_MASK) | fFlags; /** @todo PAGE FLAGS */
190 pNew->aPages[iPage].u2State = PGM_PAGE_STATE_ALLOCATED;
191 pNew->aPages[iPage].fWrittenTo = 0;
192 pNew->aPages[iPage].fSomethingElse = 0;
193 pNew->aPages[iPage].idPage = 0;
194 pNew->aPages[iPage].u32B = 0;
195 }
196 }
197 else if (fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
198 {
199 /* Allocate memory for chunk to HC ptr lookup array. */
200 rc = MMHyperAlloc(pVM, (cb >> PGM_DYNAMIC_CHUNK_SHIFT) * sizeof(void *), 16, MM_TAG_PGM, (void **)&pNew->pavHCChunkHC);
201 AssertMsgReturn(rc == VINF_SUCCESS, ("MMHyperAlloc(,%#x,,,) -> %Vrc\n", cbRam, cb), rc);
202
203 pNew->pavHCChunkGC = MMHyperHC2GC(pVM, pNew->pavHCChunkHC);
204 Assert(pNew->pavHCChunkGC);
205
206 /* Physical memory will be allocated on demand. */
207 while (iPage-- > 0)
208 {
209 pNew->aPages[iPage].HCPhys = fFlags; /** @todo PAGE FLAGS */
210 pNew->aPages[iPage].u2State = PGM_PAGE_STATE_ZERO;
211 pNew->aPages[iPage].fWrittenTo = 0;
212 pNew->aPages[iPage].fSomethingElse = 0;
213 pNew->aPages[iPage].idPage = 0;
214 pNew->aPages[iPage].u32B = 0;
215 }
216 }
217 else
218 {
219 Assert(fFlags == (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO));
220 RTHCPHYS HCPhysDummyPage = (MMR3PageDummyHCPhys(pVM) & X86_PTE_PAE_PG_MASK) | fFlags; /** @todo PAGE FLAGS */
221 while (iPage-- > 0)
222 {
223 pNew->aPages[iPage].HCPhys = HCPhysDummyPage; /** @todo PAGE FLAGS */
224 pNew->aPages[iPage].u2State = PGM_PAGE_STATE_ZERO;
225 pNew->aPages[iPage].fWrittenTo = 0;
226 pNew->aPages[iPage].fSomethingElse = 0;
227 pNew->aPages[iPage].idPage = 0;
228 pNew->aPages[iPage].u32B = 0;
229 }
230 }
231
232 /*
233 * Insert the new RAM range.
234 */
235 pgmLock(pVM);
236 pNew->pNextHC = pCur;
237 pNew->pNextGC = pCur ? MMHyperHC2GC(pVM, pCur) : 0;
238 if (pPrev)
239 {
240 pPrev->pNextHC = pNew;
241 pPrev->pNextGC = GCPtrNew;
242 }
243 else
244 {
245 pVM->pgm.s.pRamRangesHC = pNew;
246 pVM->pgm.s.pRamRangesGC = GCPtrNew;
247 }
248 pgmUnlock(pVM);
249 }
250 return rc;
251}
252
253
254/**
255 * Register a chunk of a the physical memory range with PGM. MM is responsible
256 * for the toplevel things - allocation and locking - while PGM is taking
257 * care of all the details and implements the physical address space virtualization.
258 *
259 *
260 * @returns VBox status.
261 * @param pVM The VM handle.
262 * @param pvRam HC virtual address of the RAM range. (page aligned)
263 * @param GCPhys GC physical address of the RAM range. (page aligned)
264 * @param cb Size of the RAM range. (page aligned)
265 * @param fFlags Flags, MM_RAM_*.
266 * @param paPages Pointer an array of physical page descriptors.
267 * @param pszDesc Description string.
268 */
269PGMR3DECL(int) PGMR3PhysRegisterChunk(PVM pVM, void *pvRam, RTGCPHYS GCPhys, size_t cb, unsigned fFlags, const SUPPAGE *paPages, const char *pszDesc)
270{
271 NOREF(pszDesc);
272
273 /*
274 * Validate input.
275 * (Not so important because callers are only MMR3PhysRegister()
276 * and PGMR3HandlerPhysicalRegisterEx(), but anyway...)
277 */
278 Log(("PGMR3PhysRegisterChunk %08X %x bytes flags %x %s\n", GCPhys, cb, fFlags, pszDesc));
279
280 Assert(paPages);
281 Assert(pvRam);
282 Assert(!(fFlags & ~0xfff));
283 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb && cb);
284 Assert(RT_ALIGN_P(pvRam, PAGE_SIZE) == pvRam);
285 Assert(!(fFlags & ~(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2 | MM_RAM_FLAGS_DYNAMIC_ALLOC)));
286 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
287 Assert(VM_IS_EMT(pVM));
288 Assert(!(GCPhys & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
289 Assert(cb == PGM_DYNAMIC_CHUNK_SIZE);
290
291 RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
292 if (GCPhysLast < GCPhys)
293 {
294 AssertMsgFailed(("The range wraps! GCPhys=%VGp cb=%#x\n", GCPhys, cb));
295 return VERR_INVALID_PARAMETER;
296 }
297
298 /*
299 * Find existing range location.
300 */
301 PPGMRAMRANGE pRam = CTXSUFF(pVM->pgm.s.pRamRanges);
302 while (pRam)
303 {
304 RTGCPHYS off = GCPhys - pRam->GCPhys;
305 if ( off < pRam->cb
306 && (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC))
307 break;
308
309 pRam = CTXSUFF(pRam->pNext);
310 }
311 AssertReturn(pRam, VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS);
312
313 unsigned off = (GCPhys - pRam->GCPhys) >> PAGE_SHIFT;
314 unsigned iPage = cb >> PAGE_SHIFT;
315 if (paPages)
316 {
317 while (iPage-- > 0)
318 pRam->aPages[off + iPage].HCPhys = (paPages[iPage].Phys & X86_PTE_PAE_PG_MASK) | fFlags; /** @todo PAGE FLAGS */
319 }
320 off >>= (PGM_DYNAMIC_CHUNK_SHIFT - PAGE_SHIFT);
321 pRam->pavHCChunkHC[off] = pvRam;
322
323 /* Notify the recompiler. */
324 REMR3NotifyPhysRamChunkRegister(pVM, GCPhys, PGM_DYNAMIC_CHUNK_SIZE, (RTHCUINTPTR)pvRam, fFlags);
325
326 return VINF_SUCCESS;
327}
328
329
330/**
331 * Allocate missing physical pages for an existing guest RAM range.
332 *
333 * @returns VBox status.
334 * @param pVM The VM handle.
335 * @param GCPhys GC physical address of the RAM range. (page aligned)
336 */
337PGMR3DECL(int) PGM3PhysGrowRange(PVM pVM, RTGCPHYS GCPhys)
338{
339 /*
340 * Walk range list.
341 */
342 pgmLock(pVM);
343
344 PPGMRAMRANGE pRam = CTXSUFF(pVM->pgm.s.pRamRanges);
345 while (pRam)
346 {
347 RTGCPHYS off = GCPhys - pRam->GCPhys;
348 if ( off < pRam->cb
349 && (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC))
350 {
351 bool fRangeExists = false;
352 unsigned off = (GCPhys - pRam->GCPhys) >> PGM_DYNAMIC_CHUNK_SHIFT;
353
354 /** @note A request made from another thread may end up in EMT after somebody else has already allocated the range. */
355 if (pRam->pavHCChunkHC[off])
356 fRangeExists = true;
357
358 pgmUnlock(pVM);
359 if (fRangeExists)
360 return VINF_SUCCESS;
361 return pgmr3PhysGrowRange(pVM, GCPhys);
362 }
363
364 pRam = CTXSUFF(pRam->pNext);
365 }
366 pgmUnlock(pVM);
367 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
368}
369
370#ifndef VBOX_WITH_NEW_PHYS_CODE
371
372/**
373 * Allocate missing physical pages for an existing guest RAM range.
374 *
375 * @returns VBox status.
376 * @param pVM The VM handle.
377 * @param pRamRange RAM range
378 * @param GCPhys GC physical address of the RAM range. (page aligned)
379 */
380int pgmr3PhysGrowRange(PVM pVM, RTGCPHYS GCPhys)
381{
382 void *pvRam;
383 int rc;
384
385 /* We must execute this function in the EMT thread, otherwise we'll run into problems. */
386 if (!VM_IS_EMT(pVM))
387 {
388 PVMREQ pReq;
389
390 AssertMsg(!PDMCritSectIsOwner(&pVM->pgm.s.CritSect), ("We own the PGM lock -> deadlock danger!!\n"));
391
392 rc = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)PGM3PhysGrowRange, 2, pVM, GCPhys);
393 if (VBOX_SUCCESS(rc))
394 {
395 rc = pReq->iStatus;
396 VMR3ReqFree(pReq);
397 }
398 return rc;
399 }
400
401 /* Round down to chunk boundary */
402 GCPhys = GCPhys & PGM_DYNAMIC_CHUNK_BASE_MASK;
403
404 STAM_COUNTER_INC(&pVM->pgm.s.StatDynRamGrow);
405 STAM_COUNTER_ADD(&pVM->pgm.s.StatDynRamTotal, PGM_DYNAMIC_CHUNK_SIZE/(1024*1024));
406
407 Log(("pgmr3PhysGrowRange: allocate chunk of size 0x%X at %VGp\n", PGM_DYNAMIC_CHUNK_SIZE, GCPhys));
408
409 unsigned cPages = PGM_DYNAMIC_CHUNK_SIZE >> PAGE_SHIFT;
410
411 for (;;)
412 {
413 rc = SUPPageAlloc(cPages, &pvRam);
414 if (VBOX_SUCCESS(rc))
415 {
416
417 rc = MMR3PhysRegisterEx(pVM, pvRam, GCPhys, PGM_DYNAMIC_CHUNK_SIZE, 0, MM_PHYS_TYPE_DYNALLOC_CHUNK, "Main Memory");
418 if (VBOX_SUCCESS(rc))
419 return rc;
420
421 SUPPageFree(pvRam, cPages);
422 }
423
424 VMSTATE enmVMState = VMR3GetState(pVM);
425 if (enmVMState != VMSTATE_RUNNING)
426 {
427 AssertMsgFailed(("Out of memory while trying to allocate a guest RAM chunk at %VGp!\n", GCPhys));
428 LogRel(("PGM: Out of memory while trying to allocate a guest RAM chunk at %VGp (VMstate=%s)!\n", GCPhys, VMR3GetStateName(enmVMState)));
429 return rc;
430 }
431
432 LogRel(("pgmr3PhysGrowRange: out of memory. pause until the user resumes execution.\n"));
433
434 /* Pause first, then inform Main. */
435 rc = VMR3SuspendNoSave(pVM);
436 AssertRC(rc);
437
438 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.");
439
440 /* Wait for resume event; will only return in that case. If the VM is stopped, the EMT thread will be destroyed. */
441 rc = VMR3WaitForResume(pVM);
442
443 /* Retry */
444 LogRel(("pgmr3PhysGrowRange: VM execution resumed -> retry.\n"));
445 }
446}
447
448#endif /* !VBOX_WITH_NEW_PHYS_CODE */
449
450/**
451 * Interface MMIO handler relocation calls.
452 *
453 * It relocates an existing physical memory range with PGM.
454 *
455 * @returns VBox status.
456 * @param pVM The VM handle.
457 * @param GCPhysOld Previous GC physical address of the RAM range. (page aligned)
458 * @param GCPhysNew New GC physical address of the RAM range. (page aligned)
459 * @param cb Size of the RAM range. (page aligned)
460 */
461PGMR3DECL(int) PGMR3PhysRelocate(PVM pVM, RTGCPHYS GCPhysOld, RTGCPHYS GCPhysNew, size_t cb)
462{
463 /*
464 * Validate input.
465 * (Not so important because callers are only MMR3PhysRelocate(),
466 * but anyway...)
467 */
468 Log(("PGMR3PhysRelocate Old %VGp New %VGp (%#x bytes)\n", GCPhysOld, GCPhysNew, cb));
469
470 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb && cb);
471 Assert(RT_ALIGN_T(GCPhysOld, PAGE_SIZE, RTGCPHYS) == GCPhysOld);
472 Assert(RT_ALIGN_T(GCPhysNew, PAGE_SIZE, RTGCPHYS) == GCPhysNew);
473 RTGCPHYS GCPhysLast;
474 GCPhysLast = GCPhysOld + (cb - 1);
475 if (GCPhysLast < GCPhysOld)
476 {
477 AssertMsgFailed(("The old range wraps! GCPhys=%VGp cb=%#x\n", GCPhysOld, cb));
478 return VERR_INVALID_PARAMETER;
479 }
480 GCPhysLast = GCPhysNew + (cb - 1);
481 if (GCPhysLast < GCPhysNew)
482 {
483 AssertMsgFailed(("The new range wraps! GCPhys=%VGp cb=%#x\n", GCPhysNew, cb));
484 return VERR_INVALID_PARAMETER;
485 }
486
487 /*
488 * Find and remove old range location.
489 */
490 pgmLock(pVM);
491 PPGMRAMRANGE pPrev = NULL;
492 PPGMRAMRANGE pCur = pVM->pgm.s.pRamRangesHC;
493 while (pCur)
494 {
495 if (pCur->GCPhys == GCPhysOld && pCur->cb == cb)
496 break;
497
498 /* next */
499 pPrev = pCur;
500 pCur = pCur->pNextHC;
501 }
502 if (pPrev)
503 {
504 pPrev->pNextHC = pCur->pNextHC;
505 pPrev->pNextGC = pCur->pNextGC;
506 }
507 else
508 {
509 pVM->pgm.s.pRamRangesHC = pCur->pNextHC;
510 pVM->pgm.s.pRamRangesGC = pCur->pNextGC;
511 }
512
513 /*
514 * Update the range.
515 */
516 pCur->GCPhys = GCPhysNew;
517 pCur->GCPhysLast= GCPhysLast;
518 PPGMRAMRANGE pNew = pCur;
519
520 /*
521 * Find range location and check for conflicts.
522 */
523 pPrev = NULL;
524 pCur = pVM->pgm.s.pRamRangesHC;
525 while (pCur)
526 {
527 if (GCPhysNew <= pCur->GCPhysLast && GCPhysLast >= pCur->GCPhys)
528 {
529 AssertMsgFailed(("Conflict! This cannot happen!\n"));
530 pgmUnlock(pVM);
531 return VERR_PGM_RAM_CONFLICT;
532 }
533 if (GCPhysLast < pCur->GCPhys)
534 break;
535
536 /* next */
537 pPrev = pCur;
538 pCur = pCur->pNextHC;
539 }
540
541 /*
542 * Reinsert the RAM range.
543 */
544 pNew->pNextHC = pCur;
545 pNew->pNextGC = pCur ? MMHyperHC2GC(pVM, pCur) : 0;
546 if (pPrev)
547 {
548 pPrev->pNextHC = pNew;
549 pPrev->pNextGC = MMHyperHC2GC(pVM, pNew);
550 }
551 else
552 {
553 pVM->pgm.s.pRamRangesHC = pNew;
554 pVM->pgm.s.pRamRangesGC = MMHyperHC2GC(pVM, pNew);
555 }
556
557 pgmUnlock(pVM);
558 return VINF_SUCCESS;
559}
560
561
562/**
563 * Interface MMR3RomRegister() and MMR3PhysReserve calls to update the
564 * flags of existing RAM ranges.
565 *
566 * @returns VBox status.
567 * @param pVM The VM handle.
568 * @param GCPhys GC physical address of the RAM range. (page aligned)
569 * @param cb Size of the RAM range. (page aligned)
570 * @param fFlags The Or flags, MM_RAM_* \#defines.
571 * @param fMask The and mask for the flags.
572 */
573PGMR3DECL(int) PGMR3PhysSetFlags(PVM pVM, RTGCPHYS GCPhys, size_t cb, unsigned fFlags, unsigned fMask)
574{
575 Log(("PGMR3PhysSetFlags %08X %x %x %x\n", GCPhys, cb, fFlags, fMask));
576
577 /*
578 * Validate input.
579 * (Not so important because caller is always MMR3RomRegister() and MMR3PhysReserve(), but anyway...)
580 */
581 Assert(!(fFlags & ~(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)));
582 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb && cb);
583 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
584 RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
585 AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
586
587 /*
588 * Lookup the range.
589 */
590 PPGMRAMRANGE pRam = CTXSUFF(pVM->pgm.s.pRamRanges);
591 while (pRam && GCPhys > pRam->GCPhysLast)
592 pRam = CTXSUFF(pRam->pNext);
593 if ( !pRam
594 || GCPhys > pRam->GCPhysLast
595 || GCPhysLast < pRam->GCPhys)
596 {
597 AssertMsgFailed(("No RAM range for %VGp-%VGp\n", GCPhys, GCPhysLast));
598 return VERR_INVALID_PARAMETER;
599 }
600
601 /*
602 * Update the requested flags.
603 */
604 RTHCPHYS fFullMask = ~(RTHCPHYS)(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)
605 | fMask;
606 unsigned iPageEnd = (GCPhysLast - pRam->GCPhys + 1) >> PAGE_SHIFT;
607 unsigned iPage = (GCPhys - pRam->GCPhys) >> PAGE_SHIFT;
608 for ( ; iPage < iPageEnd; iPage++)
609 pRam->aPages[iPage].HCPhys = (pRam->aPages[iPage].HCPhys & fFullMask) | fFlags; /** @todo PAGE FLAGS */
610
611 return VINF_SUCCESS;
612}
613
614
615/**
616 * Sets the Address Gate 20 state.
617 *
618 * @param pVM VM handle.
619 * @param fEnable True if the gate should be enabled.
620 * False if the gate should be disabled.
621 */
622PGMDECL(void) PGMR3PhysSetA20(PVM pVM, bool fEnable)
623{
624 LogFlow(("PGMR3PhysSetA20 %d (was %d)\n", fEnable, pVM->pgm.s.fA20Enabled));
625 if (pVM->pgm.s.fA20Enabled != (RTUINT)fEnable)
626 {
627 pVM->pgm.s.fA20Enabled = fEnable;
628 pVM->pgm.s.GCPhysA20Mask = ~(RTGCPHYS)(!fEnable << 20);
629 REMR3A20Set(pVM, fEnable);
630 }
631}
632
633
634/**
635 * Tree enumeration callback for dealing with age rollover.
636 * It will perform a simple compression of the current age.
637 */
638static DECLCALLBACK(int) pgmR3PhysChunkAgeingRolloverCallback(PAVLU32NODECORE pNode, void *pvUser)
639{
640 /* Age compression - ASSUMES iNow == 4. */
641 PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)pNode;
642 if (pChunk->iAge >= UINT32_C(0xffffff00))
643 pChunk->iAge = 3;
644 else if (pChunk->iAge >= UINT32_C(0xfffff000))
645 pChunk->iAge = 2;
646 else if (pChunk->iAge)
647 pChunk->iAge = 1;
648 else /* iAge = 0 */
649 pChunk->iAge = 4;
650
651 /* reinsert */
652 PVM pVM = (PVM)pvUser;
653 RTAvllU32Remove(&pVM->pgm.s.ChunkR3Map.pAgeTree, pChunk->AgeCore.Key);
654 pChunk->AgeCore.Key = pChunk->iAge;
655 RTAvllU32Insert(&pVM->pgm.s.ChunkR3Map.pAgeTree, &pChunk->AgeCore);
656 return 0;
657}
658
659
660/**
661 * Tree enumeration callback that updates the chunks that have
662 * been used since the last
663 */
664static DECLCALLBACK(int) pgmR3PhysChunkAgeingCallback(PAVLU32NODECORE pNode, void *pvUser)
665{
666 PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)pNode;
667 if (!pChunk->iAge)
668 {
669 PVM pVM = (PVM)pvUser;
670 RTAvllU32Remove(&pVM->pgm.s.ChunkR3Map.pAgeTree, pChunk->AgeCore.Key);
671 pChunk->AgeCore.Key = pChunk->iAge = pVM->pgm.s.ChunkR3Map.iNow;
672 RTAvllU32Insert(&pVM->pgm.s.ChunkR3Map.pAgeTree, &pChunk->AgeCore);
673 }
674
675 return 0;
676}
677
678
679/**
680 * Performs ageing of the ring-3 chunk mappings.
681 *
682 * @param pVM The VM handle.
683 */
684PGMR3DECL(void) PGMR3PhysChunkAgeing(PVM pVM)
685{
686 pVM->pgm.s.ChunkR3Map.AgeingCountdown = RT_MIN(pVM->pgm.s.ChunkR3Map.cMax / 4, 1024);
687 pVM->pgm.s.ChunkR3Map.iNow++;
688 if (pVM->pgm.s.ChunkR3Map.iNow == 0)
689 {
690 pVM->pgm.s.ChunkR3Map.iNow = 4;
691 RTAvlU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pTree, true /*fFromLeft*/, pgmR3PhysChunkAgeingRolloverCallback, pVM);
692 }
693 else
694 RTAvlU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pTree, true /*fFromLeft*/, pgmR3PhysChunkAgeingCallback, pVM);
695}
696
697
698/**
699 * The structure passed in the pvUser argument of pgmR3PhysChunkUnmapCandidateCallback().
700 */
701typedef struct PGMR3PHYSCHUNKUNMAPCB
702{
703 PVM pVM; /**< The VM handle. */
704 PPGMCHUNKR3MAP pChunk; /**< The chunk to unmap. */
705} PGMR3PHYSCHUNKUNMAPCB, *PPGMR3PHYSCHUNKUNMAPCB;
706
707
708/**
709 * Callback used to find the mapping that's been unused for
710 * the longest time.
711 */
712static DECLCALLBACK(int) pgmR3PhysChunkUnmapCandidateCallback(PAVLLU32NODECORE pNode, void *pvUser)
713{
714 do
715 {
716 PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)((uint8_t *)pNode - RT_OFFSETOF(PGMCHUNKR3MAP, AgeCore));
717 if ( pChunk->iAge
718 && !pChunk->cRefs)
719 {
720 /*
721 * Check that it's not in any of the TLBs.
722 */
723 PVM pVM = ((PPGMR3PHYSCHUNKUNMAPCB)pvUser)->pVM;
724 for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.ChunkR3Map.Tlb.aEntries); i++)
725 if (pVM->pgm.s.ChunkR3Map.Tlb.aEntries[i].pChunk == pChunk)
726 {
727 pChunk = NULL;
728 break;
729 }
730 if (pChunk)
731 for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.PhysTlbHC.aEntries); i++)
732 if (pVM->pgm.s.PhysTlbHC.aEntries[i].pMap == pChunk)
733 {
734 pChunk = NULL;
735 break;
736 }
737 if (pChunk)
738 {
739 ((PPGMR3PHYSCHUNKUNMAPCB)pvUser)->pChunk = pChunk;
740 return 1; /* done */
741 }
742 }
743
744 /* next with the same age - this version of the AVL API doesn't enumerate the list, so we have to do it. */
745 pNode = pNode->pList;
746 } while (pNode);
747 return 0;
748}
749
750
751/**
752 * Finds a good candidate for unmapping when the ring-3 mapping cache is full.
753 *
754 * The candidate will not be part of any TLBs, so no need to flush
755 * anything afterwards.
756 *
757 * @returns Chunk id.
758 * @param pVM The VM handle.
759 */
760static int32_t pgmR3PhysChunkFindUnmapCandidate(PVM pVM)
761{
762 /*
763 * Do tree ageing first?
764 */
765 if (pVM->pgm.s.ChunkR3Map.AgeingCountdown-- == 0)
766 PGMR3PhysChunkAgeing(pVM);
767
768 /*
769 * Enumerate the age tree starting with the left most node.
770 */
771 PGMR3PHYSCHUNKUNMAPCB Args;
772 Args.pVM = pVM;
773 Args.pChunk = NULL;
774 if (RTAvllU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pAgeTree, true /*fFromLeft*/, pgmR3PhysChunkUnmapCandidateCallback, pVM))
775 return Args.pChunk->Core.Key;
776 return INT32_MAX;
777}
778
779
780/**
781 * Maps the given chunk into the ring-3 mapping cache.
782 *
783 * This will call ring-0.
784 *
785 * @returns VBox status code.
786 * @param pVM The VM handle.
787 * @param idChunk The chunk in question.
788 * @param ppChunk Where to store the chunk tracking structure.
789 *
790 * @remarks Called from within the PGM critical section.
791 */
792int pgmR3PhysChunkMap(PVM pVM, uint32_t idChunk, PPPGMCHUNKR3MAP ppChunk)
793{
794 int rc;
795 /*
796 * Allocate a new tracking structure first.
797 */
798#if 0 /* for later when we've got a separate mapping method for ring-0. */
799 PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)MMR3HeapAlloc(pVM, MM_TAG_PGM_CHUNK_MAPPING, sizeof(*pChunk));
800 AssertReturn(pChunk, VERR_NO_MEMORY);
801#else
802 PPGMCHUNKR3MAP pChunk;
803 rc = MMHyperAlloc(pVM, sizeof(*pChunk), 0, MM_TAG_PGM_CHUNK_MAPPING, (void **)&pChunk);
804 AssertRCReturn(rc, rc);
805#endif
806 pChunk->Core.Key = idChunk;
807 pChunk->AgeCore.Key = pVM->pgm.s.ChunkR3Map.iNow;
808 pChunk->iAge = 0;
809 pChunk->cRefs = 0;
810 pChunk->cPermRefs = 0;
811 pChunk->pv = NULL;
812
813 /*
814 * Request the ring-0 part to map the chunk in question and if
815 * necessary unmap another one to make space in the mapping cache.
816 */
817 GMMMAPUNMAPCHUNKREQ Req;
818 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
819 Req.Hdr.cbReq = sizeof(Req);
820 Req.pvR3 = NULL;
821 Req.idChunkMap = idChunk;
822 Req.idChunkUnmap = INT32_MAX;
823 if (pVM->pgm.s.ChunkR3Map.c >= pVM->pgm.s.ChunkR3Map.cMax)
824 Req.idChunkUnmap = pgmR3PhysChunkFindUnmapCandidate(pVM);
825 rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_MAP_UNMAP_CHUNK, 0, &Req.Hdr);
826 if (VBOX_SUCCESS(rc))
827 {
828 /*
829 * Update the tree.
830 */
831 /* insert the new one. */
832 AssertPtr(Req.pvR3);
833 pChunk->pv = Req.pvR3;
834 bool fRc = RTAvlU32Insert(&pVM->pgm.s.ChunkR3Map.pTree, &pChunk->Core);
835 AssertRelease(fRc);
836 pVM->pgm.s.ChunkR3Map.c++;
837
838 fRc = RTAvllU32Insert(&pVM->pgm.s.ChunkR3Map.pAgeTree, &pChunk->AgeCore);
839 AssertRelease(fRc);
840
841 /* remove the unmapped one. */
842 if (Req.idChunkUnmap != INT32_MAX)
843 {
844 PPGMCHUNKR3MAP pUnmappedChunk = (PPGMCHUNKR3MAP)RTAvlU32Remove(&pVM->pgm.s.ChunkR3Map.pTree, Req.idChunkUnmap);
845 AssertRelease(pUnmappedChunk);
846 pUnmappedChunk->pv = NULL;
847 pUnmappedChunk->Core.Key = UINT32_MAX;
848#if 0 /* for later when we've got a separate mapping method for ring-0. */
849 MMR3HeapFree(pUnmappedChunk);
850#else
851 MMHyperFree(pVM, pUnmappedChunk);
852#endif
853 pVM->pgm.s.ChunkR3Map.c--;
854 }
855 }
856 else
857 {
858 AssertRC(rc);
859#if 0 /* for later when we've got a separate mapping method for ring-0. */
860 MMR3HeapFree(pChunk);
861#else
862 MMHyperFree(pVM, pChunk);
863#endif
864 pChunk = NULL;
865 }
866
867 *ppChunk = pChunk;
868 return rc;
869}
870
871
872/**
873 * For VMMCALLHOST_PGM_MAP_CHUNK, considered internal.
874 *
875 * @returns see pgmR3PhysChunkMap.
876 * @param pVM The VM handle.
877 * @param idChunk The chunk to map.
878 */
879PDMR3DECL(int) PGMR3PhysChunkMap(PVM pVM, uint32_t idChunk)
880{
881 PPGMCHUNKR3MAP pChunk;
882 return pgmR3PhysChunkMap(pVM, idChunk, &pChunk);
883}
884
885
886/**
887 * Invalidates the TLB for the ring-3 mapping cache.
888 *
889 * @param pVM The VM handle.
890 */
891PGMR3DECL(void) PGMR3PhysChunkInvalidateTLB(PVM pVM)
892{
893 pgmLock(pVM);
894 for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.ChunkR3Map.Tlb.aEntries); i++)
895 {
896 pVM->pgm.s.ChunkR3Map.Tlb.aEntries[i].idChunk = NIL_GMM_CHUNKID;
897 pVM->pgm.s.ChunkR3Map.Tlb.aEntries[i].pChunk = NULL;
898 }
899 pgmUnlock(pVM);
900}
901
902
903/**
904 * Response to VM_FF_PGM_NEED_HANDY_PAGES and VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES.
905 *
906 * @returns The following VBox status codes.
907 * @retval VINF_SUCCESS on success. FF cleared.
908 * @retval VINF_EM_NO_MEMORY if we're out of memory. The FF is not cleared in this case.
909 *
910 * @param pVM The VM handle.
911 */
912PDMR3DECL(int) PGMR3PhysAllocateHandyPages(PVM pVM)
913{
914 pgmLock(pVM);
915 int rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES, 0, NULL);
916 if (rc == VERR_GMM_SEED_ME)
917 {
918 void *pvChunk;
919 rc = SUPPageAlloc(GMM_CHUNK_SIZE >> PAGE_SHIFT, &pvChunk);
920 if (VBOX_SUCCESS(rc))
921 rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_SEED_CHUNK, (uintptr_t)pvChunk, NULL);
922 if (VBOX_FAILURE(rc))
923 {
924 LogRel(("PGM: GMM Seeding failed, rc=%Vrc\n", rc));
925 rc = VINF_EM_NO_MEMORY;
926 }
927 }
928 pgmUnlock(pVM);
929 Assert(rc == VINF_SUCCESS || rc == VINF_EM_NO_MEMORY);
930 return rc;
931}
932
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