VirtualBox

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

Last change on this file since 6854 was 6854, checked in by vboxsync, 17 years ago

All the new ROM stuff. Had to change PGMROMPAGE a bit to make it easier to work with wrt. mapping.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette