VirtualBox

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

Last change on this file since 6570 was 6546, checked in by vboxsync, 17 years ago

VBOX_WITH_NEW_PHYS_CODE changes mostly realted to REM. Killed a warning in cpu-exec.c.

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