VirtualBox

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

Last change on this file since 35499 was 35346, checked in by vboxsync, 14 years ago

VMM reorg: Moving the public include files from include/VBox to include/VBox/vmm.

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

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