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