VirtualBox

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

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

PGMPhysPageMap cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 117.8 KB
Line 
1/* $Id: PGMAllPhys.cpp 23453 2009-09-30 21:55:25Z 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# if 0 /** @todo implement locking properly */
1072 if (RT_LIKELY(pPage->cLocks != PGM_PAGE_MAX_LOCKS))
1073 if (RT_UNLIKELY(++pPage->cLocks == PGM_PAGE_MAX_LOCKS))
1074 {
1075 AssertMsgFailed(("%RGp is entering permanent locked state!\n", GCPhys));
1076 if (pMap)
1077 pMap->cRefs++; /* Extra ref to prevent it from going away. */
1078 }
1079# endif
1080 *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
1081 pLock->pvPage = pPage;
1082 pLock->pvMap = pMap;
1083 }
1084 }
1085
1086 pgmUnlock(pVM);
1087#endif /* IN_RING3 || IN_RING0 */
1088 return rc;
1089}
1090
1091
1092/**
1093 * Requests the mapping of a guest page into the current context.
1094 *
1095 * This API should only be used for very short term, as it will consume
1096 * scarse resources (R0 and GC) in the mapping cache. When you're done
1097 * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
1098 *
1099 * @returns VBox status code.
1100 * @retval VINF_SUCCESS on success.
1101 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
1102 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
1103 *
1104 * @param pVM The VM handle.
1105 * @param GCPhys The guest physical address of the page that should be mapped.
1106 * @param ppv Where to store the address corresponding to GCPhys.
1107 * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
1108 *
1109 * @remarks The caller is responsible for dealing with access handlers.
1110 * @todo Add an informational return code for pages with access handlers?
1111 *
1112 * @remark Avoid calling this API from within critical sections (other than
1113 * the PGM one) because of the deadlock risk.
1114 * @thread Any thread.
1115 */
1116VMMDECL(int) PGMPhysGCPhys2CCPtrReadOnly(PVM pVM, RTGCPHYS GCPhys, void const **ppv, PPGMPAGEMAPLOCK pLock)
1117{
1118#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1119
1120 /*
1121 * Find the page and make sure it's readable.
1122 */
1123 PPGMPAGE pPage;
1124 int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhys, &pPage);
1125 if (RT_SUCCESS(rc))
1126 {
1127 if (RT_UNLIKELY(PGM_PAGE_IS_MMIO(pPage)))
1128 rc = VERR_PGM_PHYS_PAGE_RESERVED;
1129 else
1130 {
1131 *ppv = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK)); /** @todo add a read only flag? */
1132# if 0
1133 pLock->pvMap = 0;
1134 pLock->pvPage = pPage;
1135# else
1136 pLock->u32Dummy = UINT32_MAX;
1137# endif
1138 AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* not returned */, ("%Rrc\n", rc));
1139 rc = VINF_SUCCESS;
1140 }
1141 }
1142
1143#else /* IN_RING3 || IN_RING0 */
1144 int rc = pgmLock(pVM);
1145 AssertRCReturn(rc, rc);
1146
1147 /*
1148 * Query the Physical TLB entry for the page (may fail).
1149 */
1150 PPGMPAGEMAPTLBE pTlbe;
1151 rc = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
1152 if (RT_SUCCESS(rc))
1153 {
1154 /* MMIO pages doesn't have any readable backing. */
1155 PPGMPAGE pPage = pTlbe->pPage;
1156 if (RT_UNLIKELY(PGM_PAGE_IS_MMIO(pPage)))
1157 rc = VERR_PGM_PHYS_PAGE_RESERVED;
1158 else
1159 {
1160 /*
1161 * Now, just perform the locking and calculate the return address.
1162 */
1163 PPGMPAGEMAP pMap = pTlbe->pMap;
1164 if (pMap)
1165 pMap->cRefs++;
1166# if 0 /** @todo implement locking properly */
1167 if (RT_LIKELY(pPage->cLocks != PGM_PAGE_MAX_LOCKS))
1168 if (RT_UNLIKELY(++pPage->cLocks == PGM_PAGE_MAX_LOCKS))
1169 {
1170 AssertMsgFailed(("%RGp is entering permanent locked state!\n", GCPhys));
1171 if (pMap)
1172 pMap->cRefs++; /* Extra ref to prevent it from going away. */
1173 }
1174# endif
1175 *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
1176 pLock->pvPage = pPage;
1177 pLock->pvMap = pMap;
1178 }
1179 }
1180
1181 pgmUnlock(pVM);
1182#endif /* IN_RING3 || IN_RING0 */
1183 return rc;
1184}
1185
1186
1187/**
1188 * Requests the mapping of a guest page given by virtual address into the current context.
1189 *
1190 * This API should only be used for very short term, as it will consume
1191 * scarse resources (R0 and GC) in the mapping cache. When you're done
1192 * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
1193 *
1194 * This API will assume your intention is to write to the page, and will
1195 * therefore replace shared and zero pages. If you do not intend to modify
1196 * the page, use the PGMPhysGCPtr2CCPtrReadOnly() API.
1197 *
1198 * @returns VBox status code.
1199 * @retval VINF_SUCCESS on success.
1200 * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
1201 * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
1202 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
1203 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
1204 *
1205 * @param pVCpu VMCPU handle.
1206 * @param GCPhys The guest physical address of the page that should be mapped.
1207 * @param ppv Where to store the address corresponding to GCPhys.
1208 * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
1209 *
1210 * @remark Avoid calling this API from within critical sections (other than
1211 * the PGM one) because of the deadlock risk.
1212 * @thread EMT
1213 */
1214VMMDECL(int) PGMPhysGCPtr2CCPtr(PVMCPU pVCpu, RTGCPTR GCPtr, void **ppv, PPGMPAGEMAPLOCK pLock)
1215{
1216 VM_ASSERT_EMT(pVCpu->CTX_SUFF(pVM));
1217 RTGCPHYS GCPhys;
1218 int rc = PGMPhysGCPtr2GCPhys(pVCpu, GCPtr, &GCPhys);
1219 if (RT_SUCCESS(rc))
1220 rc = PGMPhysGCPhys2CCPtr(pVCpu->CTX_SUFF(pVM), GCPhys, ppv, pLock);
1221 return rc;
1222}
1223
1224
1225/**
1226 * Requests the mapping of a guest page given by virtual address into the current context.
1227 *
1228 * This API should only be used for very short term, as it will consume
1229 * scarse resources (R0 and GC) in the mapping cache. When you're done
1230 * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
1231 *
1232 * @returns VBox status code.
1233 * @retval VINF_SUCCESS on success.
1234 * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
1235 * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
1236 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
1237 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
1238 *
1239 * @param pVCpu VMCPU handle.
1240 * @param GCPhys The guest physical address of the page that should be mapped.
1241 * @param ppv Where to store the address corresponding to GCPhys.
1242 * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
1243 *
1244 * @remark Avoid calling this API from within critical sections (other than
1245 * the PGM one) because of the deadlock risk.
1246 * @thread EMT
1247 */
1248VMMDECL(int) PGMPhysGCPtr2CCPtrReadOnly(PVMCPU pVCpu, RTGCPTR GCPtr, void const **ppv, PPGMPAGEMAPLOCK pLock)
1249{
1250 VM_ASSERT_EMT(pVCpu->CTX_SUFF(pVM));
1251 RTGCPHYS GCPhys;
1252 int rc = PGMPhysGCPtr2GCPhys(pVCpu, GCPtr, &GCPhys);
1253 if (RT_SUCCESS(rc))
1254 rc = PGMPhysGCPhys2CCPtrReadOnly(pVCpu->CTX_SUFF(pVM), GCPhys, ppv, pLock);
1255 return rc;
1256}
1257
1258
1259/**
1260 * Release the mapping of a guest page.
1261 *
1262 * This is the counter part of PGMPhysGCPhys2CCPtr, PGMPhysGCPhys2CCPtrReadOnly
1263 * PGMPhysGCPtr2CCPtr and PGMPhysGCPtr2CCPtrReadOnly.
1264 *
1265 * @param pVM The VM handle.
1266 * @param pLock The lock structure initialized by the mapping function.
1267 */
1268VMMDECL(void) PGMPhysReleasePageMappingLock(PVM pVM, PPGMPAGEMAPLOCK pLock)
1269{
1270#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1271 /* currently nothing to do here. */
1272 Assert(pLock->u32Dummy == UINT32_MAX);
1273 pLock->u32Dummy = 0;
1274
1275#else /* IN_RING3 */
1276 PPGMPAGEMAP pMap = (PPGMPAGEMAP)pLock->pvMap;
1277 if (!pMap)
1278 {
1279 /* The ZERO page and MMIO2 ends up here. */
1280 Assert(pLock->pvPage);
1281 pLock->pvPage = NULL;
1282 }
1283 else
1284 {
1285 pgmLock(pVM);
1286
1287# if 0 /** @todo implement page locking */
1288 PPGMPAGE pPage = (PPGMPAGE)pLock->pvPage;
1289 Assert(pPage->cLocks >= 1);
1290 if (pPage->cLocks != PGM_PAGE_MAX_LOCKS)
1291 pPage->cLocks--;
1292# endif
1293
1294 Assert(pMap->cRefs >= 1);
1295 pMap->cRefs--;
1296 pMap->iAge = 0;
1297
1298 pgmUnlock(pVM);
1299 }
1300#endif /* IN_RING3 */
1301}
1302
1303
1304/**
1305 * Converts a GC physical address to a HC ring-3 pointer.
1306 *
1307 * @returns VINF_SUCCESS on success.
1308 * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
1309 * page but has no physical backing.
1310 * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
1311 * GC physical address.
1312 * @returns VERR_PGM_GCPHYS_RANGE_CROSSES_BOUNDARY if the range crosses
1313 * a dynamic ram chunk boundary
1314 *
1315 * @param pVM The VM handle.
1316 * @param GCPhys The GC physical address to convert.
1317 * @param cbRange Physical range
1318 * @param pR3Ptr Where to store the R3 pointer on success.
1319 *
1320 * @deprecated Avoid when possible!
1321 */
1322VMMDECL(int) PGMPhysGCPhys2R3Ptr(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange, PRTR3PTR pR3Ptr)
1323{
1324/** @todo this is kind of hacky and needs some more work. */
1325#ifndef DEBUG_sandervl
1326 VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
1327#endif
1328
1329 Log(("PGMPhysGCPhys2R3Ptr(,%RGp,%#x,): dont use this API!\n", GCPhys, cbRange)); /** @todo eliminate this API! */
1330#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1331 AssertFailedReturn(VERR_NOT_IMPLEMENTED);
1332#else
1333 pgmLock(pVM);
1334
1335 PPGMRAMRANGE pRam;
1336 PPGMPAGE pPage;
1337 int rc = pgmPhysGetPageAndRangeEx(&pVM->pgm.s, GCPhys, &pPage, &pRam);
1338 if (RT_SUCCESS(rc))
1339 rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, (void **)pR3Ptr);
1340
1341 pgmUnlock(pVM);
1342 Assert(rc <= VINF_SUCCESS);
1343 return rc;
1344#endif
1345}
1346
1347
1348#ifdef VBOX_STRICT
1349/**
1350 * PGMPhysGCPhys2R3Ptr convenience for use with assertions.
1351 *
1352 * @returns The R3Ptr, NIL_RTR3PTR on failure.
1353 * @param pVM The VM handle.
1354 * @param GCPhys The GC Physical addresss.
1355 * @param cbRange Physical range.
1356 *
1357 * @deprecated Avoid when possible.
1358 */
1359VMMDECL(RTR3PTR) PGMPhysGCPhys2R3PtrAssert(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange)
1360{
1361 RTR3PTR R3Ptr;
1362 int rc = PGMPhysGCPhys2R3Ptr(pVM, GCPhys, cbRange, &R3Ptr);
1363 if (RT_SUCCESS(rc))
1364 return R3Ptr;
1365 return NIL_RTR3PTR;
1366}
1367#endif /* VBOX_STRICT */
1368
1369
1370/**
1371 * Converts a guest pointer to a GC physical address.
1372 *
1373 * This uses the current CR3/CR0/CR4 of the guest.
1374 *
1375 * @returns VBox status code.
1376 * @param pVCpu The VMCPU Handle
1377 * @param GCPtr The guest pointer to convert.
1378 * @param pGCPhys Where to store the GC physical address.
1379 */
1380VMMDECL(int) PGMPhysGCPtr2GCPhys(PVMCPU pVCpu, RTGCPTR GCPtr, PRTGCPHYS pGCPhys)
1381{
1382 int rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtr, NULL, pGCPhys);
1383 if (pGCPhys && RT_SUCCESS(rc))
1384 *pGCPhys |= (RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK;
1385 return rc;
1386}
1387
1388
1389/**
1390 * Converts a guest pointer to a HC physical address.
1391 *
1392 * This uses the current CR3/CR0/CR4 of the guest.
1393 *
1394 * @returns VBox status code.
1395 * @param pVCpu The VMCPU Handle
1396 * @param GCPtr The guest pointer to convert.
1397 * @param pHCPhys Where to store the HC physical address.
1398 */
1399VMMDECL(int) PGMPhysGCPtr2HCPhys(PVMCPU pVCpu, RTGCPTR GCPtr, PRTHCPHYS pHCPhys)
1400{
1401 PVM pVM = pVCpu->CTX_SUFF(pVM);
1402 RTGCPHYS GCPhys;
1403 int rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtr, NULL, &GCPhys);
1404 if (RT_SUCCESS(rc))
1405 rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), pHCPhys);
1406 return rc;
1407}
1408
1409
1410/**
1411 * Converts a guest pointer to a R3 pointer.
1412 *
1413 * This uses the current CR3/CR0/CR4 of the guest.
1414 *
1415 * @returns VBox status code.
1416 * @param pVCpu The VMCPU Handle
1417 * @param GCPtr The guest pointer to convert.
1418 * @param pR3Ptr Where to store the R3 virtual address.
1419 *
1420 * @deprecated Don't use this.
1421 */
1422VMMDECL(int) PGMPhysGCPtr2R3Ptr(PVMCPU pVCpu, RTGCPTR GCPtr, PRTR3PTR pR3Ptr)
1423{
1424 PVM pVM = pVCpu->CTX_SUFF(pVM);
1425 VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
1426 RTGCPHYS GCPhys;
1427 int rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtr, NULL, &GCPhys);
1428 if (RT_SUCCESS(rc))
1429 rc = PGMPhysGCPhys2R3Ptr(pVM, GCPhys | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), 1 /* we always stay within one page */, pR3Ptr);
1430 return rc;
1431}
1432
1433
1434
1435#undef LOG_GROUP
1436#define LOG_GROUP LOG_GROUP_PGM_PHYS_ACCESS
1437
1438
1439#ifdef IN_RING3
1440/**
1441 * Cache PGMPhys memory access
1442 *
1443 * @param pVM VM Handle.
1444 * @param pCache Cache structure pointer
1445 * @param GCPhys GC physical address
1446 * @param pbHC HC pointer corresponding to physical page
1447 *
1448 * @thread EMT.
1449 */
1450static void pgmPhysCacheAdd(PVM pVM, PGMPHYSCACHE *pCache, RTGCPHYS GCPhys, uint8_t *pbR3)
1451{
1452 uint32_t iCacheIndex;
1453
1454 Assert(VM_IS_EMT(pVM));
1455
1456 GCPhys = PHYS_PAGE_ADDRESS(GCPhys);
1457 pbR3 = (uint8_t *)PAGE_ADDRESS(pbR3);
1458
1459 iCacheIndex = ((GCPhys >> PAGE_SHIFT) & PGM_MAX_PHYSCACHE_ENTRIES_MASK);
1460
1461 ASMBitSet(&pCache->aEntries, iCacheIndex);
1462
1463 pCache->Entry[iCacheIndex].GCPhys = GCPhys;
1464 pCache->Entry[iCacheIndex].pbR3 = pbR3;
1465}
1466#endif /* IN_RING3 */
1467
1468
1469/**
1470 * Deals with reading from a page with one or more ALL access handlers.
1471 *
1472 * @returns VBox status code. Can be ignored in ring-3.
1473 * @retval VINF_SUCCESS.
1474 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
1475 *
1476 * @param pVM The VM handle.
1477 * @param pPage The page descriptor.
1478 * @param GCPhys The physical address to start reading at.
1479 * @param pvBuf Where to put the bits we read.
1480 * @param cb How much to read - less or equal to a page.
1481 */
1482static int pgmPhysReadHandler(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void *pvBuf, size_t cb)
1483{
1484 /*
1485 * The most frequent access here is MMIO and shadowed ROM.
1486 * The current code ASSUMES all these access handlers covers full pages!
1487 */
1488
1489 /*
1490 * Whatever we do we need the source page, map it first.
1491 */
1492 const void *pvSrc = NULL;
1493 int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, GCPhys, &pvSrc);
1494 if (RT_FAILURE(rc))
1495 {
1496 AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
1497 GCPhys, pPage, rc));
1498 memset(pvBuf, 0xff, cb);
1499 return VINF_SUCCESS;
1500 }
1501 rc = VINF_PGM_HANDLER_DO_DEFAULT;
1502
1503 /*
1504 * Deal with any physical handlers.
1505 */
1506 PPGMPHYSHANDLER pPhys = NULL;
1507 if (PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) == PGM_PAGE_HNDL_PHYS_STATE_ALL)
1508 {
1509#ifdef IN_RING3
1510 PPGMPHYSHANDLER pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1511 AssertReleaseMsg(pPhys, ("GCPhys=%RGp cb=%#x\n", GCPhys, cb));
1512 Assert(GCPhys >= pPhys->Core.Key && GCPhys <= pPhys->Core.KeyLast);
1513 Assert((pPhys->Core.Key & PAGE_OFFSET_MASK) == 0);
1514 Assert((pPhys->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
1515 Assert(pPhys->CTX_SUFF(pfnHandler));
1516
1517 PFNPGMR3PHYSHANDLER pfnHandler = pPhys->CTX_SUFF(pfnHandler);
1518 void *pvUser = pPhys->CTX_SUFF(pvUser);
1519
1520 Log5(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cb, pPage, R3STRING(pPhys->pszDesc) ));
1521 STAM_PROFILE_START(&pPhys->Stat, h);
1522 Assert(PGMIsLockOwner(pVM));
1523 /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
1524 pgmUnlock(pVM);
1525 rc = pfnHandler(pVM, GCPhys, (void *)pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, pvUser);
1526 pgmLock(pVM);
1527# ifdef VBOX_WITH_STATISTICS
1528 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1529 if (pPhys)
1530 STAM_PROFILE_STOP(&pPhys->Stat, h);
1531# else
1532 pPhys = NULL; /* might not be valid anymore. */
1533# endif
1534 AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp\n", rc, GCPhys));
1535#else
1536 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1537 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cb=%#x\n", GCPhys, cb));
1538 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1539#endif
1540 }
1541
1542 /*
1543 * Deal with any virtual handlers.
1544 */
1545 if (PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) == PGM_PAGE_HNDL_VIRT_STATE_ALL)
1546 {
1547 unsigned iPage;
1548 PPGMVIRTHANDLER pVirt;
1549
1550 int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pVirt, &iPage);
1551 AssertReleaseMsg(RT_SUCCESS(rc2), ("GCPhys=%RGp cb=%#x rc2=%Rrc\n", GCPhys, cb, rc2));
1552 Assert((pVirt->Core.Key & PAGE_OFFSET_MASK) == 0);
1553 Assert((pVirt->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
1554 Assert(GCPhys >= pVirt->aPhysToVirt[iPage].Core.Key && GCPhys <= pVirt->aPhysToVirt[iPage].Core.KeyLast);
1555
1556#ifdef IN_RING3
1557 if (pVirt->pfnHandlerR3)
1558 {
1559 if (!pPhys)
1560 Log5(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] virt %s\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc) ));
1561 else
1562 Log(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] phys/virt %s/%s\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc), R3STRING(pPhys->pszDesc) ));
1563 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
1564 + (iPage << PAGE_SHIFT)
1565 + (GCPhys & PAGE_OFFSET_MASK);
1566
1567 STAM_PROFILE_START(&pVirt->Stat, h);
1568 rc2 = pVirt->CTX_SUFF(pfnHandler)(pVM, GCPtr, (void *)pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, /*pVirt->CTX_SUFF(pvUser)*/ NULL);
1569 STAM_PROFILE_STOP(&pVirt->Stat, h);
1570 if (rc2 == VINF_SUCCESS)
1571 rc = VINF_SUCCESS;
1572 AssertLogRelMsg(rc2 == VINF_SUCCESS || rc2 == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc2, GCPhys, pPage, pVirt->pszDesc));
1573 }
1574 else
1575 Log5(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] virt %s [no handler]\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc) ));
1576#else
1577 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1578 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cb=%#x\n", GCPhys, cb));
1579 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1580#endif
1581 }
1582
1583 /*
1584 * Take the default action.
1585 */
1586 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
1587 memcpy(pvBuf, pvSrc, cb);
1588 return rc;
1589}
1590
1591
1592/**
1593 * Read physical memory.
1594 *
1595 * This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
1596 * want to ignore those.
1597 *
1598 * @returns VBox status code. Can be ignored in ring-3.
1599 * @retval VINF_SUCCESS.
1600 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
1601 *
1602 * @param pVM VM Handle.
1603 * @param GCPhys Physical address start reading from.
1604 * @param pvBuf Where to put the read bits.
1605 * @param cbRead How many bytes to read.
1606 */
1607VMMDECL(int) PGMPhysRead(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
1608{
1609 AssertMsgReturn(cbRead > 0, ("don't even think about reading zero bytes!\n"), VINF_SUCCESS);
1610 LogFlow(("PGMPhysRead: %RGp %d\n", GCPhys, cbRead));
1611
1612 STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,PhysRead));
1613 STAM_COUNTER_ADD(&pVM->pgm.s.CTX_MID_Z(Stat,PhysReadBytes), cbRead);
1614
1615 pgmLock(pVM);
1616
1617 /*
1618 * Copy loop on ram ranges.
1619 */
1620 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
1621 for (;;)
1622 {
1623 /* Find range. */
1624 while (pRam && GCPhys > pRam->GCPhysLast)
1625 pRam = pRam->CTX_SUFF(pNext);
1626 /* Inside range or not? */
1627 if (pRam && GCPhys >= pRam->GCPhys)
1628 {
1629 /*
1630 * Must work our way thru this page by page.
1631 */
1632 RTGCPHYS off = GCPhys - pRam->GCPhys;
1633 while (off < pRam->cb)
1634 {
1635 unsigned iPage = off >> PAGE_SHIFT;
1636 PPGMPAGE pPage = &pRam->aPages[iPage];
1637 size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
1638 if (cb > cbRead)
1639 cb = cbRead;
1640
1641 /*
1642 * Any ALL access handlers?
1643 */
1644 if (RT_UNLIKELY(PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage)))
1645 {
1646 int rc = pgmPhysReadHandler(pVM, pPage, pRam->GCPhys + off, pvBuf, cb);
1647 if (RT_FAILURE(rc))
1648 {
1649 pgmUnlock(pVM);
1650 return rc;
1651 }
1652 }
1653 else
1654 {
1655 /*
1656 * Get the pointer to the page.
1657 */
1658 const void *pvSrc;
1659 int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, pRam->GCPhys + off, &pvSrc);
1660 if (RT_SUCCESS(rc))
1661 memcpy(pvBuf, pvSrc, cb);
1662 else
1663 {
1664 AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
1665 pRam->GCPhys + off, pPage, rc));
1666 memset(pvBuf, 0xff, cb);
1667 }
1668 }
1669
1670 /* next page */
1671 if (cb >= cbRead)
1672 {
1673 pgmUnlock(pVM);
1674 return VINF_SUCCESS;
1675 }
1676 cbRead -= cb;
1677 off += cb;
1678 pvBuf = (char *)pvBuf + cb;
1679 } /* walk pages in ram range. */
1680
1681 GCPhys = pRam->GCPhysLast + 1;
1682 }
1683 else
1684 {
1685 LogFlow(("PGMPhysRead: Unassigned %RGp size=%u\n", GCPhys, cbRead));
1686
1687 /*
1688 * Unassigned address space.
1689 */
1690 if (!pRam)
1691 break;
1692 size_t cb = pRam->GCPhys - GCPhys;
1693 if (cb >= cbRead)
1694 {
1695 memset(pvBuf, 0xff, cbRead);
1696 break;
1697 }
1698 memset(pvBuf, 0xff, cb);
1699
1700 cbRead -= cb;
1701 pvBuf = (char *)pvBuf + cb;
1702 GCPhys += cb;
1703 }
1704 } /* Ram range walk */
1705
1706 pgmUnlock(pVM);
1707 return VINF_SUCCESS;
1708}
1709
1710
1711/**
1712 * Deals with writing to a page with one or more WRITE or ALL access handlers.
1713 *
1714 * @returns VBox status code. Can be ignored in ring-3.
1715 * @retval VINF_SUCCESS.
1716 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
1717 *
1718 * @param pVM The VM handle.
1719 * @param pPage The page descriptor.
1720 * @param GCPhys The physical address to start writing at.
1721 * @param pvBuf What to write.
1722 * @param cbWrite How much to write - less or equal to a page.
1723 */
1724static int pgmPhysWriteHandler(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void const *pvBuf, size_t cbWrite)
1725{
1726 void *pvDst = NULL;
1727 int rc;
1728
1729 /*
1730 * Give priority to physical handlers (like #PF does).
1731 *
1732 * Hope for a lonely physical handler first that covers the whole
1733 * write area. This should be a pretty frequent case with MMIO and
1734 * the heavy usage of full page handlers in the page pool.
1735 */
1736 if ( !PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage)
1737 || PGM_PAGE_IS_MMIO(pPage) /* screw virtual handlers on MMIO pages */)
1738 {
1739 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1740 if (pCur)
1741 {
1742 Assert(GCPhys >= pCur->Core.Key && GCPhys <= pCur->Core.KeyLast);
1743 Assert(pCur->CTX_SUFF(pfnHandler));
1744
1745 size_t cbRange = pCur->Core.KeyLast - GCPhys + 1;
1746 if (cbRange > cbWrite)
1747 cbRange = cbWrite;
1748
1749#ifndef IN_RING3
1750 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1751 NOREF(cbRange);
1752 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
1753 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1754
1755#else /* IN_RING3 */
1756 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pCur->pszDesc) ));
1757 if (!PGM_PAGE_IS_MMIO(pPage))
1758 rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst);
1759 else
1760 rc = VINF_SUCCESS;
1761 if (RT_SUCCESS(rc))
1762 {
1763 PFNPGMR3PHYSHANDLER pfnHandler = pCur->CTX_SUFF(pfnHandler);
1764 void *pvUser = pCur->CTX_SUFF(pvUser);
1765
1766 STAM_PROFILE_START(&pCur->Stat, h);
1767 Assert(PGMIsLockOwner(pVM));
1768 /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
1769 pgmUnlock(pVM);
1770 rc = pfnHandler(pVM, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, pvUser);
1771 pgmLock(pVM);
1772# ifdef VBOX_WITH_STATISTICS
1773 pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1774 if (pCur)
1775 STAM_PROFILE_STOP(&pCur->Stat, h);
1776# else
1777 pCur = NULL; /* might not be valid anymore. */
1778# endif
1779 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
1780 memcpy(pvDst, pvBuf, cbRange);
1781 else
1782 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 : ""));
1783 }
1784 else
1785 AssertLogRelMsgFailedReturn(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
1786 GCPhys, pPage, rc), rc);
1787 if (RT_LIKELY(cbRange == cbWrite))
1788 return VINF_SUCCESS;
1789
1790 /* more fun to be had below */
1791 cbWrite -= cbRange;
1792 GCPhys += cbRange;
1793 pvBuf = (uint8_t *)pvBuf + cbRange;
1794 pvDst = (uint8_t *)pvDst + cbRange;
1795#endif /* IN_RING3 */
1796 }
1797 /* else: the handler is somewhere else in the page, deal with it below. */
1798 Assert(!PGM_PAGE_IS_MMIO(pPage)); /* MMIO handlers are all PAGE_SIZEed! */
1799 }
1800 /*
1801 * A virtual handler without any interfering physical handlers.
1802 * Hopefully it'll conver the whole write.
1803 */
1804 else if (!PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage))
1805 {
1806 unsigned iPage;
1807 PPGMVIRTHANDLER pCur;
1808 rc = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pCur, &iPage);
1809 if (RT_SUCCESS(rc))
1810 {
1811 size_t cbRange = (PAGE_OFFSET_MASK & pCur->Core.KeyLast) - (PAGE_OFFSET_MASK & GCPhys) + 1;
1812 if (cbRange > cbWrite)
1813 cbRange = cbWrite;
1814
1815#ifndef IN_RING3
1816 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1817 NOREF(cbRange);
1818 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
1819 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1820
1821#else /* IN_RING3 */
1822
1823 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] virt %s\n", GCPhys, cbRange, pPage, R3STRING(pCur->pszDesc) ));
1824 rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst);
1825 if (RT_SUCCESS(rc))
1826 {
1827 rc = VINF_PGM_HANDLER_DO_DEFAULT;
1828 if (pCur->pfnHandlerR3)
1829 {
1830 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pCur->Core.Key & PAGE_BASE_GC_MASK)
1831 + (iPage << PAGE_SHIFT)
1832 + (GCPhys & PAGE_OFFSET_MASK);
1833
1834 STAM_PROFILE_START(&pCur->Stat, h);
1835 rc = pCur->CTX_SUFF(pfnHandler)(pVM, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, /*pCur->CTX_SUFF(pvUser)*/ NULL);
1836 STAM_PROFILE_STOP(&pCur->Stat, h);
1837 }
1838 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
1839 memcpy(pvDst, pvBuf, cbRange);
1840 else
1841 AssertLogRelMsg(rc == VINF_SUCCESS, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pCur->pszDesc));
1842 }
1843 else
1844 AssertLogRelMsgFailedReturn(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
1845 GCPhys, pPage, rc), rc);
1846 if (RT_LIKELY(cbRange == cbWrite))
1847 return VINF_SUCCESS;
1848
1849 /* more fun to be had below */
1850 cbWrite -= cbRange;
1851 GCPhys += cbRange;
1852 pvBuf = (uint8_t *)pvBuf + cbRange;
1853 pvDst = (uint8_t *)pvDst + cbRange;
1854#endif
1855 }
1856 /* else: the handler is somewhere else in the page, deal with it below. */
1857 }
1858
1859 /*
1860 * Deal with all the odd ends.
1861 */
1862
1863 /* We need a writable destination page. */
1864 if (!pvDst)
1865 {
1866 rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst);
1867 AssertLogRelMsgReturn(RT_SUCCESS(rc),
1868 ("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
1869 GCPhys, pPage, rc), rc);
1870 }
1871
1872 /* The loop state (big + ugly). */
1873 unsigned iVirtPage = 0;
1874 PPGMVIRTHANDLER pVirt = NULL;
1875 uint32_t offVirt = PAGE_SIZE;
1876 uint32_t offVirtLast = PAGE_SIZE;
1877 bool fMoreVirt = PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage);
1878
1879 PPGMPHYSHANDLER pPhys = NULL;
1880 uint32_t offPhys = PAGE_SIZE;
1881 uint32_t offPhysLast = PAGE_SIZE;
1882 bool fMorePhys = PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage);
1883
1884 /* The loop. */
1885 for (;;)
1886 {
1887 /*
1888 * Find the closest handler at or above GCPhys.
1889 */
1890 if (fMoreVirt && !pVirt)
1891 {
1892 int rc = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pVirt, &iVirtPage);
1893 if (RT_SUCCESS(rc))
1894 {
1895 offVirt = 0;
1896 offVirtLast = (pVirt->aPhysToVirt[iVirtPage].Core.KeyLast & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
1897 }
1898 else
1899 {
1900 PPGMPHYS2VIRTHANDLER pVirtPhys;
1901 pVirtPhys = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers,
1902 GCPhys, true /* fAbove */);
1903 if ( pVirtPhys
1904 && (pVirtPhys->Core.Key >> PAGE_SHIFT) == (GCPhys >> PAGE_SHIFT))
1905 {
1906 /* ASSUME that pVirtPhys only covers one page. */
1907 Assert((pVirtPhys->Core.Key >> PAGE_SHIFT) == (pVirtPhys->Core.KeyLast >> PAGE_SHIFT));
1908 Assert(pVirtPhys->Core.Key > GCPhys);
1909
1910 pVirt = (PPGMVIRTHANDLER)((uintptr_t)pVirtPhys + pVirtPhys->offVirtHandler);
1911 iVirtPage = pVirtPhys - &pVirt->aPhysToVirt[0]; Assert(iVirtPage == 0);
1912 offVirt = (pVirtPhys->Core.Key & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
1913 offVirtLast = (pVirtPhys->Core.KeyLast & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
1914 }
1915 else
1916 {
1917 pVirt = NULL;
1918 fMoreVirt = false;
1919 offVirt = offVirtLast = PAGE_SIZE;
1920 }
1921 }
1922 }
1923
1924 if (fMorePhys && !pPhys)
1925 {
1926 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1927 if (pPhys)
1928 {
1929 offPhys = 0;
1930 offPhysLast = pPhys->Core.KeyLast - GCPhys; /* ASSUMES < 4GB handlers... */
1931 }
1932 else
1933 {
1934 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers,
1935 GCPhys, true /* fAbove */);
1936 if ( pPhys
1937 && pPhys->Core.Key <= GCPhys + (cbWrite - 1))
1938 {
1939 offPhys = pPhys->Core.Key - GCPhys;
1940 offPhysLast = pPhys->Core.KeyLast - GCPhys; /* ASSUMES < 4GB handlers... */
1941 }
1942 else
1943 {
1944 pPhys = NULL;
1945 fMorePhys = false;
1946 offPhys = offPhysLast = PAGE_SIZE;
1947 }
1948 }
1949 }
1950
1951 /*
1952 * Handle access to space without handlers (that's easy).
1953 */
1954 rc = VINF_PGM_HANDLER_DO_DEFAULT;
1955 uint32_t cbRange = (uint32_t)cbWrite;
1956 if (offPhys && offVirt)
1957 {
1958 if (cbRange > offPhys)
1959 cbRange = offPhys;
1960 if (cbRange > offVirt)
1961 cbRange = offVirt;
1962 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] miss\n", GCPhys, cbRange, pPage));
1963 }
1964 /*
1965 * Physical handler.
1966 */
1967 else if (!offPhys && offVirt)
1968 {
1969 if (cbRange > offPhysLast + 1)
1970 cbRange = offPhysLast + 1;
1971 if (cbRange > offVirt)
1972 cbRange = offVirt;
1973#ifdef IN_RING3
1974 PFNPGMR3PHYSHANDLER pfnHandler = pPhys->CTX_SUFF(pfnHandler);
1975 void *pvUser = pPhys->CTX_SUFF(pvUser);
1976
1977 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pPhys->pszDesc) ));
1978 STAM_PROFILE_START(&pPhys->Stat, h);
1979 Assert(PGMIsLockOwner(pVM));
1980 /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
1981 pgmUnlock(pVM);
1982 rc = pfnHandler(pVM, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, pvUser);
1983 pgmLock(pVM);
1984# ifdef VBOX_WITH_STATISTICS
1985 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1986 if (pPhys)
1987 STAM_PROFILE_STOP(&pPhys->Stat, h);
1988# else
1989 pPhys = NULL; /* might not be valid anymore. */
1990# endif
1991 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 : ""));
1992#else
1993 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1994 NOREF(cbRange);
1995 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
1996 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1997#endif
1998 }
1999 /*
2000 * Virtual handler.
2001 */
2002 else if (offPhys && !offVirt)
2003 {
2004 if (cbRange > offVirtLast + 1)
2005 cbRange = offVirtLast + 1;
2006 if (cbRange > offPhys)
2007 cbRange = offPhys;
2008#ifdef IN_RING3
2009 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pVirt->pszDesc) ));
2010 if (pVirt->pfnHandlerR3)
2011 {
2012 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
2013 + (iVirtPage << PAGE_SHIFT)
2014 + (GCPhys & PAGE_OFFSET_MASK);
2015 STAM_PROFILE_START(&pVirt->Stat, h);
2016 rc = pVirt->CTX_SUFF(pfnHandler)(pVM, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, /*pCur->CTX_SUFF(pvUser)*/ NULL);
2017 STAM_PROFILE_STOP(&pVirt->Stat, h);
2018 AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pVirt->pszDesc));
2019 }
2020 pVirt = NULL;
2021#else
2022 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
2023 NOREF(cbRange);
2024 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
2025 return VERR_PGM_PHYS_WR_HIT_HANDLER;
2026#endif
2027 }
2028 /*
2029 * Both... give the physical one priority.
2030 */
2031 else
2032 {
2033 Assert(!offPhys && !offVirt);
2034 if (cbRange > offVirtLast + 1)
2035 cbRange = offVirtLast + 1;
2036 if (cbRange > offPhysLast + 1)
2037 cbRange = offPhysLast + 1;
2038
2039#ifdef IN_RING3
2040 if (pVirt->pfnHandlerR3)
2041 Log(("pgmPhysWriteHandler: overlapping phys and virt handlers at %RGp %R[pgmpage]; cbRange=%#x\n", GCPhys, pPage, cbRange));
2042 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys/virt %s/%s\n", GCPhys, cbRange, pPage, R3STRING(pPhys->pszDesc), R3STRING(pVirt->pszDesc) ));
2043
2044 PFNPGMR3PHYSHANDLER pfnHandler = pPhys->CTX_SUFF(pfnHandler);
2045 void *pvUser = pPhys->CTX_SUFF(pvUser);
2046
2047 STAM_PROFILE_START(&pPhys->Stat, h);
2048 Assert(PGMIsLockOwner(pVM));
2049 /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
2050 pgmUnlock(pVM);
2051 rc = pfnHandler(pVM, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, pvUser);
2052 pgmLock(pVM);
2053# ifdef VBOX_WITH_STATISTICS
2054 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
2055 if (pPhys)
2056 STAM_PROFILE_STOP(&pPhys->Stat, h);
2057# else
2058 pPhys = NULL; /* might not be valid anymore. */
2059# endif
2060 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 : ""));
2061 if (pVirt->pfnHandlerR3)
2062 {
2063
2064 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
2065 + (iVirtPage << PAGE_SHIFT)
2066 + (GCPhys & PAGE_OFFSET_MASK);
2067 STAM_PROFILE_START(&pVirt->Stat, h);
2068 int rc2 = pVirt->CTX_SUFF(pfnHandler)(pVM, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, /*pCur->CTX_SUFF(pvUser)*/ NULL);
2069 STAM_PROFILE_STOP(&pVirt->Stat, h);
2070 if (rc2 == VINF_SUCCESS && rc == VINF_PGM_HANDLER_DO_DEFAULT)
2071 rc = VINF_SUCCESS;
2072 else
2073 AssertLogRelMsg(rc2 == VINF_SUCCESS || rc2 == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pVirt->pszDesc));
2074 }
2075 pPhys = NULL;
2076 pVirt = NULL;
2077#else
2078 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
2079 NOREF(cbRange);
2080 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
2081 return VERR_PGM_PHYS_WR_HIT_HANDLER;
2082#endif
2083 }
2084 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
2085 memcpy(pvDst, pvBuf, cbRange);
2086
2087 /*
2088 * Advance if we've got more stuff to do.
2089 */
2090 if (cbRange >= cbWrite)
2091 return VINF_SUCCESS;
2092
2093 cbWrite -= cbRange;
2094 GCPhys += cbRange;
2095 pvBuf = (uint8_t *)pvBuf + cbRange;
2096 pvDst = (uint8_t *)pvDst + cbRange;
2097
2098 offPhys -= cbRange;
2099 offPhysLast -= cbRange;
2100 offVirt -= cbRange;
2101 offVirtLast -= cbRange;
2102 }
2103}
2104
2105
2106/**
2107 * Write to physical memory.
2108 *
2109 * This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
2110 * want to ignore those.
2111 *
2112 * @returns VBox status code. Can be ignored in ring-3.
2113 * @retval VINF_SUCCESS.
2114 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
2115 *
2116 * @param pVM VM Handle.
2117 * @param GCPhys Physical address to write to.
2118 * @param pvBuf What to write.
2119 * @param cbWrite How many bytes to write.
2120 */
2121VMMDECL(int) PGMPhysWrite(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite)
2122{
2123 AssertMsg(!pVM->pgm.s.fNoMorePhysWrites, ("Calling PGMPhysWrite after pgmR3Save()!\n"));
2124 AssertMsgReturn(cbWrite > 0, ("don't even think about writing zero bytes!\n"), VINF_SUCCESS);
2125 LogFlow(("PGMPhysWrite: %RGp %d\n", GCPhys, cbWrite));
2126
2127 STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,PhysWrite));
2128 STAM_COUNTER_ADD(&pVM->pgm.s.CTX_MID_Z(Stat,PhysWriteBytes), cbWrite);
2129
2130 pgmLock(pVM);
2131
2132 /*
2133 * Copy loop on ram ranges.
2134 */
2135 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
2136 for (;;)
2137 {
2138 /* Find range. */
2139 while (pRam && GCPhys > pRam->GCPhysLast)
2140 pRam = pRam->CTX_SUFF(pNext);
2141 /* Inside range or not? */
2142 if (pRam && GCPhys >= pRam->GCPhys)
2143 {
2144 /*
2145 * Must work our way thru this page by page.
2146 */
2147 RTGCPTR off = GCPhys - pRam->GCPhys;
2148 while (off < pRam->cb)
2149 {
2150 RTGCPTR iPage = off >> PAGE_SHIFT;
2151 PPGMPAGE pPage = &pRam->aPages[iPage];
2152 size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
2153 if (cb > cbWrite)
2154 cb = cbWrite;
2155
2156 /*
2157 * Any active WRITE or ALL access handlers?
2158 */
2159 if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage))
2160 {
2161 int rc = pgmPhysWriteHandler(pVM, pPage, pRam->GCPhys + off, pvBuf, cb);
2162 if (RT_FAILURE(rc))
2163 {
2164 pgmUnlock(pVM);
2165 return rc;
2166 }
2167 }
2168 else
2169 {
2170 /*
2171 * Get the pointer to the page.
2172 */
2173 void *pvDst;
2174 int rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, pRam->GCPhys + off, &pvDst);
2175 if (RT_SUCCESS(rc))
2176 memcpy(pvDst, pvBuf, cb);
2177 else
2178 AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
2179 pRam->GCPhys + off, pPage, rc));
2180 }
2181
2182 /* next page */
2183 if (cb >= cbWrite)
2184 {
2185 pgmUnlock(pVM);
2186 return VINF_SUCCESS;
2187 }
2188
2189 cbWrite -= cb;
2190 off += cb;
2191 pvBuf = (const char *)pvBuf + cb;
2192 } /* walk pages in ram range */
2193
2194 GCPhys = pRam->GCPhysLast + 1;
2195 }
2196 else
2197 {
2198 /*
2199 * Unassigned address space, skip it.
2200 */
2201 if (!pRam)
2202 break;
2203 size_t cb = pRam->GCPhys - GCPhys;
2204 if (cb >= cbWrite)
2205 break;
2206 cbWrite -= cb;
2207 pvBuf = (const char *)pvBuf + cb;
2208 GCPhys += cb;
2209 }
2210 } /* Ram range walk */
2211
2212 pgmUnlock(pVM);
2213 return VINF_SUCCESS;
2214}
2215
2216
2217/**
2218 * Read from guest physical memory by GC physical address, bypassing
2219 * MMIO and access handlers.
2220 *
2221 * @returns VBox status.
2222 * @param pVM VM handle.
2223 * @param pvDst The destination address.
2224 * @param GCPhysSrc The source address (GC physical address).
2225 * @param cb The number of bytes to read.
2226 */
2227VMMDECL(int) PGMPhysSimpleReadGCPhys(PVM pVM, void *pvDst, RTGCPHYS GCPhysSrc, size_t cb)
2228{
2229 /*
2230 * Treat the first page as a special case.
2231 */
2232 if (!cb)
2233 return VINF_SUCCESS;
2234
2235 /* map the 1st page */
2236 void const *pvSrc;
2237 PGMPAGEMAPLOCK Lock;
2238 int rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhysSrc, &pvSrc, &Lock);
2239 if (RT_FAILURE(rc))
2240 return rc;
2241
2242 /* optimize for the case where access is completely within the first page. */
2243 size_t cbPage = PAGE_SIZE - (GCPhysSrc & PAGE_OFFSET_MASK);
2244 if (RT_LIKELY(cb <= cbPage))
2245 {
2246 memcpy(pvDst, pvSrc, cb);
2247 PGMPhysReleasePageMappingLock(pVM, &Lock);
2248 return VINF_SUCCESS;
2249 }
2250
2251 /* copy to the end of the page. */
2252 memcpy(pvDst, pvSrc, cbPage);
2253 PGMPhysReleasePageMappingLock(pVM, &Lock);
2254 GCPhysSrc += cbPage;
2255 pvDst = (uint8_t *)pvDst + cbPage;
2256 cb -= cbPage;
2257
2258 /*
2259 * Page by page.
2260 */
2261 for (;;)
2262 {
2263 /* map the page */
2264 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhysSrc, &pvSrc, &Lock);
2265 if (RT_FAILURE(rc))
2266 return rc;
2267
2268 /* last page? */
2269 if (cb <= PAGE_SIZE)
2270 {
2271 memcpy(pvDst, pvSrc, cb);
2272 PGMPhysReleasePageMappingLock(pVM, &Lock);
2273 return VINF_SUCCESS;
2274 }
2275
2276 /* copy the entire page and advance */
2277 memcpy(pvDst, pvSrc, PAGE_SIZE);
2278 PGMPhysReleasePageMappingLock(pVM, &Lock);
2279 GCPhysSrc += PAGE_SIZE;
2280 pvDst = (uint8_t *)pvDst + PAGE_SIZE;
2281 cb -= PAGE_SIZE;
2282 }
2283 /* won't ever get here. */
2284}
2285
2286
2287/**
2288 * Write to guest physical memory referenced by GC pointer.
2289 * Write memory to GC physical address in guest physical memory.
2290 *
2291 * This will bypass MMIO and access handlers.
2292 *
2293 * @returns VBox status.
2294 * @param pVM VM handle.
2295 * @param GCPhysDst The GC physical address of the destination.
2296 * @param pvSrc The source buffer.
2297 * @param cb The number of bytes to write.
2298 */
2299VMMDECL(int) PGMPhysSimpleWriteGCPhys(PVM pVM, RTGCPHYS GCPhysDst, const void *pvSrc, size_t cb)
2300{
2301 LogFlow(("PGMPhysSimpleWriteGCPhys: %RGp %zu\n", GCPhysDst, cb));
2302
2303 /*
2304 * Treat the first page as a special case.
2305 */
2306 if (!cb)
2307 return VINF_SUCCESS;
2308
2309 /* map the 1st page */
2310 void *pvDst;
2311 PGMPAGEMAPLOCK Lock;
2312 int rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
2313 if (RT_FAILURE(rc))
2314 return rc;
2315
2316 /* optimize for the case where access is completely within the first page. */
2317 size_t cbPage = PAGE_SIZE - (GCPhysDst & PAGE_OFFSET_MASK);
2318 if (RT_LIKELY(cb <= cbPage))
2319 {
2320 memcpy(pvDst, pvSrc, cb);
2321 PGMPhysReleasePageMappingLock(pVM, &Lock);
2322 return VINF_SUCCESS;
2323 }
2324
2325 /* copy to the end of the page. */
2326 memcpy(pvDst, pvSrc, cbPage);
2327 PGMPhysReleasePageMappingLock(pVM, &Lock);
2328 GCPhysDst += cbPage;
2329 pvSrc = (const uint8_t *)pvSrc + cbPage;
2330 cb -= cbPage;
2331
2332 /*
2333 * Page by page.
2334 */
2335 for (;;)
2336 {
2337 /* map the page */
2338 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
2339 if (RT_FAILURE(rc))
2340 return rc;
2341
2342 /* last page? */
2343 if (cb <= PAGE_SIZE)
2344 {
2345 memcpy(pvDst, pvSrc, cb);
2346 PGMPhysReleasePageMappingLock(pVM, &Lock);
2347 return VINF_SUCCESS;
2348 }
2349
2350 /* copy the entire page and advance */
2351 memcpy(pvDst, pvSrc, PAGE_SIZE);
2352 PGMPhysReleasePageMappingLock(pVM, &Lock);
2353 GCPhysDst += PAGE_SIZE;
2354 pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
2355 cb -= PAGE_SIZE;
2356 }
2357 /* won't ever get here. */
2358}
2359
2360
2361/**
2362 * Read from guest physical memory referenced by GC pointer.
2363 *
2364 * This function uses the current CR3/CR0/CR4 of the guest and will
2365 * bypass access handlers and not set any accessed bits.
2366 *
2367 * @returns VBox status.
2368 * @param pVCpu The VMCPU handle.
2369 * @param pvDst The destination address.
2370 * @param GCPtrSrc The source address (GC pointer).
2371 * @param cb The number of bytes to read.
2372 */
2373VMMDECL(int) PGMPhysSimpleReadGCPtr(PVMCPU pVCpu, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
2374{
2375 PVM pVM = pVCpu->CTX_SUFF(pVM);
2376
2377 /*
2378 * Treat the first page as a special case.
2379 */
2380 if (!cb)
2381 return VINF_SUCCESS;
2382
2383 STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,PhysSimpleRead));
2384 STAM_COUNTER_ADD(&pVM->pgm.s.CTX_MID_Z(Stat,PhysSimpleReadBytes), cb);
2385
2386 /* Take the PGM lock here, because many called functions take the lock for a very short period. That's counter-productive
2387 * when many VCPUs are fighting for the lock.
2388 */
2389 pgmLock(pVM);
2390
2391 /* map the 1st page */
2392 void const *pvSrc;
2393 PGMPAGEMAPLOCK Lock;
2394 int rc = PGMPhysGCPtr2CCPtrReadOnly(pVCpu, GCPtrSrc, &pvSrc, &Lock);
2395 if (RT_FAILURE(rc))
2396 {
2397 pgmUnlock(pVM);
2398 return rc;
2399 }
2400
2401 /* optimize for the case where access is completely within the first page. */
2402 size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
2403 if (RT_LIKELY(cb <= cbPage))
2404 {
2405 memcpy(pvDst, pvSrc, cb);
2406 PGMPhysReleasePageMappingLock(pVM, &Lock);
2407 pgmUnlock(pVM);
2408 return VINF_SUCCESS;
2409 }
2410
2411 /* copy to the end of the page. */
2412 memcpy(pvDst, pvSrc, cbPage);
2413 PGMPhysReleasePageMappingLock(pVM, &Lock);
2414 GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + cbPage);
2415 pvDst = (uint8_t *)pvDst + cbPage;
2416 cb -= cbPage;
2417
2418 /*
2419 * Page by page.
2420 */
2421 for (;;)
2422 {
2423 /* map the page */
2424 rc = PGMPhysGCPtr2CCPtrReadOnly(pVCpu, GCPtrSrc, &pvSrc, &Lock);
2425 if (RT_FAILURE(rc))
2426 {
2427 pgmUnlock(pVM);
2428 return rc;
2429 }
2430
2431 /* last page? */
2432 if (cb <= PAGE_SIZE)
2433 {
2434 memcpy(pvDst, pvSrc, cb);
2435 PGMPhysReleasePageMappingLock(pVM, &Lock);
2436 pgmUnlock(pVM);
2437 return VINF_SUCCESS;
2438 }
2439
2440 /* copy the entire page and advance */
2441 memcpy(pvDst, pvSrc, PAGE_SIZE);
2442 PGMPhysReleasePageMappingLock(pVM, &Lock);
2443 GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + PAGE_SIZE);
2444 pvDst = (uint8_t *)pvDst + PAGE_SIZE;
2445 cb -= PAGE_SIZE;
2446 }
2447 /* won't ever get here. */
2448}
2449
2450
2451/**
2452 * Write to guest physical memory referenced by GC pointer.
2453 *
2454 * This function uses the current CR3/CR0/CR4 of the guest and will
2455 * bypass access handlers and not set dirty or accessed bits.
2456 *
2457 * @returns VBox status.
2458 * @param pVCpu The VMCPU handle.
2459 * @param GCPtrDst The destination address (GC pointer).
2460 * @param pvSrc The source address.
2461 * @param cb The number of bytes to write.
2462 */
2463VMMDECL(int) PGMPhysSimpleWriteGCPtr(PVMCPU pVCpu, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
2464{
2465 PVM pVM = pVCpu->CTX_SUFF(pVM);
2466
2467 /*
2468 * Treat the first page as a special case.
2469 */
2470 if (!cb)
2471 return VINF_SUCCESS;
2472
2473 STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,PhysSimpleWrite));
2474 STAM_COUNTER_ADD(&pVM->pgm.s.CTX_MID_Z(Stat,PhysSimpleWriteBytes), cb);
2475
2476 /* map the 1st page */
2477 void *pvDst;
2478 PGMPAGEMAPLOCK Lock;
2479 int rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
2480 if (RT_FAILURE(rc))
2481 return rc;
2482
2483 /* optimize for the case where access is completely within the first page. */
2484 size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
2485 if (RT_LIKELY(cb <= cbPage))
2486 {
2487 memcpy(pvDst, pvSrc, cb);
2488 PGMPhysReleasePageMappingLock(pVM, &Lock);
2489 return VINF_SUCCESS;
2490 }
2491
2492 /* copy to the end of the page. */
2493 memcpy(pvDst, pvSrc, cbPage);
2494 PGMPhysReleasePageMappingLock(pVM, &Lock);
2495 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
2496 pvSrc = (const uint8_t *)pvSrc + cbPage;
2497 cb -= cbPage;
2498
2499 /*
2500 * Page by page.
2501 */
2502 for (;;)
2503 {
2504 /* map the page */
2505 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
2506 if (RT_FAILURE(rc))
2507 return rc;
2508
2509 /* last page? */
2510 if (cb <= PAGE_SIZE)
2511 {
2512 memcpy(pvDst, pvSrc, cb);
2513 PGMPhysReleasePageMappingLock(pVM, &Lock);
2514 return VINF_SUCCESS;
2515 }
2516
2517 /* copy the entire page and advance */
2518 memcpy(pvDst, pvSrc, PAGE_SIZE);
2519 PGMPhysReleasePageMappingLock(pVM, &Lock);
2520 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + PAGE_SIZE);
2521 pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
2522 cb -= PAGE_SIZE;
2523 }
2524 /* won't ever get here. */
2525}
2526
2527
2528/**
2529 * Write to guest physical memory referenced by GC pointer and update the PTE.
2530 *
2531 * This function uses the current CR3/CR0/CR4 of the guest and will
2532 * bypass access handlers but will set any dirty and accessed bits in the PTE.
2533 *
2534 * If you don't want to set the dirty bit, use PGMPhysSimpleWriteGCPtr().
2535 *
2536 * @returns VBox status.
2537 * @param pVCpu The VMCPU handle.
2538 * @param GCPtrDst The destination address (GC pointer).
2539 * @param pvSrc The source address.
2540 * @param cb The number of bytes to write.
2541 */
2542VMMDECL(int) PGMPhysSimpleDirtyWriteGCPtr(PVMCPU pVCpu, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
2543{
2544 PVM pVM = pVCpu->CTX_SUFF(pVM);
2545
2546 /*
2547 * Treat the first page as a special case.
2548 * Btw. this is the same code as in PGMPhyssimpleWriteGCPtr excep for the PGMGstModifyPage.
2549 */
2550 if (!cb)
2551 return VINF_SUCCESS;
2552
2553 /* map the 1st page */
2554 void *pvDst;
2555 PGMPAGEMAPLOCK Lock;
2556 int rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
2557 if (RT_FAILURE(rc))
2558 return rc;
2559
2560 /* optimize for the case where access is completely within the first page. */
2561 size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
2562 if (RT_LIKELY(cb <= cbPage))
2563 {
2564 memcpy(pvDst, pvSrc, cb);
2565 PGMPhysReleasePageMappingLock(pVM, &Lock);
2566 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
2567 return VINF_SUCCESS;
2568 }
2569
2570 /* copy to the end of the page. */
2571 memcpy(pvDst, pvSrc, cbPage);
2572 PGMPhysReleasePageMappingLock(pVM, &Lock);
2573 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
2574 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
2575 pvSrc = (const uint8_t *)pvSrc + cbPage;
2576 cb -= cbPage;
2577
2578 /*
2579 * Page by page.
2580 */
2581 for (;;)
2582 {
2583 /* map the page */
2584 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
2585 if (RT_FAILURE(rc))
2586 return rc;
2587
2588 /* last page? */
2589 if (cb <= PAGE_SIZE)
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 the entire page and advance */
2598 memcpy(pvDst, pvSrc, PAGE_SIZE);
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 + PAGE_SIZE);
2602 pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
2603 cb -= PAGE_SIZE;
2604 }
2605 /* won't ever get here. */
2606}
2607
2608
2609/**
2610 * Read from guest physical memory referenced by GC pointer.
2611 *
2612 * This function uses the current CR3/CR0/CR4 of the guest and will
2613 * respect access handlers and set accessed bits.
2614 *
2615 * @returns VBox status.
2616 * @param pVCpu The VMCPU handle.
2617 * @param pvDst The destination address.
2618 * @param GCPtrSrc The source address (GC pointer).
2619 * @param cb The number of bytes to read.
2620 * @thread The vCPU EMT.
2621 */
2622VMMDECL(int) PGMPhysReadGCPtr(PVMCPU pVCpu, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
2623{
2624 RTGCPHYS GCPhys;
2625 uint64_t fFlags;
2626 int rc;
2627 PVM pVM = pVCpu->CTX_SUFF(pVM);
2628
2629 /*
2630 * Anything to do?
2631 */
2632 if (!cb)
2633 return VINF_SUCCESS;
2634
2635 LogFlow(("PGMPhysReadGCPtr: %RGv %zu\n", GCPtrSrc, cb));
2636
2637 /*
2638 * Optimize reads within a single page.
2639 */
2640 if (((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
2641 {
2642 /* Convert virtual to physical address + flags */
2643 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrSrc, &fFlags, &GCPhys);
2644 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrSrc), rc);
2645 GCPhys |= (RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK;
2646
2647 /* mark the guest page as accessed. */
2648 if (!(fFlags & X86_PTE_A))
2649 {
2650 rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
2651 AssertRC(rc);
2652 }
2653
2654 return PGMPhysRead(pVM, GCPhys, pvDst, cb);
2655 }
2656
2657 /*
2658 * Page by page.
2659 */
2660 for (;;)
2661 {
2662 /* Convert virtual to physical address + flags */
2663 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrSrc, &fFlags, &GCPhys);
2664 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrSrc), rc);
2665 GCPhys |= (RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK;
2666
2667 /* mark the guest page as accessed. */
2668 if (!(fFlags & X86_PTE_A))
2669 {
2670 rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
2671 AssertRC(rc);
2672 }
2673
2674 /* copy */
2675 size_t cbRead = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
2676 rc = PGMPhysRead(pVM, GCPhys, pvDst, cbRead);
2677 if (cbRead >= cb || RT_FAILURE(rc))
2678 return rc;
2679
2680 /* next */
2681 cb -= cbRead;
2682 pvDst = (uint8_t *)pvDst + cbRead;
2683 GCPtrSrc += cbRead;
2684 }
2685}
2686
2687
2688/**
2689 * Write to guest physical memory referenced by GC pointer.
2690 *
2691 * This function uses the current CR3/CR0/CR4 of the guest and will
2692 * respect access handlers and set dirty and accessed bits.
2693 *
2694 * @returns VBox status.
2695 * @retval VINF_SUCCESS.
2696 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
2697 *
2698 * @param pVCpu The VMCPU handle.
2699 * @param GCPtrDst The destination address (GC pointer).
2700 * @param pvSrc The source address.
2701 * @param cb The number of bytes to write.
2702 */
2703VMMDECL(int) PGMPhysWriteGCPtr(PVMCPU pVCpu, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
2704{
2705 RTGCPHYS GCPhys;
2706 uint64_t fFlags;
2707 int rc;
2708 PVM pVM = pVCpu->CTX_SUFF(pVM);
2709
2710 /*
2711 * Anything to do?
2712 */
2713 if (!cb)
2714 return VINF_SUCCESS;
2715
2716 LogFlow(("PGMPhysWriteGCPtr: %RGv %zu\n", GCPtrDst, cb));
2717
2718 /*
2719 * Optimize writes within a single page.
2720 */
2721 if (((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
2722 {
2723 /* Convert virtual to physical address + flags */
2724 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrDst, &fFlags, &GCPhys);
2725 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrDst), rc);
2726 GCPhys |= (RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK;
2727
2728 /* Mention when we ignore X86_PTE_RW... */
2729 if (!(fFlags & X86_PTE_RW))
2730 Log(("PGMPhysGCPtr2GCPhys: Writing to RO page %RGv %#x\n", GCPtrDst, cb));
2731
2732 /* Mark the guest page as accessed and dirty if necessary. */
2733 if ((fFlags & (X86_PTE_A | X86_PTE_D)) != (X86_PTE_A | X86_PTE_D))
2734 {
2735 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
2736 AssertRC(rc);
2737 }
2738
2739 return PGMPhysWrite(pVM, GCPhys, pvSrc, cb);
2740 }
2741
2742 /*
2743 * Page by page.
2744 */
2745 for (;;)
2746 {
2747 /* Convert virtual to physical address + flags */
2748 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrDst, &fFlags, &GCPhys);
2749 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrDst), rc);
2750 GCPhys |= (RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK;
2751
2752 /* Mention when we ignore X86_PTE_RW... */
2753 if (!(fFlags & X86_PTE_RW))
2754 Log(("PGMPhysGCPtr2GCPhys: Writing to RO page %RGv %#x\n", GCPtrDst, cb));
2755
2756 /* Mark the guest page as accessed and dirty if necessary. */
2757 if ((fFlags & (X86_PTE_A | X86_PTE_D)) != (X86_PTE_A | X86_PTE_D))
2758 {
2759 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
2760 AssertRC(rc);
2761 }
2762
2763 /* copy */
2764 size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
2765 int rc = PGMPhysWrite(pVM, GCPhys, pvSrc, cbWrite);
2766 if (cbWrite >= cb || RT_FAILURE(rc))
2767 return rc;
2768
2769 /* next */
2770 cb -= cbWrite;
2771 pvSrc = (uint8_t *)pvSrc + cbWrite;
2772 GCPtrDst += cbWrite;
2773 }
2774}
2775
2776
2777/**
2778 * Performs a read of guest virtual memory for instruction emulation.
2779 *
2780 * This will check permissions, raise exceptions and update the access bits.
2781 *
2782 * The current implementation will bypass all access handlers. It may later be
2783 * changed to at least respect MMIO.
2784 *
2785 *
2786 * @returns VBox status code suitable to scheduling.
2787 * @retval VINF_SUCCESS if the read was performed successfully.
2788 * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
2789 * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
2790 *
2791 * @param pVCpu The VMCPU handle.
2792 * @param pCtxCore The context core.
2793 * @param pvDst Where to put the bytes we've read.
2794 * @param GCPtrSrc The source address.
2795 * @param cb The number of bytes to read. Not more than a page.
2796 *
2797 * @remark This function will dynamically map physical pages in GC. This may unmap
2798 * mappings done by the caller. Be careful!
2799 */
2800VMMDECL(int) PGMPhysInterpretedRead(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb)
2801{
2802 PVM pVM = pVCpu->CTX_SUFF(pVM);
2803 Assert(cb <= PAGE_SIZE);
2804
2805/** @todo r=bird: This isn't perfect!
2806 * -# It's not checking for reserved bits being 1.
2807 * -# It's not correctly dealing with the access bit.
2808 * -# It's not respecting MMIO memory or any other access handlers.
2809 */
2810 /*
2811 * 1. Translate virtual to physical. This may fault.
2812 * 2. Map the physical address.
2813 * 3. Do the read operation.
2814 * 4. Set access bits if required.
2815 */
2816 int rc;
2817 unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
2818 if (cb <= cb1)
2819 {
2820 /*
2821 * Not crossing pages.
2822 */
2823 RTGCPHYS GCPhys;
2824 uint64_t fFlags;
2825 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags, &GCPhys);
2826 if (RT_SUCCESS(rc))
2827 {
2828 /** @todo we should check reserved bits ... */
2829 void *pvSrc;
2830 rc = PGM_GCPHYS_2_PTR(pVM, GCPhys, &pvSrc);
2831 switch (rc)
2832 {
2833 case VINF_SUCCESS:
2834 Log(("PGMPhysInterpretedRead: pvDst=%p pvSrc=%p cb=%d\n", pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb));
2835 memcpy(pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
2836 break;
2837 case VERR_PGM_PHYS_PAGE_RESERVED:
2838 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
2839 memset(pvDst, 0, cb); /** @todo this is wrong, it should be 0xff */
2840 break;
2841 default:
2842 return rc;
2843 }
2844
2845 /** @todo access bit emulation isn't 100% correct. */
2846 if (!(fFlags & X86_PTE_A))
2847 {
2848 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
2849 AssertRC(rc);
2850 }
2851 return VINF_SUCCESS;
2852 }
2853 }
2854 else
2855 {
2856 /*
2857 * Crosses pages.
2858 */
2859 size_t cb2 = cb - cb1;
2860 uint64_t fFlags1;
2861 RTGCPHYS GCPhys1;
2862 uint64_t fFlags2;
2863 RTGCPHYS GCPhys2;
2864 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags1, &GCPhys1);
2865 if (RT_SUCCESS(rc))
2866 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
2867 if (RT_SUCCESS(rc))
2868 {
2869 /** @todo we should check reserved bits ... */
2870 AssertMsgFailed(("cb=%d cb1=%d cb2=%d GCPtrSrc=%RGv\n", cb, cb1, cb2, GCPtrSrc));
2871 void *pvSrc1;
2872 rc = PGM_GCPHYS_2_PTR(pVM, GCPhys1, &pvSrc1);
2873 switch (rc)
2874 {
2875 case VINF_SUCCESS:
2876 memcpy(pvDst, (uint8_t *)pvSrc1 + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
2877 break;
2878 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
2879 memset(pvDst, 0, cb1); /** @todo this is wrong, it should be 0xff */
2880 break;
2881 default:
2882 return rc;
2883 }
2884
2885 void *pvSrc2;
2886 rc = PGM_GCPHYS_2_PTR(pVM, GCPhys2, &pvSrc2);
2887 switch (rc)
2888 {
2889 case VINF_SUCCESS:
2890 memcpy((uint8_t *)pvDst + cb1, pvSrc2, cb2);
2891 break;
2892 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
2893 memset((uint8_t *)pvDst + cb1, 0, cb2); /** @todo this is wrong, it should be 0xff */
2894 break;
2895 default:
2896 return rc;
2897 }
2898
2899 if (!(fFlags1 & X86_PTE_A))
2900 {
2901 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
2902 AssertRC(rc);
2903 }
2904 if (!(fFlags2 & X86_PTE_A))
2905 {
2906 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
2907 AssertRC(rc);
2908 }
2909 return VINF_SUCCESS;
2910 }
2911 }
2912
2913 /*
2914 * Raise a #PF.
2915 */
2916 uint32_t uErr;
2917
2918 /* Get the current privilege level. */
2919 uint32_t cpl = CPUMGetGuestCPL(pVCpu, pCtxCore);
2920 switch (rc)
2921 {
2922 case VINF_SUCCESS:
2923 uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
2924 break;
2925
2926 case VERR_PAGE_NOT_PRESENT:
2927 case VERR_PAGE_TABLE_NOT_PRESENT:
2928 uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
2929 break;
2930
2931 default:
2932 AssertMsgFailed(("rc=%Rrc GCPtrSrc=%RGv cb=%#x\n", rc, GCPtrSrc, cb));
2933 return rc;
2934 }
2935 Log(("PGMPhysInterpretedRead: GCPtrSrc=%RGv cb=%#x -> #PF(%#x)\n", GCPtrSrc, cb, uErr));
2936 return TRPMRaiseXcptErrCR2(pVCpu, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
2937}
2938
2939
2940/**
2941 * Performs a read of guest virtual memory for instruction emulation.
2942 *
2943 * This will check permissions, raise exceptions and update the access bits.
2944 *
2945 * The current implementation will bypass all access handlers. It may later be
2946 * changed to at least respect MMIO.
2947 *
2948 *
2949 * @returns VBox status code suitable to scheduling.
2950 * @retval VINF_SUCCESS if the read was performed successfully.
2951 * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
2952 * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
2953 *
2954 * @param pVCpu The VMCPU handle.
2955 * @param pCtxCore The context core.
2956 * @param pvDst Where to put the bytes we've read.
2957 * @param GCPtrSrc The source address.
2958 * @param cb The number of bytes to read. Not more than a page.
2959 * @param fRaiseTrap If set the trap will be raised on as per spec, if clear
2960 * an appropriate error status will be returned (no
2961 * informational at all).
2962 *
2963 *
2964 * @remarks Takes the PGM lock.
2965 * @remarks A page fault on the 2nd page of the access will be raised without
2966 * writing the bits on the first page since we're ASSUMING that the
2967 * caller is emulating an instruction access.
2968 * @remarks This function will dynamically map physical pages in GC. This may
2969 * unmap mappings done by the caller. Be careful!
2970 */
2971VMMDECL(int) PGMPhysInterpretedReadNoHandlers(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb, bool fRaiseTrap)
2972{
2973 PVM pVM = pVCpu->CTX_SUFF(pVM);
2974 Assert(cb <= PAGE_SIZE);
2975
2976 /*
2977 * 1. Translate virtual to physical. This may fault.
2978 * 2. Map the physical address.
2979 * 3. Do the read operation.
2980 * 4. Set access bits if required.
2981 */
2982 int rc;
2983 unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
2984 if (cb <= cb1)
2985 {
2986 /*
2987 * Not crossing pages.
2988 */
2989 RTGCPHYS GCPhys;
2990 uint64_t fFlags;
2991 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags, &GCPhys);
2992 if (RT_SUCCESS(rc))
2993 {
2994 if (1) /** @todo we should check reserved bits ... */
2995 {
2996 const void *pvSrc;
2997 PGMPAGEMAPLOCK Lock;
2998 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys, &pvSrc, &Lock);
2999 switch (rc)
3000 {
3001 case VINF_SUCCESS:
3002 Log(("PGMPhysInterpretedReadNoHandlers: pvDst=%p pvSrc=%p (%RGv) cb=%d\n",
3003 pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), GCPtrSrc, cb));
3004 memcpy(pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
3005 break;
3006 case VERR_PGM_PHYS_PAGE_RESERVED:
3007 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3008 memset(pvDst, 0xff, cb);
3009 break;
3010 default:
3011 AssertMsgFailed(("%Rrc\n", rc));
3012 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3013 return rc;
3014 }
3015 PGMPhysReleasePageMappingLock(pVM, &Lock);
3016
3017 if (!(fFlags & X86_PTE_A))
3018 {
3019 /** @todo access bit emulation isn't 100% correct. */
3020 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
3021 AssertRC(rc);
3022 }
3023 return VINF_SUCCESS;
3024 }
3025 }
3026 }
3027 else
3028 {
3029 /*
3030 * Crosses pages.
3031 */
3032 size_t cb2 = cb - cb1;
3033 uint64_t fFlags1;
3034 RTGCPHYS GCPhys1;
3035 uint64_t fFlags2;
3036 RTGCPHYS GCPhys2;
3037 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags1, &GCPhys1);
3038 if (RT_SUCCESS(rc))
3039 {
3040 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
3041 if (RT_SUCCESS(rc))
3042 {
3043 if (1) /** @todo we should check reserved bits ... */
3044 {
3045 const void *pvSrc;
3046 PGMPAGEMAPLOCK Lock;
3047 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys1, &pvSrc, &Lock);
3048 switch (rc)
3049 {
3050 case VINF_SUCCESS:
3051 Log(("PGMPhysInterpretedReadNoHandlers: pvDst=%p pvSrc=%p (%RGv) cb=%d [2]\n",
3052 pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), GCPtrSrc, cb1));
3053 memcpy(pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
3054 PGMPhysReleasePageMappingLock(pVM, &Lock);
3055 break;
3056 case VERR_PGM_PHYS_PAGE_RESERVED:
3057 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3058 memset(pvDst, 0xff, cb1);
3059 break;
3060 default:
3061 AssertMsgFailed(("%Rrc\n", rc));
3062 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3063 return rc;
3064 }
3065
3066 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys2, &pvSrc, &Lock);
3067 switch (rc)
3068 {
3069 case VINF_SUCCESS:
3070 memcpy((uint8_t *)pvDst + cb1, pvSrc, cb2);
3071 PGMPhysReleasePageMappingLock(pVM, &Lock);
3072 break;
3073 case VERR_PGM_PHYS_PAGE_RESERVED:
3074 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3075 memset((uint8_t *)pvDst + cb1, 0xff, cb2);
3076 break;
3077 default:
3078 AssertMsgFailed(("%Rrc\n", rc));
3079 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3080 return rc;
3081 }
3082
3083 if (!(fFlags1 & X86_PTE_A))
3084 {
3085 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
3086 AssertRC(rc);
3087 }
3088 if (!(fFlags2 & X86_PTE_A))
3089 {
3090 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
3091 AssertRC(rc);
3092 }
3093 return VINF_SUCCESS;
3094 }
3095 /* sort out which page */
3096 }
3097 else
3098 GCPtrSrc += cb1; /* fault on 2nd page */
3099 }
3100 }
3101
3102 /*
3103 * Raise a #PF if we're allowed to do that.
3104 */
3105 /* Calc the error bits. */
3106 uint32_t cpl = CPUMGetGuestCPL(pVCpu, pCtxCore);
3107 uint32_t uErr;
3108 switch (rc)
3109 {
3110 case VINF_SUCCESS:
3111 uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
3112 rc = VERR_ACCESS_DENIED;
3113 break;
3114
3115 case VERR_PAGE_NOT_PRESENT:
3116 case VERR_PAGE_TABLE_NOT_PRESENT:
3117 uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
3118 break;
3119
3120 default:
3121 AssertMsgFailed(("rc=%Rrc GCPtrSrc=%RGv cb=%#x\n", rc, GCPtrSrc, cb));
3122 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3123 return rc;
3124 }
3125 if (fRaiseTrap)
3126 {
3127 Log(("PGMPhysInterpretedReadNoHandlers: GCPtrSrc=%RGv cb=%#x -> Raised #PF(%#x)\n", GCPtrSrc, cb, uErr));
3128 return TRPMRaiseXcptErrCR2(pVCpu, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
3129 }
3130 Log(("PGMPhysInterpretedReadNoHandlers: GCPtrSrc=%RGv cb=%#x -> #PF(%#x) [!raised]\n", GCPtrSrc, cb, uErr));
3131 return rc;
3132}
3133
3134
3135/**
3136 * Performs a write to guest virtual memory for instruction emulation.
3137 *
3138 * This will check permissions, raise exceptions and update the dirty and access
3139 * bits.
3140 *
3141 * @returns VBox status code suitable to scheduling.
3142 * @retval VINF_SUCCESS if the read was performed successfully.
3143 * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
3144 * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
3145 *
3146 * @param pVCpu The VMCPU handle.
3147 * @param pCtxCore The context core.
3148 * @param GCPtrDst The destination address.
3149 * @param pvSrc What to write.
3150 * @param cb The number of bytes to write. Not more than a page.
3151 * @param fRaiseTrap If set the trap will be raised on as per spec, if clear
3152 * an appropriate error status will be returned (no
3153 * informational at all).
3154 *
3155 * @remarks Takes the PGM lock.
3156 * @remarks A page fault on the 2nd page of the access will be raised without
3157 * writing the bits on the first page since we're ASSUMING that the
3158 * caller is emulating an instruction access.
3159 * @remarks This function will dynamically map physical pages in GC. This may
3160 * unmap mappings done by the caller. Be careful!
3161 */
3162VMMDECL(int) PGMPhysInterpretedWriteNoHandlers(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb, bool fRaiseTrap)
3163{
3164 Assert(cb <= PAGE_SIZE);
3165 PVM pVM = pVCpu->CTX_SUFF(pVM);
3166
3167 /*
3168 * 1. Translate virtual to physical. This may fault.
3169 * 2. Map the physical address.
3170 * 3. Do the write operation.
3171 * 4. Set access bits if required.
3172 */
3173 int rc;
3174 unsigned cb1 = PAGE_SIZE - (GCPtrDst & PAGE_OFFSET_MASK);
3175 if (cb <= cb1)
3176 {
3177 /*
3178 * Not crossing pages.
3179 */
3180 RTGCPHYS GCPhys;
3181 uint64_t fFlags;
3182 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrDst, &fFlags, &GCPhys);
3183 if (RT_SUCCESS(rc))
3184 {
3185 if ( (fFlags & X86_PTE_RW) /** @todo Also check reserved bits. */
3186 || ( !(CPUMGetGuestCR0(pVCpu) & X86_CR0_WP)
3187 && CPUMGetGuestCPL(pVCpu, pCtxCore) <= 2) ) /** @todo it's 2, right? Check cpl check below as well. */
3188 {
3189 void *pvDst;
3190 PGMPAGEMAPLOCK Lock;
3191 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys, &pvDst, &Lock);
3192 switch (rc)
3193 {
3194 case VINF_SUCCESS:
3195 Log(("PGMPhysInterpretedWriteNoHandlers: pvDst=%p (%RGv) pvSrc=%p cb=%d\n",
3196 (uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), GCPtrDst, pvSrc, cb));
3197 memcpy((uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), pvSrc, cb);
3198 PGMPhysReleasePageMappingLock(pVM, &Lock);
3199 break;
3200 case VERR_PGM_PHYS_PAGE_RESERVED:
3201 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3202 /* bit bucket */
3203 break;
3204 default:
3205 AssertMsgFailed(("%Rrc\n", rc));
3206 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3207 return rc;
3208 }
3209
3210 if (!(fFlags & (X86_PTE_A | X86_PTE_D)))
3211 {
3212 /** @todo dirty & access bit emulation isn't 100% correct. */
3213 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
3214 AssertRC(rc);
3215 }
3216 return VINF_SUCCESS;
3217 }
3218 rc = VERR_ACCESS_DENIED;
3219 }
3220 }
3221 else
3222 {
3223 /*
3224 * Crosses pages.
3225 */
3226 size_t cb2 = cb - cb1;
3227 uint64_t fFlags1;
3228 RTGCPHYS GCPhys1;
3229 uint64_t fFlags2;
3230 RTGCPHYS GCPhys2;
3231 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrDst, &fFlags1, &GCPhys1);
3232 if (RT_SUCCESS(rc))
3233 {
3234 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrDst + cb1, &fFlags2, &GCPhys2);
3235 if (RT_SUCCESS(rc))
3236 {
3237 if ( ( (fFlags1 & X86_PTE_RW) /** @todo Also check reserved bits. */
3238 && (fFlags2 & X86_PTE_RW))
3239 || ( !(CPUMGetGuestCR0(pVCpu) & X86_CR0_WP)
3240 && CPUMGetGuestCPL(pVCpu, pCtxCore) <= 2) )
3241 {
3242 void *pvDst;
3243 PGMPAGEMAPLOCK Lock;
3244 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys1, &pvDst, &Lock);
3245 switch (rc)
3246 {
3247 case VINF_SUCCESS:
3248 Log(("PGMPhysInterpretedWriteNoHandlers: pvDst=%p (%RGv) pvSrc=%p cb=%d\n",
3249 (uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), GCPtrDst, pvSrc, cb1));
3250 memcpy((uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), pvSrc, cb1);
3251 PGMPhysReleasePageMappingLock(pVM, &Lock);
3252 break;
3253 case VERR_PGM_PHYS_PAGE_RESERVED:
3254 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3255 /* bit bucket */
3256 break;
3257 default:
3258 AssertMsgFailed(("%Rrc\n", rc));
3259 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3260 return rc;
3261 }
3262
3263 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys2, &pvDst, &Lock);
3264 switch (rc)
3265 {
3266 case VINF_SUCCESS:
3267 memcpy(pvDst, (const uint8_t *)pvSrc + cb1, cb2);
3268 PGMPhysReleasePageMappingLock(pVM, &Lock);
3269 break;
3270 case VERR_PGM_PHYS_PAGE_RESERVED:
3271 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3272 /* bit bucket */
3273 break;
3274 default:
3275 AssertMsgFailed(("%Rrc\n", rc));
3276 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3277 return rc;
3278 }
3279
3280 if (!(fFlags1 & (X86_PTE_A | X86_PTE_RW)))
3281 {
3282 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrDst, 1, (X86_PTE_A | X86_PTE_RW), ~(uint64_t)(X86_PTE_A | X86_PTE_RW));
3283 AssertRC(rc);
3284 }
3285 if (!(fFlags2 & (X86_PTE_A | X86_PTE_RW)))
3286 {
3287 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrDst + cb1, 1, (X86_PTE_A | X86_PTE_RW), ~(uint64_t)(X86_PTE_A | X86_PTE_RW));
3288 AssertRC(rc);
3289 }
3290 return VINF_SUCCESS;
3291 }
3292 if ((fFlags1 & (X86_PTE_RW)) == X86_PTE_RW)
3293 GCPtrDst += cb1; /* fault on the 2nd page. */
3294 rc = VERR_ACCESS_DENIED;
3295 }
3296 else
3297 GCPtrDst += cb1; /* fault on the 2nd page. */
3298 }
3299 }
3300
3301 /*
3302 * Raise a #PF if we're allowed to do that.
3303 */
3304 /* Calc the error bits. */
3305 uint32_t uErr;
3306 uint32_t cpl = CPUMGetGuestCPL(pVCpu, pCtxCore);
3307 switch (rc)
3308 {
3309 case VINF_SUCCESS:
3310 uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
3311 rc = VERR_ACCESS_DENIED;
3312 break;
3313
3314 case VERR_ACCESS_DENIED:
3315 uErr = (cpl >= 2) ? X86_TRAP_PF_RW | X86_TRAP_PF_US : X86_TRAP_PF_RW;
3316 break;
3317
3318 case VERR_PAGE_NOT_PRESENT:
3319 case VERR_PAGE_TABLE_NOT_PRESENT:
3320 uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
3321 break;
3322
3323 default:
3324 AssertMsgFailed(("rc=%Rrc GCPtrDst=%RGv cb=%#x\n", rc, GCPtrDst, cb));
3325 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3326 return rc;
3327 }
3328 if (fRaiseTrap)
3329 {
3330 Log(("PGMPhysInterpretedWriteNoHandlers: GCPtrDst=%RGv cb=%#x -> Raised #PF(%#x)\n", GCPtrDst, cb, uErr));
3331 return TRPMRaiseXcptErrCR2(pVCpu, pCtxCore, X86_XCPT_PF, uErr, GCPtrDst);
3332 }
3333 Log(("PGMPhysInterpretedWriteNoHandlers: GCPtrDst=%RGv cb=%#x -> #PF(%#x) [!raised]\n", GCPtrDst, cb, uErr));
3334 return rc;
3335}
3336
3337
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