VirtualBox

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

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

Large page code cleanup

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

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