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