1 | /* $Id: PGMPhys.cpp 4387 2007-08-27 14:24:53Z 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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * 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 | /**
|
---|
47 | * Interface that the MMR3RamRegister(), MMR3RomRegister() and MMIO handler
|
---|
48 | * registration APIs calls to inform PGM about memory registrations.
|
---|
49 | *
|
---|
50 | * It registers the physical memory range with PGM. MM is responsible
|
---|
51 | * for the toplevel things - allocation and locking - while PGM is taking
|
---|
52 | * care of all the details and implements the physical address space virtualization.
|
---|
53 | *
|
---|
54 | * @returns VBox status.
|
---|
55 | * @param pVM The VM handle.
|
---|
56 | * @param pvRam HC virtual address of the RAM range. (page aligned)
|
---|
57 | * @param GCPhys GC physical address of the RAM range. (page aligned)
|
---|
58 | * @param cb Size of the RAM range. (page aligned)
|
---|
59 | * @param fFlags Flags, MM_RAM_*.
|
---|
60 | * @param paPages Pointer an array of physical page descriptors.
|
---|
61 | * @param pszDesc Description string.
|
---|
62 | */
|
---|
63 | PGMR3DECL(int) PGMR3PhysRegister(PVM pVM, void *pvRam, RTGCPHYS GCPhys, size_t cb, unsigned fFlags, const SUPPAGE *paPages, const char *pszDesc)
|
---|
64 | {
|
---|
65 | /*
|
---|
66 | * Validate input.
|
---|
67 | * (Not so important because callers are only MMR3PhysRegister()
|
---|
68 | * and PGMR3HandlerPhysicalRegisterEx(), but anyway...)
|
---|
69 | */
|
---|
70 | Log(("PGMR3PhysRegister %08X %x bytes flags %x %s\n", GCPhys, cb, fFlags, pszDesc));
|
---|
71 |
|
---|
72 | Assert((fFlags & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_DYNAMIC_ALLOC)) || paPages);
|
---|
73 | /*Assert(!(fFlags & MM_RAM_FLAGS_RESERVED) || !paPages);*/
|
---|
74 | Assert((fFlags == (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO)) || (fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC) || pvRam);
|
---|
75 | /*Assert(!(fFlags & MM_RAM_FLAGS_RESERVED) || !pvRam);*/
|
---|
76 | Assert(!(fFlags & ~0xfff));
|
---|
77 | Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb && cb);
|
---|
78 | Assert(RT_ALIGN_P(pvRam, PAGE_SIZE) == pvRam);
|
---|
79 | Assert(!(fFlags & ~(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2 | MM_RAM_FLAGS_DYNAMIC_ALLOC)));
|
---|
80 | Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
|
---|
81 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
82 | if (GCPhysLast < GCPhys)
|
---|
83 | {
|
---|
84 | AssertMsgFailed(("The range wraps! GCPhys=%VGp cb=%#x\n", GCPhys, cb));
|
---|
85 | return VERR_INVALID_PARAMETER;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * Find range location and check for conflicts.
|
---|
90 | */
|
---|
91 | PPGMRAMRANGE pPrev = NULL;
|
---|
92 | PPGMRAMRANGE pCur = pVM->pgm.s.pRamRangesHC;
|
---|
93 | while (pCur)
|
---|
94 | {
|
---|
95 | if (GCPhys <= pCur->GCPhysLast && GCPhysLast >= pCur->GCPhys)
|
---|
96 | {
|
---|
97 | AssertMsgFailed(("Conflict! This cannot happen!\n"));
|
---|
98 | return VERR_PGM_RAM_CONFLICT;
|
---|
99 | }
|
---|
100 | if (GCPhysLast < pCur->GCPhys)
|
---|
101 | break;
|
---|
102 |
|
---|
103 | /* next */
|
---|
104 | pPrev = pCur;
|
---|
105 | pCur = pCur->pNextHC;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * Allocate RAM range.
|
---|
110 | * Small ranges are allocated from the heap, big ones have separate mappings.
|
---|
111 | */
|
---|
112 | size_t cbRam = RT_OFFSETOF(PGMRAMRANGE, aHCPhys[cb >> PAGE_SHIFT]);
|
---|
113 | PPGMRAMRANGE pNew;
|
---|
114 | RTGCPTR GCPtrNew;
|
---|
115 | int rc;
|
---|
116 | if (cbRam > PAGE_SIZE / 2)
|
---|
117 | { /* large */
|
---|
118 | cbRam = RT_ALIGN_Z(cbRam, PAGE_SIZE);
|
---|
119 | rc = SUPPageAlloc(cbRam >> PAGE_SHIFT, (void **)&pNew);
|
---|
120 | if (VBOX_SUCCESS(rc))
|
---|
121 | {
|
---|
122 | rc = MMR3HyperMapHCRam(pVM, pNew, cbRam, true, pszDesc, &GCPtrNew);
|
---|
123 | if (VBOX_SUCCESS(rc))
|
---|
124 | {
|
---|
125 | Assert(MMHyperHC2GC(pVM, pNew) == GCPtrNew);
|
---|
126 | rc = MMR3HyperReserve(pVM, PAGE_SIZE, "fence", NULL);
|
---|
127 | }
|
---|
128 | else
|
---|
129 | {
|
---|
130 | AssertMsgFailed(("MMR3HyperMapHCRam(,,%#x,,,) -> %Vrc\n", cbRam, rc));
|
---|
131 | SUPPageFree(pNew, cbRam >> PAGE_SHIFT);
|
---|
132 | }
|
---|
133 | }
|
---|
134 | else
|
---|
135 | AssertMsgFailed(("SUPPageAlloc(%#x,,) -> %Vrc\n", cbRam >> PAGE_SHIFT, rc));
|
---|
136 | }
|
---|
137 | else
|
---|
138 | { /* small */
|
---|
139 | rc = MMHyperAlloc(pVM, cbRam, 16, MM_TAG_PGM, (void **)&pNew);
|
---|
140 | if (VBOX_SUCCESS(rc))
|
---|
141 | GCPtrNew = MMHyperHC2GC(pVM, pNew);
|
---|
142 | else
|
---|
143 | AssertMsgFailed(("MMHyperAlloc(,%#x,,,) -> %Vrc\n", cbRam, cb));
|
---|
144 | }
|
---|
145 | if (VBOX_SUCCESS(rc))
|
---|
146 | {
|
---|
147 | /*
|
---|
148 | * Initialize the range.
|
---|
149 | */
|
---|
150 | pNew->pvHC = pvRam;
|
---|
151 | pNew->GCPhys = GCPhys;
|
---|
152 | pNew->GCPhysLast = GCPhysLast;
|
---|
153 | pNew->cb = cb;
|
---|
154 | pNew->fFlags = fFlags;
|
---|
155 | pNew->pavHCChunkHC = NULL;
|
---|
156 | pNew->pavHCChunkGC = 0;
|
---|
157 |
|
---|
158 | unsigned iPage = cb >> PAGE_SHIFT;
|
---|
159 | if (paPages)
|
---|
160 | {
|
---|
161 | while (iPage-- > 0)
|
---|
162 | pNew->aHCPhys[iPage] = (paPages[iPage].Phys & X86_PTE_PAE_PG_MASK) | fFlags;
|
---|
163 | }
|
---|
164 | else if (fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
|
---|
165 | {
|
---|
166 | /* Allocate memory for chunk to HC ptr lookup array. */
|
---|
167 | rc = MMHyperAlloc(pVM, (cb >> PGM_DYNAMIC_CHUNK_SHIFT) * sizeof(void *), 16, MM_TAG_PGM, (void **)&pNew->pavHCChunkHC);
|
---|
168 | AssertMsgReturn(rc == VINF_SUCCESS, ("MMHyperAlloc(,%#x,,,) -> %Vrc\n", cbRam, cb), rc);
|
---|
169 |
|
---|
170 | pNew->pavHCChunkGC = MMHyperHC2GC(pVM, pNew->pavHCChunkHC);
|
---|
171 | Assert(pNew->pavHCChunkGC);
|
---|
172 |
|
---|
173 | /* Physical memory will be allocated on demand. */
|
---|
174 | while (iPage-- > 0)
|
---|
175 | pNew->aHCPhys[iPage] = fFlags;
|
---|
176 | }
|
---|
177 | else
|
---|
178 | {
|
---|
179 | Assert(fFlags == (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO));
|
---|
180 | RTHCPHYS HCPhysDummyPage = (MMR3PageDummyHCPhys(pVM) & X86_PTE_PAE_PG_MASK) | fFlags;
|
---|
181 | while (iPage-- > 0)
|
---|
182 | pNew->aHCPhys[iPage] = HCPhysDummyPage;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /*
|
---|
186 | * Insert the new RAM range.
|
---|
187 | */
|
---|
188 | pgmLock(pVM);
|
---|
189 | pNew->pNextHC = pCur;
|
---|
190 | pNew->pNextGC = pCur ? MMHyperHC2GC(pVM, pCur) : 0;
|
---|
191 | if (pPrev)
|
---|
192 | {
|
---|
193 | pPrev->pNextHC = pNew;
|
---|
194 | pPrev->pNextGC = GCPtrNew;
|
---|
195 | }
|
---|
196 | else
|
---|
197 | {
|
---|
198 | pVM->pgm.s.pRamRangesHC = pNew;
|
---|
199 | pVM->pgm.s.pRamRangesGC = GCPtrNew;
|
---|
200 | }
|
---|
201 | pgmUnlock(pVM);
|
---|
202 | }
|
---|
203 | return rc;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Register a chunk of a the physical memory range with PGM. MM is responsible
|
---|
209 | * for the toplevel things - allocation and locking - while PGM is taking
|
---|
210 | * care of all the details and implements the physical address space virtualization.
|
---|
211 | *
|
---|
212 | *
|
---|
213 | * @returns VBox status.
|
---|
214 | * @param pVM The VM handle.
|
---|
215 | * @param pvRam HC virtual address of the RAM range. (page aligned)
|
---|
216 | * @param GCPhys GC physical address of the RAM range. (page aligned)
|
---|
217 | * @param cb Size of the RAM range. (page aligned)
|
---|
218 | * @param fFlags Flags, MM_RAM_*.
|
---|
219 | * @param paPages Pointer an array of physical page descriptors.
|
---|
220 | * @param pszDesc Description string.
|
---|
221 | */
|
---|
222 | PGMR3DECL(int) PGMR3PhysRegisterChunk(PVM pVM, void *pvRam, RTGCPHYS GCPhys, size_t cb, unsigned fFlags, const SUPPAGE *paPages, const char *pszDesc)
|
---|
223 | {
|
---|
224 | #ifdef PGM_DYNAMIC_RAM_ALLOC
|
---|
225 | NOREF(pszDesc);
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * Validate input.
|
---|
229 | * (Not so important because callers are only MMR3PhysRegister()
|
---|
230 | * and PGMR3HandlerPhysicalRegisterEx(), but anyway...)
|
---|
231 | */
|
---|
232 | Log(("PGMR3PhysRegisterChunk %08X %x bytes flags %x %s\n", GCPhys, cb, fFlags, pszDesc));
|
---|
233 |
|
---|
234 | Assert(paPages);
|
---|
235 | Assert(pvRam);
|
---|
236 | Assert(!(fFlags & ~0xfff));
|
---|
237 | Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb && cb);
|
---|
238 | Assert(RT_ALIGN_P(pvRam, PAGE_SIZE) == pvRam);
|
---|
239 | Assert(!(fFlags & ~(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2 | MM_RAM_FLAGS_DYNAMIC_ALLOC)));
|
---|
240 | Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
|
---|
241 | Assert(VM_IS_EMT(pVM));
|
---|
242 | Assert(!(GCPhys & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
|
---|
243 | Assert(cb == PGM_DYNAMIC_CHUNK_SIZE);
|
---|
244 |
|
---|
245 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
246 | if (GCPhysLast < GCPhys)
|
---|
247 | {
|
---|
248 | AssertMsgFailed(("The range wraps! GCPhys=%VGp cb=%#x\n", GCPhys, cb));
|
---|
249 | return VERR_INVALID_PARAMETER;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /*
|
---|
253 | * Find existing range location.
|
---|
254 | */
|
---|
255 | PPGMRAMRANGE pRam = CTXSUFF(pVM->pgm.s.pRamRanges);
|
---|
256 | while (pRam)
|
---|
257 | {
|
---|
258 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
259 | if ( off < pRam->cb
|
---|
260 | && (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC))
|
---|
261 | break;
|
---|
262 |
|
---|
263 | pRam = CTXSUFF(pRam->pNext);
|
---|
264 | }
|
---|
265 | AssertReturn(pRam, VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS);
|
---|
266 |
|
---|
267 | unsigned off = (GCPhys - pRam->GCPhys) >> PAGE_SHIFT;
|
---|
268 | unsigned iPage = cb >> PAGE_SHIFT;
|
---|
269 | if (paPages)
|
---|
270 | {
|
---|
271 | while (iPage-- > 0)
|
---|
272 | pRam->aHCPhys[off + iPage] = (paPages[iPage].Phys & X86_PTE_PAE_PG_MASK) | fFlags;
|
---|
273 | }
|
---|
274 | off >>= (PGM_DYNAMIC_CHUNK_SHIFT - PAGE_SHIFT);
|
---|
275 | pRam->pavHCChunkHC[off] = pvRam;
|
---|
276 |
|
---|
277 | /* Notify the recompiler. */
|
---|
278 | REMR3NotifyPhysRamChunkRegister(pVM, GCPhys, PGM_DYNAMIC_CHUNK_SIZE, (RTHCUINTPTR)pvRam, fFlags);
|
---|
279 |
|
---|
280 | return VINF_SUCCESS;
|
---|
281 | #else /* !PGM_DYNAMIC_RAM_ALLOC */
|
---|
282 | AssertReleaseMsgFailed(("Shouldn't ever get here when PGM_DYNAMIC_RAM_ALLOC isn't defined!\n"));
|
---|
283 | return VERR_INTERNAL_ERROR;
|
---|
284 | #endif /* !PGM_DYNAMIC_RAM_ALLOC */
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * Allocate missing physical pages for an existing guest RAM range.
|
---|
290 | *
|
---|
291 | * @returns VBox status.
|
---|
292 | * @param pVM The VM handle.
|
---|
293 | * @param GCPhys GC physical address of the RAM range. (page aligned)
|
---|
294 | */
|
---|
295 | PGMR3DECL(int) PGM3PhysGrowRange(PVM pVM, RTGCPHYS GCPhys)
|
---|
296 | {
|
---|
297 | /*
|
---|
298 | * Walk range list.
|
---|
299 | */
|
---|
300 | pgmLock(pVM);
|
---|
301 |
|
---|
302 | PPGMRAMRANGE pRam = CTXSUFF(pVM->pgm.s.pRamRanges);
|
---|
303 | while (pRam)
|
---|
304 | {
|
---|
305 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
306 | if ( off < pRam->cb
|
---|
307 | && (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC))
|
---|
308 | {
|
---|
309 | bool fRangeExists = false;
|
---|
310 | unsigned off = (GCPhys - pRam->GCPhys) >> PGM_DYNAMIC_CHUNK_SHIFT;
|
---|
311 |
|
---|
312 | /** @note A request made from another thread may end up in EMT after somebody else has already allocated the range. */
|
---|
313 | if (pRam->pavHCChunkHC[off])
|
---|
314 | fRangeExists = true;
|
---|
315 |
|
---|
316 | pgmUnlock(pVM);
|
---|
317 | if (fRangeExists)
|
---|
318 | return VINF_SUCCESS;
|
---|
319 | return pgmr3PhysGrowRange(pVM, GCPhys);
|
---|
320 | }
|
---|
321 |
|
---|
322 | pRam = CTXSUFF(pRam->pNext);
|
---|
323 | }
|
---|
324 | pgmUnlock(pVM);
|
---|
325 | return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * Allocate missing physical pages for an existing guest RAM range.
|
---|
331 | *
|
---|
332 | * @returns VBox status.
|
---|
333 | * @param pVM The VM handle.
|
---|
334 | * @param pRamRange RAM range
|
---|
335 | * @param GCPhys GC physical address of the RAM range. (page aligned)
|
---|
336 | */
|
---|
337 | int pgmr3PhysGrowRange(PVM pVM, RTGCPHYS GCPhys)
|
---|
338 | {
|
---|
339 | void *pvRam;
|
---|
340 | int rc;
|
---|
341 |
|
---|
342 | /* We must execute this function in the EMT thread, otherwise we'll run into problems. */
|
---|
343 | if (!VM_IS_EMT(pVM))
|
---|
344 | {
|
---|
345 | PVMREQ pReq;
|
---|
346 |
|
---|
347 | AssertMsg(!PDMCritSectIsOwner(&pVM->pgm.s.CritSect), ("We own the PGM lock -> deadlock danger!!\n"));
|
---|
348 |
|
---|
349 | rc = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)PGM3PhysGrowRange, 2, pVM, GCPhys);
|
---|
350 | if (VBOX_SUCCESS(rc))
|
---|
351 | {
|
---|
352 | rc = pReq->iStatus;
|
---|
353 | VMR3ReqFree(pReq);
|
---|
354 | }
|
---|
355 | return rc;
|
---|
356 | }
|
---|
357 |
|
---|
358 | /* Round down to chunk boundary */
|
---|
359 | GCPhys = GCPhys & PGM_DYNAMIC_CHUNK_BASE_MASK;
|
---|
360 |
|
---|
361 | STAM_COUNTER_INC(&pVM->pgm.s.StatDynRamGrow);
|
---|
362 | STAM_COUNTER_ADD(&pVM->pgm.s.StatDynRamTotal, PGM_DYNAMIC_CHUNK_SIZE/(1024*1024));
|
---|
363 |
|
---|
364 | Log(("pgmr3PhysGrowRange: allocate chunk of size 0x%X at %VGp\n", PGM_DYNAMIC_CHUNK_SIZE, GCPhys));
|
---|
365 |
|
---|
366 | unsigned cPages = PGM_DYNAMIC_CHUNK_SIZE >> PAGE_SHIFT;
|
---|
367 | rc = SUPPageAlloc(cPages, &pvRam);
|
---|
368 | if (VBOX_SUCCESS(rc))
|
---|
369 | {
|
---|
370 | VMSTATE enmVMState = VMR3GetState(pVM);
|
---|
371 |
|
---|
372 | rc = MMR3PhysRegisterEx(pVM, pvRam, GCPhys, PGM_DYNAMIC_CHUNK_SIZE, 0, MM_PHYS_TYPE_DYNALLOC_CHUNK, "Main Memory");
|
---|
373 | if ( VBOX_SUCCESS(rc)
|
---|
374 | || enmVMState != VMSTATE_RUNNING)
|
---|
375 | {
|
---|
376 | if (VBOX_FAILURE(rc))
|
---|
377 | {
|
---|
378 | AssertMsgFailed(("Out of memory while trying to allocate a guest RAM chunk at %VGp!\n", GCPhys));
|
---|
379 | LogRel(("PGM: Out of memory while trying to allocate a guest RAM chunk at %VGp (VMstate=%s)!\n", GCPhys, VMR3GetStateName(enmVMState)));
|
---|
380 | }
|
---|
381 | return rc;
|
---|
382 | }
|
---|
383 |
|
---|
384 | SUPPageFree(pvRam, cPages);
|
---|
385 |
|
---|
386 | LogRel(("pgmr3PhysGrowRange: out of memory. pause until the user resumes execution.\n"));
|
---|
387 |
|
---|
388 | /* Pause first, then inform Main. */
|
---|
389 | rc = VMR3SuspendNoSave(pVM);
|
---|
390 | AssertRC(rc);
|
---|
391 |
|
---|
392 | 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.");
|
---|
393 |
|
---|
394 | /* Wait for resume event; will only return in that case. If the VM is stopped, the EMT thread will be destroyed. */
|
---|
395 | rc = VMR3WaitForResume(pVM);
|
---|
396 |
|
---|
397 | /* Retry */
|
---|
398 | LogRel(("pgmr3PhysGrowRange: VM execution resumed -> retry.\n"));
|
---|
399 | return pgmr3PhysGrowRange(pVM, GCPhys);
|
---|
400 | }
|
---|
401 | return rc;
|
---|
402 | }
|
---|
403 |
|
---|
404 |
|
---|
405 | /**
|
---|
406 | * Interface MMIO handler relocation calls.
|
---|
407 | *
|
---|
408 | * It relocates an existing physical memory range with PGM.
|
---|
409 | *
|
---|
410 | * @returns VBox status.
|
---|
411 | * @param pVM The VM handle.
|
---|
412 | * @param GCPhysOld Previous GC physical address of the RAM range. (page aligned)
|
---|
413 | * @param GCPhysNew New GC physical address of the RAM range. (page aligned)
|
---|
414 | * @param cb Size of the RAM range. (page aligned)
|
---|
415 | */
|
---|
416 | PGMR3DECL(int) PGMR3PhysRelocate(PVM pVM, RTGCPHYS GCPhysOld, RTGCPHYS GCPhysNew, size_t cb)
|
---|
417 | {
|
---|
418 | /*
|
---|
419 | * Validate input.
|
---|
420 | * (Not so important because callers are only MMR3PhysRelocate(),
|
---|
421 | * but anyway...)
|
---|
422 | */
|
---|
423 | Log(("PGMR3PhysRelocate Old %VGp New %VGp (%#x bytes)\n", GCPhysOld, GCPhysNew, cb));
|
---|
424 |
|
---|
425 | Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb && cb);
|
---|
426 | Assert(RT_ALIGN_T(GCPhysOld, PAGE_SIZE, RTGCPHYS) == GCPhysOld);
|
---|
427 | Assert(RT_ALIGN_T(GCPhysNew, PAGE_SIZE, RTGCPHYS) == GCPhysNew);
|
---|
428 | RTGCPHYS GCPhysLast;
|
---|
429 | GCPhysLast = GCPhysOld + (cb - 1);
|
---|
430 | if (GCPhysLast < GCPhysOld)
|
---|
431 | {
|
---|
432 | AssertMsgFailed(("The old range wraps! GCPhys=%VGp cb=%#x\n", GCPhysOld, cb));
|
---|
433 | return VERR_INVALID_PARAMETER;
|
---|
434 | }
|
---|
435 | GCPhysLast = GCPhysNew + (cb - 1);
|
---|
436 | if (GCPhysLast < GCPhysNew)
|
---|
437 | {
|
---|
438 | AssertMsgFailed(("The new range wraps! GCPhys=%VGp cb=%#x\n", GCPhysNew, cb));
|
---|
439 | return VERR_INVALID_PARAMETER;
|
---|
440 | }
|
---|
441 |
|
---|
442 | /*
|
---|
443 | * Find and remove old range location.
|
---|
444 | */
|
---|
445 | pgmLock(pVM);
|
---|
446 | PPGMRAMRANGE pPrev = NULL;
|
---|
447 | PPGMRAMRANGE pCur = pVM->pgm.s.pRamRangesHC;
|
---|
448 | while (pCur)
|
---|
449 | {
|
---|
450 | if (pCur->GCPhys == GCPhysOld && pCur->cb == cb)
|
---|
451 | break;
|
---|
452 |
|
---|
453 | /* next */
|
---|
454 | pPrev = pCur;
|
---|
455 | pCur = pCur->pNextHC;
|
---|
456 | }
|
---|
457 | if (pPrev)
|
---|
458 | {
|
---|
459 | pPrev->pNextHC = pCur->pNextHC;
|
---|
460 | pPrev->pNextGC = pCur->pNextGC;
|
---|
461 | }
|
---|
462 | else
|
---|
463 | {
|
---|
464 | pVM->pgm.s.pRamRangesHC = pCur->pNextHC;
|
---|
465 | pVM->pgm.s.pRamRangesGC = pCur->pNextGC;
|
---|
466 | }
|
---|
467 |
|
---|
468 | /*
|
---|
469 | * Update the range.
|
---|
470 | */
|
---|
471 | pCur->GCPhys = GCPhysNew;
|
---|
472 | pCur->GCPhysLast= GCPhysLast;
|
---|
473 | PPGMRAMRANGE pNew = pCur;
|
---|
474 |
|
---|
475 | /*
|
---|
476 | * Find range location and check for conflicts.
|
---|
477 | */
|
---|
478 | pPrev = NULL;
|
---|
479 | pCur = pVM->pgm.s.pRamRangesHC;
|
---|
480 | while (pCur)
|
---|
481 | {
|
---|
482 | if (GCPhysNew <= pCur->GCPhysLast && GCPhysLast >= pCur->GCPhys)
|
---|
483 | {
|
---|
484 | AssertMsgFailed(("Conflict! This cannot happen!\n"));
|
---|
485 | pgmUnlock(pVM);
|
---|
486 | return VERR_PGM_RAM_CONFLICT;
|
---|
487 | }
|
---|
488 | if (GCPhysLast < pCur->GCPhys)
|
---|
489 | break;
|
---|
490 |
|
---|
491 | /* next */
|
---|
492 | pPrev = pCur;
|
---|
493 | pCur = pCur->pNextHC;
|
---|
494 | }
|
---|
495 |
|
---|
496 | /*
|
---|
497 | * Reinsert the RAM range.
|
---|
498 | */
|
---|
499 | pNew->pNextHC = pCur;
|
---|
500 | pNew->pNextGC = pCur ? MMHyperHC2GC(pVM, pCur) : 0;
|
---|
501 | if (pPrev)
|
---|
502 | {
|
---|
503 | pPrev->pNextHC = pNew;
|
---|
504 | pPrev->pNextGC = MMHyperHC2GC(pVM, pNew);
|
---|
505 | }
|
---|
506 | else
|
---|
507 | {
|
---|
508 | pVM->pgm.s.pRamRangesHC = pNew;
|
---|
509 | pVM->pgm.s.pRamRangesGC = MMHyperHC2GC(pVM, pNew);
|
---|
510 | }
|
---|
511 |
|
---|
512 | pgmUnlock(pVM);
|
---|
513 | return VINF_SUCCESS;
|
---|
514 | }
|
---|
515 |
|
---|
516 |
|
---|
517 | /**
|
---|
518 | * Interface MMR3RomRegister() and MMR3PhysReserve calls to update the
|
---|
519 | * flags of existing RAM ranges.
|
---|
520 | *
|
---|
521 | * @returns VBox status.
|
---|
522 | * @param pVM The VM handle.
|
---|
523 | * @param GCPhys GC physical address of the RAM range. (page aligned)
|
---|
524 | * @param cb Size of the RAM range. (page aligned)
|
---|
525 | * @param fFlags The Or flags, MM_RAM_* \#defines.
|
---|
526 | * @param fMask The and mask for the flags.
|
---|
527 | */
|
---|
528 | PGMR3DECL(int) PGMR3PhysSetFlags(PVM pVM, RTGCPHYS GCPhys, size_t cb, unsigned fFlags, unsigned fMask)
|
---|
529 | {
|
---|
530 | Log(("PGMR3PhysSetFlags %08X %x %x %x\n", GCPhys, cb, fFlags, fMask));
|
---|
531 |
|
---|
532 | /*
|
---|
533 | * Validate input.
|
---|
534 | * (Not so important because caller is always MMR3RomRegister() and MMR3PhysReserve(), but anyway...)
|
---|
535 | */
|
---|
536 | Assert(!(fFlags & ~(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)));
|
---|
537 | Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb && cb);
|
---|
538 | Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
|
---|
539 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
540 | AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
|
---|
541 |
|
---|
542 | /*
|
---|
543 | * Lookup the range.
|
---|
544 | */
|
---|
545 | PPGMRAMRANGE pRam = CTXSUFF(pVM->pgm.s.pRamRanges);
|
---|
546 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
547 | pRam = CTXSUFF(pRam->pNext);
|
---|
548 | if ( !pRam
|
---|
549 | || GCPhys > pRam->GCPhysLast
|
---|
550 | || GCPhysLast < pRam->GCPhys)
|
---|
551 | {
|
---|
552 | AssertMsgFailed(("No RAM range for %VGp-%VGp\n", GCPhys, GCPhysLast));
|
---|
553 | return VERR_INVALID_PARAMETER;
|
---|
554 | }
|
---|
555 |
|
---|
556 | /*
|
---|
557 | * Update the requested flags.
|
---|
558 | */
|
---|
559 | RTHCPHYS fFullMask = ~(RTHCPHYS)(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)
|
---|
560 | | fMask;
|
---|
561 | unsigned iPageEnd = (GCPhysLast - pRam->GCPhys + 1) >> PAGE_SHIFT;
|
---|
562 | unsigned iPage = (GCPhys - pRam->GCPhys) >> PAGE_SHIFT;
|
---|
563 | for ( ; iPage < iPageEnd; iPage++)
|
---|
564 | pRam->aHCPhys[iPage] = (pRam->aHCPhys[iPage] & fFullMask) | fFlags;
|
---|
565 |
|
---|
566 | return VINF_SUCCESS;
|
---|
567 | }
|
---|
568 |
|
---|
569 |
|
---|
570 | /**
|
---|
571 | * Sets the Address Gate 20 state.
|
---|
572 | *
|
---|
573 | * @param pVM VM handle.
|
---|
574 | * @param fEnable True if the gate should be enabled.
|
---|
575 | * False if the gate should be disabled.
|
---|
576 | */
|
---|
577 | PGMDECL(void) PGMR3PhysSetA20(PVM pVM, bool fEnable)
|
---|
578 | {
|
---|
579 | LogFlow(("PGMR3PhysSetA20 %d (was %d)\n", fEnable, pVM->pgm.s.fA20Enabled));
|
---|
580 | if (pVM->pgm.s.fA20Enabled != (RTUINT)fEnable)
|
---|
581 | {
|
---|
582 | pVM->pgm.s.fA20Enabled = fEnable;
|
---|
583 | pVM->pgm.s.GCPhysA20Mask = ~(RTGCPHYS)(!fEnable << 20);
|
---|
584 | REMR3A20Set(pVM, fEnable);
|
---|
585 | }
|
---|
586 | }
|
---|
587 |
|
---|
588 |
|
---|
589 | /*
|
---|
590 | * PGMR3PhysReadByte/Word/Dword
|
---|
591 | * PGMR3PhysWriteByte/Word/Dword
|
---|
592 | */
|
---|
593 |
|
---|
594 | #define PGMPHYSFN_READNAME PGMR3PhysReadByte
|
---|
595 | #define PGMPHYSFN_WRITENAME PGMR3PhysWriteByte
|
---|
596 | #define PGMPHYS_DATASIZE 1
|
---|
597 | #define PGMPHYS_DATATYPE uint8_t
|
---|
598 | #include "PGMPhys.h"
|
---|
599 |
|
---|
600 | #define PGMPHYSFN_READNAME PGMR3PhysReadWord
|
---|
601 | #define PGMPHYSFN_WRITENAME PGMR3PhysWriteWord
|
---|
602 | #define PGMPHYS_DATASIZE 2
|
---|
603 | #define PGMPHYS_DATATYPE uint16_t
|
---|
604 | #include "PGMPhys.h"
|
---|
605 |
|
---|
606 | #define PGMPHYSFN_READNAME PGMR3PhysReadDword
|
---|
607 | #define PGMPHYSFN_WRITENAME PGMR3PhysWriteDword
|
---|
608 | #define PGMPHYS_DATASIZE 4
|
---|
609 | #define PGMPHYS_DATATYPE uint32_t
|
---|
610 | #include "PGMPhys.h"
|
---|