VirtualBox

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

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

Paranoia

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