VirtualBox

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

Last change on this file since 30766 was 30766, checked in by vboxsync, 15 years ago

No need to flush the handy pages immediately after copy-on-write for a shared page

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

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