VirtualBox

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

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

PGMAllPhys.cpp: -Wshadow

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