VirtualBox

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

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

Unsafe physical hander usage.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 113.1 KB
Line 
1/* $Id: PGMAllPhys.cpp 20768 2009-06-22 12:15:30Z 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 Log5(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cb, pPage, R3STRING(pPhys->pszDesc) ));
1421 STAM_PROFILE_START(&pPhys->Stat, h);
1422 Assert(PGMIsLockOwner(pVM));
1423 /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
1424 pgmUnlock(pVM);
1425 rc = pPhys->CTX_SUFF(pfnHandler)(pVM, GCPhys, (void *)pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, pPhys->CTX_SUFF(pvUser));
1426 pgmLock(pVM);
1427# ifdef VBOX_WITH_STATISTICS
1428 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1429 if (pPhys)
1430 STAM_PROFILE_STOP(&pPhys->Stat, h);
1431# else
1432 pPhys = NULL; /* might not be valid anymore. */
1433# endif
1434 AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp\n", rc, GCPhys));
1435#else
1436 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1437 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cb=%#x\n", GCPhys, cb));
1438 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1439#endif
1440 }
1441
1442 /*
1443 * Deal with any virtual handlers.
1444 */
1445 if (PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) == PGM_PAGE_HNDL_VIRT_STATE_ALL)
1446 {
1447 unsigned iPage;
1448 PPGMVIRTHANDLER pVirt;
1449
1450 int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pVirt, &iPage);
1451 AssertReleaseMsg(RT_SUCCESS(rc2), ("GCPhys=%RGp cb=%#x rc2=%Rrc\n", GCPhys, cb, rc2));
1452 Assert((pVirt->Core.Key & PAGE_OFFSET_MASK) == 0);
1453 Assert((pVirt->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
1454 Assert(GCPhys >= pVirt->aPhysToVirt[iPage].Core.Key && GCPhys <= pVirt->aPhysToVirt[iPage].Core.KeyLast);
1455
1456#ifdef IN_RING3
1457 if (pVirt->pfnHandlerR3)
1458 {
1459 if (!pPhys)
1460 Log5(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] virt %s\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc) ));
1461 else
1462 Log(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] phys/virt %s/%s\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc), R3STRING(pPhys->pszDesc) ));
1463 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
1464 + (iPage << PAGE_SHIFT)
1465 + (GCPhys & PAGE_OFFSET_MASK);
1466
1467 STAM_PROFILE_START(&pVirt->Stat, h);
1468 rc2 = pVirt->CTX_SUFF(pfnHandler)(pVM, GCPtr, (void *)pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, /*pVirt->CTX_SUFF(pvUser)*/ NULL);
1469 STAM_PROFILE_STOP(&pVirt->Stat, h);
1470 if (rc2 == VINF_SUCCESS)
1471 rc = VINF_SUCCESS;
1472 AssertLogRelMsg(rc2 == VINF_SUCCESS || rc2 == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc2, GCPhys, pPage, pVirt->pszDesc));
1473 }
1474 else
1475 Log5(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] virt %s [no handler]\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc) ));
1476#else
1477 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1478 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cb=%#x\n", GCPhys, cb));
1479 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1480#endif
1481 }
1482
1483 /*
1484 * Take the default action.
1485 */
1486 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
1487 memcpy(pvBuf, pvSrc, cb);
1488 return rc;
1489}
1490
1491
1492/**
1493 * Read physical memory.
1494 *
1495 * This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
1496 * want to ignore those.
1497 *
1498 * @returns VBox status code. Can be ignored in ring-3.
1499 * @retval VINF_SUCCESS.
1500 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
1501 *
1502 * @param pVM VM Handle.
1503 * @param GCPhys Physical address start reading from.
1504 * @param pvBuf Where to put the read bits.
1505 * @param cbRead How many bytes to read.
1506 */
1507VMMDECL(int) PGMPhysRead(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
1508{
1509 AssertMsgReturn(cbRead > 0, ("don't even think about reading zero bytes!\n"), VINF_SUCCESS);
1510 LogFlow(("PGMPhysRead: %RGp %d\n", GCPhys, cbRead));
1511
1512 pgmLock(pVM);
1513
1514 /*
1515 * Copy loop on ram ranges.
1516 */
1517 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
1518 for (;;)
1519 {
1520 /* Find range. */
1521 while (pRam && GCPhys > pRam->GCPhysLast)
1522 pRam = pRam->CTX_SUFF(pNext);
1523 /* Inside range or not? */
1524 if (pRam && GCPhys >= pRam->GCPhys)
1525 {
1526 /*
1527 * Must work our way thru this page by page.
1528 */
1529 RTGCPHYS off = GCPhys - pRam->GCPhys;
1530 while (off < pRam->cb)
1531 {
1532 unsigned iPage = off >> PAGE_SHIFT;
1533 PPGMPAGE pPage = &pRam->aPages[iPage];
1534 size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
1535 if (cb > cbRead)
1536 cb = cbRead;
1537
1538 /*
1539 * Any ALL access handlers?
1540 */
1541 if (RT_UNLIKELY(PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage)))
1542 {
1543 int rc = pgmPhysReadHandler(pVM, pPage, pRam->GCPhys + off, pvBuf, cb);
1544 if (RT_FAILURE(rc))
1545 {
1546 pgmUnlock(pVM);
1547 return rc;
1548 }
1549 }
1550 else
1551 {
1552 /*
1553 * Get the pointer to the page.
1554 */
1555 const void *pvSrc;
1556 int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, pRam->GCPhys + off, &pvSrc);
1557 if (RT_SUCCESS(rc))
1558 memcpy(pvBuf, pvSrc, cb);
1559 else
1560 {
1561 AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
1562 pRam->GCPhys + off, pPage, rc));
1563 memset(pvBuf, 0xff, cb);
1564 }
1565 }
1566
1567 /* next page */
1568 if (cb >= cbRead)
1569 {
1570 pgmUnlock(pVM);
1571 return VINF_SUCCESS;
1572 }
1573 cbRead -= cb;
1574 off += cb;
1575 pvBuf = (char *)pvBuf + cb;
1576 } /* walk pages in ram range. */
1577
1578 GCPhys = pRam->GCPhysLast + 1;
1579 }
1580 else
1581 {
1582 LogFlow(("PGMPhysRead: Unassigned %RGp size=%u\n", GCPhys, cbRead));
1583
1584 /*
1585 * Unassigned address space.
1586 */
1587 if (!pRam)
1588 break;
1589 size_t cb = pRam->GCPhys - GCPhys;
1590 if (cb >= cbRead)
1591 {
1592 memset(pvBuf, 0xff, cbRead);
1593 break;
1594 }
1595 memset(pvBuf, 0xff, cb);
1596
1597 cbRead -= cb;
1598 pvBuf = (char *)pvBuf + cb;
1599 GCPhys += cb;
1600 }
1601 } /* Ram range walk */
1602
1603 pgmUnlock(pVM);
1604 return VINF_SUCCESS;
1605}
1606
1607
1608/**
1609 * Deals with writing to a page with one or more WRITE or ALL access handlers.
1610 *
1611 * @returns VBox status code. Can be ignored in ring-3.
1612 * @retval VINF_SUCCESS.
1613 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
1614 *
1615 * @param pVM The VM handle.
1616 * @param pPage The page descriptor.
1617 * @param GCPhys The physical address to start writing at.
1618 * @param pvBuf What to write.
1619 * @param cbWrite How much to write - less or equal to a page.
1620 */
1621static int pgmPhysWriteHandler(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void const *pvBuf, size_t cbWrite)
1622{
1623 void *pvDst = NULL;
1624 int rc;
1625
1626 /*
1627 * Give priority to physical handlers (like #PF does).
1628 *
1629 * Hope for a lonely physical handler first that covers the whole
1630 * write area. This should be a pretty frequent case with MMIO and
1631 * the heavy usage of full page handlers in the page pool.
1632 */
1633 if ( !PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage)
1634 || PGM_PAGE_IS_MMIO(pPage) /* screw virtual handlers on MMIO pages */)
1635 {
1636 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1637 if (pCur)
1638 {
1639 Assert(GCPhys >= pCur->Core.Key && GCPhys <= pCur->Core.KeyLast);
1640 Assert(pCur->CTX_SUFF(pfnHandler));
1641
1642 size_t cbRange = pCur->Core.KeyLast - GCPhys + 1;
1643 if (cbRange > cbWrite)
1644 cbRange = cbWrite;
1645
1646#ifndef IN_RING3
1647 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1648 NOREF(cbRange);
1649 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
1650 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1651
1652#else /* IN_RING3 */
1653 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pCur->pszDesc) ));
1654 if (!PGM_PAGE_IS_MMIO(pPage))
1655 rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst);
1656 else
1657 rc = VINF_SUCCESS;
1658 if (RT_SUCCESS(rc))
1659 {
1660 STAM_PROFILE_START(&pCur->Stat, h);
1661 Assert(PGMIsLockOwner(pVM));
1662 /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
1663 pgmUnlock(pVM);
1664 rc = pCur->CTX_SUFF(pfnHandler)(pVM, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, pCur->CTX_SUFF(pvUser));
1665 pgmLock(pVM);
1666# ifdef VBOX_WITH_STATISTICS
1667 pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1668 if (pCur)
1669 STAM_PROFILE_STOP(&pCur->Stat, h);
1670# else
1671 pCur = NULL; /* might not be valid anymore. */
1672# endif
1673 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
1674 memcpy(pvDst, pvBuf, cbRange);
1675 else
1676 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 : ""));
1677 }
1678 else
1679 AssertLogRelMsgFailedReturn(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
1680 GCPhys, pPage, rc), rc);
1681 if (RT_LIKELY(cbRange == cbWrite))
1682 return VINF_SUCCESS;
1683
1684 /* more fun to be had below */
1685 cbWrite -= cbRange;
1686 GCPhys += cbRange;
1687 pvBuf = (uint8_t *)pvBuf + cbRange;
1688 pvDst = (uint8_t *)pvDst + cbRange;
1689#endif /* IN_RING3 */
1690 }
1691 /* else: the handler is somewhere else in the page, deal with it below. */
1692 Assert(!PGM_PAGE_IS_MMIO(pPage)); /* MMIO handlers are all PAGE_SIZEed! */
1693 }
1694 /*
1695 * A virtual handler without any interfering physical handlers.
1696 * Hopefully it'll conver the whole write.
1697 */
1698 else if (!PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage))
1699 {
1700 unsigned iPage;
1701 PPGMVIRTHANDLER pCur;
1702 rc = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pCur, &iPage);
1703 if (RT_SUCCESS(rc))
1704 {
1705 size_t cbRange = (PAGE_OFFSET_MASK & pCur->Core.KeyLast) - (PAGE_OFFSET_MASK & GCPhys) + 1;
1706 if (cbRange > cbWrite)
1707 cbRange = cbWrite;
1708
1709#ifndef IN_RING3
1710 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1711 NOREF(cbRange);
1712 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
1713 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1714
1715#else /* IN_RING3 */
1716
1717 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] virt %s\n", GCPhys, cbRange, pPage, R3STRING(pCur->pszDesc) ));
1718 rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst);
1719 if (RT_SUCCESS(rc))
1720 {
1721 rc = VINF_PGM_HANDLER_DO_DEFAULT;
1722 if (pCur->pfnHandlerR3)
1723 {
1724 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pCur->Core.Key & PAGE_BASE_GC_MASK)
1725 + (iPage << PAGE_SHIFT)
1726 + (GCPhys & PAGE_OFFSET_MASK);
1727
1728 STAM_PROFILE_START(&pCur->Stat, h);
1729 rc = pCur->CTX_SUFF(pfnHandler)(pVM, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, /*pCur->CTX_SUFF(pvUser)*/ NULL);
1730 STAM_PROFILE_STOP(&pCur->Stat, h);
1731 }
1732 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
1733 memcpy(pvDst, pvBuf, cbRange);
1734 else
1735 AssertLogRelMsg(rc == VINF_SUCCESS, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pCur->pszDesc));
1736 }
1737 else
1738 AssertLogRelMsgFailedReturn(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
1739 GCPhys, pPage, rc), rc);
1740 if (RT_LIKELY(cbRange == cbWrite))
1741 return VINF_SUCCESS;
1742
1743 /* more fun to be had below */
1744 cbWrite -= cbRange;
1745 GCPhys += cbRange;
1746 pvBuf = (uint8_t *)pvBuf + cbRange;
1747 pvDst = (uint8_t *)pvDst + cbRange;
1748#endif
1749 }
1750 /* else: the handler is somewhere else in the page, deal with it below. */
1751 }
1752
1753 /*
1754 * Deal with all the odd ends.
1755 */
1756
1757 /* We need a writable destination page. */
1758 if (!pvDst)
1759 {
1760 rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst);
1761 AssertLogRelMsgReturn(RT_SUCCESS(rc),
1762 ("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
1763 GCPhys, pPage, rc), rc);
1764 }
1765
1766 /* The loop state (big + ugly). */
1767 unsigned iVirtPage = 0;
1768 PPGMVIRTHANDLER pVirt = NULL;
1769 uint32_t offVirt = PAGE_SIZE;
1770 uint32_t offVirtLast = PAGE_SIZE;
1771 bool fMoreVirt = PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage);
1772
1773 PPGMPHYSHANDLER pPhys = NULL;
1774 uint32_t offPhys = PAGE_SIZE;
1775 uint32_t offPhysLast = PAGE_SIZE;
1776 bool fMorePhys = PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage);
1777
1778 /* The loop. */
1779 for (;;)
1780 {
1781 /*
1782 * Find the closest handler at or above GCPhys.
1783 */
1784 if (fMoreVirt && !pVirt)
1785 {
1786 int rc = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pVirt, &iVirtPage);
1787 if (RT_SUCCESS(rc))
1788 {
1789 offVirt = 0;
1790 offVirtLast = (pVirt->aPhysToVirt[iVirtPage].Core.KeyLast & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
1791 }
1792 else
1793 {
1794 PPGMPHYS2VIRTHANDLER pVirtPhys;
1795 pVirtPhys = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers,
1796 GCPhys, true /* fAbove */);
1797 if ( pVirtPhys
1798 && (pVirtPhys->Core.Key >> PAGE_SHIFT) == (GCPhys >> PAGE_SHIFT))
1799 {
1800 /* ASSUME that pVirtPhys only covers one page. */
1801 Assert((pVirtPhys->Core.Key >> PAGE_SHIFT) == (pVirtPhys->Core.KeyLast >> PAGE_SHIFT));
1802 Assert(pVirtPhys->Core.Key > GCPhys);
1803
1804 pVirt = (PPGMVIRTHANDLER)((uintptr_t)pVirtPhys + pVirtPhys->offVirtHandler);
1805 iVirtPage = pVirtPhys - &pVirt->aPhysToVirt[0]; Assert(iVirtPage == 0);
1806 offVirt = (pVirtPhys->Core.Key & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
1807 offVirtLast = (pVirtPhys->Core.KeyLast & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
1808 }
1809 else
1810 {
1811 pVirt = NULL;
1812 fMoreVirt = false;
1813 offVirt = offVirtLast = PAGE_SIZE;
1814 }
1815 }
1816 }
1817
1818 if (fMorePhys && !pPhys)
1819 {
1820 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1821 if (pPhys)
1822 {
1823 offPhys = 0;
1824 offPhysLast = pPhys->Core.KeyLast - GCPhys; /* ASSUMES < 4GB handlers... */
1825 }
1826 else
1827 {
1828 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers,
1829 GCPhys, true /* fAbove */);
1830 if ( pPhys
1831 && pPhys->Core.Key <= GCPhys + (cbWrite - 1))
1832 {
1833 offPhys = pPhys->Core.Key - GCPhys;
1834 offPhysLast = pPhys->Core.KeyLast - GCPhys; /* ASSUMES < 4GB handlers... */
1835 }
1836 else
1837 {
1838 pPhys = NULL;
1839 fMorePhys = false;
1840 offPhys = offPhysLast = PAGE_SIZE;
1841 }
1842 }
1843 }
1844
1845 /*
1846 * Handle access to space without handlers (that's easy).
1847 */
1848 rc = VINF_PGM_HANDLER_DO_DEFAULT;
1849 uint32_t cbRange = (uint32_t)cbWrite;
1850 if (offPhys && offVirt)
1851 {
1852 if (cbRange > offPhys)
1853 cbRange = offPhys;
1854 if (cbRange > offVirt)
1855 cbRange = offVirt;
1856 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] miss\n", GCPhys, cbRange, pPage));
1857 }
1858 /*
1859 * Physical handler.
1860 */
1861 else if (!offPhys && offVirt)
1862 {
1863 if (cbRange > offPhysLast + 1)
1864 cbRange = offPhysLast + 1;
1865 if (cbRange > offVirt)
1866 cbRange = offVirt;
1867#ifdef IN_RING3
1868 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pPhys->pszDesc) ));
1869 STAM_PROFILE_START(&pPhys->Stat, h);
1870 Assert(PGMIsLockOwner(pVM));
1871 /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
1872 pgmUnlock(pVM);
1873 rc = pPhys->CTX_SUFF(pfnHandler)(pVM, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, pPhys->CTX_SUFF(pvUser));
1874 pgmLock(pVM);
1875# ifdef VBOX_WITH_STATISTICS
1876 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1877 if (pPhys)
1878 STAM_PROFILE_STOP(&pPhys->Stat, h);
1879# else
1880 pPhys = NULL; /* might not be valid anymore. */
1881# endif
1882 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 : ""));
1883#else
1884 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1885 NOREF(cbRange);
1886 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
1887 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1888#endif
1889 }
1890 /*
1891 * Virtual handler.
1892 */
1893 else if (offPhys && !offVirt)
1894 {
1895 if (cbRange > offVirtLast + 1)
1896 cbRange = offVirtLast + 1;
1897 if (cbRange > offPhys)
1898 cbRange = offPhys;
1899#ifdef IN_RING3
1900 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pVirt->pszDesc) ));
1901 if (pVirt->pfnHandlerR3)
1902 {
1903 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
1904 + (iVirtPage << PAGE_SHIFT)
1905 + (GCPhys & PAGE_OFFSET_MASK);
1906 STAM_PROFILE_START(&pVirt->Stat, h);
1907 rc = pVirt->CTX_SUFF(pfnHandler)(pVM, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, /*pCur->CTX_SUFF(pvUser)*/ NULL);
1908 STAM_PROFILE_STOP(&pVirt->Stat, h);
1909 AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pVirt->pszDesc));
1910 }
1911 pVirt = NULL;
1912#else
1913 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1914 NOREF(cbRange);
1915 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
1916 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1917#endif
1918 }
1919 /*
1920 * Both... give the physical one priority.
1921 */
1922 else
1923 {
1924 Assert(!offPhys && !offVirt);
1925 if (cbRange > offVirtLast + 1)
1926 cbRange = offVirtLast + 1;
1927 if (cbRange > offPhysLast + 1)
1928 cbRange = offPhysLast + 1;
1929
1930#ifdef IN_RING3
1931 if (pVirt->pfnHandlerR3)
1932 Log(("pgmPhysWriteHandler: overlapping phys and virt handlers at %RGp %R[pgmpage]; cbRange=%#x\n", GCPhys, pPage, cbRange));
1933 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys/virt %s/%s\n", GCPhys, cbRange, pPage, R3STRING(pPhys->pszDesc), R3STRING(pVirt->pszDesc) ));
1934
1935 STAM_PROFILE_START(&pPhys->Stat, h);
1936 Assert(PGMIsLockOwner(pVM));
1937 /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
1938 pgmUnlock(pVM);
1939 rc = pPhys->CTX_SUFF(pfnHandler)(pVM, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, pPhys->CTX_SUFF(pvUser));
1940 pgmLock(pVM);
1941# ifdef VBOX_WITH_STATISTICS
1942 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1943 if (pPhys)
1944 STAM_PROFILE_STOP(&pPhys->Stat, h);
1945# else
1946 pPhys = NULL; /* might not be valid anymore. */
1947# endif
1948 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 : ""));
1949 if (pVirt->pfnHandlerR3)
1950 {
1951
1952 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
1953 + (iVirtPage << PAGE_SHIFT)
1954 + (GCPhys & PAGE_OFFSET_MASK);
1955 STAM_PROFILE_START(&pVirt->Stat, h);
1956 int rc2 = pVirt->CTX_SUFF(pfnHandler)(pVM, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, /*pCur->CTX_SUFF(pvUser)*/ NULL);
1957 STAM_PROFILE_STOP(&pVirt->Stat, h);
1958 if (rc2 == VINF_SUCCESS && rc == VINF_PGM_HANDLER_DO_DEFAULT)
1959 rc = VINF_SUCCESS;
1960 else
1961 AssertLogRelMsg(rc2 == VINF_SUCCESS || rc2 == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pVirt->pszDesc));
1962 }
1963 pPhys = NULL;
1964 pVirt = NULL;
1965#else
1966 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1967 NOREF(cbRange);
1968 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
1969 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1970#endif
1971 }
1972 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
1973 memcpy(pvDst, pvBuf, cbRange);
1974
1975 /*
1976 * Advance if we've got more stuff to do.
1977 */
1978 if (cbRange >= cbWrite)
1979 return VINF_SUCCESS;
1980
1981 cbWrite -= cbRange;
1982 GCPhys += cbRange;
1983 pvBuf = (uint8_t *)pvBuf + cbRange;
1984 pvDst = (uint8_t *)pvDst + cbRange;
1985
1986 offPhys -= cbRange;
1987 offPhysLast -= cbRange;
1988 offVirt -= cbRange;
1989 offVirtLast -= cbRange;
1990 }
1991}
1992
1993
1994/**
1995 * Write to physical memory.
1996 *
1997 * This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
1998 * want to ignore those.
1999 *
2000 * @returns VBox status code. Can be ignored in ring-3.
2001 * @retval VINF_SUCCESS.
2002 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
2003 *
2004 * @param pVM VM Handle.
2005 * @param GCPhys Physical address to write to.
2006 * @param pvBuf What to write.
2007 * @param cbWrite How many bytes to write.
2008 */
2009VMMDECL(int) PGMPhysWrite(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite)
2010{
2011 AssertMsg(!pVM->pgm.s.fNoMorePhysWrites, ("Calling PGMPhysWrite after pgmR3Save()!\n"));
2012 AssertMsgReturn(cbWrite > 0, ("don't even think about writing zero bytes!\n"), VINF_SUCCESS);
2013 LogFlow(("PGMPhysWrite: %RGp %d\n", GCPhys, cbWrite));
2014
2015 pgmLock(pVM);
2016
2017 /*
2018 * Copy loop on ram ranges.
2019 */
2020 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
2021 for (;;)
2022 {
2023 /* Find range. */
2024 while (pRam && GCPhys > pRam->GCPhysLast)
2025 pRam = pRam->CTX_SUFF(pNext);
2026 /* Inside range or not? */
2027 if (pRam && GCPhys >= pRam->GCPhys)
2028 {
2029 /*
2030 * Must work our way thru this page by page.
2031 */
2032 RTGCPTR off = GCPhys - pRam->GCPhys;
2033 while (off < pRam->cb)
2034 {
2035 RTGCPTR iPage = off >> PAGE_SHIFT;
2036 PPGMPAGE pPage = &pRam->aPages[iPage];
2037 size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
2038 if (cb > cbWrite)
2039 cb = cbWrite;
2040
2041 /*
2042 * Any active WRITE or ALL access handlers?
2043 */
2044 if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage))
2045 {
2046 int rc = pgmPhysWriteHandler(pVM, pPage, pRam->GCPhys + off, pvBuf, cb);
2047 if (RT_FAILURE(rc))
2048 {
2049 pgmUnlock(pVM);
2050 return rc;
2051 }
2052 }
2053 else
2054 {
2055 /*
2056 * Get the pointer to the page.
2057 */
2058 void *pvDst;
2059 int rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, pRam->GCPhys + off, &pvDst);
2060 if (RT_SUCCESS(rc))
2061 memcpy(pvDst, pvBuf, cb);
2062 else
2063 AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
2064 pRam->GCPhys + off, pPage, rc));
2065 }
2066
2067 /* next page */
2068 if (cb >= cbWrite)
2069 {
2070 pgmUnlock(pVM);
2071 return VINF_SUCCESS;
2072 }
2073
2074 cbWrite -= cb;
2075 off += cb;
2076 pvBuf = (const char *)pvBuf + cb;
2077 } /* walk pages in ram range */
2078
2079 GCPhys = pRam->GCPhysLast + 1;
2080 }
2081 else
2082 {
2083 /*
2084 * Unassigned address space, skip it.
2085 */
2086 if (!pRam)
2087 break;
2088 size_t cb = pRam->GCPhys - GCPhys;
2089 if (cb >= cbWrite)
2090 break;
2091 cbWrite -= cb;
2092 pvBuf = (const char *)pvBuf + cb;
2093 GCPhys += cb;
2094 }
2095 } /* Ram range walk */
2096
2097 pgmUnlock(pVM);
2098 return VINF_SUCCESS;
2099}
2100
2101
2102/**
2103 * Read from guest physical memory by GC physical address, bypassing
2104 * MMIO and access handlers.
2105 *
2106 * @returns VBox status.
2107 * @param pVM VM handle.
2108 * @param pvDst The destination address.
2109 * @param GCPhysSrc The source address (GC physical address).
2110 * @param cb The number of bytes to read.
2111 */
2112VMMDECL(int) PGMPhysSimpleReadGCPhys(PVM pVM, void *pvDst, RTGCPHYS GCPhysSrc, size_t cb)
2113{
2114 /*
2115 * Treat the first page as a special case.
2116 */
2117 if (!cb)
2118 return VINF_SUCCESS;
2119
2120 /* map the 1st page */
2121 void const *pvSrc;
2122 PGMPAGEMAPLOCK Lock;
2123 int rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhysSrc, &pvSrc, &Lock);
2124 if (RT_FAILURE(rc))
2125 return rc;
2126
2127 /* optimize for the case where access is completely within the first page. */
2128 size_t cbPage = PAGE_SIZE - (GCPhysSrc & PAGE_OFFSET_MASK);
2129 if (RT_LIKELY(cb <= cbPage))
2130 {
2131 memcpy(pvDst, pvSrc, cb);
2132 PGMPhysReleasePageMappingLock(pVM, &Lock);
2133 return VINF_SUCCESS;
2134 }
2135
2136 /* copy to the end of the page. */
2137 memcpy(pvDst, pvSrc, cbPage);
2138 PGMPhysReleasePageMappingLock(pVM, &Lock);
2139 GCPhysSrc += cbPage;
2140 pvDst = (uint8_t *)pvDst + cbPage;
2141 cb -= cbPage;
2142
2143 /*
2144 * Page by page.
2145 */
2146 for (;;)
2147 {
2148 /* map the page */
2149 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhysSrc, &pvSrc, &Lock);
2150 if (RT_FAILURE(rc))
2151 return rc;
2152
2153 /* last page? */
2154 if (cb <= PAGE_SIZE)
2155 {
2156 memcpy(pvDst, pvSrc, cb);
2157 PGMPhysReleasePageMappingLock(pVM, &Lock);
2158 return VINF_SUCCESS;
2159 }
2160
2161 /* copy the entire page and advance */
2162 memcpy(pvDst, pvSrc, PAGE_SIZE);
2163 PGMPhysReleasePageMappingLock(pVM, &Lock);
2164 GCPhysSrc += PAGE_SIZE;
2165 pvDst = (uint8_t *)pvDst + PAGE_SIZE;
2166 cb -= PAGE_SIZE;
2167 }
2168 /* won't ever get here. */
2169}
2170
2171
2172/**
2173 * Write to guest physical memory referenced by GC pointer.
2174 * Write memory to GC physical address in guest physical memory.
2175 *
2176 * This will bypass MMIO and access handlers.
2177 *
2178 * @returns VBox status.
2179 * @param pVM VM handle.
2180 * @param GCPhysDst The GC physical address of the destination.
2181 * @param pvSrc The source buffer.
2182 * @param cb The number of bytes to write.
2183 */
2184VMMDECL(int) PGMPhysSimpleWriteGCPhys(PVM pVM, RTGCPHYS GCPhysDst, const void *pvSrc, size_t cb)
2185{
2186 LogFlow(("PGMPhysSimpleWriteGCPhys: %RGp %zu\n", GCPhysDst, cb));
2187
2188 /*
2189 * Treat the first page as a special case.
2190 */
2191 if (!cb)
2192 return VINF_SUCCESS;
2193
2194 /* map the 1st page */
2195 void *pvDst;
2196 PGMPAGEMAPLOCK Lock;
2197 int rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
2198 if (RT_FAILURE(rc))
2199 return rc;
2200
2201 /* optimize for the case where access is completely within the first page. */
2202 size_t cbPage = PAGE_SIZE - (GCPhysDst & PAGE_OFFSET_MASK);
2203 if (RT_LIKELY(cb <= cbPage))
2204 {
2205 memcpy(pvDst, pvSrc, cb);
2206 PGMPhysReleasePageMappingLock(pVM, &Lock);
2207 return VINF_SUCCESS;
2208 }
2209
2210 /* copy to the end of the page. */
2211 memcpy(pvDst, pvSrc, cbPage);
2212 PGMPhysReleasePageMappingLock(pVM, &Lock);
2213 GCPhysDst += cbPage;
2214 pvSrc = (const uint8_t *)pvSrc + cbPage;
2215 cb -= cbPage;
2216
2217 /*
2218 * Page by page.
2219 */
2220 for (;;)
2221 {
2222 /* map the page */
2223 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
2224 if (RT_FAILURE(rc))
2225 return rc;
2226
2227 /* last page? */
2228 if (cb <= PAGE_SIZE)
2229 {
2230 memcpy(pvDst, pvSrc, cb);
2231 PGMPhysReleasePageMappingLock(pVM, &Lock);
2232 return VINF_SUCCESS;
2233 }
2234
2235 /* copy the entire page and advance */
2236 memcpy(pvDst, pvSrc, PAGE_SIZE);
2237 PGMPhysReleasePageMappingLock(pVM, &Lock);
2238 GCPhysDst += PAGE_SIZE;
2239 pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
2240 cb -= PAGE_SIZE;
2241 }
2242 /* won't ever get here. */
2243}
2244
2245
2246/**
2247 * Read from guest physical memory referenced by GC pointer.
2248 *
2249 * This function uses the current CR3/CR0/CR4 of the guest and will
2250 * bypass access handlers and not set any accessed bits.
2251 *
2252 * @returns VBox status.
2253 * @param pVCpu The VMCPU handle.
2254 * @param pvDst The destination address.
2255 * @param GCPtrSrc The source address (GC pointer).
2256 * @param cb The number of bytes to read.
2257 */
2258VMMDECL(int) PGMPhysSimpleReadGCPtr(PVMCPU pVCpu, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
2259{
2260 PVM pVM = pVCpu->CTX_SUFF(pVM);
2261
2262 /*
2263 * Treat the first page as a special case.
2264 */
2265 if (!cb)
2266 return VINF_SUCCESS;
2267
2268 /* Take the PGM lock here, because many called functions take the lock for a very short period. That's counter-productive
2269 * when many VCPUs are fighting for the lock.
2270 */
2271 pgmLock(pVM);
2272
2273 /* map the 1st page */
2274 void const *pvSrc;
2275 PGMPAGEMAPLOCK Lock;
2276 int rc = PGMPhysGCPtr2CCPtrReadOnly(pVCpu, GCPtrSrc, &pvSrc, &Lock);
2277 if (RT_FAILURE(rc))
2278 {
2279 pgmUnlock(pVM);
2280 return rc;
2281 }
2282
2283 /* optimize for the case where access is completely within the first page. */
2284 size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
2285 if (RT_LIKELY(cb <= cbPage))
2286 {
2287 memcpy(pvDst, pvSrc, cb);
2288 PGMPhysReleasePageMappingLock(pVM, &Lock);
2289 pgmUnlock(pVM);
2290 return VINF_SUCCESS;
2291 }
2292
2293 /* copy to the end of the page. */
2294 memcpy(pvDst, pvSrc, cbPage);
2295 PGMPhysReleasePageMappingLock(pVM, &Lock);
2296 GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + cbPage);
2297 pvDst = (uint8_t *)pvDst + cbPage;
2298 cb -= cbPage;
2299
2300 /*
2301 * Page by page.
2302 */
2303 for (;;)
2304 {
2305 /* map the page */
2306 rc = PGMPhysGCPtr2CCPtrReadOnly(pVCpu, GCPtrSrc, &pvSrc, &Lock);
2307 if (RT_FAILURE(rc))
2308 {
2309 pgmUnlock(pVM);
2310 return rc;
2311 }
2312
2313 /* last page? */
2314 if (cb <= PAGE_SIZE)
2315 {
2316 memcpy(pvDst, pvSrc, cb);
2317 PGMPhysReleasePageMappingLock(pVM, &Lock);
2318 pgmUnlock(pVM);
2319 return VINF_SUCCESS;
2320 }
2321
2322 /* copy the entire page and advance */
2323 memcpy(pvDst, pvSrc, PAGE_SIZE);
2324 PGMPhysReleasePageMappingLock(pVM, &Lock);
2325 GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + PAGE_SIZE);
2326 pvDst = (uint8_t *)pvDst + PAGE_SIZE;
2327 cb -= PAGE_SIZE;
2328 }
2329 /* won't ever get here. */
2330}
2331
2332
2333/**
2334 * Write to guest physical memory referenced by GC pointer.
2335 *
2336 * This function uses the current CR3/CR0/CR4 of the guest and will
2337 * bypass access handlers and not set dirty or accessed bits.
2338 *
2339 * @returns VBox status.
2340 * @param pVCpu The VMCPU handle.
2341 * @param GCPtrDst The destination address (GC pointer).
2342 * @param pvSrc The source address.
2343 * @param cb The number of bytes to write.
2344 */
2345VMMDECL(int) PGMPhysSimpleWriteGCPtr(PVMCPU pVCpu, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
2346{
2347 PVM pVM = pVCpu->CTX_SUFF(pVM);
2348
2349 /*
2350 * Treat the first page as a special case.
2351 */
2352 if (!cb)
2353 return VINF_SUCCESS;
2354
2355 /* map the 1st page */
2356 void *pvDst;
2357 PGMPAGEMAPLOCK Lock;
2358 int rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
2359 if (RT_FAILURE(rc))
2360 return rc;
2361
2362 /* optimize for the case where access is completely within the first page. */
2363 size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
2364 if (RT_LIKELY(cb <= cbPage))
2365 {
2366 memcpy(pvDst, pvSrc, cb);
2367 PGMPhysReleasePageMappingLock(pVM, &Lock);
2368 return VINF_SUCCESS;
2369 }
2370
2371 /* copy to the end of the page. */
2372 memcpy(pvDst, pvSrc, cbPage);
2373 PGMPhysReleasePageMappingLock(pVM, &Lock);
2374 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
2375 pvSrc = (const uint8_t *)pvSrc + cbPage;
2376 cb -= cbPage;
2377
2378 /*
2379 * Page by page.
2380 */
2381 for (;;)
2382 {
2383 /* map the page */
2384 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
2385 if (RT_FAILURE(rc))
2386 return rc;
2387
2388 /* last page? */
2389 if (cb <= PAGE_SIZE)
2390 {
2391 memcpy(pvDst, pvSrc, cb);
2392 PGMPhysReleasePageMappingLock(pVM, &Lock);
2393 return VINF_SUCCESS;
2394 }
2395
2396 /* copy the entire page and advance */
2397 memcpy(pvDst, pvSrc, PAGE_SIZE);
2398 PGMPhysReleasePageMappingLock(pVM, &Lock);
2399 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + PAGE_SIZE);
2400 pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
2401 cb -= PAGE_SIZE;
2402 }
2403 /* won't ever get here. */
2404}
2405
2406
2407/**
2408 * Write to guest physical memory referenced by GC pointer and update the PTE.
2409 *
2410 * This function uses the current CR3/CR0/CR4 of the guest and will
2411 * bypass access handlers but will set any dirty and accessed bits in the PTE.
2412 *
2413 * If you don't want to set the dirty bit, use PGMPhysSimpleWriteGCPtr().
2414 *
2415 * @returns VBox status.
2416 * @param pVCpu The VMCPU handle.
2417 * @param GCPtrDst The destination address (GC pointer).
2418 * @param pvSrc The source address.
2419 * @param cb The number of bytes to write.
2420 */
2421VMMDECL(int) PGMPhysSimpleDirtyWriteGCPtr(PVMCPU pVCpu, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
2422{
2423 PVM pVM = pVCpu->CTX_SUFF(pVM);
2424
2425 /*
2426 * Treat the first page as a special case.
2427 * Btw. this is the same code as in PGMPhyssimpleWriteGCPtr excep for the PGMGstModifyPage.
2428 */
2429 if (!cb)
2430 return VINF_SUCCESS;
2431
2432 /* map the 1st page */
2433 void *pvDst;
2434 PGMPAGEMAPLOCK Lock;
2435 int rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
2436 if (RT_FAILURE(rc))
2437 return rc;
2438
2439 /* optimize for the case where access is completely within the first page. */
2440 size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
2441 if (RT_LIKELY(cb <= cbPage))
2442 {
2443 memcpy(pvDst, pvSrc, cb);
2444 PGMPhysReleasePageMappingLock(pVM, &Lock);
2445 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
2446 return VINF_SUCCESS;
2447 }
2448
2449 /* copy to the end of the page. */
2450 memcpy(pvDst, pvSrc, cbPage);
2451 PGMPhysReleasePageMappingLock(pVM, &Lock);
2452 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
2453 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
2454 pvSrc = (const uint8_t *)pvSrc + cbPage;
2455 cb -= cbPage;
2456
2457 /*
2458 * Page by page.
2459 */
2460 for (;;)
2461 {
2462 /* map the page */
2463 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
2464 if (RT_FAILURE(rc))
2465 return rc;
2466
2467 /* last page? */
2468 if (cb <= PAGE_SIZE)
2469 {
2470 memcpy(pvDst, pvSrc, cb);
2471 PGMPhysReleasePageMappingLock(pVM, &Lock);
2472 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
2473 return VINF_SUCCESS;
2474 }
2475
2476 /* copy the entire page and advance */
2477 memcpy(pvDst, pvSrc, PAGE_SIZE);
2478 PGMPhysReleasePageMappingLock(pVM, &Lock);
2479 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
2480 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + PAGE_SIZE);
2481 pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
2482 cb -= PAGE_SIZE;
2483 }
2484 /* won't ever get here. */
2485}
2486
2487
2488/**
2489 * Read from guest physical memory referenced by GC pointer.
2490 *
2491 * This function uses the current CR3/CR0/CR4 of the guest and will
2492 * respect access handlers and set accessed bits.
2493 *
2494 * @returns VBox status.
2495 * @param pVCpu The VMCPU handle.
2496 * @param pvDst The destination address.
2497 * @param GCPtrSrc The source address (GC pointer).
2498 * @param cb The number of bytes to read.
2499 * @thread The vCPU EMT.
2500 */
2501VMMDECL(int) PGMPhysReadGCPtr(PVMCPU pVCpu, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
2502{
2503 RTGCPHYS GCPhys;
2504 uint64_t fFlags;
2505 int rc;
2506 PVM pVM = pVCpu->CTX_SUFF(pVM);
2507
2508 /*
2509 * Anything to do?
2510 */
2511 if (!cb)
2512 return VINF_SUCCESS;
2513
2514 LogFlow(("PGMPhysReadGCPtr: %RGv %zu\n", GCPtrSrc, cb));
2515
2516 /*
2517 * Optimize reads within a single page.
2518 */
2519 if (((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
2520 {
2521 /* Convert virtual to physical address + flags */
2522 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrSrc, &fFlags, &GCPhys);
2523 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrSrc), rc);
2524 GCPhys |= (RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK;
2525
2526 /* mark the guest page as accessed. */
2527 if (!(fFlags & X86_PTE_A))
2528 {
2529 rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
2530 AssertRC(rc);
2531 }
2532
2533 return PGMPhysRead(pVM, GCPhys, pvDst, cb);
2534 }
2535
2536 /*
2537 * Page by page.
2538 */
2539 for (;;)
2540 {
2541 /* Convert virtual to physical address + flags */
2542 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrSrc, &fFlags, &GCPhys);
2543 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrSrc), rc);
2544 GCPhys |= (RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK;
2545
2546 /* mark the guest page as accessed. */
2547 if (!(fFlags & X86_PTE_A))
2548 {
2549 rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
2550 AssertRC(rc);
2551 }
2552
2553 /* copy */
2554 size_t cbRead = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
2555 rc = PGMPhysRead(pVM, GCPhys, pvDst, cbRead);
2556 if (cbRead >= cb || RT_FAILURE(rc))
2557 return rc;
2558
2559 /* next */
2560 cb -= cbRead;
2561 pvDst = (uint8_t *)pvDst + cbRead;
2562 GCPtrSrc += cbRead;
2563 }
2564}
2565
2566
2567/**
2568 * Write to guest physical memory referenced by GC pointer.
2569 *
2570 * This function uses the current CR3/CR0/CR4 of the guest and will
2571 * respect access handlers and set dirty and accessed bits.
2572 *
2573 * @returns VBox status.
2574 * @retval VINF_SUCCESS.
2575 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
2576 *
2577 * @param pVCpu The VMCPU handle.
2578 * @param GCPtrDst The destination address (GC pointer).
2579 * @param pvSrc The source address.
2580 * @param cb The number of bytes to write.
2581 */
2582VMMDECL(int) PGMPhysWriteGCPtr(PVMCPU pVCpu, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
2583{
2584 RTGCPHYS GCPhys;
2585 uint64_t fFlags;
2586 int rc;
2587 PVM pVM = pVCpu->CTX_SUFF(pVM);
2588
2589 /*
2590 * Anything to do?
2591 */
2592 if (!cb)
2593 return VINF_SUCCESS;
2594
2595 LogFlow(("PGMPhysWriteGCPtr: %RGv %zu\n", GCPtrDst, cb));
2596
2597 /*
2598 * Optimize writes within a single page.
2599 */
2600 if (((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
2601 {
2602 /* Convert virtual to physical address + flags */
2603 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrDst, &fFlags, &GCPhys);
2604 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrDst), rc);
2605 GCPhys |= (RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK;
2606
2607 /* Mention when we ignore X86_PTE_RW... */
2608 if (!(fFlags & X86_PTE_RW))
2609 Log(("PGMPhysGCPtr2GCPhys: Writing to RO page %RGv %#x\n", GCPtrDst, cb));
2610
2611 /* Mark the guest page as accessed and dirty if necessary. */
2612 if ((fFlags & (X86_PTE_A | X86_PTE_D)) != (X86_PTE_A | X86_PTE_D))
2613 {
2614 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
2615 AssertRC(rc);
2616 }
2617
2618 return PGMPhysWrite(pVM, GCPhys, pvSrc, cb);
2619 }
2620
2621 /*
2622 * Page by page.
2623 */
2624 for (;;)
2625 {
2626 /* Convert virtual to physical address + flags */
2627 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrDst, &fFlags, &GCPhys);
2628 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrDst), rc);
2629 GCPhys |= (RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK;
2630
2631 /* Mention when we ignore X86_PTE_RW... */
2632 if (!(fFlags & X86_PTE_RW))
2633 Log(("PGMPhysGCPtr2GCPhys: Writing to RO page %RGv %#x\n", GCPtrDst, cb));
2634
2635 /* Mark the guest page as accessed and dirty if necessary. */
2636 if ((fFlags & (X86_PTE_A | X86_PTE_D)) != (X86_PTE_A | X86_PTE_D))
2637 {
2638 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
2639 AssertRC(rc);
2640 }
2641
2642 /* copy */
2643 size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
2644 int rc = PGMPhysWrite(pVM, GCPhys, pvSrc, cbWrite);
2645 if (cbWrite >= cb || RT_FAILURE(rc))
2646 return rc;
2647
2648 /* next */
2649 cb -= cbWrite;
2650 pvSrc = (uint8_t *)pvSrc + cbWrite;
2651 GCPtrDst += cbWrite;
2652 }
2653}
2654
2655
2656/**
2657 * Performs a read of guest virtual memory for instruction emulation.
2658 *
2659 * This will check permissions, raise exceptions and update the access bits.
2660 *
2661 * The current implementation will bypass all access handlers. It may later be
2662 * changed to at least respect MMIO.
2663 *
2664 *
2665 * @returns VBox status code suitable to scheduling.
2666 * @retval VINF_SUCCESS if the read was performed successfully.
2667 * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
2668 * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
2669 *
2670 * @param pVCpu The VMCPU handle.
2671 * @param pCtxCore The context core.
2672 * @param pvDst Where to put the bytes we've read.
2673 * @param GCPtrSrc The source address.
2674 * @param cb The number of bytes to read. Not more than a page.
2675 *
2676 * @remark This function will dynamically map physical pages in GC. This may unmap
2677 * mappings done by the caller. Be careful!
2678 */
2679VMMDECL(int) PGMPhysInterpretedRead(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb)
2680{
2681 PVM pVM = pVCpu->CTX_SUFF(pVM);
2682 Assert(cb <= PAGE_SIZE);
2683
2684/** @todo r=bird: This isn't perfect!
2685 * -# It's not checking for reserved bits being 1.
2686 * -# It's not correctly dealing with the access bit.
2687 * -# It's not respecting MMIO memory or any other access handlers.
2688 */
2689 /*
2690 * 1. Translate virtual to physical. This may fault.
2691 * 2. Map the physical address.
2692 * 3. Do the read operation.
2693 * 4. Set access bits if required.
2694 */
2695 int rc;
2696 unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
2697 if (cb <= cb1)
2698 {
2699 /*
2700 * Not crossing pages.
2701 */
2702 RTGCPHYS GCPhys;
2703 uint64_t fFlags;
2704 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags, &GCPhys);
2705 if (RT_SUCCESS(rc))
2706 {
2707 /** @todo we should check reserved bits ... */
2708 void *pvSrc;
2709 rc = PGM_GCPHYS_2_PTR(pVM, GCPhys, &pvSrc);
2710 switch (rc)
2711 {
2712 case VINF_SUCCESS:
2713 Log(("PGMPhysInterpretedRead: pvDst=%p pvSrc=%p cb=%d\n", pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb));
2714 memcpy(pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
2715 break;
2716 case VERR_PGM_PHYS_PAGE_RESERVED:
2717 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
2718 memset(pvDst, 0, cb); /** @todo this is wrong, it should be 0xff */
2719 break;
2720 default:
2721 return rc;
2722 }
2723
2724 /** @todo access bit emulation isn't 100% correct. */
2725 if (!(fFlags & X86_PTE_A))
2726 {
2727 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
2728 AssertRC(rc);
2729 }
2730 return VINF_SUCCESS;
2731 }
2732 }
2733 else
2734 {
2735 /*
2736 * Crosses pages.
2737 */
2738 size_t cb2 = cb - cb1;
2739 uint64_t fFlags1;
2740 RTGCPHYS GCPhys1;
2741 uint64_t fFlags2;
2742 RTGCPHYS GCPhys2;
2743 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags1, &GCPhys1);
2744 if (RT_SUCCESS(rc))
2745 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
2746 if (RT_SUCCESS(rc))
2747 {
2748 /** @todo we should check reserved bits ... */
2749 AssertMsgFailed(("cb=%d cb1=%d cb2=%d GCPtrSrc=%RGv\n", cb, cb1, cb2, GCPtrSrc));
2750 void *pvSrc1;
2751 rc = PGM_GCPHYS_2_PTR(pVM, GCPhys1, &pvSrc1);
2752 switch (rc)
2753 {
2754 case VINF_SUCCESS:
2755 memcpy(pvDst, (uint8_t *)pvSrc1 + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
2756 break;
2757 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
2758 memset(pvDst, 0, cb1); /** @todo this is wrong, it should be 0xff */
2759 break;
2760 default:
2761 return rc;
2762 }
2763
2764 void *pvSrc2;
2765 rc = PGM_GCPHYS_2_PTR(pVM, GCPhys2, &pvSrc2);
2766 switch (rc)
2767 {
2768 case VINF_SUCCESS:
2769 memcpy((uint8_t *)pvDst + cb1, pvSrc2, cb2);
2770 break;
2771 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
2772 memset((uint8_t *)pvDst + cb1, 0, cb2); /** @todo this is wrong, it should be 0xff */
2773 break;
2774 default:
2775 return rc;
2776 }
2777
2778 if (!(fFlags1 & X86_PTE_A))
2779 {
2780 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
2781 AssertRC(rc);
2782 }
2783 if (!(fFlags2 & X86_PTE_A))
2784 {
2785 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
2786 AssertRC(rc);
2787 }
2788 return VINF_SUCCESS;
2789 }
2790 }
2791
2792 /*
2793 * Raise a #PF.
2794 */
2795 uint32_t uErr;
2796
2797 /* Get the current privilege level. */
2798 uint32_t cpl = CPUMGetGuestCPL(pVCpu, pCtxCore);
2799 switch (rc)
2800 {
2801 case VINF_SUCCESS:
2802 uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
2803 break;
2804
2805 case VERR_PAGE_NOT_PRESENT:
2806 case VERR_PAGE_TABLE_NOT_PRESENT:
2807 uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
2808 break;
2809
2810 default:
2811 AssertMsgFailed(("rc=%Rrc GCPtrSrc=%RGv cb=%#x\n", rc, GCPtrSrc, cb));
2812 return rc;
2813 }
2814 Log(("PGMPhysInterpretedRead: GCPtrSrc=%RGv cb=%#x -> #PF(%#x)\n", GCPtrSrc, cb, uErr));
2815 return TRPMRaiseXcptErrCR2(pVCpu, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
2816}
2817
2818
2819/**
2820 * Performs a read of guest virtual memory for instruction emulation.
2821 *
2822 * This will check permissions, raise exceptions and update the access bits.
2823 *
2824 * The current implementation will bypass all access handlers. It may later be
2825 * changed to at least respect MMIO.
2826 *
2827 *
2828 * @returns VBox status code suitable to scheduling.
2829 * @retval VINF_SUCCESS if the read was performed successfully.
2830 * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
2831 * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
2832 *
2833 * @param pVCpu The VMCPU handle.
2834 * @param pCtxCore The context core.
2835 * @param pvDst Where to put the bytes we've read.
2836 * @param GCPtrSrc The source address.
2837 * @param cb The number of bytes to read. Not more than a page.
2838 * @param fRaiseTrap If set the trap will be raised on as per spec, if clear
2839 * an appropriate error status will be returned (no
2840 * informational at all).
2841 *
2842 *
2843 * @remarks Takes the PGM lock.
2844 * @remarks A page fault on the 2nd page of the access will be raised without
2845 * writing the bits on the first page since we're ASSUMING that the
2846 * caller is emulating an instruction access.
2847 * @remarks This function will dynamically map physical pages in GC. This may
2848 * unmap mappings done by the caller. Be careful!
2849 */
2850VMMDECL(int) PGMPhysInterpretedReadNoHandlers(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb, bool fRaiseTrap)
2851{
2852 PVM pVM = pVCpu->CTX_SUFF(pVM);
2853 Assert(cb <= PAGE_SIZE);
2854
2855 /*
2856 * 1. Translate virtual to physical. This may fault.
2857 * 2. Map the physical address.
2858 * 3. Do the read operation.
2859 * 4. Set access bits if required.
2860 */
2861 int rc;
2862 unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
2863 if (cb <= cb1)
2864 {
2865 /*
2866 * Not crossing pages.
2867 */
2868 RTGCPHYS GCPhys;
2869 uint64_t fFlags;
2870 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags, &GCPhys);
2871 if (RT_SUCCESS(rc))
2872 {
2873 if (1) /** @todo we should check reserved bits ... */
2874 {
2875 const void *pvSrc;
2876 PGMPAGEMAPLOCK Lock;
2877 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys, &pvSrc, &Lock);
2878 switch (rc)
2879 {
2880 case VINF_SUCCESS:
2881 Log(("PGMPhysInterpretedReadNoHandlers: pvDst=%p pvSrc=%p (%RGv) cb=%d\n",
2882 pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), GCPtrSrc, cb));
2883 memcpy(pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
2884 break;
2885 case VERR_PGM_PHYS_PAGE_RESERVED:
2886 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
2887 memset(pvDst, 0xff, cb);
2888 break;
2889 default:
2890 AssertMsgFailed(("%Rrc\n", rc));
2891 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
2892 return rc;
2893 }
2894 PGMPhysReleasePageMappingLock(pVM, &Lock);
2895
2896 if (!(fFlags & X86_PTE_A))
2897 {
2898 /** @todo access bit emulation isn't 100% correct. */
2899 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
2900 AssertRC(rc);
2901 }
2902 return VINF_SUCCESS;
2903 }
2904 }
2905 }
2906 else
2907 {
2908 /*
2909 * Crosses pages.
2910 */
2911 size_t cb2 = cb - cb1;
2912 uint64_t fFlags1;
2913 RTGCPHYS GCPhys1;
2914 uint64_t fFlags2;
2915 RTGCPHYS GCPhys2;
2916 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags1, &GCPhys1);
2917 if (RT_SUCCESS(rc))
2918 {
2919 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
2920 if (RT_SUCCESS(rc))
2921 {
2922 if (1) /** @todo we should check reserved bits ... */
2923 {
2924 const void *pvSrc;
2925 PGMPAGEMAPLOCK Lock;
2926 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys1, &pvSrc, &Lock);
2927 switch (rc)
2928 {
2929 case VINF_SUCCESS:
2930 Log(("PGMPhysInterpretedReadNoHandlers: pvDst=%p pvSrc=%p (%RGv) cb=%d [2]\n",
2931 pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), GCPtrSrc, cb1));
2932 memcpy(pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
2933 PGMPhysReleasePageMappingLock(pVM, &Lock);
2934 break;
2935 case VERR_PGM_PHYS_PAGE_RESERVED:
2936 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
2937 memset(pvDst, 0xff, cb1);
2938 break;
2939 default:
2940 AssertMsgFailed(("%Rrc\n", rc));
2941 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
2942 return rc;
2943 }
2944
2945 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys2, &pvSrc, &Lock);
2946 switch (rc)
2947 {
2948 case VINF_SUCCESS:
2949 memcpy((uint8_t *)pvDst + cb1, pvSrc, cb2);
2950 PGMPhysReleasePageMappingLock(pVM, &Lock);
2951 break;
2952 case VERR_PGM_PHYS_PAGE_RESERVED:
2953 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
2954 memset((uint8_t *)pvDst + cb1, 0xff, cb2);
2955 break;
2956 default:
2957 AssertMsgFailed(("%Rrc\n", rc));
2958 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
2959 return rc;
2960 }
2961
2962 if (!(fFlags1 & X86_PTE_A))
2963 {
2964 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
2965 AssertRC(rc);
2966 }
2967 if (!(fFlags2 & X86_PTE_A))
2968 {
2969 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
2970 AssertRC(rc);
2971 }
2972 return VINF_SUCCESS;
2973 }
2974 /* sort out which page */
2975 }
2976 else
2977 GCPtrSrc += cb1; /* fault on 2nd page */
2978 }
2979 }
2980
2981 /*
2982 * Raise a #PF if we're allowed to do that.
2983 */
2984 /* Calc the error bits. */
2985 uint32_t cpl = CPUMGetGuestCPL(pVCpu, pCtxCore);
2986 uint32_t uErr;
2987 switch (rc)
2988 {
2989 case VINF_SUCCESS:
2990 uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
2991 rc = VERR_ACCESS_DENIED;
2992 break;
2993
2994 case VERR_PAGE_NOT_PRESENT:
2995 case VERR_PAGE_TABLE_NOT_PRESENT:
2996 uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
2997 break;
2998
2999 default:
3000 AssertMsgFailed(("rc=%Rrc GCPtrSrc=%RGv cb=%#x\n", rc, GCPtrSrc, cb));
3001 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3002 return rc;
3003 }
3004 if (fRaiseTrap)
3005 {
3006 Log(("PGMPhysInterpretedReadNoHandlers: GCPtrSrc=%RGv cb=%#x -> Raised #PF(%#x)\n", GCPtrSrc, cb, uErr));
3007 return TRPMRaiseXcptErrCR2(pVCpu, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
3008 }
3009 Log(("PGMPhysInterpretedReadNoHandlers: GCPtrSrc=%RGv cb=%#x -> #PF(%#x) [!raised]\n", GCPtrSrc, cb, uErr));
3010 return rc;
3011}
3012
3013
3014/**
3015 * Performs a write to guest virtual memory for instruction emulation.
3016 *
3017 * This will check permissions, raise exceptions and update the dirty and access
3018 * bits.
3019 *
3020 * @returns VBox status code suitable to scheduling.
3021 * @retval VINF_SUCCESS if the read was performed successfully.
3022 * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
3023 * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
3024 *
3025 * @param pVCpu The VMCPU handle.
3026 * @param pCtxCore The context core.
3027 * @param GCPtrDst The destination address.
3028 * @param pvSrc What to write.
3029 * @param cb The number of bytes to write. Not more than a page.
3030 * @param fRaiseTrap If set the trap will be raised on as per spec, if clear
3031 * an appropriate error status will be returned (no
3032 * informational at all).
3033 *
3034 * @remarks Takes the PGM lock.
3035 * @remarks A page fault on the 2nd page of the access will be raised without
3036 * writing the bits on the first page since we're ASSUMING that the
3037 * caller is emulating an instruction access.
3038 * @remarks This function will dynamically map physical pages in GC. This may
3039 * unmap mappings done by the caller. Be careful!
3040 */
3041VMMDECL(int) PGMPhysInterpretedWriteNoHandlers(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb, bool fRaiseTrap)
3042{
3043 Assert(cb <= PAGE_SIZE);
3044 PVM pVM = pVCpu->CTX_SUFF(pVM);
3045
3046 /*
3047 * 1. Translate virtual to physical. This may fault.
3048 * 2. Map the physical address.
3049 * 3. Do the write operation.
3050 * 4. Set access bits if required.
3051 */
3052 int rc;
3053 unsigned cb1 = PAGE_SIZE - (GCPtrDst & PAGE_OFFSET_MASK);
3054 if (cb <= cb1)
3055 {
3056 /*
3057 * Not crossing pages.
3058 */
3059 RTGCPHYS GCPhys;
3060 uint64_t fFlags;
3061 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrDst, &fFlags, &GCPhys);
3062 if (RT_SUCCESS(rc))
3063 {
3064 if ( (fFlags & X86_PTE_RW) /** @todo Also check reserved bits. */
3065 || ( !(CPUMGetGuestCR0(pVCpu) & X86_CR0_WP)
3066 && CPUMGetGuestCPL(pVCpu, pCtxCore) <= 2) ) /** @todo it's 2, right? Check cpl check below as well. */
3067 {
3068 void *pvDst;
3069 PGMPAGEMAPLOCK Lock;
3070 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys, &pvDst, &Lock);
3071 switch (rc)
3072 {
3073 case VINF_SUCCESS:
3074 Log(("PGMPhysInterpretedWriteNoHandlers: pvDst=%p (%RGv) pvSrc=%p cb=%d\n",
3075 (uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), GCPtrDst, pvSrc, cb));
3076 memcpy((uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), pvSrc, cb);
3077 PGMPhysReleasePageMappingLock(pVM, &Lock);
3078 break;
3079 case VERR_PGM_PHYS_PAGE_RESERVED:
3080 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3081 /* bit bucket */
3082 break;
3083 default:
3084 AssertMsgFailed(("%Rrc\n", rc));
3085 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3086 return rc;
3087 }
3088
3089 if (!(fFlags & (X86_PTE_A | X86_PTE_D)))
3090 {
3091 /** @todo dirty & access bit emulation isn't 100% correct. */
3092 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
3093 AssertRC(rc);
3094 }
3095 return VINF_SUCCESS;
3096 }
3097 rc = VERR_ACCESS_DENIED;
3098 }
3099 }
3100 else
3101 {
3102 /*
3103 * Crosses pages.
3104 */
3105 size_t cb2 = cb - cb1;
3106 uint64_t fFlags1;
3107 RTGCPHYS GCPhys1;
3108 uint64_t fFlags2;
3109 RTGCPHYS GCPhys2;
3110 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrDst, &fFlags1, &GCPhys1);
3111 if (RT_SUCCESS(rc))
3112 {
3113 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrDst + cb1, &fFlags2, &GCPhys2);
3114 if (RT_SUCCESS(rc))
3115 {
3116 if ( ( (fFlags1 & X86_PTE_RW) /** @todo Also check reserved bits. */
3117 && (fFlags2 & X86_PTE_RW))
3118 || ( !(CPUMGetGuestCR0(pVCpu) & X86_CR0_WP)
3119 && CPUMGetGuestCPL(pVCpu, pCtxCore) <= 2) )
3120 {
3121 void *pvDst;
3122 PGMPAGEMAPLOCK Lock;
3123 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys1, &pvDst, &Lock);
3124 switch (rc)
3125 {
3126 case VINF_SUCCESS:
3127 Log(("PGMPhysInterpretedWriteNoHandlers: pvDst=%p (%RGv) pvSrc=%p cb=%d\n",
3128 (uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), GCPtrDst, pvSrc, cb1));
3129 memcpy((uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), pvSrc, cb1);
3130 PGMPhysReleasePageMappingLock(pVM, &Lock);
3131 break;
3132 case VERR_PGM_PHYS_PAGE_RESERVED:
3133 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3134 /* bit bucket */
3135 break;
3136 default:
3137 AssertMsgFailed(("%Rrc\n", rc));
3138 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3139 return rc;
3140 }
3141
3142 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys2, &pvDst, &Lock);
3143 switch (rc)
3144 {
3145 case VINF_SUCCESS:
3146 memcpy(pvDst, (const uint8_t *)pvSrc + cb1, cb2);
3147 PGMPhysReleasePageMappingLock(pVM, &Lock);
3148 break;
3149 case VERR_PGM_PHYS_PAGE_RESERVED:
3150 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3151 /* bit bucket */
3152 break;
3153 default:
3154 AssertMsgFailed(("%Rrc\n", rc));
3155 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3156 return rc;
3157 }
3158
3159 if (!(fFlags1 & (X86_PTE_A | X86_PTE_RW)))
3160 {
3161 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrDst, 1, (X86_PTE_A | X86_PTE_RW), ~(uint64_t)(X86_PTE_A | X86_PTE_RW));
3162 AssertRC(rc);
3163 }
3164 if (!(fFlags2 & (X86_PTE_A | X86_PTE_RW)))
3165 {
3166 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrDst + cb1, 1, (X86_PTE_A | X86_PTE_RW), ~(uint64_t)(X86_PTE_A | X86_PTE_RW));
3167 AssertRC(rc);
3168 }
3169 return VINF_SUCCESS;
3170 }
3171 if ((fFlags1 & (X86_PTE_RW)) == X86_PTE_RW)
3172 GCPtrDst += cb1; /* fault on the 2nd page. */
3173 rc = VERR_ACCESS_DENIED;
3174 }
3175 else
3176 GCPtrDst += cb1; /* fault on the 2nd page. */
3177 }
3178 }
3179
3180 /*
3181 * Raise a #PF if we're allowed to do that.
3182 */
3183 /* Calc the error bits. */
3184 uint32_t uErr;
3185 uint32_t cpl = CPUMGetGuestCPL(pVCpu, pCtxCore);
3186 switch (rc)
3187 {
3188 case VINF_SUCCESS:
3189 uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
3190 rc = VERR_ACCESS_DENIED;
3191 break;
3192
3193 case VERR_ACCESS_DENIED:
3194 uErr = (cpl >= 2) ? X86_TRAP_PF_RW | X86_TRAP_PF_US : X86_TRAP_PF_RW;
3195 break;
3196
3197 case VERR_PAGE_NOT_PRESENT:
3198 case VERR_PAGE_TABLE_NOT_PRESENT:
3199 uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
3200 break;
3201
3202 default:
3203 AssertMsgFailed(("rc=%Rrc GCPtrDst=%RGv cb=%#x\n", rc, GCPtrDst, cb));
3204 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3205 return rc;
3206 }
3207 if (fRaiseTrap)
3208 {
3209 Log(("PGMPhysInterpretedWriteNoHandlers: GCPtrDst=%RGv cb=%#x -> Raised #PF(%#x)\n", GCPtrDst, cb, uErr));
3210 return TRPMRaiseXcptErrCR2(pVCpu, pCtxCore, X86_XCPT_PF, uErr, GCPtrDst);
3211 }
3212 Log(("PGMPhysInterpretedWriteNoHandlers: GCPtrDst=%RGv cb=%#x -> #PF(%#x) [!raised]\n", GCPtrDst, cb, uErr));
3213 return rc;
3214}
3215
3216
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