VirtualBox

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

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

VMM: whitespace cleanup.

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

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