1 | /* $Id: PGMAllPhys.cpp 13098 2008-10-08 17:10:32Z 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 | /** @def PGM_IGNORE_RAM_FLAGS_RESERVED
|
---|
23 | * Don't respect the MM_RAM_FLAGS_RESERVED flag when converting to HC addresses.
|
---|
24 | *
|
---|
25 | * Since this flag is currently incorrectly kept set for ROM regions we will
|
---|
26 | * have to ignore it for now so we don't break stuff.
|
---|
27 | *
|
---|
28 | * @todo this has been fixed now I believe, remove this hack.
|
---|
29 | */
|
---|
30 | #define PGM_IGNORE_RAM_FLAGS_RESERVED
|
---|
31 |
|
---|
32 |
|
---|
33 | /*******************************************************************************
|
---|
34 | * Header Files *
|
---|
35 | *******************************************************************************/
|
---|
36 | #define LOG_GROUP LOG_GROUP_PGM_PHYS
|
---|
37 | #include <VBox/pgm.h>
|
---|
38 | #include <VBox/trpm.h>
|
---|
39 | #include <VBox/vmm.h>
|
---|
40 | #include <VBox/iom.h>
|
---|
41 | #include <VBox/em.h>
|
---|
42 | #include <VBox/rem.h>
|
---|
43 | #include "PGMInternal.h"
|
---|
44 | #include <VBox/vm.h>
|
---|
45 | #include <VBox/param.h>
|
---|
46 | #include <VBox/err.h>
|
---|
47 | #include <iprt/assert.h>
|
---|
48 | #include <iprt/string.h>
|
---|
49 | #include <iprt/asm.h>
|
---|
50 | #include <VBox/log.h>
|
---|
51 | #ifdef IN_RING3
|
---|
52 | # include <iprt/thread.h>
|
---|
53 | #endif
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | #ifndef IN_RING3
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * \#PF Handler callback for Guest ROM range write access.
|
---|
61 | * We simply ignore the writes or fall back to the recompiler if we don't support the instruction.
|
---|
62 | *
|
---|
63 | * @returns VBox status code (appropritate for trap handling and GC return).
|
---|
64 | * @param pVM VM Handle.
|
---|
65 | * @param uErrorCode CPU Error code.
|
---|
66 | * @param pRegFrame Trap register frame.
|
---|
67 | * @param pvFault The fault address (cr2).
|
---|
68 | * @param GCPhysFault The GC physical address corresponding to pvFault.
|
---|
69 | * @param pvUser User argument. Pointer to the ROM range structure.
|
---|
70 | */
|
---|
71 | VMMDECL(int) pgmPhysRomWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, void *pvFault, RTGCPHYS GCPhysFault, void *pvUser)
|
---|
72 | {
|
---|
73 | int rc;
|
---|
74 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
75 | PPGMROMRANGE pRom = (PPGMROMRANGE)pvUser;
|
---|
76 | uint32_t iPage = GCPhysFault - pRom->GCPhys;
|
---|
77 | Assert(iPage < (pRom->cb >> PAGE_SHIFT));
|
---|
78 | switch (pRom->aPages[iPage].enmProt)
|
---|
79 | {
|
---|
80 | case PGMROMPROT_READ_ROM_WRITE_IGNORE:
|
---|
81 | case PGMROMPROT_READ_RAM_WRITE_IGNORE:
|
---|
82 | {
|
---|
83 | #endif
|
---|
84 | /*
|
---|
85 | * If it's a simple instruction which doesn't change the cpu state
|
---|
86 | * we will simply skip it. Otherwise we'll have to defer it to REM.
|
---|
87 | */
|
---|
88 | uint32_t cbOp;
|
---|
89 | DISCPUSTATE Cpu;
|
---|
90 | rc = EMInterpretDisasOne(pVM, pRegFrame, &Cpu, &cbOp);
|
---|
91 | if ( RT_SUCCESS(rc)
|
---|
92 | && Cpu.mode == CPUMODE_32BIT /** @todo why does this matter? */
|
---|
93 | && !(Cpu.prefix & (PREFIX_REPNE | PREFIX_REP | PREFIX_SEG)))
|
---|
94 | {
|
---|
95 | switch (Cpu.opcode)
|
---|
96 | {
|
---|
97 | /** @todo Find other instructions we can safely skip, possibly
|
---|
98 | * adding this kind of detection to DIS or EM. */
|
---|
99 | case OP_MOV:
|
---|
100 | pRegFrame->rip += cbOp;
|
---|
101 | STAM_COUNTER_INC(&pVM->pgm.s.StatRZGuestROMWriteHandled);
|
---|
102 | return VINF_SUCCESS;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | else if (RT_UNLIKELY(rc == VERR_INTERNAL_ERROR))
|
---|
106 | return rc;
|
---|
107 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
108 | break;
|
---|
109 | }
|
---|
110 |
|
---|
111 | case PGMROMPROT_READ_RAM_WRITE_RAM:
|
---|
112 | rc = PGMHandlerPhysicalPageTempOff(pVM, pRom->GCPhys, GCPhysFault & X86_PTE_PG_MASK);
|
---|
113 | AssertRC(rc);
|
---|
114 | case PGMROMPROT_READ_ROM_WRITE_RAM:
|
---|
115 | /* Handle it in ring-3 because it's *way* easier there. */
|
---|
116 | break;
|
---|
117 |
|
---|
118 | default:
|
---|
119 | AssertMsgFailedReturn(("enmProt=%d iPage=%d GCPhysFault=%RGp\n",
|
---|
120 | pRom->aPages[iPage].enmProt, iPage, GCPhysFault),
|
---|
121 | VERR_INTERNAL_ERROR);
|
---|
122 | }
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | STAM_COUNTER_INC(&pVM->pgm.s.StatRZGuestROMWriteUnhandled);
|
---|
126 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
127 | }
|
---|
128 |
|
---|
129 | #endif /* IN_RING3 */
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Checks if Address Gate 20 is enabled or not.
|
---|
133 | *
|
---|
134 | * @returns true if enabled.
|
---|
135 | * @returns false if disabled.
|
---|
136 | * @param pVM VM handle.
|
---|
137 | */
|
---|
138 | VMMDECL(bool) PGMPhysIsA20Enabled(PVM pVM)
|
---|
139 | {
|
---|
140 | LogFlow(("PGMPhysIsA20Enabled %d\n", pVM->pgm.s.fA20Enabled));
|
---|
141 | return !!pVM->pgm.s.fA20Enabled ; /* stupid MS compiler doesn't trust me. */
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Validates a GC physical address.
|
---|
147 | *
|
---|
148 | * @returns true if valid.
|
---|
149 | * @returns false if invalid.
|
---|
150 | * @param pVM The VM handle.
|
---|
151 | * @param GCPhys The physical address to validate.
|
---|
152 | */
|
---|
153 | VMMDECL(bool) PGMPhysIsGCPhysValid(PVM pVM, RTGCPHYS GCPhys)
|
---|
154 | {
|
---|
155 | PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
|
---|
156 | return pPage != NULL;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Checks if a GC physical address is a normal page,
|
---|
162 | * i.e. not ROM, MMIO or reserved.
|
---|
163 | *
|
---|
164 | * @returns true if normal.
|
---|
165 | * @returns false if invalid, ROM, MMIO or reserved page.
|
---|
166 | * @param pVM The VM handle.
|
---|
167 | * @param GCPhys The physical address to check.
|
---|
168 | */
|
---|
169 | VMMDECL(bool) PGMPhysIsGCPhysNormal(PVM pVM, RTGCPHYS GCPhys)
|
---|
170 | {
|
---|
171 | PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
|
---|
172 | return pPage
|
---|
173 | && !(pPage->HCPhys & (MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO2));
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Converts a GC physical address to a HC physical address.
|
---|
179 | *
|
---|
180 | * @returns VINF_SUCCESS on success.
|
---|
181 | * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
|
---|
182 | * page but has no physical backing.
|
---|
183 | * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
|
---|
184 | * GC physical address.
|
---|
185 | *
|
---|
186 | * @param pVM The VM handle.
|
---|
187 | * @param GCPhys The GC physical address to convert.
|
---|
188 | * @param pHCPhys Where to store the HC physical address on success.
|
---|
189 | */
|
---|
190 | VMMDECL(int) PGMPhysGCPhys2HCPhys(PVM pVM, RTGCPHYS GCPhys, PRTHCPHYS pHCPhys)
|
---|
191 | {
|
---|
192 | PPGMPAGE pPage;
|
---|
193 | int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhys, &pPage);
|
---|
194 | if (VBOX_FAILURE(rc))
|
---|
195 | return rc;
|
---|
196 |
|
---|
197 | #ifndef PGM_IGNORE_RAM_FLAGS_RESERVED
|
---|
198 | if (RT_UNLIKELY(pPage->HCPhys & MM_RAM_FLAGS_RESERVED)) /** @todo PAGE FLAGS */
|
---|
199 | return VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
200 | #endif
|
---|
201 |
|
---|
202 | *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK);
|
---|
203 | return VINF_SUCCESS;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Invalidates the GC page mapping TLB.
|
---|
209 | *
|
---|
210 | * @param pVM The VM handle.
|
---|
211 | */
|
---|
212 | VMMDECL(void) PGMPhysInvalidatePageGCMapTLB(PVM pVM)
|
---|
213 | {
|
---|
214 | /* later */
|
---|
215 | NOREF(pVM);
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Invalidates the ring-0 page mapping TLB.
|
---|
221 | *
|
---|
222 | * @param pVM The VM handle.
|
---|
223 | */
|
---|
224 | VMMDECL(void) PGMPhysInvalidatePageR0MapTLB(PVM pVM)
|
---|
225 | {
|
---|
226 | PGMPhysInvalidatePageR3MapTLB(pVM);
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Invalidates the ring-3 page mapping TLB.
|
---|
232 | *
|
---|
233 | * @param pVM The VM handle.
|
---|
234 | */
|
---|
235 | VMMDECL(void) PGMPhysInvalidatePageR3MapTLB(PVM pVM)
|
---|
236 | {
|
---|
237 | pgmLock(pVM);
|
---|
238 | for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.PhysTlbHC.aEntries); i++)
|
---|
239 | {
|
---|
240 | pVM->pgm.s.PhysTlbHC.aEntries[i].GCPhys = NIL_RTGCPHYS;
|
---|
241 | pVM->pgm.s.PhysTlbHC.aEntries[i].pPage = 0;
|
---|
242 | pVM->pgm.s.PhysTlbHC.aEntries[i].pMap = 0;
|
---|
243 | pVM->pgm.s.PhysTlbHC.aEntries[i].pv = 0;
|
---|
244 | }
|
---|
245 | pgmUnlock(pVM);
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Frees the specified RAM page.
|
---|
251 | *
|
---|
252 | * This is used by ballooning and remapping MMIO2.
|
---|
253 | *
|
---|
254 | * @param pVM Pointer to the shared VM structure.
|
---|
255 | * @param pPage Pointer to the page structure.
|
---|
256 | * @param GCPhys The guest physical address of the page, if applicable.
|
---|
257 | */
|
---|
258 | void pgmPhysFreePage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
|
---|
259 | {
|
---|
260 | AssertFatal(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM);
|
---|
261 |
|
---|
262 | /** @todo implement this... */
|
---|
263 | AssertFatalFailed();
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Makes sure that there is at least one handy page ready for use.
|
---|
270 | *
|
---|
271 | * This will also take the appropriate actions when reaching water-marks.
|
---|
272 | *
|
---|
273 | * @returns The following VBox status codes.
|
---|
274 | * @retval VINF_SUCCESS on success.
|
---|
275 | * @retval VERR_EM_NO_MEMORY if we're really out of memory.
|
---|
276 | *
|
---|
277 | * @param pVM The VM handle.
|
---|
278 | *
|
---|
279 | * @remarks Must be called from within the PGM critical section. It may
|
---|
280 | * nip back to ring-3/0 in some cases.
|
---|
281 | */
|
---|
282 | static int pgmPhysEnsureHandyPage(PVM pVM)
|
---|
283 | {
|
---|
284 | /** @remarks
|
---|
285 | * low-water mark logic for R0 & GC:
|
---|
286 | * - 75%: Set FF.
|
---|
287 | * - 50%: Force return to ring-3 ASAP.
|
---|
288 | *
|
---|
289 | * For ring-3 there is a little problem wrt to the recompiler, so:
|
---|
290 | * - 75%: Set FF.
|
---|
291 | * - 50%: Try allocate pages; on failure we'll force REM to quite ASAP.
|
---|
292 | *
|
---|
293 | * The basic idea is that we should be able to get out of any situation with
|
---|
294 | * only 50% of handy pages remaining.
|
---|
295 | *
|
---|
296 | * At the moment we'll not adjust the number of handy pages relative to the
|
---|
297 | * actual VM RAM committment, that's too much work for now.
|
---|
298 | */
|
---|
299 | Assert(pVM->pgm.s.cHandyPages <= RT_ELEMENTS(pVM->pgm.s.aHandyPages));
|
---|
300 | if ( !pVM->pgm.s.cHandyPages
|
---|
301 | #ifdef IN_RING3
|
---|
302 | || pVM->pgm.s.cHandyPages - 1 <= RT_ELEMENTS(pVM->pgm.s.aHandyPages) / 2 /* 50% */
|
---|
303 | #endif
|
---|
304 | )
|
---|
305 | {
|
---|
306 | Log(("PGM: cHandyPages=%u out of %u -> allocate more\n", pVM->pgm.s.cHandyPages - 1 <= RT_ELEMENTS(pVM->pgm.s.aHandyPages)));
|
---|
307 | #ifdef IN_RING3
|
---|
308 | int rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES, 0, NULL);
|
---|
309 | #elif defined(IN_RING0)
|
---|
310 | /** @todo call PGMR0PhysAllocateHandyPages directly - need to make sure we can call kernel code first and deal with the seeding fallback. */
|
---|
311 | int rc = VMMR0CallHost(pVM, VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES, 0);
|
---|
312 | #else
|
---|
313 | int rc = VMMGCCallHost(pVM, VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES, 0);
|
---|
314 | #endif
|
---|
315 | if (RT_UNLIKELY(rc != VINF_SUCCESS))
|
---|
316 | {
|
---|
317 | Assert(rc == VINF_EM_NO_MEMORY);
|
---|
318 | if (!pVM->pgm.s.cHandyPages)
|
---|
319 | {
|
---|
320 | LogRel(("PGM: no more handy pages!\n"));
|
---|
321 | return VERR_EM_NO_MEMORY;
|
---|
322 | }
|
---|
323 | Assert(VM_FF_ISSET(pVM, VM_FF_PGM_NEED_HANDY_PAGES));
|
---|
324 | #ifdef IN_RING3
|
---|
325 | REMR3NotifyFF(pVM);
|
---|
326 | #else
|
---|
327 | VM_FF_SET(pVM, VM_FF_TO_R3);
|
---|
328 | #endif
|
---|
329 | }
|
---|
330 | Assert(pVM->pgm.s.cHandyPages <= RT_ELEMENTS(pVM->pgm.s.aHandyPages));
|
---|
331 | }
|
---|
332 | else if (pVM->pgm.s.cHandyPages - 1 <= (RT_ELEMENTS(pVM->pgm.s.aHandyPages) / 4) * 3) /* 75% */
|
---|
333 | {
|
---|
334 | VM_FF_SET(pVM, VM_FF_PGM_NEED_HANDY_PAGES);
|
---|
335 | #ifndef IN_RING3
|
---|
336 | if (pVM->pgm.s.cHandyPages - 1 <= RT_ELEMENTS(pVM->pgm.s.aHandyPages) / 2)
|
---|
337 | {
|
---|
338 | Log(("PGM: VM_FF_TO_R3 - cHandyPages=%u out of %u\n", pVM->pgm.s.cHandyPages - 1 <= RT_ELEMENTS(pVM->pgm.s.aHandyPages)));
|
---|
339 | VM_FF_SET(pVM, VM_FF_TO_R3);
|
---|
340 | }
|
---|
341 | #endif
|
---|
342 | }
|
---|
343 |
|
---|
344 | return VINF_SUCCESS;
|
---|
345 | }
|
---|
346 |
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * Replace a zero or shared page with new page that we can write to.
|
---|
350 | *
|
---|
351 | * @returns The following VBox status codes.
|
---|
352 | * @retval VINF_SUCCESS on success, pPage is modified.
|
---|
353 | * @retval VERR_EM_NO_MEMORY if we're totally out of memory.
|
---|
354 | *
|
---|
355 | * @todo Propagate VERR_EM_NO_MEMORY up the call tree.
|
---|
356 | *
|
---|
357 | * @param pVM The VM address.
|
---|
358 | * @param pPage The physical page tracking structure. This will
|
---|
359 | * be modified on success.
|
---|
360 | * @param GCPhys The address of the page.
|
---|
361 | *
|
---|
362 | * @remarks Must be called from within the PGM critical section. It may
|
---|
363 | * nip back to ring-3/0 in some cases.
|
---|
364 | *
|
---|
365 | * @remarks This function shouldn't really fail, however if it does
|
---|
366 | * it probably means we've screwed up the size of the amount
|
---|
367 | * and/or the low-water mark of handy pages. Or, that some
|
---|
368 | * device I/O is causing a lot of pages to be allocated while
|
---|
369 | * while the host is in a low-memory condition.
|
---|
370 | */
|
---|
371 | int pgmPhysAllocPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
|
---|
372 | {
|
---|
373 | /*
|
---|
374 | * Ensure that we've got a page handy, take it and use it.
|
---|
375 | */
|
---|
376 | int rc = pgmPhysEnsureHandyPage(pVM);
|
---|
377 | if (VBOX_FAILURE(rc))
|
---|
378 | {
|
---|
379 | Assert(rc == VERR_EM_NO_MEMORY);
|
---|
380 | return rc;
|
---|
381 | }
|
---|
382 | AssertMsg(PGM_PAGE_IS_ZERO(pPage) || PGM_PAGE_IS_SHARED(pPage), ("%d %RGp\n", PGM_PAGE_GET_STATE(pPage), GCPhys));
|
---|
383 | Assert(!PGM_PAGE_IS_RESERVED(pPage));
|
---|
384 | Assert(!PGM_PAGE_IS_MMIO(pPage));
|
---|
385 |
|
---|
386 | uint32_t iHandyPage = --pVM->pgm.s.cHandyPages;
|
---|
387 | Assert(iHandyPage < RT_ELEMENTS(pVM->pgm.s.aHandyPages));
|
---|
388 | Assert(pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys != NIL_RTHCPHYS);
|
---|
389 | Assert(!(pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
|
---|
390 | Assert(pVM->pgm.s.aHandyPages[iHandyPage].idPage != NIL_GMM_PAGEID);
|
---|
391 | Assert(pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage == NIL_GMM_PAGEID);
|
---|
392 |
|
---|
393 | /*
|
---|
394 | * There are one or two action to be taken the next time we allocate handy pages:
|
---|
395 | * - Tell the GMM (global memory manager) what the page is being used for.
|
---|
396 | * (Speeds up replacement operations - sharing and defragmenting.)
|
---|
397 | * - If the current backing is shared, it must be freed.
|
---|
398 | */
|
---|
399 | const RTHCPHYS HCPhys = pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys;
|
---|
400 | pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys = GCPhys;
|
---|
401 |
|
---|
402 | if (PGM_PAGE_IS_SHARED(pPage))
|
---|
403 | {
|
---|
404 | pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage = PGM_PAGE_GET_PAGEID(pPage);
|
---|
405 | Assert(PGM_PAGE_GET_PAGEID(pPage) != NIL_GMM_PAGEID);
|
---|
406 | VM_FF_SET(pVM, VM_FF_PGM_NEED_HANDY_PAGES);
|
---|
407 |
|
---|
408 | Log2(("PGM: Replaced shared page %#x at %RGp with %#x / %RHp\n", PGM_PAGE_GET_PAGEID(pPage),
|
---|
409 | GCPhys, pVM->pgm.s.aHandyPages[iHandyPage].idPage, HCPhys));
|
---|
410 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,PageReplaceShared));
|
---|
411 | pVM->pgm.s.cSharedPages--;
|
---|
412 | /** @todo err.. what about copying the page content? */
|
---|
413 | }
|
---|
414 | else
|
---|
415 | {
|
---|
416 | Log2(("PGM: Replaced zero page %RGp with %#x / %RHp\n", GCPhys, pVM->pgm.s.aHandyPages[iHandyPage].idPage, HCPhys));
|
---|
417 | STAM_COUNTER_INC(&pVM->pgm.s.StatRZPageReplaceZero);
|
---|
418 | pVM->pgm.s.cZeroPages--;
|
---|
419 | /** @todo verify that the handy page is zero! */
|
---|
420 | }
|
---|
421 |
|
---|
422 | /*
|
---|
423 | * Do the PGMPAGE modifications.
|
---|
424 | */
|
---|
425 | pVM->pgm.s.cPrivatePages++;
|
---|
426 | PGM_PAGE_SET_HCPHYS(pPage, HCPhys);
|
---|
427 | PGM_PAGE_SET_PAGEID(pPage, pVM->pgm.s.aHandyPages[iHandyPage].idPage);
|
---|
428 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
|
---|
429 |
|
---|
430 | return VINF_SUCCESS;
|
---|
431 | }
|
---|
432 |
|
---|
433 |
|
---|
434 | /**
|
---|
435 | * Deal with pages that are not writable, i.e. not in the ALLOCATED state.
|
---|
436 | *
|
---|
437 | * @returns VBox status code.
|
---|
438 | * @retval VINF_SUCCESS on success.
|
---|
439 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
440 | *
|
---|
441 | * @param pVM The VM address.
|
---|
442 | * @param pPage The physical page tracking structure.
|
---|
443 | * @param GCPhys The address of the page.
|
---|
444 | *
|
---|
445 | * @remarks Called from within the PGM critical section.
|
---|
446 | */
|
---|
447 | int pgmPhysPageMakeWritable(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
|
---|
448 | {
|
---|
449 | switch (PGM_PAGE_GET_STATE(pPage))
|
---|
450 | {
|
---|
451 | case PGM_PAGE_STATE_WRITE_MONITORED:
|
---|
452 | PGM_PAGE_SET_WRITTEN_TO(pPage);
|
---|
453 | PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
|
---|
454 | /* fall thru */
|
---|
455 | default: /* to shut up GCC */
|
---|
456 | case PGM_PAGE_STATE_ALLOCATED:
|
---|
457 | return VINF_SUCCESS;
|
---|
458 |
|
---|
459 | /*
|
---|
460 | * Zero pages can be dummy pages for MMIO or reserved memory,
|
---|
461 | * so we need to check the flags before joining cause with
|
---|
462 | * shared page replacement.
|
---|
463 | */
|
---|
464 | case PGM_PAGE_STATE_ZERO:
|
---|
465 | if ( PGM_PAGE_IS_MMIO(pPage)
|
---|
466 | || PGM_PAGE_IS_RESERVED(pPage))
|
---|
467 | return VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
468 | /* fall thru */
|
---|
469 | case PGM_PAGE_STATE_SHARED:
|
---|
470 | return pgmPhysAllocPage(pVM, pPage, GCPhys);
|
---|
471 | }
|
---|
472 | }
|
---|
473 |
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * Maps a page into the current virtual address space so it can be accessed.
|
---|
477 | *
|
---|
478 | * @returns VBox status code.
|
---|
479 | * @retval VINF_SUCCESS on success.
|
---|
480 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
481 | *
|
---|
482 | * @param pVM The VM address.
|
---|
483 | * @param pPage The physical page tracking structure.
|
---|
484 | * @param GCPhys The address of the page.
|
---|
485 | * @param ppMap Where to store the address of the mapping tracking structure.
|
---|
486 | * @param ppv Where to store the mapping address of the page. The page
|
---|
487 | * offset is masked off!
|
---|
488 | *
|
---|
489 | * @remarks Called from within the PGM critical section.
|
---|
490 | */
|
---|
491 | int pgmPhysPageMap(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, PPPGMPAGEMAP ppMap, void **ppv)
|
---|
492 | {
|
---|
493 | #if defined(IN_GC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
494 | /*
|
---|
495 | * Just some sketchy GC/R0-darwin code.
|
---|
496 | */
|
---|
497 | *ppMap = NULL;
|
---|
498 | RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
|
---|
499 | Assert(HCPhys != pVM->pgm.s.HCPhysZeroPg);
|
---|
500 | return PGMDynMapHCPage(pVM, HCPhys, ppv);
|
---|
501 |
|
---|
502 | #else /* IN_RING3 || IN_RING0 */
|
---|
503 |
|
---|
504 | /*
|
---|
505 | * Find/make Chunk TLB entry for the mapping chunk.
|
---|
506 | */
|
---|
507 | PPGMCHUNKR3MAP pMap;
|
---|
508 | const uint32_t idChunk = PGM_PAGE_GET_CHUNKID(pPage);
|
---|
509 | PPGMCHUNKR3MAPTLBE pTlbe = &pVM->pgm.s.ChunkR3Map.Tlb.aEntries[PGM_CHUNKR3MAPTLB_IDX(idChunk)];
|
---|
510 | if (pTlbe->idChunk == idChunk)
|
---|
511 | {
|
---|
512 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbHits));
|
---|
513 | pMap = pTlbe->pChunk;
|
---|
514 | }
|
---|
515 | else if (idChunk != NIL_GMM_CHUNKID)
|
---|
516 | {
|
---|
517 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbMisses));
|
---|
518 |
|
---|
519 | /*
|
---|
520 | * Find the chunk, map it if necessary.
|
---|
521 | */
|
---|
522 | pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
|
---|
523 | if (!pMap)
|
---|
524 | {
|
---|
525 | #ifdef IN_RING0
|
---|
526 | int rc = VMMR0CallHost(pVM, VMMCALLHOST_PGM_MAP_CHUNK, idChunk);
|
---|
527 | AssertRCReturn(rc, rc);
|
---|
528 | pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
|
---|
529 | Assert(pMap);
|
---|
530 | #else
|
---|
531 | int rc = pgmR3PhysChunkMap(pVM, idChunk, &pMap);
|
---|
532 | if (VBOX_FAILURE(rc))
|
---|
533 | return rc;
|
---|
534 | #endif
|
---|
535 | }
|
---|
536 |
|
---|
537 | /*
|
---|
538 | * Enter it into the Chunk TLB.
|
---|
539 | */
|
---|
540 | pTlbe->idChunk = idChunk;
|
---|
541 | pTlbe->pChunk = pMap;
|
---|
542 | pMap->iAge = 0;
|
---|
543 | }
|
---|
544 | else
|
---|
545 | {
|
---|
546 | Assert(PGM_PAGE_IS_ZERO(pPage));
|
---|
547 | *ppv = pVM->pgm.s.CTXALLSUFF(pvZeroPg);
|
---|
548 | *ppMap = NULL;
|
---|
549 | return VINF_SUCCESS;
|
---|
550 | }
|
---|
551 |
|
---|
552 | *ppv = (uint8_t *)pMap->pv + (PGM_PAGE_GET_PAGE_IN_CHUNK(pPage) << PAGE_SHIFT);
|
---|
553 | *ppMap = pMap;
|
---|
554 | return VINF_SUCCESS;
|
---|
555 | #endif /* IN_RING3 */
|
---|
556 | }
|
---|
557 |
|
---|
558 |
|
---|
559 | #if !defined(IN_GC) && !defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
560 | /**
|
---|
561 | * Load a guest page into the ring-3 physical TLB.
|
---|
562 | *
|
---|
563 | * @returns VBox status code.
|
---|
564 | * @retval VINF_SUCCESS on success
|
---|
565 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
566 | * @param pPGM The PGM instance pointer.
|
---|
567 | * @param GCPhys The guest physical address in question.
|
---|
568 | */
|
---|
569 | int pgmPhysPageLoadIntoTlb(PPGM pPGM, RTGCPHYS GCPhys)
|
---|
570 | {
|
---|
571 | STAM_COUNTER_INC(&pPGM->CTX_MID_Z(Stat,PageMapTlbMisses));
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * Find the ram range.
|
---|
575 | * 99.8% of requests are expected to be in the first range.
|
---|
576 | */
|
---|
577 | PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
|
---|
578 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
579 | if (RT_UNLIKELY(off >= pRam->cb))
|
---|
580 | {
|
---|
581 | do
|
---|
582 | {
|
---|
583 | pRam = pRam->CTX_SUFF(pNext);
|
---|
584 | if (!pRam)
|
---|
585 | return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
|
---|
586 | off = GCPhys - pRam->GCPhys;
|
---|
587 | } while (off >= pRam->cb);
|
---|
588 | }
|
---|
589 |
|
---|
590 | /*
|
---|
591 | * Map the page.
|
---|
592 | * Make a special case for the zero page as it is kind of special.
|
---|
593 | */
|
---|
594 | PPGMPAGE pPage = &pRam->aPages[off >> PAGE_SHIFT];
|
---|
595 | PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
|
---|
596 | if (!PGM_PAGE_IS_ZERO(pPage))
|
---|
597 | {
|
---|
598 | void *pv;
|
---|
599 | PPGMPAGEMAP pMap;
|
---|
600 | int rc = pgmPhysPageMap(PGM2VM(pPGM), pPage, GCPhys, &pMap, &pv);
|
---|
601 | if (VBOX_FAILURE(rc))
|
---|
602 | return rc;
|
---|
603 | pTlbe->pMap = pMap;
|
---|
604 | pTlbe->pv = pv;
|
---|
605 | }
|
---|
606 | else
|
---|
607 | {
|
---|
608 | Assert(PGM_PAGE_GET_HCPHYS(pPage) == pPGM->HCPhysZeroPg);
|
---|
609 | pTlbe->pMap = NULL;
|
---|
610 | pTlbe->pv = pPGM->CTXALLSUFF(pvZeroPg);
|
---|
611 | }
|
---|
612 | pTlbe->pPage = pPage;
|
---|
613 | return VINF_SUCCESS;
|
---|
614 | }
|
---|
615 | #endif /* !IN_GC && !VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
|
---|
616 |
|
---|
617 |
|
---|
618 | /**
|
---|
619 | * Requests the mapping of a guest page into the current context.
|
---|
620 | *
|
---|
621 | * This API should only be used for very short term, as it will consume
|
---|
622 | * scarse resources (R0 and GC) in the mapping cache. When you're done
|
---|
623 | * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
|
---|
624 | *
|
---|
625 | * This API will assume your intention is to write to the page, and will
|
---|
626 | * therefore replace shared and zero pages. If you do not intend to modify
|
---|
627 | * the page, use the PGMPhysGCPhys2CCPtrReadOnly() API.
|
---|
628 | *
|
---|
629 | * @returns VBox status code.
|
---|
630 | * @retval VINF_SUCCESS on success.
|
---|
631 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
632 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
633 | *
|
---|
634 | * @param pVM The VM handle.
|
---|
635 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
636 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
637 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
638 | *
|
---|
639 | * @remark Avoid calling this API from within critical sections (other than
|
---|
640 | * the PGM one) because of the deadlock risk.
|
---|
641 | * @thread Any thread.
|
---|
642 | */
|
---|
643 | VMMDECL(int) PGMPhysGCPhys2CCPtr(PVM pVM, RTGCPHYS GCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
644 | {
|
---|
645 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
646 | # if defined(IN_GC) && defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
647 | /* Until a physical TLB is implemented for GC or/and R0-darwin, let PGMDynMapGCPageEx handle it. */
|
---|
648 | return PGMDynMapGCPageOff(pVM, GCPhys, ppv);
|
---|
649 |
|
---|
650 | #else
|
---|
651 | int rc = pgmLock(pVM);
|
---|
652 | AssertRCReturn(rc);
|
---|
653 |
|
---|
654 | /*
|
---|
655 | * Query the Physical TLB entry for the page (may fail).
|
---|
656 | */
|
---|
657 | PGMPHYSTLBE pTlbe;
|
---|
658 | int rc = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
|
---|
659 | if (RT_SUCCESS(rc))
|
---|
660 | {
|
---|
661 | /*
|
---|
662 | * If the page is shared, the zero page, or being write monitored
|
---|
663 | * it must be converted to an page that's writable if possible.
|
---|
664 | */
|
---|
665 | PPGMPAGE pPage = pTlbe->pPage;
|
---|
666 | if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
|
---|
667 | {
|
---|
668 | rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
|
---|
669 | /** @todo stuff is missing here! */
|
---|
670 | }
|
---|
671 | if (RT_SUCCESS(rc))
|
---|
672 | {
|
---|
673 | /*
|
---|
674 | * Now, just perform the locking and calculate the return address.
|
---|
675 | */
|
---|
676 | PPGMPAGEMAP pMap = pTlbe->pMap;
|
---|
677 | pMap->cRefs++;
|
---|
678 | if (RT_LIKELY(pPage->cLocks != PGM_PAGE_MAX_LOCKS))
|
---|
679 | if (RT_UNLIKELY(++pPage->cLocks == PGM_PAGE_MAX_LOCKS))
|
---|
680 | {
|
---|
681 | AssertMsgFailed(("%VGp is entering permanent locked state!\n", GCPhys));
|
---|
682 | pMap->cRefs++; /* Extra ref to prevent it from going away. */
|
---|
683 | }
|
---|
684 |
|
---|
685 | *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
|
---|
686 | pLock->pvPage = pPage;
|
---|
687 | pLock->pvMap = pMap;
|
---|
688 | }
|
---|
689 | }
|
---|
690 |
|
---|
691 | pgmUnlock(pVM);
|
---|
692 | return rc;
|
---|
693 |
|
---|
694 | #endif /* IN_RING3 || IN_RING0 */
|
---|
695 |
|
---|
696 | #else
|
---|
697 | /*
|
---|
698 | * Temporary fallback code.
|
---|
699 | */
|
---|
700 | # if defined(IN_GC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
701 | return PGMDynMapGCPageOff(pVM, GCPhys, ppv);
|
---|
702 | # else
|
---|
703 | return PGMPhysGCPhys2HCPtr(pVM, GCPhys, 1, ppv);
|
---|
704 | # endif
|
---|
705 | #endif
|
---|
706 | }
|
---|
707 |
|
---|
708 |
|
---|
709 | /**
|
---|
710 | * Requests the mapping of a guest page into the current context.
|
---|
711 | *
|
---|
712 | * This API should only be used for very short term, as it will consume
|
---|
713 | * scarse resources (R0 and GC) in the mapping cache. When you're done
|
---|
714 | * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
|
---|
715 | *
|
---|
716 | * @returns VBox status code.
|
---|
717 | * @retval VINF_SUCCESS on success.
|
---|
718 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
719 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
720 | *
|
---|
721 | * @param pVM The VM handle.
|
---|
722 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
723 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
724 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
725 | *
|
---|
726 | * @remark Avoid calling this API from within critical sections (other than
|
---|
727 | * the PGM one) because of the deadlock risk.
|
---|
728 | * @thread Any thread.
|
---|
729 | */
|
---|
730 | VMMDECL(int) PGMPhysGCPhys2CCPtrReadOnly(PVM pVM, RTGCPHYS GCPhys, void const **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
731 | {
|
---|
732 | /** @todo implement this */
|
---|
733 | return PGMPhysGCPhys2CCPtr(pVM, GCPhys, (void **)ppv, pLock);
|
---|
734 | }
|
---|
735 |
|
---|
736 |
|
---|
737 | /**
|
---|
738 | * Requests the mapping of a guest page given by virtual address into the current context.
|
---|
739 | *
|
---|
740 | * This API should only be used for very short term, as it will consume
|
---|
741 | * scarse resources (R0 and GC) in the mapping cache. When you're done
|
---|
742 | * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
|
---|
743 | *
|
---|
744 | * This API will assume your intention is to write to the page, and will
|
---|
745 | * therefore replace shared and zero pages. If you do not intend to modify
|
---|
746 | * the page, use the PGMPhysGCPtr2CCPtrReadOnly() API.
|
---|
747 | *
|
---|
748 | * @returns VBox status code.
|
---|
749 | * @retval VINF_SUCCESS on success.
|
---|
750 | * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
|
---|
751 | * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
|
---|
752 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
753 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
754 | *
|
---|
755 | * @param pVM The VM handle.
|
---|
756 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
757 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
758 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
759 | *
|
---|
760 | * @remark Avoid calling this API from within critical sections (other than
|
---|
761 | * the PGM one) because of the deadlock risk.
|
---|
762 | * @thread EMT
|
---|
763 | */
|
---|
764 | VMMDECL(int) PGMPhysGCPtr2CCPtr(PVM pVM, RTGCPTR GCPtr, void **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
765 | {
|
---|
766 | RTGCPHYS GCPhys;
|
---|
767 | int rc = PGMPhysGCPtr2GCPhys(pVM, GCPtr, &GCPhys);
|
---|
768 | if (VBOX_SUCCESS(rc))
|
---|
769 | rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys, ppv, pLock);
|
---|
770 | return rc;
|
---|
771 | }
|
---|
772 |
|
---|
773 |
|
---|
774 | /**
|
---|
775 | * Requests the mapping of a guest page given by virtual address into the current context.
|
---|
776 | *
|
---|
777 | * This API should only be used for very short term, as it will consume
|
---|
778 | * scarse resources (R0 and GC) in the mapping cache. When you're done
|
---|
779 | * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
|
---|
780 | *
|
---|
781 | * @returns VBox status code.
|
---|
782 | * @retval VINF_SUCCESS on success.
|
---|
783 | * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
|
---|
784 | * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
|
---|
785 | * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
|
---|
786 | * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
|
---|
787 | *
|
---|
788 | * @param pVM The VM handle.
|
---|
789 | * @param GCPhys The guest physical address of the page that should be mapped.
|
---|
790 | * @param ppv Where to store the address corresponding to GCPhys.
|
---|
791 | * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
|
---|
792 | *
|
---|
793 | * @remark Avoid calling this API from within critical sections (other than
|
---|
794 | * the PGM one) because of the deadlock risk.
|
---|
795 | * @thread EMT
|
---|
796 | */
|
---|
797 | VMMDECL(int) PGMPhysGCPtr2CCPtrReadOnly(PVM pVM, RTGCPTR GCPtr, void const **ppv, PPGMPAGEMAPLOCK pLock)
|
---|
798 | {
|
---|
799 | RTGCPHYS GCPhys;
|
---|
800 | int rc = PGMPhysGCPtr2GCPhys(pVM, GCPtr, &GCPhys);
|
---|
801 | if (VBOX_SUCCESS(rc))
|
---|
802 | rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys, ppv, pLock);
|
---|
803 | return rc;
|
---|
804 | }
|
---|
805 |
|
---|
806 |
|
---|
807 | /**
|
---|
808 | * Release the mapping of a guest page.
|
---|
809 | *
|
---|
810 | * This is the counter part of PGMPhysGCPhys2CCPtr, PGMPhysGCPhys2CCPtrReadOnly
|
---|
811 | * PGMPhysGCPtr2CCPtr and PGMPhysGCPtr2CCPtrReadOnly.
|
---|
812 | *
|
---|
813 | * @param pVM The VM handle.
|
---|
814 | * @param pLock The lock structure initialized by the mapping function.
|
---|
815 | */
|
---|
816 | VMMDECL(void) PGMPhysReleasePageMappingLock(PVM pVM, PPGMPAGEMAPLOCK pLock)
|
---|
817 | {
|
---|
818 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
819 | #if defined(IN_GC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
820 | /* currently nothing to do here. */
|
---|
821 | /* --- postponed
|
---|
822 | #elif defined(IN_RING0)
|
---|
823 | */
|
---|
824 |
|
---|
825 | #else /* IN_RING3 */
|
---|
826 | pgmLock(pVM);
|
---|
827 |
|
---|
828 | PPGMPAGE pPage = (PPGMPAGE)pLock->pvPage;
|
---|
829 | Assert(pPage->cLocks >= 1);
|
---|
830 | if (pPage->cLocks != PGM_PAGE_MAX_LOCKS)
|
---|
831 | pPage->cLocks--;
|
---|
832 |
|
---|
833 | PPGMCHUNKR3MAP pChunk = (PPGMCHUNKR3MAP)pLock->pvChunk;
|
---|
834 | Assert(pChunk->cRefs >= 1);
|
---|
835 | pChunk->cRefs--;
|
---|
836 | pChunk->iAge = 0;
|
---|
837 |
|
---|
838 | pgmUnlock(pVM);
|
---|
839 | #endif /* IN_RING3 */
|
---|
840 | #else
|
---|
841 | NOREF(pVM);
|
---|
842 | NOREF(pLock);
|
---|
843 | #endif
|
---|
844 | }
|
---|
845 |
|
---|
846 |
|
---|
847 | /**
|
---|
848 | * Converts a GC physical address to a HC pointer.
|
---|
849 | *
|
---|
850 | * @returns VINF_SUCCESS on success.
|
---|
851 | * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
|
---|
852 | * page but has no physical backing.
|
---|
853 | * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
|
---|
854 | * GC physical address.
|
---|
855 | * @returns VERR_PGM_GCPHYS_RANGE_CROSSES_BOUNDARY if the range crosses
|
---|
856 | * a dynamic ram chunk boundary
|
---|
857 | * @param pVM The VM handle.
|
---|
858 | * @param GCPhys The GC physical address to convert.
|
---|
859 | * @param cbRange Physical range
|
---|
860 | * @param pHCPtr Where to store the HC pointer on success.
|
---|
861 | */
|
---|
862 | VMMDECL(int) PGMPhysGCPhys2HCPtr(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange, PRTHCPTR pHCPtr) /** @todo @bugref{1865}: HCPtr -> R3Ptr */
|
---|
863 | {
|
---|
864 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
865 | VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
|
---|
866 | #endif
|
---|
867 |
|
---|
868 | if ((GCPhys & PGM_DYNAMIC_CHUNK_BASE_MASK) != ((GCPhys+cbRange-1) & PGM_DYNAMIC_CHUNK_BASE_MASK))
|
---|
869 | {
|
---|
870 | AssertMsgFailed(("%VGp - %VGp crosses a chunk boundary!!\n", GCPhys, GCPhys+cbRange));
|
---|
871 | LogRel(("PGMPhysGCPhys2HCPtr %VGp - %VGp crosses a chunk boundary!!\n", GCPhys, GCPhys+cbRange));
|
---|
872 | return VERR_PGM_GCPHYS_RANGE_CROSSES_BOUNDARY;
|
---|
873 | }
|
---|
874 |
|
---|
875 | PPGMRAMRANGE pRam;
|
---|
876 | PPGMPAGE pPage;
|
---|
877 | int rc = pgmPhysGetPageAndRangeEx(&pVM->pgm.s, GCPhys, &pPage, &pRam);
|
---|
878 | if (VBOX_FAILURE(rc))
|
---|
879 | return rc;
|
---|
880 |
|
---|
881 | #ifndef PGM_IGNORE_RAM_FLAGS_RESERVED
|
---|
882 | if (RT_UNLIKELY(PGM_PAGE_IS_RESERVED(pPage)))
|
---|
883 | return VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
884 | #endif
|
---|
885 |
|
---|
886 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
887 | if (RT_UNLIKELY(off + cbRange > pRam->cb))
|
---|
888 | {
|
---|
889 | AssertMsgFailed(("%VGp - %VGp crosses a chunk boundary!!\n", GCPhys, GCPhys + cbRange));
|
---|
890 | return VERR_PGM_GCPHYS_RANGE_CROSSES_BOUNDARY;
|
---|
891 | }
|
---|
892 |
|
---|
893 | if (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
|
---|
894 | {
|
---|
895 | unsigned iChunk = (off >> PGM_DYNAMIC_CHUNK_SHIFT);
|
---|
896 | #if defined(IN_GC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0) /* ASSUMES this is a rare occurence */
|
---|
897 | PRTR3UINTPTR paChunkR3Ptrs = (PRTR3UINTPTR)MMHyperR3ToCC(pVM, pRam->paChunkR3Ptrs);
|
---|
898 | *pHCPtr = (RTHCPTR)(paChunkR3Ptrs[iChunk] + (off & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
|
---|
899 | #else
|
---|
900 | *pHCPtr = (RTHCPTR)(pRam->paChunkR3Ptrs[iChunk] + (off & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
|
---|
901 | #endif
|
---|
902 | }
|
---|
903 | else if (RT_LIKELY(pRam->pvR3))
|
---|
904 | *pHCPtr = (RTHCPTR)((RTR3UINTPTR)pRam->pvR3 + off);
|
---|
905 | else
|
---|
906 | return VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
907 | return VINF_SUCCESS;
|
---|
908 | }
|
---|
909 |
|
---|
910 |
|
---|
911 | /**
|
---|
912 | * PGMPhysGCPhys2HCPtr convenience for use with assertions.
|
---|
913 | *
|
---|
914 | * @returns The HCPtr, NIL_RTHCPTR on failure.
|
---|
915 | * @param pVM The VM handle.
|
---|
916 | * @param GCPhys The GC Physical addresss.
|
---|
917 | * @param cbRange Physical range.
|
---|
918 | */
|
---|
919 | VMMDECL(RTHCPTR) PGMPhysGCPhys2HCPtrAssert(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange) /** @todo @bugref{1865}: HCPtr -> R3Ptr */
|
---|
920 | {
|
---|
921 | RTHCPTR HCPtr;
|
---|
922 | int rc = PGMPhysGCPhys2HCPtr(pVM, GCPhys, cbRange, &HCPtr);
|
---|
923 | if (VBOX_SUCCESS(rc))
|
---|
924 | return HCPtr;
|
---|
925 | return NIL_RTHCPTR;
|
---|
926 | }
|
---|
927 |
|
---|
928 |
|
---|
929 | /**
|
---|
930 | * Converts a guest pointer to a GC physical address.
|
---|
931 | *
|
---|
932 | * This uses the current CR3/CR0/CR4 of the guest.
|
---|
933 | *
|
---|
934 | * @returns VBox status code.
|
---|
935 | * @param pVM The VM Handle
|
---|
936 | * @param GCPtr The guest pointer to convert.
|
---|
937 | * @param pGCPhys Where to store the GC physical address.
|
---|
938 | */
|
---|
939 | VMMDECL(int) PGMPhysGCPtr2GCPhys(PVM pVM, RTGCPTR GCPtr, PRTGCPHYS pGCPhys)
|
---|
940 | {
|
---|
941 | int rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtr, NULL, pGCPhys);
|
---|
942 | if (pGCPhys && VBOX_SUCCESS(rc))
|
---|
943 | *pGCPhys |= (RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK;
|
---|
944 | return rc;
|
---|
945 | }
|
---|
946 |
|
---|
947 |
|
---|
948 | /**
|
---|
949 | * Converts a guest pointer to a HC physical address.
|
---|
950 | *
|
---|
951 | * This uses the current CR3/CR0/CR4 of the guest.
|
---|
952 | *
|
---|
953 | * @returns VBox status code.
|
---|
954 | * @param pVM The VM Handle
|
---|
955 | * @param GCPtr The guest pointer to convert.
|
---|
956 | * @param pHCPhys Where to store the HC physical address.
|
---|
957 | */
|
---|
958 | VMMDECL(int) PGMPhysGCPtr2HCPhys(PVM pVM, RTGCPTR GCPtr, PRTHCPHYS pHCPhys)
|
---|
959 | {
|
---|
960 | RTGCPHYS GCPhys;
|
---|
961 | int rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtr, NULL, &GCPhys);
|
---|
962 | if (VBOX_SUCCESS(rc))
|
---|
963 | rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), pHCPhys);
|
---|
964 | return rc;
|
---|
965 | }
|
---|
966 |
|
---|
967 |
|
---|
968 | /**
|
---|
969 | * Converts a guest pointer to a HC pointer.
|
---|
970 | *
|
---|
971 | * This uses the current CR3/CR0/CR4 of the guest.
|
---|
972 | *
|
---|
973 | * @returns VBox status code.
|
---|
974 | * @param pVM The VM Handle
|
---|
975 | * @param GCPtr The guest pointer to convert.
|
---|
976 | * @param pHCPtr Where to store the HC virtual address.
|
---|
977 | */
|
---|
978 | VMMDECL(int) PGMPhysGCPtr2HCPtr(PVM pVM, RTGCPTR GCPtr, PRTHCPTR pHCPtr) /** @todo @bugref{1865}: HCPtr -> R3Ptr */
|
---|
979 | {
|
---|
980 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
981 | VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
|
---|
982 | #endif
|
---|
983 |
|
---|
984 | RTGCPHYS GCPhys;
|
---|
985 | int rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtr, NULL, &GCPhys);
|
---|
986 | if (VBOX_SUCCESS(rc))
|
---|
987 | rc = PGMPhysGCPhys2HCPtr(pVM, GCPhys | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), 1 /* we always stay within one page */, pHCPtr);
|
---|
988 | return rc;
|
---|
989 | }
|
---|
990 |
|
---|
991 |
|
---|
992 | /**
|
---|
993 | * Converts a guest virtual address to a HC pointer by specfied CR3 and flags.
|
---|
994 | *
|
---|
995 | * @returns VBox status code.
|
---|
996 | * @param pVM The VM Handle
|
---|
997 | * @param GCPtr The guest pointer to convert.
|
---|
998 | * @param cr3 The guest CR3.
|
---|
999 | * @param fFlags Flags used for interpreting the PD correctly: X86_CR4_PSE and X86_CR4_PAE
|
---|
1000 | * @param pHCPtr Where to store the HC pointer.
|
---|
1001 | *
|
---|
1002 | * @remark This function is used by the REM at a time where PGM could
|
---|
1003 | * potentially not be in sync. It could also be used by a
|
---|
1004 | * future DBGF API to cpu state independent conversions.
|
---|
1005 | */
|
---|
1006 | VMMDECL(int) PGMPhysGCPtr2HCPtrByGstCR3(PVM pVM, RTGCPTR GCPtr, uint64_t cr3, unsigned fFlags, PRTHCPTR pHCPtr) /** @todo @bugref{1865}: HCPtr -> R3Ptr */
|
---|
1007 | {
|
---|
1008 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
1009 | VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
|
---|
1010 | #endif
|
---|
1011 | /*
|
---|
1012 | * PAE or 32-bit?
|
---|
1013 | */
|
---|
1014 | Assert(!CPUMIsGuestInLongMode(pVM));
|
---|
1015 |
|
---|
1016 | int rc;
|
---|
1017 | if (!(fFlags & X86_CR4_PAE))
|
---|
1018 | {
|
---|
1019 | PX86PD pPD;
|
---|
1020 | rc = PGM_GCPHYS_2_PTR(pVM, cr3 & X86_CR3_PAGE_MASK, &pPD);
|
---|
1021 | if (VBOX_SUCCESS(rc))
|
---|
1022 | {
|
---|
1023 | X86PDE Pde = pPD->a[(RTGCUINTPTR)GCPtr >> X86_PD_SHIFT];
|
---|
1024 | if (Pde.n.u1Present)
|
---|
1025 | {
|
---|
1026 | if ((fFlags & X86_CR4_PSE) && Pde.b.u1Size)
|
---|
1027 | { /* (big page) */
|
---|
1028 | rc = PGMPhysGCPhys2HCPtr(pVM, pgmGstGet4MBPhysPage(&pVM->pgm.s, Pde) | ((RTGCUINTPTR)GCPtr & X86_PAGE_4M_OFFSET_MASK), 1 /* we always stay within one page */, pHCPtr);
|
---|
1029 | }
|
---|
1030 | else
|
---|
1031 | { /* (normal page) */
|
---|
1032 | PX86PT pPT;
|
---|
1033 | rc = PGM_GCPHYS_2_PTR(pVM, Pde.u & X86_PDE_PG_MASK, &pPT);
|
---|
1034 | if (VBOX_SUCCESS(rc))
|
---|
1035 | {
|
---|
1036 | X86PTE Pte = pPT->a[((RTGCUINTPTR)GCPtr >> X86_PT_SHIFT) & X86_PT_MASK];
|
---|
1037 | if (Pte.n.u1Present)
|
---|
1038 | return PGMPhysGCPhys2HCPtr(pVM, (Pte.u & X86_PTE_PG_MASK) | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), 1 /* we always stay within one page */, pHCPtr);
|
---|
1039 | rc = VERR_PAGE_NOT_PRESENT;
|
---|
1040 | }
|
---|
1041 | }
|
---|
1042 | }
|
---|
1043 | else
|
---|
1044 | rc = VERR_PAGE_TABLE_NOT_PRESENT;
|
---|
1045 | }
|
---|
1046 | }
|
---|
1047 | else
|
---|
1048 | {
|
---|
1049 | /** @todo long mode! */
|
---|
1050 | Assert(PGMGetGuestMode(pVM) < PGMMODE_AMD64);
|
---|
1051 |
|
---|
1052 | PX86PDPT pPdpt;
|
---|
1053 | rc = PGM_GCPHYS_2_PTR(pVM, cr3 & X86_CR3_PAE_PAGE_MASK, &pPdpt);
|
---|
1054 | if (VBOX_SUCCESS(rc))
|
---|
1055 | {
|
---|
1056 | X86PDPE Pdpe = pPdpt->a[((RTGCUINTPTR)GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE];
|
---|
1057 | if (Pdpe.n.u1Present)
|
---|
1058 | {
|
---|
1059 | PX86PDPAE pPD;
|
---|
1060 | rc = PGM_GCPHYS_2_PTR(pVM, Pdpe.u & X86_PDPE_PG_MASK, &pPD);
|
---|
1061 | if (VBOX_SUCCESS(rc))
|
---|
1062 | {
|
---|
1063 | X86PDEPAE Pde = pPD->a[((RTGCUINTPTR)GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK];
|
---|
1064 | if (Pde.n.u1Present)
|
---|
1065 | {
|
---|
1066 | if ((fFlags & X86_CR4_PSE) && Pde.b.u1Size)
|
---|
1067 | { /* (big page) */
|
---|
1068 | rc = PGMPhysGCPhys2HCPtr(pVM, (Pde.u & X86_PDE2M_PAE_PG_MASK) | ((RTGCUINTPTR)GCPtr & X86_PAGE_2M_OFFSET_MASK), 1 /* we always stay within one page */, pHCPtr);
|
---|
1069 | }
|
---|
1070 | else
|
---|
1071 | { /* (normal page) */
|
---|
1072 | PX86PTPAE pPT;
|
---|
1073 | rc = PGM_GCPHYS_2_PTR(pVM, (Pde.u & X86_PDE_PAE_PG_MASK), &pPT);
|
---|
1074 | if (VBOX_SUCCESS(rc))
|
---|
1075 | {
|
---|
1076 | X86PTEPAE Pte = pPT->a[((RTGCUINTPTR)GCPtr >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK];
|
---|
1077 | if (Pte.n.u1Present)
|
---|
1078 | return PGMPhysGCPhys2HCPtr(pVM, (Pte.u & X86_PTE_PAE_PG_MASK) | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), 1 /* we always stay within one page */, pHCPtr);
|
---|
1079 | rc = VERR_PAGE_NOT_PRESENT;
|
---|
1080 | }
|
---|
1081 | }
|
---|
1082 | }
|
---|
1083 | else
|
---|
1084 | rc = VERR_PAGE_TABLE_NOT_PRESENT;
|
---|
1085 | }
|
---|
1086 | }
|
---|
1087 | else
|
---|
1088 | rc = VERR_PAGE_TABLE_NOT_PRESENT;
|
---|
1089 | }
|
---|
1090 | }
|
---|
1091 | return rc;
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 |
|
---|
1095 | #undef LOG_GROUP
|
---|
1096 | #define LOG_GROUP LOG_GROUP_PGM_PHYS_ACCESS
|
---|
1097 |
|
---|
1098 |
|
---|
1099 | #ifdef IN_RING3
|
---|
1100 | /**
|
---|
1101 | * Cache PGMPhys memory access
|
---|
1102 | *
|
---|
1103 | * @param pVM VM Handle.
|
---|
1104 | * @param pCache Cache structure pointer
|
---|
1105 | * @param GCPhys GC physical address
|
---|
1106 | * @param pbHC HC pointer corresponding to physical page
|
---|
1107 | *
|
---|
1108 | * @thread EMT.
|
---|
1109 | */
|
---|
1110 | static void pgmPhysCacheAdd(PVM pVM, PGMPHYSCACHE *pCache, RTGCPHYS GCPhys, uint8_t *pbR3)
|
---|
1111 | {
|
---|
1112 | uint32_t iCacheIndex;
|
---|
1113 |
|
---|
1114 | Assert(VM_IS_EMT(pVM));
|
---|
1115 |
|
---|
1116 | GCPhys = PHYS_PAGE_ADDRESS(GCPhys);
|
---|
1117 | pbR3 = (uint8_t *)PAGE_ADDRESS(pbR3);
|
---|
1118 |
|
---|
1119 | iCacheIndex = ((GCPhys >> PAGE_SHIFT) & PGM_MAX_PHYSCACHE_ENTRIES_MASK);
|
---|
1120 |
|
---|
1121 | ASMBitSet(&pCache->aEntries, iCacheIndex);
|
---|
1122 |
|
---|
1123 | pCache->Entry[iCacheIndex].GCPhys = GCPhys;
|
---|
1124 | pCache->Entry[iCacheIndex].pbR3 = pbR3;
|
---|
1125 | }
|
---|
1126 | #endif
|
---|
1127 |
|
---|
1128 | /**
|
---|
1129 | * Read physical memory.
|
---|
1130 | *
|
---|
1131 | * This API respects access handlers and MMIO. Use PGMPhysReadGCPhys() if you
|
---|
1132 | * want to ignore those.
|
---|
1133 | *
|
---|
1134 | * @param pVM VM Handle.
|
---|
1135 | * @param GCPhys Physical address start reading from.
|
---|
1136 | * @param pvBuf Where to put the read bits.
|
---|
1137 | * @param cbRead How many bytes to read.
|
---|
1138 | */
|
---|
1139 | VMMDECL(void) PGMPhysRead(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
|
---|
1140 | {
|
---|
1141 | #ifdef IN_RING3
|
---|
1142 | bool fGrabbedLock = false;
|
---|
1143 | #endif
|
---|
1144 |
|
---|
1145 | AssertMsg(cbRead > 0, ("don't even think about reading zero bytes!\n"));
|
---|
1146 | if (cbRead == 0)
|
---|
1147 | return;
|
---|
1148 |
|
---|
1149 | LogFlow(("PGMPhysRead: %VGp %d\n", GCPhys, cbRead));
|
---|
1150 |
|
---|
1151 | #ifdef IN_RING3
|
---|
1152 | if (!VM_IS_EMT(pVM))
|
---|
1153 | {
|
---|
1154 | pgmLock(pVM);
|
---|
1155 | fGrabbedLock = true;
|
---|
1156 | }
|
---|
1157 | #endif
|
---|
1158 |
|
---|
1159 | /*
|
---|
1160 | * Copy loop on ram ranges.
|
---|
1161 | */
|
---|
1162 | PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
1163 | for (;;)
|
---|
1164 | {
|
---|
1165 | /* Find range. */
|
---|
1166 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
1167 | pRam = pRam->CTX_SUFF(pNext);
|
---|
1168 | /* Inside range or not? */
|
---|
1169 | if (pRam && GCPhys >= pRam->GCPhys)
|
---|
1170 | {
|
---|
1171 | /*
|
---|
1172 | * Must work our way thru this page by page.
|
---|
1173 | */
|
---|
1174 | RTGCPHYS off = GCPhys - pRam->GCPhys;
|
---|
1175 | while (off < pRam->cb)
|
---|
1176 | {
|
---|
1177 | unsigned iPage = off >> PAGE_SHIFT;
|
---|
1178 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
1179 | size_t cb;
|
---|
1180 |
|
---|
1181 | /* Physical chunk in dynamically allocated range not present? */
|
---|
1182 | if (RT_UNLIKELY(!PGM_PAGE_GET_HCPHYS(pPage)))
|
---|
1183 | {
|
---|
1184 | /* Treat it as reserved; return zeros */
|
---|
1185 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1186 | if (cb >= cbRead)
|
---|
1187 | {
|
---|
1188 | memset(pvBuf, 0, cbRead);
|
---|
1189 | goto end;
|
---|
1190 | }
|
---|
1191 | memset(pvBuf, 0, cb);
|
---|
1192 | }
|
---|
1193 | /* temp hacks, will be reorganized. */
|
---|
1194 | /*
|
---|
1195 | * Physical handler.
|
---|
1196 | */
|
---|
1197 | else if ( RT_UNLIKELY(PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) >= PGM_PAGE_HNDL_PHYS_STATE_ALL)
|
---|
1198 | && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
|
---|
1199 | {
|
---|
1200 | int rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
1201 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1202 |
|
---|
1203 | #ifdef IN_RING3 /** @todo deal with this in GC and R0! */
|
---|
1204 | /* find and call the handler */
|
---|
1205 | PPGMPHYSHANDLER pNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesR3->PhysHandlers, GCPhys);
|
---|
1206 | if (pNode && pNode->pfnHandlerR3)
|
---|
1207 | {
|
---|
1208 | size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
|
---|
1209 | if (cbRange < cb)
|
---|
1210 | cb = cbRange;
|
---|
1211 | if (cb > cbRead)
|
---|
1212 | cb = cbRead;
|
---|
1213 |
|
---|
1214 | void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1215 |
|
---|
1216 | /** @note Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
|
---|
1217 | rc = pNode->pfnHandlerR3(pVM, GCPhys, pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, pNode->pvUserR3);
|
---|
1218 | }
|
---|
1219 | #endif /* IN_RING3 */
|
---|
1220 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
1221 | {
|
---|
1222 | #if defined(IN_GC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1223 | void *pvSrc = NULL;
|
---|
1224 | PGMDynMapHCPageOff(pVM, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK), &pvSrc);
|
---|
1225 | #else
|
---|
1226 | void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1227 | #endif
|
---|
1228 |
|
---|
1229 | if (cb >= cbRead)
|
---|
1230 | {
|
---|
1231 | memcpy(pvBuf, pvSrc, cbRead);
|
---|
1232 | goto end;
|
---|
1233 | }
|
---|
1234 | memcpy(pvBuf, pvSrc, cb);
|
---|
1235 | }
|
---|
1236 | else if (cb >= cbRead)
|
---|
1237 | goto end;
|
---|
1238 | }
|
---|
1239 | /*
|
---|
1240 | * Virtual handlers.
|
---|
1241 | */
|
---|
1242 | else if ( RT_UNLIKELY(PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) >= PGM_PAGE_HNDL_VIRT_STATE_ALL)
|
---|
1243 | && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
|
---|
1244 | {
|
---|
1245 | int rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
1246 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1247 | #ifdef IN_RING3 /** @todo deal with this in GC and R0! */
|
---|
1248 | /* Search the whole tree for matching physical addresses (rather expensive!) */
|
---|
1249 | PPGMVIRTHANDLER pNode;
|
---|
1250 | unsigned iPage;
|
---|
1251 | int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pNode, &iPage);
|
---|
1252 | if (VBOX_SUCCESS(rc2) && pNode->pfnHandlerR3)
|
---|
1253 | {
|
---|
1254 | size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
|
---|
1255 | if (cbRange < cb)
|
---|
1256 | cb = cbRange;
|
---|
1257 | if (cb > cbRead)
|
---|
1258 | cb = cbRead;
|
---|
1259 | RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pNode->Core.Key & PAGE_BASE_GC_MASK)
|
---|
1260 | + (iPage << PAGE_SHIFT) + (off & PAGE_OFFSET_MASK);
|
---|
1261 |
|
---|
1262 | void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1263 |
|
---|
1264 | /* Note: Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
|
---|
1265 | rc = pNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, 0);
|
---|
1266 | }
|
---|
1267 | #endif /* IN_RING3 */
|
---|
1268 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
1269 | {
|
---|
1270 | #if defined(IN_GC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1271 | void *pvSrc = NULL;
|
---|
1272 | PGMDynMapHCPageOff(pVM, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK), &pvSrc);
|
---|
1273 | #else
|
---|
1274 | void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1275 | #endif
|
---|
1276 | if (cb >= cbRead)
|
---|
1277 | {
|
---|
1278 | memcpy(pvBuf, pvSrc, cbRead);
|
---|
1279 | goto end;
|
---|
1280 | }
|
---|
1281 | memcpy(pvBuf, pvSrc, cb);
|
---|
1282 | }
|
---|
1283 | else if (cb >= cbRead)
|
---|
1284 | goto end;
|
---|
1285 | }
|
---|
1286 | else
|
---|
1287 | {
|
---|
1288 | switch (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM)) /** @todo PAGE FLAGS */
|
---|
1289 | {
|
---|
1290 | /*
|
---|
1291 | * Normal memory or ROM.
|
---|
1292 | */
|
---|
1293 | case 0:
|
---|
1294 | case MM_RAM_FLAGS_ROM:
|
---|
1295 | case MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_RESERVED:
|
---|
1296 | //case MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2: /* = shadow */ - //MMIO2 isn't in the mask.
|
---|
1297 | case MM_RAM_FLAGS_MMIO2: // MMIO2 isn't in the mask.
|
---|
1298 | {
|
---|
1299 | #if defined(IN_GC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1300 | void *pvSrc = NULL;
|
---|
1301 | PGMDynMapHCPageOff(pVM, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK), &pvSrc);
|
---|
1302 | #else
|
---|
1303 | void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1304 | #endif
|
---|
1305 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1306 | if (cb >= cbRead)
|
---|
1307 | {
|
---|
1308 | #if defined(IN_RING3) && defined(PGM_PHYSMEMACCESS_CACHING)
|
---|
1309 | if (cbRead <= 4 && !fGrabbedLock /* i.e. EMT */)
|
---|
1310 | pgmPhysCacheAdd(pVM, &pVM->pgm.s.pgmphysreadcache, GCPhys, (uint8_t*)pvSrc);
|
---|
1311 | #endif /* IN_RING3 && PGM_PHYSMEMACCESS_CACHING */
|
---|
1312 | memcpy(pvBuf, pvSrc, cbRead);
|
---|
1313 | goto end;
|
---|
1314 | }
|
---|
1315 | memcpy(pvBuf, pvSrc, cb);
|
---|
1316 | break;
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | /*
|
---|
1320 | * All reserved, nothing there.
|
---|
1321 | */
|
---|
1322 | case MM_RAM_FLAGS_RESERVED:
|
---|
1323 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1324 | if (cb >= cbRead)
|
---|
1325 | {
|
---|
1326 | memset(pvBuf, 0, cbRead);
|
---|
1327 | goto end;
|
---|
1328 | }
|
---|
1329 | memset(pvBuf, 0, cb);
|
---|
1330 | break;
|
---|
1331 |
|
---|
1332 | /*
|
---|
1333 | * The rest needs to be taken more carefully.
|
---|
1334 | */
|
---|
1335 | default:
|
---|
1336 | #if 1 /** @todo r=bird: Can you do this properly please. */
|
---|
1337 | /** @todo Try MMIO; quick hack */
|
---|
1338 | if (cbRead <= 8 && IOMMMIORead(pVM, GCPhys, (uint32_t *)pvBuf, cbRead) == VINF_SUCCESS)
|
---|
1339 | goto end;
|
---|
1340 | #endif
|
---|
1341 |
|
---|
1342 | /** @todo fix me later. */
|
---|
1343 | AssertReleaseMsgFailed(("Unknown read at %VGp size %d implement the complex physical reading case %x\n",
|
---|
1344 | GCPhys, cbRead,
|
---|
1345 | pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM))); /** @todo PAGE FLAGS */
|
---|
1346 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1347 | break;
|
---|
1348 | }
|
---|
1349 | }
|
---|
1350 | cbRead -= cb;
|
---|
1351 | off += cb;
|
---|
1352 | pvBuf = (char *)pvBuf + cb;
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 | GCPhys = pRam->GCPhysLast + 1;
|
---|
1356 | }
|
---|
1357 | else
|
---|
1358 | {
|
---|
1359 | LogFlow(("PGMPhysRead: Unassigned %VGp size=%d\n", GCPhys, cbRead));
|
---|
1360 |
|
---|
1361 | /*
|
---|
1362 | * Unassigned address space.
|
---|
1363 | */
|
---|
1364 | size_t cb;
|
---|
1365 | if ( !pRam
|
---|
1366 | || (cb = pRam->GCPhys - GCPhys) >= cbRead)
|
---|
1367 | {
|
---|
1368 | memset(pvBuf, 0, cbRead);
|
---|
1369 | goto end;
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | memset(pvBuf, 0, cb);
|
---|
1373 | cbRead -= cb;
|
---|
1374 | pvBuf = (char *)pvBuf + cb;
|
---|
1375 | GCPhys += cb;
|
---|
1376 | }
|
---|
1377 | }
|
---|
1378 | end:
|
---|
1379 | #ifdef IN_RING3
|
---|
1380 | if (fGrabbedLock)
|
---|
1381 | pgmUnlock(pVM);
|
---|
1382 | #endif
|
---|
1383 | return;
|
---|
1384 | }
|
---|
1385 |
|
---|
1386 | /**
|
---|
1387 | * Write to physical memory.
|
---|
1388 | *
|
---|
1389 | * This API respects access handlers and MMIO. Use PGMPhysReadGCPhys() if you
|
---|
1390 | * want to ignore those.
|
---|
1391 | *
|
---|
1392 | * @param pVM VM Handle.
|
---|
1393 | * @param GCPhys Physical address to write to.
|
---|
1394 | * @param pvBuf What to write.
|
---|
1395 | * @param cbWrite How many bytes to write.
|
---|
1396 | */
|
---|
1397 | VMMDECL(void) PGMPhysWrite(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite)
|
---|
1398 | {
|
---|
1399 | #ifdef IN_RING3
|
---|
1400 | bool fGrabbedLock = false;
|
---|
1401 | #endif
|
---|
1402 |
|
---|
1403 | AssertMsg(!pVM->pgm.s.fNoMorePhysWrites, ("Calling PGMPhysWrite after pgmR3Save()!\n"));
|
---|
1404 | AssertMsg(cbWrite > 0, ("don't even think about writing zero bytes!\n"));
|
---|
1405 | if (cbWrite == 0)
|
---|
1406 | return;
|
---|
1407 |
|
---|
1408 | LogFlow(("PGMPhysWrite: %VGp %d\n", GCPhys, cbWrite));
|
---|
1409 |
|
---|
1410 | #ifdef IN_RING3
|
---|
1411 | if (!VM_IS_EMT(pVM))
|
---|
1412 | {
|
---|
1413 | pgmLock(pVM);
|
---|
1414 | fGrabbedLock = true;
|
---|
1415 | }
|
---|
1416 | #endif
|
---|
1417 | /*
|
---|
1418 | * Copy loop on ram ranges.
|
---|
1419 | */
|
---|
1420 | PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
1421 | for (;;)
|
---|
1422 | {
|
---|
1423 | /* Find range. */
|
---|
1424 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
1425 | pRam = pRam->CTX_SUFF(pNext);
|
---|
1426 | /* Inside range or not? */
|
---|
1427 | if (pRam && GCPhys >= pRam->GCPhys)
|
---|
1428 | {
|
---|
1429 | /*
|
---|
1430 | * Must work our way thru this page by page.
|
---|
1431 | */
|
---|
1432 | unsigned off = GCPhys - pRam->GCPhys;
|
---|
1433 | while (off < pRam->cb)
|
---|
1434 | {
|
---|
1435 | unsigned iPage = off >> PAGE_SHIFT;
|
---|
1436 | PPGMPAGE pPage = &pRam->aPages[iPage];
|
---|
1437 |
|
---|
1438 | /* Physical chunk in dynamically allocated range not present? */
|
---|
1439 | if (RT_UNLIKELY(!PGM_PAGE_GET_HCPHYS(pPage)))
|
---|
1440 | {
|
---|
1441 | int rc;
|
---|
1442 | #ifdef IN_RING3
|
---|
1443 | if (fGrabbedLock)
|
---|
1444 | {
|
---|
1445 | pgmUnlock(pVM);
|
---|
1446 | rc = pgmr3PhysGrowRange(pVM, GCPhys);
|
---|
1447 | if (rc == VINF_SUCCESS)
|
---|
1448 | PGMPhysWrite(pVM, GCPhys, pvBuf, cbWrite); /* try again; can't assume pRam is still valid (paranoia) */
|
---|
1449 | return;
|
---|
1450 | }
|
---|
1451 | rc = pgmr3PhysGrowRange(pVM, GCPhys);
|
---|
1452 | #else
|
---|
1453 | rc = CTXALLMID(VMM, CallHost)(pVM, VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
|
---|
1454 | #endif
|
---|
1455 | if (rc != VINF_SUCCESS)
|
---|
1456 | goto end;
|
---|
1457 | }
|
---|
1458 |
|
---|
1459 | size_t cb;
|
---|
1460 | /* temporary hack, will reogranize is later. */
|
---|
1461 | /*
|
---|
1462 | * Virtual handlers
|
---|
1463 | */
|
---|
1464 | if ( PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage)
|
---|
1465 | && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
|
---|
1466 | {
|
---|
1467 | if (PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage))
|
---|
1468 | {
|
---|
1469 | /*
|
---|
1470 | * Physical write handler + virtual write handler.
|
---|
1471 | * Consider this a quick workaround for the CSAM + shadow caching problem.
|
---|
1472 | *
|
---|
1473 | * We hand it to the shadow caching first since it requires the unchanged
|
---|
1474 | * data. CSAM will have to put up with it already being changed.
|
---|
1475 | */
|
---|
1476 | int rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
1477 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1478 | #ifdef IN_RING3 /** @todo deal with this in GC and R0! */
|
---|
1479 | /* 1. The physical handler */
|
---|
1480 | PPGMPHYSHANDLER pPhysNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesR3->PhysHandlers, GCPhys);
|
---|
1481 | if (pPhysNode && pPhysNode->pfnHandlerR3)
|
---|
1482 | {
|
---|
1483 | size_t cbRange = pPhysNode->Core.KeyLast - GCPhys + 1;
|
---|
1484 | if (cbRange < cb)
|
---|
1485 | cb = cbRange;
|
---|
1486 | if (cb > cbWrite)
|
---|
1487 | cb = cbWrite;
|
---|
1488 |
|
---|
1489 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1490 |
|
---|
1491 | /* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
|
---|
1492 | rc = pPhysNode->pfnHandlerR3(pVM, GCPhys, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, pPhysNode->pvUserR3);
|
---|
1493 | }
|
---|
1494 |
|
---|
1495 | /* 2. The virtual handler (will see incorrect data) */
|
---|
1496 | PPGMVIRTHANDLER pVirtNode;
|
---|
1497 | unsigned iPage;
|
---|
1498 | int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pVirtNode, &iPage);
|
---|
1499 | if (VBOX_SUCCESS(rc2) && pVirtNode->pfnHandlerR3)
|
---|
1500 | {
|
---|
1501 | size_t cbRange = pVirtNode->Core.KeyLast - GCPhys + 1;
|
---|
1502 | if (cbRange < cb)
|
---|
1503 | cb = cbRange;
|
---|
1504 | if (cb > cbWrite)
|
---|
1505 | cb = cbWrite;
|
---|
1506 | RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirtNode->Core.Key & PAGE_BASE_GC_MASK)
|
---|
1507 | + (iPage << PAGE_SHIFT) + (off & PAGE_OFFSET_MASK);
|
---|
1508 |
|
---|
1509 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1510 |
|
---|
1511 | /* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
|
---|
1512 | rc2 = pVirtNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, 0);
|
---|
1513 | if ( ( rc2 != VINF_PGM_HANDLER_DO_DEFAULT
|
---|
1514 | && rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
1515 | || ( VBOX_FAILURE(rc2)
|
---|
1516 | && VBOX_SUCCESS(rc)))
|
---|
1517 | rc = rc2;
|
---|
1518 | }
|
---|
1519 | #endif /* IN_RING3 */
|
---|
1520 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
1521 | {
|
---|
1522 | #if defined(IN_GC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1523 | void *pvDst = NULL;
|
---|
1524 | PGMDynMapHCPageOff(pVM, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK), &pvDst);
|
---|
1525 | #else
|
---|
1526 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1527 | #endif
|
---|
1528 | if (cb >= cbWrite)
|
---|
1529 | {
|
---|
1530 | memcpy(pvDst, pvBuf, cbWrite);
|
---|
1531 | goto end;
|
---|
1532 | }
|
---|
1533 | memcpy(pvDst, pvBuf, cb);
|
---|
1534 | }
|
---|
1535 | else if (cb >= cbWrite)
|
---|
1536 | goto end;
|
---|
1537 | }
|
---|
1538 | else
|
---|
1539 | {
|
---|
1540 | int rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
1541 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1542 | #ifdef IN_RING3
|
---|
1543 | /** @todo deal with this in GC and R0! */
|
---|
1544 | /* Search the whole tree for matching physical addresses (rather expensive!) */
|
---|
1545 | PPGMVIRTHANDLER pNode;
|
---|
1546 | unsigned iPage;
|
---|
1547 | int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pNode, &iPage);
|
---|
1548 | if (VBOX_SUCCESS(rc2) && pNode->pfnHandlerR3)
|
---|
1549 | {
|
---|
1550 | size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
|
---|
1551 | if (cbRange < cb)
|
---|
1552 | cb = cbRange;
|
---|
1553 | if (cb > cbWrite)
|
---|
1554 | cb = cbWrite;
|
---|
1555 | RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pNode->Core.Key & PAGE_BASE_GC_MASK)
|
---|
1556 | + (iPage << PAGE_SHIFT) + (off & PAGE_OFFSET_MASK);
|
---|
1557 |
|
---|
1558 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1559 |
|
---|
1560 | /* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
|
---|
1561 | rc = pNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, 0);
|
---|
1562 | }
|
---|
1563 | #endif /* IN_RING3 */
|
---|
1564 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
1565 | {
|
---|
1566 | #if defined(IN_GC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1567 | void *pvDst = NULL;
|
---|
1568 | PGMDynMapHCPageOff(pVM, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK), &pvDst);
|
---|
1569 | #else
|
---|
1570 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1571 | #endif
|
---|
1572 | if (cb >= cbWrite)
|
---|
1573 | {
|
---|
1574 | memcpy(pvDst, pvBuf, cbWrite);
|
---|
1575 | goto end;
|
---|
1576 | }
|
---|
1577 | memcpy(pvDst, pvBuf, cb);
|
---|
1578 | }
|
---|
1579 | else if (cb >= cbWrite)
|
---|
1580 | goto end;
|
---|
1581 | }
|
---|
1582 | }
|
---|
1583 | /*
|
---|
1584 | * Physical handler.
|
---|
1585 | */
|
---|
1586 | else if ( RT_UNLIKELY(PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) >= PGM_PAGE_HNDL_PHYS_STATE_WRITE)
|
---|
1587 | && !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
|
---|
1588 | {
|
---|
1589 | int rc = VINF_PGM_HANDLER_DO_DEFAULT;
|
---|
1590 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1591 | #ifdef IN_RING3 /** @todo deal with this in GC and R0! */
|
---|
1592 | /* find and call the handler */
|
---|
1593 | PPGMPHYSHANDLER pNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesR3->PhysHandlers, GCPhys);
|
---|
1594 | if (pNode && pNode->pfnHandlerR3)
|
---|
1595 | {
|
---|
1596 | size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
|
---|
1597 | if (cbRange < cb)
|
---|
1598 | cb = cbRange;
|
---|
1599 | if (cb > cbWrite)
|
---|
1600 | cb = cbWrite;
|
---|
1601 |
|
---|
1602 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1603 |
|
---|
1604 | /** @todo Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
|
---|
1605 | rc = pNode->pfnHandlerR3(pVM, GCPhys, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, pNode->pvUserR3);
|
---|
1606 | }
|
---|
1607 | #endif /* IN_RING3 */
|
---|
1608 | if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
|
---|
1609 | {
|
---|
1610 | #if defined(IN_GC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1611 | void *pvDst = NULL;
|
---|
1612 | PGMDynMapHCPageOff(pVM, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK), &pvDst);
|
---|
1613 | #else
|
---|
1614 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1615 | #endif
|
---|
1616 | if (cb >= cbWrite)
|
---|
1617 | {
|
---|
1618 | memcpy(pvDst, pvBuf, cbWrite);
|
---|
1619 | goto end;
|
---|
1620 | }
|
---|
1621 | memcpy(pvDst, pvBuf, cb);
|
---|
1622 | }
|
---|
1623 | else if (cb >= cbWrite)
|
---|
1624 | goto end;
|
---|
1625 | }
|
---|
1626 | else
|
---|
1627 | {
|
---|
1628 | /** @todo r=bird: missing MM_RAM_FLAGS_ROM here, we shall not allow anyone to overwrite the ROM! */
|
---|
1629 | switch (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)) /** @todo PAGE FLAGS */
|
---|
1630 | {
|
---|
1631 | /*
|
---|
1632 | * Normal memory, MMIO2 or writable shadow ROM.
|
---|
1633 | */
|
---|
1634 | case 0:
|
---|
1635 | case MM_RAM_FLAGS_MMIO2:
|
---|
1636 | case MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2: /* shadow rom */
|
---|
1637 | {
|
---|
1638 | #if defined(IN_GC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
|
---|
1639 | void *pvDst = NULL;
|
---|
1640 | PGMDynMapHCPageOff(pVM, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK), &pvDst);
|
---|
1641 | #else
|
---|
1642 | void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
|
---|
1643 | #endif
|
---|
1644 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1645 | if (cb >= cbWrite)
|
---|
1646 | {
|
---|
1647 | #if defined(IN_RING3) && defined(PGM_PHYSMEMACCESS_CACHING)
|
---|
1648 | if (cbWrite <= 4 && !fGrabbedLock /* i.e. EMT */)
|
---|
1649 | pgmPhysCacheAdd(pVM, &pVM->pgm.s.pgmphyswritecache, GCPhys, (uint8_t*)pvDst);
|
---|
1650 | #endif /* IN_RING3 && PGM_PHYSMEMACCESS_CACHING */
|
---|
1651 | memcpy(pvDst, pvBuf, cbWrite);
|
---|
1652 | goto end;
|
---|
1653 | }
|
---|
1654 | memcpy(pvDst, pvBuf, cb);
|
---|
1655 | break;
|
---|
1656 | }
|
---|
1657 |
|
---|
1658 | /*
|
---|
1659 | * All reserved, nothing there.
|
---|
1660 | */
|
---|
1661 | case MM_RAM_FLAGS_RESERVED:
|
---|
1662 | case MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO2:
|
---|
1663 | cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
|
---|
1664 | if (cb >= cbWrite)
|
---|
1665 | goto end;
|
---|
1666 | break;
|
---|
1667 |
|
---|
1668 |
|
---|
1669 | /*
|
---|
1670 | * The rest needs to be taken more carefully.
|
---|
1671 | */
|
---|
1672 | default:
|
---|
1673 | #if 1 /** @todo r=bird: Can you do this properly please. */
|
---|
1674 | /** @todo Try MMIO; quick hack */
|
---|
1675 | if (cbWrite <= 8 && IOMMMIOWrite(pVM, GCPhys, *(uint32_t *)pvBuf, cbWrite) == VINF_SUCCESS)
|
---|
1676 | goto end;
|
---|
1677 | #endif
|
---|
1678 |
|
---|
1679 | /** @todo fix me later. */
|
---|
1680 | AssertReleaseMsgFailed(("Unknown write at %VGp size %d implement the complex physical writing case %x\n",
|
---|
1681 | GCPhys, cbWrite,
|
---|
1682 | (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)))); /** @todo PAGE FLAGS */
|
---|
1683 | /* skip the write */
|
---|
1684 | cb = cbWrite;
|
---|
1685 | break;
|
---|
1686 | }
|
---|
1687 | }
|
---|
1688 |
|
---|
1689 | cbWrite -= cb;
|
---|
1690 | off += cb;
|
---|
1691 | pvBuf = (const char *)pvBuf + cb;
|
---|
1692 | }
|
---|
1693 |
|
---|
1694 | GCPhys = pRam->GCPhysLast + 1;
|
---|
1695 | }
|
---|
1696 | else
|
---|
1697 | {
|
---|
1698 | /*
|
---|
1699 | * Unassigned address space.
|
---|
1700 | */
|
---|
1701 | size_t cb;
|
---|
1702 | if ( !pRam
|
---|
1703 | || (cb = pRam->GCPhys - GCPhys) >= cbWrite)
|
---|
1704 | goto end;
|
---|
1705 |
|
---|
1706 | cbWrite -= cb;
|
---|
1707 | pvBuf = (const char *)pvBuf + cb;
|
---|
1708 | GCPhys += cb;
|
---|
1709 | }
|
---|
1710 | }
|
---|
1711 | end:
|
---|
1712 | #ifdef IN_RING3
|
---|
1713 | if (fGrabbedLock)
|
---|
1714 | pgmUnlock(pVM);
|
---|
1715 | #endif
|
---|
1716 | return;
|
---|
1717 | }
|
---|
1718 |
|
---|
1719 | #if !defined(IN_GC) && !defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0) /* Ring 0 & 3 only */ /** @todo @bugref{1865,3202}: this'll be fun! */
|
---|
1720 |
|
---|
1721 | /**
|
---|
1722 | * Read from guest physical memory by GC physical address, bypassing
|
---|
1723 | * MMIO and access handlers.
|
---|
1724 | *
|
---|
1725 | * @returns VBox status.
|
---|
1726 | * @param pVM VM handle.
|
---|
1727 | * @param pvDst The destination address.
|
---|
1728 | * @param GCPhysSrc The source address (GC physical address).
|
---|
1729 | * @param cb The number of bytes to read.
|
---|
1730 | */
|
---|
1731 | VMMDECL(int) PGMPhysReadGCPhys(PVM pVM, void *pvDst, RTGCPHYS GCPhysSrc, size_t cb)
|
---|
1732 | {
|
---|
1733 | /*
|
---|
1734 | * Anything to be done?
|
---|
1735 | */
|
---|
1736 | if (!cb)
|
---|
1737 | return VINF_SUCCESS;
|
---|
1738 |
|
---|
1739 | /*
|
---|
1740 | * Loop ram ranges.
|
---|
1741 | */
|
---|
1742 | for (PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
1743 | pRam;
|
---|
1744 | pRam = pRam->CTX_SUFF(pNext))
|
---|
1745 | {
|
---|
1746 | RTGCPHYS off = GCPhysSrc - pRam->GCPhys;
|
---|
1747 | if (off < pRam->cb)
|
---|
1748 | {
|
---|
1749 | if (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
|
---|
1750 | {
|
---|
1751 | /* Copy page by page as we're not dealing with a linear HC range. */
|
---|
1752 | for (;;)
|
---|
1753 | {
|
---|
1754 | /* convert */
|
---|
1755 | void *pvSrc;
|
---|
1756 | int rc = pgmRamGCPhys2HCPtrWithRange(pVM, pRam, GCPhysSrc, &pvSrc);
|
---|
1757 | if (VBOX_FAILURE(rc))
|
---|
1758 | return rc;
|
---|
1759 |
|
---|
1760 | /* copy */
|
---|
1761 | size_t cbRead = PAGE_SIZE - ((RTGCUINTPTR)GCPhysSrc & PAGE_OFFSET_MASK);
|
---|
1762 | if (cbRead >= cb)
|
---|
1763 | {
|
---|
1764 | memcpy(pvDst, pvSrc, cb);
|
---|
1765 | return VINF_SUCCESS;
|
---|
1766 | }
|
---|
1767 | memcpy(pvDst, pvSrc, cbRead);
|
---|
1768 |
|
---|
1769 | /* next */
|
---|
1770 | cb -= cbRead;
|
---|
1771 | pvDst = (uint8_t *)pvDst + cbRead;
|
---|
1772 | GCPhysSrc += cbRead;
|
---|
1773 | }
|
---|
1774 | }
|
---|
1775 | else if (pRam->pvR3)
|
---|
1776 | {
|
---|
1777 | /* read */
|
---|
1778 | size_t cbRead = pRam->cb - off;
|
---|
1779 | if (cbRead >= cb)
|
---|
1780 | {
|
---|
1781 | memcpy(pvDst, (uint8_t *)pRam->pvR3 + off, cb);
|
---|
1782 | return VINF_SUCCESS;
|
---|
1783 | }
|
---|
1784 | memcpy(pvDst, (uint8_t *)pRam->pvR3 + off, cbRead);
|
---|
1785 |
|
---|
1786 | /* next */
|
---|
1787 | cb -= cbRead;
|
---|
1788 | pvDst = (uint8_t *)pvDst + cbRead;
|
---|
1789 | GCPhysSrc += cbRead;
|
---|
1790 | }
|
---|
1791 | else
|
---|
1792 | return VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
1793 | }
|
---|
1794 | else if (GCPhysSrc < pRam->GCPhysLast)
|
---|
1795 | break;
|
---|
1796 | }
|
---|
1797 | return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
|
---|
1798 | }
|
---|
1799 |
|
---|
1800 |
|
---|
1801 | /**
|
---|
1802 | * Write to guest physical memory referenced by GC pointer.
|
---|
1803 | * Write memory to GC physical address in guest physical memory.
|
---|
1804 | *
|
---|
1805 | * This will bypass MMIO and access handlers.
|
---|
1806 | *
|
---|
1807 | * @returns VBox status.
|
---|
1808 | * @param pVM VM handle.
|
---|
1809 | * @param GCPhysDst The GC physical address of the destination.
|
---|
1810 | * @param pvSrc The source buffer.
|
---|
1811 | * @param cb The number of bytes to write.
|
---|
1812 | */
|
---|
1813 | VMMDECL(int) PGMPhysWriteGCPhys(PVM pVM, RTGCPHYS GCPhysDst, const void *pvSrc, size_t cb)
|
---|
1814 | {
|
---|
1815 | /*
|
---|
1816 | * Anything to be done?
|
---|
1817 | */
|
---|
1818 | if (!cb)
|
---|
1819 | return VINF_SUCCESS;
|
---|
1820 |
|
---|
1821 | LogFlow(("PGMPhysWriteGCPhys: %VGp %d\n", GCPhysDst, cb));
|
---|
1822 |
|
---|
1823 | /*
|
---|
1824 | * Loop ram ranges.
|
---|
1825 | */
|
---|
1826 | for (PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
1827 | pRam;
|
---|
1828 | pRam = pRam->CTX_SUFF(pNext))
|
---|
1829 | {
|
---|
1830 | RTGCPHYS off = GCPhysDst - pRam->GCPhys;
|
---|
1831 | if (off < pRam->cb)
|
---|
1832 | {
|
---|
1833 | #ifdef VBOX_WITH_NEW_PHYS_CODE
|
---|
1834 | /** @todo PGMRamGCPhys2HCPtrWithRange. */
|
---|
1835 | #endif
|
---|
1836 | if (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
|
---|
1837 | {
|
---|
1838 | /* Copy page by page as we're not dealing with a linear HC range. */
|
---|
1839 | for (;;)
|
---|
1840 | {
|
---|
1841 | /* convert */
|
---|
1842 | void *pvDst;
|
---|
1843 | int rc = pgmRamGCPhys2HCPtrWithRange(pVM, pRam, GCPhysDst, &pvDst);
|
---|
1844 | if (VBOX_FAILURE(rc))
|
---|
1845 | return rc;
|
---|
1846 |
|
---|
1847 | /* copy */
|
---|
1848 | size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPhysDst & PAGE_OFFSET_MASK);
|
---|
1849 | if (cbWrite >= cb)
|
---|
1850 | {
|
---|
1851 | memcpy(pvDst, pvSrc, cb);
|
---|
1852 | return VINF_SUCCESS;
|
---|
1853 | }
|
---|
1854 | memcpy(pvDst, pvSrc, cbWrite);
|
---|
1855 |
|
---|
1856 | /* next */
|
---|
1857 | cb -= cbWrite;
|
---|
1858 | pvSrc = (uint8_t *)pvSrc + cbWrite;
|
---|
1859 | GCPhysDst += cbWrite;
|
---|
1860 | }
|
---|
1861 | }
|
---|
1862 | else if (pRam->pvR3)
|
---|
1863 | {
|
---|
1864 | /* write */
|
---|
1865 | size_t cbWrite = pRam->cb - off;
|
---|
1866 | if (cbWrite >= cb)
|
---|
1867 | {
|
---|
1868 | memcpy((uint8_t *)pRam->pvR3 + off, pvSrc, cb);
|
---|
1869 | return VINF_SUCCESS;
|
---|
1870 | }
|
---|
1871 | memcpy((uint8_t *)pRam->pvR3 + off, pvSrc, cbWrite);
|
---|
1872 |
|
---|
1873 | /* next */
|
---|
1874 | cb -= cbWrite;
|
---|
1875 | GCPhysDst += cbWrite;
|
---|
1876 | pvSrc = (uint8_t *)pvSrc + cbWrite;
|
---|
1877 | }
|
---|
1878 | else
|
---|
1879 | return VERR_PGM_PHYS_PAGE_RESERVED;
|
---|
1880 | }
|
---|
1881 | else if (GCPhysDst < pRam->GCPhysLast)
|
---|
1882 | break;
|
---|
1883 | }
|
---|
1884 | return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
|
---|
1885 | }
|
---|
1886 |
|
---|
1887 |
|
---|
1888 | /**
|
---|
1889 | * Read from guest physical memory referenced by GC pointer.
|
---|
1890 | *
|
---|
1891 | * This function uses the current CR3/CR0/CR4 of the guest and will
|
---|
1892 | * bypass access handlers and not set any accessed bits.
|
---|
1893 | *
|
---|
1894 | * @returns VBox status.
|
---|
1895 | * @param pVM VM handle.
|
---|
1896 | * @param pvDst The destination address.
|
---|
1897 | * @param GCPtrSrc The source address (GC pointer).
|
---|
1898 | * @param cb The number of bytes to read.
|
---|
1899 | */
|
---|
1900 | VMMDECL(int) PGMPhysReadGCPtr(PVM pVM, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
|
---|
1901 | {
|
---|
1902 | /*
|
---|
1903 | * Anything to do?
|
---|
1904 | */
|
---|
1905 | if (!cb)
|
---|
1906 | return VINF_SUCCESS;
|
---|
1907 |
|
---|
1908 | /*
|
---|
1909 | * Optimize reads within a single page.
|
---|
1910 | */
|
---|
1911 | if (((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
|
---|
1912 | {
|
---|
1913 | void *pvSrc;
|
---|
1914 | int rc = PGMPhysGCPtr2HCPtr(pVM, GCPtrSrc, &pvSrc);
|
---|
1915 | if (VBOX_FAILURE(rc))
|
---|
1916 | return rc;
|
---|
1917 | memcpy(pvDst, pvSrc, cb);
|
---|
1918 | return VINF_SUCCESS;
|
---|
1919 | }
|
---|
1920 |
|
---|
1921 | /*
|
---|
1922 | * Page by page.
|
---|
1923 | */
|
---|
1924 | for (;;)
|
---|
1925 | {
|
---|
1926 | /* convert */
|
---|
1927 | void *pvSrc;
|
---|
1928 | int rc = PGMPhysGCPtr2HCPtr(pVM, GCPtrSrc, &pvSrc);
|
---|
1929 | if (VBOX_FAILURE(rc))
|
---|
1930 | return rc;
|
---|
1931 |
|
---|
1932 | /* copy */
|
---|
1933 | size_t cbRead = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
|
---|
1934 | if (cbRead >= cb)
|
---|
1935 | {
|
---|
1936 | memcpy(pvDst, pvSrc, cb);
|
---|
1937 | return VINF_SUCCESS;
|
---|
1938 | }
|
---|
1939 | memcpy(pvDst, pvSrc, cbRead);
|
---|
1940 |
|
---|
1941 | /* next */
|
---|
1942 | cb -= cbRead;
|
---|
1943 | pvDst = (uint8_t *)pvDst + cbRead;
|
---|
1944 | GCPtrSrc += cbRead;
|
---|
1945 | }
|
---|
1946 | }
|
---|
1947 |
|
---|
1948 |
|
---|
1949 | /**
|
---|
1950 | * Write to guest physical memory referenced by GC pointer.
|
---|
1951 | *
|
---|
1952 | * This function uses the current CR3/CR0/CR4 of the guest and will
|
---|
1953 | * bypass access handlers and not set dirty or accessed bits.
|
---|
1954 | *
|
---|
1955 | * @returns VBox status.
|
---|
1956 | * @param pVM VM handle.
|
---|
1957 | * @param GCPtrDst The destination address (GC pointer).
|
---|
1958 | * @param pvSrc The source address.
|
---|
1959 | * @param cb The number of bytes to write.
|
---|
1960 | */
|
---|
1961 | VMMDECL(int) PGMPhysWriteGCPtr(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
|
---|
1962 | {
|
---|
1963 | /*
|
---|
1964 | * Anything to do?
|
---|
1965 | */
|
---|
1966 | if (!cb)
|
---|
1967 | return VINF_SUCCESS;
|
---|
1968 |
|
---|
1969 | LogFlow(("PGMPhysWriteGCPtr: %VGv %d\n", GCPtrDst, cb));
|
---|
1970 |
|
---|
1971 | /*
|
---|
1972 | * Optimize writes within a single page.
|
---|
1973 | */
|
---|
1974 | if (((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
|
---|
1975 | {
|
---|
1976 | void *pvDst;
|
---|
1977 | int rc = PGMPhysGCPtr2HCPtr(pVM, GCPtrDst, &pvDst);
|
---|
1978 | if (VBOX_FAILURE(rc))
|
---|
1979 | return rc;
|
---|
1980 | memcpy(pvDst, pvSrc, cb);
|
---|
1981 | return VINF_SUCCESS;
|
---|
1982 | }
|
---|
1983 |
|
---|
1984 | /*
|
---|
1985 | * Page by page.
|
---|
1986 | */
|
---|
1987 | for (;;)
|
---|
1988 | {
|
---|
1989 | /* convert */
|
---|
1990 | void *pvDst;
|
---|
1991 | int rc = PGMPhysGCPtr2HCPtr(pVM, GCPtrDst, &pvDst);
|
---|
1992 | if (VBOX_FAILURE(rc))
|
---|
1993 | return rc;
|
---|
1994 |
|
---|
1995 | /* copy */
|
---|
1996 | size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
|
---|
1997 | if (cbWrite >= cb)
|
---|
1998 | {
|
---|
1999 | memcpy(pvDst, pvSrc, cb);
|
---|
2000 | return VINF_SUCCESS;
|
---|
2001 | }
|
---|
2002 | memcpy(pvDst, pvSrc, cbWrite);
|
---|
2003 |
|
---|
2004 | /* next */
|
---|
2005 | cb -= cbWrite;
|
---|
2006 | pvSrc = (uint8_t *)pvSrc + cbWrite;
|
---|
2007 | GCPtrDst += cbWrite;
|
---|
2008 | }
|
---|
2009 | }
|
---|
2010 |
|
---|
2011 | /**
|
---|
2012 | * Read from guest physical memory referenced by GC pointer.
|
---|
2013 | *
|
---|
2014 | * This function uses the current CR3/CR0/CR4 of the guest and will
|
---|
2015 | * respect access handlers and set accessed bits.
|
---|
2016 | *
|
---|
2017 | * @returns VBox status.
|
---|
2018 | * @param pVM VM handle.
|
---|
2019 | * @param pvDst The destination address.
|
---|
2020 | * @param GCPtrSrc The source address (GC pointer).
|
---|
2021 | * @param cb The number of bytes to read.
|
---|
2022 | */
|
---|
2023 | /** @todo use the PGMPhysReadGCPtr name and rename the unsafe one to something appropriate */
|
---|
2024 | VMMDECL(int) PGMPhysReadGCPtrSafe(PVM pVM, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
|
---|
2025 | {
|
---|
2026 | RTGCPHYS GCPhys;
|
---|
2027 | int rc;
|
---|
2028 |
|
---|
2029 | /*
|
---|
2030 | * Anything to do?
|
---|
2031 | */
|
---|
2032 | if (!cb)
|
---|
2033 | return VINF_SUCCESS;
|
---|
2034 |
|
---|
2035 | LogFlow(("PGMPhysReadGCPtrSafe: %VGv %d\n", GCPtrSrc, cb));
|
---|
2036 |
|
---|
2037 | /*
|
---|
2038 | * Optimize reads within a single page.
|
---|
2039 | */
|
---|
2040 | if (((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
|
---|
2041 | {
|
---|
2042 | /* Convert virtual to physical address */
|
---|
2043 | rc = PGMPhysGCPtr2GCPhys(pVM, GCPtrSrc, &GCPhys);
|
---|
2044 | AssertRCReturn(rc, rc);
|
---|
2045 |
|
---|
2046 | /* mark the guest page as accessed. */
|
---|
2047 | rc = PGMGstModifyPage(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
|
---|
2048 | AssertRC(rc);
|
---|
2049 |
|
---|
2050 | PGMPhysRead(pVM, GCPhys, pvDst, cb);
|
---|
2051 | return VINF_SUCCESS;
|
---|
2052 | }
|
---|
2053 |
|
---|
2054 | /*
|
---|
2055 | * Page by page.
|
---|
2056 | */
|
---|
2057 | for (;;)
|
---|
2058 | {
|
---|
2059 | /* Convert virtual to physical address */
|
---|
2060 | rc = PGMPhysGCPtr2GCPhys(pVM, GCPtrSrc, &GCPhys);
|
---|
2061 | AssertRCReturn(rc, rc);
|
---|
2062 |
|
---|
2063 | /* mark the guest page as accessed. */
|
---|
2064 | int rc = PGMGstModifyPage(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
|
---|
2065 | AssertRC(rc);
|
---|
2066 |
|
---|
2067 | /* copy */
|
---|
2068 | size_t cbRead = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
|
---|
2069 | if (cbRead >= cb)
|
---|
2070 | {
|
---|
2071 | PGMPhysRead(pVM, GCPhys, pvDst, cb);
|
---|
2072 | return VINF_SUCCESS;
|
---|
2073 | }
|
---|
2074 | PGMPhysRead(pVM, GCPhys, pvDst, cbRead);
|
---|
2075 |
|
---|
2076 | /* next */
|
---|
2077 | cb -= cbRead;
|
---|
2078 | pvDst = (uint8_t *)pvDst + cbRead;
|
---|
2079 | GCPtrSrc += cbRead;
|
---|
2080 | }
|
---|
2081 | }
|
---|
2082 |
|
---|
2083 |
|
---|
2084 | /**
|
---|
2085 | * Write to guest physical memory referenced by GC pointer.
|
---|
2086 | *
|
---|
2087 | * This function uses the current CR3/CR0/CR4 of the guest and will
|
---|
2088 | * respect access handlers and set dirty and accessed bits.
|
---|
2089 | *
|
---|
2090 | * @returns VBox status.
|
---|
2091 | * @param pVM VM handle.
|
---|
2092 | * @param GCPtrDst The destination address (GC pointer).
|
---|
2093 | * @param pvSrc The source address.
|
---|
2094 | * @param cb The number of bytes to write.
|
---|
2095 | */
|
---|
2096 | /** @todo use the PGMPhysWriteGCPtr name and rename the unsafe one to something appropriate */
|
---|
2097 | VMMDECL(int) PGMPhysWriteGCPtrSafe(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
|
---|
2098 | {
|
---|
2099 | RTGCPHYS GCPhys;
|
---|
2100 | int rc;
|
---|
2101 |
|
---|
2102 | /*
|
---|
2103 | * Anything to do?
|
---|
2104 | */
|
---|
2105 | if (!cb)
|
---|
2106 | return VINF_SUCCESS;
|
---|
2107 |
|
---|
2108 | LogFlow(("PGMPhysWriteGCPtrSafe: %VGv %d\n", GCPtrDst, cb));
|
---|
2109 |
|
---|
2110 | /*
|
---|
2111 | * Optimize writes within a single page.
|
---|
2112 | */
|
---|
2113 | if (((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
|
---|
2114 | {
|
---|
2115 | /* Convert virtual to physical address */
|
---|
2116 | rc = PGMPhysGCPtr2GCPhys(pVM, GCPtrDst, &GCPhys);
|
---|
2117 | AssertRCReturn(rc, rc);
|
---|
2118 |
|
---|
2119 | /* mark the guest page as accessed and dirty. */
|
---|
2120 | rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
|
---|
2121 | AssertRC(rc);
|
---|
2122 |
|
---|
2123 | PGMPhysWrite(pVM, GCPhys, pvSrc, cb);
|
---|
2124 | return VINF_SUCCESS;
|
---|
2125 | }
|
---|
2126 |
|
---|
2127 | /*
|
---|
2128 | * Page by page.
|
---|
2129 | */
|
---|
2130 | for (;;)
|
---|
2131 | {
|
---|
2132 | /* Convert virtual to physical address */
|
---|
2133 | rc = PGMPhysGCPtr2GCPhys(pVM, GCPtrDst, &GCPhys);
|
---|
2134 | AssertRCReturn(rc, rc);
|
---|
2135 |
|
---|
2136 | /* mark the guest page as accessed and dirty. */
|
---|
2137 | rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
|
---|
2138 | AssertRC(rc);
|
---|
2139 |
|
---|
2140 | /* copy */
|
---|
2141 | size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
|
---|
2142 | if (cbWrite >= cb)
|
---|
2143 | {
|
---|
2144 | PGMPhysWrite(pVM, GCPhys, pvSrc, cb);
|
---|
2145 | return VINF_SUCCESS;
|
---|
2146 | }
|
---|
2147 | PGMPhysWrite(pVM, GCPhys, pvSrc, cbWrite);
|
---|
2148 |
|
---|
2149 | /* next */
|
---|
2150 | cb -= cbWrite;
|
---|
2151 | pvSrc = (uint8_t *)pvSrc + cbWrite;
|
---|
2152 | GCPtrDst += cbWrite;
|
---|
2153 | }
|
---|
2154 | }
|
---|
2155 |
|
---|
2156 | /**
|
---|
2157 | * Write to guest physical memory referenced by GC pointer and update the PTE.
|
---|
2158 | *
|
---|
2159 | * This function uses the current CR3/CR0/CR4 of the guest and will
|
---|
2160 | * bypass access handlers and set any dirty and accessed bits in the PTE.
|
---|
2161 | *
|
---|
2162 | * If you don't want to set the dirty bit, use PGMPhysWriteGCPtr().
|
---|
2163 | *
|
---|
2164 | * @returns VBox status.
|
---|
2165 | * @param pVM VM handle.
|
---|
2166 | * @param GCPtrDst The destination address (GC pointer).
|
---|
2167 | * @param pvSrc The source address.
|
---|
2168 | * @param cb The number of bytes to write.
|
---|
2169 | */
|
---|
2170 | VMMDECL(int) PGMPhysWriteGCPtrDirty(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
|
---|
2171 | {
|
---|
2172 | /*
|
---|
2173 | * Anything to do?
|
---|
2174 | */
|
---|
2175 | if (!cb)
|
---|
2176 | return VINF_SUCCESS;
|
---|
2177 |
|
---|
2178 | /*
|
---|
2179 | * Optimize writes within a single page.
|
---|
2180 | */
|
---|
2181 | if (((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
|
---|
2182 | {
|
---|
2183 | void *pvDst;
|
---|
2184 | int rc = PGMPhysGCPtr2HCPtr(pVM, GCPtrDst, &pvDst);
|
---|
2185 | if (VBOX_FAILURE(rc))
|
---|
2186 | return rc;
|
---|
2187 | memcpy(pvDst, pvSrc, cb);
|
---|
2188 | rc = PGMGstModifyPage(pVM, GCPtrDst, cb, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
|
---|
2189 | AssertRC(rc);
|
---|
2190 | return VINF_SUCCESS;
|
---|
2191 | }
|
---|
2192 |
|
---|
2193 | /*
|
---|
2194 | * Page by page.
|
---|
2195 | */
|
---|
2196 | for (;;)
|
---|
2197 | {
|
---|
2198 | /* convert */
|
---|
2199 | void *pvDst;
|
---|
2200 | int rc = PGMPhysGCPtr2HCPtr(pVM, GCPtrDst, &pvDst);
|
---|
2201 | if (VBOX_FAILURE(rc))
|
---|
2202 | return rc;
|
---|
2203 |
|
---|
2204 | /* mark the guest page as accessed and dirty. */
|
---|
2205 | rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
|
---|
2206 | AssertRC(rc);
|
---|
2207 |
|
---|
2208 | /* copy */
|
---|
2209 | size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
|
---|
2210 | if (cbWrite >= cb)
|
---|
2211 | {
|
---|
2212 | memcpy(pvDst, pvSrc, cb);
|
---|
2213 | return VINF_SUCCESS;
|
---|
2214 | }
|
---|
2215 | memcpy(pvDst, pvSrc, cbWrite);
|
---|
2216 |
|
---|
2217 | /* next */
|
---|
2218 | cb -= cbWrite;
|
---|
2219 | GCPtrDst += cbWrite;
|
---|
2220 | pvSrc = (char *)pvSrc + cbWrite;
|
---|
2221 | }
|
---|
2222 | }
|
---|
2223 |
|
---|
2224 | #endif /* !IN_GC */
|
---|
2225 |
|
---|
2226 |
|
---|
2227 |
|
---|
2228 | /**
|
---|
2229 | * Performs a read of guest virtual memory for instruction emulation.
|
---|
2230 | *
|
---|
2231 | * This will check permissions, raise exceptions and update the access bits.
|
---|
2232 | *
|
---|
2233 | * The current implementation will bypass all access handlers. It may later be
|
---|
2234 | * changed to at least respect MMIO.
|
---|
2235 | *
|
---|
2236 | *
|
---|
2237 | * @returns VBox status code suitable to scheduling.
|
---|
2238 | * @retval VINF_SUCCESS if the read was performed successfully.
|
---|
2239 | * @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
|
---|
2240 | * @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
|
---|
2241 | *
|
---|
2242 | * @param pVM The VM handle.
|
---|
2243 | * @param pCtxCore The context core.
|
---|
2244 | * @param pvDst Where to put the bytes we've read.
|
---|
2245 | * @param GCPtrSrc The source address.
|
---|
2246 | * @param cb The number of bytes to read. Not more than a page.
|
---|
2247 | *
|
---|
2248 | * @remark This function will dynamically map physical pages in GC. This may unmap
|
---|
2249 | * mappings done by the caller. Be careful!
|
---|
2250 | */
|
---|
2251 | VMMDECL(int) PGMPhysInterpretedRead(PVM pVM, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb)
|
---|
2252 | {
|
---|
2253 | Assert(cb <= PAGE_SIZE);
|
---|
2254 |
|
---|
2255 | /** @todo r=bird: This isn't perfect!
|
---|
2256 | * -# It's not checking for reserved bits being 1.
|
---|
2257 | * -# It's not correctly dealing with the access bit.
|
---|
2258 | * -# It's not respecting MMIO memory or any other access handlers.
|
---|
2259 | */
|
---|
2260 | /*
|
---|
2261 | * 1. Translate virtual to physical. This may fault.
|
---|
2262 | * 2. Map the physical address.
|
---|
2263 | * 3. Do the read operation.
|
---|
2264 | * 4. Set access bits if required.
|
---|
2265 | */
|
---|
2266 | int rc;
|
---|
2267 | unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
|
---|
2268 | if (cb <= cb1)
|
---|
2269 | {
|
---|
2270 | /*
|
---|
2271 | * Not crossing pages.
|
---|
2272 | */
|
---|
2273 | RTGCPHYS GCPhys;
|
---|
2274 | uint64_t fFlags;
|
---|
2275 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc, &fFlags, &GCPhys);
|
---|
2276 | if (VBOX_SUCCESS(rc))
|
---|
2277 | {
|
---|
2278 | /** @todo we should check reserved bits ... */
|
---|
2279 | void *pvSrc;
|
---|
2280 | rc = PGM_GCPHYS_2_PTR(pVM, GCPhys, &pvSrc);
|
---|
2281 | switch (rc)
|
---|
2282 | {
|
---|
2283 | case VINF_SUCCESS:
|
---|
2284 | Log(("PGMPhysInterpretedRead: pvDst=%p pvSrc=%p cb=%d\n", pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb));
|
---|
2285 | memcpy(pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
|
---|
2286 | break;
|
---|
2287 | case VERR_PGM_PHYS_PAGE_RESERVED:
|
---|
2288 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
2289 | memset(pvDst, 0, cb);
|
---|
2290 | break;
|
---|
2291 | default:
|
---|
2292 | return rc;
|
---|
2293 | }
|
---|
2294 |
|
---|
2295 | /** @todo access bit emulation isn't 100% correct. */
|
---|
2296 | if (!(fFlags & X86_PTE_A))
|
---|
2297 | {
|
---|
2298 | rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
|
---|
2299 | AssertRC(rc);
|
---|
2300 | }
|
---|
2301 | return VINF_SUCCESS;
|
---|
2302 | }
|
---|
2303 | }
|
---|
2304 | else
|
---|
2305 | {
|
---|
2306 | /*
|
---|
2307 | * Crosses pages.
|
---|
2308 | */
|
---|
2309 | unsigned cb2 = cb - cb1;
|
---|
2310 | uint64_t fFlags1;
|
---|
2311 | RTGCPHYS GCPhys1;
|
---|
2312 | uint64_t fFlags2;
|
---|
2313 | RTGCPHYS GCPhys2;
|
---|
2314 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc, &fFlags1, &GCPhys1);
|
---|
2315 | if (VBOX_SUCCESS(rc))
|
---|
2316 | rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
|
---|
2317 | if (VBOX_SUCCESS(rc))
|
---|
2318 | {
|
---|
2319 | /** @todo we should check reserved bits ... */
|
---|
2320 | AssertMsgFailed(("cb=%d cb1=%d cb2=%d GCPtrSrc=%VGv\n", cb, cb1, cb2, GCPtrSrc));
|
---|
2321 | void *pvSrc1;
|
---|
2322 | rc = PGM_GCPHYS_2_PTR(pVM, GCPhys1, &pvSrc1);
|
---|
2323 | switch (rc)
|
---|
2324 | {
|
---|
2325 | case VINF_SUCCESS:
|
---|
2326 | memcpy(pvDst, (uint8_t *)pvSrc1 + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
|
---|
2327 | break;
|
---|
2328 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
2329 | memset(pvDst, 0, cb1);
|
---|
2330 | break;
|
---|
2331 | default:
|
---|
2332 | return rc;
|
---|
2333 | }
|
---|
2334 |
|
---|
2335 | void *pvSrc2;
|
---|
2336 | rc = PGM_GCPHYS_2_PTR(pVM, GCPhys2, &pvSrc2);
|
---|
2337 | switch (rc)
|
---|
2338 | {
|
---|
2339 | case VINF_SUCCESS:
|
---|
2340 | memcpy((uint8_t *)pvDst + cb1, pvSrc2, cb2);
|
---|
2341 | break;
|
---|
2342 | case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
|
---|
2343 | memset((uint8_t *)pvDst + cb1, 0, cb2);
|
---|
2344 | break;
|
---|
2345 | default:
|
---|
2346 | return rc;
|
---|
2347 | }
|
---|
2348 |
|
---|
2349 | if (!(fFlags1 & X86_PTE_A))
|
---|
2350 | {
|
---|
2351 | rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
|
---|
2352 | AssertRC(rc);
|
---|
2353 | }
|
---|
2354 | if (!(fFlags2 & X86_PTE_A))
|
---|
2355 | {
|
---|
2356 | rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
|
---|
2357 | AssertRC(rc);
|
---|
2358 | }
|
---|
2359 | return VINF_SUCCESS;
|
---|
2360 | }
|
---|
2361 | }
|
---|
2362 |
|
---|
2363 | /*
|
---|
2364 | * Raise a #PF.
|
---|
2365 | */
|
---|
2366 | uint32_t uErr;
|
---|
2367 |
|
---|
2368 | /* Get the current privilege level. */
|
---|
2369 | uint32_t cpl = CPUMGetGuestCPL(pVM, pCtxCore);
|
---|
2370 | switch (rc)
|
---|
2371 | {
|
---|
2372 | case VINF_SUCCESS:
|
---|
2373 | uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
|
---|
2374 | break;
|
---|
2375 |
|
---|
2376 | case VERR_PAGE_NOT_PRESENT:
|
---|
2377 | case VERR_PAGE_TABLE_NOT_PRESENT:
|
---|
2378 | uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
|
---|
2379 | break;
|
---|
2380 |
|
---|
2381 | default:
|
---|
2382 | AssertMsgFailed(("rc=%Vrc GCPtrSrc=%VGv cb=%#x\n", rc, GCPtrSrc, cb));
|
---|
2383 | return rc;
|
---|
2384 | }
|
---|
2385 | Log(("PGMPhysInterpretedRead: GCPtrSrc=%VGv cb=%#x -> #PF(%#x)\n", GCPtrSrc, cb, uErr));
|
---|
2386 | return TRPMRaiseXcptErrCR2(pVM, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
|
---|
2387 | }
|
---|
2388 |
|
---|
2389 | /// @todo VMMDECL(int) PGMPhysInterpretedWrite(PVM pVM, PCPUMCTXCORE pCtxCore, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
|
---|
2390 |
|
---|
2391 |
|
---|