PGMPool.cpp revision c33db29e7b41467a35675031f5f5233839909083
52f16f53a955f5b24bc2132c418a5fffb700f089vboxsync/* $Id$ */
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync/** @file
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * PGM Shadow Page Pool.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync */
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync/*
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2006-2007 innotek GmbH
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * available from http://www.virtualbox.org. This file is free software;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * you can redistribute it and/or modify it under the terms of the GNU
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * General Public License (GPL) as published by the Free Software
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync */
aba0e602e244ae7c4f11b50fc6d2440f5a762038vboxsync
aba0e602e244ae7c4f11b50fc6d2440f5a762038vboxsync/** @page pg_pgm_pool PGM Shadow Page Pool
aba0e602e244ae7c4f11b50fc6d2440f5a762038vboxsync *
aba0e602e244ae7c4f11b50fc6d2440f5a762038vboxsync * Motivations:
aba0e602e244ae7c4f11b50fc6d2440f5a762038vboxsync * -# Relationship between shadow page tables and physical guest pages. This
aba0e602e244ae7c4f11b50fc6d2440f5a762038vboxsync * should allow us to skip most of the global flushes now following access
aba0e602e244ae7c4f11b50fc6d2440f5a762038vboxsync * handler changes. The main expense is flushing shadow pages.
aba0e602e244ae7c4f11b50fc6d2440f5a762038vboxsync * -# Limit the pool size (currently it's kind of limitless IIRC).
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * -# Allocate shadow pages from GC. Currently we're allocating at SyncCR3 time.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * -# Required for 64-bit guests.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * -# Combining the PD cache and page pool in order to simplify caching.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * @section sec_pgm_pool_outline Design Outline
64e0c74b525c440a571ce06f3eb6234d75913d76vboxsync *
64241796dca8fa36d3fca205e01b4320193a36b7vboxsync * The shadow page pool tracks pages used for shadowing paging structures (i.e. page
64241796dca8fa36d3fca205e01b4320193a36b7vboxsync * tables, page directory, page directory pointer table and page map level-4). Each
64241796dca8fa36d3fca205e01b4320193a36b7vboxsync * page in the pool has an unique identifier. This identifier is used to link a guest
d6aa6429f99fb7648883eb612f8a52b9aaf3bff4vboxsync * physical page to a shadow PT. The identifier is a non-zero value and has a
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync * relativly low max value - say 14 bits. This makes it possible to fit it into the
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * upper bits of the of the aHCPhys entries in the ram range.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
c312e1b81dffe42e0fb766020fb8defaeade05d6vboxsync * By restricting host physical memory to the first 48 bits (which is the announced
c312e1b81dffe42e0fb766020fb8defaeade05d6vboxsync * physical memory range of the K8L chip (scheduled for 2008)), we can safely use the
a34996f4849a881e4112ba993984dcd2388b8bf2vboxsync * upper 16 bits for shadow page ID and reference counting.
a34996f4849a881e4112ba993984dcd2388b8bf2vboxsync *
a34996f4849a881e4112ba993984dcd2388b8bf2vboxsync * Now, it's possible for a page to be aliased, i.e. mapped by more than one PT or
a34996f4849a881e4112ba993984dcd2388b8bf2vboxsync * PD. This is solved by creating a list of physical cross reference extents when
a34996f4849a881e4112ba993984dcd2388b8bf2vboxsync * ever this happens. Each node in the list (extent) is can contain 3 page pool
a34996f4849a881e4112ba993984dcd2388b8bf2vboxsync * indexes. The list it self is chained using indexes into the paPhysExt array.
64241796dca8fa36d3fca205e01b4320193a36b7vboxsync *
64241796dca8fa36d3fca205e01b4320193a36b7vboxsync *
64241796dca8fa36d3fca205e01b4320193a36b7vboxsync * @section sec_pgm_pool_life Life Cycle of a Shadow Page
a34996f4849a881e4112ba993984dcd2388b8bf2vboxsync *
a34996f4849a881e4112ba993984dcd2388b8bf2vboxsync * -# The SyncPT function requests a page from the pool.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync * The request includes the kind of page it is (PT/PD, PAE/legacy), the
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * address of the page it's shadowing, and more.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * -# The pool responds to the request by allocating a new page.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * When the cache is enabled, it will first check if it's in the cache.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * Should the pool be exhausted, one of two things can be done:
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * -# Flush the whole pool and current CR3.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * -# Use the cache to find a page which can be flushed (~age).
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * -# The SyncPT function will sync one or more pages and insert it into the
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * shadow PD.
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync * -# The SyncPage function may sync more pages on a later \#PFs.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * -# The page is freed / flushed in SyncCR3 (perhaps) and some other cases.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * When caching is enabled, the page isn't flush but remains in the cache.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * @section sec_pgm_pool_impl Monitoring
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * We always monitor PAGE_SIZE chunks of memory. When we've got multiple shadow
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * pages for the same PAGE_SIZE of guest memory (PAE and mixed PD/PT) the pages
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * sharing the monitor get linked using the iMonitoredNext/Prev. The head page
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * is the pvUser to the access handlers.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * @section sec_pgm_pool_impl Implementation
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * The pool will take pages from the MM page pool. The tracking data (attributes,
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * bitmaps and so on) are allocated from the hypervisor heap. The pool content can
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * be accessed both by using the page id and the physical address (HC). The former
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * is managed by means of an array, the latter by an offset based AVL tree.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * Flushing of a pool page means that we iterate the content (we know what kind
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * it is) and updates the link information in the ram range.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * ...
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync */
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync/*******************************************************************************
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync* Header Files *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync*******************************************************************************/
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#define LOG_GROUP LOG_GROUP_PGM_POOL
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <VBox/pgm.h>
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync#include <VBox/mm.h>
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync#include "PGMInternal.h"
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync#include <VBox/vm.h>
54828795a553ed0731f308ebda81675ad2c39d58vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <VBox/log.h>
54828795a553ed0731f308ebda81675ad2c39d58vboxsync#include <VBox/err.h>
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <iprt/asm.h>
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <iprt/string.h>
54828795a553ed0731f308ebda81675ad2c39d58vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync/*******************************************************************************
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync* Internal Functions *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync*******************************************************************************/
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#ifdef PGMPOOL_WITH_MONITORING
50f0e2e83362e100d306a411980d555d46aa00a8vboxsyncstatic DECLCALLBACK(int) pgmR3PoolAccessHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser);
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync#endif /* PGMPOOL_WITH_MONITORING */
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync
54828795a553ed0731f308ebda81675ad2c39d58vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync/**
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * Initalizes the pool
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * @returns VBox status code.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * @param pVM The VM handle.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync */
b0dfb334954c0552bb583967a3077ec88fd00471vboxsyncint pgmR3PoolInit(PVM pVM)
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync{
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync /*
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync * Query Pool config.
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync */
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync PCFGMNODE pCfg = CFGMR3GetChild(CFGMR3GetRoot(pVM), "/PGM/Pool");
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync uint16_t cMaxPages;
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync int rc = CFGMR3QueryU16(pCfg, "MaxPages", &cMaxPages);
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync cMaxPages = 4*_1M >> PAGE_SHIFT;
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync else if (VBOX_FAILURE(rc))
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync AssertRCReturn(rc, rc);
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync else
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync AssertMsgReturn(cMaxPages <= PGMPOOL_IDX_LAST && cMaxPages >= RT_ALIGN(PGMPOOL_IDX_FIRST, 16),
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync ("cMaxPages=%u (%#x)\n", cMaxPages, cMaxPages), VERR_INVALID_PARAMETER);
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync cMaxPages = RT_ALIGN(cMaxPages, 16);
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync uint16_t cMaxUsers;
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync rc = CFGMR3QueryU16(pCfg, "MaxUsers", &cMaxUsers);
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync cMaxUsers = cMaxPages * 2;
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync else if (VBOX_FAILURE(rc))
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync AssertRCReturn(rc, rc);
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync else
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync AssertMsgReturn(cMaxUsers >= cMaxPages && cMaxPages <= _32K,
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync ("cMaxUsers=%u (%#x)\n", cMaxUsers, cMaxUsers), VERR_INVALID_PARAMETER);
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync uint16_t cMaxPhysExts;
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync rc = CFGMR3QueryU16(pCfg, "MaxPhysExts", &cMaxPhysExts);
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync cMaxPhysExts = RT_MAX(cMaxPages * 2, PGMPOOL_IDX_LAST);
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync else if (VBOX_FAILURE(rc))
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync AssertRCReturn(rc, rc);
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync else
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync AssertMsgReturn(cMaxPhysExts >= 16 && cMaxPages <= PGMPOOL_IDX_LAST,
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync ("cMaxPhysExts=%u (%#x)\n", cMaxPhysExts, cMaxUsers), VERR_INVALID_PARAMETER);
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync bool fCacheEnabled;
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync rc = CFGMR3QueryBool(pCfg, "CacheEnabled", &fCacheEnabled);
2084a447d1acb619df7c393fac41b79d517e4b3dvboxsync if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync fCacheEnabled = true;
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync else if (VBOX_FAILURE(rc))
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync AssertRCReturn(rc, rc);
5c320d1b52e5e4ae280cd680c2c64de5f5f13d22vboxsync
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync Log(("pgmR3PoolInit: cMaxPages=%#RX16 cMaxUsers=%#RX16 cMaxPhysExts=%#RX16 fCacheEnable=%RTbool\n",
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync cMaxPages, cMaxUsers, cMaxPhysExts, fCacheEnabled));
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync /*
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync * Allocate the data structures.
5c320d1b52e5e4ae280cd680c2c64de5f5f13d22vboxsync */
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync uint32_t cb = RT_OFFSETOF(PGMPOOL, aPages[cMaxPages]);
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync#ifdef PGMPOOL_WITH_USER_TRACKING
b3d4b85739cf74a503b1f8bbb7c7f4de26c1c09fvboxsync cb += cMaxUsers * sizeof(PGMPOOLUSER);
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync#endif
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
b60e4b0625949fd68ed97f1353e2174c5b3192e5vboxsync cb += cMaxPhysExts * sizeof(PGMPOOLPHYSEXT);
b60e4b0625949fd68ed97f1353e2174c5b3192e5vboxsync#endif
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync PPGMPOOL pPool;
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync rc = MMR3HyperAllocOnceNoRel(pVM, cb, 0, MM_TAG_PGM_POOL, (void **)&pPool);
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync if (VBOX_FAILURE(rc))
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync return rc;
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync pVM->pgm.s.pPoolHC = pPool;
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync pVM->pgm.s.pPoolGC = MMHyperHC2GC(pVM, pPool);
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync /*
64e0c74b525c440a571ce06f3eb6234d75913d76vboxsync * Initialize it.
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync */
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync pPool->pVMHC = pVM;
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync pPool->pVMGC = pVM->pVMGC;
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync pPool->cMaxPages = cMaxPages;
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync pPool->cCurPages = PGMPOOL_IDX_FIRST;
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync#ifdef PGMPOOL_WITH_USER_TRACKING
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync pPool->iUserFreeHead = 0;
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync pPool->cMaxUsers = cMaxUsers;
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync PPGMPOOLUSER paUsers = (PPGMPOOLUSER)&pPool->aPages[pPool->cMaxPages];
052deaa01d8fcd5cec4dff857833538940b751c3vboxsync pPool->paUsersHC = paUsers;
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync pPool->paUsersGC = MMHyperHC2GC(pVM, paUsers);
39a628c9e979cb2355caa57eb099b13cb922783cvboxsync for (unsigned i = 0; i < cMaxUsers; i++)
39a628c9e979cb2355caa57eb099b13cb922783cvboxsync {
26bef2fb65df80a28b9972e0a43a92be367417d9vboxsync paUsers[i].iNext = i + 1;
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync paUsers[i].iUser = NIL_PGMPOOL_IDX;
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync paUsers[i].iUserTable = 0xfffe;
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync }
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync paUsers[cMaxUsers - 1].iNext = NIL_PGMPOOL_USER_INDEX;
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync#endif
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
39a628c9e979cb2355caa57eb099b13cb922783cvboxsync pPool->iPhysExtFreeHead = 0;
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync pPool->cMaxPhysExts = cMaxPhysExts;
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync PPGMPOOLPHYSEXT paPhysExts = (PPGMPOOLPHYSEXT)&paUsers[cMaxUsers];
d308e8fdb9e4d827ed10d26d1581d265602f6f46vboxsync pPool->paPhysExtsHC = paPhysExts;
d308e8fdb9e4d827ed10d26d1581d265602f6f46vboxsync pPool->paPhysExtsGC = MMHyperHC2GC(pVM, paPhysExts);
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync for (unsigned i = 0; i < cMaxPhysExts; i++)
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync {
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync paPhysExts[i].iNext = i + 1;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
7dbde0174637fbfd00c50b383f654e46878eaa8evboxsync paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync }
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync#endif
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync#ifdef PGMPOOL_WITH_CACHE
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync for (unsigned i = 0; i < ELEMENTS(pPool->aiHash); i++)
7dbde0174637fbfd00c50b383f654e46878eaa8evboxsync pPool->aiHash[i] = NIL_PGMPOOL_IDX;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync pPool->iAgeHead = NIL_PGMPOOL_IDX;
208aaecb51db539a2f7f3d25f38dd2efd0f014a3vboxsync pPool->iAgeTail = NIL_PGMPOOL_IDX;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync pPool->fCacheEnabled = fCacheEnabled;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync#endif
2673f120424ba2bb67c6da0eb851b65b22b0cba4vboxsync#ifdef PGMPOOL_WITH_MONITORING
2673f120424ba2bb67c6da0eb851b65b22b0cba4vboxsync pPool->pfnAccessHandlerR3 = pgmR3PoolAccessHandler;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync pPool->pszAccessHandler = "Guest Paging Access Handler";
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#endif
7dbde0174637fbfd00c50b383f654e46878eaa8evboxsync pPool->HCPhysTree = 0;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync /* The NIL entry. */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync Assert(NIL_PGMPOOL_IDX == 0);
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync pPool->aPages[NIL_PGMPOOL_IDX].enmKind = PGMPOOLKIND_INVALID;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync /* The Shadow 32-bit PD. */
208aaecb51db539a2f7f3d25f38dd2efd0f014a3vboxsync pPool->aPages[PGMPOOL_IDX_PD].Core.Key = NIL_RTHCPHYS;
b83d9b1072dd8491c7ffe37830e8fd10f2dba561vboxsync pPool->aPages[PGMPOOL_IDX_PD].GCPhys = NIL_RTGCPHYS;
208aaecb51db539a2f7f3d25f38dd2efd0f014a3vboxsync pPool->aPages[PGMPOOL_IDX_PD].pvPageHC = pVM->pgm.s.pHC32BitPD;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync pPool->aPages[PGMPOOL_IDX_PD].enmKind = PGMPOOLKIND_ROOT_32BIT_PD;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync pPool->aPages[PGMPOOL_IDX_PD].idx = PGMPOOL_IDX_PD;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync /* The Shadow PAE PDs. This is actually 4 pages! */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync pPool->aPages[PGMPOOL_IDX_PAE_PD].Core.Key = NIL_RTHCPHYS;
7dbde0174637fbfd00c50b383f654e46878eaa8evboxsync pPool->aPages[PGMPOOL_IDX_PAE_PD].GCPhys = NIL_RTGCPHYS;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync pPool->aPages[PGMPOOL_IDX_PAE_PD].pvPageHC = pVM->pgm.s.apHCPaePDs[0];
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync pPool->aPages[PGMPOOL_IDX_PAE_PD].enmKind = PGMPOOLKIND_ROOT_PAE_PD;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync pPool->aPages[PGMPOOL_IDX_PAE_PD].idx = PGMPOOL_IDX_PAE_PD;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync /* The Shadow PDPT. */
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync pPool->aPages[PGMPOOL_IDX_PDPT].Core.Key = NIL_RTHCPHYS;
7dbde0174637fbfd00c50b383f654e46878eaa8evboxsync pPool->aPages[PGMPOOL_IDX_PDPT].GCPhys = NIL_RTGCPHYS;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync pPool->aPages[PGMPOOL_IDX_PDPT].pvPageHC = pVM->pgm.s.pHCPaePDPT;
208aaecb51db539a2f7f3d25f38dd2efd0f014a3vboxsync pPool->aPages[PGMPOOL_IDX_PDPT].enmKind = PGMPOOLKIND_ROOT_PDPT;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync pPool->aPages[PGMPOOL_IDX_PDPT].idx = PGMPOOL_IDX_PDPT;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync /* The Shadow Page Map Level-4. */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync pPool->aPages[PGMPOOL_IDX_PML4].Core.Key = NIL_RTHCPHYS;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync pPool->aPages[PGMPOOL_IDX_PML4].GCPhys = NIL_RTGCPHYS;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync pPool->aPages[PGMPOOL_IDX_PML4].pvPageHC = pVM->pgm.s.pHCPaePML4;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync pPool->aPages[PGMPOOL_IDX_PML4].enmKind = PGMPOOLKIND_ROOT_PML4;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync pPool->aPages[PGMPOOL_IDX_PML4].idx = PGMPOOL_IDX_PML4;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync
6c09ee027ad80a5fe6b70ad652253ae6edd09121vboxsync /*
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync * Set common stuff.
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync */
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync for (unsigned iPage = 1; iPage < PGMPOOL_IDX_FIRST; iPage++)
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync {
7dbde0174637fbfd00c50b383f654e46878eaa8evboxsync pPool->aPages[iPage].iNext = NIL_PGMPOOL_IDX;
2673f120424ba2bb67c6da0eb851b65b22b0cba4vboxsync#ifdef PGMPOOL_WITH_USER_TRACKING
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync pPool->aPages[iPage].iUserHead = NIL_PGMPOOL_USER_INDEX;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#endif
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#ifdef PGMPOOL_WITH_MONITORING
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync pPool->aPages[iPage].iModifiedNext = NIL_PGMPOOL_IDX;
7dbde0174637fbfd00c50b383f654e46878eaa8evboxsync pPool->aPages[iPage].iModifiedPrev = NIL_PGMPOOL_IDX;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync pPool->aPages[iPage].iMonitoredNext = NIL_PGMPOOL_IDX;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync pPool->aPages[iPage].iMonitoredNext = NIL_PGMPOOL_IDX;
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#endif
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync#ifdef PGMPOOL_WITH_CACHE
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync pPool->aPages[iPage].iAgeNext = NIL_PGMPOOL_IDX;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync pPool->aPages[iPage].iAgePrev = NIL_PGMPOOL_IDX;
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync#endif
7dbde0174637fbfd00c50b383f654e46878eaa8evboxsync Assert(VALID_PTR(pPool->aPages[iPage].pvPageHC));
846739bf4fe90231599e8de3a8c792ae2851b1d1vboxsync Assert(pPool->aPages[iPage].idx == iPage);
5b5f34343f19663d77572b58d4cc70bf57e64b51vboxsync Assert(pPool->aPages[iPage].GCPhys == NIL_RTGCPHYS);
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync Assert(!pPool->aPages[iPage].fSeenNonGlobal);
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync Assert(!pPool->aPages[iPage].fMonitored);
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync Assert(!pPool->aPages[iPage].fCached);
846739bf4fe90231599e8de3a8c792ae2851b1d1vboxsync Assert(!pPool->aPages[iPage].fZeroed);
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync Assert(!pPool->aPages[iPage].fReusedFlushPending);
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync }
846739bf4fe90231599e8de3a8c792ae2851b1d1vboxsync
5b5f34343f19663d77572b58d4cc70bf57e64b51vboxsync#ifdef VBOX_WITH_STATISTICS
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync /*
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync * Register statistics.
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync */
846739bf4fe90231599e8de3a8c792ae2851b1d1vboxsync STAM_REG(pVM, &pPool->cCurPages, STAMTYPE_U16, "/PGM/Pool/cCurPages", STAMUNIT_PAGES, "Current pool size.");
846739bf4fe90231599e8de3a8c792ae2851b1d1vboxsync STAM_REG(pVM, &pPool->cMaxPages, STAMTYPE_U16, "/PGM/Pool/cMaxPages", STAMUNIT_PAGES, "Max pool size.");
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync STAM_REG(pVM, &pPool->cUsedPages, STAMTYPE_U16, "/PGM/Pool/cUsedPages", STAMUNIT_PAGES, "The number of pages currently in use.");
846739bf4fe90231599e8de3a8c792ae2851b1d1vboxsync STAM_REG(pVM, &pPool->cUsedPagesHigh, STAMTYPE_U16_RESET, "/PGM/Pool/cUsedPagesHigh", STAMUNIT_PAGES, "The high watermark for cUsedPages.");
5b5f34343f19663d77572b58d4cc70bf57e64b51vboxsync STAM_REG(pVM, &pPool->StatAlloc, STAMTYPE_PROFILE_ADV, "/PGM/Pool/Alloc", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolAlloc.");
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync STAM_REG(pVM, &pPool->StatClearAll, STAMTYPE_PROFILE, "/PGM/Pool/ClearAll", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolClearAll.");
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync STAM_REG(pVM, &pPool->StatFlushAllInt, STAMTYPE_PROFILE, "/PGM/Pool/FlushAllInt", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolFlushAllInt.");
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync STAM_REG(pVM, &pPool->StatFlushPage, STAMTYPE_PROFILE, "/PGM/Pool/FlushPage", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolFlushPage.");
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync STAM_REG(pVM, &pPool->StatFree, STAMTYPE_PROFILE, "/PGM/Pool/Free", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolFree.");
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync STAM_REG(pVM, &pPool->StatZeroPage, STAMTYPE_PROFILE, "/PGM/Pool/ZeroPage", STAMUNIT_TICKS_PER_CALL, "Profiling time spend zeroing pages. Overlaps with Alloc.");
3c3a5ab35783f4d31cb5d3a15db9daadeb804daavboxsync# ifdef PGMPOOL_WITH_USER_TRACKING
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync STAM_REG(pVM, &pPool->cMaxUsers, STAMTYPE_U16, "/PGM/Pool/Track/cMaxUsers", STAMUNIT_COUNT, "Max user tracking records.");
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync STAM_REG(pVM, &pPool->cPresent, STAMTYPE_U32, "/PGM/Pool/Track/cPresent", STAMUNIT_COUNT, "Number of present page table entries.");
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync STAM_REG(pVM, &pPool->StatTrackDeref, STAMTYPE_PROFILE, "/PGM/Pool/Track/Deref", STAMUNIT_OCCURENCES, "Profiling of pgmPoolTrackDeref.");
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync STAM_REG(pVM, &pPool->StatTrackFlushGCPhysPT, STAMTYPE_PROFILE, "/PGM/Pool/Track/FlushGCPhysPT", STAMUNIT_OCCURENCES, "Profiling of pgmPoolTrackFlushGCPhysPT.");
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync STAM_REG(pVM, &pPool->StatTrackFlushGCPhysPTs, STAMTYPE_PROFILE, "/PGM/Pool/Track/FlushGCPhysPTs", STAMUNIT_OCCURENCES, "Profiling of pgmPoolTrackFlushGCPhysPTs.");
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync STAM_REG(pVM, &pPool->StatTrackFlushGCPhysPTsSlow, STAMTYPE_PROFILE, "/PGM/Pool/Track/FlushGCPhysPTsSlow", STAMUNIT_OCCURENCES, "Profiling of pgmPoolTrackFlushGCPhysPTsSlow.");
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync STAM_REG(pVM, &pPool->StatTrackFreeUpOneUser, STAMTYPE_COUNTER, "/PGM/Pool/Track/FreeUpOneUser", STAMUNIT_OCCURENCES, "The number of times we were out of user tracking records.");
208aaecb51db539a2f7f3d25f38dd2efd0f014a3vboxsync# endif
b83d9b1072dd8491c7ffe37830e8fd10f2dba561vboxsync# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
208aaecb51db539a2f7f3d25f38dd2efd0f014a3vboxsync STAM_REG(pVM, &pPool->StatTrackDerefGCPhys, STAMTYPE_PROFILE, "/PGM/Pool/Track/DrefGCPhys", STAMUNIT_OCCURENCES, "Profiling deref activity related tracking GC physical pages.");
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync STAM_REG(pVM, &pPool->StatTrackLinearRamSearches, STAMTYPE_COUNTER, "/PGM/Pool/Track/LinearRamSearches", STAMUNIT_OCCURENCES, "The number of times we had to do linear ram searches.");
STAM_REG(pVM, &pPool->StamTrackPhysExtAllocFailures,STAMTYPE_COUNTER, "/PGM/Pool/Track/PhysExtAllocFailures", STAMUNIT_OCCURENCES, "The number of failing pgmPoolTrackPhysExtAlloc calls.");
# endif
# ifdef PGMPOOL_WITH_MONITORING
STAM_REG(pVM, &pPool->StatMonitorGC, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/GC", STAMUNIT_TICKS_PER_CALL, "Profiling the GC PT access handler.");
STAM_REG(pVM, &pPool->StatMonitorGCEmulateInstr, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/GCEmulateInstr", STAMUNIT_OCCURENCES, "Times we've failed interpreting the instruction.");
STAM_REG(pVM, &pPool->StatMonitorGCFlushPage, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/GCFlushPage", STAMUNIT_TICKS_PER_CALL, "Profiling the pgmPoolFlushPage calls made from the GC PT access handler.");
STAM_REG(pVM, &pPool->StatMonitorGCFork, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/GCFork", STAMUNIT_OCCURENCES, "Times we've detected fork().");
STAM_REG(pVM, &pPool->StatMonitorGCHandled, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/GCHandled", STAMUNIT_TICKS_PER_CALL, "Profiling the GC access we've handled (except REP STOSD).");
STAM_REG(pVM, &pPool->StatMonitorGCIntrFailPatch1, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/GCIntrFailPatch1", STAMUNIT_OCCURENCES, "Times we've failed interpreting a patch code instruction.");
STAM_REG(pVM, &pPool->StatMonitorGCIntrFailPatch2, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/GCIntrFailPatch2", STAMUNIT_OCCURENCES, "Times we've failed interpreting a patch code instruction during flushing.");
STAM_REG(pVM, &pPool->StatMonitorGCRepPrefix, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/GCRepPrefix", STAMUNIT_OCCURENCES, "The number of times we've seen rep prefixes we can't handle.");
STAM_REG(pVM, &pPool->StatMonitorGCRepStosd, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/GCRepStosd", STAMUNIT_TICKS_PER_CALL, "Profiling the REP STOSD cases we've handled.");
STAM_REG(pVM, &pPool->StatMonitorHC, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/HC", STAMUNIT_TICKS_PER_CALL, "Profiling the HC PT access handler.");
STAM_REG(pVM, &pPool->StatMonitorHCEmulateInstr, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/HCEmulateInstr", STAMUNIT_OCCURENCES, "Times we've failed interpreting the instruction.");
STAM_REG(pVM, &pPool->StatMonitorHCFlushPage, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/HCFlushPage", STAMUNIT_TICKS_PER_CALL, "Profiling the pgmPoolFlushPage calls made from the HC PT access handler.");
STAM_REG(pVM, &pPool->StatMonitorHCFork, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/HCFork", STAMUNIT_OCCURENCES, "Times we've detected fork().");
STAM_REG(pVM, &pPool->StatMonitorHCHandled, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/HCHandled", STAMUNIT_TICKS_PER_CALL, "Profiling the HC access we've handled (except REP STOSD).");
STAM_REG(pVM, &pPool->StatMonitorHCRepPrefix, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/HCRepPrefix", STAMUNIT_OCCURENCES, "The number of times we've seen rep prefixes we can't handle.");
STAM_REG(pVM, &pPool->StatMonitorHCRepStosd, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/HCRepStosd", STAMUNIT_TICKS_PER_CALL, "Profiling the REP STOSD cases we've handled.");
STAM_REG(pVM, &pPool->StatMonitorHCAsync, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/HCAsync", STAMUNIT_OCCURENCES, "Times we're called in an async thread and need to flush.");
STAM_REG(pVM, &pPool->cModifiedPages, STAMTYPE_U16, "/PGM/Pool/Monitor/cModifiedPages", STAMUNIT_PAGES, "The current cModifiedPages value.");
STAM_REG(pVM, &pPool->cModifiedPagesHigh, STAMTYPE_U16_RESET, "/PGM/Pool/Monitor/cModifiedPagesHigh", STAMUNIT_PAGES, "The high watermark for cModifiedPages.");
# endif
# ifdef PGMPOOL_WITH_CACHE
STAM_REG(pVM, &pPool->StatCacheHits, STAMTYPE_COUNTER, "/PGM/Pool/Cache/Hits", STAMUNIT_OCCURENCES, "The number of pgmPoolAlloc calls satisfied by the cache.");
STAM_REG(pVM, &pPool->StatCacheMisses, STAMTYPE_COUNTER, "/PGM/Pool/Cache/Misses", STAMUNIT_OCCURENCES, "The number of pgmPoolAlloc calls not statisfied by the cache.");
STAM_REG(pVM, &pPool->StatCacheKindMismatches, STAMTYPE_COUNTER, "/PGM/Pool/Cache/KindMismatches", STAMUNIT_OCCURENCES, "The number of shadow page kind mismatches. (Better be low, preferably 0!)");
STAM_REG(pVM, &pPool->StatCacheFreeUpOne, STAMTYPE_COUNTER, "/PGM/Pool/Cache/FreeUpOne", STAMUNIT_OCCURENCES, "The number of times the cache was asked to free up a page.");
STAM_REG(pVM, &pPool->StatCacheCacheable, STAMTYPE_COUNTER, "/PGM/Pool/Cache/Cacheable", STAMUNIT_OCCURENCES, "The number of cacheable allocations.");
STAM_REG(pVM, &pPool->StatCacheUncacheable, STAMTYPE_COUNTER, "/PGM/Pool/Cache/Uncacheable", STAMUNIT_OCCURENCES, "The number of uncacheable allocations.");
# endif
#endif /* VBOX_WITH_STATISTICS */
return VINF_SUCCESS;
}
/**
* Relocate the page pool data.
*
* @param pVM The VM handle.
*/
void pgmR3PoolRelocate(PVM pVM)
{
pVM->pgm.s.pPoolGC = MMHyperHC2GC(pVM, pVM->pgm.s.pPoolHC);
pVM->pgm.s.pPoolHC->pVMGC = pVM->pVMGC;
#ifdef PGMPOOL_WITH_USER_TRACKING
pVM->pgm.s.pPoolHC->paUsersGC = MMHyperHC2GC(pVM, pVM->pgm.s.pPoolHC->paUsersHC);
#endif
#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
pVM->pgm.s.pPoolHC->paPhysExtsGC = MMHyperHC2GC(pVM, pVM->pgm.s.pPoolHC->paPhysExtsHC);
#endif
#ifdef PGMPOOL_WITH_MONITORING
int rc = PDMR3GetSymbolGC(pVM, NULL, "pgmPoolAccessHandler", &pVM->pgm.s.pPoolHC->pfnAccessHandlerGC);
AssertReleaseRC(rc);
/* init order hack. */
if (!pVM->pgm.s.pPoolHC->pfnAccessHandlerR0)
{
rc = PDMR3GetSymbolR0(pVM, NULL, "pgmPoolAccessHandler", &pVM->pgm.s.pPoolHC->pfnAccessHandlerR0);
AssertReleaseRC(rc);
}
#endif
}
/**
* Reset notification.
*
* This will flush the pool.
* @param pVM The VM handle.
*/
void pgmR3PoolReset(PVM pVM)
{
pgmPoolFlushAll(pVM);
}
/**
* Grows the shadow page pool.
*
* I.e. adds more pages to it, assuming that hasn't reached cMaxPages yet.
*
* @returns VBox status code.
* @param pVM The VM handle.
*/
PDMR3DECL(int) PGMR3PoolGrow(PVM pVM)
{
PPGMPOOL pPool = pVM->pgm.s.pPoolHC;
AssertReturn(pPool->cCurPages < pPool->cMaxPages, VERR_INTERNAL_ERROR);
/*
* How much to grow it by?
*/
uint32_t cPages = pPool->cMaxPages - pPool->cCurPages;
cPages = RT_MIN(PGMPOOL_CFG_MAX_GROW, cPages);
LogFlow(("PGMR3PoolGrow: Growing the pool by %d (%#x) pages.\n", cPages, cPages));
for (unsigned i = pPool->cCurPages; cPages-- > 0; i++)
{
PPGMPOOLPAGE pPage = &pPool->aPages[i];
pPage->pvPageHC = MMR3PageAlloc(pVM);
if (!pPage->pvPageHC)
{
Log(("We're out of memory!! i=%d\n", i));
return i ? VINF_SUCCESS : VERR_NO_PAGE_MEMORY;
}
pPage->Core.Key = MMPage2Phys(pVM, pPage->pvPageHC);
pPage->GCPhys = NIL_RTGCPHYS;
pPage->enmKind = PGMPOOLKIND_FREE;
pPage->idx = pPage - &pPool->aPages[0];
pPage->iNext = pPool->iFreeHead;
#ifdef PGMPOOL_WITH_USER_TRACKING
pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
#endif
#ifdef PGMPOOL_WITH_MONITORING
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
#endif
#ifdef PGMPOOL_WITH_CACHE
pPage->iAgeNext = NIL_PGMPOOL_IDX;
pPage->iAgePrev = NIL_PGMPOOL_IDX;
#endif
/* commit it */
bool fRc = RTAvloHCPhysInsert(&pPool->HCPhysTree, &pPage->Core); Assert(fRc); NOREF(fRc);
pPool->iFreeHead = i;
pPool->cCurPages = i + 1;
}
Assert(pPool->cCurPages <= pPool->cMaxPages);
return VINF_SUCCESS;
}
#ifdef PGMPOOL_WITH_MONITORING
/**
* Worker used by pgmR3PoolAccessHandler when it's invoked by an async thread.
*
* @param pPool The pool.
* @param pPage The page.
*/
static DECLCALLBACK(void) pgmR3PoolFlushReusedPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
/* for the present this should be safe enough I think... */
pgmLock(pPool->pVMHC);
if ( pPage->fReusedFlushPending
&& pPage->enmKind != PGMPOOLKIND_FREE)
pgmPoolFlushPage(pPool, pPage);
pgmUnlock(pPool->pVMHC);
}
/**
* \#PF Handler callback for PT write accesses.
*
* The handler can not raise any faults, it's mainly for monitoring write access
* to certain pages.
*
* @returns VINF_SUCCESS if the handler have carried out the operation.
* @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
* @param pVM VM Handle.
* @param GCPhys The physical address the guest is writing to.
* @param pvPhys The HC mapping of that address.
* @param pvBuf What the guest is reading/writing.
* @param cbBuf How much it's reading/writing.
* @param enmAccessType The access type.
* @param pvUser User argument.
*/
static DECLCALLBACK(int) pgmR3PoolAccessHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser)
{
STAM_PROFILE_START(&pVM->pgm.s.pPoolHC->StatMonitorHC, a);
PPGMPOOL pPool = pVM->pgm.s.pPoolHC;
PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)pvUser;
LogFlow(("pgmR3PoolAccessHandler: GCPhys=%VGp %p:{.Core=%RHp, .idx=%d, .GCPhys=%RGp, .enmType=%d}\n",
GCPhys, pPage, pPage->Core.Key, pPage->idx, pPage->GCPhys, pPage->enmKind));
/*
* We don't have to be very sophisiticated about this since there are relativly few calls here.
* However, we must try our best to detect any non-cpu accesses (disk / networking).
*
* Just to make life more interesting, we'll have to deal with the async threads too.
* We cannot flush a page if we're in an async thread because of REM notifications.
*/
if (!VM_IS_EMT(pVM))
{
Log(("pgmR3PoolAccessHandler: async thread, requesting EMT to flush the page: %p:{.Core=%RHp, .idx=%d, .GCPhys=%RGp, .enmType=%d}\n",
pPage, pPage->Core.Key, pPage->idx, pPage->GCPhys, pPage->enmKind));
STAM_COUNTER_INC(&pPool->StatMonitorHCAsync);
if (!pPage->fReusedFlushPending)
{
int rc = VMR3ReqCallEx(pPool->pVMHC, NULL, 0, VMREQFLAGS_NO_WAIT | VMREQFLAGS_VOID, (PFNRT)pgmR3PoolFlushReusedPage, 2, pPool, pPage);
AssertRCReturn(rc, rc);
pPage->fReusedFlushPending = true;
pPage->cModifications += 0x1000;
}
pgmPoolMonitorChainChanging(pPool, pPage, GCPhys, pvPhys, NULL);
/** @todo r=bird: making unsafe assumption about not crossing entries here! */
while (cbBuf > 4)
{
cbBuf -= 4;
pvPhys = (uint8_t *)pvPhys + 4;
GCPhys += 4;
pgmPoolMonitorChainChanging(pPool, pPage, GCPhys, pvPhys, NULL);
}
STAM_PROFILE_STOP(&pPool->StatMonitorHC, a);
}
else if ( (pPage->fCR3Mix || pPage->cModifications < 96) /* it's cheaper here. */
&& cbBuf <= 4)
{
/* Clear the shadow entry. */
if (!pPage->cModifications++)
pgmPoolMonitorModifiedInsert(pPool, pPage);
/** @todo r=bird: making unsafe assumption about not crossing entries here! */
pgmPoolMonitorChainChanging(pPool, pPage, GCPhys, pvPhys, NULL);
STAM_PROFILE_STOP(&pPool->StatMonitorHC, a);
}
else
{
pgmPoolMonitorChainFlush(pPool, pPage); /* ASSUME that VERR_PGM_POOL_CLEARED can be ignored here and that FFs will deal with it in due time. */
STAM_PROFILE_STOP_EX(&pPool->StatMonitorHC, &pPool->StatMonitorHCFlushPage, a);
}
return VINF_PGM_HANDLER_DO_DEFAULT;
}
#endif /* PGMPOOL_WITH_MONITORING */