VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/PGMAllPhys.cpp@ 31631

Last change on this file since 31631 was 31444, checked in by vboxsync, 14 years ago

PGM: Don't let the ATA device exhaust the dynamic mapping cache - implemented actual unlocking of pages in PGMPhysReleasePageMappingLock. (RC and darwin.x86+R0)

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette