VirtualBox

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

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

VMM: Kicking out raw-mode - PGM Virt Handlers, ++. bugref:9517

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