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