PGMPool.cpp revision bc2244812082bd1de1455f779c3ca4af1dd9a50c
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/* $Id$ */
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/** @file
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * PGM Shadow Page Pool.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync */
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/*
5654aa8329bbe2838fa5733f28c1a0461c9e6453vboxsync * Copyright (C) 2006-2007 Oracle Corporation
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * available from http://www.virtualbox.org. This file is free software;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync */
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync/** @page pg_pgm_pool PGM Shadow Page Pool
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * Motivations:
f84cd77241a1c4b9106a92280611c659243e10d1vboxsync * -# Relationship between shadow page tables and physical guest pages. This
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync * should allow us to skip most of the global flushes now following access
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync * handler changes. The main expense is flushing shadow pages.
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync * -# Limit the pool size if necessary (default is kind of limitless).
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync * -# Allocate shadow pages from RC. We use to only do this in SyncCR3.
0c437bb10c61b229407a7517efde04dfe3b1e4a1vboxsync * -# Required for 64-bit guests.
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync * -# Combining the PD cache and page pool in order to simplify caching.
0c437bb10c61b229407a7517efde04dfe3b1e4a1vboxsync *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * @section sec_pgm_pool_outline Design Outline
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync *
209c11e4b5dbb310116c99a42d773163928e002bvboxsync * The shadow page pool tracks pages used for shadowing paging structures (i.e.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * page tables, page directory, page directory pointer table and page map
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync * level-4). Each page in the pool has an unique identifier. This identifier is
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync * used to link a guest physical page to a shadow PT. The identifier is a
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync * non-zero value and has a relativly low max value - say 14 bits. This makes it
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync * possible to fit it into the upper bits of the of the aHCPhys entries in the
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * ram range.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync * By restricting host physical memory to the first 48 bits (which is the
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * announced physical memory range of the K8L chip (scheduled for 2008)), we
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * can safely use the upper 16 bits for shadow page ID and reference counting.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync * Update: The 48 bit assumption will be lifted with the new physical memory
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync * management (PGMPAGE), so we won't have any trouble when someone stuffs 2TB
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * into a box in some years.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync * Now, it's possible for a page to be aliased, i.e. mapped by more than one PT
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync * or PD. This is solved by creating a list of physical cross reference extents
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync * when ever this happens. Each node in the list (extent) is can contain 3 page
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync * pool indexes. The list it self is chained using indexes into the paPhysExt
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync * array.
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync *
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync *
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync * @section sec_pgm_pool_life Life Cycle of a Shadow Page
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync *
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync * -# The SyncPT function requests a page from the pool.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * The request includes the kind of page it is (PT/PD, PAE/legacy), the
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * address of the page it's shadowing, and more.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * -# The pool responds to the request by allocating a new page.
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync * When the cache is enabled, it will first check if it's in the cache.
4bfa7b58e362a1bca0628643c352c137900bf01avboxsync * Should the pool be exhausted, one of two things can be done:
df25990f935e7fd32acd9be9a156aff8d10facf2vboxsync * -# Flush the whole pool and current CR3.
6420f75ffc86ab6494eb5e95418f0c95e71e8068vboxsync * -# Use the cache to find a page which can be flushed (~age).
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * -# The SyncPT function will sync one or more pages and insert it into the
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * shadow PD.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * -# The SyncPage function may sync more pages on a later \#PFs.
faf968cea88f2ab4bcc3325b17bc8b095a8e3642vboxsync * -# The page is freed / flushed in SyncCR3 (perhaps) and some other cases.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync * When caching is enabled, the page isn't flush but remains in the cache.
c7a00ac75c7941df2afb62e6fd7ffdf1795e6c76vboxsync *
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync *
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @section sec_pgm_pool_impl Monitoring
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync *
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * We always monitor PAGE_SIZE chunks of memory. When we've got multiple shadow
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * pages for the same PAGE_SIZE of guest memory (PAE and mixed PD/PT) the pages
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * sharing the monitor get linked using the iMonitoredNext/Prev. The head page
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * is the pvUser to the access handlers.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync *
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync *
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @section sec_pgm_pool_impl Implementation
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync *
683371bbf37760161d1b8454ce978acf89bbb04fvboxsync * The pool will take pages from the MM page pool. The tracking data
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * (attributes, bitmaps and so on) are allocated from the hypervisor heap. The
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * pool content can be accessed both by using the page id and the physical
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * address (HC). The former is managed by means of an array, the latter by an
f94f82d66536c7332c347dd9a3a9f0f8c79247f4vboxsync * offset based AVL tree.
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync *
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * Flushing of a pool page means that we iterate the content (we know what kind
43dff6077acb4176145b18bdb862eb73620182d2vboxsync * it is) and updates the link information in the ram range.
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync *
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * ...
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync */
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync/*******************************************************************************
b40179b44fea65b72b2f226f62af1ed7bd3c48fcvboxsync* Header Files *
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync*******************************************************************************/
43dff6077acb4176145b18bdb862eb73620182d2vboxsync#define LOG_GROUP LOG_GROUP_PGM_POOL
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync#include <VBox/vmm/pgm.h>
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync#include <VBox/vmm/mm.h>
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync#include "PGMInternal.h"
0d4bc23ca3867d6dbedd76d5b1e3725c766adb75vboxsync#include <VBox/vmm/vm.h>
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync#include "PGMInline.h"
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync#include <VBox/log.h>
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync#include <VBox/err.h>
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync#include <iprt/asm.h>
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync#include <iprt/string.h>
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync#include <VBox/dbg.h>
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync/*******************************************************************************
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync* Internal Functions *
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync*******************************************************************************/
ff78b877ed7acd25e2d384570a938441455d6a95vboxsyncstatic DECLCALLBACK(int) pgmR3PoolAccessHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf, PGMACCESSTYPE enmAccessType, void *pvUser);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync#ifdef VBOX_WITH_DEBUGGER
ff78b877ed7acd25e2d384570a938441455d6a95vboxsyncstatic DECLCALLBACK(int) pgmR3PoolCmdCheck(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync#endif
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync#ifdef VBOX_WITH_DEBUGGER
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync/** Command descriptors. */
ff78b877ed7acd25e2d384570a938441455d6a95vboxsyncstatic const DBGCCMD g_aCmds[] =
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync{
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync /* pszCmd, cArgsMin, cArgsMax, paArgDesc, cArgDescs, fFlags, pfnHandler pszSyntax, ....pszDescription */
5654aa8329bbe2838fa5733f28c1a0461c9e6453vboxsync { "pgmpoolcheck", 0, 0, NULL, 0, 0, pgmR3PoolCmdCheck, "", "Check the pgm pool pages." },
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync};
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync#endif
5654aa8329bbe2838fa5733f28c1a0461c9e6453vboxsync
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync/**
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * Initializes the pool
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync *
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @returns VBox status code.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * @param pVM The VM handle.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync */
ff78b877ed7acd25e2d384570a938441455d6a95vboxsyncint pgmR3PoolInit(PVM pVM)
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync{
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync int rc;
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync AssertCompile(NIL_PGMPOOL_IDX == 0);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync /* pPage->cLocked is an unsigned byte. */
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync AssertCompile(VMM_MAX_CPU_COUNT <= 255);
b40179b44fea65b72b2f226f62af1ed7bd3c48fcvboxsync
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync /*
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync * Query Pool config.
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync */
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync PCFGMNODE pCfg = CFGMR3GetChild(CFGMR3GetRoot(pVM), "/PGM/Pool");
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync /* Default pgm pool size is 1024 pages (4MB). */
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync uint16_t cMaxPages = 1024;
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync /* Adjust it up relative to the RAM size, using the nested paging formula. */
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync uint64_t cbRam;
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync rc = CFGMR3QueryU64Def(CFGMR3GetRoot(pVM), "RamSize", &cbRam, 0); AssertRCReturn(rc, rc);
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync uint64_t u64MaxPages = (cbRam >> 9)
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync + (cbRam >> 18)
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync + (cbRam >> 27)
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync + 32 * PAGE_SIZE;
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync u64MaxPages >>= PAGE_SHIFT;
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync if (u64MaxPages > PGMPOOL_IDX_LAST)
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync cMaxPages = PGMPOOL_IDX_LAST;
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync else
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync cMaxPages = (uint16_t)u64MaxPages;
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync
ff78b877ed7acd25e2d384570a938441455d6a95vboxsync /** @cfgm{/PGM/Pool/MaxPages, uint16_t, #pages, 16, 0x3fff, F(ram-size)}
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * The max size of the shadow page pool in pages. The pool will grow dynamically
436b5c616e019c5e62053657c52d3ab5562ecbbfvboxsync * up to this limit.
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync */
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync rc = CFGMR3QueryU16Def(pCfg, "MaxPages", &cMaxPages, cMaxPages);
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync AssertLogRelRCReturn(rc, rc);
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync AssertLogRelMsgReturn(cMaxPages <= PGMPOOL_IDX_LAST && cMaxPages >= RT_ALIGN(PGMPOOL_IDX_FIRST, 16),
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync ("cMaxPages=%u (%#x)\n", cMaxPages, cMaxPages), VERR_INVALID_PARAMETER);
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync cMaxPages = RT_ALIGN(cMaxPages, 16);
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync if (cMaxPages > PGMPOOL_IDX_LAST)
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync cMaxPages = PGMPOOL_IDX_LAST;
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync LogRel(("PGMPool: cMaxPages=%u (u64MaxPages=%llu)\n", cMaxPages, u64MaxPages));
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync
4bfa7b58e362a1bca0628643c352c137900bf01avboxsync /** todo:
df25990f935e7fd32acd9be9a156aff8d10facf2vboxsync * We need to be much more careful with our allocation strategy here.
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync * For nested paging we don't need pool user info nor extents at all, but
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync * we can't check for nested paging here (too early during init to get a
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync * confirmation it can be used). The default for large memory configs is a
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync * bit large for shadow paging, so I've restricted the extent maximum to 8k
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync * (8k * 16 = 128k of hyper heap).
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync *
c7a00ac75c7941df2afb62e6fd7ffdf1795e6c76vboxsync * Also when large page support is enabled, we typically don't need so much,
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync * although that depends on the availability of 2 MB chunks on the host.
c7a00ac75c7941df2afb62e6fd7ffdf1795e6c76vboxsync */
c7a00ac75c7941df2afb62e6fd7ffdf1795e6c76vboxsync
c7a00ac75c7941df2afb62e6fd7ffdf1795e6c76vboxsync /** @cfgm{/PGM/Pool/MaxUsers, uint16_t, #users, MaxUsers, 32K, MaxPages*2}
c7a00ac75c7941df2afb62e6fd7ffdf1795e6c76vboxsync * The max number of shadow page user tracking records. Each shadow page has
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync * zero of other shadow pages (or CR3s) that references it, or uses it if you
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync * like. The structures describing these relationships are allocated from a
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync * fixed sized pool. This configuration variable defines the pool size.
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync */
30868e719f5a45ec4689ecb2616767cb1fd02c28vboxsync uint16_t cMaxUsers;
3609dfc9f2733f4dc836c6a6bb3745398f280fcevboxsync rc = CFGMR3QueryU16Def(pCfg, "MaxUsers", &cMaxUsers, cMaxPages * 2);
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync AssertLogRelRCReturn(rc, rc);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync AssertLogRelMsgReturn(cMaxUsers >= cMaxPages && cMaxPages <= _32K,
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync ("cMaxUsers=%u (%#x)\n", cMaxUsers, cMaxUsers), VERR_INVALID_PARAMETER);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync /** @cfgm{/PGM/Pool/MaxPhysExts, uint16_t, #extents, 16, MaxPages * 2, MIN(MaxPages*2,8192)}
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync * The max number of extents for tracking aliased guest pages.
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync uint16_t cMaxPhysExts;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync rc = CFGMR3QueryU16Def(pCfg, "MaxPhysExts", &cMaxPhysExts,
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync RT_MIN(cMaxPages * 2, 8192 /* 8Ki max as this eat too much hyper heap */));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync AssertLogRelRCReturn(rc, rc);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync AssertLogRelMsgReturn(cMaxPhysExts >= 16 && cMaxPhysExts <= PGMPOOL_IDX_LAST,
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync ("cMaxPhysExts=%u (%#x)\n", cMaxPhysExts, cMaxPhysExts), VERR_INVALID_PARAMETER);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync /** @cfgm{/PGM/Pool/ChacheEnabled, bool, true}
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * Enables or disabling caching of shadow pages. Caching means that we will try
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * reuse shadow pages instead of recreating them everything SyncCR3, SyncPT or
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * SyncPage requests one. When reusing a shadow page, we can save time
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * reconstructing it and it's children.
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync bool fCacheEnabled;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync rc = CFGMR3QueryBoolDef(pCfg, "CacheEnabled", &fCacheEnabled, true);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync AssertLogRelRCReturn(rc, rc);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync LogRel(("pgmR3PoolInit: cMaxPages=%#RX16 cMaxUsers=%#RX16 cMaxPhysExts=%#RX16 fCacheEnable=%RTbool\n",
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync cMaxPages, cMaxUsers, cMaxPhysExts, fCacheEnabled));
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync
2afbe132eb7931e0125141eabe3a48e08f1ffab5vboxsync /*
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * Allocate the data structures.
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync uint32_t cb = RT_OFFSETOF(PGMPOOL, aPages[cMaxPages]);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync cb += cMaxUsers * sizeof(PGMPOOLUSER);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync cb += cMaxPhysExts * sizeof(PGMPOOLPHYSEXT);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync PPGMPOOL pPool;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync rc = MMR3HyperAllocOnceNoRel(pVM, cb, 0, MM_TAG_PGM_POOL, (void **)&pPool);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync if (RT_FAILURE(rc))
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync return rc;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pVM->pgm.s.pPoolR3 = pPool;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pVM->pgm.s.pPoolR0 = MMHyperR3ToR0(pVM, pPool);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pVM->pgm.s.pPoolRC = MMHyperR3ToRC(pVM, pPool);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync /*
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * Initialize it.
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->pVMR3 = pVM;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->pVMR0 = pVM->pVMR0;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->pVMRC = pVM->pVMRC;
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync pPool->cMaxPages = cMaxPages;
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync pPool->cCurPages = PGMPOOL_IDX_FIRST;
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync pPool->iUserFreeHead = 0;
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync pPool->cMaxUsers = cMaxUsers;
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync PPGMPOOLUSER paUsers = (PPGMPOOLUSER)&pPool->aPages[pPool->cMaxPages];
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->paUsersR3 = paUsers;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->paUsersR0 = MMHyperR3ToR0(pVM, paUsers);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->paUsersRC = MMHyperR3ToRC(pVM, paUsers);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync for (unsigned i = 0; i < cMaxUsers; i++)
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync {
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync paUsers[i].iNext = i + 1;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync paUsers[i].iUser = NIL_PGMPOOL_IDX;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync paUsers[i].iUserTable = 0xfffffffe;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync }
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync paUsers[cMaxUsers - 1].iNext = NIL_PGMPOOL_USER_INDEX;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->iPhysExtFreeHead = 0;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->cMaxPhysExts = cMaxPhysExts;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync PPGMPOOLPHYSEXT paPhysExts = (PPGMPOOLPHYSEXT)&paUsers[cMaxUsers];
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->paPhysExtsR3 = paPhysExts;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->paPhysExtsR0 = MMHyperR3ToR0(pVM, paPhysExts);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->paPhysExtsRC = MMHyperR3ToRC(pVM, paPhysExts);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync for (unsigned i = 0; i < cMaxPhysExts; i++)
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync {
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync paPhysExts[i].iNext = i + 1;
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync paPhysExts[i].apte[0] = NIL_PGMPOOL_PHYSEXT_IDX_PTE;
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync paPhysExts[i].apte[1] = NIL_PGMPOOL_PHYSEXT_IDX_PTE;
2508d15edddcae0b79002fae3fe103d6c4836810vboxsync paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync paPhysExts[i].apte[2] = NIL_PGMPOOL_PHYSEXT_IDX_PTE;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync }
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pPool->aiHash); i++)
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aiHash[i] = NIL_PGMPOOL_IDX;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->iAgeHead = NIL_PGMPOOL_IDX;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->iAgeTail = NIL_PGMPOOL_IDX;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->fCacheEnabled = fCacheEnabled;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->pfnAccessHandlerR3 = pgmR3PoolAccessHandler;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->pszAccessHandler = "Guest Paging Access Handler";
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->HCPhysTree = 0;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync /* The NIL entry. */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync Assert(NIL_PGMPOOL_IDX == 0);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[NIL_PGMPOOL_IDX].enmKind = PGMPOOLKIND_INVALID;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync /* The Shadow 32-bit PD. (32 bits guest paging) */
2afbe132eb7931e0125141eabe3a48e08f1ffab5vboxsync pPool->aPages[PGMPOOL_IDX_PD].Core.Key = NIL_RTHCPHYS;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_PD].GCPhys = NIL_RTGCPHYS;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_PD].pvPageR3 = 0;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_PD].enmKind = PGMPOOLKIND_32BIT_PD;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_PD].idx = PGMPOOL_IDX_PD;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync /* The Shadow PDPT. */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_PDPT].Core.Key = NIL_RTHCPHYS;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_PDPT].GCPhys = NIL_RTGCPHYS;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_PDPT].pvPageR3 = 0;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_PDPT].enmKind = PGMPOOLKIND_PAE_PDPT;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_PDPT].idx = PGMPOOL_IDX_PDPT;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync /* The Shadow AMD64 CR3. */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_AMD64_CR3].Core.Key = NIL_RTHCPHYS;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_AMD64_CR3].GCPhys = NIL_RTGCPHYS;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_AMD64_CR3].pvPageR3 = 0;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_AMD64_CR3].enmKind = PGMPOOLKIND_64BIT_PML4;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_AMD64_CR3].idx = PGMPOOL_IDX_AMD64_CR3;
2afbe132eb7931e0125141eabe3a48e08f1ffab5vboxsync
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync /* The Nested Paging CR3. */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_NESTED_ROOT].Core.Key = NIL_RTHCPHYS;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_NESTED_ROOT].GCPhys = NIL_RTGCPHYS;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_NESTED_ROOT].pvPageR3 = 0;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_NESTED_ROOT].enmKind = PGMPOOLKIND_ROOT_NESTED;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[PGMPOOL_IDX_NESTED_ROOT].idx = PGMPOOL_IDX_NESTED_ROOT;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync /*
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * Set common stuff.
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync */
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync for (unsigned iPage = 1; iPage < PGMPOOL_IDX_FIRST; iPage++)
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync {
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[iPage].iNext = NIL_PGMPOOL_IDX;
4bfa7b58e362a1bca0628643c352c137900bf01avboxsync pPool->aPages[iPage].iUserHead = NIL_PGMPOOL_USER_INDEX;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[iPage].iModifiedNext = NIL_PGMPOOL_IDX;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[iPage].iModifiedPrev = NIL_PGMPOOL_IDX;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[iPage].iMonitoredNext = NIL_PGMPOOL_IDX;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[iPage].iMonitoredNext = NIL_PGMPOOL_IDX;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[iPage].iAgeNext = NIL_PGMPOOL_IDX;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync pPool->aPages[iPage].iAgePrev = NIL_PGMPOOL_IDX;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync Assert(pPool->aPages[iPage].idx == iPage);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync Assert(pPool->aPages[iPage].GCPhys == NIL_RTGCPHYS);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync Assert(!pPool->aPages[iPage].fSeenNonGlobal);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync Assert(!pPool->aPages[iPage].fMonitored);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync Assert(!pPool->aPages[iPage].fCached);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync Assert(!pPool->aPages[iPage].fZeroed);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync Assert(!pPool->aPages[iPage].fReusedFlushPending);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync }
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync#ifdef VBOX_WITH_STATISTICS
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync /*
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync * Register statistics.
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync */
1999ae03c34840fa4d712fd2e020120b2cb7182avboxsync STAM_REG(pVM, &pPool->cCurPages, STAMTYPE_U16, "/PGM/Pool/cCurPages", STAMUNIT_PAGES, "Current pool size.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->cMaxPages, STAMTYPE_U16, "/PGM/Pool/cMaxPages", STAMUNIT_PAGES, "Max pool size.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->cUsedPages, STAMTYPE_U16, "/PGM/Pool/cUsedPages", STAMUNIT_PAGES, "The number of pages currently in use.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->cUsedPagesHigh, STAMTYPE_U16_RESET, "/PGM/Pool/cUsedPagesHigh", STAMUNIT_PAGES, "The high watermark for cUsedPages.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatAlloc, STAMTYPE_PROFILE_ADV, "/PGM/Pool/Alloc", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolAlloc.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatClearAll, STAMTYPE_PROFILE, "/PGM/Pool/ClearAll", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmR3PoolClearAll.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatR3Reset, STAMTYPE_PROFILE, "/PGM/Pool/R3Reset", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmR3PoolReset.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatFlushPage, STAMTYPE_PROFILE, "/PGM/Pool/FlushPage", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolFlushPage.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatFree, STAMTYPE_PROFILE, "/PGM/Pool/Free", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolFree.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatForceFlushPage, STAMTYPE_COUNTER, "/PGM/Pool/FlushForce", STAMUNIT_OCCURENCES, "Counting explicit flushes by PGMPoolFlushPage().");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatForceFlushDirtyPage, STAMTYPE_COUNTER, "/PGM/Pool/FlushForceDirty", STAMUNIT_OCCURENCES, "Counting explicit flushes of dirty pages by PGMPoolFlushPage().");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatForceFlushReused, STAMTYPE_COUNTER, "/PGM/Pool/FlushReused", STAMUNIT_OCCURENCES, "Counting flushes for reused pages.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatZeroPage, STAMTYPE_PROFILE, "/PGM/Pool/ZeroPage", STAMUNIT_TICKS_PER_CALL, "Profiling time spent zeroing pages. Overlaps with Alloc.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->cMaxUsers, STAMTYPE_U16, "/PGM/Pool/Track/cMaxUsers", STAMUNIT_COUNT, "Max user tracking records.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->cPresent, STAMTYPE_U32, "/PGM/Pool/Track/cPresent", STAMUNIT_COUNT, "Number of present page table entries.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatTrackDeref, STAMTYPE_PROFILE, "/PGM/Pool/Track/Deref", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolTrackDeref.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatTrackFlushGCPhysPT, STAMTYPE_PROFILE, "/PGM/Pool/Track/FlushGCPhysPT", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolTrackFlushGCPhysPT.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatTrackFlushGCPhysPTs, STAMTYPE_PROFILE, "/PGM/Pool/Track/FlushGCPhysPTs", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolTrackFlushGCPhysPTs.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatTrackFlushGCPhysPTsSlow, STAMTYPE_PROFILE, "/PGM/Pool/Track/FlushGCPhysPTsSlow", STAMUNIT_TICKS_PER_CALL, "Profiling of pgmPoolTrackFlushGCPhysPTsSlow.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatTrackFlushEntry, STAMTYPE_COUNTER, "/PGM/Pool/Track/Entry/Flush", STAMUNIT_COUNT, "Nr of flushed entries.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatTrackFlushEntryKeep, STAMTYPE_COUNTER, "/PGM/Pool/Track/Entry/Update", STAMUNIT_COUNT, "Nr of updated entries.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatTrackFreeUpOneUser, STAMTYPE_COUNTER, "/PGM/Pool/Track/FreeUpOneUser", STAMUNIT_TICKS_PER_CALL, "The number of times we were out of user tracking records.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatTrackDerefGCPhys, STAMTYPE_PROFILE, "/PGM/Pool/Track/DrefGCPhys", STAMUNIT_TICKS_PER_CALL, "Profiling deref activity related tracking GC physical pages.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatTrackLinearRamSearches, STAMTYPE_COUNTER, "/PGM/Pool/Track/LinearRamSearches", STAMUNIT_OCCURENCES, "The number of times we had to do linear ram searches.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StamTrackPhysExtAllocFailures,STAMTYPE_COUNTER, "/PGM/Pool/Track/PhysExtAllocFailures", STAMUNIT_OCCURENCES, "The number of failing pgmPoolTrackPhysExtAlloc calls.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatMonitorRZ, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/RZ", STAMUNIT_TICKS_PER_CALL, "Profiling the RC/R0 access handler.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatMonitorRZEmulateInstr, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/RZ/EmulateInstr", STAMUNIT_OCCURENCES, "Times we've failed interpreting the instruction.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatMonitorRZFlushPage, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/RZ/FlushPage", STAMUNIT_TICKS_PER_CALL, "Profiling the pgmPoolFlushPage calls made from the RC/R0 access handler.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatMonitorRZFlushReinit, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/RZ/FlushReinit", STAMUNIT_OCCURENCES, "Times we've detected a page table reinit.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatMonitorRZFlushModOverflow,STAMTYPE_COUNTER, "/PGM/Pool/Monitor/RZ/FlushOverflow", STAMUNIT_OCCURENCES, "Counting flushes for pages that are modified too often.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatMonitorRZFork, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/RZ/Fork", STAMUNIT_OCCURENCES, "Times we've detected fork().");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatMonitorRZHandled, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/RZ/Handled", STAMUNIT_TICKS_PER_CALL, "Profiling the RC/R0 access we've handled (except REP STOSD).");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatMonitorRZIntrFailPatch1, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/RZ/IntrFailPatch1", STAMUNIT_OCCURENCES, "Times we've failed interpreting a patch code instruction.");
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync STAM_REG(pVM, &pPool->StatMonitorRZIntrFailPatch2, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/RZ/IntrFailPatch2", STAMUNIT_OCCURENCES, "Times we've failed interpreting a patch code instruction during flushing.");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_REG(pVM, &pPool->StatMonitorRZRepPrefix, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/RZ/RepPrefix", STAMUNIT_OCCURENCES, "The number of times we've seen rep prefixes we can't handle.");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_REG(pVM, &pPool->StatMonitorRZRepStosd, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/RZ/RepStosd", STAMUNIT_TICKS_PER_CALL, "Profiling the REP STOSD cases we've handled.");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_REG(pVM, &pPool->StatMonitorRZFaultPT, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/RZ/Fault/PT", STAMUNIT_OCCURENCES, "Nr of handled PT faults.");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_REG(pVM, &pPool->StatMonitorRZFaultPD, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/RZ/Fault/PD", STAMUNIT_OCCURENCES, "Nr of handled PD faults.");
fa8716d08ff627a8e1c14bcac56e8e3867b3f795vboxsync STAM_REG(pVM, &pPool->StatMonitorRZFaultPDPT, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/RZ/Fault/PDPT", STAMUNIT_OCCURENCES, "Nr of handled PDPT faults.");
df25990f935e7fd32acd9be9a156aff8d10facf2vboxsync STAM_REG(pVM, &pPool->StatMonitorRZFaultPML4, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/RZ/Fault/PML4", STAMUNIT_OCCURENCES, "Nr of handled PML4 faults.");
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync STAM_REG(pVM, &pPool->StatMonitorR3, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/R3", STAMUNIT_TICKS_PER_CALL, "Profiling the R3 access handler.");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_REG(pVM, &pPool->StatMonitorR3EmulateInstr, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/R3/EmulateInstr", STAMUNIT_OCCURENCES, "Times we've failed interpreting the instruction.");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_REG(pVM, &pPool->StatMonitorR3FlushPage, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/R3/FlushPage", STAMUNIT_TICKS_PER_CALL, "Profiling the pgmPoolFlushPage calls made from the R3 access handler.");
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync STAM_REG(pVM, &pPool->StatMonitorR3FlushReinit, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/R3/FlushReinit", STAMUNIT_OCCURENCES, "Times we've detected a page table reinit.");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_REG(pVM, &pPool->StatMonitorR3FlushModOverflow,STAMTYPE_COUNTER, "/PGM/Pool/Monitor/R3/FlushOverflow", STAMUNIT_OCCURENCES, "Counting flushes for pages that are modified too often.");
c7ff622115966b69b482bd2896662e40d823b22fvboxsync STAM_REG(pVM, &pPool->StatMonitorR3Fork, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/R3/Fork", STAMUNIT_OCCURENCES, "Times we've detected fork().");
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync STAM_REG(pVM, &pPool->StatMonitorR3Handled, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/R3/Handled", STAMUNIT_TICKS_PER_CALL, "Profiling the R3 access we've handled (except REP STOSD).");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_REG(pVM, &pPool->StatMonitorR3RepPrefix, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/R3/RepPrefix", STAMUNIT_OCCURENCES, "The number of times we've seen rep prefixes we can't handle.");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_REG(pVM, &pPool->StatMonitorR3RepStosd, STAMTYPE_PROFILE, "/PGM/Pool/Monitor/R3/RepStosd", STAMUNIT_TICKS_PER_CALL, "Profiling the REP STOSD cases we've handled.");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_REG(pVM, &pPool->StatMonitorR3FaultPT, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/R3/Fault/PT", STAMUNIT_OCCURENCES, "Nr of handled PT faults.");
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync STAM_REG(pVM, &pPool->StatMonitorR3FaultPD, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/R3/Fault/PD", STAMUNIT_OCCURENCES, "Nr of handled PD faults.");
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_REG(pVM, &pPool->StatMonitorR3FaultPDPT, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/R3/Fault/PDPT", STAMUNIT_OCCURENCES, "Nr of handled PDPT faults.");
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync STAM_REG(pVM, &pPool->StatMonitorR3FaultPML4, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/R3/Fault/PML4", STAMUNIT_OCCURENCES, "Nr of handled PML4 faults.");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_REG(pVM, &pPool->StatMonitorR3Async, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/R3/Async", STAMUNIT_OCCURENCES, "Times we're called in an async thread and need to flush.");
289060a0c3cb1d509f2cb01fca060796212376f6vboxsync STAM_REG(pVM, &pPool->cModifiedPages, STAMTYPE_U16, "/PGM/Pool/Monitor/cModifiedPages", STAMUNIT_PAGES, "The current cModifiedPages value.");
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync STAM_REG(pVM, &pPool->cModifiedPagesHigh, STAMTYPE_U16_RESET, "/PGM/Pool/Monitor/cModifiedPagesHigh", STAMUNIT_PAGES, "The high watermark for cModifiedPages.");
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync STAM_REG(pVM, &pPool->StatResetDirtyPages, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/Dirty/Resets", STAMUNIT_OCCURENCES, "Times we've called pgmPoolResetDirtyPages (and there were dirty page).");
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync STAM_REG(pVM, &pPool->StatDirtyPage, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/Dirty/Pages", STAMUNIT_OCCURENCES, "Times we've called pgmPoolAddDirtyPage.");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_REG(pVM, &pPool->StatDirtyPageDupFlush, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/Dirty/FlushDup", STAMUNIT_OCCURENCES, "Times we've had to flush duplicates for dirty page management.");
3f1e0eea71cabeb90529e546f16eb7aee513fde9vboxsync STAM_REG(pVM, &pPool->StatDirtyPageOverFlowFlush, STAMTYPE_COUNTER, "/PGM/Pool/Monitor/Dirty/FlushOverflow",STAMUNIT_OCCURENCES, "Times we've had to flush because of overflow.");
3f1e0eea71cabeb90529e546f16eb7aee513fde9vboxsync STAM_REG(pVM, &pPool->StatCacheHits, STAMTYPE_COUNTER, "/PGM/Pool/Cache/Hits", STAMUNIT_OCCURENCES, "The number of pgmPoolAlloc calls satisfied by the cache.");
3f1e0eea71cabeb90529e546f16eb7aee513fde9vboxsync STAM_REG(pVM, &pPool->StatCacheMisses, STAMTYPE_COUNTER, "/PGM/Pool/Cache/Misses", STAMUNIT_OCCURENCES, "The number of pgmPoolAlloc calls not statisfied by the cache.");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync 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!)");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync 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.");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_REG(pVM, &pPool->StatCacheCacheable, STAMTYPE_COUNTER, "/PGM/Pool/Cache/Cacheable", STAMUNIT_OCCURENCES, "The number of cacheable allocations.");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync STAM_REG(pVM, &pPool->StatCacheUncacheable, STAMTYPE_COUNTER, "/PGM/Pool/Cache/Uncacheable", STAMUNIT_OCCURENCES, "The number of uncacheable allocations.");
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#endif /* VBOX_WITH_STATISTICS */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#ifdef VBOX_WITH_DEBUGGER
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync /*
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * Debugger commands.
1986f56777969a25707ab214f8dd070804be666cvboxsync */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync static bool s_fRegisteredCmds = false;
1986f56777969a25707ab214f8dd070804be666cvboxsync if (!s_fRegisteredCmds)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync {
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync rc = DBGCRegisterCommands(&g_aCmds[0], RT_ELEMENTS(g_aCmds));
1986f56777969a25707ab214f8dd070804be666cvboxsync if (RT_SUCCESS(rc))
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync s_fRegisteredCmds = true;
1986f56777969a25707ab214f8dd070804be666cvboxsync }
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync#endif
1986f56777969a25707ab214f8dd070804be666cvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync return VINF_SUCCESS;
1986f56777969a25707ab214f8dd070804be666cvboxsync}
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync/**
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * Relocate the page pool data.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * @param pVM The VM handle.
1986f56777969a25707ab214f8dd070804be666cvboxsync */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsyncvoid pgmR3PoolRelocate(PVM pVM)
1986f56777969a25707ab214f8dd070804be666cvboxsync{
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync pVM->pgm.s.pPoolRC = MMHyperR3ToRC(pVM, pVM->pgm.s.pPoolR3);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync pVM->pgm.s.pPoolR3->pVMRC = pVM->pVMRC;
1986f56777969a25707ab214f8dd070804be666cvboxsync pVM->pgm.s.pPoolR3->paUsersRC = MMHyperR3ToRC(pVM, pVM->pgm.s.pPoolR3->paUsersR3);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync pVM->pgm.s.pPoolR3->paPhysExtsRC = MMHyperR3ToRC(pVM, pVM->pgm.s.pPoolR3->paPhysExtsR3);
1986f56777969a25707ab214f8dd070804be666cvboxsync int rc = PDMR3LdrGetSymbolRC(pVM, NULL, "pgmPoolAccessHandler", &pVM->pgm.s.pPoolR3->pfnAccessHandlerRC);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync AssertReleaseRC(rc);
1986f56777969a25707ab214f8dd070804be666cvboxsync /* init order hack. */
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync if (!pVM->pgm.s.pPoolR3->pfnAccessHandlerR0)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync {
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync rc = PDMR3LdrGetSymbolR0(pVM, NULL, "pgmPoolAccessHandler", &pVM->pgm.s.pPoolR3->pfnAccessHandlerR0);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync AssertReleaseRC(rc);
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync }
134a71c1528b56afe4db843ab63ec5a5b849535bvboxsync}
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync/**
0bc7c910e57c78c68e89122e2244cc073d1ef06evboxsync * Grows the shadow page pool.
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync *
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync * I.e. adds more pages to it, assuming that hasn't reached cMaxPages yet.
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync *
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync * @returns VBox status code.
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync * @param pVM The VM handle.
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync */
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsyncVMMR3DECL(int) PGMR3PoolGrow(PVM pVM)
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync{
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync PPGMPOOL pPool = pVM->pgm.s.pPoolR3;
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync AssertReturn(pPool->cCurPages < pPool->cMaxPages, VERR_PGM_POOL_MAXED_OUT_ALREADY);
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync /* With 32-bit guests and no EPT, the CR3 limits the root pages to low
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync (below 4 GB) memory. */
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync /** @todo change the pool to handle ROOT page allocations specially when
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync * required. */
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync bool fCanUseHighMemory = HWACCMIsNestedPagingActive(pVM)
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync && HWACCMGetShwPagingMode(pVM) == PGMMODE_EPT;
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync pgmLock(pVM);
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync /*
0bc7c910e57c78c68e89122e2244cc073d1ef06evboxsync * How much to grow it by?
0bc7c910e57c78c68e89122e2244cc073d1ef06evboxsync */
addc480d0d7650db6323467bbdab6c21836a2928vboxsync uint32_t cPages = pPool->cMaxPages - pPool->cCurPages;
0bc7c910e57c78c68e89122e2244cc073d1ef06evboxsync cPages = RT_MIN(PGMPOOL_CFG_MAX_GROW, cPages);
0bc7c910e57c78c68e89122e2244cc073d1ef06evboxsync LogFlow(("PGMR3PoolGrow: Growing the pool by %d (%#x) pages. fCanUseHighMemory=%RTbool\n", cPages, cPages, fCanUseHighMemory));
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync for (unsigned i = pPool->cCurPages; cPages-- > 0; i++)
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync {
3c941112ffb137d71a8e457fcc3915f2d464ed2avboxsync PPGMPOOLPAGE pPage = &pPool->aPages[i];
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync if (fCanUseHighMemory)
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync pPage->pvPageR3 = MMR3PageAlloc(pVM);
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync else
3c941112ffb137d71a8e457fcc3915f2d464ed2avboxsync pPage->pvPageR3 = MMR3PageAllocLow(pVM);
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync if (!pPage->pvPageR3)
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync {
7c9a5eca233baf6ede345ace077a00bd0b7af1efvboxsync Log(("We're out of memory!! i=%d fCanUseHighMemory=%RTbool\n", i, fCanUseHighMemory));
3c941112ffb137d71a8e457fcc3915f2d464ed2avboxsync pgmUnlock(pVM);
3c941112ffb137d71a8e457fcc3915f2d464ed2avboxsync return i ? VINF_SUCCESS : VERR_NO_PAGE_MEMORY;
3c941112ffb137d71a8e457fcc3915f2d464ed2avboxsync }
42c1972c22e09797b4b24afbd0ec114ed076c37cvboxsync pPage->Core.Key = MMPage2Phys(pVM, pPage->pvPageR3);
1986f56777969a25707ab214f8dd070804be666cvboxsync AssertFatal(pPage->Core.Key < _4G || fCanUseHighMemory);
1986f56777969a25707ab214f8dd070804be666cvboxsync pPage->GCPhys = NIL_RTGCPHYS;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync pPage->enmKind = PGMPOOLKIND_FREE;
611910c4ba57eb6db5c0d508ca7b923efd654aecvboxsync pPage->idx = pPage - &pPool->aPages[0];
6f516ad9911d9037a18778742caa955fe362f8ffvboxsync LogFlow(("PGMR3PoolGrow: insert page #%#x - %RHp\n", pPage->idx, pPage->Core.Key));
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync pPage->iNext = pPool->iFreeHead;
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync pPage->iModifiedNext = NIL_PGMPOOL_IDX;
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
4bfa7b58e362a1bca0628643c352c137900bf01avboxsync pPage->iAgeNext = NIL_PGMPOOL_IDX;
67b4b089c50d0ab8ad847dddd8c0e0812fdadc9evboxsync pPage->iAgePrev = NIL_PGMPOOL_IDX;
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync /* commit it */
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync bool fRc = RTAvloHCPhysInsert(&pPool->HCPhysTree, &pPage->Core); Assert(fRc); NOREF(fRc);
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync pPool->iFreeHead = i;
150283991b1a312acbe86c67d3420f6463b38878vboxsync pPool->cCurPages = i + 1;
150283991b1a312acbe86c67d3420f6463b38878vboxsync }
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync pgmUnlock(pVM);
150283991b1a312acbe86c67d3420f6463b38878vboxsync Assert(pPool->cCurPages <= pPool->cMaxPages);
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync return VINF_SUCCESS;
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync}
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync/**
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync * Worker used by pgmR3PoolAccessHandler when it's invoked by an async thread.
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync *
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync * @param pPool The pool.
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync * @param pPage The page.
150283991b1a312acbe86c67d3420f6463b38878vboxsync */
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsyncstatic DECLCALLBACK(void) pgmR3PoolFlushReusedPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync{
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync /* for the present this should be safe enough I think... */
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync pgmLock(pPool->pVMR3);
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync if ( pPage->fReusedFlushPending
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync && pPage->enmKind != PGMPOOLKIND_FREE)
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync pgmPoolFlushPage(pPool, pPage);
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync pgmUnlock(pPool->pVMR3);
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync}
1999ae03c34840fa4d712fd2e020120b2cb7182avboxsync
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync/**
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync * \#PF Handler callback for PT write accesses.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync *
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * The handler can not raise any faults, it's mainly for monitoring write access
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * to certain pages.
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync *
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * @returns VINF_SUCCESS if the handler has carried out the operation.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * @param pVM VM Handle.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * @param GCPhys The physical address the guest is writing to.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * @param pvPhys The HC mapping of that address.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * @param pvBuf What the guest is reading/writing.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * @param cbBuf How much it's reading/writing.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * @param enmAccessType The access type.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * @param pvUser User argument.
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync */
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsyncstatic DECLCALLBACK(int) pgmR3PoolAccessHandler(PVM pVM, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf,
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync PGMACCESSTYPE enmAccessType, void *pvUser)
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync{
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync STAM_PROFILE_START(&pVM->pgm.s.pPoolR3->StatMonitorR3, a);
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync PPGMPOOL pPool = pVM->pgm.s.pPoolR3;
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)pvUser;
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync PVMCPU pVCpu = VMMGetCpu(pVM);
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync LogFlow(("pgmR3PoolAccessHandler: GCPhys=%RGp %p:{.Core=%RHp, .idx=%d, .GCPhys=%RGp, .enmType=%d}\n",
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync GCPhys, pPage, pPage->Core.Key, pPage->idx, pPage->GCPhys, pPage->enmKind));
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync NOREF(pvBuf); NOREF(enmAccessType);
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync /*
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * We don't have to be very sophisticated about this since there are relativly few calls here.
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * However, we must try our best to detect any non-cpu accesses (disk / networking).
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync *
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync * Just to make life more interesting, we'll have to deal with the async threads too.
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync * We cannot flush a page if we're in an async thread because of REM notifications.
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync */
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync pgmLock(pVM);
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync if (PHYS_PAGE_ADDRESS(GCPhys) != PHYS_PAGE_ADDRESS(pPage->GCPhys))
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync {
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync /* Pool page changed while we were waiting for the lock; ignore. */
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync Log(("CPU%d: pgmR3PoolAccessHandler pgm pool page for %RGp changed (to %RGp) while waiting!\n", pVCpu->idCpu, PHYS_PAGE_ADDRESS(GCPhys), PHYS_PAGE_ADDRESS(pPage->GCPhys)));
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync pgmUnlock(pVM);
6e4b0f4821f335d37975004f6a7badab8bc48b6fvboxsync return VINF_PGM_HANDLER_DO_DEFAULT;
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync }
150283991b1a312acbe86c67d3420f6463b38878vboxsync
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync Assert(pPage->enmKind != PGMPOOLKIND_FREE);
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync /* @todo this code doesn't make any sense. remove the if (!pVCpu) block */
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync if (!pVCpu) /** @todo This shouldn't happen any longer, all access handlers will be called on an EMT. All ring-3 handlers, except MMIO, already own the PGM lock. @bugref{3170} */
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync {
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync Log(("pgmR3PoolAccessHandler: async thread, requesting EMT to flush the page: %p:{.Core=%RHp, .idx=%d, .GCPhys=%RGp, .enmType=%d}\n",
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync pPage, pPage->Core.Key, pPage->idx, pPage->GCPhys, pPage->enmKind));
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync STAM_COUNTER_INC(&pPool->StatMonitorR3Async);
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync if (!pPage->fReusedFlushPending)
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync {
683371bbf37760161d1b8454ce978acf89bbb04fvboxsync pgmUnlock(pVM);
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync int rc = VMR3ReqCallVoidNoWait(pPool->pVMR3, VMCPUID_ANY, (PFNRT)pgmR3PoolFlushReusedPage, 2, pPool, pPage);
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync AssertRCReturn(rc, rc);
cc723cf07e365cd40b517b9c5da4f113e9469745vboxsync pgmLock(pVM);
pPage->fReusedFlushPending = true;
pPage->cModifications += 0x1000;
}
pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhys, pvPhys, 0 /* unknown write size */);
/** @todo r=bird: making unsafe assumption about not crossing entries here! */
while (cbBuf > 4)
{
cbBuf -= 4;
pvPhys = (uint8_t *)pvPhys + 4;
GCPhys += 4;
pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhys, pvPhys, 0 /* unknown write size */);
}
STAM_PROFILE_STOP(&pPool->StatMonitorR3, a);
}
else if ( ( pPage->cModifications < 96 /* it's cheaper here. */
|| pgmPoolIsPageLocked(pPage)
)
&& cbBuf <= 4)
{
/* Clear the shadow entry. */
if (!pPage->cModifications++)
pgmPoolMonitorModifiedInsert(pPool, pPage);
/** @todo r=bird: making unsafe assumption about not crossing entries here! */
pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhys, pvPhys, 0 /* unknown write size */);
STAM_PROFILE_STOP(&pPool->StatMonitorR3, 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->StatMonitorR3, &pPool->StatMonitorR3FlushPage, a);
}
pgmUnlock(pVM);
return VINF_PGM_HANDLER_DO_DEFAULT;
}
/**
* Rendezvous callback used by pgmR3PoolClearAll that clears all shadow pages
* and all modification counters.
*
* This is only called on one of the EMTs while the other ones are waiting for
* it to complete this function.
*
* @returns VINF_SUCCESS (VBox strict status code).
* @param pVM The VM handle.
* @param pVCpu The VMCPU for the EMT we're being called on. Unused.
* @param fpvFlushRemTlb When not NULL, we'll flush the REM TLB as well.
* (This is the pvUser, so it has to be void *.)
*
*/
DECLCALLBACK(VBOXSTRICTRC) pgmR3PoolClearAllRendezvous(PVM pVM, PVMCPU pVCpu, void *fpvFlushRemTbl)
{
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
STAM_PROFILE_START(&pPool->StatClearAll, c);
NOREF(pVCpu);
pgmLock(pVM);
Log(("pgmR3PoolClearAllRendezvous: cUsedPages=%d fpvFlushRemTbl=%RTbool\n", pPool->cUsedPages, !!fpvFlushRemTbl));
/*
* Iterate all the pages until we've encountered all that are in use.
* This is a simple but not quite optimal solution.
*/
unsigned cModifiedPages = 0; NOREF(cModifiedPages);
unsigned cLeft = pPool->cUsedPages;
uint32_t iPage = pPool->cCurPages;
while (--iPage >= PGMPOOL_IDX_FIRST)
{
PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
if (pPage->GCPhys != NIL_RTGCPHYS)
{
switch (pPage->enmKind)
{
/*
* We only care about shadow page tables that reference physical memory
*/
#ifdef PGM_WITH_LARGE_PAGES
case PGMPOOLKIND_EPT_PD_FOR_PHYS: /* Large pages reference 2 MB of physical memory, so we must clear them. */
if (pPage->cPresent)
{
PX86PDPAE pShwPD = (PX86PDPAE)PGMPOOL_PAGE_2_PTR_V2(pPool->CTX_SUFF(pVM), pVCpu, pPage);
for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
{
if ( pShwPD->a[i].n.u1Present
&& pShwPD->a[i].b.u1Size)
{
Assert(!(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING));
pShwPD->a[i].u = 0;
Assert(pPage->cPresent);
pPage->cPresent--;
}
}
if (pPage->cPresent == 0)
pPage->iFirstPresent = NIL_PGMPOOL_PRESENT_INDEX;
}
goto default_case;
case PGMPOOLKIND_PAE_PD_PHYS: /* Large pages reference 2 MB of physical memory, so we must clear them. */
if (pPage->cPresent)
{
PEPTPD pShwPD = (PEPTPD)PGMPOOL_PAGE_2_PTR_V2(pPool->CTX_SUFF(pVM), pVCpu, pPage);
for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
{
Assert((pShwPD->a[i].u & UINT64_C(0xfff0000000000f80)) == 0);
if ( pShwPD->a[i].n.u1Present
&& pShwPD->a[i].b.u1Size)
{
Assert(!(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING));
pShwPD->a[i].u = 0;
Assert(pPage->cPresent);
pPage->cPresent--;
}
}
if (pPage->cPresent == 0)
pPage->iFirstPresent = NIL_PGMPOOL_PRESENT_INDEX;
}
goto default_case;
#endif /* PGM_WITH_LARGE_PAGES */
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
{
if (pPage->cPresent)
{
void *pvShw = PGMPOOL_PAGE_2_PTR_V2(pPool->CTX_SUFF(pVM), pVCpu, pPage);
STAM_PROFILE_START(&pPool->StatZeroPage, z);
#if 0
/* Useful check for leaking references; *very* expensive though. */
switch (pPage->enmKind)
{
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
{
bool fFoundFirst = false;
PPGMSHWPTPAE pPT = (PPGMSHWPTPAE)pvShw;
for (unsigned ptIndex = 0; ptIndex < RT_ELEMENTS(pPT->a); ptIndex++)
{
if (pPT->a[ptIndex].u)
{
if (!fFoundFirst)
{
AssertFatalMsg(pPage->iFirstPresent <= ptIndex, ("ptIndex = %d first present = %d\n", ptIndex, pPage->iFirstPresent));
if (pPage->iFirstPresent != ptIndex)
Log(("ptIndex = %d first present = %d\n", ptIndex, pPage->iFirstPresent));
fFoundFirst = true;
}
if (PGMSHWPTEPAE_IS_P(pPT->a[ptIndex]))
{
pgmPoolTracDerefGCPhysHint(pPool, pPage, PGMSHWPTEPAE_GET_HCPHYS(pPT->a[ptIndex]), NIL_RTGCPHYS);
if (pPage->iFirstPresent == ptIndex)
pPage->iFirstPresent = NIL_PGMPOOL_PRESENT_INDEX;
}
}
}
AssertFatalMsg(pPage->cPresent == 0, ("cPresent = %d pPage = %RGv\n", pPage->cPresent, pPage->GCPhys));
break;
}
default:
break;
}
#endif
ASMMemZeroPage(pvShw);
STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
pPage->cPresent = 0;
pPage->iFirstPresent = NIL_PGMPOOL_PRESENT_INDEX;
}
}
/* fall thru */
#ifdef PGM_WITH_LARGE_PAGES
default_case:
#endif
default:
Assert(!pPage->cModifications || ++cModifiedPages);
Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->cModifications = 0;
break;
}
if (!--cLeft)
break;
}
}
/* swipe the special pages too. */
for (iPage = PGMPOOL_IDX_FIRST_SPECIAL; iPage < PGMPOOL_IDX_FIRST; iPage++)
{
PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
if (pPage->GCPhys != NIL_RTGCPHYS)
{
Assert(!pPage->cModifications || ++cModifiedPages);
Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->cModifications = 0;
}
}
#ifndef DEBUG_michael
AssertMsg(cModifiedPages == pPool->cModifiedPages, ("%d != %d\n", cModifiedPages, pPool->cModifiedPages));
#endif
pPool->iModifiedHead = NIL_PGMPOOL_IDX;
pPool->cModifiedPages = 0;
/*
* Clear all the GCPhys links and rebuild the phys ext free list.
*/
for (PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRangesX);
pRam;
pRam = pRam->CTX_SUFF(pNext))
{
iPage = pRam->cb >> PAGE_SHIFT;
while (iPage-- > 0)
PGM_PAGE_SET_TRACKING(pVM, &pRam->aPages[iPage], 0);
}
pPool->iPhysExtFreeHead = 0;
PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
const unsigned cMaxPhysExts = pPool->cMaxPhysExts;
for (unsigned i = 0; i < cMaxPhysExts; i++)
{
paPhysExts[i].iNext = i + 1;
paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
paPhysExts[i].apte[0] = NIL_PGMPOOL_PHYSEXT_IDX_PTE;
paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
paPhysExts[i].apte[1] = NIL_PGMPOOL_PHYSEXT_IDX_PTE;
paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
paPhysExts[i].apte[2] = NIL_PGMPOOL_PHYSEXT_IDX_PTE;
}
paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
/* Reset all dirty pages to reactivate the page monitoring. */
/* Note: we must do this *after* clearing all page references and shadow page tables as there might be stale references to
* recently removed MMIO ranges around that might otherwise end up asserting in pgmPoolTracDerefGCPhysHint
*/
for (unsigned i = 0; i < RT_ELEMENTS(pPool->aDirtyPages); i++)
{
PPGMPOOLPAGE pPage;
unsigned idxPage;
if (pPool->aDirtyPages[i].uIdx == NIL_PGMPOOL_IDX)
continue;
idxPage = pPool->aDirtyPages[i].uIdx;
AssertRelease(idxPage != NIL_PGMPOOL_IDX);
pPage = &pPool->aPages[idxPage];
Assert(pPage->idx == idxPage);
Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX && pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
AssertMsg(pPage->fDirty, ("Page %RGp (slot=%d) not marked dirty!", pPage->GCPhys, i));
Log(("Reactivate dirty page %RGp\n", pPage->GCPhys));
/* First write protect the page again to catch all write accesses. (before checking for changes -> SMP) */
int rc = PGMHandlerPhysicalReset(pVM, pPage->GCPhys & PAGE_BASE_GC_MASK);
AssertRCSuccess(rc);
pPage->fDirty = false;
pPool->aDirtyPages[i].uIdx = NIL_PGMPOOL_IDX;
}
/* Clear all dirty pages. */
pPool->idxFreeDirtyPage = 0;
pPool->cDirtyPages = 0;
#endif
/* Clear the PGM_SYNC_CLEAR_PGM_POOL flag on all VCPUs to prevent redundant flushes. */
for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
pVM->aCpus[idCpu].pgm.s.fSyncFlags &= ~PGM_SYNC_CLEAR_PGM_POOL;
/* Flush job finished. */
VM_FF_CLEAR(pVM, VM_FF_PGM_POOL_FLUSH_PENDING);
pPool->cPresent = 0;
pgmUnlock(pVM);
PGM_INVL_ALL_VCPU_TLBS(pVM);
if (fpvFlushRemTbl)
for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
CPUMSetChangedFlags(&pVM->aCpus[idCpu], CPUM_CHANGED_GLOBAL_TLB_FLUSH);
STAM_PROFILE_STOP(&pPool->StatClearAll, c);
return VINF_SUCCESS;
}
/**
* Clears the shadow page pool.
*
* @param pVM The VM handle.
* @param fFlushRemTlb When set, the REM TLB is scheduled for flushing as
* well.
*/
void pgmR3PoolClearAll(PVM pVM, bool fFlushRemTlb)
{
int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, pgmR3PoolClearAllRendezvous, &fFlushRemTlb);
AssertRC(rc);
}
/**
* Protect all pgm pool page table entries to monitor writes
*
* @param pVM The VM handle.
*
* Remark: assumes the caller will flush all TLBs (!!)
*/
void pgmR3PoolWriteProtectPages(PVM pVM)
{
PGM_LOCK_ASSERT_OWNER(pVM);
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
unsigned cLeft = pPool->cUsedPages;
unsigned iPage = pPool->cCurPages;
while (--iPage >= PGMPOOL_IDX_FIRST)
{
PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
if ( pPage->GCPhys != NIL_RTGCPHYS
&& pPage->cPresent)
{
union
{
void *pv;
PX86PT pPT;
PPGMSHWPTPAE pPTPae;
PEPTPT pPTEpt;
} uShw;
uShw.pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
switch (pPage->enmKind)
{
/*
* We only care about shadow page tables.
*/
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
for (unsigned iShw = 0; iShw < RT_ELEMENTS(uShw.pPT->a); iShw++)
{
if (uShw.pPT->a[iShw].n.u1Present)
uShw.pPT->a[iShw].n.u1Write = 0;
}
break;
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
for (unsigned iShw = 0; iShw < RT_ELEMENTS(uShw.pPTPae->a); iShw++)
{
if (PGMSHWPTEPAE_IS_P(uShw.pPTPae->a[iShw]))
PGMSHWPTEPAE_SET_RO(uShw.pPTPae->a[iShw]);
}
break;
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
for (unsigned iShw = 0; iShw < RT_ELEMENTS(uShw.pPTEpt->a); iShw++)
{
if (uShw.pPTEpt->a[iShw].n.u1Present)
uShw.pPTEpt->a[iShw].n.u1Write = 0;
}
break;
default:
break;
}
if (!--cLeft)
break;
}
}
}
#ifdef VBOX_WITH_DEBUGGER
/**
* The '.pgmpoolcheck' command.
*
* @returns VBox status.
* @param pCmd Pointer to the command descriptor (as registered).
* @param pCmdHlp Pointer to command helper functions.
* @param pVM Pointer to the current VM (if any).
* @param paArgs Pointer to (readonly) array of arguments.
* @param cArgs Number of arguments in the array.
*/
static DECLCALLBACK(int) pgmR3PoolCmdCheck(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
{
DBGC_CMDHLP_REQ_VM_RET(pCmdHlp, pCmd, pVM);
DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs == 0);
uint32_t cErrors = 0;
NOREF(paArgs);
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
for (unsigned i = 0; i < pPool->cCurPages; i++)
{
PPGMPOOLPAGE pPage = &pPool->aPages[i];
bool fFirstMsg = true;
/* Todo: cover other paging modes too. */
if (pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT)
{
PPGMSHWPTPAE pShwPT = (PPGMSHWPTPAE)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
{
PX86PTPAE pGstPT;
PGMPAGEMAPLOCK LockPage;
int rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, pPage->GCPhys, (const void **)&pGstPT, &LockPage); AssertReleaseRC(rc);
/* Check if any PTEs are out of sync. */
for (unsigned j = 0; j < RT_ELEMENTS(pShwPT->a); j++)
{
if (PGMSHWPTEPAE_IS_P(pShwPT->a[j]))
{
RTHCPHYS HCPhys = NIL_RTHCPHYS;
rc = PGMPhysGCPhys2HCPhys(pPool->CTX_SUFF(pVM), pGstPT->a[j].u & X86_PTE_PAE_PG_MASK, &HCPhys);
if ( rc != VINF_SUCCESS
|| PGMSHWPTEPAE_GET_HCPHYS(pShwPT->a[j]) != HCPhys)
{
if (fFirstMsg)
{
DBGCCmdHlpPrintf(pCmdHlp, "Check pool page %RGp\n", pPage->GCPhys);
fFirstMsg = false;
}
DBGCCmdHlpPrintf(pCmdHlp, "Mismatch HCPhys: rc=%Rrc idx=%d guest %RX64 shw=%RX64 vs %RHp\n", rc, j, pGstPT->a[j].u, PGMSHWPTEPAE_GET_LOG(pShwPT->a[j]), HCPhys);
cErrors++;
}
else if ( PGMSHWPTEPAE_IS_RW(pShwPT->a[j])
&& !pGstPT->a[j].n.u1Write)
{
if (fFirstMsg)
{
DBGCCmdHlpPrintf(pCmdHlp, "Check pool page %RGp\n", pPage->GCPhys);
fFirstMsg = false;
}
DBGCCmdHlpPrintf(pCmdHlp, "Mismatch r/w gst/shw: idx=%d guest %RX64 shw=%RX64 vs %RHp\n", j, pGstPT->a[j].u, PGMSHWPTEPAE_GET_LOG(pShwPT->a[j]), HCPhys);
cErrors++;
}
}
}
PGMPhysReleasePageMappingLock(pVM, &LockPage);
}
/* Make sure this page table can't be written to from any shadow mapping. */
RTHCPHYS HCPhysPT = NIL_RTHCPHYS;
int rc = PGMPhysGCPhys2HCPhys(pPool->CTX_SUFF(pVM), pPage->GCPhys, &HCPhysPT);
AssertMsgRC(rc, ("PGMPhysGCPhys2HCPhys failed with rc=%d for %RGp\n", rc, pPage->GCPhys));
if (rc == VINF_SUCCESS)
{
for (unsigned j = 0; j < pPool->cCurPages; j++)
{
PPGMPOOLPAGE pTempPage = &pPool->aPages[j];
if (pTempPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT)
{
PPGMSHWPTPAE pShwPT2 = (PPGMSHWPTPAE)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pTempPage);
for (unsigned k = 0; k < RT_ELEMENTS(pShwPT->a); k++)
{
if ( PGMSHWPTEPAE_IS_P_RW(pShwPT2->a[k])
# ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
&& !pPage->fDirty
# endif
&& PGMSHWPTEPAE_GET_HCPHYS(pShwPT2->a[k]) == HCPhysPT)
{
if (fFirstMsg)
{
DBGCCmdHlpPrintf(pCmdHlp, "Check pool page %RGp\n", pPage->GCPhys);
fFirstMsg = false;
}
DBGCCmdHlpPrintf(pCmdHlp, "Mismatch: r/w: GCPhys=%RGp idx=%d shw %RX64 %RX64\n", pTempPage->GCPhys, k, PGMSHWPTEPAE_GET_LOG(pShwPT->a[k]), PGMSHWPTEPAE_GET_LOG(pShwPT2->a[k]));
cErrors++;
}
}
}
}
}
}
}
if (cErrors > 0)
return DBGCCmdHlpFail(pCmdHlp, pCmd, "Found %#x errors", cErrors);
return VINF_SUCCESS;
}
#endif /* VBOX_WITH_DEBUGGER */