VirtualBox

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

Last change on this file since 20776 was 20770, checked in by vboxsync, 16 years ago

More paranoia

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