VirtualBox

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

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

PGM: Page lock counters. (disabled)

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