VirtualBox

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

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

PGM: Made PGMPhysRead/Write return VERR_PGM_PHYS_WR_HIT_HANDLER when encountering a handler in R0 and RC. Adjusted PGMPhysReadGCPtr and PGMPhysWriteGCPtr to not waste time on setting A and D bits if they are already set.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 136.3 KB
Line 
1/* $Id: PGMAllPhys.cpp 17534 2009-03-08 03:05:52Z 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 AssertMsg(rc == VINF_EM_NO_MEMORY || rc == VERR_EM_NO_MEMORY, ("%Rrc\n", rc));
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 * @returns VBox status code. Can be ignored in ring-3.
1402 * @retval VINF_SUCCESS.
1403 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
1404 *
1405 * @param pVM The VM handle.
1406 * @param pPage The page descriptor.
1407 * @param GCPhys The physical address to start reading at.
1408 * @param pvBuf Where to put the bits we read.
1409 * @param cb How much to read - less or equal to a page.
1410 */
1411static int pgmPhysReadHandler(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void *pvBuf, size_t cb)
1412{
1413 /*
1414 * The most frequent access here is MMIO and shadowed ROM.
1415 * The current code ASSUMES all these access handlers covers full pages!
1416 */
1417
1418 /*
1419 * Whatever we do we need the source page, map it first.
1420 */
1421 const void *pvSrc = NULL;
1422 int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, GCPhys, &pvSrc);
1423 if (RT_FAILURE(rc))
1424 {
1425 AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
1426 GCPhys, pPage, rc));
1427 memset(pvBuf, 0xff, cb);
1428 return VINF_SUCCESS;
1429 }
1430 rc = VINF_PGM_HANDLER_DO_DEFAULT;
1431
1432 /*
1433 * Deal with any physical handlers.
1434 */
1435 PPGMPHYSHANDLER pPhys = NULL;
1436 if (PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) == PGM_PAGE_HNDL_PHYS_STATE_ALL)
1437 {
1438#ifdef IN_RING3
1439 PPGMPHYSHANDLER pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1440 AssertReleaseMsg(pPhys, ("GCPhys=%RGp cb=%#x\n", GCPhys, cb));
1441 Assert(GCPhys >= pPhys->Core.Key && GCPhys <= pPhys->Core.KeyLast);
1442 Assert((pPhys->Core.Key & PAGE_OFFSET_MASK) == 0);
1443 Assert((pPhys->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
1444 Assert(pPhys->CTX_SUFF(pfnHandler));
1445
1446 Log5(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cb, pPage, R3STRING(pPhys->pszDesc) ));
1447 STAM_PROFILE_START(&pPhys->Stat, h);
1448 rc = pPhys->CTX_SUFF(pfnHandler)(pVM, GCPhys, (void *)pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, pPhys->CTX_SUFF(pvUser));
1449 STAM_PROFILE_STOP(&pPhys->Stat, h);
1450 AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp\n", rc, GCPhys));
1451#else
1452 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1453 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cb=%#x\n", GCPhys, cb));
1454 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1455#endif
1456 }
1457
1458 /*
1459 * Deal with any virtual handlers.
1460 */
1461 if (PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) == PGM_PAGE_HNDL_VIRT_STATE_ALL)
1462 {
1463 unsigned iPage;
1464 PPGMVIRTHANDLER pVirt;
1465
1466 int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pVirt, &iPage);
1467 AssertReleaseMsg(RT_SUCCESS(rc2), ("GCPhys=%RGp cb=%#x rc2=%Rrc\n", GCPhys, cb, rc2));
1468 Assert((pVirt->Core.Key & PAGE_OFFSET_MASK) == 0);
1469 Assert((pVirt->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
1470 Assert(GCPhys >= pVirt->aPhysToVirt[iPage].Core.Key && GCPhys <= pVirt->aPhysToVirt[iPage].Core.KeyLast);
1471
1472#ifdef IN_RING3
1473 if (pVirt->pfnHandlerR3)
1474 {
1475 if (!pPhys)
1476 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] virt %s\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc) ));
1477 else
1478 Log(("pgmPhysWriteHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] phys/virt %s/%s\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc), R3STRING(pPhys->pszDesc) ));
1479 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
1480 + (iPage << PAGE_SHIFT)
1481 + (GCPhys & PAGE_OFFSET_MASK);
1482
1483 STAM_PROFILE_START(&pVirt->Stat, h);
1484 rc2 = pVirt->CTX_SUFF(pfnHandler)(pVM, GCPtr, (void *)pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, /*pVirt->CTX_SUFF(pvUser)*/ NULL);
1485 STAM_PROFILE_STOP(&pVirt->Stat, h);
1486 if (rc2 == VINF_SUCCESS)
1487 rc = VINF_SUCCESS;
1488 AssertLogRelMsg(rc2 == VINF_SUCCESS || rc2 == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc2, GCPhys, pPage, pVirt->pszDesc));
1489 }
1490 else
1491 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] virt %s [no handler]\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc) ));
1492#else
1493 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1494 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cb=%#x\n", GCPhys, cb));
1495 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1496#endif
1497 }
1498
1499 /*
1500 * Take the default action.
1501 */
1502 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
1503 memcpy(pvBuf, pvSrc, cb);
1504 return rc;
1505}
1506
1507
1508/**
1509 * Read physical memory.
1510 *
1511 * This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
1512 * want to ignore those.
1513 *
1514 * @returns VBox status code. Can be ignored in ring-3.
1515 * @retval VINF_SUCCESS.
1516 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
1517 *
1518 * @param pVM VM Handle.
1519 * @param GCPhys Physical address start reading from.
1520 * @param pvBuf Where to put the read bits.
1521 * @param cbRead How many bytes to read.
1522 */
1523VMMDECL(int) PGMPhysRead(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
1524{
1525 AssertMsgReturn(cbRead > 0, ("don't even think about reading zero bytes!\n"), VINF_SUCCESS);
1526 LogFlow(("PGMPhysRead: %RGp %d\n", GCPhys, cbRead));
1527
1528 pgmLock(pVM);
1529
1530 /*
1531 * Copy loop on ram ranges.
1532 */
1533 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
1534 for (;;)
1535 {
1536 /* Find range. */
1537 while (pRam && GCPhys > pRam->GCPhysLast)
1538 pRam = pRam->CTX_SUFF(pNext);
1539 /* Inside range or not? */
1540 if (pRam && GCPhys >= pRam->GCPhys)
1541 {
1542 /*
1543 * Must work our way thru this page by page.
1544 */
1545 RTGCPHYS off = GCPhys - pRam->GCPhys;
1546 while (off < pRam->cb)
1547 {
1548 unsigned iPage = off >> PAGE_SHIFT;
1549 PPGMPAGE pPage = &pRam->aPages[iPage];
1550 size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
1551 if (cb > cbRead)
1552 cb = cbRead;
1553
1554 /*
1555 * Any ALL access handlers?
1556 */
1557 if (RT_UNLIKELY(PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage)))
1558 {
1559 int rc = pgmPhysReadHandler(pVM, pPage, pRam->GCPhys + off, pvBuf, cb);
1560 if (RT_FAILURE(rc))
1561 return rc;
1562 }
1563 else
1564 {
1565 /*
1566 * Get the pointer to the page.
1567 */
1568 const void *pvSrc;
1569 int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, pRam->GCPhys + off, &pvSrc);
1570 if (RT_SUCCESS(rc))
1571 memcpy(pvBuf, pvSrc, cb);
1572 else
1573 {
1574 AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
1575 pRam->GCPhys + off, pPage, rc));
1576 memset(pvBuf, 0xff, cb);
1577 }
1578 }
1579
1580 /* next page */
1581 if (cb >= cbRead)
1582 {
1583 pgmUnlock(pVM);
1584 return VINF_SUCCESS;
1585 }
1586 cbRead -= cb;
1587 off += cb;
1588 pvBuf = (char *)pvBuf + cb;
1589 } /* walk pages in ram range. */
1590
1591 GCPhys = pRam->GCPhysLast + 1;
1592 }
1593 else
1594 {
1595 LogFlow(("PGMPhysRead: Unassigned %RGp size=%u\n", GCPhys, cbRead));
1596
1597 /*
1598 * Unassigned address space.
1599 */
1600 if (!pRam)
1601 break;
1602 size_t cb = pRam->GCPhys - GCPhys;
1603 if (cb >= cbRead)
1604 {
1605 memset(pvBuf, 0xff, cbRead);
1606 break;
1607 }
1608 memset(pvBuf, 0xff, cb);
1609
1610 cbRead -= cb;
1611 pvBuf = (char *)pvBuf + cb;
1612 GCPhys += cb;
1613 }
1614 } /* Ram range walk */
1615
1616 pgmUnlock(pVM);
1617 return VINF_SUCCESS;
1618}
1619
1620#else /* Old PGMPhysRead */
1621
1622/**
1623 * Read physical memory.
1624 *
1625 * This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
1626 * want to ignore those.
1627 *
1628 * @param pVM VM Handle.
1629 * @param GCPhys Physical address start reading from.
1630 * @param pvBuf Where to put the read bits.
1631 * @param cbRead How many bytes to read.
1632 */
1633VMMDECL(void) PGMPhysRead(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
1634{
1635#ifdef IN_RING3
1636 bool fGrabbedLock = false;
1637#endif
1638
1639 AssertMsg(cbRead > 0, ("don't even think about reading zero bytes!\n"));
1640 if (cbRead == 0)
1641 return;
1642
1643 LogFlow(("PGMPhysRead: %RGp %d\n", GCPhys, cbRead));
1644
1645#ifdef IN_RING3
1646 if (!VM_IS_EMT(pVM))
1647 {
1648 pgmLock(pVM);
1649 fGrabbedLock = true;
1650 }
1651#endif
1652
1653 /*
1654 * Copy loop on ram ranges.
1655 */
1656 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
1657 for (;;)
1658 {
1659 /* Find range. */
1660 while (pRam && GCPhys > pRam->GCPhysLast)
1661 pRam = pRam->CTX_SUFF(pNext);
1662 /* Inside range or not? */
1663 if (pRam && GCPhys >= pRam->GCPhys)
1664 {
1665 /*
1666 * Must work our way thru this page by page.
1667 */
1668 RTGCPHYS off = GCPhys - pRam->GCPhys;
1669 while (off < pRam->cb)
1670 {
1671 unsigned iPage = off >> PAGE_SHIFT;
1672 PPGMPAGE pPage = &pRam->aPages[iPage];
1673 size_t cb;
1674
1675 /* Physical chunk in dynamically allocated range not present? */
1676 if (RT_UNLIKELY(!PGM_PAGE_GET_HCPHYS(pPage)))
1677 {
1678 /* Treat it as reserved; return zeros */
1679 cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
1680 if (cb >= cbRead)
1681 {
1682 memset(pvBuf, 0, cbRead);
1683 goto l_End;
1684 }
1685 memset(pvBuf, 0, cb);
1686 }
1687 /* temp hacks, will be reorganized. */
1688 /*
1689 * Physical handler.
1690 */
1691 else if ( RT_UNLIKELY(PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) >= PGM_PAGE_HNDL_PHYS_STATE_ALL)
1692 && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
1693 {
1694 int rc = VINF_PGM_HANDLER_DO_DEFAULT;
1695 cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
1696
1697#ifdef IN_RING3 /** @todo deal with this in GC and R0! */
1698 /* find and call the handler */
1699 PPGMPHYSHANDLER pNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesR3->PhysHandlers, GCPhys);
1700 if (pNode && pNode->pfnHandlerR3)
1701 {
1702 size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
1703 if (cbRange < cb)
1704 cb = cbRange;
1705 if (cb > cbRead)
1706 cb = cbRead;
1707
1708 void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
1709
1710 /* Note! Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
1711 rc = pNode->pfnHandlerR3(pVM, GCPhys, pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, pNode->pvUserR3);
1712 }
1713#endif /* IN_RING3 */
1714 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
1715 {
1716#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1717 void *pvSrc = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
1718#else
1719 void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
1720#endif
1721
1722 if (cb >= cbRead)
1723 {
1724 memcpy(pvBuf, pvSrc, cbRead);
1725 goto l_End;
1726 }
1727 memcpy(pvBuf, pvSrc, cb);
1728 }
1729 else if (cb >= cbRead)
1730 goto l_End;
1731 }
1732 /*
1733 * Virtual handlers.
1734 */
1735 else if ( RT_UNLIKELY(PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) >= PGM_PAGE_HNDL_VIRT_STATE_ALL)
1736 && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
1737 {
1738 int rc = VINF_PGM_HANDLER_DO_DEFAULT;
1739 cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
1740#ifdef IN_RING3 /** @todo deal with this in GC and R0! */
1741 /* Search the whole tree for matching physical addresses (rather expensive!) */
1742 PPGMVIRTHANDLER pNode;
1743 unsigned iPage;
1744 int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pNode, &iPage);
1745 if (RT_SUCCESS(rc2) && pNode->pfnHandlerR3)
1746 {
1747 size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
1748 if (cbRange < cb)
1749 cb = cbRange;
1750 if (cb > cbRead)
1751 cb = cbRead;
1752 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pNode->Core.Key & PAGE_BASE_GC_MASK)
1753 + (iPage << PAGE_SHIFT) + (off & PAGE_OFFSET_MASK);
1754
1755 void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
1756
1757 /* Note! Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
1758 rc = pNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, 0);
1759 }
1760#endif /* IN_RING3 */
1761 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
1762 {
1763#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1764 void *pvSrc = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
1765#else
1766 void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
1767#endif
1768 if (cb >= cbRead)
1769 {
1770 memcpy(pvBuf, pvSrc, cbRead);
1771 goto l_End;
1772 }
1773 memcpy(pvBuf, pvSrc, cb);
1774 }
1775 else if (cb >= cbRead)
1776 goto l_End;
1777 }
1778 else
1779 {
1780 switch (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM)) /** @todo PAGE FLAGS */
1781 {
1782 /*
1783 * Normal memory or ROM.
1784 */
1785 case 0:
1786 case MM_RAM_FLAGS_ROM:
1787 case MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_RESERVED:
1788 //case MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2: /* = shadow */ - //MMIO2 isn't in the mask.
1789 case MM_RAM_FLAGS_MMIO2: // MMIO2 isn't in the mask.
1790 {
1791#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1792 void *pvSrc = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
1793#else
1794 void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
1795#endif
1796 cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
1797 if (cb >= cbRead)
1798 {
1799#if defined(IN_RING3) && defined(PGM_PHYSMEMACCESS_CACHING)
1800 if (cbRead <= 4 && !fGrabbedLock /* i.e. EMT */)
1801 pgmPhysCacheAdd(pVM, &pVM->pgm.s.pgmphysreadcache, GCPhys, (uint8_t*)pvSrc);
1802#endif /* IN_RING3 && PGM_PHYSMEMACCESS_CACHING */
1803 memcpy(pvBuf, pvSrc, cbRead);
1804 goto l_End;
1805 }
1806 memcpy(pvBuf, pvSrc, cb);
1807 break;
1808 }
1809
1810 /*
1811 * All reserved, nothing there.
1812 */
1813 case MM_RAM_FLAGS_RESERVED:
1814 cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
1815 if (cb >= cbRead)
1816 {
1817 memset(pvBuf, 0, cbRead);
1818 goto l_End;
1819 }
1820 memset(pvBuf, 0, cb);
1821 break;
1822
1823 /*
1824 * The rest needs to be taken more carefully.
1825 */
1826 default:
1827#if 1 /** @todo r=bird: Can you do this properly please. */
1828 /** @todo Try MMIO; quick hack */
1829 if (cbRead <= 8 && IOMMMIORead(pVM, GCPhys, (uint32_t *)pvBuf, cbRead) == VINF_SUCCESS)
1830 goto l_End;
1831#endif
1832
1833 /** @todo fix me later. */
1834 AssertReleaseMsgFailed(("Unknown read at %RGp size %u implement the complex physical reading case %RHp\n",
1835 GCPhys, cbRead,
1836 pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM))); /** @todo PAGE FLAGS */
1837 cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
1838 break;
1839 }
1840 }
1841
1842 cbRead -= cb;
1843 off += cb;
1844 pvBuf = (char *)pvBuf + cb;
1845 }
1846
1847 GCPhys = pRam->GCPhysLast + 1;
1848 }
1849 else
1850 {
1851 LogFlow(("PGMPhysRead: Unassigned %RGp size=%u\n", GCPhys, cbRead));
1852
1853 /*
1854 * Unassigned address space.
1855 */
1856 size_t cb;
1857 if ( !pRam
1858 || (cb = pRam->GCPhys - GCPhys) >= cbRead)
1859 {
1860 memset(pvBuf, 0, cbRead);
1861 goto l_End;
1862 }
1863
1864 memset(pvBuf, 0, cb); /** @todo this is wrong, unassigne == 0xff not 0x00! */
1865 cbRead -= cb;
1866 pvBuf = (char *)pvBuf + cb;
1867 GCPhys += cb;
1868 }
1869 }
1870l_End:
1871#ifdef IN_RING3
1872 if (fGrabbedLock)
1873 pgmUnlock(pVM);
1874#endif
1875 return;
1876}
1877
1878#endif /* Old PGMPhysRead */
1879#ifdef VBOX_WITH_NEW_PHYS_CODE
1880
1881/**
1882 * Deals with writing to a page with one or more WRITE or ALL access handlers.
1883 *
1884 * @returns VBox status code. Can be ignored in ring-3.
1885 * @retval VINF_SUCCESS.
1886 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
1887 *
1888 * @param pVM The VM handle.
1889 * @param pPage The page descriptor.
1890 * @param GCPhys The physical address to start writing at.
1891 * @param pvBuf What to write.
1892 * @param cbWrite How much to write - less or equal to a page.
1893 */
1894static int pgmPhysWriteHandler(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void const *pvBuf, size_t cbWrite)
1895{
1896 void *pvDst = NULL;
1897 int rc;
1898
1899 /*
1900 * Give priority to physical handlers (like #PF does).
1901 *
1902 * Hope for a lonely physical handler first that covers the whole
1903 * write area. This should be a pretty frequent case with MMIO and
1904 * the heavy usage of full page handlers in the page pool.
1905 */
1906 if ( !PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage)
1907 || PGM_PAGE_IS_MMIO(pPage) /* screw virtual handlers on MMIO pages */)
1908 {
1909 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1910 if (pCur)
1911 {
1912 Assert(GCPhys >= pCur->Core.Key && GCPhys <= pCur->Core.KeyLast);
1913 Assert(pCur->CTX_SUFF(pfnHandler));
1914
1915 size_t cbRange = pCur->Core.KeyLast - GCPhys + 1;
1916 if (cbRange > cbWrite)
1917 cbRange = cbWrite;
1918
1919#ifndef IN_RING3
1920 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1921 NOREF(cbRange);
1922 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
1923 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1924
1925#else /* IN_RING3 */
1926 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pCur->pszDesc) ));
1927 if (!PGM_PAGE_IS_MMIO(pPage))
1928 rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst);
1929 else
1930 rc = VINF_SUCCESS;
1931 if (RT_SUCCESS(rc))
1932 {
1933 STAM_PROFILE_START(&pCur->Stat, h);
1934 rc = pCur->CTX_SUFF(pfnHandler)(pVM, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, pCur->CTX_SUFF(pvUser));
1935 STAM_PROFILE_STOP(&pCur->Stat, h);
1936 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
1937 memcpy(pvDst, pvBuf, cbRange);
1938 else
1939 AssertLogRelMsg(rc == VINF_SUCCESS, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pCur->pszDesc));
1940 }
1941 else
1942 AssertLogRelMsgFailedReturn(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
1943 GCPhys, pPage, rc), rc);
1944 if (RT_LIKELY(cbRange == cbWrite))
1945 return VINF_SUCCESS;
1946
1947 /* more fun to be had below */
1948 cbWrite -= cbRange;
1949 GCPhys += cbRange;
1950 pvBuf = (uint8_t *)pvBuf + cbRange;
1951 pvDst = (uint8_t *)pvDst + cbRange;
1952#endif /* IN_RING3 */
1953 }
1954 /* else: the handler is somewhere else in the page, deal with it below. */
1955 Assert(!PGM_PAGE_IS_MMIO(pPage)); /* MMIO handlers are all PAGE_SIZEed! */
1956 }
1957 /*
1958 * A virtual handler without any interfering physical handlers.
1959 * Hopefully it'll conver the whole write.
1960 */
1961 else if (!PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage))
1962 {
1963 unsigned iPage;
1964 PPGMVIRTHANDLER pCur;
1965 rc = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pCur, &iPage);
1966 if (RT_SUCCESS(rc))
1967 {
1968 size_t cbRange = (PAGE_OFFSET_MASK & pCur->Core.KeyLast) - (PAGE_OFFSET_MASK & GCPhys) + 1;
1969 if (cbRange > cbWrite)
1970 cbRange = cbWrite;
1971
1972#ifndef IN_RING3
1973 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
1974 NOREF(cbRange);
1975 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
1976 return VERR_PGM_PHYS_WR_HIT_HANDLER;
1977
1978#else /* IN_RING3 */
1979
1980 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] virt %s\n", GCPhys, cbRange, pPage, R3STRING(pCur->pszDesc) ));
1981 rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst);
1982 if (RT_SUCCESS(rc))
1983 {
1984 rc = VINF_PGM_HANDLER_DO_DEFAULT;
1985 if (pCur->pfnHandlerR3)
1986 {
1987 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pCur->Core.Key & PAGE_BASE_GC_MASK)
1988 + (iPage << PAGE_SHIFT)
1989 + (GCPhys & PAGE_OFFSET_MASK);
1990
1991 STAM_PROFILE_START(&pCur->Stat, h);
1992 rc = pCur->CTX_SUFF(pfnHandler)(pVM, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, /*pCur->CTX_SUFF(pvUser)*/ NULL);
1993 STAM_PROFILE_STOP(&pCur->Stat, h);
1994 }
1995 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
1996 memcpy(pvDst, pvBuf, cbRange);
1997 else
1998 AssertLogRelMsg(rc == VINF_SUCCESS, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pCur->pszDesc));
1999 }
2000 else
2001 AssertLogRelMsgFailedReturn(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
2002 GCPhys, pPage, rc), rc);
2003 if (RT_LIKELY(cbRange == cbWrite))
2004 return VINF_SUCCESS;
2005
2006 /* more fun to be had below */
2007 cbWrite -= cbRange;
2008 GCPhys += cbRange;
2009 pvBuf = (uint8_t *)pvBuf + cbRange;
2010 pvDst = (uint8_t *)pvDst + cbRange;
2011#endif
2012 }
2013 /* else: the handler is somewhere else in the page, deal with it below. */
2014 }
2015
2016 /*
2017 * Deal with all the odd ends.
2018 */
2019
2020 /* We need a writable destination page. */
2021 if (!pvDst)
2022 {
2023 rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst);
2024 AssertLogRelMsgReturn(RT_SUCCESS(rc),
2025 ("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
2026 GCPhys, pPage, rc), rc);
2027 }
2028
2029 /* The loop state (big + ugly). */
2030 unsigned iVirtPage = 0;
2031 PPGMVIRTHANDLER pVirt = NULL;
2032 uint32_t offVirt = PAGE_SIZE;
2033 uint32_t offVirtLast = PAGE_SIZE;
2034 bool fMoreVirt = PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage);
2035
2036 PPGMPHYSHANDLER pPhys = NULL;
2037 uint32_t offPhys = PAGE_SIZE;
2038 uint32_t offPhysLast = PAGE_SIZE;
2039 bool fMorePhys = PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage);
2040
2041 /* The loop. */
2042 for (;;)
2043 {
2044 /*
2045 * Find the closest handler at or above GCPhys.
2046 */
2047 if (fMoreVirt && !pVirt)
2048 {
2049 int rc = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pVirt, &iVirtPage);
2050 if (RT_SUCCESS(rc))
2051 {
2052 offVirt = 0;
2053 offVirtLast = (pVirt->aPhysToVirt[iVirtPage].Core.KeyLast & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
2054 }
2055 else
2056 {
2057 PPGMPHYS2VIRTHANDLER pVirtPhys;
2058 pVirtPhys = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers,
2059 GCPhys, true /* fAbove */);
2060 if ( pVirtPhys
2061 && (pVirtPhys->Core.Key >> PAGE_SHIFT) == (GCPhys >> PAGE_SHIFT))
2062 {
2063 /* ASSUME that pVirtPhys only covers one page. */
2064 Assert((pVirtPhys->Core.Key >> PAGE_SHIFT) == (pVirtPhys->Core.KeyLast >> PAGE_SHIFT));
2065 Assert(pVirtPhys->Core.Key > GCPhys);
2066
2067 pVirt = (PPGMVIRTHANDLER)((uintptr_t)pVirtPhys + pVirtPhys->offVirtHandler);
2068 iVirtPage = pVirtPhys - &pVirt->aPhysToVirt[0]; Assert(iVirtPage == 0);
2069 offVirt = (pVirtPhys->Core.Key & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
2070 offVirtLast = (pVirtPhys->Core.KeyLast & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
2071 }
2072 else
2073 {
2074 pVirt = NULL;
2075 fMoreVirt = false;
2076 offVirt = offVirtLast = PAGE_SIZE;
2077 }
2078 }
2079 }
2080
2081 if (fMorePhys && !pPhys)
2082 {
2083 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
2084 if (pPhys)
2085 {
2086 offPhys = 0;
2087 offPhysLast = pPhys->Core.KeyLast - GCPhys; /* ASSUMES < 4GB handlers... */
2088 }
2089 else
2090 {
2091 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers,
2092 GCPhys, true /* fAbove */);
2093 if ( pPhys
2094 && pPhys->Core.Key <= GCPhys + (cbWrite - 1))
2095 {
2096 offPhys = pPhys->Core.Key - GCPhys;
2097 offPhysLast = pPhys->Core.KeyLast - GCPhys; /* ASSUMES < 4GB handlers... */
2098 }
2099 else
2100 {
2101 pPhys = NULL;
2102 fMorePhys = false;
2103 offPhys = offPhysLast = PAGE_SIZE;
2104 }
2105 }
2106 }
2107
2108 /*
2109 * Handle access to space without handlers (that's easy).
2110 */
2111 rc = VINF_PGM_HANDLER_DO_DEFAULT;
2112 uint32_t cbRange = (uint32_t)cbWrite;
2113 if (offPhys && offVirt)
2114 {
2115 if (cbRange > offPhys)
2116 cbRange = offPhys;
2117 if (cbRange > offVirt)
2118 cbRange = offVirt;
2119 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] miss\n", GCPhys, cbRange, pPage));
2120 }
2121 /*
2122 * Physical handler.
2123 */
2124 else if (!offPhys && offVirt)
2125 {
2126 if (cbRange > offPhysLast + 1)
2127 cbRange = offPhysLast + 1;
2128 if (cbRange > offVirt)
2129 cbRange = offVirt;
2130#ifdef IN_RING3
2131 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pPhys->pszDesc) ));
2132 STAM_PROFILE_START(&pPhys->Stat, h);
2133 rc = pPhys->CTX_SUFF(pfnHandler)(pVM, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, pPhys->CTX_SUFF(pvUser));
2134 STAM_PROFILE_STOP(&pPhys->Stat, h);
2135 AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pPhys->pszDesc));
2136 pPhys = NULL;
2137#else
2138 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
2139 NOREF(cbRange);
2140 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
2141 return VERR_PGM_PHYS_WR_HIT_HANDLER;
2142#endif
2143 }
2144 /*
2145 * Virtual handler.
2146 */
2147 else if (offPhys && !offVirt)
2148 {
2149 if (cbRange > offVirtLast + 1)
2150 cbRange = offVirtLast + 1;
2151 if (cbRange > offPhys)
2152 cbRange = offPhys;
2153#ifdef IN_RING3
2154 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pVirt->pszDesc) ));
2155 if (pVirt->pfnHandlerR3)
2156 {
2157 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
2158 + (iVirtPage << PAGE_SHIFT)
2159 + (GCPhys & PAGE_OFFSET_MASK);
2160 STAM_PROFILE_START(&pVirt->Stat, h);
2161 rc = pVirt->CTX_SUFF(pfnHandler)(pVM, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, /*pCur->CTX_SUFF(pvUser)*/ NULL);
2162 STAM_PROFILE_STOP(&pVirt->Stat, h);
2163 AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pVirt->pszDesc));
2164 }
2165 pVirt = NULL;
2166#else
2167 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
2168 NOREF(cbRange);
2169 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
2170 return VERR_PGM_PHYS_WR_HIT_HANDLER;
2171#endif
2172 }
2173 /*
2174 * Both... give the physical one priority.
2175 */
2176 else
2177 {
2178 Assert(!offPhys && !offVirt);
2179 if (cbRange > offVirtLast + 1)
2180 cbRange = offVirtLast + 1;
2181 if (cbRange > offPhysLast + 1)
2182 cbRange = offPhysLast + 1;
2183
2184#ifdef IN_RING3
2185 if (pVirt->pfnHandlerR3)
2186 Log(("pgmPhysWriteHandler: overlapping phys and virt handlers at %RGp %R[pgmpage]; cbRange=%#x\n", GCPhys, pPage, cbRange));
2187 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys/virt %s/%s\n", GCPhys, cbRange, pPage, R3STRING(pPhys->pszDesc), R3STRING(pVirt->pszDesc) ));
2188
2189 STAM_PROFILE_START(&pPhys->Stat, h);
2190 rc = pPhys->CTX_SUFF(pfnHandler)(pVM, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, pPhys->CTX_SUFF(pvUser));
2191 STAM_PROFILE_STOP(&pPhys->Stat, h);
2192 AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pPhys->pszDesc));
2193 if (pVirt->pfnHandlerR3)
2194 {
2195
2196 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
2197 + (iVirtPage << PAGE_SHIFT)
2198 + (GCPhys & PAGE_OFFSET_MASK);
2199 STAM_PROFILE_START(&pVirt->Stat, h);
2200 int rc2 = pVirt->CTX_SUFF(pfnHandler)(pVM, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, /*pCur->CTX_SUFF(pvUser)*/ NULL);
2201 STAM_PROFILE_STOP(&pVirt->Stat, h);
2202 AssertLogRelMsg(rc2 != VINF_SUCCESS && rc2 != VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pVirt->pszDesc));
2203 if (rc2 == VINF_SUCCESS && rc == VINF_PGM_HANDLER_DO_DEFAULT)
2204 rc = VINF_SUCCESS;
2205 }
2206 pPhys = NULL;
2207 pVirt = NULL;
2208#else
2209 /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
2210 NOREF(cbRange);
2211 //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
2212 return VERR_PGM_PHYS_WR_HIT_HANDLER;
2213#endif
2214 }
2215 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
2216 memcpy(pvDst, pvBuf, cbRange);
2217
2218 /*
2219 * Advance if we've got more stuff to do.
2220 */
2221 if (cbRange >= cbWrite)
2222 return VINF_SUCCESS;
2223
2224 cbWrite -= cbRange;
2225 GCPhys += cbRange;
2226 pvBuf = (uint8_t *)pvBuf + cbRange;
2227 pvDst = (uint8_t *)pvDst + cbRange;
2228
2229 offPhys -= cbRange;
2230 offPhysLast -= cbRange;
2231 offVirt -= cbRange;
2232 offVirtLast -= cbRange;
2233 }
2234}
2235
2236
2237/**
2238 * Write to physical memory.
2239 *
2240 * This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
2241 * want to ignore those.
2242 *
2243 * @returns VBox status code. Can be ignored in ring-3.
2244 * @retval VINF_SUCCESS.
2245 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
2246 *
2247 * @param pVM VM Handle.
2248 * @param GCPhys Physical address to write to.
2249 * @param pvBuf What to write.
2250 * @param cbWrite How many bytes to write.
2251 */
2252VMMDECL(int) PGMPhysWrite(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite)
2253{
2254 AssertMsg(!pVM->pgm.s.fNoMorePhysWrites, ("Calling PGMPhysWrite after pgmR3Save()!\n"));
2255 AssertMsgReturn(cbWrite > 0, ("don't even think about writing zero bytes!\n"), VINF_SUCCESS);
2256 LogFlow(("PGMPhysWrite: %RGp %d\n", GCPhys, cbWrite));
2257
2258 pgmLock(pVM);
2259
2260 /*
2261 * Copy loop on ram ranges.
2262 */
2263 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
2264 for (;;)
2265 {
2266 /* Find range. */
2267 while (pRam && GCPhys > pRam->GCPhysLast)
2268 pRam = pRam->CTX_SUFF(pNext);
2269 /* Inside range or not? */
2270 if (pRam && GCPhys >= pRam->GCPhys)
2271 {
2272 /*
2273 * Must work our way thru this page by page.
2274 */
2275 RTGCPTR off = GCPhys - pRam->GCPhys;
2276 while (off < pRam->cb)
2277 {
2278 RTGCPTR iPage = off >> PAGE_SHIFT;
2279 PPGMPAGE pPage = &pRam->aPages[iPage];
2280 size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
2281 if (cb > cbWrite)
2282 cb = cbWrite;
2283
2284 /*
2285 * Any active WRITE or ALL access handlers?
2286 */
2287 if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage))
2288 {
2289 int rc = pgmPhysWriteHandler(pVM, pPage, pRam->GCPhys + off, pvBuf, cb);
2290 if (RT_FAILURE(rc))
2291 return rc;
2292 }
2293 else
2294 {
2295 /*
2296 * Get the pointer to the page.
2297 */
2298 void *pvDst;
2299 int rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, pRam->GCPhys + off, &pvDst);
2300 if (RT_SUCCESS(rc))
2301 memcpy(pvDst, pvBuf, cb);
2302 else
2303 AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
2304 pRam->GCPhys + off, pPage, rc));
2305 }
2306
2307 /* next page */
2308 if (cb >= cbWrite)
2309 {
2310 pgmUnlock(pVM);
2311 return VINF_SUCCESS;
2312 }
2313
2314 cbWrite -= cb;
2315 off += cb;
2316 pvBuf = (const char *)pvBuf + cb;
2317 } /* walk pages in ram range */
2318
2319 GCPhys = pRam->GCPhysLast + 1;
2320 }
2321 else
2322 {
2323 /*
2324 * Unassigned address space, skip it.
2325 */
2326 if (!pRam)
2327 break;
2328 size_t cb = pRam->GCPhys - GCPhys;
2329 if (cb >= cbWrite)
2330 break;
2331 cbWrite -= cb;
2332 pvBuf = (const char *)pvBuf + cb;
2333 GCPhys += cb;
2334 }
2335 } /* Ram range walk */
2336
2337 pgmUnlock(pVM);
2338 return VINF_SUCCESS;
2339}
2340
2341#else /* Old PGMPhysWrite */
2342
2343/**
2344 * Write to physical memory.
2345 *
2346 * This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
2347 * want to ignore those.
2348 *
2349 * @param pVM VM Handle.
2350 * @param GCPhys Physical address to write to.
2351 * @param pvBuf What to write.
2352 * @param cbWrite How many bytes to write.
2353 */
2354VMMDECL(void) PGMPhysWrite(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite)
2355{
2356#ifdef IN_RING3
2357 bool fGrabbedLock = false;
2358#endif
2359
2360 AssertMsg(!pVM->pgm.s.fNoMorePhysWrites, ("Calling PGMPhysWrite after pgmR3Save()!\n"));
2361 AssertMsg(cbWrite > 0, ("don't even think about writing zero bytes!\n"));
2362 if (cbWrite == 0)
2363 return;
2364
2365 LogFlow(("PGMPhysWrite: %RGp %d\n", GCPhys, cbWrite));
2366
2367#ifdef IN_RING3
2368 if (!VM_IS_EMT(pVM))
2369 {
2370 pgmLock(pVM);
2371 fGrabbedLock = true;
2372 }
2373#endif
2374 /*
2375 * Copy loop on ram ranges.
2376 */
2377 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
2378 for (;;)
2379 {
2380 /* Find range. */
2381 while (pRam && GCPhys > pRam->GCPhysLast)
2382 pRam = pRam->CTX_SUFF(pNext);
2383 /* Inside range or not? */
2384 if (pRam && GCPhys >= pRam->GCPhys)
2385 {
2386 /*
2387 * Must work our way thru this page by page.
2388 */
2389 RTGCPTR off = GCPhys - pRam->GCPhys;
2390 while (off < pRam->cb)
2391 {
2392 RTGCPTR iPage = off >> PAGE_SHIFT;
2393 PPGMPAGE pPage = &pRam->aPages[iPage];
2394
2395 /* Physical chunk in dynamically allocated range not present? */
2396 if (RT_UNLIKELY(!PGM_PAGE_GET_HCPHYS(pPage)))
2397 {
2398 int rc;
2399#ifdef IN_RING3
2400 if (fGrabbedLock)
2401 {
2402 pgmUnlock(pVM);
2403 rc = pgmr3PhysGrowRange(pVM, GCPhys);
2404 if (rc == VINF_SUCCESS)
2405 PGMPhysWrite(pVM, GCPhys, pvBuf, cbWrite); /* try again; can't assume pRam is still valid (paranoia) */
2406 return;
2407 }
2408 rc = pgmr3PhysGrowRange(pVM, GCPhys);
2409#else
2410 rc = CTXALLMID(VMM, CallHost)(pVM, VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
2411#endif
2412 if (rc != VINF_SUCCESS)
2413 goto l_End;
2414 }
2415
2416 size_t cb;
2417 /* temporary hack, will reogranize is later. */
2418 /*
2419 * Virtual handlers
2420 */
2421 if ( PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage)
2422 && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
2423 {
2424 if (PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage))
2425 {
2426 /*
2427 * Physical write handler + virtual write handler.
2428 * Consider this a quick workaround for the CSAM + shadow caching problem.
2429 *
2430 * We hand it to the shadow caching first since it requires the unchanged
2431 * data. CSAM will have to put up with it already being changed.
2432 */
2433 int rc = VINF_PGM_HANDLER_DO_DEFAULT;
2434 cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
2435#ifdef IN_RING3 /** @todo deal with this in GC and R0! */
2436 /* 1. The physical handler */
2437 PPGMPHYSHANDLER pPhysNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesR3->PhysHandlers, GCPhys);
2438 if (pPhysNode && pPhysNode->pfnHandlerR3)
2439 {
2440 size_t cbRange = pPhysNode->Core.KeyLast - GCPhys + 1;
2441 if (cbRange < cb)
2442 cb = cbRange;
2443 if (cb > cbWrite)
2444 cb = cbWrite;
2445
2446 void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
2447
2448 /* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
2449 rc = pPhysNode->pfnHandlerR3(pVM, GCPhys, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, pPhysNode->pvUserR3);
2450 }
2451
2452 /* 2. The virtual handler (will see incorrect data) */
2453 PPGMVIRTHANDLER pVirtNode;
2454 unsigned iPage;
2455 int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pVirtNode, &iPage);
2456 if (RT_SUCCESS(rc2) && pVirtNode->pfnHandlerR3)
2457 {
2458 size_t cbRange = pVirtNode->Core.KeyLast - GCPhys + 1;
2459 if (cbRange < cb)
2460 cb = cbRange;
2461 if (cb > cbWrite)
2462 cb = cbWrite;
2463 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirtNode->Core.Key & PAGE_BASE_GC_MASK)
2464 + (iPage << PAGE_SHIFT) + (off & PAGE_OFFSET_MASK);
2465
2466 void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
2467
2468 /* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
2469 rc2 = pVirtNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, 0);
2470 if ( ( rc2 != VINF_PGM_HANDLER_DO_DEFAULT
2471 && rc == VINF_PGM_HANDLER_DO_DEFAULT)
2472 || ( RT_FAILURE(rc2)
2473 && RT_SUCCESS(rc)))
2474 rc = rc2;
2475 }
2476#endif /* IN_RING3 */
2477 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
2478 {
2479#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
2480 void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
2481#else
2482 void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
2483#endif
2484 if (cb >= cbWrite)
2485 {
2486 memcpy(pvDst, pvBuf, cbWrite);
2487 goto l_End;
2488 }
2489 memcpy(pvDst, pvBuf, cb);
2490 }
2491 else if (cb >= cbWrite)
2492 goto l_End;
2493 }
2494 else
2495 {
2496 int rc = VINF_PGM_HANDLER_DO_DEFAULT;
2497 cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
2498#ifdef IN_RING3
2499/** @todo deal with this in GC and R0! */
2500 /* Search the whole tree for matching physical addresses (rather expensive!) */
2501 PPGMVIRTHANDLER pNode;
2502 unsigned iPage;
2503 int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pNode, &iPage);
2504 if (RT_SUCCESS(rc2) && pNode->pfnHandlerR3)
2505 {
2506 size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
2507 if (cbRange < cb)
2508 cb = cbRange;
2509 if (cb > cbWrite)
2510 cb = cbWrite;
2511 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pNode->Core.Key & PAGE_BASE_GC_MASK)
2512 + (iPage << PAGE_SHIFT) + (off & PAGE_OFFSET_MASK);
2513
2514 void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
2515
2516 /* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
2517 rc = pNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, 0);
2518 }
2519#endif /* IN_RING3 */
2520 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
2521 {
2522#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
2523 void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
2524#else
2525 void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
2526#endif
2527 if (cb >= cbWrite)
2528 {
2529 memcpy(pvDst, pvBuf, cbWrite);
2530 goto l_End;
2531 }
2532 memcpy(pvDst, pvBuf, cb);
2533 }
2534 else if (cb >= cbWrite)
2535 goto l_End;
2536 }
2537 }
2538 /*
2539 * Physical handler.
2540 */
2541 else if ( RT_UNLIKELY(PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) >= PGM_PAGE_HNDL_PHYS_STATE_WRITE)
2542 && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
2543 {
2544 int rc = VINF_PGM_HANDLER_DO_DEFAULT;
2545 cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
2546#ifdef IN_RING3 /** @todo deal with this in GC and R0! */
2547 /* find and call the handler */
2548 PPGMPHYSHANDLER pNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesR3->PhysHandlers, GCPhys);
2549 if (pNode && pNode->pfnHandlerR3)
2550 {
2551 size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
2552 if (cbRange < cb)
2553 cb = cbRange;
2554 if (cb > cbWrite)
2555 cb = cbWrite;
2556
2557 void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
2558
2559 /** @todo Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
2560 rc = pNode->pfnHandlerR3(pVM, GCPhys, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, pNode->pvUserR3);
2561 }
2562#endif /* IN_RING3 */
2563 if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
2564 {
2565#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
2566 void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
2567#else
2568 void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
2569#endif
2570 if (cb >= cbWrite)
2571 {
2572 memcpy(pvDst, pvBuf, cbWrite);
2573 goto l_End;
2574 }
2575 memcpy(pvDst, pvBuf, cb);
2576 }
2577 else if (cb >= cbWrite)
2578 goto l_End;
2579 }
2580 else
2581 {
2582 /** @todo r=bird: missing MM_RAM_FLAGS_ROM here, we shall not allow anyone to overwrite the ROM! */
2583 switch (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)) /** @todo PAGE FLAGS */
2584 {
2585 /*
2586 * Normal memory, MMIO2 or writable shadow ROM.
2587 */
2588 case 0:
2589 case MM_RAM_FLAGS_MMIO2:
2590 case MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2: /* shadow rom */
2591 {
2592#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
2593 void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
2594#else
2595 void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
2596#endif
2597 cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
2598 if (cb >= cbWrite)
2599 {
2600#if defined(IN_RING3) && defined(PGM_PHYSMEMACCESS_CACHING)
2601 if (cbWrite <= 4 && !fGrabbedLock /* i.e. EMT */)
2602 pgmPhysCacheAdd(pVM, &pVM->pgm.s.pgmphyswritecache, GCPhys, (uint8_t*)pvDst);
2603#endif /* IN_RING3 && PGM_PHYSMEMACCESS_CACHING */
2604 memcpy(pvDst, pvBuf, cbWrite);
2605 goto l_End;
2606 }
2607 memcpy(pvDst, pvBuf, cb);
2608 break;
2609 }
2610
2611 /*
2612 * All reserved, nothing there.
2613 */
2614 case MM_RAM_FLAGS_RESERVED:
2615 case MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO2:
2616 cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
2617 if (cb >= cbWrite)
2618 goto l_End;
2619 break;
2620
2621
2622 /*
2623 * The rest needs to be taken more carefully.
2624 */
2625 default:
2626#if 1 /** @todo r=bird: Can you do this properly please. */
2627 /** @todo Try MMIO; quick hack */
2628 if (cbWrite <= 8 && IOMMMIOWrite(pVM, GCPhys, *(uint32_t *)pvBuf, cbWrite) == VINF_SUCCESS)
2629 goto l_End;
2630#endif
2631
2632 /** @todo fix me later. */
2633 AssertReleaseMsgFailed(("Unknown write at %RGp size %u implement the complex physical writing case %RHp\n",
2634 GCPhys, cbWrite,
2635 (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)))); /** @todo PAGE FLAGS */
2636 /* skip the write */
2637 cb = cbWrite;
2638 break;
2639 }
2640 }
2641
2642 cbWrite -= cb;
2643 off += cb;
2644 pvBuf = (const char *)pvBuf + cb;
2645 }
2646
2647 GCPhys = pRam->GCPhysLast + 1;
2648 }
2649 else
2650 {
2651 /*
2652 * Unassigned address space.
2653 */
2654 size_t cb;
2655 if ( !pRam
2656 || (cb = pRam->GCPhys - GCPhys) >= cbWrite)
2657 goto l_End;
2658
2659 cbWrite -= cb;
2660 pvBuf = (const char *)pvBuf + cb;
2661 GCPhys += cb;
2662 }
2663 }
2664l_End:
2665#ifdef IN_RING3
2666 if (fGrabbedLock)
2667 pgmUnlock(pVM);
2668#endif
2669 return;
2670}
2671
2672#endif /* Old PGMPhysWrite */
2673
2674
2675/**
2676 * Read from guest physical memory by GC physical address, bypassing
2677 * MMIO and access handlers.
2678 *
2679 * @returns VBox status.
2680 * @param pVM VM handle.
2681 * @param pvDst The destination address.
2682 * @param GCPhysSrc The source address (GC physical address).
2683 * @param cb The number of bytes to read.
2684 */
2685VMMDECL(int) PGMPhysSimpleReadGCPhys(PVM pVM, void *pvDst, RTGCPHYS GCPhysSrc, size_t cb)
2686{
2687 /*
2688 * Treat the first page as a special case.
2689 */
2690 if (!cb)
2691 return VINF_SUCCESS;
2692
2693 /* map the 1st page */
2694 void const *pvSrc;
2695 PGMPAGEMAPLOCK Lock;
2696 int rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhysSrc, &pvSrc, &Lock);
2697 if (RT_FAILURE(rc))
2698 return rc;
2699
2700 /* optimize for the case where access is completely within the first page. */
2701 size_t cbPage = PAGE_SIZE - (GCPhysSrc & PAGE_OFFSET_MASK);
2702 if (RT_LIKELY(cb <= cbPage))
2703 {
2704 memcpy(pvDst, pvSrc, cb);
2705 PGMPhysReleasePageMappingLock(pVM, &Lock);
2706 return VINF_SUCCESS;
2707 }
2708
2709 /* copy to the end of the page. */
2710 memcpy(pvDst, pvSrc, cbPage);
2711 PGMPhysReleasePageMappingLock(pVM, &Lock);
2712 GCPhysSrc += cbPage;
2713 pvDst = (uint8_t *)pvDst + cbPage;
2714 cb -= cbPage;
2715
2716 /*
2717 * Page by page.
2718 */
2719 for (;;)
2720 {
2721 /* map the page */
2722 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhysSrc, &pvSrc, &Lock);
2723 if (RT_FAILURE(rc))
2724 return rc;
2725
2726 /* last page? */
2727 if (cb <= PAGE_SIZE)
2728 {
2729 memcpy(pvDst, pvSrc, cb);
2730 PGMPhysReleasePageMappingLock(pVM, &Lock);
2731 return VINF_SUCCESS;
2732 }
2733
2734 /* copy the entire page and advance */
2735 memcpy(pvDst, pvSrc, PAGE_SIZE);
2736 PGMPhysReleasePageMappingLock(pVM, &Lock);
2737 GCPhysSrc += PAGE_SIZE;
2738 pvDst = (uint8_t *)pvDst + PAGE_SIZE;
2739 cb -= PAGE_SIZE;
2740 }
2741 /* won't ever get here. */
2742}
2743
2744#ifndef IN_RC /* Ring 0 & 3 only. (Just not needed in GC.) */
2745
2746/**
2747 * Write to guest physical memory referenced by GC pointer.
2748 * Write memory to GC physical address in guest physical memory.
2749 *
2750 * This will bypass MMIO and access handlers.
2751 *
2752 * @returns VBox status.
2753 * @param pVM VM handle.
2754 * @param GCPhysDst The GC physical address of the destination.
2755 * @param pvSrc The source buffer.
2756 * @param cb The number of bytes to write.
2757 */
2758VMMDECL(int) PGMPhysSimpleWriteGCPhys(PVM pVM, RTGCPHYS GCPhysDst, const void *pvSrc, size_t cb)
2759{
2760 LogFlow(("PGMPhysSimpleWriteGCPhys: %RGp %zu\n", GCPhysDst, cb));
2761
2762 /*
2763 * Treat the first page as a special case.
2764 */
2765 if (!cb)
2766 return VINF_SUCCESS;
2767
2768 /* map the 1st page */
2769 void *pvDst;
2770 PGMPAGEMAPLOCK Lock;
2771 int rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
2772 if (RT_FAILURE(rc))
2773 return rc;
2774
2775 /* optimize for the case where access is completely within the first page. */
2776 size_t cbPage = PAGE_SIZE - (GCPhysDst & PAGE_OFFSET_MASK);
2777 if (RT_LIKELY(cb <= cbPage))
2778 {
2779 memcpy(pvDst, pvSrc, cb);
2780 PGMPhysReleasePageMappingLock(pVM, &Lock);
2781 return VINF_SUCCESS;
2782 }
2783
2784 /* copy to the end of the page. */
2785 memcpy(pvDst, pvSrc, cbPage);
2786 PGMPhysReleasePageMappingLock(pVM, &Lock);
2787 GCPhysDst += cbPage;
2788 pvSrc = (const uint8_t *)pvSrc + cbPage;
2789 cb -= cbPage;
2790
2791 /*
2792 * Page by page.
2793 */
2794 for (;;)
2795 {
2796 /* map the page */
2797 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
2798 if (RT_FAILURE(rc))
2799 return rc;
2800
2801 /* last page? */
2802 if (cb <= PAGE_SIZE)
2803 {
2804 memcpy(pvDst, pvSrc, cb);
2805 PGMPhysReleasePageMappingLock(pVM, &Lock);
2806 return VINF_SUCCESS;
2807 }
2808
2809 /* copy the entire page and advance */
2810 memcpy(pvDst, pvSrc, PAGE_SIZE);
2811 PGMPhysReleasePageMappingLock(pVM, &Lock);
2812 GCPhysDst += PAGE_SIZE;
2813 pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
2814 cb -= PAGE_SIZE;
2815 }
2816 /* won't ever get here. */
2817}
2818
2819
2820/**
2821 * Read from guest physical memory referenced by GC pointer.
2822 *
2823 * This function uses the current CR3/CR0/CR4 of the guest and will
2824 * bypass access handlers and not set any accessed bits.
2825 *
2826 * @returns VBox status.
2827 * @param pVM VM handle.
2828 * @param pvDst The destination address.
2829 * @param GCPtrSrc The source address (GC pointer).
2830 * @param cb The number of bytes to read.
2831 */
2832VMMDECL(int) PGMPhysSimpleReadGCPtr(PVM pVM, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
2833{
2834 /*
2835 * Treat the first page as a special case.
2836 */
2837 if (!cb)
2838 return VINF_SUCCESS;
2839
2840 /* map the 1st page */
2841 void const *pvSrc;
2842 PGMPAGEMAPLOCK Lock;
2843 int rc = PGMPhysGCPtr2CCPtrReadOnly(pVM, GCPtrSrc, &pvSrc, &Lock);
2844 if (RT_FAILURE(rc))
2845 return rc;
2846
2847 /* optimize for the case where access is completely within the first page. */
2848 size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
2849 if (RT_LIKELY(cb <= cbPage))
2850 {
2851 memcpy(pvDst, pvSrc, cb);
2852 PGMPhysReleasePageMappingLock(pVM, &Lock);
2853 return VINF_SUCCESS;
2854 }
2855
2856 /* copy to the end of the page. */
2857 memcpy(pvDst, pvSrc, cbPage);
2858 PGMPhysReleasePageMappingLock(pVM, &Lock);
2859 GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + cbPage);
2860 pvDst = (uint8_t *)pvDst + cbPage;
2861 cb -= cbPage;
2862
2863 /*
2864 * Page by page.
2865 */
2866 for (;;)
2867 {
2868 /* map the page */
2869 rc = PGMPhysGCPtr2CCPtrReadOnly(pVM, GCPtrSrc, &pvSrc, &Lock);
2870 if (RT_FAILURE(rc))
2871 return rc;
2872
2873 /* last page? */
2874 if (cb <= PAGE_SIZE)
2875 {
2876 memcpy(pvDst, pvSrc, cb);
2877 PGMPhysReleasePageMappingLock(pVM, &Lock);
2878 return VINF_SUCCESS;
2879 }
2880
2881 /* copy the entire page and advance */
2882 memcpy(pvDst, pvSrc, PAGE_SIZE);
2883 PGMPhysReleasePageMappingLock(pVM, &Lock);
2884 GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + PAGE_SIZE);
2885 pvDst = (uint8_t *)pvDst + PAGE_SIZE;
2886 cb -= PAGE_SIZE;
2887 }
2888 /* won't ever get here. */
2889}
2890
2891
2892/**
2893 * Write to guest physical memory referenced by GC pointer.
2894 *
2895 * This function uses the current CR3/CR0/CR4 of the guest and will
2896 * bypass access handlers and not set dirty or accessed bits.
2897 *
2898 * @returns VBox status.
2899 * @param pVM VM handle.
2900 * @param GCPtrDst The destination address (GC pointer).
2901 * @param pvSrc The source address.
2902 * @param cb The number of bytes to write.
2903 */
2904VMMDECL(int) PGMPhysSimpleWriteGCPtr(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
2905{
2906 /*
2907 * Treat the first page as a special case.
2908 */
2909 if (!cb)
2910 return VINF_SUCCESS;
2911
2912 /* map the 1st page */
2913 void *pvDst;
2914 PGMPAGEMAPLOCK Lock;
2915 int rc = PGMPhysGCPtr2CCPtr(pVM, GCPtrDst, &pvDst, &Lock);
2916 if (RT_FAILURE(rc))
2917 return rc;
2918
2919 /* optimize for the case where access is completely within the first page. */
2920 size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
2921 if (RT_LIKELY(cb <= cbPage))
2922 {
2923 memcpy(pvDst, pvSrc, cb);
2924 PGMPhysReleasePageMappingLock(pVM, &Lock);
2925 return VINF_SUCCESS;
2926 }
2927
2928 /* copy to the end of the page. */
2929 memcpy(pvDst, pvSrc, cbPage);
2930 PGMPhysReleasePageMappingLock(pVM, &Lock);
2931 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
2932 pvSrc = (const uint8_t *)pvSrc + cbPage;
2933 cb -= cbPage;
2934
2935 /*
2936 * Page by page.
2937 */
2938 for (;;)
2939 {
2940 /* map the page */
2941 rc = PGMPhysGCPtr2CCPtr(pVM, GCPtrDst, &pvDst, &Lock);
2942 if (RT_FAILURE(rc))
2943 return rc;
2944
2945 /* last page? */
2946 if (cb <= PAGE_SIZE)
2947 {
2948 memcpy(pvDst, pvSrc, cb);
2949 PGMPhysReleasePageMappingLock(pVM, &Lock);
2950 return VINF_SUCCESS;
2951 }
2952
2953 /* copy the entire page and advance */
2954 memcpy(pvDst, pvSrc, PAGE_SIZE);
2955 PGMPhysReleasePageMappingLock(pVM, &Lock);
2956 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + PAGE_SIZE);
2957 pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
2958 cb -= PAGE_SIZE;
2959 }
2960 /* won't ever get here. */
2961}
2962
2963
2964/**
2965 * Write to guest physical memory referenced by GC pointer and update the PTE.
2966 *
2967 * This function uses the current CR3/CR0/CR4 of the guest and will
2968 * bypass access handlers but will set any dirty and accessed bits in the PTE.
2969 *
2970 * If you don't want to set the dirty bit, use PGMPhysSimpleWriteGCPtr().
2971 *
2972 * @returns VBox status.
2973 * @param pVM VM handle.
2974 * @param GCPtrDst The destination address (GC pointer).
2975 * @param pvSrc The source address.
2976 * @param cb The number of bytes to write.
2977 */
2978VMMDECL(int) PGMPhysSimpleDirtyWriteGCPtr(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
2979{
2980 /*
2981 * Treat the first page as a special case.
2982 * Btw. this is the same code as in PGMPhyssimpleWriteGCPtr excep for the PGMGstModifyPage.
2983 */
2984 if (!cb)
2985 return VINF_SUCCESS;
2986
2987 /* map the 1st page */
2988 void *pvDst;
2989 PGMPAGEMAPLOCK Lock;
2990 int rc = PGMPhysGCPtr2CCPtr(pVM, GCPtrDst, &pvDst, &Lock);
2991 if (RT_FAILURE(rc))
2992 return rc;
2993
2994 /* optimize for the case where access is completely within the first page. */
2995 size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
2996 if (RT_LIKELY(cb <= cbPage))
2997 {
2998 memcpy(pvDst, pvSrc, cb);
2999 PGMPhysReleasePageMappingLock(pVM, &Lock);
3000 rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
3001 return VINF_SUCCESS;
3002 }
3003
3004 /* copy to the end of the page. */
3005 memcpy(pvDst, pvSrc, cbPage);
3006 PGMPhysReleasePageMappingLock(pVM, &Lock);
3007 rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
3008 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
3009 pvSrc = (const uint8_t *)pvSrc + cbPage;
3010 cb -= cbPage;
3011
3012 /*
3013 * Page by page.
3014 */
3015 for (;;)
3016 {
3017 /* map the page */
3018 rc = PGMPhysGCPtr2CCPtr(pVM, GCPtrDst, &pvDst, &Lock);
3019 if (RT_FAILURE(rc))
3020 return rc;
3021
3022 /* last page? */
3023 if (cb <= PAGE_SIZE)
3024 {
3025 memcpy(pvDst, pvSrc, cb);
3026 PGMPhysReleasePageMappingLock(pVM, &Lock);
3027 rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
3028 return VINF_SUCCESS;
3029 }
3030
3031 /* copy the entire page and advance */
3032 memcpy(pvDst, pvSrc, PAGE_SIZE);
3033 PGMPhysReleasePageMappingLock(pVM, &Lock);
3034 rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
3035 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + PAGE_SIZE);
3036 pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
3037 cb -= PAGE_SIZE;
3038 }
3039 /* won't ever get here. */
3040}
3041
3042
3043/**
3044 * Read from guest physical memory referenced by GC pointer.
3045 *
3046 * This function uses the current CR3/CR0/CR4 of the guest and will
3047 * respect access handlers and set accessed bits.
3048 *
3049 * @returns VBox status.
3050 * @param pVM VM handle.
3051 * @param pvDst The destination address.
3052 * @param GCPtrSrc The source address (GC pointer).
3053 * @param cb The number of bytes to read.
3054 * @thread The vCPU EMT.
3055 */
3056VMMDECL(int) PGMPhysReadGCPtr(PVM pVM, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
3057{
3058 RTGCPHYS GCPhys;
3059 uint64_t fFlags;
3060 int rc;
3061
3062 /*
3063 * Anything to do?
3064 */
3065 if (!cb)
3066 return VINF_SUCCESS;
3067
3068 LogFlow(("PGMPhysReadGCPtr: %RGv %zu\n", GCPtrSrc, cb));
3069
3070 /*
3071 * Optimize reads within a single page.
3072 */
3073 if (((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
3074 {
3075 /* Convert virtual to physical address + flags */
3076 rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtrSrc, &fFlags, &GCPhys);
3077 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrSrc), rc);
3078 GCPhys |= (RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK;
3079
3080 /* mark the guest page as accessed. */
3081 if (!(fFlags & X86_PTE_A))
3082 {
3083 rc = PGMGstModifyPage(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
3084 AssertRC(rc);
3085 }
3086
3087#ifdef VBOX_WITH_NEW_PHYS_CODE
3088 return PGMPhysRead(pVM, GCPhys, pvDst, cb);
3089#else
3090 PGMPhysRead(pVM, GCPhys, pvDst, cb);
3091 return VINF_SUCCESS;
3092#endif
3093 }
3094
3095 /*
3096 * Page by page.
3097 */
3098 for (;;)
3099 {
3100 /* Convert virtual to physical address + flags */
3101 rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtrSrc, &fFlags, &GCPhys);
3102 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrSrc), rc);
3103 GCPhys |= (RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK;
3104
3105 /* mark the guest page as accessed. */
3106 if (!(fFlags & X86_PTE_A))
3107 {
3108 rc = PGMGstModifyPage(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
3109 AssertRC(rc);
3110 }
3111
3112 /* copy */
3113 size_t cbRead = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
3114#ifdef VBOX_WITH_NEW_PHYS_CODE
3115 rc = PGMPhysRead(pVM, GCPhys, pvDst, cbRead);
3116 if (cbRead >= cb || RT_FAILURE(rc))
3117 return rc;
3118#else
3119 if (cbRead >= cb)
3120 {
3121 PGMPhysRead(pVM, GCPhys, pvDst, cb);
3122 return VINF_SUCCESS;
3123 }
3124 PGMPhysRead(pVM, GCPhys, pvDst, cbRead);
3125#endif
3126
3127 /* next */
3128 cb -= cbRead;
3129 pvDst = (uint8_t *)pvDst + cbRead;
3130 GCPtrSrc += cbRead;
3131 }
3132}
3133
3134
3135/**
3136 * Write to guest physical memory referenced by GC pointer.
3137 *
3138 * This function uses the current CR3/CR0/CR4 of the guest and will
3139 * respect access handlers and set dirty and accessed bits.
3140 *
3141 * @returns VBox status.
3142 * @retval VINF_SUCCESS.
3143 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
3144 *
3145 * @param pVM VM handle.
3146 * @param GCPtrDst The destination address (GC pointer).
3147 * @param pvSrc The source address.
3148 * @param cb The number of bytes to write.
3149 */
3150VMMDECL(int) PGMPhysWriteGCPtr(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
3151{
3152 RTGCPHYS GCPhys;
3153 uint64_t fFlags;
3154 int rc;
3155
3156 /*
3157 * Anything to do?
3158 */
3159 if (!cb)
3160 return VINF_SUCCESS;
3161
3162 LogFlow(("PGMPhysWriteGCPtr: %RGv %zu\n", GCPtrDst, cb));
3163
3164 /*
3165 * Optimize writes within a single page.
3166 */
3167 if (((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
3168 {
3169 /* Convert virtual to physical address + flags */
3170 rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtrDst, &fFlags, &GCPhys);
3171 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrDst), rc);
3172 GCPhys |= (RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK;
3173
3174 /* Mention when we ignore X86_PTE_RW... */
3175 if (!(fFlags & X86_PTE_RW))
3176 Log(("PGMPhysGCPtr2GCPhys: Writing to RO page %RGv %#x\n", GCPtrDst, cb));
3177
3178 /* Mark the guest page as accessed and dirty if necessary. */
3179 if ((fFlags & (X86_PTE_A | X86_PTE_D)) != (X86_PTE_A | X86_PTE_D))
3180 {
3181 rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
3182 AssertRC(rc);
3183 }
3184
3185#ifdef VBOX_WITH_NEW_PHYS_CODE
3186 return PGMPhysWrite(pVM, GCPhys, pvSrc, cb);
3187#else
3188 PGMPhysWrite(pVM, GCPhys, pvSrc, cb);
3189 return VINF_SUCCESS;
3190#endif
3191 }
3192
3193 /*
3194 * Page by page.
3195 */
3196 for (;;)
3197 {
3198 /* Convert virtual to physical address + flags */
3199 rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtrDst, &fFlags, &GCPhys);
3200 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrDst), rc);
3201 GCPhys |= (RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK;
3202
3203 /* Mention when we ignore X86_PTE_RW... */
3204 if (!(fFlags & X86_PTE_RW))
3205 Log(("PGMPhysGCPtr2GCPhys: Writing to RO page %RGv %#x\n", GCPtrDst, cb));
3206
3207 /* Mark the guest page as accessed and dirty if necessary. */
3208 if ((fFlags & (X86_PTE_A | X86_PTE_D)) != (X86_PTE_A | X86_PTE_D))
3209 {
3210 rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
3211 AssertRC(rc);
3212 }
3213
3214 /* copy */
3215 size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
3216#ifdef VBOX_WITH_NEW_PHYS_CODE
3217 int rc = PGMPhysWrite(pVM, GCPhys, pvSrc, cbWrite);
3218 if (cbWrite >= cb || RT_FAILURE(rc))
3219 return rc;
3220#else
3221 if (cbWrite >= cb)
3222 {
3223 PGMPhysWrite(pVM, GCPhys, pvSrc, cb);
3224 return VINF_SUCCESS;
3225 }
3226 PGMPhysWrite(pVM, GCPhys, pvSrc, cbWrite);
3227#endif
3228
3229 /* next */
3230 cb -= cbWrite;
3231 pvSrc = (uint8_t *)pvSrc + cbWrite;
3232 GCPtrDst += cbWrite;
3233 }
3234}
3235
3236#endif /* !IN_RC */
3237
3238/**
3239 * Performs a read of guest virtual memory for instruction emulation.
3240 *
3241 * This will check permissions, raise exceptions and update the access bits.
3242 *
3243 * The current implementation will bypass all access handlers. It may later be
3244 * changed to at least respect MMIO.
3245 *
3246 *
3247 * @returns VBox status code suitable to scheduling.
3248 * @retval VINF_SUCCESS if the read was performed successfully.
3249 * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
3250 * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
3251 *
3252 * @param pVM The VM handle.
3253 * @param pCtxCore The context core.
3254 * @param pvDst Where to put the bytes we've read.
3255 * @param GCPtrSrc The source address.
3256 * @param cb The number of bytes to read. Not more than a page.
3257 *
3258 * @remark This function will dynamically map physical pages in GC. This may unmap
3259 * mappings done by the caller. Be careful!
3260 */
3261VMMDECL(int) PGMPhysInterpretedRead(PVM pVM, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb)
3262{
3263 Assert(cb <= PAGE_SIZE);
3264
3265/** @todo r=bird: This isn't perfect!
3266 * -# It's not checking for reserved bits being 1.
3267 * -# It's not correctly dealing with the access bit.
3268 * -# It's not respecting MMIO memory or any other access handlers.
3269 */
3270 /*
3271 * 1. Translate virtual to physical. This may fault.
3272 * 2. Map the physical address.
3273 * 3. Do the read operation.
3274 * 4. Set access bits if required.
3275 */
3276 int rc;
3277 unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
3278 if (cb <= cb1)
3279 {
3280 /*
3281 * Not crossing pages.
3282 */
3283 RTGCPHYS GCPhys;
3284 uint64_t fFlags;
3285 rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc, &fFlags, &GCPhys);
3286 if (RT_SUCCESS(rc))
3287 {
3288 /** @todo we should check reserved bits ... */
3289 void *pvSrc;
3290 rc = PGM_GCPHYS_2_PTR(pVM, GCPhys, &pvSrc);
3291 switch (rc)
3292 {
3293 case VINF_SUCCESS:
3294 Log(("PGMPhysInterpretedRead: pvDst=%p pvSrc=%p cb=%d\n", pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb));
3295 memcpy(pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
3296 break;
3297 case VERR_PGM_PHYS_PAGE_RESERVED:
3298 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3299 memset(pvDst, 0, cb); /** @todo this is wrong, it should be 0xff */
3300 break;
3301 default:
3302 return rc;
3303 }
3304
3305 /** @todo access bit emulation isn't 100% correct. */
3306 if (!(fFlags & X86_PTE_A))
3307 {
3308 rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
3309 AssertRC(rc);
3310 }
3311 return VINF_SUCCESS;
3312 }
3313 }
3314 else
3315 {
3316 /*
3317 * Crosses pages.
3318 */
3319 size_t cb2 = cb - cb1;
3320 uint64_t fFlags1;
3321 RTGCPHYS GCPhys1;
3322 uint64_t fFlags2;
3323 RTGCPHYS GCPhys2;
3324 rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc, &fFlags1, &GCPhys1);
3325 if (RT_SUCCESS(rc))
3326 rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
3327 if (RT_SUCCESS(rc))
3328 {
3329 /** @todo we should check reserved bits ... */
3330 AssertMsgFailed(("cb=%d cb1=%d cb2=%d GCPtrSrc=%RGv\n", cb, cb1, cb2, GCPtrSrc));
3331 void *pvSrc1;
3332 rc = PGM_GCPHYS_2_PTR(pVM, GCPhys1, &pvSrc1);
3333 switch (rc)
3334 {
3335 case VINF_SUCCESS:
3336 memcpy(pvDst, (uint8_t *)pvSrc1 + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
3337 break;
3338 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3339 memset(pvDst, 0, cb1); /** @todo this is wrong, it should be 0xff */
3340 break;
3341 default:
3342 return rc;
3343 }
3344
3345 void *pvSrc2;
3346 rc = PGM_GCPHYS_2_PTR(pVM, GCPhys2, &pvSrc2);
3347 switch (rc)
3348 {
3349 case VINF_SUCCESS:
3350 memcpy((uint8_t *)pvDst + cb1, pvSrc2, cb2);
3351 break;
3352 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3353 memset((uint8_t *)pvDst + cb1, 0, cb2); /** @todo this is wrong, it should be 0xff */
3354 break;
3355 default:
3356 return rc;
3357 }
3358
3359 if (!(fFlags1 & X86_PTE_A))
3360 {
3361 rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
3362 AssertRC(rc);
3363 }
3364 if (!(fFlags2 & X86_PTE_A))
3365 {
3366 rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
3367 AssertRC(rc);
3368 }
3369 return VINF_SUCCESS;
3370 }
3371 }
3372
3373 /*
3374 * Raise a #PF.
3375 */
3376 uint32_t uErr;
3377
3378 /* Get the current privilege level. */
3379 uint32_t cpl = CPUMGetGuestCPL(pVM, pCtxCore);
3380 switch (rc)
3381 {
3382 case VINF_SUCCESS:
3383 uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
3384 break;
3385
3386 case VERR_PAGE_NOT_PRESENT:
3387 case VERR_PAGE_TABLE_NOT_PRESENT:
3388 uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
3389 break;
3390
3391 default:
3392 AssertMsgFailed(("rc=%Rrc GCPtrSrc=%RGv cb=%#x\n", rc, GCPtrSrc, cb));
3393 return rc;
3394 }
3395 Log(("PGMPhysInterpretedRead: GCPtrSrc=%RGv cb=%#x -> #PF(%#x)\n", GCPtrSrc, cb, uErr));
3396 return TRPMRaiseXcptErrCR2(pVM, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
3397}
3398
3399
3400/**
3401 * Performs a read of guest virtual memory for instruction emulation.
3402 *
3403 * This will check permissions, raise exceptions and update the access bits.
3404 *
3405 * The current implementation will bypass all access handlers. It may later be
3406 * changed to at least respect MMIO.
3407 *
3408 *
3409 * @returns VBox status code suitable to scheduling.
3410 * @retval VINF_SUCCESS if the read was performed successfully.
3411 * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
3412 * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
3413 *
3414 * @param pVM The VM handle.
3415 * @param pCtxCore The context core.
3416 * @param pvDst Where to put the bytes we've read.
3417 * @param GCPtrSrc The source address.
3418 * @param cb The number of bytes to read. Not more than a page.
3419 * @param fRaiseTrap If set the trap will be raised on as per spec, if clear
3420 * an appropriate error status will be returned (no
3421 * informational at all).
3422 *
3423 *
3424 * @remarks Takes the PGM lock.
3425 * @remarks A page fault on the 2nd page of the access will be raised without
3426 * writing the bits on the first page since we're ASSUMING that the
3427 * caller is emulating an instruction access.
3428 * @remarks This function will dynamically map physical pages in GC. This may
3429 * unmap mappings done by the caller. Be careful!
3430 */
3431VMMDECL(int) PGMPhysInterpretedReadNoHandlers(PVM pVM, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb, bool fRaiseTrap)
3432{
3433 Assert(cb <= PAGE_SIZE);
3434
3435 /*
3436 * 1. Translate virtual to physical. This may fault.
3437 * 2. Map the physical address.
3438 * 3. Do the read operation.
3439 * 4. Set access bits if required.
3440 */
3441 int rc;
3442 unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
3443 if (cb <= cb1)
3444 {
3445 /*
3446 * Not crossing pages.
3447 */
3448 RTGCPHYS GCPhys;
3449 uint64_t fFlags;
3450 rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc, &fFlags, &GCPhys);
3451 if (RT_SUCCESS(rc))
3452 {
3453 if (1) /** @todo we should check reserved bits ... */
3454 {
3455 const void *pvSrc;
3456 PGMPAGEMAPLOCK Lock;
3457 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys, &pvSrc, &Lock);
3458 switch (rc)
3459 {
3460 case VINF_SUCCESS:
3461 Log(("PGMPhysInterpretedReadNoHandlers: pvDst=%p pvSrc=%p (%RGv) cb=%d\n",
3462 pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), GCPtrSrc, cb));
3463 memcpy(pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
3464 break;
3465 case VERR_PGM_PHYS_PAGE_RESERVED:
3466 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3467 memset(pvDst, 0xff, cb);
3468 break;
3469 default:
3470 AssertMsgFailed(("%Rrc\n", rc));
3471 AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
3472 return rc;
3473 }
3474 PGMPhysReleasePageMappingLock(pVM, &Lock);
3475
3476 if (!(fFlags & X86_PTE_A))
3477 {
3478 /** @todo access bit emulation isn't 100% correct. */
3479 rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
3480 AssertRC(rc);
3481 }
3482 return VINF_SUCCESS;
3483 }
3484 }
3485 }
3486 else
3487 {
3488 /*
3489 * Crosses pages.
3490 */
3491 size_t cb2 = cb - cb1;
3492 uint64_t fFlags1;
3493 RTGCPHYS GCPhys1;
3494 uint64_t fFlags2;
3495 RTGCPHYS GCPhys2;
3496 rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc, &fFlags1, &GCPhys1);
3497 if (RT_SUCCESS(rc))
3498 {
3499 rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
3500 if (RT_SUCCESS(rc))
3501 {
3502 if (1) /** @todo we should check reserved bits ... */
3503 {
3504 const void *pvSrc;
3505 PGMPAGEMAPLOCK Lock;
3506 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys1, &pvSrc, &Lock);
3507 switch (rc)
3508 {
3509 case VINF_SUCCESS:
3510 Log(("PGMPhysInterpretedReadNoHandlers: pvDst=%p pvSrc=%p (%RGv) cb=%d [2]\n",
3511 pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), GCPtrSrc, cb1));
3512 memcpy(pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
3513 PGMPhysReleasePageMappingLock(pVM, &Lock);
3514 break;
3515 case VERR_PGM_PHYS_PAGE_RESERVED:
3516 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3517 memset(pvDst, 0xff, cb1);
3518 break;
3519 default:
3520 AssertMsgFailed(("%Rrc\n", rc));
3521 AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
3522 return rc;
3523 }
3524
3525 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys2, &pvSrc, &Lock);
3526 switch (rc)
3527 {
3528 case VINF_SUCCESS:
3529 memcpy((uint8_t *)pvDst + cb1, pvSrc, cb2);
3530 PGMPhysReleasePageMappingLock(pVM, &Lock);
3531 break;
3532 case VERR_PGM_PHYS_PAGE_RESERVED:
3533 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3534 memset((uint8_t *)pvDst + cb1, 0xff, cb2);
3535 break;
3536 default:
3537 AssertMsgFailed(("%Rrc\n", rc));
3538 AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
3539 return rc;
3540 }
3541
3542 if (!(fFlags1 & X86_PTE_A))
3543 {
3544 rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
3545 AssertRC(rc);
3546 }
3547 if (!(fFlags2 & X86_PTE_A))
3548 {
3549 rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
3550 AssertRC(rc);
3551 }
3552 return VINF_SUCCESS;
3553 }
3554 /* sort out which page */
3555 }
3556 else
3557 GCPtrSrc += cb1; /* fault on 2nd page */
3558 }
3559 }
3560
3561 /*
3562 * Raise a #PF if we're allowed to do that.
3563 */
3564 /* Calc the error bits. */
3565 uint32_t cpl = CPUMGetGuestCPL(pVM, pCtxCore);
3566 uint32_t uErr;
3567 switch (rc)
3568 {
3569 case VINF_SUCCESS:
3570 uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
3571 rc = VERR_ACCESS_DENIED;
3572 break;
3573
3574 case VERR_PAGE_NOT_PRESENT:
3575 case VERR_PAGE_TABLE_NOT_PRESENT:
3576 uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
3577 break;
3578
3579 default:
3580 AssertMsgFailed(("rc=%Rrc GCPtrSrc=%RGv cb=%#x\n", rc, GCPtrSrc, cb));
3581 AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
3582 return rc;
3583 }
3584 if (fRaiseTrap)
3585 {
3586 Log(("PGMPhysInterpretedReadNoHandlers: GCPtrSrc=%RGv cb=%#x -> Raised #PF(%#x)\n", GCPtrSrc, cb, uErr));
3587 return TRPMRaiseXcptErrCR2(pVM, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
3588 }
3589 Log(("PGMPhysInterpretedReadNoHandlers: GCPtrSrc=%RGv cb=%#x -> #PF(%#x) [!raised]\n", GCPtrSrc, cb, uErr));
3590 return rc;
3591}
3592
3593
3594/**
3595 * Performs a write to guest virtual memory for instruction emulation.
3596 *
3597 * This will check permissions, raise exceptions and update the dirty and access
3598 * bits.
3599 *
3600 * @returns VBox status code suitable to scheduling.
3601 * @retval VINF_SUCCESS if the read was performed successfully.
3602 * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
3603 * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
3604 *
3605 * @param pVM The VM handle.
3606 * @param pCtxCore The context core.
3607 * @param GCPtrDst The destination address.
3608 * @param pvSrc What to write.
3609 * @param cb The number of bytes to write. Not more than a page.
3610 * @param fRaiseTrap If set the trap will be raised on as per spec, if clear
3611 * an appropriate error status will be returned (no
3612 * informational at all).
3613 *
3614 * @remarks Takes the PGM lock.
3615 * @remarks A page fault on the 2nd page of the access will be raised without
3616 * writing the bits on the first page since we're ASSUMING that the
3617 * caller is emulating an instruction access.
3618 * @remarks This function will dynamically map physical pages in GC. This may
3619 * unmap mappings done by the caller. Be careful!
3620 */
3621VMMDECL(int) PGMPhysInterpretedWriteNoHandlers(PVM pVM, PCPUMCTXCORE pCtxCore, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb, bool fRaiseTrap)
3622{
3623 Assert(cb <= PAGE_SIZE);
3624
3625 /*
3626 * 1. Translate virtual to physical. This may fault.
3627 * 2. Map the physical address.
3628 * 3. Do the write operation.
3629 * 4. Set access bits if required.
3630 */
3631 int rc;
3632 unsigned cb1 = PAGE_SIZE - (GCPtrDst & PAGE_OFFSET_MASK);
3633 if (cb <= cb1)
3634 {
3635 /*
3636 * Not crossing pages.
3637 */
3638 RTGCPHYS GCPhys;
3639 uint64_t fFlags;
3640 rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrDst, &fFlags, &GCPhys);
3641 if (RT_SUCCESS(rc))
3642 {
3643 if ( (fFlags & X86_PTE_RW) /** @todo Also check reserved bits. */
3644 || ( !(CPUMGetGuestCR0(pVM) & X86_CR0_WP)
3645 && CPUMGetGuestCPL(pVM, pCtxCore) <= 2) ) /** @todo it's 2, right? Check cpl check below as well. */
3646 {
3647 void *pvDst;
3648 PGMPAGEMAPLOCK Lock;
3649 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys, &pvDst, &Lock);
3650 switch (rc)
3651 {
3652 case VINF_SUCCESS:
3653 Log(("PGMPhysInterpretedWriteNoHandlers: pvDst=%p (%RGv) pvSrc=%p cb=%d\n",
3654 (uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), GCPtrDst, pvSrc, cb));
3655 memcpy((uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), pvSrc, cb);
3656 PGMPhysReleasePageMappingLock(pVM, &Lock);
3657 break;
3658 case VERR_PGM_PHYS_PAGE_RESERVED:
3659 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3660 /* bit bucket */
3661 break;
3662 default:
3663 AssertMsgFailed(("%Rrc\n", rc));
3664 AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
3665 return rc;
3666 }
3667
3668 if (!(fFlags & (X86_PTE_A | X86_PTE_D)))
3669 {
3670 /** @todo dirty & access bit emulation isn't 100% correct. */
3671 rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
3672 AssertRC(rc);
3673 }
3674 return VINF_SUCCESS;
3675 }
3676 rc = VERR_ACCESS_DENIED;
3677 }
3678 }
3679 else
3680 {
3681 /*
3682 * Crosses pages.
3683 */
3684 size_t cb2 = cb - cb1;
3685 uint64_t fFlags1;
3686 RTGCPHYS GCPhys1;
3687 uint64_t fFlags2;
3688 RTGCPHYS GCPhys2;
3689 rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrDst, &fFlags1, &GCPhys1);
3690 if (RT_SUCCESS(rc))
3691 {
3692 rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrDst + cb1, &fFlags2, &GCPhys2);
3693 if (RT_SUCCESS(rc))
3694 {
3695 if ( ( (fFlags1 & X86_PTE_RW) /** @todo Also check reserved bits. */
3696 && (fFlags2 & X86_PTE_RW))
3697 || ( !(CPUMGetGuestCR0(pVM) & X86_CR0_WP)
3698 && CPUMGetGuestCPL(pVM, pCtxCore) <= 2) )
3699 {
3700 void *pvDst;
3701 PGMPAGEMAPLOCK Lock;
3702 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys1, &pvDst, &Lock);
3703 switch (rc)
3704 {
3705 case VINF_SUCCESS:
3706 Log(("PGMPhysInterpretedWriteNoHandlers: pvDst=%p (%RGv) pvSrc=%p cb=%d\n",
3707 (uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), GCPtrDst, pvSrc, cb1));
3708 memcpy((uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), pvSrc, cb1);
3709 PGMPhysReleasePageMappingLock(pVM, &Lock);
3710 break;
3711 case VERR_PGM_PHYS_PAGE_RESERVED:
3712 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3713 /* bit bucket */
3714 break;
3715 default:
3716 AssertMsgFailed(("%Rrc\n", rc));
3717 AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
3718 return rc;
3719 }
3720
3721 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys2, &pvDst, &Lock);
3722 switch (rc)
3723 {
3724 case VINF_SUCCESS:
3725 memcpy(pvDst, (const uint8_t *)pvSrc + cb1, cb2);
3726 PGMPhysReleasePageMappingLock(pVM, &Lock);
3727 break;
3728 case VERR_PGM_PHYS_PAGE_RESERVED:
3729 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3730 /* bit bucket */
3731 break;
3732 default:
3733 AssertMsgFailed(("%Rrc\n", rc));
3734 AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
3735 return rc;
3736 }
3737
3738 if (!(fFlags1 & (X86_PTE_A | X86_PTE_RW)))
3739 {
3740 rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrDst, 1, (X86_PTE_A | X86_PTE_RW), ~(uint64_t)(X86_PTE_A | X86_PTE_RW));
3741 AssertRC(rc);
3742 }
3743 if (!(fFlags2 & (X86_PTE_A | X86_PTE_RW)))
3744 {
3745 rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrDst + cb1, 1, (X86_PTE_A | X86_PTE_RW), ~(uint64_t)(X86_PTE_A | X86_PTE_RW));
3746 AssertRC(rc);
3747 }
3748 return VINF_SUCCESS;
3749 }
3750 if ((fFlags1 & (X86_PTE_RW)) == X86_PTE_RW)
3751 GCPtrDst += cb1; /* fault on the 2nd page. */
3752 rc = VERR_ACCESS_DENIED;
3753 }
3754 else
3755 GCPtrDst += cb1; /* fault on the 2nd page. */
3756 }
3757 }
3758
3759 /*
3760 * Raise a #PF if we're allowed to do that.
3761 */
3762 /* Calc the error bits. */
3763 uint32_t uErr;
3764 uint32_t cpl = CPUMGetGuestCPL(pVM, pCtxCore);
3765 switch (rc)
3766 {
3767 case VINF_SUCCESS:
3768 uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
3769 rc = VERR_ACCESS_DENIED;
3770 break;
3771
3772 case VERR_ACCESS_DENIED:
3773 uErr = (cpl >= 2) ? X86_TRAP_PF_RW | X86_TRAP_PF_US : X86_TRAP_PF_RW;
3774 break;
3775
3776 case VERR_PAGE_NOT_PRESENT:
3777 case VERR_PAGE_TABLE_NOT_PRESENT:
3778 uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
3779 break;
3780
3781 default:
3782 AssertMsgFailed(("rc=%Rrc GCPtrDst=%RGv cb=%#x\n", rc, GCPtrDst, cb));
3783 AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
3784 return rc;
3785 }
3786 if (fRaiseTrap)
3787 {
3788 Log(("PGMPhysInterpretedWriteNoHandlers: GCPtrDst=%RGv cb=%#x -> Raised #PF(%#x)\n", GCPtrDst, cb, uErr));
3789 return TRPMRaiseXcptErrCR2(pVM, pCtxCore, X86_XCPT_PF, uErr, GCPtrDst);
3790 }
3791 Log(("PGMPhysInterpretedWriteNoHandlers: GCPtrDst=%RGv cb=%#x -> #PF(%#x) [!raised]\n", GCPtrDst, cb, uErr));
3792 return rc;
3793}
3794
3795
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