VirtualBox

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

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

Grab the PGM lock in PGMPhysSimpleReadGCPtr

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