VirtualBox

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

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

VMM: Grumble! Reverted the wrong code before comitting.

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