VirtualBox

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

Last change on this file since 81150 was 81150, checked in by vboxsync, 5 years ago

VMM,/Makefile.kmk: Kicked out more recompiler related code. bugref:9576

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