1 | /* $Id: PGMAllPhys.cpp 26616 2010-02-17 15:44:15Z 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 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #define LOG_GROUP LOG_GROUP_PGM_PHYS
|
---|
26 | #include <VBox/pgm.h>
|
---|
27 | #include <VBox/trpm.h>
|
---|
28 | #include <VBox/vmm.h>
|
---|
29 | #include <VBox/iom.h>
|
---|
30 | #include <VBox/em.h>
|
---|
31 | #include <VBox/rem.h>
|
---|
32 | #include "../PGMInternal.h"
|
---|
33 | #include <VBox/vm.h>
|
---|
34 | #include "../PGMInline.h"
|
---|
35 | #include <VBox/param.h>
|
---|
36 | #include <VBox/err.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/asm.h>
|
---|
40 | #include <VBox/log.h>
|
---|
41 | #ifdef IN_RING3
|
---|
42 | # include <iprt/thread.h>
|
---|
43 | #endif
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Defined Constants And Macros *
|
---|
48 | *******************************************************************************/
|
---|
49 | /** Enable the physical TLB. */
|
---|
50 | #define PGM_WITH_PHYS_TLB
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 | #ifndef IN_RING3
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * \#PF Handler callback for Guest ROM range write access.
|
---|
58 | * We simply ignore the writes or fall back to the recompiler if we don't support the instruction.
|
---|
59 | *
|
---|
60 | * @returns VBox status code (appropritate for trap handling and GC return).
|
---|
61 | * @param pVM VM Handle.
|
---|
62 | * @param uErrorCode CPU Error code.
|
---|
63 | * @param pRegFrame Trap register frame.
|
---|
64 | * @param pvFault The fault address (cr2).
|
---|
65 | * @param GCPhysFault The GC physical address corresponding to pvFault.
|
---|
66 | * @param pvUser User argument. Pointer to the ROM range structure.
|
---|
67 | */
|
---|
68 | VMMDECL(int) pgmPhysRomWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
|
---|
69 | {
|
---|
70 | int rc;
|
---|
71 | PPGMROMRANGE pRom = (PPGMROMRANGE)pvUser;
|
---|
72 | uint32_t iPage = (GCPhysFault - pRom->GCPhys) >> PAGE_SHIFT;
|
---|
73 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
74 |
|
---|
75 | Assert(iPage < (pRom->cb >> PAGE_SHIFT));
|
---|
76 | switch (pRom->aPages[iPage].enmProt)
|
---|
77 | {
|
---|
78 | case PGMROMPROT_READ_ROM_WRITE_IGNORE:
|
---|
79 | case PGMROMPROT_READ_RAM_WRITE_IGNORE:
|
---|
80 | {
|
---|
81 | /*
|
---|
82 | * If it's a simple instruction which doesn't change the cpu state
|
---|
83 | * we will simply skip it. Otherwise we'll have to defer it to REM.
|
---|
84 | */
|
---|
85 | uint32_t cbOp;
|
---|
86 | PDISCPUSTATE pDis = &pVCpu->pgm.s.DisState;
|
---|
87 | rc = EMInterpretDisasOne(pVM, pVCpu, pRegFrame, pDis, &cbOp);
|
---|
88 | if ( RT_SUCCESS(rc)
|
---|
89 | && pDis->mode == CPUMODE_32BIT /** @todo why does this matter? */
|
---|
90 | && !(pDis->prefix & (PREFIX_REPNE | PREFIX_REP | PREFIX_SEG)))
|
---|
91 | {
|
---|
92 | switch (pDis->opcode)
|
---|
93 | {
|
---|
94 | /** @todo Find other instructions we can safely skip, possibly
|
---|
95 | * adding this kind of detection to DIS or EM. */
|
---|
96 | case OP_MOV:
|
---|
97 | pRegFrame->rip += cbOp;
|
---|
98 | STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZGuestROMWriteHandled);
|
---|
99 | return VINF_SUCCESS;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | else if (RT_UNLIKELY(rc == VERR_INTERNAL_ERROR))
|
---|
103 | return rc;
|
---|
104 | break;
|
---|
105 | }
|
---|
106 |
|
---|
107 | case PGMROMPROT_READ_RAM_WRITE_RAM:
|
---|
108 | pRom->aPages[iPage].LiveSave.fWrittenTo = true;
|
---|
109 | rc = PGMHandlerPhysicalPageTempOff(pVM, pRom->GCPhys, GCPhysFault & X86_PTE_PG_MASK);
|
---|
110 | AssertRC(rc);
|
---|
111 | break; /** @todo Must edit the shadow PT and restart the instruction, not use the interpreter! */
|
---|
112 |
|
---|
113 | case PGMROMPROT_READ_ROM_WRITE_RAM:
|
---|
114 | /* Handle it in ring-3 because it's *way* easier there. */
|
---|
115 | pRom->aPages[iPage].LiveSave.fWrittenTo = true;
|
---|
116 | break;
|
---|
117 |
|
---|
118 | default:
|
---|
119 | AssertMsgFailedReturn(("enmProt=%d iPage=%d GCPhysFault=%RGp\n",
|
---|
120 | pRom->aPages[iPage].enmProt, iPage, GCPhysFault),
|
---|
121 | VERR_INTERNAL_ERROR);
|
---|
122 | }
|
---|
123 |
|
---|
124 | STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZGuestROMWriteUnhandled);
|
---|
125 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
126 | }
|
---|
127 |
|
---|
128 | #endif /* IN_RING3 */
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Checks if Address Gate 20 is enabled or not.
|
---|
132 | *
|
---|
133 | * @returns true if enabled.
|
---|
134 | * @returns false if disabled.
|
---|
135 | * @param pVCpu VMCPU handle.
|
---|
136 | */
|
---|
137 | VMMDECL(bool) PGMPhysIsA20Enabled(PVMCPU pVCpu)
|
---|
138 | {
|
---|
139 | LogFlow(("PGMPhysIsA20Enabled %d\n", pVCpu->pgm.s.fA20Enabled));
|
---|
140 | return pVCpu->pgm.s.fA20Enabled;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Validates a GC physical address.
|
---|
146 | *
|
---|
147 | * @returns true if valid.
|
---|
148 | * @returns false if invalid.
|
---|
149 | * @param pVM The VM handle.
|
---|
150 | * @param GCPhys The physical address to validate.
|
---|
151 | */
|
---|
152 | VMMDECL(bool) PGMPhysIsGCPhysValid(PVM pVM, RTGCPHYS GCPhys)
|
---|
153 | {
|
---|
154 | PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
|
---|
155 | return pPage != NULL;
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Checks if a GC physical address is a normal page,
|
---|
161 | * i.e. not ROM, MMIO or reserved.
|
---|
162 | *
|
---|
163 | * @returns true if normal.
|
---|
164 | * @returns false if invalid, ROM, MMIO or reserved page.
|
---|
165 | * @param pVM The VM handle.
|
---|
166 | * @param GCPhys The physical address to check.
|
---|
167 | */
|
---|
168 | VMMDECL(bool) PGMPhysIsGCPhysNormal(PVM pVM, RTGCPHYS GCPhys)
|
---|
169 | {
|
---|
170 | PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
|
---|
171 | return pPage
|
---|
172 | && PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM;
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Converts a GC physical address to a HC physical address.
|
---|
178 | *
|
---|
179 | * @returns VINF_SUCCESS on success.
|
---|
180 | * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
|
---|
181 | * page but has no physical backing.
|
---|
182 | * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
|
---|
183 | * GC physical address.
|
---|
184 | *
|
---|
185 | * @param pVM The VM handle.
|
---|
186 | * @param GCPhys The GC physical address to convert.
|
---|
187 | * @param pHCPhys Where to store the HC physical address on success.
|
---|
188 | */
|
---|
189 | VMMDECL(int) PGMPhysGCPhys2HCPhys(PVM pVM, RTGCPHYS GCPhys, PRTHCPHYS pHCPhys)
|
---|
190 | {
|
---|
191 | pgmLock(pVM);
|
---|
192 | PPGMPAGE pPage;
|
---|
193 | int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhys, &pPage);
|
---|
194 | if (RT_SUCCESS(rc))
|
---|
195 | *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK);
|
---|
196 | pgmUnlock(pVM);
|
---|
197 | return rc;
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Invalidates all page mapping TLBs.
|
---|
203 | *
|
---|
204 | * @param pVM The VM handle.
|
---|
205 | */
|
---|
206 | VMMDECL(void) PGMPhysInvalidatePageMapTLB(PVM pVM)
|
---|
207 | {
|
---|
208 | pgmLock(pVM);
|
---|
209 | STAM_COUNTER_INC(&pVM->pgm.s.StatPageMapTlbFlushes);
|
---|
210 | /* Clear the shared R0/R3 TLB completely. */
|
---|
211 | for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.PhysTlbHC.aEntries); i++)
|
---|
212 | {
|
---|
213 | pVM->pgm.s.PhysTlbHC.aEntries[i].GCPhys = NIL_RTGCPHYS;
|
---|
214 | pVM->pgm.s.PhysTlbHC.aEntries[i].pPage = 0;
|
---|
215 | pVM->pgm.s.PhysTlbHC.aEntries[i].pMap = 0;
|
---|
216 | pVM->pgm.s.PhysTlbHC.aEntries[i].pv = 0;
|
---|
217 | }
|
---|
218 | /* @todo clear the RC TLB whenever we add it. */
|
---|
219 | pgmUnlock(pVM);
|
---|
220 | }
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Invalidates a page mapping TLB entry
|
---|
224 | *
|
---|
225 | * @param pVM The VM handle.
|
---|
226 | * @param GCPhys GCPhys entry to flush
|
---|
227 | */
|
---|
228 | VMMDECL(void) PGMPhysInvalidatePageMapTLBEntry(PVM pVM, RTGCPHYS GCPhys)
|
---|
229 | {
|
---|
230 | Assert(PGMIsLocked(pVM));
|
---|
231 |
|
---|
232 | STAM_COUNTER_INC(&pVM->pgm.s.StatPageMapTlbFlushEntry);
|
---|
233 | /* Clear the shared R0/R3 TLB entry. */
|
---|
234 | #ifdef IN_RC
|
---|
235 | unsigned idx = PGM_PAGER3MAPTLB_IDX(GCPhys);
|
---|
236 | pVM->pgm.s.PhysTlbHC.aEntries[idx].GCPhys = NIL_RTGCPHYS;
|
---|
237 | pVM->pgm.s.PhysTlbHC.aEntries[idx].pPage = 0;
|
---|
238 | pVM->pgm.s.PhysTlbHC.aEntries[idx].pMap = 0;
|
---|
239 | pVM->pgm.s.PhysTlbHC.aEntries[idx].pv = 0;
|
---|
240 | #else
|
---|
241 | PPGMPAGEMAPTLBE pTlbe = &pVM->pgm.s.CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
|
---|
242 | pTlbe->GCPhys = NIL_RTGCPHYS;
|
---|
243 | pTlbe->pPage = 0;
|
---|
244 | pTlbe->pMap = 0;
|
---|
245 | pTlbe->pv = 0;
|
---|
246 | #endif
|
---|
247 | /* @todo clear the RC TLB whenever we add it. */
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Makes sure that there is at least one handy page ready for use.
|
---|
252 | *
|
---|
253 | * This will also take the appropriate actions when reaching water-marks.
|
---|
254 | *
|
---|
255 | * @returns VBox status code.
|
---|
256 | * @retval VINF_SUCCESS on success.
|
---|
257 | * @retval VERR_EM_NO_MEMORY if we're really out of memory.
|
---|
258 | *
|
---|
259 | * @param pVM The VM handle.
|
---|
260 | *
|
---|
261 | * @remarks Must be called from within the PGM critical section. It may
|
---|
262 | * nip back to ring-3/0 in some cases.
|
---|
263 | */
|
---|
264 | static int pgmPhysEnsureHandyPage(PVM pVM)
|
---|
265 | {
|
---|
266 | AssertMsg(pVM->pgm.s.cHandyPages <= RT_ELEMENTS(pVM->pgm.s.aHandyPages), ("%d\n", pVM->pgm.s.cHandyPages));
|
---|
267 |
|
---|
268 | /*
|
---|
269 | * Do we need to do anything special?
|
---|
270 | */
|
---|
271 | #ifdef IN_RING3
|
---|
272 | if (pVM->pgm.s.cHandyPages <= RT_MAX(PGM_HANDY_PAGES_SET_FF, PGM_HANDY_PAGES_R3_ALLOC))
|
---|
273 | #else
|
---|
274 | if (pVM->pgm.s.cHandyPages <= RT_MAX(PGM_HANDY_PAGES_SET_FF, PGM_HANDY_PAGES_RZ_TO_R3))
|
---|
275 | #endif
|
---|
276 | {
|
---|
277 | /*
|
---|
278 | * Allocate pages only if we're out of them, or in ring-3, almost out.
|
---|
279 | */
|
---|
280 | #ifdef IN_RING3
|
---|
281 | if (pVM->pgm.s.cHandyPages <= PGM_HANDY_PAGES_R3_ALLOC)
|
---|
282 | #else
|
---|
283 | if (pVM->pgm.s.cHandyPages <= PGM_HANDY_PAGES_RZ_ALLOC)
|
---|
284 | #endif
|
---|
285 | {
|
---|
286 | Log(("PGM: cHandyPages=%u out of %u -> allocate more; VM_FF_PGM_NO_MEMORY=%RTbool\n",
|
---|
287 | pVM->pgm.s.cHandyPages, RT_ELEMENTS(pVM->pgm.s.aHandyPages), VM_FF_ISSET(pVM, VM_FF_PGM_NO_MEMORY) ));
|
---|
288 | #ifdef IN_RING3
|
---|
289 | int rc = PGMR3PhysAllocateHandyPages(pVM);
|
---|
290 | #else
|
---|
291 | int rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_PGM_ALLOCATE_HANDY_PAGES, 0);
|
---|
292 | #endif
|
---|
293 | if (RT_UNLIKELY(rc != VINF_SUCCESS))
|
---|
294 | {
|
---|
295 | if (RT_FAILURE(rc))
|
---|
296 | return rc;
|
---|
297 | AssertMsgReturn(rc == VINF_EM_NO_MEMORY, ("%Rrc\n", rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
|
---|
298 | if (!pVM->pgm.s.cHandyPages)
|
---|
299 | {
|
---|
300 | LogRel(("PGM: no more handy pages!\n"));
|
---|
301 | return VERR_EM_NO_MEMORY;
|
---|
302 | }
|
---|
303 | Assert(VM_FF_ISSET(pVM, VM_FF_PGM_NEED_HANDY_PAGES));
|
---|
304 | Assert(VM_FF_ISSET(pVM, VM_FF_PGM_NO_MEMORY));
|
---|
305 | #ifdef IN_RING3
|
---|
306 | REMR3NotifyFF(pVM);
|
---|
307 | #else
|
---|
308 | VMCPU_FF_SET(VMMGetCpu(pVM), VMCPU_FF_TO_R3); /* paranoia */
|
---|
309 | #endif
|
---|
310 | }
|
---|
311 | AssertMsgReturn( pVM->pgm.s.cHandyPages > 0
|
---|
312 | && pVM->pgm.s.cHandyPages <= RT_ELEMENTS(pVM->pgm.s.aHandyPages),
|
---|
313 | ("%u\n", pVM->pgm.s.cHandyPages),
|
---|
314 | VERR_INTERNAL_ERROR);
|
---|
315 | }
|
---|
316 | else
|
---|
317 | {
|
---|
318 | if (pVM->pgm.s.cHandyPages <= PGM_HANDY_PAGES_SET_FF)
|
---|
319 | VM_FF_SET(pVM, VM_FF_PGM_NEED_HANDY_PAGES);
|
---|
320 | #ifndef IN_RING3
|
---|
321 | if (pVM->pgm.s.cHandyPages <= PGM_HANDY_PAGES_RZ_TO_R3)
|
---|
322 | {
|
---|
323 | Log(("PGM: VM_FF_TO_R3 - cHandyPages=%u out of %u\n", pVM->pgm.s.cHandyPages, RT_ELEMENTS(pVM->pgm.s.aHandyPages)));
|
---|
324 | VMCPU_FF_SET(VMMGetCpu(pVM), VMCPU_FF_TO_R3);
|
---|
325 | }
|
---|
326 | #endif
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | return VINF_SUCCESS;
|
---|
331 | }
|
---|
332 |
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * Replace a zero or shared page with new page that we can write to.
|
---|
336 | *
|
---|
337 | * @returns The following VBox status codes.
|
---|
338 | * @retval VINF_SUCCESS on success, pPage is modified.
|
---|
339 | * @retval VINF_PGM_SYNC_CR3 on success and a page pool flush is pending.
|
---|
340 | * @retval VERR_EM_NO_MEMORY if we're totally out of memory.
|
---|
341 | *
|
---|
342 | * @todo Propagate VERR_EM_NO_MEMORY up the call tree.
|
---|
343 | *
|
---|
344 | * @param pVM The VM address.
|
---|
345 | * @param pPage The physical page tracking structure. This will
|
---|
346 | * be modified on success.
|
---|
347 | * @param GCPhys The address of the page.
|
---|
348 | *
|
---|
349 | * @remarks Must be called from within the PGM critical section. It may
|
---|
350 | * nip back to ring-3/0 in some cases.
|
---|
351 | *
|
---|
352 | * @remarks This function shouldn't really fail, however if it does
|
---|
353 | * it probably means we've screwed up the size of handy pages and/or
|
---|
354 | * the low-water mark. Or, that some device I/O is causing a lot of
|
---|
355 | * pages to be allocated while while the host is in a low-memory
|
---|
356 | * condition. This latter should be handled elsewhere and in a more
|
---|
357 | * controlled manner, it's on the @bugref{3170} todo list...
|
---|
358 | */
|
---|
359 | int pgmPhysAllocPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
|
---|
360 | {
|
---|
361 | LogFlow(("pgmPhysAllocPage: %R[pgmpage] %RGp\n", pPage, GCPhys));
|
---|
362 |
|
---|
363 | /*
|
---|
364 | * Prereqs.
|
---|
365 | */
|
---|
366 | Assert(PGMIsLocked(pVM));
|
---|
367 | AssertMsg(PGM_PAGE_IS_ZERO(pPage) || PGM_PAGE_IS_SHARED(pPage), ("%R[pgmpage] %RGp\n", pPage, GCPhys));
|
---|
368 | Assert(!PGM_PAGE_IS_MMIO(pPage));
|
---|
369 |
|
---|
370 |
|
---|
371 | /*
|
---|
372 | * Flush any shadow page table mappings of the page.
|
---|
373 | * When VBOX_WITH_NEW_LAZY_PAGE_ALLOC isn't defined, there shouldn't be any.
|
---|
374 | */
|
---|
375 | bool fFlushTLBs = false;
|
---|
376 | int rc = pgmPoolTrackFlushGCPhys(pVM, pPage, &fFlushTLBs);
|
---|
377 | AssertMsgReturn(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3, ("%Rrc\n", rc), RT_FAILURE(rc) ? rc : VERR_IPE_UNEXPECTED_STATUS);
|
---|
378 |
|
---|
379 | /*
|
---|
380 | * Ensure that we've got a page handy, take it and use it.
|
---|
381 | */
|
---|
382 | int rc2 = pgmPhysEnsureHandyPage(pVM);
|
---|
383 | if (RT_FAILURE(rc2))
|
---|
384 | {
|
---|
385 | if (fFlushTLBs)
|
---|
386 | PGM_INVL_ALL_VCPU_TLBS(pVM);
|
---|
387 | Assert(rc2 == VERR_EM_NO_MEMORY);
|
---|
388 | return rc2;
|
---|
389 | }
|
---|
390 | /* re-assert preconditions since pgmPhysEnsureHandyPage may do a context switch. */
|
---|
391 | Assert(PGMIsLocked(pVM));
|
---|
392 | AssertMsg(PGM_PAGE_IS_ZERO(pPage) || PGM_PAGE_IS_SHARED(pPage), ("%R[pgmpage] %RGp\n", pPage, GCPhys));
|
---|
393 | Assert(!PGM_PAGE_IS_MMIO(pPage));
|
---|
394 |
|
---|
395 | uint32_t iHandyPage = --pVM->pgm.s.cHandyPages;
|
---|
396 | AssertMsg(iHandyPage < RT_ELEMENTS(pVM->pgm.s.aHandyPages), ("%d\n", iHandyPage));
|
---|
397 | Assert(pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys != NIL_RTHCPHYS);
|
---|
398 | Assert(!(pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
|
---|
399 | Assert(pVM->pgm.s.aHandyPages[iHandyPage].idPage != NIL_GMM_PAGEID);
|
---|
400 | Assert(pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage == NIL_GMM_PAGEID);
|
---|
401 |
|
---|
402 | /*
|
---|
403 | * There are one or two action to be taken the next time we allocate handy pages:
|
---|
404 | * - Tell the GMM (global memory manager) what the page is being used for.
|
---|
405 | * (Speeds up replacement operations - sharing and defragmenting.)
|
---|
406 | * - If the current backing is shared, it must be freed.
|
---|
407 | */
|
---|
408 | const RTHCPHYS HCPhys = pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys;
|
---|
409 | pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys = GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK;
|
---|
410 |
|
---|
411 | if (PGM_PAGE_IS_SHARED(pPage))
|
---|
412 | {
|
---|
413 | pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage = PGM_PAGE_GET_PAGEID(pPage);
|
---|
414 | Assert(PGM_PAGE_GET_PAGEID(pPage) != NIL_GMM_PAGEID);
|
---|
415 | VM_FF_SET(pVM, VM_FF_PGM_NEED_HANDY_PAGES);
|
---|
416 |
|
---|
417 | Log2(("PGM: Replaced shared page %#x at %RGp with %#x / %RHp\n", PGM_PAGE_GET_PAGEID(pPage),
|
---|
418 | GCPhys, pVM->pgm.s.aHandyPages[iHandyPage].idPage, HCPhys));
|
---|
419 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,PageReplaceShared));
|
---|
420 | pVM->pgm.s.cSharedPages--;
|
---|
421 | AssertMsgFailed(("TODO: copy shared page content")); /** @todo err.. what about copying the page content? */
|
---|
422 | }
|
---|
423 | else
|
---|
424 | {
|
---|
425 | Log2(("PGM: Replaced zero page %RGp with %#x / %RHp\n", GCPhys, pVM->pgm.s.aHandyPages[iHandyPage].idPage, HCPhys));
|
---|
426 | STAM_COUNTER_INC(&pVM->pgm.s.StatRZPageReplaceZero);
|
---|
427 | pVM->pgm.s.cZeroPages--;
|
---|
428 | Assert(pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage == NIL_GMM_PAGEID);
|
---|
429 | }
|
---|
430 |
|
---|
431 | /*
|
---|
432 | * Do the PGMPAGE modifications.
|
---|
433 | */
|
---|
434 | pVM->pgm.s.cPrivatePages++;
|
---|
435 | PGM_PAGE_SET_HCPHYS(pPage, HCPhys);
|
---|
436 | PGM_PAGE_SET_PAGEID(pPage, pVM->pgm.s.aHandyPages[iHandyPage].idPage);
|
---|
437 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
|
---|
438 | PGM_PAGE_SET_PDE_TYPE(pPage, PGM_PAGE_PDE_TYPE_PT);
|
---|
439 | PGMPhysInvalidatePageMapTLBEntry(pVM, GCPhys);
|
---|
440 |
|
---|
441 | if ( fFlushTLBs
|
---|
442 | && rc != VINF_PGM_GCPHYS_ALIASED)
|
---|
443 | PGM_INVL_ALL_VCPU_TLBS(pVM);
|
---|
444 | return rc;
|
---|
445 | }
|
---|
446 |
|
---|
447 | /**
|
---|
448 | * Replace a 2 MB range of zero pages with new pages that we can write to.
|
---|
449 | *
|
---|
450 | * @returns The following VBox status codes.
|
---|
451 | * @retval VINF_SUCCESS on success, pPage is modified.
|
---|
452 | * @retval VINF_PGM_SYNC_CR3 on success and a page pool flush is pending.
|
---|
453 | * @retval VERR_EM_NO_MEMORY if we're totally out of memory.
|
---|
454 | *
|
---|
455 | * @todo Propagate VERR_EM_NO_MEMORY up the call tree.
|
---|
456 | *
|
---|
457 | * @param pVM The VM address.
|
---|
458 | * @param GCPhys The address of the page.
|
---|
459 | *
|
---|
460 | * @remarks Must be called from within the PGM critical section. It may
|
---|
461 | * nip back to ring-3/0 in some cases.
|
---|
462 | *
|
---|
463 | * @remarks This function shouldn't really fail, however if it does
|
---|
464 | * it probably means we've screwed up the size of handy pages and/or
|
---|
465 | * the low-water mark. Or, that some device I/O is causing a lot of
|
---|
466 | * pages to be allocated while while the host is in a low-memory
|
---|
467 | * condition. This latter should be handled elsewhere and in a more
|
---|
468 | * controlled manner, it's on the @bugref{3170} todo list...
|
---|
469 | */
|
---|
470 | int pgmPhysAllocLargePage(PVM pVM, RTGCPHYS GCPhys)
|
---|
471 | {
|
---|
472 | LogFlow(("pgmPhysAllocLargePage: %RGp\n", GCPhys));
|
---|
473 |
|
---|
474 | /*
|
---|
475 | * Prereqs.
|
---|
476 | */
|
---|
477 | Assert(PGMIsLocked(pVM));
|
---|
478 | Assert((GCPhys & X86_PD_PAE_MASK) == 0);
|
---|
479 |
|
---|
480 | #ifdef IN_RING3
|
---|
481 | int rc = PGMR3PhysAllocateLargeHandyPage(pVM, GCPhys);
|
---|
482 | #else
|
---|
483 | int rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_PGM_ALLOCATE_LARGE_HANDY_PAGE, GCPhys);
|
---|
484 | #endif
|
---|
485 | return rc;
|
---|
486 | }
|
---|
487 |
|
---|
488 | /**
|
---|
489 | * Deal with a write monitored page.
|
---|
490 | *
|
---|
491 | * @returns VBox strict status code.
|
---|
492 | *
|
---|
493 | * @param pVM The VM address.
|
---|
494 | * @param pPage The physical page tracking structure.
|
---|
495 | *
|
---|
496 | * @remarks Called from within the PGM critical section.
|
---|
497 | */
|
---|
498 | void pgmPhysPageMakeWriteMonitoredWritable(PVM pVM, PPGMPAGE pPage)
|
---|
499 | {
|
---|
500 | Assert(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_WRITE_MONITORED);
|
---|
501 | PGM_PAGE_SET_WRITTEN_TO(pPage);
|
---|
502 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
|
---|
503 | Assert(pVM->pgm.s.cMonitoredPages > 0);
|
---|
504 | pVM->pgm.s.cMonitoredPages--;
|
---|
505 | pVM->pgm.s.cWrittenToPages++;
|
---|
506 | }
|
---|
507 |
|
---|
508 |
|
---|
509 | /**
|
---|
510 | * Deal with pages that are not writable, i.e. not in the ALLOCATED state.
|
---|
511 | *
|
---|
512 | * @returns VBox strict status code.
|
---|
513 | * @retval VINF_SUCCESS on success.
|
---|
514 | * @retval VINF_PGM_SYNC_CR3 on success and a page pool flush is pending.
|
---|
515 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
516 | *
|
---|
517 | * @param pVM The VM address.
|
---|
518 | * @param pPage The physical page tracking structure.
|
---|
519 | * @param GCPhys The address of the page.
|
---|
520 | *
|
---|
521 | * @remarks Called from within the PGM critical section.
|
---|
522 | */
|
---|
523 | int pgmPhysPageMakeWritable(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
|
---|
524 | {
|
---|
525 | switch (PGM_PAGE_GET_STATE(pPage))
|
---|
526 | {
|
---|
527 | case PGM_PAGE_STATE_WRITE_MONITORED:
|
---|
528 | pgmPhysPageMakeWriteMonitoredWritable(pVM, pPage);
|
---|
529 | /* fall thru */
|
---|
530 | default: /* to shut up GCC */
|
---|
531 | case PGM_PAGE_STATE_ALLOCATED:
|
---|
532 | return VINF_SUCCESS;
|
---|
533 |
|
---|
534 | /*
|
---|
535 | * Zero pages can be dummy pages for MMIO or reserved memory,
|
---|
536 | * so we need to check the flags before joining cause with
|
---|
537 | * shared page replacement.
|
---|
538 | */
|
---|
539 | case PGM_PAGE_STATE_ZERO:
|
---|
540 | if (PGM_PAGE_IS_MMIO(pPage))
|
---|
541 | return VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
542 | /* fall thru */
|
---|
543 | case PGM_PAGE_STATE_SHARED:
|
---|
544 | return pgmPhysAllocPage(pVM, pPage, GCPhys);
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 |
|
---|
549 | /**
|
---|
550 | * Wrapper for pgmPhysPageMakeWritable which enters the critsect.
|
---|
551 | *
|
---|
552 | * @returns VBox strict status code.
|
---|
553 | * @retval VINF_SUCCESS on success.
|
---|
554 | * @retval VINF_PGM_SYNC_CR3 on success and a page pool flush is pending.
|
---|
555 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
556 | *
|
---|
557 | * @param pVM The VM address.
|
---|
558 | * @param pPage The physical page tracking structure.
|
---|
559 | * @param GCPhys The address of the page.
|
---|
560 | */
|
---|
561 | int pgmPhysPageMakeWritableUnlocked(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
|
---|
562 | {
|
---|
563 | int rc = pgmLock(pVM);
|
---|
564 | if (RT_SUCCESS(rc))
|
---|
565 | {
|
---|
566 | rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
|
---|
567 | pgmUnlock(pVM);
|
---|
568 | }
|
---|
569 | return rc;
|
---|
570 | }
|
---|
571 |
|
---|
572 |
|
---|
573 | /**
|
---|
574 | * Internal usage: Map the page specified by its GMM ID.
|
---|
575 | *
|
---|
576 | * This is similar to pgmPhysPageMap
|
---|
577 | *
|
---|
578 | * @returns VBox status code.
|
---|
579 | *
|
---|
580 | * @param pVM The VM handle.
|
---|
581 | * @param idPage The Page ID.
|
---|
582 | * @param HCPhys The physical address (for RC).
|
---|
583 | * @param ppv Where to store the mapping address.
|
---|
584 | *
|
---|
585 | * @remarks Called from within the PGM critical section. The mapping is only
|
---|
586 | * valid while your inside this section.
|
---|
587 | */
|
---|
588 | int pgmPhysPageMapByPageID(PVM pVM, uint32_t idPage, RTHCPHYS HCPhys, void **ppv)
|
---|
589 | {
|
---|
590 | /*
|
---|
591 | * Validation.
|
---|
592 | */
|
---|
593 | Assert(PGMIsLocked(pVM));
|
---|
594 | AssertReturn(HCPhys && !(HCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
595 | const uint32_t idChunk = idPage >> GMM_CHUNKID_SHIFT;
|
---|
596 | AssertReturn(idChunk != NIL_GMM_CHUNKID, VERR_INVALID_PARAMETER);
|
---|
597 |
|
---|
598 | #ifdef IN_RC
|
---|
599 | /*
|
---|
600 | * Map it by HCPhys.
|
---|
601 | */
|
---|
602 | return PGMDynMapHCPage(pVM, HCPhys, ppv);
|
---|
603 |
|
---|
604 | #elif defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
605 | /*
|
---|
606 | * Map it by HCPhys.
|
---|
607 | */
|
---|
608 | return pgmR0DynMapHCPageInlined(&pVM->pgm.s, HCPhys, ppv);
|
---|
609 |
|
---|
610 | #else
|
---|
611 | /*
|
---|
612 | * Find/make Chunk TLB entry for the mapping chunk.
|
---|
613 | */
|
---|
614 | PPGMCHUNKR3MAP pMap;
|
---|
615 | PPGMCHUNKR3MAPTLBE pTlbe = &pVM->pgm.s.ChunkR3Map.Tlb.aEntries[PGM_CHUNKR3MAPTLB_IDX(idChunk)];
|
---|
616 | if (pTlbe->idChunk == idChunk)
|
---|
617 | {
|
---|
618 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbHits));
|
---|
619 | pMap = pTlbe->pChunk;
|
---|
620 | }
|
---|
621 | else
|
---|
622 | {
|
---|
623 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbMisses));
|
---|
624 |
|
---|
625 | /*
|
---|
626 | * Find the chunk, map it if necessary.
|
---|
627 | */
|
---|
628 | pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
|
---|
629 | if (!pMap)
|
---|
630 | {
|
---|
631 | # ifdef IN_RING0
|
---|
632 | int rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_PGM_MAP_CHUNK, idChunk);
|
---|
633 | AssertRCReturn(rc, rc);
|
---|
634 | pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
|
---|
635 | Assert(pMap);
|
---|
636 | # else
|
---|
637 | int rc = pgmR3PhysChunkMap(pVM, idChunk, &pMap);
|
---|
638 | if (RT_FAILURE(rc))
|
---|
639 | return rc;
|
---|
640 | # endif
|
---|
641 | }
|
---|
642 |
|
---|
643 | /*
|
---|
644 | * Enter it into the Chunk TLB.
|
---|
645 | */
|
---|
646 | pTlbe->idChunk = idChunk;
|
---|
647 | pTlbe->pChunk = pMap;
|
---|
648 | pMap->iAge = 0;
|
---|
649 | }
|
---|
650 |
|
---|
651 | *ppv = (uint8_t *)pMap->pv + ((idPage &GMM_PAGEID_IDX_MASK) << PAGE_SHIFT);
|
---|
652 | return VINF_SUCCESS;
|
---|
653 | #endif
|
---|
654 | }
|
---|
655 |
|
---|
656 |
|
---|
657 | /**
|
---|
658 | * Maps a page into the current virtual address space so it can be accessed.
|
---|
659 | *
|
---|
660 | * @returns VBox status code.
|
---|
661 | * @retval VINF_SUCCESS on success.
|
---|
662 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
663 | *
|
---|
664 | * @param pVM The VM address.
|
---|
665 | * @param pPage The physical page tracking structure.
|
---|
666 | * @param GCPhys The address of the page.
|
---|
667 | * @param ppMap Where to store the address of the mapping tracking structure.
|
---|
668 | * @param ppv Where to store the mapping address of the page. The page
|
---|
669 | * offset is masked off!
|
---|
670 | *
|
---|
671 | * @remarks Called from within the PGM critical section.
|
---|
672 | */
|
---|
673 | static int pgmPhysPageMapCommon(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, PPPGMPAGEMAP ppMap, void **ppv)
|
---|
674 | {
|
---|
675 | Assert(PGMIsLocked(pVM));
|
---|
676 |
|
---|
677 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
678 | /*
|
---|
679 | * Just some sketchy GC/R0-darwin code.
|
---|
680 | */
|
---|
681 | *ppMap = NULL;
|
---|
682 | RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
|
---|
683 | Assert(HCPhys != pVM->pgm.s.HCPhysZeroPg);
|
---|
684 | # ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
|
---|
685 | pgmR0DynMapHCPageInlined(&pVM->pgm.s, HCPhys, ppv);
|
---|
686 | # else
|
---|
687 | PGMDynMapHCPage(pVM, HCPhys, ppv);
|
---|
688 | # endif
|
---|
689 | return VINF_SUCCESS;
|
---|
690 |
|
---|
691 | #else /* IN_RING3 || IN_RING0 */
|
---|
692 |
|
---|
693 |
|
---|
694 | /*
|
---|
695 | * Special case: ZERO and MMIO2 pages.
|
---|
696 | */
|
---|
697 | const uint32_t idChunk = PGM_PAGE_GET_CHUNKID(pPage);
|
---|
698 | if (idChunk == NIL_GMM_CHUNKID)
|
---|
699 | {
|
---|
700 | AssertMsgReturn(PGM_PAGE_GET_PAGEID(pPage) == NIL_GMM_PAGEID, ("pPage=%R[pgmpage]\n", pPage), VERR_INTERNAL_ERROR_2);
|
---|
701 | if (PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2)
|
---|
702 | {
|
---|
703 | /* Lookup the MMIO2 range and use pvR3 to calc the address. */
|
---|
704 | PPGMRAMRANGE pRam = pgmPhysGetRange(&pVM->pgm.s, GCPhys);
|
---|
705 | AssertMsgReturn(pRam || !pRam->pvR3, ("pRam=%p pPage=%R[pgmpage]\n", pRam, pPage), VERR_INTERNAL_ERROR_2);
|
---|
706 | *ppv = (void *)((uintptr_t)pRam->pvR3 + (uintptr_t)((GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK) - pRam->GCPhys));
|
---|
707 | }
|
---|
708 | else if (PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO)
|
---|
709 | {
|
---|
710 | /** @todo deal with aliased MMIO2 pages somehow...
|
---|
711 | * One solution would be to seed MMIO2 pages to GMM and get unique Page IDs for
|
---|
712 | * them, that would also avoid this mess. It would actually be kind of
|
---|
713 | * elegant... */
|
---|
714 | AssertLogRelMsgFailedReturn(("%RGp\n", GCPhys), VERR_INTERNAL_ERROR_3);
|
---|
715 | }
|
---|
716 | else
|
---|
717 | {
|
---|
718 | /** @todo handle MMIO2 */
|
---|
719 | AssertMsgReturn(PGM_PAGE_IS_ZERO(pPage), ("pPage=%R[pgmpage]\n", pPage), VERR_INTERNAL_ERROR_2);
|
---|
720 | AssertMsgReturn(PGM_PAGE_GET_HCPHYS(pPage) == pVM->pgm.s.HCPhysZeroPg,
|
---|
721 | ("pPage=%R[pgmpage]\n", pPage),
|
---|
722 | VERR_INTERNAL_ERROR_2);
|
---|
723 | *ppv = pVM->pgm.s.CTXALLSUFF(pvZeroPg);
|
---|
724 | }
|
---|
725 | *ppMap = NULL;
|
---|
726 | return VINF_SUCCESS;
|
---|
727 | }
|
---|
728 |
|
---|
729 | /*
|
---|
730 | * Find/make Chunk TLB entry for the mapping chunk.
|
---|
731 | */
|
---|
732 | PPGMCHUNKR3MAP pMap;
|
---|
733 | PPGMCHUNKR3MAPTLBE pTlbe = &pVM->pgm.s.ChunkR3Map.Tlb.aEntries[PGM_CHUNKR3MAPTLB_IDX(idChunk)];
|
---|
734 | if (pTlbe->idChunk == idChunk)
|
---|
735 | {
|
---|
736 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbHits));
|
---|
737 | pMap = pTlbe->pChunk;
|
---|
738 | }
|
---|
739 | else
|
---|
740 | {
|
---|
741 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbMisses));
|
---|
742 |
|
---|
743 | /*
|
---|
744 | * Find the chunk, map it if necessary.
|
---|
745 | */
|
---|
746 | pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
|
---|
747 | if (!pMap)
|
---|
748 | {
|
---|
749 | #ifdef IN_RING0
|
---|
750 | int rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_PGM_MAP_CHUNK, idChunk);
|
---|
751 | AssertRCReturn(rc, rc);
|
---|
752 | pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
|
---|
753 | Assert(pMap);
|
---|
754 | #else
|
---|
755 | int rc = pgmR3PhysChunkMap(pVM, idChunk, &pMap);
|
---|
756 | if (RT_FAILURE(rc))
|
---|
757 | return rc;
|
---|
758 | #endif
|
---|
759 | }
|
---|
760 |
|
---|
761 | /*
|
---|
762 | * Enter it into the Chunk TLB.
|
---|
763 | */
|
---|
764 | pTlbe->idChunk = idChunk;
|
---|
765 | pTlbe->pChunk = pMap;
|
---|
766 | pMap->iAge = 0;
|
---|
767 | }
|
---|
768 |
|
---|
769 | *ppv = (uint8_t *)pMap->pv + (PGM_PAGE_GET_PAGE_IN_CHUNK(pPage) << PAGE_SHIFT);
|
---|
770 | *ppMap = pMap;
|
---|
771 | return VINF_SUCCESS;
|
---|
772 | #endif /* IN_RING3 */
|
---|
773 | }
|
---|
774 |
|
---|
775 |
|
---|
776 | /**
|
---|
777 | * Combination of pgmPhysPageMakeWritable and pgmPhysPageMapWritable.
|
---|
778 | *
|
---|
779 | * This is typically used is paths where we cannot use the TLB methods (like ROM
|
---|
780 | * pages) or where there is no point in using them since we won't get many hits.
|
---|
781 | *
|
---|
782 | * @returns VBox strict status code.
|
---|
783 | * @retval VINF_SUCCESS on success.
|
---|
784 | * @retval VINF_PGM_SYNC_CR3 on success and a page pool flush is pending.
|
---|
785 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
786 | *
|
---|
787 | * @param pVM The VM address.
|
---|
788 | * @param pPage The physical page tracking structure.
|
---|
789 | * @param GCPhys The address of the page.
|
---|
790 | * @param ppv Where to store the mapping address of the page. The page
|
---|
791 | * offset is masked off!
|
---|
792 | *
|
---|
793 | * @remarks Called from within the PGM critical section. The mapping is only
|
---|
794 | * valid while your inside this section.
|
---|
795 | */
|
---|
796 | int pgmPhysPageMakeWritableAndMap(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv)
|
---|
797 | {
|
---|
798 | int rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
|
---|
799 | if (RT_SUCCESS(rc))
|
---|
800 | {
|
---|
801 | AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* returned */, ("%Rrc\n", rc));
|
---|
802 | PPGMPAGEMAP pMapIgnore;
|
---|
803 | int rc2 = pgmPhysPageMapCommon(pVM, pPage, GCPhys, &pMapIgnore, ppv);
|
---|
804 | if (RT_FAILURE(rc2)) /* preserve rc */
|
---|
805 | rc = rc2;
|
---|
806 | }
|
---|
807 | return rc;
|
---|
808 | }
|
---|
809 |
|
---|
810 |
|
---|
811 | /**
|
---|
812 | * Maps a page into the current virtual address space so it can be accessed for
|
---|
813 | * both writing and reading.
|
---|
814 | *
|
---|
815 | * This is typically used is paths where we cannot use the TLB methods (like ROM
|
---|
816 | * pages) or where there is no point in using them since we won't get many hits.
|
---|
817 | *
|
---|
818 | * @returns VBox status code.
|
---|
819 | * @retval VINF_SUCCESS on success.
|
---|
820 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
821 | *
|
---|
822 | * @param pVM The VM address.
|
---|
823 | * @param pPage The physical page tracking structure. Must be in the
|
---|
824 | * allocated state.
|
---|
825 | * @param GCPhys The address of the page.
|
---|
826 | * @param ppv Where to store the mapping address of the page. The page
|
---|
827 | * offset is masked off!
|
---|
828 | *
|
---|
829 | * @remarks Called from within the PGM critical section. The mapping is only
|
---|
830 | * valid while your inside this section.
|
---|
831 | */
|
---|
832 | int pgmPhysPageMap(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv)
|
---|
833 | {
|
---|
834 | Assert(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED);
|
---|
835 | PPGMPAGEMAP pMapIgnore;
|
---|
836 | return pgmPhysPageMapCommon(pVM, pPage, GCPhys, &pMapIgnore, ppv);
|
---|
837 | }
|
---|
838 |
|
---|
839 |
|
---|
840 | /**
|
---|
841 | * Maps a page into the current virtual address space so it can be accessed for
|
---|
842 | * reading.
|
---|
843 | *
|
---|
844 | * This is typically used is paths where we cannot use the TLB methods (like ROM
|
---|
845 | * pages) or where there is no point in using them since we won't get many hits.
|
---|
846 | *
|
---|
847 | * @returns VBox status code.
|
---|
848 | * @retval VINF_SUCCESS on success.
|
---|
849 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
850 | *
|
---|
851 | * @param pVM The VM address.
|
---|
852 | * @param pPage The physical page tracking structure.
|
---|
853 | * @param GCPhys The address of the page.
|
---|
854 | * @param ppv Where to store the mapping address of the page. The page
|
---|
855 | * offset is masked off!
|
---|
856 | *
|
---|
857 | * @remarks Called from within the PGM critical section. The mapping is only
|
---|
858 | * valid while your inside this section.
|
---|
859 | */
|
---|
860 | int pgmPhysPageMapReadOnly(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void const **ppv)
|
---|
861 | {
|
---|
862 | PPGMPAGEMAP pMapIgnore;
|
---|
863 | return pgmPhysPageMapCommon(pVM, pPage, GCPhys, &pMapIgnore, (void **)ppv);
|
---|
864 | }
|
---|
865 |
|
---|
866 |
|
---|
867 | #if !defined(IN_RC) && !defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
868 | /**
|
---|
869 | * Load a guest page into the ring-3 physical TLB.
|
---|
870 | *
|
---|
871 | * @returns VBox status code.
|
---|
872 | * @retval VINF_SUCCESS on success
|
---|
873 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
874 | * @param pPGM The PGM instance pointer.
|
---|
875 | * @param GCPhys The guest physical address in question.
|
---|
876 | */
|
---|
877 | int pgmPhysPageLoadIntoTlb(PPGM pPGM, RTGCPHYS GCPhys)
|
---|
878 | {
|
---|
879 | Assert(PGMIsLocked(PGM2VM(pPGM)));
|
---|
880 | STAM_COUNTER_INC(&pPGM->CTX_MID_Z(Stat,PageMapTlbMisses));
|
---|
881 |
|
---|
882 | /*
|
---|
883 | * Find the ram range.
|
---|
884 | * 99.8% of requests are expected to be in the first range.
|
---|
885 | */
|
---|
886 | PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
|
---|
887 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
888 | if (RT_UNLIKELY(off >= pRam->cb))
|
---|
889 | {
|
---|
890 | do
|
---|
891 | {
|
---|
892 | pRam = pRam->CTX_SUFF(pNext);
|
---|
893 | if (!pRam)
|
---|
894 | return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
|
---|
895 | off = GCPhys - pRam->GCPhys;
|
---|
896 | } while (off >= pRam->cb);
|
---|
897 | }
|
---|
898 |
|
---|
899 | /*
|
---|
900 | * Map the page.
|
---|
901 | * Make a special case for the zero page as it is kind of special.
|
---|
902 | */
|
---|
903 | PPGMPAGE pPage = &pRam->aPages[off >> PAGE_SHIFT];
|
---|
904 | PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
|
---|
905 | if (!PGM_PAGE_IS_ZERO(pPage))
|
---|
906 | {
|
---|
907 | void *pv;
|
---|
908 | PPGMPAGEMAP pMap;
|
---|
909 | int rc = pgmPhysPageMapCommon(PGM2VM(pPGM), pPage, GCPhys, &pMap, &pv);
|
---|
910 | if (RT_FAILURE(rc))
|
---|
911 | return rc;
|
---|
912 | pTlbe->pMap = pMap;
|
---|
913 | pTlbe->pv = pv;
|
---|
914 | Assert(!((uintptr_t)pTlbe->pv & PAGE_OFFSET_MASK));
|
---|
915 | }
|
---|
916 | else
|
---|
917 | {
|
---|
918 | Assert(PGM_PAGE_GET_HCPHYS(pPage) == pPGM->HCPhysZeroPg);
|
---|
919 | pTlbe->pMap = NULL;
|
---|
920 | pTlbe->pv = pPGM->CTXALLSUFF(pvZeroPg);
|
---|
921 | }
|
---|
922 | #ifdef PGM_WITH_PHYS_TLB
|
---|
923 | pTlbe->GCPhys = GCPhys & X86_PTE_PAE_PG_MASK;
|
---|
924 | #else
|
---|
925 | pTlbe->GCPhys = NIL_RTGCPHYS;
|
---|
926 | #endif
|
---|
927 | pTlbe->pPage = pPage;
|
---|
928 | return VINF_SUCCESS;
|
---|
929 | }
|
---|
930 |
|
---|
931 |
|
---|
932 | /**
|
---|
933 | * Load a guest page into the ring-3 physical TLB.
|
---|
934 | *
|
---|
935 | * @returns VBox status code.
|
---|
936 | * @retval VINF_SUCCESS on success
|
---|
937 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
938 | *
|
---|
939 | * @param pPGM The PGM instance pointer.
|
---|
940 | * @param pPage Pointer to the PGMPAGE structure corresponding to
|
---|
941 | * GCPhys.
|
---|
942 | * @param GCPhys The guest physical address in question.
|
---|
943 | */
|
---|
944 | int pgmPhysPageLoadIntoTlbWithPage(PPGM pPGM, PPGMPAGE pPage, RTGCPHYS GCPhys)
|
---|
945 | {
|
---|
946 | Assert(PGMIsLocked(PGM2VM(pPGM)));
|
---|
947 | STAM_COUNTER_INC(&pPGM->CTX_MID_Z(Stat,PageMapTlbMisses));
|
---|
948 |
|
---|
949 | /*
|
---|
950 | * Map the page.
|
---|
951 | * Make a special case for the zero page as it is kind of special.
|
---|
952 | */
|
---|
953 | PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
|
---|
954 | if (!PGM_PAGE_IS_ZERO(pPage))
|
---|
955 | {
|
---|
956 | void *pv;
|
---|
957 | PPGMPAGEMAP pMap;
|
---|
958 | int rc = pgmPhysPageMapCommon(PGM2VM(pPGM), pPage, GCPhys, &pMap, &pv);
|
---|
959 | if (RT_FAILURE(rc))
|
---|
960 | return rc;
|
---|
961 | pTlbe->pMap = pMap;
|
---|
962 | pTlbe->pv = pv;
|
---|
963 | Assert(!((uintptr_t)pTlbe->pv & PAGE_OFFSET_MASK));
|
---|
964 | }
|
---|
965 | else
|
---|
966 | {
|
---|
967 | Assert(PGM_PAGE_GET_HCPHYS(pPage) == pPGM->HCPhysZeroPg);
|
---|
968 | pTlbe->pMap = NULL;
|
---|
969 | pTlbe->pv = pPGM->CTXALLSUFF(pvZeroPg);
|
---|
970 | }
|
---|
971 | #ifdef PGM_WITH_PHYS_TLB
|
---|
972 | pTlbe->GCPhys = GCPhys & X86_PTE_PAE_PG_MASK;
|
---|
973 | #else
|
---|
974 | pTlbe->GCPhys = NIL_RTGCPHYS;
|
---|
975 | #endif
|
---|
976 | pTlbe->pPage = pPage;
|
---|
977 | return VINF_SUCCESS;
|
---|
978 | }
|
---|
979 | #endif /* !IN_RC && !VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
|
---|
980 |
|
---|
981 |
|
---|
982 | /**
|
---|
983 | * Internal version of PGMPhysGCPhys2CCPtr that expects the caller to
|
---|
984 | * own the PGM lock and therefore not need to lock the mapped page.
|
---|
985 | *
|
---|
986 | * @returns VBox status code.
|
---|
987 | * @retval VINF_SUCCESS on success.
|
---|
988 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
989 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
990 | *
|
---|
991 | * @param pVM The VM handle.
|
---|
992 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
993 | * @param pPage Pointer to the PGMPAGE structure for the page.
|
---|
994 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
995 | *
|
---|
996 | * @internal
|
---|
997 | */
|
---|
998 | int pgmPhysGCPhys2CCPtrInternal(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv)
|
---|
999 | {
|
---|
1000 | int rc;
|
---|
1001 | AssertReturn(pPage, VERR_INTERNAL_ERROR);
|
---|
1002 | Assert(PGMIsLocked(pVM));
|
---|
1003 |
|
---|
1004 | /*
|
---|
1005 | * Make sure the page is writable.
|
---|
1006 | */
|
---|
1007 | if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
|
---|
1008 | {
|
---|
1009 | rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
|
---|
1010 | if (RT_FAILURE(rc))
|
---|
1011 | return rc;
|
---|
1012 | AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* not returned */, ("%Rrc\n", rc));
|
---|
1013 | }
|
---|
1014 | Assert(PGM_PAGE_GET_HCPHYS(pPage) != 0);
|
---|
1015 |
|
---|
1016 | /*
|
---|
1017 | * Get the mapping address.
|
---|
1018 | */
|
---|
1019 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1020 | *ppv = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK));
|
---|
1021 | #else
|
---|
1022 | PPGMPAGEMAPTLBE pTlbe;
|
---|
1023 | rc = pgmPhysPageQueryTlbeWithPage(&pVM->pgm.s, pPage, GCPhys, &pTlbe);
|
---|
1024 | if (RT_FAILURE(rc))
|
---|
1025 | return rc;
|
---|
1026 | *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
|
---|
1027 | #endif
|
---|
1028 | return VINF_SUCCESS;
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 |
|
---|
1032 | /**
|
---|
1033 | * Internal version of PGMPhysGCPhys2CCPtrReadOnly that expects the caller to
|
---|
1034 | * own the PGM lock and therefore not need to lock the mapped page.
|
---|
1035 | *
|
---|
1036 | * @returns VBox status code.
|
---|
1037 | * @retval VINF_SUCCESS on success.
|
---|
1038 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
1039 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
1040 | *
|
---|
1041 | * @param pVM The VM handle.
|
---|
1042 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
1043 | * @param pPage Pointer to the PGMPAGE structure for the page.
|
---|
1044 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
1045 | *
|
---|
1046 | * @internal
|
---|
1047 | */
|
---|
1048 | int pgmPhysGCPhys2CCPtrInternalReadOnly(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, const void **ppv)
|
---|
1049 | {
|
---|
1050 | AssertReturn(pPage, VERR_INTERNAL_ERROR);
|
---|
1051 | Assert(PGMIsLocked(pVM));
|
---|
1052 | Assert(PGM_PAGE_GET_HCPHYS(pPage) != 0);
|
---|
1053 |
|
---|
1054 | /*
|
---|
1055 | * Get the mapping address.
|
---|
1056 | */
|
---|
1057 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1058 | *ppv = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK)); /** @todo add a read only flag? */
|
---|
1059 | #else
|
---|
1060 | PPGMPAGEMAPTLBE pTlbe;
|
---|
1061 | int rc = pgmPhysPageQueryTlbeWithPage(&pVM->pgm.s, pPage, GCPhys, &pTlbe);
|
---|
1062 | if (RT_FAILURE(rc))
|
---|
1063 | return rc;
|
---|
1064 | *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
|
---|
1065 | #endif
|
---|
1066 | return VINF_SUCCESS;
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 |
|
---|
1070 | /**
|
---|
1071 | * Requests the mapping of a guest page into the current context.
|
---|
1072 | *
|
---|
1073 | * This API should only be used for very short term, as it will consume
|
---|
1074 | * scarse resources (R0 and GC) in the mapping cache. When you're done
|
---|
1075 | * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
|
---|
1076 | *
|
---|
1077 | * This API will assume your intention is to write to the page, and will
|
---|
1078 | * therefore replace shared and zero pages. If you do not intend to modify
|
---|
1079 | * the page, use the PGMPhysGCPhys2CCPtrReadOnly() API.
|
---|
1080 | *
|
---|
1081 | * @returns VBox status code.
|
---|
1082 | * @retval VINF_SUCCESS on success.
|
---|
1083 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
1084 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
1085 | *
|
---|
1086 | * @param pVM The VM handle.
|
---|
1087 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
1088 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
1089 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
1090 | *
|
---|
1091 | * @remarks The caller is responsible for dealing with access handlers.
|
---|
1092 | * @todo Add an informational return code for pages with access handlers?
|
---|
1093 | *
|
---|
1094 | * @remark Avoid calling this API from within critical sections (other than the
|
---|
1095 | * PGM one) because of the deadlock risk. External threads may need to
|
---|
1096 | * delegate jobs to the EMTs.
|
---|
1097 | * @thread Any thread.
|
---|
1098 | */
|
---|
1099 | VMMDECL(int) PGMPhysGCPhys2CCPtr(PVM pVM, RTGCPHYS GCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
1100 | {
|
---|
1101 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1102 |
|
---|
1103 | /*
|
---|
1104 | * Find the page and make sure it's writable.
|
---|
1105 | */
|
---|
1106 | PPGMPAGE pPage;
|
---|
1107 | int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhys, &pPage);
|
---|
1108 | if (RT_SUCCESS(rc))
|
---|
1109 | {
|
---|
1110 | if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
|
---|
1111 | rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
|
---|
1112 | if (RT_SUCCESS(rc))
|
---|
1113 | {
|
---|
1114 | *ppv = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK)); /** @todo add a read only flag? */
|
---|
1115 | # if 0
|
---|
1116 | pLock->pvMap = 0;
|
---|
1117 | pLock->pvPage = pPage;
|
---|
1118 | # else
|
---|
1119 | pLock->u32Dummy = UINT32_MAX;
|
---|
1120 | # endif
|
---|
1121 | AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* not returned */, ("%Rrc\n", rc));
|
---|
1122 | rc = VINF_SUCCESS;
|
---|
1123 | }
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | #else /* IN_RING3 || IN_RING0 */
|
---|
1127 | int rc = pgmLock(pVM);
|
---|
1128 | AssertRCReturn(rc, rc);
|
---|
1129 |
|
---|
1130 | /*
|
---|
1131 | * Query the Physical TLB entry for the page (may fail).
|
---|
1132 | */
|
---|
1133 | PPGMPAGEMAPTLBE pTlbe;
|
---|
1134 | rc = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
|
---|
1135 | if (RT_SUCCESS(rc))
|
---|
1136 | {
|
---|
1137 | /*
|
---|
1138 | * If the page is shared, the zero page, or being write monitored
|
---|
1139 | * it must be converted to an page that's writable if possible.
|
---|
1140 | */
|
---|
1141 | PPGMPAGE pPage = pTlbe->pPage;
|
---|
1142 | if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
|
---|
1143 | {
|
---|
1144 | rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
|
---|
1145 | if (RT_SUCCESS(rc))
|
---|
1146 | {
|
---|
1147 | AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* not returned */, ("%Rrc\n", rc));
|
---|
1148 | rc = pgmPhysPageQueryTlbeWithPage(&pVM->pgm.s, pPage, GCPhys, &pTlbe);
|
---|
1149 | }
|
---|
1150 | }
|
---|
1151 | if (RT_SUCCESS(rc))
|
---|
1152 | {
|
---|
1153 | /*
|
---|
1154 | * Now, just perform the locking and calculate the return address.
|
---|
1155 | */
|
---|
1156 | PPGMPAGEMAP pMap = pTlbe->pMap;
|
---|
1157 | if (pMap)
|
---|
1158 | pMap->cRefs++;
|
---|
1159 |
|
---|
1160 | unsigned cLocks = PGM_PAGE_GET_WRITE_LOCKS(pPage);
|
---|
1161 | if (RT_LIKELY(cLocks < PGM_PAGE_MAX_LOCKS - 1))
|
---|
1162 | {
|
---|
1163 | if (cLocks == 0)
|
---|
1164 | pVM->pgm.s.cWriteLockedPages++;
|
---|
1165 | PGM_PAGE_INC_WRITE_LOCKS(pPage);
|
---|
1166 | }
|
---|
1167 | else if (cLocks != PGM_PAGE_GET_WRITE_LOCKS(pPage))
|
---|
1168 | {
|
---|
1169 | PGM_PAGE_INC_WRITE_LOCKS(pPage);
|
---|
1170 | AssertMsgFailed(("%RGp / %R[pgmpage] is entering permanent write locked state!\n", GCPhys, pPage));
|
---|
1171 | if (pMap)
|
---|
1172 | pMap->cRefs++; /* Extra ref to prevent it from going away. */
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
|
---|
1176 | pLock->uPageAndType = (uintptr_t)pPage | PGMPAGEMAPLOCK_TYPE_WRITE;
|
---|
1177 | pLock->pvMap = pMap;
|
---|
1178 | }
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | pgmUnlock(pVM);
|
---|
1182 | #endif /* IN_RING3 || IN_RING0 */
|
---|
1183 | return rc;
|
---|
1184 | }
|
---|
1185 |
|
---|
1186 |
|
---|
1187 | /**
|
---|
1188 | * Requests the mapping of a guest page into the current context.
|
---|
1189 | *
|
---|
1190 | * This API should only be used for very short term, as it will consume
|
---|
1191 | * scarse resources (R0 and GC) in the mapping cache. When you're done
|
---|
1192 | * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
|
---|
1193 | *
|
---|
1194 | * @returns VBox status code.
|
---|
1195 | * @retval VINF_SUCCESS on success.
|
---|
1196 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
1197 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
1198 | *
|
---|
1199 | * @param pVM The VM handle.
|
---|
1200 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
1201 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
1202 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
1203 | *
|
---|
1204 | * @remarks The caller is responsible for dealing with access handlers.
|
---|
1205 | * @todo Add an informational return code for pages with access handlers?
|
---|
1206 | *
|
---|
1207 | * @remark Avoid calling this API from within critical sections (other than
|
---|
1208 | * the PGM one) because of the deadlock risk.
|
---|
1209 | * @thread Any thread.
|
---|
1210 | */
|
---|
1211 | VMMDECL(int) PGMPhysGCPhys2CCPtrReadOnly(PVM pVM, RTGCPHYS GCPhys, void const **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
1212 | {
|
---|
1213 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1214 |
|
---|
1215 | /*
|
---|
1216 | * Find the page and make sure it's readable.
|
---|
1217 | */
|
---|
1218 | PPGMPAGE pPage;
|
---|
1219 | int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhys, &pPage);
|
---|
1220 | if (RT_SUCCESS(rc))
|
---|
1221 | {
|
---|
1222 | if (RT_UNLIKELY(PGM_PAGE_IS_MMIO(pPage)))
|
---|
1223 | rc = VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
1224 | else
|
---|
1225 | {
|
---|
1226 | *ppv = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK)); /** @todo add a read only flag? */
|
---|
1227 | # if 0
|
---|
1228 | pLock->pvMap = 0;
|
---|
1229 | pLock->pvPage = pPage;
|
---|
1230 | # else
|
---|
1231 | pLock->u32Dummy = UINT32_MAX;
|
---|
1232 | # endif
|
---|
1233 | AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* not returned */, ("%Rrc\n", rc));
|
---|
1234 | rc = VINF_SUCCESS;
|
---|
1235 | }
|
---|
1236 | }
|
---|
1237 |
|
---|
1238 | #else /* IN_RING3 || IN_RING0 */
|
---|
1239 | int rc = pgmLock(pVM);
|
---|
1240 | AssertRCReturn(rc, rc);
|
---|
1241 |
|
---|
1242 | /*
|
---|
1243 | * Query the Physical TLB entry for the page (may fail).
|
---|
1244 | */
|
---|
1245 | PPGMPAGEMAPTLBE pTlbe;
|
---|
1246 | rc = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
|
---|
1247 | if (RT_SUCCESS(rc))
|
---|
1248 | {
|
---|
1249 | /* MMIO pages doesn't have any readable backing. */
|
---|
1250 | PPGMPAGE pPage = pTlbe->pPage;
|
---|
1251 | if (RT_UNLIKELY(PGM_PAGE_IS_MMIO(pPage)))
|
---|
1252 | rc = VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
1253 | else
|
---|
1254 | {
|
---|
1255 | /*
|
---|
1256 | * Now, just perform the locking and calculate the return address.
|
---|
1257 | */
|
---|
1258 | PPGMPAGEMAP pMap = pTlbe->pMap;
|
---|
1259 | if (pMap)
|
---|
1260 | pMap->cRefs++;
|
---|
1261 |
|
---|
1262 | unsigned cLocks = PGM_PAGE_GET_READ_LOCKS(pPage);
|
---|
1263 | if (RT_LIKELY(cLocks < PGM_PAGE_MAX_LOCKS - 1))
|
---|
1264 | {
|
---|
1265 | if (cLocks == 0)
|
---|
1266 | pVM->pgm.s.cReadLockedPages++;
|
---|
1267 | PGM_PAGE_INC_READ_LOCKS(pPage);
|
---|
1268 | }
|
---|
1269 | else if (cLocks != PGM_PAGE_GET_READ_LOCKS(pPage))
|
---|
1270 | {
|
---|
1271 | PGM_PAGE_INC_READ_LOCKS(pPage);
|
---|
1272 | AssertMsgFailed(("%RGp / %R[pgmpage] is entering permanent readonly locked state!\n", GCPhys, pPage));
|
---|
1273 | if (pMap)
|
---|
1274 | pMap->cRefs++; /* Extra ref to prevent it from going away. */
|
---|
1275 | }
|
---|
1276 |
|
---|
1277 | *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
|
---|
1278 | pLock->uPageAndType = (uintptr_t)pPage | PGMPAGEMAPLOCK_TYPE_READ;
|
---|
1279 | pLock->pvMap = pMap;
|
---|
1280 | }
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | pgmUnlock(pVM);
|
---|
1284 | #endif /* IN_RING3 || IN_RING0 */
|
---|
1285 | return rc;
|
---|
1286 | }
|
---|
1287 |
|
---|
1288 |
|
---|
1289 | /**
|
---|
1290 | * Requests the mapping of a guest page given by virtual address into the current context.
|
---|
1291 | *
|
---|
1292 | * This API should only be used for very short term, as it will consume
|
---|
1293 | * scarse resources (R0 and GC) in the mapping cache. When you're done
|
---|
1294 | * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
|
---|
1295 | *
|
---|
1296 | * This API will assume your intention is to write to the page, and will
|
---|
1297 | * therefore replace shared and zero pages. If you do not intend to modify
|
---|
1298 | * the page, use the PGMPhysGCPtr2CCPtrReadOnly() API.
|
---|
1299 | *
|
---|
1300 | * @returns VBox status code.
|
---|
1301 | * @retval VINF_SUCCESS on success.
|
---|
1302 | * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
|
---|
1303 | * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
|
---|
1304 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
1305 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
1306 | *
|
---|
1307 | * @param pVCpu VMCPU handle.
|
---|
1308 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
1309 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
1310 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
1311 | *
|
---|
1312 | * @remark Avoid calling this API from within critical sections (other than
|
---|
1313 | * the PGM one) because of the deadlock risk.
|
---|
1314 | * @thread EMT
|
---|
1315 | */
|
---|
1316 | VMMDECL(int) PGMPhysGCPtr2CCPtr(PVMCPU pVCpu, RTGCPTR GCPtr, void **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
1317 | {
|
---|
1318 | VM_ASSERT_EMT(pVCpu->CTX_SUFF(pVM));
|
---|
1319 | RTGCPHYS GCPhys;
|
---|
1320 | int rc = PGMPhysGCPtr2GCPhys(pVCpu, GCPtr, &GCPhys);
|
---|
1321 | if (RT_SUCCESS(rc))
|
---|
1322 | rc = PGMPhysGCPhys2CCPtr(pVCpu->CTX_SUFF(pVM), GCPhys, ppv, pLock);
|
---|
1323 | return rc;
|
---|
1324 | }
|
---|
1325 |
|
---|
1326 |
|
---|
1327 | /**
|
---|
1328 | * Requests the mapping of a guest page given by virtual address into the current context.
|
---|
1329 | *
|
---|
1330 | * This API should only be used for very short term, as it will consume
|
---|
1331 | * scarse resources (R0 and GC) in the mapping cache. When you're done
|
---|
1332 | * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
|
---|
1333 | *
|
---|
1334 | * @returns VBox status code.
|
---|
1335 | * @retval VINF_SUCCESS on success.
|
---|
1336 | * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
|
---|
1337 | * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
|
---|
1338 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
1339 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
1340 | *
|
---|
1341 | * @param pVCpu VMCPU handle.
|
---|
1342 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
1343 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
1344 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
1345 | *
|
---|
1346 | * @remark Avoid calling this API from within critical sections (other than
|
---|
1347 | * the PGM one) because of the deadlock risk.
|
---|
1348 | * @thread EMT
|
---|
1349 | */
|
---|
1350 | VMMDECL(int) PGMPhysGCPtr2CCPtrReadOnly(PVMCPU pVCpu, RTGCPTR GCPtr, void const **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
1351 | {
|
---|
1352 | VM_ASSERT_EMT(pVCpu->CTX_SUFF(pVM));
|
---|
1353 | RTGCPHYS GCPhys;
|
---|
1354 | int rc = PGMPhysGCPtr2GCPhys(pVCpu, GCPtr, &GCPhys);
|
---|
1355 | if (RT_SUCCESS(rc))
|
---|
1356 | rc = PGMPhysGCPhys2CCPtrReadOnly(pVCpu->CTX_SUFF(pVM), GCPhys, ppv, pLock);
|
---|
1357 | return rc;
|
---|
1358 | }
|
---|
1359 |
|
---|
1360 |
|
---|
1361 | /**
|
---|
1362 | * Release the mapping of a guest page.
|
---|
1363 | *
|
---|
1364 | * This is the counter part of PGMPhysGCPhys2CCPtr, PGMPhysGCPhys2CCPtrReadOnly
|
---|
1365 | * PGMPhysGCPtr2CCPtr and PGMPhysGCPtr2CCPtrReadOnly.
|
---|
1366 | *
|
---|
1367 | * @param pVM The VM handle.
|
---|
1368 | * @param pLock The lock structure initialized by the mapping function.
|
---|
1369 | */
|
---|
1370 | VMMDECL(void) PGMPhysReleasePageMappingLock(PVM pVM, PPGMPAGEMAPLOCK pLock)
|
---|
1371 | {
|
---|
1372 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1373 | /* currently nothing to do here. */
|
---|
1374 | Assert(pLock->u32Dummy == UINT32_MAX);
|
---|
1375 | pLock->u32Dummy = 0;
|
---|
1376 |
|
---|
1377 | #else /* IN_RING3 */
|
---|
1378 | PPGMPAGEMAP pMap = (PPGMPAGEMAP)pLock->pvMap;
|
---|
1379 | PPGMPAGE pPage = (PPGMPAGE)(pLock->uPageAndType & ~PGMPAGEMAPLOCK_TYPE_MASK);
|
---|
1380 | bool fWriteLock = (pLock->uPageAndType & PGMPAGEMAPLOCK_TYPE_MASK) == PGMPAGEMAPLOCK_TYPE_WRITE;
|
---|
1381 |
|
---|
1382 | pLock->uPageAndType = 0;
|
---|
1383 | pLock->pvMap = NULL;
|
---|
1384 |
|
---|
1385 | pgmLock(pVM);
|
---|
1386 | if (fWriteLock)
|
---|
1387 | {
|
---|
1388 | unsigned cLocks = PGM_PAGE_GET_WRITE_LOCKS(pPage);
|
---|
1389 | Assert(cLocks > 0);
|
---|
1390 | if (RT_LIKELY(cLocks > 0 && cLocks < PGM_PAGE_MAX_LOCKS))
|
---|
1391 | {
|
---|
1392 | if (cLocks == 1)
|
---|
1393 | {
|
---|
1394 | Assert(pVM->pgm.s.cWriteLockedPages > 0);
|
---|
1395 | pVM->pgm.s.cWriteLockedPages--;
|
---|
1396 | }
|
---|
1397 | PGM_PAGE_DEC_WRITE_LOCKS(pPage);
|
---|
1398 | }
|
---|
1399 |
|
---|
1400 | if (PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_WRITE_MONITORED)
|
---|
1401 | {
|
---|
1402 | PGM_PAGE_SET_WRITTEN_TO(pPage);
|
---|
1403 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
|
---|
1404 | Assert(pVM->pgm.s.cMonitoredPages > 0);
|
---|
1405 | pVM->pgm.s.cMonitoredPages--;
|
---|
1406 | pVM->pgm.s.cWrittenToPages++;
|
---|
1407 | }
|
---|
1408 | }
|
---|
1409 | else
|
---|
1410 | {
|
---|
1411 | unsigned cLocks = PGM_PAGE_GET_READ_LOCKS(pPage);
|
---|
1412 | Assert(cLocks > 0);
|
---|
1413 | if (RT_LIKELY(cLocks > 0 && cLocks < PGM_PAGE_MAX_LOCKS))
|
---|
1414 | {
|
---|
1415 | if (cLocks == 1)
|
---|
1416 | {
|
---|
1417 | Assert(pVM->pgm.s.cReadLockedPages > 0);
|
---|
1418 | pVM->pgm.s.cReadLockedPages--;
|
---|
1419 | }
|
---|
1420 | PGM_PAGE_DEC_READ_LOCKS(pPage);
|
---|
1421 | }
|
---|
1422 | }
|
---|
1423 |
|
---|
1424 | if (pMap)
|
---|
1425 | {
|
---|
1426 | Assert(pMap->cRefs >= 1);
|
---|
1427 | pMap->cRefs--;
|
---|
1428 | pMap->iAge = 0;
|
---|
1429 | }
|
---|
1430 | pgmUnlock(pVM);
|
---|
1431 | #endif /* IN_RING3 */
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 |
|
---|
1435 | /**
|
---|
1436 | * Converts a GC physical address to a HC ring-3 pointer.
|
---|
1437 | *
|
---|
1438 | * @returns VINF_SUCCESS on success.
|
---|
1439 | * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
|
---|
1440 | * page but has no physical backing.
|
---|
1441 | * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
|
---|
1442 | * GC physical address.
|
---|
1443 | * @returns VERR_PGM_GCPHYS_RANGE_CROSSES_BOUNDARY if the range crosses
|
---|
1444 | * a dynamic ram chunk boundary
|
---|
1445 | *
|
---|
1446 | * @param pVM The VM handle.
|
---|
1447 | * @param GCPhys The GC physical address to convert.
|
---|
1448 | * @param cbRange Physical range
|
---|
1449 | * @param pR3Ptr Where to store the R3 pointer on success.
|
---|
1450 | *
|
---|
1451 | * @deprecated Avoid when possible!
|
---|
1452 | */
|
---|
1453 | VMMDECL(int) PGMPhysGCPhys2R3Ptr(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange, PRTR3PTR pR3Ptr)
|
---|
1454 | {
|
---|
1455 | /** @todo this is kind of hacky and needs some more work. */
|
---|
1456 | #ifndef DEBUG_sandervl
|
---|
1457 | VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
|
---|
1458 | #endif
|
---|
1459 |
|
---|
1460 | Log(("PGMPhysGCPhys2R3Ptr(,%RGp,%#x,): dont use this API!\n", GCPhys, cbRange)); /** @todo eliminate this API! */
|
---|
1461 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1462 | AssertFailedReturn(VERR_NOT_IMPLEMENTED);
|
---|
1463 | #else
|
---|
1464 | pgmLock(pVM);
|
---|
1465 |
|
---|
1466 | PPGMRAMRANGE pRam;
|
---|
1467 | PPGMPAGE pPage;
|
---|
1468 | int rc = pgmPhysGetPageAndRangeEx(&pVM->pgm.s, GCPhys, &pPage, &pRam);
|
---|
1469 | if (RT_SUCCESS(rc))
|
---|
1470 | rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, (void **)pR3Ptr);
|
---|
1471 |
|
---|
1472 | pgmUnlock(pVM);
|
---|
1473 | Assert(rc <= VINF_SUCCESS);
|
---|
1474 | return rc;
|
---|
1475 | #endif
|
---|
1476 | }
|
---|
1477 |
|
---|
1478 |
|
---|
1479 | #ifdef VBOX_STRICT
|
---|
1480 | /**
|
---|
1481 | * PGMPhysGCPhys2R3Ptr convenience for use with assertions.
|
---|
1482 | *
|
---|
1483 | * @returns The R3Ptr, NIL_RTR3PTR on failure.
|
---|
1484 | * @param pVM The VM handle.
|
---|
1485 | * @param GCPhys The GC Physical addresss.
|
---|
1486 | * @param cbRange Physical range.
|
---|
1487 | *
|
---|
1488 | * @deprecated Avoid when possible.
|
---|
1489 | */
|
---|
1490 | VMMDECL(RTR3PTR) PGMPhysGCPhys2R3PtrAssert(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange)
|
---|
1491 | {
|
---|
1492 | RTR3PTR R3Ptr;
|
---|
1493 | int rc = PGMPhysGCPhys2R3Ptr(pVM, GCPhys, cbRange, &R3Ptr);
|
---|
1494 | if (RT_SUCCESS(rc))
|
---|
1495 | return R3Ptr;
|
---|
1496 | return NIL_RTR3PTR;
|
---|
1497 | }
|
---|
1498 | #endif /* VBOX_STRICT */
|
---|
1499 |
|
---|
1500 |
|
---|
1501 | /**
|
---|
1502 | * Converts a guest pointer to a GC physical address.
|
---|
1503 | *
|
---|
1504 | * This uses the current CR3/CR0/CR4 of the guest.
|
---|
1505 | *
|
---|
1506 | * @returns VBox status code.
|
---|
1507 | * @param pVCpu The VMCPU Handle
|
---|
1508 | * @param GCPtr The guest pointer to convert.
|
---|
1509 | * @param pGCPhys Where to store the GC physical address.
|
---|
1510 | */
|
---|
1511 | VMMDECL(int) PGMPhysGCPtr2GCPhys(PVMCPU pVCpu, RTGCPTR GCPtr, PRTGCPHYS pGCPhys)
|
---|
1512 | {
|
---|
1513 | int rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtr, NULL, pGCPhys);
|
---|
1514 | if (pGCPhys && RT_SUCCESS(rc))
|
---|
1515 | *pGCPhys |= (RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK;
|
---|
1516 | return rc;
|
---|
1517 | }
|
---|
1518 |
|
---|
1519 |
|
---|
1520 | /**
|
---|
1521 | * Converts a guest pointer to a HC physical address.
|
---|
1522 | *
|
---|
1523 | * This uses the current CR3/CR0/CR4 of the guest.
|
---|
1524 | *
|
---|
1525 | * @returns VBox status code.
|
---|
1526 | * @param pVCpu The VMCPU Handle
|
---|
1527 | * @param GCPtr The guest pointer to convert.
|
---|
1528 | * @param pHCPhys Where to store the HC physical address.
|
---|
1529 | */
|
---|
1530 | VMMDECL(int) PGMPhysGCPtr2HCPhys(PVMCPU pVCpu, RTGCPTR GCPtr, PRTHCPHYS pHCPhys)
|
---|
1531 | {
|
---|
1532 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
1533 | RTGCPHYS GCPhys;
|
---|
1534 | int rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtr, NULL, &GCPhys);
|
---|
1535 | if (RT_SUCCESS(rc))
|
---|
1536 | rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), pHCPhys);
|
---|
1537 | return rc;
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 |
|
---|
1541 | /**
|
---|
1542 | * Converts a guest pointer to a R3 pointer.
|
---|
1543 | *
|
---|
1544 | * This uses the current CR3/CR0/CR4 of the guest.
|
---|
1545 | *
|
---|
1546 | * @returns VBox status code.
|
---|
1547 | * @param pVCpu The VMCPU Handle
|
---|
1548 | * @param GCPtr The guest pointer to convert.
|
---|
1549 | * @param pR3Ptr Where to store the R3 virtual address.
|
---|
1550 | *
|
---|
1551 | * @deprecated Don't use this.
|
---|
1552 | */
|
---|
1553 | VMMDECL(int) PGMPhysGCPtr2R3Ptr(PVMCPU pVCpu, RTGCPTR GCPtr, PRTR3PTR pR3Ptr)
|
---|
1554 | {
|
---|
1555 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
1556 | VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
|
---|
1557 | RTGCPHYS GCPhys;
|
---|
1558 | int rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtr, NULL, &GCPhys);
|
---|
1559 | if (RT_SUCCESS(rc))
|
---|
1560 | rc = PGMPhysGCPhys2R3Ptr(pVM, GCPhys | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), 1 /* we always stay within one page */, pR3Ptr);
|
---|
1561 | return rc;
|
---|
1562 | }
|
---|
1563 |
|
---|
1564 |
|
---|
1565 |
|
---|
1566 | #undef LOG_GROUP
|
---|
1567 | #define LOG_GROUP LOG_GROUP_PGM_PHYS_ACCESS
|
---|
1568 |
|
---|
1569 |
|
---|
1570 | #ifdef IN_RING3
|
---|
1571 | /**
|
---|
1572 | * Cache PGMPhys memory access
|
---|
1573 | *
|
---|
1574 | * @param pVM VM Handle.
|
---|
1575 | * @param pCache Cache structure pointer
|
---|
1576 | * @param GCPhys GC physical address
|
---|
1577 | * @param pbHC HC pointer corresponding to physical page
|
---|
1578 | *
|
---|
1579 | * @thread EMT.
|
---|
1580 | */
|
---|
1581 | static void pgmPhysCacheAdd(PVM pVM, PGMPHYSCACHE *pCache, RTGCPHYS GCPhys, uint8_t *pbR3)
|
---|
1582 | {
|
---|
1583 | uint32_t iCacheIndex;
|
---|
1584 |
|
---|
1585 | Assert(VM_IS_EMT(pVM));
|
---|
1586 |
|
---|
1587 | GCPhys = PHYS_PAGE_ADDRESS(GCPhys);
|
---|
1588 | pbR3 = (uint8_t *)PAGE_ADDRESS(pbR3);
|
---|
1589 |
|
---|
1590 | iCacheIndex = ((GCPhys >> PAGE_SHIFT) & PGM_MAX_PHYSCACHE_ENTRIES_MASK);
|
---|
1591 |
|
---|
1592 | ASMBitSet(&pCache->aEntries, iCacheIndex);
|
---|
1593 |
|
---|
1594 | pCache->Entry[iCacheIndex].GCPhys = GCPhys;
|
---|
1595 | pCache->Entry[iCacheIndex].pbR3 = pbR3;
|
---|
1596 | }
|
---|
1597 | #endif /* IN_RING3 */
|
---|
1598 |
|
---|
1599 |
|
---|
1600 | /**
|
---|
1601 | * Deals with reading from a page with one or more ALL access handlers.
|
---|
1602 | *
|
---|
1603 | * @returns VBox status code. Can be ignored in ring-3.
|
---|
1604 | * @retval VINF_SUCCESS.
|
---|
1605 | * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
|
---|
1606 | *
|
---|
1607 | * @param pVM The VM handle.
|
---|
1608 | * @param pPage The page descriptor.
|
---|
1609 | * @param GCPhys The physical address to start reading at.
|
---|
1610 | * @param pvBuf Where to put the bits we read.
|
---|
1611 | * @param cb How much to read - less or equal to a page.
|
---|
1612 | */
|
---|
1613 | static int pgmPhysReadHandler(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void *pvBuf, size_t cb)
|
---|
1614 | {
|
---|
1615 | /*
|
---|
1616 | * The most frequent access here is MMIO and shadowed ROM.
|
---|
1617 | * The current code ASSUMES all these access handlers covers full pages!
|
---|
1618 | */
|
---|
1619 |
|
---|
1620 | /*
|
---|
1621 | * Whatever we do we need the source page, map it first.
|
---|
1622 | */
|
---|
1623 | const void *pvSrc = NULL;
|
---|
1624 | int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, GCPhys, &pvSrc);
|
---|
1625 | if (RT_FAILURE(rc))
|
---|
1626 | {
|
---|
1627 | AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
1628 | GCPhys, pPage, rc));
|
---|
1629 | memset(pvBuf, 0xff, cb);
|
---|
1630 | return VINF_SUCCESS;
|
---|
1631 | }
|
---|
1632 | rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
1633 |
|
---|
1634 | /*
|
---|
1635 | * Deal with any physical handlers.
|
---|
1636 | */
|
---|
1637 | PPGMPHYSHANDLER pPhys = NULL;
|
---|
1638 | if (PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) == PGM_PAGE_HNDL_PHYS_STATE_ALL)
|
---|
1639 | {
|
---|
1640 | #ifdef IN_RING3
|
---|
1641 | pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
1642 | AssertReleaseMsg(pPhys, ("GCPhys=%RGp cb=%#x\n", GCPhys, cb));
|
---|
1643 | Assert(GCPhys >= pPhys->Core.Key && GCPhys <= pPhys->Core.KeyLast);
|
---|
1644 | Assert((pPhys->Core.Key & PAGE_OFFSET_MASK) == 0);
|
---|
1645 | Assert((pPhys->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
|
---|
1646 | Assert(pPhys->CTX_SUFF(pfnHandler));
|
---|
1647 |
|
---|
1648 | PFNPGMR3PHYSHANDLER pfnHandler = pPhys->CTX_SUFF(pfnHandler);
|
---|
1649 | void *pvUser = pPhys->CTX_SUFF(pvUser);
|
---|
1650 |
|
---|
1651 | Log5(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cb, pPage, R3STRING(pPhys->pszDesc) ));
|
---|
1652 | STAM_PROFILE_START(&pPhys->Stat, h);
|
---|
1653 | Assert(PGMIsLockOwner(pVM));
|
---|
1654 | /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
|
---|
1655 | pgmUnlock(pVM);
|
---|
1656 | rc = pfnHandler(pVM, GCPhys, (void *)pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, pvUser);
|
---|
1657 | pgmLock(pVM);
|
---|
1658 | # ifdef VBOX_WITH_STATISTICS
|
---|
1659 | pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
1660 | if (pPhys)
|
---|
1661 | STAM_PROFILE_STOP(&pPhys->Stat, h);
|
---|
1662 | # else
|
---|
1663 | pPhys = NULL; /* might not be valid anymore. */
|
---|
1664 | # endif
|
---|
1665 | AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp\n", rc, GCPhys));
|
---|
1666 | #else
|
---|
1667 | /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
|
---|
1668 | //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cb=%#x\n", GCPhys, cb));
|
---|
1669 | return VERR_PGM_PHYS_WR_HIT_HANDLER;
|
---|
1670 | #endif
|
---|
1671 | }
|
---|
1672 |
|
---|
1673 | /*
|
---|
1674 | * Deal with any virtual handlers.
|
---|
1675 | */
|
---|
1676 | if (PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) == PGM_PAGE_HNDL_VIRT_STATE_ALL)
|
---|
1677 | {
|
---|
1678 | unsigned iPage;
|
---|
1679 | PPGMVIRTHANDLER pVirt;
|
---|
1680 |
|
---|
1681 | int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pVirt, &iPage);
|
---|
1682 | AssertReleaseMsg(RT_SUCCESS(rc2), ("GCPhys=%RGp cb=%#x rc2=%Rrc\n", GCPhys, cb, rc2));
|
---|
1683 | Assert((pVirt->Core.Key & PAGE_OFFSET_MASK) == 0);
|
---|
1684 | Assert((pVirt->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
|
---|
1685 | Assert(GCPhys >= pVirt->aPhysToVirt[iPage].Core.Key && GCPhys <= pVirt->aPhysToVirt[iPage].Core.KeyLast);
|
---|
1686 |
|
---|
1687 | #ifdef IN_RING3
|
---|
1688 | if (pVirt->pfnHandlerR3)
|
---|
1689 | {
|
---|
1690 | if (!pPhys)
|
---|
1691 | Log5(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] virt %s\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc) ));
|
---|
1692 | else
|
---|
1693 | Log(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] phys/virt %s/%s\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc), R3STRING(pPhys->pszDesc) ));
|
---|
1694 | RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
|
---|
1695 | + (iPage << PAGE_SHIFT)
|
---|
1696 | + (GCPhys & PAGE_OFFSET_MASK);
|
---|
1697 |
|
---|
1698 | STAM_PROFILE_START(&pVirt->Stat, h);
|
---|
1699 | rc2 = pVirt->CTX_SUFF(pfnHandler)(pVM, GCPtr, (void *)pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, /*pVirt->CTX_SUFF(pvUser)*/ NULL);
|
---|
1700 | STAM_PROFILE_STOP(&pVirt->Stat, h);
|
---|
1701 | if (rc2 == VINF_SUCCESS)
|
---|
1702 | rc = VINF_SUCCESS;
|
---|
1703 | AssertLogRelMsg(rc2 == VINF_SUCCESS || rc2 == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc2, GCPhys, pPage, pVirt->pszDesc));
|
---|
1704 | }
|
---|
1705 | else
|
---|
1706 | Log5(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] virt %s [no handler]\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc) ));
|
---|
1707 | #else
|
---|
1708 | /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
|
---|
1709 | //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cb=%#x\n", GCPhys, cb));
|
---|
1710 | return VERR_PGM_PHYS_WR_HIT_HANDLER;
|
---|
1711 | #endif
|
---|
1712 | }
|
---|
1713 |
|
---|
1714 | /*
|
---|
1715 | * Take the default action.
|
---|
1716 | */
|
---|
1717 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
1718 | memcpy(pvBuf, pvSrc, cb);
|
---|
1719 | return rc;
|
---|
1720 | }
|
---|
1721 |
|
---|
1722 |
|
---|
1723 | /**
|
---|
1724 | * Read physical memory.
|
---|
1725 | *
|
---|
1726 | * This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
|
---|
1727 | * want to ignore those.
|
---|
1728 | *
|
---|
1729 | * @returns VBox status code. Can be ignored in ring-3.
|
---|
1730 | * @retval VINF_SUCCESS.
|
---|
1731 | * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
|
---|
1732 | *
|
---|
1733 | * @param pVM VM Handle.
|
---|
1734 | * @param GCPhys Physical address start reading from.
|
---|
1735 | * @param pvBuf Where to put the read bits.
|
---|
1736 | * @param cbRead How many bytes to read.
|
---|
1737 | */
|
---|
1738 | VMMDECL(int) PGMPhysRead(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
|
---|
1739 | {
|
---|
1740 | AssertMsgReturn(cbRead > 0, ("don't even think about reading zero bytes!\n"), VINF_SUCCESS);
|
---|
1741 | LogFlow(("PGMPhysRead: %RGp %d\n", GCPhys, cbRead));
|
---|
1742 |
|
---|
1743 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,PhysRead));
|
---|
1744 | STAM_COUNTER_ADD(&pVM->pgm.s.CTX_MID_Z(Stat,PhysReadBytes), cbRead);
|
---|
1745 |
|
---|
1746 | pgmLock(pVM);
|
---|
1747 |
|
---|
1748 | /*
|
---|
1749 | * Copy loop on ram ranges.
|
---|
1750 | */
|
---|
1751 | PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
1752 | for (;;)
|
---|
1753 | {
|
---|
1754 | /* Find range. */
|
---|
1755 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
1756 | pRam = pRam->CTX_SUFF(pNext);
|
---|
1757 | /* Inside range or not? */
|
---|
1758 | if (pRam && GCPhys >= pRam->GCPhys)
|
---|
1759 | {
|
---|
1760 | /*
|
---|
1761 | * Must work our way thru this page by page.
|
---|
1762 | */
|
---|
1763 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
1764 | while (off < pRam->cb)
|
---|
1765 | {
|
---|
1766 | unsigned iPage = off >> PAGE_SHIFT;
|
---|
1767 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
1768 | size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1769 | if (cb > cbRead)
|
---|
1770 | cb = cbRead;
|
---|
1771 |
|
---|
1772 | /*
|
---|
1773 | * Any ALL access handlers?
|
---|
1774 | */
|
---|
1775 | if (RT_UNLIKELY(PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage)))
|
---|
1776 | {
|
---|
1777 | int rc = pgmPhysReadHandler(pVM, pPage, pRam->GCPhys + off, pvBuf, cb);
|
---|
1778 | if (RT_FAILURE(rc))
|
---|
1779 | {
|
---|
1780 | pgmUnlock(pVM);
|
---|
1781 | return rc;
|
---|
1782 | }
|
---|
1783 | }
|
---|
1784 | else
|
---|
1785 | {
|
---|
1786 | /*
|
---|
1787 | * Get the pointer to the page.
|
---|
1788 | */
|
---|
1789 | const void *pvSrc;
|
---|
1790 | int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, pRam->GCPhys + off, &pvSrc);
|
---|
1791 | if (RT_SUCCESS(rc))
|
---|
1792 | memcpy(pvBuf, pvSrc, cb);
|
---|
1793 | else
|
---|
1794 | {
|
---|
1795 | AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
1796 | pRam->GCPhys + off, pPage, rc));
|
---|
1797 | memset(pvBuf, 0xff, cb);
|
---|
1798 | }
|
---|
1799 | }
|
---|
1800 |
|
---|
1801 | /* next page */
|
---|
1802 | if (cb >= cbRead)
|
---|
1803 | {
|
---|
1804 | pgmUnlock(pVM);
|
---|
1805 | return VINF_SUCCESS;
|
---|
1806 | }
|
---|
1807 | cbRead -= cb;
|
---|
1808 | off += cb;
|
---|
1809 | pvBuf = (char *)pvBuf + cb;
|
---|
1810 | } /* walk pages in ram range. */
|
---|
1811 |
|
---|
1812 | GCPhys = pRam->GCPhysLast + 1;
|
---|
1813 | }
|
---|
1814 | else
|
---|
1815 | {
|
---|
1816 | LogFlow(("PGMPhysRead: Unassigned %RGp size=%u\n", GCPhys, cbRead));
|
---|
1817 |
|
---|
1818 | /*
|
---|
1819 | * Unassigned address space.
|
---|
1820 | */
|
---|
1821 | if (!pRam)
|
---|
1822 | break;
|
---|
1823 | size_t cb = pRam->GCPhys - GCPhys;
|
---|
1824 | if (cb >= cbRead)
|
---|
1825 | {
|
---|
1826 | memset(pvBuf, 0xff, cbRead);
|
---|
1827 | break;
|
---|
1828 | }
|
---|
1829 | memset(pvBuf, 0xff, cb);
|
---|
1830 |
|
---|
1831 | cbRead -= cb;
|
---|
1832 | pvBuf = (char *)pvBuf + cb;
|
---|
1833 | GCPhys += cb;
|
---|
1834 | }
|
---|
1835 | } /* Ram range walk */
|
---|
1836 |
|
---|
1837 | pgmUnlock(pVM);
|
---|
1838 | return VINF_SUCCESS;
|
---|
1839 | }
|
---|
1840 |
|
---|
1841 |
|
---|
1842 | /**
|
---|
1843 | * Deals with writing to a page with one or more WRITE or ALL access handlers.
|
---|
1844 | *
|
---|
1845 | * @returns VBox status code. Can be ignored in ring-3.
|
---|
1846 | * @retval VINF_SUCCESS.
|
---|
1847 | * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
|
---|
1848 | *
|
---|
1849 | * @param pVM The VM handle.
|
---|
1850 | * @param pPage The page descriptor.
|
---|
1851 | * @param GCPhys The physical address to start writing at.
|
---|
1852 | * @param pvBuf What to write.
|
---|
1853 | * @param cbWrite How much to write - less or equal to a page.
|
---|
1854 | */
|
---|
1855 | static int pgmPhysWriteHandler(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void const *pvBuf, size_t cbWrite)
|
---|
1856 | {
|
---|
1857 | void *pvDst = NULL;
|
---|
1858 | int rc;
|
---|
1859 |
|
---|
1860 | /*
|
---|
1861 | * Give priority to physical handlers (like #PF does).
|
---|
1862 | *
|
---|
1863 | * Hope for a lonely physical handler first that covers the whole
|
---|
1864 | * write area. This should be a pretty frequent case with MMIO and
|
---|
1865 | * the heavy usage of full page handlers in the page pool.
|
---|
1866 | */
|
---|
1867 | if ( !PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage)
|
---|
1868 | || PGM_PAGE_IS_MMIO(pPage) /* screw virtual handlers on MMIO pages */)
|
---|
1869 | {
|
---|
1870 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
1871 | if (pCur)
|
---|
1872 | {
|
---|
1873 | Assert(GCPhys >= pCur->Core.Key && GCPhys <= pCur->Core.KeyLast);
|
---|
1874 | Assert(pCur->CTX_SUFF(pfnHandler));
|
---|
1875 |
|
---|
1876 | size_t cbRange = pCur->Core.KeyLast - GCPhys + 1;
|
---|
1877 | if (cbRange > cbWrite)
|
---|
1878 | cbRange = cbWrite;
|
---|
1879 |
|
---|
1880 | #ifndef IN_RING3
|
---|
1881 | /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
|
---|
1882 | NOREF(cbRange);
|
---|
1883 | //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
|
---|
1884 | return VERR_PGM_PHYS_WR_HIT_HANDLER;
|
---|
1885 |
|
---|
1886 | #else /* IN_RING3 */
|
---|
1887 | Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pCur->pszDesc) ));
|
---|
1888 | if (!PGM_PAGE_IS_MMIO(pPage))
|
---|
1889 | rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst);
|
---|
1890 | else
|
---|
1891 | rc = VINF_SUCCESS;
|
---|
1892 | if (RT_SUCCESS(rc))
|
---|
1893 | {
|
---|
1894 | PFNPGMR3PHYSHANDLER pfnHandler = pCur->CTX_SUFF(pfnHandler);
|
---|
1895 | void *pvUser = pCur->CTX_SUFF(pvUser);
|
---|
1896 |
|
---|
1897 | STAM_PROFILE_START(&pCur->Stat, h);
|
---|
1898 | Assert(PGMIsLockOwner(pVM));
|
---|
1899 | /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
|
---|
1900 | pgmUnlock(pVM);
|
---|
1901 | rc = pfnHandler(pVM, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, pvUser);
|
---|
1902 | pgmLock(pVM);
|
---|
1903 | # ifdef VBOX_WITH_STATISTICS
|
---|
1904 | pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
1905 | if (pCur)
|
---|
1906 | STAM_PROFILE_STOP(&pCur->Stat, h);
|
---|
1907 | # else
|
---|
1908 | pCur = NULL; /* might not be valid anymore. */
|
---|
1909 | # endif
|
---|
1910 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
1911 | memcpy(pvDst, pvBuf, cbRange);
|
---|
1912 | else
|
---|
1913 | AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, (pCur) ? pCur->pszDesc : ""));
|
---|
1914 | }
|
---|
1915 | else
|
---|
1916 | AssertLogRelMsgFailedReturn(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
1917 | GCPhys, pPage, rc), rc);
|
---|
1918 | if (RT_LIKELY(cbRange == cbWrite))
|
---|
1919 | return VINF_SUCCESS;
|
---|
1920 |
|
---|
1921 | /* more fun to be had below */
|
---|
1922 | cbWrite -= cbRange;
|
---|
1923 | GCPhys += cbRange;
|
---|
1924 | pvBuf = (uint8_t *)pvBuf + cbRange;
|
---|
1925 | pvDst = (uint8_t *)pvDst + cbRange;
|
---|
1926 | #endif /* IN_RING3 */
|
---|
1927 | }
|
---|
1928 | /* else: the handler is somewhere else in the page, deal with it below. */
|
---|
1929 | Assert(!PGM_PAGE_IS_MMIO(pPage)); /* MMIO handlers are all PAGE_SIZEed! */
|
---|
1930 | }
|
---|
1931 | /*
|
---|
1932 | * A virtual handler without any interfering physical handlers.
|
---|
1933 | * Hopefully it'll conver the whole write.
|
---|
1934 | */
|
---|
1935 | else if (!PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage))
|
---|
1936 | {
|
---|
1937 | unsigned iPage;
|
---|
1938 | PPGMVIRTHANDLER pCur;
|
---|
1939 | rc = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pCur, &iPage);
|
---|
1940 | if (RT_SUCCESS(rc))
|
---|
1941 | {
|
---|
1942 | size_t cbRange = (PAGE_OFFSET_MASK & pCur->Core.KeyLast) - (PAGE_OFFSET_MASK & GCPhys) + 1;
|
---|
1943 | if (cbRange > cbWrite)
|
---|
1944 | cbRange = cbWrite;
|
---|
1945 |
|
---|
1946 | #ifndef IN_RING3
|
---|
1947 | /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
|
---|
1948 | NOREF(cbRange);
|
---|
1949 | //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
|
---|
1950 | return VERR_PGM_PHYS_WR_HIT_HANDLER;
|
---|
1951 |
|
---|
1952 | #else /* IN_RING3 */
|
---|
1953 |
|
---|
1954 | Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] virt %s\n", GCPhys, cbRange, pPage, R3STRING(pCur->pszDesc) ));
|
---|
1955 | rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst);
|
---|
1956 | if (RT_SUCCESS(rc))
|
---|
1957 | {
|
---|
1958 | rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
1959 | if (pCur->pfnHandlerR3)
|
---|
1960 | {
|
---|
1961 | RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pCur->Core.Key & PAGE_BASE_GC_MASK)
|
---|
1962 | + (iPage << PAGE_SHIFT)
|
---|
1963 | + (GCPhys & PAGE_OFFSET_MASK);
|
---|
1964 |
|
---|
1965 | STAM_PROFILE_START(&pCur->Stat, h);
|
---|
1966 | rc = pCur->CTX_SUFF(pfnHandler)(pVM, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, /*pCur->CTX_SUFF(pvUser)*/ NULL);
|
---|
1967 | STAM_PROFILE_STOP(&pCur->Stat, h);
|
---|
1968 | }
|
---|
1969 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
1970 | memcpy(pvDst, pvBuf, cbRange);
|
---|
1971 | else
|
---|
1972 | AssertLogRelMsg(rc == VINF_SUCCESS, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pCur->pszDesc));
|
---|
1973 | }
|
---|
1974 | else
|
---|
1975 | AssertLogRelMsgFailedReturn(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
1976 | GCPhys, pPage, rc), rc);
|
---|
1977 | if (RT_LIKELY(cbRange == cbWrite))
|
---|
1978 | return VINF_SUCCESS;
|
---|
1979 |
|
---|
1980 | /* more fun to be had below */
|
---|
1981 | cbWrite -= cbRange;
|
---|
1982 | GCPhys += cbRange;
|
---|
1983 | pvBuf = (uint8_t *)pvBuf + cbRange;
|
---|
1984 | pvDst = (uint8_t *)pvDst + cbRange;
|
---|
1985 | #endif
|
---|
1986 | }
|
---|
1987 | /* else: the handler is somewhere else in the page, deal with it below. */
|
---|
1988 | }
|
---|
1989 |
|
---|
1990 | /*
|
---|
1991 | * Deal with all the odd ends.
|
---|
1992 | */
|
---|
1993 |
|
---|
1994 | /* We need a writable destination page. */
|
---|
1995 | if (!pvDst)
|
---|
1996 | {
|
---|
1997 | rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst);
|
---|
1998 | AssertLogRelMsgReturn(RT_SUCCESS(rc),
|
---|
1999 | ("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
2000 | GCPhys, pPage, rc), rc);
|
---|
2001 | }
|
---|
2002 |
|
---|
2003 | /* The loop state (big + ugly). */
|
---|
2004 | unsigned iVirtPage = 0;
|
---|
2005 | PPGMVIRTHANDLER pVirt = NULL;
|
---|
2006 | uint32_t offVirt = PAGE_SIZE;
|
---|
2007 | uint32_t offVirtLast = PAGE_SIZE;
|
---|
2008 | bool fMoreVirt = PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage);
|
---|
2009 |
|
---|
2010 | PPGMPHYSHANDLER pPhys = NULL;
|
---|
2011 | uint32_t offPhys = PAGE_SIZE;
|
---|
2012 | uint32_t offPhysLast = PAGE_SIZE;
|
---|
2013 | bool fMorePhys = PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage);
|
---|
2014 |
|
---|
2015 | /* The loop. */
|
---|
2016 | for (;;)
|
---|
2017 | {
|
---|
2018 | /*
|
---|
2019 | * Find the closest handler at or above GCPhys.
|
---|
2020 | */
|
---|
2021 | if (fMoreVirt && !pVirt)
|
---|
2022 | {
|
---|
2023 | rc = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pVirt, &iVirtPage);
|
---|
2024 | if (RT_SUCCESS(rc))
|
---|
2025 | {
|
---|
2026 | offVirt = 0;
|
---|
2027 | offVirtLast = (pVirt->aPhysToVirt[iVirtPage].Core.KeyLast & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
|
---|
2028 | }
|
---|
2029 | else
|
---|
2030 | {
|
---|
2031 | PPGMPHYS2VIRTHANDLER pVirtPhys;
|
---|
2032 | pVirtPhys = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers,
|
---|
2033 | GCPhys, true /* fAbove */);
|
---|
2034 | if ( pVirtPhys
|
---|
2035 | && (pVirtPhys->Core.Key >> PAGE_SHIFT) == (GCPhys >> PAGE_SHIFT))
|
---|
2036 | {
|
---|
2037 | /* ASSUME that pVirtPhys only covers one page. */
|
---|
2038 | Assert((pVirtPhys->Core.Key >> PAGE_SHIFT) == (pVirtPhys->Core.KeyLast >> PAGE_SHIFT));
|
---|
2039 | Assert(pVirtPhys->Core.Key > GCPhys);
|
---|
2040 |
|
---|
2041 | pVirt = (PPGMVIRTHANDLER)((uintptr_t)pVirtPhys + pVirtPhys->offVirtHandler);
|
---|
2042 | iVirtPage = pVirtPhys - &pVirt->aPhysToVirt[0]; Assert(iVirtPage == 0);
|
---|
2043 | offVirt = (pVirtPhys->Core.Key & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
|
---|
2044 | offVirtLast = (pVirtPhys->Core.KeyLast & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
|
---|
2045 | }
|
---|
2046 | else
|
---|
2047 | {
|
---|
2048 | pVirt = NULL;
|
---|
2049 | fMoreVirt = false;
|
---|
2050 | offVirt = offVirtLast = PAGE_SIZE;
|
---|
2051 | }
|
---|
2052 | }
|
---|
2053 | }
|
---|
2054 |
|
---|
2055 | if (fMorePhys && !pPhys)
|
---|
2056 | {
|
---|
2057 | pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
2058 | if (pPhys)
|
---|
2059 | {
|
---|
2060 | offPhys = 0;
|
---|
2061 | offPhysLast = pPhys->Core.KeyLast - GCPhys; /* ASSUMES < 4GB handlers... */
|
---|
2062 | }
|
---|
2063 | else
|
---|
2064 | {
|
---|
2065 | pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers,
|
---|
2066 | GCPhys, true /* fAbove */);
|
---|
2067 | if ( pPhys
|
---|
2068 | && pPhys->Core.Key <= GCPhys + (cbWrite - 1))
|
---|
2069 | {
|
---|
2070 | offPhys = pPhys->Core.Key - GCPhys;
|
---|
2071 | offPhysLast = pPhys->Core.KeyLast - GCPhys; /* ASSUMES < 4GB handlers... */
|
---|
2072 | }
|
---|
2073 | else
|
---|
2074 | {
|
---|
2075 | pPhys = NULL;
|
---|
2076 | fMorePhys = false;
|
---|
2077 | offPhys = offPhysLast = PAGE_SIZE;
|
---|
2078 | }
|
---|
2079 | }
|
---|
2080 | }
|
---|
2081 |
|
---|
2082 | /*
|
---|
2083 | * Handle access to space without handlers (that's easy).
|
---|
2084 | */
|
---|
2085 | rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
2086 | uint32_t cbRange = (uint32_t)cbWrite;
|
---|
2087 | if (offPhys && offVirt)
|
---|
2088 | {
|
---|
2089 | if (cbRange > offPhys)
|
---|
2090 | cbRange = offPhys;
|
---|
2091 | if (cbRange > offVirt)
|
---|
2092 | cbRange = offVirt;
|
---|
2093 | Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] miss\n", GCPhys, cbRange, pPage));
|
---|
2094 | }
|
---|
2095 | /*
|
---|
2096 | * Physical handler.
|
---|
2097 | */
|
---|
2098 | else if (!offPhys && offVirt)
|
---|
2099 | {
|
---|
2100 | if (cbRange > offPhysLast + 1)
|
---|
2101 | cbRange = offPhysLast + 1;
|
---|
2102 | if (cbRange > offVirt)
|
---|
2103 | cbRange = offVirt;
|
---|
2104 | #ifdef IN_RING3
|
---|
2105 | PFNPGMR3PHYSHANDLER pfnHandler = pPhys->CTX_SUFF(pfnHandler);
|
---|
2106 | void *pvUser = pPhys->CTX_SUFF(pvUser);
|
---|
2107 |
|
---|
2108 | Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pPhys->pszDesc) ));
|
---|
2109 | STAM_PROFILE_START(&pPhys->Stat, h);
|
---|
2110 | Assert(PGMIsLockOwner(pVM));
|
---|
2111 | /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
|
---|
2112 | pgmUnlock(pVM);
|
---|
2113 | rc = pfnHandler(pVM, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, pvUser);
|
---|
2114 | pgmLock(pVM);
|
---|
2115 | # ifdef VBOX_WITH_STATISTICS
|
---|
2116 | pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
2117 | if (pPhys)
|
---|
2118 | STAM_PROFILE_STOP(&pPhys->Stat, h);
|
---|
2119 | # else
|
---|
2120 | pPhys = NULL; /* might not be valid anymore. */
|
---|
2121 | # endif
|
---|
2122 | AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, (pPhys) ? pPhys->pszDesc : ""));
|
---|
2123 | #else
|
---|
2124 | /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
|
---|
2125 | NOREF(cbRange);
|
---|
2126 | //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
|
---|
2127 | return VERR_PGM_PHYS_WR_HIT_HANDLER;
|
---|
2128 | #endif
|
---|
2129 | }
|
---|
2130 | /*
|
---|
2131 | * Virtual handler.
|
---|
2132 | */
|
---|
2133 | else if (offPhys && !offVirt)
|
---|
2134 | {
|
---|
2135 | if (cbRange > offVirtLast + 1)
|
---|
2136 | cbRange = offVirtLast + 1;
|
---|
2137 | if (cbRange > offPhys)
|
---|
2138 | cbRange = offPhys;
|
---|
2139 | #ifdef IN_RING3
|
---|
2140 | Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pVirt->pszDesc) ));
|
---|
2141 | if (pVirt->pfnHandlerR3)
|
---|
2142 | {
|
---|
2143 | RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
|
---|
2144 | + (iVirtPage << PAGE_SHIFT)
|
---|
2145 | + (GCPhys & PAGE_OFFSET_MASK);
|
---|
2146 | STAM_PROFILE_START(&pVirt->Stat, h);
|
---|
2147 | rc = pVirt->CTX_SUFF(pfnHandler)(pVM, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, /*pCur->CTX_SUFF(pvUser)*/ NULL);
|
---|
2148 | STAM_PROFILE_STOP(&pVirt->Stat, h);
|
---|
2149 | AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pVirt->pszDesc));
|
---|
2150 | }
|
---|
2151 | pVirt = NULL;
|
---|
2152 | #else
|
---|
2153 | /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
|
---|
2154 | NOREF(cbRange);
|
---|
2155 | //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
|
---|
2156 | return VERR_PGM_PHYS_WR_HIT_HANDLER;
|
---|
2157 | #endif
|
---|
2158 | }
|
---|
2159 | /*
|
---|
2160 | * Both... give the physical one priority.
|
---|
2161 | */
|
---|
2162 | else
|
---|
2163 | {
|
---|
2164 | Assert(!offPhys && !offVirt);
|
---|
2165 | if (cbRange > offVirtLast + 1)
|
---|
2166 | cbRange = offVirtLast + 1;
|
---|
2167 | if (cbRange > offPhysLast + 1)
|
---|
2168 | cbRange = offPhysLast + 1;
|
---|
2169 |
|
---|
2170 | #ifdef IN_RING3
|
---|
2171 | if (pVirt->pfnHandlerR3)
|
---|
2172 | Log(("pgmPhysWriteHandler: overlapping phys and virt handlers at %RGp %R[pgmpage]; cbRange=%#x\n", GCPhys, pPage, cbRange));
|
---|
2173 | Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys/virt %s/%s\n", GCPhys, cbRange, pPage, R3STRING(pPhys->pszDesc), R3STRING(pVirt->pszDesc) ));
|
---|
2174 |
|
---|
2175 | PFNPGMR3PHYSHANDLER pfnHandler = pPhys->CTX_SUFF(pfnHandler);
|
---|
2176 | void *pvUser = pPhys->CTX_SUFF(pvUser);
|
---|
2177 |
|
---|
2178 | STAM_PROFILE_START(&pPhys->Stat, h);
|
---|
2179 | Assert(PGMIsLockOwner(pVM));
|
---|
2180 | /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
|
---|
2181 | pgmUnlock(pVM);
|
---|
2182 | rc = pfnHandler(pVM, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, pvUser);
|
---|
2183 | pgmLock(pVM);
|
---|
2184 | # ifdef VBOX_WITH_STATISTICS
|
---|
2185 | pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
2186 | if (pPhys)
|
---|
2187 | STAM_PROFILE_STOP(&pPhys->Stat, h);
|
---|
2188 | # else
|
---|
2189 | pPhys = NULL; /* might not be valid anymore. */
|
---|
2190 | # endif
|
---|
2191 | AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, (pPhys) ? pPhys->pszDesc : ""));
|
---|
2192 | if (pVirt->pfnHandlerR3)
|
---|
2193 | {
|
---|
2194 |
|
---|
2195 | RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
|
---|
2196 | + (iVirtPage << PAGE_SHIFT)
|
---|
2197 | + (GCPhys & PAGE_OFFSET_MASK);
|
---|
2198 | STAM_PROFILE_START(&pVirt->Stat, h2);
|
---|
2199 | int rc2 = pVirt->CTX_SUFF(pfnHandler)(pVM, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, /*pCur->CTX_SUFF(pvUser)*/ NULL);
|
---|
2200 | STAM_PROFILE_STOP(&pVirt->Stat, h2);
|
---|
2201 | if (rc2 == VINF_SUCCESS && rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
2202 | rc = VINF_SUCCESS;
|
---|
2203 | else
|
---|
2204 | AssertLogRelMsg(rc2 == VINF_SUCCESS || rc2 == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pVirt->pszDesc));
|
---|
2205 | }
|
---|
2206 | pPhys = NULL;
|
---|
2207 | pVirt = NULL;
|
---|
2208 | #else
|
---|
2209 | /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
|
---|
2210 | NOREF(cbRange);
|
---|
2211 | //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
|
---|
2212 | return VERR_PGM_PHYS_WR_HIT_HANDLER;
|
---|
2213 | #endif
|
---|
2214 | }
|
---|
2215 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
2216 | memcpy(pvDst, pvBuf, cbRange);
|
---|
2217 |
|
---|
2218 | /*
|
---|
2219 | * Advance if we've got more stuff to do.
|
---|
2220 | */
|
---|
2221 | if (cbRange >= cbWrite)
|
---|
2222 | return VINF_SUCCESS;
|
---|
2223 |
|
---|
2224 | cbWrite -= cbRange;
|
---|
2225 | GCPhys += cbRange;
|
---|
2226 | pvBuf = (uint8_t *)pvBuf + cbRange;
|
---|
2227 | pvDst = (uint8_t *)pvDst + cbRange;
|
---|
2228 |
|
---|
2229 | offPhys -= cbRange;
|
---|
2230 | offPhysLast -= cbRange;
|
---|
2231 | offVirt -= cbRange;
|
---|
2232 | offVirtLast -= cbRange;
|
---|
2233 | }
|
---|
2234 | }
|
---|
2235 |
|
---|
2236 |
|
---|
2237 | /**
|
---|
2238 | * Write to physical memory.
|
---|
2239 | *
|
---|
2240 | * This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
|
---|
2241 | * want to ignore those.
|
---|
2242 | *
|
---|
2243 | * @returns VBox status code. Can be ignored in ring-3.
|
---|
2244 | * @retval VINF_SUCCESS.
|
---|
2245 | * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
|
---|
2246 | *
|
---|
2247 | * @param pVM VM Handle.
|
---|
2248 | * @param GCPhys Physical address to write to.
|
---|
2249 | * @param pvBuf What to write.
|
---|
2250 | * @param cbWrite How many bytes to write.
|
---|
2251 | */
|
---|
2252 | VMMDECL(int) PGMPhysWrite(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite)
|
---|
2253 | {
|
---|
2254 | AssertMsg(!pVM->pgm.s.fNoMorePhysWrites, ("Calling PGMPhysWrite after pgmR3Save()!\n"));
|
---|
2255 | AssertMsgReturn(cbWrite > 0, ("don't even think about writing zero bytes!\n"), VINF_SUCCESS);
|
---|
2256 | LogFlow(("PGMPhysWrite: %RGp %d\n", GCPhys, cbWrite));
|
---|
2257 |
|
---|
2258 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,PhysWrite));
|
---|
2259 | STAM_COUNTER_ADD(&pVM->pgm.s.CTX_MID_Z(Stat,PhysWriteBytes), cbWrite);
|
---|
2260 |
|
---|
2261 | pgmLock(pVM);
|
---|
2262 |
|
---|
2263 | /*
|
---|
2264 | * Copy loop on ram ranges.
|
---|
2265 | */
|
---|
2266 | PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
2267 | for (;;)
|
---|
2268 | {
|
---|
2269 | /* Find range. */
|
---|
2270 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
2271 | pRam = pRam->CTX_SUFF(pNext);
|
---|
2272 | /* Inside range or not? */
|
---|
2273 | if (pRam && GCPhys >= pRam->GCPhys)
|
---|
2274 | {
|
---|
2275 | /*
|
---|
2276 | * Must work our way thru this page by page.
|
---|
2277 | */
|
---|
2278 | RTGCPTR off = GCPhys - pRam->GCPhys;
|
---|
2279 | while (off < pRam->cb)
|
---|
2280 | {
|
---|
2281 | RTGCPTR iPage = off >> PAGE_SHIFT;
|
---|
2282 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
2283 | size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
2284 | if (cb > cbWrite)
|
---|
2285 | cb = cbWrite;
|
---|
2286 |
|
---|
2287 | /*
|
---|
2288 | * Any active WRITE or ALL access handlers?
|
---|
2289 | */
|
---|
2290 | if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage))
|
---|
2291 | {
|
---|
2292 | int rc = pgmPhysWriteHandler(pVM, pPage, pRam->GCPhys + off, pvBuf, cb);
|
---|
2293 | if (RT_FAILURE(rc))
|
---|
2294 | {
|
---|
2295 | pgmUnlock(pVM);
|
---|
2296 | return rc;
|
---|
2297 | }
|
---|
2298 | }
|
---|
2299 | else
|
---|
2300 | {
|
---|
2301 | /*
|
---|
2302 | * Get the pointer to the page.
|
---|
2303 | */
|
---|
2304 | void *pvDst;
|
---|
2305 | int rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, pRam->GCPhys + off, &pvDst);
|
---|
2306 | if (RT_SUCCESS(rc))
|
---|
2307 | memcpy(pvDst, pvBuf, cb);
|
---|
2308 | else
|
---|
2309 | AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
2310 | pRam->GCPhys + off, pPage, rc));
|
---|
2311 | }
|
---|
2312 |
|
---|
2313 | /* next page */
|
---|
2314 | if (cb >= cbWrite)
|
---|
2315 | {
|
---|
2316 | pgmUnlock(pVM);
|
---|
2317 | return VINF_SUCCESS;
|
---|
2318 | }
|
---|
2319 |
|
---|
2320 | cbWrite -= cb;
|
---|
2321 | off += cb;
|
---|
2322 | pvBuf = (const char *)pvBuf + cb;
|
---|
2323 | } /* walk pages in ram range */
|
---|
2324 |
|
---|
2325 | GCPhys = pRam->GCPhysLast + 1;
|
---|
2326 | }
|
---|
2327 | else
|
---|
2328 | {
|
---|
2329 | /*
|
---|
2330 | * Unassigned address space, skip it.
|
---|
2331 | */
|
---|
2332 | if (!pRam)
|
---|
2333 | break;
|
---|
2334 | size_t cb = pRam->GCPhys - GCPhys;
|
---|
2335 | if (cb >= cbWrite)
|
---|
2336 | break;
|
---|
2337 | cbWrite -= cb;
|
---|
2338 | pvBuf = (const char *)pvBuf + cb;
|
---|
2339 | GCPhys += cb;
|
---|
2340 | }
|
---|
2341 | } /* Ram range walk */
|
---|
2342 |
|
---|
2343 | pgmUnlock(pVM);
|
---|
2344 | return VINF_SUCCESS;
|
---|
2345 | }
|
---|
2346 |
|
---|
2347 |
|
---|
2348 | /**
|
---|
2349 | * Read from guest physical memory by GC physical address, bypassing
|
---|
2350 | * MMIO and access handlers.
|
---|
2351 | *
|
---|
2352 | * @returns VBox status.
|
---|
2353 | * @param pVM VM handle.
|
---|
2354 | * @param pvDst The destination address.
|
---|
2355 | * @param GCPhysSrc The source address (GC physical address).
|
---|
2356 | * @param cb The number of bytes to read.
|
---|
2357 | */
|
---|
2358 | VMMDECL(int) PGMPhysSimpleReadGCPhys(PVM pVM, void *pvDst, RTGCPHYS GCPhysSrc, size_t cb)
|
---|
2359 | {
|
---|
2360 | /*
|
---|
2361 | * Treat the first page as a special case.
|
---|
2362 | */
|
---|
2363 | if (!cb)
|
---|
2364 | return VINF_SUCCESS;
|
---|
2365 |
|
---|
2366 | /* map the 1st page */
|
---|
2367 | void const *pvSrc;
|
---|
2368 | PGMPAGEMAPLOCK Lock;
|
---|
2369 | int rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhysSrc, &pvSrc, &Lock);
|
---|
2370 | if (RT_FAILURE(rc))
|
---|
2371 | return rc;
|
---|
2372 |
|
---|
2373 | /* optimize for the case where access is completely within the first page. */
|
---|
2374 | size_t cbPage = PAGE_SIZE - (GCPhysSrc & PAGE_OFFSET_MASK);
|
---|
2375 | if (RT_LIKELY(cb <= cbPage))
|
---|
2376 | {
|
---|
2377 | memcpy(pvDst, pvSrc, cb);
|
---|
2378 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2379 | return VINF_SUCCESS;
|
---|
2380 | }
|
---|
2381 |
|
---|
2382 | /* copy to the end of the page. */
|
---|
2383 | memcpy(pvDst, pvSrc, cbPage);
|
---|
2384 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2385 | GCPhysSrc += cbPage;
|
---|
2386 | pvDst = (uint8_t *)pvDst + cbPage;
|
---|
2387 | cb -= cbPage;
|
---|
2388 |
|
---|
2389 | /*
|
---|
2390 | * Page by page.
|
---|
2391 | */
|
---|
2392 | for (;;)
|
---|
2393 | {
|
---|
2394 | /* map the page */
|
---|
2395 | rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhysSrc, &pvSrc, &Lock);
|
---|
2396 | if (RT_FAILURE(rc))
|
---|
2397 | return rc;
|
---|
2398 |
|
---|
2399 | /* last page? */
|
---|
2400 | if (cb <= PAGE_SIZE)
|
---|
2401 | {
|
---|
2402 | memcpy(pvDst, pvSrc, cb);
|
---|
2403 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2404 | return VINF_SUCCESS;
|
---|
2405 | }
|
---|
2406 |
|
---|
2407 | /* copy the entire page and advance */
|
---|
2408 | memcpy(pvDst, pvSrc, PAGE_SIZE);
|
---|
2409 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2410 | GCPhysSrc += PAGE_SIZE;
|
---|
2411 | pvDst = (uint8_t *)pvDst + PAGE_SIZE;
|
---|
2412 | cb -= PAGE_SIZE;
|
---|
2413 | }
|
---|
2414 | /* won't ever get here. */
|
---|
2415 | }
|
---|
2416 |
|
---|
2417 |
|
---|
2418 | /**
|
---|
2419 | * Write to guest physical memory referenced by GC pointer.
|
---|
2420 | * Write memory to GC physical address in guest physical memory.
|
---|
2421 | *
|
---|
2422 | * This will bypass MMIO and access handlers.
|
---|
2423 | *
|
---|
2424 | * @returns VBox status.
|
---|
2425 | * @param pVM VM handle.
|
---|
2426 | * @param GCPhysDst The GC physical address of the destination.
|
---|
2427 | * @param pvSrc The source buffer.
|
---|
2428 | * @param cb The number of bytes to write.
|
---|
2429 | */
|
---|
2430 | VMMDECL(int) PGMPhysSimpleWriteGCPhys(PVM pVM, RTGCPHYS GCPhysDst, const void *pvSrc, size_t cb)
|
---|
2431 | {
|
---|
2432 | LogFlow(("PGMPhysSimpleWriteGCPhys: %RGp %zu\n", GCPhysDst, cb));
|
---|
2433 |
|
---|
2434 | /*
|
---|
2435 | * Treat the first page as a special case.
|
---|
2436 | */
|
---|
2437 | if (!cb)
|
---|
2438 | return VINF_SUCCESS;
|
---|
2439 |
|
---|
2440 | /* map the 1st page */
|
---|
2441 | void *pvDst;
|
---|
2442 | PGMPAGEMAPLOCK Lock;
|
---|
2443 | int rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
|
---|
2444 | if (RT_FAILURE(rc))
|
---|
2445 | return rc;
|
---|
2446 |
|
---|
2447 | /* optimize for the case where access is completely within the first page. */
|
---|
2448 | size_t cbPage = PAGE_SIZE - (GCPhysDst & PAGE_OFFSET_MASK);
|
---|
2449 | if (RT_LIKELY(cb <= cbPage))
|
---|
2450 | {
|
---|
2451 | memcpy(pvDst, pvSrc, cb);
|
---|
2452 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2453 | return VINF_SUCCESS;
|
---|
2454 | }
|
---|
2455 |
|
---|
2456 | /* copy to the end of the page. */
|
---|
2457 | memcpy(pvDst, pvSrc, cbPage);
|
---|
2458 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2459 | GCPhysDst += cbPage;
|
---|
2460 | pvSrc = (const uint8_t *)pvSrc + cbPage;
|
---|
2461 | cb -= cbPage;
|
---|
2462 |
|
---|
2463 | /*
|
---|
2464 | * Page by page.
|
---|
2465 | */
|
---|
2466 | for (;;)
|
---|
2467 | {
|
---|
2468 | /* map the page */
|
---|
2469 | rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
|
---|
2470 | if (RT_FAILURE(rc))
|
---|
2471 | return rc;
|
---|
2472 |
|
---|
2473 | /* last page? */
|
---|
2474 | if (cb <= PAGE_SIZE)
|
---|
2475 | {
|
---|
2476 | memcpy(pvDst, pvSrc, cb);
|
---|
2477 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2478 | return VINF_SUCCESS;
|
---|
2479 | }
|
---|
2480 |
|
---|
2481 | /* copy the entire page and advance */
|
---|
2482 | memcpy(pvDst, pvSrc, PAGE_SIZE);
|
---|
2483 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2484 | GCPhysDst += PAGE_SIZE;
|
---|
2485 | pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
|
---|
2486 | cb -= PAGE_SIZE;
|
---|
2487 | }
|
---|
2488 | /* won't ever get here. */
|
---|
2489 | }
|
---|
2490 |
|
---|
2491 |
|
---|
2492 | /**
|
---|
2493 | * Read from guest physical memory referenced by GC pointer.
|
---|
2494 | *
|
---|
2495 | * This function uses the current CR3/CR0/CR4 of the guest and will
|
---|
2496 | * bypass access handlers and not set any accessed bits.
|
---|
2497 | *
|
---|
2498 | * @returns VBox status.
|
---|
2499 | * @param pVCpu The VMCPU handle.
|
---|
2500 | * @param pvDst The destination address.
|
---|
2501 | * @param GCPtrSrc The source address (GC pointer).
|
---|
2502 | * @param cb The number of bytes to read.
|
---|
2503 | */
|
---|
2504 | VMMDECL(int) PGMPhysSimpleReadGCPtr(PVMCPU pVCpu, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
|
---|
2505 | {
|
---|
2506 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
2507 |
|
---|
2508 | /*
|
---|
2509 | * Treat the first page as a special case.
|
---|
2510 | */
|
---|
2511 | if (!cb)
|
---|
2512 | return VINF_SUCCESS;
|
---|
2513 |
|
---|
2514 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,PhysSimpleRead));
|
---|
2515 | STAM_COUNTER_ADD(&pVM->pgm.s.CTX_MID_Z(Stat,PhysSimpleReadBytes), cb);
|
---|
2516 |
|
---|
2517 | /* Take the PGM lock here, because many called functions take the lock for a very short period. That's counter-productive
|
---|
2518 | * when many VCPUs are fighting for the lock.
|
---|
2519 | */
|
---|
2520 | pgmLock(pVM);
|
---|
2521 |
|
---|
2522 | /* map the 1st page */
|
---|
2523 | void const *pvSrc;
|
---|
2524 | PGMPAGEMAPLOCK Lock;
|
---|
2525 | int rc = PGMPhysGCPtr2CCPtrReadOnly(pVCpu, GCPtrSrc, &pvSrc, &Lock);
|
---|
2526 | if (RT_FAILURE(rc))
|
---|
2527 | {
|
---|
2528 | pgmUnlock(pVM);
|
---|
2529 | return rc;
|
---|
2530 | }
|
---|
2531 |
|
---|
2532 | /* optimize for the case where access is completely within the first page. */
|
---|
2533 | size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
|
---|
2534 | if (RT_LIKELY(cb <= cbPage))
|
---|
2535 | {
|
---|
2536 | memcpy(pvDst, pvSrc, cb);
|
---|
2537 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2538 | pgmUnlock(pVM);
|
---|
2539 | return VINF_SUCCESS;
|
---|
2540 | }
|
---|
2541 |
|
---|
2542 | /* copy to the end of the page. */
|
---|
2543 | memcpy(pvDst, pvSrc, cbPage);
|
---|
2544 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2545 | GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + cbPage);
|
---|
2546 | pvDst = (uint8_t *)pvDst + cbPage;
|
---|
2547 | cb -= cbPage;
|
---|
2548 |
|
---|
2549 | /*
|
---|
2550 | * Page by page.
|
---|
2551 | */
|
---|
2552 | for (;;)
|
---|
2553 | {
|
---|
2554 | /* map the page */
|
---|
2555 | rc = PGMPhysGCPtr2CCPtrReadOnly(pVCpu, GCPtrSrc, &pvSrc, &Lock);
|
---|
2556 | if (RT_FAILURE(rc))
|
---|
2557 | {
|
---|
2558 | pgmUnlock(pVM);
|
---|
2559 | return rc;
|
---|
2560 | }
|
---|
2561 |
|
---|
2562 | /* last page? */
|
---|
2563 | if (cb <= PAGE_SIZE)
|
---|
2564 | {
|
---|
2565 | memcpy(pvDst, pvSrc, cb);
|
---|
2566 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2567 | pgmUnlock(pVM);
|
---|
2568 | return VINF_SUCCESS;
|
---|
2569 | }
|
---|
2570 |
|
---|
2571 | /* copy the entire page and advance */
|
---|
2572 | memcpy(pvDst, pvSrc, PAGE_SIZE);
|
---|
2573 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2574 | GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + PAGE_SIZE);
|
---|
2575 | pvDst = (uint8_t *)pvDst + PAGE_SIZE;
|
---|
2576 | cb -= PAGE_SIZE;
|
---|
2577 | }
|
---|
2578 | /* won't ever get here. */
|
---|
2579 | }
|
---|
2580 |
|
---|
2581 |
|
---|
2582 | /**
|
---|
2583 | * Write to guest physical memory referenced by GC pointer.
|
---|
2584 | *
|
---|
2585 | * This function uses the current CR3/CR0/CR4 of the guest and will
|
---|
2586 | * bypass access handlers and not set dirty or accessed bits.
|
---|
2587 | *
|
---|
2588 | * @returns VBox status.
|
---|
2589 | * @param pVCpu The VMCPU handle.
|
---|
2590 | * @param GCPtrDst The destination address (GC pointer).
|
---|
2591 | * @param pvSrc The source address.
|
---|
2592 | * @param cb The number of bytes to write.
|
---|
2593 | */
|
---|
2594 | VMMDECL(int) PGMPhysSimpleWriteGCPtr(PVMCPU pVCpu, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
|
---|
2595 | {
|
---|
2596 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
2597 |
|
---|
2598 | /*
|
---|
2599 | * Treat the first page as a special case.
|
---|
2600 | */
|
---|
2601 | if (!cb)
|
---|
2602 | return VINF_SUCCESS;
|
---|
2603 |
|
---|
2604 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,PhysSimpleWrite));
|
---|
2605 | STAM_COUNTER_ADD(&pVM->pgm.s.CTX_MID_Z(Stat,PhysSimpleWriteBytes), cb);
|
---|
2606 |
|
---|
2607 | /* map the 1st page */
|
---|
2608 | void *pvDst;
|
---|
2609 | PGMPAGEMAPLOCK Lock;
|
---|
2610 | int rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
|
---|
2611 | if (RT_FAILURE(rc))
|
---|
2612 | return rc;
|
---|
2613 |
|
---|
2614 | /* optimize for the case where access is completely within the first page. */
|
---|
2615 | size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
|
---|
2616 | if (RT_LIKELY(cb <= cbPage))
|
---|
2617 | {
|
---|
2618 | memcpy(pvDst, pvSrc, cb);
|
---|
2619 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2620 | return VINF_SUCCESS;
|
---|
2621 | }
|
---|
2622 |
|
---|
2623 | /* copy to the end of the page. */
|
---|
2624 | memcpy(pvDst, pvSrc, cbPage);
|
---|
2625 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2626 | GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
|
---|
2627 | pvSrc = (const uint8_t *)pvSrc + cbPage;
|
---|
2628 | cb -= cbPage;
|
---|
2629 |
|
---|
2630 | /*
|
---|
2631 | * Page by page.
|
---|
2632 | */
|
---|
2633 | for (;;)
|
---|
2634 | {
|
---|
2635 | /* map the page */
|
---|
2636 | rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
|
---|
2637 | if (RT_FAILURE(rc))
|
---|
2638 | return rc;
|
---|
2639 |
|
---|
2640 | /* last page? */
|
---|
2641 | if (cb <= PAGE_SIZE)
|
---|
2642 | {
|
---|
2643 | memcpy(pvDst, pvSrc, cb);
|
---|
2644 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2645 | return VINF_SUCCESS;
|
---|
2646 | }
|
---|
2647 |
|
---|
2648 | /* copy the entire page and advance */
|
---|
2649 | memcpy(pvDst, pvSrc, PAGE_SIZE);
|
---|
2650 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2651 | GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + PAGE_SIZE);
|
---|
2652 | pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
|
---|
2653 | cb -= PAGE_SIZE;
|
---|
2654 | }
|
---|
2655 | /* won't ever get here. */
|
---|
2656 | }
|
---|
2657 |
|
---|
2658 |
|
---|
2659 | /**
|
---|
2660 | * Write to guest physical memory referenced by GC pointer and update the PTE.
|
---|
2661 | *
|
---|
2662 | * This function uses the current CR3/CR0/CR4 of the guest and will
|
---|
2663 | * bypass access handlers but will set any dirty and accessed bits in the PTE.
|
---|
2664 | *
|
---|
2665 | * If you don't want to set the dirty bit, use PGMPhysSimpleWriteGCPtr().
|
---|
2666 | *
|
---|
2667 | * @returns VBox status.
|
---|
2668 | * @param pVCpu The VMCPU handle.
|
---|
2669 | * @param GCPtrDst The destination address (GC pointer).
|
---|
2670 | * @param pvSrc The source address.
|
---|
2671 | * @param cb The number of bytes to write.
|
---|
2672 | */
|
---|
2673 | VMMDECL(int) PGMPhysSimpleDirtyWriteGCPtr(PVMCPU pVCpu, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
|
---|
2674 | {
|
---|
2675 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
2676 |
|
---|
2677 | /*
|
---|
2678 | * Treat the first page as a special case.
|
---|
2679 | * Btw. this is the same code as in PGMPhyssimpleWriteGCPtr excep for the PGMGstModifyPage.
|
---|
2680 | */
|
---|
2681 | if (!cb)
|
---|
2682 | return VINF_SUCCESS;
|
---|
2683 |
|
---|
2684 | /* map the 1st page */
|
---|
2685 | void *pvDst;
|
---|
2686 | PGMPAGEMAPLOCK Lock;
|
---|
2687 | int rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
|
---|
2688 | if (RT_FAILURE(rc))
|
---|
2689 | return rc;
|
---|
2690 |
|
---|
2691 | /* optimize for the case where access is completely within the first page. */
|
---|
2692 | size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
|
---|
2693 | if (RT_LIKELY(cb <= cbPage))
|
---|
2694 | {
|
---|
2695 | memcpy(pvDst, pvSrc, cb);
|
---|
2696 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2697 | rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
|
---|
2698 | return VINF_SUCCESS;
|
---|
2699 | }
|
---|
2700 |
|
---|
2701 | /* copy to the end of the page. */
|
---|
2702 | memcpy(pvDst, pvSrc, cbPage);
|
---|
2703 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2704 | rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
|
---|
2705 | GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
|
---|
2706 | pvSrc = (const uint8_t *)pvSrc + cbPage;
|
---|
2707 | cb -= cbPage;
|
---|
2708 |
|
---|
2709 | /*
|
---|
2710 | * Page by page.
|
---|
2711 | */
|
---|
2712 | for (;;)
|
---|
2713 | {
|
---|
2714 | /* map the page */
|
---|
2715 | rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
|
---|
2716 | if (RT_FAILURE(rc))
|
---|
2717 | return rc;
|
---|
2718 |
|
---|
2719 | /* last page? */
|
---|
2720 | if (cb <= PAGE_SIZE)
|
---|
2721 | {
|
---|
2722 | memcpy(pvDst, pvSrc, cb);
|
---|
2723 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2724 | rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
|
---|
2725 | return VINF_SUCCESS;
|
---|
2726 | }
|
---|
2727 |
|
---|
2728 | /* copy the entire page and advance */
|
---|
2729 | memcpy(pvDst, pvSrc, PAGE_SIZE);
|
---|
2730 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2731 | rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
|
---|
2732 | GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + PAGE_SIZE);
|
---|
2733 | pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
|
---|
2734 | cb -= PAGE_SIZE;
|
---|
2735 | }
|
---|
2736 | /* won't ever get here. */
|
---|
2737 | }
|
---|
2738 |
|
---|
2739 |
|
---|
2740 | /**
|
---|
2741 | * Read from guest physical memory referenced by GC pointer.
|
---|
2742 | *
|
---|
2743 | * This function uses the current CR3/CR0/CR4 of the guest and will
|
---|
2744 | * respect access handlers and set accessed bits.
|
---|
2745 | *
|
---|
2746 | * @returns VBox status.
|
---|
2747 | * @param pVCpu The VMCPU handle.
|
---|
2748 | * @param pvDst The destination address.
|
---|
2749 | * @param GCPtrSrc The source address (GC pointer).
|
---|
2750 | * @param cb The number of bytes to read.
|
---|
2751 | * @thread The vCPU EMT.
|
---|
2752 | */
|
---|
2753 | VMMDECL(int) PGMPhysReadGCPtr(PVMCPU pVCpu, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
|
---|
2754 | {
|
---|
2755 | RTGCPHYS GCPhys;
|
---|
2756 | uint64_t fFlags;
|
---|
2757 | int rc;
|
---|
2758 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
2759 |
|
---|
2760 | /*
|
---|
2761 | * Anything to do?
|
---|
2762 | */
|
---|
2763 | if (!cb)
|
---|
2764 | return VINF_SUCCESS;
|
---|
2765 |
|
---|
2766 | LogFlow(("PGMPhysReadGCPtr: %RGv %zu\n", GCPtrSrc, cb));
|
---|
2767 |
|
---|
2768 | /*
|
---|
2769 | * Optimize reads within a single page.
|
---|
2770 | */
|
---|
2771 | if (((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
|
---|
2772 | {
|
---|
2773 | /* Convert virtual to physical address + flags */
|
---|
2774 | rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrSrc, &fFlags, &GCPhys);
|
---|
2775 | AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrSrc), rc);
|
---|
2776 | GCPhys |= (RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK;
|
---|
2777 |
|
---|
2778 | /* mark the guest page as accessed. */
|
---|
2779 | if (!(fFlags & X86_PTE_A))
|
---|
2780 | {
|
---|
2781 | rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
|
---|
2782 | AssertRC(rc);
|
---|
2783 | }
|
---|
2784 |
|
---|
2785 | return PGMPhysRead(pVM, GCPhys, pvDst, cb);
|
---|
2786 | }
|
---|
2787 |
|
---|
2788 | /*
|
---|
2789 | * Page by page.
|
---|
2790 | */
|
---|
2791 | for (;;)
|
---|
2792 | {
|
---|
2793 | /* Convert virtual to physical address + flags */
|
---|
2794 | rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrSrc, &fFlags, &GCPhys);
|
---|
2795 | AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrSrc), rc);
|
---|
2796 | GCPhys |= (RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK;
|
---|
2797 |
|
---|
2798 | /* mark the guest page as accessed. */
|
---|
2799 | if (!(fFlags & X86_PTE_A))
|
---|
2800 | {
|
---|
2801 | rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
|
---|
2802 | AssertRC(rc);
|
---|
2803 | }
|
---|
2804 |
|
---|
2805 | /* copy */
|
---|
2806 | size_t cbRead = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
|
---|
2807 | rc = PGMPhysRead(pVM, GCPhys, pvDst, cbRead);
|
---|
2808 | if (cbRead >= cb || RT_FAILURE(rc))
|
---|
2809 | return rc;
|
---|
2810 |
|
---|
2811 | /* next */
|
---|
2812 | cb -= cbRead;
|
---|
2813 | pvDst = (uint8_t *)pvDst + cbRead;
|
---|
2814 | GCPtrSrc += cbRead;
|
---|
2815 | }
|
---|
2816 | }
|
---|
2817 |
|
---|
2818 |
|
---|
2819 | /**
|
---|
2820 | * Write to guest physical memory referenced by GC pointer.
|
---|
2821 | *
|
---|
2822 | * This function uses the current CR3/CR0/CR4 of the guest and will
|
---|
2823 | * respect access handlers and set dirty and accessed bits.
|
---|
2824 | *
|
---|
2825 | * @returns VBox status.
|
---|
2826 | * @retval VINF_SUCCESS.
|
---|
2827 | * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
|
---|
2828 | *
|
---|
2829 | * @param pVCpu The VMCPU handle.
|
---|
2830 | * @param GCPtrDst The destination address (GC pointer).
|
---|
2831 | * @param pvSrc The source address.
|
---|
2832 | * @param cb The number of bytes to write.
|
---|
2833 | */
|
---|
2834 | VMMDECL(int) PGMPhysWriteGCPtr(PVMCPU pVCpu, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
|
---|
2835 | {
|
---|
2836 | RTGCPHYS GCPhys;
|
---|
2837 | uint64_t fFlags;
|
---|
2838 | int rc;
|
---|
2839 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
2840 |
|
---|
2841 | /*
|
---|
2842 | * Anything to do?
|
---|
2843 | */
|
---|
2844 | if (!cb)
|
---|
2845 | return VINF_SUCCESS;
|
---|
2846 |
|
---|
2847 | LogFlow(("PGMPhysWriteGCPtr: %RGv %zu\n", GCPtrDst, cb));
|
---|
2848 |
|
---|
2849 | /*
|
---|
2850 | * Optimize writes within a single page.
|
---|
2851 | */
|
---|
2852 | if (((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
|
---|
2853 | {
|
---|
2854 | /* Convert virtual to physical address + flags */
|
---|
2855 | rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrDst, &fFlags, &GCPhys);
|
---|
2856 | AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrDst), rc);
|
---|
2857 | GCPhys |= (RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK;
|
---|
2858 |
|
---|
2859 | /* Mention when we ignore X86_PTE_RW... */
|
---|
2860 | if (!(fFlags & X86_PTE_RW))
|
---|
2861 | Log(("PGMPhysGCPtr2GCPhys: Writing to RO page %RGv %#x\n", GCPtrDst, cb));
|
---|
2862 |
|
---|
2863 | /* Mark the guest page as accessed and dirty if necessary. */
|
---|
2864 | if ((fFlags & (X86_PTE_A | X86_PTE_D)) != (X86_PTE_A | X86_PTE_D))
|
---|
2865 | {
|
---|
2866 | rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
|
---|
2867 | AssertRC(rc);
|
---|
2868 | }
|
---|
2869 |
|
---|
2870 | return PGMPhysWrite(pVM, GCPhys, pvSrc, cb);
|
---|
2871 | }
|
---|
2872 |
|
---|
2873 | /*
|
---|
2874 | * Page by page.
|
---|
2875 | */
|
---|
2876 | for (;;)
|
---|
2877 | {
|
---|
2878 | /* Convert virtual to physical address + flags */
|
---|
2879 | rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrDst, &fFlags, &GCPhys);
|
---|
2880 | AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrDst), rc);
|
---|
2881 | GCPhys |= (RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK;
|
---|
2882 |
|
---|
2883 | /* Mention when we ignore X86_PTE_RW... */
|
---|
2884 | if (!(fFlags & X86_PTE_RW))
|
---|
2885 | Log(("PGMPhysGCPtr2GCPhys: Writing to RO page %RGv %#x\n", GCPtrDst, cb));
|
---|
2886 |
|
---|
2887 | /* Mark the guest page as accessed and dirty if necessary. */
|
---|
2888 | if ((fFlags & (X86_PTE_A | X86_PTE_D)) != (X86_PTE_A | X86_PTE_D))
|
---|
2889 | {
|
---|
2890 | rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
|
---|
2891 | AssertRC(rc);
|
---|
2892 | }
|
---|
2893 |
|
---|
2894 | /* copy */
|
---|
2895 | size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
|
---|
2896 | rc = PGMPhysWrite(pVM, GCPhys, pvSrc, cbWrite);
|
---|
2897 | if (cbWrite >= cb || RT_FAILURE(rc))
|
---|
2898 | return rc;
|
---|
2899 |
|
---|
2900 | /* next */
|
---|
2901 | cb -= cbWrite;
|
---|
2902 | pvSrc = (uint8_t *)pvSrc + cbWrite;
|
---|
2903 | GCPtrDst += cbWrite;
|
---|
2904 | }
|
---|
2905 | }
|
---|
2906 |
|
---|
2907 |
|
---|
2908 | /**
|
---|
2909 | * Performs a read of guest virtual memory for instruction emulation.
|
---|
2910 | *
|
---|
2911 | * This will check permissions, raise exceptions and update the access bits.
|
---|
2912 | *
|
---|
2913 | * The current implementation will bypass all access handlers. It may later be
|
---|
2914 | * changed to at least respect MMIO.
|
---|
2915 | *
|
---|
2916 | *
|
---|
2917 | * @returns VBox status code suitable to scheduling.
|
---|
2918 | * @retval VINF_SUCCESS if the read was performed successfully.
|
---|
2919 | * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
|
---|
2920 | * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
|
---|
2921 | *
|
---|
2922 | * @param pVCpu The VMCPU handle.
|
---|
2923 | * @param pCtxCore The context core.
|
---|
2924 | * @param pvDst Where to put the bytes we've read.
|
---|
2925 | * @param GCPtrSrc The source address.
|
---|
2926 | * @param cb The number of bytes to read. Not more than a page.
|
---|
2927 | *
|
---|
2928 | * @remark This function will dynamically map physical pages in GC. This may unmap
|
---|
2929 | * mappings done by the caller. Be careful!
|
---|
2930 | */
|
---|
2931 | VMMDECL(int) PGMPhysInterpretedRead(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb)
|
---|
2932 | {
|
---|
2933 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
2934 | Assert(cb <= PAGE_SIZE);
|
---|
2935 |
|
---|
2936 | /** @todo r=bird: This isn't perfect!
|
---|
2937 | * -# It's not checking for reserved bits being 1.
|
---|
2938 | * -# It's not correctly dealing with the access bit.
|
---|
2939 | * -# It's not respecting MMIO memory or any other access handlers.
|
---|
2940 | */
|
---|
2941 | /*
|
---|
2942 | * 1. Translate virtual to physical. This may fault.
|
---|
2943 | * 2. Map the physical address.
|
---|
2944 | * 3. Do the read operation.
|
---|
2945 | * 4. Set access bits if required.
|
---|
2946 | */
|
---|
2947 | int rc;
|
---|
2948 | unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
|
---|
2949 | if (cb <= cb1)
|
---|
2950 | {
|
---|
2951 | /*
|
---|
2952 | * Not crossing pages.
|
---|
2953 | */
|
---|
2954 | RTGCPHYS GCPhys;
|
---|
2955 | uint64_t fFlags;
|
---|
2956 | rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags, &GCPhys);
|
---|
2957 | if (RT_SUCCESS(rc))
|
---|
2958 | {
|
---|
2959 | /** @todo we should check reserved bits ... */
|
---|
2960 | void *pvSrc;
|
---|
2961 | rc = PGM_GCPHYS_2_PTR(pVM, GCPhys, &pvSrc);
|
---|
2962 | switch (rc)
|
---|
2963 | {
|
---|
2964 | case VINF_SUCCESS:
|
---|
2965 | Log(("PGMPhysInterpretedRead: pvDst=%p pvSrc=%p cb=%d\n", pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb));
|
---|
2966 | memcpy(pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
|
---|
2967 | break;
|
---|
2968 | case VERR_PGM_PHYS_PAGE_RESERVED:
|
---|
2969 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
2970 | memset(pvDst, 0, cb); /** @todo this is wrong, it should be 0xff */
|
---|
2971 | break;
|
---|
2972 | default:
|
---|
2973 | return rc;
|
---|
2974 | }
|
---|
2975 |
|
---|
2976 | /** @todo access bit emulation isn't 100% correct. */
|
---|
2977 | if (!(fFlags & X86_PTE_A))
|
---|
2978 | {
|
---|
2979 | rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
|
---|
2980 | AssertRC(rc);
|
---|
2981 | }
|
---|
2982 | return VINF_SUCCESS;
|
---|
2983 | }
|
---|
2984 | }
|
---|
2985 | else
|
---|
2986 | {
|
---|
2987 | /*
|
---|
2988 | * Crosses pages.
|
---|
2989 | */
|
---|
2990 | size_t cb2 = cb - cb1;
|
---|
2991 | uint64_t fFlags1;
|
---|
2992 | RTGCPHYS GCPhys1;
|
---|
2993 | uint64_t fFlags2;
|
---|
2994 | RTGCPHYS GCPhys2;
|
---|
2995 | rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags1, &GCPhys1);
|
---|
2996 | if (RT_SUCCESS(rc))
|
---|
2997 | rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
|
---|
2998 | if (RT_SUCCESS(rc))
|
---|
2999 | {
|
---|
3000 | /** @todo we should check reserved bits ... */
|
---|
3001 | AssertMsgFailed(("cb=%d cb1=%d cb2=%d GCPtrSrc=%RGv\n", cb, cb1, cb2, GCPtrSrc));
|
---|
3002 | void *pvSrc1;
|
---|
3003 | rc = PGM_GCPHYS_2_PTR(pVM, GCPhys1, &pvSrc1);
|
---|
3004 | switch (rc)
|
---|
3005 | {
|
---|
3006 | case VINF_SUCCESS:
|
---|
3007 | memcpy(pvDst, (uint8_t *)pvSrc1 + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
|
---|
3008 | break;
|
---|
3009 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3010 | memset(pvDst, 0, cb1); /** @todo this is wrong, it should be 0xff */
|
---|
3011 | break;
|
---|
3012 | default:
|
---|
3013 | return rc;
|
---|
3014 | }
|
---|
3015 |
|
---|
3016 | void *pvSrc2;
|
---|
3017 | rc = PGM_GCPHYS_2_PTR(pVM, GCPhys2, &pvSrc2);
|
---|
3018 | switch (rc)
|
---|
3019 | {
|
---|
3020 | case VINF_SUCCESS:
|
---|
3021 | memcpy((uint8_t *)pvDst + cb1, pvSrc2, cb2);
|
---|
3022 | break;
|
---|
3023 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3024 | memset((uint8_t *)pvDst + cb1, 0, cb2); /** @todo this is wrong, it should be 0xff */
|
---|
3025 | break;
|
---|
3026 | default:
|
---|
3027 | return rc;
|
---|
3028 | }
|
---|
3029 |
|
---|
3030 | if (!(fFlags1 & X86_PTE_A))
|
---|
3031 | {
|
---|
3032 | rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
|
---|
3033 | AssertRC(rc);
|
---|
3034 | }
|
---|
3035 | if (!(fFlags2 & X86_PTE_A))
|
---|
3036 | {
|
---|
3037 | rc = PGMGstModifyPage(pVCpu, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
|
---|
3038 | AssertRC(rc);
|
---|
3039 | }
|
---|
3040 | return VINF_SUCCESS;
|
---|
3041 | }
|
---|
3042 | }
|
---|
3043 |
|
---|
3044 | /*
|
---|
3045 | * Raise a #PF.
|
---|
3046 | */
|
---|
3047 | uint32_t uErr;
|
---|
3048 |
|
---|
3049 | /* Get the current privilege level. */
|
---|
3050 | uint32_t cpl = CPUMGetGuestCPL(pVCpu, pCtxCore);
|
---|
3051 | switch (rc)
|
---|
3052 | {
|
---|
3053 | case VINF_SUCCESS:
|
---|
3054 | uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
|
---|
3055 | break;
|
---|
3056 |
|
---|
3057 | case VERR_PAGE_NOT_PRESENT:
|
---|
3058 | case VERR_PAGE_TABLE_NOT_PRESENT:
|
---|
3059 | uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
|
---|
3060 | break;
|
---|
3061 |
|
---|
3062 | default:
|
---|
3063 | AssertMsgFailed(("rc=%Rrc GCPtrSrc=%RGv cb=%#x\n", rc, GCPtrSrc, cb));
|
---|
3064 | return rc;
|
---|
3065 | }
|
---|
3066 | Log(("PGMPhysInterpretedRead: GCPtrSrc=%RGv cb=%#x -> #PF(%#x)\n", GCPtrSrc, cb, uErr));
|
---|
3067 | return TRPMRaiseXcptErrCR2(pVCpu, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
|
---|
3068 | }
|
---|
3069 |
|
---|
3070 |
|
---|
3071 | /**
|
---|
3072 | * Performs a read of guest virtual memory for instruction emulation.
|
---|
3073 | *
|
---|
3074 | * This will check permissions, raise exceptions and update the access bits.
|
---|
3075 | *
|
---|
3076 | * The current implementation will bypass all access handlers. It may later be
|
---|
3077 | * changed to at least respect MMIO.
|
---|
3078 | *
|
---|
3079 | *
|
---|
3080 | * @returns VBox status code suitable to scheduling.
|
---|
3081 | * @retval VINF_SUCCESS if the read was performed successfully.
|
---|
3082 | * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
|
---|
3083 | * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
|
---|
3084 | *
|
---|
3085 | * @param pVCpu The VMCPU handle.
|
---|
3086 | * @param pCtxCore The context core.
|
---|
3087 | * @param pvDst Where to put the bytes we've read.
|
---|
3088 | * @param GCPtrSrc The source address.
|
---|
3089 | * @param cb The number of bytes to read. Not more than a page.
|
---|
3090 | * @param fRaiseTrap If set the trap will be raised on as per spec, if clear
|
---|
3091 | * an appropriate error status will be returned (no
|
---|
3092 | * informational at all).
|
---|
3093 | *
|
---|
3094 | *
|
---|
3095 | * @remarks Takes the PGM lock.
|
---|
3096 | * @remarks A page fault on the 2nd page of the access will be raised without
|
---|
3097 | * writing the bits on the first page since we're ASSUMING that the
|
---|
3098 | * caller is emulating an instruction access.
|
---|
3099 | * @remarks This function will dynamically map physical pages in GC. This may
|
---|
3100 | * unmap mappings done by the caller. Be careful!
|
---|
3101 | */
|
---|
3102 | VMMDECL(int) PGMPhysInterpretedReadNoHandlers(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb, bool fRaiseTrap)
|
---|
3103 | {
|
---|
3104 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
3105 | Assert(cb <= PAGE_SIZE);
|
---|
3106 |
|
---|
3107 | /*
|
---|
3108 | * 1. Translate virtual to physical. This may fault.
|
---|
3109 | * 2. Map the physical address.
|
---|
3110 | * 3. Do the read operation.
|
---|
3111 | * 4. Set access bits if required.
|
---|
3112 | */
|
---|
3113 | int rc;
|
---|
3114 | unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
|
---|
3115 | if (cb <= cb1)
|
---|
3116 | {
|
---|
3117 | /*
|
---|
3118 | * Not crossing pages.
|
---|
3119 | */
|
---|
3120 | RTGCPHYS GCPhys;
|
---|
3121 | uint64_t fFlags;
|
---|
3122 | rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags, &GCPhys);
|
---|
3123 | if (RT_SUCCESS(rc))
|
---|
3124 | {
|
---|
3125 | if (1) /** @todo we should check reserved bits ... */
|
---|
3126 | {
|
---|
3127 | const void *pvSrc;
|
---|
3128 | PGMPAGEMAPLOCK Lock;
|
---|
3129 | rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys, &pvSrc, &Lock);
|
---|
3130 | switch (rc)
|
---|
3131 | {
|
---|
3132 | case VINF_SUCCESS:
|
---|
3133 | Log(("PGMPhysInterpretedReadNoHandlers: pvDst=%p pvSrc=%p (%RGv) cb=%d\n",
|
---|
3134 | pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), GCPtrSrc, cb));
|
---|
3135 | memcpy(pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
|
---|
3136 | break;
|
---|
3137 | case VERR_PGM_PHYS_PAGE_RESERVED:
|
---|
3138 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3139 | memset(pvDst, 0xff, cb);
|
---|
3140 | break;
|
---|
3141 | default:
|
---|
3142 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
3143 | AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
|
---|
3144 | return rc;
|
---|
3145 | }
|
---|
3146 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3147 |
|
---|
3148 | if (!(fFlags & X86_PTE_A))
|
---|
3149 | {
|
---|
3150 | /** @todo access bit emulation isn't 100% correct. */
|
---|
3151 | rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
|
---|
3152 | AssertRC(rc);
|
---|
3153 | }
|
---|
3154 | return VINF_SUCCESS;
|
---|
3155 | }
|
---|
3156 | }
|
---|
3157 | }
|
---|
3158 | else
|
---|
3159 | {
|
---|
3160 | /*
|
---|
3161 | * Crosses pages.
|
---|
3162 | */
|
---|
3163 | size_t cb2 = cb - cb1;
|
---|
3164 | uint64_t fFlags1;
|
---|
3165 | RTGCPHYS GCPhys1;
|
---|
3166 | uint64_t fFlags2;
|
---|
3167 | RTGCPHYS GCPhys2;
|
---|
3168 | rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags1, &GCPhys1);
|
---|
3169 | if (RT_SUCCESS(rc))
|
---|
3170 | {
|
---|
3171 | rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
|
---|
3172 | if (RT_SUCCESS(rc))
|
---|
3173 | {
|
---|
3174 | if (1) /** @todo we should check reserved bits ... */
|
---|
3175 | {
|
---|
3176 | const void *pvSrc;
|
---|
3177 | PGMPAGEMAPLOCK Lock;
|
---|
3178 | rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys1, &pvSrc, &Lock);
|
---|
3179 | switch (rc)
|
---|
3180 | {
|
---|
3181 | case VINF_SUCCESS:
|
---|
3182 | Log(("PGMPhysInterpretedReadNoHandlers: pvDst=%p pvSrc=%p (%RGv) cb=%d [2]\n",
|
---|
3183 | pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), GCPtrSrc, cb1));
|
---|
3184 | memcpy(pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
|
---|
3185 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3186 | break;
|
---|
3187 | case VERR_PGM_PHYS_PAGE_RESERVED:
|
---|
3188 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3189 | memset(pvDst, 0xff, cb1);
|
---|
3190 | break;
|
---|
3191 | default:
|
---|
3192 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
3193 | AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
|
---|
3194 | return rc;
|
---|
3195 | }
|
---|
3196 |
|
---|
3197 | rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys2, &pvSrc, &Lock);
|
---|
3198 | switch (rc)
|
---|
3199 | {
|
---|
3200 | case VINF_SUCCESS:
|
---|
3201 | memcpy((uint8_t *)pvDst + cb1, pvSrc, cb2);
|
---|
3202 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3203 | break;
|
---|
3204 | case VERR_PGM_PHYS_PAGE_RESERVED:
|
---|
3205 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3206 | memset((uint8_t *)pvDst + cb1, 0xff, cb2);
|
---|
3207 | break;
|
---|
3208 | default:
|
---|
3209 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
3210 | AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
|
---|
3211 | return rc;
|
---|
3212 | }
|
---|
3213 |
|
---|
3214 | if (!(fFlags1 & X86_PTE_A))
|
---|
3215 | {
|
---|
3216 | rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
|
---|
3217 | AssertRC(rc);
|
---|
3218 | }
|
---|
3219 | if (!(fFlags2 & X86_PTE_A))
|
---|
3220 | {
|
---|
3221 | rc = PGMGstModifyPage(pVCpu, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
|
---|
3222 | AssertRC(rc);
|
---|
3223 | }
|
---|
3224 | return VINF_SUCCESS;
|
---|
3225 | }
|
---|
3226 | /* sort out which page */
|
---|
3227 | }
|
---|
3228 | else
|
---|
3229 | GCPtrSrc += cb1; /* fault on 2nd page */
|
---|
3230 | }
|
---|
3231 | }
|
---|
3232 |
|
---|
3233 | /*
|
---|
3234 | * Raise a #PF if we're allowed to do that.
|
---|
3235 | */
|
---|
3236 | /* Calc the error bits. */
|
---|
3237 | uint32_t cpl = CPUMGetGuestCPL(pVCpu, pCtxCore);
|
---|
3238 | uint32_t uErr;
|
---|
3239 | switch (rc)
|
---|
3240 | {
|
---|
3241 | case VINF_SUCCESS:
|
---|
3242 | uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
|
---|
3243 | rc = VERR_ACCESS_DENIED;
|
---|
3244 | break;
|
---|
3245 |
|
---|
3246 | case VERR_PAGE_NOT_PRESENT:
|
---|
3247 | case VERR_PAGE_TABLE_NOT_PRESENT:
|
---|
3248 | uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
|
---|
3249 | break;
|
---|
3250 |
|
---|
3251 | default:
|
---|
3252 | AssertMsgFailed(("rc=%Rrc GCPtrSrc=%RGv cb=%#x\n", rc, GCPtrSrc, cb));
|
---|
3253 | AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
|
---|
3254 | return rc;
|
---|
3255 | }
|
---|
3256 | if (fRaiseTrap)
|
---|
3257 | {
|
---|
3258 | Log(("PGMPhysInterpretedReadNoHandlers: GCPtrSrc=%RGv cb=%#x -> Raised #PF(%#x)\n", GCPtrSrc, cb, uErr));
|
---|
3259 | return TRPMRaiseXcptErrCR2(pVCpu, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
|
---|
3260 | }
|
---|
3261 | Log(("PGMPhysInterpretedReadNoHandlers: GCPtrSrc=%RGv cb=%#x -> #PF(%#x) [!raised]\n", GCPtrSrc, cb, uErr));
|
---|
3262 | return rc;
|
---|
3263 | }
|
---|
3264 |
|
---|
3265 |
|
---|
3266 | /**
|
---|
3267 | * Performs a write to guest virtual memory for instruction emulation.
|
---|
3268 | *
|
---|
3269 | * This will check permissions, raise exceptions and update the dirty and access
|
---|
3270 | * bits.
|
---|
3271 | *
|
---|
3272 | * @returns VBox status code suitable to scheduling.
|
---|
3273 | * @retval VINF_SUCCESS if the read was performed successfully.
|
---|
3274 | * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
|
---|
3275 | * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
|
---|
3276 | *
|
---|
3277 | * @param pVCpu The VMCPU handle.
|
---|
3278 | * @param pCtxCore The context core.
|
---|
3279 | * @param GCPtrDst The destination address.
|
---|
3280 | * @param pvSrc What to write.
|
---|
3281 | * @param cb The number of bytes to write. Not more than a page.
|
---|
3282 | * @param fRaiseTrap If set the trap will be raised on as per spec, if clear
|
---|
3283 | * an appropriate error status will be returned (no
|
---|
3284 | * informational at all).
|
---|
3285 | *
|
---|
3286 | * @remarks Takes the PGM lock.
|
---|
3287 | * @remarks A page fault on the 2nd page of the access will be raised without
|
---|
3288 | * writing the bits on the first page since we're ASSUMING that the
|
---|
3289 | * caller is emulating an instruction access.
|
---|
3290 | * @remarks This function will dynamically map physical pages in GC. This may
|
---|
3291 | * unmap mappings done by the caller. Be careful!
|
---|
3292 | */
|
---|
3293 | VMMDECL(int) PGMPhysInterpretedWriteNoHandlers(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb, bool fRaiseTrap)
|
---|
3294 | {
|
---|
3295 | Assert(cb <= PAGE_SIZE);
|
---|
3296 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
3297 |
|
---|
3298 | /*
|
---|
3299 | * 1. Translate virtual to physical. This may fault.
|
---|
3300 | * 2. Map the physical address.
|
---|
3301 | * 3. Do the write operation.
|
---|
3302 | * 4. Set access bits if required.
|
---|
3303 | */
|
---|
3304 | int rc;
|
---|
3305 | unsigned cb1 = PAGE_SIZE - (GCPtrDst & PAGE_OFFSET_MASK);
|
---|
3306 | if (cb <= cb1)
|
---|
3307 | {
|
---|
3308 | /*
|
---|
3309 | * Not crossing pages.
|
---|
3310 | */
|
---|
3311 | RTGCPHYS GCPhys;
|
---|
3312 | uint64_t fFlags;
|
---|
3313 | rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrDst, &fFlags, &GCPhys);
|
---|
3314 | if (RT_SUCCESS(rc))
|
---|
3315 | {
|
---|
3316 | if ( (fFlags & X86_PTE_RW) /** @todo Also check reserved bits. */
|
---|
3317 | || ( !(CPUMGetGuestCR0(pVCpu) & X86_CR0_WP)
|
---|
3318 | && CPUMGetGuestCPL(pVCpu, pCtxCore) <= 2) ) /** @todo it's 2, right? Check cpl check below as well. */
|
---|
3319 | {
|
---|
3320 | void *pvDst;
|
---|
3321 | PGMPAGEMAPLOCK Lock;
|
---|
3322 | rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys, &pvDst, &Lock);
|
---|
3323 | switch (rc)
|
---|
3324 | {
|
---|
3325 | case VINF_SUCCESS:
|
---|
3326 | Log(("PGMPhysInterpretedWriteNoHandlers: pvDst=%p (%RGv) pvSrc=%p cb=%d\n",
|
---|
3327 | (uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), GCPtrDst, pvSrc, cb));
|
---|
3328 | memcpy((uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), pvSrc, cb);
|
---|
3329 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3330 | break;
|
---|
3331 | case VERR_PGM_PHYS_PAGE_RESERVED:
|
---|
3332 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3333 | /* bit bucket */
|
---|
3334 | break;
|
---|
3335 | default:
|
---|
3336 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
3337 | AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
|
---|
3338 | return rc;
|
---|
3339 | }
|
---|
3340 |
|
---|
3341 | if (!(fFlags & (X86_PTE_A | X86_PTE_D)))
|
---|
3342 | {
|
---|
3343 | /** @todo dirty & access bit emulation isn't 100% correct. */
|
---|
3344 | rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
|
---|
3345 | AssertRC(rc);
|
---|
3346 | }
|
---|
3347 | return VINF_SUCCESS;
|
---|
3348 | }
|
---|
3349 | rc = VERR_ACCESS_DENIED;
|
---|
3350 | }
|
---|
3351 | }
|
---|
3352 | else
|
---|
3353 | {
|
---|
3354 | /*
|
---|
3355 | * Crosses pages.
|
---|
3356 | */
|
---|
3357 | size_t cb2 = cb - cb1;
|
---|
3358 | uint64_t fFlags1;
|
---|
3359 | RTGCPHYS GCPhys1;
|
---|
3360 | uint64_t fFlags2;
|
---|
3361 | RTGCPHYS GCPhys2;
|
---|
3362 | rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrDst, &fFlags1, &GCPhys1);
|
---|
3363 | if (RT_SUCCESS(rc))
|
---|
3364 | {
|
---|
3365 | rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrDst + cb1, &fFlags2, &GCPhys2);
|
---|
3366 | if (RT_SUCCESS(rc))
|
---|
3367 | {
|
---|
3368 | if ( ( (fFlags1 & X86_PTE_RW) /** @todo Also check reserved bits. */
|
---|
3369 | && (fFlags2 & X86_PTE_RW))
|
---|
3370 | || ( !(CPUMGetGuestCR0(pVCpu) & X86_CR0_WP)
|
---|
3371 | && CPUMGetGuestCPL(pVCpu, pCtxCore) <= 2) )
|
---|
3372 | {
|
---|
3373 | void *pvDst;
|
---|
3374 | PGMPAGEMAPLOCK Lock;
|
---|
3375 | rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys1, &pvDst, &Lock);
|
---|
3376 | switch (rc)
|
---|
3377 | {
|
---|
3378 | case VINF_SUCCESS:
|
---|
3379 | Log(("PGMPhysInterpretedWriteNoHandlers: pvDst=%p (%RGv) pvSrc=%p cb=%d\n",
|
---|
3380 | (uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), GCPtrDst, pvSrc, cb1));
|
---|
3381 | memcpy((uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), pvSrc, cb1);
|
---|
3382 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3383 | break;
|
---|
3384 | case VERR_PGM_PHYS_PAGE_RESERVED:
|
---|
3385 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3386 | /* bit bucket */
|
---|
3387 | break;
|
---|
3388 | default:
|
---|
3389 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
3390 | AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
|
---|
3391 | return rc;
|
---|
3392 | }
|
---|
3393 |
|
---|
3394 | rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys2, &pvDst, &Lock);
|
---|
3395 | switch (rc)
|
---|
3396 | {
|
---|
3397 | case VINF_SUCCESS:
|
---|
3398 | memcpy(pvDst, (const uint8_t *)pvSrc + cb1, cb2);
|
---|
3399 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3400 | break;
|
---|
3401 | case VERR_PGM_PHYS_PAGE_RESERVED:
|
---|
3402 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3403 | /* bit bucket */
|
---|
3404 | break;
|
---|
3405 | default:
|
---|
3406 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
3407 | AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
|
---|
3408 | return rc;
|
---|
3409 | }
|
---|
3410 |
|
---|
3411 | if (!(fFlags1 & (X86_PTE_A | X86_PTE_RW)))
|
---|
3412 | {
|
---|
3413 | rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, (X86_PTE_A | X86_PTE_RW), ~(uint64_t)(X86_PTE_A | X86_PTE_RW));
|
---|
3414 | AssertRC(rc);
|
---|
3415 | }
|
---|
3416 | if (!(fFlags2 & (X86_PTE_A | X86_PTE_RW)))
|
---|
3417 | {
|
---|
3418 | rc = PGMGstModifyPage(pVCpu, GCPtrDst + cb1, 1, (X86_PTE_A | X86_PTE_RW), ~(uint64_t)(X86_PTE_A | X86_PTE_RW));
|
---|
3419 | AssertRC(rc);
|
---|
3420 | }
|
---|
3421 | return VINF_SUCCESS;
|
---|
3422 | }
|
---|
3423 | if ((fFlags1 & (X86_PTE_RW)) == X86_PTE_RW)
|
---|
3424 | GCPtrDst += cb1; /* fault on the 2nd page. */
|
---|
3425 | rc = VERR_ACCESS_DENIED;
|
---|
3426 | }
|
---|
3427 | else
|
---|
3428 | GCPtrDst += cb1; /* fault on the 2nd page. */
|
---|
3429 | }
|
---|
3430 | }
|
---|
3431 |
|
---|
3432 | /*
|
---|
3433 | * Raise a #PF if we're allowed to do that.
|
---|
3434 | */
|
---|
3435 | /* Calc the error bits. */
|
---|
3436 | uint32_t uErr;
|
---|
3437 | uint32_t cpl = CPUMGetGuestCPL(pVCpu, pCtxCore);
|
---|
3438 | switch (rc)
|
---|
3439 | {
|
---|
3440 | case VINF_SUCCESS:
|
---|
3441 | uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
|
---|
3442 | rc = VERR_ACCESS_DENIED;
|
---|
3443 | break;
|
---|
3444 |
|
---|
3445 | case VERR_ACCESS_DENIED:
|
---|
3446 | uErr = (cpl >= 2) ? X86_TRAP_PF_RW | X86_TRAP_PF_US : X86_TRAP_PF_RW;
|
---|
3447 | break;
|
---|
3448 |
|
---|
3449 | case VERR_PAGE_NOT_PRESENT:
|
---|
3450 | case VERR_PAGE_TABLE_NOT_PRESENT:
|
---|
3451 | uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
|
---|
3452 | break;
|
---|
3453 |
|
---|
3454 | default:
|
---|
3455 | AssertMsgFailed(("rc=%Rrc GCPtrDst=%RGv cb=%#x\n", rc, GCPtrDst, cb));
|
---|
3456 | AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
|
---|
3457 | return rc;
|
---|
3458 | }
|
---|
3459 | if (fRaiseTrap)
|
---|
3460 | {
|
---|
3461 | Log(("PGMPhysInterpretedWriteNoHandlers: GCPtrDst=%RGv cb=%#x -> Raised #PF(%#x)\n", GCPtrDst, cb, uErr));
|
---|
3462 | return TRPMRaiseXcptErrCR2(pVCpu, pCtxCore, X86_XCPT_PF, uErr, GCPtrDst);
|
---|
3463 | }
|
---|
3464 | Log(("PGMPhysInterpretedWriteNoHandlers: GCPtrDst=%RGv cb=%#x -> #PF(%#x) [!raised]\n", GCPtrDst, cb, uErr));
|
---|
3465 | return rc;
|
---|
3466 | }
|
---|
3467 |
|
---|