VirtualBox

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

Last change on this file since 78327 was 77241, checked in by vboxsync, 6 years ago

VMM: Added dev helps for bulk page mapping locking. VMMDev will be using this for a new variation on the HGCM page list parameter type. bugref:9172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 180.7 KB
Line 
1/* $Id: PGMAllPhys.cpp 77241 2019-02-10 22:30:33Z vboxsync $ */
2/** @file
3 * PGM - Page Manager and Monitor, Physical Memory Addressing.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
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
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_PGM_PHYS
23#include <VBox/vmm/pgm.h>
24#include <VBox/vmm/trpm.h>
25#include <VBox/vmm/vmm.h>
26#include <VBox/vmm/iom.h>
27#include <VBox/vmm/em.h>
28#include <VBox/vmm/nem.h>
29#ifdef VBOX_WITH_REM
30# include <VBox/vmm/rem.h>
31#endif
32#include "PGMInternal.h"
33#include <VBox/vmm/vm.h>
34#include "PGMInline.h"
35#include <VBox/param.h>
36#include <VBox/err.h>
37#include <iprt/assert.h>
38#include <iprt/string.h>
39#include <iprt/asm-amd64-x86.h>
40#include <VBox/log.h>
41#ifdef IN_RING3
42# include <iprt/thread.h>
43#endif
44
45
46/*********************************************************************************************************************************
47* Defined Constants And Macros *
48*********************************************************************************************************************************/
49/** Enable the physical TLB. */
50#define PGM_WITH_PHYS_TLB
51
52/** @def PGM_HANDLER_PHYS_IS_VALID_STATUS
53 * Checks if valid physical access handler return code (normal handler, not PF).
54 *
55 * Checks if the given strict status code is one of the expected ones for a
56 * physical access handler in the current context.
57 *
58 * @returns true or false.
59 * @param a_rcStrict The status code.
60 * @param a_fWrite Whether it is a write or read being serviced.
61 *
62 * @remarks We wish to keep the list of statuses here as short as possible.
63 * When changing, please make sure to update the PGMPhysRead,
64 * PGMPhysWrite, PGMPhysReadGCPtr and PGMPhysWriteGCPtr docs too.
65 */
66#ifdef IN_RING3
67# define PGM_HANDLER_PHYS_IS_VALID_STATUS(a_rcStrict, a_fWrite) \
68 ( (a_rcStrict) == VINF_SUCCESS \
69 || (a_rcStrict) == VINF_PGM_HANDLER_DO_DEFAULT)
70#elif defined(IN_RING0) || defined(IN_RC)
71#define PGM_HANDLER_PHYS_IS_VALID_STATUS(a_rcStrict, a_fWrite) \
72 ( (a_rcStrict) == VINF_SUCCESS \
73 || (a_rcStrict) == VINF_PGM_HANDLER_DO_DEFAULT \
74 \
75 || (a_rcStrict) == ((a_fWrite) ? VINF_IOM_R3_MMIO_WRITE : VINF_IOM_R3_MMIO_READ) \
76 || (a_rcStrict) == VINF_IOM_R3_MMIO_READ_WRITE \
77 || ((a_rcStrict) == VINF_IOM_R3_MMIO_COMMIT_WRITE && (a_fWrite)) \
78 \
79 || (a_rcStrict) == VINF_EM_RAW_EMULATE_INSTR \
80 || (a_rcStrict) == VINF_EM_DBG_STOP \
81 || (a_rcStrict) == VINF_EM_DBG_EVENT \
82 || (a_rcStrict) == VINF_EM_DBG_BREAKPOINT \
83 || (a_rcStrict) == VINF_EM_OFF \
84 || (a_rcStrict) == VINF_EM_SUSPEND \
85 || (a_rcStrict) == VINF_EM_RESET \
86 )
87#else
88# error "Context?"
89#endif
90
91/** @def PGM_HANDLER_VIRT_IS_VALID_STATUS
92 * Checks if valid virtual access handler return code (normal handler, not PF).
93 *
94 * Checks if the given strict status code is one of the expected ones for a
95 * virtual access handler in the current context.
96 *
97 * @returns true or false.
98 * @param a_rcStrict The status code.
99 * @param a_fWrite Whether it is a write or read being serviced.
100 *
101 * @remarks We wish to keep the list of statuses here as short as possible.
102 * When changing, please make sure to update the PGMPhysRead,
103 * PGMPhysWrite, PGMPhysReadGCPtr and PGMPhysWriteGCPtr docs too.
104 */
105#ifdef IN_RING3
106# define PGM_HANDLER_VIRT_IS_VALID_STATUS(a_rcStrict, a_fWrite) \
107 ( (a_rcStrict) == VINF_SUCCESS \
108 || (a_rcStrict) == VINF_PGM_HANDLER_DO_DEFAULT)
109#elif defined(IN_RING0)
110# define PGM_HANDLER_VIRT_IS_VALID_STATUS(a_rcStrict, a_fWrite) \
111 (false /* no virtual handlers in ring-0! */ )
112#elif defined(IN_RC)
113# define PGM_HANDLER_VIRT_IS_VALID_STATUS(a_rcStrict, a_fWrite) \
114 ( (a_rcStrict) == VINF_SUCCESS \
115 || (a_rcStrict) == VINF_PGM_HANDLER_DO_DEFAULT \
116 \
117 || ((a_fWrite) ? (a_rcStrict) == VINF_EM_RAW_EMULATE_INSTR_GDT_FAULT : 0) \
118 || ((a_fWrite) ? (a_rcStrict) == VINF_EM_RAW_EMULATE_INSTR_LDT_FAULT : 0) \
119 || ((a_fWrite) ? (a_rcStrict) == VINF_EM_RAW_EMULATE_INSTR_TSS_FAULT : 0) \
120 || ((a_fWrite) ? (a_rcStrict) == VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT : 0) \
121 || ((a_fWrite) ? (a_rcStrict) == VINF_SELM_SYNC_GDT : 0) \
122 || ((a_fWrite) ? (a_rcStrict) == VINF_CSAM_PENDING_ACTION : 0) \
123 || (a_rcStrict) == VINF_PATM_CHECK_PATCH_PAGE \
124 \
125 || (a_rcStrict) == VINF_EM_RAW_EMULATE_INSTR \
126 || (a_rcStrict) == VINF_EM_DBG_STOP \
127 || (a_rcStrict) == VINF_EM_DBG_EVENT \
128 || (a_rcStrict) == VINF_EM_DBG_BREAKPOINT \
129 )
130#else
131# error "Context?"
132#endif
133
134
135
136#ifndef IN_RING3
137
138/**
139 * @callback_method_impl{FNPGMPHYSHANDLER,
140 * Dummy for forcing ring-3 handling of the access.}
141 */
142DECLEXPORT(VBOXSTRICTRC)
143pgmPhysHandlerRedirectToHC(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf,
144 PGMACCESSTYPE enmAccessType, PGMACCESSORIGIN enmOrigin, void *pvUser)
145{
146 NOREF(pVM); NOREF(pVCpu); NOREF(GCPhys); NOREF(pvPhys); NOREF(pvBuf); NOREF(cbBuf);
147 NOREF(enmAccessType); NOREF(enmOrigin); NOREF(pvUser);
148 return VINF_EM_RAW_EMULATE_INSTR;
149}
150
151
152/**
153 * @callback_method_impl{FNPGMRZPHYSPFHANDLER,
154 * Dummy for forcing ring-3 handling of the access.}
155 */
156VMMDECL(VBOXSTRICTRC) pgmPhysPfHandlerRedirectToHC(PVM pVM, PVMCPU pVCpu, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame,
157 RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
158{
159 NOREF(pVM); NOREF(pVCpu); NOREF(uErrorCode); NOREF(pRegFrame); NOREF(pvFault); NOREF(GCPhysFault); NOREF(pvUser);
160 return VINF_EM_RAW_EMULATE_INSTR;
161}
162
163
164/**
165 * @callback_method_impl{FNPGMRZPHYSPFHANDLER,
166 * \#PF access handler callback for guest ROM range write access.}
167 *
168 * @remarks The @a pvUser argument points to the PGMROMRANGE.
169 */
170DECLEXPORT(VBOXSTRICTRC) pgmPhysRomWritePfHandler(PVM pVM, PVMCPU pVCpu, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame,
171 RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
172{
173 int rc;
174 PPGMROMRANGE pRom = (PPGMROMRANGE)pvUser;
175 uint32_t iPage = (GCPhysFault - pRom->GCPhys) >> PAGE_SHIFT;
176 NOREF(uErrorCode); NOREF(pvFault);
177
178 Assert(uErrorCode & X86_TRAP_PF_RW); /* This shall not be used for read access! */
179
180 Assert(iPage < (pRom->cb >> PAGE_SHIFT));
181 switch (pRom->aPages[iPage].enmProt)
182 {
183 case PGMROMPROT_READ_ROM_WRITE_IGNORE:
184 case PGMROMPROT_READ_RAM_WRITE_IGNORE:
185 {
186 /*
187 * If it's a simple instruction which doesn't change the cpu state
188 * we will simply skip it. Otherwise we'll have to defer it to REM.
189 */
190 uint32_t cbOp;
191 PDISCPUSTATE pDis = &pVCpu->pgm.s.DisState;
192 rc = EMInterpretDisasCurrent(pVM, pVCpu, pDis, &cbOp);
193 if ( RT_SUCCESS(rc)
194 && pDis->uCpuMode == DISCPUMODE_32BIT /** @todo why does this matter? */
195 && !(pDis->fPrefix & (DISPREFIX_REPNE | DISPREFIX_REP | DISPREFIX_SEG)))
196 {
197 switch (pDis->bOpCode)
198 {
199 /** @todo Find other instructions we can safely skip, possibly
200 * adding this kind of detection to DIS or EM. */
201 case OP_MOV:
202 pRegFrame->rip += cbOp;
203 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZGuestROMWriteHandled);
204 return VINF_SUCCESS;
205 }
206 }
207 break;
208 }
209
210 case PGMROMPROT_READ_RAM_WRITE_RAM:
211 pRom->aPages[iPage].LiveSave.fWrittenTo = true;
212 rc = PGMHandlerPhysicalPageTempOff(pVM, pRom->GCPhys, GCPhysFault & X86_PTE_PG_MASK);
213 AssertRC(rc);
214 break; /** @todo Must edit the shadow PT and restart the instruction, not use the interpreter! */
215
216 case PGMROMPROT_READ_ROM_WRITE_RAM:
217 /* Handle it in ring-3 because it's *way* easier there. */
218 pRom->aPages[iPage].LiveSave.fWrittenTo = true;
219 break;
220
221 default:
222 AssertMsgFailedReturn(("enmProt=%d iPage=%d GCPhysFault=%RGp\n",
223 pRom->aPages[iPage].enmProt, iPage, GCPhysFault),
224 VERR_IPE_NOT_REACHED_DEFAULT_CASE);
225 }
226
227 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZGuestROMWriteUnhandled);
228 return VINF_EM_RAW_EMULATE_INSTR;
229}
230
231#endif /* !IN_RING3 */
232
233
234/**
235 * @callback_method_impl{FNPGMPHYSHANDLER,
236 * Access handler callback for ROM write accesses.}
237 *
238 * @remarks The @a pvUser argument points to the PGMROMRANGE.
239 */
240PGM_ALL_CB2_DECL(VBOXSTRICTRC)
241pgmPhysRomWriteHandler(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf,
242 PGMACCESSTYPE enmAccessType, PGMACCESSORIGIN enmOrigin, void *pvUser)
243{
244 PPGMROMRANGE pRom = (PPGMROMRANGE)pvUser;
245 const uint32_t iPage = (GCPhys - pRom->GCPhys) >> PAGE_SHIFT;
246 Assert(iPage < (pRom->cb >> PAGE_SHIFT));
247 PPGMROMPAGE pRomPage = &pRom->aPages[iPage];
248 Log5(("pgmPhysRomWriteHandler: %d %c %#08RGp %#04zx\n", pRomPage->enmProt, enmAccessType == PGMACCESSTYPE_READ ? 'R' : 'W', GCPhys, cbBuf));
249 NOREF(pVCpu); NOREF(pvPhys); NOREF(enmOrigin);
250
251 if (enmAccessType == PGMACCESSTYPE_READ)
252 {
253 switch (pRomPage->enmProt)
254 {
255 /*
256 * Take the default action.
257 */
258 case PGMROMPROT_READ_ROM_WRITE_IGNORE:
259 case PGMROMPROT_READ_RAM_WRITE_IGNORE:
260 case PGMROMPROT_READ_ROM_WRITE_RAM:
261 case PGMROMPROT_READ_RAM_WRITE_RAM:
262 return VINF_PGM_HANDLER_DO_DEFAULT;
263
264 default:
265 AssertMsgFailedReturn(("enmProt=%d iPage=%d GCPhys=%RGp\n",
266 pRom->aPages[iPage].enmProt, iPage, GCPhys),
267 VERR_IPE_NOT_REACHED_DEFAULT_CASE);
268 }
269 }
270 else
271 {
272 Assert(enmAccessType == PGMACCESSTYPE_WRITE);
273 switch (pRomPage->enmProt)
274 {
275 /*
276 * Ignore writes.
277 */
278 case PGMROMPROT_READ_ROM_WRITE_IGNORE:
279 case PGMROMPROT_READ_RAM_WRITE_IGNORE:
280 return VINF_SUCCESS;
281
282 /*
283 * Write to the RAM page.
284 */
285 case PGMROMPROT_READ_ROM_WRITE_RAM:
286 case PGMROMPROT_READ_RAM_WRITE_RAM: /* yes this will get here too, it's *way* simpler that way. */
287 {
288 /* This should be impossible now, pvPhys doesn't work cross page anylonger. */
289 Assert(((GCPhys - pRom->GCPhys + cbBuf - 1) >> PAGE_SHIFT) == iPage);
290
291 /*
292 * Take the lock, do lazy allocation, map the page and copy the data.
293 *
294 * Note that we have to bypass the mapping TLB since it works on
295 * guest physical addresses and entering the shadow page would
296 * kind of screw things up...
297 */
298 int rc = pgmLock(pVM);
299 AssertRC(rc);
300
301 PPGMPAGE pShadowPage = &pRomPage->Shadow;
302 if (!PGMROMPROT_IS_ROM(pRomPage->enmProt))
303 {
304 pShadowPage = pgmPhysGetPage(pVM, GCPhys);
305 AssertLogRelReturn(pShadowPage, VERR_PGM_PHYS_PAGE_GET_IPE);
306 }
307
308 void *pvDstPage;
309 rc = pgmPhysPageMakeWritableAndMap(pVM, pShadowPage, GCPhys & X86_PTE_PG_MASK, &pvDstPage);
310 if (RT_SUCCESS(rc))
311 {
312 memcpy((uint8_t *)pvDstPage + (GCPhys & PAGE_OFFSET_MASK), pvBuf, cbBuf);
313 pRomPage->LiveSave.fWrittenTo = true;
314
315 AssertMsg( rc == VINF_SUCCESS
316 || ( rc == VINF_PGM_SYNC_CR3
317 && VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL))
318 , ("%Rrc\n", rc));
319 rc = VINF_SUCCESS;
320 }
321
322 pgmUnlock(pVM);
323 return rc;
324 }
325
326 default:
327 AssertMsgFailedReturn(("enmProt=%d iPage=%d GCPhys=%RGp\n",
328 pRom->aPages[iPage].enmProt, iPage, GCPhys),
329 VERR_IPE_NOT_REACHED_DEFAULT_CASE);
330 }
331 }
332}
333
334
335/**
336 * Invalidates the RAM range TLBs.
337 *
338 * @param pVM The cross context VM structure.
339 */
340void pgmPhysInvalidRamRangeTlbs(PVM pVM)
341{
342 pgmLock(pVM);
343 for (uint32_t i = 0; i < PGM_RAMRANGE_TLB_ENTRIES; i++)
344 {
345 pVM->pgm.s.apRamRangesTlbR3[i] = NIL_RTR3PTR;
346 pVM->pgm.s.apRamRangesTlbR0[i] = NIL_RTR0PTR;
347 pVM->pgm.s.apRamRangesTlbRC[i] = NIL_RTRCPTR;
348 }
349 pgmUnlock(pVM);
350}
351
352
353/**
354 * Tests if a value of type RTGCPHYS is negative if the type had been signed
355 * instead of unsigned.
356 *
357 * @returns @c true if negative, @c false if positive or zero.
358 * @param a_GCPhys The value to test.
359 * @todo Move me to iprt/types.h.
360 */
361#define RTGCPHYS_IS_NEGATIVE(a_GCPhys) ((a_GCPhys) & ((RTGCPHYS)1 << (sizeof(RTGCPHYS)*8 - 1)))
362
363
364/**
365 * Slow worker for pgmPhysGetRange.
366 *
367 * @copydoc pgmPhysGetRange
368 */
369PPGMRAMRANGE pgmPhysGetRangeSlow(PVM pVM, RTGCPHYS GCPhys)
370{
371 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,RamRangeTlbMisses));
372
373 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRangeTree);
374 while (pRam)
375 {
376 RTGCPHYS off = GCPhys - pRam->GCPhys;
377 if (off < pRam->cb)
378 {
379 pVM->pgm.s.CTX_SUFF(apRamRangesTlb)[PGM_RAMRANGE_TLB_IDX(GCPhys)] = pRam;
380 return pRam;
381 }
382 if (RTGCPHYS_IS_NEGATIVE(off))
383 pRam = pRam->CTX_SUFF(pLeft);
384 else
385 pRam = pRam->CTX_SUFF(pRight);
386 }
387 return NULL;
388}
389
390
391/**
392 * Slow worker for pgmPhysGetRangeAtOrAbove.
393 *
394 * @copydoc pgmPhysGetRangeAtOrAbove
395 */
396PPGMRAMRANGE pgmPhysGetRangeAtOrAboveSlow(PVM pVM, RTGCPHYS GCPhys)
397{
398 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,RamRangeTlbMisses));
399
400 PPGMRAMRANGE pLastLeft = NULL;
401 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRangeTree);
402 while (pRam)
403 {
404 RTGCPHYS off = GCPhys - pRam->GCPhys;
405 if (off < pRam->cb)
406 {
407 pVM->pgm.s.CTX_SUFF(apRamRangesTlb)[PGM_RAMRANGE_TLB_IDX(GCPhys)] = pRam;
408 return pRam;
409 }
410 if (RTGCPHYS_IS_NEGATIVE(off))
411 {
412 pLastLeft = pRam;
413 pRam = pRam->CTX_SUFF(pLeft);
414 }
415 else
416 pRam = pRam->CTX_SUFF(pRight);
417 }
418 return pLastLeft;
419}
420
421
422/**
423 * Slow worker for pgmPhysGetPage.
424 *
425 * @copydoc pgmPhysGetPage
426 */
427PPGMPAGE pgmPhysGetPageSlow(PVM pVM, RTGCPHYS GCPhys)
428{
429 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,RamRangeTlbMisses));
430
431 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRangeTree);
432 while (pRam)
433 {
434 RTGCPHYS off = GCPhys - pRam->GCPhys;
435 if (off < pRam->cb)
436 {
437 pVM->pgm.s.CTX_SUFF(apRamRangesTlb)[PGM_RAMRANGE_TLB_IDX(GCPhys)] = pRam;
438 return &pRam->aPages[off >> PAGE_SHIFT];
439 }
440
441 if (RTGCPHYS_IS_NEGATIVE(off))
442 pRam = pRam->CTX_SUFF(pLeft);
443 else
444 pRam = pRam->CTX_SUFF(pRight);
445 }
446 return NULL;
447}
448
449
450/**
451 * Slow worker for pgmPhysGetPageEx.
452 *
453 * @copydoc pgmPhysGetPageEx
454 */
455int pgmPhysGetPageExSlow(PVM pVM, RTGCPHYS GCPhys, PPPGMPAGE ppPage)
456{
457 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,RamRangeTlbMisses));
458
459 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRangeTree);
460 while (pRam)
461 {
462 RTGCPHYS off = GCPhys - pRam->GCPhys;
463 if (off < pRam->cb)
464 {
465 pVM->pgm.s.CTX_SUFF(apRamRangesTlb)[PGM_RAMRANGE_TLB_IDX(GCPhys)] = pRam;
466 *ppPage = &pRam->aPages[off >> PAGE_SHIFT];
467 return VINF_SUCCESS;
468 }
469
470 if (RTGCPHYS_IS_NEGATIVE(off))
471 pRam = pRam->CTX_SUFF(pLeft);
472 else
473 pRam = pRam->CTX_SUFF(pRight);
474 }
475
476 *ppPage = NULL;
477 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
478}
479
480
481/**
482 * Slow worker for pgmPhysGetPageAndRangeEx.
483 *
484 * @copydoc pgmPhysGetPageAndRangeEx
485 */
486int pgmPhysGetPageAndRangeExSlow(PVM pVM, RTGCPHYS GCPhys, PPPGMPAGE ppPage, PPGMRAMRANGE *ppRam)
487{
488 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,RamRangeTlbMisses));
489
490 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRangeTree);
491 while (pRam)
492 {
493 RTGCPHYS off = GCPhys - pRam->GCPhys;
494 if (off < pRam->cb)
495 {
496 pVM->pgm.s.CTX_SUFF(apRamRangesTlb)[PGM_RAMRANGE_TLB_IDX(GCPhys)] = pRam;
497 *ppRam = pRam;
498 *ppPage = &pRam->aPages[off >> PAGE_SHIFT];
499 return VINF_SUCCESS;
500 }
501
502 if (RTGCPHYS_IS_NEGATIVE(off))
503 pRam = pRam->CTX_SUFF(pLeft);
504 else
505 pRam = pRam->CTX_SUFF(pRight);
506 }
507
508 *ppRam = NULL;
509 *ppPage = NULL;
510 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
511}
512
513
514/**
515 * Checks if Address Gate 20 is enabled or not.
516 *
517 * @returns true if enabled.
518 * @returns false if disabled.
519 * @param pVCpu The cross context virtual CPU structure.
520 */
521VMMDECL(bool) PGMPhysIsA20Enabled(PVMCPU pVCpu)
522{
523 LogFlow(("PGMPhysIsA20Enabled %d\n", pVCpu->pgm.s.fA20Enabled));
524 return pVCpu->pgm.s.fA20Enabled;
525}
526
527
528/**
529 * Validates a GC physical address.
530 *
531 * @returns true if valid.
532 * @returns false if invalid.
533 * @param pVM The cross context VM structure.
534 * @param GCPhys The physical address to validate.
535 */
536VMMDECL(bool) PGMPhysIsGCPhysValid(PVM pVM, RTGCPHYS GCPhys)
537{
538 PPGMPAGE pPage = pgmPhysGetPage(pVM, GCPhys);
539 return pPage != NULL;
540}
541
542
543/**
544 * Checks if a GC physical address is a normal page,
545 * i.e. not ROM, MMIO or reserved.
546 *
547 * @returns true if normal.
548 * @returns false if invalid, ROM, MMIO or reserved page.
549 * @param pVM The cross context VM structure.
550 * @param GCPhys The physical address to check.
551 */
552VMMDECL(bool) PGMPhysIsGCPhysNormal(PVM pVM, RTGCPHYS GCPhys)
553{
554 PPGMPAGE pPage = pgmPhysGetPage(pVM, GCPhys);
555 return pPage
556 && PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM;
557}
558
559
560/**
561 * Converts a GC physical address to a HC physical address.
562 *
563 * @returns VINF_SUCCESS on success.
564 * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
565 * page but has no physical backing.
566 * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
567 * GC physical address.
568 *
569 * @param pVM The cross context VM structure.
570 * @param GCPhys The GC physical address to convert.
571 * @param pHCPhys Where to store the HC physical address on success.
572 */
573VMM_INT_DECL(int) PGMPhysGCPhys2HCPhys(PVM pVM, RTGCPHYS GCPhys, PRTHCPHYS pHCPhys)
574{
575 pgmLock(pVM);
576 PPGMPAGE pPage;
577 int rc = pgmPhysGetPageEx(pVM, GCPhys, &pPage);
578 if (RT_SUCCESS(rc))
579 *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK);
580 pgmUnlock(pVM);
581 return rc;
582}
583
584
585/**
586 * Invalidates all page mapping TLBs.
587 *
588 * @param pVM The cross context VM structure.
589 */
590void pgmPhysInvalidatePageMapTLB(PVM pVM)
591{
592 pgmLock(pVM);
593 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->StatPageMapTlbFlushes);
594
595 /* Clear the shared R0/R3 TLB completely. */
596 for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.PhysTlbHC.aEntries); i++)
597 {
598 pVM->pgm.s.PhysTlbHC.aEntries[i].GCPhys = NIL_RTGCPHYS;
599 pVM->pgm.s.PhysTlbHC.aEntries[i].pPage = 0;
600 pVM->pgm.s.PhysTlbHC.aEntries[i].pMap = 0;
601 pVM->pgm.s.PhysTlbHC.aEntries[i].pv = 0;
602 }
603
604 /** @todo clear the RC TLB whenever we add it. */
605
606 pgmUnlock(pVM);
607}
608
609
610/**
611 * Invalidates a page mapping TLB entry
612 *
613 * @param pVM The cross context VM structure.
614 * @param GCPhys GCPhys entry to flush
615 */
616void pgmPhysInvalidatePageMapTLBEntry(PVM pVM, RTGCPHYS GCPhys)
617{
618 PGM_LOCK_ASSERT_OWNER(pVM);
619
620 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->StatPageMapTlbFlushEntry);
621
622#ifdef IN_RC
623 unsigned idx = PGM_PAGER3MAPTLB_IDX(GCPhys);
624 pVM->pgm.s.PhysTlbHC.aEntries[idx].GCPhys = NIL_RTGCPHYS;
625 pVM->pgm.s.PhysTlbHC.aEntries[idx].pPage = 0;
626 pVM->pgm.s.PhysTlbHC.aEntries[idx].pMap = 0;
627 pVM->pgm.s.PhysTlbHC.aEntries[idx].pv = 0;
628#else
629 /* Clear the shared R0/R3 TLB entry. */
630 PPGMPAGEMAPTLBE pTlbe = &pVM->pgm.s.CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
631 pTlbe->GCPhys = NIL_RTGCPHYS;
632 pTlbe->pPage = 0;
633 pTlbe->pMap = 0;
634 pTlbe->pv = 0;
635#endif
636
637 /** @todo clear the RC TLB whenever we add it. */
638}
639
640
641/**
642 * Makes sure that there is at least one handy page ready for use.
643 *
644 * This will also take the appropriate actions when reaching water-marks.
645 *
646 * @returns VBox status code.
647 * @retval VINF_SUCCESS on success.
648 * @retval VERR_EM_NO_MEMORY if we're really out of memory.
649 *
650 * @param pVM The cross context VM structure.
651 *
652 * @remarks Must be called from within the PGM critical section. It may
653 * nip back to ring-3/0 in some cases.
654 */
655static int pgmPhysEnsureHandyPage(PVM pVM)
656{
657 AssertMsg(pVM->pgm.s.cHandyPages <= RT_ELEMENTS(pVM->pgm.s.aHandyPages), ("%d\n", pVM->pgm.s.cHandyPages));
658
659 /*
660 * Do we need to do anything special?
661 */
662#ifdef IN_RING3
663 if (pVM->pgm.s.cHandyPages <= RT_MAX(PGM_HANDY_PAGES_SET_FF, PGM_HANDY_PAGES_R3_ALLOC))
664#else
665 if (pVM->pgm.s.cHandyPages <= RT_MAX(PGM_HANDY_PAGES_SET_FF, PGM_HANDY_PAGES_RZ_TO_R3))
666#endif
667 {
668 /*
669 * Allocate pages only if we're out of them, or in ring-3, almost out.
670 */
671#ifdef IN_RING3
672 if (pVM->pgm.s.cHandyPages <= PGM_HANDY_PAGES_R3_ALLOC)
673#else
674 if (pVM->pgm.s.cHandyPages <= PGM_HANDY_PAGES_RZ_ALLOC)
675#endif
676 {
677 Log(("PGM: cHandyPages=%u out of %u -> allocate more; VM_FF_PGM_NO_MEMORY=%RTbool\n",
678 pVM->pgm.s.cHandyPages, RT_ELEMENTS(pVM->pgm.s.aHandyPages), VM_FF_IS_SET(pVM, VM_FF_PGM_NO_MEMORY) ));
679#ifdef IN_RING3
680 int rc = PGMR3PhysAllocateHandyPages(pVM);
681#else
682 int rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_PGM_ALLOCATE_HANDY_PAGES, 0);
683#endif
684 if (RT_UNLIKELY(rc != VINF_SUCCESS))
685 {
686 if (RT_FAILURE(rc))
687 return rc;
688 AssertMsgReturn(rc == VINF_EM_NO_MEMORY, ("%Rrc\n", rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
689 if (!pVM->pgm.s.cHandyPages)
690 {
691 LogRel(("PGM: no more handy pages!\n"));
692 return VERR_EM_NO_MEMORY;
693 }
694 Assert(VM_FF_IS_SET(pVM, VM_FF_PGM_NEED_HANDY_PAGES));
695 Assert(VM_FF_IS_SET(pVM, VM_FF_PGM_NO_MEMORY));
696#ifdef IN_RING3
697# ifdef VBOX_WITH_REM
698 REMR3NotifyFF(pVM);
699# endif
700#else
701 VMCPU_FF_SET(VMMGetCpu(pVM), VMCPU_FF_TO_R3); /* paranoia */
702#endif
703 }
704 AssertMsgReturn( pVM->pgm.s.cHandyPages > 0
705 && pVM->pgm.s.cHandyPages <= RT_ELEMENTS(pVM->pgm.s.aHandyPages),
706 ("%u\n", pVM->pgm.s.cHandyPages),
707 VERR_PGM_HANDY_PAGE_IPE);
708 }
709 else
710 {
711 if (pVM->pgm.s.cHandyPages <= PGM_HANDY_PAGES_SET_FF)
712 VM_FF_SET(pVM, VM_FF_PGM_NEED_HANDY_PAGES);
713#ifndef IN_RING3
714 if (pVM->pgm.s.cHandyPages <= PGM_HANDY_PAGES_RZ_TO_R3)
715 {
716 Log(("PGM: VM_FF_TO_R3 - cHandyPages=%u out of %u\n", pVM->pgm.s.cHandyPages, RT_ELEMENTS(pVM->pgm.s.aHandyPages)));
717 VMCPU_FF_SET(VMMGetCpu(pVM), VMCPU_FF_TO_R3);
718 }
719#endif
720 }
721 }
722
723 return VINF_SUCCESS;
724}
725
726
727
728/**
729 * Replace a zero or shared page with new page that we can write to.
730 *
731 * @returns The following VBox status codes.
732 * @retval VINF_SUCCESS on success, pPage is modified.
733 * @retval VINF_PGM_SYNC_CR3 on success and a page pool flush is pending.
734 * @retval VERR_EM_NO_MEMORY if we're totally out of memory.
735 *
736 * @todo Propagate VERR_EM_NO_MEMORY up the call tree.
737 *
738 * @param pVM The cross context VM structure.
739 * @param pPage The physical page tracking structure. This will
740 * be modified on success.
741 * @param GCPhys The address of the page.
742 *
743 * @remarks Must be called from within the PGM critical section. It may
744 * nip back to ring-3/0 in some cases.
745 *
746 * @remarks This function shouldn't really fail, however if it does
747 * it probably means we've screwed up the size of handy pages and/or
748 * the low-water mark. Or, that some device I/O is causing a lot of
749 * pages to be allocated while while the host is in a low-memory
750 * condition. This latter should be handled elsewhere and in a more
751 * controlled manner, it's on the @bugref{3170} todo list...
752 */
753int pgmPhysAllocPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
754{
755 LogFlow(("pgmPhysAllocPage: %R[pgmpage] %RGp\n", pPage, GCPhys));
756
757 /*
758 * Prereqs.
759 */
760 PGM_LOCK_ASSERT_OWNER(pVM);
761 AssertMsg(PGM_PAGE_IS_ZERO(pPage) || PGM_PAGE_IS_SHARED(pPage), ("%R[pgmpage] %RGp\n", pPage, GCPhys));
762 Assert(!PGM_PAGE_IS_MMIO_OR_ALIAS(pPage));
763
764# ifdef PGM_WITH_LARGE_PAGES
765 /*
766 * Try allocate a large page if applicable.
767 */
768 if ( PGMIsUsingLargePages(pVM)
769 && PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM
770 && !VM_IS_NEM_ENABLED(pVM)) /** @todo NEM: Implement large pages support. */
771 {
772 RTGCPHYS GCPhysBase = GCPhys & X86_PDE2M_PAE_PG_MASK;
773 PPGMPAGE pBasePage;
774
775 int rc = pgmPhysGetPageEx(pVM, GCPhysBase, &pBasePage);
776 AssertRCReturn(rc, rc); /* paranoia; can't happen. */
777 if (PGM_PAGE_GET_PDE_TYPE(pBasePage) == PGM_PAGE_PDE_TYPE_DONTCARE)
778 {
779 rc = pgmPhysAllocLargePage(pVM, GCPhys);
780 if (rc == VINF_SUCCESS)
781 return rc;
782 }
783 /* Mark the base as type page table, so we don't check over and over again. */
784 PGM_PAGE_SET_PDE_TYPE(pVM, pBasePage, PGM_PAGE_PDE_TYPE_PT);
785
786 /* fall back to 4KB pages. */
787 }
788# endif
789
790 /*
791 * Flush any shadow page table mappings of the page.
792 * When VBOX_WITH_NEW_LAZY_PAGE_ALLOC isn't defined, there shouldn't be any.
793 */
794 bool fFlushTLBs = false;
795 int rc = pgmPoolTrackUpdateGCPhys(pVM, GCPhys, pPage, true /*fFlushTLBs*/, &fFlushTLBs);
796 AssertMsgReturn(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3, ("%Rrc\n", rc), RT_FAILURE(rc) ? rc : VERR_IPE_UNEXPECTED_STATUS);
797
798 /*
799 * Ensure that we've got a page handy, take it and use it.
800 */
801 int rc2 = pgmPhysEnsureHandyPage(pVM);
802 if (RT_FAILURE(rc2))
803 {
804 if (fFlushTLBs)
805 PGM_INVL_ALL_VCPU_TLBS(pVM);
806 Assert(rc2 == VERR_EM_NO_MEMORY);
807 return rc2;
808 }
809 /* re-assert preconditions since pgmPhysEnsureHandyPage may do a context switch. */
810 PGM_LOCK_ASSERT_OWNER(pVM);
811 AssertMsg(PGM_PAGE_IS_ZERO(pPage) || PGM_PAGE_IS_SHARED(pPage), ("%R[pgmpage] %RGp\n", pPage, GCPhys));
812 Assert(!PGM_PAGE_IS_MMIO_OR_ALIAS(pPage));
813
814 uint32_t iHandyPage = --pVM->pgm.s.cHandyPages;
815 AssertMsg(iHandyPage < RT_ELEMENTS(pVM->pgm.s.aHandyPages), ("%d\n", iHandyPage));
816 Assert(pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys != NIL_RTHCPHYS);
817 Assert(!(pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
818 Assert(pVM->pgm.s.aHandyPages[iHandyPage].idPage != NIL_GMM_PAGEID);
819 Assert(pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage == NIL_GMM_PAGEID);
820
821 /*
822 * There are one or two action to be taken the next time we allocate handy pages:
823 * - Tell the GMM (global memory manager) what the page is being used for.
824 * (Speeds up replacement operations - sharing and defragmenting.)
825 * - If the current backing is shared, it must be freed.
826 */
827 const RTHCPHYS HCPhys = pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys;
828 pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys = GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK;
829
830 void const *pvSharedPage = NULL;
831 if (PGM_PAGE_IS_SHARED(pPage))
832 {
833 /* Mark this shared page for freeing/dereferencing. */
834 pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage = PGM_PAGE_GET_PAGEID(pPage);
835 Assert(PGM_PAGE_GET_PAGEID(pPage) != NIL_GMM_PAGEID);
836
837 Log(("PGM: Replaced shared page %#x at %RGp with %#x / %RHp\n", PGM_PAGE_GET_PAGEID(pPage),
838 GCPhys, pVM->pgm.s.aHandyPages[iHandyPage].idPage, HCPhys));
839 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PageReplaceShared));
840 pVM->pgm.s.cSharedPages--;
841
842 /* Grab the address of the page so we can make a copy later on. (safe) */
843 rc = pgmPhysPageMapReadOnly(pVM, pPage, GCPhys, &pvSharedPage);
844 AssertRC(rc);
845 }
846 else
847 {
848 Log2(("PGM: Replaced zero page %RGp with %#x / %RHp\n", GCPhys, pVM->pgm.s.aHandyPages[iHandyPage].idPage, HCPhys));
849 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->StatRZPageReplaceZero);
850 pVM->pgm.s.cZeroPages--;
851 }
852
853 /*
854 * Do the PGMPAGE modifications.
855 */
856 pVM->pgm.s.cPrivatePages++;
857 PGM_PAGE_SET_HCPHYS(pVM, pPage, HCPhys);
858 PGM_PAGE_SET_PAGEID(pVM, pPage, pVM->pgm.s.aHandyPages[iHandyPage].idPage);
859 PGM_PAGE_SET_STATE(pVM, pPage, PGM_PAGE_STATE_ALLOCATED);
860 PGM_PAGE_SET_PDE_TYPE(pVM, pPage, PGM_PAGE_PDE_TYPE_PT);
861 pgmPhysInvalidatePageMapTLBEntry(pVM, GCPhys);
862
863 /* Copy the shared page contents to the replacement page. */
864 if (pvSharedPage)
865 {
866 /* Get the virtual address of the new page. */
867 PGMPAGEMAPLOCK PgMpLck;
868 void *pvNewPage;
869 rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvNewPage, &PgMpLck); AssertRC(rc);
870 if (RT_SUCCESS(rc))
871 {
872 memcpy(pvNewPage, pvSharedPage, PAGE_SIZE); /** @todo todo write ASMMemCopyPage */
873 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
874 }
875 }
876
877 if ( fFlushTLBs
878 && rc != VINF_PGM_GCPHYS_ALIASED)
879 PGM_INVL_ALL_VCPU_TLBS(pVM);
880
881#ifndef IN_RC
882 /*
883 * Notify NEM about the mapping change for this page.
884 *
885 * Note! Shadow ROM pages are complicated as they can definitely be
886 * allocated while not visible, so play safe.
887 */
888 if (VM_IS_NEM_ENABLED(pVM))
889 {
890 PGMPAGETYPE enmType = (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage);
891 if ( enmType != PGMPAGETYPE_ROM_SHADOW
892 || pgmPhysGetPage(pVM, GCPhys) == pPage)
893 {
894 uint8_t u2State = PGM_PAGE_GET_NEM_STATE(pPage);
895 rc2 = NEMHCNotifyPhysPageAllocated(pVM, GCPhys & ~(RTGCPHYS)X86_PAGE_OFFSET_MASK, HCPhys,
896 pgmPhysPageCalcNemProtection(pPage, enmType), enmType, &u2State);
897 if (RT_SUCCESS(rc))
898 PGM_PAGE_SET_NEM_STATE(pPage, u2State);
899 else
900 rc = rc2;
901 }
902 }
903#endif
904
905 return rc;
906}
907
908#ifdef PGM_WITH_LARGE_PAGES
909
910/**
911 * Replace a 2 MB range of zero pages with new pages that we can write to.
912 *
913 * @returns The following VBox status codes.
914 * @retval VINF_SUCCESS on success, pPage is modified.
915 * @retval VINF_PGM_SYNC_CR3 on success and a page pool flush is pending.
916 * @retval VERR_EM_NO_MEMORY if we're totally out of memory.
917 *
918 * @todo Propagate VERR_EM_NO_MEMORY up the call tree.
919 *
920 * @param pVM The cross context VM structure.
921 * @param GCPhys The address of the page.
922 *
923 * @remarks Must be called from within the PGM critical section. It may
924 * nip back to ring-3/0 in some cases.
925 */
926int pgmPhysAllocLargePage(PVM pVM, RTGCPHYS GCPhys)
927{
928 RTGCPHYS GCPhysBase = GCPhys & X86_PDE2M_PAE_PG_MASK;
929 LogFlow(("pgmPhysAllocLargePage: %RGp base %RGp\n", GCPhys, GCPhysBase));
930 Assert(!VM_IS_NEM_ENABLED(pVM)); /** @todo NEM: Large page support. */
931
932 /*
933 * Prereqs.
934 */
935 PGM_LOCK_ASSERT_OWNER(pVM);
936 Assert(PGMIsUsingLargePages(pVM));
937
938 PPGMPAGE pFirstPage;
939 int rc = pgmPhysGetPageEx(pVM, GCPhysBase, &pFirstPage);
940 if ( RT_SUCCESS(rc)
941 && PGM_PAGE_GET_TYPE(pFirstPage) == PGMPAGETYPE_RAM)
942 {
943 unsigned uPDEType = PGM_PAGE_GET_PDE_TYPE(pFirstPage);
944
945 /* Don't call this function for already allocated pages. */
946 Assert(uPDEType != PGM_PAGE_PDE_TYPE_PDE);
947
948 if ( uPDEType == PGM_PAGE_PDE_TYPE_DONTCARE
949 && PGM_PAGE_GET_STATE(pFirstPage) == PGM_PAGE_STATE_ZERO)
950 {
951 /* Lazy approach: check all pages in the 2 MB range.
952 * The whole range must be ram and unallocated. */
953 GCPhys = GCPhysBase;
954 unsigned iPage;
955 for (iPage = 0; iPage < _2M/PAGE_SIZE; iPage++)
956 {
957 PPGMPAGE pSubPage;
958 rc = pgmPhysGetPageEx(pVM, GCPhys, &pSubPage);
959 if ( RT_FAILURE(rc)
960 || PGM_PAGE_GET_TYPE(pSubPage) != PGMPAGETYPE_RAM /* Anything other than ram implies monitoring. */
961 || PGM_PAGE_GET_STATE(pSubPage) != PGM_PAGE_STATE_ZERO) /* Allocated, monitored or shared means we can't use a large page here */
962 {
963 LogFlow(("Found page %RGp with wrong attributes (type=%d; state=%d); cancel check. rc=%d\n", GCPhys, PGM_PAGE_GET_TYPE(pSubPage), PGM_PAGE_GET_STATE(pSubPage), rc));
964 break;
965 }
966 Assert(PGM_PAGE_GET_PDE_TYPE(pSubPage) == PGM_PAGE_PDE_TYPE_DONTCARE);
967 GCPhys += PAGE_SIZE;
968 }
969 if (iPage != _2M/PAGE_SIZE)
970 {
971 /* Failed. Mark as requiring a PT so we don't check the whole thing again in the future. */
972 STAM_REL_COUNTER_INC(&pVM->pgm.s.StatLargePageRefused);
973 PGM_PAGE_SET_PDE_TYPE(pVM, pFirstPage, PGM_PAGE_PDE_TYPE_PT);
974 return VERR_PGM_INVALID_LARGE_PAGE_RANGE;
975 }
976
977 /*
978 * Do the allocation.
979 */
980# ifdef IN_RING3
981 rc = PGMR3PhysAllocateLargeHandyPage(pVM, GCPhysBase);
982# else
983 rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_PGM_ALLOCATE_LARGE_HANDY_PAGE, GCPhysBase);
984# endif
985 if (RT_SUCCESS(rc))
986 {
987 Assert(PGM_PAGE_GET_STATE(pFirstPage) == PGM_PAGE_STATE_ALLOCATED);
988 pVM->pgm.s.cLargePages++;
989 return VINF_SUCCESS;
990 }
991
992 /* If we fail once, it most likely means the host's memory is too
993 fragmented; don't bother trying again. */
994 LogFlow(("pgmPhysAllocLargePage failed with %Rrc\n", rc));
995 PGMSetLargePageUsage(pVM, false);
996 return rc;
997 }
998 }
999 return VERR_PGM_INVALID_LARGE_PAGE_RANGE;
1000}
1001
1002
1003/**
1004 * Recheck the entire 2 MB range to see if we can use it again as a large page.
1005 *
1006 * @returns The following VBox status codes.
1007 * @retval VINF_SUCCESS on success, the large page can be used again
1008 * @retval VERR_PGM_INVALID_LARGE_PAGE_RANGE if it can't be reused
1009 *
1010 * @param pVM The cross context VM structure.
1011 * @param GCPhys The address of the page.
1012 * @param pLargePage Page structure of the base page
1013 */
1014int pgmPhysRecheckLargePage(PVM pVM, RTGCPHYS GCPhys, PPGMPAGE pLargePage)
1015{
1016 STAM_REL_COUNTER_INC(&pVM->pgm.s.StatLargePageRecheck);
1017
1018 Assert(!VM_IS_NEM_ENABLED(pVM)); /** @todo NEM: Large page support. */
1019
1020 GCPhys &= X86_PDE2M_PAE_PG_MASK;
1021
1022 /* Check the base page. */
1023 Assert(PGM_PAGE_GET_PDE_TYPE(pLargePage) == PGM_PAGE_PDE_TYPE_PDE_DISABLED);
1024 if ( PGM_PAGE_GET_STATE(pLargePage) != PGM_PAGE_STATE_ALLOCATED
1025 || PGM_PAGE_GET_TYPE(pLargePage) != PGMPAGETYPE_RAM
1026 || PGM_PAGE_GET_HNDL_PHYS_STATE(pLargePage) != PGM_PAGE_HNDL_PHYS_STATE_NONE)
1027 {
1028 LogFlow(("pgmPhysRecheckLargePage: checks failed for base page %x %x %x\n", PGM_PAGE_GET_STATE(pLargePage), PGM_PAGE_GET_TYPE(pLargePage), PGM_PAGE_GET_HNDL_PHYS_STATE(pLargePage)));
1029 return VERR_PGM_INVALID_LARGE_PAGE_RANGE;
1030 }
1031
1032 STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,IsValidLargePage), a);
1033 /* Check all remaining pages in the 2 MB range. */
1034 unsigned i;
1035 GCPhys += PAGE_SIZE;
1036 for (i = 1; i < _2M/PAGE_SIZE; i++)
1037 {
1038 PPGMPAGE pPage;
1039 int rc = pgmPhysGetPageEx(pVM, GCPhys, &pPage);
1040 AssertRCBreak(rc);
1041
1042 if ( PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED
1043 || PGM_PAGE_GET_PDE_TYPE(pPage) != PGM_PAGE_PDE_TYPE_PDE
1044 || PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_RAM
1045 || PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_NONE)
1046 {
1047 LogFlow(("pgmPhysRecheckLargePage: checks failed for page %d; %x %x %x\n", i, PGM_PAGE_GET_STATE(pPage), PGM_PAGE_GET_TYPE(pPage), PGM_PAGE_GET_HNDL_PHYS_STATE(pPage)));
1048 break;
1049 }
1050
1051 GCPhys += PAGE_SIZE;
1052 }
1053 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,IsValidLargePage), a);
1054
1055 if (i == _2M/PAGE_SIZE)
1056 {
1057 PGM_PAGE_SET_PDE_TYPE(pVM, pLargePage, PGM_PAGE_PDE_TYPE_PDE);
1058 pVM->pgm.s.cLargePagesDisabled--;
1059 Log(("pgmPhysRecheckLargePage: page %RGp can be reused!\n", GCPhys - _2M));
1060 return VINF_SUCCESS;
1061 }
1062
1063 return VERR_PGM_INVALID_LARGE_PAGE_RANGE;
1064}
1065
1066#endif /* PGM_WITH_LARGE_PAGES */
1067
1068
1069/**
1070 * Deal with a write monitored page.
1071 *
1072 * @returns VBox strict status code.
1073 *
1074 * @param pVM The cross context VM structure.
1075 * @param pPage The physical page tracking structure.
1076 * @param GCPhys The guest physical address of the page.
1077 * PGMPhysReleasePageMappingLock() passes NIL_RTGCPHYS in a
1078 * very unlikely situation where it is okay that we let NEM
1079 * fix the page access in a lazy fasion.
1080 *
1081 * @remarks Called from within the PGM critical section.
1082 */
1083void pgmPhysPageMakeWriteMonitoredWritable(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
1084{
1085 Assert(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_WRITE_MONITORED);
1086 PGM_PAGE_SET_WRITTEN_TO(pVM, pPage);
1087 PGM_PAGE_SET_STATE(pVM, pPage, PGM_PAGE_STATE_ALLOCATED);
1088 Assert(pVM->pgm.s.cMonitoredPages > 0);
1089 pVM->pgm.s.cMonitoredPages--;
1090 pVM->pgm.s.cWrittenToPages++;
1091
1092#ifndef IN_RC
1093 /*
1094 * Notify NEM about the protection change so we won't spin forever.
1095 *
1096 * Note! NEM need to be handle to lazily correct page protection as we cannot
1097 * really get it 100% right here it seems. The page pool does this too.
1098 */
1099 if (VM_IS_NEM_ENABLED(pVM) && GCPhys != NIL_RTGCPHYS)
1100 {
1101 uint8_t u2State = PGM_PAGE_GET_NEM_STATE(pPage);
1102 PGMPAGETYPE enmType = (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage);
1103 NEMHCNotifyPhysPageProtChanged(pVM, GCPhys, PGM_PAGE_GET_HCPHYS(pPage),
1104 pgmPhysPageCalcNemProtection(pPage, enmType), enmType, &u2State);
1105 PGM_PAGE_SET_NEM_STATE(pPage, u2State);
1106 }
1107#else
1108 RT_NOREF(GCPhys);
1109#endif
1110}
1111
1112
1113/**
1114 * Deal with pages that are not writable, i.e. not in the ALLOCATED state.
1115 *
1116 * @returns VBox strict status code.
1117 * @retval VINF_SUCCESS on success.
1118 * @retval VINF_PGM_SYNC_CR3 on success and a page pool flush is pending.
1119 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
1120 *
1121 * @param pVM The cross context VM structure.
1122 * @param pPage The physical page tracking structure.
1123 * @param GCPhys The address of the page.
1124 *
1125 * @remarks Called from within the PGM critical section.
1126 */
1127int pgmPhysPageMakeWritable(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
1128{
1129 PGM_LOCK_ASSERT_OWNER(pVM);
1130 switch (PGM_PAGE_GET_STATE(pPage))
1131 {
1132 case PGM_PAGE_STATE_WRITE_MONITORED:
1133 pgmPhysPageMakeWriteMonitoredWritable(pVM, pPage, GCPhys);
1134 RT_FALL_THRU();
1135 default: /* to shut up GCC */
1136 case PGM_PAGE_STATE_ALLOCATED:
1137 return VINF_SUCCESS;
1138
1139 /*
1140 * Zero pages can be dummy pages for MMIO or reserved memory,
1141 * so we need to check the flags before joining cause with
1142 * shared page replacement.
1143 */
1144 case PGM_PAGE_STATE_ZERO:
1145 if (PGM_PAGE_IS_MMIO(pPage))
1146 return VERR_PGM_PHYS_PAGE_RESERVED;
1147 RT_FALL_THRU();
1148 case PGM_PAGE_STATE_SHARED:
1149 return pgmPhysAllocPage(pVM, pPage, GCPhys);
1150
1151 /* Not allowed to write to ballooned pages. */
1152 case PGM_PAGE_STATE_BALLOONED:
1153 return VERR_PGM_PHYS_PAGE_BALLOONED;
1154 }
1155}
1156
1157
1158/**
1159 * Internal usage: Map the page specified by its GMM ID.
1160 *
1161 * This is similar to pgmPhysPageMap
1162 *
1163 * @returns VBox status code.
1164 *
1165 * @param pVM The cross context VM structure.
1166 * @param idPage The Page ID.
1167 * @param HCPhys The physical address (for RC).
1168 * @param ppv Where to store the mapping address.
1169 *
1170 * @remarks Called from within the PGM critical section. The mapping is only
1171 * valid while you are inside this section.
1172 */
1173int pgmPhysPageMapByPageID(PVM pVM, uint32_t idPage, RTHCPHYS HCPhys, void **ppv)
1174{
1175 /*
1176 * Validation.
1177 */
1178 PGM_LOCK_ASSERT_OWNER(pVM);
1179 AssertReturn(HCPhys && !(HCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
1180 const uint32_t idChunk = idPage >> GMM_CHUNKID_SHIFT;
1181 AssertReturn(idChunk != NIL_GMM_CHUNKID, VERR_INVALID_PARAMETER);
1182
1183#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1184 /*
1185 * Map it by HCPhys.
1186 */
1187 return pgmRZDynMapHCPageInlined(VMMGetCpu(pVM), HCPhys, ppv RTLOG_COMMA_SRC_POS);
1188
1189#else
1190 /*
1191 * Find/make Chunk TLB entry for the mapping chunk.
1192 */
1193 PPGMCHUNKR3MAP pMap;
1194 PPGMCHUNKR3MAPTLBE pTlbe = &pVM->pgm.s.ChunkR3Map.Tlb.aEntries[PGM_CHUNKR3MAPTLB_IDX(idChunk)];
1195 if (pTlbe->idChunk == idChunk)
1196 {
1197 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,ChunkR3MapTlbHits));
1198 pMap = pTlbe->pChunk;
1199 }
1200 else
1201 {
1202 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,ChunkR3MapTlbMisses));
1203
1204 /*
1205 * Find the chunk, map it if necessary.
1206 */
1207 pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
1208 if (pMap)
1209 pMap->iLastUsed = pVM->pgm.s.ChunkR3Map.iNow;
1210 else
1211 {
1212# ifdef IN_RING0
1213 int rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_PGM_MAP_CHUNK, idChunk);
1214 AssertRCReturn(rc, rc);
1215 pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
1216 Assert(pMap);
1217# else
1218 int rc = pgmR3PhysChunkMap(pVM, idChunk, &pMap);
1219 if (RT_FAILURE(rc))
1220 return rc;
1221# endif
1222 }
1223
1224 /*
1225 * Enter it into the Chunk TLB.
1226 */
1227 pTlbe->idChunk = idChunk;
1228 pTlbe->pChunk = pMap;
1229 }
1230
1231 *ppv = (uint8_t *)pMap->pv + ((idPage &GMM_PAGEID_IDX_MASK) << PAGE_SHIFT);
1232 return VINF_SUCCESS;
1233#endif
1234}
1235
1236
1237/**
1238 * Maps a page into the current virtual address space so it can be accessed.
1239 *
1240 * @returns VBox status code.
1241 * @retval VINF_SUCCESS on success.
1242 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
1243 *
1244 * @param pVM The cross context VM structure.
1245 * @param pPage The physical page tracking structure.
1246 * @param GCPhys The address of the page.
1247 * @param ppMap Where to store the address of the mapping tracking structure.
1248 * @param ppv Where to store the mapping address of the page. The page
1249 * offset is masked off!
1250 *
1251 * @remarks Called from within the PGM critical section.
1252 */
1253static int pgmPhysPageMapCommon(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, PPPGMPAGEMAP ppMap, void **ppv)
1254{
1255 PGM_LOCK_ASSERT_OWNER(pVM);
1256 NOREF(GCPhys);
1257
1258#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1259 /*
1260 * Just some sketchy GC/R0-darwin code.
1261 */
1262 *ppMap = NULL;
1263 RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
1264 Assert(HCPhys != pVM->pgm.s.HCPhysZeroPg);
1265 pgmRZDynMapHCPageInlined(VMMGetCpu(pVM), HCPhys, ppv RTLOG_COMMA_SRC_POS);
1266 return VINF_SUCCESS;
1267
1268#else /* IN_RING3 || IN_RING0 */
1269
1270
1271 /*
1272 * Special cases: MMIO2, ZERO and specially aliased MMIO pages.
1273 */
1274 if ( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2
1275 || PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO)
1276 {
1277 /* Decode the page id to a page in a MMIO2 ram range. */
1278 uint8_t idMmio2 = PGM_MMIO2_PAGEID_GET_MMIO2_ID(PGM_PAGE_GET_PAGEID(pPage));
1279 uint32_t iPage = PGM_MMIO2_PAGEID_GET_IDX(PGM_PAGE_GET_PAGEID(pPage));
1280 AssertLogRelMsgReturn((uint8_t)(idMmio2 - 1U) < RT_ELEMENTS(pVM->pgm.s.CTX_SUFF(apMmio2Ranges)),
1281 ("idMmio2=%u size=%u type=%u GCPHys=%#RGp Id=%u State=%u", idMmio2,
1282 RT_ELEMENTS(pVM->pgm.s.CTX_SUFF(apMmio2Ranges)), PGM_PAGE_GET_TYPE(pPage), GCPhys,
1283 pPage->s.idPage, pPage->s.uStateY),
1284 VERR_PGM_PHYS_PAGE_MAP_MMIO2_IPE);
1285 PPGMREGMMIORANGE pMmio2Range = pVM->pgm.s.CTX_SUFF(apMmio2Ranges)[idMmio2 - 1];
1286 AssertLogRelReturn(pMmio2Range, VERR_PGM_PHYS_PAGE_MAP_MMIO2_IPE);
1287 AssertLogRelReturn(pMmio2Range->idMmio2 == idMmio2, VERR_PGM_PHYS_PAGE_MAP_MMIO2_IPE);
1288 AssertLogRelReturn(iPage < (pMmio2Range->RamRange.cb >> PAGE_SHIFT), VERR_PGM_PHYS_PAGE_MAP_MMIO2_IPE);
1289 *ppv = (uint8_t *)pMmio2Range->RamRange.pvR3 + ((uintptr_t)iPage << PAGE_SHIFT);
1290 *ppMap = NULL;
1291 return VINF_SUCCESS;
1292 }
1293
1294 const uint32_t idChunk = PGM_PAGE_GET_CHUNKID(pPage);
1295 if (idChunk == NIL_GMM_CHUNKID)
1296 {
1297 AssertMsgReturn(PGM_PAGE_GET_PAGEID(pPage) == NIL_GMM_PAGEID, ("pPage=%R[pgmpage]\n", pPage),
1298 VERR_PGM_PHYS_PAGE_MAP_IPE_1);
1299 if (!PGM_PAGE_IS_SPECIAL_ALIAS_MMIO(pPage))
1300 {
1301 AssertMsgReturn(PGM_PAGE_IS_ZERO(pPage), ("pPage=%R[pgmpage]\n", pPage),
1302 VERR_PGM_PHYS_PAGE_MAP_IPE_3);
1303 AssertMsgReturn(PGM_PAGE_GET_HCPHYS(pPage)== pVM->pgm.s.HCPhysZeroPg, ("pPage=%R[pgmpage]\n", pPage),
1304 VERR_PGM_PHYS_PAGE_MAP_IPE_4);
1305 *ppv = pVM->pgm.s.CTXALLSUFF(pvZeroPg);
1306 }
1307 else
1308 *ppv = pVM->pgm.s.CTXALLSUFF(pvZeroPg);
1309 *ppMap = NULL;
1310 return VINF_SUCCESS;
1311 }
1312
1313 /*
1314 * Find/make Chunk TLB entry for the mapping chunk.
1315 */
1316 PPGMCHUNKR3MAP pMap;
1317 PPGMCHUNKR3MAPTLBE pTlbe = &pVM->pgm.s.ChunkR3Map.Tlb.aEntries[PGM_CHUNKR3MAPTLB_IDX(idChunk)];
1318 if (pTlbe->idChunk == idChunk)
1319 {
1320 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,ChunkR3MapTlbHits));
1321 pMap = pTlbe->pChunk;
1322 AssertPtr(pMap->pv);
1323 }
1324 else
1325 {
1326 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,ChunkR3MapTlbMisses));
1327
1328 /*
1329 * Find the chunk, map it if necessary.
1330 */
1331 pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
1332 if (pMap)
1333 {
1334 AssertPtr(pMap->pv);
1335 pMap->iLastUsed = pVM->pgm.s.ChunkR3Map.iNow;
1336 }
1337 else
1338 {
1339#ifdef IN_RING0
1340 int rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_PGM_MAP_CHUNK, idChunk);
1341 AssertRCReturn(rc, rc);
1342 pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
1343 Assert(pMap);
1344#else
1345 int rc = pgmR3PhysChunkMap(pVM, idChunk, &pMap);
1346 if (RT_FAILURE(rc))
1347 return rc;
1348#endif
1349 AssertPtr(pMap->pv);
1350 }
1351
1352 /*
1353 * Enter it into the Chunk TLB.
1354 */
1355 pTlbe->idChunk = idChunk;
1356 pTlbe->pChunk = pMap;
1357 }
1358
1359 *ppv = (uint8_t *)pMap->pv + (PGM_PAGE_GET_PAGE_IN_CHUNK(pPage) << PAGE_SHIFT);
1360 *ppMap = pMap;
1361 return VINF_SUCCESS;
1362#endif /* IN_RING3 */
1363}
1364
1365
1366/**
1367 * Combination of pgmPhysPageMakeWritable and pgmPhysPageMapWritable.
1368 *
1369 * This is typically used is paths where we cannot use the TLB methods (like ROM
1370 * pages) or where there is no point in using them since we won't get many hits.
1371 *
1372 * @returns VBox strict status code.
1373 * @retval VINF_SUCCESS on success.
1374 * @retval VINF_PGM_SYNC_CR3 on success and a page pool flush is pending.
1375 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
1376 *
1377 * @param pVM The cross context VM structure.
1378 * @param pPage The physical page tracking structure.
1379 * @param GCPhys The address of the page.
1380 * @param ppv Where to store the mapping address of the page. The page
1381 * offset is masked off!
1382 *
1383 * @remarks Called from within the PGM critical section. The mapping is only
1384 * valid while you are inside section.
1385 */
1386int pgmPhysPageMakeWritableAndMap(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv)
1387{
1388 int rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
1389 if (RT_SUCCESS(rc))
1390 {
1391 AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* returned */, ("%Rrc\n", rc));
1392 PPGMPAGEMAP pMapIgnore;
1393 int rc2 = pgmPhysPageMapCommon(pVM, pPage, GCPhys, &pMapIgnore, ppv);
1394 if (RT_FAILURE(rc2)) /* preserve rc */
1395 rc = rc2;
1396 }
1397 return rc;
1398}
1399
1400
1401/**
1402 * Maps a page into the current virtual address space so it can be accessed for
1403 * both writing and reading.
1404 *
1405 * This is typically used is paths where we cannot use the TLB methods (like ROM
1406 * pages) or where there is no point in using them since we won't get many hits.
1407 *
1408 * @returns VBox status code.
1409 * @retval VINF_SUCCESS on success.
1410 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
1411 *
1412 * @param pVM The cross context VM structure.
1413 * @param pPage The physical page tracking structure. Must be in the
1414 * allocated state.
1415 * @param GCPhys The address of the page.
1416 * @param ppv Where to store the mapping address of the page. The page
1417 * offset is masked off!
1418 *
1419 * @remarks Called from within the PGM critical section. The mapping is only
1420 * valid while you are inside section.
1421 */
1422int pgmPhysPageMap(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv)
1423{
1424 Assert(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED);
1425 PPGMPAGEMAP pMapIgnore;
1426 return pgmPhysPageMapCommon(pVM, pPage, GCPhys, &pMapIgnore, ppv);
1427}
1428
1429
1430/**
1431 * Maps a page into the current virtual address space so it can be accessed for
1432 * reading.
1433 *
1434 * This is typically used is paths where we cannot use the TLB methods (like ROM
1435 * pages) or where there is no point in using them since we won't get many hits.
1436 *
1437 * @returns VBox status code.
1438 * @retval VINF_SUCCESS on success.
1439 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
1440 *
1441 * @param pVM The cross context VM structure.
1442 * @param pPage The physical page tracking structure.
1443 * @param GCPhys The address of the page.
1444 * @param ppv Where to store the mapping address of the page. The page
1445 * offset is masked off!
1446 *
1447 * @remarks Called from within the PGM critical section. The mapping is only
1448 * valid while you are inside this section.
1449 */
1450int pgmPhysPageMapReadOnly(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void const **ppv)
1451{
1452 PPGMPAGEMAP pMapIgnore;
1453 return pgmPhysPageMapCommon(pVM, pPage, GCPhys, &pMapIgnore, (void **)ppv);
1454}
1455
1456#if !defined(IN_RC) && !defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1457
1458/**
1459 * Load a guest page into the ring-3 physical TLB.
1460 *
1461 * @returns VBox status code.
1462 * @retval VINF_SUCCESS on success
1463 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
1464 * @param pPGM The PGM instance pointer.
1465 * @param GCPhys The guest physical address in question.
1466 */
1467int pgmPhysPageLoadIntoTlb(PVM pVM, RTGCPHYS GCPhys)
1468{
1469 PGM_LOCK_ASSERT_OWNER(pVM);
1470
1471 /*
1472 * Find the ram range and page and hand it over to the with-page function.
1473 * 99.8% of requests are expected to be in the first range.
1474 */
1475 PPGMPAGE pPage = pgmPhysGetPage(pVM, GCPhys);
1476 if (!pPage)
1477 {
1478 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PageMapTlbMisses));
1479 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
1480 }
1481
1482 return pgmPhysPageLoadIntoTlbWithPage(pVM, pPage, GCPhys);
1483}
1484
1485
1486/**
1487 * Load a guest page into the ring-3 physical TLB.
1488 *
1489 * @returns VBox status code.
1490 * @retval VINF_SUCCESS on success
1491 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
1492 *
1493 * @param pVM The cross context VM structure.
1494 * @param pPage Pointer to the PGMPAGE structure corresponding to
1495 * GCPhys.
1496 * @param GCPhys The guest physical address in question.
1497 */
1498int pgmPhysPageLoadIntoTlbWithPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
1499{
1500 PGM_LOCK_ASSERT_OWNER(pVM);
1501 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PageMapTlbMisses));
1502
1503 /*
1504 * Map the page.
1505 * Make a special case for the zero page as it is kind of special.
1506 */
1507 PPGMPAGEMAPTLBE pTlbe = &pVM->pgm.s.CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
1508 if ( !PGM_PAGE_IS_ZERO(pPage)
1509 && !PGM_PAGE_IS_BALLOONED(pPage))
1510 {
1511 void *pv;
1512 PPGMPAGEMAP pMap;
1513 int rc = pgmPhysPageMapCommon(pVM, pPage, GCPhys, &pMap, &pv);
1514 if (RT_FAILURE(rc))
1515 return rc;
1516 pTlbe->pMap = pMap;
1517 pTlbe->pv = pv;
1518 Assert(!((uintptr_t)pTlbe->pv & PAGE_OFFSET_MASK));
1519 }
1520 else
1521 {
1522 AssertMsg(PGM_PAGE_GET_HCPHYS(pPage) == pVM->pgm.s.HCPhysZeroPg, ("%RGp/%R[pgmpage]\n", GCPhys, pPage));
1523 pTlbe->pMap = NULL;
1524 pTlbe->pv = pVM->pgm.s.CTXALLSUFF(pvZeroPg);
1525 }
1526#ifdef PGM_WITH_PHYS_TLB
1527 if ( PGM_PAGE_GET_TYPE(pPage) < PGMPAGETYPE_ROM_SHADOW
1528 || PGM_PAGE_GET_TYPE(pPage) > PGMPAGETYPE_ROM)
1529 pTlbe->GCPhys = GCPhys & X86_PTE_PAE_PG_MASK;
1530 else
1531 pTlbe->GCPhys = NIL_RTGCPHYS; /* ROM: Problematic because of the two pages. :-/ */
1532#else
1533 pTlbe->GCPhys = NIL_RTGCPHYS;
1534#endif
1535 pTlbe->pPage = pPage;
1536 return VINF_SUCCESS;
1537}
1538
1539#endif /* !IN_RC && !VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
1540
1541/**
1542 * Internal version of PGMPhysGCPhys2CCPtr that expects the caller to
1543 * own the PGM lock and therefore not need to lock the mapped page.
1544 *
1545 * @returns VBox status code.
1546 * @retval VINF_SUCCESS on success.
1547 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
1548 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
1549 *
1550 * @param pVM The cross context VM structure.
1551 * @param GCPhys The guest physical address of the page that should be mapped.
1552 * @param pPage Pointer to the PGMPAGE structure for the page.
1553 * @param ppv Where to store the address corresponding to GCPhys.
1554 *
1555 * @internal
1556 * @deprecated Use pgmPhysGCPhys2CCPtrInternalEx.
1557 */
1558int pgmPhysGCPhys2CCPtrInternalDepr(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv)
1559{
1560 int rc;
1561 AssertReturn(pPage, VERR_PGM_PHYS_NULL_PAGE_PARAM);
1562 PGM_LOCK_ASSERT_OWNER(pVM);
1563 pVM->pgm.s.cDeprecatedPageLocks++;
1564
1565 /*
1566 * Make sure the page is writable.
1567 */
1568 if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
1569 {
1570 rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
1571 if (RT_FAILURE(rc))
1572 return rc;
1573 AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* not returned */, ("%Rrc\n", rc));
1574 }
1575 Assert(PGM_PAGE_GET_HCPHYS(pPage) != 0);
1576
1577 /*
1578 * Get the mapping address.
1579 */
1580#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1581 void *pv;
1582 rc = pgmRZDynMapHCPageInlined(VMMGetCpu(pVM),
1583 PGM_PAGE_GET_HCPHYS(pPage),
1584 &pv
1585 RTLOG_COMMA_SRC_POS);
1586 if (RT_FAILURE(rc))
1587 return rc;
1588 *ppv = (void *)((uintptr_t)pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
1589#else
1590 PPGMPAGEMAPTLBE pTlbe;
1591 rc = pgmPhysPageQueryTlbeWithPage(pVM, pPage, GCPhys, &pTlbe);
1592 if (RT_FAILURE(rc))
1593 return rc;
1594 *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
1595#endif
1596 return VINF_SUCCESS;
1597}
1598
1599#if !defined(IN_RC) && !defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1600
1601/**
1602 * Locks a page mapping for writing.
1603 *
1604 * @param pVM The cross context VM structure.
1605 * @param pPage The page.
1606 * @param pTlbe The mapping TLB entry for the page.
1607 * @param pLock The lock structure (output).
1608 */
1609DECLINLINE(void) pgmPhysPageMapLockForWriting(PVM pVM, PPGMPAGE pPage, PPGMPAGEMAPTLBE pTlbe, PPGMPAGEMAPLOCK pLock)
1610{
1611 PPGMPAGEMAP pMap = pTlbe->pMap;
1612 if (pMap)
1613 pMap->cRefs++;
1614
1615 unsigned cLocks = PGM_PAGE_GET_WRITE_LOCKS(pPage);
1616 if (RT_LIKELY(cLocks < PGM_PAGE_MAX_LOCKS - 1))
1617 {
1618 if (cLocks == 0)
1619 pVM->pgm.s.cWriteLockedPages++;
1620 PGM_PAGE_INC_WRITE_LOCKS(pPage);
1621 }
1622 else if (cLocks != PGM_PAGE_MAX_LOCKS)
1623 {
1624 PGM_PAGE_INC_WRITE_LOCKS(pPage);
1625 AssertMsgFailed(("%R[pgmpage] is entering permanent write locked state!\n", pPage));
1626 if (pMap)
1627 pMap->cRefs++; /* Extra ref to prevent it from going away. */
1628 }
1629
1630 pLock->uPageAndType = (uintptr_t)pPage | PGMPAGEMAPLOCK_TYPE_WRITE;
1631 pLock->pvMap = pMap;
1632}
1633
1634/**
1635 * Locks a page mapping for reading.
1636 *
1637 * @param pVM The cross context VM structure.
1638 * @param pPage The page.
1639 * @param pTlbe The mapping TLB entry for the page.
1640 * @param pLock The lock structure (output).
1641 */
1642DECLINLINE(void) pgmPhysPageMapLockForReading(PVM pVM, PPGMPAGE pPage, PPGMPAGEMAPTLBE pTlbe, PPGMPAGEMAPLOCK pLock)
1643{
1644 PPGMPAGEMAP pMap = pTlbe->pMap;
1645 if (pMap)
1646 pMap->cRefs++;
1647
1648 unsigned cLocks = PGM_PAGE_GET_READ_LOCKS(pPage);
1649 if (RT_LIKELY(cLocks < PGM_PAGE_MAX_LOCKS - 1))
1650 {
1651 if (cLocks == 0)
1652 pVM->pgm.s.cReadLockedPages++;
1653 PGM_PAGE_INC_READ_LOCKS(pPage);
1654 }
1655 else if (cLocks != PGM_PAGE_MAX_LOCKS)
1656 {
1657 PGM_PAGE_INC_READ_LOCKS(pPage);
1658 AssertMsgFailed(("%R[pgmpage] is entering permanent read locked state!\n", pPage));
1659 if (pMap)
1660 pMap->cRefs++; /* Extra ref to prevent it from going away. */
1661 }
1662
1663 pLock->uPageAndType = (uintptr_t)pPage | PGMPAGEMAPLOCK_TYPE_READ;
1664 pLock->pvMap = pMap;
1665}
1666
1667#endif /* !IN_RC && !VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
1668
1669
1670/**
1671 * Internal version of PGMPhysGCPhys2CCPtr that expects the caller to
1672 * own the PGM lock and have access to the page structure.
1673 *
1674 * @returns VBox status code.
1675 * @retval VINF_SUCCESS on success.
1676 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
1677 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
1678 *
1679 * @param pVM The cross context VM structure.
1680 * @param GCPhys The guest physical address of the page that should be mapped.
1681 * @param pPage Pointer to the PGMPAGE structure for the page.
1682 * @param ppv Where to store the address corresponding to GCPhys.
1683 * @param pLock Where to store the lock information that
1684 * pgmPhysReleaseInternalPageMappingLock needs.
1685 *
1686 * @internal
1687 */
1688int pgmPhysGCPhys2CCPtrInternal(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
1689{
1690 int rc;
1691 AssertReturn(pPage, VERR_PGM_PHYS_NULL_PAGE_PARAM);
1692 PGM_LOCK_ASSERT_OWNER(pVM);
1693
1694 /*
1695 * Make sure the page is writable.
1696 */
1697 if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
1698 {
1699 rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
1700 if (RT_FAILURE(rc))
1701 return rc;
1702 AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* not returned */, ("%Rrc\n", rc));
1703 }
1704 Assert(PGM_PAGE_GET_HCPHYS(pPage) != 0);
1705
1706 /*
1707 * Do the job.
1708 */
1709#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1710 void *pv;
1711 PVMCPU pVCpu = VMMGetCpu(pVM);
1712 rc = pgmRZDynMapHCPageInlined(pVCpu,
1713 PGM_PAGE_GET_HCPHYS(pPage),
1714 &pv
1715 RTLOG_COMMA_SRC_POS);
1716 if (RT_FAILURE(rc))
1717 return rc;
1718 *ppv = (void *)((uintptr_t)pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
1719 pLock->pvPage = pv;
1720 pLock->pVCpu = pVCpu;
1721
1722#else
1723 PPGMPAGEMAPTLBE pTlbe;
1724 rc = pgmPhysPageQueryTlbeWithPage(pVM, pPage, GCPhys, &pTlbe);
1725 if (RT_FAILURE(rc))
1726 return rc;
1727 pgmPhysPageMapLockForWriting(pVM, pPage, pTlbe, pLock);
1728 *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
1729#endif
1730 return VINF_SUCCESS;
1731}
1732
1733
1734/**
1735 * Internal version of PGMPhysGCPhys2CCPtrReadOnly that expects the caller to
1736 * own the PGM lock and have access to the page structure.
1737 *
1738 * @returns VBox status code.
1739 * @retval VINF_SUCCESS on success.
1740 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
1741 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
1742 *
1743 * @param pVM The cross context VM structure.
1744 * @param GCPhys The guest physical address of the page that should be mapped.
1745 * @param pPage Pointer to the PGMPAGE structure for the page.
1746 * @param ppv Where to store the address corresponding to GCPhys.
1747 * @param pLock Where to store the lock information that
1748 * pgmPhysReleaseInternalPageMappingLock needs.
1749 *
1750 * @internal
1751 */
1752int pgmPhysGCPhys2CCPtrInternalReadOnly(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, const void **ppv, PPGMPAGEMAPLOCK pLock)
1753{
1754 AssertReturn(pPage, VERR_PGM_PHYS_NULL_PAGE_PARAM);
1755 PGM_LOCK_ASSERT_OWNER(pVM);
1756 Assert(PGM_PAGE_GET_HCPHYS(pPage) != 0);
1757
1758 /*
1759 * Do the job.
1760 */
1761#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1762 void *pv;
1763 PVMCPU pVCpu = VMMGetCpu(pVM);
1764 int rc = pgmRZDynMapHCPageInlined(pVCpu,
1765 PGM_PAGE_GET_HCPHYS(pPage),
1766 &pv
1767 RTLOG_COMMA_SRC_POS); /** @todo add a read only flag? */
1768 if (RT_FAILURE(rc))
1769 return rc;
1770 *ppv = (void *)((uintptr_t)pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
1771 pLock->pvPage = pv;
1772 pLock->pVCpu = pVCpu;
1773
1774#else
1775 PPGMPAGEMAPTLBE pTlbe;
1776 int rc = pgmPhysPageQueryTlbeWithPage(pVM, pPage, GCPhys, &pTlbe);
1777 if (RT_FAILURE(rc))
1778 return rc;
1779 pgmPhysPageMapLockForReading(pVM, pPage, pTlbe, pLock);
1780 *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
1781#endif
1782 return VINF_SUCCESS;
1783}
1784
1785
1786/**
1787 * Requests the mapping of a guest page into the current context.
1788 *
1789 * This API should only be used for very short term, as it will consume scarse
1790 * resources (R0 and GC) in the mapping cache. When you're done with the page,
1791 * call PGMPhysReleasePageMappingLock() ASAP to release it.
1792 *
1793 * This API will assume your intention is to write to the page, and will
1794 * therefore replace shared and zero pages. If you do not intend to modify
1795 * the page, use the PGMPhysGCPhys2CCPtrReadOnly() API.
1796 *
1797 * @returns VBox status code.
1798 * @retval VINF_SUCCESS on success.
1799 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
1800 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
1801 *
1802 * @param pVM The cross context VM structure.
1803 * @param GCPhys The guest physical address of the page that should be
1804 * mapped.
1805 * @param ppv Where to store the address corresponding to GCPhys.
1806 * @param pLock Where to store the lock information that
1807 * PGMPhysReleasePageMappingLock needs.
1808 *
1809 * @remarks The caller is responsible for dealing with access handlers.
1810 * @todo Add an informational return code for pages with access handlers?
1811 *
1812 * @remark Avoid calling this API from within critical sections (other than
1813 * the PGM one) because of the deadlock risk. External threads may
1814 * need to delegate jobs to the EMTs.
1815 * @remarks Only one page is mapped! Make no assumption about what's after or
1816 * before the returned page!
1817 * @thread Any thread.
1818 */
1819VMM_INT_DECL(int) PGMPhysGCPhys2CCPtr(PVM pVM, RTGCPHYS GCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
1820{
1821 int rc = pgmLock(pVM);
1822 AssertRCReturn(rc, rc);
1823
1824#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1825 /*
1826 * Find the page and make sure it's writable.
1827 */
1828 PPGMPAGE pPage;
1829 rc = pgmPhysGetPageEx(pVM, GCPhys, &pPage);
1830 if (RT_SUCCESS(rc))
1831 {
1832 if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
1833 rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
1834 if (RT_SUCCESS(rc))
1835 {
1836 AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* not returned */, ("%Rrc\n", rc));
1837
1838 PVMCPU pVCpu = VMMGetCpu(pVM);
1839 void *pv;
1840 rc = pgmRZDynMapHCPageInlined(pVCpu,
1841 PGM_PAGE_GET_HCPHYS(pPage),
1842 &pv
1843 RTLOG_COMMA_SRC_POS);
1844 if (RT_SUCCESS(rc))
1845 {
1846 AssertRCSuccess(rc);
1847
1848 pv = (void *)((uintptr_t)pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
1849 *ppv = pv;
1850 pLock->pvPage = pv;
1851 pLock->pVCpu = pVCpu;
1852 }
1853 }
1854 }
1855
1856#else /* IN_RING3 || IN_RING0 */
1857 /*
1858 * Query the Physical TLB entry for the page (may fail).
1859 */
1860 PPGMPAGEMAPTLBE pTlbe;
1861 rc = pgmPhysPageQueryTlbe(pVM, GCPhys, &pTlbe);
1862 if (RT_SUCCESS(rc))
1863 {
1864 /*
1865 * If the page is shared, the zero page, or being write monitored
1866 * it must be converted to a page that's writable if possible.
1867 */
1868 PPGMPAGE pPage = pTlbe->pPage;
1869 if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
1870 {
1871 rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
1872 if (RT_SUCCESS(rc))
1873 {
1874 AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* not returned */, ("%Rrc\n", rc));
1875 rc = pgmPhysPageQueryTlbeWithPage(pVM, pPage, GCPhys, &pTlbe);
1876 }
1877 }
1878 if (RT_SUCCESS(rc))
1879 {
1880 /*
1881 * Now, just perform the locking and calculate the return address.
1882 */
1883 pgmPhysPageMapLockForWriting(pVM, pPage, pTlbe, pLock);
1884 *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
1885 }
1886 }
1887
1888#endif /* IN_RING3 || IN_RING0 */
1889 pgmUnlock(pVM);
1890 return rc;
1891}
1892
1893
1894/**
1895 * Requests the mapping of a guest page into the current context.
1896 *
1897 * This API should only be used for very short term, as it will consume scarse
1898 * resources (R0 and GC) in the mapping cache. When you're done with the page,
1899 * call PGMPhysReleasePageMappingLock() ASAP to release it.
1900 *
1901 * @returns VBox status code.
1902 * @retval VINF_SUCCESS on success.
1903 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
1904 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
1905 *
1906 * @param pVM The cross context VM structure.
1907 * @param GCPhys The guest physical address of the page that should be
1908 * mapped.
1909 * @param ppv Where to store the address corresponding to GCPhys.
1910 * @param pLock Where to store the lock information that
1911 * PGMPhysReleasePageMappingLock needs.
1912 *
1913 * @remarks The caller is responsible for dealing with access handlers.
1914 * @todo Add an informational return code for pages with access handlers?
1915 *
1916 * @remarks Avoid calling this API from within critical sections (other than
1917 * the PGM one) because of the deadlock risk.
1918 * @remarks Only one page is mapped! Make no assumption about what's after or
1919 * before the returned page!
1920 * @thread Any thread.
1921 */
1922VMM_INT_DECL(int) PGMPhysGCPhys2CCPtrReadOnly(PVM pVM, RTGCPHYS GCPhys, void const **ppv, PPGMPAGEMAPLOCK pLock)
1923{
1924 int rc = pgmLock(pVM);
1925 AssertRCReturn(rc, rc);
1926
1927#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1928 /*
1929 * Find the page and make sure it's readable.
1930 */
1931 PPGMPAGE pPage;
1932 rc = pgmPhysGetPageEx(pVM, GCPhys, &pPage);
1933 if (RT_SUCCESS(rc))
1934 {
1935 if (RT_UNLIKELY(PGM_PAGE_IS_MMIO_OR_SPECIAL_ALIAS(pPage)))
1936 rc = VERR_PGM_PHYS_PAGE_RESERVED;
1937 else
1938 {
1939 PVMCPU pVCpu = VMMGetCpu(pVM);
1940 void *pv;
1941 rc = pgmRZDynMapHCPageInlined(pVCpu,
1942 PGM_PAGE_GET_HCPHYS(pPage),
1943 &pv
1944 RTLOG_COMMA_SRC_POS); /** @todo add a read only flag? */
1945 if (RT_SUCCESS(rc))
1946 {
1947 AssertRCSuccess(rc);
1948
1949 pv = (void *)((uintptr_t)pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
1950 *ppv = pv;
1951 pLock->pvPage = pv;
1952 pLock->pVCpu = pVCpu;
1953 }
1954 }
1955 }
1956
1957#else /* IN_RING3 || IN_RING0 */
1958 /*
1959 * Query the Physical TLB entry for the page (may fail).
1960 */
1961 PPGMPAGEMAPTLBE pTlbe;
1962 rc = pgmPhysPageQueryTlbe(pVM, GCPhys, &pTlbe);
1963 if (RT_SUCCESS(rc))
1964 {
1965 /* MMIO pages doesn't have any readable backing. */
1966 PPGMPAGE pPage = pTlbe->pPage;
1967 if (RT_UNLIKELY(PGM_PAGE_IS_MMIO_OR_SPECIAL_ALIAS(pPage)))
1968 rc = VERR_PGM_PHYS_PAGE_RESERVED;
1969 else
1970 {
1971 /*
1972 * Now, just perform the locking and calculate the return address.
1973 */
1974 pgmPhysPageMapLockForReading(pVM, pPage, pTlbe, pLock);
1975 *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
1976 }
1977 }
1978
1979#endif /* IN_RING3 || IN_RING0 */
1980 pgmUnlock(pVM);
1981 return rc;
1982}
1983
1984
1985/**
1986 * Requests the mapping of a guest page given by virtual address into the current context.
1987 *
1988 * This API should only be used for very short term, as it will consume
1989 * scarse resources (R0 and GC) in the mapping cache. When you're done
1990 * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
1991 *
1992 * This API will assume your intention is to write to the page, and will
1993 * therefore replace shared and zero pages. If you do not intend to modify
1994 * the page, use the PGMPhysGCPtr2CCPtrReadOnly() API.
1995 *
1996 * @returns VBox status code.
1997 * @retval VINF_SUCCESS on success.
1998 * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
1999 * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
2000 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
2001 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
2002 *
2003 * @param pVCpu The cross context virtual CPU structure.
2004 * @param GCPtr The guest physical address of the page that should be
2005 * mapped.
2006 * @param ppv Where to store the address corresponding to GCPhys.
2007 * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
2008 *
2009 * @remark Avoid calling this API from within critical sections (other than
2010 * the PGM one) because of the deadlock risk.
2011 * @thread EMT
2012 */
2013VMM_INT_DECL(int) PGMPhysGCPtr2CCPtr(PVMCPU pVCpu, RTGCPTR GCPtr, void **ppv, PPGMPAGEMAPLOCK pLock)
2014{
2015 VM_ASSERT_EMT(pVCpu->CTX_SUFF(pVM));
2016 RTGCPHYS GCPhys;
2017 int rc = PGMPhysGCPtr2GCPhys(pVCpu, GCPtr, &GCPhys);
2018 if (RT_SUCCESS(rc))
2019 rc = PGMPhysGCPhys2CCPtr(pVCpu->CTX_SUFF(pVM), GCPhys, ppv, pLock);
2020 return rc;
2021}
2022
2023
2024/**
2025 * Requests the mapping of a guest page given by virtual address into the current context.
2026 *
2027 * This API should only be used for very short term, as it will consume
2028 * scarse resources (R0 and GC) in the mapping cache. When you're done
2029 * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
2030 *
2031 * @returns VBox status code.
2032 * @retval VINF_SUCCESS on success.
2033 * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
2034 * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
2035 * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
2036 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
2037 *
2038 * @param pVCpu The cross context virtual CPU structure.
2039 * @param GCPtr The guest physical address of the page that should be
2040 * mapped.
2041 * @param ppv Where to store the address corresponding to GCPtr.
2042 * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
2043 *
2044 * @remark Avoid calling this API from within critical sections (other than
2045 * the PGM one) because of the deadlock risk.
2046 * @thread EMT
2047 */
2048VMM_INT_DECL(int) PGMPhysGCPtr2CCPtrReadOnly(PVMCPU pVCpu, RTGCPTR GCPtr, void const **ppv, PPGMPAGEMAPLOCK pLock)
2049{
2050 VM_ASSERT_EMT(pVCpu->CTX_SUFF(pVM));
2051 RTGCPHYS GCPhys;
2052 int rc = PGMPhysGCPtr2GCPhys(pVCpu, GCPtr, &GCPhys);
2053 if (RT_SUCCESS(rc))
2054 rc = PGMPhysGCPhys2CCPtrReadOnly(pVCpu->CTX_SUFF(pVM), GCPhys, ppv, pLock);
2055 return rc;
2056}
2057
2058
2059/**
2060 * Release the mapping of a guest page.
2061 *
2062 * This is the counter part of PGMPhysGCPhys2CCPtr, PGMPhysGCPhys2CCPtrReadOnly
2063 * PGMPhysGCPtr2CCPtr and PGMPhysGCPtr2CCPtrReadOnly.
2064 *
2065 * @param pVM The cross context VM structure.
2066 * @param pLock The lock structure initialized by the mapping function.
2067 */
2068VMMDECL(void) PGMPhysReleasePageMappingLock(PVM pVM, PPGMPAGEMAPLOCK pLock)
2069{
2070#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
2071 Assert(pLock->pvPage != NULL);
2072 Assert(pLock->pVCpu == VMMGetCpu(pVM)); RT_NOREF_PV(pVM);
2073 PGM_DYNMAP_UNUSED_HINT(pLock->pVCpu, pLock->pvPage);
2074 pLock->pVCpu = NULL;
2075 pLock->pvPage = NULL;
2076
2077#else
2078 PPGMPAGEMAP pMap = (PPGMPAGEMAP)pLock->pvMap;
2079 PPGMPAGE pPage = (PPGMPAGE)(pLock->uPageAndType & ~PGMPAGEMAPLOCK_TYPE_MASK);
2080 bool fWriteLock = (pLock->uPageAndType & PGMPAGEMAPLOCK_TYPE_MASK) == PGMPAGEMAPLOCK_TYPE_WRITE;
2081
2082 pLock->uPageAndType = 0;
2083 pLock->pvMap = NULL;
2084
2085 pgmLock(pVM);
2086 if (fWriteLock)
2087 {
2088 unsigned cLocks = PGM_PAGE_GET_WRITE_LOCKS(pPage);
2089 Assert(cLocks > 0);
2090 if (RT_LIKELY(cLocks > 0 && cLocks < PGM_PAGE_MAX_LOCKS))
2091 {
2092 if (cLocks == 1)
2093 {
2094 Assert(pVM->pgm.s.cWriteLockedPages > 0);
2095 pVM->pgm.s.cWriteLockedPages--;
2096 }
2097 PGM_PAGE_DEC_WRITE_LOCKS(pPage);
2098 }
2099
2100 if (PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_WRITE_MONITORED)
2101 { /* probably extremely likely */ }
2102 else
2103 pgmPhysPageMakeWriteMonitoredWritable(pVM, pPage, NIL_RTGCPHYS);
2104 }
2105 else
2106 {
2107 unsigned cLocks = PGM_PAGE_GET_READ_LOCKS(pPage);
2108 Assert(cLocks > 0);
2109 if (RT_LIKELY(cLocks > 0 && cLocks < PGM_PAGE_MAX_LOCKS))
2110 {
2111 if (cLocks == 1)
2112 {
2113 Assert(pVM->pgm.s.cReadLockedPages > 0);
2114 pVM->pgm.s.cReadLockedPages--;
2115 }
2116 PGM_PAGE_DEC_READ_LOCKS(pPage);
2117 }
2118 }
2119
2120 if (pMap)
2121 {
2122 Assert(pMap->cRefs >= 1);
2123 pMap->cRefs--;
2124 }
2125 pgmUnlock(pVM);
2126#endif /* IN_RING3 */
2127}
2128
2129
2130#ifdef IN_RING3
2131/**
2132 * Release the mapping of multiple guest pages.
2133 *
2134 * This is the counter part to PGMR3PhysBulkGCPhys2CCPtrExternal() and
2135 * PGMR3PhysBulkGCPhys2CCPtrReadOnlyExternal().
2136 *
2137 * @param pVM The cross context VM structure.
2138 * @param cPages Number of pages to unlock.
2139 * @param paLocks Array of locks lock structure initialized by the mapping
2140 * function.
2141 */
2142VMMDECL(void) PGMPhysBulkReleasePageMappingLocks(PVM pVM, uint32_t cPages, PPGMPAGEMAPLOCK paLocks)
2143{
2144 Assert(cPages > 0);
2145 bool const fWriteLock = (paLocks[0].uPageAndType & PGMPAGEMAPLOCK_TYPE_MASK) == PGMPAGEMAPLOCK_TYPE_WRITE;
2146#ifdef VBOX_STRICT
2147 for (uint32_t i = 1; i < cPages; i++)
2148 {
2149 Assert(fWriteLock == ((paLocks[i].uPageAndType & PGMPAGEMAPLOCK_TYPE_MASK) == PGMPAGEMAPLOCK_TYPE_WRITE));
2150 AssertPtr(paLocks[i].uPageAndType);
2151 }
2152#endif
2153
2154 pgmLock(pVM);
2155 if (fWriteLock)
2156 {
2157 /*
2158 * Write locks:
2159 */
2160 for (uint32_t i = 0; i < cPages; i++)
2161 {
2162 PPGMPAGE pPage = (PPGMPAGE)(paLocks[i].uPageAndType & ~PGMPAGEMAPLOCK_TYPE_MASK);
2163 unsigned cLocks = PGM_PAGE_GET_WRITE_LOCKS(pPage);
2164 Assert(cLocks > 0);
2165 if (RT_LIKELY(cLocks > 0 && cLocks < PGM_PAGE_MAX_LOCKS))
2166 {
2167 if (cLocks == 1)
2168 {
2169 Assert(pVM->pgm.s.cWriteLockedPages > 0);
2170 pVM->pgm.s.cWriteLockedPages--;
2171 }
2172 PGM_PAGE_DEC_WRITE_LOCKS(pPage);
2173 }
2174
2175 if (PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_WRITE_MONITORED)
2176 { /* probably extremely likely */ }
2177 else
2178 pgmPhysPageMakeWriteMonitoredWritable(pVM, pPage, NIL_RTGCPHYS);
2179
2180 PPGMPAGEMAP pMap = (PPGMPAGEMAP)paLocks[i].pvMap;
2181 if (pMap)
2182 {
2183 Assert(pMap->cRefs >= 1);
2184 pMap->cRefs--;
2185 }
2186
2187 /* Yield the lock: */
2188 if ((i & 1023) == 1023)
2189 {
2190 pgmLock(pVM);
2191 pgmUnlock(pVM);
2192 }
2193 }
2194 }
2195 else
2196 {
2197 /*
2198 * Read locks:
2199 */
2200 for (uint32_t i = 0; i < cPages; i++)
2201 {
2202 PPGMPAGE pPage = (PPGMPAGE)(paLocks[i].uPageAndType & ~PGMPAGEMAPLOCK_TYPE_MASK);
2203 unsigned cLocks = PGM_PAGE_GET_READ_LOCKS(pPage);
2204 Assert(cLocks > 0);
2205 if (RT_LIKELY(cLocks > 0 && cLocks < PGM_PAGE_MAX_LOCKS))
2206 {
2207 if (cLocks == 1)
2208 {
2209 Assert(pVM->pgm.s.cReadLockedPages > 0);
2210 pVM->pgm.s.cReadLockedPages--;
2211 }
2212 PGM_PAGE_DEC_READ_LOCKS(pPage);
2213 }
2214
2215 PPGMPAGEMAP pMap = (PPGMPAGEMAP)paLocks[i].pvMap;
2216 if (pMap)
2217 {
2218 Assert(pMap->cRefs >= 1);
2219 pMap->cRefs--;
2220 }
2221
2222 /* Yield the lock: */
2223 if ((i & 1023) == 1023)
2224 {
2225 pgmLock(pVM);
2226 pgmUnlock(pVM);
2227 }
2228 }
2229 }
2230 pgmUnlock(pVM);
2231
2232 RT_BZERO(paLocks, sizeof(paLocks[0]) * cPages);
2233}
2234#endif /* IN_RING3 */
2235
2236
2237/**
2238 * Release the internal mapping of a guest page.
2239 *
2240 * This is the counter part of pgmPhysGCPhys2CCPtrInternalEx and
2241 * pgmPhysGCPhys2CCPtrInternalReadOnly.
2242 *
2243 * @param pVM The cross context VM structure.
2244 * @param pLock The lock structure initialized by the mapping function.
2245 *
2246 * @remarks Caller must hold the PGM lock.
2247 */
2248void pgmPhysReleaseInternalPageMappingLock(PVM pVM, PPGMPAGEMAPLOCK pLock)
2249{
2250 PGM_LOCK_ASSERT_OWNER(pVM);
2251 PGMPhysReleasePageMappingLock(pVM, pLock); /* lazy for now */
2252}
2253
2254
2255/**
2256 * Converts a GC physical address to a HC ring-3 pointer.
2257 *
2258 * @returns VINF_SUCCESS on success.
2259 * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
2260 * page but has no physical backing.
2261 * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
2262 * GC physical address.
2263 * @returns VERR_PGM_GCPHYS_RANGE_CROSSES_BOUNDARY if the range crosses
2264 * a dynamic ram chunk boundary
2265 *
2266 * @param pVM The cross context VM structure.
2267 * @param GCPhys The GC physical address to convert.
2268 * @param pR3Ptr Where to store the R3 pointer on success.
2269 *
2270 * @deprecated Avoid when possible!
2271 */
2272int pgmPhysGCPhys2R3Ptr(PVM pVM, RTGCPHYS GCPhys, PRTR3PTR pR3Ptr)
2273{
2274/** @todo this is kind of hacky and needs some more work. */
2275#ifndef DEBUG_sandervl
2276 VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
2277#endif
2278
2279 Log(("pgmPhysGCPhys2R3Ptr(,%RGp,): dont use this API!\n", GCPhys)); /** @todo eliminate this API! */
2280#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
2281 NOREF(pVM); NOREF(pR3Ptr); RT_NOREF_PV(GCPhys);
2282 AssertFailedReturn(VERR_NOT_IMPLEMENTED);
2283#else
2284 pgmLock(pVM);
2285
2286 PPGMRAMRANGE pRam;
2287 PPGMPAGE pPage;
2288 int rc = pgmPhysGetPageAndRangeEx(pVM, GCPhys, &pPage, &pRam);
2289 if (RT_SUCCESS(rc))
2290 rc = pgmPhysGCPhys2CCPtrInternalDepr(pVM, pPage, GCPhys, (void **)pR3Ptr);
2291
2292 pgmUnlock(pVM);
2293 Assert(rc <= VINF_SUCCESS);
2294 return rc;
2295#endif
2296}
2297
2298#if 0 /*defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)*/
2299
2300/**
2301 * Maps and locks a guest CR3 or PD (PAE) page.
2302 *
2303 * @returns VINF_SUCCESS on success.
2304 * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
2305 * page but has no physical backing.
2306 * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
2307 * GC physical address.
2308 * @returns VERR_PGM_GCPHYS_RANGE_CROSSES_BOUNDARY if the range crosses
2309 * a dynamic ram chunk boundary
2310 *
2311 * @param pVM The cross context VM structure.
2312 * @param GCPhys The GC physical address to convert.
2313 * @param pR3Ptr Where to store the R3 pointer on success. This may or
2314 * may not be valid in ring-0 depending on the
2315 * VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 build option.
2316 *
2317 * @remarks The caller must own the PGM lock.
2318 */
2319int pgmPhysCr3ToHCPtr(PVM pVM, RTGCPHYS GCPhys, PRTR3PTR pR3Ptr)
2320{
2321
2322 PPGMRAMRANGE pRam;
2323 PPGMPAGE pPage;
2324 int rc = pgmPhysGetPageAndRangeEx(pVM, GCPhys, &pPage, &pRam);
2325 if (RT_SUCCESS(rc))
2326 rc = pgmPhysGCPhys2CCPtrInternalDepr(pVM, pPage, GCPhys, (void **)pR3Ptr);
2327 Assert(rc <= VINF_SUCCESS);
2328 return rc;
2329}
2330
2331
2332int pgmPhysCr3ToHCPtr(PVM pVM, RTGCPHYS GCPhys, PRTR3PTR pR3Ptr)
2333{
2334
2335}
2336
2337#endif
2338
2339/**
2340 * Converts a guest pointer to a GC physical address.
2341 *
2342 * This uses the current CR3/CR0/CR4 of the guest.
2343 *
2344 * @returns VBox status code.
2345 * @param pVCpu The cross context virtual CPU structure.
2346 * @param GCPtr The guest pointer to convert.
2347 * @param pGCPhys Where to store the GC physical address.
2348 */
2349VMMDECL(int) PGMPhysGCPtr2GCPhys(PVMCPU pVCpu, RTGCPTR GCPtr, PRTGCPHYS pGCPhys)
2350{
2351 int rc = PGMGstGetPage(pVCpu, (RTGCUINTPTR)GCPtr, NULL, pGCPhys);
2352 if (pGCPhys && RT_SUCCESS(rc))
2353 *pGCPhys |= (RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK;
2354 return rc;
2355}
2356
2357
2358/**
2359 * Converts a guest pointer to a HC physical address.
2360 *
2361 * This uses the current CR3/CR0/CR4 of the guest.
2362 *
2363 * @returns VBox status code.
2364 * @param pVCpu The cross context virtual CPU structure.
2365 * @param GCPtr The guest pointer to convert.
2366 * @param pHCPhys Where to store the HC physical address.
2367 */
2368VMM_INT_DECL(int) PGMPhysGCPtr2HCPhys(PVMCPU pVCpu, RTGCPTR GCPtr, PRTHCPHYS pHCPhys)
2369{
2370 PVM pVM = pVCpu->CTX_SUFF(pVM);
2371 RTGCPHYS GCPhys;
2372 int rc = PGMGstGetPage(pVCpu, (RTGCUINTPTR)GCPtr, NULL, &GCPhys);
2373 if (RT_SUCCESS(rc))
2374 rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), pHCPhys);
2375 return rc;
2376}
2377
2378
2379
2380#undef LOG_GROUP
2381#define LOG_GROUP LOG_GROUP_PGM_PHYS_ACCESS
2382
2383
2384#if defined(IN_RING3) && defined(SOME_UNUSED_FUNCTION)
2385/**
2386 * Cache PGMPhys memory access
2387 *
2388 * @param pVM The cross context VM structure.
2389 * @param pCache Cache structure pointer
2390 * @param GCPhys GC physical address
2391 * @param pbHC HC pointer corresponding to physical page
2392 *
2393 * @thread EMT.
2394 */
2395static void pgmPhysCacheAdd(PVM pVM, PGMPHYSCACHE *pCache, RTGCPHYS GCPhys, uint8_t *pbR3)
2396{
2397 uint32_t iCacheIndex;
2398
2399 Assert(VM_IS_EMT(pVM));
2400
2401 GCPhys = PHYS_PAGE_ADDRESS(GCPhys);
2402 pbR3 = (uint8_t *)PAGE_ADDRESS(pbR3);
2403
2404 iCacheIndex = ((GCPhys >> PAGE_SHIFT) & PGM_MAX_PHYSCACHE_ENTRIES_MASK);
2405
2406 ASMBitSet(&pCache->aEntries, iCacheIndex);
2407
2408 pCache->Entry[iCacheIndex].GCPhys = GCPhys;
2409 pCache->Entry[iCacheIndex].pbR3 = pbR3;
2410}
2411#endif /* IN_RING3 */
2412
2413
2414/**
2415 * Deals with reading from a page with one or more ALL access handlers.
2416 *
2417 * @returns Strict VBox status code in ring-0 and raw-mode, ignorable in ring-3.
2418 * See PGM_HANDLER_PHYS_IS_VALID_STATUS and
2419 * PGM_HANDLER_VIRT_IS_VALID_STATUS for details.
2420 *
2421 * @param pVM The cross context VM structure.
2422 * @param pPage The page descriptor.
2423 * @param GCPhys The physical address to start reading at.
2424 * @param pvBuf Where to put the bits we read.
2425 * @param cb How much to read - less or equal to a page.
2426 * @param enmOrigin The origin of this call.
2427 */
2428static VBOXSTRICTRC pgmPhysReadHandler(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void *pvBuf, size_t cb,
2429 PGMACCESSORIGIN enmOrigin)
2430{
2431 /*
2432 * The most frequent access here is MMIO and shadowed ROM.
2433 * The current code ASSUMES all these access handlers covers full pages!
2434 */
2435
2436 /*
2437 * Whatever we do we need the source page, map it first.
2438 */
2439 PGMPAGEMAPLOCK PgMpLck;
2440 const void *pvSrc = NULL;
2441 int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, GCPhys, &pvSrc, &PgMpLck);
2442/** @todo Check how this can work for MMIO pages? */
2443 if (RT_FAILURE(rc))
2444 {
2445 AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
2446 GCPhys, pPage, rc));
2447 memset(pvBuf, 0xff, cb);
2448 return VINF_SUCCESS;
2449 }
2450
2451 VBOXSTRICTRC rcStrict = VINF_PGM_HANDLER_DO_DEFAULT;
2452
2453 /*
2454 * Deal with any physical handlers.
2455 */
2456 PVMCPU pVCpu = VMMGetCpu(pVM);
2457 PPGMPHYSHANDLER pPhys = NULL;
2458 if ( PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) == PGM_PAGE_HNDL_PHYS_STATE_ALL
2459 || PGM_PAGE_IS_MMIO_OR_SPECIAL_ALIAS(pPage))
2460 {
2461 pPhys = pgmHandlerPhysicalLookup(pVM, GCPhys);
2462 AssertReleaseMsg(pPhys, ("GCPhys=%RGp cb=%#x\n", GCPhys, cb));
2463 Assert(GCPhys >= pPhys->Core.Key && GCPhys <= pPhys->Core.KeyLast);
2464 Assert((pPhys->Core.Key & PAGE_OFFSET_MASK) == 0);
2465 Assert((pPhys->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
2466#ifndef IN_RING3
2467 if (enmOrigin != PGMACCESSORIGIN_IEM)
2468 {
2469 /* Cannot reliably handle informational status codes in this context */
2470 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
2471 return VERR_PGM_PHYS_WR_HIT_HANDLER;
2472 }
2473#endif
2474 PFNPGMPHYSHANDLER pfnHandler = PGMPHYSHANDLER_GET_TYPE(pVM, pPhys)->CTX_SUFF(pfnHandler); Assert(pfnHandler);
2475 void *pvUser = pPhys->CTX_SUFF(pvUser);
2476
2477 Log5(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cb, pPage, R3STRING(pPhys->pszDesc) ));
2478 STAM_PROFILE_START(&pPhys->Stat, h);
2479 PGM_LOCK_ASSERT_OWNER(pVM);
2480
2481 /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
2482 pgmUnlock(pVM);
2483 rcStrict = pfnHandler(pVM, pVCpu, GCPhys, (void *)pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, enmOrigin, pvUser);
2484 pgmLock(pVM);
2485
2486#ifdef VBOX_WITH_STATISTICS
2487 pPhys = pgmHandlerPhysicalLookup(pVM, GCPhys);
2488 if (pPhys)
2489 STAM_PROFILE_STOP(&pPhys->Stat, h);
2490#else
2491 pPhys = NULL; /* might not be valid anymore. */
2492#endif
2493 AssertLogRelMsg(PGM_HANDLER_PHYS_IS_VALID_STATUS(rcStrict, false),
2494 ("rcStrict=%Rrc GCPhys=%RGp\n", VBOXSTRICTRC_VAL(rcStrict), GCPhys));
2495 if ( rcStrict != VINF_PGM_HANDLER_DO_DEFAULT
2496 && !PGM_PHYS_RW_IS_SUCCESS(rcStrict))
2497 {
2498 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
2499 return rcStrict;
2500 }
2501 }
2502
2503#if !defined(IN_RING0) && defined(VBOX_WITH_RAW_MODE)
2504 /*
2505 * Deal with any virtual handlers.
2506 */
2507 if (PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) == PGM_PAGE_HNDL_VIRT_STATE_ALL)
2508 {
2509 unsigned iPage;
2510 PPGMVIRTHANDLER pVirt = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &iPage);
2511 AssertReleaseMsg(pVirt, ("GCPhys=%RGp cb=%#x\n", GCPhys, cb));
2512 Assert((pVirt->Core.Key & PAGE_OFFSET_MASK) == 0);
2513 Assert((pVirt->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
2514 Assert(GCPhys >= pVirt->aPhysToVirt[iPage].Core.Key && GCPhys <= pVirt->aPhysToVirt[iPage].Core.KeyLast);
2515
2516# ifndef IN_RING3
2517 if (enmOrigin != PGMACCESSORIGIN_IEM)
2518 {
2519 /* Cannot reliably handle informational status codes in this context */
2520 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
2521 return VERR_PGM_PHYS_WR_HIT_HANDLER;
2522 }
2523# endif
2524 PPGMVIRTHANDLERTYPEINT pVirtType = PGMVIRTANDLER_GET_TYPE(pVM, pVirt);
2525 if (!pPhys)
2526 Log5(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] virt %s\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc) ));
2527 else
2528 Log(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] phys/virt %s/%s\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc), R3STRING(pPhys->pszDesc) ));
2529 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
2530 + (iPage << PAGE_SHIFT)
2531 + (GCPhys & PAGE_OFFSET_MASK);
2532
2533 STAM_PROFILE_START(&pVirt->Stat, h);
2534 VBOXSTRICTRC rcStrict2 = pVirtType->CTX_SUFF(pfnHandler)(pVM, pVCpu, GCPtr, (void *)pvSrc, pvBuf, cb,
2535 PGMACCESSTYPE_READ, enmOrigin, pVirt->CTX_SUFF(pvUser));
2536 STAM_PROFILE_STOP(&pVirt->Stat, h);
2537
2538 /* Merge status codes. */
2539 if (rcStrict2 == VINF_SUCCESS)
2540 {
2541 if (rcStrict == VINF_PGM_HANDLER_DO_DEFAULT)
2542 rcStrict = VINF_SUCCESS;
2543 }
2544 else if (rcStrict2 != VINF_PGM_HANDLER_DO_DEFAULT)
2545 {
2546 AssertLogRelMsg(PGM_HANDLER_VIRT_IS_VALID_STATUS(rcStrict2, false),
2547 ("rcStrict2=%Rrc (rcStrict=%Rrc) GCPhys=%RGp pPage=%R[pgmpage] %s\n",
2548 VBOXSTRICTRC_VAL(rcStrict2), VBOXSTRICTRC_VAL(rcStrict), GCPhys, pPage, pVirt->pszDesc));
2549 if (!PGM_PHYS_RW_IS_SUCCESS(rcStrict2))
2550 {
2551 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
2552 return rcStrict2;
2553 }
2554 if (rcStrict == VINF_PGM_HANDLER_DO_DEFAULT)
2555 rcStrict = rcStrict2;
2556 else
2557 PGM_PHYS_RW_DO_UPDATE_STRICT_RC(rcStrict, rcStrict2);
2558 }
2559 }
2560#endif /* !IN_RING0 && VBOX_WITH_RAW_MODE */
2561
2562 /*
2563 * Take the default action.
2564 */
2565 if (rcStrict == VINF_PGM_HANDLER_DO_DEFAULT)
2566 {
2567 memcpy(pvBuf, pvSrc, cb);
2568 rcStrict = VINF_SUCCESS;
2569 }
2570 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
2571 return rcStrict;
2572}
2573
2574
2575/**
2576 * Read physical memory.
2577 *
2578 * This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
2579 * want to ignore those.
2580 *
2581 * @returns Strict VBox status code in raw-mode and ring-0, normal VBox status
2582 * code in ring-3. Use PGM_PHYS_RW_IS_SUCCESS to check.
2583 * @retval VINF_SUCCESS in all context - read completed.
2584 *
2585 * @retval VINF_EM_OFF in RC and R0 - read completed.
2586 * @retval VINF_EM_SUSPEND in RC and R0 - read completed.
2587 * @retval VINF_EM_RESET in RC and R0 - read completed.
2588 * @retval VINF_EM_HALT in RC and R0 - read completed.
2589 * @retval VINF_SELM_SYNC_GDT in RC only - read completed.
2590 *
2591 * @retval VINF_EM_DBG_STOP in RC and R0 - read completed.
2592 * @retval VINF_EM_DBG_BREAKPOINT in RC and R0 - read completed.
2593 * @retval VINF_EM_RAW_EMULATE_INSTR in RC and R0 only.
2594 *
2595 * @retval VINF_IOM_R3_MMIO_READ in RC and R0.
2596 * @retval VINF_IOM_R3_MMIO_READ_WRITE in RC and R0.
2597 *
2598 * @retval VINF_PATM_CHECK_PATCH_PAGE in RC only.
2599 *
2600 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in RC and R0 for access origins that
2601 * haven't been cleared for strict status codes yet.
2602 *
2603 * @param pVM The cross context VM structure.
2604 * @param GCPhys Physical address start reading from.
2605 * @param pvBuf Where to put the read bits.
2606 * @param cbRead How many bytes to read.
2607 * @param enmOrigin The origin of this call.
2608 */
2609VMMDECL(VBOXSTRICTRC) PGMPhysRead(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead, PGMACCESSORIGIN enmOrigin)
2610{
2611 AssertMsgReturn(cbRead > 0, ("don't even think about reading zero bytes!\n"), VINF_SUCCESS);
2612 LogFlow(("PGMPhysRead: %RGp %d\n", GCPhys, cbRead));
2613
2614 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PhysRead));
2615 STAM_COUNTER_ADD(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PhysReadBytes), cbRead);
2616
2617 pgmLock(pVM);
2618
2619 /*
2620 * Copy loop on ram ranges.
2621 */
2622 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
2623 PPGMRAMRANGE pRam = pgmPhysGetRangeAtOrAbove(pVM, GCPhys);
2624 for (;;)
2625 {
2626 /* Inside range or not? */
2627 if (pRam && GCPhys >= pRam->GCPhys)
2628 {
2629 /*
2630 * Must work our way thru this page by page.
2631 */
2632 RTGCPHYS off = GCPhys - pRam->GCPhys;
2633 while (off < pRam->cb)
2634 {
2635 unsigned iPage = off >> PAGE_SHIFT;
2636 PPGMPAGE pPage = &pRam->aPages[iPage];
2637 size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
2638 if (cb > cbRead)
2639 cb = cbRead;
2640
2641 /*
2642 * Normal page? Get the pointer to it.
2643 */
2644 if ( !PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage)
2645 && !PGM_PAGE_IS_SPECIAL_ALIAS_MMIO(pPage))
2646 {
2647 /*
2648 * Get the pointer to the page.
2649 */
2650 PGMPAGEMAPLOCK PgMpLck;
2651 const void *pvSrc;
2652 int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, pRam->GCPhys + off, &pvSrc, &PgMpLck);
2653 if (RT_SUCCESS(rc))
2654 {
2655 memcpy(pvBuf, pvSrc, cb);
2656 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
2657 }
2658 else
2659 {
2660 AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
2661 pRam->GCPhys + off, pPage, rc));
2662 memset(pvBuf, 0xff, cb);
2663 }
2664 }
2665 /*
2666 * Have ALL/MMIO access handlers.
2667 */
2668 else
2669 {
2670 VBOXSTRICTRC rcStrict2 = pgmPhysReadHandler(pVM, pPage, pRam->GCPhys + off, pvBuf, cb, enmOrigin);
2671 if (PGM_PHYS_RW_IS_SUCCESS(rcStrict2))
2672 PGM_PHYS_RW_DO_UPDATE_STRICT_RC(rcStrict, rcStrict2);
2673 else
2674 {
2675 memset(pvBuf, 0xff, cb);
2676 pgmUnlock(pVM);
2677 return rcStrict2;
2678 }
2679 }
2680
2681 /* next page */
2682 if (cb >= cbRead)
2683 {
2684 pgmUnlock(pVM);
2685 return rcStrict;
2686 }
2687 cbRead -= cb;
2688 off += cb;
2689 pvBuf = (char *)pvBuf + cb;
2690 } /* walk pages in ram range. */
2691
2692 GCPhys = pRam->GCPhysLast + 1;
2693 }
2694 else
2695 {
2696 LogFlow(("PGMPhysRead: Unassigned %RGp size=%u\n", GCPhys, cbRead));
2697
2698 /*
2699 * Unassigned address space.
2700 */
2701 size_t cb = pRam ? pRam->GCPhys - GCPhys : ~(size_t)0;
2702 if (cb >= cbRead)
2703 {
2704 memset(pvBuf, 0xff, cbRead);
2705 break;
2706 }
2707 memset(pvBuf, 0xff, cb);
2708
2709 cbRead -= cb;
2710 pvBuf = (char *)pvBuf + cb;
2711 GCPhys += cb;
2712 }
2713
2714 /* Advance range if necessary. */
2715 while (pRam && GCPhys > pRam->GCPhysLast)
2716 pRam = pRam->CTX_SUFF(pNext);
2717 } /* Ram range walk */
2718
2719 pgmUnlock(pVM);
2720 return rcStrict;
2721}
2722
2723
2724/**
2725 * Deals with writing to a page with one or more WRITE or ALL access handlers.
2726 *
2727 * @returns Strict VBox status code in ring-0 and raw-mode, ignorable in ring-3.
2728 * See PGM_HANDLER_PHYS_IS_VALID_STATUS and
2729 * PGM_HANDLER_VIRT_IS_VALID_STATUS for details.
2730 *
2731 * @param pVM The cross context VM structure.
2732 * @param pPage The page descriptor.
2733 * @param GCPhys The physical address to start writing at.
2734 * @param pvBuf What to write.
2735 * @param cbWrite How much to write - less or equal to a page.
2736 * @param enmOrigin The origin of this call.
2737 */
2738static VBOXSTRICTRC pgmPhysWriteHandler(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void const *pvBuf, size_t cbWrite,
2739 PGMACCESSORIGIN enmOrigin)
2740{
2741 PGMPAGEMAPLOCK PgMpLck;
2742 void *pvDst = NULL;
2743 VBOXSTRICTRC rcStrict;
2744
2745 /*
2746 * Give priority to physical handlers (like #PF does).
2747 *
2748 * Hope for a lonely physical handler first that covers the whole
2749 * write area. This should be a pretty frequent case with MMIO and
2750 * the heavy usage of full page handlers in the page pool.
2751 */
2752 PVMCPU pVCpu = VMMGetCpu(pVM);
2753 if ( !PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage)
2754 || PGM_PAGE_IS_MMIO_OR_SPECIAL_ALIAS(pPage) /* screw virtual handlers on MMIO pages */)
2755 {
2756 PPGMPHYSHANDLER pCur = pgmHandlerPhysicalLookup(pVM, GCPhys);
2757 if (pCur)
2758 {
2759 Assert(GCPhys >= pCur->Core.Key && GCPhys <= pCur->Core.KeyLast);
2760#ifndef IN_RING3
2761 if (enmOrigin != PGMACCESSORIGIN_IEM)
2762 /* Cannot reliably handle informational status codes in this context */
2763 return VERR_PGM_PHYS_WR_HIT_HANDLER;
2764#endif
2765 size_t cbRange = pCur->Core.KeyLast - GCPhys + 1;
2766 if (cbRange > cbWrite)
2767 cbRange = cbWrite;
2768
2769 Assert(PGMPHYSHANDLER_GET_TYPE(pVM, pCur)->CTX_SUFF(pfnHandler));
2770 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n",
2771 GCPhys, cbRange, pPage, R3STRING(pCur->pszDesc) ));
2772 if (!PGM_PAGE_IS_MMIO_OR_SPECIAL_ALIAS(pPage))
2773 rcStrict = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst, &PgMpLck);
2774 else
2775 rcStrict = VINF_SUCCESS;
2776 if (RT_SUCCESS(rcStrict))
2777 {
2778 PFNPGMPHYSHANDLER pfnHandler = PGMPHYSHANDLER_GET_TYPE(pVM, pCur)->CTX_SUFF(pfnHandler);
2779 void *pvUser = pCur->CTX_SUFF(pvUser);
2780 STAM_PROFILE_START(&pCur->Stat, h);
2781
2782 /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
2783 PGM_LOCK_ASSERT_OWNER(pVM);
2784 pgmUnlock(pVM);
2785 rcStrict = pfnHandler(pVM, pVCpu, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, enmOrigin, pvUser);
2786 pgmLock(pVM);
2787
2788#ifdef VBOX_WITH_STATISTICS
2789 pCur = pgmHandlerPhysicalLookup(pVM, GCPhys);
2790 if (pCur)
2791 STAM_PROFILE_STOP(&pCur->Stat, h);
2792#else
2793 pCur = NULL; /* might not be valid anymore. */
2794#endif
2795 if (rcStrict == VINF_PGM_HANDLER_DO_DEFAULT)
2796 {
2797 if (pvDst)
2798 memcpy(pvDst, pvBuf, cbRange);
2799 rcStrict = VINF_SUCCESS;
2800 }
2801 else
2802 AssertLogRelMsg(PGM_HANDLER_PHYS_IS_VALID_STATUS(rcStrict, true),
2803 ("rcStrict=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n",
2804 VBOXSTRICTRC_VAL(rcStrict), GCPhys, pPage, pCur ? R3STRING(pCur->pszDesc) : ""));
2805 }
2806 else
2807 AssertLogRelMsgFailedReturn(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
2808 GCPhys, pPage, VBOXSTRICTRC_VAL(rcStrict)), rcStrict);
2809 if (RT_LIKELY(cbRange == cbWrite) || !PGM_PHYS_RW_IS_SUCCESS(rcStrict))
2810 {
2811 if (pvDst)
2812 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
2813 return rcStrict;
2814 }
2815
2816 /* more fun to be had below */
2817 cbWrite -= cbRange;
2818 GCPhys += cbRange;
2819 pvBuf = (uint8_t *)pvBuf + cbRange;
2820 pvDst = (uint8_t *)pvDst + cbRange;
2821 }
2822 else /* The handler is somewhere else in the page, deal with it below. */
2823 rcStrict = VINF_SUCCESS;
2824 Assert(!PGM_PAGE_IS_MMIO_OR_ALIAS(pPage)); /* MMIO handlers are all PAGE_SIZEed! */
2825 }
2826#if !defined(IN_RING0) && defined(VBOX_WITH_RAW_MODE)
2827 /*
2828 * A virtual handler without any interfering physical handlers.
2829 * Hopefully it'll cover the whole write.
2830 */
2831 else if (!PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage))
2832 {
2833 unsigned iPage;
2834 PPGMVIRTHANDLER pVirt = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &iPage);
2835 if (pVirt)
2836 {
2837# ifndef IN_RING3
2838 if (enmOrigin != PGMACCESSORIGIN_IEM)
2839 /* Cannot reliably handle informational status codes in this context */
2840 return VERR_PGM_PHYS_WR_HIT_HANDLER;
2841# endif
2842 PPGMVIRTHANDLERTYPEINT pVirtType = PGMVIRTANDLER_GET_TYPE(pVM, pVirt);
2843 size_t cbRange = (PAGE_OFFSET_MASK & pVirt->Core.KeyLast) - (PAGE_OFFSET_MASK & GCPhys) + 1;
2844 if (cbRange > cbWrite)
2845 cbRange = cbWrite;
2846
2847 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] virt %s\n",
2848 GCPhys, cbRange, pPage, R3STRING(pVirt->pszDesc) ));
2849 rcStrict = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst, &PgMpLck);
2850 if (RT_SUCCESS(rcStrict))
2851 {
2852 Assert(pVirtType->CTX_SUFF(pfnHandler));
2853 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
2854 + (iPage << PAGE_SHIFT)
2855 + (GCPhys & PAGE_OFFSET_MASK);
2856
2857 STAM_PROFILE_START(&pVirt->Stat, h);
2858 rcStrict = pVirtType->CTX_SUFF(pfnHandler)(pVM, pVCpu, GCPtr, pvDst, (void *)pvBuf, cbRange,
2859 PGMACCESSTYPE_WRITE, enmOrigin, pVirt->CTX_SUFF(pvUser));
2860 STAM_PROFILE_STOP(&pVirt->Stat, h);
2861 if (rcStrict == VINF_PGM_HANDLER_DO_DEFAULT)
2862 {
2863 memcpy(pvDst, pvBuf, cbRange);
2864 rcStrict = VINF_SUCCESS;
2865 }
2866 else
2867 AssertLogRelMsg(PGM_HANDLER_VIRT_IS_VALID_STATUS(rcStrict, true),
2868 ("rcStrict=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n",
2869 VBOXSTRICTRC_VAL(rcStrict), GCPhys, pPage, R3STRING(pVirt->pszDesc)));
2870 }
2871 else
2872 AssertLogRelMsgFailedReturn(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
2873 GCPhys, pPage, VBOXSTRICTRC_VAL(rcStrict)), rcStrict);
2874 if (RT_LIKELY(cbRange == cbWrite) || !PGM_PHYS_RW_IS_SUCCESS(rcStrict))
2875 {
2876 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
2877 return rcStrict;
2878 }
2879
2880 /* more fun to be had below */
2881 cbWrite -= cbRange;
2882 GCPhys += cbRange;
2883 pvBuf = (uint8_t *)pvBuf + cbRange;
2884 pvDst = (uint8_t *)pvDst + cbRange;
2885 }
2886 else /* The handler is somewhere else in the page, deal with it below. */
2887 rcStrict = VINF_SUCCESS;
2888 }
2889#endif /* !IN_RING0 && VBOX_WITH_RAW_MODE */
2890 else
2891 rcStrict = VINF_SUCCESS;
2892
2893
2894 /*
2895 * Deal with all the odd ends.
2896 */
2897 Assert(rcStrict != VINF_PGM_HANDLER_DO_DEFAULT);
2898
2899 /* We need a writable destination page. */
2900 if (!pvDst)
2901 {
2902 int rc2 = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst, &PgMpLck);
2903 AssertLogRelMsgReturn(RT_SUCCESS(rc2),
2904 ("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n", GCPhys, pPage, rc2),
2905 rc2);
2906 }
2907
2908 /* The loop state (big + ugly). */
2909#if !defined(IN_RING0) && defined(VBOX_WITH_RAW_MODE)
2910 unsigned iVirtPage = 0;
2911 PPGMVIRTHANDLER pVirt = NULL;
2912 uint32_t offVirt = PAGE_SIZE;
2913 uint32_t offVirtLast = PAGE_SIZE;
2914 bool fMoreVirt = PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage);
2915#else
2916 uint32_t const offVirt = UINT32_MAX;
2917#endif
2918
2919 PPGMPHYSHANDLER pPhys = NULL;
2920 uint32_t offPhys = PAGE_SIZE;
2921 uint32_t offPhysLast = PAGE_SIZE;
2922 bool fMorePhys = PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage);
2923
2924 /* The loop. */
2925 for (;;)
2926 {
2927#if !defined(IN_RING0) && defined(VBOX_WITH_RAW_MODE)
2928 /*
2929 * Find the closest handler at or above GCPhys.
2930 */
2931 if (fMoreVirt && !pVirt)
2932 {
2933 pVirt = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &iVirtPage);
2934 if (pVirt)
2935 {
2936 offVirt = 0;
2937 offVirtLast = (pVirt->aPhysToVirt[iVirtPage].Core.KeyLast & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
2938 }
2939 else
2940 {
2941 PPGMPHYS2VIRTHANDLER pVirtPhys;
2942 pVirtPhys = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers,
2943 GCPhys, true /* fAbove */);
2944 if ( pVirtPhys
2945 && (pVirtPhys->Core.Key >> PAGE_SHIFT) == (GCPhys >> PAGE_SHIFT))
2946 {
2947 /* ASSUME that pVirtPhys only covers one page. */
2948 Assert((pVirtPhys->Core.Key >> PAGE_SHIFT) == (pVirtPhys->Core.KeyLast >> PAGE_SHIFT));
2949 Assert(pVirtPhys->Core.Key > GCPhys);
2950
2951 pVirt = (PPGMVIRTHANDLER)((uintptr_t)pVirtPhys + pVirtPhys->offVirtHandler);
2952 iVirtPage = pVirtPhys - &pVirt->aPhysToVirt[0]; Assert(iVirtPage == 0);
2953 offVirt = (pVirtPhys->Core.Key & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
2954 offVirtLast = (pVirtPhys->Core.KeyLast & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
2955 }
2956 else
2957 {
2958 pVirt = NULL;
2959 fMoreVirt = false;
2960 offVirt = offVirtLast = PAGE_SIZE;
2961 }
2962 }
2963 }
2964#endif
2965
2966 if (fMorePhys && !pPhys)
2967 {
2968 pPhys = pgmHandlerPhysicalLookup(pVM, GCPhys);
2969 if (pPhys)
2970 {
2971 offPhys = 0;
2972 offPhysLast = pPhys->Core.KeyLast - GCPhys; /* ASSUMES < 4GB handlers... */
2973 }
2974 else
2975 {
2976 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers,
2977 GCPhys, true /* fAbove */);
2978 if ( pPhys
2979 && pPhys->Core.Key <= GCPhys + (cbWrite - 1))
2980 {
2981 offPhys = pPhys->Core.Key - GCPhys;
2982 offPhysLast = pPhys->Core.KeyLast - GCPhys; /* ASSUMES < 4GB handlers... */
2983 }
2984 else
2985 {
2986 pPhys = NULL;
2987 fMorePhys = false;
2988 offPhys = offPhysLast = PAGE_SIZE;
2989 }
2990 }
2991 }
2992
2993 /*
2994 * Handle access to space without handlers (that's easy).
2995 */
2996 VBOXSTRICTRC rcStrict2 = VINF_PGM_HANDLER_DO_DEFAULT;
2997 uint32_t cbRange = (uint32_t)cbWrite;
2998 if (offPhys != 0 && offVirt != 0)
2999 {
3000 if (cbRange > offPhys)
3001 cbRange = offPhys;
3002 if (cbRange > offVirt)
3003 cbRange = offVirt;
3004 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] miss\n", GCPhys, cbRange, pPage));
3005 }
3006 /*
3007 * Physical handler.
3008 */
3009 else if (!offPhys && offVirt)
3010 {
3011#ifndef IN_RING3
3012 if (enmOrigin != PGMACCESSORIGIN_IEM)
3013 /* Cannot reliably handle informational status codes in this context */
3014 return VERR_PGM_PHYS_WR_HIT_HANDLER;
3015#endif
3016 if (cbRange > offPhysLast + 1)
3017 cbRange = offPhysLast + 1;
3018 if (cbRange > offVirt)
3019 cbRange = offVirt;
3020
3021 PFNPGMPHYSHANDLER pfnHandler = PGMPHYSHANDLER_GET_TYPE(pVM, pPhys)->CTX_SUFF(pfnHandler);
3022 void *pvUser = pPhys->CTX_SUFF(pvUser);
3023
3024 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pPhys->pszDesc) ));
3025 STAM_PROFILE_START(&pPhys->Stat, h);
3026
3027 /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
3028 PGM_LOCK_ASSERT_OWNER(pVM);
3029 pgmUnlock(pVM);
3030 rcStrict2 = pfnHandler(pVM, pVCpu, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, enmOrigin, pvUser);
3031 pgmLock(pVM);
3032
3033#ifdef VBOX_WITH_STATISTICS
3034 pPhys = pgmHandlerPhysicalLookup(pVM, GCPhys);
3035 if (pPhys)
3036 STAM_PROFILE_STOP(&pPhys->Stat, h);
3037#else
3038 pPhys = NULL; /* might not be valid anymore. */
3039#endif
3040 AssertLogRelMsg(PGM_HANDLER_PHYS_IS_VALID_STATUS(rcStrict2, true),
3041 ("rcStrict2=%Rrc (rcStrict=%Rrc) GCPhys=%RGp pPage=%R[pgmpage] %s\n", VBOXSTRICTRC_VAL(rcStrict2),
3042 VBOXSTRICTRC_VAL(rcStrict), GCPhys, pPage, pPhys ? R3STRING(pPhys->pszDesc) : ""));
3043 }
3044#if !defined(IN_RING0) && defined(VBOX_WITH_RAW_MODE)
3045 /*
3046 * Virtual handler.
3047 */
3048 else if (offPhys && !offVirt)
3049 {
3050# ifndef IN_RING3
3051 if (enmOrigin != PGMACCESSORIGIN_IEM)
3052 /* Cannot reliably handle informational status codes in this context */
3053 return VERR_PGM_PHYS_WR_HIT_HANDLER;
3054# endif
3055 if (cbRange > offVirtLast + 1)
3056 cbRange = offVirtLast + 1;
3057 if (cbRange > offPhys)
3058 cbRange = offPhys;
3059
3060 PPGMVIRTHANDLERTYPEINT pVirtType = PGMVIRTANDLER_GET_TYPE(pVM, pVirt);
3061 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pVirt->pszDesc) ));
3062 Assert(pVirtType->CTX_SUFF(pfnHandler));
3063 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
3064 + (iVirtPage << PAGE_SHIFT)
3065 + (GCPhys & PAGE_OFFSET_MASK);
3066 STAM_PROFILE_START(&pVirt->Stat, h);
3067 rcStrict2 = pVirtType->CTX_SUFF(pfnHandler)(pVM, pVCpu, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE,
3068 enmOrigin, pVirt->CTX_SUFF(pvUser));
3069 STAM_PROFILE_STOP(&pVirt->Stat, h);
3070 AssertLogRelMsg(PGM_HANDLER_VIRT_IS_VALID_STATUS(rcStrict2, true),
3071 ("rcStrict2=%Rrc (rcStrict=%Rrc) GCPhys=%RGp pPage=%R[pgmpage] %s\n", VBOXSTRICTRC_VAL(rcStrict2),
3072 VBOXSTRICTRC_VAL(rcStrict), GCPhys, pPage, pPhys ? R3STRING(pPhys->pszDesc) : ""));
3073 pVirt = NULL;
3074 }
3075 /*
3076 * Both... give the physical one priority.
3077 */
3078 else
3079 {
3080# ifndef IN_RING3
3081 if (enmOrigin != PGMACCESSORIGIN_IEM)
3082 /* Cannot reliably handle informational status codes in this context */
3083 return VERR_PGM_PHYS_WR_HIT_HANDLER;
3084# endif
3085 Assert(!offPhys && !offVirt);
3086 if (cbRange > offVirtLast + 1)
3087 cbRange = offVirtLast + 1;
3088 if (cbRange > offPhysLast + 1)
3089 cbRange = offPhysLast + 1;
3090
3091 PPGMVIRTHANDLERTYPEINT pVirtType = PGMVIRTANDLER_GET_TYPE(pVM, pVirt);
3092 if (pVirtType->pfnHandlerR3)
3093 Log(("pgmPhysWriteHandler: overlapping phys and virt handlers at %RGp %R[pgmpage]; cbRange=%#x\n", GCPhys, pPage, cbRange));
3094 Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys/virt %s/%s\n", GCPhys, cbRange, pPage, R3STRING(pPhys->pszDesc), R3STRING(pVirt->pszDesc) ));
3095
3096 PFNPGMPHYSHANDLER pfnHandler = PGMPHYSHANDLER_GET_TYPE(pVM, pPhys)->CTX_SUFF(pfnHandler);
3097 void *pvUser = pPhys->CTX_SUFF(pvUser);
3098 STAM_PROFILE_START(&pPhys->Stat, h);
3099
3100 /* Release the PGM lock as MMIO handlers take the IOM lock. (deadlock prevention) */
3101 PGM_LOCK_ASSERT_OWNER(pVM);
3102 pgmUnlock(pVM);
3103 rcStrict2 = pfnHandler(pVM, pVCpu, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, enmOrigin, pvUser);
3104 pgmLock(pVM);
3105
3106# ifdef VBOX_WITH_STATISTICS
3107 pPhys = pgmHandlerPhysicalLookup(pVM, GCPhys);
3108 if (pPhys)
3109 STAM_PROFILE_STOP(&pPhys->Stat, h);
3110# else
3111 pPhys = NULL; /* might not be valid anymore. */
3112# endif
3113 AssertLogRelMsg(PGM_HANDLER_PHYS_IS_VALID_STATUS(rcStrict2, true),
3114 ("rcStrict2=%Rrc (rcStrict=%Rrc) GCPhys=%RGp pPage=%R[pgmpage] %s\n", VBOXSTRICTRC_VAL(rcStrict2),
3115 VBOXSTRICTRC_VAL(rcStrict), GCPhys, pPage, pPhys ? R3STRING(pPhys->pszDesc) : ""));
3116 if (rcStrict2 == VINF_PGM_HANDLER_DO_DEFAULT || PGM_PHYS_RW_IS_SUCCESS(rcStrict2))
3117 {
3118 Assert(pVirtType->CTX_SUFF(pfnHandler));
3119 RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
3120 + (iVirtPage << PAGE_SHIFT)
3121 + (GCPhys & PAGE_OFFSET_MASK);
3122 pvUser = pVirt->CTX_SUFF(pvUser);
3123
3124 STAM_PROFILE_START(&pVirt->Stat, h2);
3125 VBOXSTRICTRC rcStrict3 = pVirtType->CTX_SUFF(pfnHandler)(pVM, pVCpu, GCPtr, pvDst, (void *)pvBuf, cbRange,
3126 PGMACCESSTYPE_WRITE, enmOrigin, pvUser);
3127 STAM_PROFILE_STOP(&pVirt->Stat, h2);
3128
3129 /* Merge the 3rd status into the 2nd. */
3130 if (rcStrict3 == VINF_SUCCESS)
3131 {
3132 if (rcStrict2 == VINF_PGM_HANDLER_DO_DEFAULT)
3133 rcStrict2 = VINF_SUCCESS;
3134 }
3135 else if (rcStrict3 != VINF_PGM_HANDLER_DO_DEFAULT)
3136 {
3137 AssertLogRelMsg(PGM_HANDLER_VIRT_IS_VALID_STATUS(rcStrict3, true),
3138 ("rcStrict3=%Rrc (rcStrict2=%Rrc) (rcStrict=%Rrc) GCPhys=%RGp pPage=%R[pgmpage] %s\n",
3139 VBOXSTRICTRC_VAL(rcStrict3), VBOXSTRICTRC_VAL(rcStrict2), VBOXSTRICTRC_VAL(rcStrict),
3140 GCPhys, pPage, R3STRING(pVirt->pszDesc) ));
3141 if (rcStrict2 == VINF_PGM_HANDLER_DO_DEFAULT)
3142 rcStrict2 = rcStrict3;
3143 else if (!PGM_PHYS_RW_IS_SUCCESS(rcStrict3))
3144 rcStrict2 = rcStrict3;
3145 else
3146 PGM_PHYS_RW_DO_UPDATE_STRICT_RC(rcStrict2, rcStrict3);
3147 }
3148 }
3149 pPhys = NULL;
3150 pVirt = NULL;
3151 }
3152#endif /* !IN_RING0 && VBOX_WITH_RAW_MODE */
3153
3154
3155 /*
3156 * Execute the default action and merge the status codes.
3157 */
3158 if (rcStrict2 == VINF_PGM_HANDLER_DO_DEFAULT)
3159 {
3160 memcpy(pvDst, pvBuf, cbRange);
3161 rcStrict2 = VINF_SUCCESS;
3162 }
3163 else if (!PGM_PHYS_RW_IS_SUCCESS(rcStrict2))
3164 {
3165 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
3166 return rcStrict2;
3167 }
3168 else
3169 PGM_PHYS_RW_DO_UPDATE_STRICT_RC(rcStrict, rcStrict2);
3170
3171 /*
3172 * Advance if we've got more stuff to do.
3173 */
3174 if (cbRange >= cbWrite)
3175 {
3176 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
3177 return rcStrict;
3178 }
3179
3180
3181 cbWrite -= cbRange;
3182 GCPhys += cbRange;
3183 pvBuf = (uint8_t *)pvBuf + cbRange;
3184 pvDst = (uint8_t *)pvDst + cbRange;
3185
3186 offPhys -= cbRange;
3187 offPhysLast -= cbRange;
3188#if !defined(IN_RING0) && defined(VBOX_WITH_RAW_MODE)
3189 offVirt -= cbRange;
3190 offVirtLast -= cbRange;
3191#endif
3192 }
3193}
3194
3195
3196/**
3197 * Write to physical memory.
3198 *
3199 * This API respects access handlers and MMIO. Use PGMPhysSimpleWriteGCPhys() if you
3200 * want to ignore those.
3201 *
3202 * @returns Strict VBox status code in raw-mode and ring-0, normal VBox status
3203 * code in ring-3. Use PGM_PHYS_RW_IS_SUCCESS to check.
3204 * @retval VINF_SUCCESS in all context - write completed.
3205 *
3206 * @retval VINF_EM_OFF in RC and R0 - write completed.
3207 * @retval VINF_EM_SUSPEND in RC and R0 - write completed.
3208 * @retval VINF_EM_RESET in RC and R0 - write completed.
3209 * @retval VINF_EM_HALT in RC and R0 - write completed.
3210 * @retval VINF_SELM_SYNC_GDT in RC only - write completed.
3211 *
3212 * @retval VINF_EM_DBG_STOP in RC and R0 - write completed.
3213 * @retval VINF_EM_DBG_BREAKPOINT in RC and R0 - write completed.
3214 * @retval VINF_EM_RAW_EMULATE_INSTR in RC and R0 only.
3215 *
3216 * @retval VINF_IOM_R3_MMIO_WRITE in RC and R0.
3217 * @retval VINF_IOM_R3_MMIO_READ_WRITE in RC and R0.
3218 * @retval VINF_IOM_R3_MMIO_COMMIT_WRITE in RC and R0.
3219 *
3220 * @retval VINF_EM_RAW_EMULATE_INSTR_GDT_FAULT in RC only - write completed.
3221 * @retval VINF_EM_RAW_EMULATE_INSTR_LDT_FAULT in RC only.
3222 * @retval VINF_EM_RAW_EMULATE_INSTR_TSS_FAULT in RC only.
3223 * @retval VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT in RC only.
3224 * @retval VINF_CSAM_PENDING_ACTION in RC only.
3225 * @retval VINF_PATM_CHECK_PATCH_PAGE in RC only.
3226 *
3227 * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in RC and R0 for access origins that
3228 * haven't been cleared for strict status codes yet.
3229 *
3230 *
3231 * @param pVM The cross context VM structure.
3232 * @param GCPhys Physical address to write to.
3233 * @param pvBuf What to write.
3234 * @param cbWrite How many bytes to write.
3235 * @param enmOrigin Who is calling.
3236 */
3237VMMDECL(VBOXSTRICTRC) PGMPhysWrite(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite, PGMACCESSORIGIN enmOrigin)
3238{
3239 AssertMsg(!pVM->pgm.s.fNoMorePhysWrites, ("Calling PGMPhysWrite after pgmR3Save()! enmOrigin=%d\n", enmOrigin));
3240 AssertMsgReturn(cbWrite > 0, ("don't even think about writing zero bytes!\n"), VINF_SUCCESS);
3241 LogFlow(("PGMPhysWrite: %RGp %d\n", GCPhys, cbWrite));
3242
3243 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PhysWrite));
3244 STAM_COUNTER_ADD(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PhysWriteBytes), cbWrite);
3245
3246 pgmLock(pVM);
3247
3248 /*
3249 * Copy loop on ram ranges.
3250 */
3251 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
3252 PPGMRAMRANGE pRam = pgmPhysGetRangeAtOrAbove(pVM, GCPhys);
3253 for (;;)
3254 {
3255 /* Inside range or not? */
3256 if (pRam && GCPhys >= pRam->GCPhys)
3257 {
3258 /*
3259 * Must work our way thru this page by page.
3260 */
3261 RTGCPTR off = GCPhys - pRam->GCPhys;
3262 while (off < pRam->cb)
3263 {
3264 RTGCPTR iPage = off >> PAGE_SHIFT;
3265 PPGMPAGE pPage = &pRam->aPages[iPage];
3266 size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
3267 if (cb > cbWrite)
3268 cb = cbWrite;
3269
3270 /*
3271 * Normal page? Get the pointer to it.
3272 */
3273 if ( !PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage)
3274 && !PGM_PAGE_IS_SPECIAL_ALIAS_MMIO(pPage))
3275 {
3276 PGMPAGEMAPLOCK PgMpLck;
3277 void *pvDst;
3278 int rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, pRam->GCPhys + off, &pvDst, &PgMpLck);
3279 if (RT_SUCCESS(rc))
3280 {
3281 Assert(!PGM_PAGE_IS_BALLOONED(pPage));
3282 memcpy(pvDst, pvBuf, cb);
3283 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
3284 }
3285 /* Ignore writes to ballooned pages. */
3286 else if (!PGM_PAGE_IS_BALLOONED(pPage))
3287 AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
3288 pRam->GCPhys + off, pPage, rc));
3289 }
3290 /*
3291 * Active WRITE or ALL access handlers.
3292 */
3293 else
3294 {
3295 VBOXSTRICTRC rcStrict2 = pgmPhysWriteHandler(pVM, pPage, pRam->GCPhys + off, pvBuf, cb, enmOrigin);
3296 if (PGM_PHYS_RW_IS_SUCCESS(rcStrict2))
3297 PGM_PHYS_RW_DO_UPDATE_STRICT_RC(rcStrict, rcStrict2);
3298 else
3299 {
3300 pgmUnlock(pVM);
3301 return rcStrict2;
3302 }
3303 }
3304
3305 /* next page */
3306 if (cb >= cbWrite)
3307 {
3308 pgmUnlock(pVM);
3309 return rcStrict;
3310 }
3311
3312 cbWrite -= cb;
3313 off += cb;
3314 pvBuf = (const char *)pvBuf + cb;
3315 } /* walk pages in ram range */
3316
3317 GCPhys = pRam->GCPhysLast + 1;
3318 }
3319 else
3320 {
3321 /*
3322 * Unassigned address space, skip it.
3323 */
3324 if (!pRam)
3325 break;
3326 size_t cb = pRam->GCPhys - GCPhys;
3327 if (cb >= cbWrite)
3328 break;
3329 cbWrite -= cb;
3330 pvBuf = (const char *)pvBuf + cb;
3331 GCPhys += cb;
3332 }
3333
3334 /* Advance range if necessary. */
3335 while (pRam && GCPhys > pRam->GCPhysLast)
3336 pRam = pRam->CTX_SUFF(pNext);
3337 } /* Ram range walk */
3338
3339 pgmUnlock(pVM);
3340 return rcStrict;
3341}
3342
3343
3344/**
3345 * Read from guest physical memory by GC physical address, bypassing
3346 * MMIO and access handlers.
3347 *
3348 * @returns VBox status code.
3349 * @param pVM The cross context VM structure.
3350 * @param pvDst The destination address.
3351 * @param GCPhysSrc The source address (GC physical address).
3352 * @param cb The number of bytes to read.
3353 */
3354VMMDECL(int) PGMPhysSimpleReadGCPhys(PVM pVM, void *pvDst, RTGCPHYS GCPhysSrc, size_t cb)
3355{
3356 /*
3357 * Treat the first page as a special case.
3358 */
3359 if (!cb)
3360 return VINF_SUCCESS;
3361
3362 /* map the 1st page */
3363 void const *pvSrc;
3364 PGMPAGEMAPLOCK Lock;
3365 int rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhysSrc, &pvSrc, &Lock);
3366 if (RT_FAILURE(rc))
3367 return rc;
3368
3369 /* optimize for the case where access is completely within the first page. */
3370 size_t cbPage = PAGE_SIZE - (GCPhysSrc & PAGE_OFFSET_MASK);
3371 if (RT_LIKELY(cb <= cbPage))
3372 {
3373 memcpy(pvDst, pvSrc, cb);
3374 PGMPhysReleasePageMappingLock(pVM, &Lock);
3375 return VINF_SUCCESS;
3376 }
3377
3378 /* copy to the end of the page. */
3379 memcpy(pvDst, pvSrc, cbPage);
3380 PGMPhysReleasePageMappingLock(pVM, &Lock);
3381 GCPhysSrc += cbPage;
3382 pvDst = (uint8_t *)pvDst + cbPage;
3383 cb -= cbPage;
3384
3385 /*
3386 * Page by page.
3387 */
3388 for (;;)
3389 {
3390 /* map the page */
3391 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhysSrc, &pvSrc, &Lock);
3392 if (RT_FAILURE(rc))
3393 return rc;
3394
3395 /* last page? */
3396 if (cb <= PAGE_SIZE)
3397 {
3398 memcpy(pvDst, pvSrc, cb);
3399 PGMPhysReleasePageMappingLock(pVM, &Lock);
3400 return VINF_SUCCESS;
3401 }
3402
3403 /* copy the entire page and advance */
3404 memcpy(pvDst, pvSrc, PAGE_SIZE);
3405 PGMPhysReleasePageMappingLock(pVM, &Lock);
3406 GCPhysSrc += PAGE_SIZE;
3407 pvDst = (uint8_t *)pvDst + PAGE_SIZE;
3408 cb -= PAGE_SIZE;
3409 }
3410 /* won't ever get here. */
3411}
3412
3413
3414/**
3415 * Write to guest physical memory referenced by GC pointer.
3416 * Write memory to GC physical address in guest physical memory.
3417 *
3418 * This will bypass MMIO and access handlers.
3419 *
3420 * @returns VBox status code.
3421 * @param pVM The cross context VM structure.
3422 * @param GCPhysDst The GC physical address of the destination.
3423 * @param pvSrc The source buffer.
3424 * @param cb The number of bytes to write.
3425 */
3426VMMDECL(int) PGMPhysSimpleWriteGCPhys(PVM pVM, RTGCPHYS GCPhysDst, const void *pvSrc, size_t cb)
3427{
3428 LogFlow(("PGMPhysSimpleWriteGCPhys: %RGp %zu\n", GCPhysDst, cb));
3429
3430 /*
3431 * Treat the first page as a special case.
3432 */
3433 if (!cb)
3434 return VINF_SUCCESS;
3435
3436 /* map the 1st page */
3437 void *pvDst;
3438 PGMPAGEMAPLOCK Lock;
3439 int rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
3440 if (RT_FAILURE(rc))
3441 return rc;
3442
3443 /* optimize for the case where access is completely within the first page. */
3444 size_t cbPage = PAGE_SIZE - (GCPhysDst & PAGE_OFFSET_MASK);
3445 if (RT_LIKELY(cb <= cbPage))
3446 {
3447 memcpy(pvDst, pvSrc, cb);
3448 PGMPhysReleasePageMappingLock(pVM, &Lock);
3449 return VINF_SUCCESS;
3450 }
3451
3452 /* copy to the end of the page. */
3453 memcpy(pvDst, pvSrc, cbPage);
3454 PGMPhysReleasePageMappingLock(pVM, &Lock);
3455 GCPhysDst += cbPage;
3456 pvSrc = (const uint8_t *)pvSrc + cbPage;
3457 cb -= cbPage;
3458
3459 /*
3460 * Page by page.
3461 */
3462 for (;;)
3463 {
3464 /* map the page */
3465 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
3466 if (RT_FAILURE(rc))
3467 return rc;
3468
3469 /* last page? */
3470 if (cb <= PAGE_SIZE)
3471 {
3472 memcpy(pvDst, pvSrc, cb);
3473 PGMPhysReleasePageMappingLock(pVM, &Lock);
3474 return VINF_SUCCESS;
3475 }
3476
3477 /* copy the entire page and advance */
3478 memcpy(pvDst, pvSrc, PAGE_SIZE);
3479 PGMPhysReleasePageMappingLock(pVM, &Lock);
3480 GCPhysDst += PAGE_SIZE;
3481 pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
3482 cb -= PAGE_SIZE;
3483 }
3484 /* won't ever get here. */
3485}
3486
3487
3488/**
3489 * Read from guest physical memory referenced by GC pointer.
3490 *
3491 * This function uses the current CR3/CR0/CR4 of the guest and will
3492 * bypass access handlers and not set any accessed bits.
3493 *
3494 * @returns VBox status code.
3495 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
3496 * @param pvDst The destination address.
3497 * @param GCPtrSrc The source address (GC pointer).
3498 * @param cb The number of bytes to read.
3499 */
3500VMMDECL(int) PGMPhysSimpleReadGCPtr(PVMCPU pVCpu, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
3501{
3502 PVM pVM = pVCpu->CTX_SUFF(pVM);
3503/** @todo fix the macro / state handling: VMCPU_ASSERT_EMT_OR_GURU(pVCpu); */
3504
3505 /*
3506 * Treat the first page as a special case.
3507 */
3508 if (!cb)
3509 return VINF_SUCCESS;
3510
3511 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PhysSimpleRead));
3512 STAM_COUNTER_ADD(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PhysSimpleReadBytes), cb);
3513
3514 /* Take the PGM lock here, because many called functions take the lock for a very short period. That's counter-productive
3515 * when many VCPUs are fighting for the lock.
3516 */
3517 pgmLock(pVM);
3518
3519 /* map the 1st page */
3520 void const *pvSrc;
3521 PGMPAGEMAPLOCK Lock;
3522 int rc = PGMPhysGCPtr2CCPtrReadOnly(pVCpu, GCPtrSrc, &pvSrc, &Lock);
3523 if (RT_FAILURE(rc))
3524 {
3525 pgmUnlock(pVM);
3526 return rc;
3527 }
3528
3529 /* optimize for the case where access is completely within the first page. */
3530 size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
3531 if (RT_LIKELY(cb <= cbPage))
3532 {
3533 memcpy(pvDst, pvSrc, cb);
3534 PGMPhysReleasePageMappingLock(pVM, &Lock);
3535 pgmUnlock(pVM);
3536 return VINF_SUCCESS;
3537 }
3538
3539 /* copy to the end of the page. */
3540 memcpy(pvDst, pvSrc, cbPage);
3541 PGMPhysReleasePageMappingLock(pVM, &Lock);
3542 GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + cbPage);
3543 pvDst = (uint8_t *)pvDst + cbPage;
3544 cb -= cbPage;
3545
3546 /*
3547 * Page by page.
3548 */
3549 for (;;)
3550 {
3551 /* map the page */
3552 rc = PGMPhysGCPtr2CCPtrReadOnly(pVCpu, GCPtrSrc, &pvSrc, &Lock);
3553 if (RT_FAILURE(rc))
3554 {
3555 pgmUnlock(pVM);
3556 return rc;
3557 }
3558
3559 /* last page? */
3560 if (cb <= PAGE_SIZE)
3561 {
3562 memcpy(pvDst, pvSrc, cb);
3563 PGMPhysReleasePageMappingLock(pVM, &Lock);
3564 pgmUnlock(pVM);
3565 return VINF_SUCCESS;
3566 }
3567
3568 /* copy the entire page and advance */
3569 memcpy(pvDst, pvSrc, PAGE_SIZE);
3570 PGMPhysReleasePageMappingLock(pVM, &Lock);
3571 GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + PAGE_SIZE);
3572 pvDst = (uint8_t *)pvDst + PAGE_SIZE;
3573 cb -= PAGE_SIZE;
3574 }
3575 /* won't ever get here. */
3576}
3577
3578
3579/**
3580 * Write to guest physical memory referenced by GC pointer.
3581 *
3582 * This function uses the current CR3/CR0/CR4 of the guest and will
3583 * bypass access handlers and not set dirty or accessed bits.
3584 *
3585 * @returns VBox status code.
3586 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
3587 * @param GCPtrDst The destination address (GC pointer).
3588 * @param pvSrc The source address.
3589 * @param cb The number of bytes to write.
3590 */
3591VMMDECL(int) PGMPhysSimpleWriteGCPtr(PVMCPU pVCpu, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
3592{
3593 PVM pVM = pVCpu->CTX_SUFF(pVM);
3594 VMCPU_ASSERT_EMT(pVCpu);
3595
3596 /*
3597 * Treat the first page as a special case.
3598 */
3599 if (!cb)
3600 return VINF_SUCCESS;
3601
3602 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PhysSimpleWrite));
3603 STAM_COUNTER_ADD(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PhysSimpleWriteBytes), cb);
3604
3605 /* map the 1st page */
3606 void *pvDst;
3607 PGMPAGEMAPLOCK Lock;
3608 int rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
3609 if (RT_FAILURE(rc))
3610 return rc;
3611
3612 /* optimize for the case where access is completely within the first page. */
3613 size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
3614 if (RT_LIKELY(cb <= cbPage))
3615 {
3616 memcpy(pvDst, pvSrc, cb);
3617 PGMPhysReleasePageMappingLock(pVM, &Lock);
3618 return VINF_SUCCESS;
3619 }
3620
3621 /* copy to the end of the page. */
3622 memcpy(pvDst, pvSrc, cbPage);
3623 PGMPhysReleasePageMappingLock(pVM, &Lock);
3624 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
3625 pvSrc = (const uint8_t *)pvSrc + cbPage;
3626 cb -= cbPage;
3627
3628 /*
3629 * Page by page.
3630 */
3631 for (;;)
3632 {
3633 /* map the page */
3634 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
3635 if (RT_FAILURE(rc))
3636 return rc;
3637
3638 /* last page? */
3639 if (cb <= PAGE_SIZE)
3640 {
3641 memcpy(pvDst, pvSrc, cb);
3642 PGMPhysReleasePageMappingLock(pVM, &Lock);
3643 return VINF_SUCCESS;
3644 }
3645
3646 /* copy the entire page and advance */
3647 memcpy(pvDst, pvSrc, PAGE_SIZE);
3648 PGMPhysReleasePageMappingLock(pVM, &Lock);
3649 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + PAGE_SIZE);
3650 pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
3651 cb -= PAGE_SIZE;
3652 }
3653 /* won't ever get here. */
3654}
3655
3656
3657/**
3658 * Write to guest physical memory referenced by GC pointer and update the PTE.
3659 *
3660 * This function uses the current CR3/CR0/CR4 of the guest and will
3661 * bypass access handlers but will set any dirty and accessed bits in the PTE.
3662 *
3663 * If you don't want to set the dirty bit, use PGMPhysSimpleWriteGCPtr().
3664 *
3665 * @returns VBox status code.
3666 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
3667 * @param GCPtrDst The destination address (GC pointer).
3668 * @param pvSrc The source address.
3669 * @param cb The number of bytes to write.
3670 */
3671VMMDECL(int) PGMPhysSimpleDirtyWriteGCPtr(PVMCPU pVCpu, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
3672{
3673 PVM pVM = pVCpu->CTX_SUFF(pVM);
3674 VMCPU_ASSERT_EMT(pVCpu);
3675
3676 /*
3677 * Treat the first page as a special case.
3678 * Btw. this is the same code as in PGMPhyssimpleWriteGCPtr excep for the PGMGstModifyPage.
3679 */
3680 if (!cb)
3681 return VINF_SUCCESS;
3682
3683 /* map the 1st page */
3684 void *pvDst;
3685 PGMPAGEMAPLOCK Lock;
3686 int rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
3687 if (RT_FAILURE(rc))
3688 return rc;
3689
3690 /* optimize for the case where access is completely within the first page. */
3691 size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
3692 if (RT_LIKELY(cb <= cbPage))
3693 {
3694 memcpy(pvDst, pvSrc, cb);
3695 PGMPhysReleasePageMappingLock(pVM, &Lock);
3696 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
3697 return VINF_SUCCESS;
3698 }
3699
3700 /* copy to the end of the page. */
3701 memcpy(pvDst, pvSrc, cbPage);
3702 PGMPhysReleasePageMappingLock(pVM, &Lock);
3703 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
3704 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
3705 pvSrc = (const uint8_t *)pvSrc + cbPage;
3706 cb -= cbPage;
3707
3708 /*
3709 * Page by page.
3710 */
3711 for (;;)
3712 {
3713 /* map the page */
3714 rc = PGMPhysGCPtr2CCPtr(pVCpu, GCPtrDst, &pvDst, &Lock);
3715 if (RT_FAILURE(rc))
3716 return rc;
3717
3718 /* last page? */
3719 if (cb <= PAGE_SIZE)
3720 {
3721 memcpy(pvDst, pvSrc, cb);
3722 PGMPhysReleasePageMappingLock(pVM, &Lock);
3723 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
3724 return VINF_SUCCESS;
3725 }
3726
3727 /* copy the entire page and advance */
3728 memcpy(pvDst, pvSrc, PAGE_SIZE);
3729 PGMPhysReleasePageMappingLock(pVM, &Lock);
3730 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
3731 GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + PAGE_SIZE);
3732 pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
3733 cb -= PAGE_SIZE;
3734 }
3735 /* won't ever get here. */
3736}
3737
3738
3739/**
3740 * Read from guest physical memory referenced by GC pointer.
3741 *
3742 * This function uses the current CR3/CR0/CR4 of the guest and will
3743 * respect access handlers and set accessed bits.
3744 *
3745 * @returns Strict VBox status, see PGMPhysRead for details.
3746 * @retval VERR_PAGE_TABLE_NOT_PRESENT if there is no page mapped at the
3747 * specified virtual address.
3748 *
3749 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
3750 * @param pvDst The destination address.
3751 * @param GCPtrSrc The source address (GC pointer).
3752 * @param cb The number of bytes to read.
3753 * @param enmOrigin Who is calling.
3754 * @thread EMT(pVCpu)
3755 */
3756VMMDECL(VBOXSTRICTRC) PGMPhysReadGCPtr(PVMCPU pVCpu, void *pvDst, RTGCPTR GCPtrSrc, size_t cb, PGMACCESSORIGIN enmOrigin)
3757{
3758 RTGCPHYS GCPhys;
3759 uint64_t fFlags;
3760 int rc;
3761 PVM pVM = pVCpu->CTX_SUFF(pVM);
3762 VMCPU_ASSERT_EMT(pVCpu);
3763
3764 /*
3765 * Anything to do?
3766 */
3767 if (!cb)
3768 return VINF_SUCCESS;
3769
3770 LogFlow(("PGMPhysReadGCPtr: %RGv %zu\n", GCPtrSrc, cb));
3771
3772 /*
3773 * Optimize reads within a single page.
3774 */
3775 if (((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
3776 {
3777 /* Convert virtual to physical address + flags */
3778 rc = PGMGstGetPage(pVCpu, (RTGCUINTPTR)GCPtrSrc, &fFlags, &GCPhys);
3779 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrSrc), rc);
3780 GCPhys |= (RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK;
3781
3782 /* mark the guest page as accessed. */
3783 if (!(fFlags & X86_PTE_A))
3784 {
3785 rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
3786 AssertRC(rc);
3787 }
3788
3789 return PGMPhysRead(pVM, GCPhys, pvDst, cb, enmOrigin);
3790 }
3791
3792 /*
3793 * Page by page.
3794 */
3795 for (;;)
3796 {
3797 /* Convert virtual to physical address + flags */
3798 rc = PGMGstGetPage(pVCpu, (RTGCUINTPTR)GCPtrSrc, &fFlags, &GCPhys);
3799 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrSrc), rc);
3800 GCPhys |= (RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK;
3801
3802 /* mark the guest page as accessed. */
3803 if (!(fFlags & X86_PTE_A))
3804 {
3805 rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
3806 AssertRC(rc);
3807 }
3808
3809 /* copy */
3810 size_t cbRead = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
3811 if (cbRead < cb)
3812 {
3813 VBOXSTRICTRC rcStrict = PGMPhysRead(pVM, GCPhys, pvDst, cbRead, enmOrigin);
3814 if (RT_LIKELY(rcStrict == VINF_SUCCESS))
3815 { /* likely */ }
3816 else
3817 return rcStrict;
3818 }
3819 else /* Last page (cbRead is PAGE_SIZE, we only need cb!) */
3820 return PGMPhysRead(pVM, GCPhys, pvDst, cb, enmOrigin);
3821
3822 /* next */
3823 Assert(cb > cbRead);
3824 cb -= cbRead;
3825 pvDst = (uint8_t *)pvDst + cbRead;
3826 GCPtrSrc += cbRead;
3827 }
3828}
3829
3830
3831/**
3832 * Write to guest physical memory referenced by GC pointer.
3833 *
3834 * This function uses the current CR3/CR0/CR4 of the guest and will
3835 * respect access handlers and set dirty and accessed bits.
3836 *
3837 * @returns Strict VBox status, see PGMPhysWrite for details.
3838 * @retval VERR_PAGE_TABLE_NOT_PRESENT if there is no page mapped at the
3839 * specified virtual address.
3840 *
3841 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
3842 * @param GCPtrDst The destination address (GC pointer).
3843 * @param pvSrc The source address.
3844 * @param cb The number of bytes to write.
3845 * @param enmOrigin Who is calling.
3846 */
3847VMMDECL(VBOXSTRICTRC) PGMPhysWriteGCPtr(PVMCPU pVCpu, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb, PGMACCESSORIGIN enmOrigin)
3848{
3849 RTGCPHYS GCPhys;
3850 uint64_t fFlags;
3851 int rc;
3852 PVM pVM = pVCpu->CTX_SUFF(pVM);
3853 VMCPU_ASSERT_EMT(pVCpu);
3854
3855 /*
3856 * Anything to do?
3857 */
3858 if (!cb)
3859 return VINF_SUCCESS;
3860
3861 LogFlow(("PGMPhysWriteGCPtr: %RGv %zu\n", GCPtrDst, cb));
3862
3863 /*
3864 * Optimize writes within a single page.
3865 */
3866 if (((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
3867 {
3868 /* Convert virtual to physical address + flags */
3869 rc = PGMGstGetPage(pVCpu, (RTGCUINTPTR)GCPtrDst, &fFlags, &GCPhys);
3870 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrDst), rc);
3871 GCPhys |= (RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK;
3872
3873 /* Mention when we ignore X86_PTE_RW... */
3874 if (!(fFlags & X86_PTE_RW))
3875 Log(("PGMPhysWriteGCPtr: Writing to RO page %RGv %#x\n", GCPtrDst, cb));
3876
3877 /* Mark the guest page as accessed and dirty if necessary. */
3878 if ((fFlags & (X86_PTE_A | X86_PTE_D)) != (X86_PTE_A | X86_PTE_D))
3879 {
3880 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
3881 AssertRC(rc);
3882 }
3883
3884 return PGMPhysWrite(pVM, GCPhys, pvSrc, cb, enmOrigin);
3885 }
3886
3887 /*
3888 * Page by page.
3889 */
3890 for (;;)
3891 {
3892 /* Convert virtual to physical address + flags */
3893 rc = PGMGstGetPage(pVCpu, (RTGCUINTPTR)GCPtrDst, &fFlags, &GCPhys);
3894 AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrDst), rc);
3895 GCPhys |= (RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK;
3896
3897 /* Mention when we ignore X86_PTE_RW... */
3898 if (!(fFlags & X86_PTE_RW))
3899 Log(("PGMPhysWriteGCPtr: Writing to RO page %RGv %#x\n", GCPtrDst, cb));
3900
3901 /* Mark the guest page as accessed and dirty if necessary. */
3902 if ((fFlags & (X86_PTE_A | X86_PTE_D)) != (X86_PTE_A | X86_PTE_D))
3903 {
3904 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
3905 AssertRC(rc);
3906 }
3907
3908 /* copy */
3909 size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
3910 if (cbWrite < cb)
3911 {
3912 VBOXSTRICTRC rcStrict = PGMPhysWrite(pVM, GCPhys, pvSrc, cbWrite, enmOrigin);
3913 if (RT_LIKELY(rcStrict == VINF_SUCCESS))
3914 { /* likely */ }
3915 else
3916 return rcStrict;
3917 }
3918 else /* Last page (cbWrite is PAGE_SIZE, we only need cb!) */
3919 return PGMPhysWrite(pVM, GCPhys, pvSrc, cb, enmOrigin);
3920
3921 /* next */
3922 Assert(cb > cbWrite);
3923 cb -= cbWrite;
3924 pvSrc = (uint8_t *)pvSrc + cbWrite;
3925 GCPtrDst += cbWrite;
3926 }
3927}
3928
3929
3930/**
3931 * Performs a read of guest virtual memory for instruction emulation.
3932 *
3933 * This will check permissions, raise exceptions and update the access bits.
3934 *
3935 * The current implementation will bypass all access handlers. It may later be
3936 * changed to at least respect MMIO.
3937 *
3938 *
3939 * @returns VBox status code suitable to scheduling.
3940 * @retval VINF_SUCCESS if the read was performed successfully.
3941 * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
3942 * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
3943 *
3944 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
3945 * @param pCtxCore The context core.
3946 * @param pvDst Where to put the bytes we've read.
3947 * @param GCPtrSrc The source address.
3948 * @param cb The number of bytes to read. Not more than a page.
3949 *
3950 * @remark This function will dynamically map physical pages in GC. This may unmap
3951 * mappings done by the caller. Be careful!
3952 */
3953VMMDECL(int) PGMPhysInterpretedRead(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb)
3954{
3955 PVM pVM = pVCpu->CTX_SUFF(pVM);
3956 Assert(cb <= PAGE_SIZE);
3957 VMCPU_ASSERT_EMT(pVCpu);
3958
3959/** @todo r=bird: This isn't perfect!
3960 * -# It's not checking for reserved bits being 1.
3961 * -# It's not correctly dealing with the access bit.
3962 * -# It's not respecting MMIO memory or any other access handlers.
3963 */
3964 /*
3965 * 1. Translate virtual to physical. This may fault.
3966 * 2. Map the physical address.
3967 * 3. Do the read operation.
3968 * 4. Set access bits if required.
3969 */
3970 int rc;
3971 unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
3972 if (cb <= cb1)
3973 {
3974 /*
3975 * Not crossing pages.
3976 */
3977 RTGCPHYS GCPhys;
3978 uint64_t fFlags;
3979 rc = PGMGstGetPage(pVCpu, GCPtrSrc, &fFlags, &GCPhys);
3980 if (RT_SUCCESS(rc))
3981 {
3982 /** @todo we should check reserved bits ... */
3983 PGMPAGEMAPLOCK PgMpLck;
3984 void const *pvSrc;
3985 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys, &pvSrc, &PgMpLck);
3986 switch (rc)
3987 {
3988 case VINF_SUCCESS:
3989 Log(("PGMPhysInterpretedRead: pvDst=%p pvSrc=%p cb=%d\n", pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb));
3990 memcpy(pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
3991 PGMPhysReleasePageMappingLock(pVM, &PgMpLck);
3992 break;
3993 case VERR_PGM_PHYS_PAGE_RESERVED:
3994 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
3995 memset(pvDst, 0xff, cb);
3996 break;
3997 default:
3998 Assert(RT_FAILURE_NP(rc));
3999 return rc;
4000 }
4001
4002 /** @todo access bit emulation isn't 100% correct. */
4003 if (!(fFlags & X86_PTE_A))
4004 {
4005 rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
4006 AssertRC(rc);
4007 }
4008 return VINF_SUCCESS;
4009 }
4010 }
4011 else
4012 {
4013 /*
4014 * Crosses pages.
4015 */
4016 size_t cb2 = cb - cb1;
4017 uint64_t fFlags1;
4018 RTGCPHYS GCPhys1;
4019 uint64_t fFlags2;
4020 RTGCPHYS GCPhys2;
4021 rc = PGMGstGetPage(pVCpu, GCPtrSrc, &fFlags1, &GCPhys1);
4022 if (RT_SUCCESS(rc))
4023 {
4024 rc = PGMGstGetPage(pVCpu, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
4025 if (RT_SUCCESS(rc))
4026 {
4027 /** @todo we should check reserved bits ... */
4028 AssertMsgFailed(("cb=%d cb1=%d cb2=%d GCPtrSrc=%RGv\n", cb, cb1, cb2, GCPtrSrc));
4029 PGMPAGEMAPLOCK PgMpLck;
4030 void const *pvSrc1;
4031 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys1, &pvSrc1, &PgMpLck);
4032 switch (rc)
4033 {
4034 case VINF_SUCCESS:
4035 memcpy(pvDst, (uint8_t *)pvSrc1 + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
4036 PGMPhysReleasePageMappingLock(pVM, &PgMpLck);
4037 break;
4038 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
4039 memset(pvDst, 0xff, cb1);
4040 break;
4041 default:
4042 Assert(RT_FAILURE_NP(rc));
4043 return rc;
4044 }
4045
4046 void const *pvSrc2;
4047 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys2, &pvSrc2, &PgMpLck);
4048 switch (rc)
4049 {
4050 case VINF_SUCCESS:
4051 memcpy((uint8_t *)pvDst + cb1, pvSrc2, cb2);
4052 PGMPhysReleasePageMappingLock(pVM, &PgMpLck);
4053 break;
4054 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
4055 memset((uint8_t *)pvDst + cb1, 0xff, cb2);
4056 break;
4057 default:
4058 Assert(RT_FAILURE_NP(rc));
4059 return rc;
4060 }
4061
4062 if (!(fFlags1 & X86_PTE_A))
4063 {
4064 rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
4065 AssertRC(rc);
4066 }
4067 if (!(fFlags2 & X86_PTE_A))
4068 {
4069 rc = PGMGstModifyPage(pVCpu, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
4070 AssertRC(rc);
4071 }
4072 return VINF_SUCCESS;
4073 }
4074 }
4075 }
4076
4077 /*
4078 * Raise a #PF.
4079 */
4080 uint32_t uErr;
4081
4082 /* Get the current privilege level. */
4083 uint32_t cpl = CPUMGetGuestCPL(pVCpu);
4084 switch (rc)
4085 {
4086 case VINF_SUCCESS:
4087 uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
4088 break;
4089
4090 case VERR_PAGE_NOT_PRESENT:
4091 case VERR_PAGE_TABLE_NOT_PRESENT:
4092 uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
4093 break;
4094
4095 default:
4096 AssertMsgFailed(("rc=%Rrc GCPtrSrc=%RGv cb=%#x\n", rc, GCPtrSrc, cb));
4097 return rc;
4098 }
4099 Log(("PGMPhysInterpretedRead: GCPtrSrc=%RGv cb=%#x -> #PF(%#x)\n", GCPtrSrc, cb, uErr));
4100 return TRPMRaiseXcptErrCR2(pVCpu, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
4101}
4102
4103
4104/**
4105 * Performs a read of guest virtual memory for instruction emulation.
4106 *
4107 * This will check permissions, raise exceptions and update the access bits.
4108 *
4109 * The current implementation will bypass all access handlers. It may later be
4110 * changed to at least respect MMIO.
4111 *
4112 *
4113 * @returns VBox status code suitable to scheduling.
4114 * @retval VINF_SUCCESS if the read was performed successfully.
4115 * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
4116 * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
4117 *
4118 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
4119 * @param pCtxCore The context core.
4120 * @param pvDst Where to put the bytes we've read.
4121 * @param GCPtrSrc The source address.
4122 * @param cb The number of bytes to read. Not more than a page.
4123 * @param fRaiseTrap If set the trap will be raised on as per spec, if clear
4124 * an appropriate error status will be returned (no
4125 * informational at all).
4126 *
4127 *
4128 * @remarks Takes the PGM lock.
4129 * @remarks A page fault on the 2nd page of the access will be raised without
4130 * writing the bits on the first page since we're ASSUMING that the
4131 * caller is emulating an instruction access.
4132 * @remarks This function will dynamically map physical pages in GC. This may
4133 * unmap mappings done by the caller. Be careful!
4134 */
4135VMMDECL(int) PGMPhysInterpretedReadNoHandlers(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb,
4136 bool fRaiseTrap)
4137{
4138 PVM pVM = pVCpu->CTX_SUFF(pVM);
4139 Assert(cb <= PAGE_SIZE);
4140 VMCPU_ASSERT_EMT(pVCpu);
4141
4142 /*
4143 * 1. Translate virtual to physical. This may fault.
4144 * 2. Map the physical address.
4145 * 3. Do the read operation.
4146 * 4. Set access bits if required.
4147 */
4148 int rc;
4149 unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
4150 if (cb <= cb1)
4151 {
4152 /*
4153 * Not crossing pages.
4154 */
4155 RTGCPHYS GCPhys;
4156 uint64_t fFlags;
4157 rc = PGMGstGetPage(pVCpu, GCPtrSrc, &fFlags, &GCPhys);
4158 if (RT_SUCCESS(rc))
4159 {
4160 if (1) /** @todo we should check reserved bits ... */
4161 {
4162 const void *pvSrc;
4163 PGMPAGEMAPLOCK Lock;
4164 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys, &pvSrc, &Lock);
4165 switch (rc)
4166 {
4167 case VINF_SUCCESS:
4168 Log(("PGMPhysInterpretedReadNoHandlers: pvDst=%p pvSrc=%p (%RGv) cb=%d\n",
4169 pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), GCPtrSrc, cb));
4170 memcpy(pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
4171 PGMPhysReleasePageMappingLock(pVM, &Lock);
4172 break;
4173 case VERR_PGM_PHYS_PAGE_RESERVED:
4174 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
4175 memset(pvDst, 0xff, cb);
4176 break;
4177 default:
4178 AssertMsgFailed(("%Rrc\n", rc));
4179 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
4180 return rc;
4181 }
4182
4183 if (!(fFlags & X86_PTE_A))
4184 {
4185 /** @todo access bit emulation isn't 100% correct. */
4186 rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
4187 AssertRC(rc);
4188 }
4189 return VINF_SUCCESS;
4190 }
4191 }
4192 }
4193 else
4194 {
4195 /*
4196 * Crosses pages.
4197 */
4198 size_t cb2 = cb - cb1;
4199 uint64_t fFlags1;
4200 RTGCPHYS GCPhys1;
4201 uint64_t fFlags2;
4202 RTGCPHYS GCPhys2;
4203 rc = PGMGstGetPage(pVCpu, GCPtrSrc, &fFlags1, &GCPhys1);
4204 if (RT_SUCCESS(rc))
4205 {
4206 rc = PGMGstGetPage(pVCpu, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
4207 if (RT_SUCCESS(rc))
4208 {
4209 if (1) /** @todo we should check reserved bits ... */
4210 {
4211 const void *pvSrc;
4212 PGMPAGEMAPLOCK Lock;
4213 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys1, &pvSrc, &Lock);
4214 switch (rc)
4215 {
4216 case VINF_SUCCESS:
4217 Log(("PGMPhysInterpretedReadNoHandlers: pvDst=%p pvSrc=%p (%RGv) cb=%d [2]\n",
4218 pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), GCPtrSrc, cb1));
4219 memcpy(pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
4220 PGMPhysReleasePageMappingLock(pVM, &Lock);
4221 break;
4222 case VERR_PGM_PHYS_PAGE_RESERVED:
4223 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
4224 memset(pvDst, 0xff, cb1);
4225 break;
4226 default:
4227 AssertMsgFailed(("%Rrc\n", rc));
4228 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
4229 return rc;
4230 }
4231
4232 rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys2, &pvSrc, &Lock);
4233 switch (rc)
4234 {
4235 case VINF_SUCCESS:
4236 memcpy((uint8_t *)pvDst + cb1, pvSrc, cb2);
4237 PGMPhysReleasePageMappingLock(pVM, &Lock);
4238 break;
4239 case VERR_PGM_PHYS_PAGE_RESERVED:
4240 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
4241 memset((uint8_t *)pvDst + cb1, 0xff, cb2);
4242 break;
4243 default:
4244 AssertMsgFailed(("%Rrc\n", rc));
4245 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
4246 return rc;
4247 }
4248
4249 if (!(fFlags1 & X86_PTE_A))
4250 {
4251 rc = PGMGstModifyPage(pVCpu, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
4252 AssertRC(rc);
4253 }
4254 if (!(fFlags2 & X86_PTE_A))
4255 {
4256 rc = PGMGstModifyPage(pVCpu, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
4257 AssertRC(rc);
4258 }
4259 return VINF_SUCCESS;
4260 }
4261 /* sort out which page */
4262 }
4263 else
4264 GCPtrSrc += cb1; /* fault on 2nd page */
4265 }
4266 }
4267
4268 /*
4269 * Raise a #PF if we're allowed to do that.
4270 */
4271 /* Calc the error bits. */
4272 uint32_t cpl = CPUMGetGuestCPL(pVCpu);
4273 uint32_t uErr;
4274 switch (rc)
4275 {
4276 case VINF_SUCCESS:
4277 uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
4278 rc = VERR_ACCESS_DENIED;
4279 break;
4280
4281 case VERR_PAGE_NOT_PRESENT:
4282 case VERR_PAGE_TABLE_NOT_PRESENT:
4283 uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
4284 break;
4285
4286 default:
4287 AssertMsgFailed(("rc=%Rrc GCPtrSrc=%RGv cb=%#x\n", rc, GCPtrSrc, cb));
4288 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
4289 return rc;
4290 }
4291 if (fRaiseTrap)
4292 {
4293 Log(("PGMPhysInterpretedReadNoHandlers: GCPtrSrc=%RGv cb=%#x -> Raised #PF(%#x)\n", GCPtrSrc, cb, uErr));
4294 return TRPMRaiseXcptErrCR2(pVCpu, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
4295 }
4296 Log(("PGMPhysInterpretedReadNoHandlers: GCPtrSrc=%RGv cb=%#x -> #PF(%#x) [!raised]\n", GCPtrSrc, cb, uErr));
4297 return rc;
4298}
4299
4300
4301/**
4302 * Performs a write to guest virtual memory for instruction emulation.
4303 *
4304 * This will check permissions, raise exceptions and update the dirty and access
4305 * bits.
4306 *
4307 * @returns VBox status code suitable to scheduling.
4308 * @retval VINF_SUCCESS if the read was performed successfully.
4309 * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
4310 * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
4311 *
4312 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
4313 * @param pCtxCore The context core.
4314 * @param GCPtrDst The destination address.
4315 * @param pvSrc What to write.
4316 * @param cb The number of bytes to write. Not more than a page.
4317 * @param fRaiseTrap If set the trap will be raised on as per spec, if clear
4318 * an appropriate error status will be returned (no
4319 * informational at all).
4320 *
4321 * @remarks Takes the PGM lock.
4322 * @remarks A page fault on the 2nd page of the access will be raised without
4323 * writing the bits on the first page since we're ASSUMING that the
4324 * caller is emulating an instruction access.
4325 * @remarks This function will dynamically map physical pages in GC. This may
4326 * unmap mappings done by the caller. Be careful!
4327 */
4328VMMDECL(int) PGMPhysInterpretedWriteNoHandlers(PVMCPU pVCpu, PCPUMCTXCORE pCtxCore, RTGCPTR GCPtrDst, const void *pvSrc,
4329 size_t cb, bool fRaiseTrap)
4330{
4331 Assert(cb <= PAGE_SIZE);
4332 PVM pVM = pVCpu->CTX_SUFF(pVM);
4333 VMCPU_ASSERT_EMT(pVCpu);
4334
4335 /*
4336 * 1. Translate virtual to physical. This may fault.
4337 * 2. Map the physical address.
4338 * 3. Do the write operation.
4339 * 4. Set access bits if required.
4340 */
4341 /** @todo Since this method is frequently used by EMInterpret or IOM
4342 * upon a write fault to an write access monitored page, we can
4343 * reuse the guest page table walking from the \#PF code. */
4344 int rc;
4345 unsigned cb1 = PAGE_SIZE - (GCPtrDst & PAGE_OFFSET_MASK);
4346 if (cb <= cb1)
4347 {
4348 /*
4349 * Not crossing pages.
4350 */
4351 RTGCPHYS GCPhys;
4352 uint64_t fFlags;
4353 rc = PGMGstGetPage(pVCpu, GCPtrDst, &fFlags, &GCPhys);
4354 if (RT_SUCCESS(rc))
4355 {
4356 if ( (fFlags & X86_PTE_RW) /** @todo Also check reserved bits. */
4357 || ( !(CPUMGetGuestCR0(pVCpu) & X86_CR0_WP)
4358 && CPUMGetGuestCPL(pVCpu) <= 2) ) /** @todo it's 2, right? Check cpl check below as well. */
4359 {
4360 void *pvDst;
4361 PGMPAGEMAPLOCK Lock;
4362 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys, &pvDst, &Lock);
4363 switch (rc)
4364 {
4365 case VINF_SUCCESS:
4366 Log(("PGMPhysInterpretedWriteNoHandlers: pvDst=%p (%RGv) pvSrc=%p cb=%d\n",
4367 (uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), GCPtrDst, pvSrc, cb));
4368 memcpy((uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), pvSrc, cb);
4369 PGMPhysReleasePageMappingLock(pVM, &Lock);
4370 break;
4371 case VERR_PGM_PHYS_PAGE_RESERVED:
4372 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
4373 /* bit bucket */
4374 break;
4375 default:
4376 AssertMsgFailed(("%Rrc\n", rc));
4377 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
4378 return rc;
4379 }
4380
4381 if (!(fFlags & (X86_PTE_A | X86_PTE_D)))
4382 {
4383 /** @todo dirty & access bit emulation isn't 100% correct. */
4384 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
4385 AssertRC(rc);
4386 }
4387 return VINF_SUCCESS;
4388 }
4389 rc = VERR_ACCESS_DENIED;
4390 }
4391 }
4392 else
4393 {
4394 /*
4395 * Crosses pages.
4396 */
4397 size_t cb2 = cb - cb1;
4398 uint64_t fFlags1;
4399 RTGCPHYS GCPhys1;
4400 uint64_t fFlags2;
4401 RTGCPHYS GCPhys2;
4402 rc = PGMGstGetPage(pVCpu, GCPtrDst, &fFlags1, &GCPhys1);
4403 if (RT_SUCCESS(rc))
4404 {
4405 rc = PGMGstGetPage(pVCpu, GCPtrDst + cb1, &fFlags2, &GCPhys2);
4406 if (RT_SUCCESS(rc))
4407 {
4408 if ( ( (fFlags1 & X86_PTE_RW) /** @todo Also check reserved bits. */
4409 && (fFlags2 & X86_PTE_RW))
4410 || ( !(CPUMGetGuestCR0(pVCpu) & X86_CR0_WP)
4411 && CPUMGetGuestCPL(pVCpu) <= 2) )
4412 {
4413 void *pvDst;
4414 PGMPAGEMAPLOCK Lock;
4415 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys1, &pvDst, &Lock);
4416 switch (rc)
4417 {
4418 case VINF_SUCCESS:
4419 Log(("PGMPhysInterpretedWriteNoHandlers: pvDst=%p (%RGv) pvSrc=%p cb=%d\n",
4420 (uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), GCPtrDst, pvSrc, cb1));
4421 memcpy((uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), pvSrc, cb1);
4422 PGMPhysReleasePageMappingLock(pVM, &Lock);
4423 break;
4424 case VERR_PGM_PHYS_PAGE_RESERVED:
4425 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
4426 /* bit bucket */
4427 break;
4428 default:
4429 AssertMsgFailed(("%Rrc\n", rc));
4430 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
4431 return rc;
4432 }
4433
4434 rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys2, &pvDst, &Lock);
4435 switch (rc)
4436 {
4437 case VINF_SUCCESS:
4438 memcpy(pvDst, (const uint8_t *)pvSrc + cb1, cb2);
4439 PGMPhysReleasePageMappingLock(pVM, &Lock);
4440 break;
4441 case VERR_PGM_PHYS_PAGE_RESERVED:
4442 case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
4443 /* bit bucket */
4444 break;
4445 default:
4446 AssertMsgFailed(("%Rrc\n", rc));
4447 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
4448 return rc;
4449 }
4450
4451 if (!(fFlags1 & (X86_PTE_A | X86_PTE_RW)))
4452 {
4453 rc = PGMGstModifyPage(pVCpu, GCPtrDst, 1, (X86_PTE_A | X86_PTE_RW), ~(uint64_t)(X86_PTE_A | X86_PTE_RW));
4454 AssertRC(rc);
4455 }
4456 if (!(fFlags2 & (X86_PTE_A | X86_PTE_RW)))
4457 {
4458 rc = PGMGstModifyPage(pVCpu, GCPtrDst + cb1, 1, (X86_PTE_A | X86_PTE_RW), ~(uint64_t)(X86_PTE_A | X86_PTE_RW));
4459 AssertRC(rc);
4460 }
4461 return VINF_SUCCESS;
4462 }
4463 if ((fFlags1 & (X86_PTE_RW)) == X86_PTE_RW)
4464 GCPtrDst += cb1; /* fault on the 2nd page. */
4465 rc = VERR_ACCESS_DENIED;
4466 }
4467 else
4468 GCPtrDst += cb1; /* fault on the 2nd page. */
4469 }
4470 }
4471
4472 /*
4473 * Raise a #PF if we're allowed to do that.
4474 */
4475 /* Calc the error bits. */
4476 uint32_t uErr;
4477 uint32_t cpl = CPUMGetGuestCPL(pVCpu);
4478 switch (rc)
4479 {
4480 case VINF_SUCCESS:
4481 uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
4482 rc = VERR_ACCESS_DENIED;
4483 break;
4484
4485 case VERR_ACCESS_DENIED:
4486 uErr = (cpl >= 2) ? X86_TRAP_PF_RW | X86_TRAP_PF_US : X86_TRAP_PF_RW;
4487 break;
4488
4489 case VERR_PAGE_NOT_PRESENT:
4490 case VERR_PAGE_TABLE_NOT_PRESENT:
4491 uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
4492 break;
4493
4494 default:
4495 AssertMsgFailed(("rc=%Rrc GCPtrDst=%RGv cb=%#x\n", rc, GCPtrDst, cb));
4496 AssertReturn(RT_FAILURE(rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
4497 return rc;
4498 }
4499 if (fRaiseTrap)
4500 {
4501 Log(("PGMPhysInterpretedWriteNoHandlers: GCPtrDst=%RGv cb=%#x -> Raised #PF(%#x)\n", GCPtrDst, cb, uErr));
4502 return TRPMRaiseXcptErrCR2(pVCpu, pCtxCore, X86_XCPT_PF, uErr, GCPtrDst);
4503 }
4504 Log(("PGMPhysInterpretedWriteNoHandlers: GCPtrDst=%RGv cb=%#x -> #PF(%#x) [!raised]\n", GCPtrDst, cb, uErr));
4505 return rc;
4506}
4507
4508
4509/**
4510 * Return the page type of the specified physical address.
4511 *
4512 * @returns The page type.
4513 * @param pVM The cross context VM structure.
4514 * @param GCPhys Guest physical address
4515 */
4516VMM_INT_DECL(PGMPAGETYPE) PGMPhysGetPageType(PVM pVM, RTGCPHYS GCPhys)
4517{
4518 pgmLock(pVM);
4519 PPGMPAGE pPage = pgmPhysGetPage(pVM, GCPhys);
4520 PGMPAGETYPE enmPgType = pPage ? (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage) : PGMPAGETYPE_INVALID;
4521 pgmUnlock(pVM);
4522
4523 return enmPgType;
4524}
4525
4526
4527/**
4528 * Converts a GC physical address to a HC ring-3 pointer, with some
4529 * additional checks.
4530 *
4531 * @returns VBox status code (no informational statuses).
4532 *
4533 * @param pVM The cross context VM structure.
4534 * @param pVCpu The cross context virtual CPU structure of the
4535 * calling EMT.
4536 * @param GCPhys The GC physical address to convert. This API mask
4537 * the A20 line when necessary.
4538 * @param puTlbPhysRev Where to read the physical TLB revision. Needs to
4539 * be done while holding the PGM lock.
4540 * @param ppb Where to store the pointer corresponding to GCPhys
4541 * on success.
4542 * @param pfTlb The TLB flags and revision. We only add stuff.
4543 *
4544 * @remarks This is more or a less a copy of PGMR3PhysTlbGCPhys2Ptr and
4545 * PGMPhysIemGCPhys2Ptr.
4546 *
4547 * @thread EMT(pVCpu).
4548 */
4549VMM_INT_DECL(int) PGMPhysIemGCPhys2PtrNoLock(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, uint64_t const volatile *puTlbPhysRev,
4550#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
4551 R3PTRTYPE(uint8_t *) *ppb,
4552#else
4553 R3R0PTRTYPE(uint8_t *) *ppb,
4554#endif
4555 uint64_t *pfTlb)
4556{
4557 PGM_A20_APPLY_TO_VAR(pVCpu, GCPhys);
4558 Assert(!(GCPhys & X86_PAGE_OFFSET_MASK));
4559
4560 pgmLock(pVM);
4561
4562 PPGMRAMRANGE pRam;
4563 PPGMPAGE pPage;
4564 int rc = pgmPhysGetPageAndRangeEx(pVM, GCPhys, &pPage, &pRam);
4565 if (RT_SUCCESS(rc))
4566 {
4567 if (!PGM_PAGE_IS_BALLOONED(pPage))
4568 {
4569 if (!PGM_PAGE_IS_SPECIAL_ALIAS_MMIO(pPage))
4570 {
4571 if (!PGM_PAGE_HAS_ANY_HANDLERS(pPage))
4572 {
4573 /*
4574 * No access handler.
4575 */
4576 switch (PGM_PAGE_GET_STATE(pPage))
4577 {
4578 case PGM_PAGE_STATE_ALLOCATED:
4579 *pfTlb |= *puTlbPhysRev;
4580 break;
4581 case PGM_PAGE_STATE_BALLOONED:
4582 AssertFailed();
4583 RT_FALL_THRU();
4584 case PGM_PAGE_STATE_ZERO:
4585 case PGM_PAGE_STATE_SHARED:
4586 case PGM_PAGE_STATE_WRITE_MONITORED:
4587 *pfTlb |= *puTlbPhysRev | PGMIEMGCPHYS2PTR_F_NO_WRITE;
4588 break;
4589 }
4590#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
4591 *pfTlb |= PGMIEMGCPHYS2PTR_F_NO_MAPPINGR3;
4592 *ppb = NULL;
4593#else
4594 PPGMPAGER3MAPTLBE pTlbe;
4595 rc = pgmPhysPageQueryTlbeWithPage(pVM, pPage, GCPhys, &pTlbe);
4596 AssertLogRelRCReturn(rc, rc);
4597 *ppb = (uint8_t *)pTlbe->pv;
4598#endif
4599 }
4600 else if (PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage))
4601 {
4602 /*
4603 * MMIO or similar all access handler: Catch all access.
4604 */
4605 *pfTlb |= *puTlbPhysRev
4606 | PGMIEMGCPHYS2PTR_F_NO_WRITE | PGMIEMGCPHYS2PTR_F_NO_READ | PGMIEMGCPHYS2PTR_F_NO_MAPPINGR3;
4607 *ppb = NULL;
4608 }
4609 else
4610 {
4611 /*
4612 * Write access handler: Catch write accesses if active.
4613 */
4614 if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage))
4615 *pfTlb |= *puTlbPhysRev | PGMIEMGCPHYS2PTR_F_NO_WRITE;
4616 else
4617 switch (PGM_PAGE_GET_STATE(pPage))
4618 {
4619 case PGM_PAGE_STATE_ALLOCATED:
4620 *pfTlb |= *puTlbPhysRev;
4621 break;
4622 case PGM_PAGE_STATE_BALLOONED:
4623 AssertFailed();
4624 RT_FALL_THRU();
4625 case PGM_PAGE_STATE_ZERO:
4626 case PGM_PAGE_STATE_SHARED:
4627 case PGM_PAGE_STATE_WRITE_MONITORED:
4628 *pfTlb |= *puTlbPhysRev | PGMIEMGCPHYS2PTR_F_NO_WRITE;
4629 break;
4630 }
4631#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
4632 *pfTlb |= PGMIEMGCPHYS2PTR_F_NO_MAPPINGR3;
4633 *ppb = NULL;
4634#else
4635 PPGMPAGER3MAPTLBE pTlbe;
4636 rc = pgmPhysPageQueryTlbeWithPage(pVM, pPage, GCPhys, &pTlbe);
4637 AssertLogRelRCReturn(rc, rc);
4638 *ppb = (uint8_t *)pTlbe->pv;
4639#endif
4640 }
4641 }
4642 else
4643 {
4644 /* Alias MMIO: For now, we catch all access. */
4645 *pfTlb |= *puTlbPhysRev
4646 | PGMIEMGCPHYS2PTR_F_NO_WRITE | PGMIEMGCPHYS2PTR_F_NO_READ | PGMIEMGCPHYS2PTR_F_NO_MAPPINGR3;
4647 *ppb = NULL;
4648 }
4649 }
4650 else
4651 {
4652 /* Ballooned: Shouldn't get here, but we read zero page via PGMPhysRead and writes goes to /dev/null. */
4653 *pfTlb |= *puTlbPhysRev | PGMIEMGCPHYS2PTR_F_NO_WRITE | PGMIEMGCPHYS2PTR_F_NO_READ | PGMIEMGCPHYS2PTR_F_NO_MAPPINGR3;
4654 *ppb = NULL;
4655 }
4656 Log6(("PGMPhysIemGCPhys2PtrNoLock: GCPhys=%RGp *ppb=%p *pfTlb=%#RX64 pPage=%R[pgmpage]\n", GCPhys, *ppb, *pfTlb, pPage));
4657 }
4658 else
4659 {
4660 *pfTlb |= *puTlbPhysRev | PGMIEMGCPHYS2PTR_F_NO_WRITE | PGMIEMGCPHYS2PTR_F_NO_READ | PGMIEMGCPHYS2PTR_F_NO_MAPPINGR3;
4661 *ppb = NULL;
4662 Log6(("PGMPhysIemGCPhys2PtrNoLock: GCPhys=%RGp *ppb=%p *pfTlb=%#RX64 (rc=%Rrc)\n", GCPhys, *ppb, *pfTlb, rc));
4663 }
4664
4665 pgmUnlock(pVM);
4666 return VINF_SUCCESS;
4667}
4668
4669
4670/**
4671 * Converts a GC physical address to a HC ring-3 pointer, with some
4672 * additional checks.
4673 *
4674 * @returns VBox status code (no informational statuses).
4675 * @retval VINF_SUCCESS on success.
4676 * @retval VERR_PGM_PHYS_TLB_CATCH_WRITE and *ppv set if the page has a write
4677 * access handler of some kind.
4678 * @retval VERR_PGM_PHYS_TLB_CATCH_ALL if the page has a handler catching all
4679 * accesses or is odd in any way.
4680 * @retval VERR_PGM_PHYS_TLB_UNASSIGNED if the page doesn't exist.
4681 *
4682 * @param pVM The cross context VM structure.
4683 * @param pVCpu The cross context virtual CPU structure of the
4684 * calling EMT.
4685 * @param GCPhys The GC physical address to convert. This API mask
4686 * the A20 line when necessary.
4687 * @param fWritable Whether write access is required.
4688 * @param fByPassHandlers Whether to bypass access handlers.
4689 * @param ppv Where to store the pointer corresponding to GCPhys
4690 * on success.
4691 * @param pLock
4692 *
4693 * @remarks This is more or a less a copy of PGMR3PhysTlbGCPhys2Ptr.
4694 * @thread EMT(pVCpu).
4695 */
4696VMM_INT_DECL(int) PGMPhysIemGCPhys2Ptr(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, bool fWritable, bool fByPassHandlers,
4697 void **ppv, PPGMPAGEMAPLOCK pLock)
4698{
4699 PGM_A20_APPLY_TO_VAR(pVCpu, GCPhys);
4700
4701 pgmLock(pVM);
4702
4703 PPGMRAMRANGE pRam;
4704 PPGMPAGE pPage;
4705 int rc = pgmPhysGetPageAndRangeEx(pVM, GCPhys, &pPage, &pRam);
4706 if (RT_SUCCESS(rc))
4707 {
4708 if (PGM_PAGE_IS_BALLOONED(pPage))
4709 rc = VERR_PGM_PHYS_TLB_CATCH_WRITE;
4710 else if (PGM_PAGE_IS_SPECIAL_ALIAS_MMIO(pPage))
4711 rc = VERR_PGM_PHYS_TLB_CATCH_ALL;
4712 else if ( !PGM_PAGE_HAS_ANY_HANDLERS(pPage)
4713 || (fByPassHandlers && !PGM_PAGE_IS_MMIO(pPage)) )
4714 rc = VINF_SUCCESS;
4715 else
4716 {
4717 if (PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage)) /* catches MMIO */
4718 {
4719 Assert(!fByPassHandlers || PGM_PAGE_IS_MMIO(pPage));
4720 rc = VERR_PGM_PHYS_TLB_CATCH_ALL;
4721 }
4722 else if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage) && fWritable)
4723 {
4724 Assert(!fByPassHandlers);
4725 rc = VERR_PGM_PHYS_TLB_CATCH_WRITE;
4726 }
4727 }
4728 if (RT_SUCCESS(rc))
4729 {
4730 int rc2;
4731
4732 /* Make sure what we return is writable. */
4733 if (fWritable)
4734 switch (PGM_PAGE_GET_STATE(pPage))
4735 {
4736 case PGM_PAGE_STATE_ALLOCATED:
4737 break;
4738 case PGM_PAGE_STATE_BALLOONED:
4739 AssertFailed();
4740 break;
4741 case PGM_PAGE_STATE_ZERO:
4742 case PGM_PAGE_STATE_SHARED:
4743 case PGM_PAGE_STATE_WRITE_MONITORED:
4744 rc2 = pgmPhysPageMakeWritable(pVM, pPage, GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK);
4745 AssertLogRelRCReturn(rc2, rc2);
4746 break;
4747 }
4748
4749#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
4750 void *pv;
4751 rc = pgmRZDynMapHCPageInlined(pVCpu,
4752 PGM_PAGE_GET_HCPHYS(pPage),
4753 &pv
4754 RTLOG_COMMA_SRC_POS);
4755 if (RT_FAILURE(rc))
4756 return rc;
4757 *ppv = (void *)((uintptr_t)pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
4758 pLock->pvPage = pv;
4759 pLock->pVCpu = pVCpu;
4760
4761#else
4762 /* Get a ring-3 mapping of the address. */
4763 PPGMPAGER3MAPTLBE pTlbe;
4764 rc2 = pgmPhysPageQueryTlbeWithPage(pVM, pPage, GCPhys, &pTlbe);
4765 AssertLogRelRCReturn(rc2, rc2);
4766
4767 /* Lock it and calculate the address. */
4768 if (fWritable)
4769 pgmPhysPageMapLockForWriting(pVM, pPage, pTlbe, pLock);
4770 else
4771 pgmPhysPageMapLockForReading(pVM, pPage, pTlbe, pLock);
4772 *ppv = (void *)((uintptr_t)pTlbe->pv | (uintptr_t)(GCPhys & PAGE_OFFSET_MASK));
4773#endif
4774
4775 Log6(("PGMPhysIemGCPhys2Ptr: GCPhys=%RGp rc=%Rrc pPage=%R[pgmpage] *ppv=%p\n", GCPhys, rc, pPage, *ppv));
4776 }
4777 else
4778 Log6(("PGMPhysIemGCPhys2Ptr: GCPhys=%RGp rc=%Rrc pPage=%R[pgmpage]\n", GCPhys, rc, pPage));
4779
4780 /* else: handler catching all access, no pointer returned. */
4781 }
4782 else
4783 rc = VERR_PGM_PHYS_TLB_UNASSIGNED;
4784
4785 pgmUnlock(pVM);
4786 return rc;
4787}
4788
4789
4790/**
4791 * Checks if the give GCPhys page requires special handling for the given access
4792 * because it's MMIO or otherwise monitored.
4793 *
4794 * @returns VBox status code (no informational statuses).
4795 * @retval VINF_SUCCESS on success.
4796 * @retval VERR_PGM_PHYS_TLB_CATCH_WRITE and *ppv set if the page has a write
4797 * access handler of some kind.
4798 * @retval VERR_PGM_PHYS_TLB_CATCH_ALL if the page has a handler catching all
4799 * accesses or is odd in any way.
4800 * @retval VERR_PGM_PHYS_TLB_UNASSIGNED if the page doesn't exist.
4801 *
4802 * @param pVM The cross context VM structure.
4803 * @param GCPhys The GC physical address to convert. Since this is
4804 * only used for filling the REM TLB, the A20 mask must
4805 * be applied before calling this API.
4806 * @param fWritable Whether write access is required.
4807 * @param fByPassHandlers Whether to bypass access handlers.
4808 *
4809 * @remarks This is a watered down version PGMPhysIemGCPhys2Ptr and really just
4810 * a stop gap thing that should be removed once there is a better TLB
4811 * for virtual address accesses.
4812 */
4813VMM_INT_DECL(int) PGMPhysIemQueryAccess(PVM pVM, RTGCPHYS GCPhys, bool fWritable, bool fByPassHandlers)
4814{
4815 pgmLock(pVM);
4816 PGM_A20_ASSERT_MASKED(VMMGetCpu(pVM), GCPhys);
4817
4818 PPGMRAMRANGE pRam;
4819 PPGMPAGE pPage;
4820 int rc = pgmPhysGetPageAndRangeEx(pVM, GCPhys, &pPage, &pRam);
4821 if (RT_SUCCESS(rc))
4822 {
4823 if (PGM_PAGE_IS_BALLOONED(pPage))
4824 rc = VERR_PGM_PHYS_TLB_CATCH_WRITE;
4825 else if (PGM_PAGE_IS_SPECIAL_ALIAS_MMIO(pPage))
4826 rc = VERR_PGM_PHYS_TLB_CATCH_ALL;
4827 else if ( !PGM_PAGE_HAS_ANY_HANDLERS(pPage)
4828 || (fByPassHandlers && !PGM_PAGE_IS_MMIO(pPage)) )
4829 rc = VINF_SUCCESS;
4830 else
4831 {
4832 if (PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage)) /* catches MMIO */
4833 {
4834 Assert(!fByPassHandlers || PGM_PAGE_IS_MMIO(pPage));
4835 rc = VERR_PGM_PHYS_TLB_CATCH_ALL;
4836 }
4837 else if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage) && fWritable)
4838 {
4839 Assert(!fByPassHandlers);
4840 rc = VERR_PGM_PHYS_TLB_CATCH_WRITE;
4841 }
4842 }
4843 }
4844
4845 pgmUnlock(pVM);
4846 return rc;
4847}
4848
4849#ifndef IN_RC
4850
4851/**
4852 * Interface used by NEM to check what to do on a memory access exit.
4853 *
4854 * @returns VBox status code.
4855 * @param pVM The cross context VM structure.
4856 * @param pVCpu The cross context per virtual CPU structure.
4857 * Optional.
4858 * @param GCPhys The guest physical address.
4859 * @param fMakeWritable Whether to try make the page writable or not. If it
4860 * cannot be made writable, NEM_PAGE_PROT_WRITE won't
4861 * be returned and the return code will be unaffected
4862 * @param pInfo Where to return the page information. This is
4863 * initialized even on failure.
4864 * @param pfnChecker Page in-sync checker callback. Optional.
4865 * @param pvUser User argument to pass to pfnChecker.
4866 */
4867VMM_INT_DECL(int) PGMPhysNemPageInfoChecker(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, bool fMakeWritable, PPGMPHYSNEMPAGEINFO pInfo,
4868 PFNPGMPHYSNEMCHECKPAGE pfnChecker, void *pvUser)
4869{
4870 pgmLock(pVM);
4871
4872 PPGMPAGE pPage;
4873 int rc = pgmPhysGetPageEx(pVM, GCPhys, &pPage);
4874 if (RT_SUCCESS(rc))
4875 {
4876 /* Try make it writable if requested. */
4877 pInfo->u2OldNemState = PGM_PAGE_GET_NEM_STATE(pPage);
4878 if (fMakeWritable)
4879 switch (PGM_PAGE_GET_STATE(pPage))
4880 {
4881 case PGM_PAGE_STATE_SHARED:
4882 case PGM_PAGE_STATE_WRITE_MONITORED:
4883 case PGM_PAGE_STATE_ZERO:
4884 rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
4885 if (rc == VERR_PGM_PHYS_PAGE_RESERVED)
4886 rc = VINF_SUCCESS;
4887 break;
4888 }
4889
4890 /* Fill in the info. */
4891 pInfo->HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
4892 pInfo->u2NemState = PGM_PAGE_GET_NEM_STATE(pPage);
4893 pInfo->fHasHandlers = PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage) ? 1 : 0;
4894 PGMPAGETYPE const enmType = (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage);
4895 pInfo->enmType = enmType;
4896 pInfo->fNemProt = pgmPhysPageCalcNemProtection(pPage, enmType);
4897 switch (PGM_PAGE_GET_STATE(pPage))
4898 {
4899 case PGM_PAGE_STATE_ALLOCATED:
4900 pInfo->fZeroPage = 0;
4901 break;
4902
4903 case PGM_PAGE_STATE_ZERO:
4904 pInfo->fZeroPage = 1;
4905 break;
4906
4907 case PGM_PAGE_STATE_WRITE_MONITORED:
4908 pInfo->fZeroPage = 0;
4909 break;
4910
4911 case PGM_PAGE_STATE_SHARED:
4912 pInfo->fZeroPage = 0;
4913 break;
4914
4915 case PGM_PAGE_STATE_BALLOONED:
4916 pInfo->fZeroPage = 1;
4917 break;
4918
4919 default:
4920 pInfo->fZeroPage = 1;
4921 AssertFailedStmt(rc = VERR_PGM_PHYS_PAGE_GET_IPE);
4922 }
4923
4924 /* Call the checker and update NEM state. */
4925 if (pfnChecker)
4926 {
4927 rc = pfnChecker(pVM, pVCpu, GCPhys, pInfo, pvUser);
4928 PGM_PAGE_SET_NEM_STATE(pPage, pInfo->u2NemState);
4929 }
4930
4931 /* Done. */
4932 pgmUnlock(pVM);
4933 }
4934 else
4935 {
4936 pgmUnlock(pVM);
4937
4938 pInfo->HCPhys = NIL_RTHCPHYS;
4939 pInfo->fNemProt = NEM_PAGE_PROT_NONE;
4940 pInfo->u2NemState = 0;
4941 pInfo->fHasHandlers = 0;
4942 pInfo->fZeroPage = 0;
4943 pInfo->enmType = PGMPAGETYPE_INVALID;
4944 }
4945
4946 return rc;
4947}
4948
4949
4950/**
4951 * NEM helper that performs @a pfnCallback on pages with NEM state @a uMinState
4952 * or higher.
4953 *
4954 * @returns VBox status code from callback.
4955 * @param pVM The cross context VM structure.
4956 * @param pVCpu The cross context per CPU structure. This is
4957 * optional as its only for passing to callback.
4958 * @param uMinState The minimum NEM state value to call on.
4959 * @param pfnCallback The callback function.
4960 * @param pvUser User argument for the callback.
4961 */
4962VMM_INT_DECL(int) PGMPhysNemEnumPagesByState(PVM pVM, PVMCPU pVCpu, uint8_t uMinState,
4963 PFNPGMPHYSNEMENUMCALLBACK pfnCallback, void *pvUser)
4964{
4965 /*
4966 * Just brute force this problem.
4967 */
4968 pgmLock(pVM);
4969 int rc = VINF_SUCCESS;
4970 for (PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRangesX); pRam; pRam = pRam->CTX_SUFF(pNext))
4971 {
4972 uint32_t const cPages = pRam->cb >> X86_PAGE_SHIFT;
4973 for (uint32_t iPage = 0; iPage < cPages; iPage++)
4974 {
4975 uint8_t u2State = PGM_PAGE_GET_NEM_STATE(&pRam->aPages[iPage]);
4976 if (u2State < uMinState)
4977 { /* likely */ }
4978 else
4979 {
4980 rc = pfnCallback(pVM, pVCpu, pRam->GCPhys + ((RTGCPHYS)iPage << X86_PAGE_SHIFT), &u2State, pvUser);
4981 if (RT_SUCCESS(rc))
4982 PGM_PAGE_SET_NEM_STATE(&pRam->aPages[iPage], u2State);
4983 else
4984 break;
4985 }
4986 }
4987 }
4988 pgmUnlock(pVM);
4989
4990 return rc;
4991
4992}
4993
4994#endif /* !IN_RC */
4995
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