1 | /* $Id: PGMPhys.cpp 16045 2009-01-19 16:27:59Z 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 */
|
---|
52 | DECLCALLBACK(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 | */
|
---|
93 | static 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 | */
|
---|
126 | static 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 | */
|
---|
157 | static 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 | */
|
---|
187 | VMMR3DECL(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 | */
|
---|
286 | int 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 | */
|
---|
406 | VMMR3DECL(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 | */
|
---|
532 | VMMR3DECL(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 | */
|
---|
604 | DECLINLINE(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 | */
|
---|
645 | VMMR3DECL(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 | */
|
---|
744 | VMMR3DECL(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 | */
|
---|
833 | VMMR3DECL(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 | */
|
---|
947 | VMMR3DECL(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 | */
|
---|
1020 | VMMR3DECL(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 | */
|
---|
1057 | VMMR3DECL(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 | * Maps a portion of an MMIO2 region into kernel space (host).
|
---|
1078 | *
|
---|
1079 | * The kernel mapping will become invalid when the MMIO2 memory is deregistered
|
---|
1080 | * or the VM is terminated.
|
---|
1081 | *
|
---|
1082 | * @return VBox status code.
|
---|
1083 | *
|
---|
1084 | * @param pVM Pointer to the shared VM structure.
|
---|
1085 | * @param pDevIns The device owning the MMIO2 memory.
|
---|
1086 | * @param iRegion The region.
|
---|
1087 | * @param off The offset into the region. Must be page aligned.
|
---|
1088 | * @param cb The number of bytes to map. Must be page aligned.
|
---|
1089 | * @param pszDesc Mapping description.
|
---|
1090 | * @param pR0Ptr Where to store the R0 address.
|
---|
1091 | */
|
---|
1092 | VMMR3DECL(int) PGMR3PhysMMIO2MapKernel(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS off, RTGCPHYS cb,
|
---|
1093 | const char *pszDesc, PRTR0PTR pR0Ptr)
|
---|
1094 | {
|
---|
1095 | /*
|
---|
1096 | * Validate input.
|
---|
1097 | */
|
---|
1098 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
1099 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
1100 | AssertReturn(iRegion <= UINT8_MAX, VERR_INVALID_PARAMETER);
|
---|
1101 |
|
---|
1102 | PPGMMMIO2RANGE pCur = pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion);
|
---|
1103 | AssertReturn(pCur, VERR_NOT_FOUND);
|
---|
1104 | AssertReturn(off < pCur->RamRange.cb, VERR_INVALID_PARAMETER);
|
---|
1105 | AssertReturn(cb <= pCur->RamRange.cb, VERR_INVALID_PARAMETER);
|
---|
1106 | AssertReturn(off + cb <= pCur->RamRange.cb, VERR_INVALID_PARAMETER);
|
---|
1107 |
|
---|
1108 | /*
|
---|
1109 | * Pass the request on to the support library/driver.
|
---|
1110 | */
|
---|
1111 | int rc = SUPR3PageMapKernel(pCur->pvR3, off, cb, 0, pR0Ptr);
|
---|
1112 |
|
---|
1113 | return rc;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 |
|
---|
1117 | /**
|
---|
1118 | * Registers a ROM image.
|
---|
1119 | *
|
---|
1120 | * Shadowed ROM images requires double the amount of backing memory, so,
|
---|
1121 | * don't use that unless you have to. Shadowing of ROM images is process
|
---|
1122 | * where we can select where the reads go and where the writes go. On real
|
---|
1123 | * hardware the chipset provides means to configure this. We provide
|
---|
1124 | * PGMR3PhysProtectROM() for this purpose.
|
---|
1125 | *
|
---|
1126 | * A read-only copy of the ROM image will always be kept around while we
|
---|
1127 | * will allocate RAM pages for the changes on demand (unless all memory
|
---|
1128 | * is configured to be preallocated).
|
---|
1129 | *
|
---|
1130 | * @returns VBox status.
|
---|
1131 | * @param pVM VM Handle.
|
---|
1132 | * @param pDevIns The device instance owning the ROM.
|
---|
1133 | * @param GCPhys First physical address in the range.
|
---|
1134 | * Must be page aligned!
|
---|
1135 | * @param cbRange The size of the range (in bytes).
|
---|
1136 | * Must be page aligned!
|
---|
1137 | * @param pvBinary Pointer to the binary data backing the ROM image.
|
---|
1138 | * This must be exactly \a cbRange in size.
|
---|
1139 | * @param fFlags Mask of flags. PGMPHYS_ROM_FLAG_SHADOWED
|
---|
1140 | * and/or PGMPHYS_ROM_FLAG_PERMANENT_BINARY.
|
---|
1141 | * @param pszDesc Pointer to description string. This must not be freed.
|
---|
1142 | *
|
---|
1143 | * @remark There is no way to remove the rom, automatically on device cleanup or
|
---|
1144 | * manually from the device yet. This isn't difficult in any way, it's
|
---|
1145 | * just not something we expect to be necessary for a while.
|
---|
1146 | */
|
---|
1147 | VMMR3DECL(int) PGMR3PhysRomRegister(PVM pVM, PPDMDEVINS pDevIns, RTGCPHYS GCPhys, RTGCPHYS cb,
|
---|
1148 | const void *pvBinary, uint32_t fFlags, const char *pszDesc)
|
---|
1149 | {
|
---|
1150 | Log(("PGMR3PhysRomRegister: pDevIns=%p GCPhys=%RGp(-%RGp) cb=%RGp pvBinary=%p fFlags=%#x pszDesc=%s\n",
|
---|
1151 | pDevIns, GCPhys, GCPhys + cb, cb, pvBinary, fFlags, pszDesc));
|
---|
1152 |
|
---|
1153 | /*
|
---|
1154 | * Validate input.
|
---|
1155 | */
|
---|
1156 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
1157 | AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
|
---|
1158 | AssertReturn(RT_ALIGN_T(cb, PAGE_SIZE, RTGCPHYS) == cb, VERR_INVALID_PARAMETER);
|
---|
1159 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
1160 | AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
|
---|
1161 | AssertPtrReturn(pvBinary, VERR_INVALID_PARAMETER);
|
---|
1162 | AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
|
---|
1163 | AssertReturn(!(fFlags & ~(PGMPHYS_ROM_FLAG_SHADOWED | PGMPHYS_ROM_FLAG_PERMANENT_BINARY)), VERR_INVALID_PARAMETER);
|
---|
1164 | VM_ASSERT_STATE_RETURN(pVM, VMSTATE_CREATING, VERR_VM_INVALID_VM_STATE);
|
---|
1165 |
|
---|
1166 | const uint32_t cPages = cb >> PAGE_SHIFT;
|
---|
1167 |
|
---|
1168 | /*
|
---|
1169 | * Find the ROM location in the ROM list first.
|
---|
1170 | */
|
---|
1171 | PPGMROMRANGE pRomPrev = NULL;
|
---|
1172 | PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3;
|
---|
1173 | while (pRom && GCPhysLast >= pRom->GCPhys)
|
---|
1174 | {
|
---|
1175 | if ( GCPhys <= pRom->GCPhysLast
|
---|
1176 | && GCPhysLast >= pRom->GCPhys)
|
---|
1177 | AssertLogRelMsgFailedReturn(("%RGp-%RGp (%s) conflicts with existing %RGp-%RGp (%s)\n",
|
---|
1178 | GCPhys, GCPhysLast, pszDesc,
|
---|
1179 | pRom->GCPhys, pRom->GCPhysLast, pRom->pszDesc),
|
---|
1180 | VERR_PGM_RAM_CONFLICT);
|
---|
1181 | /* next */
|
---|
1182 | pRomPrev = pRom;
|
---|
1183 | pRom = pRom->pNextR3;
|
---|
1184 | }
|
---|
1185 |
|
---|
1186 | /*
|
---|
1187 | * Find the RAM location and check for conflicts.
|
---|
1188 | *
|
---|
1189 | * Conflict detection is a bit different than for RAM
|
---|
1190 | * registration since a ROM can be located within a RAM
|
---|
1191 | * range. So, what we have to check for is other memory
|
---|
1192 | * types (other than RAM that is) and that we don't span
|
---|
1193 | * more than one RAM range (layz).
|
---|
1194 | */
|
---|
1195 | bool fRamExists = false;
|
---|
1196 | PPGMRAMRANGE pRamPrev = NULL;
|
---|
1197 | PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3;
|
---|
1198 | while (pRam && GCPhysLast >= pRam->GCPhys)
|
---|
1199 | {
|
---|
1200 | if ( GCPhys <= pRam->GCPhysLast
|
---|
1201 | && GCPhysLast >= pRam->GCPhys)
|
---|
1202 | {
|
---|
1203 | /* completely within? */
|
---|
1204 | AssertLogRelMsgReturn( GCPhys >= pRam->GCPhys
|
---|
1205 | && GCPhysLast <= pRam->GCPhysLast,
|
---|
1206 | ("%RGp-%RGp (%s) falls partly outside %RGp-%RGp (%s)\n",
|
---|
1207 | GCPhys, GCPhysLast, pszDesc,
|
---|
1208 | pRam->GCPhys, pRam->GCPhysLast, pRam->pszDesc),
|
---|
1209 | VERR_PGM_RAM_CONFLICT);
|
---|
1210 | fRamExists = true;
|
---|
1211 | break;
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | /* next */
|
---|
1215 | pRamPrev = pRam;
|
---|
1216 | pRam = pRam->pNextR3;
|
---|
1217 | }
|
---|
1218 | if (fRamExists)
|
---|
1219 | {
|
---|
1220 | PPGMPAGE pPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
1221 | uint32_t cPagesLeft = cPages;
|
---|
1222 | while (cPagesLeft-- > 0)
|
---|
1223 | {
|
---|
1224 | AssertLogRelMsgReturn(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM,
|
---|
1225 | ("%RGp isn't a RAM page (%d) - registering %RGp-%RGp (%s).\n",
|
---|
1226 | GCPhys, PGM_PAGE_GET_TYPE(pPage), GCPhys, GCPhysLast, pszDesc),
|
---|
1227 | VERR_PGM_RAM_CONFLICT);
|
---|
1228 | Assert(PGM_PAGE_IS_ZERO(pPage));
|
---|
1229 | pPage++;
|
---|
1230 | }
|
---|
1231 | }
|
---|
1232 |
|
---|
1233 | /*
|
---|
1234 | * Update the base memory reservation if necessary.
|
---|
1235 | */
|
---|
1236 | uint32_t cExtraBaseCost = fRamExists ? cPages : 0;
|
---|
1237 | if (fFlags & PGMPHYS_ROM_FLAG_SHADOWED)
|
---|
1238 | cExtraBaseCost += cPages;
|
---|
1239 | if (cExtraBaseCost)
|
---|
1240 | {
|
---|
1241 | int rc = MMR3IncreaseBaseReservation(pVM, cExtraBaseCost);
|
---|
1242 | if (RT_FAILURE(rc))
|
---|
1243 | return rc;
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | /*
|
---|
1247 | * Allocate memory for the virgin copy of the RAM.
|
---|
1248 | */
|
---|
1249 | PGMMALLOCATEPAGESREQ pReq;
|
---|
1250 | int rc = GMMR3AllocatePagesPrepare(pVM, &pReq, cPages, GMMACCOUNT_BASE);
|
---|
1251 | AssertRCReturn(rc, rc);
|
---|
1252 |
|
---|
1253 | for (uint32_t iPage = 0; iPage < cPages; iPage++)
|
---|
1254 | {
|
---|
1255 | pReq->aPages[iPage].HCPhysGCPhys = GCPhys + (iPage << PAGE_SHIFT);
|
---|
1256 | pReq->aPages[iPage].idPage = NIL_GMM_PAGEID;
|
---|
1257 | pReq->aPages[iPage].idSharedPage = NIL_GMM_PAGEID;
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | pgmLock(pVM);
|
---|
1261 | rc = GMMR3AllocatePagesPerform(pVM, pReq);
|
---|
1262 | pgmUnlock(pVM);
|
---|
1263 | if (RT_FAILURE(rc))
|
---|
1264 | {
|
---|
1265 | GMMR3AllocatePagesCleanup(pReq);
|
---|
1266 | return rc;
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | /*
|
---|
1270 | * Allocate the new ROM range and RAM range (if necessary).
|
---|
1271 | */
|
---|
1272 | PPGMROMRANGE pRomNew;
|
---|
1273 | rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMROMRANGE, aPages[cPages]), 0, MM_TAG_PGM_PHYS, (void **)&pRomNew);
|
---|
1274 | if (RT_SUCCESS(rc))
|
---|
1275 | {
|
---|
1276 | PPGMRAMRANGE pRamNew = NULL;
|
---|
1277 | if (!fRamExists)
|
---|
1278 | rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMRAMRANGE, aPages[cPages]), sizeof(PGMPAGE), MM_TAG_PGM_PHYS, (void **)&pRamNew);
|
---|
1279 | if (RT_SUCCESS(rc))
|
---|
1280 | {
|
---|
1281 | pgmLock(pVM);
|
---|
1282 |
|
---|
1283 | /*
|
---|
1284 | * Initialize and insert the RAM range (if required).
|
---|
1285 | */
|
---|
1286 | PPGMROMPAGE pRomPage = &pRomNew->aPages[0];
|
---|
1287 | if (!fRamExists)
|
---|
1288 | {
|
---|
1289 | pRamNew->GCPhys = GCPhys;
|
---|
1290 | pRamNew->GCPhysLast = GCPhysLast;
|
---|
1291 | pRamNew->pszDesc = pszDesc;
|
---|
1292 | pRamNew->cb = cb;
|
---|
1293 | pRamNew->fFlags = 0;
|
---|
1294 | pRamNew->pvR3 = NULL;
|
---|
1295 |
|
---|
1296 | PPGMPAGE pPage = &pRamNew->aPages[0];
|
---|
1297 | for (uint32_t iPage = 0; iPage < cPages; iPage++, pPage++, pRomPage++)
|
---|
1298 | {
|
---|
1299 | PGM_PAGE_INIT(pPage,
|
---|
1300 | pReq->aPages[iPage].HCPhysGCPhys,
|
---|
1301 | pReq->aPages[iPage].idPage,
|
---|
1302 | PGMPAGETYPE_ROM,
|
---|
1303 | PGM_PAGE_STATE_ALLOCATED);
|
---|
1304 |
|
---|
1305 | pRomPage->Virgin = *pPage;
|
---|
1306 | }
|
---|
1307 |
|
---|
1308 | pgmR3PhysLinkRamRange(pVM, pRamNew, pRamPrev);
|
---|
1309 | }
|
---|
1310 | else
|
---|
1311 | {
|
---|
1312 | PPGMPAGE pPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
1313 | for (uint32_t iPage = 0; iPage < cPages; iPage++, pPage++, pRomPage++)
|
---|
1314 | {
|
---|
1315 | PGM_PAGE_SET_TYPE(pPage, PGMPAGETYPE_ROM);
|
---|
1316 | PGM_PAGE_SET_HCPHYS(pPage, pReq->aPages[iPage].HCPhysGCPhys);
|
---|
1317 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
|
---|
1318 | PGM_PAGE_SET_PAGEID(pPage, pReq->aPages[iPage].idPage);
|
---|
1319 |
|
---|
1320 | pRomPage->Virgin = *pPage;
|
---|
1321 | }
|
---|
1322 |
|
---|
1323 | pRamNew = pRam;
|
---|
1324 | }
|
---|
1325 | pgmUnlock(pVM);
|
---|
1326 |
|
---|
1327 |
|
---|
1328 | /*
|
---|
1329 | * Register the write access handler for the range (PGMROMPROT_READ_ROM_WRITE_IGNORE).
|
---|
1330 | */
|
---|
1331 | rc = PGMR3HandlerPhysicalRegister(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE, GCPhys, GCPhysLast,
|
---|
1332 | #if 0 /** @todo we actually need a ring-3 write handler here for shadowed ROMs, so hack REM! */
|
---|
1333 | pgmR3PhysRomWriteHandler, pRomNew,
|
---|
1334 | #else
|
---|
1335 | NULL, NULL,
|
---|
1336 | #endif
|
---|
1337 | NULL, "pgmPhysRomWriteHandler", MMHyperCCToR0(pVM, pRomNew),
|
---|
1338 | NULL, "pgmPhysRomWriteHandler", MMHyperCCToRC(pVM, pRomNew), pszDesc);
|
---|
1339 | if (RT_SUCCESS(rc))
|
---|
1340 | {
|
---|
1341 | pgmLock(pVM);
|
---|
1342 |
|
---|
1343 | /*
|
---|
1344 | * Copy the image over to the virgin pages.
|
---|
1345 | * This must be done after linking in the RAM range.
|
---|
1346 | */
|
---|
1347 | PPGMPAGE pRamPage = &pRamNew->aPages[(GCPhys - pRamNew->GCPhys) >> PAGE_SHIFT];
|
---|
1348 | for (uint32_t iPage = 0; iPage < cPages; iPage++, pRamPage++)
|
---|
1349 | {
|
---|
1350 | void *pvDstPage;
|
---|
1351 | PPGMPAGEMAP pMapIgnored;
|
---|
1352 | rc = pgmPhysPageMap(pVM, pRamPage, GCPhys + (iPage << PAGE_SHIFT), &pMapIgnored, &pvDstPage);
|
---|
1353 | if (RT_FAILURE(rc))
|
---|
1354 | {
|
---|
1355 | VMSetError(pVM, rc, RT_SRC_POS, "Failed to map virgin ROM page at %RGp", GCPhys);
|
---|
1356 | break;
|
---|
1357 | }
|
---|
1358 | memcpy(pvDstPage, (const uint8_t *)pvBinary + (iPage << PAGE_SHIFT), PAGE_SIZE);
|
---|
1359 | }
|
---|
1360 | if (RT_SUCCESS(rc))
|
---|
1361 | {
|
---|
1362 | /*
|
---|
1363 | * Initialize the ROM range.
|
---|
1364 | * Note that the Virgin member of the pages has already been initialized above.
|
---|
1365 | */
|
---|
1366 | pRomNew->GCPhys = GCPhys;
|
---|
1367 | pRomNew->GCPhysLast = GCPhysLast;
|
---|
1368 | pRomNew->cb = cb;
|
---|
1369 | pRomNew->fFlags = fFlags;
|
---|
1370 | pRomNew->pvOriginal = fFlags & PGMPHYS_ROM_FLAG_PERMANENT_BINARY ? pvBinary : NULL;
|
---|
1371 | pRomNew->pszDesc = pszDesc;
|
---|
1372 |
|
---|
1373 | for (unsigned iPage = 0; iPage < cPages; iPage++)
|
---|
1374 | {
|
---|
1375 | PPGMROMPAGE pPage = &pRomNew->aPages[iPage];
|
---|
1376 | pPage->enmProt = PGMROMPROT_READ_ROM_WRITE_IGNORE;
|
---|
1377 | PGM_PAGE_INIT_ZERO_REAL(&pPage->Shadow, pVM, PGMPAGETYPE_ROM_SHADOW);
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | /*
|
---|
1381 | * Insert the ROM range, tell REM and return successfully.
|
---|
1382 | */
|
---|
1383 | pRomNew->pNextR3 = pRom;
|
---|
1384 | pRomNew->pNextR0 = pRom ? MMHyperCCToR0(pVM, pRom) : NIL_RTR0PTR;
|
---|
1385 | pRomNew->pNextRC = pRom ? MMHyperCCToRC(pVM, pRom) : NIL_RTRCPTR;
|
---|
1386 |
|
---|
1387 | if (pRomPrev)
|
---|
1388 | {
|
---|
1389 | pRomPrev->pNextR3 = pRomNew;
|
---|
1390 | pRomPrev->pNextR0 = MMHyperCCToR0(pVM, pRomNew);
|
---|
1391 | pRomPrev->pNextRC = MMHyperCCToRC(pVM, pRomNew);
|
---|
1392 | }
|
---|
1393 | else
|
---|
1394 | {
|
---|
1395 | pVM->pgm.s.pRomRangesR3 = pRomNew;
|
---|
1396 | pVM->pgm.s.pRomRangesR0 = MMHyperCCToR0(pVM, pRomNew);
|
---|
1397 | pVM->pgm.s.pRomRangesRC = MMHyperCCToRC(pVM, pRomNew);
|
---|
1398 | }
|
---|
1399 |
|
---|
1400 | REMR3NotifyPhysRomRegister(pVM, GCPhys, cb, NULL, false); /** @todo fix shadowing and REM. */
|
---|
1401 |
|
---|
1402 | GMMR3AllocatePagesCleanup(pReq);
|
---|
1403 | pgmUnlock(pVM);
|
---|
1404 | return VINF_SUCCESS;
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | /* bail out */
|
---|
1408 |
|
---|
1409 | pgmUnlock(pVM);
|
---|
1410 | int rc2 = PGMHandlerPhysicalDeregister(pVM, GCPhys);
|
---|
1411 | AssertRC(rc2);
|
---|
1412 | pgmLock(pVM);
|
---|
1413 | }
|
---|
1414 |
|
---|
1415 | pgmR3PhysUnlinkRamRange2(pVM, pRamNew, pRamPrev);
|
---|
1416 | if (pRamNew)
|
---|
1417 | MMHyperFree(pVM, pRamNew);
|
---|
1418 | }
|
---|
1419 | MMHyperFree(pVM, pRomNew);
|
---|
1420 | }
|
---|
1421 |
|
---|
1422 | /** @todo Purge the mapping cache or something... */
|
---|
1423 | GMMR3FreeAllocatedPages(pVM, pReq);
|
---|
1424 | GMMR3AllocatePagesCleanup(pReq);
|
---|
1425 | pgmUnlock(pVM);
|
---|
1426 | return rc;
|
---|
1427 | }
|
---|
1428 |
|
---|
1429 |
|
---|
1430 | /**
|
---|
1431 | * \#PF Handler callback for ROM write accesses.
|
---|
1432 | *
|
---|
1433 | * @returns VINF_SUCCESS if the handler have carried out the operation.
|
---|
1434 | * @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
|
---|
1435 | * @param pVM VM Handle.
|
---|
1436 | * @param GCPhys The physical address the guest is writing to.
|
---|
1437 | * @param pvPhys The HC mapping of that address.
|
---|
1438 | * @param pvBuf What the guest is reading/writing.
|
---|
1439 | * @param cbBuf How much it's reading/writing.
|
---|
1440 | * @param enmAccessType The access type.
|
---|
1441 | * @param pvUser User argument.
|
---|
1442 | */
|
---|
1443 | /*static - shut up warning */
|
---|
1444 | DECLCALLBACK(int) pgmR3PhysRomWriteHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser)
|
---|
1445 | {
|
---|
1446 | PPGMROMRANGE pRom = (PPGMROMRANGE)pvUser;
|
---|
1447 | const uint32_t iPage = GCPhys - pRom->GCPhys;
|
---|
1448 | Assert(iPage < (pRom->cb >> PAGE_SHIFT));
|
---|
1449 | PPGMROMPAGE pRomPage = &pRom->aPages[iPage];
|
---|
1450 | switch (pRomPage->enmProt)
|
---|
1451 | {
|
---|
1452 | /*
|
---|
1453 | * Ignore.
|
---|
1454 | */
|
---|
1455 | case PGMROMPROT_READ_ROM_WRITE_IGNORE:
|
---|
1456 | case PGMROMPROT_READ_RAM_WRITE_IGNORE:
|
---|
1457 | return VINF_SUCCESS;
|
---|
1458 |
|
---|
1459 | /*
|
---|
1460 | * Write to the ram page.
|
---|
1461 | */
|
---|
1462 | case PGMROMPROT_READ_ROM_WRITE_RAM:
|
---|
1463 | case PGMROMPROT_READ_RAM_WRITE_RAM: /* yes this will get here too, it's *way* simpler that way. */
|
---|
1464 | {
|
---|
1465 | /* This should be impossible now, pvPhys doesn't work cross page anylonger. */
|
---|
1466 | Assert(((GCPhys - pRom->GCPhys + cbBuf - 1) >> PAGE_SHIFT) == iPage);
|
---|
1467 |
|
---|
1468 | /*
|
---|
1469 | * Take the lock, do lazy allocation, map the page and copy the data.
|
---|
1470 | *
|
---|
1471 | * Note that we have to bypass the mapping TLB since it works on
|
---|
1472 | * guest physical addresses and entering the shadow page would
|
---|
1473 | * kind of screw things up...
|
---|
1474 | */
|
---|
1475 | int rc = pgmLock(pVM);
|
---|
1476 | AssertRC(rc);
|
---|
1477 |
|
---|
1478 | if (RT_UNLIKELY(PGM_PAGE_GET_STATE(&pRomPage->Shadow) != PGM_PAGE_STATE_ALLOCATED))
|
---|
1479 | {
|
---|
1480 | rc = pgmPhysPageMakeWritable(pVM, &pRomPage->Shadow, GCPhys);
|
---|
1481 | if (RT_FAILURE(rc))
|
---|
1482 | {
|
---|
1483 | pgmUnlock(pVM);
|
---|
1484 | return rc;
|
---|
1485 | }
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | void *pvDstPage;
|
---|
1489 | PPGMPAGEMAP pMapIgnored;
|
---|
1490 | rc = pgmPhysPageMap(pVM, &pRomPage->Shadow, GCPhys & X86_PTE_PG_MASK, &pMapIgnored, &pvDstPage);
|
---|
1491 | if (RT_SUCCESS(rc))
|
---|
1492 | memcpy((uint8_t *)pvDstPage + (GCPhys & PAGE_OFFSET_MASK), pvBuf, cbBuf);
|
---|
1493 |
|
---|
1494 | pgmUnlock(pVM);
|
---|
1495 | return rc;
|
---|
1496 | }
|
---|
1497 |
|
---|
1498 | default:
|
---|
1499 | AssertMsgFailedReturn(("enmProt=%d iPage=%d GCPhys=%RGp\n",
|
---|
1500 | pRom->aPages[iPage].enmProt, iPage, GCPhys),
|
---|
1501 | VERR_INTERNAL_ERROR);
|
---|
1502 | }
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 |
|
---|
1506 |
|
---|
1507 | /**
|
---|
1508 | * Called by PGMR3Reset to reset the shadow, switch to the virgin,
|
---|
1509 | * and verify that the virgin part is untouched.
|
---|
1510 | *
|
---|
1511 | * This is done after the normal memory has been cleared.
|
---|
1512 | *
|
---|
1513 | * ASSUMES that the caller owns the PGM lock.
|
---|
1514 | *
|
---|
1515 | * @param pVM The VM handle.
|
---|
1516 | */
|
---|
1517 | int pgmR3PhysRomReset(PVM pVM)
|
---|
1518 | {
|
---|
1519 | for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
|
---|
1520 | {
|
---|
1521 | const uint32_t cPages = pRom->cb >> PAGE_SHIFT;
|
---|
1522 |
|
---|
1523 | if (pRom->fFlags & PGMPHYS_ROM_FLAG_SHADOWED)
|
---|
1524 | {
|
---|
1525 | /*
|
---|
1526 | * Reset the physical handler.
|
---|
1527 | */
|
---|
1528 | int rc = PGMR3PhysRomProtect(pVM, pRom->GCPhys, pRom->cb, PGMROMPROT_READ_ROM_WRITE_IGNORE);
|
---|
1529 | AssertRCReturn(rc, rc);
|
---|
1530 |
|
---|
1531 | /*
|
---|
1532 | * What we do with the shadow pages depends on the memory
|
---|
1533 | * preallocation option. If not enabled, we'll just throw
|
---|
1534 | * out all the dirty pages and replace them by the zero page.
|
---|
1535 | */
|
---|
1536 | if (1)///@todo !pVM->pgm.f.fRamPreAlloc)
|
---|
1537 | {
|
---|
1538 | /* Count dirty shadow pages. */
|
---|
1539 | uint32_t cDirty = 0;
|
---|
1540 | uint32_t iPage = cPages;
|
---|
1541 | while (iPage-- > 0)
|
---|
1542 | if (PGM_PAGE_GET_STATE(&pRom->aPages[iPage].Shadow) != PGM_PAGE_STATE_ZERO)
|
---|
1543 | cDirty++;
|
---|
1544 | if (cDirty)
|
---|
1545 | {
|
---|
1546 | /* Free the dirty pages. */
|
---|
1547 | PGMMFREEPAGESREQ pReq;
|
---|
1548 | rc = GMMR3FreePagesPrepare(pVM, &pReq, cDirty, GMMACCOUNT_BASE);
|
---|
1549 | AssertRCReturn(rc, rc);
|
---|
1550 |
|
---|
1551 | uint32_t iReqPage = 0;
|
---|
1552 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
1553 | if (PGM_PAGE_GET_STATE(&pRom->aPages[iPage].Shadow) != PGM_PAGE_STATE_ZERO)
|
---|
1554 | {
|
---|
1555 | pReq->aPages[iReqPage].idPage = PGM_PAGE_GET_PAGEID(&pRom->aPages[iPage].Shadow);
|
---|
1556 | iReqPage++;
|
---|
1557 | }
|
---|
1558 |
|
---|
1559 | rc = GMMR3FreePagesPerform(pVM, pReq);
|
---|
1560 | GMMR3FreePagesCleanup(pReq);
|
---|
1561 | AssertRCReturn(rc, rc);
|
---|
1562 |
|
---|
1563 | /* setup the zero page. */
|
---|
1564 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
1565 | if (PGM_PAGE_GET_STATE(&pRom->aPages[iPage].Shadow) != PGM_PAGE_STATE_ZERO)
|
---|
1566 | PGM_PAGE_INIT_ZERO_REAL(&pRom->aPages[iPage].Shadow, pVM, PGMPAGETYPE_ROM_SHADOW);
|
---|
1567 | }
|
---|
1568 | }
|
---|
1569 | else
|
---|
1570 | {
|
---|
1571 | /* clear all the pages. */
|
---|
1572 | for (uint32_t iPage = 0; iPage < cPages; iPage++)
|
---|
1573 | {
|
---|
1574 | const RTGCPHYS GCPhys = pRom->GCPhys + (iPage << PAGE_SHIFT);
|
---|
1575 | rc = pgmPhysPageMakeWritable(pVM, &pRom->aPages[iPage].Shadow, GCPhys);
|
---|
1576 | if (RT_FAILURE(rc))
|
---|
1577 | break;
|
---|
1578 |
|
---|
1579 | void *pvDstPage;
|
---|
1580 | PPGMPAGEMAP pMapIgnored;
|
---|
1581 | rc = pgmPhysPageMap(pVM, &pRom->aPages[iPage].Shadow, GCPhys, &pMapIgnored, &pvDstPage);
|
---|
1582 | if (RT_FAILURE(rc))
|
---|
1583 | break;
|
---|
1584 | ASMMemZeroPage(pvDstPage);
|
---|
1585 | }
|
---|
1586 | AssertRCReturn(rc, rc);
|
---|
1587 | }
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | #ifdef VBOX_STRICT
|
---|
1591 | /*
|
---|
1592 | * Verify that the virgin page is unchanged if possible.
|
---|
1593 | */
|
---|
1594 | if (pRom->pvOriginal)
|
---|
1595 | {
|
---|
1596 | uint8_t const *pbSrcPage = (uint8_t const *)pRom->pvOriginal;
|
---|
1597 | for (uint32_t iPage = 0; iPage < cPages; iPage++, pbSrcPage += PAGE_SIZE)
|
---|
1598 | {
|
---|
1599 | const RTGCPHYS GCPhys = pRom->GCPhys + (iPage << PAGE_SHIFT);
|
---|
1600 | PPGMPAGEMAP pMapIgnored;
|
---|
1601 | void *pvDstPage;
|
---|
1602 | int rc = pgmPhysPageMap(pVM, &pRom->aPages[iPage].Virgin, GCPhys, &pMapIgnored, &pvDstPage);
|
---|
1603 | if (RT_FAILURE(rc))
|
---|
1604 | break;
|
---|
1605 | if (memcmp(pvDstPage, pbSrcPage, PAGE_SIZE))
|
---|
1606 | LogRel(("pgmR3PhysRomReset: %RGp rom page changed (%s) - loaded saved state?\n",
|
---|
1607 | GCPhys, pRom->pszDesc));
|
---|
1608 | }
|
---|
1609 | }
|
---|
1610 | #endif
|
---|
1611 | }
|
---|
1612 |
|
---|
1613 | return VINF_SUCCESS;
|
---|
1614 | }
|
---|
1615 |
|
---|
1616 |
|
---|
1617 | /**
|
---|
1618 | * Change the shadowing of a range of ROM pages.
|
---|
1619 | *
|
---|
1620 | * This is intended for implementing chipset specific memory registers
|
---|
1621 | * and will not be very strict about the input. It will silently ignore
|
---|
1622 | * any pages that are not the part of a shadowed ROM.
|
---|
1623 | *
|
---|
1624 | * @returns VBox status code.
|
---|
1625 | * @param pVM Pointer to the shared VM structure.
|
---|
1626 | * @param GCPhys Where to start. Page aligned.
|
---|
1627 | * @param cb How much to change. Page aligned.
|
---|
1628 | * @param enmProt The new ROM protection.
|
---|
1629 | */
|
---|
1630 | VMMR3DECL(int) PGMR3PhysRomProtect(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, PGMROMPROT enmProt)
|
---|
1631 | {
|
---|
1632 | /*
|
---|
1633 | * Check input
|
---|
1634 | */
|
---|
1635 | if (!cb)
|
---|
1636 | return VINF_SUCCESS;
|
---|
1637 | AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
1638 | AssertReturn(!(cb & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
1639 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
1640 | AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
|
---|
1641 | AssertReturn(enmProt >= PGMROMPROT_INVALID && enmProt <= PGMROMPROT_END, VERR_INVALID_PARAMETER);
|
---|
1642 |
|
---|
1643 | /*
|
---|
1644 | * Process the request.
|
---|
1645 | */
|
---|
1646 | bool fFlushedPool = false;
|
---|
1647 | for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
|
---|
1648 | if ( GCPhys <= pRom->GCPhysLast
|
---|
1649 | && GCPhysLast >= pRom->GCPhys)
|
---|
1650 | {
|
---|
1651 | /*
|
---|
1652 | * Iterate the relevant pages and the ncessary make changes.
|
---|
1653 | */
|
---|
1654 | bool fChanges = false;
|
---|
1655 | uint32_t const cPages = pRom->GCPhysLast > GCPhysLast
|
---|
1656 | ? pRom->cb >> PAGE_SHIFT
|
---|
1657 | : (GCPhysLast - pRom->GCPhys) >> PAGE_SHIFT;
|
---|
1658 | for (uint32_t iPage = (GCPhys - pRom->GCPhys) >> PAGE_SHIFT;
|
---|
1659 | iPage < cPages;
|
---|
1660 | iPage++)
|
---|
1661 | {
|
---|
1662 | PPGMROMPAGE pRomPage = &pRom->aPages[iPage];
|
---|
1663 | if (PGMROMPROT_IS_ROM(pRomPage->enmProt) != PGMROMPROT_IS_ROM(enmProt))
|
---|
1664 | {
|
---|
1665 | fChanges = true;
|
---|
1666 |
|
---|
1667 | /* flush the page pool first so we don't leave any usage references dangling. */
|
---|
1668 | if (!fFlushedPool)
|
---|
1669 | {
|
---|
1670 | pgmPoolFlushAll(pVM);
|
---|
1671 | fFlushedPool = true;
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 | PPGMPAGE pOld = PGMROMPROT_IS_ROM(pRomPage->enmProt) ? &pRomPage->Virgin : &pRomPage->Shadow;
|
---|
1675 | PPGMPAGE pNew = PGMROMPROT_IS_ROM(pRomPage->enmProt) ? &pRomPage->Shadow : &pRomPage->Virgin;
|
---|
1676 | PPGMPAGE pRamPage = pgmPhysGetPage(&pVM->pgm.s, pRom->GCPhys + (iPage << PAGE_SHIFT));
|
---|
1677 |
|
---|
1678 | *pOld = *pRamPage;
|
---|
1679 | *pRamPage = *pNew;
|
---|
1680 | /** @todo preserve the volatile flags (handlers) when these have been moved out of HCPhys! */
|
---|
1681 | }
|
---|
1682 | }
|
---|
1683 |
|
---|
1684 | /*
|
---|
1685 | * Reset the access handler if we made changes, no need
|
---|
1686 | * to optimize this.
|
---|
1687 | */
|
---|
1688 | if (fChanges)
|
---|
1689 | {
|
---|
1690 | int rc = PGMHandlerPhysicalReset(pVM, pRom->GCPhys);
|
---|
1691 | AssertRCReturn(rc, rc);
|
---|
1692 | }
|
---|
1693 |
|
---|
1694 | /* Advance - cb isn't updated. */
|
---|
1695 | GCPhys = pRom->GCPhys + (cPages << PAGE_SHIFT);
|
---|
1696 | }
|
---|
1697 |
|
---|
1698 | return VINF_SUCCESS;
|
---|
1699 | }
|
---|
1700 |
|
---|
1701 |
|
---|
1702 | /**
|
---|
1703 | * Interface that the MMR3RamRegister(), MMR3RomRegister() and MMIO handler
|
---|
1704 | * registration APIs calls to inform PGM about memory registrations.
|
---|
1705 | *
|
---|
1706 | * It registers the physical memory range with PGM. MM is responsible
|
---|
1707 | * for the toplevel things - allocation and locking - while PGM is taking
|
---|
1708 | * care of all the details and implements the physical address space virtualization.
|
---|
1709 | *
|
---|
1710 | * @returns VBox status.
|
---|
1711 | * @param pVM The VM handle.
|
---|
1712 | * @param pvRam HC virtual address of the RAM range. (page aligned)
|
---|
1713 | * @param GCPhys GC physical address of the RAM range. (page aligned)
|
---|
1714 | * @param cb Size of the RAM range. (page aligned)
|
---|
1715 | * @param fFlags Flags, MM_RAM_*.
|
---|
1716 | * @param paPages Pointer an array of physical page descriptors.
|
---|
1717 | * @param pszDesc Description string.
|
---|
1718 | */
|
---|
1719 | VMMR3DECL(int) PGMR3PhysRegister(PVM pVM, void *pvRam, RTGCPHYS GCPhys, size_t cb, unsigned fFlags, const SUPPAGE *paPages, const char *pszDesc)
|
---|
1720 | {
|
---|
1721 | /*
|
---|
1722 | * Validate input.
|
---|
1723 | * (Not so important because callers are only MMR3PhysRegister()
|
---|
1724 | * and PGMR3HandlerPhysicalRegisterEx(), but anyway...)
|
---|
1725 | */
|
---|
1726 | Log(("PGMR3PhysRegister %08X %x bytes flags %x %s\n", GCPhys, cb, fFlags, pszDesc));
|
---|
1727 |
|
---|
1728 | Assert((fFlags & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_DYNAMIC_ALLOC)) || paPages);
|
---|
1729 | /*Assert(!(fFlags & MM_RAM_FLAGS_RESERVED) || !paPages);*/
|
---|
1730 | Assert((fFlags == (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO)) || (fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC) || pvRam);
|
---|
1731 | /*Assert(!(fFlags & MM_RAM_FLAGS_RESERVED) || !pvRam);*/
|
---|
1732 | Assert(!(fFlags & ~0xfff));
|
---|
1733 | Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb && cb);
|
---|
1734 | Assert(RT_ALIGN_P(pvRam, PAGE_SIZE) == pvRam);
|
---|
1735 | Assert(!(fFlags & ~(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2 | MM_RAM_FLAGS_DYNAMIC_ALLOC)));
|
---|
1736 | Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
|
---|
1737 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
1738 | if (GCPhysLast < GCPhys)
|
---|
1739 | {
|
---|
1740 | AssertMsgFailed(("The range wraps! GCPhys=%RGp cb=%#x\n", GCPhys, cb));
|
---|
1741 | return VERR_INVALID_PARAMETER;
|
---|
1742 | }
|
---|
1743 |
|
---|
1744 | /*
|
---|
1745 | * Find range location and check for conflicts.
|
---|
1746 | */
|
---|
1747 | PPGMRAMRANGE pPrev = NULL;
|
---|
1748 | PPGMRAMRANGE pCur = pVM->pgm.s.pRamRangesR3;
|
---|
1749 | while (pCur)
|
---|
1750 | {
|
---|
1751 | if (GCPhys <= pCur->GCPhysLast && GCPhysLast >= pCur->GCPhys)
|
---|
1752 | {
|
---|
1753 | AssertMsgFailed(("Conflict! This cannot happen!\n"));
|
---|
1754 | return VERR_PGM_RAM_CONFLICT;
|
---|
1755 | }
|
---|
1756 | if (GCPhysLast < pCur->GCPhys)
|
---|
1757 | break;
|
---|
1758 |
|
---|
1759 | /* next */
|
---|
1760 | pPrev = pCur;
|
---|
1761 | pCur = pCur->pNextR3;
|
---|
1762 | }
|
---|
1763 |
|
---|
1764 | /*
|
---|
1765 | * Allocate RAM range.
|
---|
1766 | * Small ranges are allocated from the heap, big ones have separate mappings.
|
---|
1767 | */
|
---|
1768 | size_t cbRam = RT_OFFSETOF(PGMRAMRANGE, aPages[cb >> PAGE_SHIFT]);
|
---|
1769 | PPGMRAMRANGE pNew;
|
---|
1770 | int rc = VERR_NO_MEMORY;
|
---|
1771 | if (cbRam > PAGE_SIZE / 2)
|
---|
1772 | { /* large */
|
---|
1773 | cbRam = RT_ALIGN_Z(cbRam, PAGE_SIZE);
|
---|
1774 | rc = MMR3HyperAllocOnceNoRel(pVM, cbRam, PAGE_SIZE, MM_TAG_PGM_PHYS, (void **)&pNew);
|
---|
1775 | AssertMsgRC(rc, ("MMR3HyperAllocOnceNoRel(,%#x,,) -> %Rrc\n", cbRam, rc));
|
---|
1776 | }
|
---|
1777 | else
|
---|
1778 | { /* small */
|
---|
1779 | rc = MMHyperAlloc(pVM, cbRam, 16, MM_TAG_PGM, (void **)&pNew);
|
---|
1780 | AssertMsgRC(rc, ("MMHyperAlloc(,%#x,,,) -> %Rrc\n", cbRam, rc));
|
---|
1781 | }
|
---|
1782 | if (RT_SUCCESS(rc))
|
---|
1783 | {
|
---|
1784 | /*
|
---|
1785 | * Initialize the range.
|
---|
1786 | */
|
---|
1787 | pNew->pvR3 = pvRam;
|
---|
1788 | pNew->GCPhys = GCPhys;
|
---|
1789 | pNew->GCPhysLast = GCPhysLast;
|
---|
1790 | pNew->cb = cb;
|
---|
1791 | pNew->fFlags = fFlags;
|
---|
1792 | pNew->paChunkR3Ptrs = NULL;
|
---|
1793 |
|
---|
1794 | unsigned iPage = (unsigned)(cb >> PAGE_SHIFT);
|
---|
1795 | if (paPages)
|
---|
1796 | {
|
---|
1797 | while (iPage-- > 0)
|
---|
1798 | {
|
---|
1799 | PGM_PAGE_INIT(&pNew->aPages[iPage], paPages[iPage].Phys & X86_PTE_PAE_PG_MASK, NIL_GMM_PAGEID,
|
---|
1800 | fFlags & MM_RAM_FLAGS_MMIO2 ? PGMPAGETYPE_MMIO2 : PGMPAGETYPE_RAM,
|
---|
1801 | PGM_PAGE_STATE_ALLOCATED);
|
---|
1802 | pNew->aPages[iPage].HCPhys |= fFlags; /** @todo PAGE FLAGS*/
|
---|
1803 | }
|
---|
1804 | }
|
---|
1805 | else if (fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
|
---|
1806 | {
|
---|
1807 | /* Allocate memory for chunk to HC ptr lookup array. */
|
---|
1808 | rc = MMHyperAlloc(pVM, (cb >> PGM_DYNAMIC_CHUNK_SHIFT) * sizeof(void *), 16, MM_TAG_PGM, (void **)&pNew->paChunkR3Ptrs);
|
---|
1809 | AssertMsgReturn(rc == VINF_SUCCESS, ("MMHyperAlloc(,%#x,,,) -> %Rrc\n", cbRam, cb), rc);
|
---|
1810 |
|
---|
1811 | /* Physical memory will be allocated on demand. */
|
---|
1812 | while (iPage-- > 0)
|
---|
1813 | {
|
---|
1814 | PGM_PAGE_INIT(&pNew->aPages[iPage], 0, NIL_GMM_PAGEID, PGMPAGETYPE_RAM, PGM_PAGE_STATE_ZERO);
|
---|
1815 | pNew->aPages[iPage].HCPhys = fFlags; /** @todo PAGE FLAGS */
|
---|
1816 | }
|
---|
1817 | }
|
---|
1818 | else
|
---|
1819 | {
|
---|
1820 | Assert(fFlags == (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO));
|
---|
1821 | RTHCPHYS HCPhysDummyPage = MMR3PageDummyHCPhys(pVM);
|
---|
1822 | while (iPage-- > 0)
|
---|
1823 | {
|
---|
1824 | PGM_PAGE_INIT(&pNew->aPages[iPage], HCPhysDummyPage, NIL_GMM_PAGEID, PGMPAGETYPE_MMIO, PGM_PAGE_STATE_ZERO);
|
---|
1825 | pNew->aPages[iPage].HCPhys |= fFlags; /** @todo PAGE FLAGS*/
|
---|
1826 | }
|
---|
1827 | }
|
---|
1828 |
|
---|
1829 | /*
|
---|
1830 | * Insert the new RAM range.
|
---|
1831 | */
|
---|
1832 | pgmLock(pVM);
|
---|
1833 | pNew->pNextR3 = pCur;
|
---|
1834 | pNew->pNextR0 = pCur ? MMHyperCCToR0(pVM, pCur) : NIL_RTR0PTR;
|
---|
1835 | pNew->pNextRC = pCur ? MMHyperCCToRC(pVM, pCur) : NIL_RTRCPTR;
|
---|
1836 | if (pPrev)
|
---|
1837 | {
|
---|
1838 | pPrev->pNextR3 = pNew;
|
---|
1839 | pPrev->pNextR0 = MMHyperCCToR0(pVM, pNew);
|
---|
1840 | pPrev->pNextRC = MMHyperCCToRC(pVM, pNew);
|
---|
1841 | }
|
---|
1842 | else
|
---|
1843 | {
|
---|
1844 | pVM->pgm.s.pRamRangesR3 = pNew;
|
---|
1845 | pVM->pgm.s.pRamRangesR0 = MMHyperCCToR0(pVM, pNew);
|
---|
1846 | pVM->pgm.s.pRamRangesRC = MMHyperCCToRC(pVM, pNew);
|
---|
1847 | }
|
---|
1848 | pgmUnlock(pVM);
|
---|
1849 | }
|
---|
1850 | return rc;
|
---|
1851 | }
|
---|
1852 |
|
---|
1853 | #ifndef VBOX_WITH_NEW_PHYS_CODE
|
---|
1854 |
|
---|
1855 | /**
|
---|
1856 | * Register a chunk of a the physical memory range with PGM. MM is responsible
|
---|
1857 | * for the toplevel things - allocation and locking - while PGM is taking
|
---|
1858 | * care of all the details and implements the physical address space virtualization.
|
---|
1859 | *
|
---|
1860 | *
|
---|
1861 | * @returns VBox status.
|
---|
1862 | * @param pVM The VM handle.
|
---|
1863 | * @param pvRam HC virtual address of the RAM range. (page aligned)
|
---|
1864 | * @param GCPhys GC physical address of the RAM range. (page aligned)
|
---|
1865 | * @param cb Size of the RAM range. (page aligned)
|
---|
1866 | * @param fFlags Flags, MM_RAM_*.
|
---|
1867 | * @param paPages Pointer an array of physical page descriptors.
|
---|
1868 | * @param pszDesc Description string.
|
---|
1869 | */
|
---|
1870 | VMMR3DECL(int) PGMR3PhysRegisterChunk(PVM pVM, void *pvRam, RTGCPHYS GCPhys, size_t cb, unsigned fFlags, const SUPPAGE *paPages, const char *pszDesc)
|
---|
1871 | {
|
---|
1872 | NOREF(pszDesc);
|
---|
1873 |
|
---|
1874 | /*
|
---|
1875 | * Validate input.
|
---|
1876 | * (Not so important because callers are only MMR3PhysRegister()
|
---|
1877 | * and PGMR3HandlerPhysicalRegisterEx(), but anyway...)
|
---|
1878 | */
|
---|
1879 | Log(("PGMR3PhysRegisterChunk %08X %x bytes flags %x %s\n", GCPhys, cb, fFlags, pszDesc));
|
---|
1880 |
|
---|
1881 | Assert(paPages);
|
---|
1882 | Assert(pvRam);
|
---|
1883 | Assert(!(fFlags & ~0xfff));
|
---|
1884 | Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb && cb);
|
---|
1885 | Assert(RT_ALIGN_P(pvRam, PAGE_SIZE) == pvRam);
|
---|
1886 | Assert(!(fFlags & ~(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2 | MM_RAM_FLAGS_DYNAMIC_ALLOC)));
|
---|
1887 | Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
|
---|
1888 | Assert(VM_IS_EMT(pVM));
|
---|
1889 | Assert(!(GCPhys & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
|
---|
1890 | Assert(cb == PGM_DYNAMIC_CHUNK_SIZE);
|
---|
1891 |
|
---|
1892 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
1893 | if (GCPhysLast < GCPhys)
|
---|
1894 | {
|
---|
1895 | AssertMsgFailed(("The range wraps! GCPhys=%RGp cb=%#x\n", GCPhys, cb));
|
---|
1896 | return VERR_INVALID_PARAMETER;
|
---|
1897 | }
|
---|
1898 |
|
---|
1899 | /*
|
---|
1900 | * Find existing range location.
|
---|
1901 | */
|
---|
1902 | PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
1903 | while (pRam)
|
---|
1904 | {
|
---|
1905 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
1906 | if ( off < pRam->cb
|
---|
1907 | && (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC))
|
---|
1908 | break;
|
---|
1909 |
|
---|
1910 | pRam = pRam->CTX_SUFF(pNext);
|
---|
1911 | }
|
---|
1912 | AssertReturn(pRam, VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS);
|
---|
1913 |
|
---|
1914 | unsigned off = (GCPhys - pRam->GCPhys) >> PAGE_SHIFT;
|
---|
1915 | unsigned iPage = (unsigned)(cb >> PAGE_SHIFT);
|
---|
1916 | if (paPages)
|
---|
1917 | {
|
---|
1918 | while (iPage-- > 0)
|
---|
1919 | pRam->aPages[off + iPage].HCPhys = (paPages[iPage].Phys & X86_PTE_PAE_PG_MASK) | fFlags; /** @todo PAGE FLAGS */
|
---|
1920 | }
|
---|
1921 | off >>= (PGM_DYNAMIC_CHUNK_SHIFT - PAGE_SHIFT);
|
---|
1922 | pRam->paChunkR3Ptrs[off] = (uintptr_t)pvRam;
|
---|
1923 |
|
---|
1924 | /* Notify the recompiler. */
|
---|
1925 | REMR3NotifyPhysRamChunkRegister(pVM, GCPhys, PGM_DYNAMIC_CHUNK_SIZE, (RTHCUINTPTR)pvRam, fFlags);
|
---|
1926 |
|
---|
1927 | return VINF_SUCCESS;
|
---|
1928 | }
|
---|
1929 |
|
---|
1930 |
|
---|
1931 | /**
|
---|
1932 | * Allocate missing physical pages for an existing guest RAM range.
|
---|
1933 | *
|
---|
1934 | * @returns VBox status.
|
---|
1935 | * @param pVM The VM handle.
|
---|
1936 | * @param GCPhys GC physical address of the RAM range. (page aligned)
|
---|
1937 | */
|
---|
1938 | VMMR3DECL(int) PGM3PhysGrowRange(PVM pVM, PCRTGCPHYS pGCPhys)
|
---|
1939 | {
|
---|
1940 | RTGCPHYS GCPhys = *pGCPhys;
|
---|
1941 |
|
---|
1942 | /*
|
---|
1943 | * Walk range list.
|
---|
1944 | */
|
---|
1945 | pgmLock(pVM);
|
---|
1946 |
|
---|
1947 | PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
1948 | while (pRam)
|
---|
1949 | {
|
---|
1950 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
1951 | if ( off < pRam->cb
|
---|
1952 | && (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC))
|
---|
1953 | {
|
---|
1954 | bool fRangeExists = false;
|
---|
1955 | unsigned off = (GCPhys - pRam->GCPhys) >> PGM_DYNAMIC_CHUNK_SHIFT;
|
---|
1956 |
|
---|
1957 | /* Note: A request made from another thread may end up in EMT after somebody else has already allocated the range. */
|
---|
1958 | if (pRam->paChunkR3Ptrs[off])
|
---|
1959 | fRangeExists = true;
|
---|
1960 |
|
---|
1961 | pgmUnlock(pVM);
|
---|
1962 | if (fRangeExists)
|
---|
1963 | return VINF_SUCCESS;
|
---|
1964 | return pgmr3PhysGrowRange(pVM, GCPhys);
|
---|
1965 | }
|
---|
1966 |
|
---|
1967 | pRam = pRam->CTX_SUFF(pNext);
|
---|
1968 | }
|
---|
1969 | pgmUnlock(pVM);
|
---|
1970 | return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
|
---|
1971 | }
|
---|
1972 |
|
---|
1973 |
|
---|
1974 | /**
|
---|
1975 | * Allocate missing physical pages for an existing guest RAM range.
|
---|
1976 | *
|
---|
1977 | * @returns VBox status.
|
---|
1978 | * @param pVM The VM handle.
|
---|
1979 | * @param pRamRange RAM range
|
---|
1980 | * @param GCPhys GC physical address of the RAM range. (page aligned)
|
---|
1981 | */
|
---|
1982 | int pgmr3PhysGrowRange(PVM pVM, RTGCPHYS GCPhys)
|
---|
1983 | {
|
---|
1984 | void *pvRam;
|
---|
1985 | int rc;
|
---|
1986 |
|
---|
1987 | /* We must execute this function in the EMT thread, otherwise we'll run into problems. */
|
---|
1988 | if (!VM_IS_EMT(pVM))
|
---|
1989 | {
|
---|
1990 | PVMREQ pReq;
|
---|
1991 | const RTGCPHYS GCPhysParam = GCPhys;
|
---|
1992 |
|
---|
1993 | AssertMsg(!PDMCritSectIsOwner(&pVM->pgm.s.CritSect), ("We own the PGM lock -> deadlock danger!!\n"));
|
---|
1994 |
|
---|
1995 | rc = VMR3ReqCall(pVM, VMREQDEST_ANY, &pReq, RT_INDEFINITE_WAIT, (PFNRT)PGM3PhysGrowRange, 2, pVM, &GCPhysParam);
|
---|
1996 | if (RT_SUCCESS(rc))
|
---|
1997 | {
|
---|
1998 | rc = pReq->iStatus;
|
---|
1999 | VMR3ReqFree(pReq);
|
---|
2000 | }
|
---|
2001 | return rc;
|
---|
2002 | }
|
---|
2003 |
|
---|
2004 | /* Round down to chunk boundary */
|
---|
2005 | GCPhys = GCPhys & PGM_DYNAMIC_CHUNK_BASE_MASK;
|
---|
2006 |
|
---|
2007 | STAM_COUNTER_INC(&pVM->pgm.s.StatR3DynRamGrow);
|
---|
2008 | STAM_COUNTER_ADD(&pVM->pgm.s.StatR3DynRamTotal, PGM_DYNAMIC_CHUNK_SIZE/(1024*1024));
|
---|
2009 |
|
---|
2010 | Log(("pgmr3PhysGrowRange: allocate chunk of size 0x%X at %RGp\n", PGM_DYNAMIC_CHUNK_SIZE, GCPhys));
|
---|
2011 |
|
---|
2012 | unsigned cPages = PGM_DYNAMIC_CHUNK_SIZE >> PAGE_SHIFT;
|
---|
2013 |
|
---|
2014 | for (;;)
|
---|
2015 | {
|
---|
2016 | rc = SUPPageAlloc(cPages, &pvRam);
|
---|
2017 | if (RT_SUCCESS(rc))
|
---|
2018 | {
|
---|
2019 |
|
---|
2020 | rc = MMR3PhysRegisterEx(pVM, pvRam, GCPhys, PGM_DYNAMIC_CHUNK_SIZE, 0, MM_PHYS_TYPE_DYNALLOC_CHUNK, "Main Memory");
|
---|
2021 | if (RT_SUCCESS(rc))
|
---|
2022 | return rc;
|
---|
2023 |
|
---|
2024 | SUPPageFree(pvRam, cPages);
|
---|
2025 | }
|
---|
2026 |
|
---|
2027 | VMSTATE enmVMState = VMR3GetState(pVM);
|
---|
2028 | if (enmVMState != VMSTATE_RUNNING)
|
---|
2029 | {
|
---|
2030 | AssertMsgFailed(("Out of memory while trying to allocate a guest RAM chunk at %RGp!\n", GCPhys));
|
---|
2031 | LogRel(("PGM: Out of memory while trying to allocate a guest RAM chunk at %RGp (VMstate=%s)!\n", GCPhys, VMR3GetStateName(enmVMState)));
|
---|
2032 | return rc;
|
---|
2033 | }
|
---|
2034 |
|
---|
2035 | LogRel(("pgmr3PhysGrowRange: out of memory. pause until the user resumes execution.\n"));
|
---|
2036 |
|
---|
2037 | /* Pause first, then inform Main. */
|
---|
2038 | rc = VMR3SuspendNoSave(pVM);
|
---|
2039 | AssertRC(rc);
|
---|
2040 |
|
---|
2041 | 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");
|
---|
2042 |
|
---|
2043 | /* Wait for resume event; will only return in that case. If the VM is stopped, the EMT thread will be destroyed. */
|
---|
2044 | rc = VMR3WaitForResume(pVM);
|
---|
2045 |
|
---|
2046 | /* Retry */
|
---|
2047 | LogRel(("pgmr3PhysGrowRange: VM execution resumed -> retry.\n"));
|
---|
2048 | }
|
---|
2049 | }
|
---|
2050 |
|
---|
2051 | #endif /* !VBOX_WITH_NEW_PHYS_CODE */
|
---|
2052 |
|
---|
2053 |
|
---|
2054 | /**
|
---|
2055 | * Interface MMR3RomRegister() and MMR3PhysReserve calls to update the
|
---|
2056 | * flags of existing RAM ranges.
|
---|
2057 | *
|
---|
2058 | * @returns VBox status.
|
---|
2059 | * @param pVM The VM handle.
|
---|
2060 | * @param GCPhys GC physical address of the RAM range. (page aligned)
|
---|
2061 | * @param cb Size of the RAM range. (page aligned)
|
---|
2062 | * @param fFlags The Or flags, MM_RAM_* \#defines.
|
---|
2063 | * @param fMask The and mask for the flags.
|
---|
2064 | */
|
---|
2065 | VMMR3DECL(int) PGMR3PhysSetFlags(PVM pVM, RTGCPHYS GCPhys, size_t cb, unsigned fFlags, unsigned fMask)
|
---|
2066 | {
|
---|
2067 | Log(("PGMR3PhysSetFlags %08X %x %x %x\n", GCPhys, cb, fFlags, fMask));
|
---|
2068 |
|
---|
2069 | /*
|
---|
2070 | * Validate input.
|
---|
2071 | * (Not so important because caller is always MMR3RomRegister() and MMR3PhysReserve(), but anyway...)
|
---|
2072 | */
|
---|
2073 | Assert(!(fFlags & ~(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)));
|
---|
2074 | Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb && cb);
|
---|
2075 | Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
|
---|
2076 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
2077 | AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
|
---|
2078 |
|
---|
2079 | /*
|
---|
2080 | * Lookup the range.
|
---|
2081 | */
|
---|
2082 | PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
2083 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
2084 | pRam = pRam->CTX_SUFF(pNext);
|
---|
2085 | if ( !pRam
|
---|
2086 | || GCPhys > pRam->GCPhysLast
|
---|
2087 | || GCPhysLast < pRam->GCPhys)
|
---|
2088 | {
|
---|
2089 | AssertMsgFailed(("No RAM range for %RGp-%RGp\n", GCPhys, GCPhysLast));
|
---|
2090 | return VERR_INVALID_PARAMETER;
|
---|
2091 | }
|
---|
2092 |
|
---|
2093 | /*
|
---|
2094 | * Update the requested flags.
|
---|
2095 | */
|
---|
2096 | RTHCPHYS fFullMask = ~(RTHCPHYS)(MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)
|
---|
2097 | | fMask;
|
---|
2098 | unsigned iPageEnd = (GCPhysLast - pRam->GCPhys + 1) >> PAGE_SHIFT;
|
---|
2099 | unsigned iPage = (GCPhys - pRam->GCPhys) >> PAGE_SHIFT;
|
---|
2100 | for ( ; iPage < iPageEnd; iPage++)
|
---|
2101 | pRam->aPages[iPage].HCPhys = (pRam->aPages[iPage].HCPhys & fFullMask) | fFlags; /** @todo PAGE FLAGS */
|
---|
2102 |
|
---|
2103 | return VINF_SUCCESS;
|
---|
2104 | }
|
---|
2105 |
|
---|
2106 |
|
---|
2107 | /**
|
---|
2108 | * Sets the Address Gate 20 state.
|
---|
2109 | *
|
---|
2110 | * @param pVM VM handle.
|
---|
2111 | * @param fEnable True if the gate should be enabled.
|
---|
2112 | * False if the gate should be disabled.
|
---|
2113 | */
|
---|
2114 | VMMDECL(void) PGMR3PhysSetA20(PVM pVM, bool fEnable)
|
---|
2115 | {
|
---|
2116 | LogFlow(("PGMR3PhysSetA20 %d (was %d)\n", fEnable, pVM->pgm.s.fA20Enabled));
|
---|
2117 | if (pVM->pgm.s.fA20Enabled != (RTUINT)fEnable)
|
---|
2118 | {
|
---|
2119 | pVM->pgm.s.fA20Enabled = fEnable;
|
---|
2120 | pVM->pgm.s.GCPhysA20Mask = ~(RTGCPHYS)(!fEnable << 20);
|
---|
2121 | REMR3A20Set(pVM, fEnable);
|
---|
2122 | /** @todo we're not handling this correctly for VT-x / AMD-V. See #2911 */
|
---|
2123 | }
|
---|
2124 | }
|
---|
2125 |
|
---|
2126 |
|
---|
2127 | /**
|
---|
2128 | * Tree enumeration callback for dealing with age rollover.
|
---|
2129 | * It will perform a simple compression of the current age.
|
---|
2130 | */
|
---|
2131 | static DECLCALLBACK(int) pgmR3PhysChunkAgeingRolloverCallback(PAVLU32NODECORE pNode, void *pvUser)
|
---|
2132 | {
|
---|
2133 | /* Age compression - ASSUMES iNow == 4. */
|
---|
2134 | PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)pNode;
|
---|
2135 | if (pChunk->iAge >= UINT32_C(0xffffff00))
|
---|
2136 | pChunk->iAge = 3;
|
---|
2137 | else if (pChunk->iAge >= UINT32_C(0xfffff000))
|
---|
2138 | pChunk->iAge = 2;
|
---|
2139 | else if (pChunk->iAge)
|
---|
2140 | pChunk->iAge = 1;
|
---|
2141 | else /* iAge = 0 */
|
---|
2142 | pChunk->iAge = 4;
|
---|
2143 |
|
---|
2144 | /* reinsert */
|
---|
2145 | PVM pVM = (PVM)pvUser;
|
---|
2146 | RTAvllU32Remove(&pVM->pgm.s.ChunkR3Map.pAgeTree, pChunk->AgeCore.Key);
|
---|
2147 | pChunk->AgeCore.Key = pChunk->iAge;
|
---|
2148 | RTAvllU32Insert(&pVM->pgm.s.ChunkR3Map.pAgeTree, &pChunk->AgeCore);
|
---|
2149 | return 0;
|
---|
2150 | }
|
---|
2151 |
|
---|
2152 |
|
---|
2153 | /**
|
---|
2154 | * Tree enumeration callback that updates the chunks that have
|
---|
2155 | * been used since the last
|
---|
2156 | */
|
---|
2157 | static DECLCALLBACK(int) pgmR3PhysChunkAgeingCallback(PAVLU32NODECORE pNode, void *pvUser)
|
---|
2158 | {
|
---|
2159 | PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)pNode;
|
---|
2160 | if (!pChunk->iAge)
|
---|
2161 | {
|
---|
2162 | PVM pVM = (PVM)pvUser;
|
---|
2163 | RTAvllU32Remove(&pVM->pgm.s.ChunkR3Map.pAgeTree, pChunk->AgeCore.Key);
|
---|
2164 | pChunk->AgeCore.Key = pChunk->iAge = pVM->pgm.s.ChunkR3Map.iNow;
|
---|
2165 | RTAvllU32Insert(&pVM->pgm.s.ChunkR3Map.pAgeTree, &pChunk->AgeCore);
|
---|
2166 | }
|
---|
2167 |
|
---|
2168 | return 0;
|
---|
2169 | }
|
---|
2170 |
|
---|
2171 |
|
---|
2172 | /**
|
---|
2173 | * Performs ageing of the ring-3 chunk mappings.
|
---|
2174 | *
|
---|
2175 | * @param pVM The VM handle.
|
---|
2176 | */
|
---|
2177 | VMMR3DECL(void) PGMR3PhysChunkAgeing(PVM pVM)
|
---|
2178 | {
|
---|
2179 | pVM->pgm.s.ChunkR3Map.AgeingCountdown = RT_MIN(pVM->pgm.s.ChunkR3Map.cMax / 4, 1024);
|
---|
2180 | pVM->pgm.s.ChunkR3Map.iNow++;
|
---|
2181 | if (pVM->pgm.s.ChunkR3Map.iNow == 0)
|
---|
2182 | {
|
---|
2183 | pVM->pgm.s.ChunkR3Map.iNow = 4;
|
---|
2184 | RTAvlU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pTree, true /*fFromLeft*/, pgmR3PhysChunkAgeingRolloverCallback, pVM);
|
---|
2185 | }
|
---|
2186 | else
|
---|
2187 | RTAvlU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pTree, true /*fFromLeft*/, pgmR3PhysChunkAgeingCallback, pVM);
|
---|
2188 | }
|
---|
2189 |
|
---|
2190 |
|
---|
2191 | /**
|
---|
2192 | * The structure passed in the pvUser argument of pgmR3PhysChunkUnmapCandidateCallback().
|
---|
2193 | */
|
---|
2194 | typedef struct PGMR3PHYSCHUNKUNMAPCB
|
---|
2195 | {
|
---|
2196 | PVM pVM; /**< The VM handle. */
|
---|
2197 | PPGMCHUNKR3MAP pChunk; /**< The chunk to unmap. */
|
---|
2198 | } PGMR3PHYSCHUNKUNMAPCB, *PPGMR3PHYSCHUNKUNMAPCB;
|
---|
2199 |
|
---|
2200 |
|
---|
2201 | /**
|
---|
2202 | * Callback used to find the mapping that's been unused for
|
---|
2203 | * the longest time.
|
---|
2204 | */
|
---|
2205 | static DECLCALLBACK(int) pgmR3PhysChunkUnmapCandidateCallback(PAVLLU32NODECORE pNode, void *pvUser)
|
---|
2206 | {
|
---|
2207 | do
|
---|
2208 | {
|
---|
2209 | PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)((uint8_t *)pNode - RT_OFFSETOF(PGMCHUNKR3MAP, AgeCore));
|
---|
2210 | if ( pChunk->iAge
|
---|
2211 | && !pChunk->cRefs)
|
---|
2212 | {
|
---|
2213 | /*
|
---|
2214 | * Check that it's not in any of the TLBs.
|
---|
2215 | */
|
---|
2216 | PVM pVM = ((PPGMR3PHYSCHUNKUNMAPCB)pvUser)->pVM;
|
---|
2217 | for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.ChunkR3Map.Tlb.aEntries); i++)
|
---|
2218 | if (pVM->pgm.s.ChunkR3Map.Tlb.aEntries[i].pChunk == pChunk)
|
---|
2219 | {
|
---|
2220 | pChunk = NULL;
|
---|
2221 | break;
|
---|
2222 | }
|
---|
2223 | if (pChunk)
|
---|
2224 | for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.PhysTlbHC.aEntries); i++)
|
---|
2225 | if (pVM->pgm.s.PhysTlbHC.aEntries[i].pMap == pChunk)
|
---|
2226 | {
|
---|
2227 | pChunk = NULL;
|
---|
2228 | break;
|
---|
2229 | }
|
---|
2230 | if (pChunk)
|
---|
2231 | {
|
---|
2232 | ((PPGMR3PHYSCHUNKUNMAPCB)pvUser)->pChunk = pChunk;
|
---|
2233 | return 1; /* done */
|
---|
2234 | }
|
---|
2235 | }
|
---|
2236 |
|
---|
2237 | /* next with the same age - this version of the AVL API doesn't enumerate the list, so we have to do it. */
|
---|
2238 | pNode = pNode->pList;
|
---|
2239 | } while (pNode);
|
---|
2240 | return 0;
|
---|
2241 | }
|
---|
2242 |
|
---|
2243 |
|
---|
2244 | /**
|
---|
2245 | * Finds a good candidate for unmapping when the ring-3 mapping cache is full.
|
---|
2246 | *
|
---|
2247 | * The candidate will not be part of any TLBs, so no need to flush
|
---|
2248 | * anything afterwards.
|
---|
2249 | *
|
---|
2250 | * @returns Chunk id.
|
---|
2251 | * @param pVM The VM handle.
|
---|
2252 | */
|
---|
2253 | static int32_t pgmR3PhysChunkFindUnmapCandidate(PVM pVM)
|
---|
2254 | {
|
---|
2255 | /*
|
---|
2256 | * Do tree ageing first?
|
---|
2257 | */
|
---|
2258 | if (pVM->pgm.s.ChunkR3Map.AgeingCountdown-- == 0)
|
---|
2259 | PGMR3PhysChunkAgeing(pVM);
|
---|
2260 |
|
---|
2261 | /*
|
---|
2262 | * Enumerate the age tree starting with the left most node.
|
---|
2263 | */
|
---|
2264 | PGMR3PHYSCHUNKUNMAPCB Args;
|
---|
2265 | Args.pVM = pVM;
|
---|
2266 | Args.pChunk = NULL;
|
---|
2267 | if (RTAvllU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pAgeTree, true /*fFromLeft*/, pgmR3PhysChunkUnmapCandidateCallback, pVM))
|
---|
2268 | return Args.pChunk->Core.Key;
|
---|
2269 | return INT32_MAX;
|
---|
2270 | }
|
---|
2271 |
|
---|
2272 |
|
---|
2273 | /**
|
---|
2274 | * Maps the given chunk into the ring-3 mapping cache.
|
---|
2275 | *
|
---|
2276 | * This will call ring-0.
|
---|
2277 | *
|
---|
2278 | * @returns VBox status code.
|
---|
2279 | * @param pVM The VM handle.
|
---|
2280 | * @param idChunk The chunk in question.
|
---|
2281 | * @param ppChunk Where to store the chunk tracking structure.
|
---|
2282 | *
|
---|
2283 | * @remarks Called from within the PGM critical section.
|
---|
2284 | */
|
---|
2285 | int pgmR3PhysChunkMap(PVM pVM, uint32_t idChunk, PPPGMCHUNKR3MAP ppChunk)
|
---|
2286 | {
|
---|
2287 | int rc;
|
---|
2288 | /*
|
---|
2289 | * Allocate a new tracking structure first.
|
---|
2290 | */
|
---|
2291 | #if 0 /* for later when we've got a separate mapping method for ring-0. */
|
---|
2292 | PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)MMR3HeapAlloc(pVM, MM_TAG_PGM_CHUNK_MAPPING, sizeof(*pChunk));
|
---|
2293 | AssertReturn(pChunk, VERR_NO_MEMORY);
|
---|
2294 | #else
|
---|
2295 | PPGMCHUNKR3MAP pChunk;
|
---|
2296 | rc = MMHyperAlloc(pVM, sizeof(*pChunk), 0, MM_TAG_PGM_CHUNK_MAPPING, (void **)&pChunk);
|
---|
2297 | AssertRCReturn(rc, rc);
|
---|
2298 | #endif
|
---|
2299 | pChunk->Core.Key = idChunk;
|
---|
2300 | pChunk->AgeCore.Key = pVM->pgm.s.ChunkR3Map.iNow;
|
---|
2301 | pChunk->iAge = 0;
|
---|
2302 | pChunk->cRefs = 0;
|
---|
2303 | pChunk->cPermRefs = 0;
|
---|
2304 | pChunk->pv = NULL;
|
---|
2305 |
|
---|
2306 | /*
|
---|
2307 | * Request the ring-0 part to map the chunk in question and if
|
---|
2308 | * necessary unmap another one to make space in the mapping cache.
|
---|
2309 | */
|
---|
2310 | GMMMAPUNMAPCHUNKREQ Req;
|
---|
2311 | Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
2312 | Req.Hdr.cbReq = sizeof(Req);
|
---|
2313 | Req.pvR3 = NULL;
|
---|
2314 | Req.idChunkMap = idChunk;
|
---|
2315 | Req.idChunkUnmap = INT32_MAX;
|
---|
2316 | if (pVM->pgm.s.ChunkR3Map.c >= pVM->pgm.s.ChunkR3Map.cMax)
|
---|
2317 | Req.idChunkUnmap = pgmR3PhysChunkFindUnmapCandidate(pVM);
|
---|
2318 | rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_MAP_UNMAP_CHUNK, 0, &Req.Hdr);
|
---|
2319 | if (RT_SUCCESS(rc))
|
---|
2320 | {
|
---|
2321 | /*
|
---|
2322 | * Update the tree.
|
---|
2323 | */
|
---|
2324 | /* insert the new one. */
|
---|
2325 | AssertPtr(Req.pvR3);
|
---|
2326 | pChunk->pv = Req.pvR3;
|
---|
2327 | bool fRc = RTAvlU32Insert(&pVM->pgm.s.ChunkR3Map.pTree, &pChunk->Core);
|
---|
2328 | AssertRelease(fRc);
|
---|
2329 | pVM->pgm.s.ChunkR3Map.c++;
|
---|
2330 |
|
---|
2331 | fRc = RTAvllU32Insert(&pVM->pgm.s.ChunkR3Map.pAgeTree, &pChunk->AgeCore);
|
---|
2332 | AssertRelease(fRc);
|
---|
2333 |
|
---|
2334 | /* remove the unmapped one. */
|
---|
2335 | if (Req.idChunkUnmap != INT32_MAX)
|
---|
2336 | {
|
---|
2337 | PPGMCHUNKR3MAP pUnmappedChunk = (PPGMCHUNKR3MAP)RTAvlU32Remove(&pVM->pgm.s.ChunkR3Map.pTree, Req.idChunkUnmap);
|
---|
2338 | AssertRelease(pUnmappedChunk);
|
---|
2339 | pUnmappedChunk->pv = NULL;
|
---|
2340 | pUnmappedChunk->Core.Key = UINT32_MAX;
|
---|
2341 | #if 0 /* for later when we've got a separate mapping method for ring-0. */
|
---|
2342 | MMR3HeapFree(pUnmappedChunk);
|
---|
2343 | #else
|
---|
2344 | MMHyperFree(pVM, pUnmappedChunk);
|
---|
2345 | #endif
|
---|
2346 | pVM->pgm.s.ChunkR3Map.c--;
|
---|
2347 | }
|
---|
2348 | }
|
---|
2349 | else
|
---|
2350 | {
|
---|
2351 | AssertRC(rc);
|
---|
2352 | #if 0 /* for later when we've got a separate mapping method for ring-0. */
|
---|
2353 | MMR3HeapFree(pChunk);
|
---|
2354 | #else
|
---|
2355 | MMHyperFree(pVM, pChunk);
|
---|
2356 | #endif
|
---|
2357 | pChunk = NULL;
|
---|
2358 | }
|
---|
2359 |
|
---|
2360 | *ppChunk = pChunk;
|
---|
2361 | return rc;
|
---|
2362 | }
|
---|
2363 |
|
---|
2364 |
|
---|
2365 | /**
|
---|
2366 | * For VMMCALLHOST_PGM_MAP_CHUNK, considered internal.
|
---|
2367 | *
|
---|
2368 | * @returns see pgmR3PhysChunkMap.
|
---|
2369 | * @param pVM The VM handle.
|
---|
2370 | * @param idChunk The chunk to map.
|
---|
2371 | */
|
---|
2372 | VMMR3DECL(int) PGMR3PhysChunkMap(PVM pVM, uint32_t idChunk)
|
---|
2373 | {
|
---|
2374 | PPGMCHUNKR3MAP pChunk;
|
---|
2375 | return pgmR3PhysChunkMap(pVM, idChunk, &pChunk);
|
---|
2376 | }
|
---|
2377 |
|
---|
2378 |
|
---|
2379 | /**
|
---|
2380 | * Invalidates the TLB for the ring-3 mapping cache.
|
---|
2381 | *
|
---|
2382 | * @param pVM The VM handle.
|
---|
2383 | */
|
---|
2384 | VMMR3DECL(void) PGMR3PhysChunkInvalidateTLB(PVM pVM)
|
---|
2385 | {
|
---|
2386 | pgmLock(pVM);
|
---|
2387 | for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.ChunkR3Map.Tlb.aEntries); i++)
|
---|
2388 | {
|
---|
2389 | pVM->pgm.s.ChunkR3Map.Tlb.aEntries[i].idChunk = NIL_GMM_CHUNKID;
|
---|
2390 | pVM->pgm.s.ChunkR3Map.Tlb.aEntries[i].pChunk = NULL;
|
---|
2391 | }
|
---|
2392 | pgmUnlock(pVM);
|
---|
2393 | }
|
---|
2394 |
|
---|
2395 |
|
---|
2396 | /**
|
---|
2397 | * Response to VM_FF_PGM_NEED_HANDY_PAGES and VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES.
|
---|
2398 | *
|
---|
2399 | * @returns The following VBox status codes.
|
---|
2400 | * @retval VINF_SUCCESS on success. FF cleared.
|
---|
2401 | * @retval VINF_EM_NO_MEMORY if we're out of memory. The FF is not cleared in this case.
|
---|
2402 | *
|
---|
2403 | * @param pVM The VM handle.
|
---|
2404 | */
|
---|
2405 | VMMR3DECL(int) PGMR3PhysAllocateHandyPages(PVM pVM)
|
---|
2406 | {
|
---|
2407 | pgmLock(pVM);
|
---|
2408 | int rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES, 0, NULL);
|
---|
2409 | if (rc == VERR_GMM_SEED_ME)
|
---|
2410 | {
|
---|
2411 | void *pvChunk;
|
---|
2412 | rc = SUPPageAlloc(GMM_CHUNK_SIZE >> PAGE_SHIFT, &pvChunk);
|
---|
2413 | if (RT_SUCCESS(rc))
|
---|
2414 | rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_SEED_CHUNK, (uintptr_t)pvChunk, NULL);
|
---|
2415 | if (RT_FAILURE(rc))
|
---|
2416 | {
|
---|
2417 | LogRel(("PGM: GMM Seeding failed, rc=%Rrc\n", rc));
|
---|
2418 | rc = VINF_EM_NO_MEMORY;
|
---|
2419 | }
|
---|
2420 | }
|
---|
2421 | pgmUnlock(pVM);
|
---|
2422 | Assert(rc == VINF_SUCCESS || rc == VINF_EM_NO_MEMORY);
|
---|
2423 | return rc;
|
---|
2424 | }
|
---|
2425 |
|
---|
2426 |
|
---|
2427 | /**
|
---|
2428 | * Converts a GC physical address to a HC ring-3 pointer, with some
|
---|
2429 | * additional checks.
|
---|
2430 | *
|
---|
2431 | * @returns VBox status code.
|
---|
2432 | * @retval VINF_SUCCESS on success.
|
---|
2433 | * @retval VINF_PGM_PHYS_TLB_CATCH_WRITE and *pvPtr set if the page has a write
|
---|
2434 | * access handler of some kind.
|
---|
2435 | * @retval VERR_PGM_PHYS_TLB_CATCH_ALL if the page has a handler catching all
|
---|
2436 | * accesses or is odd in any way.
|
---|
2437 | * @retval VERR_PGM_PHYS_TLB_UNASSIGNED if the page doesn't exist.
|
---|
2438 | *
|
---|
2439 | * @param pVM The VM handle.
|
---|
2440 | * @param GCPhys The GC physical address to convert.
|
---|
2441 | * @param fWritable Whether write access is required.
|
---|
2442 | * @param pR3Ptr Where to store the R3 pointer on success.
|
---|
2443 | */
|
---|
2444 | VMMR3DECL(int) PGMR3PhysTlbGCPhys2Ptr(PVM pVM, RTGCPHYS GCPhys, bool fWritable, void **pvPtr)
|
---|
2445 | {
|
---|
2446 | pgmLock(pVM);
|
---|
2447 |
|
---|
2448 | PPGMRAMRANGE pRam;
|
---|
2449 | PPGMPAGE pPage;
|
---|
2450 | int rc = pgmPhysGetPageAndRangeEx(&pVM->pgm.s, GCPhys, &pPage, &pRam);
|
---|
2451 | if (RT_SUCCESS(rc))
|
---|
2452 | {
|
---|
2453 | #if 0 /** @todo ifndef PGM_IGNORE_RAM_FLAGS_RESERVED */
|
---|
2454 | if (RT_UNLIKELY(PGM_PAGE_IS_RESERVED(pPage)))
|
---|
2455 | rc = VERR_PGM_PHYS_TLB_UNASSIGNED;
|
---|
2456 | #endif
|
---|
2457 | {
|
---|
2458 | #if 0 /** @todo looks like the system ROM is registered incorrectly, 0xfe000 claims to be a zero page. */
|
---|
2459 | if (fWritable && PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED)
|
---|
2460 | rc = VERR_PGM_PHYS_TLB_UNASSIGNED; /** @todo VBOX_WITH_NEW_PHYS_CODE: remap it to a writeable page. */
|
---|
2461 | #else
|
---|
2462 | if (0)
|
---|
2463 | /* nothing */;
|
---|
2464 | #endif
|
---|
2465 | else if (PGM_PAGE_HAS_ANY_HANDLERS(pPage))
|
---|
2466 | {
|
---|
2467 | if (PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage)) /* catches MMIO */
|
---|
2468 | rc = VERR_PGM_PHYS_TLB_CATCH_ALL;
|
---|
2469 | else if (fWritable && PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage))
|
---|
2470 | rc = VINF_PGM_PHYS_TLB_CATCH_WRITE;
|
---|
2471 | else
|
---|
2472 | {
|
---|
2473 | /* Temporariliy disabled phycial handler(s), since the recompiler
|
---|
2474 | doesn't get notified when it's reset we'll have to pretend its
|
---|
2475 | operating normally. */
|
---|
2476 | if (pgmHandlerPhysicalIsAll(pVM, GCPhys))
|
---|
2477 | rc = VERR_PGM_PHYS_TLB_CATCH_ALL;
|
---|
2478 | else
|
---|
2479 | rc = VINF_PGM_PHYS_TLB_CATCH_WRITE;
|
---|
2480 | }
|
---|
2481 | }
|
---|
2482 | else
|
---|
2483 | rc = VINF_SUCCESS;
|
---|
2484 | if (RT_SUCCESS(rc))
|
---|
2485 | {
|
---|
2486 | if (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
|
---|
2487 | {
|
---|
2488 | AssertMsg(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM, ("GCPhys=%RGp type=%d\n", GCPhys, PGM_PAGE_GET_TYPE(pPage)));
|
---|
2489 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
2490 | unsigned iChunk = (off >> PGM_DYNAMIC_CHUNK_SHIFT);
|
---|
2491 | *pvPtr = (void *)(pRam->paChunkR3Ptrs[iChunk] + (off & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
|
---|
2492 | }
|
---|
2493 | else if (RT_LIKELY(pRam->pvR3))
|
---|
2494 | {
|
---|
2495 | AssertMsg(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM || PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2, ("GCPhys=%RGp type=%d\n", GCPhys, PGM_PAGE_GET_TYPE(pPage)));
|
---|
2496 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
2497 | *pvPtr = (uint8_t *)pRam->pvR3 + off;
|
---|
2498 | }
|
---|
2499 | else
|
---|
2500 | rc = VERR_PGM_PHYS_TLB_UNASSIGNED;
|
---|
2501 | }
|
---|
2502 | }
|
---|
2503 | }
|
---|
2504 | else
|
---|
2505 | rc = VERR_PGM_PHYS_TLB_UNASSIGNED;
|
---|
2506 |
|
---|
2507 | pgmUnlock(pVM);
|
---|
2508 | return rc;
|
---|
2509 | }
|
---|
2510 |
|
---|
2511 |
|
---|
2512 |
|
---|