1 | /* $Id: PGMAllPhys.cpp 18207 2009-03-24 16:55:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PGM - Page Manager and Monitor, Physical Memory Addressing.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Defined Constants And Macros *
|
---|
24 | *******************************************************************************/
|
---|
25 | /** @def PGM_IGNORE_RAM_FLAGS_RESERVED
|
---|
26 | * Don't respect the MM_RAM_FLAGS_RESERVED flag when converting to HC addresses.
|
---|
27 | *
|
---|
28 | * Since this flag is currently incorrectly kept set for ROM regions we will
|
---|
29 | * have to ignore it for now so we don't break stuff.
|
---|
30 | *
|
---|
31 | * @todo this has been fixed now I believe, remove this hack.
|
---|
32 | */
|
---|
33 | #define PGM_IGNORE_RAM_FLAGS_RESERVED
|
---|
34 |
|
---|
35 |
|
---|
36 | /*******************************************************************************
|
---|
37 | * Header Files *
|
---|
38 | *******************************************************************************/
|
---|
39 | #define LOG_GROUP LOG_GROUP_PGM_PHYS
|
---|
40 | #include <VBox/pgm.h>
|
---|
41 | #include <VBox/trpm.h>
|
---|
42 | #include <VBox/vmm.h>
|
---|
43 | #include <VBox/iom.h>
|
---|
44 | #include <VBox/em.h>
|
---|
45 | #include <VBox/rem.h>
|
---|
46 | #include "PGMInternal.h"
|
---|
47 | #include <VBox/vm.h>
|
---|
48 | #include <VBox/param.h>
|
---|
49 | #include <VBox/err.h>
|
---|
50 | #include <iprt/assert.h>
|
---|
51 | #include <iprt/string.h>
|
---|
52 | #include <iprt/asm.h>
|
---|
53 | #include <VBox/log.h>
|
---|
54 | #ifdef IN_RING3
|
---|
55 | # include <iprt/thread.h>
|
---|
56 | #endif
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 | #ifndef IN_RING3
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * \#PF Handler callback for Guest ROM range write access.
|
---|
64 | * We simply ignore the writes or fall back to the recompiler if we don't support the instruction.
|
---|
65 | *
|
---|
66 | * @returns VBox status code (appropritate for trap handling and GC return).
|
---|
67 | * @param pVM VM Handle.
|
---|
68 | * @param uErrorCode CPU Error code.
|
---|
69 | * @param pRegFrame Trap register frame.
|
---|
70 | * @param pvFault The fault address (cr2).
|
---|
71 | * @param GCPhysFault The GC physical address corresponding to pvFault.
|
---|
72 | * @param pvUser User argument. Pointer to the ROM range structure.
|
---|
73 | */
|
---|
74 | VMMDECL(int) pgmPhysRomWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
|
---|
75 | {
|
---|
76 | int rc;
|
---|
77 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
78 | PPGMROMRANGE pRom = (PPGMROMRANGE)pvUser;
|
---|
79 | uint32_t iPage = (GCPhysFault - pRom->GCPhys) >> PAGE_SHIFT;
|
---|
80 | Assert(iPage < (pRom->cb >> PAGE_SHIFT));
|
---|
81 | switch (pRom->aPages[iPage].enmProt)
|
---|
82 | {
|
---|
83 | case PGMROMPROT_READ_ROM_WRITE_IGNORE:
|
---|
84 | case PGMROMPROT_READ_RAM_WRITE_IGNORE:
|
---|
85 | {
|
---|
86 | #endif
|
---|
87 | /*
|
---|
88 | * If it's a simple instruction which doesn't change the cpu state
|
---|
89 | * we will simply skip it. Otherwise we'll have to defer it to REM.
|
---|
90 | */
|
---|
91 | uint32_t cbOp;
|
---|
92 | DISCPUSTATE Cpu;
|
---|
93 | rc = EMInterpretDisasOne(pVM, pRegFrame, &Cpu, &cbOp);
|
---|
94 | if ( RT_SUCCESS(rc)
|
---|
95 | && Cpu.mode == CPUMODE_32BIT /** @todo why does this matter? */
|
---|
96 | && !(Cpu.prefix & (PREFIX_REPNE | PREFIX_REP | PREFIX_SEG)))
|
---|
97 | {
|
---|
98 | switch (Cpu.opcode)
|
---|
99 | {
|
---|
100 | /** @todo Find other instructions we can safely skip, possibly
|
---|
101 | * adding this kind of detection to DIS or EM. */
|
---|
102 | case OP_MOV:
|
---|
103 | pRegFrame->rip += cbOp;
|
---|
104 | STAM_COUNTER_INC(&pVM->pgm.s.StatRZGuestROMWriteHandled);
|
---|
105 | return VINF_SUCCESS;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | else if (RT_UNLIKELY(rc == VERR_INTERNAL_ERROR))
|
---|
109 | return rc;
|
---|
110 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
111 | break;
|
---|
112 | }
|
---|
113 |
|
---|
114 | case PGMROMPROT_READ_RAM_WRITE_RAM:
|
---|
115 | rc = PGMHandlerPhysicalPageTempOff(pVM, pRom->GCPhys, GCPhysFault & X86_PTE_PG_MASK);
|
---|
116 | AssertRC(rc);
|
---|
117 | break; /** @todo Must edit the shadow PT and restart the instruction, not use the interpreter! */
|
---|
118 |
|
---|
119 | case PGMROMPROT_READ_ROM_WRITE_RAM:
|
---|
120 | /* Handle it in ring-3 because it's *way* easier there. */
|
---|
121 | break;
|
---|
122 |
|
---|
123 | default:
|
---|
124 | AssertMsgFailedReturn(("enmProt=%d iPage=%d GCPhysFault=%RGp\n",
|
---|
125 | pRom->aPages[iPage].enmProt, iPage, GCPhysFault),
|
---|
126 | VERR_INTERNAL_ERROR);
|
---|
127 | }
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | STAM_COUNTER_INC(&pVM->pgm.s.StatRZGuestROMWriteUnhandled);
|
---|
131 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
132 | }
|
---|
133 |
|
---|
134 | #endif /* IN_RING3 */
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Checks if Address Gate 20 is enabled or not.
|
---|
138 | *
|
---|
139 | * @returns true if enabled.
|
---|
140 | * @returns false if disabled.
|
---|
141 | * @param pVM VM handle.
|
---|
142 | */
|
---|
143 | VMMDECL(bool) PGMPhysIsA20Enabled(PVM pVM)
|
---|
144 | {
|
---|
145 | LogFlow(("PGMPhysIsA20Enabled %d\n", pVM->pgm.s.fA20Enabled));
|
---|
146 | return pVM->pgm.s.fA20Enabled;
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Validates a GC physical address.
|
---|
152 | *
|
---|
153 | * @returns true if valid.
|
---|
154 | * @returns false if invalid.
|
---|
155 | * @param pVM The VM handle.
|
---|
156 | * @param GCPhys The physical address to validate.
|
---|
157 | */
|
---|
158 | VMMDECL(bool) PGMPhysIsGCPhysValid(PVM pVM, RTGCPHYS GCPhys)
|
---|
159 | {
|
---|
160 | PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
|
---|
161 | return pPage != NULL;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Checks if a GC physical address is a normal page,
|
---|
167 | * i.e. not ROM, MMIO or reserved.
|
---|
168 | *
|
---|
169 | * @returns true if normal.
|
---|
170 | * @returns false if invalid, ROM, MMIO or reserved page.
|
---|
171 | * @param pVM The VM handle.
|
---|
172 | * @param GCPhys The physical address to check.
|
---|
173 | */
|
---|
174 | VMMDECL(bool) PGMPhysIsGCPhysNormal(PVM pVM, RTGCPHYS GCPhys)
|
---|
175 | {
|
---|
176 | PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
|
---|
177 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
178 | return pPage
|
---|
179 | && PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM;
|
---|
180 | #else
|
---|
181 | return pPage
|
---|
182 | && !(pPage->HCPhys & (MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO2));
|
---|
183 | #endif
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Converts a GC physical address to a HC physical address.
|
---|
189 | *
|
---|
190 | * @returns VINF_SUCCESS on success.
|
---|
191 | * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
|
---|
192 | * page but has no physical backing.
|
---|
193 | * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
|
---|
194 | * GC physical address.
|
---|
195 | *
|
---|
196 | * @param pVM The VM handle.
|
---|
197 | * @param GCPhys The GC physical address to convert.
|
---|
198 | * @param pHCPhys Where to store the HC physical address on success.
|
---|
199 | */
|
---|
200 | VMMDECL(int) PGMPhysGCPhys2HCPhys(PVM pVM, RTGCPHYS GCPhys, PRTHCPHYS pHCPhys)
|
---|
201 | {
|
---|
202 | PPGMPAGE pPage;
|
---|
203 | int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhys, &pPage);
|
---|
204 | if (RT_FAILURE(rc))
|
---|
205 | return rc;
|
---|
206 |
|
---|
207 | #ifndef PGM_IGNORE_RAM_FLAGS_RESERVED
|
---|
208 | if (RT_UNLIKELY(pPage->HCPhys & MM_RAM_FLAGS_RESERVED)) /** @todo PAGE FLAGS */
|
---|
209 | return VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
210 | #endif
|
---|
211 |
|
---|
212 | *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK);
|
---|
213 | return VINF_SUCCESS;
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Invalidates the GC page mapping TLB.
|
---|
219 | *
|
---|
220 | * @param pVM The VM handle.
|
---|
221 | */
|
---|
222 | VMMDECL(void) PGMPhysInvalidatePageGCMapTLB(PVM pVM)
|
---|
223 | {
|
---|
224 | /* later */
|
---|
225 | NOREF(pVM);
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Invalidates the ring-0 page mapping TLB.
|
---|
231 | *
|
---|
232 | * @param pVM The VM handle.
|
---|
233 | */
|
---|
234 | VMMDECL(void) PGMPhysInvalidatePageR0MapTLB(PVM pVM)
|
---|
235 | {
|
---|
236 | PGMPhysInvalidatePageR3MapTLB(pVM);
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Invalidates the ring-3 page mapping TLB.
|
---|
242 | *
|
---|
243 | * @param pVM The VM handle.
|
---|
244 | */
|
---|
245 | VMMDECL(void) PGMPhysInvalidatePageR3MapTLB(PVM pVM)
|
---|
246 | {
|
---|
247 | pgmLock(pVM);
|
---|
248 | for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.PhysTlbHC.aEntries); i++)
|
---|
249 | {
|
---|
250 | pVM->pgm.s.PhysTlbHC.aEntries[i].GCPhys = NIL_RTGCPHYS;
|
---|
251 | pVM->pgm.s.PhysTlbHC.aEntries[i].pPage = 0;
|
---|
252 | pVM->pgm.s.PhysTlbHC.aEntries[i].pMap = 0;
|
---|
253 | pVM->pgm.s.PhysTlbHC.aEntries[i].pv = 0;
|
---|
254 | }
|
---|
255 | pgmUnlock(pVM);
|
---|
256 | }
|
---|
257 |
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * Makes sure that there is at least one handy page ready for use.
|
---|
261 | *
|
---|
262 | * This will also take the appropriate actions when reaching water-marks.
|
---|
263 | *
|
---|
264 | * @returns The following VBox status codes.
|
---|
265 | * @retval VINF_SUCCESS on success.
|
---|
266 | * @retval VERR_EM_NO_MEMORY if we're really out of memory.
|
---|
267 | *
|
---|
268 | * @param pVM The VM handle.
|
---|
269 | *
|
---|
270 | * @remarks Must be called from within the PGM critical section. It may
|
---|
271 | * nip back to ring-3/0 in some cases.
|
---|
272 | */
|
---|
273 | static int pgmPhysEnsureHandyPage(PVM pVM)
|
---|
274 | {
|
---|
275 | /** @remarks
|
---|
276 | * low-water mark logic for R0 & GC:
|
---|
277 | * - 75%: Set FF.
|
---|
278 | * - 50%: Force return to ring-3 ASAP.
|
---|
279 | *
|
---|
280 | * For ring-3 there is a little problem wrt to the recompiler, so:
|
---|
281 | * - 75%: Set FF.
|
---|
282 | * - 50%: Try allocate pages; on failure we'll force REM to quite ASAP.
|
---|
283 | *
|
---|
284 | * The basic idea is that we should be able to get out of any situation with
|
---|
285 | * only 50% of handy pages remaining.
|
---|
286 | *
|
---|
287 | * At the moment we'll not adjust the number of handy pages relative to the
|
---|
288 | * actual VM RAM committment, that's too much work for now.
|
---|
289 | */
|
---|
290 | Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect));
|
---|
291 | AssertMsg(pVM->pgm.s.cHandyPages <= RT_ELEMENTS(pVM->pgm.s.aHandyPages), ("%d\n", pVM->pgm.s.cHandyPages));
|
---|
292 | if ( !pVM->pgm.s.cHandyPages
|
---|
293 | #ifdef IN_RING3
|
---|
294 | || pVM->pgm.s.cHandyPages - 1 <= RT_ELEMENTS(pVM->pgm.s.aHandyPages) / 2 /* 50% */
|
---|
295 | #endif
|
---|
296 | )
|
---|
297 | {
|
---|
298 | Log(("PGM: cHandyPages=%u out of %u -> allocate more\n", pVM->pgm.s.cHandyPages, RT_ELEMENTS(pVM->pgm.s.aHandyPages)));
|
---|
299 | #ifdef IN_RING3
|
---|
300 | int rc = PGMR3PhysAllocateHandyPages(pVM);
|
---|
301 | #elif defined(IN_RING0)
|
---|
302 | int rc = VMMR0CallHost(pVM, VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES, 0);
|
---|
303 | #else
|
---|
304 | int rc = VMMGCCallHost(pVM, VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES, 0);
|
---|
305 | #endif
|
---|
306 | if (RT_UNLIKELY(rc != VINF_SUCCESS))
|
---|
307 | {
|
---|
308 | AssertMsg(rc == VINF_EM_NO_MEMORY || rc == VERR_EM_NO_MEMORY, ("%Rrc\n", rc));
|
---|
309 | if (!pVM->pgm.s.cHandyPages)
|
---|
310 | {
|
---|
311 | LogRel(("PGM: no more handy pages!\n"));
|
---|
312 | return VERR_EM_NO_MEMORY;
|
---|
313 | }
|
---|
314 | Assert(VM_FF_ISSET(pVM, VM_FF_PGM_NEED_HANDY_PAGES));
|
---|
315 | #ifdef IN_RING3
|
---|
316 | REMR3NotifyFF(pVM);
|
---|
317 | #else
|
---|
318 | VM_FF_SET(pVM, VM_FF_TO_R3);
|
---|
319 | #endif
|
---|
320 | }
|
---|
321 | AssertMsgReturn( pVM->pgm.s.cHandyPages > 0
|
---|
322 | && pVM->pgm.s.cHandyPages <= RT_ELEMENTS(pVM->pgm.s.aHandyPages),
|
---|
323 | ("%u\n", pVM->pgm.s.cHandyPages),
|
---|
324 | VERR_INTERNAL_ERROR);
|
---|
325 | }
|
---|
326 | else if (pVM->pgm.s.cHandyPages - 1 <= (RT_ELEMENTS(pVM->pgm.s.aHandyPages) / 4) * 3) /* 75% */
|
---|
327 | {
|
---|
328 | VM_FF_SET(pVM, VM_FF_PGM_NEED_HANDY_PAGES);
|
---|
329 | #ifndef IN_RING3
|
---|
330 | if (pVM->pgm.s.cHandyPages - 1 <= RT_ELEMENTS(pVM->pgm.s.aHandyPages) / 2)
|
---|
331 | {
|
---|
332 | Log(("PGM: VM_FF_TO_R3 - cHandyPages=%u out of %u\n", pVM->pgm.s.cHandyPages - 1, RT_ELEMENTS(pVM->pgm.s.aHandyPages)));
|
---|
333 | VM_FF_SET(pVM, VM_FF_TO_R3);
|
---|
334 | }
|
---|
335 | #endif
|
---|
336 | }
|
---|
337 |
|
---|
338 | return VINF_SUCCESS;
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Replace a zero or shared page with new page that we can write to.
|
---|
344 | *
|
---|
345 | * @returns The following VBox status codes.
|
---|
346 | * @retval VINF_SUCCESS on success, pPage is modified.
|
---|
347 | * @retval VINF_PGM_SYNC_CR3 on success and a page pool flush is pending.
|
---|
348 | * @retval VERR_EM_NO_MEMORY if we're totally out of memory.
|
---|
349 | *
|
---|
350 | * @todo Propagate VERR_EM_NO_MEMORY up the call tree.
|
---|
351 | *
|
---|
352 | * @param pVM The VM address.
|
---|
353 | * @param pPage The physical page tracking structure. This will
|
---|
354 | * be modified on success.
|
---|
355 | * @param GCPhys The address of the page.
|
---|
356 | *
|
---|
357 | * @remarks Must be called from within the PGM critical section. It may
|
---|
358 | * nip back to ring-3/0 in some cases.
|
---|
359 | *
|
---|
360 | * @remarks This function shouldn't really fail, however if it does
|
---|
361 | * it probably means we've screwed up the size of the amount
|
---|
362 | * and/or the low-water mark of handy pages. Or, that some
|
---|
363 | * device I/O is causing a lot of pages to be allocated while
|
---|
364 | * while the host is in a low-memory condition.
|
---|
365 | */
|
---|
366 | int pgmPhysAllocPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
|
---|
367 | {
|
---|
368 | LogFlow(("pgmPhysAllocPage: %R[pgmpage] %RGp\n", pPage, GCPhys));
|
---|
369 |
|
---|
370 | /*
|
---|
371 | * Prereqs.
|
---|
372 | */
|
---|
373 | Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect));
|
---|
374 | AssertMsg(PGM_PAGE_IS_ZERO(pPage) || PGM_PAGE_IS_SHARED(pPage), ("%R[pgmpage] %RGp\n", pPage, GCPhys));
|
---|
375 | Assert(!PGM_PAGE_IS_MMIO(pPage));
|
---|
376 |
|
---|
377 |
|
---|
378 | /*
|
---|
379 | * Flush any shadow page table mappings of the page.
|
---|
380 | * When VBOX_WITH_NEW_LAZY_PAGE_ALLOC isn't defined, there shouldn't be any.
|
---|
381 | */
|
---|
382 | bool fFlushTLBs = false;
|
---|
383 | int rc = pgmPoolTrackFlushGCPhys(pVM, pPage, &fFlushTLBs);
|
---|
384 | if (rc == VINF_SUCCESS)
|
---|
385 | /* nothing */;
|
---|
386 | else if (rc == VINF_PGM_GCPHYS_ALIASED)
|
---|
387 | {
|
---|
388 | pVM->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
|
---|
389 | VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
|
---|
390 | rc = VINF_PGM_SYNC_CR3;
|
---|
391 | }
|
---|
392 | else
|
---|
393 | {
|
---|
394 | AssertRCReturn(rc, rc);
|
---|
395 | AssertMsgFailedReturn(("%Rrc\n", rc), VERR_INTERNAL_ERROR);
|
---|
396 | }
|
---|
397 |
|
---|
398 | /*
|
---|
399 | * Ensure that we've got a page handy, take it and use it.
|
---|
400 | */
|
---|
401 | int rc2 = pgmPhysEnsureHandyPage(pVM);
|
---|
402 | if (RT_FAILURE(rc2))
|
---|
403 | {
|
---|
404 | if (fFlushTLBs)
|
---|
405 | PGM_INVL_GUEST_TLBS();
|
---|
406 | Assert(rc2 == VERR_EM_NO_MEMORY);
|
---|
407 | return rc2;
|
---|
408 | }
|
---|
409 | /* re-assert preconditions since pgmPhysEnsureHandyPage may do a context switch. */
|
---|
410 | Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect));
|
---|
411 | AssertMsg(PGM_PAGE_IS_ZERO(pPage) || PGM_PAGE_IS_SHARED(pPage), ("%R[pgmpage] %RGp\n", pPage, GCPhys));
|
---|
412 | Assert(!PGM_PAGE_IS_MMIO(pPage));
|
---|
413 |
|
---|
414 | uint32_t iHandyPage = --pVM->pgm.s.cHandyPages;
|
---|
415 | AssertMsg(iHandyPage < RT_ELEMENTS(pVM->pgm.s.aHandyPages), ("%d\n", iHandyPage));
|
---|
416 | Assert(pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys != NIL_RTHCPHYS);
|
---|
417 | Assert(!(pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
|
---|
418 | Assert(pVM->pgm.s.aHandyPages[iHandyPage].idPage != NIL_GMM_PAGEID);
|
---|
419 | Assert(pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage == NIL_GMM_PAGEID);
|
---|
420 |
|
---|
421 | /*
|
---|
422 | * There are one or two action to be taken the next time we allocate handy pages:
|
---|
423 | * - Tell the GMM (global memory manager) what the page is being used for.
|
---|
424 | * (Speeds up replacement operations - sharing and defragmenting.)
|
---|
425 | * - If the current backing is shared, it must be freed.
|
---|
426 | */
|
---|
427 | const RTHCPHYS HCPhys = pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys;
|
---|
428 | pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys = GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK;
|
---|
429 |
|
---|
430 | if (PGM_PAGE_IS_SHARED(pPage))
|
---|
431 | {
|
---|
432 | pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage = PGM_PAGE_GET_PAGEID(pPage);
|
---|
433 | Assert(PGM_PAGE_GET_PAGEID(pPage) != NIL_GMM_PAGEID);
|
---|
434 | VM_FF_SET(pVM, VM_FF_PGM_NEED_HANDY_PAGES);
|
---|
435 |
|
---|
436 | Log2(("PGM: Replaced shared page %#x at %RGp with %#x / %RHp\n", PGM_PAGE_GET_PAGEID(pPage),
|
---|
437 | GCPhys, pVM->pgm.s.aHandyPages[iHandyPage].idPage, HCPhys));
|
---|
438 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,PageReplaceShared));
|
---|
439 | pVM->pgm.s.cSharedPages--;
|
---|
440 | AssertMsgFailed(("TODO: copy shared page content")); /** @todo err.. what about copying the page content? */
|
---|
441 | }
|
---|
442 | else
|
---|
443 | {
|
---|
444 | Log2(("PGM: Replaced zero page %RGp with %#x / %RHp\n", GCPhys, pVM->pgm.s.aHandyPages[iHandyPage].idPage, HCPhys));
|
---|
445 | STAM_COUNTER_INC(&pVM->pgm.s.StatRZPageReplaceZero);
|
---|
446 | pVM->pgm.s.cZeroPages--;
|
---|
447 | Assert(pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage == NIL_GMM_PAGEID);
|
---|
448 | }
|
---|
449 |
|
---|
450 | /*
|
---|
451 | * Do the PGMPAGE modifications.
|
---|
452 | */
|
---|
453 | pVM->pgm.s.cPrivatePages++;
|
---|
454 | PGM_PAGE_SET_HCPHYS(pPage, HCPhys);
|
---|
455 | PGM_PAGE_SET_PAGEID(pPage, pVM->pgm.s.aHandyPages[iHandyPage].idPage);
|
---|
456 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
|
---|
457 |
|
---|
458 | if ( fFlushTLBs
|
---|
459 | && rc != VINF_PGM_GCPHYS_ALIASED)
|
---|
460 | PGM_INVL_GUEST_TLBS();
|
---|
461 | return rc;
|
---|
462 | }
|
---|
463 |
|
---|
464 |
|
---|
465 | /**
|
---|
466 | * Deal with pages that are not writable, i.e. not in the ALLOCATED state.
|
---|
467 | *
|
---|
468 | * @returns VBox status code.
|
---|
469 | * @retval VINF_SUCCESS on success.
|
---|
470 | * @retval VINF_PGM_SYNC_CR3 on success and a page pool flush is pending.
|
---|
471 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
472 | *
|
---|
473 | * @param pVM The VM address.
|
---|
474 | * @param pPage The physical page tracking structure.
|
---|
475 | * @param GCPhys The address of the page.
|
---|
476 | *
|
---|
477 | * @remarks Called from within the PGM critical section.
|
---|
478 | */
|
---|
479 | int pgmPhysPageMakeWritable(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
|
---|
480 | {
|
---|
481 | switch (PGM_PAGE_GET_STATE(pPage))
|
---|
482 | {
|
---|
483 | case PGM_PAGE_STATE_WRITE_MONITORED:
|
---|
484 | PGM_PAGE_SET_WRITTEN_TO(pPage);
|
---|
485 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
|
---|
486 | /* fall thru */
|
---|
487 | default: /* to shut up GCC */
|
---|
488 | case PGM_PAGE_STATE_ALLOCATED:
|
---|
489 | return VINF_SUCCESS;
|
---|
490 |
|
---|
491 | /*
|
---|
492 | * Zero pages can be dummy pages for MMIO or reserved memory,
|
---|
493 | * so we need to check the flags before joining cause with
|
---|
494 | * shared page replacement.
|
---|
495 | */
|
---|
496 | case PGM_PAGE_STATE_ZERO:
|
---|
497 | if (PGM_PAGE_IS_MMIO(pPage))
|
---|
498 | return VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
499 | /* fall thru */
|
---|
500 | case PGM_PAGE_STATE_SHARED:
|
---|
501 | return pgmPhysAllocPage(pVM, pPage, GCPhys);
|
---|
502 | }
|
---|
503 | }
|
---|
504 |
|
---|
505 |
|
---|
506 | /**
|
---|
507 | * Wrapper for pgmPhysPageMakeWritable which enters the critsect.
|
---|
508 | *
|
---|
509 | * @returns VBox status code.
|
---|
510 | * @retval VINF_SUCCESS on success.
|
---|
511 | * @retval VINF_PGM_SYNC_CR3 on success and a page pool flush is pending.
|
---|
512 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
513 | *
|
---|
514 | * @param pVM The VM address.
|
---|
515 | * @param pPage The physical page tracking structure.
|
---|
516 | * @param GCPhys The address of the page.
|
---|
517 | */
|
---|
518 | int pgmPhysPageMakeWritableUnlocked(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
|
---|
519 | {
|
---|
520 | int rc = pgmLock(pVM);
|
---|
521 | if (RT_SUCCESS(rc))
|
---|
522 | {
|
---|
523 | rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
|
---|
524 | pgmUnlock(pVM);
|
---|
525 | }
|
---|
526 | return rc;
|
---|
527 | }
|
---|
528 |
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * Internal usage: Map the page specified by its GMM ID.
|
---|
532 | *
|
---|
533 | * This is similar to pgmPhysPageMap
|
---|
534 | *
|
---|
535 | * @returns VBox status code.
|
---|
536 | *
|
---|
537 | * @param pVM The VM handle.
|
---|
538 | * @param idPage The Page ID.
|
---|
539 | * @param HCPhys The physical address (for RC).
|
---|
540 | * @param ppv Where to store the mapping address.
|
---|
541 | *
|
---|
542 | * @remarks Called from within the PGM critical section.
|
---|
543 | */
|
---|
544 | int pgmPhysPageMapByPageID(PVM pVM, uint32_t idPage, RTHCPHYS HCPhys, void **ppv)
|
---|
545 | {
|
---|
546 | /*
|
---|
547 | * Validation.
|
---|
548 | */
|
---|
549 | Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect));
|
---|
550 | AssertReturn(HCPhys && !(HCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
551 | const uint32_t idChunk = idPage >> GMM_CHUNKID_SHIFT;
|
---|
552 | AssertReturn(idChunk != NIL_GMM_CHUNKID, VERR_INVALID_PARAMETER);
|
---|
553 |
|
---|
554 | #ifdef IN_RC
|
---|
555 | /*
|
---|
556 | * Map it by HCPhys.
|
---|
557 | */
|
---|
558 | return PGMDynMapHCPage(pVM, HCPhys, ppv);
|
---|
559 |
|
---|
560 | #elif defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
561 | /*
|
---|
562 | * Map it by HCPhys.
|
---|
563 | */
|
---|
564 | return pgmR0DynMapHCPageInlined(&pVM->pgm.s, HCPhys, ppv);
|
---|
565 |
|
---|
566 | #else
|
---|
567 | /*
|
---|
568 | * Find/make Chunk TLB entry for the mapping chunk.
|
---|
569 | */
|
---|
570 | PPGMCHUNKR3MAP pMap;
|
---|
571 | PPGMCHUNKR3MAPTLBE pTlbe = &pVM->pgm.s.ChunkR3Map.Tlb.aEntries[PGM_CHUNKR3MAPTLB_IDX(idChunk)];
|
---|
572 | if (pTlbe->idChunk == idChunk)
|
---|
573 | {
|
---|
574 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbHits));
|
---|
575 | pMap = pTlbe->pChunk;
|
---|
576 | }
|
---|
577 | else
|
---|
578 | {
|
---|
579 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbMisses));
|
---|
580 |
|
---|
581 | /*
|
---|
582 | * Find the chunk, map it if necessary.
|
---|
583 | */
|
---|
584 | pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
|
---|
585 | if (!pMap)
|
---|
586 | {
|
---|
587 | # ifdef IN_RING0
|
---|
588 | int rc = VMMR0CallHost(pVM, VMMCALLHOST_PGM_MAP_CHUNK, idChunk);
|
---|
589 | AssertRCReturn(rc, rc);
|
---|
590 | pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
|
---|
591 | Assert(pMap);
|
---|
592 | # else
|
---|
593 | int rc = pgmR3PhysChunkMap(pVM, idChunk, &pMap);
|
---|
594 | if (RT_FAILURE(rc))
|
---|
595 | return rc;
|
---|
596 | # endif
|
---|
597 | }
|
---|
598 |
|
---|
599 | /*
|
---|
600 | * Enter it into the Chunk TLB.
|
---|
601 | */
|
---|
602 | pTlbe->idChunk = idChunk;
|
---|
603 | pTlbe->pChunk = pMap;
|
---|
604 | pMap->iAge = 0;
|
---|
605 | }
|
---|
606 |
|
---|
607 | *ppv = (uint8_t *)pMap->pv + ((idPage &GMM_PAGEID_IDX_MASK) << PAGE_SHIFT);
|
---|
608 | return VINF_SUCCESS;
|
---|
609 | #endif
|
---|
610 | }
|
---|
611 |
|
---|
612 |
|
---|
613 | /**
|
---|
614 | * Maps a page into the current virtual address space so it can be accessed.
|
---|
615 | *
|
---|
616 | * @returns VBox status code.
|
---|
617 | * @retval VINF_SUCCESS on success.
|
---|
618 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
619 | *
|
---|
620 | * @param pVM The VM address.
|
---|
621 | * @param pPage The physical page tracking structure.
|
---|
622 | * @param GCPhys The address of the page.
|
---|
623 | * @param ppMap Where to store the address of the mapping tracking structure.
|
---|
624 | * @param ppv Where to store the mapping address of the page. The page
|
---|
625 | * offset is masked off!
|
---|
626 | *
|
---|
627 | * @remarks Called from within the PGM critical section.
|
---|
628 | */
|
---|
629 | int pgmPhysPageMap(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, PPPGMPAGEMAP ppMap, void **ppv)
|
---|
630 | {
|
---|
631 | Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect));
|
---|
632 |
|
---|
633 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
634 | /*
|
---|
635 | * Just some sketchy GC/R0-darwin code.
|
---|
636 | */
|
---|
637 | *ppMap = NULL;
|
---|
638 | RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
|
---|
639 | Assert(HCPhys != pVM->pgm.s.HCPhysZeroPg);
|
---|
640 | # ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
|
---|
641 | pgmR0DynMapHCPageInlined(&pVM->pgm.s, HCPhys, ppv);
|
---|
642 | # else
|
---|
643 | PGMDynMapHCPage(pVM, HCPhys, ppv);
|
---|
644 | # endif
|
---|
645 | return VINF_SUCCESS;
|
---|
646 |
|
---|
647 | #else /* IN_RING3 || IN_RING0 */
|
---|
648 |
|
---|
649 |
|
---|
650 | /*
|
---|
651 | * Special case: ZERO and MMIO2 pages.
|
---|
652 | */
|
---|
653 | const uint32_t idChunk = PGM_PAGE_GET_CHUNKID(pPage);
|
---|
654 | if (idChunk == NIL_GMM_CHUNKID)
|
---|
655 | {
|
---|
656 | AssertMsgReturn(PGM_PAGE_GET_PAGEID(pPage) == NIL_GMM_PAGEID, ("pPage=%R[pgmpage]\n", pPage), VERR_INTERNAL_ERROR);
|
---|
657 | if (PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2)
|
---|
658 | {
|
---|
659 | /* Lookup the MMIO2 range and use pvR3 to calc the address. */
|
---|
660 | PPGMRAMRANGE pRam = pgmPhysGetRange(&pVM->pgm.s, GCPhys);
|
---|
661 | AssertMsgReturn(pRam || !pRam->pvR3, ("pRam=%p pPage=%R[pgmpage]\n", pRam, pPage), VERR_INTERNAL_ERROR);
|
---|
662 | *ppv = (void *)((uintptr_t)pRam->pvR3 + (GCPhys - pRam->GCPhys));
|
---|
663 | }
|
---|
664 | else if (PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO)
|
---|
665 | {
|
---|
666 | /** @todo deal with aliased MMIO2 pages somehow...
|
---|
667 | * One solution would be to seed MMIO2 pages to GMM and get unique Page IDs for
|
---|
668 | * them, that would also avoid this mess. It would actually be kind of
|
---|
669 | * elegant... */
|
---|
670 | AssertFailedReturn(VERR_INTERNAL_ERROR);
|
---|
671 | }
|
---|
672 | else
|
---|
673 | {
|
---|
674 | /** @todo handle MMIO2 */
|
---|
675 | AssertMsgReturn(PGM_PAGE_IS_ZERO(pPage), ("pPage=%R[pgmpage]\n", pPage), VERR_INTERNAL_ERROR);
|
---|
676 | AssertMsgReturn(PGM_PAGE_GET_HCPHYS(pPage) == pVM->pgm.s.HCPhysZeroPg,
|
---|
677 | ("pPage=%R[pgmpage]\n", pPage),
|
---|
678 | VERR_INTERNAL_ERROR);
|
---|
679 | *ppv = pVM->pgm.s.CTXALLSUFF(pvZeroPg);
|
---|
680 | }
|
---|
681 | *ppMap = NULL;
|
---|
682 | return VINF_SUCCESS;
|
---|
683 | }
|
---|
684 |
|
---|
685 | /*
|
---|
686 | * Find/make Chunk TLB entry for the mapping chunk.
|
---|
687 | */
|
---|
688 | PPGMCHUNKR3MAP pMap;
|
---|
689 | PPGMCHUNKR3MAPTLBE pTlbe = &pVM->pgm.s.ChunkR3Map.Tlb.aEntries[PGM_CHUNKR3MAPTLB_IDX(idChunk)];
|
---|
690 | if (pTlbe->idChunk == idChunk)
|
---|
691 | {
|
---|
692 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbHits));
|
---|
693 | pMap = pTlbe->pChunk;
|
---|
694 | }
|
---|
695 | else
|
---|
696 | {
|
---|
697 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbMisses));
|
---|
698 |
|
---|
699 | /*
|
---|
700 | * Find the chunk, map it if necessary.
|
---|
701 | */
|
---|
702 | pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
|
---|
703 | if (!pMap)
|
---|
704 | {
|
---|
705 | #ifdef IN_RING0
|
---|
706 | int rc = VMMR0CallHost(pVM, VMMCALLHOST_PGM_MAP_CHUNK, idChunk);
|
---|
707 | AssertRCReturn(rc, rc);
|
---|
708 | pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
|
---|
709 | Assert(pMap);
|
---|
710 | #else
|
---|
711 | int rc = pgmR3PhysChunkMap(pVM, idChunk, &pMap);
|
---|
712 | if (RT_FAILURE(rc))
|
---|
713 | return rc;
|
---|
714 | #endif
|
---|
715 | }
|
---|
716 |
|
---|
717 | /*
|
---|
718 | * Enter it into the Chunk TLB.
|
---|
719 | */
|
---|
720 | pTlbe->idChunk = idChunk;
|
---|
721 | pTlbe->pChunk = pMap;
|
---|
722 | pMap->iAge = 0;
|
---|
723 | }
|
---|
724 |
|
---|
725 | *ppv = (uint8_t *)pMap->pv + (PGM_PAGE_GET_PAGE_IN_CHUNK(pPage) << PAGE_SHIFT);
|
---|
726 | *ppMap = pMap;
|
---|
727 | return VINF_SUCCESS;
|
---|
728 | #endif /* IN_RING3 */
|
---|
729 | }
|
---|
730 |
|
---|
731 |
|
---|
732 | #if !defined(IN_RC) && !defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
733 | /**
|
---|
734 | * Load a guest page into the ring-3 physical TLB.
|
---|
735 | *
|
---|
736 | * @returns VBox status code.
|
---|
737 | * @retval VINF_SUCCESS on success
|
---|
738 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
739 | * @param pPGM The PGM instance pointer.
|
---|
740 | * @param GCPhys The guest physical address in question.
|
---|
741 | */
|
---|
742 | int pgmPhysPageLoadIntoTlb(PPGM pPGM, RTGCPHYS GCPhys)
|
---|
743 | {
|
---|
744 | STAM_COUNTER_INC(&pPGM->CTX_MID_Z(Stat,PageMapTlbMisses));
|
---|
745 |
|
---|
746 | /*
|
---|
747 | * Find the ram range.
|
---|
748 | * 99.8% of requests are expected to be in the first range.
|
---|
749 | */
|
---|
750 | PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
|
---|
751 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
752 | if (RT_UNLIKELY(off >= pRam->cb))
|
---|
753 | {
|
---|
754 | do
|
---|
755 | {
|
---|
756 | pRam = pRam->CTX_SUFF(pNext);
|
---|
757 | if (!pRam)
|
---|
758 | return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
|
---|
759 | off = GCPhys - pRam->GCPhys;
|
---|
760 | } while (off >= pRam->cb);
|
---|
761 | }
|
---|
762 |
|
---|
763 | /*
|
---|
764 | * Map the page.
|
---|
765 | * Make a special case for the zero page as it is kind of special.
|
---|
766 | */
|
---|
767 | PPGMPAGE pPage = &pRam->aPages[off >> PAGE_SHIFT];
|
---|
768 | PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
|
---|
769 | if (!PGM_PAGE_IS_ZERO(pPage))
|
---|
770 | {
|
---|
771 | void *pv;
|
---|
772 | PPGMPAGEMAP pMap;
|
---|
773 | int rc = pgmPhysPageMap(PGM2VM(pPGM), pPage, GCPhys, &pMap, &pv);
|
---|
774 | if (RT_FAILURE(rc))
|
---|
775 | return rc;
|
---|
776 | pTlbe->pMap = pMap;
|
---|
777 | pTlbe->pv = pv;
|
---|
778 | }
|
---|
779 | else
|
---|
780 | {
|
---|
781 | Assert(PGM_PAGE_GET_HCPHYS(pPage) == pPGM->HCPhysZeroPg);
|
---|
782 | pTlbe->pMap = NULL;
|
---|
783 | pTlbe->pv = pPGM->CTXALLSUFF(pvZeroPg);
|
---|
784 | }
|
---|
785 | pTlbe->pPage = pPage;
|
---|
786 | return VINF_SUCCESS;
|
---|
787 | }
|
---|
788 |
|
---|
789 |
|
---|
790 | /**
|
---|
791 | * Load a guest page into the ring-3 physical TLB.
|
---|
792 | *
|
---|
793 | * @returns VBox status code.
|
---|
794 | * @retval VINF_SUCCESS on success
|
---|
795 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
796 | *
|
---|
797 | * @param pPGM The PGM instance pointer.
|
---|
798 | * @param pPage Pointer to the PGMPAGE structure corresponding to
|
---|
799 | * GCPhys.
|
---|
800 | * @param GCPhys The guest physical address in question.
|
---|
801 | */
|
---|
802 | int pgmPhysPageLoadIntoTlbWithPage(PPGM pPGM, PPGMPAGE pPage, RTGCPHYS GCPhys)
|
---|
803 | {
|
---|
804 | STAM_COUNTER_INC(&pPGM->CTX_MID_Z(Stat,PageMapTlbMisses));
|
---|
805 |
|
---|
806 | /*
|
---|
807 | * Map the page.
|
---|
808 | * Make a special case for the zero page as it is kind of special.
|
---|
809 | */
|
---|
810 | PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
|
---|
811 | if (!PGM_PAGE_IS_ZERO(pPage))
|
---|
812 | {
|
---|
813 | void *pv;
|
---|
814 | PPGMPAGEMAP pMap;
|
---|
815 | int rc = pgmPhysPageMap(PGM2VM(pPGM), pPage, GCPhys, &pMap, &pv);
|
---|
816 | if (RT_FAILURE(rc))
|
---|
817 | return rc;
|
---|
818 | pTlbe->pMap = pMap;
|
---|
819 | pTlbe->pv = pv;
|
---|
820 | }
|
---|
821 | else
|
---|
822 | {
|
---|
823 | Assert(PGM_PAGE_GET_HCPHYS(pPage) == pPGM->HCPhysZeroPg);
|
---|
824 | pTlbe->pMap = NULL;
|
---|
825 | pTlbe->pv = pPGM->CTXALLSUFF(pvZeroPg);
|
---|
826 | }
|
---|
827 | pTlbe->pPage = pPage;
|
---|
828 | return VINF_SUCCESS;
|
---|
829 | }
|
---|
830 | #endif /* !IN_RC && !VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
|
---|
831 |
|
---|
832 |
|
---|
833 | /**
|
---|
834 | * Internal version of PGMPhysGCPhys2CCPtr that expects the caller to
|
---|
835 | * own the PGM lock and therefore not need to lock the mapped page.
|
---|
836 | *
|
---|
837 | * @returns VBox status code.
|
---|
838 | * @retval VINF_SUCCESS on success.
|
---|
839 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
840 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
841 | *
|
---|
842 | * @param pVM The VM handle.
|
---|
843 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
844 | * @param pPage Pointer to the PGMPAGE structure for the page.
|
---|
845 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
846 | *
|
---|
847 | * @internal
|
---|
848 | */
|
---|
849 | int pgmPhysGCPhys2CCPtrInternal(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv)
|
---|
850 | {
|
---|
851 | int rc;
|
---|
852 | AssertReturn(pPage, VERR_INTERNAL_ERROR);
|
---|
853 | Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect) || VM_IS_EMT(pVM));
|
---|
854 |
|
---|
855 | /*
|
---|
856 | * Make sure the page is writable.
|
---|
857 | */
|
---|
858 | if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
|
---|
859 | {
|
---|
860 | rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
|
---|
861 | if (RT_FAILURE(rc))
|
---|
862 | return rc;
|
---|
863 | AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* not returned */, ("%Rrc\n", rc));
|
---|
864 | }
|
---|
865 | Assert(PGM_PAGE_GET_HCPHYS(pPage) != 0);
|
---|
866 |
|
---|
867 | /*
|
---|
868 | * Get the mapping address.
|
---|
869 | */
|
---|
870 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
871 | *ppv = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK));
|
---|
872 | #else
|
---|
873 | PPGMPAGEMAPTLBE pTlbe;
|
---|
874 | rc = pgmPhysPageQueryTlbeWithPage(&pVM->pgm.s, pPage, GCPhys, &pTlbe);
|
---|
875 | if (RT_FAILURE(rc))
|
---|
876 | return rc;
|
---|
877 | *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
|
---|
878 | #endif
|
---|
879 | return VINF_SUCCESS;
|
---|
880 | }
|
---|
881 |
|
---|
882 |
|
---|
883 | /**
|
---|
884 | * Internal version of PGMPhysGCPhys2CCPtrReadOnly that expects the caller to
|
---|
885 | * own the PGM lock and therefore not need to lock the mapped page.
|
---|
886 | *
|
---|
887 | * @returns VBox status code.
|
---|
888 | * @retval VINF_SUCCESS on success.
|
---|
889 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
890 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
891 | *
|
---|
892 | * @param pVM The VM handle.
|
---|
893 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
894 | * @param pPage Pointer to the PGMPAGE structure for the page.
|
---|
895 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
896 | *
|
---|
897 | * @internal
|
---|
898 | */
|
---|
899 | int pgmPhysGCPhys2CCPtrInternalReadOnly(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, const void **ppv)
|
---|
900 | {
|
---|
901 | AssertReturn(pPage, VERR_INTERNAL_ERROR);
|
---|
902 | Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect) || VM_IS_EMT(pVM));
|
---|
903 | Assert(PGM_PAGE_GET_HCPHYS(pPage) != 0);
|
---|
904 |
|
---|
905 | /*
|
---|
906 | * Get the mapping address.
|
---|
907 | */
|
---|
908 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
909 | *ppv = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK)); /** @todo add a read only flag? */
|
---|
910 | #else
|
---|
911 | PPGMPAGEMAPTLBE pTlbe;
|
---|
912 | int rc = pgmPhysPageQueryTlbeWithPage(&pVM->pgm.s, pPage, GCPhys, &pTlbe);
|
---|
913 | if (RT_FAILURE(rc))
|
---|
914 | return rc;
|
---|
915 | *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
|
---|
916 | #endif
|
---|
917 | return VINF_SUCCESS;
|
---|
918 | }
|
---|
919 |
|
---|
920 |
|
---|
921 | /**
|
---|
922 | * Requests the mapping of a guest page into the current context.
|
---|
923 | *
|
---|
924 | * This API should only be used for very short term, as it will consume
|
---|
925 | * scarse resources (R0 and GC) in the mapping cache. When you're done
|
---|
926 | * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
|
---|
927 | *
|
---|
928 | * This API will assume your intention is to write to the page, and will
|
---|
929 | * therefore replace shared and zero pages. If you do not intend to modify
|
---|
930 | * the page, use the PGMPhysGCPhys2CCPtrReadOnly() API.
|
---|
931 | *
|
---|
932 | * @returns VBox status code.
|
---|
933 | * @retval VINF_SUCCESS on success.
|
---|
934 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
935 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
936 | *
|
---|
937 | * @param pVM The VM handle.
|
---|
938 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
939 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
940 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
941 | *
|
---|
942 | * @remarks The caller is responsible for dealing with access handlers.
|
---|
943 | * @todo Add an informational return code for pages with access handlers?
|
---|
944 | *
|
---|
945 | * @remark Avoid calling this API from within critical sections (other than the
|
---|
946 | * PGM one) because of the deadlock risk. External threads may need to
|
---|
947 | * delegate jobs to the EMTs.
|
---|
948 | * @thread Any thread.
|
---|
949 | */
|
---|
950 | VMMDECL(int) PGMPhysGCPhys2CCPtr(PVM pVM, RTGCPHYS GCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
951 | {
|
---|
952 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
953 | # if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
954 |
|
---|
955 | /*
|
---|
956 | * Find the page and make sure it's writable.
|
---|
957 | */
|
---|
958 | PPGMPAGE pPage;
|
---|
959 | int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhys, &pPage);
|
---|
960 | if (RT_SUCCESS(rc))
|
---|
961 | {
|
---|
962 | if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
|
---|
963 | rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
|
---|
964 | if (RT_SUCCESS(rc))
|
---|
965 | {
|
---|
966 | *ppv = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK)); /** @todo add a read only flag? */
|
---|
967 | #if 0
|
---|
968 | pLock->pvMap = 0;
|
---|
969 | pLock->pvPage = pPage;
|
---|
970 | #else
|
---|
971 | pLock->u32Dummy = UINT32_MAX;
|
---|
972 | #endif
|
---|
973 | AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* not returned */, ("%Rrc\n", rc));
|
---|
974 | rc = VINF_SUCCESS;
|
---|
975 | }
|
---|
976 | }
|
---|
977 |
|
---|
978 | # else /* IN_RING3 || IN_RING0 */
|
---|
979 | int rc = pgmLock(pVM);
|
---|
980 | AssertRCReturn(rc, rc);
|
---|
981 |
|
---|
982 | /*
|
---|
983 | * Query the Physical TLB entry for the page (may fail).
|
---|
984 | */
|
---|
985 | PPGMPAGEMAPTLBE pTlbe;
|
---|
986 | rc = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
|
---|
987 | if (RT_SUCCESS(rc))
|
---|
988 | {
|
---|
989 | /*
|
---|
990 | * If the page is shared, the zero page, or being write monitored
|
---|
991 | * it must be converted to an page that's writable if possible.
|
---|
992 | */
|
---|
993 | PPGMPAGE pPage = pTlbe->pPage;
|
---|
994 | if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
|
---|
995 | {
|
---|
996 | rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
|
---|
997 | if (RT_SUCCESS(rc))
|
---|
998 | {
|
---|
999 | AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* not returned */, ("%Rrc\n", rc));
|
---|
1000 | rc = pgmPhysPageQueryTlbeWithPage(&pVM->pgm.s, pPage, GCPhys, &pTlbe);
|
---|
1001 | }
|
---|
1002 | }
|
---|
1003 | if (RT_SUCCESS(rc))
|
---|
1004 | {
|
---|
1005 | /*
|
---|
1006 | * Now, just perform the locking and calculate the return address.
|
---|
1007 | */
|
---|
1008 | PPGMPAGEMAP pMap = pTlbe->pMap;
|
---|
1009 | if (pMap)
|
---|
1010 | pMap->cRefs++;
|
---|
1011 | #if 0 /** @todo implement locking properly */
|
---|
1012 | if (RT_LIKELY(pPage->cLocks != PGM_PAGE_MAX_LOCKS))
|
---|
1013 | if (RT_UNLIKELY(++pPage->cLocks == PGM_PAGE_MAX_LOCKS))
|
---|
1014 | {
|
---|
1015 | AssertMsgFailed(("%RGp is entering permanent locked state!\n", GCPhys));
|
---|
1016 | if (pMap)
|
---|
1017 | pMap->cRefs++; /* Extra ref to prevent it from going away. */
|
---|
1018 | }
|
---|
1019 | #endif
|
---|
1020 | *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
|
---|
1021 | pLock->pvPage = pPage;
|
---|
1022 | pLock->pvMap = pMap;
|
---|
1023 | }
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | pgmUnlock(pVM);
|
---|
1027 | #endif /* IN_RING3 || IN_RING0 */
|
---|
1028 | return rc;
|
---|
1029 |
|
---|
1030 | #else
|
---|
1031 | /*
|
---|
1032 | * Temporary fallback code.
|
---|
1033 | */
|
---|
1034 | # if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1035 | /** @todo @bugref{3202}: check up this path. */
|
---|
1036 | return PGMDynMapGCPageOff(pVM, GCPhys, ppv);
|
---|
1037 | # else
|
---|
1038 | return PGMPhysGCPhys2R3Ptr(pVM, GCPhys, 1, (PRTR3PTR)ppv);
|
---|
1039 | # endif
|
---|
1040 | #endif
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 |
|
---|
1044 | /**
|
---|
1045 | * Requests the mapping of a guest page into the current context.
|
---|
1046 | *
|
---|
1047 | * This API should only be used for very short term, as it will consume
|
---|
1048 | * scarse resources (R0 and GC) in the mapping cache. When you're done
|
---|
1049 | * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
|
---|
1050 | *
|
---|
1051 | * @returns VBox status code.
|
---|
1052 | * @retval VINF_SUCCESS on success.
|
---|
1053 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
1054 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
1055 | *
|
---|
1056 | * @param pVM The VM handle.
|
---|
1057 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
1058 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
1059 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
1060 | *
|
---|
1061 | * @remarks The caller is responsible for dealing with access handlers.
|
---|
1062 | * @todo Add an informational return code for pages with access handlers?
|
---|
1063 | *
|
---|
1064 | * @remark Avoid calling this API from within critical sections (other than
|
---|
1065 | * the PGM one) because of the deadlock risk.
|
---|
1066 | * @thread Any thread.
|
---|
1067 | */
|
---|
1068 | VMMDECL(int) PGMPhysGCPhys2CCPtrReadOnly(PVM pVM, RTGCPHYS GCPhys, void const **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
1069 | {
|
---|
1070 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
1071 | # if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1072 |
|
---|
1073 | /*
|
---|
1074 | * Find the page and make sure it's readable.
|
---|
1075 | */
|
---|
1076 | PPGMPAGE pPage;
|
---|
1077 | int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhys, &pPage);
|
---|
1078 | if (RT_SUCCESS(rc))
|
---|
1079 | {
|
---|
1080 | if (RT_UNLIKELY(PGM_PAGE_IS_MMIO(pPage)))
|
---|
1081 | rc = VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
1082 | else
|
---|
1083 | {
|
---|
1084 | *ppv = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK)); /** @todo add a read only flag? */
|
---|
1085 | #if 0
|
---|
1086 | pLock->pvMap = 0;
|
---|
1087 | pLock->pvPage = pPage;
|
---|
1088 | #else
|
---|
1089 | pLock->u32Dummy = UINT32_MAX;
|
---|
1090 | #endif
|
---|
1091 | AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 /* not returned */, ("%Rrc\n", rc));
|
---|
1092 | rc = VINF_SUCCESS;
|
---|
1093 | }
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | # else /* IN_RING3 || IN_RING0 */
|
---|
1097 | int rc = pgmLock(pVM);
|
---|
1098 | AssertRCReturn(rc, rc);
|
---|
1099 |
|
---|
1100 | /*
|
---|
1101 | * Query the Physical TLB entry for the page (may fail).
|
---|
1102 | */
|
---|
1103 | PPGMPAGEMAPTLBE pTlbe;
|
---|
1104 | rc = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
|
---|
1105 | if (RT_SUCCESS(rc))
|
---|
1106 | {
|
---|
1107 | /* MMIO pages doesn't have any readable backing. */
|
---|
1108 | PPGMPAGE pPage = pTlbe->pPage;
|
---|
1109 | if (RT_UNLIKELY(PGM_PAGE_IS_MMIO(pPage)))
|
---|
1110 | rc = VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
1111 | else
|
---|
1112 | {
|
---|
1113 | /*
|
---|
1114 | * Now, just perform the locking and calculate the return address.
|
---|
1115 | */
|
---|
1116 | PPGMPAGEMAP pMap = pTlbe->pMap;
|
---|
1117 | if (pMap)
|
---|
1118 | pMap->cRefs++;
|
---|
1119 | #if 0 /** @todo implement locking properly */
|
---|
1120 | if (RT_LIKELY(pPage->cLocks != PGM_PAGE_MAX_LOCKS))
|
---|
1121 | if (RT_UNLIKELY(++pPage->cLocks == PGM_PAGE_MAX_LOCKS))
|
---|
1122 | {
|
---|
1123 | AssertMsgFailed(("%RGp is entering permanent locked state!\n", GCPhys));
|
---|
1124 | if (pMap)
|
---|
1125 | pMap->cRefs++; /* Extra ref to prevent it from going away. */
|
---|
1126 | }
|
---|
1127 | #endif
|
---|
1128 | *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
|
---|
1129 | pLock->pvPage = pPage;
|
---|
1130 | pLock->pvMap = pMap;
|
---|
1131 | }
|
---|
1132 | }
|
---|
1133 |
|
---|
1134 | pgmUnlock(pVM);
|
---|
1135 | #endif /* IN_RING3 || IN_RING0 */
|
---|
1136 | return rc;
|
---|
1137 |
|
---|
1138 | #else /* !VBOX_WITH_NEW_PHYS_CODE */
|
---|
1139 | /*
|
---|
1140 | * Fallback code.
|
---|
1141 | */
|
---|
1142 | return PGMPhysGCPhys2CCPtr(pVM, GCPhys, (void **)ppv, pLock);
|
---|
1143 | #endif /* !VBOX_WITH_NEW_PHYS_CODE */
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 |
|
---|
1147 | /**
|
---|
1148 | * Requests the mapping of a guest page given by virtual address into the current context.
|
---|
1149 | *
|
---|
1150 | * This API should only be used for very short term, as it will consume
|
---|
1151 | * scarse resources (R0 and GC) in the mapping cache. When you're done
|
---|
1152 | * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
|
---|
1153 | *
|
---|
1154 | * This API will assume your intention is to write to the page, and will
|
---|
1155 | * therefore replace shared and zero pages. If you do not intend to modify
|
---|
1156 | * the page, use the PGMPhysGCPtr2CCPtrReadOnly() API.
|
---|
1157 | *
|
---|
1158 | * @returns VBox status code.
|
---|
1159 | * @retval VINF_SUCCESS on success.
|
---|
1160 | * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
|
---|
1161 | * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
|
---|
1162 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
1163 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
1164 | *
|
---|
1165 | * @param pVM The VM handle.
|
---|
1166 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
1167 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
1168 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
1169 | *
|
---|
1170 | * @remark Avoid calling this API from within critical sections (other than
|
---|
1171 | * the PGM one) because of the deadlock risk.
|
---|
1172 | * @thread EMT
|
---|
1173 | */
|
---|
1174 | VMMDECL(int) PGMPhysGCPtr2CCPtr(PVM pVM, RTGCPTR GCPtr, void **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
1175 | {
|
---|
1176 | VM_ASSERT_EMT(pVM);
|
---|
1177 | RTGCPHYS GCPhys;
|
---|
1178 | int rc = PGMPhysGCPtr2GCPhys(pVM, GCPtr, &GCPhys);
|
---|
1179 | if (RT_SUCCESS(rc))
|
---|
1180 | rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys, ppv, pLock);
|
---|
1181 | return rc;
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 |
|
---|
1185 | /**
|
---|
1186 | * Requests the mapping of a guest page given by virtual address into the current context.
|
---|
1187 | *
|
---|
1188 | * This API should only be used for very short term, as it will consume
|
---|
1189 | * scarse resources (R0 and GC) in the mapping cache. When you're done
|
---|
1190 | * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
|
---|
1191 | *
|
---|
1192 | * @returns VBox status code.
|
---|
1193 | * @retval VINF_SUCCESS on success.
|
---|
1194 | * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
|
---|
1195 | * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
|
---|
1196 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
1197 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
1198 | *
|
---|
1199 | * @param pVM The VM handle.
|
---|
1200 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
1201 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
1202 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
1203 | *
|
---|
1204 | * @remark Avoid calling this API from within critical sections (other than
|
---|
1205 | * the PGM one) because of the deadlock risk.
|
---|
1206 | * @thread EMT
|
---|
1207 | */
|
---|
1208 | VMMDECL(int) PGMPhysGCPtr2CCPtrReadOnly(PVM pVM, RTGCPTR GCPtr, void const **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
1209 | {
|
---|
1210 | /* Debugger uses this API too */
|
---|
1211 | //VM_ASSERT_EMT(pVM);
|
---|
1212 | RTGCPHYS GCPhys;
|
---|
1213 | int rc = PGMPhysGCPtr2GCPhys(pVM, GCPtr, &GCPhys);
|
---|
1214 | if (RT_SUCCESS(rc))
|
---|
1215 | rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys, ppv, pLock);
|
---|
1216 | return rc;
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 |
|
---|
1220 | /**
|
---|
1221 | * Release the mapping of a guest page.
|
---|
1222 | *
|
---|
1223 | * This is the counter part of PGMPhysGCPhys2CCPtr, PGMPhysGCPhys2CCPtrReadOnly
|
---|
1224 | * PGMPhysGCPtr2CCPtr and PGMPhysGCPtr2CCPtrReadOnly.
|
---|
1225 | *
|
---|
1226 | * @param pVM The VM handle.
|
---|
1227 | * @param pLock The lock structure initialized by the mapping function.
|
---|
1228 | */
|
---|
1229 | VMMDECL(void) PGMPhysReleasePageMappingLock(PVM pVM, PPGMPAGEMAPLOCK pLock)
|
---|
1230 | {
|
---|
1231 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
1232 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1233 | /* currently nothing to do here. */
|
---|
1234 | Assert(pLock->u32Dummy == UINT32_MAX);
|
---|
1235 | pLock->u32Dummy = 0;
|
---|
1236 |
|
---|
1237 | #else /* IN_RING3 */
|
---|
1238 | PPGMPAGEMAP pMap = (PPGMPAGEMAP)pLock->pvMap;
|
---|
1239 | if (!pMap)
|
---|
1240 | {
|
---|
1241 | /* The ZERO page and MMIO2 ends up here. */
|
---|
1242 | Assert(pLock->pvPage);
|
---|
1243 | pLock->pvPage = NULL;
|
---|
1244 | }
|
---|
1245 | else
|
---|
1246 | {
|
---|
1247 | pgmLock(pVM);
|
---|
1248 |
|
---|
1249 | # if 0 /** @todo implement page locking */
|
---|
1250 | PPGMPAGE pPage = (PPGMPAGE)pLock->pvPage;
|
---|
1251 | Assert(pPage->cLocks >= 1);
|
---|
1252 | if (pPage->cLocks != PGM_PAGE_MAX_LOCKS)
|
---|
1253 | pPage->cLocks--;
|
---|
1254 | # endif
|
---|
1255 |
|
---|
1256 | Assert(pMap->cRefs >= 1);
|
---|
1257 | pMap->cRefs--;
|
---|
1258 | pMap->iAge = 0;
|
---|
1259 |
|
---|
1260 | pgmUnlock(pVM);
|
---|
1261 | }
|
---|
1262 | #endif /* IN_RING3 */
|
---|
1263 | #else
|
---|
1264 | NOREF(pVM);
|
---|
1265 | NOREF(pLock);
|
---|
1266 | #endif
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 |
|
---|
1270 | /**
|
---|
1271 | * Converts a GC physical address to a HC ring-3 pointer.
|
---|
1272 | *
|
---|
1273 | * @returns VINF_SUCCESS on success.
|
---|
1274 | * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
|
---|
1275 | * page but has no physical backing.
|
---|
1276 | * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
|
---|
1277 | * GC physical address.
|
---|
1278 | * @returns VERR_PGM_GCPHYS_RANGE_CROSSES_BOUNDARY if the range crosses
|
---|
1279 | * a dynamic ram chunk boundary
|
---|
1280 | *
|
---|
1281 | * @param pVM The VM handle.
|
---|
1282 | * @param GCPhys The GC physical address to convert.
|
---|
1283 | * @param cbRange Physical range
|
---|
1284 | * @param pR3Ptr Where to store the R3 pointer on success.
|
---|
1285 | *
|
---|
1286 | * @deprecated Avoid when possible!
|
---|
1287 | */
|
---|
1288 | VMMDECL(int) PGMPhysGCPhys2R3Ptr(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange, PRTR3PTR pR3Ptr)
|
---|
1289 | {
|
---|
1290 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
1291 | /** @todo this is kind of hacky and needs some more work. */
|
---|
1292 | VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
|
---|
1293 |
|
---|
1294 | Log(("PGMPhysGCPhys2R3Ptr(,%RGp,%#x,): dont use this API!\n", GCPhys, cbRange)); /** @todo eliminate this API! */
|
---|
1295 | # if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1296 | AssertFailedReturn(VERR_NOT_IMPLEMENTED);
|
---|
1297 | # else
|
---|
1298 | pgmLock(pVM);
|
---|
1299 |
|
---|
1300 | PPGMRAMRANGE pRam;
|
---|
1301 | PPGMPAGE pPage;
|
---|
1302 | int rc = pgmPhysGetPageAndRangeEx(&pVM->pgm.s, GCPhys, &pPage, &pRam);
|
---|
1303 | if (RT_SUCCESS(rc))
|
---|
1304 | rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, (void **)pR3Ptr);
|
---|
1305 |
|
---|
1306 | pgmUnlock(pVM);
|
---|
1307 | Assert(rc <= VINF_SUCCESS);
|
---|
1308 | return rc;
|
---|
1309 | # endif
|
---|
1310 |
|
---|
1311 | #else /* !VBOX_WITH_NEW_PHYS_CODE */
|
---|
1312 |
|
---|
1313 | if ((GCPhys & PGM_DYNAMIC_CHUNK_BASE_MASK) != ((GCPhys+cbRange-1) & PGM_DYNAMIC_CHUNK_BASE_MASK))
|
---|
1314 | {
|
---|
1315 | AssertMsgFailed(("%RGp - %RGp crosses a chunk boundary!!\n", GCPhys, GCPhys+cbRange));
|
---|
1316 | LogRel(("PGMPhysGCPhys2HCPtr %RGp - %RGp crosses a chunk boundary!!\n", GCPhys, GCPhys+cbRange));
|
---|
1317 | return VERR_PGM_GCPHYS_RANGE_CROSSES_BOUNDARY;
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 | PPGMRAMRANGE pRam;
|
---|
1321 | PPGMPAGE pPage;
|
---|
1322 | int rc = pgmPhysGetPageAndRangeEx(&pVM->pgm.s, GCPhys, &pPage, &pRam);
|
---|
1323 | if (RT_FAILURE(rc))
|
---|
1324 | return rc;
|
---|
1325 |
|
---|
1326 | #ifndef PGM_IGNORE_RAM_FLAGS_RESERVED
|
---|
1327 | if (RT_UNLIKELY(PGM_PAGE_IS_RESERVED(pPage)))
|
---|
1328 | return VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
1329 | #endif
|
---|
1330 |
|
---|
1331 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
1332 | if (RT_UNLIKELY(off + cbRange > pRam->cb))
|
---|
1333 | {
|
---|
1334 | AssertMsgFailed(("%RGp - %RGp crosses a chunk boundary!!\n", GCPhys, GCPhys + cbRange));
|
---|
1335 | return VERR_PGM_GCPHYS_RANGE_CROSSES_BOUNDARY;
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | if (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
|
---|
1339 | {
|
---|
1340 | unsigned iChunk = (off >> PGM_DYNAMIC_CHUNK_SHIFT);
|
---|
1341 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0) /* ASSUMES this is a rare occurence */
|
---|
1342 | PRTR3UINTPTR paChunkR3Ptrs = (PRTR3UINTPTR)MMHyperR3ToCC(pVM, pRam->paChunkR3Ptrs);
|
---|
1343 | *pR3Ptr = (RTR3PTR)(paChunkR3Ptrs[iChunk] + (off & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
|
---|
1344 | #else
|
---|
1345 | *pR3Ptr = (RTR3PTR)(pRam->paChunkR3Ptrs[iChunk] + (off & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
|
---|
1346 | #endif
|
---|
1347 | }
|
---|
1348 | else if (RT_LIKELY(pRam->pvR3))
|
---|
1349 | *pR3Ptr = (RTR3PTR)((RTR3UINTPTR)pRam->pvR3 + off);
|
---|
1350 | else
|
---|
1351 | return VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
1352 | return VINF_SUCCESS;
|
---|
1353 | #endif /* !VBOX_WITH_NEW_PHYS_CODE */
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 |
|
---|
1357 | #ifdef VBOX_STRICT
|
---|
1358 | /**
|
---|
1359 | * PGMPhysGCPhys2R3Ptr convenience for use with assertions.
|
---|
1360 | *
|
---|
1361 | * @returns The R3Ptr, NIL_RTR3PTR on failure.
|
---|
1362 | * @param pVM The VM handle.
|
---|
1363 | * @param GCPhys The GC Physical addresss.
|
---|
1364 | * @param cbRange Physical range.
|
---|
1365 | *
|
---|
1366 | * @deprecated Avoid when possible.
|
---|
1367 | */
|
---|
1368 | VMMDECL(RTR3PTR) PGMPhysGCPhys2R3PtrAssert(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange)
|
---|
1369 | {
|
---|
1370 | RTR3PTR R3Ptr;
|
---|
1371 | int rc = PGMPhysGCPhys2R3Ptr(pVM, GCPhys, cbRange, &R3Ptr);
|
---|
1372 | if (RT_SUCCESS(rc))
|
---|
1373 | return R3Ptr;
|
---|
1374 | return NIL_RTR3PTR;
|
---|
1375 | }
|
---|
1376 | #endif /* VBOX_STRICT */
|
---|
1377 |
|
---|
1378 |
|
---|
1379 | /**
|
---|
1380 | * Converts a guest pointer to a GC physical address.
|
---|
1381 | *
|
---|
1382 | * This uses the current CR3/CR0/CR4 of the guest.
|
---|
1383 | *
|
---|
1384 | * @returns VBox status code.
|
---|
1385 | * @param pVM The VM Handle
|
---|
1386 | * @param GCPtr The guest pointer to convert.
|
---|
1387 | * @param pGCPhys Where to store the GC physical address.
|
---|
1388 | */
|
---|
1389 | VMMDECL(int) PGMPhysGCPtr2GCPhys(PVM pVM, RTGCPTR GCPtr, PRTGCPHYS pGCPhys)
|
---|
1390 | {
|
---|
1391 | int rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtr, NULL, pGCPhys);
|
---|
1392 | if (pGCPhys && RT_SUCCESS(rc))
|
---|
1393 | *pGCPhys |= (RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK;
|
---|
1394 | return rc;
|
---|
1395 | }
|
---|
1396 |
|
---|
1397 |
|
---|
1398 | /**
|
---|
1399 | * Converts a guest pointer to a HC physical address.
|
---|
1400 | *
|
---|
1401 | * This uses the current CR3/CR0/CR4 of the guest.
|
---|
1402 | *
|
---|
1403 | * @returns VBox status code.
|
---|
1404 | * @param pVM The VM Handle
|
---|
1405 | * @param GCPtr The guest pointer to convert.
|
---|
1406 | * @param pHCPhys Where to store the HC physical address.
|
---|
1407 | */
|
---|
1408 | VMMDECL(int) PGMPhysGCPtr2HCPhys(PVM pVM, RTGCPTR GCPtr, PRTHCPHYS pHCPhys)
|
---|
1409 | {
|
---|
1410 | RTGCPHYS GCPhys;
|
---|
1411 | int rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtr, NULL, &GCPhys);
|
---|
1412 | if (RT_SUCCESS(rc))
|
---|
1413 | rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), pHCPhys);
|
---|
1414 | return rc;
|
---|
1415 | }
|
---|
1416 |
|
---|
1417 |
|
---|
1418 | /**
|
---|
1419 | * Converts a guest pointer to a R3 pointer.
|
---|
1420 | *
|
---|
1421 | * This uses the current CR3/CR0/CR4 of the guest.
|
---|
1422 | *
|
---|
1423 | * @returns VBox status code.
|
---|
1424 | * @param pVM The VM Handle
|
---|
1425 | * @param GCPtr The guest pointer to convert.
|
---|
1426 | * @param pR3Ptr Where to store the R3 virtual address.
|
---|
1427 | *
|
---|
1428 | * @deprecated Don't use this.
|
---|
1429 | */
|
---|
1430 | VMMDECL(int) PGMPhysGCPtr2R3Ptr(PVM pVM, RTGCPTR GCPtr, PRTR3PTR pR3Ptr)
|
---|
1431 | {
|
---|
1432 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
1433 | VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
|
---|
1434 | #endif
|
---|
1435 |
|
---|
1436 | RTGCPHYS GCPhys;
|
---|
1437 | int rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtr, NULL, &GCPhys);
|
---|
1438 | if (RT_SUCCESS(rc))
|
---|
1439 | rc = PGMPhysGCPhys2R3Ptr(pVM, GCPhys | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), 1 /* we always stay within one page */, pR3Ptr);
|
---|
1440 | return rc;
|
---|
1441 | }
|
---|
1442 |
|
---|
1443 |
|
---|
1444 |
|
---|
1445 | #undef LOG_GROUP
|
---|
1446 | #define LOG_GROUP LOG_GROUP_PGM_PHYS_ACCESS
|
---|
1447 |
|
---|
1448 |
|
---|
1449 | #ifdef IN_RING3
|
---|
1450 | /**
|
---|
1451 | * Cache PGMPhys memory access
|
---|
1452 | *
|
---|
1453 | * @param pVM VM Handle.
|
---|
1454 | * @param pCache Cache structure pointer
|
---|
1455 | * @param GCPhys GC physical address
|
---|
1456 | * @param pbHC HC pointer corresponding to physical page
|
---|
1457 | *
|
---|
1458 | * @thread EMT.
|
---|
1459 | */
|
---|
1460 | static void pgmPhysCacheAdd(PVM pVM, PGMPHYSCACHE *pCache, RTGCPHYS GCPhys, uint8_t *pbR3)
|
---|
1461 | {
|
---|
1462 | uint32_t iCacheIndex;
|
---|
1463 |
|
---|
1464 | Assert(VM_IS_EMT(pVM));
|
---|
1465 |
|
---|
1466 | GCPhys = PHYS_PAGE_ADDRESS(GCPhys);
|
---|
1467 | pbR3 = (uint8_t *)PAGE_ADDRESS(pbR3);
|
---|
1468 |
|
---|
1469 | iCacheIndex = ((GCPhys >> PAGE_SHIFT) & PGM_MAX_PHYSCACHE_ENTRIES_MASK);
|
---|
1470 |
|
---|
1471 | ASMBitSet(&pCache->aEntries, iCacheIndex);
|
---|
1472 |
|
---|
1473 | pCache->Entry[iCacheIndex].GCPhys = GCPhys;
|
---|
1474 | pCache->Entry[iCacheIndex].pbR3 = pbR3;
|
---|
1475 | }
|
---|
1476 | #endif /* IN_RING3 */
|
---|
1477 |
|
---|
1478 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
1479 |
|
---|
1480 | /**
|
---|
1481 | * Deals with reading from a page with one or more ALL access handlers.
|
---|
1482 | *
|
---|
1483 | * @returns VBox status code. Can be ignored in ring-3.
|
---|
1484 | * @retval VINF_SUCCESS.
|
---|
1485 | * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
|
---|
1486 | *
|
---|
1487 | * @param pVM The VM handle.
|
---|
1488 | * @param pPage The page descriptor.
|
---|
1489 | * @param GCPhys The physical address to start reading at.
|
---|
1490 | * @param pvBuf Where to put the bits we read.
|
---|
1491 | * @param cb How much to read - less or equal to a page.
|
---|
1492 | */
|
---|
1493 | static int pgmPhysReadHandler(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void *pvBuf, size_t cb)
|
---|
1494 | {
|
---|
1495 | /*
|
---|
1496 | * The most frequent access here is MMIO and shadowed ROM.
|
---|
1497 | * The current code ASSUMES all these access handlers covers full pages!
|
---|
1498 | */
|
---|
1499 |
|
---|
1500 | /*
|
---|
1501 | * Whatever we do we need the source page, map it first.
|
---|
1502 | */
|
---|
1503 | const void *pvSrc = NULL;
|
---|
1504 | int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, GCPhys, &pvSrc);
|
---|
1505 | if (RT_FAILURE(rc))
|
---|
1506 | {
|
---|
1507 | AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
1508 | GCPhys, pPage, rc));
|
---|
1509 | memset(pvBuf, 0xff, cb);
|
---|
1510 | return VINF_SUCCESS;
|
---|
1511 | }
|
---|
1512 | rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
1513 |
|
---|
1514 | /*
|
---|
1515 | * Deal with any physical handlers.
|
---|
1516 | */
|
---|
1517 | PPGMPHYSHANDLER pPhys = NULL;
|
---|
1518 | if (PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) == PGM_PAGE_HNDL_PHYS_STATE_ALL)
|
---|
1519 | {
|
---|
1520 | #ifdef IN_RING3
|
---|
1521 | PPGMPHYSHANDLER pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
1522 | AssertReleaseMsg(pPhys, ("GCPhys=%RGp cb=%#x\n", GCPhys, cb));
|
---|
1523 | Assert(GCPhys >= pPhys->Core.Key && GCPhys <= pPhys->Core.KeyLast);
|
---|
1524 | Assert((pPhys->Core.Key & PAGE_OFFSET_MASK) == 0);
|
---|
1525 | Assert((pPhys->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
|
---|
1526 | Assert(pPhys->CTX_SUFF(pfnHandler));
|
---|
1527 |
|
---|
1528 | Log5(("pgmPhysReadHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cb, pPage, R3STRING(pPhys->pszDesc) ));
|
---|
1529 | STAM_PROFILE_START(&pPhys->Stat, h);
|
---|
1530 | rc = pPhys->CTX_SUFF(pfnHandler)(pVM, GCPhys, (void *)pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, pPhys->CTX_SUFF(pvUser));
|
---|
1531 | STAM_PROFILE_STOP(&pPhys->Stat, h);
|
---|
1532 | AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp\n", rc, GCPhys));
|
---|
1533 | #else
|
---|
1534 | /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
|
---|
1535 | //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cb=%#x\n", GCPhys, cb));
|
---|
1536 | return VERR_PGM_PHYS_WR_HIT_HANDLER;
|
---|
1537 | #endif
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 | /*
|
---|
1541 | * Deal with any virtual handlers.
|
---|
1542 | */
|
---|
1543 | if (PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) == PGM_PAGE_HNDL_VIRT_STATE_ALL)
|
---|
1544 | {
|
---|
1545 | unsigned iPage;
|
---|
1546 | PPGMVIRTHANDLER pVirt;
|
---|
1547 |
|
---|
1548 | int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pVirt, &iPage);
|
---|
1549 | AssertReleaseMsg(RT_SUCCESS(rc2), ("GCPhys=%RGp cb=%#x rc2=%Rrc\n", GCPhys, cb, rc2));
|
---|
1550 | Assert((pVirt->Core.Key & PAGE_OFFSET_MASK) == 0);
|
---|
1551 | Assert((pVirt->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
|
---|
1552 | Assert(GCPhys >= pVirt->aPhysToVirt[iPage].Core.Key && GCPhys <= pVirt->aPhysToVirt[iPage].Core.KeyLast);
|
---|
1553 |
|
---|
1554 | #ifdef IN_RING3
|
---|
1555 | if (pVirt->pfnHandlerR3)
|
---|
1556 | {
|
---|
1557 | if (!pPhys)
|
---|
1558 | Log5(("pgmPhysWriteHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] virt %s\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc) ));
|
---|
1559 | else
|
---|
1560 | Log(("pgmPhysWriteHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] phys/virt %s/%s\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc), R3STRING(pPhys->pszDesc) ));
|
---|
1561 | RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
|
---|
1562 | + (iPage << PAGE_SHIFT)
|
---|
1563 | + (GCPhys & PAGE_OFFSET_MASK);
|
---|
1564 |
|
---|
1565 | STAM_PROFILE_START(&pVirt->Stat, h);
|
---|
1566 | rc2 = pVirt->CTX_SUFF(pfnHandler)(pVM, GCPtr, (void *)pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, /*pVirt->CTX_SUFF(pvUser)*/ NULL);
|
---|
1567 | STAM_PROFILE_STOP(&pVirt->Stat, h);
|
---|
1568 | if (rc2 == VINF_SUCCESS)
|
---|
1569 | rc = VINF_SUCCESS;
|
---|
1570 | AssertLogRelMsg(rc2 == VINF_SUCCESS || rc2 == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc2, GCPhys, pPage, pVirt->pszDesc));
|
---|
1571 | }
|
---|
1572 | else
|
---|
1573 | Log5(("pgmPhysWriteHandler: GCPhys=%RGp cb=%#x pPage=%R[pgmpage] virt %s [no handler]\n", GCPhys, cb, pPage, R3STRING(pVirt->pszDesc) ));
|
---|
1574 | #else
|
---|
1575 | /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
|
---|
1576 | //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cb=%#x\n", GCPhys, cb));
|
---|
1577 | return VERR_PGM_PHYS_WR_HIT_HANDLER;
|
---|
1578 | #endif
|
---|
1579 | }
|
---|
1580 |
|
---|
1581 | /*
|
---|
1582 | * Take the default action.
|
---|
1583 | */
|
---|
1584 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
1585 | memcpy(pvBuf, pvSrc, cb);
|
---|
1586 | return rc;
|
---|
1587 | }
|
---|
1588 |
|
---|
1589 |
|
---|
1590 | /**
|
---|
1591 | * Read physical memory.
|
---|
1592 | *
|
---|
1593 | * This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
|
---|
1594 | * want to ignore those.
|
---|
1595 | *
|
---|
1596 | * @returns VBox status code. Can be ignored in ring-3.
|
---|
1597 | * @retval VINF_SUCCESS.
|
---|
1598 | * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
|
---|
1599 | *
|
---|
1600 | * @param pVM VM Handle.
|
---|
1601 | * @param GCPhys Physical address start reading from.
|
---|
1602 | * @param pvBuf Where to put the read bits.
|
---|
1603 | * @param cbRead How many bytes to read.
|
---|
1604 | */
|
---|
1605 | VMMDECL(int) PGMPhysRead(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
|
---|
1606 | {
|
---|
1607 | AssertMsgReturn(cbRead > 0, ("don't even think about reading zero bytes!\n"), VINF_SUCCESS);
|
---|
1608 | LogFlow(("PGMPhysRead: %RGp %d\n", GCPhys, cbRead));
|
---|
1609 |
|
---|
1610 | pgmLock(pVM);
|
---|
1611 |
|
---|
1612 | /*
|
---|
1613 | * Copy loop on ram ranges.
|
---|
1614 | */
|
---|
1615 | PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
1616 | for (;;)
|
---|
1617 | {
|
---|
1618 | /* Find range. */
|
---|
1619 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
1620 | pRam = pRam->CTX_SUFF(pNext);
|
---|
1621 | /* Inside range or not? */
|
---|
1622 | if (pRam && GCPhys >= pRam->GCPhys)
|
---|
1623 | {
|
---|
1624 | /*
|
---|
1625 | * Must work our way thru this page by page.
|
---|
1626 | */
|
---|
1627 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
1628 | while (off < pRam->cb)
|
---|
1629 | {
|
---|
1630 | unsigned iPage = off >> PAGE_SHIFT;
|
---|
1631 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
1632 | size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1633 | if (cb > cbRead)
|
---|
1634 | cb = cbRead;
|
---|
1635 |
|
---|
1636 | /*
|
---|
1637 | * Any ALL access handlers?
|
---|
1638 | */
|
---|
1639 | if (RT_UNLIKELY(PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage)))
|
---|
1640 | {
|
---|
1641 | int rc = pgmPhysReadHandler(pVM, pPage, pRam->GCPhys + off, pvBuf, cb);
|
---|
1642 | if (RT_FAILURE(rc))
|
---|
1643 | return rc;
|
---|
1644 | }
|
---|
1645 | else
|
---|
1646 | {
|
---|
1647 | /*
|
---|
1648 | * Get the pointer to the page.
|
---|
1649 | */
|
---|
1650 | const void *pvSrc;
|
---|
1651 | int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, pRam->GCPhys + off, &pvSrc);
|
---|
1652 | if (RT_SUCCESS(rc))
|
---|
1653 | memcpy(pvBuf, pvSrc, cb);
|
---|
1654 | else
|
---|
1655 | {
|
---|
1656 | AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
1657 | pRam->GCPhys + off, pPage, rc));
|
---|
1658 | memset(pvBuf, 0xff, cb);
|
---|
1659 | }
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | /* next page */
|
---|
1663 | if (cb >= cbRead)
|
---|
1664 | {
|
---|
1665 | pgmUnlock(pVM);
|
---|
1666 | return VINF_SUCCESS;
|
---|
1667 | }
|
---|
1668 | cbRead -= cb;
|
---|
1669 | off += cb;
|
---|
1670 | pvBuf = (char *)pvBuf + cb;
|
---|
1671 | } /* walk pages in ram range. */
|
---|
1672 |
|
---|
1673 | GCPhys = pRam->GCPhysLast + 1;
|
---|
1674 | }
|
---|
1675 | else
|
---|
1676 | {
|
---|
1677 | LogFlow(("PGMPhysRead: Unassigned %RGp size=%u\n", GCPhys, cbRead));
|
---|
1678 |
|
---|
1679 | /*
|
---|
1680 | * Unassigned address space.
|
---|
1681 | */
|
---|
1682 | if (!pRam)
|
---|
1683 | break;
|
---|
1684 | size_t cb = pRam->GCPhys - GCPhys;
|
---|
1685 | if (cb >= cbRead)
|
---|
1686 | {
|
---|
1687 | memset(pvBuf, 0xff, cbRead);
|
---|
1688 | break;
|
---|
1689 | }
|
---|
1690 | memset(pvBuf, 0xff, cb);
|
---|
1691 |
|
---|
1692 | cbRead -= cb;
|
---|
1693 | pvBuf = (char *)pvBuf + cb;
|
---|
1694 | GCPhys += cb;
|
---|
1695 | }
|
---|
1696 | } /* Ram range walk */
|
---|
1697 |
|
---|
1698 | pgmUnlock(pVM);
|
---|
1699 | return VINF_SUCCESS;
|
---|
1700 | }
|
---|
1701 |
|
---|
1702 | #else /* Old PGMPhysRead */
|
---|
1703 |
|
---|
1704 | /**
|
---|
1705 | * Read physical memory.
|
---|
1706 | *
|
---|
1707 | * This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
|
---|
1708 | * want to ignore those.
|
---|
1709 | *
|
---|
1710 | * @param pVM VM Handle.
|
---|
1711 | * @param GCPhys Physical address start reading from.
|
---|
1712 | * @param pvBuf Where to put the read bits.
|
---|
1713 | * @param cbRead How many bytes to read.
|
---|
1714 | */
|
---|
1715 | VMMDECL(void) PGMPhysRead(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
|
---|
1716 | {
|
---|
1717 | #ifdef IN_RING3
|
---|
1718 | bool fGrabbedLock = false;
|
---|
1719 | #endif
|
---|
1720 |
|
---|
1721 | AssertMsg(cbRead > 0, ("don't even think about reading zero bytes!\n"));
|
---|
1722 | if (cbRead == 0)
|
---|
1723 | return;
|
---|
1724 |
|
---|
1725 | LogFlow(("PGMPhysRead: %RGp %d\n", GCPhys, cbRead));
|
---|
1726 |
|
---|
1727 | #ifdef IN_RING3
|
---|
1728 | if (!VM_IS_EMT(pVM))
|
---|
1729 | {
|
---|
1730 | pgmLock(pVM);
|
---|
1731 | fGrabbedLock = true;
|
---|
1732 | }
|
---|
1733 | #endif
|
---|
1734 |
|
---|
1735 | /*
|
---|
1736 | * Copy loop on ram ranges.
|
---|
1737 | */
|
---|
1738 | PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
1739 | for (;;)
|
---|
1740 | {
|
---|
1741 | /* Find range. */
|
---|
1742 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
1743 | pRam = pRam->CTX_SUFF(pNext);
|
---|
1744 | /* Inside range or not? */
|
---|
1745 | if (pRam && GCPhys >= pRam->GCPhys)
|
---|
1746 | {
|
---|
1747 | /*
|
---|
1748 | * Must work our way thru this page by page.
|
---|
1749 | */
|
---|
1750 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
1751 | while (off < pRam->cb)
|
---|
1752 | {
|
---|
1753 | unsigned iPage = off >> PAGE_SHIFT;
|
---|
1754 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
1755 | size_t cb;
|
---|
1756 |
|
---|
1757 | /* Physical chunk in dynamically allocated range not present? */
|
---|
1758 | if (RT_UNLIKELY(!PGM_PAGE_GET_HCPHYS(pPage)))
|
---|
1759 | {
|
---|
1760 | /* Treat it as reserved; return zeros */
|
---|
1761 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1762 | if (cb >= cbRead)
|
---|
1763 | {
|
---|
1764 | memset(pvBuf, 0, cbRead);
|
---|
1765 | goto l_End;
|
---|
1766 | }
|
---|
1767 | memset(pvBuf, 0, cb);
|
---|
1768 | }
|
---|
1769 | /* temp hacks, will be reorganized. */
|
---|
1770 | /*
|
---|
1771 | * Physical handler.
|
---|
1772 | */
|
---|
1773 | else if ( RT_UNLIKELY(PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) >= PGM_PAGE_HNDL_PHYS_STATE_ALL)
|
---|
1774 | && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
|
---|
1775 | {
|
---|
1776 | int rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
1777 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1778 |
|
---|
1779 | #ifdef IN_RING3 /** @todo deal with this in GC and R0! */
|
---|
1780 | /* find and call the handler */
|
---|
1781 | PPGMPHYSHANDLER pNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesR3->PhysHandlers, GCPhys);
|
---|
1782 | if (pNode && pNode->pfnHandlerR3)
|
---|
1783 | {
|
---|
1784 | size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
|
---|
1785 | if (cbRange < cb)
|
---|
1786 | cb = cbRange;
|
---|
1787 | if (cb > cbRead)
|
---|
1788 | cb = cbRead;
|
---|
1789 |
|
---|
1790 | void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1791 |
|
---|
1792 | /* Note! Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
|
---|
1793 | rc = pNode->pfnHandlerR3(pVM, GCPhys, pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, pNode->pvUserR3);
|
---|
1794 | }
|
---|
1795 | #endif /* IN_RING3 */
|
---|
1796 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
1797 | {
|
---|
1798 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1799 | void *pvSrc = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
|
---|
1800 | #else
|
---|
1801 | void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1802 | #endif
|
---|
1803 |
|
---|
1804 | if (cb >= cbRead)
|
---|
1805 | {
|
---|
1806 | memcpy(pvBuf, pvSrc, cbRead);
|
---|
1807 | goto l_End;
|
---|
1808 | }
|
---|
1809 | memcpy(pvBuf, pvSrc, cb);
|
---|
1810 | }
|
---|
1811 | else if (cb >= cbRead)
|
---|
1812 | goto l_End;
|
---|
1813 | }
|
---|
1814 | /*
|
---|
1815 | * Virtual handlers.
|
---|
1816 | */
|
---|
1817 | else if ( RT_UNLIKELY(PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) >= PGM_PAGE_HNDL_VIRT_STATE_ALL)
|
---|
1818 | && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
|
---|
1819 | {
|
---|
1820 | int rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
1821 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1822 | #ifdef IN_RING3 /** @todo deal with this in GC and R0! */
|
---|
1823 | /* Search the whole tree for matching physical addresses (rather expensive!) */
|
---|
1824 | PPGMVIRTHANDLER pNode;
|
---|
1825 | unsigned iPage;
|
---|
1826 | int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pNode, &iPage);
|
---|
1827 | if (RT_SUCCESS(rc2) && pNode->pfnHandlerR3)
|
---|
1828 | {
|
---|
1829 | size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
|
---|
1830 | if (cbRange < cb)
|
---|
1831 | cb = cbRange;
|
---|
1832 | if (cb > cbRead)
|
---|
1833 | cb = cbRead;
|
---|
1834 | RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pNode->Core.Key & PAGE_BASE_GC_MASK)
|
---|
1835 | + (iPage << PAGE_SHIFT) + (off & PAGE_OFFSET_MASK);
|
---|
1836 |
|
---|
1837 | void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1838 |
|
---|
1839 | /* Note! Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
|
---|
1840 | rc = pNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, 0);
|
---|
1841 | }
|
---|
1842 | #endif /* IN_RING3 */
|
---|
1843 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
1844 | {
|
---|
1845 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1846 | void *pvSrc = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
|
---|
1847 | #else
|
---|
1848 | void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1849 | #endif
|
---|
1850 | if (cb >= cbRead)
|
---|
1851 | {
|
---|
1852 | memcpy(pvBuf, pvSrc, cbRead);
|
---|
1853 | goto l_End;
|
---|
1854 | }
|
---|
1855 | memcpy(pvBuf, pvSrc, cb);
|
---|
1856 | }
|
---|
1857 | else if (cb >= cbRead)
|
---|
1858 | goto l_End;
|
---|
1859 | }
|
---|
1860 | else
|
---|
1861 | {
|
---|
1862 | switch (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM)) /** @todo PAGE FLAGS */
|
---|
1863 | {
|
---|
1864 | /*
|
---|
1865 | * Normal memory or ROM.
|
---|
1866 | */
|
---|
1867 | case 0:
|
---|
1868 | case MM_RAM_FLAGS_ROM:
|
---|
1869 | case MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_RESERVED:
|
---|
1870 | //case MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2: /* = shadow */ - //MMIO2 isn't in the mask.
|
---|
1871 | case MM_RAM_FLAGS_MMIO2: // MMIO2 isn't in the mask.
|
---|
1872 | {
|
---|
1873 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1874 | void *pvSrc = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
|
---|
1875 | #else
|
---|
1876 | void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1877 | #endif
|
---|
1878 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1879 | if (cb >= cbRead)
|
---|
1880 | {
|
---|
1881 | #if defined(IN_RING3) && defined(PGM_PHYSMEMACCESS_CACHING)
|
---|
1882 | if (cbRead <= 4 && !fGrabbedLock /* i.e. EMT */)
|
---|
1883 | pgmPhysCacheAdd(pVM, &pVM->pgm.s.pgmphysreadcache, GCPhys, (uint8_t*)pvSrc);
|
---|
1884 | #endif /* IN_RING3 && PGM_PHYSMEMACCESS_CACHING */
|
---|
1885 | memcpy(pvBuf, pvSrc, cbRead);
|
---|
1886 | goto l_End;
|
---|
1887 | }
|
---|
1888 | memcpy(pvBuf, pvSrc, cb);
|
---|
1889 | break;
|
---|
1890 | }
|
---|
1891 |
|
---|
1892 | /*
|
---|
1893 | * All reserved, nothing there.
|
---|
1894 | */
|
---|
1895 | case MM_RAM_FLAGS_RESERVED:
|
---|
1896 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1897 | if (cb >= cbRead)
|
---|
1898 | {
|
---|
1899 | memset(pvBuf, 0, cbRead);
|
---|
1900 | goto l_End;
|
---|
1901 | }
|
---|
1902 | memset(pvBuf, 0, cb);
|
---|
1903 | break;
|
---|
1904 |
|
---|
1905 | /*
|
---|
1906 | * The rest needs to be taken more carefully.
|
---|
1907 | */
|
---|
1908 | default:
|
---|
1909 | #if 1 /** @todo r=bird: Can you do this properly please. */
|
---|
1910 | /** @todo Try MMIO; quick hack */
|
---|
1911 | if (cbRead <= 8 && IOMMMIORead(pVM, GCPhys, (uint32_t *)pvBuf, cbRead) == VINF_SUCCESS)
|
---|
1912 | goto l_End;
|
---|
1913 | #endif
|
---|
1914 |
|
---|
1915 | /** @todo fix me later. */
|
---|
1916 | AssertReleaseMsgFailed(("Unknown read at %RGp size %u implement the complex physical reading case %RHp\n",
|
---|
1917 | GCPhys, cbRead,
|
---|
1918 | pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM))); /** @todo PAGE FLAGS */
|
---|
1919 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1920 | break;
|
---|
1921 | }
|
---|
1922 | }
|
---|
1923 |
|
---|
1924 | cbRead -= cb;
|
---|
1925 | off += cb;
|
---|
1926 | pvBuf = (char *)pvBuf + cb;
|
---|
1927 | }
|
---|
1928 |
|
---|
1929 | GCPhys = pRam->GCPhysLast + 1;
|
---|
1930 | }
|
---|
1931 | else
|
---|
1932 | {
|
---|
1933 | LogFlow(("PGMPhysRead: Unassigned %RGp size=%u\n", GCPhys, cbRead));
|
---|
1934 |
|
---|
1935 | /*
|
---|
1936 | * Unassigned address space.
|
---|
1937 | */
|
---|
1938 | size_t cb;
|
---|
1939 | if ( !pRam
|
---|
1940 | || (cb = pRam->GCPhys - GCPhys) >= cbRead)
|
---|
1941 | {
|
---|
1942 | memset(pvBuf, 0, cbRead);
|
---|
1943 | goto l_End;
|
---|
1944 | }
|
---|
1945 |
|
---|
1946 | memset(pvBuf, 0, cb); /** @todo this is wrong, unassigne == 0xff not 0x00! */
|
---|
1947 | cbRead -= cb;
|
---|
1948 | pvBuf = (char *)pvBuf + cb;
|
---|
1949 | GCPhys += cb;
|
---|
1950 | }
|
---|
1951 | }
|
---|
1952 | l_End:
|
---|
1953 | #ifdef IN_RING3
|
---|
1954 | if (fGrabbedLock)
|
---|
1955 | pgmUnlock(pVM);
|
---|
1956 | #endif
|
---|
1957 | return;
|
---|
1958 | }
|
---|
1959 |
|
---|
1960 | #endif /* Old PGMPhysRead */
|
---|
1961 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
1962 |
|
---|
1963 | /**
|
---|
1964 | * Deals with writing to a page with one or more WRITE or ALL access handlers.
|
---|
1965 | *
|
---|
1966 | * @returns VBox status code. Can be ignored in ring-3.
|
---|
1967 | * @retval VINF_SUCCESS.
|
---|
1968 | * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
|
---|
1969 | *
|
---|
1970 | * @param pVM The VM handle.
|
---|
1971 | * @param pPage The page descriptor.
|
---|
1972 | * @param GCPhys The physical address to start writing at.
|
---|
1973 | * @param pvBuf What to write.
|
---|
1974 | * @param cbWrite How much to write - less or equal to a page.
|
---|
1975 | */
|
---|
1976 | static int pgmPhysWriteHandler(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void const *pvBuf, size_t cbWrite)
|
---|
1977 | {
|
---|
1978 | void *pvDst = NULL;
|
---|
1979 | int rc;
|
---|
1980 |
|
---|
1981 | /*
|
---|
1982 | * Give priority to physical handlers (like #PF does).
|
---|
1983 | *
|
---|
1984 | * Hope for a lonely physical handler first that covers the whole
|
---|
1985 | * write area. This should be a pretty frequent case with MMIO and
|
---|
1986 | * the heavy usage of full page handlers in the page pool.
|
---|
1987 | */
|
---|
1988 | if ( !PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage)
|
---|
1989 | || PGM_PAGE_IS_MMIO(pPage) /* screw virtual handlers on MMIO pages */)
|
---|
1990 | {
|
---|
1991 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
1992 | if (pCur)
|
---|
1993 | {
|
---|
1994 | Assert(GCPhys >= pCur->Core.Key && GCPhys <= pCur->Core.KeyLast);
|
---|
1995 | Assert(pCur->CTX_SUFF(pfnHandler));
|
---|
1996 |
|
---|
1997 | size_t cbRange = pCur->Core.KeyLast - GCPhys + 1;
|
---|
1998 | if (cbRange > cbWrite)
|
---|
1999 | cbRange = cbWrite;
|
---|
2000 |
|
---|
2001 | #ifndef IN_RING3
|
---|
2002 | /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
|
---|
2003 | NOREF(cbRange);
|
---|
2004 | //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
|
---|
2005 | return VERR_PGM_PHYS_WR_HIT_HANDLER;
|
---|
2006 |
|
---|
2007 | #else /* IN_RING3 */
|
---|
2008 | Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pCur->pszDesc) ));
|
---|
2009 | if (!PGM_PAGE_IS_MMIO(pPage))
|
---|
2010 | rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst);
|
---|
2011 | else
|
---|
2012 | rc = VINF_SUCCESS;
|
---|
2013 | if (RT_SUCCESS(rc))
|
---|
2014 | {
|
---|
2015 | STAM_PROFILE_START(&pCur->Stat, h);
|
---|
2016 | rc = pCur->CTX_SUFF(pfnHandler)(pVM, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, pCur->CTX_SUFF(pvUser));
|
---|
2017 | STAM_PROFILE_STOP(&pCur->Stat, h);
|
---|
2018 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
2019 | memcpy(pvDst, pvBuf, cbRange);
|
---|
2020 | else
|
---|
2021 | AssertLogRelMsg(rc == VINF_SUCCESS, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pCur->pszDesc));
|
---|
2022 | }
|
---|
2023 | else
|
---|
2024 | AssertLogRelMsgFailedReturn(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
2025 | GCPhys, pPage, rc), rc);
|
---|
2026 | if (RT_LIKELY(cbRange == cbWrite))
|
---|
2027 | return VINF_SUCCESS;
|
---|
2028 |
|
---|
2029 | /* more fun to be had below */
|
---|
2030 | cbWrite -= cbRange;
|
---|
2031 | GCPhys += cbRange;
|
---|
2032 | pvBuf = (uint8_t *)pvBuf + cbRange;
|
---|
2033 | pvDst = (uint8_t *)pvDst + cbRange;
|
---|
2034 | #endif /* IN_RING3 */
|
---|
2035 | }
|
---|
2036 | /* else: the handler is somewhere else in the page, deal with it below. */
|
---|
2037 | Assert(!PGM_PAGE_IS_MMIO(pPage)); /* MMIO handlers are all PAGE_SIZEed! */
|
---|
2038 | }
|
---|
2039 | /*
|
---|
2040 | * A virtual handler without any interfering physical handlers.
|
---|
2041 | * Hopefully it'll conver the whole write.
|
---|
2042 | */
|
---|
2043 | else if (!PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage))
|
---|
2044 | {
|
---|
2045 | unsigned iPage;
|
---|
2046 | PPGMVIRTHANDLER pCur;
|
---|
2047 | rc = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pCur, &iPage);
|
---|
2048 | if (RT_SUCCESS(rc))
|
---|
2049 | {
|
---|
2050 | size_t cbRange = (PAGE_OFFSET_MASK & pCur->Core.KeyLast) - (PAGE_OFFSET_MASK & GCPhys) + 1;
|
---|
2051 | if (cbRange > cbWrite)
|
---|
2052 | cbRange = cbWrite;
|
---|
2053 |
|
---|
2054 | #ifndef IN_RING3
|
---|
2055 | /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
|
---|
2056 | NOREF(cbRange);
|
---|
2057 | //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
|
---|
2058 | return VERR_PGM_PHYS_WR_HIT_HANDLER;
|
---|
2059 |
|
---|
2060 | #else /* IN_RING3 */
|
---|
2061 |
|
---|
2062 | Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] virt %s\n", GCPhys, cbRange, pPage, R3STRING(pCur->pszDesc) ));
|
---|
2063 | rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst);
|
---|
2064 | if (RT_SUCCESS(rc))
|
---|
2065 | {
|
---|
2066 | rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
2067 | if (pCur->pfnHandlerR3)
|
---|
2068 | {
|
---|
2069 | RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pCur->Core.Key & PAGE_BASE_GC_MASK)
|
---|
2070 | + (iPage << PAGE_SHIFT)
|
---|
2071 | + (GCPhys & PAGE_OFFSET_MASK);
|
---|
2072 |
|
---|
2073 | STAM_PROFILE_START(&pCur->Stat, h);
|
---|
2074 | rc = pCur->CTX_SUFF(pfnHandler)(pVM, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, /*pCur->CTX_SUFF(pvUser)*/ NULL);
|
---|
2075 | STAM_PROFILE_STOP(&pCur->Stat, h);
|
---|
2076 | }
|
---|
2077 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
2078 | memcpy(pvDst, pvBuf, cbRange);
|
---|
2079 | else
|
---|
2080 | AssertLogRelMsg(rc == VINF_SUCCESS, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pCur->pszDesc));
|
---|
2081 | }
|
---|
2082 | else
|
---|
2083 | AssertLogRelMsgFailedReturn(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
2084 | GCPhys, pPage, rc), rc);
|
---|
2085 | if (RT_LIKELY(cbRange == cbWrite))
|
---|
2086 | return VINF_SUCCESS;
|
---|
2087 |
|
---|
2088 | /* more fun to be had below */
|
---|
2089 | cbWrite -= cbRange;
|
---|
2090 | GCPhys += cbRange;
|
---|
2091 | pvBuf = (uint8_t *)pvBuf + cbRange;
|
---|
2092 | pvDst = (uint8_t *)pvDst + cbRange;
|
---|
2093 | #endif
|
---|
2094 | }
|
---|
2095 | /* else: the handler is somewhere else in the page, deal with it below. */
|
---|
2096 | }
|
---|
2097 |
|
---|
2098 | /*
|
---|
2099 | * Deal with all the odd ends.
|
---|
2100 | */
|
---|
2101 |
|
---|
2102 | /* We need a writable destination page. */
|
---|
2103 | if (!pvDst)
|
---|
2104 | {
|
---|
2105 | rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDst);
|
---|
2106 | AssertLogRelMsgReturn(RT_SUCCESS(rc),
|
---|
2107 | ("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
2108 | GCPhys, pPage, rc), rc);
|
---|
2109 | }
|
---|
2110 |
|
---|
2111 | /* The loop state (big + ugly). */
|
---|
2112 | unsigned iVirtPage = 0;
|
---|
2113 | PPGMVIRTHANDLER pVirt = NULL;
|
---|
2114 | uint32_t offVirt = PAGE_SIZE;
|
---|
2115 | uint32_t offVirtLast = PAGE_SIZE;
|
---|
2116 | bool fMoreVirt = PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage);
|
---|
2117 |
|
---|
2118 | PPGMPHYSHANDLER pPhys = NULL;
|
---|
2119 | uint32_t offPhys = PAGE_SIZE;
|
---|
2120 | uint32_t offPhysLast = PAGE_SIZE;
|
---|
2121 | bool fMorePhys = PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage);
|
---|
2122 |
|
---|
2123 | /* The loop. */
|
---|
2124 | for (;;)
|
---|
2125 | {
|
---|
2126 | /*
|
---|
2127 | * Find the closest handler at or above GCPhys.
|
---|
2128 | */
|
---|
2129 | if (fMoreVirt && !pVirt)
|
---|
2130 | {
|
---|
2131 | int rc = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pVirt, &iVirtPage);
|
---|
2132 | if (RT_SUCCESS(rc))
|
---|
2133 | {
|
---|
2134 | offVirt = 0;
|
---|
2135 | offVirtLast = (pVirt->aPhysToVirt[iVirtPage].Core.KeyLast & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
|
---|
2136 | }
|
---|
2137 | else
|
---|
2138 | {
|
---|
2139 | PPGMPHYS2VIRTHANDLER pVirtPhys;
|
---|
2140 | pVirtPhys = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers,
|
---|
2141 | GCPhys, true /* fAbove */);
|
---|
2142 | if ( pVirtPhys
|
---|
2143 | && (pVirtPhys->Core.Key >> PAGE_SHIFT) == (GCPhys >> PAGE_SHIFT))
|
---|
2144 | {
|
---|
2145 | /* ASSUME that pVirtPhys only covers one page. */
|
---|
2146 | Assert((pVirtPhys->Core.Key >> PAGE_SHIFT) == (pVirtPhys->Core.KeyLast >> PAGE_SHIFT));
|
---|
2147 | Assert(pVirtPhys->Core.Key > GCPhys);
|
---|
2148 |
|
---|
2149 | pVirt = (PPGMVIRTHANDLER)((uintptr_t)pVirtPhys + pVirtPhys->offVirtHandler);
|
---|
2150 | iVirtPage = pVirtPhys - &pVirt->aPhysToVirt[0]; Assert(iVirtPage == 0);
|
---|
2151 | offVirt = (pVirtPhys->Core.Key & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
|
---|
2152 | offVirtLast = (pVirtPhys->Core.KeyLast & PAGE_OFFSET_MASK) - (GCPhys & PAGE_OFFSET_MASK);
|
---|
2153 | }
|
---|
2154 | else
|
---|
2155 | {
|
---|
2156 | pVirt = NULL;
|
---|
2157 | fMoreVirt = false;
|
---|
2158 | offVirt = offVirtLast = PAGE_SIZE;
|
---|
2159 | }
|
---|
2160 | }
|
---|
2161 | }
|
---|
2162 |
|
---|
2163 | if (fMorePhys && !pPhys)
|
---|
2164 | {
|
---|
2165 | pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
2166 | if (pPhys)
|
---|
2167 | {
|
---|
2168 | offPhys = 0;
|
---|
2169 | offPhysLast = pPhys->Core.KeyLast - GCPhys; /* ASSUMES < 4GB handlers... */
|
---|
2170 | }
|
---|
2171 | else
|
---|
2172 | {
|
---|
2173 | pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers,
|
---|
2174 | GCPhys, true /* fAbove */);
|
---|
2175 | if ( pPhys
|
---|
2176 | && pPhys->Core.Key <= GCPhys + (cbWrite - 1))
|
---|
2177 | {
|
---|
2178 | offPhys = pPhys->Core.Key - GCPhys;
|
---|
2179 | offPhysLast = pPhys->Core.KeyLast - GCPhys; /* ASSUMES < 4GB handlers... */
|
---|
2180 | }
|
---|
2181 | else
|
---|
2182 | {
|
---|
2183 | pPhys = NULL;
|
---|
2184 | fMorePhys = false;
|
---|
2185 | offPhys = offPhysLast = PAGE_SIZE;
|
---|
2186 | }
|
---|
2187 | }
|
---|
2188 | }
|
---|
2189 |
|
---|
2190 | /*
|
---|
2191 | * Handle access to space without handlers (that's easy).
|
---|
2192 | */
|
---|
2193 | rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
2194 | uint32_t cbRange = (uint32_t)cbWrite;
|
---|
2195 | if (offPhys && offVirt)
|
---|
2196 | {
|
---|
2197 | if (cbRange > offPhys)
|
---|
2198 | cbRange = offPhys;
|
---|
2199 | if (cbRange > offVirt)
|
---|
2200 | cbRange = offVirt;
|
---|
2201 | Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] miss\n", GCPhys, cbRange, pPage));
|
---|
2202 | }
|
---|
2203 | /*
|
---|
2204 | * Physical handler.
|
---|
2205 | */
|
---|
2206 | else if (!offPhys && offVirt)
|
---|
2207 | {
|
---|
2208 | if (cbRange > offPhysLast + 1)
|
---|
2209 | cbRange = offPhysLast + 1;
|
---|
2210 | if (cbRange > offVirt)
|
---|
2211 | cbRange = offVirt;
|
---|
2212 | #ifdef IN_RING3
|
---|
2213 | Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pPhys->pszDesc) ));
|
---|
2214 | STAM_PROFILE_START(&pPhys->Stat, h);
|
---|
2215 | rc = pPhys->CTX_SUFF(pfnHandler)(pVM, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, pPhys->CTX_SUFF(pvUser));
|
---|
2216 | STAM_PROFILE_STOP(&pPhys->Stat, h);
|
---|
2217 | AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pPhys->pszDesc));
|
---|
2218 | pPhys = NULL;
|
---|
2219 | #else
|
---|
2220 | /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
|
---|
2221 | NOREF(cbRange);
|
---|
2222 | //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
|
---|
2223 | return VERR_PGM_PHYS_WR_HIT_HANDLER;
|
---|
2224 | #endif
|
---|
2225 | }
|
---|
2226 | /*
|
---|
2227 | * Virtual handler.
|
---|
2228 | */
|
---|
2229 | else if (offPhys && !offVirt)
|
---|
2230 | {
|
---|
2231 | if (cbRange > offVirtLast + 1)
|
---|
2232 | cbRange = offVirtLast + 1;
|
---|
2233 | if (cbRange > offPhys)
|
---|
2234 | cbRange = offPhys;
|
---|
2235 | #ifdef IN_RING3
|
---|
2236 | Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys %s\n", GCPhys, cbRange, pPage, R3STRING(pVirt->pszDesc) ));
|
---|
2237 | if (pVirt->pfnHandlerR3)
|
---|
2238 | {
|
---|
2239 | RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
|
---|
2240 | + (iVirtPage << PAGE_SHIFT)
|
---|
2241 | + (GCPhys & PAGE_OFFSET_MASK);
|
---|
2242 | STAM_PROFILE_START(&pVirt->Stat, h);
|
---|
2243 | rc = pVirt->CTX_SUFF(pfnHandler)(pVM, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, /*pCur->CTX_SUFF(pvUser)*/ NULL);
|
---|
2244 | STAM_PROFILE_STOP(&pVirt->Stat, h);
|
---|
2245 | AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pVirt->pszDesc));
|
---|
2246 | }
|
---|
2247 | pVirt = NULL;
|
---|
2248 | #else
|
---|
2249 | /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
|
---|
2250 | NOREF(cbRange);
|
---|
2251 | //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
|
---|
2252 | return VERR_PGM_PHYS_WR_HIT_HANDLER;
|
---|
2253 | #endif
|
---|
2254 | }
|
---|
2255 | /*
|
---|
2256 | * Both... give the physical one priority.
|
---|
2257 | */
|
---|
2258 | else
|
---|
2259 | {
|
---|
2260 | Assert(!offPhys && !offVirt);
|
---|
2261 | if (cbRange > offVirtLast + 1)
|
---|
2262 | cbRange = offVirtLast + 1;
|
---|
2263 | if (cbRange > offPhysLast + 1)
|
---|
2264 | cbRange = offPhysLast + 1;
|
---|
2265 |
|
---|
2266 | #ifdef IN_RING3
|
---|
2267 | if (pVirt->pfnHandlerR3)
|
---|
2268 | Log(("pgmPhysWriteHandler: overlapping phys and virt handlers at %RGp %R[pgmpage]; cbRange=%#x\n", GCPhys, pPage, cbRange));
|
---|
2269 | Log5(("pgmPhysWriteHandler: GCPhys=%RGp cbRange=%#x pPage=%R[pgmpage] phys/virt %s/%s\n", GCPhys, cbRange, pPage, R3STRING(pPhys->pszDesc), R3STRING(pVirt->pszDesc) ));
|
---|
2270 |
|
---|
2271 | STAM_PROFILE_START(&pPhys->Stat, h);
|
---|
2272 | rc = pPhys->CTX_SUFF(pfnHandler)(pVM, GCPhys, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, pPhys->CTX_SUFF(pvUser));
|
---|
2273 | STAM_PROFILE_STOP(&pPhys->Stat, h);
|
---|
2274 | AssertLogRelMsg(rc == VINF_SUCCESS || rc == VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pPhys->pszDesc));
|
---|
2275 | if (pVirt->pfnHandlerR3)
|
---|
2276 | {
|
---|
2277 |
|
---|
2278 | RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirt->Core.Key & PAGE_BASE_GC_MASK)
|
---|
2279 | + (iVirtPage << PAGE_SHIFT)
|
---|
2280 | + (GCPhys & PAGE_OFFSET_MASK);
|
---|
2281 | STAM_PROFILE_START(&pVirt->Stat, h);
|
---|
2282 | int rc2 = pVirt->CTX_SUFF(pfnHandler)(pVM, GCPtr, pvDst, (void *)pvBuf, cbRange, PGMACCESSTYPE_WRITE, /*pCur->CTX_SUFF(pvUser)*/ NULL);
|
---|
2283 | STAM_PROFILE_STOP(&pVirt->Stat, h);
|
---|
2284 | AssertLogRelMsg(rc2 != VINF_SUCCESS && rc2 != VINF_PGM_HANDLER_DO_DEFAULT, ("rc=%Rrc GCPhys=%RGp pPage=%R[pgmpage] %s\n", rc, GCPhys, pPage, pVirt->pszDesc));
|
---|
2285 | if (rc2 == VINF_SUCCESS && rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
2286 | rc = VINF_SUCCESS;
|
---|
2287 | }
|
---|
2288 | pPhys = NULL;
|
---|
2289 | pVirt = NULL;
|
---|
2290 | #else
|
---|
2291 | /* In R0 and RC the callbacks cannot handle this context, so we'll fail. */
|
---|
2292 | NOREF(cbRange);
|
---|
2293 | //AssertReleaseMsgFailed(("Wrong API! GCPhys=%RGp cbRange=%#x\n", GCPhys, cbRange));
|
---|
2294 | return VERR_PGM_PHYS_WR_HIT_HANDLER;
|
---|
2295 | #endif
|
---|
2296 | }
|
---|
2297 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
2298 | memcpy(pvDst, pvBuf, cbRange);
|
---|
2299 |
|
---|
2300 | /*
|
---|
2301 | * Advance if we've got more stuff to do.
|
---|
2302 | */
|
---|
2303 | if (cbRange >= cbWrite)
|
---|
2304 | return VINF_SUCCESS;
|
---|
2305 |
|
---|
2306 | cbWrite -= cbRange;
|
---|
2307 | GCPhys += cbRange;
|
---|
2308 | pvBuf = (uint8_t *)pvBuf + cbRange;
|
---|
2309 | pvDst = (uint8_t *)pvDst + cbRange;
|
---|
2310 |
|
---|
2311 | offPhys -= cbRange;
|
---|
2312 | offPhysLast -= cbRange;
|
---|
2313 | offVirt -= cbRange;
|
---|
2314 | offVirtLast -= cbRange;
|
---|
2315 | }
|
---|
2316 | }
|
---|
2317 |
|
---|
2318 |
|
---|
2319 | /**
|
---|
2320 | * Write to physical memory.
|
---|
2321 | *
|
---|
2322 | * This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
|
---|
2323 | * want to ignore those.
|
---|
2324 | *
|
---|
2325 | * @returns VBox status code. Can be ignored in ring-3.
|
---|
2326 | * @retval VINF_SUCCESS.
|
---|
2327 | * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
|
---|
2328 | *
|
---|
2329 | * @param pVM VM Handle.
|
---|
2330 | * @param GCPhys Physical address to write to.
|
---|
2331 | * @param pvBuf What to write.
|
---|
2332 | * @param cbWrite How many bytes to write.
|
---|
2333 | */
|
---|
2334 | VMMDECL(int) PGMPhysWrite(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite)
|
---|
2335 | {
|
---|
2336 | AssertMsg(!pVM->pgm.s.fNoMorePhysWrites, ("Calling PGMPhysWrite after pgmR3Save()!\n"));
|
---|
2337 | AssertMsgReturn(cbWrite > 0, ("don't even think about writing zero bytes!\n"), VINF_SUCCESS);
|
---|
2338 | LogFlow(("PGMPhysWrite: %RGp %d\n", GCPhys, cbWrite));
|
---|
2339 |
|
---|
2340 | pgmLock(pVM);
|
---|
2341 |
|
---|
2342 | /*
|
---|
2343 | * Copy loop on ram ranges.
|
---|
2344 | */
|
---|
2345 | PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
2346 | for (;;)
|
---|
2347 | {
|
---|
2348 | /* Find range. */
|
---|
2349 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
2350 | pRam = pRam->CTX_SUFF(pNext);
|
---|
2351 | /* Inside range or not? */
|
---|
2352 | if (pRam && GCPhys >= pRam->GCPhys)
|
---|
2353 | {
|
---|
2354 | /*
|
---|
2355 | * Must work our way thru this page by page.
|
---|
2356 | */
|
---|
2357 | RTGCPTR off = GCPhys - pRam->GCPhys;
|
---|
2358 | while (off < pRam->cb)
|
---|
2359 | {
|
---|
2360 | RTGCPTR iPage = off >> PAGE_SHIFT;
|
---|
2361 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
2362 | size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
2363 | if (cb > cbWrite)
|
---|
2364 | cb = cbWrite;
|
---|
2365 |
|
---|
2366 | /*
|
---|
2367 | * Any active WRITE or ALL access handlers?
|
---|
2368 | */
|
---|
2369 | if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage))
|
---|
2370 | {
|
---|
2371 | int rc = pgmPhysWriteHandler(pVM, pPage, pRam->GCPhys + off, pvBuf, cb);
|
---|
2372 | if (RT_FAILURE(rc))
|
---|
2373 | return rc;
|
---|
2374 | }
|
---|
2375 | else
|
---|
2376 | {
|
---|
2377 | /*
|
---|
2378 | * Get the pointer to the page.
|
---|
2379 | */
|
---|
2380 | void *pvDst;
|
---|
2381 | int rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, pRam->GCPhys + off, &pvDst);
|
---|
2382 | if (RT_SUCCESS(rc))
|
---|
2383 | memcpy(pvDst, pvBuf, cb);
|
---|
2384 | else
|
---|
2385 | AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
|
---|
2386 | pRam->GCPhys + off, pPage, rc));
|
---|
2387 | }
|
---|
2388 |
|
---|
2389 | /* next page */
|
---|
2390 | if (cb >= cbWrite)
|
---|
2391 | {
|
---|
2392 | pgmUnlock(pVM);
|
---|
2393 | return VINF_SUCCESS;
|
---|
2394 | }
|
---|
2395 |
|
---|
2396 | cbWrite -= cb;
|
---|
2397 | off += cb;
|
---|
2398 | pvBuf = (const char *)pvBuf + cb;
|
---|
2399 | } /* walk pages in ram range */
|
---|
2400 |
|
---|
2401 | GCPhys = pRam->GCPhysLast + 1;
|
---|
2402 | }
|
---|
2403 | else
|
---|
2404 | {
|
---|
2405 | /*
|
---|
2406 | * Unassigned address space, skip it.
|
---|
2407 | */
|
---|
2408 | if (!pRam)
|
---|
2409 | break;
|
---|
2410 | size_t cb = pRam->GCPhys - GCPhys;
|
---|
2411 | if (cb >= cbWrite)
|
---|
2412 | break;
|
---|
2413 | cbWrite -= cb;
|
---|
2414 | pvBuf = (const char *)pvBuf + cb;
|
---|
2415 | GCPhys += cb;
|
---|
2416 | }
|
---|
2417 | } /* Ram range walk */
|
---|
2418 |
|
---|
2419 | pgmUnlock(pVM);
|
---|
2420 | return VINF_SUCCESS;
|
---|
2421 | }
|
---|
2422 |
|
---|
2423 | #else /* Old PGMPhysWrite */
|
---|
2424 |
|
---|
2425 | /**
|
---|
2426 | * Write to physical memory.
|
---|
2427 | *
|
---|
2428 | * This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
|
---|
2429 | * want to ignore those.
|
---|
2430 | *
|
---|
2431 | * @param pVM VM Handle.
|
---|
2432 | * @param GCPhys Physical address to write to.
|
---|
2433 | * @param pvBuf What to write.
|
---|
2434 | * @param cbWrite How many bytes to write.
|
---|
2435 | */
|
---|
2436 | VMMDECL(void) PGMPhysWrite(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite)
|
---|
2437 | {
|
---|
2438 | #ifdef IN_RING3
|
---|
2439 | bool fGrabbedLock = false;
|
---|
2440 | #endif
|
---|
2441 |
|
---|
2442 | AssertMsg(!pVM->pgm.s.fNoMorePhysWrites, ("Calling PGMPhysWrite after pgmR3Save()!\n"));
|
---|
2443 | AssertMsg(cbWrite > 0, ("don't even think about writing zero bytes!\n"));
|
---|
2444 | if (cbWrite == 0)
|
---|
2445 | return;
|
---|
2446 |
|
---|
2447 | LogFlow(("PGMPhysWrite: %RGp %d\n", GCPhys, cbWrite));
|
---|
2448 |
|
---|
2449 | #ifdef IN_RING3
|
---|
2450 | if (!VM_IS_EMT(pVM))
|
---|
2451 | {
|
---|
2452 | pgmLock(pVM);
|
---|
2453 | fGrabbedLock = true;
|
---|
2454 | }
|
---|
2455 | #endif
|
---|
2456 | /*
|
---|
2457 | * Copy loop on ram ranges.
|
---|
2458 | */
|
---|
2459 | PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
2460 | for (;;)
|
---|
2461 | {
|
---|
2462 | /* Find range. */
|
---|
2463 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
2464 | pRam = pRam->CTX_SUFF(pNext);
|
---|
2465 | /* Inside range or not? */
|
---|
2466 | if (pRam && GCPhys >= pRam->GCPhys)
|
---|
2467 | {
|
---|
2468 | /*
|
---|
2469 | * Must work our way thru this page by page.
|
---|
2470 | */
|
---|
2471 | RTGCPTR off = GCPhys - pRam->GCPhys;
|
---|
2472 | while (off < pRam->cb)
|
---|
2473 | {
|
---|
2474 | RTGCPTR iPage = off >> PAGE_SHIFT;
|
---|
2475 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
2476 |
|
---|
2477 | /* Physical chunk in dynamically allocated range not present? */
|
---|
2478 | if (RT_UNLIKELY(!PGM_PAGE_GET_HCPHYS(pPage)))
|
---|
2479 | {
|
---|
2480 | int rc;
|
---|
2481 | RTGCPHYS GCPhysPage = pRam->GCPhys + off;
|
---|
2482 | #ifdef IN_RING3
|
---|
2483 | if (fGrabbedLock)
|
---|
2484 | {
|
---|
2485 | pgmUnlock(pVM);
|
---|
2486 | rc = pgmr3PhysGrowRange(pVM, GCPhysPage);
|
---|
2487 | if (rc == VINF_SUCCESS)
|
---|
2488 | PGMPhysWrite(pVM, GCPhysPage, pvBuf, cbWrite); /* try again; can't assume pRam is still valid (paranoia) */
|
---|
2489 | return;
|
---|
2490 | }
|
---|
2491 | rc = pgmr3PhysGrowRange(pVM, GCPhysPage);
|
---|
2492 | #else
|
---|
2493 | rc = CTXALLMID(VMM, CallHost)(pVM, VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhysPage);
|
---|
2494 | #endif
|
---|
2495 | if (rc != VINF_SUCCESS)
|
---|
2496 | goto l_End;
|
---|
2497 | }
|
---|
2498 |
|
---|
2499 | size_t cb;
|
---|
2500 | /* temporary hack, will reogranize is later. */
|
---|
2501 | /*
|
---|
2502 | * Virtual handlers
|
---|
2503 | */
|
---|
2504 | if ( PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage)
|
---|
2505 | && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
|
---|
2506 | {
|
---|
2507 | if (PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage))
|
---|
2508 | {
|
---|
2509 | /*
|
---|
2510 | * Physical write handler + virtual write handler.
|
---|
2511 | * Consider this a quick workaround for the CSAM + shadow caching problem.
|
---|
2512 | *
|
---|
2513 | * We hand it to the shadow caching first since it requires the unchanged
|
---|
2514 | * data. CSAM will have to put up with it already being changed.
|
---|
2515 | */
|
---|
2516 | int rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
2517 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
2518 | #ifdef IN_RING3 /** @todo deal with this in GC and R0! */
|
---|
2519 | /* 1. The physical handler */
|
---|
2520 | PPGMPHYSHANDLER pPhysNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesR3->PhysHandlers, GCPhys);
|
---|
2521 | if (pPhysNode && pPhysNode->pfnHandlerR3)
|
---|
2522 | {
|
---|
2523 | size_t cbRange = pPhysNode->Core.KeyLast - GCPhys + 1;
|
---|
2524 | if (cbRange < cb)
|
---|
2525 | cb = cbRange;
|
---|
2526 | if (cb > cbWrite)
|
---|
2527 | cb = cbWrite;
|
---|
2528 |
|
---|
2529 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
2530 |
|
---|
2531 | /* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
|
---|
2532 | rc = pPhysNode->pfnHandlerR3(pVM, GCPhys, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, pPhysNode->pvUserR3);
|
---|
2533 | }
|
---|
2534 |
|
---|
2535 | /* 2. The virtual handler (will see incorrect data) */
|
---|
2536 | PPGMVIRTHANDLER pVirtNode;
|
---|
2537 | unsigned iPage;
|
---|
2538 | int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pVirtNode, &iPage);
|
---|
2539 | if (RT_SUCCESS(rc2) && pVirtNode->pfnHandlerR3)
|
---|
2540 | {
|
---|
2541 | size_t cbRange = pVirtNode->Core.KeyLast - GCPhys + 1;
|
---|
2542 | if (cbRange < cb)
|
---|
2543 | cb = cbRange;
|
---|
2544 | if (cb > cbWrite)
|
---|
2545 | cb = cbWrite;
|
---|
2546 | RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirtNode->Core.Key & PAGE_BASE_GC_MASK)
|
---|
2547 | + (iPage << PAGE_SHIFT) + (off & PAGE_OFFSET_MASK);
|
---|
2548 |
|
---|
2549 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
2550 |
|
---|
2551 | /* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
|
---|
2552 | rc2 = pVirtNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, 0);
|
---|
2553 | if ( ( rc2 != VINF_PGM_HANDLER_DO_DEFAULT
|
---|
2554 | && rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
2555 | || ( RT_FAILURE(rc2)
|
---|
2556 | && RT_SUCCESS(rc)))
|
---|
2557 | rc = rc2;
|
---|
2558 | }
|
---|
2559 | #endif /* IN_RING3 */
|
---|
2560 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
2561 | {
|
---|
2562 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
2563 | void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
|
---|
2564 | #else
|
---|
2565 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
2566 | #endif
|
---|
2567 | if (cb >= cbWrite)
|
---|
2568 | {
|
---|
2569 | memcpy(pvDst, pvBuf, cbWrite);
|
---|
2570 | goto l_End;
|
---|
2571 | }
|
---|
2572 | memcpy(pvDst, pvBuf, cb);
|
---|
2573 | }
|
---|
2574 | else if (cb >= cbWrite)
|
---|
2575 | goto l_End;
|
---|
2576 | }
|
---|
2577 | else
|
---|
2578 | {
|
---|
2579 | int rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
2580 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
2581 | #ifdef IN_RING3
|
---|
2582 | /** @todo deal with this in GC and R0! */
|
---|
2583 | /* Search the whole tree for matching physical addresses (rather expensive!) */
|
---|
2584 | PPGMVIRTHANDLER pNode;
|
---|
2585 | unsigned iPage;
|
---|
2586 | int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pNode, &iPage);
|
---|
2587 | if (RT_SUCCESS(rc2) && pNode->pfnHandlerR3)
|
---|
2588 | {
|
---|
2589 | size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
|
---|
2590 | if (cbRange < cb)
|
---|
2591 | cb = cbRange;
|
---|
2592 | if (cb > cbWrite)
|
---|
2593 | cb = cbWrite;
|
---|
2594 | RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pNode->Core.Key & PAGE_BASE_GC_MASK)
|
---|
2595 | + (iPage << PAGE_SHIFT) + (off & PAGE_OFFSET_MASK);
|
---|
2596 |
|
---|
2597 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
2598 |
|
---|
2599 | /* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
|
---|
2600 | rc = pNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, 0);
|
---|
2601 | }
|
---|
2602 | #endif /* IN_RING3 */
|
---|
2603 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
2604 | {
|
---|
2605 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
2606 | void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
|
---|
2607 | #else
|
---|
2608 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
2609 | #endif
|
---|
2610 | if (cb >= cbWrite)
|
---|
2611 | {
|
---|
2612 | memcpy(pvDst, pvBuf, cbWrite);
|
---|
2613 | goto l_End;
|
---|
2614 | }
|
---|
2615 | memcpy(pvDst, pvBuf, cb);
|
---|
2616 | }
|
---|
2617 | else if (cb >= cbWrite)
|
---|
2618 | goto l_End;
|
---|
2619 | }
|
---|
2620 | }
|
---|
2621 | /*
|
---|
2622 | * Physical handler.
|
---|
2623 | */
|
---|
2624 | else if ( RT_UNLIKELY(PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) >= PGM_PAGE_HNDL_PHYS_STATE_WRITE)
|
---|
2625 | && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
|
---|
2626 | {
|
---|
2627 | int rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
2628 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
2629 | #ifdef IN_RING3 /** @todo deal with this in GC and R0! */
|
---|
2630 | /* find and call the handler */
|
---|
2631 | PPGMPHYSHANDLER pNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesR3->PhysHandlers, GCPhys);
|
---|
2632 | if (pNode && pNode->pfnHandlerR3)
|
---|
2633 | {
|
---|
2634 | size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
|
---|
2635 | if (cbRange < cb)
|
---|
2636 | cb = cbRange;
|
---|
2637 | if (cb > cbWrite)
|
---|
2638 | cb = cbWrite;
|
---|
2639 |
|
---|
2640 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
2641 |
|
---|
2642 | /** @todo Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
|
---|
2643 | rc = pNode->pfnHandlerR3(pVM, GCPhys, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, pNode->pvUserR3);
|
---|
2644 | }
|
---|
2645 | #endif /* IN_RING3 */
|
---|
2646 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
2647 | {
|
---|
2648 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
2649 | void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
|
---|
2650 | #else
|
---|
2651 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
2652 | #endif
|
---|
2653 | if (cb >= cbWrite)
|
---|
2654 | {
|
---|
2655 | memcpy(pvDst, pvBuf, cbWrite);
|
---|
2656 | goto l_End;
|
---|
2657 | }
|
---|
2658 | memcpy(pvDst, pvBuf, cb);
|
---|
2659 | }
|
---|
2660 | else if (cb >= cbWrite)
|
---|
2661 | goto l_End;
|
---|
2662 | }
|
---|
2663 | else
|
---|
2664 | {
|
---|
2665 | /** @todo r=bird: missing MM_RAM_FLAGS_ROM here, we shall not allow anyone to overwrite the ROM! */
|
---|
2666 | switch (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)) /** @todo PAGE FLAGS */
|
---|
2667 | {
|
---|
2668 | /*
|
---|
2669 | * Normal memory, MMIO2 or writable shadow ROM.
|
---|
2670 | */
|
---|
2671 | case 0:
|
---|
2672 | case MM_RAM_FLAGS_MMIO2:
|
---|
2673 | case MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2: /* shadow rom */
|
---|
2674 | {
|
---|
2675 | #if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
2676 | void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
|
---|
2677 | #else
|
---|
2678 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
2679 | #endif
|
---|
2680 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
2681 | if (cb >= cbWrite)
|
---|
2682 | {
|
---|
2683 | #if defined(IN_RING3) && defined(PGM_PHYSMEMACCESS_CACHING)
|
---|
2684 | if (cbWrite <= 4 && !fGrabbedLock /* i.e. EMT */)
|
---|
2685 | pgmPhysCacheAdd(pVM, &pVM->pgm.s.pgmphyswritecache, GCPhys, (uint8_t*)pvDst);
|
---|
2686 | #endif /* IN_RING3 && PGM_PHYSMEMACCESS_CACHING */
|
---|
2687 | memcpy(pvDst, pvBuf, cbWrite);
|
---|
2688 | goto l_End;
|
---|
2689 | }
|
---|
2690 | memcpy(pvDst, pvBuf, cb);
|
---|
2691 | break;
|
---|
2692 | }
|
---|
2693 |
|
---|
2694 | /*
|
---|
2695 | * All reserved, nothing there.
|
---|
2696 | */
|
---|
2697 | case MM_RAM_FLAGS_RESERVED:
|
---|
2698 | case MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO2:
|
---|
2699 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
2700 | if (cb >= cbWrite)
|
---|
2701 | goto l_End;
|
---|
2702 | break;
|
---|
2703 |
|
---|
2704 |
|
---|
2705 | /*
|
---|
2706 | * The rest needs to be taken more carefully.
|
---|
2707 | */
|
---|
2708 | default:
|
---|
2709 | #if 1 /** @todo r=bird: Can you do this properly please. */
|
---|
2710 | /** @todo Try MMIO; quick hack */
|
---|
2711 | if (cbWrite <= 8 && IOMMMIOWrite(pVM, GCPhys, *(uint32_t *)pvBuf, cbWrite) == VINF_SUCCESS)
|
---|
2712 | goto l_End;
|
---|
2713 | #endif
|
---|
2714 |
|
---|
2715 | /** @todo fix me later. */
|
---|
2716 | AssertReleaseMsgFailed(("Unknown write at %RGp size %u implement the complex physical writing case %RHp\n",
|
---|
2717 | GCPhys, cbWrite,
|
---|
2718 | (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)))); /** @todo PAGE FLAGS */
|
---|
2719 | /* skip the write */
|
---|
2720 | cb = cbWrite;
|
---|
2721 | break;
|
---|
2722 | }
|
---|
2723 | }
|
---|
2724 |
|
---|
2725 | cbWrite -= cb;
|
---|
2726 | off += cb;
|
---|
2727 | pvBuf = (const char *)pvBuf + cb;
|
---|
2728 | }
|
---|
2729 |
|
---|
2730 | GCPhys = pRam->GCPhysLast + 1;
|
---|
2731 | }
|
---|
2732 | else
|
---|
2733 | {
|
---|
2734 | /*
|
---|
2735 | * Unassigned address space.
|
---|
2736 | */
|
---|
2737 | size_t cb;
|
---|
2738 | if ( !pRam
|
---|
2739 | || (cb = pRam->GCPhys - GCPhys) >= cbWrite)
|
---|
2740 | goto l_End;
|
---|
2741 |
|
---|
2742 | cbWrite -= cb;
|
---|
2743 | pvBuf = (const char *)pvBuf + cb;
|
---|
2744 | GCPhys += cb;
|
---|
2745 | }
|
---|
2746 | }
|
---|
2747 | l_End:
|
---|
2748 | #ifdef IN_RING3
|
---|
2749 | if (fGrabbedLock)
|
---|
2750 | pgmUnlock(pVM);
|
---|
2751 | #endif
|
---|
2752 | return;
|
---|
2753 | }
|
---|
2754 |
|
---|
2755 | #endif /* Old PGMPhysWrite */
|
---|
2756 |
|
---|
2757 |
|
---|
2758 | /**
|
---|
2759 | * Read from guest physical memory by GC physical address, bypassing
|
---|
2760 | * MMIO and access handlers.
|
---|
2761 | *
|
---|
2762 | * @returns VBox status.
|
---|
2763 | * @param pVM VM handle.
|
---|
2764 | * @param pvDst The destination address.
|
---|
2765 | * @param GCPhysSrc The source address (GC physical address).
|
---|
2766 | * @param cb The number of bytes to read.
|
---|
2767 | */
|
---|
2768 | VMMDECL(int) PGMPhysSimpleReadGCPhys(PVM pVM, void *pvDst, RTGCPHYS GCPhysSrc, size_t cb)
|
---|
2769 | {
|
---|
2770 | /*
|
---|
2771 | * Treat the first page as a special case.
|
---|
2772 | */
|
---|
2773 | if (!cb)
|
---|
2774 | return VINF_SUCCESS;
|
---|
2775 |
|
---|
2776 | /* map the 1st page */
|
---|
2777 | void const *pvSrc;
|
---|
2778 | PGMPAGEMAPLOCK Lock;
|
---|
2779 | int rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhysSrc, &pvSrc, &Lock);
|
---|
2780 | if (RT_FAILURE(rc))
|
---|
2781 | return rc;
|
---|
2782 |
|
---|
2783 | /* optimize for the case where access is completely within the first page. */
|
---|
2784 | size_t cbPage = PAGE_SIZE - (GCPhysSrc & PAGE_OFFSET_MASK);
|
---|
2785 | if (RT_LIKELY(cb <= cbPage))
|
---|
2786 | {
|
---|
2787 | memcpy(pvDst, pvSrc, cb);
|
---|
2788 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2789 | return VINF_SUCCESS;
|
---|
2790 | }
|
---|
2791 |
|
---|
2792 | /* copy to the end of the page. */
|
---|
2793 | memcpy(pvDst, pvSrc, cbPage);
|
---|
2794 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2795 | GCPhysSrc += cbPage;
|
---|
2796 | pvDst = (uint8_t *)pvDst + cbPage;
|
---|
2797 | cb -= cbPage;
|
---|
2798 |
|
---|
2799 | /*
|
---|
2800 | * Page by page.
|
---|
2801 | */
|
---|
2802 | for (;;)
|
---|
2803 | {
|
---|
2804 | /* map the page */
|
---|
2805 | rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhysSrc, &pvSrc, &Lock);
|
---|
2806 | if (RT_FAILURE(rc))
|
---|
2807 | return rc;
|
---|
2808 |
|
---|
2809 | /* last page? */
|
---|
2810 | if (cb <= PAGE_SIZE)
|
---|
2811 | {
|
---|
2812 | memcpy(pvDst, pvSrc, cb);
|
---|
2813 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2814 | return VINF_SUCCESS;
|
---|
2815 | }
|
---|
2816 |
|
---|
2817 | /* copy the entire page and advance */
|
---|
2818 | memcpy(pvDst, pvSrc, PAGE_SIZE);
|
---|
2819 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2820 | GCPhysSrc += PAGE_SIZE;
|
---|
2821 | pvDst = (uint8_t *)pvDst + PAGE_SIZE;
|
---|
2822 | cb -= PAGE_SIZE;
|
---|
2823 | }
|
---|
2824 | /* won't ever get here. */
|
---|
2825 | }
|
---|
2826 |
|
---|
2827 | #ifndef IN_RC /* Ring 0 & 3 only. (Just not needed in GC.) */
|
---|
2828 |
|
---|
2829 | /**
|
---|
2830 | * Write to guest physical memory referenced by GC pointer.
|
---|
2831 | * Write memory to GC physical address in guest physical memory.
|
---|
2832 | *
|
---|
2833 | * This will bypass MMIO and access handlers.
|
---|
2834 | *
|
---|
2835 | * @returns VBox status.
|
---|
2836 | * @param pVM VM handle.
|
---|
2837 | * @param GCPhysDst The GC physical address of the destination.
|
---|
2838 | * @param pvSrc The source buffer.
|
---|
2839 | * @param cb The number of bytes to write.
|
---|
2840 | */
|
---|
2841 | VMMDECL(int) PGMPhysSimpleWriteGCPhys(PVM pVM, RTGCPHYS GCPhysDst, const void *pvSrc, size_t cb)
|
---|
2842 | {
|
---|
2843 | LogFlow(("PGMPhysSimpleWriteGCPhys: %RGp %zu\n", GCPhysDst, cb));
|
---|
2844 |
|
---|
2845 | /*
|
---|
2846 | * Treat the first page as a special case.
|
---|
2847 | */
|
---|
2848 | if (!cb)
|
---|
2849 | return VINF_SUCCESS;
|
---|
2850 |
|
---|
2851 | /* map the 1st page */
|
---|
2852 | void *pvDst;
|
---|
2853 | PGMPAGEMAPLOCK Lock;
|
---|
2854 | int rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
|
---|
2855 | if (RT_FAILURE(rc))
|
---|
2856 | return rc;
|
---|
2857 |
|
---|
2858 | /* optimize for the case where access is completely within the first page. */
|
---|
2859 | size_t cbPage = PAGE_SIZE - (GCPhysDst & PAGE_OFFSET_MASK);
|
---|
2860 | if (RT_LIKELY(cb <= cbPage))
|
---|
2861 | {
|
---|
2862 | memcpy(pvDst, pvSrc, cb);
|
---|
2863 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2864 | return VINF_SUCCESS;
|
---|
2865 | }
|
---|
2866 |
|
---|
2867 | /* copy to the end of the page. */
|
---|
2868 | memcpy(pvDst, pvSrc, cbPage);
|
---|
2869 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2870 | GCPhysDst += cbPage;
|
---|
2871 | pvSrc = (const uint8_t *)pvSrc + cbPage;
|
---|
2872 | cb -= cbPage;
|
---|
2873 |
|
---|
2874 | /*
|
---|
2875 | * Page by page.
|
---|
2876 | */
|
---|
2877 | for (;;)
|
---|
2878 | {
|
---|
2879 | /* map the page */
|
---|
2880 | rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
|
---|
2881 | if (RT_FAILURE(rc))
|
---|
2882 | return rc;
|
---|
2883 |
|
---|
2884 | /* last page? */
|
---|
2885 | if (cb <= PAGE_SIZE)
|
---|
2886 | {
|
---|
2887 | memcpy(pvDst, pvSrc, cb);
|
---|
2888 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2889 | return VINF_SUCCESS;
|
---|
2890 | }
|
---|
2891 |
|
---|
2892 | /* copy the entire page and advance */
|
---|
2893 | memcpy(pvDst, pvSrc, PAGE_SIZE);
|
---|
2894 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2895 | GCPhysDst += PAGE_SIZE;
|
---|
2896 | pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
|
---|
2897 | cb -= PAGE_SIZE;
|
---|
2898 | }
|
---|
2899 | /* won't ever get here. */
|
---|
2900 | }
|
---|
2901 |
|
---|
2902 |
|
---|
2903 | /**
|
---|
2904 | * Read from guest physical memory referenced by GC pointer.
|
---|
2905 | *
|
---|
2906 | * This function uses the current CR3/CR0/CR4 of the guest and will
|
---|
2907 | * bypass access handlers and not set any accessed bits.
|
---|
2908 | *
|
---|
2909 | * @returns VBox status.
|
---|
2910 | * @param pVM VM handle.
|
---|
2911 | * @param pvDst The destination address.
|
---|
2912 | * @param GCPtrSrc The source address (GC pointer).
|
---|
2913 | * @param cb The number of bytes to read.
|
---|
2914 | */
|
---|
2915 | VMMDECL(int) PGMPhysSimpleReadGCPtr(PVM pVM, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
|
---|
2916 | {
|
---|
2917 | /*
|
---|
2918 | * Treat the first page as a special case.
|
---|
2919 | */
|
---|
2920 | if (!cb)
|
---|
2921 | return VINF_SUCCESS;
|
---|
2922 |
|
---|
2923 | /* map the 1st page */
|
---|
2924 | void const *pvSrc;
|
---|
2925 | PGMPAGEMAPLOCK Lock;
|
---|
2926 | int rc = PGMPhysGCPtr2CCPtrReadOnly(pVM, GCPtrSrc, &pvSrc, &Lock);
|
---|
2927 | if (RT_FAILURE(rc))
|
---|
2928 | return rc;
|
---|
2929 |
|
---|
2930 | /* optimize for the case where access is completely within the first page. */
|
---|
2931 | size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
|
---|
2932 | if (RT_LIKELY(cb <= cbPage))
|
---|
2933 | {
|
---|
2934 | memcpy(pvDst, pvSrc, cb);
|
---|
2935 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2936 | return VINF_SUCCESS;
|
---|
2937 | }
|
---|
2938 |
|
---|
2939 | /* copy to the end of the page. */
|
---|
2940 | memcpy(pvDst, pvSrc, cbPage);
|
---|
2941 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2942 | GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + cbPage);
|
---|
2943 | pvDst = (uint8_t *)pvDst + cbPage;
|
---|
2944 | cb -= cbPage;
|
---|
2945 |
|
---|
2946 | /*
|
---|
2947 | * Page by page.
|
---|
2948 | */
|
---|
2949 | for (;;)
|
---|
2950 | {
|
---|
2951 | /* map the page */
|
---|
2952 | rc = PGMPhysGCPtr2CCPtrReadOnly(pVM, GCPtrSrc, &pvSrc, &Lock);
|
---|
2953 | if (RT_FAILURE(rc))
|
---|
2954 | return rc;
|
---|
2955 |
|
---|
2956 | /* last page? */
|
---|
2957 | if (cb <= PAGE_SIZE)
|
---|
2958 | {
|
---|
2959 | memcpy(pvDst, pvSrc, cb);
|
---|
2960 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2961 | return VINF_SUCCESS;
|
---|
2962 | }
|
---|
2963 |
|
---|
2964 | /* copy the entire page and advance */
|
---|
2965 | memcpy(pvDst, pvSrc, PAGE_SIZE);
|
---|
2966 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
2967 | GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + PAGE_SIZE);
|
---|
2968 | pvDst = (uint8_t *)pvDst + PAGE_SIZE;
|
---|
2969 | cb -= PAGE_SIZE;
|
---|
2970 | }
|
---|
2971 | /* won't ever get here. */
|
---|
2972 | }
|
---|
2973 |
|
---|
2974 |
|
---|
2975 | /**
|
---|
2976 | * Write to guest physical memory referenced by GC pointer.
|
---|
2977 | *
|
---|
2978 | * This function uses the current CR3/CR0/CR4 of the guest and will
|
---|
2979 | * bypass access handlers and not set dirty or accessed bits.
|
---|
2980 | *
|
---|
2981 | * @returns VBox status.
|
---|
2982 | * @param pVM VM handle.
|
---|
2983 | * @param GCPtrDst The destination address (GC pointer).
|
---|
2984 | * @param pvSrc The source address.
|
---|
2985 | * @param cb The number of bytes to write.
|
---|
2986 | */
|
---|
2987 | VMMDECL(int) PGMPhysSimpleWriteGCPtr(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
|
---|
2988 | {
|
---|
2989 | /*
|
---|
2990 | * Treat the first page as a special case.
|
---|
2991 | */
|
---|
2992 | if (!cb)
|
---|
2993 | return VINF_SUCCESS;
|
---|
2994 |
|
---|
2995 | /* map the 1st page */
|
---|
2996 | void *pvDst;
|
---|
2997 | PGMPAGEMAPLOCK Lock;
|
---|
2998 | int rc = PGMPhysGCPtr2CCPtr(pVM, GCPtrDst, &pvDst, &Lock);
|
---|
2999 | if (RT_FAILURE(rc))
|
---|
3000 | return rc;
|
---|
3001 |
|
---|
3002 | /* optimize for the case where access is completely within the first page. */
|
---|
3003 | size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
|
---|
3004 | if (RT_LIKELY(cb <= cbPage))
|
---|
3005 | {
|
---|
3006 | memcpy(pvDst, pvSrc, cb);
|
---|
3007 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3008 | return VINF_SUCCESS;
|
---|
3009 | }
|
---|
3010 |
|
---|
3011 | /* copy to the end of the page. */
|
---|
3012 | memcpy(pvDst, pvSrc, cbPage);
|
---|
3013 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3014 | GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
|
---|
3015 | pvSrc = (const uint8_t *)pvSrc + cbPage;
|
---|
3016 | cb -= cbPage;
|
---|
3017 |
|
---|
3018 | /*
|
---|
3019 | * Page by page.
|
---|
3020 | */
|
---|
3021 | for (;;)
|
---|
3022 | {
|
---|
3023 | /* map the page */
|
---|
3024 | rc = PGMPhysGCPtr2CCPtr(pVM, GCPtrDst, &pvDst, &Lock);
|
---|
3025 | if (RT_FAILURE(rc))
|
---|
3026 | return rc;
|
---|
3027 |
|
---|
3028 | /* last page? */
|
---|
3029 | if (cb <= PAGE_SIZE)
|
---|
3030 | {
|
---|
3031 | memcpy(pvDst, pvSrc, cb);
|
---|
3032 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3033 | return VINF_SUCCESS;
|
---|
3034 | }
|
---|
3035 |
|
---|
3036 | /* copy the entire page and advance */
|
---|
3037 | memcpy(pvDst, pvSrc, PAGE_SIZE);
|
---|
3038 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3039 | GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + PAGE_SIZE);
|
---|
3040 | pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
|
---|
3041 | cb -= PAGE_SIZE;
|
---|
3042 | }
|
---|
3043 | /* won't ever get here. */
|
---|
3044 | }
|
---|
3045 |
|
---|
3046 |
|
---|
3047 | /**
|
---|
3048 | * Write to guest physical memory referenced by GC pointer and update the PTE.
|
---|
3049 | *
|
---|
3050 | * This function uses the current CR3/CR0/CR4 of the guest and will
|
---|
3051 | * bypass access handlers but will set any dirty and accessed bits in the PTE.
|
---|
3052 | *
|
---|
3053 | * If you don't want to set the dirty bit, use PGMPhysSimpleWriteGCPtr().
|
---|
3054 | *
|
---|
3055 | * @returns VBox status.
|
---|
3056 | * @param pVM VM handle.
|
---|
3057 | * @param GCPtrDst The destination address (GC pointer).
|
---|
3058 | * @param pvSrc The source address.
|
---|
3059 | * @param cb The number of bytes to write.
|
---|
3060 | */
|
---|
3061 | VMMDECL(int) PGMPhysSimpleDirtyWriteGCPtr(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
|
---|
3062 | {
|
---|
3063 | /*
|
---|
3064 | * Treat the first page as a special case.
|
---|
3065 | * Btw. this is the same code as in PGMPhyssimpleWriteGCPtr excep for the PGMGstModifyPage.
|
---|
3066 | */
|
---|
3067 | if (!cb)
|
---|
3068 | return VINF_SUCCESS;
|
---|
3069 |
|
---|
3070 | /* map the 1st page */
|
---|
3071 | void *pvDst;
|
---|
3072 | PGMPAGEMAPLOCK Lock;
|
---|
3073 | int rc = PGMPhysGCPtr2CCPtr(pVM, GCPtrDst, &pvDst, &Lock);
|
---|
3074 | if (RT_FAILURE(rc))
|
---|
3075 | return rc;
|
---|
3076 |
|
---|
3077 | /* optimize for the case where access is completely within the first page. */
|
---|
3078 | size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
|
---|
3079 | if (RT_LIKELY(cb <= cbPage))
|
---|
3080 | {
|
---|
3081 | memcpy(pvDst, pvSrc, cb);
|
---|
3082 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3083 | rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
|
---|
3084 | return VINF_SUCCESS;
|
---|
3085 | }
|
---|
3086 |
|
---|
3087 | /* copy to the end of the page. */
|
---|
3088 | memcpy(pvDst, pvSrc, cbPage);
|
---|
3089 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3090 | rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
|
---|
3091 | GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
|
---|
3092 | pvSrc = (const uint8_t *)pvSrc + cbPage;
|
---|
3093 | cb -= cbPage;
|
---|
3094 |
|
---|
3095 | /*
|
---|
3096 | * Page by page.
|
---|
3097 | */
|
---|
3098 | for (;;)
|
---|
3099 | {
|
---|
3100 | /* map the page */
|
---|
3101 | rc = PGMPhysGCPtr2CCPtr(pVM, GCPtrDst, &pvDst, &Lock);
|
---|
3102 | if (RT_FAILURE(rc))
|
---|
3103 | return rc;
|
---|
3104 |
|
---|
3105 | /* last page? */
|
---|
3106 | if (cb <= PAGE_SIZE)
|
---|
3107 | {
|
---|
3108 | memcpy(pvDst, pvSrc, cb);
|
---|
3109 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3110 | rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
|
---|
3111 | return VINF_SUCCESS;
|
---|
3112 | }
|
---|
3113 |
|
---|
3114 | /* copy the entire page and advance */
|
---|
3115 | memcpy(pvDst, pvSrc, PAGE_SIZE);
|
---|
3116 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3117 | rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
|
---|
3118 | GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + PAGE_SIZE);
|
---|
3119 | pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
|
---|
3120 | cb -= PAGE_SIZE;
|
---|
3121 | }
|
---|
3122 | /* won't ever get here. */
|
---|
3123 | }
|
---|
3124 |
|
---|
3125 |
|
---|
3126 | /**
|
---|
3127 | * Read from guest physical memory referenced by GC pointer.
|
---|
3128 | *
|
---|
3129 | * This function uses the current CR3/CR0/CR4 of the guest and will
|
---|
3130 | * respect access handlers and set accessed bits.
|
---|
3131 | *
|
---|
3132 | * @returns VBox status.
|
---|
3133 | * @param pVM VM handle.
|
---|
3134 | * @param pvDst The destination address.
|
---|
3135 | * @param GCPtrSrc The source address (GC pointer).
|
---|
3136 | * @param cb The number of bytes to read.
|
---|
3137 | * @thread The vCPU EMT.
|
---|
3138 | */
|
---|
3139 | VMMDECL(int) PGMPhysReadGCPtr(PVM pVM, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
|
---|
3140 | {
|
---|
3141 | RTGCPHYS GCPhys;
|
---|
3142 | uint64_t fFlags;
|
---|
3143 | int rc;
|
---|
3144 |
|
---|
3145 | /*
|
---|
3146 | * Anything to do?
|
---|
3147 | */
|
---|
3148 | if (!cb)
|
---|
3149 | return VINF_SUCCESS;
|
---|
3150 |
|
---|
3151 | LogFlow(("PGMPhysReadGCPtr: %RGv %zu\n", GCPtrSrc, cb));
|
---|
3152 |
|
---|
3153 | /*
|
---|
3154 | * Optimize reads within a single page.
|
---|
3155 | */
|
---|
3156 | if (((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
|
---|
3157 | {
|
---|
3158 | /* Convert virtual to physical address + flags */
|
---|
3159 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtrSrc, &fFlags, &GCPhys);
|
---|
3160 | AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrSrc), rc);
|
---|
3161 | GCPhys |= (RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK;
|
---|
3162 |
|
---|
3163 | /* mark the guest page as accessed. */
|
---|
3164 | if (!(fFlags & X86_PTE_A))
|
---|
3165 | {
|
---|
3166 | rc = PGMGstModifyPage(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
|
---|
3167 | AssertRC(rc);
|
---|
3168 | }
|
---|
3169 |
|
---|
3170 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
3171 | return PGMPhysRead(pVM, GCPhys, pvDst, cb);
|
---|
3172 | #else
|
---|
3173 | PGMPhysRead(pVM, GCPhys, pvDst, cb);
|
---|
3174 | return VINF_SUCCESS;
|
---|
3175 | #endif
|
---|
3176 | }
|
---|
3177 |
|
---|
3178 | /*
|
---|
3179 | * Page by page.
|
---|
3180 | */
|
---|
3181 | for (;;)
|
---|
3182 | {
|
---|
3183 | /* Convert virtual to physical address + flags */
|
---|
3184 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtrSrc, &fFlags, &GCPhys);
|
---|
3185 | AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrSrc), rc);
|
---|
3186 | GCPhys |= (RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK;
|
---|
3187 |
|
---|
3188 | /* mark the guest page as accessed. */
|
---|
3189 | if (!(fFlags & X86_PTE_A))
|
---|
3190 | {
|
---|
3191 | rc = PGMGstModifyPage(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
|
---|
3192 | AssertRC(rc);
|
---|
3193 | }
|
---|
3194 |
|
---|
3195 | /* copy */
|
---|
3196 | size_t cbRead = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
|
---|
3197 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
3198 | rc = PGMPhysRead(pVM, GCPhys, pvDst, cbRead);
|
---|
3199 | if (cbRead >= cb || RT_FAILURE(rc))
|
---|
3200 | return rc;
|
---|
3201 | #else
|
---|
3202 | if (cbRead >= cb)
|
---|
3203 | {
|
---|
3204 | PGMPhysRead(pVM, GCPhys, pvDst, cb);
|
---|
3205 | return VINF_SUCCESS;
|
---|
3206 | }
|
---|
3207 | PGMPhysRead(pVM, GCPhys, pvDst, cbRead);
|
---|
3208 | #endif
|
---|
3209 |
|
---|
3210 | /* next */
|
---|
3211 | cb -= cbRead;
|
---|
3212 | pvDst = (uint8_t *)pvDst + cbRead;
|
---|
3213 | GCPtrSrc += cbRead;
|
---|
3214 | }
|
---|
3215 | }
|
---|
3216 |
|
---|
3217 |
|
---|
3218 | /**
|
---|
3219 | * Write to guest physical memory referenced by GC pointer.
|
---|
3220 | *
|
---|
3221 | * This function uses the current CR3/CR0/CR4 of the guest and will
|
---|
3222 | * respect access handlers and set dirty and accessed bits.
|
---|
3223 | *
|
---|
3224 | * @returns VBox status.
|
---|
3225 | * @retval VINF_SUCCESS.
|
---|
3226 | * @retval VERR_PGM_PHYS_WR_HIT_HANDLER in R0 and GC, NEVER in R3.
|
---|
3227 | *
|
---|
3228 | * @param pVM VM handle.
|
---|
3229 | * @param GCPtrDst The destination address (GC pointer).
|
---|
3230 | * @param pvSrc The source address.
|
---|
3231 | * @param cb The number of bytes to write.
|
---|
3232 | */
|
---|
3233 | VMMDECL(int) PGMPhysWriteGCPtr(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
|
---|
3234 | {
|
---|
3235 | RTGCPHYS GCPhys;
|
---|
3236 | uint64_t fFlags;
|
---|
3237 | int rc;
|
---|
3238 |
|
---|
3239 | /*
|
---|
3240 | * Anything to do?
|
---|
3241 | */
|
---|
3242 | if (!cb)
|
---|
3243 | return VINF_SUCCESS;
|
---|
3244 |
|
---|
3245 | LogFlow(("PGMPhysWriteGCPtr: %RGv %zu\n", GCPtrDst, cb));
|
---|
3246 |
|
---|
3247 | /*
|
---|
3248 | * Optimize writes within a single page.
|
---|
3249 | */
|
---|
3250 | if (((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
|
---|
3251 | {
|
---|
3252 | /* Convert virtual to physical address + flags */
|
---|
3253 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtrDst, &fFlags, &GCPhys);
|
---|
3254 | AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrDst), rc);
|
---|
3255 | GCPhys |= (RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK;
|
---|
3256 |
|
---|
3257 | /* Mention when we ignore X86_PTE_RW... */
|
---|
3258 | if (!(fFlags & X86_PTE_RW))
|
---|
3259 | Log(("PGMPhysGCPtr2GCPhys: Writing to RO page %RGv %#x\n", GCPtrDst, cb));
|
---|
3260 |
|
---|
3261 | /* Mark the guest page as accessed and dirty if necessary. */
|
---|
3262 | if ((fFlags & (X86_PTE_A | X86_PTE_D)) != (X86_PTE_A | X86_PTE_D))
|
---|
3263 | {
|
---|
3264 | rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
|
---|
3265 | AssertRC(rc);
|
---|
3266 | }
|
---|
3267 |
|
---|
3268 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
3269 | return PGMPhysWrite(pVM, GCPhys, pvSrc, cb);
|
---|
3270 | #else
|
---|
3271 | PGMPhysWrite(pVM, GCPhys, pvSrc, cb);
|
---|
3272 | return VINF_SUCCESS;
|
---|
3273 | #endif
|
---|
3274 | }
|
---|
3275 |
|
---|
3276 | /*
|
---|
3277 | * Page by page.
|
---|
3278 | */
|
---|
3279 | for (;;)
|
---|
3280 | {
|
---|
3281 | /* Convert virtual to physical address + flags */
|
---|
3282 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtrDst, &fFlags, &GCPhys);
|
---|
3283 | AssertMsgRCReturn(rc, ("GetPage failed with %Rrc for %RGv\n", rc, GCPtrDst), rc);
|
---|
3284 | GCPhys |= (RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK;
|
---|
3285 |
|
---|
3286 | /* Mention when we ignore X86_PTE_RW... */
|
---|
3287 | if (!(fFlags & X86_PTE_RW))
|
---|
3288 | Log(("PGMPhysGCPtr2GCPhys: Writing to RO page %RGv %#x\n", GCPtrDst, cb));
|
---|
3289 |
|
---|
3290 | /* Mark the guest page as accessed and dirty if necessary. */
|
---|
3291 | if ((fFlags & (X86_PTE_A | X86_PTE_D)) != (X86_PTE_A | X86_PTE_D))
|
---|
3292 | {
|
---|
3293 | rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
|
---|
3294 | AssertRC(rc);
|
---|
3295 | }
|
---|
3296 |
|
---|
3297 | /* copy */
|
---|
3298 | size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
|
---|
3299 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
3300 | int rc = PGMPhysWrite(pVM, GCPhys, pvSrc, cbWrite);
|
---|
3301 | if (cbWrite >= cb || RT_FAILURE(rc))
|
---|
3302 | return rc;
|
---|
3303 | #else
|
---|
3304 | if (cbWrite >= cb)
|
---|
3305 | {
|
---|
3306 | PGMPhysWrite(pVM, GCPhys, pvSrc, cb);
|
---|
3307 | return VINF_SUCCESS;
|
---|
3308 | }
|
---|
3309 | PGMPhysWrite(pVM, GCPhys, pvSrc, cbWrite);
|
---|
3310 | #endif
|
---|
3311 |
|
---|
3312 | /* next */
|
---|
3313 | cb -= cbWrite;
|
---|
3314 | pvSrc = (uint8_t *)pvSrc + cbWrite;
|
---|
3315 | GCPtrDst += cbWrite;
|
---|
3316 | }
|
---|
3317 | }
|
---|
3318 |
|
---|
3319 | #endif /* !IN_RC */
|
---|
3320 |
|
---|
3321 | /**
|
---|
3322 | * Performs a read of guest virtual memory for instruction emulation.
|
---|
3323 | *
|
---|
3324 | * This will check permissions, raise exceptions and update the access bits.
|
---|
3325 | *
|
---|
3326 | * The current implementation will bypass all access handlers. It may later be
|
---|
3327 | * changed to at least respect MMIO.
|
---|
3328 | *
|
---|
3329 | *
|
---|
3330 | * @returns VBox status code suitable to scheduling.
|
---|
3331 | * @retval VINF_SUCCESS if the read was performed successfully.
|
---|
3332 | * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
|
---|
3333 | * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
|
---|
3334 | *
|
---|
3335 | * @param pVM The VM handle.
|
---|
3336 | * @param pCtxCore The context core.
|
---|
3337 | * @param pvDst Where to put the bytes we've read.
|
---|
3338 | * @param GCPtrSrc The source address.
|
---|
3339 | * @param cb The number of bytes to read. Not more than a page.
|
---|
3340 | *
|
---|
3341 | * @remark This function will dynamically map physical pages in GC. This may unmap
|
---|
3342 | * mappings done by the caller. Be careful!
|
---|
3343 | */
|
---|
3344 | VMMDECL(int) PGMPhysInterpretedRead(PVM pVM, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb)
|
---|
3345 | {
|
---|
3346 | Assert(cb <= PAGE_SIZE);
|
---|
3347 |
|
---|
3348 | /** @todo r=bird: This isn't perfect!
|
---|
3349 | * -# It's not checking for reserved bits being 1.
|
---|
3350 | * -# It's not correctly dealing with the access bit.
|
---|
3351 | * -# It's not respecting MMIO memory or any other access handlers.
|
---|
3352 | */
|
---|
3353 | /*
|
---|
3354 | * 1. Translate virtual to physical. This may fault.
|
---|
3355 | * 2. Map the physical address.
|
---|
3356 | * 3. Do the read operation.
|
---|
3357 | * 4. Set access bits if required.
|
---|
3358 | */
|
---|
3359 | int rc;
|
---|
3360 | unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
|
---|
3361 | if (cb <= cb1)
|
---|
3362 | {
|
---|
3363 | /*
|
---|
3364 | * Not crossing pages.
|
---|
3365 | */
|
---|
3366 | RTGCPHYS GCPhys;
|
---|
3367 | uint64_t fFlags;
|
---|
3368 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc, &fFlags, &GCPhys);
|
---|
3369 | if (RT_SUCCESS(rc))
|
---|
3370 | {
|
---|
3371 | /** @todo we should check reserved bits ... */
|
---|
3372 | void *pvSrc;
|
---|
3373 | rc = PGM_GCPHYS_2_PTR(pVM, GCPhys, &pvSrc);
|
---|
3374 | switch (rc)
|
---|
3375 | {
|
---|
3376 | case VINF_SUCCESS:
|
---|
3377 | Log(("PGMPhysInterpretedRead: pvDst=%p pvSrc=%p cb=%d\n", pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb));
|
---|
3378 | memcpy(pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
|
---|
3379 | break;
|
---|
3380 | case VERR_PGM_PHYS_PAGE_RESERVED:
|
---|
3381 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3382 | memset(pvDst, 0, cb); /** @todo this is wrong, it should be 0xff */
|
---|
3383 | break;
|
---|
3384 | default:
|
---|
3385 | return rc;
|
---|
3386 | }
|
---|
3387 |
|
---|
3388 | /** @todo access bit emulation isn't 100% correct. */
|
---|
3389 | if (!(fFlags & X86_PTE_A))
|
---|
3390 | {
|
---|
3391 | rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
|
---|
3392 | AssertRC(rc);
|
---|
3393 | }
|
---|
3394 | return VINF_SUCCESS;
|
---|
3395 | }
|
---|
3396 | }
|
---|
3397 | else
|
---|
3398 | {
|
---|
3399 | /*
|
---|
3400 | * Crosses pages.
|
---|
3401 | */
|
---|
3402 | size_t cb2 = cb - cb1;
|
---|
3403 | uint64_t fFlags1;
|
---|
3404 | RTGCPHYS GCPhys1;
|
---|
3405 | uint64_t fFlags2;
|
---|
3406 | RTGCPHYS GCPhys2;
|
---|
3407 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc, &fFlags1, &GCPhys1);
|
---|
3408 | if (RT_SUCCESS(rc))
|
---|
3409 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
|
---|
3410 | if (RT_SUCCESS(rc))
|
---|
3411 | {
|
---|
3412 | /** @todo we should check reserved bits ... */
|
---|
3413 | AssertMsgFailed(("cb=%d cb1=%d cb2=%d GCPtrSrc=%RGv\n", cb, cb1, cb2, GCPtrSrc));
|
---|
3414 | void *pvSrc1;
|
---|
3415 | rc = PGM_GCPHYS_2_PTR(pVM, GCPhys1, &pvSrc1);
|
---|
3416 | switch (rc)
|
---|
3417 | {
|
---|
3418 | case VINF_SUCCESS:
|
---|
3419 | memcpy(pvDst, (uint8_t *)pvSrc1 + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
|
---|
3420 | break;
|
---|
3421 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3422 | memset(pvDst, 0, cb1); /** @todo this is wrong, it should be 0xff */
|
---|
3423 | break;
|
---|
3424 | default:
|
---|
3425 | return rc;
|
---|
3426 | }
|
---|
3427 |
|
---|
3428 | void *pvSrc2;
|
---|
3429 | rc = PGM_GCPHYS_2_PTR(pVM, GCPhys2, &pvSrc2);
|
---|
3430 | switch (rc)
|
---|
3431 | {
|
---|
3432 | case VINF_SUCCESS:
|
---|
3433 | memcpy((uint8_t *)pvDst + cb1, pvSrc2, cb2);
|
---|
3434 | break;
|
---|
3435 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3436 | memset((uint8_t *)pvDst + cb1, 0, cb2); /** @todo this is wrong, it should be 0xff */
|
---|
3437 | break;
|
---|
3438 | default:
|
---|
3439 | return rc;
|
---|
3440 | }
|
---|
3441 |
|
---|
3442 | if (!(fFlags1 & X86_PTE_A))
|
---|
3443 | {
|
---|
3444 | rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
|
---|
3445 | AssertRC(rc);
|
---|
3446 | }
|
---|
3447 | if (!(fFlags2 & X86_PTE_A))
|
---|
3448 | {
|
---|
3449 | rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
|
---|
3450 | AssertRC(rc);
|
---|
3451 | }
|
---|
3452 | return VINF_SUCCESS;
|
---|
3453 | }
|
---|
3454 | }
|
---|
3455 |
|
---|
3456 | /*
|
---|
3457 | * Raise a #PF.
|
---|
3458 | */
|
---|
3459 | uint32_t uErr;
|
---|
3460 |
|
---|
3461 | /* Get the current privilege level. */
|
---|
3462 | uint32_t cpl = CPUMGetGuestCPL(pVM, pCtxCore);
|
---|
3463 | switch (rc)
|
---|
3464 | {
|
---|
3465 | case VINF_SUCCESS:
|
---|
3466 | uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
|
---|
3467 | break;
|
---|
3468 |
|
---|
3469 | case VERR_PAGE_NOT_PRESENT:
|
---|
3470 | case VERR_PAGE_TABLE_NOT_PRESENT:
|
---|
3471 | uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
|
---|
3472 | break;
|
---|
3473 |
|
---|
3474 | default:
|
---|
3475 | AssertMsgFailed(("rc=%Rrc GCPtrSrc=%RGv cb=%#x\n", rc, GCPtrSrc, cb));
|
---|
3476 | return rc;
|
---|
3477 | }
|
---|
3478 | Log(("PGMPhysInterpretedRead: GCPtrSrc=%RGv cb=%#x -> #PF(%#x)\n", GCPtrSrc, cb, uErr));
|
---|
3479 | return TRPMRaiseXcptErrCR2(pVM, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
|
---|
3480 | }
|
---|
3481 |
|
---|
3482 |
|
---|
3483 | /**
|
---|
3484 | * Performs a read of guest virtual memory for instruction emulation.
|
---|
3485 | *
|
---|
3486 | * This will check permissions, raise exceptions and update the access bits.
|
---|
3487 | *
|
---|
3488 | * The current implementation will bypass all access handlers. It may later be
|
---|
3489 | * changed to at least respect MMIO.
|
---|
3490 | *
|
---|
3491 | *
|
---|
3492 | * @returns VBox status code suitable to scheduling.
|
---|
3493 | * @retval VINF_SUCCESS if the read was performed successfully.
|
---|
3494 | * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
|
---|
3495 | * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
|
---|
3496 | *
|
---|
3497 | * @param pVM The VM handle.
|
---|
3498 | * @param pCtxCore The context core.
|
---|
3499 | * @param pvDst Where to put the bytes we've read.
|
---|
3500 | * @param GCPtrSrc The source address.
|
---|
3501 | * @param cb The number of bytes to read. Not more than a page.
|
---|
3502 | * @param fRaiseTrap If set the trap will be raised on as per spec, if clear
|
---|
3503 | * an appropriate error status will be returned (no
|
---|
3504 | * informational at all).
|
---|
3505 | *
|
---|
3506 | *
|
---|
3507 | * @remarks Takes the PGM lock.
|
---|
3508 | * @remarks A page fault on the 2nd page of the access will be raised without
|
---|
3509 | * writing the bits on the first page since we're ASSUMING that the
|
---|
3510 | * caller is emulating an instruction access.
|
---|
3511 | * @remarks This function will dynamically map physical pages in GC. This may
|
---|
3512 | * unmap mappings done by the caller. Be careful!
|
---|
3513 | */
|
---|
3514 | VMMDECL(int) PGMPhysInterpretedReadNoHandlers(PVM pVM, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb, bool fRaiseTrap)
|
---|
3515 | {
|
---|
3516 | Assert(cb <= PAGE_SIZE);
|
---|
3517 |
|
---|
3518 | /*
|
---|
3519 | * 1. Translate virtual to physical. This may fault.
|
---|
3520 | * 2. Map the physical address.
|
---|
3521 | * 3. Do the read operation.
|
---|
3522 | * 4. Set access bits if required.
|
---|
3523 | */
|
---|
3524 | int rc;
|
---|
3525 | unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
|
---|
3526 | if (cb <= cb1)
|
---|
3527 | {
|
---|
3528 | /*
|
---|
3529 | * Not crossing pages.
|
---|
3530 | */
|
---|
3531 | RTGCPHYS GCPhys;
|
---|
3532 | uint64_t fFlags;
|
---|
3533 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc, &fFlags, &GCPhys);
|
---|
3534 | if (RT_SUCCESS(rc))
|
---|
3535 | {
|
---|
3536 | if (1) /** @todo we should check reserved bits ... */
|
---|
3537 | {
|
---|
3538 | const void *pvSrc;
|
---|
3539 | PGMPAGEMAPLOCK Lock;
|
---|
3540 | rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys, &pvSrc, &Lock);
|
---|
3541 | switch (rc)
|
---|
3542 | {
|
---|
3543 | case VINF_SUCCESS:
|
---|
3544 | Log(("PGMPhysInterpretedReadNoHandlers: pvDst=%p pvSrc=%p (%RGv) cb=%d\n",
|
---|
3545 | pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), GCPtrSrc, cb));
|
---|
3546 | memcpy(pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
|
---|
3547 | break;
|
---|
3548 | case VERR_PGM_PHYS_PAGE_RESERVED:
|
---|
3549 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3550 | memset(pvDst, 0xff, cb);
|
---|
3551 | break;
|
---|
3552 | default:
|
---|
3553 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
3554 | AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
|
---|
3555 | return rc;
|
---|
3556 | }
|
---|
3557 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3558 |
|
---|
3559 | if (!(fFlags & X86_PTE_A))
|
---|
3560 | {
|
---|
3561 | /** @todo access bit emulation isn't 100% correct. */
|
---|
3562 | rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
|
---|
3563 | AssertRC(rc);
|
---|
3564 | }
|
---|
3565 | return VINF_SUCCESS;
|
---|
3566 | }
|
---|
3567 | }
|
---|
3568 | }
|
---|
3569 | else
|
---|
3570 | {
|
---|
3571 | /*
|
---|
3572 | * Crosses pages.
|
---|
3573 | */
|
---|
3574 | size_t cb2 = cb - cb1;
|
---|
3575 | uint64_t fFlags1;
|
---|
3576 | RTGCPHYS GCPhys1;
|
---|
3577 | uint64_t fFlags2;
|
---|
3578 | RTGCPHYS GCPhys2;
|
---|
3579 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc, &fFlags1, &GCPhys1);
|
---|
3580 | if (RT_SUCCESS(rc))
|
---|
3581 | {
|
---|
3582 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
|
---|
3583 | if (RT_SUCCESS(rc))
|
---|
3584 | {
|
---|
3585 | if (1) /** @todo we should check reserved bits ... */
|
---|
3586 | {
|
---|
3587 | const void *pvSrc;
|
---|
3588 | PGMPAGEMAPLOCK Lock;
|
---|
3589 | rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys1, &pvSrc, &Lock);
|
---|
3590 | switch (rc)
|
---|
3591 | {
|
---|
3592 | case VINF_SUCCESS:
|
---|
3593 | Log(("PGMPhysInterpretedReadNoHandlers: pvDst=%p pvSrc=%p (%RGv) cb=%d [2]\n",
|
---|
3594 | pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), GCPtrSrc, cb1));
|
---|
3595 | memcpy(pvDst, (const uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
|
---|
3596 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3597 | break;
|
---|
3598 | case VERR_PGM_PHYS_PAGE_RESERVED:
|
---|
3599 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3600 | memset(pvDst, 0xff, cb1);
|
---|
3601 | break;
|
---|
3602 | default:
|
---|
3603 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
3604 | AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
|
---|
3605 | return rc;
|
---|
3606 | }
|
---|
3607 |
|
---|
3608 | rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys2, &pvSrc, &Lock);
|
---|
3609 | switch (rc)
|
---|
3610 | {
|
---|
3611 | case VINF_SUCCESS:
|
---|
3612 | memcpy((uint8_t *)pvDst + cb1, pvSrc, cb2);
|
---|
3613 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3614 | break;
|
---|
3615 | case VERR_PGM_PHYS_PAGE_RESERVED:
|
---|
3616 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3617 | memset((uint8_t *)pvDst + cb1, 0xff, cb2);
|
---|
3618 | break;
|
---|
3619 | default:
|
---|
3620 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
3621 | AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
|
---|
3622 | return rc;
|
---|
3623 | }
|
---|
3624 |
|
---|
3625 | if (!(fFlags1 & X86_PTE_A))
|
---|
3626 | {
|
---|
3627 | rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
|
---|
3628 | AssertRC(rc);
|
---|
3629 | }
|
---|
3630 | if (!(fFlags2 & X86_PTE_A))
|
---|
3631 | {
|
---|
3632 | rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
|
---|
3633 | AssertRC(rc);
|
---|
3634 | }
|
---|
3635 | return VINF_SUCCESS;
|
---|
3636 | }
|
---|
3637 | /* sort out which page */
|
---|
3638 | }
|
---|
3639 | else
|
---|
3640 | GCPtrSrc += cb1; /* fault on 2nd page */
|
---|
3641 | }
|
---|
3642 | }
|
---|
3643 |
|
---|
3644 | /*
|
---|
3645 | * Raise a #PF if we're allowed to do that.
|
---|
3646 | */
|
---|
3647 | /* Calc the error bits. */
|
---|
3648 | uint32_t cpl = CPUMGetGuestCPL(pVM, pCtxCore);
|
---|
3649 | uint32_t uErr;
|
---|
3650 | switch (rc)
|
---|
3651 | {
|
---|
3652 | case VINF_SUCCESS:
|
---|
3653 | uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
|
---|
3654 | rc = VERR_ACCESS_DENIED;
|
---|
3655 | break;
|
---|
3656 |
|
---|
3657 | case VERR_PAGE_NOT_PRESENT:
|
---|
3658 | case VERR_PAGE_TABLE_NOT_PRESENT:
|
---|
3659 | uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
|
---|
3660 | break;
|
---|
3661 |
|
---|
3662 | default:
|
---|
3663 | AssertMsgFailed(("rc=%Rrc GCPtrSrc=%RGv cb=%#x\n", rc, GCPtrSrc, cb));
|
---|
3664 | AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
|
---|
3665 | return rc;
|
---|
3666 | }
|
---|
3667 | if (fRaiseTrap)
|
---|
3668 | {
|
---|
3669 | Log(("PGMPhysInterpretedReadNoHandlers: GCPtrSrc=%RGv cb=%#x -> Raised #PF(%#x)\n", GCPtrSrc, cb, uErr));
|
---|
3670 | return TRPMRaiseXcptErrCR2(pVM, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
|
---|
3671 | }
|
---|
3672 | Log(("PGMPhysInterpretedReadNoHandlers: GCPtrSrc=%RGv cb=%#x -> #PF(%#x) [!raised]\n", GCPtrSrc, cb, uErr));
|
---|
3673 | return rc;
|
---|
3674 | }
|
---|
3675 |
|
---|
3676 |
|
---|
3677 | /**
|
---|
3678 | * Performs a write to guest virtual memory for instruction emulation.
|
---|
3679 | *
|
---|
3680 | * This will check permissions, raise exceptions and update the dirty and access
|
---|
3681 | * bits.
|
---|
3682 | *
|
---|
3683 | * @returns VBox status code suitable to scheduling.
|
---|
3684 | * @retval VINF_SUCCESS if the read was performed successfully.
|
---|
3685 | * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
|
---|
3686 | * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
|
---|
3687 | *
|
---|
3688 | * @param pVM The VM handle.
|
---|
3689 | * @param pCtxCore The context core.
|
---|
3690 | * @param GCPtrDst The destination address.
|
---|
3691 | * @param pvSrc What to write.
|
---|
3692 | * @param cb The number of bytes to write. Not more than a page.
|
---|
3693 | * @param fRaiseTrap If set the trap will be raised on as per spec, if clear
|
---|
3694 | * an appropriate error status will be returned (no
|
---|
3695 | * informational at all).
|
---|
3696 | *
|
---|
3697 | * @remarks Takes the PGM lock.
|
---|
3698 | * @remarks A page fault on the 2nd page of the access will be raised without
|
---|
3699 | * writing the bits on the first page since we're ASSUMING that the
|
---|
3700 | * caller is emulating an instruction access.
|
---|
3701 | * @remarks This function will dynamically map physical pages in GC. This may
|
---|
3702 | * unmap mappings done by the caller. Be careful!
|
---|
3703 | */
|
---|
3704 | VMMDECL(int) PGMPhysInterpretedWriteNoHandlers(PVM pVM, PCPUMCTXCORE pCtxCore, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb, bool fRaiseTrap)
|
---|
3705 | {
|
---|
3706 | Assert(cb <= PAGE_SIZE);
|
---|
3707 |
|
---|
3708 | /*
|
---|
3709 | * 1. Translate virtual to physical. This may fault.
|
---|
3710 | * 2. Map the physical address.
|
---|
3711 | * 3. Do the write operation.
|
---|
3712 | * 4. Set access bits if required.
|
---|
3713 | */
|
---|
3714 | int rc;
|
---|
3715 | unsigned cb1 = PAGE_SIZE - (GCPtrDst & PAGE_OFFSET_MASK);
|
---|
3716 | if (cb <= cb1)
|
---|
3717 | {
|
---|
3718 | /*
|
---|
3719 | * Not crossing pages.
|
---|
3720 | */
|
---|
3721 | RTGCPHYS GCPhys;
|
---|
3722 | uint64_t fFlags;
|
---|
3723 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrDst, &fFlags, &GCPhys);
|
---|
3724 | if (RT_SUCCESS(rc))
|
---|
3725 | {
|
---|
3726 | if ( (fFlags & X86_PTE_RW) /** @todo Also check reserved bits. */
|
---|
3727 | || ( !(CPUMGetGuestCR0(pVM) & X86_CR0_WP)
|
---|
3728 | && CPUMGetGuestCPL(pVM, pCtxCore) <= 2) ) /** @todo it's 2, right? Check cpl check below as well. */
|
---|
3729 | {
|
---|
3730 | void *pvDst;
|
---|
3731 | PGMPAGEMAPLOCK Lock;
|
---|
3732 | rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys, &pvDst, &Lock);
|
---|
3733 | switch (rc)
|
---|
3734 | {
|
---|
3735 | case VINF_SUCCESS:
|
---|
3736 | Log(("PGMPhysInterpretedWriteNoHandlers: pvDst=%p (%RGv) pvSrc=%p cb=%d\n",
|
---|
3737 | (uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), GCPtrDst, pvSrc, cb));
|
---|
3738 | memcpy((uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), pvSrc, cb);
|
---|
3739 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3740 | break;
|
---|
3741 | case VERR_PGM_PHYS_PAGE_RESERVED:
|
---|
3742 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3743 | /* bit bucket */
|
---|
3744 | break;
|
---|
3745 | default:
|
---|
3746 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
3747 | AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
|
---|
3748 | return rc;
|
---|
3749 | }
|
---|
3750 |
|
---|
3751 | if (!(fFlags & (X86_PTE_A | X86_PTE_D)))
|
---|
3752 | {
|
---|
3753 | /** @todo dirty & access bit emulation isn't 100% correct. */
|
---|
3754 | rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
|
---|
3755 | AssertRC(rc);
|
---|
3756 | }
|
---|
3757 | return VINF_SUCCESS;
|
---|
3758 | }
|
---|
3759 | rc = VERR_ACCESS_DENIED;
|
---|
3760 | }
|
---|
3761 | }
|
---|
3762 | else
|
---|
3763 | {
|
---|
3764 | /*
|
---|
3765 | * Crosses pages.
|
---|
3766 | */
|
---|
3767 | size_t cb2 = cb - cb1;
|
---|
3768 | uint64_t fFlags1;
|
---|
3769 | RTGCPHYS GCPhys1;
|
---|
3770 | uint64_t fFlags2;
|
---|
3771 | RTGCPHYS GCPhys2;
|
---|
3772 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrDst, &fFlags1, &GCPhys1);
|
---|
3773 | if (RT_SUCCESS(rc))
|
---|
3774 | {
|
---|
3775 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrDst + cb1, &fFlags2, &GCPhys2);
|
---|
3776 | if (RT_SUCCESS(rc))
|
---|
3777 | {
|
---|
3778 | if ( ( (fFlags1 & X86_PTE_RW) /** @todo Also check reserved bits. */
|
---|
3779 | && (fFlags2 & X86_PTE_RW))
|
---|
3780 | || ( !(CPUMGetGuestCR0(pVM) & X86_CR0_WP)
|
---|
3781 | && CPUMGetGuestCPL(pVM, pCtxCore) <= 2) )
|
---|
3782 | {
|
---|
3783 | void *pvDst;
|
---|
3784 | PGMPAGEMAPLOCK Lock;
|
---|
3785 | rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys1, &pvDst, &Lock);
|
---|
3786 | switch (rc)
|
---|
3787 | {
|
---|
3788 | case VINF_SUCCESS:
|
---|
3789 | Log(("PGMPhysInterpretedWriteNoHandlers: pvDst=%p (%RGv) pvSrc=%p cb=%d\n",
|
---|
3790 | (uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), GCPtrDst, pvSrc, cb1));
|
---|
3791 | memcpy((uint8_t *)pvDst + (GCPtrDst & PAGE_OFFSET_MASK), pvSrc, cb1);
|
---|
3792 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3793 | break;
|
---|
3794 | case VERR_PGM_PHYS_PAGE_RESERVED:
|
---|
3795 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3796 | /* bit bucket */
|
---|
3797 | break;
|
---|
3798 | default:
|
---|
3799 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
3800 | AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
|
---|
3801 | return rc;
|
---|
3802 | }
|
---|
3803 |
|
---|
3804 | rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys2, &pvDst, &Lock);
|
---|
3805 | switch (rc)
|
---|
3806 | {
|
---|
3807 | case VINF_SUCCESS:
|
---|
3808 | memcpy(pvDst, (const uint8_t *)pvSrc + cb1, cb2);
|
---|
3809 | PGMPhysReleasePageMappingLock(pVM, &Lock);
|
---|
3810 | break;
|
---|
3811 | case VERR_PGM_PHYS_PAGE_RESERVED:
|
---|
3812 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
3813 | /* bit bucket */
|
---|
3814 | break;
|
---|
3815 | default:
|
---|
3816 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
3817 | AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
|
---|
3818 | return rc;
|
---|
3819 | }
|
---|
3820 |
|
---|
3821 | if (!(fFlags1 & (X86_PTE_A | X86_PTE_RW)))
|
---|
3822 | {
|
---|
3823 | rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrDst, 1, (X86_PTE_A | X86_PTE_RW), ~(uint64_t)(X86_PTE_A | X86_PTE_RW));
|
---|
3824 | AssertRC(rc);
|
---|
3825 | }
|
---|
3826 | if (!(fFlags2 & (X86_PTE_A | X86_PTE_RW)))
|
---|
3827 | {
|
---|
3828 | rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrDst + cb1, 1, (X86_PTE_A | X86_PTE_RW), ~(uint64_t)(X86_PTE_A | X86_PTE_RW));
|
---|
3829 | AssertRC(rc);
|
---|
3830 | }
|
---|
3831 | return VINF_SUCCESS;
|
---|
3832 | }
|
---|
3833 | if ((fFlags1 & (X86_PTE_RW)) == X86_PTE_RW)
|
---|
3834 | GCPtrDst += cb1; /* fault on the 2nd page. */
|
---|
3835 | rc = VERR_ACCESS_DENIED;
|
---|
3836 | }
|
---|
3837 | else
|
---|
3838 | GCPtrDst += cb1; /* fault on the 2nd page. */
|
---|
3839 | }
|
---|
3840 | }
|
---|
3841 |
|
---|
3842 | /*
|
---|
3843 | * Raise a #PF if we're allowed to do that.
|
---|
3844 | */
|
---|
3845 | /* Calc the error bits. */
|
---|
3846 | uint32_t uErr;
|
---|
3847 | uint32_t cpl = CPUMGetGuestCPL(pVM, pCtxCore);
|
---|
3848 | switch (rc)
|
---|
3849 | {
|
---|
3850 | case VINF_SUCCESS:
|
---|
3851 | uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
|
---|
3852 | rc = VERR_ACCESS_DENIED;
|
---|
3853 | break;
|
---|
3854 |
|
---|
3855 | case VERR_ACCESS_DENIED:
|
---|
3856 | uErr = (cpl >= 2) ? X86_TRAP_PF_RW | X86_TRAP_PF_US : X86_TRAP_PF_RW;
|
---|
3857 | break;
|
---|
3858 |
|
---|
3859 | case VERR_PAGE_NOT_PRESENT:
|
---|
3860 | case VERR_PAGE_TABLE_NOT_PRESENT:
|
---|
3861 | uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
|
---|
3862 | break;
|
---|
3863 |
|
---|
3864 | default:
|
---|
3865 | AssertMsgFailed(("rc=%Rrc GCPtrDst=%RGv cb=%#x\n", rc, GCPtrDst, cb));
|
---|
3866 | AssertReturn(RT_FAILURE(rc), VERR_INTERNAL_ERROR);
|
---|
3867 | return rc;
|
---|
3868 | }
|
---|
3869 | if (fRaiseTrap)
|
---|
3870 | {
|
---|
3871 | Log(("PGMPhysInterpretedWriteNoHandlers: GCPtrDst=%RGv cb=%#x -> Raised #PF(%#x)\n", GCPtrDst, cb, uErr));
|
---|
3872 | return TRPMRaiseXcptErrCR2(pVM, pCtxCore, X86_XCPT_PF, uErr, GCPtrDst);
|
---|
3873 | }
|
---|
3874 | Log(("PGMPhysInterpretedWriteNoHandlers: GCPtrDst=%RGv cb=%#x -> #PF(%#x) [!raised]\n", GCPtrDst, cb, uErr));
|
---|
3875 | return rc;
|
---|
3876 | }
|
---|
3877 |
|
---|
3878 |
|
---|