PGMAllPhys.cpp revision 436b5c616e019c5e62053657c52d3ab5562ecbbf
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/* $Id$ */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/** @file
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * PGM - Page Manager and Monitor, Physical Memory Addressing.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * available from http://www.virtualbox.org. This file is free software;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * you can redistribute it and/or modify it under the terms of the GNU
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * General Public License (GPL) as published by the Free Software
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * additional information or have any questions.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/*******************************************************************************
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync* Defined Constants And Macros *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync*******************************************************************************/
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/** @def PGM_IGNORE_RAM_FLAGS_RESERVED
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Don't respect the MM_RAM_FLAGS_RESERVED flag when converting to HC addresses.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Since this flag is currently incorrectly kept set for ROM regions we will
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * have to ignore it for now so we don't break stuff.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @todo this has been fixed now I believe, remove this hack.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#define PGM_IGNORE_RAM_FLAGS_RESERVED
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync/*******************************************************************************
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync* Header Files *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync*******************************************************************************/
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#define LOG_GROUP LOG_GROUP_PGM_PHYS
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#include <VBox/pgm.h>
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#include <VBox/trpm.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <VBox/vmm.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <VBox/iom.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <VBox/em.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <VBox/rem.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include "PGMInternal.h"
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <VBox/vm.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <VBox/param.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <VBox/err.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <iprt/assert.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <iprt/string.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <iprt/asm.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <VBox/log.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#ifdef IN_RING3
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync# include <iprt/thread.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#endif
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#ifndef IN_RING3
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * \#PF Handler callback for Guest ROM range write access.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * We simply ignore the writes or fall back to the recompiler if we don't support the instruction.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code (appropritate for trap handling and GC return).
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM VM Handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param uErrorCode CPU Error code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pRegFrame Trap register frame.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pvFault The fault address (cr2).
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhysFault The GC physical address corresponding to pvFault.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pvUser User argument. Pointer to the ROM range structure.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(int) pgmPhysRomWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, void *pvFault, RTGCPHYS GCPhysFault, void *pvUser)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#ifdef VBOX_WITH_NEW_PHYS_CODE
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMROMRANGE pRom = (PPGMROMRANGE)pvUser;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync uint32_t iPage = GCPhysFault - pRom->GCPhys;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync Assert(iPage < (pRom->cb >> PAGE_SHIFT));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync switch (pRom->aPages[iPage].enmProt)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync case PGMROMPROT_READ_ROM_WRITE_IGNORE:
956230513f1e35e9d694c219c7009034b71bb836vboxsync case PGMROMPROT_READ_RAM_WRITE_IGNORE:
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync#endif
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * If it's a simple instruction which doesn't change the cpu state
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * we will simply skip it. Otherwise we'll have to defer it to REM.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync uint32_t cbOp;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync DISCPUSTATE Cpu;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = EMInterpretDisasOne(pVM, pRegFrame, &Cpu, &cbOp);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if ( RT_SUCCESS(rc)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync && Cpu.mode == CPUMODE_32BIT /** @todo why does this matter? */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync && !(Cpu.prefix & (PREFIX_REPNE | PREFIX_REP | PREFIX_SEG)))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync switch (Cpu.opcode)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** @todo Find other instructions we can safely skip, possibly
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * adding this kind of detection to DIS or EM. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync case OP_MOV:
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pRegFrame->rip += cbOp;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatRZGuestROMWriteHandled);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return VINF_SUCCESS;
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync else if (RT_UNLIKELY(rc == VERR_INTERNAL_ERROR))
956230513f1e35e9d694c219c7009034b71bb836vboxsync return rc;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#ifdef VBOX_WITH_NEW_PHYS_CODE
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync break;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync case PGMROMPROT_READ_RAM_WRITE_RAM:
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync rc = PGMHandlerPhysicalPageTempOff(pVM, pRom->GCPhys, GCPhysFault & X86_PTE_PG_MASK);
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync AssertRC(rc);
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync break; /** @todo Must restart the instruction, not use the interpreter! */
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync case PGMROMPROT_READ_ROM_WRITE_RAM:
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync /* Handle it in ring-3 because it's *way* easier there. */
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync break;
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync default:
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync AssertMsgFailedReturn(("enmProt=%d iPage=%d GCPhysFault=%RGp\n",
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync pRom->aPages[iPage].enmProt, iPage, GCPhysFault),
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync VERR_INTERNAL_ERROR);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync#endif
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatRZGuestROMWriteUnhandled);
956230513f1e35e9d694c219c7009034b71bb836vboxsync return VINF_EM_RAW_EMULATE_INSTR;
956230513f1e35e9d694c219c7009034b71bb836vboxsync}
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync#endif /* IN_RING3 */
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync/**
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Checks if Address Gate 20 is enabled or not.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns true if enabled.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns false if disabled.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pVM VM handle.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsyncVMMDECL(bool) PGMPhysIsA20Enabled(PVM pVM)
956230513f1e35e9d694c219c7009034b71bb836vboxsync{
956230513f1e35e9d694c219c7009034b71bb836vboxsync LogFlow(("PGMPhysIsA20Enabled %d\n", pVM->pgm.s.fA20Enabled));
956230513f1e35e9d694c219c7009034b71bb836vboxsync return !!pVM->pgm.s.fA20Enabled ; /* stupid MS compiler doesn't trust me. */
956230513f1e35e9d694c219c7009034b71bb836vboxsync}
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync/**
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Validates a GC physical address.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns true if valid.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns false if invalid.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pVM The VM handle.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param GCPhys The physical address to validate.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsyncVMMDECL(bool) PGMPhysIsGCPhysValid(PVM pVM, RTGCPHYS GCPhys)
956230513f1e35e9d694c219c7009034b71bb836vboxsync{
956230513f1e35e9d694c219c7009034b71bb836vboxsync PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
956230513f1e35e9d694c219c7009034b71bb836vboxsync return pPage != NULL;
956230513f1e35e9d694c219c7009034b71bb836vboxsync}
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync/**
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Checks if a GC physical address is a normal page,
956230513f1e35e9d694c219c7009034b71bb836vboxsync * i.e. not ROM, MMIO or reserved.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns true if normal.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns false if invalid, ROM, MMIO or reserved page.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The physical address to check.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(bool) PGMPhysIsGCPhysNormal(PVM pVM, RTGCPHYS GCPhys)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhys);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#ifdef VBOX_WITH_NEW_PHYS_CODE
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return pPage
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync && PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#else
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return pPage
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync && !(pPage->HCPhys & (MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO2));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#endif
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Converts a GC physical address to a HC physical address.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns VINF_SUCCESS on success.
7cd65b78ef52a960d2237ca56abb05385a12217cvboxsync * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
7cd65b78ef52a960d2237ca56abb05385a12217cvboxsync * page but has no physical backing.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
956230513f1e35e9d694c219c7009034b71bb836vboxsync * GC physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The GC physical address to convert.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pHCPhys Where to store the HC physical address on success.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(int) PGMPhysGCPhys2HCPhys(PVM pVM, RTGCPHYS GCPhys, PRTHCPHYS pHCPhys)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMPAGE pPage;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhys, &pPage);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_FAILURE(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return rc;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#ifndef PGM_IGNORE_RAM_FLAGS_RESERVED
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_UNLIKELY(pPage->HCPhys & MM_RAM_FLAGS_RESERVED)) /** @todo PAGE FLAGS */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return VERR_PGM_PHYS_PAGE_RESERVED;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#endif
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return VINF_SUCCESS;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Invalidates the GC page mapping TLB.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(void) PGMPhysInvalidatePageGCMapTLB(PVM pVM)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /* later */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync NOREF(pVM);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
bd7c18002f48884a132bb0967408b6111dec326evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Invalidates the ring-0 page mapping TLB.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(void) PGMPhysInvalidatePageR0MapTLB(PVM pVM)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PGMPhysInvalidatePageR3MapTLB(pVM);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Invalidates the ring-3 page mapping TLB.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(void) PGMPhysInvalidatePageR3MapTLB(PVM pVM)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pgmLock(pVM);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.PhysTlbHC.aEntries); i++)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pVM->pgm.s.PhysTlbHC.aEntries[i].GCPhys = NIL_RTGCPHYS;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pVM->pgm.s.PhysTlbHC.aEntries[i].pPage = 0;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pVM->pgm.s.PhysTlbHC.aEntries[i].pMap = 0;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pVM->pgm.s.PhysTlbHC.aEntries[i].pv = 0;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pgmUnlock(pVM);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
666edc71e2906904545617ad6fae769f7d0bbf08vboxsync
666edc71e2906904545617ad6fae769f7d0bbf08vboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Frees the specified RAM page.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This is used by ballooning and remapping MMIO2.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM Pointer to the shared VM structure.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pPage Pointer to the page structure.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param GCPhys The guest physical address of the page, if applicable.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncvoid pgmPhysFreePage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertFatal(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** @todo implement this... */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertFatalFailed();
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Makes sure that there is at least one handy page ready for use.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This will also take the appropriate actions when reaching water-marks.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns The following VBox status codes.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VINF_SUCCESS on success.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_EM_NO_MEMORY if we're really out of memory.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @remarks Must be called from within the PGM critical section. It may
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * nip back to ring-3/0 in some cases.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncstatic int pgmPhysEnsureHandyPage(PVM pVM)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** @remarks
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * low-water mark logic for R0 & GC:
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * - 75%: Set FF.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * - 50%: Force return to ring-3 ASAP.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * For ring-3 there is a little problem wrt to the recompiler, so:
956230513f1e35e9d694c219c7009034b71bb836vboxsync * - 75%: Set FF.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * - 50%: Try allocate pages; on failure we'll force REM to quite ASAP.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * The basic idea is that we should be able to get out of any situation with
bd7c18002f48884a132bb0967408b6111dec326evboxsync * only 50% of handy pages remaining.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * At the moment we'll not adjust the number of handy pages relative to the
956230513f1e35e9d694c219c7009034b71bb836vboxsync * actual VM RAM committment, that's too much work for now.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect));
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(pVM->pgm.s.cHandyPages <= RT_ELEMENTS(pVM->pgm.s.aHandyPages));
956230513f1e35e9d694c219c7009034b71bb836vboxsync if ( !pVM->pgm.s.cHandyPages
956230513f1e35e9d694c219c7009034b71bb836vboxsync#ifdef IN_RING3
956230513f1e35e9d694c219c7009034b71bb836vboxsync || pVM->pgm.s.cHandyPages - 1 <= RT_ELEMENTS(pVM->pgm.s.aHandyPages) / 2 /* 50% */
956230513f1e35e9d694c219c7009034b71bb836vboxsync#endif
956230513f1e35e9d694c219c7009034b71bb836vboxsync )
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
bd7c18002f48884a132bb0967408b6111dec326evboxsync Log(("PGM: cHandyPages=%u out of %u -> allocate more\n", pVM->pgm.s.cHandyPages, RT_ELEMENTS(pVM->pgm.s.aHandyPages)));
956230513f1e35e9d694c219c7009034b71bb836vboxsync#ifdef IN_RING3
956230513f1e35e9d694c219c7009034b71bb836vboxsync int rc = PGMR3PhysAllocateHandyPages(pVM);
956230513f1e35e9d694c219c7009034b71bb836vboxsync#elif defined(IN_RING0)
956230513f1e35e9d694c219c7009034b71bb836vboxsync int rc = VMMR0CallHost(pVM, VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES, 0);
956230513f1e35e9d694c219c7009034b71bb836vboxsync#else
956230513f1e35e9d694c219c7009034b71bb836vboxsync int rc = VMMGCCallHost(pVM, VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES, 0);
956230513f1e35e9d694c219c7009034b71bb836vboxsync#endif
956230513f1e35e9d694c219c7009034b71bb836vboxsync if (RT_UNLIKELY(rc != VINF_SUCCESS))
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(rc == VINF_EM_NO_MEMORY);
956230513f1e35e9d694c219c7009034b71bb836vboxsync if (!pVM->pgm.s.cHandyPages)
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync LogRel(("PGM: no more handy pages!\n"));
956230513f1e35e9d694c219c7009034b71bb836vboxsync return VERR_EM_NO_MEMORY;
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(VM_FF_ISSET(pVM, VM_FF_PGM_NEED_HANDY_PAGES));
956230513f1e35e9d694c219c7009034b71bb836vboxsync#ifdef IN_RING3
956230513f1e35e9d694c219c7009034b71bb836vboxsync REMR3NotifyFF(pVM);
956230513f1e35e9d694c219c7009034b71bb836vboxsync#else
956230513f1e35e9d694c219c7009034b71bb836vboxsync VM_FF_SET(pVM, VM_FF_TO_R3);
956230513f1e35e9d694c219c7009034b71bb836vboxsync#endif
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(pVM->pgm.s.cHandyPages <= RT_ELEMENTS(pVM->pgm.s.aHandyPages));
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync else if (pVM->pgm.s.cHandyPages - 1 <= (RT_ELEMENTS(pVM->pgm.s.aHandyPages) / 4) * 3) /* 75% */
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync VM_FF_SET(pVM, VM_FF_PGM_NEED_HANDY_PAGES);
956230513f1e35e9d694c219c7009034b71bb836vboxsync#ifndef IN_RING3
956230513f1e35e9d694c219c7009034b71bb836vboxsync if (pVM->pgm.s.cHandyPages - 1 <= RT_ELEMENTS(pVM->pgm.s.aHandyPages) / 2)
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync Log(("PGM: VM_FF_TO_R3 - cHandyPages=%u out of %u\n", pVM->pgm.s.cHandyPages - 1, RT_ELEMENTS(pVM->pgm.s.aHandyPages)));
956230513f1e35e9d694c219c7009034b71bb836vboxsync VM_FF_SET(pVM, VM_FF_TO_R3);
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync#endif
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync return VINF_SUCCESS;
956230513f1e35e9d694c219c7009034b71bb836vboxsync}
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync/**
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Replace a zero or shared page with new page that we can write to.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns The following VBox status codes.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @retval VINF_SUCCESS on success, pPage is modified.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @retval VERR_EM_NO_MEMORY if we're totally out of memory.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @todo Propagate VERR_EM_NO_MEMORY up the call tree.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pVM The VM address.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pPage The physical page tracking structure. This will
956230513f1e35e9d694c219c7009034b71bb836vboxsync * be modified on success.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param GCPhys The address of the page.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @remarks Must be called from within the PGM critical section. It may
956230513f1e35e9d694c219c7009034b71bb836vboxsync * nip back to ring-3/0 in some cases.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @remarks This function shouldn't really fail, however if it does
956230513f1e35e9d694c219c7009034b71bb836vboxsync * it probably means we've screwed up the size of the amount
956230513f1e35e9d694c219c7009034b71bb836vboxsync * and/or the low-water mark of handy pages. Or, that some
956230513f1e35e9d694c219c7009034b71bb836vboxsync * device I/O is causing a lot of pages to be allocated while
956230513f1e35e9d694c219c7009034b71bb836vboxsync * while the host is in a low-memory condition.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsyncint pgmPhysAllocPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
956230513f1e35e9d694c219c7009034b71bb836vboxsync{
956230513f1e35e9d694c219c7009034b71bb836vboxsync /*
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Ensure that we've got a page handy, take it and use it.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsync int rc = pgmPhysEnsureHandyPage(pVM);
956230513f1e35e9d694c219c7009034b71bb836vboxsync if (RT_FAILURE(rc))
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(rc == VERR_EM_NO_MEMORY);
956230513f1e35e9d694c219c7009034b71bb836vboxsync return rc;
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect));
956230513f1e35e9d694c219c7009034b71bb836vboxsync AssertMsg(PGM_PAGE_IS_ZERO(pPage) || PGM_PAGE_IS_SHARED(pPage), ("%d %RGp\n", PGM_PAGE_GET_STATE(pPage), GCPhys));
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(!PGM_PAGE_IS_MMIO(pPage));
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync uint32_t iHandyPage = --pVM->pgm.s.cHandyPages;
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(iHandyPage < RT_ELEMENTS(pVM->pgm.s.aHandyPages));
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys != NIL_RTHCPHYS);
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(!(pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(pVM->pgm.s.aHandyPages[iHandyPage].idPage != NIL_GMM_PAGEID);
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage == NIL_GMM_PAGEID);
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync /*
956230513f1e35e9d694c219c7009034b71bb836vboxsync * There are one or two action to be taken the next time we allocate handy pages:
956230513f1e35e9d694c219c7009034b71bb836vboxsync * - Tell the GMM (global memory manager) what the page is being used for.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * (Speeds up replacement operations - sharing and defragmenting.)
956230513f1e35e9d694c219c7009034b71bb836vboxsync * - If the current backing is shared, it must be freed.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsync const RTHCPHYS HCPhys = pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys;
956230513f1e35e9d694c219c7009034b71bb836vboxsync pVM->pgm.s.aHandyPages[iHandyPage].HCPhysGCPhys = GCPhys;
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync if (PGM_PAGE_IS_SHARED(pPage))
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage = PGM_PAGE_GET_PAGEID(pPage);
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(PGM_PAGE_GET_PAGEID(pPage) != NIL_GMM_PAGEID);
956230513f1e35e9d694c219c7009034b71bb836vboxsync VM_FF_SET(pVM, VM_FF_PGM_NEED_HANDY_PAGES);
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync Log2(("PGM: Replaced shared page %#x at %RGp with %#x / %RHp\n", PGM_PAGE_GET_PAGEID(pPage),
956230513f1e35e9d694c219c7009034b71bb836vboxsync GCPhys, pVM->pgm.s.aHandyPages[iHandyPage].idPage, HCPhys));
956230513f1e35e9d694c219c7009034b71bb836vboxsync STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,PageReplaceShared));
956230513f1e35e9d694c219c7009034b71bb836vboxsync pVM->pgm.s.cSharedPages--;
956230513f1e35e9d694c219c7009034b71bb836vboxsync AssertMsgFailed(("TODO: copy shared page content")); /** @todo err.. what about copying the page content? */
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync else
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync Log2(("PGM: Replaced zero page %RGp with %#x / %RHp\n", GCPhys, pVM->pgm.s.aHandyPages[iHandyPage].idPage, HCPhys));
956230513f1e35e9d694c219c7009034b71bb836vboxsync STAM_COUNTER_INC(&pVM->pgm.s.StatRZPageReplaceZero);
956230513f1e35e9d694c219c7009034b71bb836vboxsync pVM->pgm.s.cZeroPages--;
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(pVM->pgm.s.aHandyPages[iHandyPage].idSharedPage == NIL_GMM_PAGEID);
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync /*
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Do the PGMPAGE modifications.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsync pVM->pgm.s.cPrivatePages++;
956230513f1e35e9d694c219c7009034b71bb836vboxsync PGM_PAGE_SET_HCPHYS(pPage, HCPhys);
956230513f1e35e9d694c219c7009034b71bb836vboxsync PGM_PAGE_SET_PAGEID(pPage, pVM->pgm.s.aHandyPages[iHandyPage].idPage);
956230513f1e35e9d694c219c7009034b71bb836vboxsync PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync return VINF_SUCCESS;
956230513f1e35e9d694c219c7009034b71bb836vboxsync}
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync/**
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Deal with pages that are not writable, i.e. not in the ALLOCATED state.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns VBox status code.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @retval VINF_SUCCESS on success.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pVM The VM address.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pPage The physical page tracking structure.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param GCPhys The address of the page.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @remarks Called from within the PGM critical section.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsyncint pgmPhysPageMakeWritable(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys)
956230513f1e35e9d694c219c7009034b71bb836vboxsync{
956230513f1e35e9d694c219c7009034b71bb836vboxsync switch (PGM_PAGE_GET_STATE(pPage))
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync case PGM_PAGE_STATE_WRITE_MONITORED:
956230513f1e35e9d694c219c7009034b71bb836vboxsync PGM_PAGE_SET_WRITTEN_TO(pPage);
956230513f1e35e9d694c219c7009034b71bb836vboxsync PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
956230513f1e35e9d694c219c7009034b71bb836vboxsync /* fall thru */
956230513f1e35e9d694c219c7009034b71bb836vboxsync default: /* to shut up GCC */
956230513f1e35e9d694c219c7009034b71bb836vboxsync case PGM_PAGE_STATE_ALLOCATED:
956230513f1e35e9d694c219c7009034b71bb836vboxsync return VINF_SUCCESS;
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync /*
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Zero pages can be dummy pages for MMIO or reserved memory,
956230513f1e35e9d694c219c7009034b71bb836vboxsync * so we need to check the flags before joining cause with
956230513f1e35e9d694c219c7009034b71bb836vboxsync * shared page replacement.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsync case PGM_PAGE_STATE_ZERO:
956230513f1e35e9d694c219c7009034b71bb836vboxsync if (PGM_PAGE_IS_MMIO(pPage))
956230513f1e35e9d694c219c7009034b71bb836vboxsync return VERR_PGM_PHYS_PAGE_RESERVED;
956230513f1e35e9d694c219c7009034b71bb836vboxsync /* fall thru */
956230513f1e35e9d694c219c7009034b71bb836vboxsync case PGM_PAGE_STATE_SHARED:
956230513f1e35e9d694c219c7009034b71bb836vboxsync return pgmPhysAllocPage(pVM, pPage, GCPhys);
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync}
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync/**
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Internal usage: Map the page specified by its GMM ID.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * This is similar to pgmPhysPageMap
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @returns VBox status code.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pVM The VM handle.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param idPage The Page ID.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param HCPhys The physical address (for RC).
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param ppv Where to store the mapping address.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @remarks Called from within the PGM critical section.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsyncint pgmPhysPageMapByPageID(PVM pVM, uint32_t idPage, RTHCPHYS HCPhys, void **ppv)
956230513f1e35e9d694c219c7009034b71bb836vboxsync{
956230513f1e35e9d694c219c7009034b71bb836vboxsync /*
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Validation.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect));
956230513f1e35e9d694c219c7009034b71bb836vboxsync AssertReturn(HCPhys && !(HCPhys & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
956230513f1e35e9d694c219c7009034b71bb836vboxsync const uint32_t idChunk = idPage >> GMM_CHUNKID_SHIFT;
956230513f1e35e9d694c219c7009034b71bb836vboxsync AssertReturn(idChunk != NIL_GMM_CHUNKID, VERR_INVALID_PARAMETER);
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync#ifdef IN_RC
956230513f1e35e9d694c219c7009034b71bb836vboxsync /*
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Map it by HCPhys.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsync return PGMDynMapHCPage(pVM, HCPhys, ppv);
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync#elif defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
956230513f1e35e9d694c219c7009034b71bb836vboxsync /*
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Map it by HCPhys.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsync return pgmR0DynMapHCPageInlined(pVM, HCPhys, ppv);
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync#else
956230513f1e35e9d694c219c7009034b71bb836vboxsync /*
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Find/make Chunk TLB entry for the mapping chunk.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsync PPGMCHUNKR3MAP pMap;
956230513f1e35e9d694c219c7009034b71bb836vboxsync PPGMCHUNKR3MAPTLBE pTlbe = &pVM->pgm.s.ChunkR3Map.Tlb.aEntries[PGM_CHUNKR3MAPTLB_IDX(idChunk)];
956230513f1e35e9d694c219c7009034b71bb836vboxsync if (pTlbe->idChunk == idChunk)
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbHits));
956230513f1e35e9d694c219c7009034b71bb836vboxsync pMap = pTlbe->pChunk;
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync else
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbMisses));
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync /*
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Find the chunk, map it if necessary.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsync pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
956230513f1e35e9d694c219c7009034b71bb836vboxsync if (!pMap)
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync# ifdef IN_RING0
956230513f1e35e9d694c219c7009034b71bb836vboxsync int rc = VMMR0CallHost(pVM, VMMCALLHOST_PGM_MAP_CHUNK, idChunk);
956230513f1e35e9d694c219c7009034b71bb836vboxsync AssertRCReturn(rc, rc);
956230513f1e35e9d694c219c7009034b71bb836vboxsync pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
956230513f1e35e9d694c219c7009034b71bb836vboxsync Assert(pMap);
956230513f1e35e9d694c219c7009034b71bb836vboxsync# else
956230513f1e35e9d694c219c7009034b71bb836vboxsync int rc = pgmR3PhysChunkMap(pVM, idChunk, &pMap);
956230513f1e35e9d694c219c7009034b71bb836vboxsync if (RT_FAILURE(rc))
956230513f1e35e9d694c219c7009034b71bb836vboxsync return rc;
956230513f1e35e9d694c219c7009034b71bb836vboxsync# endif
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync /*
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Enter it into the Chunk TLB.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsync pTlbe->idChunk = idChunk;
956230513f1e35e9d694c219c7009034b71bb836vboxsync pTlbe->pChunk = pMap;
956230513f1e35e9d694c219c7009034b71bb836vboxsync pMap->iAge = 0;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = (uint8_t *)pMap->pv + ((idPage &GMM_PAGEID_IDX_MASK) << PAGE_SHIFT);
db22a92f701bfcc17823963d6c8b745f68d44e30vboxsync return VINF_SUCCESS;
db22a92f701bfcc17823963d6c8b745f68d44e30vboxsync#endif
db22a92f701bfcc17823963d6c8b745f68d44e30vboxsync}
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Maps a page into the current virtual address space so it can be accessed.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VINF_SUCCESS on success.
db22a92f701bfcc17823963d6c8b745f68d44e30vboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
db22a92f701bfcc17823963d6c8b745f68d44e30vboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pPage The physical page tracking structure.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The address of the page.
db22a92f701bfcc17823963d6c8b745f68d44e30vboxsync * @param ppMap Where to store the address of the mapping tracking structure.
db22a92f701bfcc17823963d6c8b745f68d44e30vboxsync * @param ppv Where to store the mapping address of the page. The page
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * offset is masked off!
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @remarks Called from within the PGM critical section.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncint pgmPhysPageMap(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, PPPGMPAGEMAP ppMap, void **ppv)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect));
db22a92f701bfcc17823963d6c8b745f68d44e30vboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
db22a92f701bfcc17823963d6c8b745f68d44e30vboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Just some sketchy GC/R0-darwin code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsync *ppMap = NULL;
956230513f1e35e9d694c219c7009034b71bb836vboxsync RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(pPage);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync Assert(HCPhys != pVM->pgm.s.HCPhysZeroPg);
956230513f1e35e9d694c219c7009034b71bb836vboxsync# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
956230513f1e35e9d694c219c7009034b71bb836vboxsync pgmR0DynMapHCPageInlined(&pVM->pgm.s, HCPhys, ppv);
956230513f1e35e9d694c219c7009034b71bb836vboxsync# else
956230513f1e35e9d694c219c7009034b71bb836vboxsync PGMDynMapHCPage(pVM, HCPhys, ppv);
956230513f1e35e9d694c219c7009034b71bb836vboxsync# endif
956230513f1e35e9d694c219c7009034b71bb836vboxsync return VINF_SUCCESS;
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync#else /* IN_RING3 || IN_RING0 */
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync /*
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Special case: ZERO and MMIO2 pages.
956230513f1e35e9d694c219c7009034b71bb836vboxsync */
956230513f1e35e9d694c219c7009034b71bb836vboxsync const uint32_t idChunk = PGM_PAGE_GET_CHUNKID(pPage);
956230513f1e35e9d694c219c7009034b71bb836vboxsync if (idChunk == NIL_GMM_CHUNKID)
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync AssertMsgReturn(PGM_PAGE_GET_PAGEID(pPage) == NIL_GMM_PAGEID, ("pPage=%R[pgmpage]\n", pPage), VERR_INTERNAL_ERROR);
956230513f1e35e9d694c219c7009034b71bb836vboxsync if (PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2)
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync /* Lookup the MMIO2 range and use pvR3 to calc the address. */
956230513f1e35e9d694c219c7009034b71bb836vboxsync PPGMRAMRANGE pRam = pgmPhysGetRange(&pVM->pgm.s, GCPhys);
956230513f1e35e9d694c219c7009034b71bb836vboxsync AssertMsgReturn(pRam || !pRam->pvR3, ("pRam=%p pPage=%R[pgmpage]\n", pRam, pPage), VERR_INTERNAL_ERROR);
956230513f1e35e9d694c219c7009034b71bb836vboxsync *ppv = (void *)((uintptr_t)pRam->pvR3 + (GCPhys - pRam->GCPhys));
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync else if (PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO)
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync /** @todo deal with aliased MMIO2 pages somehow...
956230513f1e35e9d694c219c7009034b71bb836vboxsync * One solution would be to seed MMIO2 pages to GMM and get unique Page IDs for
956230513f1e35e9d694c219c7009034b71bb836vboxsync * them, that would also avoid this mess. It would actually be kind of
956230513f1e35e9d694c219c7009034b71bb836vboxsync * elegant... */
956230513f1e35e9d694c219c7009034b71bb836vboxsync AssertFailedReturn(VERR_INTERNAL_ERROR);
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync else
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** @todo handle MMIO2 */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertMsgReturn(PGM_PAGE_IS_ZERO(pPage), ("pPage=%R[pgmpage]\n", pPage), VERR_INTERNAL_ERROR);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertMsgReturn(PGM_PAGE_GET_HCPHYS(pPage) == pVM->pgm.s.HCPhysZeroPg,
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync ("pPage=%R[pgmpage]\n", pPage),
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync VERR_INTERNAL_ERROR);
956230513f1e35e9d694c219c7009034b71bb836vboxsync *ppv = pVM->pgm.s.CTXALLSUFF(pvZeroPg);
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
956230513f1e35e9d694c219c7009034b71bb836vboxsync *ppMap = NULL;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return VINF_SUCCESS;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Find/make Chunk TLB entry for the mapping chunk.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMCHUNKR3MAP pMap;
956230513f1e35e9d694c219c7009034b71bb836vboxsync PPGMCHUNKR3MAPTLBE pTlbe = &pVM->pgm.s.ChunkR3Map.Tlb.aEntries[PGM_CHUNKR3MAPTLB_IDX(idChunk)];
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (pTlbe->idChunk == idChunk)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbHits));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pMap = pTlbe->pChunk;
956230513f1e35e9d694c219c7009034b71bb836vboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync else
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,ChunkR3MapTlbMisses));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Find the chunk, map it if necessary.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (!pMap)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync#ifdef IN_RING0
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc = VMMR0CallHost(pVM, VMMCALLHOST_PGM_MAP_CHUNK, idChunk);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertRCReturn(rc, rc);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pMap = (PPGMCHUNKR3MAP)RTAvlU32Get(&pVM->pgm.s.ChunkR3Map.pTree, idChunk);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync Assert(pMap);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#else
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc = pgmR3PhysChunkMap(pVM, idChunk, &pMap);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_FAILURE(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return rc;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#endif
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Enter it into the Chunk TLB.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pTlbe->idChunk = idChunk;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pTlbe->pChunk = pMap;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pMap->iAge = 0;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = (uint8_t *)pMap->pv + (PGM_PAGE_GET_PAGE_IN_CHUNK(pPage) << PAGE_SHIFT);
956230513f1e35e9d694c219c7009034b71bb836vboxsync *ppMap = pMap;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return VINF_SUCCESS;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#endif /* IN_RING3 */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#if !defined(IN_RC) && !defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
956230513f1e35e9d694c219c7009034b71bb836vboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Load a guest page into the ring-3 physical TLB.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VINF_SUCCESS on success
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pPGM The PGM instance pointer.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The guest physical address in question.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncint pgmPhysPageLoadIntoTlb(PPGM pPGM, RTGCPHYS GCPhys)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync STAM_COUNTER_INC(&pPGM->CTX_MID_Z(Stat,PageMapTlbMisses));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Find the ram range.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * 99.8% of requests are expected to be in the first range.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTGCPHYS off = GCPhys - pRam->GCPhys;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_UNLIKELY(off >= pRam->cb))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync do
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pRam = pRam->CTX_SUFF(pNext);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (!pRam)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync off = GCPhys - pRam->GCPhys;
956230513f1e35e9d694c219c7009034b71bb836vboxsync } while (off >= pRam->cb);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Map the page.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Make a special case for the zero page as it is kind of special.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMPAGE pPage = &pRam->aPages[off >> PAGE_SHIFT];
956230513f1e35e9d694c219c7009034b71bb836vboxsync PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (!PGM_PAGE_IS_ZERO(pPage))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync void *pv;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMPAGEMAP pMap;
956230513f1e35e9d694c219c7009034b71bb836vboxsync int rc = pgmPhysPageMap(PGM2VM(pPGM), pPage, GCPhys, &pMap, &pv);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_FAILURE(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return rc;
956230513f1e35e9d694c219c7009034b71bb836vboxsync pTlbe->pMap = pMap;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pTlbe->pv = pv;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync else
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync Assert(PGM_PAGE_GET_HCPHYS(pPage) == pPGM->HCPhysZeroPg);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pTlbe->pMap = NULL;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pTlbe->pv = pPGM->CTXALLSUFF(pvZeroPg);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pTlbe->pPage = pPage;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return VINF_SUCCESS;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Load a guest page into the ring-3 physical TLB.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VINF_SUCCESS on success
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pPGM The PGM instance pointer.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pPage Pointer to the PGMPAGE structure corresponding to
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * GCPhys.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The guest physical address in question.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncint pgmPhysPageLoadIntoTlbWithPage(PPGM pPGM, PPGMPAGE pPage, RTGCPHYS GCPhys)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync STAM_COUNTER_INC(&pPGM->CTX_MID_Z(Stat,PageMapTlbMisses));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Map the page.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Make a special case for the zero page as it is kind of special.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (!PGM_PAGE_IS_ZERO(pPage))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync void *pv;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMPAGEMAP pMap;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc = pgmPhysPageMap(PGM2VM(pPGM), pPage, GCPhys, &pMap, &pv);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_FAILURE(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return rc;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pTlbe->pMap = pMap;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pTlbe->pv = pv;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync else
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync Assert(PGM_PAGE_GET_HCPHYS(pPage) == pPGM->HCPhysZeroPg);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pTlbe->pMap = NULL;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pTlbe->pv = pPGM->CTXALLSUFF(pvZeroPg);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pTlbe->pPage = pPage;
956230513f1e35e9d694c219c7009034b71bb836vboxsync return VINF_SUCCESS;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#endif /* !IN_RC && !VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
956230513f1e35e9d694c219c7009034b71bb836vboxsync/**
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Internal version of PGMPhysGCPhys2CCPtr that expects the caller to
956230513f1e35e9d694c219c7009034b71bb836vboxsync * own the PGM lock and therefore not need to lock the mapped page.
956230513f1e35e9d694c219c7009034b71bb836vboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @retval VINF_SUCCESS on success.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The guest physical address of the page that should be mapped.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pPage Pointer to the PGMPAGE structure for the page.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param ppv Where to store the address corresponding to GCPhys.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @internal
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncint pgmPhysGCPhys2CCPtrInternal(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertReturn(pPage, VERR_INTERNAL_ERROR);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect) || VM_IS_EMT(pVM));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Make sure the page is writable.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_FAILURE(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return rc;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync Assert(PGM_PAGE_GET_HCPHYS(pPage) != 0);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Get the mapping address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#else
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMPAGEMAPTLBE pTlbe;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = pgmPhysPageQueryTlbeWithPage(&pVM->pgm.s, pPage, GCPhys, &pTlbe);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_FAILURE(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return rc;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#endif
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return VINF_SUCCESS;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Internal version of PGMPhysGCPhys2CCPtrReadOnly that expects the caller to
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * own the PGM lock and therefore not need to lock the mapped page.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VINF_SUCCESS on success.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The guest physical address of the page that should be mapped.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pPage Pointer to the PGMPAGE structure for the page.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param ppv Where to store the address corresponding to GCPhys.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @internal
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncint pgmPhysGCPhys2CCPtrInternalReadOnly(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, const void **ppv)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertReturn(pPage, VERR_INTERNAL_ERROR);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect) || VM_IS_EMT(pVM));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync Assert(PGM_PAGE_GET_HCPHYS(pPage) != 0);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Get the mapping address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK)); /** @todo add a read only flag? */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#else
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMPAGEMAPTLBE pTlbe;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = pgmPhysPageQueryTlbeWithPage(&pVM->pgm.s, pPage, GCPhys, &pTlbe);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_FAILURE(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return rc;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#endif
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return VINF_SUCCESS;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Requests the mapping of a guest page into the current context.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This API should only be used for very short term, as it will consume
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * scarse resources (R0 and GC) in the mapping cache. When you're done
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This API will assume your intention is to write to the page, and will
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * therefore replace shared and zero pages. If you do not intend to modify
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * the page, use the PGMPhysGCPhys2CCPtrReadOnly() API.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VINF_SUCCESS on success.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The guest physical address of the page that should be mapped.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param ppv Where to store the address corresponding to GCPhys.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
8f827447008ecf94b4c101b6ac2b795b760d802dvboxsync * @remark Avoid calling this API from within critical sections (other than
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * the PGM one) because of the deadlock risk.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @thread Any thread.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(int) PGMPhysGCPhys2CCPtr(PVM pVM, RTGCPHYS GCPhys, void **ppv, PPGMPAGEMAPLOCK pLock)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#ifdef VBOX_WITH_NEW_PHYS_CODE
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync# if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
956230513f1e35e9d694c219c7009034b71bb836vboxsync * Find the page and make sure it's writable.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMPAGE pPage;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhys, &pPage);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_SUCCESS(rc))
956230513f1e35e9d694c219c7009034b71bb836vboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_SUCCESS(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK)); /** @todo add a read only flag? */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#if 0
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pLock->pvMap = 0;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pLock->pvPage = pPage;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#else
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pLock->u32Dummy = UINT32_MAX;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#endif
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync# else
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc = pgmLock(pVM);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertRCReturn(rc, rc);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Query the Physical TLB entry for the page (may fail).
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMPAGEMAPTLBE pTlbe;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = pgmPhysPageQueryTlbe(&pVM->pgm.s, GCPhys, &pTlbe);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_SUCCESS(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * If the page is shared, the zero page, or being write monitored
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * it must be converted to an page that's writable if possible.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMPAGE pPage = pTlbe->pPage;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_UNLIKELY(PGM_PAGE_GET_STATE(pPage) != PGM_PAGE_STATE_ALLOCATED))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync rc = pgmPhysPageMakeWritable(pVM, pPage, GCPhys);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_SUCCESS(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = pgmPhysPageQueryTlbeWithPage(&pVM->pgm.s, pPage, GCPhys, &pTlbe);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_SUCCESS(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Now, just perform the locking and calculate the return address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PPGMPAGEMAP pMap = pTlbe->pMap;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pMap->cRefs++;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#if 0 /** @Todo implement locking properly */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_LIKELY(pPage->cLocks != PGM_PAGE_MAX_LOCKS))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_UNLIKELY(++pPage->cLocks == PGM_PAGE_MAX_LOCKS))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertMsgFailed(("%RGp is entering permanent locked state!\n", GCPhys));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pMap->cRefs++; /* Extra ref to prevent it from going away. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#endif
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *ppv = (void *)((uintptr_t)pTlbe->pv | (GCPhys & PAGE_OFFSET_MASK));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pLock->pvPage = pPage;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pLock->pvMap = pMap;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pgmUnlock(pVM);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#endif /* IN_RING3 || IN_RING0 */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return rc;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#else
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Temporary fallback code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync# if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/** @todo @bugref{3202}: check up this path. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return PGMDynMapGCPageOff(pVM, GCPhys, ppv);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync# else
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return PGMPhysGCPhys2R3Ptr(pVM, GCPhys, 1, (PRTR3PTR)ppv);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync# endif
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#endif
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Requests the mapping of a guest page into the current context.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This API should only be used for very short term, as it will consume
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * scarse resources (R0 and GC) in the mapping cache. When you're done
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
8f827447008ecf94b4c101b6ac2b795b760d802dvboxsync * @retval VINF_SUCCESS on success.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The guest physical address of the page that should be mapped.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param ppv Where to store the address corresponding to GCPhys.
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
956230513f1e35e9d694c219c7009034b71bb836vboxsync * @remark Avoid calling this API from within critical sections (other than
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * the PGM one) because of the deadlock risk.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @thread Any thread.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(int) PGMPhysGCPhys2CCPtrReadOnly(PVM pVM, RTGCPHYS GCPhys, void const **ppv, PPGMPAGEMAPLOCK pLock)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** @todo implement this */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return PGMPhysGCPhys2CCPtr(pVM, GCPhys, (void **)ppv, pLock);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Requests the mapping of a guest page given by virtual address into the current context.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This API should only be used for very short term, as it will consume
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * scarse resources (R0 and GC) in the mapping cache. When you're done
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This API will assume your intention is to write to the page, and will
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * therefore replace shared and zero pages. If you do not intend to modify
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * the page, use the PGMPhysGCPtr2CCPtrReadOnly() API.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns VBox status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VINF_SUCCESS on success.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPhys The guest physical address of the page that should be mapped.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param ppv Where to store the address corresponding to GCPhys.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @remark Avoid calling this API from within critical sections (other than
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * the PGM one) because of the deadlock risk.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @thread EMT
35d0bfb0c568c691325cade7618555e3d3df918avboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncVMMDECL(int) PGMPhysGCPtr2CCPtr(PVM pVM, RTGCPTR GCPtr, void **ppv, PPGMPAGEMAPLOCK pLock)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTGCPHYS GCPhys;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc = PGMPhysGCPtr2GCPhys(pVM, GCPtr, &GCPhys);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_SUCCESS(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys, ppv, pLock);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return rc;
956230513f1e35e9d694c219c7009034b71bb836vboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Requests the mapping of a guest page given by virtual address into the current context.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This API should only be used for very short term, as it will consume
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * scarse resources (R0 and GC) in the mapping cache. When you're done
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * with the page, call PGMPhysReleasePageMappingLock() ASAP to release it.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync *
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @returns VBox status code.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @retval VINF_SUCCESS on success.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @retval VERR_PAGE_TABLE_NOT_PRESENT if the page directory for the virtual address isn't present.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @retval VERR_PAGE_NOT_PRESENT if the page at the virtual address isn't present.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @retval VERR_PGM_PHYS_PAGE_RESERVED it it's a valid page but has no physical backing.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync *
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @param pVM The VM handle.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @param GCPhys The guest physical address of the page that should be mapped.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @param ppv Where to store the address corresponding to GCPhys.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @param pLock Where to store the lock information that PGMPhysReleasePageMappingLock needs.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync *
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @remark Avoid calling this API from within critical sections (other than
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * the PGM one) because of the deadlock risk.
35d0bfb0c568c691325cade7618555e3d3df918avboxsync * @thread EMT
35d0bfb0c568c691325cade7618555e3d3df918avboxsync */
35d0bfb0c568c691325cade7618555e3d3df918avboxsyncVMMDECL(int) PGMPhysGCPtr2CCPtrReadOnly(PVM pVM, RTGCPTR GCPtr, void const **ppv, PPGMPAGEMAPLOCK pLock)
35d0bfb0c568c691325cade7618555e3d3df918avboxsync{
35d0bfb0c568c691325cade7618555e3d3df918avboxsync RTGCPHYS GCPhys;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync int rc = PGMPhysGCPtr2GCPhys(pVM, GCPtr, &GCPhys);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync if (RT_SUCCESS(rc))
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys, ppv, pLock);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync return rc;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync}
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync/**
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * Release the mapping of a guest page.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * This is the counter part of PGMPhysGCPhys2CCPtr, PGMPhysGCPhys2CCPtrReadOnly
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync * PGMPhysGCPtr2CCPtr and PGMPhysGCPtr2CCPtrReadOnly.
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync *
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync * @param pVM The VM handle.
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync * @param pLock The lock structure initialized by the mapping function.
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync */
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsyncVMMDECL(void) PGMPhysReleasePageMappingLock(PVM pVM, PPGMPAGEMAPLOCK pLock)
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync{
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#ifdef VBOX_WITH_NEW_PHYS_CODE
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync /* currently nothing to do here. */
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync Assert(pLock->u32Dummy == UINT32_MAX);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync pLock->u32Dummy = 0;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#else /* IN_RING3 */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync PPGMPAGEMAP pMap = (PPGMPAGEMAP)pLock->pvMap;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync if (!pMap)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync {
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync /* The ZERO page and MMIO2 ends up here. */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync Assert(pLock->pvPage);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync pLock->pvPage = NULL;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync }
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync else
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync {
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync pgmLock(pVM);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync# if 0 /** @todo implement page locking */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync PPGMPAGE pPage = (PPGMPAGE)pLock->pvPage;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync Assert(pPage->cLocks >= 1);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync if (pPage->cLocks != PGM_PAGE_MAX_LOCKS)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync pPage->cLocks--;
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync# endif
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync Assert(pMap->cRefs >= 1);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync pMap->cRefs--;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync pMap->iAge = 0;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync pgmUnlock(pVM);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync }
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#endif /* IN_RING3 */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#else
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync NOREF(pVM);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync NOREF(pLock);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#endif
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync}
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync/**
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync * Converts a GC physical address to a HC ring-3 pointer.
d012a89a724ba60c4fb5e74ce51f8b404fda4a8cvboxsync *
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @returns VINF_SUCCESS on success.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @returns VERR_PGM_PHYS_PAGE_RESERVED it it's a valid GC physical
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * page but has no physical backing.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @returns VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * GC physical address.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @returns VERR_PGM_GCPHYS_RANGE_CROSSES_BOUNDARY if the range crosses
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * a dynamic ram chunk boundary
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @param pVM The VM handle.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @param GCPhys The GC physical address to convert.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @param cbRange Physical range
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pR3Ptr Where to store the R3 pointer on success.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync */
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsyncVMMDECL(int) PGMPhysGCPhys2R3Ptr(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange, PRTR3PTR pR3Ptr)
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync{
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#ifdef VBOX_WITH_NEW_PHYS_CODE
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync LogAlways(("PGMPhysGCPhys2R3Ptr(,%RGp,%#x,): dont use this API!\n", GCPhys, cbRange)); /** @todo eliminate this API! */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync# if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync AssertFailedReturn(VERR_NOT_IMPLEMENTED);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync# else
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync PPGMRAMRANGE pRam;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync PPGMPAGE pPage;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync int rc = pgmPhysGetPageAndRangeEx(&pVM->pgm.s, GCPhys, &pPage, &pRam);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync if (RT_FAILURE(rc))
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync return rc;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync void *pv;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pv);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync if (RT_FAILURE(rc))
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync return rc;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *pR3Ptr = (RTR3PTR)pv;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync return VINF_SUCCESS;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync# endif
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#else /* !VBOX_WITH_NEW_PHYS_CODE */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync if ((GCPhys & PGM_DYNAMIC_CHUNK_BASE_MASK) != ((GCPhys+cbRange-1) & PGM_DYNAMIC_CHUNK_BASE_MASK))
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync {
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync AssertMsgFailed(("%RGp - %RGp crosses a chunk boundary!!\n", GCPhys, GCPhys+cbRange));
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync LogRel(("PGMPhysGCPhys2HCPtr %RGp - %RGp crosses a chunk boundary!!\n", GCPhys, GCPhys+cbRange));
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync return VERR_PGM_GCPHYS_RANGE_CROSSES_BOUNDARY;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync }
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync PPGMRAMRANGE pRam;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync PPGMPAGE pPage;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync int rc = pgmPhysGetPageAndRangeEx(&pVM->pgm.s, GCPhys, &pPage, &pRam);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync if (RT_FAILURE(rc))
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync return rc;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#ifndef PGM_IGNORE_RAM_FLAGS_RESERVED
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync if (RT_UNLIKELY(PGM_PAGE_IS_RESERVED(pPage)))
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync return VERR_PGM_PHYS_PAGE_RESERVED;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#endif
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync RTGCPHYS off = GCPhys - pRam->GCPhys;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync if (RT_UNLIKELY(off + cbRange > pRam->cb))
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync {
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync AssertMsgFailed(("%RGp - %RGp crosses a chunk boundary!!\n", GCPhys, GCPhys + cbRange));
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync return VERR_PGM_GCPHYS_RANGE_CROSSES_BOUNDARY;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync }
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync if (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync {
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync unsigned iChunk = (off >> PGM_DYNAMIC_CHUNK_SHIFT);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0) /* ASSUMES this is a rare occurence */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync PRTR3UINTPTR paChunkR3Ptrs = (PRTR3UINTPTR)MMHyperR3ToCC(pVM, pRam->paChunkR3Ptrs);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *pR3Ptr = (RTR3PTR)(paChunkR3Ptrs[iChunk] + (off & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#else
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *pR3Ptr = (RTR3PTR)(pRam->paChunkR3Ptrs[iChunk] + (off & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#endif
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync }
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync else if (RT_LIKELY(pRam->pvR3))
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *pR3Ptr = (RTR3PTR)((RTR3UINTPTR)pRam->pvR3 + off);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync else
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync return VERR_PGM_PHYS_PAGE_RESERVED;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync return VINF_SUCCESS;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#endif /* !VBOX_WITH_NEW_PHYS_CODE */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync}
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync/**
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * PGMPhysGCPhys2R3Ptr convenience for use with assertions.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @returns The R3Ptr, NIL_RTR3PTR on failure.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pVM The VM handle.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param GCPhys The GC Physical addresss.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param cbRange Physical range.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsyncVMMDECL(RTR3PTR) PGMPhysGCPhys2R3PtrAssert(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync{
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync RTR3PTR R3Ptr;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync int rc = PGMPhysGCPhys2R3Ptr(pVM, GCPhys, cbRange, &R3Ptr);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync if (RT_SUCCESS(rc))
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync return R3Ptr;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync return NIL_RTR3PTR;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync}
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync/**
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * Converts a guest pointer to a GC physical address.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * This uses the current CR3/CR0/CR4 of the guest.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @returns VBox status code.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pVM The VM Handle
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param GCPtr The guest pointer to convert.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pGCPhys Where to store the GC physical address.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsyncVMMDECL(int) PGMPhysGCPtr2GCPhys(PVM pVM, RTGCPTR GCPtr, PRTGCPHYS pGCPhys)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync{
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync int rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtr, NULL, pGCPhys);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync if (pGCPhys && RT_SUCCESS(rc))
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *pGCPhys |= (RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync return rc;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync}
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync/**
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * Converts a guest pointer to a HC physical address.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * This uses the current CR3/CR0/CR4 of the guest.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @returns VBox status code.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pVM The VM Handle
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param GCPtr The guest pointer to convert.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pHCPhys Where to store the HC physical address.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsyncVMMDECL(int) PGMPhysGCPtr2HCPhys(PVM pVM, RTGCPTR GCPtr, PRTHCPHYS pHCPhys)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync{
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync RTGCPHYS GCPhys;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync int rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtr, NULL, &GCPhys);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync if (RT_SUCCESS(rc))
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), pHCPhys);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync return rc;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync}
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync/**
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * Converts a guest pointer to a R3 pointer.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *
53ef3f0380481e398f8edd344fc07b4e5763ba65vboxsync * This uses the current CR3/CR0/CR4 of the guest.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync *
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @returns VBox status code.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pVM The VM Handle
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param GCPtr The guest pointer to convert.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @param pR3Ptr Where to store the R3 virtual address.
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsyncVMMDECL(int) PGMPhysGCPtr2R3Ptr(PVM pVM, RTGCPTR GCPtr, PRTR3PTR pR3Ptr)
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync{
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#ifdef VBOX_WITH_NEW_PHYS_CODE
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync#endif
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync RTGCPHYS GCPhys;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync int rc = PGM_GST_PFN(GetPage,pVM)(pVM, (RTGCUINTPTR)GCPtr, NULL, &GCPhys);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync if (RT_SUCCESS(rc))
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync rc = PGMPhysGCPhys2R3Ptr(pVM, GCPhys | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK), 1 /* we always stay within one page */, pR3Ptr);
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync return rc;
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync}
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync/**
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * Converts a guest virtual address to a HC ring-3 pointer by specfied CR3 and
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * flags.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
a917e98de4393d1090e536fdbeb04c285f98c92evboxsync * @returns VBox status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pVM The VM Handle
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param GCPtr The guest pointer to convert.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param cr3 The guest CR3.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @param fFlags Flags used for interpreting the PD correctly: X86_CR4_PSE and X86_CR4_PAE
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @param pR3Ptr Where to store the R3 pointer.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync *
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * @remark This function is used by the REM at a time where PGM could
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * potentially not be in sync. It could also be used by a
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * future DBGF API to cpu state independent conversions.
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync */
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsyncVMMDECL(int) PGMPhysGCPtr2R3PtrByGstCR3(PVM pVM, RTGCPTR GCPtr, uint64_t cr3, unsigned fFlags, PRTR3PTR pR3Ptr)
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync{
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync#ifdef VBOX_WITH_NEW_PHYS_CODE
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync VM_ASSERT_EMT(pVM); /* no longer safe for use outside the EMT thread! */
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync#endif
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync /*
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync * PAE or 32-bit?
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync */
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync Assert(!CPUMIsGuestInLongMode(pVM));
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync int rc;
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync if (!(fFlags & X86_CR4_PAE))
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync {
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync PX86PD pPD;
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync rc = PGM_GCPHYS_2_PTR(pVM, cr3 & X86_CR3_PAGE_MASK, &pPD);
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync if (RT_SUCCESS(rc))
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync {
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync X86PDE Pde = pPD->a[(RTGCUINTPTR)GCPtr >> X86_PD_SHIFT];
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync if (Pde.n.u1Present)
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync {
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync if ((fFlags & X86_CR4_PSE) && Pde.b.u1Size)
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync { /* (big page) */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = PGMPhysGCPhys2R3Ptr(pVM, pgmGstGet4MBPhysPage(&pVM->pgm.s, Pde) | ((RTGCUINTPTR)GCPtr & X86_PAGE_4M_OFFSET_MASK),
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync 1 /* we always stay within one page */, pR3Ptr);
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync }
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync else
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync { /* (normal page) */
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync PX86PT pPT;
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync rc = PGM_GCPHYS_2_PTR(pVM, Pde.u & X86_PDE_PG_MASK, &pPT);
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync if (RT_SUCCESS(rc))
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync {
956230513f1e35e9d694c219c7009034b71bb836vboxsync X86PTE Pte = pPT->a[((RTGCUINTPTR)GCPtr >> X86_PT_SHIFT) & X86_PT_MASK];
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync if (Pte.n.u1Present)
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync return PGMPhysGCPhys2R3Ptr(pVM, (Pte.u & X86_PTE_PG_MASK) | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK),
110a95f133a07d2c98a580d226abf8850bc2bd47vboxsync 1 /* we always stay within one page */, pR3Ptr);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = VERR_PAGE_NOT_PRESENT;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
}
}
else
rc = VERR_PAGE_TABLE_NOT_PRESENT;
}
}
else
{
/** @todo long mode! */
Assert(PGMGetGuestMode(pVM) < PGMMODE_AMD64);
PX86PDPT pPdpt;
rc = PGM_GCPHYS_2_PTR(pVM, cr3 & X86_CR3_PAE_PAGE_MASK, &pPdpt);
if (RT_SUCCESS(rc))
{
X86PDPE Pdpe = pPdpt->a[((RTGCUINTPTR)GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE];
if (Pdpe.n.u1Present)
{
PX86PDPAE pPD;
rc = PGM_GCPHYS_2_PTR(pVM, Pdpe.u & X86_PDPE_PG_MASK, &pPD);
if (RT_SUCCESS(rc))
{
X86PDEPAE Pde = pPD->a[((RTGCUINTPTR)GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK];
if (Pde.n.u1Present)
{
if ((fFlags & X86_CR4_PSE) && Pde.b.u1Size)
{ /* (big page) */
rc = PGMPhysGCPhys2R3Ptr(pVM, (Pde.u & X86_PDE2M_PAE_PG_MASK) | ((RTGCUINTPTR)GCPtr & X86_PAGE_2M_OFFSET_MASK),
1 /* we always stay within one page */, pR3Ptr);
}
else
{ /* (normal page) */
PX86PTPAE pPT;
rc = PGM_GCPHYS_2_PTR(pVM, (Pde.u & X86_PDE_PAE_PG_MASK), &pPT);
if (RT_SUCCESS(rc))
{
X86PTEPAE Pte = pPT->a[((RTGCUINTPTR)GCPtr >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK];
if (Pte.n.u1Present)
return PGMPhysGCPhys2R3Ptr(pVM, (Pte.u & X86_PTE_PAE_PG_MASK) | ((RTGCUINTPTR)GCPtr & PAGE_OFFSET_MASK),
1 /* we always stay within one page */, pR3Ptr);
rc = VERR_PAGE_NOT_PRESENT;
}
}
}
else
rc = VERR_PAGE_TABLE_NOT_PRESENT;
}
}
else
rc = VERR_PAGE_TABLE_NOT_PRESENT;
}
}
return rc;
}
#undef LOG_GROUP
#define LOG_GROUP LOG_GROUP_PGM_PHYS_ACCESS
#ifdef IN_RING3
/**
* Cache PGMPhys memory access
*
* @param pVM VM Handle.
* @param pCache Cache structure pointer
* @param GCPhys GC physical address
* @param pbHC HC pointer corresponding to physical page
*
* @thread EMT.
*/
static void pgmPhysCacheAdd(PVM pVM, PGMPHYSCACHE *pCache, RTGCPHYS GCPhys, uint8_t *pbR3)
{
uint32_t iCacheIndex;
Assert(VM_IS_EMT(pVM));
GCPhys = PHYS_PAGE_ADDRESS(GCPhys);
pbR3 = (uint8_t *)PAGE_ADDRESS(pbR3);
iCacheIndex = ((GCPhys >> PAGE_SHIFT) & PGM_MAX_PHYSCACHE_ENTRIES_MASK);
ASMBitSet(&pCache->aEntries, iCacheIndex);
pCache->Entry[iCacheIndex].GCPhys = GCPhys;
pCache->Entry[iCacheIndex].pbR3 = pbR3;
}
#endif /* IN_RING3 */
#ifdef VBOX_WITH_NEW_PHYS_CODE
/**
* Deals with reading from a page with one or more ALL access handlers.
*
* @param pVM The VM handle.
* @param pPage The page descriptor.
* @param GCPhys The physical address to start reading at.
* @param pvBuf Where to put the bits we read.
* @param cbRead How much to read - less or equal to a page.
*/
static void pgmPhysReadHandler(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
{
AssertFailed();
}
/**
* Read physical memory.
*
* This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
* want to ignore those.
*
* @param pVM VM Handle.
* @param GCPhys Physical address start reading from.
* @param pvBuf Where to put the read bits.
* @param cbRead How many bytes to read.
*/
VMMDECL(void) PGMPhysRead(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
{
AssertMsgReturnVoid(cbRead > 0, ("don't even think about reading zero bytes!\n"));
LogFlow(("PGMPhysRead: %RGp %d\n", GCPhys, cbRead));
pgmLock(pVM);
/*
* Copy loop on ram ranges.
*/
PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
for (;;)
{
/* Find range. */
while (pRam && GCPhys > pRam->GCPhysLast)
pRam = pRam->CTX_SUFF(pNext);
/* Inside range or not? */
if (pRam && GCPhys >= pRam->GCPhys)
{
/*
* Must work our way thru this page by page.
*/
RTGCPHYS off = GCPhys - pRam->GCPhys;
while (off < pRam->cb)
{
unsigned iPage = off >> PAGE_SHIFT;
PPGMPAGE pPage = &pRam->aPages[iPage];
size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
if (cb > cbRead)
cb = cbRead;
/*
* Any ALL access handlers?
*/
if (RT_UNLIKELY(PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage)))
pgmPhysReadHandler(pVM, pPage, pRam->GCPhys + off, pvBuf, cb);
else
{
/*
* Get the pointer to the page.
*/
const void *pvSrc;
int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pPage, pRam->GCPhys + off, &pvSrc);
if (RT_SUCCESS(rc))
memcpy(pvBuf, pvSrc, cb);
else
AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternalReadOnly failed on %RGp / %R[pgmpage] -> %Rrc\n",
pRam->GCPhys + off, pPage, rc));
}
/* next page */
if (cb >= cbRead)
{
pgmUnlock(pVM);
return;
}
cbRead -= cb;
off += cb;
pvBuf = (char *)pvBuf + cb;
} /* walk pages in ram range. */
GCPhys = pRam->GCPhysLast + 1;
}
else
{
LogFlow(("PGMPhysRead: Unassigned %RGp size=%u\n", GCPhys, cbRead));
/*
* Unassigned address space.
*/
if (!pRam)
break;
size_t cb = pRam->GCPhys - GCPhys;
if (cb >= cbRead)
{
#if 0 /** @todo enable this later. */
memset(pvBuf, 0xff, cbRead);
#else
memset(pvBuf, 0, cbRead);
#endif
break;
}
#if 0 /** @todo enable this later. */
memset(pvBuf, 0xff, cb);
#else
memset(pvBuf, 0, cb);
#endif
cbRead -= cb;
pvBuf = (char *)pvBuf + cb;
GCPhys += cb;
}
} /* Ram range walk */
pgmUnlock(pVM);
}
#else /* Old PGMPhysRead */
/**
* Read physical memory.
*
* This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
* want to ignore those.
*
* @param pVM VM Handle.
* @param GCPhys Physical address start reading from.
* @param pvBuf Where to put the read bits.
* @param cbRead How many bytes to read.
*/
VMMDECL(void) PGMPhysRead(PVM pVM, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
{
#ifdef IN_RING3
bool fGrabbedLock = false;
#endif
AssertMsg(cbRead > 0, ("don't even think about reading zero bytes!\n"));
if (cbRead == 0)
return;
LogFlow(("PGMPhysRead: %RGp %d\n", GCPhys, cbRead));
#ifdef IN_RING3
if (!VM_IS_EMT(pVM))
{
pgmLock(pVM);
fGrabbedLock = true;
}
#endif
/*
* Copy loop on ram ranges.
*/
PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
for (;;)
{
/* Find range. */
while (pRam && GCPhys > pRam->GCPhysLast)
pRam = pRam->CTX_SUFF(pNext);
/* Inside range or not? */
if (pRam && GCPhys >= pRam->GCPhys)
{
/*
* Must work our way thru this page by page.
*/
RTGCPHYS off = GCPhys - pRam->GCPhys;
while (off < pRam->cb)
{
unsigned iPage = off >> PAGE_SHIFT;
PPGMPAGE pPage = &pRam->aPages[iPage];
size_t cb;
/* Physical chunk in dynamically allocated range not present? */
if (RT_UNLIKELY(!PGM_PAGE_GET_HCPHYS(pPage)))
{
/* Treat it as reserved; return zeros */
cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
if (cb >= cbRead)
{
memset(pvBuf, 0, cbRead);
goto l_End;
}
memset(pvBuf, 0, cb);
}
/* temp hacks, will be reorganized. */
/*
* Physical handler.
*/
else if ( RT_UNLIKELY(PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) >= PGM_PAGE_HNDL_PHYS_STATE_ALL)
&& !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
{
int rc = VINF_PGM_HANDLER_DO_DEFAULT;
cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
#ifdef IN_RING3 /** @todo deal with this in GC and R0! */
/* find and call the handler */
PPGMPHYSHANDLER pNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesR3->PhysHandlers, GCPhys);
if (pNode && pNode->pfnHandlerR3)
{
size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
if (cbRange < cb)
cb = cbRange;
if (cb > cbRead)
cb = cbRead;
void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
/* Note! Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
rc = pNode->pfnHandlerR3(pVM, GCPhys, pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, pNode->pvUserR3);
}
#endif /* IN_RING3 */
if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
{
#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
void *pvSrc = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
#else
void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
#endif
if (cb >= cbRead)
{
memcpy(pvBuf, pvSrc, cbRead);
goto l_End;
}
memcpy(pvBuf, pvSrc, cb);
}
else if (cb >= cbRead)
goto l_End;
}
/*
* Virtual handlers.
*/
else if ( RT_UNLIKELY(PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) >= PGM_PAGE_HNDL_VIRT_STATE_ALL)
&& !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
{
int rc = VINF_PGM_HANDLER_DO_DEFAULT;
cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
#ifdef IN_RING3 /** @todo deal with this in GC and R0! */
/* Search the whole tree for matching physical addresses (rather expensive!) */
PPGMVIRTHANDLER pNode;
unsigned iPage;
int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pNode, &iPage);
if (RT_SUCCESS(rc2) && pNode->pfnHandlerR3)
{
size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
if (cbRange < cb)
cb = cbRange;
if (cb > cbRead)
cb = cbRead;
RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pNode->Core.Key & PAGE_BASE_GC_MASK)
+ (iPage << PAGE_SHIFT) + (off & PAGE_OFFSET_MASK);
void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
/* Note! Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
rc = pNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvSrc, pvBuf, cb, PGMACCESSTYPE_READ, 0);
}
#endif /* IN_RING3 */
if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
{
#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
void *pvSrc = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
#else
void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
#endif
if (cb >= cbRead)
{
memcpy(pvBuf, pvSrc, cbRead);
goto l_End;
}
memcpy(pvBuf, pvSrc, cb);
}
else if (cb >= cbRead)
goto l_End;
}
else
{
switch (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM)) /** @todo PAGE FLAGS */
{
/*
* Normal memory or ROM.
*/
case 0:
case MM_RAM_FLAGS_ROM:
case MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_RESERVED:
//case MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2: /* = shadow */ - //MMIO2 isn't in the mask.
case MM_RAM_FLAGS_MMIO2: // MMIO2 isn't in the mask.
{
#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
void *pvSrc = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
#else
void *pvSrc = PGMRAMRANGE_GETHCPTR(pRam, off)
#endif
cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
if (cb >= cbRead)
{
#if defined(IN_RING3) && defined(PGM_PHYSMEMACCESS_CACHING)
if (cbRead <= 4 && !fGrabbedLock /* i.e. EMT */)
pgmPhysCacheAdd(pVM, &pVM->pgm.s.pgmphysreadcache, GCPhys, (uint8_t*)pvSrc);
#endif /* IN_RING3 && PGM_PHYSMEMACCESS_CACHING */
memcpy(pvBuf, pvSrc, cbRead);
goto l_End;
}
memcpy(pvBuf, pvSrc, cb);
break;
}
/*
* All reserved, nothing there.
*/
case MM_RAM_FLAGS_RESERVED:
cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
if (cb >= cbRead)
{
memset(pvBuf, 0, cbRead);
goto l_End;
}
memset(pvBuf, 0, cb);
break;
/*
* The rest needs to be taken more carefully.
*/
default:
#if 1 /** @todo r=bird: Can you do this properly please. */
/** @todo Try MMIO; quick hack */
if (cbRead <= 8 && IOMMMIORead(pVM, GCPhys, (uint32_t *)pvBuf, cbRead) == VINF_SUCCESS)
goto l_End;
#endif
/** @todo fix me later. */
AssertReleaseMsgFailed(("Unknown read at %RGp size %u implement the complex physical reading case %RHp\n",
GCPhys, cbRead,
pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_ROM))); /** @todo PAGE FLAGS */
cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
break;
}
}
cbRead -= cb;
off += cb;
pvBuf = (char *)pvBuf + cb;
}
GCPhys = pRam->GCPhysLast + 1;
}
else
{
LogFlow(("PGMPhysRead: Unassigned %RGp size=%u\n", GCPhys, cbRead));
/*
* Unassigned address space.
*/
size_t cb;
if ( !pRam
|| (cb = pRam->GCPhys - GCPhys) >= cbRead)
{
memset(pvBuf, 0, cbRead);
goto l_End;
}
memset(pvBuf, 0, cb); /** @todo this is wrong, unassigne == 0xff not 0x00! */
cbRead -= cb;
pvBuf = (char *)pvBuf + cb;
GCPhys += cb;
}
}
l_End:
#ifdef IN_RING3
if (fGrabbedLock)
pgmUnlock(pVM);
#endif
return;
}
#endif /* Old PGMPhysRead */
#ifdef VBOX_WITH_NEW_PHYS_CODE
/**
* Deals with writing to a page with one or more WRITE or ALL access handlers.
*
* @param pVM The VM handle.
* @param pPage The page descriptor.
* @param GCPhys The physical address to start writing at.
* @param pvBuf What to write.
* @param cbWrite How much to write - less or equal to a page.
*/
static void pgmPhysWriteHandler(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void const *pvBuf, size_t cbWrite)
{
AssertFailed();
}
/**
* Write to physical memory.
*
* This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
* want to ignore those.
*
* @param pVM VM Handle.
* @param GCPhys Physical address to write to.
* @param pvBuf What to write.
* @param cbWrite How many bytes to write.
*/
VMMDECL(void) PGMPhysWrite(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite)
{
AssertMsg(!pVM->pgm.s.fNoMorePhysWrites, ("Calling PGMPhysWrite after pgmR3Save()!\n"));
AssertMsgReturnVoid(cbWrite > 0, ("don't even think about writing zero bytes!\n"));
LogFlow(("PGMPhysWrite: %RGp %d\n", GCPhys, cbWrite));
pgmLock(pVM);
/*
* Copy loop on ram ranges.
*/
PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
for (;;)
{
/* Find range. */
while (pRam && GCPhys > pRam->GCPhysLast)
pRam = pRam->CTX_SUFF(pNext);
/* Inside range or not? */
if (pRam && GCPhys >= pRam->GCPhys)
{
/*
* Must work our way thru this page by page.
*/
RTGCPTR off = GCPhys - pRam->GCPhys;
while (off < pRam->cb)
{
RTGCPTR iPage = off >> PAGE_SHIFT;
PPGMPAGE pPage = &pRam->aPages[iPage];
size_t cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
if (cb > cbWrite)
cb = cbWrite;
/*
* Any active WRITE or ALL access handlers?
*/
if (PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage))
pgmPhysWriteHandler(pVM, pPage, pRam->GCPhys + off, pvBuf, cb);
else
{
/*
* Get the pointer to the page.
*/
void *pvDst;
int rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, pRam->GCPhys + off, &pvDst);
if (RT_SUCCESS(rc))
memcpy(pvDst, pvBuf, cb);
else
AssertLogRelMsgFailed(("pgmPhysGCPhys2CCPtrInternal failed on %RGp / %R[pgmpage] -> %Rrc\n",
pRam->GCPhys + off, pPage, rc));
}
/* next page */
if (cb >= cbWrite)
{
pgmUnlock(pVM);
return;
}
cbWrite -= cb;
off += cb;
pvBuf = (const char *)pvBuf + cb;
} /* walk pages in ram range */
GCPhys = pRam->GCPhysLast + 1;
}
else
{
/*
* Unassigned address space, skip it.
*/
if (!pRam)
break;
size_t cb = pRam->GCPhys - GCPhys;
if (cb >= cbWrite)
break;
cbWrite -= cb;
pvBuf = (const char *)pvBuf + cb;
GCPhys += cb;
}
} /* Ram range walk */
pgmUnlock(pVM);
}
#else /* Old PGMPhysWrite */
/**
* Write to physical memory.
*
* This API respects access handlers and MMIO. Use PGMPhysSimpleReadGCPhys() if you
* want to ignore those.
*
* @param pVM VM Handle.
* @param GCPhys Physical address to write to.
* @param pvBuf What to write.
* @param cbWrite How many bytes to write.
*/
VMMDECL(void) PGMPhysWrite(PVM pVM, RTGCPHYS GCPhys, const void *pvBuf, size_t cbWrite)
{
#ifdef IN_RING3
bool fGrabbedLock = false;
#endif
AssertMsg(!pVM->pgm.s.fNoMorePhysWrites, ("Calling PGMPhysWrite after pgmR3Save()!\n"));
AssertMsg(cbWrite > 0, ("don't even think about writing zero bytes!\n"));
if (cbWrite == 0)
return;
LogFlow(("PGMPhysWrite: %RGp %d\n", GCPhys, cbWrite));
#ifdef IN_RING3
if (!VM_IS_EMT(pVM))
{
pgmLock(pVM);
fGrabbedLock = true;
}
#endif
/*
* Copy loop on ram ranges.
*/
PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
for (;;)
{
/* Find range. */
while (pRam && GCPhys > pRam->GCPhysLast)
pRam = pRam->CTX_SUFF(pNext);
/* Inside range or not? */
if (pRam && GCPhys >= pRam->GCPhys)
{
/*
* Must work our way thru this page by page.
*/
RTGCPTR off = GCPhys - pRam->GCPhys;
while (off < pRam->cb)
{
RTGCPTR iPage = off >> PAGE_SHIFT;
PPGMPAGE pPage = &pRam->aPages[iPage];
/* Physical chunk in dynamically allocated range not present? */
if (RT_UNLIKELY(!PGM_PAGE_GET_HCPHYS(pPage)))
{
int rc;
#ifdef IN_RING3
if (fGrabbedLock)
{
pgmUnlock(pVM);
rc = pgmr3PhysGrowRange(pVM, GCPhys);
if (rc == VINF_SUCCESS)
PGMPhysWrite(pVM, GCPhys, pvBuf, cbWrite); /* try again; can't assume pRam is still valid (paranoia) */
return;
}
rc = pgmr3PhysGrowRange(pVM, GCPhys);
#else
rc = CTXALLMID(VMM, CallHost)(pVM, VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
#endif
if (rc != VINF_SUCCESS)
goto l_End;
}
size_t cb;
/* temporary hack, will reogranize is later. */
/*
* Virtual handlers
*/
if ( PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage)
&& !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
{
if (PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage))
{
/*
* Physical write handler + virtual write handler.
* Consider this a quick workaround for the CSAM + shadow caching problem.
*
* We hand it to the shadow caching first since it requires the unchanged
* data. CSAM will have to put up with it already being changed.
*/
int rc = VINF_PGM_HANDLER_DO_DEFAULT;
cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
#ifdef IN_RING3 /** @todo deal with this in GC and R0! */
/* 1. The physical handler */
PPGMPHYSHANDLER pPhysNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesR3->PhysHandlers, GCPhys);
if (pPhysNode && pPhysNode->pfnHandlerR3)
{
size_t cbRange = pPhysNode->Core.KeyLast - GCPhys + 1;
if (cbRange < cb)
cb = cbRange;
if (cb > cbWrite)
cb = cbWrite;
void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
/* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
rc = pPhysNode->pfnHandlerR3(pVM, GCPhys, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, pPhysNode->pvUserR3);
}
/* 2. The virtual handler (will see incorrect data) */
PPGMVIRTHANDLER pVirtNode;
unsigned iPage;
int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pVirtNode, &iPage);
if (RT_SUCCESS(rc2) && pVirtNode->pfnHandlerR3)
{
size_t cbRange = pVirtNode->Core.KeyLast - GCPhys + 1;
if (cbRange < cb)
cb = cbRange;
if (cb > cbWrite)
cb = cbWrite;
RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pVirtNode->Core.Key & PAGE_BASE_GC_MASK)
+ (iPage << PAGE_SHIFT) + (off & PAGE_OFFSET_MASK);
void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
/* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
rc2 = pVirtNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, 0);
if ( ( rc2 != VINF_PGM_HANDLER_DO_DEFAULT
&& rc == VINF_PGM_HANDLER_DO_DEFAULT)
|| ( RT_FAILURE(rc2)
&& RT_SUCCESS(rc)))
rc = rc2;
}
#endif /* IN_RING3 */
if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
{
#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
#else
void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
#endif
if (cb >= cbWrite)
{
memcpy(pvDst, pvBuf, cbWrite);
goto l_End;
}
memcpy(pvDst, pvBuf, cb);
}
else if (cb >= cbWrite)
goto l_End;
}
else
{
int rc = VINF_PGM_HANDLER_DO_DEFAULT;
cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
#ifdef IN_RING3
/** @todo deal with this in GC and R0! */
/* Search the whole tree for matching physical addresses (rather expensive!) */
PPGMVIRTHANDLER pNode;
unsigned iPage;
int rc2 = pgmHandlerVirtualFindByPhysAddr(pVM, GCPhys, &pNode, &iPage);
if (RT_SUCCESS(rc2) && pNode->pfnHandlerR3)
{
size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
if (cbRange < cb)
cb = cbRange;
if (cb > cbWrite)
cb = cbWrite;
RTGCUINTPTR GCPtr = ((RTGCUINTPTR)pNode->Core.Key & PAGE_BASE_GC_MASK)
+ (iPage << PAGE_SHIFT) + (off & PAGE_OFFSET_MASK);
void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
/* Note! Dangerous assumption that R3 handlers don't do anything that really requires an EMT lock! */
rc = pNode->pfnHandlerR3(pVM, (RTGCPTR)GCPtr, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, 0);
}
#endif /* IN_RING3 */
if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
{
#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
#else
void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
#endif
if (cb >= cbWrite)
{
memcpy(pvDst, pvBuf, cbWrite);
goto l_End;
}
memcpy(pvDst, pvBuf, cb);
}
else if (cb >= cbWrite)
goto l_End;
}
}
/*
* Physical handler.
*/
else if ( RT_UNLIKELY(PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) >= PGM_PAGE_HNDL_PHYS_STATE_WRITE)
&& !(pPage->HCPhys & MM_RAM_FLAGS_MMIO)) /// @todo PAGE FLAGS
{
int rc = VINF_PGM_HANDLER_DO_DEFAULT;
cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
#ifdef IN_RING3 /** @todo deal with this in GC and R0! */
/* find and call the handler */
PPGMPHYSHANDLER pNode = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.pTreesR3->PhysHandlers, GCPhys);
if (pNode && pNode->pfnHandlerR3)
{
size_t cbRange = pNode->Core.KeyLast - GCPhys + 1;
if (cbRange < cb)
cb = cbRange;
if (cb > cbWrite)
cb = cbWrite;
void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
/** @todo Dangerous assumption that HC handlers don't do anything that really requires an EMT lock! */
rc = pNode->pfnHandlerR3(pVM, GCPhys, pvDst, (void *)pvBuf, cb, PGMACCESSTYPE_WRITE, pNode->pvUserR3);
}
#endif /* IN_RING3 */
if (rc == VINF_PGM_HANDLER_DO_DEFAULT)
{
#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
#else
void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
#endif
if (cb >= cbWrite)
{
memcpy(pvDst, pvBuf, cbWrite);
goto l_End;
}
memcpy(pvDst, pvBuf, cb);
}
else if (cb >= cbWrite)
goto l_End;
}
else
{
/** @todo r=bird: missing MM_RAM_FLAGS_ROM here, we shall not allow anyone to overwrite the ROM! */
switch (pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)) /** @todo PAGE FLAGS */
{
/*
* Normal memory, MMIO2 or writable shadow ROM.
*/
case 0:
case MM_RAM_FLAGS_MMIO2:
case MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2: /* shadow rom */
{
#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
void *pvDst = pgmDynMapHCPageOff(&pVM->pgm.s, PGM_PAGE_GET_HCPHYS(pPage) + (off & PAGE_OFFSET_MASK));
#else
void *pvDst = PGMRAMRANGE_GETHCPTR(pRam, off)
#endif
cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
if (cb >= cbWrite)
{
#if defined(IN_RING3) && defined(PGM_PHYSMEMACCESS_CACHING)
if (cbWrite <= 4 && !fGrabbedLock /* i.e. EMT */)
pgmPhysCacheAdd(pVM, &pVM->pgm.s.pgmphyswritecache, GCPhys, (uint8_t*)pvDst);
#endif /* IN_RING3 && PGM_PHYSMEMACCESS_CACHING */
memcpy(pvDst, pvBuf, cbWrite);
goto l_End;
}
memcpy(pvDst, pvBuf, cb);
break;
}
/*
* All reserved, nothing there.
*/
case MM_RAM_FLAGS_RESERVED:
case MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO2:
cb = PAGE_SIZE - (off & PAGE_OFFSET_MASK);
if (cb >= cbWrite)
goto l_End;
break;
/*
* The rest needs to be taken more carefully.
*/
default:
#if 1 /** @todo r=bird: Can you do this properly please. */
/** @todo Try MMIO; quick hack */
if (cbWrite <= 8 && IOMMMIOWrite(pVM, GCPhys, *(uint32_t *)pvBuf, cbWrite) == VINF_SUCCESS)
goto l_End;
#endif
/** @todo fix me later. */
AssertReleaseMsgFailed(("Unknown write at %RGp size %u implement the complex physical writing case %RHp\n",
GCPhys, cbWrite,
(pPage->HCPhys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2)))); /** @todo PAGE FLAGS */
/* skip the write */
cb = cbWrite;
break;
}
}
cbWrite -= cb;
off += cb;
pvBuf = (const char *)pvBuf + cb;
}
GCPhys = pRam->GCPhysLast + 1;
}
else
{
/*
* Unassigned address space.
*/
size_t cb;
if ( !pRam
|| (cb = pRam->GCPhys - GCPhys) >= cbWrite)
goto l_End;
cbWrite -= cb;
pvBuf = (const char *)pvBuf + cb;
GCPhys += cb;
}
}
l_End:
#ifdef IN_RING3
if (fGrabbedLock)
pgmUnlock(pVM);
#endif
return;
}
#endif /* Old PGMPhysWrite */
/**
* Read from guest physical memory by GC physical address, bypassing
* MMIO and access handlers.
*
* @returns VBox status.
* @param pVM VM handle.
* @param pvDst The destination address.
* @param GCPhysSrc The source address (GC physical address).
* @param cb The number of bytes to read.
*/
VMMDECL(int) PGMPhysSimpleReadGCPhys(PVM pVM, void *pvDst, RTGCPHYS GCPhysSrc, size_t cb)
{
/*
* Treat the first page as a special case.
*/
if (!cb)
return VINF_SUCCESS;
/* map the 1st page */
void const *pvSrc;
PGMPAGEMAPLOCK Lock;
int rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhysSrc, &pvSrc, &Lock);
if (RT_FAILURE(rc))
return rc;
/* optimize for the case where access is completely within the first page. */
size_t cbPage = PAGE_SIZE - (GCPhysSrc & PAGE_OFFSET_MASK);
if (RT_LIKELY(cb <= cbPage))
{
memcpy(pvDst, pvSrc, cb);
PGMPhysReleasePageMappingLock(pVM, &Lock);
return VINF_SUCCESS;
}
/* copy to the end of the page. */
memcpy(pvDst, pvSrc, cbPage);
PGMPhysReleasePageMappingLock(pVM, &Lock);
GCPhysSrc += cbPage;
pvDst = (uint8_t *)pvDst + cbPage;
cb -= cbPage;
/*
* Page by page.
*/
for (;;)
{
/* map the page */
rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhysSrc, &pvSrc, &Lock);
if (RT_FAILURE(rc))
return rc;
/* last page? */
if (cb <= PAGE_SIZE)
{
memcpy(pvDst, pvSrc, cb);
PGMPhysReleasePageMappingLock(pVM, &Lock);
return VINF_SUCCESS;
}
/* copy the entire page and advance */
memcpy(pvDst, pvSrc, PAGE_SIZE);
PGMPhysReleasePageMappingLock(pVM, &Lock);
GCPhysSrc += PAGE_SIZE;
pvDst = (uint8_t *)pvDst + PAGE_SIZE;
cb -= PAGE_SIZE;
}
/* won't ever get here. */
}
#ifndef IN_RC /* Ring 0 & 3 only. (Just not needed in GC.) */
/**
* Write to guest physical memory referenced by GC pointer.
* Write memory to GC physical address in guest physical memory.
*
* This will bypass MMIO and access handlers.
*
* @returns VBox status.
* @param pVM VM handle.
* @param GCPhysDst The GC physical address of the destination.
* @param pvSrc The source buffer.
* @param cb The number of bytes to write.
*/
VMMDECL(int) PGMPhysSimpleWriteGCPhys(PVM pVM, RTGCPHYS GCPhysDst, const void *pvSrc, size_t cb)
{
LogFlow(("PGMPhysSimpleWriteGCPhys: %RGp %zu\n", GCPhysDst, cb));
/*
* Treat the first page as a special case.
*/
if (!cb)
return VINF_SUCCESS;
/* map the 1st page */
void *pvDst;
PGMPAGEMAPLOCK Lock;
int rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
if (RT_FAILURE(rc))
return rc;
/* optimize for the case where access is completely within the first page. */
size_t cbPage = PAGE_SIZE - (GCPhysDst & PAGE_OFFSET_MASK);
if (RT_LIKELY(cb <= cbPage))
{
memcpy(pvDst, pvSrc, cb);
PGMPhysReleasePageMappingLock(pVM, &Lock);
return VINF_SUCCESS;
}
/* copy to the end of the page. */
memcpy(pvDst, pvSrc, cbPage);
PGMPhysReleasePageMappingLock(pVM, &Lock);
GCPhysDst += cbPage;
pvSrc = (const uint8_t *)pvSrc + cbPage;
cb -= cbPage;
/*
* Page by page.
*/
for (;;)
{
/* map the page */
rc = PGMPhysGCPhys2CCPtr(pVM, GCPhysDst, &pvDst, &Lock);
if (RT_FAILURE(rc))
return rc;
/* last page? */
if (cb <= PAGE_SIZE)
{
memcpy(pvDst, pvSrc, cb);
PGMPhysReleasePageMappingLock(pVM, &Lock);
return VINF_SUCCESS;
}
/* copy the entire page and advance */
memcpy(pvDst, pvSrc, PAGE_SIZE);
PGMPhysReleasePageMappingLock(pVM, &Lock);
GCPhysDst += PAGE_SIZE;
pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
cb -= PAGE_SIZE;
}
/* won't ever get here. */
}
/**
* Read from guest physical memory referenced by GC pointer.
*
* This function uses the current CR3/CR0/CR4 of the guest and will
* bypass access handlers and not set any accessed bits.
*
* @returns VBox status.
* @param pVM VM handle.
* @param pvDst The destination address.
* @param GCPtrSrc The source address (GC pointer).
* @param cb The number of bytes to read.
*/
VMMDECL(int) PGMPhysSimpleReadGCPtr(PVM pVM, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
{
/*
* Treat the first page as a special case.
*/
if (!cb)
return VINF_SUCCESS;
/* map the 1st page */
void const *pvSrc;
PGMPAGEMAPLOCK Lock;
int rc = PGMPhysGCPtr2CCPtrReadOnly(pVM, GCPtrSrc, &pvSrc, &Lock);
if (RT_FAILURE(rc))
return rc;
/* optimize for the case where access is completely within the first page. */
size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
if (RT_LIKELY(cb <= cbPage))
{
memcpy(pvDst, pvSrc, cb);
PGMPhysReleasePageMappingLock(pVM, &Lock);
return VINF_SUCCESS;
}
/* copy to the end of the page. */
memcpy(pvDst, pvSrc, cbPage);
PGMPhysReleasePageMappingLock(pVM, &Lock);
GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + cbPage);
pvDst = (uint8_t *)pvDst + cbPage;
cb -= cbPage;
/*
* Page by page.
*/
for (;;)
{
/* map the page */
rc = PGMPhysGCPtr2CCPtrReadOnly(pVM, GCPtrSrc, &pvSrc, &Lock);
if (RT_FAILURE(rc))
return rc;
/* last page? */
if (cb <= PAGE_SIZE)
{
memcpy(pvDst, pvSrc, cb);
PGMPhysReleasePageMappingLock(pVM, &Lock);
return VINF_SUCCESS;
}
/* copy the entire page and advance */
memcpy(pvDst, pvSrc, PAGE_SIZE);
PGMPhysReleasePageMappingLock(pVM, &Lock);
GCPtrSrc = (RTGCPTR)((RTGCUINTPTR)GCPtrSrc + PAGE_SIZE);
pvDst = (uint8_t *)pvDst + PAGE_SIZE;
cb -= PAGE_SIZE;
}
/* won't ever get here. */
}
/**
* Write to guest physical memory referenced by GC pointer.
*
* This function uses the current CR3/CR0/CR4 of the guest and will
* bypass access handlers and not set dirty or accessed bits.
*
* @returns VBox status.
* @param pVM VM handle.
* @param GCPtrDst The destination address (GC pointer).
* @param pvSrc The source address.
* @param cb The number of bytes to write.
*/
VMMDECL(int) PGMPhysSimpleWriteGCPtr(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
{
/*
* Treat the first page as a special case.
*/
if (!cb)
return VINF_SUCCESS;
/* map the 1st page */
void *pvDst;
PGMPAGEMAPLOCK Lock;
int rc = PGMPhysGCPtr2CCPtr(pVM, GCPtrDst, &pvDst, &Lock);
if (RT_FAILURE(rc))
return rc;
/* optimize for the case where access is completely within the first page. */
size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
if (RT_LIKELY(cb <= cbPage))
{
memcpy(pvDst, pvSrc, cb);
PGMPhysReleasePageMappingLock(pVM, &Lock);
return VINF_SUCCESS;
}
/* copy to the end of the page. */
memcpy(pvDst, pvSrc, cbPage);
PGMPhysReleasePageMappingLock(pVM, &Lock);
GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
pvSrc = (const uint8_t *)pvSrc + cbPage;
cb -= cbPage;
/*
* Page by page.
*/
for (;;)
{
/* map the page */
rc = PGMPhysGCPtr2CCPtr(pVM, GCPtrDst, &pvDst, &Lock);
if (RT_FAILURE(rc))
return rc;
/* last page? */
if (cb <= PAGE_SIZE)
{
memcpy(pvDst, pvSrc, cb);
PGMPhysReleasePageMappingLock(pVM, &Lock);
return VINF_SUCCESS;
}
/* copy the entire page and advance */
memcpy(pvDst, pvSrc, PAGE_SIZE);
PGMPhysReleasePageMappingLock(pVM, &Lock);
GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + PAGE_SIZE);
pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
cb -= PAGE_SIZE;
}
/* won't ever get here. */
}
/**
* Write to guest physical memory referenced by GC pointer and update the PTE.
*
* This function uses the current CR3/CR0/CR4 of the guest and will
* bypass access handlers but will set any dirty and accessed bits in the PTE.
*
* If you don't want to set the dirty bit, use PGMPhysSimpleWriteGCPtr().
*
* @returns VBox status.
* @param pVM VM handle.
* @param GCPtrDst The destination address (GC pointer).
* @param pvSrc The source address.
* @param cb The number of bytes to write.
*/
VMMDECL(int) PGMPhysSimpleDirtyWriteGCPtr(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
{
/*
* Treat the first page as a special case.
* Btw. this is the same code as in PGMPhyssimpleWriteGCPtr excep for the PGMGstModifyPage.
*/
if (!cb)
return VINF_SUCCESS;
/* map the 1st page */
void *pvDst;
PGMPAGEMAPLOCK Lock;
int rc = PGMPhysGCPtr2CCPtr(pVM, GCPtrDst, &pvDst, &Lock);
if (RT_FAILURE(rc))
return rc;
/* optimize for the case where access is completely within the first page. */
size_t cbPage = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
if (RT_LIKELY(cb <= cbPage))
{
memcpy(pvDst, pvSrc, cb);
PGMPhysReleasePageMappingLock(pVM, &Lock);
rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
return VINF_SUCCESS;
}
/* copy to the end of the page. */
memcpy(pvDst, pvSrc, cbPage);
PGMPhysReleasePageMappingLock(pVM, &Lock);
rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + cbPage);
pvSrc = (const uint8_t *)pvSrc + cbPage;
cb -= cbPage;
/*
* Page by page.
*/
for (;;)
{
/* map the page */
rc = PGMPhysGCPtr2CCPtr(pVM, GCPtrDst, &pvDst, &Lock);
if (RT_FAILURE(rc))
return rc;
/* last page? */
if (cb <= PAGE_SIZE)
{
memcpy(pvDst, pvSrc, cb);
PGMPhysReleasePageMappingLock(pVM, &Lock);
rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
return VINF_SUCCESS;
}
/* copy the entire page and advance */
memcpy(pvDst, pvSrc, PAGE_SIZE);
PGMPhysReleasePageMappingLock(pVM, &Lock);
rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D)); AssertRC(rc);
GCPtrDst = (RTGCPTR)((RTGCUINTPTR)GCPtrDst + PAGE_SIZE);
pvSrc = (const uint8_t *)pvSrc + PAGE_SIZE;
cb -= PAGE_SIZE;
}
/* won't ever get here. */
}
/**
* Read from guest physical memory referenced by GC pointer.
*
* This function uses the current CR3/CR0/CR4 of the guest and will
* respect access handlers and set accessed bits.
*
* @returns VBox status.
* @param pVM VM handle.
* @param pvDst The destination address.
* @param GCPtrSrc The source address (GC pointer).
* @param cb The number of bytes to read.
*/
VMMDECL(int) PGMPhysReadGCPtr(PVM pVM, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
{
RTGCPHYS GCPhys;
int rc;
/*
* Anything to do?
*/
if (!cb)
return VINF_SUCCESS;
LogFlow(("PGMPhysReadGCPtr: %RGv %zu\n", GCPtrSrc, cb));
/*
* Optimize reads within a single page.
*/
if (((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
{
/* Convert virtual to physical address */
rc = PGMPhysGCPtr2GCPhys(pVM, GCPtrSrc, &GCPhys);
AssertRCReturn(rc, rc);
/* mark the guest page as accessed. */
rc = PGMGstModifyPage(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
AssertRC(rc);
PGMPhysRead(pVM, GCPhys, pvDst, cb);
return VINF_SUCCESS;
}
/*
* Page by page.
*/
for (;;)
{
/* Convert virtual to physical address */
rc = PGMPhysGCPtr2GCPhys(pVM, GCPtrSrc, &GCPhys);
AssertRCReturn(rc, rc);
/* mark the guest page as accessed. */
int rc = PGMGstModifyPage(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)(X86_PTE_A));
AssertRC(rc);
/* copy */
size_t cbRead = PAGE_SIZE - ((RTGCUINTPTR)GCPtrSrc & PAGE_OFFSET_MASK);
if (cbRead >= cb)
{
PGMPhysRead(pVM, GCPhys, pvDst, cb);
return VINF_SUCCESS;
}
PGMPhysRead(pVM, GCPhys, pvDst, cbRead);
/* next */
cb -= cbRead;
pvDst = (uint8_t *)pvDst + cbRead;
GCPtrSrc += cbRead;
}
}
/**
* Write to guest physical memory referenced by GC pointer.
*
* This function uses the current CR3/CR0/CR4 of the guest and will
* respect access handlers and set dirty and accessed bits.
*
* @returns VBox status.
* @param pVM VM handle.
* @param GCPtrDst The destination address (GC pointer).
* @param pvSrc The source address.
* @param cb The number of bytes to write.
*/
VMMDECL(int) PGMPhysWriteGCPtr(PVM pVM, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)
{
RTGCPHYS GCPhys;
int rc;
/*
* Anything to do?
*/
if (!cb)
return VINF_SUCCESS;
LogFlow(("PGMPhysWriteGCPtr: %RGv %zu\n", GCPtrDst, cb));
/*
* Optimize writes within a single page.
*/
if (((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK) + cb <= PAGE_SIZE)
{
/* Convert virtual to physical address */
rc = PGMPhysGCPtr2GCPhys(pVM, GCPtrDst, &GCPhys);
AssertMsgRCReturn(rc, ("PGMPhysGCPtr2GCPhys failed with %Rrc for %RGv\n", rc, GCPtrDst), rc);
/* mark the guest page as accessed and dirty. */
rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
AssertRC(rc);
PGMPhysWrite(pVM, GCPhys, pvSrc, cb);
return VINF_SUCCESS;
}
/*
* Page by page.
*/
for (;;)
{
/* Convert virtual to physical address */
rc = PGMPhysGCPtr2GCPhys(pVM, GCPtrDst, &GCPhys);
AssertRCReturn(rc, rc);
/* mark the guest page as accessed and dirty. */
rc = PGMGstModifyPage(pVM, GCPtrDst, 1, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
AssertRC(rc);
/* copy */
size_t cbWrite = PAGE_SIZE - ((RTGCUINTPTR)GCPtrDst & PAGE_OFFSET_MASK);
if (cbWrite >= cb)
{
PGMPhysWrite(pVM, GCPhys, pvSrc, cb);
return VINF_SUCCESS;
}
PGMPhysWrite(pVM, GCPhys, pvSrc, cbWrite);
/* next */
cb -= cbWrite;
pvSrc = (uint8_t *)pvSrc + cbWrite;
GCPtrDst += cbWrite;
}
}
#endif /* !IN_RC */
/**
* Performs a read of guest virtual memory for instruction emulation.
*
* This will check permissions, raise exceptions and update the access bits.
*
* The current implementation will bypass all access handlers. It may later be
* changed to at least respect MMIO.
*
*
* @returns VBox status code suitable to scheduling.
* @retval VINF_SUCCESS if the read was performed successfully.
* @retval VINF_EM_RAW_GUEST_TRAP if an exception was raised but not dispatched yet.
* @retval VINF_TRPM_XCPT_DISPATCHED if an exception was raised and dispatched.
*
* @param pVM The VM handle.
* @param pCtxCore The context core.
* @param pvDst Where to put the bytes we've read.
* @param GCPtrSrc The source address.
* @param cb The number of bytes to read. Not more than a page.
*
* @remark This function will dynamically map physical pages in GC. This may unmap
* mappings done by the caller. Be careful!
*/
VMMDECL(int) PGMPhysInterpretedRead(PVM pVM, PCPUMCTXCORE pCtxCore, void *pvDst, RTGCUINTPTR GCPtrSrc, size_t cb)
{
Assert(cb <= PAGE_SIZE);
/** @todo r=bird: This isn't perfect!
* -# It's not checking for reserved bits being 1.
* -# It's not correctly dealing with the access bit.
* -# It's not respecting MMIO memory or any other access handlers.
*/
/*
* 1. Translate virtual to physical. This may fault.
* 2. Map the physical address.
* 3. Do the read operation.
* 4. Set access bits if required.
*/
int rc;
unsigned cb1 = PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK);
if (cb <= cb1)
{
/*
* Not crossing pages.
*/
RTGCPHYS GCPhys;
uint64_t fFlags;
rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc, &fFlags, &GCPhys);
if (RT_SUCCESS(rc))
{
/** @todo we should check reserved bits ... */
void *pvSrc;
rc = PGM_GCPHYS_2_PTR(pVM, GCPhys, &pvSrc);
switch (rc)
{
case VINF_SUCCESS:
Log(("PGMPhysInterpretedRead: pvDst=%p pvSrc=%p cb=%d\n", pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb));
memcpy(pvDst, (uint8_t *)pvSrc + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
break;
case VERR_PGM_PHYS_PAGE_RESERVED:
case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
memset(pvDst, 0, cb); /** @todo this is wrong, it should be 0xff */
break;
default:
return rc;
}
/** @todo access bit emulation isn't 100% correct. */
if (!(fFlags & X86_PTE_A))
{
rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
AssertRC(rc);
}
return VINF_SUCCESS;
}
}
else
{
/*
* Crosses pages.
*/
size_t cb2 = cb - cb1;
uint64_t fFlags1;
RTGCPHYS GCPhys1;
uint64_t fFlags2;
RTGCPHYS GCPhys2;
rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc, &fFlags1, &GCPhys1);
if (RT_SUCCESS(rc))
rc = PGM_GST_PFN(GetPage,pVM)(pVM, GCPtrSrc + cb1, &fFlags2, &GCPhys2);
if (RT_SUCCESS(rc))
{
/** @todo we should check reserved bits ... */
AssertMsgFailed(("cb=%d cb1=%d cb2=%d GCPtrSrc=%RGv\n", cb, cb1, cb2, GCPtrSrc));
void *pvSrc1;
rc = PGM_GCPHYS_2_PTR(pVM, GCPhys1, &pvSrc1);
switch (rc)
{
case VINF_SUCCESS:
memcpy(pvDst, (uint8_t *)pvSrc1 + (GCPtrSrc & PAGE_OFFSET_MASK), cb1);
break;
case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
memset(pvDst, 0, cb1); /** @todo this is wrong, it should be 0xff */
break;
default:
return rc;
}
void *pvSrc2;
rc = PGM_GCPHYS_2_PTR(pVM, GCPhys2, &pvSrc2);
switch (rc)
{
case VINF_SUCCESS:
memcpy((uint8_t *)pvDst + cb1, pvSrc2, cb2);
break;
case VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS:
memset((uint8_t *)pvDst + cb1, 0, cb2); /** @todo this is wrong, it should be 0xff */
break;
default:
return rc;
}
if (!(fFlags1 & X86_PTE_A))
{
rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
AssertRC(rc);
}
if (!(fFlags2 & X86_PTE_A))
{
rc = PGM_GST_PFN(ModifyPage,pVM)(pVM, GCPtrSrc + cb1, 1, X86_PTE_A, ~(uint64_t)X86_PTE_A);
AssertRC(rc);
}
return VINF_SUCCESS;
}
}
/*
* Raise a #PF.
*/
uint32_t uErr;
/* Get the current privilege level. */
uint32_t cpl = CPUMGetGuestCPL(pVM, pCtxCore);
switch (rc)
{
case VINF_SUCCESS:
uErr = (cpl >= 2) ? X86_TRAP_PF_RSVD | X86_TRAP_PF_US : X86_TRAP_PF_RSVD;
break;
case VERR_PAGE_NOT_PRESENT:
case VERR_PAGE_TABLE_NOT_PRESENT:
uErr = (cpl >= 2) ? X86_TRAP_PF_US : 0;
break;
default:
AssertMsgFailed(("rc=%Rrc GCPtrSrc=%RGv cb=%#x\n", rc, GCPtrSrc, cb));
return rc;
}
Log(("PGMPhysInterpretedRead: GCPtrSrc=%RGv cb=%#x -> #PF(%#x)\n", GCPtrSrc, cb, uErr));
return TRPMRaiseXcptErrCR2(pVM, pCtxCore, X86_XCPT_PF, uErr, GCPtrSrc);
}
/// @todo VMMDECL(int) PGMPhysInterpretedWrite(PVM pVM, PCPUMCTXCORE pCtxCore, RTGCPTR GCPtrDst, const void *pvSrc, size_t cb)