VirtualBox

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

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

Stricter lock checking

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 111.9 KB
Line 
1/* $Id: PGMAllPhys.cpp 20059 2009-05-27 09:04:37Z 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 DISCPUSTATE Cpu;
79 rc = EMInterpretDisasOne(pVM, pVCpu, pRegFrame, &Cpu, &cbOp);
80 if ( RT_SUCCESS(rc)
81 && Cpu.mode == CPUMODE_32BIT /** @todo why does this matter? */
82 && !(Cpu.prefix & (PREFIX_REPNE | PREFIX_REP | PREFIX_SEG)))
83 {
84 switch (Cpu.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#ifndef IN_RC /* Ring 0 & 3 only. (Just not needed in GC.) */
2149
2150/**
2151 * Write to guest physical memory referenced by GC pointer.
2152 * Write memory to GC physical address in guest physical memory.
2153 *
2154 * This will bypass MMIO and access handlers.
2155 *
2156 * @returns VBox status.
2157 * @param pVM VM handle.
2158 * @param GCPhysDst The GC physical address of the destination.
2159 * @param pvSrc The source buffer.
2160 * @param cb The number of bytes to write.
2161 */
2162VMMDECL(int) PGMPhysSimpleWriteGCPhys(PVM pVM, RTGCPHYS GCPhysDst, const void *pvSrc, size_t cb)
2163{
2164 LogFlow(("PGMPhysSimpleWriteGCPhys: %RGp %zu\n", GCPhysDst, cb));
2165
2166 /*
2167 * Treat the first page as a special case.
2168 */
2169 if (!cb)
2170 return VINF_SUCCESS;
2171
2172 /* map the 1st page */
2173 void *pvDst;
2174 PGMPAGEMAPLOCK Lock;
2175 int rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
2176 if (RT_FAILURE(rc))
2177 return rc;
2178
2179 /* optimize for the case where access is completely within the first page. */
2180 size_t cbPage = PAGE_SIZE - (GCPhysDst & PAGE_OFFSET_MASK);
2181 if (RT_LIKELY(cb <= cbPage))
2182 {
2183 memcpy(pvDst, pvSrc, cb);
2184 PGMPhysReleasePageMappingLock(pVM, &Lock);
2185 return VINF_SUCCESS;
2186 }
2187
2188 /* copy to the end of the page. */
2189 memcpy(pvDst, pvSrc, cbPage);
2190 PGMPhysReleasePageMappingLock(pVM, &Lock);
2191 GCPhysDst += cbPage;
2192 pvSrc = (const uint8_t *)pvSrc + cbPage;
2193 cb -= cbPage;
2194
2195 /*
2196 * Page by page.
2197 */
2198 for (;;)
2199 {
2200 /* map the page */
2201 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
2202 if (RT_FAILURE(rc))
2203 return rc;
2204
2205 /* last page? */
2206 if (cb <= PAGE_SIZE)
2207 {
2208 memcpy(pvDst, pvSrc, cb);
2209 PGMPhysReleasePageMappingLock(pVM, &Lock);
2210 return VINF_SUCCESS;
2211 }
2212
2213 /* copy the entire page and advance */
2214 memcpy(pvDst, pvSrc, PAGE_SIZE);
2215 PGMPhysReleasePageMappingLock(pVM, &Lock);
2216 GCPhysDst += PAGE_SIZE;
2217 pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
2218 cb -= PAGE_SIZE;
2219 }
2220 /* won't ever get here. */
2221}
2222
2223
2224/**
2225 * Read from guest physical memory referenced by GC pointer.
2226 *
2227 * This function uses the current CR3/CR0/CR4 of the guest and will
2228 * bypass access handlers and not set any accessed bits.
2229 *
2230 * @returns VBox status.
2231 * @param pVCpu The VMCPU handle.
2232 * @param pvDst The destination address.
2233 * @param GCPtrSrc The source address (GC pointer).
2234 * @param cb The number of bytes to read.
2235 */
2236VMMDECL(int) PGMPhysSimpleReadGCPtr(PVMCPU pVCpu, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
2237{
2238 PVM pVM = pVCpu->CTX_SUFF(pVM);
2239
2240 /*
2241 * Treat the first page as a special case.
2242 */
2243 if (!cb)
2244 return VINF_SUCCESS;
2245
2246 /* map the 1st page */
2247 void const *pvSrc;
2248 PGMPAGEMAPLOCK Lock;
2249 int rc = PGMPhysGCPtr2CCPtrReadOnly(pVCpu, GCPtrSrc, &pvSrc, &Lock);
2250 if (RT_FAILURE(rc))
2251 return rc;
2252
2253 /* optimize for the case where access is completely within the first page. */
2254 size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
2255 if (RT_LIKELY(cb <= cbPage))
2256 {
2257 memcpy(pvDst, pvSrc, cb);
2258 PGMPhysReleasePageMappingLock(pVM, &Lock);
2259 return VINF_SUCCESS;
2260 }
2261
2262 /* copy to the end of the page. */
2263 memcpy(pvDst, pvSrc, cbPage);
2264 PGMPhysReleasePageMappingLock(pVM, &Lock);
2265 GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + cbPage);
2266 pvDst = (uint8_t *)pvDst + cbPage;
2267 cb -= cbPage;
2268
2269 /*
2270 * Page by page.
2271 */
2272 for (;;)
2273 {
2274 /* map the page */
2275 rc = PGMPhysGCPtr2CCPtrReadOnly(pVCpu, GCPtrSrc, &pvSrc, &Lock);
2276 if (RT_FAILURE(rc))
2277 return rc;
2278
2279 /* last page? */
2280 if (cb <= PAGE_SIZE)
2281 {
2282 memcpy(pvDst, pvSrc, cb);
2283 PGMPhysReleasePageMappingLock(pVM, &Lock);
2284 return VINF_SUCCESS;
2285 }
2286
2287 /* copy the entire page and advance */
2288 memcpy(pvDst, pvSrc, PAGE_SIZE);
2289 PGMPhysReleasePageMappingLock(pVM, &Lock);
2290 GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + PAGE_SIZE);
2291 pvDst = (uint8_t *)pvDst + PAGE_SIZE;
2292 cb -= PAGE_SIZE;
2293 }
2294 /* won't ever get here. */
2295}
2296
2297
2298/**
2299 * Write to guest physical memory referenced by GC pointer.
2300 *
2301 * This function uses the current CR3/CR0/CR4 of the guest and will
2302 * bypass access handlers and not set dirty or accessed bits.
2303 *
2304 * @returns VBox status.
2305 * @param pVCpu The VMCPU handle.
2306 * @param GCPtrDst The destination address (GC pointer).
2307 * @param pvSrc The source address.
2308 * @param cb The number of bytes to write.
2309 */
2310VMMDECL(int) PGMPhysSimpleWriteGCPtr(PVMCPU pVCpu, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
2311{
2312 PVM pVM = pVCpu->CTX_SUFF(pVM);
2313
2314 /*
2315 * Treat the first page as a special case.
2316 */
2317 if (!cb)
2318 return VINF_SUCCESS;
2319
2320 /* map the 1st page */
2321 void *pvDst;
2322 PGMPAGEMAPLOCK Lock;
2323 int rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
2324 if (RT_FAILURE(rc))
2325 return rc;
2326
2327 /* optimize for the case where access is completely within the first page. */
2328 size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
2329 if (RT_LIKELY(cb <= cbPage))
2330 {
2331 memcpy(pvDst, pvSrc, cb);
2332 PGMPhysReleasePageMappingLock(pVM, &Lock);
2333 return VINF_SUCCESS;
2334 }
2335
2336 /* copy to the end of the page. */
2337 memcpy(pvDst, pvSrc, cbPage);
2338 PGMPhysReleasePageMappingLock(pVM, &Lock);
2339 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
2340 pvSrc = (const uint8_t *)pvSrc + cbPage;
2341 cb -= cbPage;
2342
2343 /*
2344 * Page by page.
2345 */
2346 for (;;)
2347 {
2348 /* map the page */
2349 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
2350 if (RT_FAILURE(rc))
2351 return rc;
2352
2353 /* last page? */
2354 if (cb <= PAGE_SIZE)
2355 {
2356 memcpy(pvDst, pvSrc, cb);
2357 PGMPhysReleasePageMappingLock(pVM, &Lock);
2358 return VINF_SUCCESS;
2359 }
2360
2361 /* copy the entire page and advance */
2362 memcpy(pvDst, pvSrc, PAGE_SIZE);
2363 PGMPhysReleasePageMappingLock(pVM, &Lock);
2364 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + PAGE_SIZE);
2365 pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
2366 cb -= PAGE_SIZE;
2367 }
2368 /* won't ever get here. */
2369}
2370
2371
2372/**
2373 * Write to guest physical memory referenced by GC pointer and update the PTE.
2374 *
2375 * This function uses the current CR3/CR0/CR4 of the guest and will
2376 * bypass access handlers but will set any dirty and accessed bits in the PTE.
2377 *
2378 * If you don't want to set the dirty bit, use PGMPhysSimpleWriteGCPtr().
2379 *
2380 * @returns VBox status.
2381 * @param pVCpu The VMCPU handle.
2382 * @param GCPtrDst The destination address (GC pointer).
2383 * @param pvSrc The source address.
2384 * @param cb The number of bytes to write.
2385 */
2386VMMDECL(int) PGMPhysSimpleDirtyWriteGCPtr(PVMCPU pVCpu, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
2387{
2388 PVM pVM = pVCpu->CTX_SUFF(pVM);
2389
2390 /*
2391 * Treat the first page as a special case.
2392 * Btw. this is the same code as in PGMPhyssimpleWriteGCPtr excep for the PGMGstModifyPage.
2393 */
2394 if (!cb)
2395 return VINF_SUCCESS;
2396
2397 /* map the 1st page */
2398 void *pvDst;
2399 PGMPAGEMAPLOCK Lock;
2400 int rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
2401 if (RT_FAILURE(rc))
2402 return rc;
2403
2404 /* optimize for the case where access is completely within the first page. */
2405 size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
2406 if (RT_LIKELY(cb <= cbPage))
2407 {
2408 memcpy(pvDst, pvSrc, cb);
2409 PGMPhysReleasePageMappingLock(pVM, &Lock);
2410 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
2411 return VINF_SUCCESS;
2412 }
2413
2414 /* copy to the end of the page. */
2415 memcpy(pvDst, pvSrc, cbPage);
2416 PGMPhysReleasePageMappingLock(pVM, &Lock);
2417 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
2418 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
2419 pvSrc = (const uint8_t *)pvSrc + cbPage;
2420 cb -= cbPage;
2421
2422 /*
2423 * Page by page.
2424 */
2425 for (;;)
2426 {
2427 /* map the page */
2428 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
2429 if (RT_FAILURE(rc))
2430 return rc;
2431
2432 /* last page? */
2433 if (cb <= PAGE_SIZE)
2434 {
2435 memcpy(pvDst, pvSrc, cb);
2436 PGMPhysReleasePageMappingLock(pVM, &Lock);
2437 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
2438 return VINF_SUCCESS;
2439 }
2440
2441 /* copy the entire page and advance */
2442 memcpy(pvDst, pvSrc, PAGE_SIZE);
2443 PGMPhysReleasePageMappingLock(pVM, &Lock);
2444 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
2445 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + PAGE_SIZE);
2446 pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
2447 cb -= PAGE_SIZE;
2448 }
2449 /* won't ever get here. */
2450}
2451
2452
2453/**
2454 * Read from guest physical memory referenced by GC pointer.
2455 *
2456 * This function uses the current CR3/CR0/CR4 of the guest and will
2457 * respect access handlers and set accessed bits.
2458 *
2459 * @returns VBox status.
2460 * @param pVCpu The VMCPU handle.
2461 * @param pvDst The destination address.
2462 * @param GCPtrSrc The source address (GC pointer).
2463 * @param cb The number of bytes to read.
2464 * @thread The vCPU EMT.
2465 */
2466VMMDECL(int) PGMPhysReadGCPtr(PVMCPU pVCpu, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
2467{
2468 RTGCPHYS GCPhys;
2469 uint64_t fFlags;
2470 int rc;
2471 PVM pVM = pVCpu->CTX_SUFF(pVM);
2472
2473 /*
2474 * Anything to do?
2475 */
2476 if (!cb)
2477 return VINF_SUCCESS;
2478
2479 LogFlow(("PGMPhysReadGCPtr: %RGv %zu\n", GCPtrSrc, cb));
2480
2481 /*
2482 * Optimize reads within a single page.
2483 */
2484 if (((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
2485 {
2486 /* Convert virtual to physical address + flags */
2487 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrSrc, &fFlags, &GCPhys);
2488 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrSrc), rc);
2489 GCPhys |= (RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK;
2490
2491 /* mark the guest page as accessed. */
2492 if (!(fFlags & X86_PTE_A))
2493 {
2494 rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
2495 AssertRC(rc);
2496 }
2497
2498 return PGMPhysRead(pVM, GCPhys, pvDst, cb);
2499 }
2500
2501 /*
2502 * Page by page.
2503 */
2504 for (;;)
2505 {
2506 /* Convert virtual to physical address + flags */
2507 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrSrc, &fFlags, &GCPhys);
2508 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrSrc), rc);
2509 GCPhys |= (RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK;
2510
2511 /* mark the guest page as accessed. */
2512 if (!(fFlags & X86_PTE_A))
2513 {
2514 rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
2515 AssertRC(rc);
2516 }
2517
2518 /* copy */
2519 size_t cbRead = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
2520 rc = PGMPhysRead(pVM, GCPhys, pvDst, cbRead);
2521 if (cbRead >= cb || RT_FAILURE(rc))
2522 return rc;
2523
2524 /* next */
2525 cb -= cbRead;
2526 pvDst = (uint8_t *)pvDst + cbRead;
2527 GCPtrSrc += cbRead;
2528 }
2529}
2530
2531
2532/**
2533 * Write to guest physical memory referenced by GC pointer.
2534 *
2535 * This function uses the current CR3/CR0/CR4 of the guest and will
2536 * respect access handlers and set dirty and accessed bits.
2537 *
2538 * @returns VBox status.
2539 * @retval VINF_SUCCESS.
2540 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
2541 *
2542 * @param pVCpu The VMCPU handle.
2543 * @param GCPtrDst The destination address (GC pointer).
2544 * @param pvSrc The source address.
2545 * @param cb The number of bytes to write.
2546 */
2547VMMDECL(int) PGMPhysWriteGCPtr(PVMCPU pVCpu, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
2548{
2549 RTGCPHYS GCPhys;
2550 uint64_t fFlags;
2551 int rc;
2552 PVM pVM = pVCpu->CTX_SUFF(pVM);
2553
2554 /*
2555 * Anything to do?
2556 */
2557 if (!cb)
2558 return VINF_SUCCESS;
2559
2560 LogFlow(("PGMPhysWriteGCPtr: %RGv %zu\n", GCPtrDst, cb));
2561
2562 /*
2563 * Optimize writes within a single page.
2564 */
2565 if (((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
2566 {
2567 /* Convert virtual to physical address + flags */
2568 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrDst, &fFlags, &GCPhys);
2569 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrDst), rc);
2570 GCPhys |= (RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK;
2571
2572 /* Mention when we ignore X86_PTE_RW... */
2573 if (!(fFlags & X86_PTE_RW))
2574 Log(("PGMPhysGCPtr2GCPhys: Writing to RO page %RGv %#x\n", GCPtrDst, cb));
2575
2576 /* Mark the guest page as accessed and dirty if necessary. */
2577 if ((fFlags & (X86_PTE_A | X86_PTE_D)) != (X86_PTE_A | X86_PTE_D))
2578 {
2579 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
2580 AssertRC(rc);
2581 }
2582
2583 return PGMPhysWrite(pVM, GCPhys, pvSrc, cb);
2584 }
2585
2586 /*
2587 * Page by page.
2588 */
2589 for (;;)
2590 {
2591 /* Convert virtual to physical address + flags */
2592 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, (RTGCUINTPTR)GCPtrDst, &fFlags, &GCPhys);
2593 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrDst), rc);
2594 GCPhys |= (RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK;
2595
2596 /* Mention when we ignore X86_PTE_RW... */
2597 if (!(fFlags & X86_PTE_RW))
2598 Log(("PGMPhysGCPtr2GCPhys: Writing to RO page %RGv %#x\n", GCPtrDst, cb));
2599
2600 /* Mark the guest page as accessed and dirty if necessary. */
2601 if ((fFlags & (X86_PTE_A | X86_PTE_D)) != (X86_PTE_A | X86_PTE_D))
2602 {
2603 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
2604 AssertRC(rc);
2605 }
2606
2607 /* copy */
2608 size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
2609 int rc = PGMPhysWrite(pVM, GCPhys, pvSrc, cbWrite);
2610 if (cbWrite >= cb || RT_FAILURE(rc))
2611 return rc;
2612
2613 /* next */
2614 cb -= cbWrite;
2615 pvSrc = (uint8_t *)pvSrc + cbWrite;
2616 GCPtrDst += cbWrite;
2617 }
2618}
2619
2620#endif /* !IN_RC */
2621
2622/**
2623 * Performs a read of guest virtual memory for instruction emulation.
2624 *
2625 * This will check permissions, raise exceptions and update the access bits.
2626 *
2627 * The current implementation will bypass all access handlers. It may later be
2628 * changed to at least respect MMIO.
2629 *
2630 *
2631 * @returns VBox status code suitable to scheduling.
2632 * @retval VINF_SUCCESS if the read was performed successfully.
2633 * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
2634 * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
2635 *
2636 * @param pVCpu The VMCPU handle.
2637 * @param pCtxCore The context core.
2638 * @param pvDst Where to put the bytes we've read.
2639 * @param GCPtrSrc The source address.
2640 * @param cb The number of bytes to read. Not more than a page.
2641 *
2642 * @remark This function will dynamically map physical pages in GC. This may unmap
2643 * mappings done by the caller. Be careful!
2644 */
2645VMMDECL(int) PGMPhysInterpretedRead(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb)
2646{
2647 PVM pVM = pVCpu->CTX_SUFF(pVM);
2648 Assert(cb <= PAGE_SIZE);
2649
2650/** @todo r=bird: This isn't perfect!
2651 * -# It's not checking for reserved bits being 1.
2652 * -# It's not correctly dealing with the access bit.
2653 * -# It's not respecting MMIO memory or any other access handlers.
2654 */
2655 /*
2656 * 1. Translate virtual to physical. This may fault.
2657 * 2. Map the physical address.
2658 * 3. Do the read operation.
2659 * 4. Set access bits if required.
2660 */
2661 int rc;
2662 unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
2663 if (cb <= cb1)
2664 {
2665 /*
2666 * Not crossing pages.
2667 */
2668 RTGCPHYS GCPhys;
2669 uint64_t fFlags;
2670 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags, &GCPhys);
2671 if (RT_SUCCESS(rc))
2672 {
2673 /** @todo we should check reserved bits ... */
2674 void *pvSrc;
2675 rc = PGM_GCPHYS_2_PTR(pVM, GCPhys, &pvSrc);
2676 switch (rc)
2677 {
2678 case VINF_SUCCESS:
2679 Log(("PGMPhysInterpretedRead: pvDst=%p pvSrc=%p cb=%d\n", pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb));
2680 memcpy(pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
2681 break;
2682 case VERR_PGM_PHYS_PAGE_RESERVED:
2683 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
2684 memset(pvDst, 0, cb); /** @todo this is wrong, it should be 0xff */
2685 break;
2686 default:
2687 return rc;
2688 }
2689
2690 /** @todo access bit emulation isn't 100% correct. */
2691 if (!(fFlags & X86_PTE_A))
2692 {
2693 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
2694 AssertRC(rc);
2695 }
2696 return VINF_SUCCESS;
2697 }
2698 }
2699 else
2700 {
2701 /*
2702 * Crosses pages.
2703 */
2704 size_t cb2 = cb - cb1;
2705 uint64_t fFlags1;
2706 RTGCPHYS GCPhys1;
2707 uint64_t fFlags2;
2708 RTGCPHYS GCPhys2;
2709 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags1, &GCPhys1);
2710 if (RT_SUCCESS(rc))
2711 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
2712 if (RT_SUCCESS(rc))
2713 {
2714 /** @todo we should check reserved bits ... */
2715 AssertMsgFailed(("cb=%d cb1=%d cb2=%d GCPtrSrc=%RGv\n", cb, cb1, cb2, GCPtrSrc));
2716 void *pvSrc1;
2717 rc = PGM_GCPHYS_2_PTR(pVM, GCPhys1, &pvSrc1);
2718 switch (rc)
2719 {
2720 case VINF_SUCCESS:
2721 memcpy(pvDst, (uint8_t *)pvSrc1 + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
2722 break;
2723 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
2724 memset(pvDst, 0, cb1); /** @todo this is wrong, it should be 0xff */
2725 break;
2726 default:
2727 return rc;
2728 }
2729
2730 void *pvSrc2;
2731 rc = PGM_GCPHYS_2_PTR(pVM, GCPhys2, &pvSrc2);
2732 switch (rc)
2733 {
2734 case VINF_SUCCESS:
2735 memcpy((uint8_t *)pvDst + cb1, pvSrc2, cb2);
2736 break;
2737 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
2738 memset((uint8_t *)pvDst + cb1, 0, cb2); /** @todo this is wrong, it should be 0xff */
2739 break;
2740 default:
2741 return rc;
2742 }
2743
2744 if (!(fFlags1 & X86_PTE_A))
2745 {
2746 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
2747 AssertRC(rc);
2748 }
2749 if (!(fFlags2 & X86_PTE_A))
2750 {
2751 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
2752 AssertRC(rc);
2753 }
2754 return VINF_SUCCESS;
2755 }
2756 }
2757
2758 /*
2759 * Raise a #PF.
2760 */
2761 uint32_t uErr;
2762
2763 /* Get the current privilege level. */
2764 uint32_t cpl = CPUMGetGuestCPL(pVCpu, pCtxCore);
2765 switch (rc)
2766 {
2767 case VINF_SUCCESS:
2768 uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
2769 break;
2770
2771 case VERR_PAGE_NOT_PRESENT:
2772 case VERR_PAGE_TABLE_NOT_PRESENT:
2773 uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
2774 break;
2775
2776 default:
2777 AssertMsgFailed(("rc=%Rrc GCPtrSrc=%RGv cb=%#x\n", rc, GCPtrSrc, cb));
2778 return rc;
2779 }
2780 Log(("PGMPhysInterpretedRead: GCPtrSrc=%RGv cb=%#x -> #PF(%#x)\n", GCPtrSrc, cb, uErr));
2781 return TRPMRaiseXcptErrCR2(pVCpu, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
2782}
2783
2784
2785/**
2786 * Performs a read of guest virtual memory for instruction emulation.
2787 *
2788 * This will check permissions, raise exceptions and update the access bits.
2789 *
2790 * The current implementation will bypass all access handlers. It may later be
2791 * changed to at least respect MMIO.
2792 *
2793 *
2794 * @returns VBox status code suitable to scheduling.
2795 * @retval VINF_SUCCESS if the read was performed successfully.
2796 * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
2797 * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
2798 *
2799 * @param pVCpu The VMCPU handle.
2800 * @param pCtxCore The context core.
2801 * @param pvDst Where to put the bytes we've read.
2802 * @param GCPtrSrc The source address.
2803 * @param cb The number of bytes to read. Not more than a page.
2804 * @param fRaiseTrap If set the trap will be raised on as per spec, if clear
2805 * an appropriate error status will be returned (no
2806 * informational at all).
2807 *
2808 *
2809 * @remarks Takes the PGM lock.
2810 * @remarks A page fault on the 2nd page of the access will be raised without
2811 * writing the bits on the first page since we're ASSUMING that the
2812 * caller is emulating an instruction access.
2813 * @remarks This function will dynamically map physical pages in GC. This may
2814 * unmap mappings done by the caller. Be careful!
2815 */
2816VMMDECL(int) PGMPhysInterpretedReadNoHandlers(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb, bool fRaiseTrap)
2817{
2818 PVM pVM = pVCpu->CTX_SUFF(pVM);
2819 Assert(cb <= PAGE_SIZE);
2820
2821 /*
2822 * 1. Translate virtual to physical. This may fault.
2823 * 2. Map the physical address.
2824 * 3. Do the read operation.
2825 * 4. Set access bits if required.
2826 */
2827 int rc;
2828 unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
2829 if (cb <= cb1)
2830 {
2831 /*
2832 * Not crossing pages.
2833 */
2834 RTGCPHYS GCPhys;
2835 uint64_t fFlags;
2836 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags, &GCPhys);
2837 if (RT_SUCCESS(rc))
2838 {
2839 if (1) /** @todo we should check reserved bits ... */
2840 {
2841 const void *pvSrc;
2842 PGMPAGEMAPLOCK Lock;
2843 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys, &pvSrc, &Lock);
2844 switch (rc)
2845 {
2846 case VINF_SUCCESS:
2847 Log(("PGMPhysInterpretedReadNoHandlers: pvDst=%p pvSrc=%p (%RGv) cb=%d\n",
2848 pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), GCPtrSrc, cb));
2849 memcpy(pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
2850 break;
2851 case VERR_PGM_PHYS_PAGE_RESERVED:
2852 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
2853 memset(pvDst, 0xff, cb);
2854 break;
2855 default:
2856 AssertMsgFailed(("%Rrc\n", rc));
2857 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
2858 return rc;
2859 }
2860 PGMPhysReleasePageMappingLock(pVM, &Lock);
2861
2862 if (!(fFlags & X86_PTE_A))
2863 {
2864 /** @todo access bit emulation isn't 100% correct. */
2865 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
2866 AssertRC(rc);
2867 }
2868 return VINF_SUCCESS;
2869 }
2870 }
2871 }
2872 else
2873 {
2874 /*
2875 * Crosses pages.
2876 */
2877 size_t cb2 = cb - cb1;
2878 uint64_t fFlags1;
2879 RTGCPHYS GCPhys1;
2880 uint64_t fFlags2;
2881 RTGCPHYS GCPhys2;
2882 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc, &fFlags1, &GCPhys1);
2883 if (RT_SUCCESS(rc))
2884 {
2885 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
2886 if (RT_SUCCESS(rc))
2887 {
2888 if (1) /** @todo we should check reserved bits ... */
2889 {
2890 const void *pvSrc;
2891 PGMPAGEMAPLOCK Lock;
2892 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys1, &pvSrc, &Lock);
2893 switch (rc)
2894 {
2895 case VINF_SUCCESS:
2896 Log(("PGMPhysInterpretedReadNoHandlers: pvDst=%p pvSrc=%p (%RGv) cb=%d [2]\n",
2897 pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), GCPtrSrc, cb1));
2898 memcpy(pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
2899 PGMPhysReleasePageMappingLock(pVM, &Lock);
2900 break;
2901 case VERR_PGM_PHYS_PAGE_RESERVED:
2902 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
2903 memset(pvDst, 0xff, cb1);
2904 break;
2905 default:
2906 AssertMsgFailed(("%Rrc\n", rc));
2907 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
2908 return rc;
2909 }
2910
2911 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys2, &pvSrc, &Lock);
2912 switch (rc)
2913 {
2914 case VINF_SUCCESS:
2915 memcpy((uint8_t *)pvDst + cb1, pvSrc, cb2);
2916 PGMPhysReleasePageMappingLock(pVM, &Lock);
2917 break;
2918 case VERR_PGM_PHYS_PAGE_RESERVED:
2919 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
2920 memset((uint8_t *)pvDst + cb1, 0xff, cb2);
2921 break;
2922 default:
2923 AssertMsgFailed(("%Rrc\n", rc));
2924 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
2925 return rc;
2926 }
2927
2928 if (!(fFlags1 & X86_PTE_A))
2929 {
2930 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
2931 AssertRC(rc);
2932 }
2933 if (!(fFlags2 & X86_PTE_A))
2934 {
2935 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
2936 AssertRC(rc);
2937 }
2938 return VINF_SUCCESS;
2939 }
2940 /* sort out which page */
2941 }
2942 else
2943 GCPtrSrc += cb1; /* fault on 2nd page */
2944 }
2945 }
2946
2947 /*
2948 * Raise a #PF if we're allowed to do that.
2949 */
2950 /* Calc the error bits. */
2951 uint32_t cpl = CPUMGetGuestCPL(pVCpu, pCtxCore);
2952 uint32_t uErr;
2953 switch (rc)
2954 {
2955 case VINF_SUCCESS:
2956 uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
2957 rc = VERR_ACCESS_DENIED;
2958 break;
2959
2960 case VERR_PAGE_NOT_PRESENT:
2961 case VERR_PAGE_TABLE_NOT_PRESENT:
2962 uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
2963 break;
2964
2965 default:
2966 AssertMsgFailed(("rc=%Rrc GCPtrSrc=%RGv cb=%#x\n", rc, GCPtrSrc, cb));
2967 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
2968 return rc;
2969 }
2970 if (fRaiseTrap)
2971 {
2972 Log(("PGMPhysInterpretedReadNoHandlers: GCPtrSrc=%RGv cb=%#x -> Raised #PF(%#x)\n", GCPtrSrc, cb, uErr));
2973 return TRPMRaiseXcptErrCR2(pVCpu, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
2974 }
2975 Log(("PGMPhysInterpretedReadNoHandlers: GCPtrSrc=%RGv cb=%#x -> #PF(%#x) [!raised]\n", GCPtrSrc, cb, uErr));
2976 return rc;
2977}
2978
2979
2980/**
2981 * Performs a write to guest virtual memory for instruction emulation.
2982 *
2983 * This will check permissions, raise exceptions and update the dirty and access
2984 * bits.
2985 *
2986 * @returns VBox status code suitable to scheduling.
2987 * @retval VINF_SUCCESS if the read was performed successfully.
2988 * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
2989 * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
2990 *
2991 * @param pVCpu The VMCPU handle.
2992 * @param pCtxCore The context core.
2993 * @param GCPtrDst The destination address.
2994 * @param pvSrc What to write.
2995 * @param cb The number of bytes to write. Not more than a page.
2996 * @param fRaiseTrap If set the trap will be raised on as per spec, if clear
2997 * an appropriate error status will be returned (no
2998 * informational at all).
2999 *
3000 * @remarks Takes the PGM lock.
3001 * @remarks A page fault on the 2nd page of the access will be raised without
3002 * writing the bits on the first page since we're ASSUMING that the
3003 * caller is emulating an instruction access.
3004 * @remarks This function will dynamically map physical pages in GC. This may
3005 * unmap mappings done by the caller. Be careful!
3006 */
3007VMMDECL(int) PGMPhysInterpretedWriteNoHandlers(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb, bool fRaiseTrap)
3008{
3009 Assert(cb <= PAGE_SIZE);
3010 PVM pVM = pVCpu->CTX_SUFF(pVM);
3011
3012 /*
3013 * 1. Translate virtual to physical. This may fault.
3014 * 2. Map the physical address.
3015 * 3. Do the write operation.
3016 * 4. Set access bits if required.
3017 */
3018 int rc;
3019 unsigned cb1 = PAGE_SIZE - (GCPtrDst & PAGE_OFFSET_MASK);
3020 if (cb <= cb1)
3021 {
3022 /*
3023 * Not crossing pages.
3024 */
3025 RTGCPHYS GCPhys;
3026 uint64_t fFlags;
3027 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrDst, &fFlags, &GCPhys);
3028 if (RT_SUCCESS(rc))
3029 {
3030 if ( (fFlags & X86_PTE_RW) /** @todo Also check reserved bits. */
3031 || ( !(CPUMGetGuestCR0(pVCpu) & X86_CR0_WP)
3032 && CPUMGetGuestCPL(pVCpu, pCtxCore) <= 2) ) /** @todo it's 2, right? Check cpl check below as well. */
3033 {
3034 void *pvDst;
3035 PGMPAGEMAPLOCK Lock;
3036 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys, &pvDst, &Lock);
3037 switch (rc)
3038 {
3039 case VINF_SUCCESS:
3040 Log(("PGMPhysInterpretedWriteNoHandlers: pvDst=%p (%RGv) pvSrc=%p cb=%d\n",
3041 (uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), GCPtrDst, pvSrc, cb));
3042 memcpy((uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), pvSrc, cb);
3043 PGMPhysReleasePageMappingLock(pVM, &Lock);
3044 break;
3045 case VERR_PGM_PHYS_PAGE_RESERVED:
3046 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3047 /* bit bucket */
3048 break;
3049 default:
3050 AssertMsgFailed(("%Rrc\n", rc));
3051 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3052 return rc;
3053 }
3054
3055 if (!(fFlags & (X86_PTE_A | X86_PTE_D)))
3056 {
3057 /** @todo dirty & access bit emulation isn't 100% correct. */
3058 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
3059 AssertRC(rc);
3060 }
3061 return VINF_SUCCESS;
3062 }
3063 rc = VERR_ACCESS_DENIED;
3064 }
3065 }
3066 else
3067 {
3068 /*
3069 * Crosses pages.
3070 */
3071 size_t cb2 = cb - cb1;
3072 uint64_t fFlags1;
3073 RTGCPHYS GCPhys1;
3074 uint64_t fFlags2;
3075 RTGCPHYS GCPhys2;
3076 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrDst, &fFlags1, &GCPhys1);
3077 if (RT_SUCCESS(rc))
3078 {
3079 rc = PGM_GST_PFN(GetPage,pVCpu)(pVCpu, GCPtrDst + cb1, &fFlags2, &GCPhys2);
3080 if (RT_SUCCESS(rc))
3081 {
3082 if ( ( (fFlags1 & X86_PTE_RW) /** @todo Also check reserved bits. */
3083 && (fFlags2 & X86_PTE_RW))
3084 || ( !(CPUMGetGuestCR0(pVCpu) & X86_CR0_WP)
3085 && CPUMGetGuestCPL(pVCpu, pCtxCore) <= 2) )
3086 {
3087 void *pvDst;
3088 PGMPAGEMAPLOCK Lock;
3089 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys1, &pvDst, &Lock);
3090 switch (rc)
3091 {
3092 case VINF_SUCCESS:
3093 Log(("PGMPhysInterpretedWriteNoHandlers: pvDst=%p (%RGv) pvSrc=%p cb=%d\n",
3094 (uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), GCPtrDst, pvSrc, cb1));
3095 memcpy((uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), pvSrc, cb1);
3096 PGMPhysReleasePageMappingLock(pVM, &Lock);
3097 break;
3098 case VERR_PGM_PHYS_PAGE_RESERVED:
3099 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3100 /* bit bucket */
3101 break;
3102 default:
3103 AssertMsgFailed(("%Rrc\n", rc));
3104 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3105 return rc;
3106 }
3107
3108 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys2, &pvDst, &Lock);
3109 switch (rc)
3110 {
3111 case VINF_SUCCESS:
3112 memcpy(pvDst, (const uint8_t *)pvSrc + cb1, cb2);
3113 PGMPhysReleasePageMappingLock(pVM, &Lock);
3114 break;
3115 case VERR_PGM_PHYS_PAGE_RESERVED:
3116 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3117 /* bit bucket */
3118 break;
3119 default:
3120 AssertMsgFailed(("%Rrc\n", rc));
3121 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3122 return rc;
3123 }
3124
3125 if (!(fFlags1 & (X86_PTE_A | X86_PTE_RW)))
3126 {
3127 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrDst, 1, (X86_PTE_A | X86_PTE_RW), ~(uint64_t)(X86_PTE_A | X86_PTE_RW));
3128 AssertRC(rc);
3129 }
3130 if (!(fFlags2 & (X86_PTE_A | X86_PTE_RW)))
3131 {
3132 rc = PGM_GST_PFN(ModifyPage,pVCpu)(pVCpu, GCPtrDst + cb1, 1, (X86_PTE_A | X86_PTE_RW), ~(uint64_t)(X86_PTE_A | X86_PTE_RW));
3133 AssertRC(rc);
3134 }
3135 return VINF_SUCCESS;
3136 }
3137 if ((fFlags1 & (X86_PTE_RW)) == X86_PTE_RW)
3138 GCPtrDst += cb1; /* fault on the 2nd page. */
3139 rc = VERR_ACCESS_DENIED;
3140 }
3141 else
3142 GCPtrDst += cb1; /* fault on the 2nd page. */
3143 }
3144 }
3145
3146 /*
3147 * Raise a #PF if we're allowed to do that.
3148 */
3149 /* Calc the error bits. */
3150 uint32_t uErr;
3151 uint32_t cpl = CPUMGetGuestCPL(pVCpu, pCtxCore);
3152 switch (rc)
3153 {
3154 case VINF_SUCCESS:
3155 uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
3156 rc = VERR_ACCESS_DENIED;
3157 break;
3158
3159 case VERR_ACCESS_DENIED:
3160 uErr = (cpl >= 2) ? X86_TRAP_PF_RW | X86_TRAP_PF_US : X86_TRAP_PF_RW;
3161 break;
3162
3163 case VERR_PAGE_NOT_PRESENT:
3164 case VERR_PAGE_TABLE_NOT_PRESENT:
3165 uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
3166 break;
3167
3168 default:
3169 AssertMsgFailed(("rc=%Rrc GCPtrDst=%RGv cb=%#x\n", rc, GCPtrDst, cb));
3170 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
3171 return rc;
3172 }
3173 if (fRaiseTrap)
3174 {
3175 Log(("PGMPhysInterpretedWriteNoHandlers: GCPtrDst=%RGv cb=%#x -> Raised #PF(%#x)\n", GCPtrDst, cb, uErr));
3176 return TRPMRaiseXcptErrCR2(pVCpu, pCtxCore, X86_XCPT_PF, uErr, GCPtrDst);
3177 }
3178 Log(("PGMPhysInterpretedWriteNoHandlers: GCPtrDst=%RGv cb=%#x -> #PF(%#x) [!raised]\n", GCPtrDst, cb, uErr));
3179 return rc;
3180}
3181
3182
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