VirtualBox

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

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

fix

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