1 | /* $Id: PGMPhys.cpp 22698 2009-09-02 09:17: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_PHYS
|
---|
27 | #include <VBox/pgm.h>
|
---|
28 | #include <VBox/iom.h>
|
---|
29 | #include <VBox/mm.h>
|
---|
30 | #include <VBox/stam.h>
|
---|
31 | #include <VBox/rem.h>
|
---|
32 | #include <VBox/pdmdev.h>
|
---|
33 | #include "PGMInternal.h"
|
---|
34 | #include <VBox/vm.h>
|
---|
35 | #include <VBox/sup.h>
|
---|
36 | #include <VBox/param.h>
|
---|
37 | #include <VBox/err.h>
|
---|
38 | #include <VBox/log.h>
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/alloc.h>
|
---|
41 | #include <iprt/asm.h>
|
---|
42 | #include <iprt/thread.h>
|
---|
43 | #include <iprt/string.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Defined Constants And Macros *
|
---|
48 | *******************************************************************************/
|
---|
49 | /** The number of pages to free in one batch. */
|
---|
50 | #define PGMPHYS_FREE_PAGE_BATCH_SIZE 128
|
---|
51 |
|
---|
52 |
|
---|
53 | /*******************************************************************************
|
---|
54 | * Internal Functions *
|
---|
55 | *******************************************************************************/
|
---|
56 | static DECLCALLBACK(int) pgmR3PhysRomWriteHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser);
|
---|
57 | static int pgmPhysFreePage(PVM pVM, PGMMFREEPAGESREQ pReq, uint32_t *pcPendingPages, PPGMPAGE pPage, RTGCPHYS GCPhys);
|
---|
58 |
|
---|
59 |
|
---|
60 | /*
|
---|
61 | * PGMR3PhysReadU8-64
|
---|
62 | * PGMR3PhysWriteU8-64
|
---|
63 | */
|
---|
64 | #define PGMPHYSFN_READNAME PGMR3PhysReadU8
|
---|
65 | #define PGMPHYSFN_WRITENAME PGMR3PhysWriteU8
|
---|
66 | #define PGMPHYS_DATASIZE 1
|
---|
67 | #define PGMPHYS_DATATYPE uint8_t
|
---|
68 | #include "PGMPhysRWTmpl.h"
|
---|
69 |
|
---|
70 | #define PGMPHYSFN_READNAME PGMR3PhysReadU16
|
---|
71 | #define PGMPHYSFN_WRITENAME PGMR3PhysWriteU16
|
---|
72 | #define PGMPHYS_DATASIZE 2
|
---|
73 | #define PGMPHYS_DATATYPE uint16_t
|
---|
74 | #include "PGMPhysRWTmpl.h"
|
---|
75 |
|
---|
76 | #define PGMPHYSFN_READNAME PGMR3PhysReadU32
|
---|
77 | #define PGMPHYSFN_WRITENAME PGMR3PhysWriteU32
|
---|
78 | #define PGMPHYS_DATASIZE 4
|
---|
79 | #define PGMPHYS_DATATYPE uint32_t
|
---|
80 | #include "PGMPhysRWTmpl.h"
|
---|
81 |
|
---|
82 | #define PGMPHYSFN_READNAME PGMR3PhysReadU64
|
---|
83 | #define PGMPHYSFN_WRITENAME PGMR3PhysWriteU64
|
---|
84 | #define PGMPHYS_DATASIZE 8
|
---|
85 | #define PGMPHYS_DATATYPE uint64_t
|
---|
86 | #include "PGMPhysRWTmpl.h"
|
---|
87 |
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * EMT worker for PGMR3PhysReadExternal.
|
---|
91 | */
|
---|
92 | static DECLCALLBACK(int) pgmR3PhysReadExternalEMT(PVM pVM, PRTGCPHYS pGCPhys, void *pvBuf, size_t cbRead)
|
---|
93 | {
|
---|
94 | PGMPhysRead(pVM, *pGCPhys, pvBuf, cbRead);
|
---|
95 | return VINF_SUCCESS;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Write to physical memory, external users.
|
---|
101 | *
|
---|
102 | * @returns VBox status code.
|
---|
103 | * @retval VINF_SUCCESS.
|
---|
104 | *
|
---|
105 | * @param pVM VM Handle.
|
---|
106 | * @param GCPhys Physical address to write to.
|
---|
107 | * @param pvBuf What to write.
|
---|
108 | * @param cbWrite How many bytes to write.
|
---|
109 | *
|
---|
110 | * @thread Any but EMTs.
|
---|
111 | */
|
---|
112 | VMMR3DECL(int) PGMR3PhysReadExternal(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
|
---|
113 | {
|
---|
114 | VM_ASSERT_OTHER_THREAD(pVM);
|
---|
115 |
|
---|
116 | AssertMsgReturn(cbRead > 0, ("don't even think about reading zero bytes!\n"), VINF_SUCCESS);
|
---|
117 | LogFlow(("PGMR3PhysReadExternal: %RGp %d\n", GCPhys, cbRead));
|
---|
118 |
|
---|
119 | pgmLock(pVM);
|
---|
120 |
|
---|
121 | /*
|
---|
122 | * Copy loop on ram ranges.
|
---|
123 | */
|
---|
124 | PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
125 | for (;;)
|
---|
126 | {
|
---|
127 | /* Find range. */
|
---|
128 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
129 | pRam = pRam->CTX_SUFF(pNext);
|
---|
130 | /* Inside range or not? */
|
---|
131 | if (pRam && GCPhys >= pRam->GCPhys)
|
---|
132 | {
|
---|
133 | /*
|
---|
134 | * Must work our way thru this page by page.
|
---|
135 | */
|
---|
136 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
137 | while (off < pRam->cb)
|
---|
138 | {
|
---|
139 | unsigned iPage = off >> PAGE_SHIFT;
|
---|
140 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * If the page has an ALL access handler, we'll have to
|
---|
144 | * delegate the job to EMT.
|
---|
145 | */
|
---|
146 | if (PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage))
|
---|
147 | {
|
---|
148 | pgmUnlock(pVM);
|
---|
149 |
|
---|
150 | PVMREQ pReq = NULL;
|
---|
151 | int rc = VMR3ReqCall(pVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT,
|
---|
152 | (PFNRT)pgmR3PhysReadExternalEMT, 4, pVM, &GCPhys, pvBuf, cbRead);
|
---|
153 | if (RT_SUCCESS(rc))
|
---|
154 | {
|
---|
155 | rc = pReq->iStatus;
|
---|
156 | VMR3ReqFree(pReq);
|
---|
157 | }
|
---|
158 | return rc;
|
---|
159 | }
|
---|
160 | Assert(!PGM_PAGE_IS_MMIO(pPage));
|
---|
161 |
|
---|
162 | /*
|
---|
163 | * Simple stuff, go ahead.
|
---|
164 | */
|
---|
165 | size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
166 | if (cb > cbRead)
|
---|
167 | cb = cbRead;
|
---|
168 | const void *pvSrc;
|
---|
169 | int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, pRam->GCPhys + off, &pvSrc);
|
---|
170 | if (RT_SUCCESS(rc))
|
---|
171 | memcpy(pvBuf, pvSrc, cb);
|
---|
172 | else
|
---|
173 | {
|
---|
174 | AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
175 | pRam->GCPhys + off, pPage, rc));
|
---|
176 | memset(pvBuf, 0xff, cb);
|
---|
177 | }
|
---|
178 |
|
---|
179 | /* next page */
|
---|
180 | if (cb >= cbRead)
|
---|
181 | {
|
---|
182 | pgmUnlock(pVM);
|
---|
183 | return VINF_SUCCESS;
|
---|
184 | }
|
---|
185 | cbRead -= cb;
|
---|
186 | off += cb;
|
---|
187 | GCPhys += cb;
|
---|
188 | pvBuf = (char *)pvBuf + cb;
|
---|
189 | } /* walk pages in ram range. */
|
---|
190 | }
|
---|
191 | else
|
---|
192 | {
|
---|
193 | LogFlow(("PGMPhysRead: Unassigned %RGp size=%u\n", GCPhys, cbRead));
|
---|
194 |
|
---|
195 | /*
|
---|
196 | * Unassigned address space.
|
---|
197 | */
|
---|
198 | if (!pRam)
|
---|
199 | break;
|
---|
200 | size_t cb = pRam->GCPhys - GCPhys;
|
---|
201 | if (cb >= cbRead)
|
---|
202 | {
|
---|
203 | memset(pvBuf, 0xff, cbRead);
|
---|
204 | break;
|
---|
205 | }
|
---|
206 | memset(pvBuf, 0xff, cb);
|
---|
207 |
|
---|
208 | cbRead -= cb;
|
---|
209 | pvBuf = (char *)pvBuf + cb;
|
---|
210 | GCPhys += cb;
|
---|
211 | }
|
---|
212 | } /* Ram range walk */
|
---|
213 |
|
---|
214 | pgmUnlock(pVM);
|
---|
215 |
|
---|
216 | return VINF_SUCCESS;
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * EMT worker for PGMR3PhysWriteExternal.
|
---|
222 | */
|
---|
223 | static DECLCALLBACK(int) pgmR3PhysWriteExternalEMT(PVM pVM, PRTGCPHYS pGCPhys, const void *pvBuf, size_t cbWrite)
|
---|
224 | {
|
---|
225 | /** @todo VERR_EM_NO_MEMORY */
|
---|
226 | PGMPhysWrite(pVM, *pGCPhys, pvBuf, cbWrite);
|
---|
227 | return VINF_SUCCESS;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Write to physical memory, external users.
|
---|
233 | *
|
---|
234 | * @returns VBox status code.
|
---|
235 | * @retval VINF_SUCCESS.
|
---|
236 | * @retval VERR_EM_NO_MEMORY.
|
---|
237 | *
|
---|
238 | * @param pVM VM Handle.
|
---|
239 | * @param GCPhys Physical address to write to.
|
---|
240 | * @param pvBuf What to write.
|
---|
241 | * @param cbWrite How many bytes to write.
|
---|
242 | *
|
---|
243 | * @thread Any but EMTs.
|
---|
244 | */
|
---|
245 | VMMDECL(int) PGMR3PhysWriteExternal(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite)
|
---|
246 | {
|
---|
247 | VM_ASSERT_OTHER_THREAD(pVM);
|
---|
248 |
|
---|
249 | AssertMsg(!pVM->pgm.s.fNoMorePhysWrites, ("Calling PGMR3PhysWriteExternal after pgmR3Save()!\n"));
|
---|
250 | AssertMsgReturn(cbWrite > 0, ("don't even think about writing zero bytes!\n"), VINF_SUCCESS);
|
---|
251 | LogFlow(("PGMR3PhysWriteExternal: %RGp %d\n", GCPhys, cbWrite));
|
---|
252 |
|
---|
253 | pgmLock(pVM);
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * Copy loop on ram ranges, stop when we hit something difficult.
|
---|
257 | */
|
---|
258 | PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
259 | for (;;)
|
---|
260 | {
|
---|
261 | /* Find range. */
|
---|
262 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
263 | pRam = pRam->CTX_SUFF(pNext);
|
---|
264 | /* Inside range or not? */
|
---|
265 | if (pRam && GCPhys >= pRam->GCPhys)
|
---|
266 | {
|
---|
267 | /*
|
---|
268 | * Must work our way thru this page by page.
|
---|
269 | */
|
---|
270 | RTGCPTR off = GCPhys - pRam->GCPhys;
|
---|
271 | while (off < pRam->cb)
|
---|
272 | {
|
---|
273 | RTGCPTR iPage = off >> PAGE_SHIFT;
|
---|
274 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
275 |
|
---|
276 | /*
|
---|
277 | * It the page is in any way problematic, we have to
|
---|
278 | * do the work on the EMT. Anything that needs to be made
|
---|
279 | * writable or involves access handlers is problematic.
|
---|
280 | */
|
---|
281 | if ( PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage)
|
---|
282 | || PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED)
|
---|
283 | {
|
---|
284 | pgmUnlock(pVM);
|
---|
285 |
|
---|
286 | PVMREQ pReq = NULL;
|
---|
287 | int rc = VMR3ReqCall(pVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT,
|
---|
288 | (PFNRT)pgmR3PhysWriteExternalEMT, 4, pVM, &GCPhys, pvBuf, cbWrite);
|
---|
289 | if (RT_SUCCESS(rc))
|
---|
290 | {
|
---|
291 | rc = pReq->iStatus;
|
---|
292 | VMR3ReqFree(pReq);
|
---|
293 | }
|
---|
294 | return rc;
|
---|
295 | }
|
---|
296 | Assert(!PGM_PAGE_IS_MMIO(pPage));
|
---|
297 |
|
---|
298 | /*
|
---|
299 | * Simple stuff, go ahead.
|
---|
300 | */
|
---|
301 | size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
302 | if (cb > cbWrite)
|
---|
303 | cb = cbWrite;
|
---|
304 | void *pvDst;
|
---|
305 | int rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, pRam->GCPhys + off, &pvDst);
|
---|
306 | if (RT_SUCCESS(rc))
|
---|
307 | memcpy(pvDst, pvBuf, cb);
|
---|
308 | else
|
---|
309 | AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
310 | pRam->GCPhys + off, pPage, rc));
|
---|
311 |
|
---|
312 | /* next page */
|
---|
313 | if (cb >= cbWrite)
|
---|
314 | {
|
---|
315 | pgmUnlock(pVM);
|
---|
316 | return VINF_SUCCESS;
|
---|
317 | }
|
---|
318 |
|
---|
319 | cbWrite -= cb;
|
---|
320 | off += cb;
|
---|
321 | GCPhys += cb;
|
---|
322 | pvBuf = (const char *)pvBuf + cb;
|
---|
323 | } /* walk pages in ram range */
|
---|
324 | }
|
---|
325 | else
|
---|
326 | {
|
---|
327 | /*
|
---|
328 | * Unassigned address space, skip it.
|
---|
329 | */
|
---|
330 | if (!pRam)
|
---|
331 | break;
|
---|
332 | size_t cb = pRam->GCPhys - GCPhys;
|
---|
333 | if (cb >= cbWrite)
|
---|
334 | break;
|
---|
335 | cbWrite -= cb;
|
---|
336 | pvBuf = (const char *)pvBuf + cb;
|
---|
337 | GCPhys += cb;
|
---|
338 | }
|
---|
339 | } /* Ram range walk */
|
---|
340 |
|
---|
341 | pgmUnlock(pVM);
|
---|
342 | return VINF_SUCCESS;
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * VMR3ReqCall worker for PGMR3PhysGCPhys2CCPtrExternal to make pages writable.
|
---|
348 | *
|
---|
349 | * @returns see PGMR3PhysGCPhys2CCPtrExternal
|
---|
350 | * @param pVM The VM handle.
|
---|
351 | * @param pGCPhys Pointer to the guest physical address.
|
---|
352 | * @param ppv Where to store the mapping address.
|
---|
353 | * @param pLock Where to store the lock.
|
---|
354 | */
|
---|
355 | static DECLCALLBACK(int) pgmR3PhysGCPhys2CCPtrDelegated(PVM pVM, PRTGCPHYS pGCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
356 | {
|
---|
357 | /*
|
---|
358 | * Just hand it to PGMPhysGCPhys2CCPtr and check that it's not a page with
|
---|
359 | * an access handler after it succeeds.
|
---|
360 | */
|
---|
361 | int rc = pgmLock(pVM);
|
---|
362 | AssertRCReturn(rc, rc);
|
---|
363 |
|
---|
364 | rc = PGMPhysGCPhys2CCPtr(pVM, *pGCPhys, ppv, pLock);
|
---|
365 | if (RT_SUCCESS(rc))
|
---|
366 | {
|
---|
367 | PPGMPAGEMAPTLBE pTlbe;
|
---|
368 | int rc2 = pgmPhysPageQueryTlbe(&pVM->pgm.s, *pGCPhys, &pTlbe);
|
---|
369 | AssertFatalRC(rc2);
|
---|
370 | PPGMPAGE pPage = pTlbe->pPage;
|
---|
371 | if (PGM_PAGE_IS_MMIO(pPage))
|
---|
372 | {
|
---|
373 | PGMPhysReleasePageMappingLock(pVM, pLock);
|
---|
374 | rc = VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
375 | }
|
---|
376 | else
|
---|
377 | if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage))
|
---|
378 | {
|
---|
379 | /* We *must* flush any corresponding pgm pool page here, otherwise we'll
|
---|
380 | * not be informed about writes and keep bogus gst->shw mappings around.
|
---|
381 | */
|
---|
382 | PGMPoolFlushPage(pVM, *pGCPhys);
|
---|
383 | Assert(!PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage));
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | pgmUnlock(pVM);
|
---|
388 | return rc;
|
---|
389 | }
|
---|
390 |
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Requests the mapping of a guest page into ring-3, external threads.
|
---|
394 | *
|
---|
395 | * When you're done with the page, call PGMPhysReleasePageMappingLock() ASAP to
|
---|
396 | * release it.
|
---|
397 | *
|
---|
398 | * This API will assume your intention is to write to the page, and will
|
---|
399 | * therefore replace shared and zero pages. If you do not intend to modify the
|
---|
400 | * page, use the PGMR3PhysGCPhys2CCPtrReadOnlyExternal() API.
|
---|
401 | *
|
---|
402 | * @returns VBox status code.
|
---|
403 | * @retval VINF_SUCCESS on success.
|
---|
404 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical
|
---|
405 | * backing or if the page has any active access handlers. The caller
|
---|
406 | * must fall back on using PGMR3PhysWriteExternal.
|
---|
407 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
408 | *
|
---|
409 | * @param pVM The VM handle.
|
---|
410 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
411 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
412 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
413 | *
|
---|
414 | * @remark Avoid calling this API from within critical sections (other than the
|
---|
415 | * PGM one) because of the deadlock risk when we have to delegating the
|
---|
416 | * task to an EMT.
|
---|
417 | * @thread Any.
|
---|
418 | */
|
---|
419 | VMMR3DECL(int) PGMR3PhysGCPhys2CCPtrExternal(PVM pVM, RTGCPHYS GCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
420 | {
|
---|
421 | AssertPtr(ppv);
|
---|
422 | AssertPtr(pLock);
|
---|
423 |
|
---|
424 | Assert(VM_IS_EMT(pVM) || !PGMIsLockOwner(pVM));
|
---|
425 |
|
---|
426 | int rc = pgmLock(pVM);
|
---|
427 | AssertRCReturn(rc, rc);
|
---|
428 |
|
---|
429 | /*
|
---|
430 | * Query the Physical TLB entry for the page (may fail).
|
---|
431 | */
|
---|
432 | PPGMPAGEMAPTLBE pTlbe;
|
---|
433 | rc = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
|
---|
434 | if (RT_SUCCESS(rc))
|
---|
435 | {
|
---|
436 | PPGMPAGE pPage = pTlbe->pPage;
|
---|
437 | if (PGM_PAGE_IS_MMIO(pPage))
|
---|
438 | rc = VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
439 | else
|
---|
440 | {
|
---|
441 | /*
|
---|
442 | * If the page is shared, the zero page, or being write monitored
|
---|
443 | * it must be converted to an page that's writable if possible.
|
---|
444 | * This has to be done on an EMT.
|
---|
445 | */
|
---|
446 | if ( PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage)
|
---|
447 | || RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
|
---|
448 | {
|
---|
449 | pgmUnlock(pVM);
|
---|
450 |
|
---|
451 | PVMREQ pReq = NULL;
|
---|
452 | rc = VMR3ReqCall(pVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT,
|
---|
453 | (PFNRT)pgmR3PhysGCPhys2CCPtrDelegated, 4, pVM, &GCPhys, ppv, pLock);
|
---|
454 | if (RT_SUCCESS(rc))
|
---|
455 | {
|
---|
456 | rc = pReq->iStatus;
|
---|
457 | VMR3ReqFree(pReq);
|
---|
458 | }
|
---|
459 | return rc;
|
---|
460 | }
|
---|
461 |
|
---|
462 | /*
|
---|
463 | * Now, just perform the locking and calculate the return address.
|
---|
464 | */
|
---|
465 | PPGMPAGEMAP pMap = pTlbe->pMap;
|
---|
466 | pMap->cRefs++;
|
---|
467 | #if 0 /** @todo implement locking properly */
|
---|
468 | if (RT_LIKELY(pPage->cLocks != PGM_PAGE_MAX_LOCKS))
|
---|
469 | if (RT_UNLIKELY(++pPage->cLocks == PGM_PAGE_MAX_LOCKS))
|
---|
470 | {
|
---|
471 | AssertMsgFailed(("%RGp is entering permanent locked state!\n", GCPhys));
|
---|
472 | pMap->cRefs++; /* Extra ref to prevent it from going away. */
|
---|
473 | }
|
---|
474 | #endif
|
---|
475 | *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
|
---|
476 | pLock->pvPage = pPage;
|
---|
477 | pLock->pvMap = pMap;
|
---|
478 | }
|
---|
479 | }
|
---|
480 |
|
---|
481 | pgmUnlock(pVM);
|
---|
482 | return rc;
|
---|
483 | }
|
---|
484 |
|
---|
485 |
|
---|
486 | /**
|
---|
487 | * Requests the mapping of a guest page into ring-3, external threads.
|
---|
488 | *
|
---|
489 | * When you're done with the page, call PGMPhysReleasePageMappingLock() ASAP to
|
---|
490 | * release it.
|
---|
491 | *
|
---|
492 | * @returns VBox status code.
|
---|
493 | * @retval VINF_SUCCESS on success.
|
---|
494 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical
|
---|
495 | * backing or if the page as an active ALL access handler. The caller
|
---|
496 | * must fall back on using PGMPhysRead.
|
---|
497 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
498 | *
|
---|
499 | * @param pVM The VM handle.
|
---|
500 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
501 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
502 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
503 | *
|
---|
504 | * @remark Avoid calling this API from within critical sections (other than
|
---|
505 | * the PGM one) because of the deadlock risk.
|
---|
506 | * @thread Any.
|
---|
507 | */
|
---|
508 | VMMR3DECL(int) PGMR3PhysGCPhys2CCPtrReadOnlyExternal(PVM pVM, RTGCPHYS GCPhys, void const **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
509 | {
|
---|
510 | int rc = pgmLock(pVM);
|
---|
511 | AssertRCReturn(rc, rc);
|
---|
512 |
|
---|
513 | /*
|
---|
514 | * Query the Physical TLB entry for the page (may fail).
|
---|
515 | */
|
---|
516 | PPGMPAGEMAPTLBE pTlbe;
|
---|
517 | rc = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
|
---|
518 | if (RT_SUCCESS(rc))
|
---|
519 | {
|
---|
520 | PPGMPAGE pPage = pTlbe->pPage;
|
---|
521 | #if 1
|
---|
522 | /* MMIO pages doesn't have any readable backing. */
|
---|
523 | if (PGM_PAGE_IS_MMIO(pPage))
|
---|
524 | rc = VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
525 | #else
|
---|
526 | if (PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage))
|
---|
527 | rc = VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
528 | #endif
|
---|
529 | else
|
---|
530 | {
|
---|
531 | /*
|
---|
532 | * Now, just perform the locking and calculate the return address.
|
---|
533 | */
|
---|
534 | PPGMPAGEMAP pMap = pTlbe->pMap;
|
---|
535 | pMap->cRefs++;
|
---|
536 | #if 0 /** @todo implement locking properly */
|
---|
537 | if (RT_LIKELY(pPage->cLocks != PGM_PAGE_MAX_LOCKS))
|
---|
538 | if (RT_UNLIKELY(++pPage->cLocks == PGM_PAGE_MAX_LOCKS))
|
---|
539 | {
|
---|
540 | AssertMsgFailed(("%RGp is entering permanent locked state!\n", GCPhys));
|
---|
541 | pMap->cRefs++; /* Extra ref to prevent it from going away. */
|
---|
542 | }
|
---|
543 | #endif
|
---|
544 | *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
|
---|
545 | pLock->pvPage = pPage;
|
---|
546 | pLock->pvMap = pMap;
|
---|
547 | }
|
---|
548 | }
|
---|
549 |
|
---|
550 | pgmUnlock(pVM);
|
---|
551 | return rc;
|
---|
552 | }
|
---|
553 |
|
---|
554 |
|
---|
555 | /**
|
---|
556 | * Relinks the RAM ranges using the pSelfRC and pSelfR0 pointers.
|
---|
557 | *
|
---|
558 | * Called when anything was relocated.
|
---|
559 | *
|
---|
560 | * @param pVM Pointer to the shared VM structure.
|
---|
561 | */
|
---|
562 | void pgmR3PhysRelinkRamRanges(PVM pVM)
|
---|
563 | {
|
---|
564 | PPGMRAMRANGE pCur;
|
---|
565 |
|
---|
566 | #ifdef VBOX_STRICT
|
---|
567 | for (pCur = pVM->pgm.s.pRamRangesR3; pCur; pCur = pCur->pNextR3)
|
---|
568 | {
|
---|
569 | Assert((pCur->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pCur->pSelfR0 == MMHyperCCToR0(pVM, pCur));
|
---|
570 | Assert((pCur->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pCur->pSelfRC == MMHyperCCToRC(pVM, pCur));
|
---|
571 | Assert((pCur->GCPhys & PAGE_OFFSET_MASK) == 0);
|
---|
572 | Assert((pCur->GCPhysLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
|
---|
573 | Assert((pCur->cb & PAGE_OFFSET_MASK) == 0);
|
---|
574 | Assert(pCur->cb == pCur->GCPhysLast - pCur->GCPhys + 1);
|
---|
575 | for (PPGMRAMRANGE pCur2 = pVM->pgm.s.pRamRangesR3; pCur2; pCur2 = pCur2->pNextR3)
|
---|
576 | Assert( pCur2 == pCur
|
---|
577 | || strcmp(pCur2->pszDesc, pCur->pszDesc)); /** @todo fix MMIO ranges!! */
|
---|
578 | }
|
---|
579 | #endif
|
---|
580 |
|
---|
581 | pCur = pVM->pgm.s.pRamRangesR3;
|
---|
582 | if (pCur)
|
---|
583 | {
|
---|
584 | pVM->pgm.s.pRamRangesR0 = pCur->pSelfR0;
|
---|
585 | pVM->pgm.s.pRamRangesRC = pCur->pSelfRC;
|
---|
586 |
|
---|
587 | for (; pCur->pNextR3; pCur = pCur->pNextR3)
|
---|
588 | {
|
---|
589 | pCur->pNextR0 = pCur->pNextR3->pSelfR0;
|
---|
590 | pCur->pNextRC = pCur->pNextR3->pSelfRC;
|
---|
591 | }
|
---|
592 |
|
---|
593 | Assert(pCur->pNextR0 == NIL_RTR0PTR);
|
---|
594 | Assert(pCur->pNextRC == NIL_RTRCPTR);
|
---|
595 | }
|
---|
596 | else
|
---|
597 | {
|
---|
598 | Assert(pVM->pgm.s.pRamRangesR0 == NIL_RTR0PTR);
|
---|
599 | Assert(pVM->pgm.s.pRamRangesRC == NIL_RTRCPTR);
|
---|
600 | }
|
---|
601 | }
|
---|
602 |
|
---|
603 |
|
---|
604 | /**
|
---|
605 | * Links a new RAM range into the list.
|
---|
606 | *
|
---|
607 | * @param pVM Pointer to the shared VM structure.
|
---|
608 | * @param pNew Pointer to the new list entry.
|
---|
609 | * @param pPrev Pointer to the previous list entry. If NULL, insert as head.
|
---|
610 | */
|
---|
611 | static void pgmR3PhysLinkRamRange(PVM pVM, PPGMRAMRANGE pNew, PPGMRAMRANGE pPrev)
|
---|
612 | {
|
---|
613 | AssertMsg(pNew->pszDesc, ("%RGp-%RGp\n", pNew->GCPhys, pNew->GCPhysLast));
|
---|
614 | Assert((pNew->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pNew->pSelfR0 == MMHyperCCToR0(pVM, pNew));
|
---|
615 | Assert((pNew->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pNew->pSelfRC == MMHyperCCToRC(pVM, pNew));
|
---|
616 |
|
---|
617 | pgmLock(pVM);
|
---|
618 |
|
---|
619 | PPGMRAMRANGE pRam = pPrev ? pPrev->pNextR3 : pVM->pgm.s.pRamRangesR3;
|
---|
620 | pNew->pNextR3 = pRam;
|
---|
621 | pNew->pNextR0 = pRam ? pRam->pSelfR0 : NIL_RTR0PTR;
|
---|
622 | pNew->pNextRC = pRam ? pRam->pSelfRC : NIL_RTRCPTR;
|
---|
623 |
|
---|
624 | if (pPrev)
|
---|
625 | {
|
---|
626 | pPrev->pNextR3 = pNew;
|
---|
627 | pPrev->pNextR0 = pNew->pSelfR0;
|
---|
628 | pPrev->pNextRC = pNew->pSelfRC;
|
---|
629 | }
|
---|
630 | else
|
---|
631 | {
|
---|
632 | pVM->pgm.s.pRamRangesR3 = pNew;
|
---|
633 | pVM->pgm.s.pRamRangesR0 = pNew->pSelfR0;
|
---|
634 | pVM->pgm.s.pRamRangesRC = pNew->pSelfRC;
|
---|
635 | }
|
---|
636 |
|
---|
637 | pgmUnlock(pVM);
|
---|
638 | }
|
---|
639 |
|
---|
640 |
|
---|
641 | /**
|
---|
642 | * Unlink an existing RAM range from the list.
|
---|
643 | *
|
---|
644 | * @param pVM Pointer to the shared VM structure.
|
---|
645 | * @param pRam Pointer to the new list entry.
|
---|
646 | * @param pPrev Pointer to the previous list entry. If NULL, insert as head.
|
---|
647 | */
|
---|
648 | static void pgmR3PhysUnlinkRamRange2(PVM pVM, PPGMRAMRANGE pRam, PPGMRAMRANGE pPrev)
|
---|
649 | {
|
---|
650 | Assert(pPrev ? pPrev->pNextR3 == pRam : pVM->pgm.s.pRamRangesR3 == pRam);
|
---|
651 | Assert((pRam->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pRam->pSelfR0 == MMHyperCCToR0(pVM, pRam));
|
---|
652 | Assert((pRam->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING) || pRam->pSelfRC == MMHyperCCToRC(pVM, pRam));
|
---|
653 |
|
---|
654 | pgmLock(pVM);
|
---|
655 |
|
---|
656 | PPGMRAMRANGE pNext = pRam->pNextR3;
|
---|
657 | if (pPrev)
|
---|
658 | {
|
---|
659 | pPrev->pNextR3 = pNext;
|
---|
660 | pPrev->pNextR0 = pNext ? pNext->pSelfR0 : NIL_RTR0PTR;
|
---|
661 | pPrev->pNextRC = pNext ? pNext->pSelfRC : NIL_RTRCPTR;
|
---|
662 | }
|
---|
663 | else
|
---|
664 | {
|
---|
665 | Assert(pVM->pgm.s.pRamRangesR3 == pRam);
|
---|
666 | pVM->pgm.s.pRamRangesR3 = pNext;
|
---|
667 | pVM->pgm.s.pRamRangesR0 = pNext ? pNext->pSelfR0 : NIL_RTR0PTR;
|
---|
668 | pVM->pgm.s.pRamRangesRC = pNext ? pNext->pSelfRC : NIL_RTRCPTR;
|
---|
669 | }
|
---|
670 |
|
---|
671 | pgmUnlock(pVM);
|
---|
672 | }
|
---|
673 |
|
---|
674 |
|
---|
675 | /**
|
---|
676 | * Unlink an existing RAM range from the list.
|
---|
677 | *
|
---|
678 | * @param pVM Pointer to the shared VM structure.
|
---|
679 | * @param pRam Pointer to the new list entry.
|
---|
680 | */
|
---|
681 | static void pgmR3PhysUnlinkRamRange(PVM pVM, PPGMRAMRANGE pRam)
|
---|
682 | {
|
---|
683 | pgmLock(pVM);
|
---|
684 |
|
---|
685 | /* find prev. */
|
---|
686 | PPGMRAMRANGE pPrev = NULL;
|
---|
687 | PPGMRAMRANGE pCur = pVM->pgm.s.pRamRangesR3;
|
---|
688 | while (pCur != pRam)
|
---|
689 | {
|
---|
690 | pPrev = pCur;
|
---|
691 | pCur = pCur->pNextR3;
|
---|
692 | }
|
---|
693 | AssertFatal(pCur);
|
---|
694 |
|
---|
695 | pgmR3PhysUnlinkRamRange2(pVM, pRam, pPrev);
|
---|
696 |
|
---|
697 | pgmUnlock(pVM);
|
---|
698 | }
|
---|
699 |
|
---|
700 |
|
---|
701 | /**
|
---|
702 | * Frees a range of pages, replacing them with ZERO pages of the specified type.
|
---|
703 | *
|
---|
704 | * @returns VBox status code.
|
---|
705 | * @param pVM The VM handle.
|
---|
706 | * @param pRam The RAM range in which the pages resides.
|
---|
707 | * @param GCPhys The address of the first page.
|
---|
708 | * @param GCPhysLast The address of the last page.
|
---|
709 | * @param uType The page type to replace then with.
|
---|
710 | */
|
---|
711 | static int pgmR3PhysFreePageRange(PVM pVM, PPGMRAMRANGE pRam, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast, uint8_t uType)
|
---|
712 | {
|
---|
713 | uint32_t cPendingPages = 0;
|
---|
714 | PGMMFREEPAGESREQ pReq;
|
---|
715 | int rc = GMMR3FreePagesPrepare(pVM, &pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE, GMMACCOUNT_BASE);
|
---|
716 | AssertLogRelRCReturn(rc, rc);
|
---|
717 |
|
---|
718 | /* Itegerate the pages. */
|
---|
719 | PPGMPAGE pPageDst = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
720 | uint32_t cPagesLeft = ((GCPhysLast - GCPhys) >> PAGE_SHIFT) + 1;
|
---|
721 | while (cPagesLeft-- > 0)
|
---|
722 | {
|
---|
723 | rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, pPageDst, GCPhys);
|
---|
724 | AssertLogRelRCReturn(rc, rc); /* We're done for if this goes wrong. */
|
---|
725 |
|
---|
726 | PGM_PAGE_SET_TYPE(pPageDst, uType);
|
---|
727 |
|
---|
728 | GCPhys += PAGE_SIZE;
|
---|
729 | pPageDst++;
|
---|
730 | }
|
---|
731 |
|
---|
732 | if (cPendingPages)
|
---|
733 | {
|
---|
734 | rc = GMMR3FreePagesPerform(pVM, pReq, cPendingPages);
|
---|
735 | AssertLogRelRCReturn(rc, rc);
|
---|
736 | }
|
---|
737 | GMMR3FreePagesCleanup(pReq);
|
---|
738 |
|
---|
739 | return rc;
|
---|
740 | }
|
---|
741 |
|
---|
742 |
|
---|
743 | /**
|
---|
744 | * PGMR3PhysRegisterRam worker that initializes and links a RAM range.
|
---|
745 | *
|
---|
746 | * @param pVM The VM handle.
|
---|
747 | * @param pNew The new RAM range.
|
---|
748 | * @param GCPhys The address of the RAM range.
|
---|
749 | * @param GCPhysLast The last address of the RAM range.
|
---|
750 | * @param RCPtrNew The RC address if the range is floating. NIL_RTRCPTR
|
---|
751 | * if in HMA.
|
---|
752 | * @param R0PtrNew Ditto for R0.
|
---|
753 | * @param pszDesc The description.
|
---|
754 | * @param pPrev The previous RAM range (for linking).
|
---|
755 | */
|
---|
756 | static void pgmR3PhysInitAndLinkRamRange(PVM pVM, PPGMRAMRANGE pNew, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast,
|
---|
757 | RTRCPTR RCPtrNew, RTR0PTR R0PtrNew, const char *pszDesc, PPGMRAMRANGE pPrev)
|
---|
758 | {
|
---|
759 | /*
|
---|
760 | * Initialize the range.
|
---|
761 | */
|
---|
762 | pNew->pSelfR0 = R0PtrNew != NIL_RTR0PTR ? R0PtrNew : MMHyperCCToR0(pVM, pNew);
|
---|
763 | pNew->pSelfRC = RCPtrNew != NIL_RTRCPTR ? RCPtrNew : MMHyperCCToRC(pVM, pNew);
|
---|
764 | pNew->GCPhys = GCPhys;
|
---|
765 | pNew->GCPhysLast = GCPhysLast;
|
---|
766 | pNew->cb = GCPhysLast - GCPhys + 1;
|
---|
767 | pNew->pszDesc = pszDesc;
|
---|
768 | pNew->fFlags = RCPtrNew != NIL_RTRCPTR ? PGM_RAM_RANGE_FLAGS_FLOATING : 0;
|
---|
769 | pNew->pvR3 = NULL;
|
---|
770 |
|
---|
771 | uint32_t const cPages = pNew->cb >> PAGE_SHIFT;
|
---|
772 | RTGCPHYS iPage = cPages;
|
---|
773 | while (iPage-- > 0)
|
---|
774 | PGM_PAGE_INIT_ZERO(&pNew->aPages[iPage], pVM, PGMPAGETYPE_RAM);
|
---|
775 |
|
---|
776 | /* Update the page count stats. */
|
---|
777 | pVM->pgm.s.cZeroPages += cPages;
|
---|
778 | pVM->pgm.s.cAllPages += cPages;
|
---|
779 |
|
---|
780 | /*
|
---|
781 | * Link it.
|
---|
782 | */
|
---|
783 | pgmR3PhysLinkRamRange(pVM, pNew, pPrev);
|
---|
784 | }
|
---|
785 |
|
---|
786 |
|
---|
787 | /**
|
---|
788 | * Relocate a floating RAM range.
|
---|
789 | *
|
---|
790 | * @copydoc FNPGMRELOCATE.
|
---|
791 | */
|
---|
792 | static DECLCALLBACK(bool) pgmR3PhysRamRangeRelocate(PVM pVM, RTGCPTR GCPtrOld, RTGCPTR GCPtrNew, PGMRELOCATECALL enmMode, void *pvUser)
|
---|
793 | {
|
---|
794 | PPGMRAMRANGE pRam = (PPGMRAMRANGE)pvUser;
|
---|
795 | Assert(pRam->fFlags & PGM_RAM_RANGE_FLAGS_FLOATING);
|
---|
796 | Assert(pRam->pSelfRC == GCPtrOld + PAGE_SIZE);
|
---|
797 |
|
---|
798 | switch (enmMode)
|
---|
799 | {
|
---|
800 | case PGMRELOCATECALL_SUGGEST:
|
---|
801 | return true;
|
---|
802 | case PGMRELOCATECALL_RELOCATE:
|
---|
803 | {
|
---|
804 | /* Update myself and then relink all the ranges. */
|
---|
805 | pgmLock(pVM);
|
---|
806 | pRam->pSelfRC = (RTRCPTR)(GCPtrNew + PAGE_SIZE);
|
---|
807 | pgmR3PhysRelinkRamRanges(pVM);
|
---|
808 | pgmUnlock(pVM);
|
---|
809 | return true;
|
---|
810 | }
|
---|
811 |
|
---|
812 | default:
|
---|
813 | AssertFailedReturn(false);
|
---|
814 | }
|
---|
815 | }
|
---|
816 |
|
---|
817 |
|
---|
818 | /**
|
---|
819 | * PGMR3PhysRegisterRam worker that registers a high chunk.
|
---|
820 | *
|
---|
821 | * @returns VBox status code.
|
---|
822 | * @param pVM The VM handle.
|
---|
823 | * @param GCPhys The address of the RAM.
|
---|
824 | * @param cRamPages The number of RAM pages to register.
|
---|
825 | * @param cbChunk The size of the PGMRAMRANGE guest mapping.
|
---|
826 | * @param iChunk The chunk number.
|
---|
827 | * @param pszDesc The RAM range description.
|
---|
828 | * @param ppPrev Previous RAM range pointer. In/Out.
|
---|
829 | */
|
---|
830 | static int pgmR3PhysRegisterHighRamChunk(PVM pVM, RTGCPHYS GCPhys, uint32_t cRamPages,
|
---|
831 | uint32_t cbChunk, uint32_t iChunk, const char *pszDesc,
|
---|
832 | PPGMRAMRANGE *ppPrev)
|
---|
833 | {
|
---|
834 | const char *pszDescChunk = iChunk == 0
|
---|
835 | ? pszDesc
|
---|
836 | : MMR3HeapAPrintf(pVM, MM_TAG_PGM_PHYS, "%s (#%u)", pszDesc, iChunk + 1);
|
---|
837 | AssertReturn(pszDescChunk, VERR_NO_MEMORY);
|
---|
838 |
|
---|
839 | /*
|
---|
840 | * Allocate memory for the new chunk.
|
---|
841 | */
|
---|
842 | size_t const cChunkPages = RT_ALIGN_Z(RT_UOFFSETOF(PGMRAMRANGE, aPages[cRamPages]), PAGE_SIZE) >> PAGE_SHIFT;
|
---|
843 | PSUPPAGE paChunkPages = (PSUPPAGE)RTMemTmpAllocZ(sizeof(SUPPAGE) * cChunkPages);
|
---|
844 | AssertReturn(paChunkPages, VERR_NO_TMP_MEMORY);
|
---|
845 | RTR0PTR R0PtrChunk = NIL_RTR0PTR;
|
---|
846 | void *pvChunk = NULL;
|
---|
847 | int rc = SUPR3PageAllocEx(cChunkPages, 0 /*fFlags*/, &pvChunk,
|
---|
848 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
849 | VMMIsHwVirtExtForced(pVM) ? &R0PtrChunk : NULL,
|
---|
850 | #else
|
---|
851 | NULL,
|
---|
852 | #endif
|
---|
853 | paChunkPages);
|
---|
854 | if (RT_SUCCESS(rc))
|
---|
855 | {
|
---|
856 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
857 | if (!VMMIsHwVirtExtForced(pVM))
|
---|
858 | R0PtrChunk = NIL_RTR0PTR;
|
---|
859 | #else
|
---|
860 | R0PtrChunk = (uintptr_t)pvChunk;
|
---|
861 | #endif
|
---|
862 | memset(pvChunk, 0, cChunkPages << PAGE_SHIFT);
|
---|
863 |
|
---|
864 | PPGMRAMRANGE pNew = (PPGMRAMRANGE)pvChunk;
|
---|
865 |
|
---|
866 | /*
|
---|
867 | * Create a mapping and map the pages into it.
|
---|
868 | * We push these in below the HMA.
|
---|
869 | */
|
---|
870 | RTGCPTR GCPtrChunkMap = pVM->pgm.s.GCPtrPrevRamRangeMapping - cbChunk;
|
---|
871 | rc = PGMR3MapPT(pVM, GCPtrChunkMap, cbChunk, 0 /*fFlags*/, pgmR3PhysRamRangeRelocate, pNew, pszDescChunk);
|
---|
872 | if (RT_SUCCESS(rc))
|
---|
873 | {
|
---|
874 | pVM->pgm.s.GCPtrPrevRamRangeMapping = GCPtrChunkMap;
|
---|
875 |
|
---|
876 | RTGCPTR const GCPtrChunk = GCPtrChunkMap + PAGE_SIZE;
|
---|
877 | RTGCPTR GCPtrPage = GCPtrChunk;
|
---|
878 | for (uint32_t iPage = 0; iPage < cChunkPages && RT_SUCCESS(rc); iPage++, GCPtrPage += PAGE_SIZE)
|
---|
879 | rc = PGMMap(pVM, GCPtrPage, paChunkPages[iPage].Phys, PAGE_SIZE, 0);
|
---|
880 | if (RT_SUCCESS(rc))
|
---|
881 | {
|
---|
882 | /*
|
---|
883 | * Ok, init and link the range.
|
---|
884 | */
|
---|
885 | pgmR3PhysInitAndLinkRamRange(pVM, pNew, GCPhys, GCPhys + ((RTGCPHYS)cRamPages << PAGE_SHIFT) - 1,
|
---|
886 | (RTRCPTR)GCPtrChunk, R0PtrChunk, pszDescChunk, *ppPrev);
|
---|
887 | *ppPrev = pNew;
|
---|
888 | }
|
---|
889 | }
|
---|
890 |
|
---|
891 | if (RT_FAILURE(rc))
|
---|
892 | SUPR3PageFreeEx(pvChunk, cChunkPages);
|
---|
893 | }
|
---|
894 |
|
---|
895 | RTMemTmpFree(paChunkPages);
|
---|
896 | return rc;
|
---|
897 | }
|
---|
898 |
|
---|
899 |
|
---|
900 | /**
|
---|
901 | * Sets up a range RAM.
|
---|
902 | *
|
---|
903 | * This will check for conflicting registrations, make a resource
|
---|
904 | * reservation for the memory (with GMM), and setup the per-page
|
---|
905 | * tracking structures (PGMPAGE).
|
---|
906 | *
|
---|
907 | * @returns VBox stutus code.
|
---|
908 | * @param pVM Pointer to the shared VM structure.
|
---|
909 | * @param GCPhys The physical address of the RAM.
|
---|
910 | * @param cb The size of the RAM.
|
---|
911 | * @param pszDesc The description - not copied, so, don't free or change it.
|
---|
912 | */
|
---|
913 | VMMR3DECL(int) PGMR3PhysRegisterRam(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, const char *pszDesc)
|
---|
914 | {
|
---|
915 | /*
|
---|
916 | * Validate input.
|
---|
917 | */
|
---|
918 | Log(("PGMR3PhysRegisterRam: GCPhys=%RGp cb=%RGp pszDesc=%s\n", GCPhys, cb, pszDesc));
|
---|
919 | AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
|
---|
920 | AssertReturn(RT_ALIGN_T(cb, PAGE_SIZE, RTGCPHYS) == cb, VERR_INVALID_PARAMETER);
|
---|
921 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
922 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
923 | AssertMsgReturn(GCPhysLast > GCPhys, ("The range wraps! GCPhys=%RGp cb=%RGp\n", GCPhys, cb), VERR_INVALID_PARAMETER);
|
---|
924 | AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
|
---|
925 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
926 |
|
---|
927 | pgmLock(pVM);
|
---|
928 |
|
---|
929 | /*
|
---|
930 | * Find range location and check for conflicts.
|
---|
931 | * (We don't lock here because the locking by EMT is only required on update.)
|
---|
932 | */
|
---|
933 | PPGMRAMRANGE pPrev = NULL;
|
---|
934 | PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3;
|
---|
935 | while (pRam && GCPhysLast >= pRam->GCPhys)
|
---|
936 | {
|
---|
937 | if ( GCPhysLast >= pRam->GCPhys
|
---|
938 | && GCPhys <= pRam->GCPhysLast)
|
---|
939 | AssertLogRelMsgFailedReturn(("%RGp-%RGp (%s) conflicts with existing %RGp-%RGp (%s)\n",
|
---|
940 | GCPhys, GCPhysLast, pszDesc,
|
---|
941 | pRam->GCPhys, pRam->GCPhysLast, pRam->pszDesc),
|
---|
942 | VERR_PGM_RAM_CONFLICT);
|
---|
943 |
|
---|
944 | /* next */
|
---|
945 | pPrev = pRam;
|
---|
946 | pRam = pRam->pNextR3;
|
---|
947 | }
|
---|
948 |
|
---|
949 | /*
|
---|
950 | * Register it with GMM (the API bitches).
|
---|
951 | */
|
---|
952 | const RTGCPHYS cPages = cb >> PAGE_SHIFT;
|
---|
953 | int rc = MMR3IncreaseBaseReservation(pVM, cPages);
|
---|
954 | if (RT_FAILURE(rc))
|
---|
955 | {
|
---|
956 | pgmUnlock(pVM);
|
---|
957 | return rc;
|
---|
958 | }
|
---|
959 |
|
---|
960 | if ( GCPhys >= _4G
|
---|
961 | && cPages > 256)
|
---|
962 | {
|
---|
963 | /*
|
---|
964 | * The PGMRAMRANGE structures for the high memory can get very big.
|
---|
965 | * In order to avoid SUPR3PageAllocEx allocation failures due to the
|
---|
966 | * allocation size limit there and also to avoid being unable to find
|
---|
967 | * guest mapping space for them, we split this memory up into 4MB in
|
---|
968 | * (potential) raw-mode configs and 16MB chunks in forced AMD-V/VT-x
|
---|
969 | * mode.
|
---|
970 | *
|
---|
971 | * The first and last page of each mapping are guard pages and marked
|
---|
972 | * not-present. So, we've got 4186112 and 16769024 bytes available for
|
---|
973 | * the PGMRAMRANGE structure.
|
---|
974 | *
|
---|
975 | * Note! The sizes used here will influence the saved state.
|
---|
976 | */
|
---|
977 | uint32_t cbChunk;
|
---|
978 | uint32_t cPagesPerChunk;
|
---|
979 | if (VMMIsHwVirtExtForced(pVM))
|
---|
980 | {
|
---|
981 | cbChunk = 16U*_1M;
|
---|
982 | cPagesPerChunk = 1048048; /* max ~1048059 */
|
---|
983 | AssertCompile(sizeof(PGMRAMRANGE) + sizeof(PGMPAGE) * 1048048 < 16U*_1M - PAGE_SIZE * 2);
|
---|
984 | }
|
---|
985 | else
|
---|
986 | {
|
---|
987 | cbChunk = 4U*_1M;
|
---|
988 | cPagesPerChunk = 261616; /* max ~261627 */
|
---|
989 | AssertCompile(sizeof(PGMRAMRANGE) + sizeof(PGMPAGE) * 261616 < 4U*_1M - PAGE_SIZE * 2);
|
---|
990 | }
|
---|
991 | AssertRelease(RT_UOFFSETOF(PGMRAMRANGE, aPages[cPagesPerChunk]) + PAGE_SIZE * 2 <= cbChunk);
|
---|
992 |
|
---|
993 | RTGCPHYS cPagesLeft = cPages;
|
---|
994 | RTGCPHYS GCPhysChunk = GCPhys;
|
---|
995 | uint32_t iChunk = 0;
|
---|
996 | while (cPagesLeft > 0)
|
---|
997 | {
|
---|
998 | uint32_t cPagesInChunk = cPagesLeft;
|
---|
999 | if (cPagesInChunk > cPagesPerChunk)
|
---|
1000 | cPagesInChunk = cPagesPerChunk;
|
---|
1001 |
|
---|
1002 | rc = pgmR3PhysRegisterHighRamChunk(pVM, GCPhysChunk, cPagesInChunk, cbChunk, iChunk, pszDesc, &pPrev);
|
---|
1003 | AssertRCReturn(rc, rc);
|
---|
1004 |
|
---|
1005 | /* advance */
|
---|
1006 | GCPhysChunk += (RTGCPHYS)cPagesInChunk << PAGE_SHIFT;
|
---|
1007 | cPagesLeft -= cPagesInChunk;
|
---|
1008 | iChunk++;
|
---|
1009 | }
|
---|
1010 | }
|
---|
1011 | else
|
---|
1012 | {
|
---|
1013 | /*
|
---|
1014 | * Allocate, initialize and link the new RAM range.
|
---|
1015 | */
|
---|
1016 | const size_t cbRamRange = RT_OFFSETOF(PGMRAMRANGE, aPages[cPages]);
|
---|
1017 | PPGMRAMRANGE pNew;
|
---|
1018 | rc = MMR3HyperAllocOnceNoRel(pVM, cbRamRange, 0, MM_TAG_PGM_PHYS, (void **)&pNew);
|
---|
1019 | AssertLogRelMsgRCReturn(rc, ("cbRamRange=%zu\n", cbRamRange), rc);
|
---|
1020 |
|
---|
1021 | pgmR3PhysInitAndLinkRamRange(pVM, pNew, GCPhys, GCPhysLast, NIL_RTRCPTR, NIL_RTR0PTR, pszDesc, pPrev);
|
---|
1022 | }
|
---|
1023 | pgmUnlock(pVM);
|
---|
1024 |
|
---|
1025 | /*
|
---|
1026 | * Notify REM.
|
---|
1027 | */
|
---|
1028 | REMR3NotifyPhysRamRegister(pVM, GCPhys, cb, REM_NOTIFY_PHYS_RAM_FLAGS_RAM);
|
---|
1029 |
|
---|
1030 | return VINF_SUCCESS;
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 |
|
---|
1034 | /**
|
---|
1035 | * Worker called by PGMR3InitFinalize if we're configured to pre-allocate RAM.
|
---|
1036 | *
|
---|
1037 | * We do this late in the init process so that all the ROM and MMIO ranges have
|
---|
1038 | * been registered already and we don't go wasting memory on them.
|
---|
1039 | *
|
---|
1040 | * @returns VBox status code.
|
---|
1041 | *
|
---|
1042 | * @param pVM Pointer to the shared VM structure.
|
---|
1043 | */
|
---|
1044 | int pgmR3PhysRamPreAllocate(PVM pVM)
|
---|
1045 | {
|
---|
1046 | Assert(pVM->pgm.s.fRamPreAlloc);
|
---|
1047 | Log(("pgmR3PhysRamPreAllocate: enter\n"));
|
---|
1048 |
|
---|
1049 | /*
|
---|
1050 | * Walk the RAM ranges and allocate all RAM pages, halt at
|
---|
1051 | * the first allocation error.
|
---|
1052 | */
|
---|
1053 | uint64_t cPages = 0;
|
---|
1054 | uint64_t NanoTS = RTTimeNanoTS();
|
---|
1055 | pgmLock(pVM);
|
---|
1056 | for (PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3; pRam; pRam = pRam->pNextR3)
|
---|
1057 | {
|
---|
1058 | PPGMPAGE pPage = &pRam->aPages[0];
|
---|
1059 | RTGCPHYS GCPhys = pRam->GCPhys;
|
---|
1060 | uint32_t cLeft = pRam->cb >> PAGE_SHIFT;
|
---|
1061 | while (cLeft-- > 0)
|
---|
1062 | {
|
---|
1063 | if (PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM)
|
---|
1064 | {
|
---|
1065 | switch (PGM_PAGE_GET_STATE(pPage))
|
---|
1066 | {
|
---|
1067 | case PGM_PAGE_STATE_ZERO:
|
---|
1068 | {
|
---|
1069 | int rc = pgmPhysAllocPage(pVM, pPage, GCPhys);
|
---|
1070 | if (RT_FAILURE(rc))
|
---|
1071 | {
|
---|
1072 | LogRel(("PGM: RAM Pre-allocation failed at %RGp (in %s) with rc=%Rrc\n", GCPhys, pRam->pszDesc, rc));
|
---|
1073 | pgmUnlock(pVM);
|
---|
1074 | return rc;
|
---|
1075 | }
|
---|
1076 | cPages++;
|
---|
1077 | break;
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | case PGM_PAGE_STATE_ALLOCATED:
|
---|
1081 | case PGM_PAGE_STATE_WRITE_MONITORED:
|
---|
1082 | case PGM_PAGE_STATE_SHARED:
|
---|
1083 | /* nothing to do here. */
|
---|
1084 | break;
|
---|
1085 | }
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | /* next */
|
---|
1089 | pPage++;
|
---|
1090 | GCPhys += PAGE_SIZE;
|
---|
1091 | }
|
---|
1092 | }
|
---|
1093 | pgmUnlock(pVM);
|
---|
1094 | NanoTS = RTTimeNanoTS() - NanoTS;
|
---|
1095 |
|
---|
1096 | LogRel(("PGM: Pre-allocated %llu pages in %llu ms\n", cPages, NanoTS / 1000000));
|
---|
1097 | Log(("pgmR3PhysRamPreAllocate: returns VINF_SUCCESS\n"));
|
---|
1098 | return VINF_SUCCESS;
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 |
|
---|
1102 | /**
|
---|
1103 | * Resets (zeros) the RAM.
|
---|
1104 | *
|
---|
1105 | * ASSUMES that the caller owns the PGM lock.
|
---|
1106 | *
|
---|
1107 | * @returns VBox status code.
|
---|
1108 | * @param pVM Pointer to the shared VM structure.
|
---|
1109 | */
|
---|
1110 | int pgmR3PhysRamReset(PVM pVM)
|
---|
1111 | {
|
---|
1112 | Assert(PGMIsLockOwner(pVM));
|
---|
1113 | /*
|
---|
1114 | * We batch up pages before freeing them.
|
---|
1115 | */
|
---|
1116 | uint32_t cPendingPages = 0;
|
---|
1117 | PGMMFREEPAGESREQ pReq;
|
---|
1118 | int rc = GMMR3FreePagesPrepare(pVM, &pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE, GMMACCOUNT_BASE);
|
---|
1119 | AssertLogRelRCReturn(rc, rc);
|
---|
1120 |
|
---|
1121 | /*
|
---|
1122 | * Walk the ram ranges.
|
---|
1123 | */
|
---|
1124 | for (PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3; pRam; pRam = pRam->pNextR3)
|
---|
1125 | {
|
---|
1126 | uint32_t iPage = pRam->cb >> PAGE_SHIFT;
|
---|
1127 | AssertMsg(((RTGCPHYS)iPage << PAGE_SHIFT) == pRam->cb, ("%RGp %RGp\n", (RTGCPHYS)iPage << PAGE_SHIFT, pRam->cb));
|
---|
1128 |
|
---|
1129 | if (!pVM->pgm.s.fRamPreAlloc)
|
---|
1130 | {
|
---|
1131 | /* Replace all RAM pages by ZERO pages. */
|
---|
1132 | while (iPage-- > 0)
|
---|
1133 | {
|
---|
1134 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
1135 | switch (PGM_PAGE_GET_TYPE(pPage))
|
---|
1136 | {
|
---|
1137 | case PGMPAGETYPE_RAM:
|
---|
1138 | if (!PGM_PAGE_IS_ZERO(pPage))
|
---|
1139 | {
|
---|
1140 | rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, pPage, pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT));
|
---|
1141 | AssertLogRelRCReturn(rc, rc);
|
---|
1142 | }
|
---|
1143 | break;
|
---|
1144 |
|
---|
1145 | case PGMPAGETYPE_MMIO2_ALIAS_MMIO:
|
---|
1146 | pgmHandlerPhysicalResetAliasedPage(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT));
|
---|
1147 | break;
|
---|
1148 |
|
---|
1149 | case PGMPAGETYPE_MMIO2:
|
---|
1150 | case PGMPAGETYPE_ROM_SHADOW: /* handled by pgmR3PhysRomReset. */
|
---|
1151 | case PGMPAGETYPE_ROM:
|
---|
1152 | case PGMPAGETYPE_MMIO:
|
---|
1153 | break;
|
---|
1154 | default:
|
---|
1155 | AssertFailed();
|
---|
1156 | }
|
---|
1157 | } /* for each page */
|
---|
1158 | }
|
---|
1159 | else
|
---|
1160 | {
|
---|
1161 | /* Zero the memory. */
|
---|
1162 | while (iPage-- > 0)
|
---|
1163 | {
|
---|
1164 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
1165 | switch (PGM_PAGE_GET_TYPE(pPage))
|
---|
1166 | {
|
---|
1167 | case PGMPAGETYPE_RAM:
|
---|
1168 | switch (PGM_PAGE_GET_STATE(pPage))
|
---|
1169 | {
|
---|
1170 | case PGM_PAGE_STATE_ZERO:
|
---|
1171 | break;
|
---|
1172 | case PGM_PAGE_STATE_SHARED:
|
---|
1173 | case PGM_PAGE_STATE_WRITE_MONITORED:
|
---|
1174 | rc = pgmPhysPageMakeWritable(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT));
|
---|
1175 | AssertLogRelRCReturn(rc, rc);
|
---|
1176 | case PGM_PAGE_STATE_ALLOCATED:
|
---|
1177 | {
|
---|
1178 | void *pvPage;
|
---|
1179 | PPGMPAGEMAP pMapIgnored;
|
---|
1180 | rc = pgmPhysPageMap(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), &pMapIgnored, &pvPage);
|
---|
1181 | AssertLogRelRCReturn(rc, rc);
|
---|
1182 | ASMMemZeroPage(pvPage);
|
---|
1183 | break;
|
---|
1184 | }
|
---|
1185 | }
|
---|
1186 | break;
|
---|
1187 |
|
---|
1188 | case PGMPAGETYPE_MMIO2_ALIAS_MMIO:
|
---|
1189 | pgmHandlerPhysicalResetAliasedPage(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT));
|
---|
1190 | break;
|
---|
1191 |
|
---|
1192 | case PGMPAGETYPE_MMIO2:
|
---|
1193 | case PGMPAGETYPE_ROM_SHADOW:
|
---|
1194 | case PGMPAGETYPE_ROM:
|
---|
1195 | case PGMPAGETYPE_MMIO:
|
---|
1196 | break;
|
---|
1197 | default:
|
---|
1198 | AssertFailed();
|
---|
1199 |
|
---|
1200 | }
|
---|
1201 | } /* for each page */
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 | /*
|
---|
1207 | * Finish off any pages pending freeing.
|
---|
1208 | */
|
---|
1209 | if (cPendingPages)
|
---|
1210 | {
|
---|
1211 | rc = GMMR3FreePagesPerform(pVM, pReq, cPendingPages);
|
---|
1212 | AssertLogRelRCReturn(rc, rc);
|
---|
1213 | }
|
---|
1214 | GMMR3FreePagesCleanup(pReq);
|
---|
1215 |
|
---|
1216 | return VINF_SUCCESS;
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 |
|
---|
1220 | /**
|
---|
1221 | * This is the interface IOM is using to register an MMIO region.
|
---|
1222 | *
|
---|
1223 | * It will check for conflicts and ensure that a RAM range structure
|
---|
1224 | * is present before calling the PGMR3HandlerPhysicalRegister API to
|
---|
1225 | * register the callbacks.
|
---|
1226 | *
|
---|
1227 | * @returns VBox status code.
|
---|
1228 | *
|
---|
1229 | * @param pVM Pointer to the shared VM structure.
|
---|
1230 | * @param GCPhys The start of the MMIO region.
|
---|
1231 | * @param cb The size of the MMIO region.
|
---|
1232 | * @param pfnHandlerR3 The address of the ring-3 handler. (IOMR3MMIOHandler)
|
---|
1233 | * @param pvUserR3 The user argument for R3.
|
---|
1234 | * @param pfnHandlerR0 The address of the ring-0 handler. (IOMMMIOHandler)
|
---|
1235 | * @param pvUserR0 The user argument for R0.
|
---|
1236 | * @param pfnHandlerRC The address of the RC handler. (IOMMMIOHandler)
|
---|
1237 | * @param pvUserRC The user argument for RC.
|
---|
1238 | * @param pszDesc The description of the MMIO region.
|
---|
1239 | */
|
---|
1240 | VMMR3DECL(int) PGMR3PhysMMIORegister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb,
|
---|
1241 | R3PTRTYPE(PFNPGMR3PHYSHANDLER) pfnHandlerR3, RTR3PTR pvUserR3,
|
---|
1242 | R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnHandlerR0, RTR0PTR pvUserR0,
|
---|
1243 | RCPTRTYPE(PFNPGMRCPHYSHANDLER) pfnHandlerRC, RTRCPTR pvUserRC,
|
---|
1244 | R3PTRTYPE(const char *) pszDesc)
|
---|
1245 | {
|
---|
1246 | /*
|
---|
1247 | * Assert on some assumption.
|
---|
1248 | */
|
---|
1249 | VM_ASSERT_EMT(pVM);
|
---|
1250 | AssertReturn(!(cb & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
1251 | AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
1252 | AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
|
---|
1253 | AssertReturn(*pszDesc, VERR_INVALID_PARAMETER);
|
---|
1254 |
|
---|
1255 | /*
|
---|
1256 | * Make sure there's a RAM range structure for the region.
|
---|
1257 | */
|
---|
1258 | int rc;
|
---|
1259 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
1260 | bool fRamExists = false;
|
---|
1261 | PPGMRAMRANGE pRamPrev = NULL;
|
---|
1262 | PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3;
|
---|
1263 | while (pRam && GCPhysLast >= pRam->GCPhys)
|
---|
1264 | {
|
---|
1265 | if ( GCPhysLast >= pRam->GCPhys
|
---|
1266 | && GCPhys <= pRam->GCPhysLast)
|
---|
1267 | {
|
---|
1268 | /* Simplification: all within the same range. */
|
---|
1269 | AssertLogRelMsgReturn( GCPhys >= pRam->GCPhys
|
---|
1270 | && GCPhysLast <= pRam->GCPhysLast,
|
---|
1271 | ("%RGp-%RGp (MMIO/%s) falls partly outside %RGp-%RGp (%s)\n",
|
---|
1272 | GCPhys, GCPhysLast, pszDesc,
|
---|
1273 | pRam->GCPhys, pRam->GCPhysLast, pRam->pszDesc),
|
---|
1274 | VERR_PGM_RAM_CONFLICT);
|
---|
1275 |
|
---|
1276 | /* Check that it's all RAM or MMIO pages. */
|
---|
1277 | PCPGMPAGE pPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
1278 | uint32_t cLeft = cb >> PAGE_SHIFT;
|
---|
1279 | while (cLeft-- > 0)
|
---|
1280 | {
|
---|
1281 | AssertLogRelMsgReturn( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM
|
---|
1282 | || PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO,
|
---|
1283 | ("%RGp-%RGp (MMIO/%s): %RGp is not a RAM or MMIO page - type=%d desc=%s\n",
|
---|
1284 | GCPhys, GCPhysLast, pszDesc, PGM_PAGE_GET_TYPE(pPage), pRam->pszDesc),
|
---|
1285 | VERR_PGM_RAM_CONFLICT);
|
---|
1286 | pPage++;
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | /* Looks good. */
|
---|
1290 | fRamExists = true;
|
---|
1291 | break;
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | /* next */
|
---|
1295 | pRamPrev = pRam;
|
---|
1296 | pRam = pRam->pNextR3;
|
---|
1297 | }
|
---|
1298 | PPGMRAMRANGE pNew;
|
---|
1299 | if (fRamExists)
|
---|
1300 | {
|
---|
1301 | pNew = NULL;
|
---|
1302 |
|
---|
1303 | /*
|
---|
1304 | * Make all the pages in the range MMIO/ZERO pages, freeing any
|
---|
1305 | * RAM pages currently mapped here. This might not be 100% correct
|
---|
1306 | * for PCI memory, but we're doing the same thing for MMIO2 pages.
|
---|
1307 | */
|
---|
1308 | rc = pgmLock(pVM);
|
---|
1309 | if (RT_SUCCESS(rc))
|
---|
1310 | {
|
---|
1311 | rc = pgmR3PhysFreePageRange(pVM, pRam, GCPhys, GCPhysLast, PGMPAGETYPE_MMIO);
|
---|
1312 | pgmUnlock(pVM);
|
---|
1313 | }
|
---|
1314 | AssertRCReturn(rc, rc);
|
---|
1315 | }
|
---|
1316 | else
|
---|
1317 | {
|
---|
1318 | pgmLock(pVM);
|
---|
1319 |
|
---|
1320 | /*
|
---|
1321 | * No RAM range, insert an ad-hoc one.
|
---|
1322 | *
|
---|
1323 | * Note that we don't have to tell REM about this range because
|
---|
1324 | * PGMHandlerPhysicalRegisterEx will do that for us.
|
---|
1325 | */
|
---|
1326 | Log(("PGMR3PhysMMIORegister: Adding ad-hoc MMIO range for %RGp-%RGp %s\n", GCPhys, GCPhysLast, pszDesc));
|
---|
1327 |
|
---|
1328 | const uint32_t cPages = cb >> PAGE_SHIFT;
|
---|
1329 | const size_t cbRamRange = RT_OFFSETOF(PGMRAMRANGE, aPages[cPages]);
|
---|
1330 | rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMRAMRANGE, aPages[cPages]), 16, MM_TAG_PGM_PHYS, (void **)&pNew);
|
---|
1331 | AssertLogRelMsgRCReturn(rc, ("cbRamRange=%zu\n", cbRamRange), rc);
|
---|
1332 |
|
---|
1333 | /* Initialize the range. */
|
---|
1334 | pNew->pSelfR0 = MMHyperCCToR0(pVM, pNew);
|
---|
1335 | pNew->pSelfRC = MMHyperCCToRC(pVM, pNew);
|
---|
1336 | pNew->GCPhys = GCPhys;
|
---|
1337 | pNew->GCPhysLast = GCPhysLast;
|
---|
1338 | pNew->cb = cb;
|
---|
1339 | pNew->pszDesc = pszDesc;
|
---|
1340 | pNew->fFlags = 0; /** @todo add some kind of ad-hoc flag? */
|
---|
1341 |
|
---|
1342 | pNew->pvR3 = NULL;
|
---|
1343 |
|
---|
1344 | uint32_t iPage = cPages;
|
---|
1345 | while (iPage-- > 0)
|
---|
1346 | PGM_PAGE_INIT_ZERO_REAL(&pNew->aPages[iPage], pVM, PGMPAGETYPE_MMIO);
|
---|
1347 | Assert(PGM_PAGE_GET_TYPE(&pNew->aPages[0]) == PGMPAGETYPE_MMIO);
|
---|
1348 |
|
---|
1349 | /* update the page count stats. */
|
---|
1350 | pVM->pgm.s.cZeroPages += cPages;
|
---|
1351 | pVM->pgm.s.cAllPages += cPages;
|
---|
1352 |
|
---|
1353 | /* link it */
|
---|
1354 | pgmR3PhysLinkRamRange(pVM, pNew, pRamPrev);
|
---|
1355 |
|
---|
1356 | pgmUnlock(pVM);
|
---|
1357 | }
|
---|
1358 |
|
---|
1359 | /*
|
---|
1360 | * Register the access handler.
|
---|
1361 | */
|
---|
1362 | rc = PGMHandlerPhysicalRegisterEx(pVM, PGMPHYSHANDLERTYPE_MMIO, GCPhys, GCPhysLast,
|
---|
1363 | pfnHandlerR3, pvUserR3,
|
---|
1364 | pfnHandlerR0, pvUserR0,
|
---|
1365 | pfnHandlerRC, pvUserRC, pszDesc);
|
---|
1366 | if ( RT_FAILURE(rc)
|
---|
1367 | && !fRamExists)
|
---|
1368 | {
|
---|
1369 | pVM->pgm.s.cZeroPages -= cb >> PAGE_SHIFT;
|
---|
1370 | pVM->pgm.s.cAllPages -= cb >> PAGE_SHIFT;
|
---|
1371 |
|
---|
1372 | /* remove the ad-hoc range. */
|
---|
1373 | pgmR3PhysUnlinkRamRange2(pVM, pNew, pRamPrev);
|
---|
1374 | pNew->cb = pNew->GCPhys = pNew->GCPhysLast = NIL_RTGCPHYS;
|
---|
1375 | MMHyperFree(pVM, pRam);
|
---|
1376 | }
|
---|
1377 |
|
---|
1378 | return rc;
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 |
|
---|
1382 | /**
|
---|
1383 | * This is the interface IOM is using to register an MMIO region.
|
---|
1384 | *
|
---|
1385 | * It will take care of calling PGMHandlerPhysicalDeregister and clean up
|
---|
1386 | * any ad-hoc PGMRAMRANGE left behind.
|
---|
1387 | *
|
---|
1388 | * @returns VBox status code.
|
---|
1389 | * @param pVM Pointer to the shared VM structure.
|
---|
1390 | * @param GCPhys The start of the MMIO region.
|
---|
1391 | * @param cb The size of the MMIO region.
|
---|
1392 | */
|
---|
1393 | VMMR3DECL(int) PGMR3PhysMMIODeregister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb)
|
---|
1394 | {
|
---|
1395 | VM_ASSERT_EMT(pVM);
|
---|
1396 |
|
---|
1397 | /*
|
---|
1398 | * First deregister the handler, then check if we should remove the ram range.
|
---|
1399 | */
|
---|
1400 | int rc = PGMHandlerPhysicalDeregister(pVM, GCPhys);
|
---|
1401 | if (RT_SUCCESS(rc))
|
---|
1402 | {
|
---|
1403 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
1404 | PPGMRAMRANGE pRamPrev = NULL;
|
---|
1405 | PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3;
|
---|
1406 | while (pRam && GCPhysLast >= pRam->GCPhys)
|
---|
1407 | {
|
---|
1408 | /** @todo We're being a bit too careful here. rewrite. */
|
---|
1409 | if ( GCPhysLast == pRam->GCPhysLast
|
---|
1410 | && GCPhys == pRam->GCPhys)
|
---|
1411 | {
|
---|
1412 | Assert(pRam->cb == cb);
|
---|
1413 |
|
---|
1414 | /*
|
---|
1415 | * See if all the pages are dead MMIO pages.
|
---|
1416 | */
|
---|
1417 | uint32_t const cPages = cb >> PAGE_SHIFT;
|
---|
1418 | bool fAllMMIO = true;
|
---|
1419 | uint32_t iPage = 0;
|
---|
1420 | uint32_t cLeft = cPages;
|
---|
1421 | while (cLeft-- > 0)
|
---|
1422 | {
|
---|
1423 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
1424 | if ( PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO
|
---|
1425 | /*|| not-out-of-action later */)
|
---|
1426 | {
|
---|
1427 | fAllMMIO = false;
|
---|
1428 | Assert(PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO2_ALIAS_MMIO);
|
---|
1429 | AssertMsgFailed(("%RGp %R[pgmpage]\n", pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), pPage));
|
---|
1430 | break;
|
---|
1431 | }
|
---|
1432 | Assert(PGM_PAGE_IS_ZERO(pPage));
|
---|
1433 | pPage++;
|
---|
1434 | }
|
---|
1435 | if (fAllMMIO)
|
---|
1436 | {
|
---|
1437 | /*
|
---|
1438 | * Ad-hoc range, unlink and free it.
|
---|
1439 | */
|
---|
1440 | Log(("PGMR3PhysMMIODeregister: Freeing ad-hoc MMIO range for %RGp-%RGp %s\n",
|
---|
1441 | GCPhys, GCPhysLast, pRam->pszDesc));
|
---|
1442 |
|
---|
1443 | pVM->pgm.s.cAllPages -= cPages;
|
---|
1444 | pVM->pgm.s.cZeroPages -= cPages;
|
---|
1445 |
|
---|
1446 | pgmR3PhysUnlinkRamRange2(pVM, pRam, pRamPrev);
|
---|
1447 | pRam->cb = pRam->GCPhys = pRam->GCPhysLast = NIL_RTGCPHYS;
|
---|
1448 | MMHyperFree(pVM, pRam);
|
---|
1449 | break;
|
---|
1450 | }
|
---|
1451 | }
|
---|
1452 |
|
---|
1453 | /*
|
---|
1454 | * Range match? It will all be within one range (see PGMAllHandler.cpp).
|
---|
1455 | */
|
---|
1456 | if ( GCPhysLast >= pRam->GCPhys
|
---|
1457 | && GCPhys <= pRam->GCPhysLast)
|
---|
1458 | {
|
---|
1459 | Assert(GCPhys >= pRam->GCPhys);
|
---|
1460 | Assert(GCPhysLast <= pRam->GCPhysLast);
|
---|
1461 |
|
---|
1462 | /*
|
---|
1463 | * Turn the pages back into RAM pages.
|
---|
1464 | */
|
---|
1465 | uint32_t iPage = (GCPhys - pRam->GCPhys) >> PAGE_SHIFT;
|
---|
1466 | uint32_t cLeft = cb >> PAGE_SHIFT;
|
---|
1467 | while (cLeft--)
|
---|
1468 | {
|
---|
1469 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
1470 | AssertMsg(PGM_PAGE_IS_MMIO(pPage), ("%RGp %R[pgmpage]\n", pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), pPage));
|
---|
1471 | AssertMsg(PGM_PAGE_IS_ZERO(pPage), ("%RGp %R[pgmpage]\n", pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), pPage));
|
---|
1472 | if (PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO)
|
---|
1473 | PGM_PAGE_SET_TYPE(pPage, PGMPAGETYPE_RAM);
|
---|
1474 | }
|
---|
1475 | break;
|
---|
1476 | }
|
---|
1477 |
|
---|
1478 | /* next */
|
---|
1479 | pRamPrev = pRam;
|
---|
1480 | pRam = pRam->pNextR3;
|
---|
1481 | }
|
---|
1482 | }
|
---|
1483 |
|
---|
1484 | return rc;
|
---|
1485 | }
|
---|
1486 |
|
---|
1487 |
|
---|
1488 | /**
|
---|
1489 | * Locate a MMIO2 range.
|
---|
1490 | *
|
---|
1491 | * @returns Pointer to the MMIO2 range.
|
---|
1492 | * @param pVM Pointer to the shared VM structure.
|
---|
1493 | * @param pDevIns The device instance owning the region.
|
---|
1494 | * @param iRegion The region.
|
---|
1495 | */
|
---|
1496 | DECLINLINE(PPGMMMIO2RANGE) pgmR3PhysMMIO2Find(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion)
|
---|
1497 | {
|
---|
1498 | /*
|
---|
1499 | * Search the list.
|
---|
1500 | */
|
---|
1501 | for (PPGMMMIO2RANGE pCur = pVM->pgm.s.pMmio2RangesR3; pCur; pCur = pCur->pNextR3)
|
---|
1502 | if ( pCur->pDevInsR3 == pDevIns
|
---|
1503 | && pCur->iRegion == iRegion)
|
---|
1504 | return pCur;
|
---|
1505 | return NULL;
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 |
|
---|
1509 | /**
|
---|
1510 | * Allocate and register an MMIO2 region.
|
---|
1511 | *
|
---|
1512 | * As mentioned elsewhere, MMIO2 is just RAM spelled differently. It's
|
---|
1513 | * RAM associated with a device. It is also non-shared memory with a
|
---|
1514 | * permanent ring-3 mapping and page backing (presently).
|
---|
1515 | *
|
---|
1516 | * A MMIO2 range may overlap with base memory if a lot of RAM
|
---|
1517 | * is configured for the VM, in which case we'll drop the base
|
---|
1518 | * memory pages. Presently we will make no attempt to preserve
|
---|
1519 | * anything that happens to be present in the base memory that
|
---|
1520 | * is replaced, this is of course incorrectly but it's too much
|
---|
1521 | * effort.
|
---|
1522 | *
|
---|
1523 | * @returns VBox status code.
|
---|
1524 | * @retval VINF_SUCCESS on success, *ppv pointing to the R3 mapping of the memory.
|
---|
1525 | * @retval VERR_ALREADY_EXISTS if the region already exists.
|
---|
1526 | *
|
---|
1527 | * @param pVM Pointer to the shared VM structure.
|
---|
1528 | * @param pDevIns The device instance owning the region.
|
---|
1529 | * @param iRegion The region number. If the MMIO2 memory is a PCI I/O region
|
---|
1530 | * this number has to be the number of that region. Otherwise
|
---|
1531 | * it can be any number safe UINT8_MAX.
|
---|
1532 | * @param cb The size of the region. Must be page aligned.
|
---|
1533 | * @param fFlags Reserved for future use, must be zero.
|
---|
1534 | * @param ppv Where to store the pointer to the ring-3 mapping of the memory.
|
---|
1535 | * @param pszDesc The description.
|
---|
1536 | */
|
---|
1537 | VMMR3DECL(int) PGMR3PhysMMIO2Register(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS cb, uint32_t fFlags, void **ppv, const char *pszDesc)
|
---|
1538 | {
|
---|
1539 | /*
|
---|
1540 | * Validate input.
|
---|
1541 | */
|
---|
1542 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
1543 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
1544 | AssertReturn(iRegion <= UINT8_MAX, VERR_INVALID_PARAMETER);
|
---|
1545 | AssertPtrReturn(ppv, VERR_INVALID_POINTER);
|
---|
1546 | AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
|
---|
1547 | AssertReturn(*pszDesc, VERR_INVALID_PARAMETER);
|
---|
1548 | AssertReturn(pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion) == NULL, VERR_ALREADY_EXISTS);
|
---|
1549 | AssertReturn(!(cb & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
1550 | AssertReturn(cb, VERR_INVALID_PARAMETER);
|
---|
1551 | AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
|
---|
1552 |
|
---|
1553 | const uint32_t cPages = cb >> PAGE_SHIFT;
|
---|
1554 | AssertLogRelReturn(((RTGCPHYS)cPages << PAGE_SHIFT) == cb, VERR_INVALID_PARAMETER);
|
---|
1555 | AssertLogRelReturn(cPages <= INT32_MAX / 2, VERR_NO_MEMORY);
|
---|
1556 |
|
---|
1557 | /*
|
---|
1558 | * For the 2nd+ instance, mangle the description string so it's unique.
|
---|
1559 | */
|
---|
1560 | if (pDevIns->iInstance > 0) /** @todo Move to PDMDevHlp.cpp and use a real string cache. */
|
---|
1561 | {
|
---|
1562 | pszDesc = MMR3HeapAPrintf(pVM, MM_TAG_PGM_PHYS, "%s [%u]", pszDesc, pDevIns->iInstance);
|
---|
1563 | if (!pszDesc)
|
---|
1564 | return VERR_NO_MEMORY;
|
---|
1565 | }
|
---|
1566 |
|
---|
1567 | /*
|
---|
1568 | * Try reserve and allocate the backing memory first as this is what is
|
---|
1569 | * most likely to fail.
|
---|
1570 | */
|
---|
1571 | int rc = MMR3AdjustFixedReservation(pVM, cPages, pszDesc);
|
---|
1572 | if (RT_SUCCESS(rc))
|
---|
1573 | {
|
---|
1574 | void *pvPages;
|
---|
1575 | PSUPPAGE paPages = (PSUPPAGE)RTMemTmpAlloc(cPages * sizeof(SUPPAGE));
|
---|
1576 | if (RT_SUCCESS(rc))
|
---|
1577 | rc = SUPR3PageAllocEx(cPages, 0 /*fFlags*/, &pvPages, NULL /*pR0Ptr*/, paPages);
|
---|
1578 | if (RT_SUCCESS(rc))
|
---|
1579 | {
|
---|
1580 | memset(pvPages, 0, cPages * PAGE_SIZE);
|
---|
1581 |
|
---|
1582 | /*
|
---|
1583 | * Create the MMIO2 range record for it.
|
---|
1584 | */
|
---|
1585 | const size_t cbRange = RT_OFFSETOF(PGMMMIO2RANGE, RamRange.aPages[cPages]);
|
---|
1586 | PPGMMMIO2RANGE pNew;
|
---|
1587 | rc = MMR3HyperAllocOnceNoRel(pVM, cbRange, 0, MM_TAG_PGM_PHYS, (void **)&pNew);
|
---|
1588 | AssertLogRelMsgRC(rc, ("cbRamRange=%zu\n", cbRange));
|
---|
1589 | if (RT_SUCCESS(rc))
|
---|
1590 | {
|
---|
1591 | pNew->pDevInsR3 = pDevIns;
|
---|
1592 | pNew->pvR3 = pvPages;
|
---|
1593 | //pNew->pNext = NULL;
|
---|
1594 | //pNew->fMapped = false;
|
---|
1595 | //pNew->fOverlapping = false;
|
---|
1596 | pNew->iRegion = iRegion;
|
---|
1597 | pNew->RamRange.pSelfR0 = MMHyperCCToR0(pVM, &pNew->RamRange);
|
---|
1598 | pNew->RamRange.pSelfRC = MMHyperCCToRC(pVM, &pNew->RamRange);
|
---|
1599 | pNew->RamRange.GCPhys = NIL_RTGCPHYS;
|
---|
1600 | pNew->RamRange.GCPhysLast = NIL_RTGCPHYS;
|
---|
1601 | pNew->RamRange.pszDesc = pszDesc;
|
---|
1602 | pNew->RamRange.cb = cb;
|
---|
1603 | //pNew->RamRange.fFlags = 0; /// @todo MMIO2 flag?
|
---|
1604 |
|
---|
1605 | pNew->RamRange.pvR3 = pvPages;
|
---|
1606 |
|
---|
1607 | uint32_t iPage = cPages;
|
---|
1608 | while (iPage-- > 0)
|
---|
1609 | {
|
---|
1610 | PGM_PAGE_INIT(&pNew->RamRange.aPages[iPage],
|
---|
1611 | paPages[iPage].Phys & X86_PTE_PAE_PG_MASK, NIL_GMM_PAGEID,
|
---|
1612 | PGMPAGETYPE_MMIO2, PGM_PAGE_STATE_ALLOCATED);
|
---|
1613 | }
|
---|
1614 |
|
---|
1615 | /* update page count stats */
|
---|
1616 | pVM->pgm.s.cAllPages += cPages;
|
---|
1617 | pVM->pgm.s.cPrivatePages += cPages;
|
---|
1618 |
|
---|
1619 | /*
|
---|
1620 | * Link it into the list.
|
---|
1621 | * Since there is no particular order, just push it.
|
---|
1622 | */
|
---|
1623 | pgmLock(pVM);
|
---|
1624 | pNew->pNextR3 = pVM->pgm.s.pMmio2RangesR3;
|
---|
1625 | pVM->pgm.s.pMmio2RangesR3 = pNew;
|
---|
1626 | pgmUnlock(pVM);
|
---|
1627 |
|
---|
1628 | *ppv = pvPages;
|
---|
1629 | RTMemTmpFree(paPages);
|
---|
1630 | return VINF_SUCCESS;
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 | SUPR3PageFreeEx(pvPages, cPages);
|
---|
1634 | }
|
---|
1635 | RTMemTmpFree(paPages);
|
---|
1636 | MMR3AdjustFixedReservation(pVM, -(int32_t)cPages, pszDesc);
|
---|
1637 | }
|
---|
1638 | if (pDevIns->iInstance > 0)
|
---|
1639 | MMR3HeapFree((void *)pszDesc);
|
---|
1640 | return rc;
|
---|
1641 | }
|
---|
1642 |
|
---|
1643 |
|
---|
1644 | /**
|
---|
1645 | * Deregisters and frees an MMIO2 region.
|
---|
1646 | *
|
---|
1647 | * Any physical (and virtual) access handlers registered for the region must
|
---|
1648 | * be deregistered before calling this function.
|
---|
1649 | *
|
---|
1650 | * @returns VBox status code.
|
---|
1651 | * @param pVM Pointer to the shared VM structure.
|
---|
1652 | * @param pDevIns The device instance owning the region.
|
---|
1653 | * @param iRegion The region. If it's UINT32_MAX it'll be a wildcard match.
|
---|
1654 | */
|
---|
1655 | VMMR3DECL(int) PGMR3PhysMMIO2Deregister(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion)
|
---|
1656 | {
|
---|
1657 | /*
|
---|
1658 | * Validate input.
|
---|
1659 | */
|
---|
1660 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
1661 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
1662 | AssertReturn(iRegion <= UINT8_MAX || iRegion == UINT32_MAX, VERR_INVALID_PARAMETER);
|
---|
1663 |
|
---|
1664 | pgmLock(pVM);
|
---|
1665 | int rc = VINF_SUCCESS;
|
---|
1666 | unsigned cFound = 0;
|
---|
1667 | PPGMMMIO2RANGE pPrev = NULL;
|
---|
1668 | PPGMMMIO2RANGE pCur = pVM->pgm.s.pMmio2RangesR3;
|
---|
1669 | while (pCur)
|
---|
1670 | {
|
---|
1671 | if ( pCur->pDevInsR3 == pDevIns
|
---|
1672 | && ( iRegion == UINT32_MAX
|
---|
1673 | || pCur->iRegion == iRegion))
|
---|
1674 | {
|
---|
1675 | cFound++;
|
---|
1676 |
|
---|
1677 | /*
|
---|
1678 | * Unmap it if it's mapped.
|
---|
1679 | */
|
---|
1680 | if (pCur->fMapped)
|
---|
1681 | {
|
---|
1682 | int rc2 = PGMR3PhysMMIO2Unmap(pVM, pCur->pDevInsR3, pCur->iRegion, pCur->RamRange.GCPhys);
|
---|
1683 | AssertRC(rc2);
|
---|
1684 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
1685 | rc = rc2;
|
---|
1686 | }
|
---|
1687 |
|
---|
1688 | /*
|
---|
1689 | * Unlink it
|
---|
1690 | */
|
---|
1691 | PPGMMMIO2RANGE pNext = pCur->pNextR3;
|
---|
1692 | if (pPrev)
|
---|
1693 | pPrev->pNextR3 = pNext;
|
---|
1694 | else
|
---|
1695 | pVM->pgm.s.pMmio2RangesR3 = pNext;
|
---|
1696 | pCur->pNextR3 = NULL;
|
---|
1697 |
|
---|
1698 | /*
|
---|
1699 | * Free the memory.
|
---|
1700 | */
|
---|
1701 | int rc2 = SUPR3PageFreeEx(pCur->pvR3, pCur->RamRange.cb >> PAGE_SHIFT);
|
---|
1702 | AssertRC(rc2);
|
---|
1703 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
1704 | rc = rc2;
|
---|
1705 |
|
---|
1706 | uint32_t const cPages = pCur->RamRange.cb >> PAGE_SHIFT;
|
---|
1707 | rc2 = MMR3AdjustFixedReservation(pVM, -(int32_t)cPages, pCur->RamRange.pszDesc);
|
---|
1708 | AssertRC(rc2);
|
---|
1709 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
1710 | rc = rc2;
|
---|
1711 |
|
---|
1712 | /* we're leaking hyper memory here if done at runtime. */
|
---|
1713 | Assert( VMR3GetState(pVM) == VMSTATE_OFF
|
---|
1714 | || VMR3GetState(pVM) == VMSTATE_DESTROYING
|
---|
1715 | || VMR3GetState(pVM) == VMSTATE_TERMINATED
|
---|
1716 | || VMR3GetState(pVM) == VMSTATE_CREATING);
|
---|
1717 | /*rc = MMHyperFree(pVM, pCur);
|
---|
1718 | AssertRCReturn(rc, rc); - not safe, see the alloc call. */
|
---|
1719 |
|
---|
1720 |
|
---|
1721 | /* update page count stats */
|
---|
1722 | pVM->pgm.s.cAllPages -= cPages;
|
---|
1723 | pVM->pgm.s.cPrivatePages -= cPages;
|
---|
1724 |
|
---|
1725 | /* next */
|
---|
1726 | pCur = pNext;
|
---|
1727 | }
|
---|
1728 | else
|
---|
1729 | {
|
---|
1730 | pPrev = pCur;
|
---|
1731 | pCur = pCur->pNextR3;
|
---|
1732 | }
|
---|
1733 | }
|
---|
1734 | pgmUnlock(pVM);
|
---|
1735 | return !cFound && iRegion != UINT32_MAX ? VERR_NOT_FOUND : rc;
|
---|
1736 | }
|
---|
1737 |
|
---|
1738 |
|
---|
1739 | /**
|
---|
1740 | * Maps a MMIO2 region.
|
---|
1741 | *
|
---|
1742 | * This is done when a guest / the bios / state loading changes the
|
---|
1743 | * PCI config. The replacing of base memory has the same restrictions
|
---|
1744 | * as during registration, of course.
|
---|
1745 | *
|
---|
1746 | * @returns VBox status code.
|
---|
1747 | *
|
---|
1748 | * @param pVM Pointer to the shared VM structure.
|
---|
1749 | * @param pDevIns The
|
---|
1750 | */
|
---|
1751 | VMMR3DECL(int) PGMR3PhysMMIO2Map(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS GCPhys)
|
---|
1752 | {
|
---|
1753 | /*
|
---|
1754 | * Validate input
|
---|
1755 | */
|
---|
1756 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
1757 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
1758 | AssertReturn(iRegion <= UINT8_MAX, VERR_INVALID_PARAMETER);
|
---|
1759 | AssertReturn(GCPhys != NIL_RTGCPHYS, VERR_INVALID_PARAMETER);
|
---|
1760 | AssertReturn(GCPhys != 0, VERR_INVALID_PARAMETER);
|
---|
1761 | AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
1762 |
|
---|
1763 | PPGMMMIO2RANGE pCur = pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion);
|
---|
1764 | AssertReturn(pCur, VERR_NOT_FOUND);
|
---|
1765 | AssertReturn(!pCur->fMapped, VERR_WRONG_ORDER);
|
---|
1766 | Assert(pCur->RamRange.GCPhys == NIL_RTGCPHYS);
|
---|
1767 | Assert(pCur->RamRange.GCPhysLast == NIL_RTGCPHYS);
|
---|
1768 |
|
---|
1769 | const RTGCPHYS GCPhysLast = GCPhys + pCur->RamRange.cb - 1;
|
---|
1770 | AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
|
---|
1771 |
|
---|
1772 | /*
|
---|
1773 | * Find our location in the ram range list, checking for
|
---|
1774 | * restriction we don't bother implementing yet (partially overlapping).
|
---|
1775 | */
|
---|
1776 | bool fRamExists = false;
|
---|
1777 | PPGMRAMRANGE pRamPrev = NULL;
|
---|
1778 | PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3;
|
---|
1779 | while (pRam && GCPhysLast >= pRam->GCPhys)
|
---|
1780 | {
|
---|
1781 | if ( GCPhys <= pRam->GCPhysLast
|
---|
1782 | && GCPhysLast >= pRam->GCPhys)
|
---|
1783 | {
|
---|
1784 | /* completely within? */
|
---|
1785 | AssertLogRelMsgReturn( GCPhys >= pRam->GCPhys
|
---|
1786 | && GCPhysLast <= pRam->GCPhysLast,
|
---|
1787 | ("%RGp-%RGp (MMIO2/%s) falls partly outside %RGp-%RGp (%s)\n",
|
---|
1788 | GCPhys, GCPhysLast, pCur->RamRange.pszDesc,
|
---|
1789 | pRam->GCPhys, pRam->GCPhysLast, pRam->pszDesc),
|
---|
1790 | VERR_PGM_RAM_CONFLICT);
|
---|
1791 | fRamExists = true;
|
---|
1792 | break;
|
---|
1793 | }
|
---|
1794 |
|
---|
1795 | /* next */
|
---|
1796 | pRamPrev = pRam;
|
---|
1797 | pRam = pRam->pNextR3;
|
---|
1798 | }
|
---|
1799 | if (fRamExists)
|
---|
1800 | {
|
---|
1801 | PPGMPAGE pPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
1802 | uint32_t cPagesLeft = pCur->RamRange.cb >> PAGE_SHIFT;
|
---|
1803 | while (cPagesLeft-- > 0)
|
---|
1804 | {
|
---|
1805 | AssertLogRelMsgReturn(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM,
|
---|
1806 | ("%RGp isn't a RAM page (%d) - mapping %RGp-%RGp (MMIO2/%s).\n",
|
---|
1807 | GCPhys, PGM_PAGE_GET_TYPE(pPage), GCPhys, GCPhysLast, pCur->RamRange.pszDesc),
|
---|
1808 | VERR_PGM_RAM_CONFLICT);
|
---|
1809 | pPage++;
|
---|
1810 | }
|
---|
1811 | }
|
---|
1812 | Log(("PGMR3PhysMMIO2Map: %RGp-%RGp fRamExists=%RTbool %s\n",
|
---|
1813 | GCPhys, GCPhysLast, fRamExists, pCur->RamRange.pszDesc));
|
---|
1814 |
|
---|
1815 | /*
|
---|
1816 | * Make the changes.
|
---|
1817 | */
|
---|
1818 | pgmLock(pVM);
|
---|
1819 |
|
---|
1820 | pCur->RamRange.GCPhys = GCPhys;
|
---|
1821 | pCur->RamRange.GCPhysLast = GCPhysLast;
|
---|
1822 | pCur->fMapped = true;
|
---|
1823 | pCur->fOverlapping = fRamExists;
|
---|
1824 |
|
---|
1825 | if (fRamExists)
|
---|
1826 | {
|
---|
1827 | /** @todo use pgmR3PhysFreePageRange here. */
|
---|
1828 | uint32_t cPendingPages = 0;
|
---|
1829 | PGMMFREEPAGESREQ pReq;
|
---|
1830 | int rc = GMMR3FreePagesPrepare(pVM, &pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE, GMMACCOUNT_BASE);
|
---|
1831 | AssertLogRelRCReturn(rc, rc);
|
---|
1832 |
|
---|
1833 | /* replace the pages, freeing all present RAM pages. */
|
---|
1834 | PPGMPAGE pPageSrc = &pCur->RamRange.aPages[0];
|
---|
1835 | PPGMPAGE pPageDst = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
1836 | uint32_t cPagesLeft = pCur->RamRange.cb >> PAGE_SHIFT;
|
---|
1837 | while (cPagesLeft-- > 0)
|
---|
1838 | {
|
---|
1839 | rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, pPageDst, GCPhys);
|
---|
1840 | AssertLogRelRCReturn(rc, rc); /* We're done for if this goes wrong. */
|
---|
1841 |
|
---|
1842 | RTHCPHYS const HCPhys = PGM_PAGE_GET_HCPHYS(pPageSrc);
|
---|
1843 | PGM_PAGE_SET_HCPHYS(pPageDst, HCPhys);
|
---|
1844 | PGM_PAGE_SET_TYPE(pPageDst, PGMPAGETYPE_MMIO2);
|
---|
1845 | PGM_PAGE_SET_STATE(pPageDst, PGM_PAGE_STATE_ALLOCATED);
|
---|
1846 |
|
---|
1847 | pVM->pgm.s.cZeroPages--;
|
---|
1848 | GCPhys += PAGE_SIZE;
|
---|
1849 | pPageSrc++;
|
---|
1850 | pPageDst++;
|
---|
1851 | }
|
---|
1852 |
|
---|
1853 | if (cPendingPages)
|
---|
1854 | {
|
---|
1855 | rc = GMMR3FreePagesPerform(pVM, pReq, cPendingPages);
|
---|
1856 | AssertLogRelRCReturn(rc, rc);
|
---|
1857 | }
|
---|
1858 | GMMR3FreePagesCleanup(pReq);
|
---|
1859 | pgmUnlock(pVM);
|
---|
1860 | }
|
---|
1861 | else
|
---|
1862 | {
|
---|
1863 | RTGCPHYS cb = pCur->RamRange.cb;
|
---|
1864 |
|
---|
1865 | /* link in the ram range */
|
---|
1866 | pgmR3PhysLinkRamRange(pVM, &pCur->RamRange, pRamPrev);
|
---|
1867 | pgmUnlock(pVM);
|
---|
1868 |
|
---|
1869 | REMR3NotifyPhysRamRegister(pVM, GCPhys, cb, REM_NOTIFY_PHYS_RAM_FLAGS_MMIO2);
|
---|
1870 | }
|
---|
1871 |
|
---|
1872 | return VINF_SUCCESS;
|
---|
1873 | }
|
---|
1874 |
|
---|
1875 |
|
---|
1876 | /**
|
---|
1877 | * Unmaps a MMIO2 region.
|
---|
1878 | *
|
---|
1879 | * This is done when a guest / the bios / state loading changes the
|
---|
1880 | * PCI config. The replacing of base memory has the same restrictions
|
---|
1881 | * as during registration, of course.
|
---|
1882 | */
|
---|
1883 | VMMR3DECL(int) PGMR3PhysMMIO2Unmap(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS GCPhys)
|
---|
1884 | {
|
---|
1885 | bool fInformREM = false;
|
---|
1886 | RTGCPHYS GCPhysRangeREM;
|
---|
1887 | RTGCPHYS cbRangeREM;
|
---|
1888 |
|
---|
1889 | /*
|
---|
1890 | * Validate input
|
---|
1891 | */
|
---|
1892 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
1893 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
1894 | AssertReturn(iRegion <= UINT8_MAX, VERR_INVALID_PARAMETER);
|
---|
1895 | AssertReturn(GCPhys != NIL_RTGCPHYS, VERR_INVALID_PARAMETER);
|
---|
1896 | AssertReturn(GCPhys != 0, VERR_INVALID_PARAMETER);
|
---|
1897 | AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
1898 |
|
---|
1899 | PPGMMMIO2RANGE pCur = pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion);
|
---|
1900 | AssertReturn(pCur, VERR_NOT_FOUND);
|
---|
1901 | AssertReturn(pCur->fMapped, VERR_WRONG_ORDER);
|
---|
1902 | AssertReturn(pCur->RamRange.GCPhys == GCPhys, VERR_INVALID_PARAMETER);
|
---|
1903 | Assert(pCur->RamRange.GCPhysLast != NIL_RTGCPHYS);
|
---|
1904 |
|
---|
1905 | Log(("PGMR3PhysMMIO2Unmap: %RGp-%RGp %s\n",
|
---|
1906 | pCur->RamRange.GCPhys, pCur->RamRange.GCPhysLast, pCur->RamRange.pszDesc));
|
---|
1907 |
|
---|
1908 | /*
|
---|
1909 | * Unmap it.
|
---|
1910 | */
|
---|
1911 | pgmLock(pVM);
|
---|
1912 |
|
---|
1913 | if (pCur->fOverlapping)
|
---|
1914 | {
|
---|
1915 | /* Restore the RAM pages we've replaced. */
|
---|
1916 | PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3;
|
---|
1917 | while (pRam->GCPhys > pCur->RamRange.GCPhysLast)
|
---|
1918 | pRam = pRam->pNextR3;
|
---|
1919 |
|
---|
1920 | RTHCPHYS const HCPhysZeroPg = pVM->pgm.s.HCPhysZeroPg;
|
---|
1921 | Assert(HCPhysZeroPg != 0 && HCPhysZeroPg != NIL_RTHCPHYS);
|
---|
1922 | PPGMPAGE pPageDst = &pRam->aPages[(pCur->RamRange.GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
1923 | uint32_t cPagesLeft = pCur->RamRange.cb >> PAGE_SHIFT;
|
---|
1924 | while (cPagesLeft-- > 0)
|
---|
1925 | {
|
---|
1926 | PGM_PAGE_SET_HCPHYS(pPageDst, HCPhysZeroPg);
|
---|
1927 | PGM_PAGE_SET_TYPE(pPageDst, PGMPAGETYPE_RAM);
|
---|
1928 | PGM_PAGE_SET_STATE(pPageDst, PGM_PAGE_STATE_ZERO);
|
---|
1929 | PGM_PAGE_SET_PAGEID(pPageDst, NIL_GMM_PAGEID);
|
---|
1930 |
|
---|
1931 | pVM->pgm.s.cZeroPages++;
|
---|
1932 | pPageDst++;
|
---|
1933 | }
|
---|
1934 | }
|
---|
1935 | else
|
---|
1936 | {
|
---|
1937 | GCPhysRangeREM = pCur->RamRange.GCPhys;
|
---|
1938 | cbRangeREM = pCur->RamRange.cb;
|
---|
1939 | fInformREM = true;
|
---|
1940 |
|
---|
1941 | pgmR3PhysUnlinkRamRange(pVM, &pCur->RamRange);
|
---|
1942 | }
|
---|
1943 |
|
---|
1944 | pCur->RamRange.GCPhys = NIL_RTGCPHYS;
|
---|
1945 | pCur->RamRange.GCPhysLast = NIL_RTGCPHYS;
|
---|
1946 | pCur->fOverlapping = false;
|
---|
1947 | pCur->fMapped = false;
|
---|
1948 |
|
---|
1949 | pgmUnlock(pVM);
|
---|
1950 |
|
---|
1951 | if (fInformREM)
|
---|
1952 | REMR3NotifyPhysRamDeregister(pVM, GCPhysRangeREM, cbRangeREM);
|
---|
1953 |
|
---|
1954 | return VINF_SUCCESS;
|
---|
1955 | }
|
---|
1956 |
|
---|
1957 |
|
---|
1958 | /**
|
---|
1959 | * Checks if the given address is an MMIO2 base address or not.
|
---|
1960 | *
|
---|
1961 | * @returns true/false accordingly.
|
---|
1962 | * @param pVM Pointer to the shared VM structure.
|
---|
1963 | * @param pDevIns The owner of the memory, optional.
|
---|
1964 | * @param GCPhys The address to check.
|
---|
1965 | */
|
---|
1966 | VMMR3DECL(bool) PGMR3PhysMMIO2IsBase(PVM pVM, PPDMDEVINS pDevIns, RTGCPHYS GCPhys)
|
---|
1967 | {
|
---|
1968 | /*
|
---|
1969 | * Validate input
|
---|
1970 | */
|
---|
1971 | VM_ASSERT_EMT_RETURN(pVM, false);
|
---|
1972 | AssertPtrReturn(pDevIns, false);
|
---|
1973 | AssertReturn(GCPhys != NIL_RTGCPHYS, false);
|
---|
1974 | AssertReturn(GCPhys != 0, false);
|
---|
1975 | AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), false);
|
---|
1976 |
|
---|
1977 | /*
|
---|
1978 | * Search the list.
|
---|
1979 | */
|
---|
1980 | pgmLock(pVM);
|
---|
1981 | for (PPGMMMIO2RANGE pCur = pVM->pgm.s.pMmio2RangesR3; pCur; pCur = pCur->pNextR3)
|
---|
1982 | if (pCur->RamRange.GCPhys == GCPhys)
|
---|
1983 | {
|
---|
1984 | Assert(pCur->fMapped);
|
---|
1985 | pgmUnlock(pVM);
|
---|
1986 | return true;
|
---|
1987 | }
|
---|
1988 | pgmUnlock(pVM);
|
---|
1989 | return false;
|
---|
1990 | }
|
---|
1991 |
|
---|
1992 |
|
---|
1993 | /**
|
---|
1994 | * Gets the HC physical address of a page in the MMIO2 region.
|
---|
1995 | *
|
---|
1996 | * This is API is intended for MMHyper and shouldn't be called
|
---|
1997 | * by anyone else...
|
---|
1998 | *
|
---|
1999 | * @returns VBox status code.
|
---|
2000 | * @param pVM Pointer to the shared VM structure.
|
---|
2001 | * @param pDevIns The owner of the memory, optional.
|
---|
2002 | * @param iRegion The region.
|
---|
2003 | * @param off The page expressed an offset into the MMIO2 region.
|
---|
2004 | * @param pHCPhys Where to store the result.
|
---|
2005 | */
|
---|
2006 | VMMR3DECL(int) PGMR3PhysMMIO2GetHCPhys(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS off, PRTHCPHYS pHCPhys)
|
---|
2007 | {
|
---|
2008 | /*
|
---|
2009 | * Validate input
|
---|
2010 | */
|
---|
2011 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
2012 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
2013 | AssertReturn(iRegion <= UINT8_MAX, VERR_INVALID_PARAMETER);
|
---|
2014 |
|
---|
2015 | pgmLock(pVM);
|
---|
2016 | PPGMMMIO2RANGE pCur = pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion);
|
---|
2017 | AssertReturn(pCur, VERR_NOT_FOUND);
|
---|
2018 | AssertReturn(off < pCur->RamRange.cb, VERR_INVALID_PARAMETER);
|
---|
2019 |
|
---|
2020 | PCPGMPAGE pPage = &pCur->RamRange.aPages[off >> PAGE_SHIFT];
|
---|
2021 | *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage);
|
---|
2022 | pgmUnlock(pVM);
|
---|
2023 | return VINF_SUCCESS;
|
---|
2024 | }
|
---|
2025 |
|
---|
2026 |
|
---|
2027 | /**
|
---|
2028 | * Maps a portion of an MMIO2 region into kernel space (host).
|
---|
2029 | *
|
---|
2030 | * The kernel mapping will become invalid when the MMIO2 memory is deregistered
|
---|
2031 | * or the VM is terminated.
|
---|
2032 | *
|
---|
2033 | * @return VBox status code.
|
---|
2034 | *
|
---|
2035 | * @param pVM Pointer to the shared VM structure.
|
---|
2036 | * @param pDevIns The device owning the MMIO2 memory.
|
---|
2037 | * @param iRegion The region.
|
---|
2038 | * @param off The offset into the region. Must be page aligned.
|
---|
2039 | * @param cb The number of bytes to map. Must be page aligned.
|
---|
2040 | * @param pszDesc Mapping description.
|
---|
2041 | * @param pR0Ptr Where to store the R0 address.
|
---|
2042 | */
|
---|
2043 | VMMR3DECL(int) PGMR3PhysMMIO2MapKernel(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS off, RTGCPHYS cb,
|
---|
2044 | const char *pszDesc, PRTR0PTR pR0Ptr)
|
---|
2045 | {
|
---|
2046 | /*
|
---|
2047 | * Validate input.
|
---|
2048 | */
|
---|
2049 | VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
|
---|
2050 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
2051 | AssertReturn(iRegion <= UINT8_MAX, VERR_INVALID_PARAMETER);
|
---|
2052 |
|
---|
2053 | PPGMMMIO2RANGE pCur = pgmR3PhysMMIO2Find(pVM, pDevIns, iRegion);
|
---|
2054 | AssertReturn(pCur, VERR_NOT_FOUND);
|
---|
2055 | AssertReturn(off < pCur->RamRange.cb, VERR_INVALID_PARAMETER);
|
---|
2056 | AssertReturn(cb <= pCur->RamRange.cb, VERR_INVALID_PARAMETER);
|
---|
2057 | AssertReturn(off + cb <= pCur->RamRange.cb, VERR_INVALID_PARAMETER);
|
---|
2058 |
|
---|
2059 | /*
|
---|
2060 | * Pass the request on to the support library/driver.
|
---|
2061 | */
|
---|
2062 | int rc = SUPR3PageMapKernel(pCur->pvR3, off, cb, 0, pR0Ptr);
|
---|
2063 |
|
---|
2064 | return rc;
|
---|
2065 | }
|
---|
2066 |
|
---|
2067 |
|
---|
2068 | /**
|
---|
2069 | * Registers a ROM image.
|
---|
2070 | *
|
---|
2071 | * Shadowed ROM images requires double the amount of backing memory, so,
|
---|
2072 | * don't use that unless you have to. Shadowing of ROM images is process
|
---|
2073 | * where we can select where the reads go and where the writes go. On real
|
---|
2074 | * hardware the chipset provides means to configure this. We provide
|
---|
2075 | * PGMR3PhysProtectROM() for this purpose.
|
---|
2076 | *
|
---|
2077 | * A read-only copy of the ROM image will always be kept around while we
|
---|
2078 | * will allocate RAM pages for the changes on demand (unless all memory
|
---|
2079 | * is configured to be preallocated).
|
---|
2080 | *
|
---|
2081 | * @returns VBox status.
|
---|
2082 | * @param pVM VM Handle.
|
---|
2083 | * @param pDevIns The device instance owning the ROM.
|
---|
2084 | * @param GCPhys First physical address in the range.
|
---|
2085 | * Must be page aligned!
|
---|
2086 | * @param cbRange The size of the range (in bytes).
|
---|
2087 | * Must be page aligned!
|
---|
2088 | * @param pvBinary Pointer to the binary data backing the ROM image.
|
---|
2089 | * This must be exactly \a cbRange in size.
|
---|
2090 | * @param fFlags Mask of flags. PGMPHYS_ROM_FLAGS_SHADOWED
|
---|
2091 | * and/or PGMPHYS_ROM_FLAGS_PERMANENT_BINARY.
|
---|
2092 | * @param pszDesc Pointer to description string. This must not be freed.
|
---|
2093 | *
|
---|
2094 | * @remark There is no way to remove the rom, automatically on device cleanup or
|
---|
2095 | * manually from the device yet. This isn't difficult in any way, it's
|
---|
2096 | * just not something we expect to be necessary for a while.
|
---|
2097 | */
|
---|
2098 | VMMR3DECL(int) PGMR3PhysRomRegister(PVM pVM, PPDMDEVINS pDevIns, RTGCPHYS GCPhys, RTGCPHYS cb,
|
---|
2099 | const void *pvBinary, uint32_t fFlags, const char *pszDesc)
|
---|
2100 | {
|
---|
2101 | Log(("PGMR3PhysRomRegister: pDevIns=%p GCPhys=%RGp(-%RGp) cb=%RGp pvBinary=%p fFlags=%#x pszDesc=%s\n",
|
---|
2102 | pDevIns, GCPhys, GCPhys + cb, cb, pvBinary, fFlags, pszDesc));
|
---|
2103 |
|
---|
2104 | /*
|
---|
2105 | * Validate input.
|
---|
2106 | */
|
---|
2107 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
2108 | AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
|
---|
2109 | AssertReturn(RT_ALIGN_T(cb, PAGE_SIZE, RTGCPHYS) == cb, VERR_INVALID_PARAMETER);
|
---|
2110 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
2111 | AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
|
---|
2112 | AssertPtrReturn(pvBinary, VERR_INVALID_PARAMETER);
|
---|
2113 | AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
|
---|
2114 | AssertReturn(!(fFlags & ~(PGMPHYS_ROM_FLAGS_SHADOWED | PGMPHYS_ROM_FLAGS_PERMANENT_BINARY)), VERR_INVALID_PARAMETER);
|
---|
2115 | VM_ASSERT_STATE_RETURN(pVM, VMSTATE_CREATING, VERR_VM_INVALID_VM_STATE);
|
---|
2116 |
|
---|
2117 | const uint32_t cPages = cb >> PAGE_SHIFT;
|
---|
2118 |
|
---|
2119 | /*
|
---|
2120 | * Find the ROM location in the ROM list first.
|
---|
2121 | */
|
---|
2122 | PPGMROMRANGE pRomPrev = NULL;
|
---|
2123 | PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3;
|
---|
2124 | while (pRom && GCPhysLast >= pRom->GCPhys)
|
---|
2125 | {
|
---|
2126 | if ( GCPhys <= pRom->GCPhysLast
|
---|
2127 | && GCPhysLast >= pRom->GCPhys)
|
---|
2128 | AssertLogRelMsgFailedReturn(("%RGp-%RGp (%s) conflicts with existing %RGp-%RGp (%s)\n",
|
---|
2129 | GCPhys, GCPhysLast, pszDesc,
|
---|
2130 | pRom->GCPhys, pRom->GCPhysLast, pRom->pszDesc),
|
---|
2131 | VERR_PGM_RAM_CONFLICT);
|
---|
2132 | /* next */
|
---|
2133 | pRomPrev = pRom;
|
---|
2134 | pRom = pRom->pNextR3;
|
---|
2135 | }
|
---|
2136 |
|
---|
2137 | /*
|
---|
2138 | * Find the RAM location and check for conflicts.
|
---|
2139 | *
|
---|
2140 | * Conflict detection is a bit different than for RAM
|
---|
2141 | * registration since a ROM can be located within a RAM
|
---|
2142 | * range. So, what we have to check for is other memory
|
---|
2143 | * types (other than RAM that is) and that we don't span
|
---|
2144 | * more than one RAM range (layz).
|
---|
2145 | */
|
---|
2146 | bool fRamExists = false;
|
---|
2147 | PPGMRAMRANGE pRamPrev = NULL;
|
---|
2148 | PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3;
|
---|
2149 | while (pRam && GCPhysLast >= pRam->GCPhys)
|
---|
2150 | {
|
---|
2151 | if ( GCPhys <= pRam->GCPhysLast
|
---|
2152 | && GCPhysLast >= pRam->GCPhys)
|
---|
2153 | {
|
---|
2154 | /* completely within? */
|
---|
2155 | AssertLogRelMsgReturn( GCPhys >= pRam->GCPhys
|
---|
2156 | && GCPhysLast <= pRam->GCPhysLast,
|
---|
2157 | ("%RGp-%RGp (%s) falls partly outside %RGp-%RGp (%s)\n",
|
---|
2158 | GCPhys, GCPhysLast, pszDesc,
|
---|
2159 | pRam->GCPhys, pRam->GCPhysLast, pRam->pszDesc),
|
---|
2160 | VERR_PGM_RAM_CONFLICT);
|
---|
2161 | fRamExists = true;
|
---|
2162 | break;
|
---|
2163 | }
|
---|
2164 |
|
---|
2165 | /* next */
|
---|
2166 | pRamPrev = pRam;
|
---|
2167 | pRam = pRam->pNextR3;
|
---|
2168 | }
|
---|
2169 | if (fRamExists)
|
---|
2170 | {
|
---|
2171 | PPGMPAGE pPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
2172 | uint32_t cPagesLeft = cPages;
|
---|
2173 | while (cPagesLeft-- > 0)
|
---|
2174 | {
|
---|
2175 | AssertLogRelMsgReturn(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM,
|
---|
2176 | ("%RGp (%R[pgmpage]) isn't a RAM page - registering %RGp-%RGp (%s).\n",
|
---|
2177 | pRam->GCPhys + ((RTGCPHYS)(uintptr_t)(pPage - &pRam->aPages[0]) << PAGE_SHIFT),
|
---|
2178 | pPage, GCPhys, GCPhysLast, pszDesc), VERR_PGM_RAM_CONFLICT);
|
---|
2179 | Assert(PGM_PAGE_IS_ZERO(pPage));
|
---|
2180 | pPage++;
|
---|
2181 | }
|
---|
2182 | }
|
---|
2183 |
|
---|
2184 | /*
|
---|
2185 | * Update the base memory reservation if necessary.
|
---|
2186 | */
|
---|
2187 | uint32_t cExtraBaseCost = fRamExists ? cPages : 0;
|
---|
2188 | if (fFlags & PGMPHYS_ROM_FLAGS_SHADOWED)
|
---|
2189 | cExtraBaseCost += cPages;
|
---|
2190 | if (cExtraBaseCost)
|
---|
2191 | {
|
---|
2192 | int rc = MMR3IncreaseBaseReservation(pVM, cExtraBaseCost);
|
---|
2193 | if (RT_FAILURE(rc))
|
---|
2194 | return rc;
|
---|
2195 | }
|
---|
2196 |
|
---|
2197 | /*
|
---|
2198 | * Allocate memory for the virgin copy of the RAM.
|
---|
2199 | */
|
---|
2200 | PGMMALLOCATEPAGESREQ pReq;
|
---|
2201 | int rc = GMMR3AllocatePagesPrepare(pVM, &pReq, cPages, GMMACCOUNT_BASE);
|
---|
2202 | AssertRCReturn(rc, rc);
|
---|
2203 |
|
---|
2204 | for (uint32_t iPage = 0; iPage < cPages; iPage++)
|
---|
2205 | {
|
---|
2206 | pReq->aPages[iPage].HCPhysGCPhys = GCPhys + (iPage << PAGE_SHIFT);
|
---|
2207 | pReq->aPages[iPage].idPage = NIL_GMM_PAGEID;
|
---|
2208 | pReq->aPages[iPage].idSharedPage = NIL_GMM_PAGEID;
|
---|
2209 | }
|
---|
2210 |
|
---|
2211 | pgmLock(pVM);
|
---|
2212 | rc = GMMR3AllocatePagesPerform(pVM, pReq);
|
---|
2213 | pgmUnlock(pVM);
|
---|
2214 | if (RT_FAILURE(rc))
|
---|
2215 | {
|
---|
2216 | GMMR3AllocatePagesCleanup(pReq);
|
---|
2217 | return rc;
|
---|
2218 | }
|
---|
2219 |
|
---|
2220 | /*
|
---|
2221 | * Allocate the new ROM range and RAM range (if necessary).
|
---|
2222 | */
|
---|
2223 | PPGMROMRANGE pRomNew;
|
---|
2224 | rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMROMRANGE, aPages[cPages]), 0, MM_TAG_PGM_PHYS, (void **)&pRomNew);
|
---|
2225 | if (RT_SUCCESS(rc))
|
---|
2226 | {
|
---|
2227 | PPGMRAMRANGE pRamNew = NULL;
|
---|
2228 | if (!fRamExists)
|
---|
2229 | rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMRAMRANGE, aPages[cPages]), sizeof(PGMPAGE), MM_TAG_PGM_PHYS, (void **)&pRamNew);
|
---|
2230 | if (RT_SUCCESS(rc))
|
---|
2231 | {
|
---|
2232 | pgmLock(pVM);
|
---|
2233 |
|
---|
2234 | /*
|
---|
2235 | * Initialize and insert the RAM range (if required).
|
---|
2236 | */
|
---|
2237 | PPGMROMPAGE pRomPage = &pRomNew->aPages[0];
|
---|
2238 | if (!fRamExists)
|
---|
2239 | {
|
---|
2240 | pRamNew->pSelfR0 = MMHyperCCToR0(pVM, pRamNew);
|
---|
2241 | pRamNew->pSelfRC = MMHyperCCToRC(pVM, pRamNew);
|
---|
2242 | pRamNew->GCPhys = GCPhys;
|
---|
2243 | pRamNew->GCPhysLast = GCPhysLast;
|
---|
2244 | pRamNew->cb = cb;
|
---|
2245 | pRamNew->pszDesc = pszDesc;
|
---|
2246 | pRamNew->fFlags = 0;
|
---|
2247 | pRamNew->pvR3 = NULL;
|
---|
2248 |
|
---|
2249 | PPGMPAGE pPage = &pRamNew->aPages[0];
|
---|
2250 | for (uint32_t iPage = 0; iPage < cPages; iPage++, pPage++, pRomPage++)
|
---|
2251 | {
|
---|
2252 | PGM_PAGE_INIT(pPage,
|
---|
2253 | pReq->aPages[iPage].HCPhysGCPhys,
|
---|
2254 | pReq->aPages[iPage].idPage,
|
---|
2255 | PGMPAGETYPE_ROM,
|
---|
2256 | PGM_PAGE_STATE_ALLOCATED);
|
---|
2257 |
|
---|
2258 | pRomPage->Virgin = *pPage;
|
---|
2259 | }
|
---|
2260 |
|
---|
2261 | pVM->pgm.s.cAllPages += cPages;
|
---|
2262 | pgmR3PhysLinkRamRange(pVM, pRamNew, pRamPrev);
|
---|
2263 | }
|
---|
2264 | else
|
---|
2265 | {
|
---|
2266 | PPGMPAGE pPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
2267 | for (uint32_t iPage = 0; iPage < cPages; iPage++, pPage++, pRomPage++)
|
---|
2268 | {
|
---|
2269 | PGM_PAGE_SET_TYPE(pPage, PGMPAGETYPE_ROM);
|
---|
2270 | PGM_PAGE_SET_HCPHYS(pPage, pReq->aPages[iPage].HCPhysGCPhys);
|
---|
2271 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
|
---|
2272 | PGM_PAGE_SET_PAGEID(pPage, pReq->aPages[iPage].idPage);
|
---|
2273 |
|
---|
2274 | pRomPage->Virgin = *pPage;
|
---|
2275 | }
|
---|
2276 |
|
---|
2277 | pRamNew = pRam;
|
---|
2278 |
|
---|
2279 | pVM->pgm.s.cZeroPages -= cPages;
|
---|
2280 | }
|
---|
2281 | pVM->pgm.s.cPrivatePages += cPages;
|
---|
2282 |
|
---|
2283 | pgmUnlock(pVM);
|
---|
2284 |
|
---|
2285 |
|
---|
2286 | /*
|
---|
2287 | * !HACK ALERT! REM + (Shadowed) ROM ==> mess.
|
---|
2288 | *
|
---|
2289 | * If it's shadowed we'll register the handler after the ROM notification
|
---|
2290 | * so we get the access handler callbacks that we should. If it isn't
|
---|
2291 | * shadowed we'll do it the other way around to make REM use the built-in
|
---|
2292 | * ROM behavior and not the handler behavior (which is to route all access
|
---|
2293 | * to PGM atm).
|
---|
2294 | */
|
---|
2295 | if (fFlags & PGMPHYS_ROM_FLAGS_SHADOWED)
|
---|
2296 | {
|
---|
2297 | REMR3NotifyPhysRomRegister(pVM, GCPhys, cb, NULL, true /* fShadowed */);
|
---|
2298 | rc = PGMR3HandlerPhysicalRegister(pVM,
|
---|
2299 | fFlags & PGMPHYS_ROM_FLAGS_SHADOWED
|
---|
2300 | ? PGMPHYSHANDLERTYPE_PHYSICAL_ALL
|
---|
2301 | : PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
|
---|
2302 | GCPhys, GCPhysLast,
|
---|
2303 | pgmR3PhysRomWriteHandler, pRomNew,
|
---|
2304 | NULL, "pgmPhysRomWriteHandler", MMHyperCCToR0(pVM, pRomNew),
|
---|
2305 | NULL, "pgmPhysRomWriteHandler", MMHyperCCToRC(pVM, pRomNew), pszDesc);
|
---|
2306 | }
|
---|
2307 | else
|
---|
2308 | {
|
---|
2309 | rc = PGMR3HandlerPhysicalRegister(pVM,
|
---|
2310 | fFlags & PGMPHYS_ROM_FLAGS_SHADOWED
|
---|
2311 | ? PGMPHYSHANDLERTYPE_PHYSICAL_ALL
|
---|
2312 | : PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
|
---|
2313 | GCPhys, GCPhysLast,
|
---|
2314 | pgmR3PhysRomWriteHandler, pRomNew,
|
---|
2315 | NULL, "pgmPhysRomWriteHandler", MMHyperCCToR0(pVM, pRomNew),
|
---|
2316 | NULL, "pgmPhysRomWriteHandler", MMHyperCCToRC(pVM, pRomNew), pszDesc);
|
---|
2317 | REMR3NotifyPhysRomRegister(pVM, GCPhys, cb, NULL, false /* fShadowed */);
|
---|
2318 | }
|
---|
2319 | if (RT_SUCCESS(rc))
|
---|
2320 | {
|
---|
2321 | pgmLock(pVM);
|
---|
2322 |
|
---|
2323 | /*
|
---|
2324 | * Copy the image over to the virgin pages.
|
---|
2325 | * This must be done after linking in the RAM range.
|
---|
2326 | */
|
---|
2327 | PPGMPAGE pRamPage = &pRamNew->aPages[(GCPhys - pRamNew->GCPhys) >> PAGE_SHIFT];
|
---|
2328 | for (uint32_t iPage = 0; iPage < cPages; iPage++, pRamPage++)
|
---|
2329 | {
|
---|
2330 | void *pvDstPage;
|
---|
2331 | PPGMPAGEMAP pMapIgnored;
|
---|
2332 | rc = pgmPhysPageMap(pVM, pRamPage, GCPhys + (iPage << PAGE_SHIFT), &pMapIgnored, &pvDstPage);
|
---|
2333 | if (RT_FAILURE(rc))
|
---|
2334 | {
|
---|
2335 | VMSetError(pVM, rc, RT_SRC_POS, "Failed to map virgin ROM page at %RGp", GCPhys);
|
---|
2336 | break;
|
---|
2337 | }
|
---|
2338 | memcpy(pvDstPage, (const uint8_t *)pvBinary + (iPage << PAGE_SHIFT), PAGE_SIZE);
|
---|
2339 | }
|
---|
2340 | if (RT_SUCCESS(rc))
|
---|
2341 | {
|
---|
2342 | /*
|
---|
2343 | * Initialize the ROM range.
|
---|
2344 | * Note that the Virgin member of the pages has already been initialized above.
|
---|
2345 | */
|
---|
2346 | pRomNew->GCPhys = GCPhys;
|
---|
2347 | pRomNew->GCPhysLast = GCPhysLast;
|
---|
2348 | pRomNew->cb = cb;
|
---|
2349 | pRomNew->fFlags = fFlags;
|
---|
2350 | pRomNew->pvOriginal = fFlags & PGMPHYS_ROM_FLAGS_PERMANENT_BINARY ? pvBinary : NULL;
|
---|
2351 | pRomNew->pszDesc = pszDesc;
|
---|
2352 |
|
---|
2353 | for (unsigned iPage = 0; iPage < cPages; iPage++)
|
---|
2354 | {
|
---|
2355 | PPGMROMPAGE pPage = &pRomNew->aPages[iPage];
|
---|
2356 | pPage->enmProt = PGMROMPROT_READ_ROM_WRITE_IGNORE;
|
---|
2357 | PGM_PAGE_INIT_ZERO_REAL(&pPage->Shadow, pVM, PGMPAGETYPE_ROM_SHADOW);
|
---|
2358 | }
|
---|
2359 |
|
---|
2360 | /* update the page count stats */
|
---|
2361 | pVM->pgm.s.cZeroPages += cPages;
|
---|
2362 | pVM->pgm.s.cAllPages += cPages;
|
---|
2363 |
|
---|
2364 | /*
|
---|
2365 | * Insert the ROM range, tell REM and return successfully.
|
---|
2366 | */
|
---|
2367 | pRomNew->pNextR3 = pRom;
|
---|
2368 | pRomNew->pNextR0 = pRom ? MMHyperCCToR0(pVM, pRom) : NIL_RTR0PTR;
|
---|
2369 | pRomNew->pNextRC = pRom ? MMHyperCCToRC(pVM, pRom) : NIL_RTRCPTR;
|
---|
2370 |
|
---|
2371 | if (pRomPrev)
|
---|
2372 | {
|
---|
2373 | pRomPrev->pNextR3 = pRomNew;
|
---|
2374 | pRomPrev->pNextR0 = MMHyperCCToR0(pVM, pRomNew);
|
---|
2375 | pRomPrev->pNextRC = MMHyperCCToRC(pVM, pRomNew);
|
---|
2376 | }
|
---|
2377 | else
|
---|
2378 | {
|
---|
2379 | pVM->pgm.s.pRomRangesR3 = pRomNew;
|
---|
2380 | pVM->pgm.s.pRomRangesR0 = MMHyperCCToR0(pVM, pRomNew);
|
---|
2381 | pVM->pgm.s.pRomRangesRC = MMHyperCCToRC(pVM, pRomNew);
|
---|
2382 | }
|
---|
2383 |
|
---|
2384 | GMMR3AllocatePagesCleanup(pReq);
|
---|
2385 | pgmUnlock(pVM);
|
---|
2386 | return VINF_SUCCESS;
|
---|
2387 | }
|
---|
2388 |
|
---|
2389 | /* bail out */
|
---|
2390 |
|
---|
2391 | pgmUnlock(pVM);
|
---|
2392 | int rc2 = PGMHandlerPhysicalDeregister(pVM, GCPhys);
|
---|
2393 | AssertRC(rc2);
|
---|
2394 | pgmLock(pVM);
|
---|
2395 | }
|
---|
2396 |
|
---|
2397 | if (!fRamExists)
|
---|
2398 | {
|
---|
2399 | pgmR3PhysUnlinkRamRange2(pVM, pRamNew, pRamPrev);
|
---|
2400 | MMHyperFree(pVM, pRamNew);
|
---|
2401 | }
|
---|
2402 | }
|
---|
2403 | MMHyperFree(pVM, pRomNew);
|
---|
2404 | }
|
---|
2405 |
|
---|
2406 | /** @todo Purge the mapping cache or something... */
|
---|
2407 | GMMR3FreeAllocatedPages(pVM, pReq);
|
---|
2408 | GMMR3AllocatePagesCleanup(pReq);
|
---|
2409 | pgmUnlock(pVM);
|
---|
2410 | return rc;
|
---|
2411 | }
|
---|
2412 |
|
---|
2413 |
|
---|
2414 | /**
|
---|
2415 | * \#PF Handler callback for ROM write accesses.
|
---|
2416 | *
|
---|
2417 | * @returns VINF_SUCCESS if the handler have carried out the operation.
|
---|
2418 | * @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
|
---|
2419 | * @param pVM VM Handle.
|
---|
2420 | * @param GCPhys The physical address the guest is writing to.
|
---|
2421 | * @param pvPhys The HC mapping of that address.
|
---|
2422 | * @param pvBuf What the guest is reading/writing.
|
---|
2423 | * @param cbBuf How much it's reading/writing.
|
---|
2424 | * @param enmAccessType The access type.
|
---|
2425 | * @param pvUser User argument.
|
---|
2426 | */
|
---|
2427 | static DECLCALLBACK(int) pgmR3PhysRomWriteHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser)
|
---|
2428 | {
|
---|
2429 | PPGMROMRANGE pRom = (PPGMROMRANGE)pvUser;
|
---|
2430 | const uint32_t iPage = (GCPhys - pRom->GCPhys) >> PAGE_SHIFT;
|
---|
2431 | Assert(iPage < (pRom->cb >> PAGE_SHIFT));
|
---|
2432 | PPGMROMPAGE pRomPage = &pRom->aPages[iPage];
|
---|
2433 | Log5(("pgmR3PhysRomWriteHandler: %d %c %#08RGp %#04zx\n", pRomPage->enmProt, enmAccessType == PGMACCESSTYPE_READ ? 'R' : 'W', GCPhys, cbBuf));
|
---|
2434 |
|
---|
2435 | if (enmAccessType == PGMACCESSTYPE_READ)
|
---|
2436 | {
|
---|
2437 | switch (pRomPage->enmProt)
|
---|
2438 | {
|
---|
2439 | /*
|
---|
2440 | * Take the default action.
|
---|
2441 | */
|
---|
2442 | case PGMROMPROT_READ_ROM_WRITE_IGNORE:
|
---|
2443 | case PGMROMPROT_READ_RAM_WRITE_IGNORE:
|
---|
2444 | case PGMROMPROT_READ_ROM_WRITE_RAM:
|
---|
2445 | case PGMROMPROT_READ_RAM_WRITE_RAM:
|
---|
2446 | return VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
2447 |
|
---|
2448 | default:
|
---|
2449 | AssertMsgFailedReturn(("enmProt=%d iPage=%d GCPhys=%RGp\n",
|
---|
2450 | pRom->aPages[iPage].enmProt, iPage, GCPhys),
|
---|
2451 | VERR_INTERNAL_ERROR);
|
---|
2452 | }
|
---|
2453 | }
|
---|
2454 | else
|
---|
2455 | {
|
---|
2456 | Assert(enmAccessType == PGMACCESSTYPE_WRITE);
|
---|
2457 | switch (pRomPage->enmProt)
|
---|
2458 | {
|
---|
2459 | /*
|
---|
2460 | * Ignore writes.
|
---|
2461 | */
|
---|
2462 | case PGMROMPROT_READ_ROM_WRITE_IGNORE:
|
---|
2463 | case PGMROMPROT_READ_RAM_WRITE_IGNORE:
|
---|
2464 | return VINF_SUCCESS;
|
---|
2465 |
|
---|
2466 | /*
|
---|
2467 | * Write to the ram page.
|
---|
2468 | */
|
---|
2469 | case PGMROMPROT_READ_ROM_WRITE_RAM:
|
---|
2470 | case PGMROMPROT_READ_RAM_WRITE_RAM: /* yes this will get here too, it's *way* simpler that way. */
|
---|
2471 | {
|
---|
2472 | /* This should be impossible now, pvPhys doesn't work cross page anylonger. */
|
---|
2473 | Assert(((GCPhys - pRom->GCPhys + cbBuf - 1) >> PAGE_SHIFT) == iPage);
|
---|
2474 |
|
---|
2475 | /*
|
---|
2476 | * Take the lock, do lazy allocation, map the page and copy the data.
|
---|
2477 | *
|
---|
2478 | * Note that we have to bypass the mapping TLB since it works on
|
---|
2479 | * guest physical addresses and entering the shadow page would
|
---|
2480 | * kind of screw things up...
|
---|
2481 | */
|
---|
2482 | int rc = pgmLock(pVM);
|
---|
2483 | AssertRC(rc);
|
---|
2484 | PPGMPAGE pShadowPage = &pRomPage->Shadow;
|
---|
2485 | if (!PGMROMPROT_IS_ROM(pRomPage->enmProt))
|
---|
2486 | {
|
---|
2487 | pShadowPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
|
---|
2488 | AssertLogRelReturn(pShadowPage, VERR_INTERNAL_ERROR);
|
---|
2489 | }
|
---|
2490 |
|
---|
2491 | if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pShadowPage) != PGM_PAGE_STATE_ALLOCATED))
|
---|
2492 | {
|
---|
2493 | rc = pgmPhysPageMakeWritable(pVM, pShadowPage, GCPhys);
|
---|
2494 | if (RT_FAILURE(rc))
|
---|
2495 | {
|
---|
2496 | pgmUnlock(pVM);
|
---|
2497 | return rc;
|
---|
2498 | }
|
---|
2499 | AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* returned */, ("%Rrc\n", rc));
|
---|
2500 | }
|
---|
2501 |
|
---|
2502 | void *pvDstPage;
|
---|
2503 | PPGMPAGEMAP pMapIgnored;
|
---|
2504 | int rc2 = pgmPhysPageMap(pVM, pShadowPage, GCPhys & X86_PTE_PG_MASK, &pMapIgnored, &pvDstPage);
|
---|
2505 | if (RT_SUCCESS(rc2))
|
---|
2506 | memcpy((uint8_t *)pvDstPage + (GCPhys & PAGE_OFFSET_MASK), pvBuf, cbBuf);
|
---|
2507 | else
|
---|
2508 | rc = rc2;
|
---|
2509 |
|
---|
2510 | pgmUnlock(pVM);
|
---|
2511 | return rc;
|
---|
2512 | }
|
---|
2513 |
|
---|
2514 | default:
|
---|
2515 | AssertMsgFailedReturn(("enmProt=%d iPage=%d GCPhys=%RGp\n",
|
---|
2516 | pRom->aPages[iPage].enmProt, iPage, GCPhys),
|
---|
2517 | VERR_INTERNAL_ERROR);
|
---|
2518 | }
|
---|
2519 | }
|
---|
2520 | }
|
---|
2521 |
|
---|
2522 |
|
---|
2523 | /**
|
---|
2524 | * Called by PGMR3Reset to reset the shadow, switch to the virgin,
|
---|
2525 | * and verify that the virgin part is untouched.
|
---|
2526 | *
|
---|
2527 | * This is done after the normal memory has been cleared.
|
---|
2528 | *
|
---|
2529 | * ASSUMES that the caller owns the PGM lock.
|
---|
2530 | *
|
---|
2531 | * @param pVM The VM handle.
|
---|
2532 | */
|
---|
2533 | int pgmR3PhysRomReset(PVM pVM)
|
---|
2534 | {
|
---|
2535 | Assert(PGMIsLockOwner(pVM));
|
---|
2536 | for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
|
---|
2537 | {
|
---|
2538 | const uint32_t cPages = pRom->cb >> PAGE_SHIFT;
|
---|
2539 |
|
---|
2540 | if (pRom->fFlags & PGMPHYS_ROM_FLAGS_SHADOWED)
|
---|
2541 | {
|
---|
2542 | /*
|
---|
2543 | * Reset the physical handler.
|
---|
2544 | */
|
---|
2545 | int rc = PGMR3PhysRomProtect(pVM, pRom->GCPhys, pRom->cb, PGMROMPROT_READ_ROM_WRITE_IGNORE);
|
---|
2546 | AssertRCReturn(rc, rc);
|
---|
2547 |
|
---|
2548 | /*
|
---|
2549 | * What we do with the shadow pages depends on the memory
|
---|
2550 | * preallocation option. If not enabled, we'll just throw
|
---|
2551 | * out all the dirty pages and replace them by the zero page.
|
---|
2552 | */
|
---|
2553 | if (!pVM->pgm.s.fRamPreAlloc)
|
---|
2554 | {
|
---|
2555 | /* Free the dirty pages. */
|
---|
2556 | uint32_t cPendingPages = 0;
|
---|
2557 | PGMMFREEPAGESREQ pReq;
|
---|
2558 | rc = GMMR3FreePagesPrepare(pVM, &pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE, GMMACCOUNT_BASE);
|
---|
2559 | AssertRCReturn(rc, rc);
|
---|
2560 |
|
---|
2561 | for (uint32_t iPage = 0; iPage < cPages; iPage++)
|
---|
2562 | if (PGM_PAGE_GET_STATE(&pRom->aPages[iPage].Shadow) != PGM_PAGE_STATE_ZERO)
|
---|
2563 | {
|
---|
2564 | Assert(PGM_PAGE_GET_STATE(&pRom->aPages[iPage].Shadow) == PGM_PAGE_STATE_ALLOCATED);
|
---|
2565 | rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, &pRom->aPages[iPage].Shadow, pRom->GCPhys + (iPage << PAGE_SHIFT));
|
---|
2566 | AssertLogRelRCReturn(rc, rc);
|
---|
2567 | }
|
---|
2568 |
|
---|
2569 | if (cPendingPages)
|
---|
2570 | {
|
---|
2571 | rc = GMMR3FreePagesPerform(pVM, pReq, cPendingPages);
|
---|
2572 | AssertLogRelRCReturn(rc, rc);
|
---|
2573 | }
|
---|
2574 | GMMR3FreePagesCleanup(pReq);
|
---|
2575 | }
|
---|
2576 | else
|
---|
2577 | {
|
---|
2578 | /* clear all the shadow pages. */
|
---|
2579 | for (uint32_t iPage = 0; iPage < cPages; iPage++)
|
---|
2580 | {
|
---|
2581 | Assert(PGM_PAGE_GET_STATE(&pRom->aPages[iPage].Shadow) != PGM_PAGE_STATE_ZERO);
|
---|
2582 |
|
---|
2583 | const RTGCPHYS GCPhys = pRom->GCPhys + (iPage << PAGE_SHIFT);
|
---|
2584 | rc = pgmPhysPageMakeWritable(pVM, &pRom->aPages[iPage].Shadow, GCPhys);
|
---|
2585 | if (RT_FAILURE(rc))
|
---|
2586 | break;
|
---|
2587 |
|
---|
2588 | void *pvDstPage;
|
---|
2589 | PPGMPAGEMAP pMapIgnored;
|
---|
2590 | rc = pgmPhysPageMap(pVM, &pRom->aPages[iPage].Shadow, GCPhys, &pMapIgnored, &pvDstPage);
|
---|
2591 | if (RT_FAILURE(rc))
|
---|
2592 | break;
|
---|
2593 | ASMMemZeroPage(pvDstPage);
|
---|
2594 | }
|
---|
2595 | AssertRCReturn(rc, rc);
|
---|
2596 | }
|
---|
2597 | }
|
---|
2598 |
|
---|
2599 | #ifdef VBOX_STRICT
|
---|
2600 | /*
|
---|
2601 | * Verify that the virgin page is unchanged if possible.
|
---|
2602 | */
|
---|
2603 | if (pRom->pvOriginal)
|
---|
2604 | {
|
---|
2605 | uint8_t const *pbSrcPage = (uint8_t const *)pRom->pvOriginal;
|
---|
2606 | for (uint32_t iPage = 0; iPage < cPages; iPage++, pbSrcPage += PAGE_SIZE)
|
---|
2607 | {
|
---|
2608 | const RTGCPHYS GCPhys = pRom->GCPhys + (iPage << PAGE_SHIFT);
|
---|
2609 | PPGMPAGEMAP pMapIgnored;
|
---|
2610 | void *pvDstPage;
|
---|
2611 | int rc = pgmPhysPageMap(pVM, &pRom->aPages[iPage].Virgin, GCPhys, &pMapIgnored, &pvDstPage);
|
---|
2612 | if (RT_FAILURE(rc))
|
---|
2613 | break;
|
---|
2614 | if (memcmp(pvDstPage, pbSrcPage, PAGE_SIZE))
|
---|
2615 | LogRel(("pgmR3PhysRomReset: %RGp rom page changed (%s) - loaded saved state?\n",
|
---|
2616 | GCPhys, pRom->pszDesc));
|
---|
2617 | }
|
---|
2618 | }
|
---|
2619 | #endif
|
---|
2620 | }
|
---|
2621 |
|
---|
2622 | return VINF_SUCCESS;
|
---|
2623 | }
|
---|
2624 |
|
---|
2625 |
|
---|
2626 | /**
|
---|
2627 | * Change the shadowing of a range of ROM pages.
|
---|
2628 | *
|
---|
2629 | * This is intended for implementing chipset specific memory registers
|
---|
2630 | * and will not be very strict about the input. It will silently ignore
|
---|
2631 | * any pages that are not the part of a shadowed ROM.
|
---|
2632 | *
|
---|
2633 | * @returns VBox status code.
|
---|
2634 | * @retval VINF_PGM_SYNC_CR3
|
---|
2635 | *
|
---|
2636 | * @param pVM Pointer to the shared VM structure.
|
---|
2637 | * @param GCPhys Where to start. Page aligned.
|
---|
2638 | * @param cb How much to change. Page aligned.
|
---|
2639 | * @param enmProt The new ROM protection.
|
---|
2640 | */
|
---|
2641 | VMMR3DECL(int) PGMR3PhysRomProtect(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, PGMROMPROT enmProt)
|
---|
2642 | {
|
---|
2643 | /*
|
---|
2644 | * Check input
|
---|
2645 | */
|
---|
2646 | if (!cb)
|
---|
2647 | return VINF_SUCCESS;
|
---|
2648 | AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
2649 | AssertReturn(!(cb & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
2650 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
2651 | AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
|
---|
2652 | AssertReturn(enmProt >= PGMROMPROT_INVALID && enmProt <= PGMROMPROT_END, VERR_INVALID_PARAMETER);
|
---|
2653 |
|
---|
2654 | /*
|
---|
2655 | * Process the request.
|
---|
2656 | */
|
---|
2657 | pgmLock(pVM);
|
---|
2658 | int rc = VINF_SUCCESS;
|
---|
2659 | bool fFlushTLB = false;
|
---|
2660 | for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
|
---|
2661 | {
|
---|
2662 | if ( GCPhys <= pRom->GCPhysLast
|
---|
2663 | && GCPhysLast >= pRom->GCPhys
|
---|
2664 | && (pRom->fFlags & PGMPHYS_ROM_FLAGS_SHADOWED))
|
---|
2665 | {
|
---|
2666 | /*
|
---|
2667 | * Iterate the relevant pages and make necessary the changes.
|
---|
2668 | */
|
---|
2669 | bool fChanges = false;
|
---|
2670 | uint32_t const cPages = pRom->GCPhysLast <= GCPhysLast
|
---|
2671 | ? pRom->cb >> PAGE_SHIFT
|
---|
2672 | : (GCPhysLast - pRom->GCPhys + 1) >> PAGE_SHIFT;
|
---|
2673 | for (uint32_t iPage = (GCPhys - pRom->GCPhys) >> PAGE_SHIFT;
|
---|
2674 | iPage < cPages;
|
---|
2675 | iPage++)
|
---|
2676 | {
|
---|
2677 | PPGMROMPAGE pRomPage = &pRom->aPages[iPage];
|
---|
2678 | if (PGMROMPROT_IS_ROM(pRomPage->enmProt) != PGMROMPROT_IS_ROM(enmProt))
|
---|
2679 | {
|
---|
2680 | fChanges = true;
|
---|
2681 |
|
---|
2682 | /* flush references to the page. */
|
---|
2683 | PPGMPAGE pRamPage = pgmPhysGetPage(&pVM->pgm.s, pRom->GCPhys + (iPage << PAGE_SHIFT));
|
---|
2684 | int rc2 = pgmPoolTrackFlushGCPhys(pVM, pRamPage, &fFlushTLB);
|
---|
2685 | if (rc2 != VINF_SUCCESS && (rc == VINF_SUCCESS || RT_FAILURE(rc2)))
|
---|
2686 | rc = rc2;
|
---|
2687 |
|
---|
2688 | PPGMPAGE pOld = PGMROMPROT_IS_ROM(pRomPage->enmProt) ? &pRomPage->Virgin : &pRomPage->Shadow;
|
---|
2689 | PPGMPAGE pNew = PGMROMPROT_IS_ROM(pRomPage->enmProt) ? &pRomPage->Shadow : &pRomPage->Virgin;
|
---|
2690 |
|
---|
2691 | *pOld = *pRamPage;
|
---|
2692 | *pRamPage = *pNew;
|
---|
2693 | /** @todo preserve the volatile flags (handlers) when these have been moved out of HCPhys! */
|
---|
2694 | }
|
---|
2695 | pRomPage->enmProt = enmProt;
|
---|
2696 | }
|
---|
2697 |
|
---|
2698 | /*
|
---|
2699 | * Reset the access handler if we made changes, no need
|
---|
2700 | * to optimize this.
|
---|
2701 | */
|
---|
2702 | if (fChanges)
|
---|
2703 | {
|
---|
2704 | int rc = PGMHandlerPhysicalReset(pVM, pRom->GCPhys);
|
---|
2705 | if (RT_FAILURE(rc))
|
---|
2706 | {
|
---|
2707 | pgmUnlock(pVM);
|
---|
2708 | AssertRC(rc);
|
---|
2709 | return rc;
|
---|
2710 | }
|
---|
2711 | }
|
---|
2712 |
|
---|
2713 | /* Advance - cb isn't updated. */
|
---|
2714 | GCPhys = pRom->GCPhys + (cPages << PAGE_SHIFT);
|
---|
2715 | }
|
---|
2716 | }
|
---|
2717 | pgmUnlock(pVM);
|
---|
2718 | if (fFlushTLB)
|
---|
2719 | PGM_INVL_ALL_VCPU_TLBS(pVM);
|
---|
2720 |
|
---|
2721 | return rc;
|
---|
2722 | }
|
---|
2723 |
|
---|
2724 |
|
---|
2725 | /**
|
---|
2726 | * Sets the Address Gate 20 state.
|
---|
2727 | *
|
---|
2728 | * @param pVCpu The VCPU to operate on.
|
---|
2729 | * @param fEnable True if the gate should be enabled.
|
---|
2730 | * False if the gate should be disabled.
|
---|
2731 | */
|
---|
2732 | VMMDECL(void) PGMR3PhysSetA20(PVMCPU pVCpu, bool fEnable)
|
---|
2733 | {
|
---|
2734 | LogFlow(("PGMR3PhysSetA20 %d (was %d)\n", fEnable, pVCpu->pgm.s.fA20Enabled));
|
---|
2735 | if (pVCpu->pgm.s.fA20Enabled != fEnable)
|
---|
2736 | {
|
---|
2737 | pVCpu->pgm.s.fA20Enabled = fEnable;
|
---|
2738 | pVCpu->pgm.s.GCPhysA20Mask = ~(RTGCPHYS)(!fEnable << 20);
|
---|
2739 | REMR3A20Set(pVCpu->pVMR3, pVCpu, fEnable);
|
---|
2740 | /** @todo we're not handling this correctly for VT-x / AMD-V. See #2911 */
|
---|
2741 | }
|
---|
2742 | }
|
---|
2743 |
|
---|
2744 |
|
---|
2745 | /**
|
---|
2746 | * Tree enumeration callback for dealing with age rollover.
|
---|
2747 | * It will perform a simple compression of the current age.
|
---|
2748 | */
|
---|
2749 | static DECLCALLBACK(int) pgmR3PhysChunkAgeingRolloverCallback(PAVLU32NODECORE pNode, void *pvUser)
|
---|
2750 | {
|
---|
2751 | Assert(PGMIsLockOwner((PVM)pvUser));
|
---|
2752 | /* Age compression - ASSUMES iNow == 4. */
|
---|
2753 | PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)pNode;
|
---|
2754 | if (pChunk->iAge >= UINT32_C(0xffffff00))
|
---|
2755 | pChunk->iAge = 3;
|
---|
2756 | else if (pChunk->iAge >= UINT32_C(0xfffff000))
|
---|
2757 | pChunk->iAge = 2;
|
---|
2758 | else if (pChunk->iAge)
|
---|
2759 | pChunk->iAge = 1;
|
---|
2760 | else /* iAge = 0 */
|
---|
2761 | pChunk->iAge = 4;
|
---|
2762 |
|
---|
2763 | /* reinsert */
|
---|
2764 | PVM pVM = (PVM)pvUser;
|
---|
2765 | RTAvllU32Remove(&pVM->pgm.s.ChunkR3Map.pAgeTree, pChunk->AgeCore.Key);
|
---|
2766 | pChunk->AgeCore.Key = pChunk->iAge;
|
---|
2767 | RTAvllU32Insert(&pVM->pgm.s.ChunkR3Map.pAgeTree, &pChunk->AgeCore);
|
---|
2768 | return 0;
|
---|
2769 | }
|
---|
2770 |
|
---|
2771 |
|
---|
2772 | /**
|
---|
2773 | * Tree enumeration callback that updates the chunks that have
|
---|
2774 | * been used since the last
|
---|
2775 | */
|
---|
2776 | static DECLCALLBACK(int) pgmR3PhysChunkAgeingCallback(PAVLU32NODECORE pNode, void *pvUser)
|
---|
2777 | {
|
---|
2778 | PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)pNode;
|
---|
2779 | if (!pChunk->iAge)
|
---|
2780 | {
|
---|
2781 | PVM pVM = (PVM)pvUser;
|
---|
2782 | RTAvllU32Remove(&pVM->pgm.s.ChunkR3Map.pAgeTree, pChunk->AgeCore.Key);
|
---|
2783 | pChunk->AgeCore.Key = pChunk->iAge = pVM->pgm.s.ChunkR3Map.iNow;
|
---|
2784 | RTAvllU32Insert(&pVM->pgm.s.ChunkR3Map.pAgeTree, &pChunk->AgeCore);
|
---|
2785 | }
|
---|
2786 |
|
---|
2787 | return 0;
|
---|
2788 | }
|
---|
2789 |
|
---|
2790 |
|
---|
2791 | /**
|
---|
2792 | * Performs ageing of the ring-3 chunk mappings.
|
---|
2793 | *
|
---|
2794 | * @param pVM The VM handle.
|
---|
2795 | */
|
---|
2796 | VMMR3DECL(void) PGMR3PhysChunkAgeing(PVM pVM)
|
---|
2797 | {
|
---|
2798 | pgmLock(pVM);
|
---|
2799 | pVM->pgm.s.ChunkR3Map.AgeingCountdown = RT_MIN(pVM->pgm.s.ChunkR3Map.cMax / 4, 1024);
|
---|
2800 | pVM->pgm.s.ChunkR3Map.iNow++;
|
---|
2801 | if (pVM->pgm.s.ChunkR3Map.iNow == 0)
|
---|
2802 | {
|
---|
2803 | pVM->pgm.s.ChunkR3Map.iNow = 4;
|
---|
2804 | RTAvlU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pTree, true /*fFromLeft*/, pgmR3PhysChunkAgeingRolloverCallback, pVM);
|
---|
2805 | }
|
---|
2806 | else
|
---|
2807 | RTAvlU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pTree, true /*fFromLeft*/, pgmR3PhysChunkAgeingCallback, pVM);
|
---|
2808 | pgmUnlock(pVM);
|
---|
2809 | }
|
---|
2810 |
|
---|
2811 |
|
---|
2812 | /**
|
---|
2813 | * The structure passed in the pvUser argument of pgmR3PhysChunkUnmapCandidateCallback().
|
---|
2814 | */
|
---|
2815 | typedef struct PGMR3PHYSCHUNKUNMAPCB
|
---|
2816 | {
|
---|
2817 | PVM pVM; /**< The VM handle. */
|
---|
2818 | PPGMCHUNKR3MAP pChunk; /**< The chunk to unmap. */
|
---|
2819 | } PGMR3PHYSCHUNKUNMAPCB, *PPGMR3PHYSCHUNKUNMAPCB;
|
---|
2820 |
|
---|
2821 |
|
---|
2822 | /**
|
---|
2823 | * Callback used to find the mapping that's been unused for
|
---|
2824 | * the longest time.
|
---|
2825 | */
|
---|
2826 | static DECLCALLBACK(int) pgmR3PhysChunkUnmapCandidateCallback(PAVLLU32NODECORE pNode, void *pvUser)
|
---|
2827 | {
|
---|
2828 | do
|
---|
2829 | {
|
---|
2830 | PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)((uint8_t *)pNode - RT_OFFSETOF(PGMCHUNKR3MAP, AgeCore));
|
---|
2831 | if ( pChunk->iAge
|
---|
2832 | && !pChunk->cRefs)
|
---|
2833 | {
|
---|
2834 | /*
|
---|
2835 | * Check that it's not in any of the TLBs.
|
---|
2836 | */
|
---|
2837 | PVM pVM = ((PPGMR3PHYSCHUNKUNMAPCB)pvUser)->pVM;
|
---|
2838 | for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.ChunkR3Map.Tlb.aEntries); i++)
|
---|
2839 | if (pVM->pgm.s.ChunkR3Map.Tlb.aEntries[i].pChunk == pChunk)
|
---|
2840 | {
|
---|
2841 | pChunk = NULL;
|
---|
2842 | break;
|
---|
2843 | }
|
---|
2844 | if (pChunk)
|
---|
2845 | for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.PhysTlbHC.aEntries); i++)
|
---|
2846 | if (pVM->pgm.s.PhysTlbHC.aEntries[i].pMap == pChunk)
|
---|
2847 | {
|
---|
2848 | pChunk = NULL;
|
---|
2849 | break;
|
---|
2850 | }
|
---|
2851 | if (pChunk)
|
---|
2852 | {
|
---|
2853 | ((PPGMR3PHYSCHUNKUNMAPCB)pvUser)->pChunk = pChunk;
|
---|
2854 | return 1; /* done */
|
---|
2855 | }
|
---|
2856 | }
|
---|
2857 |
|
---|
2858 | /* next with the same age - this version of the AVL API doesn't enumerate the list, so we have to do it. */
|
---|
2859 | pNode = pNode->pList;
|
---|
2860 | } while (pNode);
|
---|
2861 | return 0;
|
---|
2862 | }
|
---|
2863 |
|
---|
2864 |
|
---|
2865 | /**
|
---|
2866 | * Finds a good candidate for unmapping when the ring-3 mapping cache is full.
|
---|
2867 | *
|
---|
2868 | * The candidate will not be part of any TLBs, so no need to flush
|
---|
2869 | * anything afterwards.
|
---|
2870 | *
|
---|
2871 | * @returns Chunk id.
|
---|
2872 | * @param pVM The VM handle.
|
---|
2873 | */
|
---|
2874 | static int32_t pgmR3PhysChunkFindUnmapCandidate(PVM pVM)
|
---|
2875 | {
|
---|
2876 | Assert(PGMIsLockOwner(pVM));
|
---|
2877 |
|
---|
2878 | /*
|
---|
2879 | * Do tree ageing first?
|
---|
2880 | */
|
---|
2881 | if (pVM->pgm.s.ChunkR3Map.AgeingCountdown-- == 0)
|
---|
2882 | PGMR3PhysChunkAgeing(pVM);
|
---|
2883 |
|
---|
2884 | /*
|
---|
2885 | * Enumerate the age tree starting with the left most node.
|
---|
2886 | */
|
---|
2887 | PGMR3PHYSCHUNKUNMAPCB Args;
|
---|
2888 | Args.pVM = pVM;
|
---|
2889 | Args.pChunk = NULL;
|
---|
2890 | if (RTAvllU32DoWithAll(&pVM->pgm.s.ChunkR3Map.pAgeTree, true /*fFromLeft*/, pgmR3PhysChunkUnmapCandidateCallback, pVM))
|
---|
2891 | return Args.pChunk->Core.Key;
|
---|
2892 | return INT32_MAX;
|
---|
2893 | }
|
---|
2894 |
|
---|
2895 |
|
---|
2896 | /**
|
---|
2897 | * Maps the given chunk into the ring-3 mapping cache.
|
---|
2898 | *
|
---|
2899 | * This will call ring-0.
|
---|
2900 | *
|
---|
2901 | * @returns VBox status code.
|
---|
2902 | * @param pVM The VM handle.
|
---|
2903 | * @param idChunk The chunk in question.
|
---|
2904 | * @param ppChunk Where to store the chunk tracking structure.
|
---|
2905 | *
|
---|
2906 | * @remarks Called from within the PGM critical section.
|
---|
2907 | */
|
---|
2908 | int pgmR3PhysChunkMap(PVM pVM, uint32_t idChunk, PPPGMCHUNKR3MAP ppChunk)
|
---|
2909 | {
|
---|
2910 | int rc;
|
---|
2911 |
|
---|
2912 | Assert(PGMIsLockOwner(pVM));
|
---|
2913 | /*
|
---|
2914 | * Allocate a new tracking structure first.
|
---|
2915 | */
|
---|
2916 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
2917 | PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)MMR3HeapAlloc(pVM, MM_TAG_PGM_CHUNK_MAPPING, sizeof(*pChunk));
|
---|
2918 | #else
|
---|
2919 | PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)MMR3UkHeapAlloc(pVM, MM_TAG_PGM_CHUNK_MAPPING, sizeof(*pChunk), NULL);
|
---|
2920 | #endif
|
---|
2921 | AssertReturn(pChunk, VERR_NO_MEMORY);
|
---|
2922 | pChunk->Core.Key = idChunk;
|
---|
2923 | pChunk->AgeCore.Key = pVM->pgm.s.ChunkR3Map.iNow;
|
---|
2924 | pChunk->iAge = 0;
|
---|
2925 | pChunk->cRefs = 0;
|
---|
2926 | pChunk->cPermRefs = 0;
|
---|
2927 | pChunk->pv = NULL;
|
---|
2928 |
|
---|
2929 | /*
|
---|
2930 | * Request the ring-0 part to map the chunk in question and if
|
---|
2931 | * necessary unmap another one to make space in the mapping cache.
|
---|
2932 | */
|
---|
2933 | GMMMAPUNMAPCHUNKREQ Req;
|
---|
2934 | Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
2935 | Req.Hdr.cbReq = sizeof(Req);
|
---|
2936 | Req.pvR3 = NULL;
|
---|
2937 | Req.idChunkMap = idChunk;
|
---|
2938 | Req.idChunkUnmap = NIL_GMM_CHUNKID;
|
---|
2939 | if (pVM->pgm.s.ChunkR3Map.c >= pVM->pgm.s.ChunkR3Map.cMax)
|
---|
2940 | Req.idChunkUnmap = pgmR3PhysChunkFindUnmapCandidate(pVM);
|
---|
2941 | /** @todo This is wrong. Any thread in the VM process should be able to do this,
|
---|
2942 | * there are depenenecies on this. What currently saves the day is that
|
---|
2943 | * we don't unmap anything and that all non-zero memory will therefore
|
---|
2944 | * be present when non-EMTs tries to access it. */
|
---|
2945 | rc = VMMR3CallR0(pVM, VMMR0_DO_GMM_MAP_UNMAP_CHUNK, 0, &Req.Hdr);
|
---|
2946 | if (RT_SUCCESS(rc))
|
---|
2947 | {
|
---|
2948 | /*
|
---|
2949 | * Update the tree.
|
---|
2950 | */
|
---|
2951 | /* insert the new one. */
|
---|
2952 | AssertPtr(Req.pvR3);
|
---|
2953 | pChunk->pv = Req.pvR3;
|
---|
2954 | bool fRc = RTAvlU32Insert(&pVM->pgm.s.ChunkR3Map.pTree, &pChunk->Core);
|
---|
2955 | AssertRelease(fRc);
|
---|
2956 | pVM->pgm.s.ChunkR3Map.c++;
|
---|
2957 |
|
---|
2958 | fRc = RTAvllU32Insert(&pVM->pgm.s.ChunkR3Map.pAgeTree, &pChunk->AgeCore);
|
---|
2959 | AssertRelease(fRc);
|
---|
2960 |
|
---|
2961 | /* remove the unmapped one. */
|
---|
2962 | if (Req.idChunkUnmap != NIL_GMM_CHUNKID)
|
---|
2963 | {
|
---|
2964 | PPGMCHUNKR3MAP pUnmappedChunk = (PPGMCHUNKR3MAP)RTAvlU32Remove(&pVM->pgm.s.ChunkR3Map.pTree, Req.idChunkUnmap);
|
---|
2965 | AssertRelease(pUnmappedChunk);
|
---|
2966 | pUnmappedChunk->pv = NULL;
|
---|
2967 | pUnmappedChunk->Core.Key = UINT32_MAX;
|
---|
2968 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
2969 | MMR3HeapFree(pUnmappedChunk);
|
---|
2970 | #else
|
---|
2971 | MMR3UkHeapFree(pVM, pUnmappedChunk, MM_TAG_PGM_CHUNK_MAPPING);
|
---|
2972 | #endif
|
---|
2973 | pVM->pgm.s.ChunkR3Map.c--;
|
---|
2974 | }
|
---|
2975 | }
|
---|
2976 | else
|
---|
2977 | {
|
---|
2978 | AssertRC(rc);
|
---|
2979 | #ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
|
---|
2980 | MMR3HeapFree(pChunk);
|
---|
2981 | #else
|
---|
2982 | MMR3UkHeapFree(pVM, pChunk, MM_TAG_PGM_CHUNK_MAPPING);
|
---|
2983 | #endif
|
---|
2984 | pChunk = NULL;
|
---|
2985 | }
|
---|
2986 |
|
---|
2987 | *ppChunk = pChunk;
|
---|
2988 | return rc;
|
---|
2989 | }
|
---|
2990 |
|
---|
2991 |
|
---|
2992 | /**
|
---|
2993 | * For VMMCALLRING3_PGM_MAP_CHUNK, considered internal.
|
---|
2994 | *
|
---|
2995 | * @returns see pgmR3PhysChunkMap.
|
---|
2996 | * @param pVM The VM handle.
|
---|
2997 | * @param idChunk The chunk to map.
|
---|
2998 | */
|
---|
2999 | VMMR3DECL(int) PGMR3PhysChunkMap(PVM pVM, uint32_t idChunk)
|
---|
3000 | {
|
---|
3001 | PPGMCHUNKR3MAP pChunk;
|
---|
3002 | int rc;
|
---|
3003 |
|
---|
3004 | pgmLock(pVM);
|
---|
3005 | rc = pgmR3PhysChunkMap(pVM, idChunk, &pChunk);
|
---|
3006 | pgmUnlock(pVM);
|
---|
3007 | return rc;
|
---|
3008 | }
|
---|
3009 |
|
---|
3010 |
|
---|
3011 | /**
|
---|
3012 | * Invalidates the TLB for the ring-3 mapping cache.
|
---|
3013 | *
|
---|
3014 | * @param pVM The VM handle.
|
---|
3015 | */
|
---|
3016 | VMMR3DECL(void) PGMR3PhysChunkInvalidateTLB(PVM pVM)
|
---|
3017 | {
|
---|
3018 | pgmLock(pVM);
|
---|
3019 | for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.ChunkR3Map.Tlb.aEntries); i++)
|
---|
3020 | {
|
---|
3021 | pVM->pgm.s.ChunkR3Map.Tlb.aEntries[i].idChunk = NIL_GMM_CHUNKID;
|
---|
3022 | pVM->pgm.s.ChunkR3Map.Tlb.aEntries[i].pChunk = NULL;
|
---|
3023 | }
|
---|
3024 | pgmUnlock(pVM);
|
---|
3025 | }
|
---|
3026 |
|
---|
3027 |
|
---|
3028 | /**
|
---|
3029 | * Response to VM_FF_PGM_NEED_HANDY_PAGES and VMMCALLRING3_PGM_ALLOCATE_HANDY_PAGES.
|
---|
3030 | *
|
---|
3031 | * This function will also work the VM_FF_PGM_NO_MEMORY force action flag, to
|
---|
3032 | * signal and clear the out of memory condition. When contracted, this API is
|
---|
3033 | * used to try clear the condition when the user wants to resume.
|
---|
3034 | *
|
---|
3035 | * @returns The following VBox status codes.
|
---|
3036 | * @retval VINF_SUCCESS on success. FFs cleared.
|
---|
3037 | * @retval VINF_EM_NO_MEMORY if we're out of memory. The FF is not cleared in
|
---|
3038 | * this case and it gets accompanied by VM_FF_PGM_NO_MEMORY.
|
---|
3039 | *
|
---|
3040 | * @param pVM The VM handle.
|
---|
3041 | *
|
---|
3042 | * @remarks The VINF_EM_NO_MEMORY status is for the benefit of the FF processing
|
---|
3043 | * in EM.cpp and shouldn't be propagated outside TRPM, HWACCM, EM and
|
---|
3044 | * pgmPhysEnsureHandyPage. There is one exception to this in the \#PF
|
---|
3045 | * handler.
|
---|
3046 | */
|
---|
3047 | VMMR3DECL(int) PGMR3PhysAllocateHandyPages(PVM pVM)
|
---|
3048 | {
|
---|
3049 | pgmLock(pVM);
|
---|
3050 |
|
---|
3051 | /*
|
---|
3052 | * Allocate more pages, noting down the index of the first new page.
|
---|
3053 | */
|
---|
3054 | uint32_t iClear = pVM->pgm.s.cHandyPages;
|
---|
3055 | AssertMsgReturn(iClear <= RT_ELEMENTS(pVM->pgm.s.aHandyPages), ("%d", iClear), VERR_INTERNAL_ERROR);
|
---|
3056 | Log(("PGMR3PhysAllocateHandyPages: %d -> %d\n", iClear, RT_ELEMENTS(pVM->pgm.s.aHandyPages)));
|
---|
3057 | int rcAlloc = VINF_SUCCESS;
|
---|
3058 | int rcSeed = VINF_SUCCESS;
|
---|
3059 | int rc = VMMR3CallR0(pVM, VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES, 0, NULL);
|
---|
3060 | while (rc == VERR_GMM_SEED_ME)
|
---|
3061 | {
|
---|
3062 | void *pvChunk;
|
---|
3063 | rcAlloc = rc = SUPR3PageAlloc(GMM_CHUNK_SIZE >> PAGE_SHIFT, &pvChunk);
|
---|
3064 | if (RT_SUCCESS(rc))
|
---|
3065 | {
|
---|
3066 | rcSeed = rc = VMMR3CallR0(pVM, VMMR0_DO_GMM_SEED_CHUNK, (uintptr_t)pvChunk, NULL);
|
---|
3067 | if (RT_FAILURE(rc))
|
---|
3068 | SUPR3PageFree(pvChunk, GMM_CHUNK_SIZE >> PAGE_SHIFT);
|
---|
3069 | }
|
---|
3070 | if (RT_SUCCESS(rc))
|
---|
3071 | rc = VMMR3CallR0(pVM, VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES, 0, NULL);
|
---|
3072 | }
|
---|
3073 |
|
---|
3074 | if (RT_SUCCESS(rc))
|
---|
3075 | {
|
---|
3076 | AssertMsg(rc == VINF_SUCCESS, ("%Rrc\n", rc));
|
---|
3077 | Assert(pVM->pgm.s.cHandyPages > 0);
|
---|
3078 | VM_FF_CLEAR(pVM, VM_FF_PGM_NEED_HANDY_PAGES);
|
---|
3079 | VM_FF_CLEAR(pVM, VM_FF_PGM_NO_MEMORY);
|
---|
3080 |
|
---|
3081 | /*
|
---|
3082 | * Clear the pages.
|
---|
3083 | */
|
---|
3084 | while (iClear < pVM->pgm.s.cHandyPages)
|
---|
3085 | {
|
---|
3086 | PGMMPAGEDESC pPage = &pVM->pgm.s.aHandyPages[iClear];
|
---|
3087 | void *pv;
|
---|
3088 | rc = pgmPhysPageMapByPageID(pVM, pPage->idPage, pPage->HCPhysGCPhys, &pv);
|
---|
3089 | AssertLogRelMsgBreak(RT_SUCCESS(rc), ("idPage=%#x HCPhysGCPhys=%RHp rc=%Rrc", pPage->idPage, pPage->HCPhysGCPhys, rc));
|
---|
3090 | ASMMemZeroPage(pv);
|
---|
3091 | iClear++;
|
---|
3092 | Log3(("PGMR3PhysAllocateHandyPages: idPage=%#x HCPhys=%RGp\n", pPage->idPage, pPage->HCPhysGCPhys));
|
---|
3093 | }
|
---|
3094 | }
|
---|
3095 | else
|
---|
3096 | {
|
---|
3097 | /*
|
---|
3098 | * We should never get here unless there is a genuine shortage of
|
---|
3099 | * memory (or some internal error). Flag the error so the VM can be
|
---|
3100 | * suspended ASAP and the user informed. If we're totally out of
|
---|
3101 | * handy pages we will return failure.
|
---|
3102 | */
|
---|
3103 | /* Report the failure. */
|
---|
3104 | LogRel(("PGM: Failed to procure handy pages; rc=%Rrc rcAlloc=%Rrc rcSeed=%Rrc cHandyPages=%#x\n"
|
---|
3105 | " cAllPages=%#x cPrivatePages=%#x cSharedPages=%#x cZeroPages=%#x\n",
|
---|
3106 | rc, rcSeed, rcAlloc,
|
---|
3107 | pVM->pgm.s.cHandyPages,
|
---|
3108 | pVM->pgm.s.cAllPages,
|
---|
3109 | pVM->pgm.s.cPrivatePages,
|
---|
3110 | pVM->pgm.s.cSharedPages,
|
---|
3111 | pVM->pgm.s.cZeroPages));
|
---|
3112 | if ( rc != VERR_NO_MEMORY
|
---|
3113 | && rc != VERR_LOCK_FAILED)
|
---|
3114 | {
|
---|
3115 | for (uint32_t i = 0; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
|
---|
3116 | {
|
---|
3117 | LogRel(("PGM: aHandyPages[#%#04x] = {.HCPhysGCPhys=%RHp, .idPage=%#08x, .idSharedPage=%#08x}\n",
|
---|
3118 | i, pVM->pgm.s.aHandyPages[i].HCPhysGCPhys, pVM->pgm.s.aHandyPages[i].idPage,
|
---|
3119 | pVM->pgm.s.aHandyPages[i].idSharedPage));
|
---|
3120 | uint32_t const idPage = pVM->pgm.s.aHandyPages[i].idPage;
|
---|
3121 | if (idPage != NIL_GMM_PAGEID)
|
---|
3122 | {
|
---|
3123 | for (PPGMRAMRANGE pRam = pVM->pgm.s.pRamRangesR3;
|
---|
3124 | pRam;
|
---|
3125 | pRam = pRam->pNextR3)
|
---|
3126 | {
|
---|
3127 | uint32_t const cPages = pRam->cb >> PAGE_SHIFT;
|
---|
3128 | for (uint32_t iPage = 0; iPage < cPages; iPage++)
|
---|
3129 | if (PGM_PAGE_GET_PAGEID(&pRam->aPages[iPage]) == idPage)
|
---|
3130 | LogRel(("PGM: Used by %RGp %R[pgmpage] (%s)\n",
|
---|
3131 | pRam->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), &pRam->aPages[iPage], pRam->pszDesc));
|
---|
3132 | }
|
---|
3133 | }
|
---|
3134 | }
|
---|
3135 | }
|
---|
3136 |
|
---|
3137 | /* Set the FFs and adjust rc. */
|
---|
3138 | VM_FF_SET(pVM, VM_FF_PGM_NEED_HANDY_PAGES);
|
---|
3139 | VM_FF_SET(pVM, VM_FF_PGM_NO_MEMORY);
|
---|
3140 | if ( rc == VERR_NO_MEMORY
|
---|
3141 | || rc == VERR_LOCK_FAILED)
|
---|
3142 | rc = VINF_EM_NO_MEMORY;
|
---|
3143 | }
|
---|
3144 |
|
---|
3145 | pgmUnlock(pVM);
|
---|
3146 | return rc;
|
---|
3147 | }
|
---|
3148 |
|
---|
3149 |
|
---|
3150 | /**
|
---|
3151 | * Frees the specified RAM page and replaces it with the ZERO page.
|
---|
3152 | *
|
---|
3153 | * This is used by ballooning, remapping MMIO2 and RAM reset.
|
---|
3154 | *
|
---|
3155 | * @param pVM Pointer to the shared VM structure.
|
---|
3156 | * @param pReq Pointer to the request.
|
---|
3157 | * @param pPage Pointer to the page structure.
|
---|
3158 | * @param GCPhys The guest physical address of the page, if applicable.
|
---|
3159 | *
|
---|
3160 | * @remarks The caller must own the PGM lock.
|
---|
3161 | */
|
---|
3162 | static int pgmPhysFreePage(PVM pVM, PGMMFREEPAGESREQ pReq, uint32_t *pcPendingPages, PPGMPAGE pPage, RTGCPHYS GCPhys)
|
---|
3163 | {
|
---|
3164 | /*
|
---|
3165 | * Assert sanity.
|
---|
3166 | */
|
---|
3167 | Assert(PGMIsLockOwner(pVM));
|
---|
3168 | if (RT_UNLIKELY( PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_RAM
|
---|
3169 | && PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_ROM_SHADOW))
|
---|
3170 | {
|
---|
3171 | AssertMsgFailed(("GCPhys=%RGp pPage=%R[pgmpage]\n", GCPhys, pPage));
|
---|
3172 | return VMSetError(pVM, VERR_PGM_PHYS_NOT_RAM, RT_SRC_POS, "GCPhys=%RGp type=%d", GCPhys, PGM_PAGE_GET_TYPE(pPage));
|
---|
3173 | }
|
---|
3174 |
|
---|
3175 | if (PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ZERO)
|
---|
3176 | return VINF_SUCCESS;
|
---|
3177 |
|
---|
3178 | const uint32_t idPage = PGM_PAGE_GET_PAGEID(pPage);
|
---|
3179 | Log3(("pgmPhysFreePage: idPage=%#x HCPhys=%RGp pPage=%R[pgmpage]\n", idPage, pPage));
|
---|
3180 | if (RT_UNLIKELY( idPage == NIL_GMM_PAGEID
|
---|
3181 | || idPage > GMM_PAGEID_LAST
|
---|
3182 | || PGM_PAGE_GET_CHUNKID(pPage) == NIL_GMM_CHUNKID))
|
---|
3183 | {
|
---|
3184 | AssertMsgFailed(("GCPhys=%RGp pPage=%R[pgmpage]\n", GCPhys, pPage));
|
---|
3185 | return VMSetError(pVM, VERR_PGM_PHYS_INVALID_PAGE_ID, RT_SRC_POS, "GCPhys=%RGp idPage=%#x", GCPhys, pPage);
|
---|
3186 | }
|
---|
3187 |
|
---|
3188 | /* update page count stats. */
|
---|
3189 | if (PGM_PAGE_IS_SHARED(pPage))
|
---|
3190 | pVM->pgm.s.cSharedPages--;
|
---|
3191 | else
|
---|
3192 | pVM->pgm.s.cPrivatePages--;
|
---|
3193 | pVM->pgm.s.cZeroPages++;
|
---|
3194 |
|
---|
3195 | /*
|
---|
3196 | * pPage = ZERO page.
|
---|
3197 | */
|
---|
3198 | PGM_PAGE_SET_HCPHYS(pPage, pVM->pgm.s.HCPhysZeroPg);
|
---|
3199 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ZERO);
|
---|
3200 | PGM_PAGE_SET_PAGEID(pPage, NIL_GMM_PAGEID);
|
---|
3201 |
|
---|
3202 | /*
|
---|
3203 | * Make sure it's not in the handy page array.
|
---|
3204 | */
|
---|
3205 | for (uint32_t i = pVM->pgm.s.cHandyPages; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
|
---|
3206 | {
|
---|
3207 | if (pVM->pgm.s.aHandyPages[i].idPage == idPage)
|
---|
3208 | {
|
---|
3209 | pVM->pgm.s.aHandyPages[i].idPage = NIL_GMM_PAGEID;
|
---|
3210 | break;
|
---|
3211 | }
|
---|
3212 | if (pVM->pgm.s.aHandyPages[i].idSharedPage == idPage)
|
---|
3213 | {
|
---|
3214 | pVM->pgm.s.aHandyPages[i].idSharedPage = NIL_GMM_PAGEID;
|
---|
3215 | break;
|
---|
3216 | }
|
---|
3217 | }
|
---|
3218 |
|
---|
3219 | /*
|
---|
3220 | * Push it onto the page array.
|
---|
3221 | */
|
---|
3222 | uint32_t iPage = *pcPendingPages;
|
---|
3223 | Assert(iPage < PGMPHYS_FREE_PAGE_BATCH_SIZE);
|
---|
3224 | *pcPendingPages += 1;
|
---|
3225 |
|
---|
3226 | pReq->aPages[iPage].idPage = idPage;
|
---|
3227 |
|
---|
3228 | if (iPage + 1 < PGMPHYS_FREE_PAGE_BATCH_SIZE)
|
---|
3229 | return VINF_SUCCESS;
|
---|
3230 |
|
---|
3231 | /*
|
---|
3232 | * Flush the pages.
|
---|
3233 | */
|
---|
3234 | int rc = GMMR3FreePagesPerform(pVM, pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE);
|
---|
3235 | if (RT_SUCCESS(rc))
|
---|
3236 | {
|
---|
3237 | GMMR3FreePagesRePrep(pVM, pReq, PGMPHYS_FREE_PAGE_BATCH_SIZE, GMMACCOUNT_BASE);
|
---|
3238 | *pcPendingPages = 0;
|
---|
3239 | }
|
---|
3240 | return rc;
|
---|
3241 | }
|
---|
3242 |
|
---|
3243 |
|
---|
3244 | /**
|
---|
3245 | * Converts a GC physical address to a HC ring-3 pointer, with some
|
---|
3246 | * additional checks.
|
---|
3247 | *
|
---|
3248 | * @returns VBox status code.
|
---|
3249 | * @retval VINF_SUCCESS on success.
|
---|
3250 | * @retval VINF_PGM_PHYS_TLB_CATCH_WRITE and *ppv set if the page has a write
|
---|
3251 | * access handler of some kind.
|
---|
3252 | * @retval VERR_PGM_PHYS_TLB_CATCH_ALL if the page has a handler catching all
|
---|
3253 | * accesses or is odd in any way.
|
---|
3254 | * @retval VERR_PGM_PHYS_TLB_UNASSIGNED if the page doesn't exist.
|
---|
3255 | *
|
---|
3256 | * @param pVM The VM handle.
|
---|
3257 | * @param GCPhys The GC physical address to convert.
|
---|
3258 | * @param fWritable Whether write access is required.
|
---|
3259 | * @param ppv Where to store the pointer corresponding to GCPhys on
|
---|
3260 | * success.
|
---|
3261 | */
|
---|
3262 | VMMR3DECL(int) PGMR3PhysTlbGCPhys2Ptr(PVM pVM, RTGCPHYS GCPhys, bool fWritable, void **ppv)
|
---|
3263 | {
|
---|
3264 | pgmLock(pVM);
|
---|
3265 |
|
---|
3266 | PPGMRAMRANGE pRam;
|
---|
3267 | PPGMPAGE pPage;
|
---|
3268 | int rc = pgmPhysGetPageAndRangeEx(&pVM->pgm.s, GCPhys, &pPage, &pRam);
|
---|
3269 | if (RT_SUCCESS(rc))
|
---|
3270 | {
|
---|
3271 | if (!PGM_PAGE_HAS_ANY_HANDLERS(pPage))
|
---|
3272 | rc = VINF_SUCCESS;
|
---|
3273 | else
|
---|
3274 | {
|
---|
3275 | if (PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage)) /* catches MMIO */
|
---|
3276 | rc = VERR_PGM_PHYS_TLB_CATCH_ALL;
|
---|
3277 | else if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage))
|
---|
3278 | {
|
---|
3279 | /** @todo Handle TLB loads of virtual handlers so ./test.sh can be made to work
|
---|
3280 | * in -norawr0 mode. */
|
---|
3281 | if (fWritable)
|
---|
3282 | rc = VINF_PGM_PHYS_TLB_CATCH_WRITE;
|
---|
3283 | }
|
---|
3284 | else
|
---|
3285 | {
|
---|
3286 | /* Temporarily disabled physical handler(s), since the recompiler
|
---|
3287 | doesn't get notified when it's reset we'll have to pretend it's
|
---|
3288 | operating normally. */
|
---|
3289 | if (pgmHandlerPhysicalIsAll(pVM, GCPhys))
|
---|
3290 | rc = VERR_PGM_PHYS_TLB_CATCH_ALL;
|
---|
3291 | else
|
---|
3292 | rc = VINF_PGM_PHYS_TLB_CATCH_WRITE;
|
---|
3293 | }
|
---|
3294 | }
|
---|
3295 | if (RT_SUCCESS(rc))
|
---|
3296 | {
|
---|
3297 | int rc2;
|
---|
3298 |
|
---|
3299 | /* Make sure what we return is writable. */
|
---|
3300 | if (fWritable && rc != VINF_PGM_PHYS_TLB_CATCH_WRITE)
|
---|
3301 | switch (PGM_PAGE_GET_STATE(pPage))
|
---|
3302 | {
|
---|
3303 | case PGM_PAGE_STATE_ALLOCATED:
|
---|
3304 | break;
|
---|
3305 | case PGM_PAGE_STATE_ZERO:
|
---|
3306 | case PGM_PAGE_STATE_SHARED:
|
---|
3307 | case PGM_PAGE_STATE_WRITE_MONITORED:
|
---|
3308 | rc2 = pgmPhysPageMakeWritable(pVM, pPage, GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK);
|
---|
3309 | AssertLogRelRCReturn(rc2, rc2);
|
---|
3310 | break;
|
---|
3311 | }
|
---|
3312 |
|
---|
3313 | /* Get a ring-3 mapping of the address. */
|
---|
3314 | PPGMPAGER3MAPTLBE pTlbe;
|
---|
3315 | rc2 = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
|
---|
3316 | AssertLogRelRCReturn(rc2, rc2);
|
---|
3317 | *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
|
---|
3318 | /** @todo mapping/locking hell; this isn't horribly efficient since
|
---|
3319 | * pgmPhysPageLoadIntoTlb will repeat the lookup we've done here. */
|
---|
3320 |
|
---|
3321 | Log6(("PGMR3PhysTlbGCPhys2Ptr: GCPhys=%RGp rc=%Rrc pPage=%R[pgmpage] *ppv=%p\n", GCPhys, rc, pPage, *ppv));
|
---|
3322 | }
|
---|
3323 | else
|
---|
3324 | Log6(("PGMR3PhysTlbGCPhys2Ptr: GCPhys=%RGp rc=%Rrc pPage=%R[pgmpage]\n", GCPhys, rc, pPage));
|
---|
3325 |
|
---|
3326 | /* else: handler catching all access, no pointer returned. */
|
---|
3327 | }
|
---|
3328 | else
|
---|
3329 | rc = VERR_PGM_PHYS_TLB_UNASSIGNED;
|
---|
3330 |
|
---|
3331 | pgmUnlock(pVM);
|
---|
3332 | return rc;
|
---|
3333 | }
|
---|
3334 |
|
---|
3335 |
|
---|
3336 |
|
---|