GMMR0.cpp revision c9e2ead3626b80e3da6c393537d5d984618c1546
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/* $Id$ */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** @file
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * GMM - Global Memory Manager.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/*
f8e804dad2cc0262b6384e97c12be107cf7e19e0vboxsync * Copyright (C) 2007-2011 Oracle Corporation
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * available from http://www.virtualbox.org. This file is free software;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * you can redistribute it and/or modify it under the terms of the GNU
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * General Public License (GPL) as published by the Free Software
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** @page pg_gmm GMM - The Global Memory Manager
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * As the name indicates, this component is responsible for global memory
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * management. Currently only guest RAM is allocated from the GMM, but this
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * may change to include shadow page tables and other bits later.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Guest RAM is managed as individual pages, but allocated from the host OS
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * in chunks for reasons of portability / efficiency. To minimize the memory
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * footprint all tracking structure must be as small as possible without
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * unnecessary performance penalties.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The allocation chunks has fixed sized, the size defined at compile time
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * by the #GMM_CHUNK_SIZE \#define.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Each chunk is given an unique ID. Each page also has a unique ID. The
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * relation ship between the two IDs is:
628ddfbd43ad5365d69fddda4007598242956577vboxsync * @code
628ddfbd43ad5365d69fddda4007598242956577vboxsync * GMM_CHUNK_SHIFT = log2(GMM_CHUNK_SIZE / PAGE_SIZE);
628ddfbd43ad5365d69fddda4007598242956577vboxsync * idPage = (idChunk << GMM_CHUNK_SHIFT) | iPage;
628ddfbd43ad5365d69fddda4007598242956577vboxsync * @endcode
628ddfbd43ad5365d69fddda4007598242956577vboxsync * Where iPage is the index of the page within the chunk. This ID scheme
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * permits for efficient chunk and page lookup, but it relies on the chunk size
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * to be set at compile time. The chunks are organized in an AVL tree with their
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * IDs being the keys.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The physical address of each page in an allocation chunk is maintained by
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * the #RTR0MEMOBJ and obtained using #RTR0MemObjGetPagePhysAddr. There is no
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * need to duplicate this information (it'll cost 8-bytes per page if we did).
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * So what do we need to track per page? Most importantly we need to know
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * which state the page is in:
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * - Private - Allocated for (eventually) backing one particular VM page.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * - Shared - Readonly page that is used by one or more VMs and treated
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * as COW by PGM.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * - Free - Not used by anyone.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * For the page replacement operations (sharing, defragmenting and freeing)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * to be somewhat efficient, private pages needs to be associated with a
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * particular page in a particular VM.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Tracking the usage of shared pages is impractical and expensive, so we'll
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * settle for a reference counting system instead.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Free pages will be chained on LIFOs
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * On 64-bit systems we will use a 64-bit bitfield per page, while on 32-bit
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * systems a 32-bit bitfield will have to suffice because of address space
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * limitations. The #GMMPAGE structure shows the details.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
2f0d866e126dd288169fed591c259c1c6b4016e5vboxsync *
ae5379e3e7573369566d4628ef6c597da693cc55vboxsync * @section sec_gmm_alloc_strat Page Allocation Strategy
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * The strategy for allocating pages has to take fragmentation and shared
ae5379e3e7573369566d4628ef6c597da693cc55vboxsync * pages into account, or we may end up with with 2000 chunks with only
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * a few pages in each. Shared pages cannot easily be reallocated because
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * of the inaccurate usage accounting (see above). Private pages can be
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * reallocated by a defragmentation thread in the same manner that sharing
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * is done.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * The first approach is to manage the free pages in two sets depending on
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * whether they are mainly for the allocation of shared or private pages.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * In the initial implementation there will be almost no possibility for
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * mixing shared and private pages in the same chunk (only if we're really
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * stressed on memory), but when we implement forking of VMs and have to
400d70dc1edb0f6d2a9d3a860b1b43f0b2cdfb39vboxsync * deal with lots of COW pages it'll start getting kind of interesting.
400d70dc1edb0f6d2a9d3a860b1b43f0b2cdfb39vboxsync *
400d70dc1edb0f6d2a9d3a860b1b43f0b2cdfb39vboxsync * The sets are lists of chunks with approximately the same number of
400d70dc1edb0f6d2a9d3a860b1b43f0b2cdfb39vboxsync * free pages. Say the chunk size is 1MB, meaning 256 pages, and a set
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * consists of 16 lists. So, the first list will contain the chunks with
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * 1-7 free pages, the second covers 8-15, and so on. The chunks will be
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * moved between the lists as pages are freed up or allocated.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @section sec_gmm_costs Costs
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The per page cost in kernel space is 32-bit plus whatever RTR0MEMOBJ
54d3b0107d9bf326fe6e0de92e012c791dbb1587vboxsync * entails. In addition there is the chunk cost of approximately
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * (sizeof(RT0MEMOBJ) + sizeof(CHUNK)) / 2^CHUNK_SHIFT bytes per page.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * On Windows the per page #RTR0MEMOBJ cost is 32-bit on 32-bit windows
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * and 64-bit on 64-bit windows (a PFN_NUMBER in the MDL). So, 64-bit per page.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The cost on Linux is identical, but here it's because of sizeof(struct page *).
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @section sec_gmm_legacy Legacy Mode for Non-Tier-1 Platforms
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * In legacy mode the page source is locked user pages and not
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * #RTR0MemObjAllocPhysNC, this means that a page can only be allocated
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * by the VM that locked it. We will make no attempt at implementing
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * page sharing on these systems, just do enough to make it all work.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync * @subsection sub_gmm_locking Serializing
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync *
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync * One simple fast mutex will be employed in the initial implementation, not
77cce7691847be5aef145f31ba3f9d66fc2cf594vboxsync * two as mentioned in @ref subsec_pgmPhys_Serializing.
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync *
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync * @see @ref subsec_pgmPhys_Serializing
54d3b0107d9bf326fe6e0de92e012c791dbb1587vboxsync *
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync *
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync * @section sec_gmm_overcommit Memory Over-Commitment Management
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The GVM will have to do the system wide memory over-commitment
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * management. My current ideas are:
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync * - Per VM oc policy that indicates how much to initially commit
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync * to it and what to do in a out-of-memory situation.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * - Prevent overtaxing the host.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
9e293277b378073ce86910209a246b744b4caa2cvboxsync * There are some challenges here, the main ones are configurability and
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync * security. Should we for instance permit anyone to request 100% memory
9e293277b378073ce86910209a246b744b4caa2cvboxsync * commitment? Who should be allowed to do runtime adjustments of the
9e293277b378073ce86910209a246b744b4caa2cvboxsync * config. And how to prevent these settings from being lost when the last
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync * VM process exits? The solution is probably to have an optional root
9e293277b378073ce86910209a246b744b4caa2cvboxsync * daemon the will keep VMMR0.r0 in memory and enable the security measures.
9e293277b378073ce86910209a246b744b4caa2cvboxsync *
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync *
bdd15592ca3578b623ff588055a561f58b7e5586vboxsync *
bdd15592ca3578b623ff588055a561f58b7e5586vboxsync * @section sec_gmm_numa NUMA
bdd15592ca3578b623ff588055a561f58b7e5586vboxsync *
9e293277b378073ce86910209a246b744b4caa2cvboxsync * NUMA considerations will be designed and implemented a bit later.
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync *
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync * The preliminary guesses is that we will have to try allocate memory as
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync * close as possible to the CPUs the VM is executed on (EMT and additional CPU
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync * threads). Which means it's mostly about allocation and sharing policies.
f350b7cf96f1e2f3b0cfd34cfe8726c754f43584vboxsync * Both the scheduler and allocator interface will to supply some NUMA info
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync * and we'll need to have a way to calc access costs.
f350b7cf96f1e2f3b0cfd34cfe8726c754f43584vboxsync *
f350b7cf96f1e2f3b0cfd34cfe8726c754f43584vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
f350b7cf96f1e2f3b0cfd34cfe8726c754f43584vboxsync
f350b7cf96f1e2f3b0cfd34cfe8726c754f43584vboxsync/*******************************************************************************
f350b7cf96f1e2f3b0cfd34cfe8726c754f43584vboxsync* Header Files *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync*******************************************************************************/
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#define LOG_GROUP LOG_GROUP_GMM
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include <VBox/rawpci.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include <VBox/vmm/vm.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include <VBox/vmm/gmm.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include "GMMR0Internal.h"
f350b7cf96f1e2f3b0cfd34cfe8726c754f43584vboxsync#include <VBox/vmm/gvm.h>
551d9b8ee3568ad3e11b65ce6ef2867c36375f37vboxsync#include <VBox/vmm/pgm.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include <VBox/log.h>
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync#include <VBox/param.h>
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync#include <VBox/err.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include <iprt/asm.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include <iprt/avl.h>
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync#include <iprt/list.h>
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync#include <iprt/mem.h>
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync#include <iprt/memobj.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include <iprt/semaphore.h>
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#include <iprt/string.h>
8e0c2ca3abd721979958f95b9af73b60665478c8vboxsync#include <iprt/time.h>
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/*******************************************************************************
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync* Structures and Typedefs *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync*******************************************************************************/
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** Pointer to set of free chunks. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef struct GMMCHUNKFREESET *PGMMCHUNKFREESET;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync/** Pointer to a GMM allocation chunk. */
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsynctypedef struct GMMCHUNK *PGMMCHUNK;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The per-page tracking structure employed by the GMM.
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync *
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync * On 32-bit hosts we'll some trickery is necessary to compress all
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * the information into 32-bits. When the fSharedFree member is set,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * the 30th bit decides whether it's a free page or not.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync * Because of the different layout on 32-bit and 64-bit hosts, macros
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * are used to get and set some of the data.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef union GMMPAGE
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#if HC_ARCH_BITS == 64
39592d8ff3243f6116c4e99be391bcf30a4ad187vboxsync /** Unsigned integer view. */
39592d8ff3243f6116c4e99be391bcf30a4ad187vboxsync uint64_t u;
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync
3ca89d9d8c4fc158ba28bdf82c9cc3697625ce12vboxsync /** The common view. */
39592d8ff3243f6116c4e99be391bcf30a4ad187vboxsync struct GMMPAGECOMMON
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t uStuff1 : 32;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t uStuff2 : 30;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The page state. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t u2State : 2;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync } Common;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The view of a private page. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync struct GMMPAGEPRIVATE
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The guest page frame number. (Max addressable: 2 ^ 44 - 16) */
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync uint32_t pfn;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The GVM handle. (64K VMs) */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t hGVM : 16;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Reserved. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t u16Reserved : 14;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The page state. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t u2State : 2;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync } Private;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The view of a shared page. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync struct GMMPAGESHARED
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The host page frame number. (Max addressable: 2 ^ 44 - 16) */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t pfn;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The reference count (64K VMs). */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t cRefs : 16;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Reserved. Checksum or something? Two hGVMs for forking? */
c77e7bff89c7639353778366984d51ff165ea0e3vboxsync uint32_t u14Reserved : 14;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The page state. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t u2State : 2;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync } Shared;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The view of a free page. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync struct GMMPAGEFREE
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The index of the next page in the free list. UINT16_MAX is NIL. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint16_t iNext;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Reserved. Checksum or something? */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint16_t u16Reserved0;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Reserved. Checksum or something? */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t u30Reserved1 : 30;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The page state. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t u2State : 2;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync } Free;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else /* 32-bit */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Unsigned integer view. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t u;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The common view. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync struct GMMPAGECOMMON
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t uStuff : 30;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The page state. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t u2State : 2;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync } Common;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The view of a private page. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync struct GMMPAGEPRIVATE
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The guest page frame number. (Max addressable: 2 ^ 36) */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t pfn : 24;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The GVM handle. (127 VMs) */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t hGVM : 7;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The top page state bit, MBZ. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t fZero : 1;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync } Private;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The view of a shared page. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync struct GMMPAGESHARED
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The reference count. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t cRefs : 30;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The page state. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t u2State : 2;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync } Shared;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The view of a free page. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync struct GMMPAGEFREE
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The index of the next page in the free list. UINT16_MAX is NIL. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t iNext : 16;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Reserved. Checksum or something? */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t u14Reserved : 14;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The page state. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t u2State : 2;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync } Free;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync} GMMPAGE;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncAssertCompileSize(GMMPAGE, sizeof(RTHCUINTPTR));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** Pointer to a GMMPAGE. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef GMMPAGE *PGMMPAGE;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** @name The Page States.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @{ */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** A private page. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#define GMM_PAGE_STATE_PRIVATE 0
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** A private page - alternative value used on the 32-bit implementation.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * This will never be used on 64-bit hosts. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#define GMM_PAGE_STATE_PRIVATE_32 1
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** A shared page. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#define GMM_PAGE_STATE_SHARED 2
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** A free page. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#define GMM_PAGE_STATE_FREE 3
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** @} */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** @def GMM_PAGE_IS_PRIVATE
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns true if private, false if not.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pPage The GMM page.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#if HC_ARCH_BITS == 64
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# define GMM_PAGE_IS_PRIVATE(pPage) ( (pPage)->Common.u2State == GMM_PAGE_STATE_PRIVATE )
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# define GMM_PAGE_IS_PRIVATE(pPage) ( (pPage)->Private.fZero == 0 )
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** @def GMM_PAGE_IS_SHARED
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns true if shared, false if not.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pPage The GMM page.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#define GMM_PAGE_IS_SHARED(pPage) ( (pPage)->Common.u2State == GMM_PAGE_STATE_SHARED )
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** @def GMM_PAGE_IS_FREE
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns true if free, false if not.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pPage The GMM page.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#define GMM_PAGE_IS_FREE(pPage) ( (pPage)->Common.u2State == GMM_PAGE_STATE_FREE )
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** @def GMM_PAGE_PFN_LAST
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The last valid guest pfn range.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @remark Some of the values outside the range has special meaning,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * see GMM_PAGE_PFN_UNSHAREABLE.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#if HC_ARCH_BITS == 64
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# define GMM_PAGE_PFN_LAST UINT32_C(0xfffffff0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# define GMM_PAGE_PFN_LAST UINT32_C(0x00fffff0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncAssertCompile(GMM_PAGE_PFN_LAST == (GMM_GCPHYS_LAST >> PAGE_SHIFT));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** @def GMM_PAGE_PFN_UNSHAREABLE
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Indicates that this page isn't used for normal guest memory and thus isn't shareable.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#if HC_ARCH_BITS == 64
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# define GMM_PAGE_PFN_UNSHAREABLE UINT32_C(0xfffffff1)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# define GMM_PAGE_PFN_UNSHAREABLE UINT32_C(0x00fffff1)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncAssertCompile(GMM_PAGE_PFN_UNSHAREABLE == (GMM_GCPHYS_UNSHAREABLE >> PAGE_SHIFT));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * A GMM allocation chunk ring-3 mapping record.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * This should really be associated with a session and not a VM, but
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * it's simpler to associated with a VM and cleanup with the VM object
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * is destroyed.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef struct GMMCHUNKMAP
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The mapping object. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTR0MEMOBJ MapObj;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The VM owning the mapping. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync PGVM pGVM;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync} GMMCHUNKMAP;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** Pointer to a GMM allocation chunk mapping. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef struct GMMCHUNKMAP *PGMMCHUNKMAP;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef enum GMMCHUNKTYPE
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync GMMCHUNKTYPE_INVALID = 0,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync GMMCHUNKTYPE_NON_CONTINUOUS = 1, /* 4 kb pages */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync GMMCHUNKTYPE_CONTINUOUS = 2, /* one 2 MB continuous physical range. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync GMMCHUNKTYPE_32BIT_HACK = 0x7fffffff
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync} GMMCHUNKTYPE;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * A GMM allocation chunk.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef struct GMMCHUNK
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The AVL node core.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The Key is the chunk ID. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync AVLU32NODECORE Core;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The memory object.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Either from RTR0MemObjAllocPhysNC or RTR0MemObjLockUser depending on
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * what the host can dish up with. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTR0MEMOBJ MemObj;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Pointer to the next chunk in the free list. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync PGMMCHUNK pFreeNext;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Pointer to the previous chunk in the free list. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync PGMMCHUNK pFreePrev;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Pointer to the free set this chunk belongs to. NULL for
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * chunks with no free pages. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync PGMMCHUNKFREESET pSet;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** List node in the chunk list (GMM::ChunkList). */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTLISTNODE ListNode;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Pointer to an array of mappings. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync PGMMCHUNKMAP paMappings;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of mappings. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint16_t cMappings;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The head of the list of free pages. UINT16_MAX is the NIL value. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint16_t iFreeHead;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of free pages. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint16_t cFree;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The GVM handle of the VM that first allocated pages from this chunk, this
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * is used as a preference when there are several chunks to choose from.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * When in bound memory mode this isn't a preference any longer. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint16_t hGVM;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of private pages. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint16_t cPrivate;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of shared pages. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint16_t cShared;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Chunk type */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync GMMCHUNKTYPE enmType;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The pages. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync GMMPAGE aPages[GMM_CHUNK_SIZE >> PAGE_SHIFT];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync} GMMCHUNK;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * An allocation chunk TLB entry.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef struct GMMCHUNKTLBE
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The chunk id. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t idChunk;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Pointer to the chunk. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync PGMMCHUNK pChunk;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync} GMMCHUNKTLBE;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** Pointer to an allocation chunk TLB entry. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef GMMCHUNKTLBE *PGMMCHUNKTLBE;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** The number of entries tin the allocation chunk TLB. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#define GMM_CHUNKTLB_ENTRIES 32
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** Gets the TLB entry index for the given Chunk ID. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#define GMM_CHUNKTLB_IDX(idChunk) ( (idChunk) & (GMM_CHUNKTLB_ENTRIES - 1) )
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * An allocation chunk TLB.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef struct GMMCHUNKTLB
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The TLB entries. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync GMMCHUNKTLBE aEntries[GMM_CHUNKTLB_ENTRIES];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync} GMMCHUNKTLB;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync/** Pointer to an allocation chunk TLB. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef GMMCHUNKTLB *PGMMCHUNKTLB;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
bac59dd15f093cbb8dae97ebd8f94f94786d1439vboxsync/** The GMMCHUNK::cFree shift count. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#define GMM_CHUNK_FREE_SET_SHIFT 4
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** The GMMCHUNK::cFree mask for use when considering relinking a chunk. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#define GMM_CHUNK_FREE_SET_MASK 15
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** The number of lists in set. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#define GMM_CHUNK_FREE_SET_LISTS (GMM_CHUNK_NUM_PAGES >> GMM_CHUNK_FREE_SET_SHIFT)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * A set of free chunks.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef struct GMMCHUNKFREESET
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of free pages in the set. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint64_t cFreePages;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The generation ID for the set. This is incremented whenever
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * something is linked or unlinked from this set. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint64_t idGeneration;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Chunks ordered by increasing number of free pages. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync PGMMCHUNK apLists[GMM_CHUNK_FREE_SET_LISTS];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync} GMMCHUNKFREESET;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The GMM instance data.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef struct GMM
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Magic / eye catcher. GMM_MAGIC */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t u32Magic;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of threads waiting on the mutex. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t cMtxContenders;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The fast mutex protecting the GMM.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * More fine grained locking can be implemented later if necessary. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTSEMFASTMUTEX hMtx;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#ifdef VBOX_STRICT
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The current mutex owner. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTNATIVETHREAD hMtxOwner;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The chunk tree. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync PAVLU32NODECORE pChunks;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The chunk TLB. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync GMMCHUNKTLB ChunkTLB;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The private free set. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync GMMCHUNKFREESET Private;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The shared free set. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync GMMCHUNKFREESET Shared;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Shared module tree (global). */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** @todo separate trees for distinctly different guest OSes. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync PAVLGCPTRNODECORE pGlobalSharedModuleTree;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The fast mutex protecting the GMM cleanup.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * This is serializes VMs cleaning up their memory, so that we can
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * safely leave the primary mutex (hMtx). */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTSEMFASTMUTEX hMtxCleanup;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The chunk list. For simplifying the cleanup process. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTLISTNODE ChunkList;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The maximum number of pages we're allowed to allocate.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @gcfgm 64-bit GMM/MaxPages Direct.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @gcfgm 32-bit GMM/PctPages Relative to the number of host pages. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint64_t cMaxPages;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of pages that has been reserved.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The deal is that cReservedPages - cOverCommittedPages <= cMaxPages. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint64_t cReservedPages;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of pages that we have over-committed in reservations. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint64_t cOverCommittedPages;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of actually allocated (committed if you like) pages. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint64_t cAllocatedPages;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of pages that are shared. A subset of cAllocatedPages. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint64_t cSharedPages;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of pages that are actually shared between VMs. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint64_t cDuplicatePages;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of pages that are shared that has been left behind by
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * VMs not doing proper cleanups. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint64_t cLeftBehindSharedPages;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of allocation chunks.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * (The number of pages we've allocated from the host can be derived from this.) */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t cChunks;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of current ballooned pages. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint64_t cBalloonedPages;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The legacy allocation mode indicator.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * This is determined at initialization time. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync bool fLegacyAllocationMode;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The bound memory mode indicator.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * When set, the memory will be bound to a specific VM and never
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * shared. This is always set if fLegacyAllocationMode is set.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * (Also determined at initialization time.) */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync bool fBoundMemoryMode;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of registered VMs. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint16_t cRegisteredVMs;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The number of freed chunks ever. This is used a list generation to
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * avoid restarting the cleanup scanning when the list wasn't modified. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t cFreedChunks;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** The previous allocated Chunk ID.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Used as a hint to avoid scanning the whole bitmap. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t idChunkPrev;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** Chunk ID allocation bitmap.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Bits of allocated IDs are set, free ones are clear.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * The NIL id (0) is marked allocated. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t bmChunkId[(GMM_CHUNKID_LAST + 1 + 31) / 32];
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync} GMM;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** Pointer to the GMM instance. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsynctypedef GMM *PGMM;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** The value of GMM::u32Magic (Katsuhiro Otomo). */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#define GMM_MAGIC UINT32_C(0x19540414)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/*******************************************************************************
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync* Global Variables *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync*******************************************************************************/
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** Pointer to the GMM instance data. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic PGMM g_pGMM = NULL;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** Macro for obtaining and validating the g_pGMM pointer.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * On failure it will return from the invoking function with the specified return value.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pGMM The name of the pGMM variable.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param rc The return value on failure. Use VERR_INTERNAL_ERROR for
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * VBox status codes.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#define GMM_GET_VALID_INSTANCE(pGMM, rc) \
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync do { \
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync (pGMM) = g_pGMM; \
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync AssertPtrReturn((pGMM), (rc)); \
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync AssertMsgReturn((pGMM)->u32Magic == GMM_MAGIC, ("%p - %#x\n", (pGMM), (pGMM)->u32Magic), (rc)); \
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync } while (0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** Macro for obtaining and validating the g_pGMM pointer, void function variant.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * On failure it will return from the invoking function.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pGMM The name of the pGMM variable.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#define GMM_GET_VALID_INSTANCE_VOID(pGMM) \
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync do { \
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync (pGMM) = g_pGMM; \
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync AssertPtrReturnVoid((pGMM)); \
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync AssertMsgReturnVoid((pGMM)->u32Magic == GMM_MAGIC, ("%p - %#x\n", (pGMM), (pGMM)->u32Magic)); \
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync } while (0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** @def GMM_CHECK_SANITY_UPON_ENTERING
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Checks the sanity of the GMM instance data before making changes.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * This is macro is a stub by default and must be enabled manually in the code.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns true if sane, false if not.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pGMM The name of the pGMM variable.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#if defined(VBOX_STRICT) && 0
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# define GMM_CHECK_SANITY_UPON_ENTERING(pGMM) (gmmR0SanityCheck((pGMM), __PRETTY_FUNCTION__, __LINE__) == 0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# define GMM_CHECK_SANITY_UPON_ENTERING(pGMM) (true)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** @def GMM_CHECK_SANITY_UPON_LEAVING
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Checks the sanity of the GMM instance data after making changes.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * This is macro is a stub by default and must be enabled manually in the code.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns true if sane, false if not.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pGMM The name of the pGMM variable.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#if defined(VBOX_STRICT) && 0
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# define GMM_CHECK_SANITY_UPON_LEAVING(pGMM) (gmmR0SanityCheck((pGMM), __PRETTY_FUNCTION__, __LINE__) == 0)
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync#else
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync# define GMM_CHECK_SANITY_UPON_LEAVING(pGMM) (true)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/** @def GMM_CHECK_SANITY_IN_LOOPS
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Checks the sanity of the GMM instance in the allocation loops.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * This is macro is a stub by default and must be enabled manually in the code.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns true if sane, false if not.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pGMM The name of the pGMM variable.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#if defined(VBOX_STRICT) && 0
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# define GMM_CHECK_SANITY_IN_LOOPS(pGMM) (gmmR0SanityCheck((pGMM), __PRETTY_FUNCTION__, __LINE__) == 0)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# define GMM_CHECK_SANITY_IN_LOOPS(pGMM) (true)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/*******************************************************************************
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync* Internal Functions *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync*******************************************************************************/
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic DECLCALLBACK(int) gmmR0TermDestroyChunk(PAVLU32NODECORE pNode, void *pvGMM);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic bool gmmR0CleanupVMScanChunk(PGVM pGVM, PGMMCHUNK pChunk);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/*static*/ DECLCALLBACK(int) gmmR0CleanupVMDestroyChunk(PAVLU32NODECORE pNode, void *pvGVM);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncDECLINLINE(void) gmmR0LinkChunk(PGMMCHUNK pChunk, PGMMCHUNKFREESET pSet);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncDECLINLINE(void) gmmR0UnlinkChunk(PGMMCHUNK pChunk);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic uint32_t gmmR0SanityCheck(PGMM pGMM, const char *pszFunction, unsigned uLineNo);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic void gmmR0FreeChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic void gmmR0FreeSharedPage(PGMM pGMM, uint32_t idPage, PGMMPAGE pPage);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic int gmmR0UnmapChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic void gmmR0SharedModuleCleanup(PGMM pGMM, PGVM pGVM);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Initializes the GMM component.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * This is called when the VMMR0.r0 module is loaded and protected by the
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * loader semaphore.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns VBox status code.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncGMMR0DECL(int) GMMR0Init(void)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync LogFlow(("GMMInit:\n"));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Allocate the instance data and the locks.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync PGMM pGMM = (PGMM)RTMemAllocZ(sizeof(*pGMM));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (!pGMM)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return VERR_NO_MEMORY;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pGMM->u32Magic = GMM_MAGIC;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pGMM->ChunkTLB.aEntries); i++)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pGMM->ChunkTLB.aEntries[i].idChunk = NIL_GMM_CHUNKID;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTListInit(&pGMM->ChunkList);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync ASMBitSet(&pGMM->bmChunkId[0], NIL_GMM_CHUNKID);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync int rc = RTSemFastMutexCreate(&pGMM->hMtx);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_SUCCESS(rc))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = RTSemFastMutexCreate(&pGMM->hMtxCleanup);
c77e7bff89c7639353778366984d51ff165ea0e3vboxsync if (RT_SUCCESS(rc))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Check and see if RTR0MemObjAllocPhysNC works.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#if 0 /* later, see #3170. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTR0MEMOBJ MemObj;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = RTR0MemObjAllocPhysNC(&MemObj, _64K, NIL_RTHCPHYS);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_SUCCESS(rc))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync rc = RTR0MemObjFree(MemObj, true);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync AssertRC(rc);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else if (rc == VERR_NOT_SUPPORTED)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pGMM->fLegacyAllocationMode = pGMM->fBoundMemoryMode = true;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync SUPR0Printf("GMMR0Init: RTR0MemObjAllocPhysNC(,64K,Any) -> %d!\n", rc);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# if defined(RT_OS_WINDOWS) || (defined(RT_OS_SOLARIS) && ARCH_BITS == 64) || defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pGMM->fLegacyAllocationMode = false;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# if ARCH_BITS == 32
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /* Don't reuse possibly partial chunks because of the virtual address space limitation. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pGMM->fBoundMemoryMode = true;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pGMM->fBoundMemoryMode = false;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# else
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pGMM->fLegacyAllocationMode = true;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pGMM->fBoundMemoryMode = true;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync# endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync#endif
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * Query system page count and guess a reasonable cMaxPages value.
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pGMM->cMaxPages = UINT32_MAX; /** @todo IPRT function for query ram size and such. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync g_pGMM = pGMM;
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync LogFlow(("GMMInit: pGMM=%p fLegacyAllocationMode=%RTbool fBoundMemoryMode=%RTbool\n", pGMM, pGMM->fLegacyAllocationMode, pGMM->fBoundMemoryMode));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return VINF_SUCCESS;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTSemFastMutexDestroy(pGMM->hMtx);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pGMM->u32Magic = 0;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync RTMemFree(pGMM);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync SUPR0Printf("GMMR0Init: failed! rc=%d\n", rc);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return rc;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync}
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Terminates the GMM component.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncGMMR0DECL(void) GMMR0Term(void)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync LogFlow(("GMMTerm:\n"));
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Take care / be paranoid...
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync PGMM pGMM = g_pGMM;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (!VALID_PTR(pGMM))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (pGMM->u32Magic != GMM_MAGIC)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync SUPR0Printf("GMMR0Term: u32Magic=%#x\n", pGMM->u32Magic);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Undo what init did and free all the resources we've acquired.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* Destroy the fundamentals. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync g_pGMM = NULL;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->u32Magic = ~GMM_MAGIC;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTSemFastMutexDestroy(pGMM->hMtx);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->hMtx = NIL_RTSEMFASTMUTEX;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTSemFastMutexDestroy(pGMM->hMtxCleanup);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->hMtxCleanup = NIL_RTSEMFASTMUTEX;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* free any chunks still hanging around. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTAvlU32Destroy(&pGMM->pChunks, gmmR0TermDestroyChunk, pGMM);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* finally the instance data itself. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTMemFree(pGMM);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync LogFlow(("GMMTerm: done\n"));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * RTAvlU32Destroy callback.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @returns 0
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pNode The node to destroy.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pvGMM The GMM handle.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic DECLCALLBACK(int) gmmR0TermDestroyChunk(PAVLU32NODECORE pNode, void *pvGMM)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync PGMMCHUNK pChunk = (PGMMCHUNK)pNode;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (pChunk->cFree != (GMM_CHUNK_SIZE >> PAGE_SHIFT))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync SUPR0Printf("GMMR0Term: %p/%#x: cFree=%d cPrivate=%d cShared=%d cMappings=%d\n", pChunk,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->Core.Key, pChunk->cFree, pChunk->cPrivate, pChunk->cShared, pChunk->cMappings);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync int rc = RTR0MemObjFree(pChunk->MemObj, true /* fFreeMappings */);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (RT_FAILURE(rc))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync SUPR0Printf("GMMR0Term: %p/%#x: RTRMemObjFree(%p,true) -> %d (cMappings=%d)\n", pChunk,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->Core.Key, pChunk->MemObj, rc, pChunk->cMappings);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync AssertRC(rc);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->MemObj = NIL_RTR0MEMOBJ;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTMemFree(pChunk->paMappings);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->paMappings = NULL;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTMemFree(pChunk);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync NOREF(pvGMM);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Initializes the per-VM data for the GMM.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * This is called from within the GVMM lock (from GVMMR0CreateVM)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * and should only initialize the data members so GMMR0CleanupVM
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * can deal with them. We reserve no memory or anything here,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * that's done later in GMMR0InitVM.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pGVM Pointer to the Global VM structure.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncGMMR0DECL(void) GMMR0InitPerVMData(PGVM pGVM)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync AssertCompile(RT_SIZEOFMEMB(GVM,gmm.s) <= RT_SIZEOFMEMB(GVM,gmm.padding));
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pGVM->gmm.s.enmPolicy = GMMOCPOLICY_INVALID;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pGVM->gmm.s.enmPriority = GMMPRIORITY_INVALID;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pGVM->gmm.s.fMayAllocate = false;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync}
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync/**
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Acquires the GMM giant lock.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @returns Assert status code from RTSemFastMutexRequest.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pGMM Pointer to the GMM instance.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsyncstatic int gmmR0MutexAcquire(PGMM pGMM)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync{
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync ASMAtomicIncU32(&pGMM->cMtxContenders);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync int rc = RTSemFastMutexRequest(pGMM->hMtx);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync ASMAtomicDecU32(&pGMM->cMtxContenders);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync AssertRC(rc);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#ifdef VBOX_STRICT
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->hMtxOwner = RTThreadNativeSelf();
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#endif
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return rc;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Releases the GMM giant lock.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @returns Assert status code from RTSemFastMutexRequest.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pGMM Pointer to the GMM instance.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic int gmmR0MutexRelease(PGMM pGMM)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#ifdef VBOX_STRICT
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->hMtxOwner = NIL_RTNATIVETHREAD;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#endif
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync int rc = RTSemFastMutexRelease(pGMM->hMtx);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync AssertRC(rc);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return rc;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Yields the GMM giant lock if there is contention and a certain minimum time
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * has elapsed since we took it.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @returns @c true if the mutex was yielded, @c false if not.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pGMM Pointer to the GMM instance.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param puLockNanoTS Where the lock acquisition time stamp is kept
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * (in/out).
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsyncstatic bool gmmR0MutexYield(PGMM pGMM, uint64_t *puLockNanoTS)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * If nobody is contending the mutex, don't bother checking the time.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (ASMAtomicReadU32(&pGMM->cMtxContenders) == 0)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return false;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Don't yield if we haven't executed for at least 2 milliseconds.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync uint64_t uNanoNow = RTTimeSystemNanoTS();
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (uNanoNow - *puLockNanoTS < UINT32_C(2000000))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return false;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Yield the mutex.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#ifdef VBOX_STRICT
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->hMtxOwner = NIL_RTNATIVETHREAD;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#endif
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync ASMAtomicIncU32(&pGMM->cMtxContenders);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync int rc1 = RTSemFastMutexRelease(pGMM->hMtx); AssertRC(rc1);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync RTThreadYield();
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync int rc2 = RTSemFastMutexRequest(pGMM->hMtx); AssertRC(rc2);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *puLockNanoTS = RTTimeSystemNanoTS();
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync ASMAtomicDecU32(&pGMM->cMtxContenders);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#ifdef VBOX_STRICT
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pGMM->hMtxOwner = RTThreadNativeSelf();
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#endif
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return true;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Cleans up when a VM is terminating.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pGVM Pointer to the Global VM structure.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsyncGMMR0DECL(void) GMMR0CleanupVM(PGVM pGVM)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync{
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync LogFlow(("GMMR0CleanupVM: pGVM=%p:{.pVM=%p, .hSelf=%#x}\n", pGVM, pGVM->pVM, pGVM->hSelf));
d6c4b5eecea7735227dc41255d4e742543ddc86fvboxsync
d6c4b5eecea7735227dc41255d4e742543ddc86fvboxsync PGMM pGMM;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync GMM_GET_VALID_INSTANCE_VOID(pGMM);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#ifdef VBOX_WITH_PAGE_SHARING
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Clean up all registered shared modules first.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync gmmR0SharedModuleCleanup(pGMM, pGVM);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#endif
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync int rc = RTSemFastMutexRequest(pGMM->hMtxCleanup); AssertRC(rc);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync gmmR0MutexAcquire(pGMM);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync uint64_t uLockNanoTS = RTTimeSystemNanoTS();
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync GMM_CHECK_SANITY_UPON_ENTERING(pGMM);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * The policy is 'INVALID' until the initial reservation
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * request has been serviced.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if ( pGVM->gmm.s.enmPolicy > GMMOCPOLICY_INVALID
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync && pGVM->gmm.s.enmPolicy < GMMOCPOLICY_END)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * If it's the last VM around, we can skip walking all the chunk looking
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * for the pages owned by this VM and instead flush the whole shebang.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * This takes care of the eventuality that a VM has left shared page
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * references behind (shouldn't happen of course, but you never know).
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync Assert(pGMM->cRegisteredVMs);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pGMM->cRegisteredVMs--;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#if 0 /* disabled so it won't hide bugs. */
79fe674227c3f2b82ab22a6b7d340283b610fb83vboxsync if (!pGMM->cRegisteredVMs)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTAvlU32Destroy(&pGMM->pChunks, gmmR0CleanupVMDestroyChunk, pGMM);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync for (unsigned i = 0; i < RT_ELEMENTS(pGMM->ChunkTLB.aEntries); i++)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->ChunkTLB.aEntries[i].idChunk = NIL_GMM_CHUNKID;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->ChunkTLB.aEntries[i].pChunk = NULL;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync memset(&pGMM->Private, 0, sizeof(pGMM->Private));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync memset(&pGMM->Shared, 0, sizeof(pGMM->Shared));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync memset(&pGMM->bmChunkId[0], 0, sizeof(pGMM->bmChunkId));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync ASMBitSet(&pGMM->bmChunkId[0], NIL_GMM_CHUNKID);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->cReservedPages = 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->cOverCommittedPages = 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->cAllocatedPages = 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->cSharedPages = 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->cDuplicatePages = 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->cLeftBehindSharedPages = 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->cChunks = 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->cBalloonedPages = 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync else
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#endif
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Walk the entire pool looking for pages that belong to this VM
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * and left over mappings. (This'll only catch private pages,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * shared pages will be 'left behind'.)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync uint64_t cPrivatePages = pGVM->gmm.s.cPrivatePages; /* save */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync unsigned iCountDown = 64;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync bool fRedoFromStart;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync PGMMCHUNK pChunk;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync do
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync fRedoFromStart = false;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTListForEachReverse(&pGMM->ChunkList, pChunk, GMMCHUNK, ListNode)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if ( !gmmR0CleanupVMScanChunk(pGVM, pChunk)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync || iCountDown != 0)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync iCountDown--;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync else
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync iCountDown = 64;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync uint32_t const cFreeChunksOld = pGMM->cFreedChunks;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync fRedoFromStart = gmmR0MutexYield(pGMM, &uLockNanoTS)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync && pGMM->cFreedChunks != cFreeChunksOld;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (fRedoFromStart)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync break;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync } while (fRedoFromStart);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (pGVM->gmm.s.cPrivatePages)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync SUPR0Printf("GMMR0CleanupVM: hGVM=%#x has %#x private pages that cannot be found!\n", pGVM->hSelf, pGVM->gmm.s.cPrivatePages);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->cAllocatedPages -= cPrivatePages;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Free empty chunks.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync do
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync iCountDown = 10240;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk = pGMM->Private.apLists[RT_ELEMENTS(pGMM->Private.apLists) - 1];
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync while (pChunk)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync PGMMCHUNK pNext = pChunk->pFreeNext;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if ( pChunk->cFree == GMM_CHUNK_NUM_PAGES
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync && ( !pGMM->fBoundMemoryMode
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync || pChunk->hGVM == pGVM->hSelf))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync gmmR0FreeChunk(pGMM, pGVM, pChunk);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync iCountDown = 1;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pChunk = pNext;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (--iCountDown == 0)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync uint64_t const idGenerationOld = pGMM->Private.idGeneration;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync fRedoFromStart = gmmR0MutexYield(pGMM, &uLockNanoTS)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync && pGMM->Private.idGeneration != idGenerationOld;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (fRedoFromStart)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync break;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync iCountDown = 10240;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync } while (fRedoFromStart);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Account for shared pages that weren't freed.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (pGVM->gmm.s.cSharedPages)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Assert(pGMM->cSharedPages >= pGVM->gmm.s.cSharedPages);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync SUPR0Printf("GMMR0CleanupVM: hGVM=%#x left %#x shared pages behind!\n", pGVM->hSelf, pGVM->gmm.s.cSharedPages);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->cLeftBehindSharedPages += pGVM->gmm.s.cSharedPages;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Clean up balloon statistics in case the VM process crashed.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Assert(pGMM->cBalloonedPages >= pGVM->gmm.s.cBalloonedPages);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->cBalloonedPages -= pGVM->gmm.s.cBalloonedPages;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Update the over-commitment management statistics.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->cReservedPages -= pGVM->gmm.s.Reserved.cBasePages
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync + pGVM->gmm.s.Reserved.cFixedPages
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync + pGVM->gmm.s.Reserved.cShadowPages;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync switch (pGVM->gmm.s.enmPolicy)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync case GMMOCPOLICY_NO_OC:
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync break;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync default:
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /** @todo Update GMM->cOverCommittedPages */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync break;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /* zap the GVM data. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGVM->gmm.s.enmPolicy = GMMOCPOLICY_INVALID;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGVM->gmm.s.enmPriority = GMMPRIORITY_INVALID;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGVM->gmm.s.fMayAllocate = false;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync gmmR0MutexRelease(pGMM);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTSemFastMutexRelease(pGMM->hMtxCleanup);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync LogFlow(("GMMR0CleanupVM: returns\n"));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Scan one chunk for private pages belonging to the specified VM.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @returns @c true if a mapping was found (and freed), @c false if not.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pGVM The global VM handle.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pChunk The chunk to scan.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncstatic bool gmmR0CleanupVMScanChunk(PGVM pGVM, PGMMCHUNK pChunk)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Look for pages belonging to the VM.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * (Perform some internal checks while we're scanning.)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#ifndef VBOX_STRICT
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (pChunk->cFree != (GMM_CHUNK_SIZE >> PAGE_SHIFT))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync#endif
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync unsigned cPrivate = 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync unsigned cShared = 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync unsigned cFree = 0;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync gmmR0UnlinkChunk(pChunk); /* avoiding cFreePages updates. */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync uint16_t hGVM = pGVM->hSelf;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync unsigned iPage = (GMM_CHUNK_SIZE >> PAGE_SHIFT);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync while (iPage-- > 0)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (GMM_PAGE_IS_PRIVATE(&pChunk->aPages[iPage]))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (pChunk->aPages[iPage].Private.hGVM == hGVM)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Free the page.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * The reason for not using gmmR0FreePrivatePage here is that we
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * must *not* cause the chunk to be freed from under us - we're in
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * an AVL tree walk here.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->aPages[iPage].u = 0;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pChunk->aPages[iPage].Free.iNext = pChunk->iFreeHead;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pChunk->aPages[iPage].Free.u2State = GMM_PAGE_STATE_FREE;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pChunk->iFreeHead = iPage;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pChunk->cPrivate--;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pChunk->cFree++;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pGVM->gmm.s.cPrivatePages--;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync cFree++;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync else
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync cPrivate++;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync else if (GMM_PAGE_IS_FREE(&pChunk->aPages[iPage]))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync cFree++;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync else
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync cShared++;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync gmmR0LinkChunk(pChunk, pChunk->cShared ? &g_pGMM->Shared : &g_pGMM->Private);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Did it add up?
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (RT_UNLIKELY( pChunk->cFree != cFree
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync || pChunk->cPrivate != cPrivate
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync || pChunk->cShared != cShared))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync SUPR0Printf("gmmR0CleanupVMScanChunk: Chunk %p/%#x has bogus stats - free=%d/%d private=%d/%d shared=%d/%d\n",
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->cFree, cFree, pChunk->cPrivate, cPrivate, pChunk->cShared, cShared);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pChunk->cFree = cFree;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->cPrivate = cPrivate;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->cShared = cShared;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Look for a mapping belonging to the terminating VM.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync unsigned cMappings = pChunk->cMappings;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync bool fMappingFreed = true;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync for (unsigned i = 0; i < cMappings; i++)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (pChunk->paMappings[i].pGVM == pGVM)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync RTR0MEMOBJ MemObj = pChunk->paMappings[i].MapObj;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync cMappings--;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (i < cMappings)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->paMappings[i] = pChunk->paMappings[cMappings];
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->paMappings[cMappings].pGVM = NULL;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->paMappings[cMappings].MapObj = NIL_RTR0MEMOBJ;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync Assert(pChunk->cMappings - 1U == cMappings);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->cMappings = cMappings;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync int rc = RTR0MemObjFree(MemObj, false /* fFreeMappings (NA) */);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (RT_FAILURE(rc))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync SUPR0Printf("gmmR0CleanupVMScanChunk: %p/%#x: mapping #%x: RTRMemObjFree(%p,false) -> %d \n",
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk, pChunk->Core.Key, i, MemObj, rc);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync AssertRC(rc);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync fMappingFreed = true;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync break;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * If not in bound memory mode, we should reset the hGVM field
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * if it has our handle in it.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (pChunk->hGVM == pGVM->hSelf)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (!g_pGMM->fBoundMemoryMode)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->hGVM = NIL_GVM_HANDLE;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync else if (pChunk->cFree != GMM_CHUNK_NUM_PAGES)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync SUPR0Printf("gmmR0CleanupVMScanChunk: %p/%#x: cFree=%#x - it should be 0 in bound mode!\n",
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk, pChunk->Core.Key, pChunk->cFree);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync AssertMsgFailed(("%p/%#x: cFree=%#x - it should be 0 in bound mode!\n", pChunk, pChunk->Core.Key, pChunk->cFree));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync gmmR0UnlinkChunk(pChunk);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->cFree = GMM_CHUNK_NUM_PAGES;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync gmmR0LinkChunk(pChunk, pChunk->cShared ? &g_pGMM->Shared : &g_pGMM->Private);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return fMappingFreed;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * RTAvlU32Destroy callback for GMMR0CleanupVM.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @returns 0
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pNode The node (allocation chunk) to destroy.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pvGVM Pointer to the shared VM structure.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/*static*/ DECLCALLBACK(int) gmmR0CleanupVMDestroyChunk(PAVLU32NODECORE pNode, void *pvGVM)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync PGMMCHUNK pChunk = (PGMMCHUNK)pNode;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync PGVM pGVM = (PGVM)pvGVM;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync for (unsigned i = 0; i < pChunk->cMappings; i++)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (pChunk->paMappings[i].pGVM != pGVM)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync SUPR0Printf("gmmR0CleanupVMDestroyChunk: %p/%#x: mapping #%x: pGVM=%p expected %p\n", pChunk,
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pChunk->Core.Key, i, pChunk->paMappings[i].pGVM, pGVM);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync int rc = RTR0MemObjFree(pChunk->paMappings[i].MapObj, false /* fFreeMappings (NA) */);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (RT_FAILURE(rc))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync SUPR0Printf("gmmR0CleanupVMDestroyChunk: %p/%#x: mapping #%x: RTRMemObjFree(%p,false) -> %d \n", pChunk,
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pChunk->Core.Key, i, pChunk->paMappings[i].MapObj, rc);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync AssertRC(rc);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync int rc = RTR0MemObjFree(pChunk->MemObj, true /* fFreeMappings */);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (RT_FAILURE(rc))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync SUPR0Printf("gmmR0CleanupVMDestroyChunk: %p/%#x: RTRMemObjFree(%p,true) -> %d (cMappings=%d)\n", pChunk,
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->Core.Key, pChunk->MemObj, rc, pChunk->cMappings);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync AssertRC(rc);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pChunk->MemObj = NIL_RTR0MEMOBJ;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync RTMemFree(pChunk->paMappings);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pChunk->paMappings = NULL;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync RTMemFree(pChunk);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return 0;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync}
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync/**
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * The initial resource reservations.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync *
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * This will make memory reservations according to policy and priority. If there aren't
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * sufficient resources available to sustain the VM this function will fail and all
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * future allocations requests will fail as well.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * These are just the initial reservations made very very early during the VM creation
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * process and will be adjusted later in the GMMR0UpdateReservation call after the
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * ring-3 init has completed.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync *
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @returns VBox status code.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @retval VERR_GMM_MEMORY_RESERVATION_DECLINED
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @retval VERR_GMM_
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync *
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pVM Pointer to the shared VM structure.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param idCpu VCPU id
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param cBasePages The number of pages that may be allocated for the base RAM and ROMs.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * This does not include MMIO2 and similar.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param cShadowPages The number of pages that may be allocated for shadow paging structures.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param cFixedPages The number of pages that may be allocated for fixed objects like the
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * hyper heap, MMIO2 and similar.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param enmPolicy The OC policy to use on this VM.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param enmPriority The priority in an out-of-memory situation.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @thread The creator thread / EMT.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsyncGMMR0DECL(int) GMMR0InitialReservation(PVM pVM, VMCPUID idCpu, uint64_t cBasePages, uint32_t cShadowPages, uint32_t cFixedPages,
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync GMMOCPOLICY enmPolicy, GMMPRIORITY enmPriority)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync LogFlow(("GMMR0InitialReservation: pVM=%p cBasePages=%#llx cShadowPages=%#x cFixedPages=%#x enmPolicy=%d enmPriority=%d\n",
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pVM, cBasePages, cShadowPages, cFixedPages, enmPolicy, enmPriority));
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Validate, get basics and take the semaphore.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync PGMM pGMM;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync PGVM pGVM;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (RT_FAILURE(rc))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return rc;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync AssertReturn(cBasePages, VERR_INVALID_PARAMETER);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync AssertReturn(cShadowPages, VERR_INVALID_PARAMETER);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync AssertReturn(cFixedPages, VERR_INVALID_PARAMETER);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync AssertReturn(enmPolicy > GMMOCPOLICY_INVALID && enmPolicy < GMMOCPOLICY_END, VERR_INVALID_PARAMETER);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync AssertReturn(enmPriority > GMMPRIORITY_INVALID && enmPriority < GMMPRIORITY_END, VERR_INVALID_PARAMETER);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync gmmR0MutexAcquire(pGMM);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if ( !pGVM->gmm.s.Reserved.cBasePages
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync && !pGVM->gmm.s.Reserved.cFixedPages
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync && !pGVM->gmm.s.Reserved.cShadowPages)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Check if we can accommodate this.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /* ... later ... */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (RT_SUCCESS(rc))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Update the records.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGVM->gmm.s.Reserved.cBasePages = cBasePages;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pGVM->gmm.s.Reserved.cFixedPages = cFixedPages;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGVM->gmm.s.Reserved.cShadowPages = cShadowPages;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGVM->gmm.s.enmPolicy = enmPolicy;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGVM->gmm.s.enmPriority = enmPriority;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGVM->gmm.s.fMayAllocate = true;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGMM->cReservedPages += cBasePages + cFixedPages + cShadowPages;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pGMM->cRegisteredVMs++;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync else
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync rc = VERR_WRONG_ORDER;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync else
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync rc = VERR_INTERNAL_ERROR_5;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync gmmR0MutexRelease(pGMM);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync LogFlow(("GMMR0InitialReservation: returns %Rrc\n", rc));
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return rc;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync/**
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * VMMR0 request wrapper for GMMR0InitialReservation.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync *
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @returns see GMMR0InitialReservation.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pVM Pointer to the shared VM structure.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param idCpu VCPU id
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pReq The request packet.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsyncGMMR0DECL(int) GMMR0InitialReservationReq(PVM pVM, VMCPUID idCpu, PGMMINITIALRESERVATIONREQ pReq)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync{
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Validate input and pass it on.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync AssertPtrReturn(pVM, VERR_INVALID_POINTER);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync AssertPtrReturn(pReq, VERR_INVALID_POINTER);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return GMMR0InitialReservation(pVM, idCpu, pReq->cBasePages, pReq->cShadowPages, pReq->cFixedPages, pReq->enmPolicy, pReq->enmPriority);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync}
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync/**
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * This updates the memory reservation with the additional MMIO2 and ROM pages.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @returns VBox status code.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @retval VERR_GMM_MEMORY_RESERVATION_DECLINED
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pVM Pointer to the shared VM structure.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param idCpu VCPU id
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param cBasePages The number of pages that may be allocated for the base RAM and ROMs.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * This does not include MMIO2 and similar.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param cShadowPages The number of pages that may be allocated for shadow paging structures.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param cFixedPages The number of pages that may be allocated for fixed objects like the
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * hyper heap, MMIO2 and similar.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @thread EMT.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsyncGMMR0DECL(int) GMMR0UpdateReservation(PVM pVM, VMCPUID idCpu, uint64_t cBasePages, uint32_t cShadowPages, uint32_t cFixedPages)
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync{
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync LogFlow(("GMMR0UpdateReservation: pVM=%p cBasePages=%#llx cShadowPages=%#x cFixedPages=%#x\n",
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pVM, cBasePages, cShadowPages, cFixedPages));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Validate, get basics and take the semaphore.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync PGMM pGMM;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync PGVM pGVM;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync if (RT_FAILURE(rc))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return rc;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync AssertReturn(cBasePages, VERR_INVALID_PARAMETER);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync AssertReturn(cShadowPages, VERR_INVALID_PARAMETER);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync AssertReturn(cFixedPages, VERR_INVALID_PARAMETER);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync gmmR0MutexAcquire(pGMM);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if ( pGVM->gmm.s.Reserved.cBasePages
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync && pGVM->gmm.s.Reserved.cFixedPages
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync && pGVM->gmm.s.Reserved.cShadowPages)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync {
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * Check if we can accommodate this.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /* ... later ... */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync if (RT_SUCCESS(rc))
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync {
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Update the records.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pGMM->cReservedPages -= pGVM->gmm.s.Reserved.cBasePages
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync + pGVM->gmm.s.Reserved.cFixedPages
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync + pGVM->gmm.s.Reserved.cShadowPages;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pGMM->cReservedPages += cBasePages + cFixedPages + cShadowPages;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGVM->gmm.s.Reserved.cBasePages = cBasePages;
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync pGVM->gmm.s.Reserved.cFixedPages = cFixedPages;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync pGVM->gmm.s.Reserved.cShadowPages = cShadowPages;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync else
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync rc = VERR_WRONG_ORDER;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync }
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync else
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync rc = VERR_INTERNAL_ERROR_5;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync gmmR0MutexRelease(pGMM);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync LogFlow(("GMMR0UpdateReservation: returns %Rrc\n", rc));
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync return rc;
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync}
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync/**
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * VMMR0 request wrapper for GMMR0UpdateReservation.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync *
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @returns see GMMR0UpdateReservation.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param pVM Pointer to the shared VM structure.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * @param idCpu VCPU id
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync * @param pReq The request packet.
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsyncGMMR0DECL(int) GMMR0UpdateReservationReq(PVM pVM, VMCPUID idCpu, PGMMUPDATERESERVATIONREQ pReq)
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync{
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync /*
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync * Validate input and pass it on.
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync */
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync AssertPtrReturn(pVM, VERR_INVALID_POINTER);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync AssertPtrReturn(pReq, VERR_INVALID_POINTER);
37e7010b28a4667800196960b59cd63b5434b7d7vboxsync AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync return GMMR0UpdateReservation(pVM, idCpu, pReq->cBasePages, pReq->cShadowPages, pReq->cFixedPages);
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync}
9fbcdff887bd2d679720a8a50f5601df57b32b1bvboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Performs sanity checks on a free set.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns Error count.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pGMM Pointer to the GMM instance.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pSet Pointer to the set.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszSetName The set name.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszFunction The function from which it was called.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param uLine The line number.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic uint32_t gmmR0SanityCheckSet(PGMM pGMM, PGMMCHUNKFREESET pSet, const char *pszSetName,
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync const char *pszFunction, unsigned uLineNo)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t cErrors = 0;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /*
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Count the free pages in all the chunks and match it against pSet->cFreePages.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t cPages = 0;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync for (unsigned i = 0; i < RT_ELEMENTS(pSet->apLists); i++)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync for (PGMMCHUNK pCur = pSet->apLists[i]; pCur; pCur = pCur->pFreeNext)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** @todo check that the chunk is hash into the right set. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync cPages += pCur->cFree;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (RT_UNLIKELY(cPages != pSet->cFreePages))
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync SUPR0Printf("GMM insanity: found %#x pages in the %s set, expected %#x. (%s, line %u)\n",
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync cPages, pszSetName, pSet->cFreePages, pszFunction, uLineNo);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync cErrors++;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync }
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return cErrors;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync}
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * Performs some sanity checks on the GMM while owning lock.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @returns Error count.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync *
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pGMM Pointer to the GMM instance.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param pszFunction The function from which it is called.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync * @param uLineNo The line number.
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsyncstatic uint32_t gmmR0SanityCheck(PGMM pGMM, const char *pszFunction, unsigned uLineNo)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync{
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync uint32_t cErrors = 0;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync cErrors += gmmR0SanityCheckSet(pGMM, &pGMM->Private, "private", pszFunction, uLineNo);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync cErrors += gmmR0SanityCheckSet(pGMM, &pGMM->Shared, "shared", pszFunction, uLineNo);
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync /** @todo add more sanity checks. */
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync return cErrors;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync}
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync/**
* Looks up a chunk in the tree and fill in the TLB entry for it.
*
* This is not expected to fail and will bitch if it does.
*
* @returns Pointer to the allocation chunk, NULL if not found.
* @param pGMM Pointer to the GMM instance.
* @param idChunk The ID of the chunk to find.
* @param pTlbe Pointer to the TLB entry.
*/
static PGMMCHUNK gmmR0GetChunkSlow(PGMM pGMM, uint32_t idChunk, PGMMCHUNKTLBE pTlbe)
{
PGMMCHUNK pChunk = (PGMMCHUNK)RTAvlU32Get(&pGMM->pChunks, idChunk);
AssertMsgReturn(pChunk, ("Chunk %#x not found!\n", idChunk), NULL);
pTlbe->idChunk = idChunk;
pTlbe->pChunk = pChunk;
return pChunk;
}
/**
* Finds a allocation chunk.
*
* This is not expected to fail and will bitch if it does.
*
* @returns Pointer to the allocation chunk, NULL if not found.
* @param pGMM Pointer to the GMM instance.
* @param idChunk The ID of the chunk to find.
*/
DECLINLINE(PGMMCHUNK) gmmR0GetChunk(PGMM pGMM, uint32_t idChunk)
{
/*
* Do a TLB lookup, branch if not in the TLB.
*/
PGMMCHUNKTLBE pTlbe = &pGMM->ChunkTLB.aEntries[GMM_CHUNKTLB_IDX(idChunk)];
if ( pTlbe->idChunk != idChunk
|| !pTlbe->pChunk)
return gmmR0GetChunkSlow(pGMM, idChunk, pTlbe);
return pTlbe->pChunk;
}
/**
* Finds a page.
*
* This is not expected to fail and will bitch if it does.
*
* @returns Pointer to the page, NULL if not found.
* @param pGMM Pointer to the GMM instance.
* @param idPage The ID of the page to find.
*/
DECLINLINE(PGMMPAGE) gmmR0GetPage(PGMM pGMM, uint32_t idPage)
{
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
if (RT_LIKELY(pChunk))
return &pChunk->aPages[idPage & GMM_PAGEID_IDX_MASK];
return NULL;
}
/**
* Gets the host physical address for a page given by it's ID.
*
* @returns The host physical address or NIL_RTHCPHYS.
* @param pGMM Pointer to the GMM instance.
* @param idPage The ID of the page to find.
*/
DECLINLINE(RTHCPHYS) gmmR0GetPageHCPhys(PGMM pGMM, uint32_t idPage)
{
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
if (RT_LIKELY(pChunk))
return RTR0MemObjGetPagePhysAddr(pChunk->MemObj, idPage & GMM_PAGEID_IDX_MASK);
return NIL_RTHCPHYS;
}
/**
* Unlinks the chunk from the free list it's currently on (if any).
*
* @param pChunk The allocation chunk.
*/
DECLINLINE(void) gmmR0UnlinkChunk(PGMMCHUNK pChunk)
{
PGMMCHUNKFREESET pSet = pChunk->pSet;
if (RT_LIKELY(pSet))
{
pSet->cFreePages -= pChunk->cFree;
pSet->idGeneration++;
PGMMCHUNK pPrev = pChunk->pFreePrev;
PGMMCHUNK pNext = pChunk->pFreeNext;
if (pPrev)
pPrev->pFreeNext = pNext;
else
pSet->apLists[(pChunk->cFree - 1) >> GMM_CHUNK_FREE_SET_SHIFT] = pNext;
if (pNext)
pNext->pFreePrev = pPrev;
pChunk->pSet = NULL;
pChunk->pFreeNext = NULL;
pChunk->pFreePrev = NULL;
}
else
{
Assert(!pChunk->pFreeNext);
Assert(!pChunk->pFreePrev);
Assert(!pChunk->cFree);
}
}
/**
* Links the chunk onto the appropriate free list in the specified free set.
*
* If no free entries, it's not linked into any list.
*
* @param pChunk The allocation chunk.
* @param pSet The free set.
*/
DECLINLINE(void) gmmR0LinkChunk(PGMMCHUNK pChunk, PGMMCHUNKFREESET pSet)
{
Assert(!pChunk->pSet);
Assert(!pChunk->pFreeNext);
Assert(!pChunk->pFreePrev);
if (pChunk->cFree > 0)
{
pChunk->pSet = pSet;
pChunk->pFreePrev = NULL;
unsigned iList = (pChunk->cFree - 1) >> GMM_CHUNK_FREE_SET_SHIFT;
pChunk->pFreeNext = pSet->apLists[iList];
if (pChunk->pFreeNext)
pChunk->pFreeNext->pFreePrev = pChunk;
pSet->apLists[iList] = pChunk;
pSet->cFreePages += pChunk->cFree;
pSet->idGeneration++;
}
}
/**
* Frees a Chunk ID.
*
* @param pGMM Pointer to the GMM instance.
* @param idChunk The Chunk ID to free.
*/
static void gmmR0FreeChunkId(PGMM pGMM, uint32_t idChunk)
{
AssertReturnVoid(idChunk != NIL_GMM_CHUNKID);
AssertMsg(ASMBitTest(&pGMM->bmChunkId[0], idChunk), ("%#x\n", idChunk));
ASMAtomicBitClear(&pGMM->bmChunkId[0], idChunk);
}
/**
* Allocates a new Chunk ID.
*
* @returns The Chunk ID.
* @param pGMM Pointer to the GMM instance.
*/
static uint32_t gmmR0AllocateChunkId(PGMM pGMM)
{
AssertCompile(!((GMM_CHUNKID_LAST + 1) & 31)); /* must be a multiple of 32 */
AssertCompile(NIL_GMM_CHUNKID == 0);
/*
* Try the next sequential one.
*/
int32_t idChunk = ++pGMM->idChunkPrev;
#if 0 /* test the fallback first */
if ( idChunk <= GMM_CHUNKID_LAST
&& idChunk > NIL_GMM_CHUNKID
&& !ASMAtomicBitTestAndSet(&pVMM->bmChunkId[0], idChunk))
return idChunk;
#endif
/*
* Scan sequentially from the last one.
*/
if ( (uint32_t)idChunk < GMM_CHUNKID_LAST
&& idChunk > NIL_GMM_CHUNKID)
{
idChunk = ASMBitNextClear(&pGMM->bmChunkId[0], GMM_CHUNKID_LAST + 1, idChunk);
if (idChunk > NIL_GMM_CHUNKID)
{
AssertMsgReturn(!ASMAtomicBitTestAndSet(&pGMM->bmChunkId[0], idChunk), ("%#x\n", idChunk), NIL_GMM_CHUNKID);
return pGMM->idChunkPrev = idChunk;
}
}
/*
* Ok, scan from the start.
* We're not racing anyone, so there is no need to expect failures or have restart loops.
*/
idChunk = ASMBitFirstClear(&pGMM->bmChunkId[0], GMM_CHUNKID_LAST + 1);
AssertMsgReturn(idChunk > NIL_GMM_CHUNKID, ("%#x\n", idChunk), NIL_GVM_HANDLE);
AssertMsgReturn(!ASMAtomicBitTestAndSet(&pGMM->bmChunkId[0], idChunk), ("%#x\n", idChunk), NIL_GMM_CHUNKID);
return pGMM->idChunkPrev = idChunk;
}
/**
* Registers a new chunk of memory.
*
* This is called by both gmmR0AllocateOneChunk and GMMR0SeedChunk. The caller
* must own the global lock.
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance.
* @param pSet Pointer to the set.
* @param MemObj The memory object for the chunk.
* @param hGVM The affinity of the chunk. NIL_GVM_HANDLE for no
* affinity.
* @param enmChunkType Chunk type (continuous or non-continuous)
* @param ppChunk Chunk address (out)
*/
static int gmmR0RegisterChunk(PGMM pGMM, PGMMCHUNKFREESET pSet, RTR0MEMOBJ MemObj, uint16_t hGVM, GMMCHUNKTYPE enmChunkType, PGMMCHUNK *ppChunk = NULL)
{
Assert(hGVM != NIL_GVM_HANDLE || pGMM->fBoundMemoryMode);
int rc;
PGMMCHUNK pChunk = (PGMMCHUNK)RTMemAllocZ(sizeof(*pChunk));
if (pChunk)
{
/*
* Initialize it.
*/
pChunk->MemObj = MemObj;
pChunk->cFree = GMM_CHUNK_NUM_PAGES;
pChunk->hGVM = hGVM;
pChunk->iFreeHead = 0;
pChunk->enmType = enmChunkType;
for (unsigned iPage = 0; iPage < RT_ELEMENTS(pChunk->aPages) - 1; iPage++)
{
pChunk->aPages[iPage].Free.u2State = GMM_PAGE_STATE_FREE;
pChunk->aPages[iPage].Free.iNext = iPage + 1;
}
pChunk->aPages[RT_ELEMENTS(pChunk->aPages) - 1].Free.u2State = GMM_PAGE_STATE_FREE;
pChunk->aPages[RT_ELEMENTS(pChunk->aPages) - 1].Free.iNext = UINT16_MAX;
/*
* Allocate a Chunk ID and insert it into the tree.
* This has to be done behind the mutex of course.
*/
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
pChunk->Core.Key = gmmR0AllocateChunkId(pGMM);
if ( pChunk->Core.Key != NIL_GMM_CHUNKID
&& pChunk->Core.Key <= GMM_CHUNKID_LAST
&& RTAvlU32Insert(&pGMM->pChunks, &pChunk->Core))
{
pGMM->cChunks++;
RTListAppend(&pGMM->ChunkList, &pChunk->ListNode);
gmmR0LinkChunk(pChunk, pSet);
LogFlow(("gmmR0RegisterChunk: pChunk=%p id=%#x cChunks=%d\n", pChunk, pChunk->Core.Key, pGMM->cChunks));
if (ppChunk)
*ppChunk = pChunk;
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
return VINF_SUCCESS;
}
/* bail out */
rc = VERR_INTERNAL_ERROR;
}
else
rc = VERR_INTERNAL_ERROR_5;
RTMemFree(pChunk);
}
else
rc = VERR_NO_MEMORY;
return rc;
}
/**
* Allocate one new chunk and add it to the specified free set.
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance.
* @param pSet Pointer to the set.
* @param hGVM The affinity of the new chunk.
* @param enmChunkType Chunk type (continuous or non-continuous)
* @param ppChunk Chunk address (out)
*
* @remarks Called without owning the mutex.
*/
static int gmmR0AllocateOneChunk(PGMM pGMM, PGMMCHUNKFREESET pSet, uint16_t hGVM, GMMCHUNKTYPE enmChunkType, PGMMCHUNK *ppChunk = NULL)
{
/*
* Allocate the memory.
*/
RTR0MEMOBJ MemObj;
int rc;
AssertCompile(GMM_CHUNK_SIZE == _2M);
AssertReturn(enmChunkType == GMMCHUNKTYPE_NON_CONTINUOUS || enmChunkType == GMMCHUNKTYPE_CONTINUOUS, VERR_INVALID_PARAMETER);
/* Leave the lock temporarily as the allocation might take long. */
gmmR0MutexRelease(pGMM);
if (enmChunkType == GMMCHUNKTYPE_NON_CONTINUOUS)
rc = RTR0MemObjAllocPhysNC(&MemObj, GMM_CHUNK_SIZE, NIL_RTHCPHYS);
else
rc = RTR0MemObjAllocPhysEx(&MemObj, GMM_CHUNK_SIZE, NIL_RTHCPHYS, GMM_CHUNK_SIZE);
int rc2 = gmmR0MutexAcquire(pGMM);
AssertRCReturn(rc2, rc2);
if (RT_SUCCESS(rc))
{
rc = gmmR0RegisterChunk(pGMM, pSet, MemObj, hGVM, enmChunkType, ppChunk);
if (RT_FAILURE(rc))
RTR0MemObjFree(MemObj, false /* fFreeMappings */);
}
/** @todo Check that RTR0MemObjAllocPhysNC always returns VERR_NO_MEMORY on
* allocation failure. */
return rc;
}
/**
* Attempts to allocate more pages until the requested amount is met.
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM The calling VM.
* @param pSet Pointer to the free set to grow.
* @param cPages The number of pages needed.
*
* @remarks Called owning the mutex, but will leave it temporarily while
* allocating the memory!
*/
static int gmmR0AllocateMoreChunks(PGMM pGMM, PGVM pGVM, PGMMCHUNKFREESET pSet, uint32_t cPages)
{
Assert(!pGMM->fLegacyAllocationMode);
if (!GMM_CHECK_SANITY_IN_LOOPS(pGMM))
return VERR_INTERNAL_ERROR_4;
if (!pGMM->fBoundMemoryMode)
{
/*
* Try steal free chunks from the other set first. (Only take 100% free chunks.)
*/
PGMMCHUNKFREESET pOtherSet = pSet == &pGMM->Private ? &pGMM->Shared : &pGMM->Private;
while ( pSet->cFreePages < cPages
&& pOtherSet->cFreePages >= GMM_CHUNK_NUM_PAGES)
{
PGMMCHUNK pChunk = pOtherSet->apLists[GMM_CHUNK_FREE_SET_LISTS - 1];
while (pChunk && pChunk->cFree != GMM_CHUNK_NUM_PAGES)
pChunk = pChunk->pFreeNext;
if (!pChunk)
break;
gmmR0UnlinkChunk(pChunk);
gmmR0LinkChunk(pChunk, pSet);
}
/*
* If we need still more pages, allocate new chunks.
* Note! We will leave the mutex while doing the allocation,
*/
while (pSet->cFreePages < cPages)
{
int rc = gmmR0AllocateOneChunk(pGMM, pSet, pGVM->hSelf, GMMCHUNKTYPE_NON_CONTINUOUS);
if (RT_FAILURE(rc))
return rc;
if (!GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
return VERR_INTERNAL_ERROR_5;
}
}
else
{
/*
* The memory is bound to the VM allocating it, so we have to count
* the free pages carefully as well as making sure we brand them with
* our VM handle.
*
* Note! We will leave the mutex while doing the allocation,
*/
uint16_t const hGVM = pGVM->hSelf;
for (;;)
{
/* Count and see if we've reached the goal. */
uint32_t cPagesFound = 0;
for (unsigned i = 0; i < RT_ELEMENTS(pSet->apLists); i++)
for (PGMMCHUNK pCur = pSet->apLists[i]; pCur; pCur = pCur->pFreeNext)
if (pCur->hGVM == hGVM)
{
cPagesFound += pCur->cFree;
if (cPagesFound >= cPages)
break;
}
if (cPagesFound >= cPages)
break;
/* Allocate more. */
int rc = gmmR0AllocateOneChunk(pGMM, pSet, hGVM, GMMCHUNKTYPE_NON_CONTINUOUS);
if (RT_FAILURE(rc))
return rc;
if (!GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
return VERR_INTERNAL_ERROR_5;
}
}
return VINF_SUCCESS;
}
/**
* Allocates one private page.
*
* Worker for gmmR0AllocatePages.
*
* @param pGMM Pointer to the GMM instance data.
* @param hGVM The GVM handle of the VM requesting memory.
* @param pChunk The chunk to allocate it from.
* @param pPageDesc The page descriptor.
*/
static void gmmR0AllocatePage(PGMM pGMM, uint32_t hGVM, PGMMCHUNK pChunk, PGMMPAGEDESC pPageDesc)
{
/* update the chunk stats. */
if (pChunk->hGVM == NIL_GVM_HANDLE)
pChunk->hGVM = hGVM;
Assert(pChunk->cFree);
pChunk->cFree--;
pChunk->cPrivate++;
/* unlink the first free page. */
const uint32_t iPage = pChunk->iFreeHead;
AssertReleaseMsg(iPage < RT_ELEMENTS(pChunk->aPages), ("%d\n", iPage));
PGMMPAGE pPage = &pChunk->aPages[iPage];
Assert(GMM_PAGE_IS_FREE(pPage));
pChunk->iFreeHead = pPage->Free.iNext;
Log3(("A pPage=%p iPage=%#x/%#x u2State=%d iFreeHead=%#x iNext=%#x\n",
pPage, iPage, (pChunk->Core.Key << GMM_CHUNKID_SHIFT) | iPage,
pPage->Common.u2State, pChunk->iFreeHead, pPage->Free.iNext));
/* make the page private. */
pPage->u = 0;
AssertCompile(GMM_PAGE_STATE_PRIVATE == 0);
pPage->Private.hGVM = hGVM;
AssertCompile(NIL_RTHCPHYS >= GMM_GCPHYS_LAST);
AssertCompile(GMM_GCPHYS_UNSHAREABLE >= GMM_GCPHYS_LAST);
if (pPageDesc->HCPhysGCPhys <= GMM_GCPHYS_LAST)
pPage->Private.pfn = pPageDesc->HCPhysGCPhys >> PAGE_SHIFT;
else
pPage->Private.pfn = GMM_PAGE_PFN_UNSHAREABLE; /* unshareable / unassigned - same thing. */
/* update the page descriptor. */
pPageDesc->HCPhysGCPhys = RTR0MemObjGetPagePhysAddr(pChunk->MemObj, iPage);
Assert(pPageDesc->HCPhysGCPhys != NIL_RTHCPHYS);
pPageDesc->idPage = (pChunk->Core.Key << GMM_CHUNKID_SHIFT) | iPage;
pPageDesc->idSharedPage = NIL_GMM_PAGEID;
}
/**
* Common worker for GMMR0AllocateHandyPages and GMMR0AllocatePages.
*
* @returns VBox status code:
* @retval VINF_SUCCESS on success.
* @retval VERR_GMM_SEED_ME if seeding via GMMR0SeedChunk or
* gmmR0AllocateMoreChunks is necessary.
* @retval VERR_GMM_HIT_GLOBAL_LIMIT if we've exhausted the available pages.
* @retval VERR_GMM_HIT_VM_ACCOUNT_LIMIT if we've hit the VM account limit,
* that is we're trying to allocate more than we've reserved.
*
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the shared VM structure.
* @param cPages The number of pages to allocate.
* @param paPages Pointer to the page descriptors.
* See GMMPAGEDESC for details on what is expected on input.
* @param enmAccount The account to charge.
*/
static int gmmR0AllocatePages(PGMM pGMM, PGVM pGVM, uint32_t cPages, PGMMPAGEDESC paPages, GMMACCOUNT enmAccount)
{
/*
* Check allocation limits.
*/
if (RT_UNLIKELY(pGMM->cAllocatedPages + cPages > pGMM->cMaxPages))
return VERR_GMM_HIT_GLOBAL_LIMIT;
switch (enmAccount)
{
case GMMACCOUNT_BASE:
if (RT_UNLIKELY(pGVM->gmm.s.Allocated.cBasePages + pGVM->gmm.s.cBalloonedPages + cPages > pGVM->gmm.s.Reserved.cBasePages))
{
Log(("gmmR0AllocatePages:Base: Reserved=%#llx Allocated+Ballooned+Requested=%#llx+%#llx+%#x!\n",
pGVM->gmm.s.Reserved.cBasePages, pGVM->gmm.s.Allocated.cBasePages, pGVM->gmm.s.cBalloonedPages, cPages));
return VERR_GMM_HIT_VM_ACCOUNT_LIMIT;
}
break;
case GMMACCOUNT_SHADOW:
if (RT_UNLIKELY(pGVM->gmm.s.Allocated.cShadowPages + cPages > pGVM->gmm.s.Reserved.cShadowPages))
{
Log(("gmmR0AllocatePages:Shadow: Reserved=%#llx Allocated+Requested=%#llx+%#x!\n",
pGVM->gmm.s.Reserved.cShadowPages, pGVM->gmm.s.Allocated.cShadowPages, cPages));
return VERR_GMM_HIT_VM_ACCOUNT_LIMIT;
}
break;
case GMMACCOUNT_FIXED:
if (RT_UNLIKELY(pGVM->gmm.s.Allocated.cFixedPages + cPages > pGVM->gmm.s.Reserved.cFixedPages))
{
Log(("gmmR0AllocatePages:Fixed: Reserved=%#llx Allocated+Requested=%#llx+%#x!\n",
pGVM->gmm.s.Reserved.cFixedPages, pGVM->gmm.s.Allocated.cFixedPages, cPages));
return VERR_GMM_HIT_VM_ACCOUNT_LIMIT;
}
break;
default:
AssertMsgFailedReturn(("enmAccount=%d\n", enmAccount), VERR_INTERNAL_ERROR);
}
/*
* Check if we need to allocate more memory or not. In bound memory mode this
* is a bit extra work but it's easier to do it upfront than bailing out later.
*/
PGMMCHUNKFREESET pSet = &pGMM->Private;
if (pSet->cFreePages < cPages)
return VERR_GMM_SEED_ME;
if (pGMM->fBoundMemoryMode)
{
uint16_t hGVM = pGVM->hSelf;
uint32_t cPagesFound = 0;
for (unsigned i = 0; i < RT_ELEMENTS(pSet->apLists); i++)
for (PGMMCHUNK pCur = pSet->apLists[i]; pCur; pCur = pCur->pFreeNext)
if (pCur->hGVM == hGVM)
{
cPagesFound += pCur->cFree;
if (cPagesFound >= cPages)
break;
}
if (cPagesFound < cPages)
return VERR_GMM_SEED_ME;
}
/*
* Pick the pages.
* Try make some effort keeping VMs sharing private chunks.
*/
uint16_t hGVM = pGVM->hSelf;
uint32_t iPage = 0;
/* first round, pick from chunks with an affinity to the VM. */
for (unsigned i = 0; i < RT_ELEMENTS(pSet->apLists) && iPage < cPages; i++)
{
PGMMCHUNK pCurFree = NULL;
PGMMCHUNK pCur = pSet->apLists[i];
while (pCur && iPage < cPages)
{
PGMMCHUNK pNext = pCur->pFreeNext;
if ( pCur->hGVM == hGVM
&& pCur->cFree < GMM_CHUNK_NUM_PAGES)
{
gmmR0UnlinkChunk(pCur);
for (; pCur->cFree && iPage < cPages; iPage++)
gmmR0AllocatePage(pGMM, hGVM, pCur, &paPages[iPage]);
gmmR0LinkChunk(pCur, pSet);
}
pCur = pNext;
}
}
if (iPage < cPages)
{
/* second round, pick pages from the 100% empty chunks we just skipped above. */
PGMMCHUNK pCurFree = NULL;
PGMMCHUNK pCur = pSet->apLists[RT_ELEMENTS(pSet->apLists) - 1];
while (pCur && iPage < cPages)
{
PGMMCHUNK pNext = pCur->pFreeNext;
if ( pCur->cFree == GMM_CHUNK_NUM_PAGES
&& ( pCur->hGVM == hGVM
|| !pGMM->fBoundMemoryMode))
{
gmmR0UnlinkChunk(pCur);
for (; pCur->cFree && iPage < cPages; iPage++)
gmmR0AllocatePage(pGMM, hGVM, pCur, &paPages[iPage]);
gmmR0LinkChunk(pCur, pSet);
}
pCur = pNext;
}
}
if ( iPage < cPages
&& !pGMM->fBoundMemoryMode)
{
/* third round, disregard affinity. */
unsigned i = RT_ELEMENTS(pSet->apLists);
while (i-- > 0 && iPage < cPages)
{
PGMMCHUNK pCurFree = NULL;
PGMMCHUNK pCur = pSet->apLists[i];
while (pCur && iPage < cPages)
{
PGMMCHUNK pNext = pCur->pFreeNext;
if ( pCur->cFree > GMM_CHUNK_NUM_PAGES / 2
&& cPages >= GMM_CHUNK_NUM_PAGES / 2)
pCur->hGVM = hGVM; /* change chunk affinity */
gmmR0UnlinkChunk(pCur);
for (; pCur->cFree && iPage < cPages; iPage++)
gmmR0AllocatePage(pGMM, hGVM, pCur, &paPages[iPage]);
gmmR0LinkChunk(pCur, pSet);
pCur = pNext;
}
}
}
/*
* Update the account.
*/
switch (enmAccount)
{
case GMMACCOUNT_BASE: pGVM->gmm.s.Allocated.cBasePages += iPage; break;
case GMMACCOUNT_SHADOW: pGVM->gmm.s.Allocated.cShadowPages += iPage; break;
case GMMACCOUNT_FIXED: pGVM->gmm.s.Allocated.cFixedPages += iPage; break;
default:
AssertMsgFailedReturn(("enmAccount=%d\n", enmAccount), VERR_INTERNAL_ERROR);
}
pGVM->gmm.s.cPrivatePages += iPage;
pGMM->cAllocatedPages += iPage;
AssertMsgReturn(iPage == cPages, ("%u != %u\n", iPage, cPages), VERR_INTERNAL_ERROR);
/*
* Check if we've reached some threshold and should kick one or two VMs and tell
* them to inflate their balloons a bit more... later.
*/
return VINF_SUCCESS;
}
/**
* Updates the previous allocations and allocates more pages.
*
* The handy pages are always taken from the 'base' memory account.
* The allocated pages are not cleared and will contains random garbage.
*
* @returns VBox status code:
* @retval VINF_SUCCESS on success.
* @retval VERR_NOT_OWNER if the caller is not an EMT.
* @retval VERR_GMM_PAGE_NOT_FOUND if one of the pages to update wasn't found.
* @retval VERR_GMM_PAGE_NOT_PRIVATE if one of the pages to update wasn't a
* private page.
* @retval VERR_GMM_PAGE_NOT_SHARED if one of the pages to update wasn't a
* shared page.
* @retval VERR_GMM_NOT_PAGE_OWNER if one of the pages to be updated wasn't
* owned by the VM.
* @retval VERR_GMM_SEED_ME if seeding via GMMR0SeedChunk is necessary.
* @retval VERR_GMM_HIT_GLOBAL_LIMIT if we've exhausted the available pages.
* @retval VERR_GMM_HIT_VM_ACCOUNT_LIMIT if we've hit the VM account limit,
* that is we're trying to allocate more than we've reserved.
*
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param cPagesToUpdate The number of pages to update (starting from the head).
* @param cPagesToAlloc The number of pages to allocate (starting from the head).
* @param paPages The array of page descriptors.
* See GMMPAGEDESC for details on what is expected on input.
* @thread EMT.
*/
GMMR0DECL(int) GMMR0AllocateHandyPages(PVM pVM, VMCPUID idCpu, uint32_t cPagesToUpdate, uint32_t cPagesToAlloc, PGMMPAGEDESC paPages)
{
LogFlow(("GMMR0AllocateHandyPages: pVM=%p cPagesToUpdate=%#x cPagesToAlloc=%#x paPages=%p\n",
pVM, cPagesToUpdate, cPagesToAlloc, paPages));
/*
* Validate, get basics and take the semaphore.
* (This is a relatively busy path, so make predictions where possible.)
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
PGVM pGVM;
int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
if (RT_FAILURE(rc))
return rc;
AssertPtrReturn(paPages, VERR_INVALID_PARAMETER);
AssertMsgReturn( (cPagesToUpdate && cPagesToUpdate < 1024)
|| (cPagesToAlloc && cPagesToAlloc < 1024),
("cPagesToUpdate=%#x cPagesToAlloc=%#x\n", cPagesToUpdate, cPagesToAlloc),
VERR_INVALID_PARAMETER);
unsigned iPage = 0;
for (; iPage < cPagesToUpdate; iPage++)
{
AssertMsgReturn( ( paPages[iPage].HCPhysGCPhys <= GMM_GCPHYS_LAST
&& !(paPages[iPage].HCPhysGCPhys & PAGE_OFFSET_MASK))
|| paPages[iPage].HCPhysGCPhys == NIL_RTHCPHYS
|| paPages[iPage].HCPhysGCPhys == GMM_GCPHYS_UNSHAREABLE,
("#%#x: %RHp\n", iPage, paPages[iPage].HCPhysGCPhys),
VERR_INVALID_PARAMETER);
AssertMsgReturn( paPages[iPage].idPage <= GMM_PAGEID_LAST
/*|| paPages[iPage].idPage == NIL_GMM_PAGEID*/,
("#%#x: %#x\n", iPage, paPages[iPage].idPage), VERR_INVALID_PARAMETER);
AssertMsgReturn( paPages[iPage].idPage <= GMM_PAGEID_LAST
/*|| paPages[iPage].idSharedPage == NIL_GMM_PAGEID*/,
("#%#x: %#x\n", iPage, paPages[iPage].idSharedPage), VERR_INVALID_PARAMETER);
}
for (; iPage < cPagesToAlloc; iPage++)
{
AssertMsgReturn(paPages[iPage].HCPhysGCPhys == NIL_RTHCPHYS, ("#%#x: %RHp\n", iPage, paPages[iPage].HCPhysGCPhys), VERR_INVALID_PARAMETER);
AssertMsgReturn(paPages[iPage].idPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idPage), VERR_INVALID_PARAMETER);
AssertMsgReturn(paPages[iPage].idSharedPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idSharedPage), VERR_INVALID_PARAMETER);
}
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
/* No allocations before the initial reservation has been made! */
if (RT_LIKELY( pGVM->gmm.s.Reserved.cBasePages
&& pGVM->gmm.s.Reserved.cFixedPages
&& pGVM->gmm.s.Reserved.cShadowPages))
{
/*
* Perform the updates.
* Stop on the first error.
*/
for (iPage = 0; iPage < cPagesToUpdate; iPage++)
{
if (paPages[iPage].idPage != NIL_GMM_PAGEID)
{
PGMMPAGE pPage = gmmR0GetPage(pGMM, paPages[iPage].idPage);
if (RT_LIKELY(pPage))
{
if (RT_LIKELY(GMM_PAGE_IS_PRIVATE(pPage)))
{
if (RT_LIKELY(pPage->Private.hGVM == pGVM->hSelf))
{
AssertCompile(NIL_RTHCPHYS > GMM_GCPHYS_LAST && GMM_GCPHYS_UNSHAREABLE > GMM_GCPHYS_LAST);
if (RT_LIKELY(paPages[iPage].HCPhysGCPhys <= GMM_GCPHYS_LAST))
pPage->Private.pfn = paPages[iPage].HCPhysGCPhys >> PAGE_SHIFT;
else if (paPages[iPage].HCPhysGCPhys == GMM_GCPHYS_UNSHAREABLE)
pPage->Private.pfn = GMM_PAGE_PFN_UNSHAREABLE;
/* else: NIL_RTHCPHYS nothing */
paPages[iPage].idPage = NIL_GMM_PAGEID;
paPages[iPage].HCPhysGCPhys = NIL_RTHCPHYS;
}
else
{
Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not owner! hGVM=%#x hSelf=%#x\n",
iPage, paPages[iPage].idPage, pPage->Private.hGVM, pGVM->hSelf));
rc = VERR_GMM_NOT_PAGE_OWNER;
break;
}
}
else
{
Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not private! %.*Rhxs (type %d)\n", iPage, paPages[iPage].idPage, sizeof(*pPage), pPage, pPage->Common.u2State));
rc = VERR_GMM_PAGE_NOT_PRIVATE;
break;
}
}
else
{
Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not found! (private)\n", iPage, paPages[iPage].idPage));
rc = VERR_GMM_PAGE_NOT_FOUND;
break;
}
}
if (paPages[iPage].idSharedPage != NIL_GMM_PAGEID)
{
PGMMPAGE pPage = gmmR0GetPage(pGMM, paPages[iPage].idSharedPage);
if (RT_LIKELY(pPage))
{
if (RT_LIKELY(GMM_PAGE_IS_SHARED(pPage)))
{
AssertCompile(NIL_RTHCPHYS > GMM_GCPHYS_LAST && GMM_GCPHYS_UNSHAREABLE > GMM_GCPHYS_LAST);
Assert(pPage->Shared.cRefs);
Assert(pGVM->gmm.s.cSharedPages);
Assert(pGVM->gmm.s.Allocated.cBasePages);
Log(("GMMR0AllocateHandyPages: free shared page %x cRefs=%d\n", paPages[iPage].idSharedPage, pPage->Shared.cRefs));
pGVM->gmm.s.cSharedPages--;
pGVM->gmm.s.Allocated.cBasePages--;
if (!--pPage->Shared.cRefs)
{
gmmR0FreeSharedPage(pGMM, paPages[iPage].idSharedPage, pPage);
}
else
{
Assert(pGMM->cDuplicatePages);
pGMM->cDuplicatePages--;
}
paPages[iPage].idSharedPage = NIL_GMM_PAGEID;
}
else
{
Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not shared!\n", iPage, paPages[iPage].idSharedPage));
rc = VERR_GMM_PAGE_NOT_SHARED;
break;
}
}
else
{
Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not found! (shared)\n", iPage, paPages[iPage].idSharedPage));
rc = VERR_GMM_PAGE_NOT_FOUND;
break;
}
}
}
/*
* Join paths with GMMR0AllocatePages for the allocation.
* Note! gmmR0AllocateMoreChunks may leave the protection of the mutex!
*/
while (RT_SUCCESS(rc))
{
rc = gmmR0AllocatePages(pGMM, pGVM, cPagesToAlloc, paPages, GMMACCOUNT_BASE);
if ( rc != VERR_GMM_SEED_ME
|| pGMM->fLegacyAllocationMode)
break;
rc = gmmR0AllocateMoreChunks(pGMM, pGVM, &pGMM->Private, cPagesToAlloc);
}
}
else
rc = VERR_WRONG_ORDER;
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_INTERNAL_ERROR_5;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR0AllocateHandyPages: returns %Rrc\n", rc));
return rc;
}
/**
* Allocate one or more pages.
*
* This is typically used for ROMs and MMIO2 (VRAM) during VM creation.
* The allocated pages are not cleared and will contains random garbage.
*
* @returns VBox status code:
* @retval VINF_SUCCESS on success.
* @retval VERR_NOT_OWNER if the caller is not an EMT.
* @retval VERR_GMM_SEED_ME if seeding via GMMR0SeedChunk is necessary.
* @retval VERR_GMM_HIT_GLOBAL_LIMIT if we've exhausted the available pages.
* @retval VERR_GMM_HIT_VM_ACCOUNT_LIMIT if we've hit the VM account limit,
* that is we're trying to allocate more than we've reserved.
*
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param cPages The number of pages to allocate.
* @param paPages Pointer to the page descriptors.
* See GMMPAGEDESC for details on what is expected on input.
* @param enmAccount The account to charge.
*
* @thread EMT.
*/
GMMR0DECL(int) GMMR0AllocatePages(PVM pVM, VMCPUID idCpu, uint32_t cPages, PGMMPAGEDESC paPages, GMMACCOUNT enmAccount)
{
LogFlow(("GMMR0AllocatePages: pVM=%p cPages=%#x paPages=%p enmAccount=%d\n", pVM, cPages, paPages, enmAccount));
/*
* Validate, get basics and take the semaphore.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
PGVM pGVM;
int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
if (RT_FAILURE(rc))
return rc;
AssertPtrReturn(paPages, VERR_INVALID_PARAMETER);
AssertMsgReturn(enmAccount > GMMACCOUNT_INVALID && enmAccount < GMMACCOUNT_END, ("%d\n", enmAccount), VERR_INVALID_PARAMETER);
AssertMsgReturn(cPages > 0 && cPages < RT_BIT(32 - PAGE_SHIFT), ("%#x\n", cPages), VERR_INVALID_PARAMETER);
for (unsigned iPage = 0; iPage < cPages; iPage++)
{
AssertMsgReturn( paPages[iPage].HCPhysGCPhys == NIL_RTHCPHYS
|| paPages[iPage].HCPhysGCPhys == GMM_GCPHYS_UNSHAREABLE
|| ( enmAccount == GMMACCOUNT_BASE
&& paPages[iPage].HCPhysGCPhys <= GMM_GCPHYS_LAST
&& !(paPages[iPage].HCPhysGCPhys & PAGE_OFFSET_MASK)),
("#%#x: %RHp enmAccount=%d\n", iPage, paPages[iPage].HCPhysGCPhys, enmAccount),
VERR_INVALID_PARAMETER);
AssertMsgReturn(paPages[iPage].idPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idPage), VERR_INVALID_PARAMETER);
AssertMsgReturn(paPages[iPage].idSharedPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idSharedPage), VERR_INVALID_PARAMETER);
}
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
/* No allocations before the initial reservation has been made! */
if (RT_LIKELY( pGVM->gmm.s.Reserved.cBasePages
&& pGVM->gmm.s.Reserved.cFixedPages
&& pGVM->gmm.s.Reserved.cShadowPages))
{
/*
* gmmR0AllocatePages seed loop.
* Note! gmmR0AllocateMoreChunks may leave the protection of the mutex!
*/
while (RT_SUCCESS(rc))
{
rc = gmmR0AllocatePages(pGMM, pGVM, cPages, paPages, enmAccount);
if ( rc != VERR_GMM_SEED_ME
|| pGMM->fLegacyAllocationMode)
break;
rc = gmmR0AllocateMoreChunks(pGMM, pGVM, &pGMM->Private, cPages);
}
}
else
rc = VERR_WRONG_ORDER;
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_INTERNAL_ERROR_5;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR0AllocatePages: returns %Rrc\n", rc));
return rc;
}
/**
* VMMR0 request wrapper for GMMR0AllocatePages.
*
* @returns see GMMR0AllocatePages.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param pReq The request packet.
*/
GMMR0DECL(int) GMMR0AllocatePagesReq(PVM pVM, VMCPUID idCpu, PGMMALLOCATEPAGESREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pVM, VERR_INVALID_POINTER);
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq >= RT_UOFFSETOF(GMMALLOCATEPAGESREQ, aPages[0]),
("%#x < %#x\n", pReq->Hdr.cbReq, RT_UOFFSETOF(GMMALLOCATEPAGESREQ, aPages[0])),
VERR_INVALID_PARAMETER);
AssertMsgReturn(pReq->Hdr.cbReq == RT_UOFFSETOF(GMMALLOCATEPAGESREQ, aPages[pReq->cPages]),
("%#x != %#x\n", pReq->Hdr.cbReq, RT_UOFFSETOF(GMMALLOCATEPAGESREQ, aPages[pReq->cPages])),
VERR_INVALID_PARAMETER);
return GMMR0AllocatePages(pVM, idCpu, pReq->cPages, &pReq->aPages[0], pReq->enmAccount);
}
/**
* Allocate a large page to represent guest RAM
*
* The allocated pages are not cleared and will contains random garbage.
*
* @returns VBox status code:
* @retval VINF_SUCCESS on success.
* @retval VERR_NOT_OWNER if the caller is not an EMT.
* @retval VERR_GMM_SEED_ME if seeding via GMMR0SeedChunk is necessary.
* @retval VERR_GMM_HIT_GLOBAL_LIMIT if we've exhausted the available pages.
* @retval VERR_GMM_HIT_VM_ACCOUNT_LIMIT if we've hit the VM account limit,
* that is we're trying to allocate more than we've reserved.
* @returns see GMMR0AllocatePages.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param cbPage Large page size
*/
GMMR0DECL(int) GMMR0AllocateLargePage(PVM pVM, VMCPUID idCpu, uint32_t cbPage, uint32_t *pIdPage, RTHCPHYS *pHCPhys)
{
LogFlow(("GMMR0AllocateLargePage: pVM=%p cbPage=%x\n", pVM, cbPage));
AssertReturn(cbPage == GMM_CHUNK_SIZE, VERR_INVALID_PARAMETER);
AssertPtrReturn(pIdPage, VERR_INVALID_PARAMETER);
AssertPtrReturn(pHCPhys, VERR_INVALID_PARAMETER);
/*
* Validate, get basics and take the semaphore.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
PGVM pGVM;
int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
if (RT_FAILURE(rc))
return rc;
/* Not supported in legacy mode where we allocate the memory in ring 3 and lock it in ring 0. */
if (pGMM->fLegacyAllocationMode)
return VERR_NOT_SUPPORTED;
*pHCPhys = NIL_RTHCPHYS;
*pIdPage = NIL_GMM_PAGEID;
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
const unsigned cPages = (GMM_CHUNK_SIZE >> PAGE_SHIFT);
PGMMCHUNK pChunk;
GMMPAGEDESC PageDesc;
if (RT_UNLIKELY(pGVM->gmm.s.Allocated.cBasePages + pGVM->gmm.s.cBalloonedPages + cPages > pGVM->gmm.s.Reserved.cBasePages))
{
Log(("GMMR0AllocateLargePage: Reserved=%#llx Allocated+Requested=%#llx+%#x!\n",
pGVM->gmm.s.Reserved.cBasePages, pGVM->gmm.s.Allocated.cBasePages, cPages));
gmmR0MutexRelease(pGMM);
return VERR_GMM_HIT_VM_ACCOUNT_LIMIT;
}
/* Allocate a new continuous chunk. */
rc = gmmR0AllocateOneChunk(pGMM, &pGMM->Private, pGVM->hSelf, GMMCHUNKTYPE_CONTINUOUS, &pChunk);
if (RT_FAILURE(rc))
{
gmmR0MutexRelease(pGMM);
return rc;
}
/* Unlink the new chunk from the free list. */
gmmR0UnlinkChunk(pChunk);
/* Allocate all pages. */
gmmR0AllocatePage(pGMM, pGVM->hSelf, pChunk, &PageDesc);
/* Return the first page as we'll use the whole chunk as one big page. */
*pIdPage = PageDesc.idPage;
*pHCPhys = PageDesc.HCPhysGCPhys;
for (unsigned i = 1; i < cPages; i++)
gmmR0AllocatePage(pGMM, pGVM->hSelf, pChunk, &PageDesc);
/* Update accounting. */
pGVM->gmm.s.Allocated.cBasePages += cPages;
pGVM->gmm.s.cPrivatePages += cPages;
pGMM->cAllocatedPages += cPages;
gmmR0LinkChunk(pChunk, &pGMM->Private);
}
else
rc = VERR_INTERNAL_ERROR_5;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR0AllocateLargePage: returns %Rrc\n", rc));
return rc;
}
/**
* Free a large page
*
* @returns VBox status code:
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param idPage Large page id
*/
GMMR0DECL(int) GMMR0FreeLargePage(PVM pVM, VMCPUID idCpu, uint32_t idPage)
{
LogFlow(("GMMR0FreeLargePage: pVM=%p idPage=%x\n", pVM, idPage));
/*
* Validate, get basics and take the semaphore.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
PGVM pGVM;
int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
if (RT_FAILURE(rc))
return rc;
/* Not supported in legacy mode where we allocate the memory in ring 3 and lock it in ring 0. */
if (pGMM->fLegacyAllocationMode)
return VERR_NOT_SUPPORTED;
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
const unsigned cPages = (GMM_CHUNK_SIZE >> PAGE_SHIFT);
if (RT_UNLIKELY(pGVM->gmm.s.Allocated.cBasePages < cPages))
{
Log(("GMMR0FreeLargePage: allocated=%#llx cPages=%#x!\n", pGVM->gmm.s.Allocated.cBasePages, cPages));
gmmR0MutexRelease(pGMM);
return VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH;
}
PGMMPAGE pPage = gmmR0GetPage(pGMM, idPage);
if (RT_LIKELY( pPage
&& GMM_PAGE_IS_PRIVATE(pPage)))
{
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
Assert(pChunk);
Assert(pChunk->cFree < GMM_CHUNK_NUM_PAGES);
Assert(pChunk->cPrivate > 0);
/* Release the memory immediately. */
gmmR0FreeChunk(pGMM, NULL, pChunk);
/* Update accounting. */
pGVM->gmm.s.Allocated.cBasePages -= cPages;
pGVM->gmm.s.cPrivatePages -= cPages;
pGMM->cAllocatedPages -= cPages;
}
else
rc = VERR_GMM_PAGE_NOT_FOUND;
}
else
rc = VERR_INTERNAL_ERROR_5;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR0FreeLargePage: returns %Rrc\n", rc));
return rc;
}
/**
* VMMR0 request wrapper for GMMR0FreeLargePage.
*
* @returns see GMMR0FreeLargePage.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param pReq The request packet.
*/
GMMR0DECL(int) GMMR0FreeLargePageReq(PVM pVM, VMCPUID idCpu, PGMMFREELARGEPAGEREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pVM, VERR_INVALID_POINTER);
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(GMMFREEPAGESREQ),
("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(GMMFREEPAGESREQ)),
VERR_INVALID_PARAMETER);
return GMMR0FreeLargePage(pVM, idCpu, pReq->idPage);
}
/**
* Frees a chunk, giving it back to the host OS.
*
* @param pGMM Pointer to the GMM instance.
* @param pGVM This is set when called from GMMR0CleanupVM so we can
* unmap and free the chunk in one go.
* @param pChunk The chunk to free.
*/
static void gmmR0FreeChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk)
{
Assert(pChunk->Core.Key != NIL_GMM_CHUNKID);
/*
* Cleanup hack! Unmap the chunk from the callers address space.
*/
if ( pChunk->cMappings
&& pGVM)
gmmR0UnmapChunk(pGMM, pGVM, pChunk);
/*
* If there are current mappings of the chunk, then request the
* VMs to unmap them. Reposition the chunk in the free list so
* it won't be a likely candidate for allocations.
*/
if (pChunk->cMappings)
{
/** @todo R0 -> VM request */
/* The chunk can be mapped by more than one VM if fBoundMemoryMode is false! */
Log(("gmmR0FreeChunk: chunk still has %d mappings; don't free!\n", pChunk->cMappings));
}
else
{
/*
* Try free the memory object.
*/
int rc = RTR0MemObjFree(pChunk->MemObj, false /* fFreeMappings */);
if (RT_SUCCESS(rc))
{
pChunk->MemObj = NIL_RTR0MEMOBJ;
/*
* Unlink it from everywhere.
*/
gmmR0UnlinkChunk(pChunk);
RTListNodeRemove(&pChunk->ListNode);
PAVLU32NODECORE pCore = RTAvlU32Remove(&pGMM->pChunks, pChunk->Core.Key);
Assert(pCore == &pChunk->Core); NOREF(pCore);
PGMMCHUNKTLBE pTlbe = &pGMM->ChunkTLB.aEntries[GMM_CHUNKTLB_IDX(pChunk->Core.Key)];
if (pTlbe->pChunk == pChunk)
{
pTlbe->idChunk = NIL_GMM_CHUNKID;
pTlbe->pChunk = NULL;
}
Assert(pGMM->cChunks > 0);
pGMM->cChunks--;
/*
* Free the Chunk ID and struct.
*/
gmmR0FreeChunkId(pGMM, pChunk->Core.Key);
pChunk->Core.Key = NIL_GMM_CHUNKID;
RTMemFree(pChunk->paMappings);
pChunk->paMappings = NULL;
RTMemFree(pChunk);
pGMM->cFreedChunks++;
}
else
AssertRC(rc);
}
}
/**
* Free page worker.
*
* The caller does all the statistic decrementing, we do all the incrementing.
*
* @param pGMM Pointer to the GMM instance data.
* @param pChunk Pointer to the chunk this page belongs to.
* @param idPage The Page ID.
* @param pPage Pointer to the page.
*/
static void gmmR0FreePageWorker(PGMM pGMM, PGMMCHUNK pChunk, uint32_t idPage, PGMMPAGE pPage)
{
Log3(("F pPage=%p iPage=%#x/%#x u2State=%d iFreeHead=%#x\n",
pPage, pPage - &pChunk->aPages[0], idPage, pPage->Common.u2State, pChunk->iFreeHead)); NOREF(idPage);
/*
* Put the page on the free list.
*/
pPage->u = 0;
pPage->Free.u2State = GMM_PAGE_STATE_FREE;
Assert(pChunk->iFreeHead < RT_ELEMENTS(pChunk->aPages) || pChunk->iFreeHead == UINT16_MAX);
pPage->Free.iNext = pChunk->iFreeHead;
pChunk->iFreeHead = pPage - &pChunk->aPages[0];
/*
* Update statistics (the cShared/cPrivate stats are up to date already),
* and relink the chunk if necessary.
*/
if ((pChunk->cFree & GMM_CHUNK_FREE_SET_MASK) == 0)
{
gmmR0UnlinkChunk(pChunk);
pChunk->cFree++;
gmmR0LinkChunk(pChunk, pChunk->cShared ? &pGMM->Shared : &pGMM->Private);
}
else
{
pChunk->cFree++;
pChunk->pSet->cFreePages++;
/*
* If the chunk becomes empty, consider giving memory back to the host OS.
*
* The current strategy is to try give it back if there are other chunks
* in this free list, meaning if there are at least 240 free pages in this
* category. Note that since there are probably mappings of the chunk,
* it won't be freed up instantly, which probably screws up this logic
* a bit...
*/
if (RT_UNLIKELY( pChunk->cFree == GMM_CHUNK_NUM_PAGES
&& pChunk->pFreeNext
&& pChunk->pFreePrev
&& !pGMM->fLegacyAllocationMode))
gmmR0FreeChunk(pGMM, NULL, pChunk);
}
}
/**
* Frees a shared page, the page is known to exist and be valid and such.
*
* @param pGMM Pointer to the GMM instance.
* @param idPage The Page ID
* @param pPage The page structure.
*/
DECLINLINE(void) gmmR0FreeSharedPage(PGMM pGMM, uint32_t idPage, PGMMPAGE pPage)
{
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
Assert(pChunk);
Assert(pChunk->cFree < GMM_CHUNK_NUM_PAGES);
Assert(pChunk->cShared > 0);
Assert(pGMM->cSharedPages > 0);
Assert(pGMM->cAllocatedPages > 0);
Assert(!pPage->Shared.cRefs);
pChunk->cShared--;
pGMM->cAllocatedPages--;
pGMM->cSharedPages--;
gmmR0FreePageWorker(pGMM, pChunk, idPage, pPage);
}
#ifdef VBOX_WITH_PAGE_SHARING
/**
* Converts a private page to a shared page, the page is known to exist and be valid and such.
*
* @param pGMM Pointer to the GMM instance.
* @param pGVM Pointer to the GVM instance.
* @param HCPhys Host physical address
* @param idPage The Page ID
* @param pPage The page structure.
*/
DECLINLINE(void) gmmR0ConvertToSharedPage(PGMM pGMM, PGVM pGVM, RTHCPHYS HCPhys, uint32_t idPage, PGMMPAGE pPage)
{
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
Assert(pChunk);
Assert(pChunk->cFree < GMM_CHUNK_NUM_PAGES);
Assert(GMM_PAGE_IS_PRIVATE(pPage));
pChunk->cPrivate--;
pChunk->cShared++;
pGMM->cSharedPages++;
pGVM->gmm.s.cSharedPages++;
pGVM->gmm.s.cPrivatePages--;
/* Modify the page structure. */
pPage->Shared.pfn = (uint32_t)(uint64_t)(HCPhys >> PAGE_SHIFT);
pPage->Shared.cRefs = 1;
pPage->Common.u2State = GMM_PAGE_STATE_SHARED;
}
/**
* Increase the use count of a shared page, the page is known to exist and be valid and such.
*
* @param pGMM Pointer to the GMM instance.
* @param pGVM Pointer to the GVM instance.
* @param pPage The page structure.
*/
DECLINLINE(void) gmmR0UseSharedPage(PGMM pGMM, PGVM pGVM, PGMMPAGE pPage)
{
Assert(pGMM->cSharedPages > 0);
Assert(pGMM->cAllocatedPages > 0);
pGMM->cDuplicatePages++;
pPage->Shared.cRefs++;
pGVM->gmm.s.cSharedPages++;
pGVM->gmm.s.Allocated.cBasePages++;
}
#endif /* VBOX_WITH_PAGE_SHARING */
/**
* Frees a private page, the page is known to exist and be valid and such.
*
* @param pGMM Pointer to the GMM instance.
* @param idPage The Page ID
* @param pPage The page structure.
*/
DECLINLINE(void) gmmR0FreePrivatePage(PGMM pGMM, uint32_t idPage, PGMMPAGE pPage)
{
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
Assert(pChunk);
Assert(pChunk->cFree < GMM_CHUNK_NUM_PAGES);
Assert(pChunk->cPrivate > 0);
Assert(pGMM->cAllocatedPages > 0);
pChunk->cPrivate--;
pGMM->cAllocatedPages--;
gmmR0FreePageWorker(pGMM, pChunk, idPage, pPage);
}
/**
* Common worker for GMMR0FreePages and GMMR0BalloonedPages.
*
* @returns VBox status code:
* @retval xxx
*
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the shared VM structure.
* @param cPages The number of pages to free.
* @param paPages Pointer to the page descriptors.
* @param enmAccount The account this relates to.
*/
static int gmmR0FreePages(PGMM pGMM, PGVM pGVM, uint32_t cPages, PGMMFREEPAGEDESC paPages, GMMACCOUNT enmAccount)
{
/*
* Check that the request isn't impossible wrt to the account status.
*/
switch (enmAccount)
{
case GMMACCOUNT_BASE:
if (RT_UNLIKELY(pGVM->gmm.s.Allocated.cBasePages < cPages))
{
Log(("gmmR0FreePages: allocated=%#llx cPages=%#x!\n", pGVM->gmm.s.Allocated.cBasePages, cPages));
return VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH;
}
break;
case GMMACCOUNT_SHADOW:
if (RT_UNLIKELY(pGVM->gmm.s.Allocated.cShadowPages < cPages))
{
Log(("gmmR0FreePages: allocated=%#llx cPages=%#x!\n", pGVM->gmm.s.Allocated.cShadowPages, cPages));
return VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH;
}
break;
case GMMACCOUNT_FIXED:
if (RT_UNLIKELY(pGVM->gmm.s.Allocated.cFixedPages < cPages))
{
Log(("gmmR0FreePages: allocated=%#llx cPages=%#x!\n", pGVM->gmm.s.Allocated.cFixedPages, cPages));
return VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH;
}
break;
default:
AssertMsgFailedReturn(("enmAccount=%d\n", enmAccount), VERR_INTERNAL_ERROR);
}
/*
* Walk the descriptors and free the pages.
*
* Statistics (except the account) are being updated as we go along,
* unlike the alloc code. Also, stop on the first error.
*/
int rc = VINF_SUCCESS;
uint32_t iPage;
for (iPage = 0; iPage < cPages; iPage++)
{
uint32_t idPage = paPages[iPage].idPage;
PGMMPAGE pPage = gmmR0GetPage(pGMM, idPage);
if (RT_LIKELY(pPage))
{
if (RT_LIKELY(GMM_PAGE_IS_PRIVATE(pPage)))
{
if (RT_LIKELY(pPage->Private.hGVM == pGVM->hSelf))
{
Assert(pGVM->gmm.s.cPrivatePages);
pGVM->gmm.s.cPrivatePages--;
gmmR0FreePrivatePage(pGMM, idPage, pPage);
}
else
{
Log(("gmmR0AllocatePages: #%#x/%#x: not owner! hGVM=%#x hSelf=%#x\n", iPage, idPage,
pPage->Private.hGVM, pGVM->hSelf));
rc = VERR_GMM_NOT_PAGE_OWNER;
break;
}
}
else if (RT_LIKELY(GMM_PAGE_IS_SHARED(pPage)))
{
Assert(pGVM->gmm.s.cSharedPages);
pGVM->gmm.s.cSharedPages--;
Assert(pPage->Shared.cRefs);
if (!--pPage->Shared.cRefs)
gmmR0FreeSharedPage(pGMM, idPage, pPage);
else
{
Assert(pGMM->cDuplicatePages);
pGMM->cDuplicatePages--;
}
}
else
{
Log(("gmmR0AllocatePages: #%#x/%#x: already free!\n", iPage, idPage));
rc = VERR_GMM_PAGE_ALREADY_FREE;
break;
}
}
else
{
Log(("gmmR0AllocatePages: #%#x/%#x: not found!\n", iPage, idPage));
rc = VERR_GMM_PAGE_NOT_FOUND;
break;
}
paPages[iPage].idPage = NIL_GMM_PAGEID;
}
/*
* Update the account.
*/
switch (enmAccount)
{
case GMMACCOUNT_BASE: pGVM->gmm.s.Allocated.cBasePages -= iPage; break;
case GMMACCOUNT_SHADOW: pGVM->gmm.s.Allocated.cShadowPages -= iPage; break;
case GMMACCOUNT_FIXED: pGVM->gmm.s.Allocated.cFixedPages -= iPage; break;
default:
AssertMsgFailedReturn(("enmAccount=%d\n", enmAccount), VERR_INTERNAL_ERROR);
}
/*
* Any threshold stuff to be done here?
*/
return rc;
}
/**
* Free one or more pages.
*
* This is typically used at reset time or power off.
*
* @returns VBox status code:
* @retval xxx
*
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param cPages The number of pages to allocate.
* @param paPages Pointer to the page descriptors containing the Page IDs for each page.
* @param enmAccount The account this relates to.
* @thread EMT.
*/
GMMR0DECL(int) GMMR0FreePages(PVM pVM, VMCPUID idCpu, uint32_t cPages, PGMMFREEPAGEDESC paPages, GMMACCOUNT enmAccount)
{
LogFlow(("GMMR0FreePages: pVM=%p cPages=%#x paPages=%p enmAccount=%d\n", pVM, cPages, paPages, enmAccount));
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
PGVM pGVM;
int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
if (RT_FAILURE(rc))
return rc;
AssertPtrReturn(paPages, VERR_INVALID_PARAMETER);
AssertMsgReturn(enmAccount > GMMACCOUNT_INVALID && enmAccount < GMMACCOUNT_END, ("%d\n", enmAccount), VERR_INVALID_PARAMETER);
AssertMsgReturn(cPages > 0 && cPages < RT_BIT(32 - PAGE_SHIFT), ("%#x\n", cPages), VERR_INVALID_PARAMETER);
for (unsigned iPage = 0; iPage < cPages; iPage++)
AssertMsgReturn( paPages[iPage].idPage <= GMM_PAGEID_LAST
/*|| paPages[iPage].idPage == NIL_GMM_PAGEID*/,
("#%#x: %#x\n", iPage, paPages[iPage].idPage), VERR_INVALID_PARAMETER);
/*
* Take the semaphore and call the worker function.
*/
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
rc = gmmR0FreePages(pGMM, pGVM, cPages, paPages, enmAccount);
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_INTERNAL_ERROR_5;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR0FreePages: returns %Rrc\n", rc));
return rc;
}
/**
* VMMR0 request wrapper for GMMR0FreePages.
*
* @returns see GMMR0FreePages.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param pReq The request packet.
*/
GMMR0DECL(int) GMMR0FreePagesReq(PVM pVM, VMCPUID idCpu, PGMMFREEPAGESREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pVM, VERR_INVALID_POINTER);
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq >= RT_UOFFSETOF(GMMFREEPAGESREQ, aPages[0]),
("%#x < %#x\n", pReq->Hdr.cbReq, RT_UOFFSETOF(GMMFREEPAGESREQ, aPages[0])),
VERR_INVALID_PARAMETER);
AssertMsgReturn(pReq->Hdr.cbReq == RT_UOFFSETOF(GMMFREEPAGESREQ, aPages[pReq->cPages]),
("%#x != %#x\n", pReq->Hdr.cbReq, RT_UOFFSETOF(GMMFREEPAGESREQ, aPages[pReq->cPages])),
VERR_INVALID_PARAMETER);
return GMMR0FreePages(pVM, idCpu, pReq->cPages, &pReq->aPages[0], pReq->enmAccount);
}
/**
* Report back on a memory ballooning request.
*
* The request may or may not have been initiated by the GMM. If it was initiated
* by the GMM it is important that this function is called even if no pages were
* ballooned.
*
* @returns VBox status code:
* @retval VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH
* @retval VERR_GMM_ATTEMPT_TO_DEFLATE_TOO_MUCH
* @retval VERR_GMM_OVERCOMMITTED_TRY_AGAIN_IN_A_BIT - reset condition
* indicating that we won't necessarily have sufficient RAM to boot
* the VM again and that it should pause until this changes (we'll try
* balloon some other VM). (For standard deflate we have little choice
* but to hope the VM won't use the memory that was returned to it.)
*
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param enmAction Inflate/deflate/reset
* @param cBalloonedPages The number of pages that was ballooned.
*
* @thread EMT.
*/
GMMR0DECL(int) GMMR0BalloonedPages(PVM pVM, VMCPUID idCpu, GMMBALLOONACTION enmAction, uint32_t cBalloonedPages)
{
LogFlow(("GMMR0BalloonedPages: pVM=%p enmAction=%d cBalloonedPages=%#x\n",
pVM, enmAction, cBalloonedPages));
AssertMsgReturn(cBalloonedPages < RT_BIT(32 - PAGE_SHIFT), ("%#x\n", cBalloonedPages), VERR_INVALID_PARAMETER);
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
PGVM pGVM;
int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
if (RT_FAILURE(rc))
return rc;
/*
* Take the semaphore and do some more validations.
*/
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
switch (enmAction)
{
case GMMBALLOONACTION_INFLATE:
{
if (RT_LIKELY(pGVM->gmm.s.Allocated.cBasePages + pGVM->gmm.s.cBalloonedPages + cBalloonedPages <= pGVM->gmm.s.Reserved.cBasePages))
{
/*
* Record the ballooned memory.
*/
pGMM->cBalloonedPages += cBalloonedPages;
if (pGVM->gmm.s.cReqBalloonedPages)
{
/* Codepath never taken. Might be interesting in the future to request ballooned memory from guests in low memory conditions.. */
AssertFailed();
pGVM->gmm.s.cBalloonedPages += cBalloonedPages;
pGVM->gmm.s.cReqActuallyBalloonedPages += cBalloonedPages;
Log(("GMMR0BalloonedPages: +%#x - Global=%#llx / VM: Total=%#llx Req=%#llx Actual=%#llx (pending)\n", cBalloonedPages,
pGMM->cBalloonedPages, pGVM->gmm.s.cBalloonedPages, pGVM->gmm.s.cReqBalloonedPages, pGVM->gmm.s.cReqActuallyBalloonedPages));
}
else
{
pGVM->gmm.s.cBalloonedPages += cBalloonedPages;
Log(("GMMR0BalloonedPages: +%#x - Global=%#llx / VM: Total=%#llx (user)\n",
cBalloonedPages, pGMM->cBalloonedPages, pGVM->gmm.s.cBalloonedPages));
}
}
else
{
Log(("GMMR0BalloonedPages: cBasePages=%#llx Total=%#llx cBalloonedPages=%#llx Reserved=%#llx\n",
pGVM->gmm.s.Allocated.cBasePages, pGVM->gmm.s.cBalloonedPages, cBalloonedPages, pGVM->gmm.s.Reserved.cBasePages));
rc = VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH;
}
break;
}
case GMMBALLOONACTION_DEFLATE:
{
/* Deflate. */
if (pGVM->gmm.s.cBalloonedPages >= cBalloonedPages)
{
/*
* Record the ballooned memory.
*/
Assert(pGMM->cBalloonedPages >= cBalloonedPages);
pGMM->cBalloonedPages -= cBalloonedPages;
pGVM->gmm.s.cBalloonedPages -= cBalloonedPages;
if (pGVM->gmm.s.cReqDeflatePages)
{
AssertFailed(); /* This is path is for later. */
Log(("GMMR0BalloonedPages: -%#x - Global=%#llx / VM: Total=%#llx Req=%#llx\n",
cBalloonedPages, pGMM->cBalloonedPages, pGVM->gmm.s.cBalloonedPages, pGVM->gmm.s.cReqDeflatePages));
/*
* Anything we need to do here now when the request has been completed?
*/
pGVM->gmm.s.cReqDeflatePages = 0;
}
else
Log(("GMMR0BalloonedPages: -%#x - Global=%#llx / VM: Total=%#llx (user)\n",
cBalloonedPages, pGMM->cBalloonedPages, pGVM->gmm.s.cBalloonedPages));
}
else
{
Log(("GMMR0BalloonedPages: Total=%#llx cBalloonedPages=%#llx\n", pGVM->gmm.s.cBalloonedPages, cBalloonedPages));
rc = VERR_GMM_ATTEMPT_TO_DEFLATE_TOO_MUCH;
}
break;
}
case GMMBALLOONACTION_RESET:
{
/* Reset to an empty balloon. */
Assert(pGMM->cBalloonedPages >= pGVM->gmm.s.cBalloonedPages);
pGMM->cBalloonedPages -= pGVM->gmm.s.cBalloonedPages;
pGVM->gmm.s.cBalloonedPages = 0;
break;
}
default:
rc = VERR_INVALID_PARAMETER;
break;
}
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_INTERNAL_ERROR_5;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR0BalloonedPages: returns %Rrc\n", rc));
return rc;
}
/**
* VMMR0 request wrapper for GMMR0BalloonedPages.
*
* @returns see GMMR0BalloonedPages.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param pReq The request packet.
*/
GMMR0DECL(int) GMMR0BalloonedPagesReq(PVM pVM, VMCPUID idCpu, PGMMBALLOONEDPAGESREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pVM, VERR_INVALID_POINTER);
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(GMMBALLOONEDPAGESREQ),
("%#x < %#x\n", pReq->Hdr.cbReq, sizeof(GMMBALLOONEDPAGESREQ)),
VERR_INVALID_PARAMETER);
return GMMR0BalloonedPages(pVM, idCpu, pReq->enmAction, pReq->cBalloonedPages);
}
/**
* Return memory statistics for the hypervisor
*
* @returns VBox status code:
* @param pVM Pointer to the shared VM structure.
* @param pReq The request packet.
*/
GMMR0DECL(int) GMMR0QueryHypervisorMemoryStatsReq(PVM pVM, PGMMMEMSTATSREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pVM, VERR_INVALID_POINTER);
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(GMMMEMSTATSREQ),
("%#x < %#x\n", pReq->Hdr.cbReq, sizeof(GMMMEMSTATSREQ)),
VERR_INVALID_PARAMETER);
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
pReq->cAllocPages = pGMM->cAllocatedPages;
pReq->cFreePages = (pGMM->cChunks << (GMM_CHUNK_SHIFT- PAGE_SHIFT)) - pGMM->cAllocatedPages;
pReq->cBalloonedPages = pGMM->cBalloonedPages;
pReq->cMaxPages = pGMM->cMaxPages;
pReq->cSharedPages = pGMM->cDuplicatePages;
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
return VINF_SUCCESS;
}
/**
* Return memory statistics for the VM
*
* @returns VBox status code:
* @param pVM Pointer to the shared VM structure.
* @parma idCpu Cpu id.
* @param pReq The request packet.
*/
GMMR0DECL(int) GMMR0QueryMemoryStatsReq(PVM pVM, VMCPUID idCpu, PGMMMEMSTATSREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pVM, VERR_INVALID_POINTER);
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(GMMMEMSTATSREQ),
("%#x < %#x\n", pReq->Hdr.cbReq, sizeof(GMMMEMSTATSREQ)),
VERR_INVALID_PARAMETER);
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
PGVM pGVM;
int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
if (RT_FAILURE(rc))
return rc;
/*
* Take the semaphore and do some more validations.
*/
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
pReq->cAllocPages = pGVM->gmm.s.Allocated.cBasePages;
pReq->cBalloonedPages = pGVM->gmm.s.cBalloonedPages;
pReq->cMaxPages = pGVM->gmm.s.Reserved.cBasePages;
pReq->cFreePages = pReq->cMaxPages - pReq->cAllocPages;
}
else
rc = VERR_INTERNAL_ERROR_5;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR3QueryVMMemoryStats: returns %Rrc\n", rc));
return rc;
}
/**
* Unmaps a chunk previously mapped into the address space of the current process.
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the Global VM structure.
* @param pChunk Pointer to the chunk to be unmapped.
*/
static int gmmR0UnmapChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk)
{
if (!pGMM->fLegacyAllocationMode)
{
/*
* Find the mapping and try unmapping it.
*/
uint32_t cMappings = pChunk->cMappings;
for (uint32_t i = 0; i < cMappings; i++)
{
Assert(pChunk->paMappings[i].pGVM && pChunk->paMappings[i].MapObj != NIL_RTR0MEMOBJ);
if (pChunk->paMappings[i].pGVM == pGVM)
{
/* unmap */
int rc = RTR0MemObjFree(pChunk->paMappings[i].MapObj, false /* fFreeMappings (NA) */);
if (RT_SUCCESS(rc))
{
/* update the record. */
cMappings--;
if (i < cMappings)
pChunk->paMappings[i] = pChunk->paMappings[cMappings];
pChunk->paMappings[cMappings].MapObj = NIL_RTR0MEMOBJ;
pChunk->paMappings[cMappings].pGVM = NULL;
Assert(pChunk->cMappings - 1U == cMappings);
pChunk->cMappings = cMappings;
}
return rc;
}
}
}
else if (pChunk->hGVM == pGVM->hSelf)
return VINF_SUCCESS;
Log(("gmmR0UnmapChunk: Chunk %#x is not mapped into pGVM=%p/%#x\n", pChunk->Core.Key, pGVM, pGVM->hSelf));
return VERR_GMM_CHUNK_NOT_MAPPED;
}
/**
* Maps a chunk into the user address space of the current process.
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the Global VM structure.
* @param pChunk Pointer to the chunk to be mapped.
* @param ppvR3 Where to store the ring-3 address of the mapping.
* In the VERR_GMM_CHUNK_ALREADY_MAPPED case, this will be
* contain the address of the existing mapping.
*/
static int gmmR0MapChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk, PRTR3PTR ppvR3)
{
Assert(pGMM->hMtxOwner == RTThreadNativeSelf());
/*
* If we're in legacy mode this is simple.
*/
if (pGMM->fLegacyAllocationMode)
{
if (pChunk->hGVM != pGVM->hSelf)
{
Log(("gmmR0MapChunk: chunk %#x is already mapped at %p!\n", pChunk->Core.Key, *ppvR3));
return VERR_GMM_CHUNK_NOT_FOUND;
}
*ppvR3 = RTR0MemObjAddressR3(pChunk->MemObj);
return VINF_SUCCESS;
}
/*
* Check to see if the chunk is already mapped.
*/
for (uint32_t i = 0; i < pChunk->cMappings; i++)
{
Assert(pChunk->paMappings[i].pGVM && pChunk->paMappings[i].MapObj != NIL_RTR0MEMOBJ);
if (pChunk->paMappings[i].pGVM == pGVM)
{
*ppvR3 = RTR0MemObjAddressR3(pChunk->paMappings[i].MapObj);
Log(("gmmR0MapChunk: chunk %#x is already mapped at %p!\n", pChunk->Core.Key, *ppvR3));
#ifdef VBOX_WITH_PAGE_SHARING
/* The ring-3 chunk cache can be out of sync; don't fail. */
return VINF_SUCCESS;
#else
return VERR_GMM_CHUNK_ALREADY_MAPPED;
#endif
}
}
/*
* Do the mapping.
*/
RTR0MEMOBJ MapObj;
int rc = RTR0MemObjMapUser(&MapObj, pChunk->MemObj, (RTR3PTR)-1, 0, RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
if (RT_SUCCESS(rc))
{
/* reallocate the array? assumes few users per chunk (usually one). */
unsigned iMapping = pChunk->cMappings;
if ( iMapping <= 3
|| (iMapping & 3) == 0)
{
unsigned cNewSize = iMapping <= 3
? iMapping + 1
: iMapping + 4;
Assert(cNewSize < 4 || RT_ALIGN_32(cNewSize, 4) == cNewSize);
if (RT_UNLIKELY(cNewSize > UINT16_MAX))
{
rc = RTR0MemObjFree(MapObj, false /* fFreeMappings (NA) */); AssertRC(rc);
return VERR_GMM_TOO_MANY_CHUNK_MAPPINGS;
}
void *pvMappings = RTMemRealloc(pChunk->paMappings, cNewSize * sizeof(pChunk->paMappings[0]));
if (RT_UNLIKELY(!pvMappings))
{
rc = RTR0MemObjFree(MapObj, false /* fFreeMappings (NA) */); AssertRC(rc);
return VERR_NO_MEMORY;
}
pChunk->paMappings = (PGMMCHUNKMAP)pvMappings;
}
/* insert new entry */
pChunk->paMappings[iMapping].MapObj = MapObj;
pChunk->paMappings[iMapping].pGVM = pGVM;
Assert(pChunk->cMappings == iMapping);
pChunk->cMappings = iMapping + 1;
*ppvR3 = RTR0MemObjAddressR3(MapObj);
}
return rc;
}
/**
* Check if a chunk is mapped into the specified VM
*
* @returns mapped yes/no
* @param pGVM Pointer to the Global VM structure.
* @param pChunk Pointer to the chunk to be mapped.
* @param ppvR3 Where to store the ring-3 address of the mapping.
*/
static int gmmR0IsChunkMapped(PGVM pGVM, PGMMCHUNK pChunk, PRTR3PTR ppvR3)
{
/*
* Check to see if the chunk is already mapped.
*/
for (uint32_t i = 0; i < pChunk->cMappings; i++)
{
Assert(pChunk->paMappings[i].pGVM && pChunk->paMappings[i].MapObj != NIL_RTR0MEMOBJ);
if (pChunk->paMappings[i].pGVM == pGVM)
{
*ppvR3 = RTR0MemObjAddressR3(pChunk->paMappings[i].MapObj);
return true;
}
}
*ppvR3 = NULL;
return false;
}
/**
* Map a chunk and/or unmap another chunk.
*
* The mapping and unmapping applies to the current process.
*
* This API does two things because it saves a kernel call per mapping when
* when the ring-3 mapping cache is full.
*
* @returns VBox status code.
* @param pVM The VM.
* @param idChunkMap The chunk to map. NIL_GMM_CHUNKID if nothing to map.
* @param idChunkUnmap The chunk to unmap. NIL_GMM_CHUNKID if nothing to unmap.
* @param ppvR3 Where to store the address of the mapped chunk. NULL is ok if nothing to map.
* @thread EMT
*/
GMMR0DECL(int) GMMR0MapUnmapChunk(PVM pVM, uint32_t idChunkMap, uint32_t idChunkUnmap, PRTR3PTR ppvR3)
{
LogFlow(("GMMR0MapUnmapChunk: pVM=%p idChunkMap=%#x idChunkUnmap=%#x ppvR3=%p\n",
pVM, idChunkMap, idChunkUnmap, ppvR3));
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
PGVM pGVM;
int rc = GVMMR0ByVM(pVM, &pGVM);
if (RT_FAILURE(rc))
return rc;
AssertCompile(NIL_GMM_CHUNKID == 0);
AssertMsgReturn(idChunkMap <= GMM_CHUNKID_LAST, ("%#x\n", idChunkMap), VERR_INVALID_PARAMETER);
AssertMsgReturn(idChunkUnmap <= GMM_CHUNKID_LAST, ("%#x\n", idChunkUnmap), VERR_INVALID_PARAMETER);
if ( idChunkMap == NIL_GMM_CHUNKID
&& idChunkUnmap == NIL_GMM_CHUNKID)
return VERR_INVALID_PARAMETER;
if (idChunkMap != NIL_GMM_CHUNKID)
{
AssertPtrReturn(ppvR3, VERR_INVALID_POINTER);
*ppvR3 = NIL_RTR3PTR;
}
/*
* Take the semaphore and do the work.
*
* The unmapping is done last since it's easier to undo a mapping than
* undoing an unmapping. The ring-3 mapping cache cannot not be so big
* that it pushes the user virtual address space to within a chunk of
* it it's limits, so, no problem here.
*/
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
PGMMCHUNK pMap = NULL;
if (idChunkMap != NIL_GVM_HANDLE)
{
pMap = gmmR0GetChunk(pGMM, idChunkMap);
if (RT_LIKELY(pMap))
rc = gmmR0MapChunk(pGMM, pGVM, pMap, ppvR3);
else
{
Log(("GMMR0MapUnmapChunk: idChunkMap=%#x\n", idChunkMap));
rc = VERR_GMM_CHUNK_NOT_FOUND;
}
}
if ( idChunkUnmap != NIL_GMM_CHUNKID
&& RT_SUCCESS(rc))
{
PGMMCHUNK pUnmap = gmmR0GetChunk(pGMM, idChunkUnmap);
if (RT_LIKELY(pUnmap))
rc = gmmR0UnmapChunk(pGMM, pGVM, pUnmap);
else
{
Log(("GMMR0MapUnmapChunk: idChunkUnmap=%#x\n", idChunkUnmap));
rc = VERR_GMM_CHUNK_NOT_FOUND;
}
if (RT_FAILURE(rc) && pMap)
gmmR0UnmapChunk(pGMM, pGVM, pMap);
}
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_INTERNAL_ERROR_5;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR0MapUnmapChunk: returns %Rrc\n", rc));
return rc;
}
/**
* VMMR0 request wrapper for GMMR0MapUnmapChunk.
*
* @returns see GMMR0MapUnmapChunk.
* @param pVM Pointer to the shared VM structure.
* @param pReq The request packet.
*/
GMMR0DECL(int) GMMR0MapUnmapChunkReq(PVM pVM, PGMMMAPUNMAPCHUNKREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pVM, VERR_INVALID_POINTER);
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
return GMMR0MapUnmapChunk(pVM, pReq->idChunkMap, pReq->idChunkUnmap, &pReq->pvR3);
}
/**
* Legacy mode API for supplying pages.
*
* The specified user address points to a allocation chunk sized block that
* will be locked down and used by the GMM when the GM asks for pages.
*
* @returns VBox status code.
* @param pVM The VM.
* @param idCpu VCPU id
* @param pvR3 Pointer to the chunk size memory block to lock down.
*/
GMMR0DECL(int) GMMR0SeedChunk(PVM pVM, VMCPUID idCpu, RTR3PTR pvR3)
{
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
PGVM pGVM;
int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
if (RT_FAILURE(rc))
return rc;
AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
AssertReturn(!(PAGE_OFFSET_MASK & pvR3), VERR_INVALID_POINTER);
if (!pGMM->fLegacyAllocationMode)
{
Log(("GMMR0SeedChunk: not in legacy allocation mode!\n"));
return VERR_NOT_SUPPORTED;
}
/*
* Lock the memory before taking the semaphore.
*/
RTR0MEMOBJ MemObj;
rc = RTR0MemObjLockUser(&MemObj, pvR3, GMM_CHUNK_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
if (RT_SUCCESS(rc))
{
/* Grab the lock. */
rc = gmmR0MutexAcquire(pGMM);
if (RT_SUCCESS(rc))
{
/*
* Add a new chunk with our hGVM.
*/
rc = gmmR0RegisterChunk(pGMM, &pGMM->Private, MemObj, pGVM->hSelf, GMMCHUNKTYPE_NON_CONTINUOUS);
gmmR0MutexRelease(pGMM);
}
if (RT_FAILURE(rc))
RTR0MemObjFree(MemObj, false /* fFreeMappings */);
}
LogFlow(("GMMR0SeedChunk: rc=%d (pvR3=%p)\n", rc, pvR3));
return rc;
}
typedef struct
{
PAVLGCPTRNODECORE pNode;
char *pszModuleName;
char *pszVersion;
VBOXOSFAMILY enmGuestOS;
} GMMFINDMODULEBYNAME, *PGMMFINDMODULEBYNAME;
/**
* Tree enumeration callback for finding identical modules by name and version
*/
DECLCALLBACK(int) gmmR0CheckForIdenticalModule(PAVLGCPTRNODECORE pNode, void *pvUser)
{
PGMMFINDMODULEBYNAME pInfo = (PGMMFINDMODULEBYNAME)pvUser;
PGMMSHAREDMODULE pModule = (PGMMSHAREDMODULE)pNode;
if ( pInfo
&& pInfo->enmGuestOS == pModule->enmGuestOS
/** @todo replace with RTStrNCmp */
&& !strcmp(pModule->szName, pInfo->pszModuleName)
&& !strcmp(pModule->szVersion, pInfo->pszVersion))
{
pInfo->pNode = pNode;
return 1; /* stop search */
}
return 0;
}
/**
* Registers a new shared module for the VM
*
* @returns VBox status code.
* @param pVM VM handle
* @param idCpu VCPU id
* @param enmGuestOS Guest OS type
* @param pszModuleName Module name
* @param pszVersion Module version
* @param GCBaseAddr Module base address
* @param cbModule Module size
* @param cRegions Number of shared region descriptors
* @param pRegions Shared region(s)
*/
GMMR0DECL(int) GMMR0RegisterSharedModule(PVM pVM, VMCPUID idCpu, VBOXOSFAMILY enmGuestOS, char *pszModuleName, char *pszVersion, RTGCPTR GCBaseAddr, uint32_t cbModule,
unsigned cRegions, VMMDEVSHAREDREGIONDESC *pRegions)
{
#ifdef VBOX_WITH_PAGE_SHARING
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
PGVM pGVM;
int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
if (RT_FAILURE(rc))
return rc;
Log(("GMMR0RegisterSharedModule %s %s base %RGv size %x\n", pszModuleName, pszVersion, GCBaseAddr, cbModule));
/*
* Take the semaphore and do some more validations.
*/
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
bool fNewModule = false;
/* Check if this module is already locally registered. */
PGMMSHAREDMODULEPERVM pRecVM = (PGMMSHAREDMODULEPERVM)RTAvlGCPtrGet(&pGVM->gmm.s.pSharedModuleTree, GCBaseAddr);
if (!pRecVM)
{
pRecVM = (PGMMSHAREDMODULEPERVM)RTMemAllocZ(RT_OFFSETOF(GMMSHAREDMODULEPERVM, aRegions[cRegions]));
if (!pRecVM)
{
AssertFailed();
rc = VERR_NO_MEMORY;
goto end;
}
pRecVM->Core.Key = GCBaseAddr;
pRecVM->cRegions = cRegions;
/* Save the region data as they can differ between VMs (address space scrambling or simply different loading order) */
for (unsigned i = 0; i < cRegions; i++)
{
pRecVM->aRegions[i].GCRegionAddr = pRegions[i].GCRegionAddr;
pRecVM->aRegions[i].cbRegion = RT_ALIGN_T(pRegions[i].cbRegion, PAGE_SIZE, uint32_t);
pRecVM->aRegions[i].u32Alignment = 0;
pRecVM->aRegions[i].paHCPhysPageID = NULL; /* unused */
}
bool ret = RTAvlGCPtrInsert(&pGVM->gmm.s.pSharedModuleTree, &pRecVM->Core);
Assert(ret);
Log(("GMMR0RegisterSharedModule: new local module %s\n", pszModuleName));
fNewModule = true;
}
else
rc = VINF_PGM_SHARED_MODULE_ALREADY_REGISTERED;
/* Check if this module is already globally registered. */
PGMMSHAREDMODULE pGlobalModule = (PGMMSHAREDMODULE)RTAvlGCPtrGet(&pGMM->pGlobalSharedModuleTree, GCBaseAddr);
if ( !pGlobalModule
&& enmGuestOS == VBOXOSFAMILY_Windows64)
{
/* Two identical copies of e.g. Win7 x64 will typically not have a similar virtual address space layout for dlls or kernel modules.
* Try to find identical binaries based on name and version.
*/
GMMFINDMODULEBYNAME Info;
Info.pNode = NULL;
Info.pszVersion = pszVersion;
Info.pszModuleName = pszModuleName;
Info.enmGuestOS = enmGuestOS;
Log(("Try to find identical module %s\n", pszModuleName));
int ret = RTAvlGCPtrDoWithAll(&pGMM->pGlobalSharedModuleTree, true /* fFromLeft */, gmmR0CheckForIdenticalModule, &Info);
if (ret == 1)
{
Assert(Info.pNode);
pGlobalModule = (PGMMSHAREDMODULE)Info.pNode;
Log(("Found identical module at %RGv\n", pGlobalModule->Core.Key));
}
}
if (!pGlobalModule)
{
Assert(fNewModule);
Assert(!pRecVM->fCollision);
pGlobalModule = (PGMMSHAREDMODULE)RTMemAllocZ(RT_OFFSETOF(GMMSHAREDMODULE, aRegions[cRegions]));
if (!pGlobalModule)
{
AssertFailed();
rc = VERR_NO_MEMORY;
goto end;
}
pGlobalModule->Core.Key = GCBaseAddr;
pGlobalModule->cbModule = cbModule;
/* Input limit already safe; no need to check again. */
/** @todo replace with RTStrCopy */
strcpy(pGlobalModule->szName, pszModuleName);
strcpy(pGlobalModule->szVersion, pszVersion);
pGlobalModule->enmGuestOS = enmGuestOS;
pGlobalModule->cRegions = cRegions;
for (unsigned i = 0; i < cRegions; i++)
{
Log(("New region %d base=%RGv size %x\n", i, pRegions[i].GCRegionAddr, pRegions[i].cbRegion));
pGlobalModule->aRegions[i].GCRegionAddr = pRegions[i].GCRegionAddr;
pGlobalModule->aRegions[i].cbRegion = RT_ALIGN_T(pRegions[i].cbRegion, PAGE_SIZE, uint32_t);
pGlobalModule->aRegions[i].u32Alignment = 0;
pGlobalModule->aRegions[i].paHCPhysPageID = NULL; /* uninitialized. */
}
/* Save reference. */
pRecVM->pGlobalModule = pGlobalModule;
pRecVM->fCollision = false;
pGlobalModule->cUsers++;
rc = VINF_SUCCESS;
bool ret = RTAvlGCPtrInsert(&pGMM->pGlobalSharedModuleTree, &pGlobalModule->Core);
Assert(ret);
Log(("GMMR0RegisterSharedModule: new global module %s\n", pszModuleName));
}
else
{
Assert(pGlobalModule->cUsers > 0);
/* Make sure the name and version are identical. */
/** @todo replace with RTStrNCmp */
if ( !strcmp(pGlobalModule->szName, pszModuleName)
&& !strcmp(pGlobalModule->szVersion, pszVersion))
{
/* Save reference. */
pRecVM->pGlobalModule = pGlobalModule;
if ( fNewModule
|| pRecVM->fCollision == true) /* colliding module unregistered and new one registered since the last check */
{
pGlobalModule->cUsers++;
Log(("GMMR0RegisterSharedModule: using existing module %s cUser=%d!\n", pszModuleName, pGlobalModule->cUsers));
}
pRecVM->fCollision = false;
rc = VINF_SUCCESS;
}
else
{
Log(("GMMR0RegisterSharedModule: module %s collision!\n", pszModuleName));
pRecVM->fCollision = true;
rc = VINF_PGM_SHARED_MODULE_COLLISION;
goto end;
}
}
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_INTERNAL_ERROR_5;
end:
gmmR0MutexRelease(pGMM);
return rc;
#else
return VERR_NOT_IMPLEMENTED;
#endif
}
/**
* VMMR0 request wrapper for GMMR0RegisterSharedModule.
*
* @returns see GMMR0RegisterSharedModule.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param pReq The request packet.
*/
GMMR0DECL(int) GMMR0RegisterSharedModuleReq(PVM pVM, VMCPUID idCpu, PGMMREGISTERSHAREDMODULEREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pVM, VERR_INVALID_POINTER);
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq >= sizeof(*pReq) && pReq->Hdr.cbReq == RT_UOFFSETOF(GMMREGISTERSHAREDMODULEREQ, aRegions[pReq->cRegions]), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
/* Pass back return code in the request packet to preserve informational codes. (VMMR3CallR0 chokes on them) */
pReq->rc = GMMR0RegisterSharedModule(pVM, idCpu, pReq->enmGuestOS, pReq->szName, pReq->szVersion, pReq->GCBaseAddr, pReq->cbModule, pReq->cRegions, pReq->aRegions);
return VINF_SUCCESS;
}
/**
* Unregisters a shared module for the VM
*
* @returns VBox status code.
* @param pVM VM handle
* @param idCpu VCPU id
* @param pszModuleName Module name
* @param pszVersion Module version
* @param GCBaseAddr Module base address
* @param cbModule Module size
*/
GMMR0DECL(int) GMMR0UnregisterSharedModule(PVM pVM, VMCPUID idCpu, char *pszModuleName, char *pszVersion, RTGCPTR GCBaseAddr, uint32_t cbModule)
{
#ifdef VBOX_WITH_PAGE_SHARING
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
PGVM pGVM;
int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
if (RT_FAILURE(rc))
return rc;
Log(("GMMR0UnregisterSharedModule %s %s base=%RGv size %x\n", pszModuleName, pszVersion, GCBaseAddr, cbModule));
/*
* Take the semaphore and do some more validations.
*/
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
PGMMSHAREDMODULEPERVM pRecVM = (PGMMSHAREDMODULEPERVM)RTAvlGCPtrGet(&pGVM->gmm.s.pSharedModuleTree, GCBaseAddr);
if (pRecVM)
{
/* Remove reference to global shared module. */
if (!pRecVM->fCollision)
{
PGMMSHAREDMODULE pRec = pRecVM->pGlobalModule;
Assert(pRec);
if (pRec) /* paranoia */
{
Assert(pRec->cUsers);
pRec->cUsers--;
if (pRec->cUsers == 0)
{
/* Free the ranges, but leave the pages intact as there might still be references; they will be cleared by the COW mechanism. */
for (unsigned i = 0; i < pRec->cRegions; i++)
if (pRec->aRegions[i].paHCPhysPageID)
RTMemFree(pRec->aRegions[i].paHCPhysPageID);
Assert(pRec->Core.Key == GCBaseAddr || pRec->enmGuestOS == VBOXOSFAMILY_Windows64);
Assert(pRec->cRegions == pRecVM->cRegions);
#ifdef VBOX_STRICT
for (unsigned i = 0; i < pRecVM->cRegions; i++)
{
Assert(pRecVM->aRegions[i].GCRegionAddr == pRec->aRegions[i].GCRegionAddr);
Assert(pRecVM->aRegions[i].cbRegion == pRec->aRegions[i].cbRegion);
}
#endif
/* Remove from the tree and free memory. */
RTAvlGCPtrRemove(&pGMM->pGlobalSharedModuleTree, pRec->Core.Key);
RTMemFree(pRec);
}
}
else
rc = VERR_PGM_SHARED_MODULE_REGISTRATION_INCONSISTENCY;
}
else
Assert(!pRecVM->pGlobalModule);
/* Remove from the tree and free memory. */
RTAvlGCPtrRemove(&pGVM->gmm.s.pSharedModuleTree, GCBaseAddr);
RTMemFree(pRecVM);
}
else
rc = VERR_PGM_SHARED_MODULE_NOT_FOUND;
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_INTERNAL_ERROR_5;
gmmR0MutexRelease(pGMM);
return rc;
#else
return VERR_NOT_IMPLEMENTED;
#endif
}
/**
* VMMR0 request wrapper for GMMR0UnregisterSharedModule.
*
* @returns see GMMR0UnregisterSharedModule.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param pReq The request packet.
*/
GMMR0DECL(int) GMMR0UnregisterSharedModuleReq(PVM pVM, VMCPUID idCpu, PGMMUNREGISTERSHAREDMODULEREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pVM, VERR_INVALID_POINTER);
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
return GMMR0UnregisterSharedModule(pVM, idCpu, pReq->szName, pReq->szVersion, pReq->GCBaseAddr, pReq->cbModule);
}
#ifdef VBOX_WITH_PAGE_SHARING
/**
* Checks specified shared module range for changes
*
* Performs the following tasks:
* - If a shared page is new, then it changes the GMM page type to shared and
* returns it in the pPageDesc descriptor.
* - If a shared page already exists, then it checks if the VM page is
* identical and if so frees the VM page and returns the shared page in
* pPageDesc descriptor.
*
* @remarks ASSUMES the caller has acquired the GMM semaphore!!
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the GVM instance data.
* @param pModule Module description
* @param idxRegion Region index
* @param idxPage Page index
* @param paPageDesc Page descriptor
*/
GMMR0DECL(int) GMMR0SharedModuleCheckPage(PGVM pGVM, PGMMSHAREDMODULE pModule, unsigned idxRegion, unsigned idxPage,
PGMMSHAREDPAGEDESC pPageDesc)
{
int rc = VINF_SUCCESS;
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
unsigned cPages = pModule->aRegions[idxRegion].cbRegion >> PAGE_SHIFT;
AssertReturn(idxRegion < pModule->cRegions, VERR_INVALID_PARAMETER);
AssertReturn(idxPage < cPages, VERR_INVALID_PARAMETER);
LogFlow(("GMMR0SharedModuleCheckRange %s base %RGv region %d idxPage %d\n", pModule->szName, pModule->Core.Key, idxRegion, idxPage));
PGMMSHAREDREGIONDESC pGlobalRegion = &pModule->aRegions[idxRegion];
if (!pGlobalRegion->paHCPhysPageID)
{
/* First time; create a page descriptor array. */
Log(("Allocate page descriptor array for %d pages\n", cPages));
pGlobalRegion->paHCPhysPageID = (uint32_t *)RTMemAlloc(cPages * sizeof(*pGlobalRegion->paHCPhysPageID));
if (!pGlobalRegion->paHCPhysPageID)
{
AssertFailed();
rc = VERR_NO_MEMORY;
goto end;
}
/* Invalidate all descriptors. */
for (unsigned i = 0; i < cPages; i++)
pGlobalRegion->paHCPhysPageID[i] = NIL_GMM_PAGEID;
}
/* We've seen this shared page for the first time? */
if (pGlobalRegion->paHCPhysPageID[idxPage] == NIL_GMM_PAGEID)
{
new_shared_page:
Log(("New shared page guest %RGp host %RHp\n", pPageDesc->GCPhys, pPageDesc->HCPhys));
/* Easy case: just change the internal page type. */
PGMMPAGE pPage = gmmR0GetPage(pGMM, pPageDesc->uHCPhysPageId);
if (!pPage)
{
Log(("GMMR0SharedModuleCheckPage: Invalid idPage=%#x #1 (GCPhys=%RGp HCPhys=%RHp idxRegion=%#x idxPage=%#x)\n",
pPageDesc->uHCPhysPageId, pPageDesc->GCPhys, pPageDesc->HCPhys, idxRegion, idxPage));
AssertFailed();
rc = VERR_PGM_PHYS_INVALID_PAGE_ID;
goto end;
}
AssertMsg(pPageDesc->GCPhys == (pPage->Private.pfn << 12), ("desc %RGp gmm %RGp\n", pPageDesc->HCPhys, (pPage->Private.pfn << 12)));
gmmR0ConvertToSharedPage(pGMM, pGVM, pPageDesc->HCPhys, pPageDesc->uHCPhysPageId, pPage);
/* Keep track of these references. */
pGlobalRegion->paHCPhysPageID[idxPage] = pPageDesc->uHCPhysPageId;
}
else
{
uint8_t *pbLocalPage, *pbSharedPage;
uint8_t *pbChunk;
PGMMCHUNK pChunk;
Assert(pPageDesc->uHCPhysPageId != pGlobalRegion->paHCPhysPageID[idxPage]);
Log(("Replace existing page guest %RGp host %RHp id %x -> id %x\n", pPageDesc->GCPhys, pPageDesc->HCPhys, pPageDesc->uHCPhysPageId, pGlobalRegion->paHCPhysPageID[idxPage]));
/* Get the shared page source. */
PGMMPAGE pPage = gmmR0GetPage(pGMM, pGlobalRegion->paHCPhysPageID[idxPage]);
if (!pPage)
{
Log(("GMMR0SharedModuleCheckPage: Invalid idPage=%#x #2 (idxRegion=%#x idxPage=%#x)\n",
pPageDesc->uHCPhysPageId, idxRegion, idxPage));
AssertFailed();
rc = VERR_PGM_PHYS_INVALID_PAGE_ID;
goto end;
}
if (pPage->Common.u2State != GMM_PAGE_STATE_SHARED)
{
/* Page was freed at some point; invalidate this entry. */
/** @todo this isn't really bullet proof. */
Log(("Old shared page was freed -> create a new one\n"));
pGlobalRegion->paHCPhysPageID[idxPage] = NIL_GMM_PAGEID;
goto new_shared_page; /* ugly goto */
}
Log(("Replace existing page guest host %RHp -> %RHp\n", pPageDesc->HCPhys, ((uint64_t)pPage->Shared.pfn) << PAGE_SHIFT));
/* Calculate the virtual address of the local page. */
pChunk = gmmR0GetChunk(pGMM, pPageDesc->uHCPhysPageId >> GMM_CHUNKID_SHIFT);
if (pChunk)
{
if (!gmmR0IsChunkMapped(pGVM, pChunk, (PRTR3PTR)&pbChunk))
{
Log(("GMMR0SharedModuleCheckPage: Invalid idPage=%#x #3\n", pPageDesc->uHCPhysPageId));
AssertFailed();
rc = VERR_PGM_PHYS_INVALID_PAGE_ID;
goto end;
}
pbLocalPage = pbChunk + ((pPageDesc->uHCPhysPageId & GMM_PAGEID_IDX_MASK) << PAGE_SHIFT);
}
else
{
Log(("GMMR0SharedModuleCheckPage: Invalid idPage=%#x #4\n", pPageDesc->uHCPhysPageId));
AssertFailed();
rc = VERR_PGM_PHYS_INVALID_PAGE_ID;
goto end;
}
/* Calculate the virtual address of the shared page. */
pChunk = gmmR0GetChunk(pGMM, pGlobalRegion->paHCPhysPageID[idxPage] >> GMM_CHUNKID_SHIFT);
Assert(pChunk); /* can't fail as gmmR0GetPage succeeded. */
/* Get the virtual address of the physical page; map the chunk into the VM process if not already done. */
if (!gmmR0IsChunkMapped(pGVM, pChunk, (PRTR3PTR)&pbChunk))
{
Log(("Map chunk into process!\n"));
rc = gmmR0MapChunk(pGMM, pGVM, pChunk, (PRTR3PTR)&pbChunk);
if (rc != VINF_SUCCESS)
{
AssertRC(rc);
goto end;
}
}
pbSharedPage = pbChunk + ((pGlobalRegion->paHCPhysPageID[idxPage] & GMM_PAGEID_IDX_MASK) << PAGE_SHIFT);
/** @todo write ASMMemComparePage. */
if (memcmp(pbSharedPage, pbLocalPage, PAGE_SIZE))
{
Log(("Unexpected differences found between local and shared page; skip\n"));
/* Signal to the caller that this one hasn't changed. */
pPageDesc->uHCPhysPageId = NIL_GMM_PAGEID;
goto end;
}
/* Free the old local page. */
GMMFREEPAGEDESC PageDesc;
PageDesc.idPage = pPageDesc->uHCPhysPageId;
rc = gmmR0FreePages(pGMM, pGVM, 1, &PageDesc, GMMACCOUNT_BASE);
AssertRCReturn(rc, rc);
gmmR0UseSharedPage(pGMM, pGVM, pPage);
/* Pass along the new physical address & page id. */
pPageDesc->HCPhys = ((uint64_t)pPage->Shared.pfn) << PAGE_SHIFT;
pPageDesc->uHCPhysPageId = pGlobalRegion->paHCPhysPageID[idxPage];
}
end:
return rc;
}
/**
* RTAvlGCPtrDestroy callback.
*
* @returns 0 or VERR_INTERNAL_ERROR.
* @param pNode The node to destroy.
* @param pvGVM The GVM handle.
*/
static DECLCALLBACK(int) gmmR0CleanupSharedModule(PAVLGCPTRNODECORE pNode, void *pvGVM)
{
PGVM pGVM = (PGVM)pvGVM;
PGMMSHAREDMODULEPERVM pRecVM = (PGMMSHAREDMODULEPERVM)pNode;
Assert(pRecVM->pGlobalModule || pRecVM->fCollision);
if (pRecVM->pGlobalModule)
{
PGMMSHAREDMODULE pRec = pRecVM->pGlobalModule;
AssertPtr(pRec);
Assert(pRec->cUsers);
Log(("gmmR0CleanupSharedModule: %s %s cUsers=%d\n", pRec->szName, pRec->szVersion, pRec->cUsers));
pRec->cUsers--;
if (pRec->cUsers == 0)
{
for (uint32_t i = 0; i < pRec->cRegions; i++)
if (pRec->aRegions[i].paHCPhysPageID)
RTMemFree(pRec->aRegions[i].paHCPhysPageID);
/* Remove from the tree and free memory. */
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
RTAvlGCPtrRemove(&pGMM->pGlobalSharedModuleTree, pRec->Core.Key);
RTMemFree(pRec);
}
}
RTMemFree(pRecVM);
return 0;
}
/**
* Used by GMMR0CleanupVM to clean up shared modules.
*
* This is called without taking the GMM lock so that it can be yielded as
* needed here.
*
* @param pGMM The GMM handle.
* @param pGVM The global VM handle.
*/
static void gmmR0SharedModuleCleanup(PGMM pGMM, PGVM pGVM)
{
gmmR0MutexAcquire(pGMM);
GMM_CHECK_SANITY_UPON_ENTERING(pGMM);
RTAvlGCPtrDestroy(&pGVM->gmm.s.pSharedModuleTree, gmmR0CleanupSharedModule, pGVM);
gmmR0MutexRelease(pGMM);
}
#endif /* VBOX_WITH_PAGE_SHARING */
/**
* Removes all shared modules for the specified VM
*
* @returns VBox status code.
* @param pVM VM handle
* @param idCpu VCPU id
*/
GMMR0DECL(int) GMMR0ResetSharedModules(PVM pVM, VMCPUID idCpu)
{
#ifdef VBOX_WITH_PAGE_SHARING
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
PGVM pGVM;
int rc = GVMMR0ByVMAndEMT(pVM, idCpu, &pGVM);
if (RT_FAILURE(rc))
return rc;
/*
* Take the semaphore and do some more validations.
*/
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
Log(("GMMR0ResetSharedModules\n"));
RTAvlGCPtrDestroy(&pGVM->gmm.s.pSharedModuleTree, gmmR0CleanupSharedModule, pGVM);
rc = VINF_SUCCESS;
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_INTERNAL_ERROR_5;
gmmR0MutexRelease(pGMM);
return rc;
#else
return VERR_NOT_IMPLEMENTED;
#endif
}
#ifdef VBOX_WITH_PAGE_SHARING
typedef struct
{
PGVM pGVM;
VMCPUID idCpu;
int rc;
} GMMCHECKSHAREDMODULEINFO, *PGMMCHECKSHAREDMODULEINFO;
/**
* Tree enumeration callback for checking a shared module.
*/
DECLCALLBACK(int) gmmR0CheckSharedModule(PAVLGCPTRNODECORE pNode, void *pvUser)
{
PGMMCHECKSHAREDMODULEINFO pInfo = (PGMMCHECKSHAREDMODULEINFO)pvUser;
PGMMSHAREDMODULEPERVM pLocalModule = (PGMMSHAREDMODULEPERVM)pNode;
PGMMSHAREDMODULE pGlobalModule = pLocalModule->pGlobalModule;
if ( !pLocalModule->fCollision
&& pGlobalModule)
{
Log(("gmmR0CheckSharedModule: check %s %s base=%RGv size=%x collision=%d\n", pGlobalModule->szName, pGlobalModule->szVersion, pGlobalModule->Core.Key, pGlobalModule->cbModule, pLocalModule->fCollision));
pInfo->rc = PGMR0SharedModuleCheck(pInfo->pGVM->pVM, pInfo->pGVM, pInfo->idCpu, pGlobalModule, pLocalModule->cRegions, pLocalModule->aRegions);
if (RT_FAILURE(pInfo->rc))
return 1; /* stop enumeration. */
}
return 0;
}
#endif /* VBOX_WITH_PAGE_SHARING */
#ifdef DEBUG_sandervl
/**
* Setup for a GMMR0CheckSharedModules call (to allow log flush jumps back to ring 3)
*
* @returns VBox status code.
* @param pVM VM handle
*/
GMMR0DECL(int) GMMR0CheckSharedModulesStart(PVM pVM)
{
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
/*
* Take the semaphore and do some more validations.
*/
gmmR0MutexAcquire(pGMM);
if (!GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
rc = VERR_INTERNAL_ERROR_5;
else
rc = VINF_SUCCESS;
return rc;
}
/**
* Clean up after a GMMR0CheckSharedModules call (to allow log flush jumps back to ring 3)
*
* @returns VBox status code.
* @param pVM VM handle
*/
GMMR0DECL(int) GMMR0CheckSharedModulesEnd(PVM pVM)
{
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
gmmR0MutexRelease(pGMM);
return VINF_SUCCESS;
}
#endif /* DEBUG_sandervl */
/**
* Check all shared modules for the specified VM
*
* @returns VBox status code.
* @param pVM VM handle
* @param pVCpu VMCPU handle
*/
GMMR0DECL(int) GMMR0CheckSharedModules(PVM pVM, PVMCPU pVCpu)
{
#ifdef VBOX_WITH_PAGE_SHARING
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
PGVM pGVM;
int rc = GVMMR0ByVMAndEMT(pVM, pVCpu->idCpu, &pGVM);
if (RT_FAILURE(rc))
return rc;
# ifndef DEBUG_sandervl
/*
* Take the semaphore and do some more validations.
*/
gmmR0MutexAcquire(pGMM);
# endif
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
GMMCHECKSHAREDMODULEINFO Info;
Log(("GMMR0CheckSharedModules\n"));
Info.pGVM = pGVM;
Info.idCpu = pVCpu->idCpu;
Info.rc = VINF_SUCCESS;
RTAvlGCPtrDoWithAll(&pGVM->gmm.s.pSharedModuleTree, true /* fFromLeft */, gmmR0CheckSharedModule, &Info);
rc = Info.rc;
Log(("GMMR0CheckSharedModules done!\n"));
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_INTERNAL_ERROR_5;
# ifndef DEBUG_sandervl
gmmR0MutexRelease(pGMM);
# endif
return rc;
#else
return VERR_NOT_IMPLEMENTED;
#endif
}
#if defined(VBOX_STRICT) && HC_ARCH_BITS == 64
typedef struct
{
PGVM pGVM;
PGMM pGMM;
uint8_t *pSourcePage;
bool fFoundDuplicate;
} GMMFINDDUPPAGEINFO, *PGMMFINDDUPPAGEINFO;
/**
* RTAvlU32DoWithAll callback.
*
* @returns 0
* @param pNode The node to search.
* @param pvInfo Pointer to the input parameters
*/
static DECLCALLBACK(int) gmmR0FindDupPageInChunk(PAVLU32NODECORE pNode, void *pvInfo)
{
PGMMCHUNK pChunk = (PGMMCHUNK)pNode;
PGMMFINDDUPPAGEINFO pInfo = (PGMMFINDDUPPAGEINFO)pvInfo;
PGVM pGVM = pInfo->pGVM;
PGMM pGMM = pInfo->pGMM;
uint8_t *pbChunk;
/* Only take chunks not mapped into this VM process; not entirely correct. */
if (!gmmR0IsChunkMapped(pGVM, pChunk, (PRTR3PTR)&pbChunk))
{
int rc = gmmR0MapChunk(pGMM, pGVM, pChunk, (PRTR3PTR)&pbChunk);
if (RT_SUCCESS(rc))
{
/*
* Look for duplicate pages
*/
unsigned iPage = (GMM_CHUNK_SIZE >> PAGE_SHIFT);
while (iPage-- > 0)
{
if (GMM_PAGE_IS_PRIVATE(&pChunk->aPages[iPage]))
{
uint8_t *pbDestPage = pbChunk + (iPage << PAGE_SHIFT);
if (!memcmp(pInfo->pSourcePage, pbDestPage, PAGE_SIZE))
{
pInfo->fFoundDuplicate = true;
break;
}
}
}
gmmR0UnmapChunk(pGMM, pGVM, pChunk);
}
}
return pInfo->fFoundDuplicate; /* (stops search if true) */
}
/**
* Find a duplicate of the specified page in other active VMs
*
* @returns VBox status code.
* @param pVM VM handle
* @param pReq Request packet
*/
GMMR0DECL(int) GMMR0FindDuplicatePageReq(PVM pVM, PGMMFINDDUPLICATEPAGEREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pVM, VERR_INVALID_POINTER);
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_INTERNAL_ERROR);
PGVM pGVM;
int rc = GVMMR0ByVM(pVM, &pGVM);
if (RT_FAILURE(rc))
return rc;
/*
* Take the semaphore and do some more validations.
*/
rc = gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
uint8_t *pbChunk;
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, pReq->idPage >> GMM_CHUNKID_SHIFT);
if (pChunk)
{
if (gmmR0IsChunkMapped(pGVM, pChunk, (PRTR3PTR)&pbChunk))
{
uint8_t *pbSourcePage = pbChunk + ((pReq->idPage & GMM_PAGEID_IDX_MASK) << PAGE_SHIFT);
PGMMPAGE pPage = gmmR0GetPage(pGMM, pReq->idPage);
if (pPage)
{
GMMFINDDUPPAGEINFO Info;
Info.pGVM = pGVM;
Info.pGMM = pGMM;
Info.pSourcePage = pbSourcePage;
Info.fFoundDuplicate = false;
RTAvlU32DoWithAll(&pGMM->pChunks, true /* fFromLeft */, gmmR0FindDupPageInChunk, &Info);
pReq->fDuplicate = Info.fFoundDuplicate;
}
else
{
AssertFailed();
rc = VERR_PGM_PHYS_INVALID_PAGE_ID;
}
}
else
AssertFailed();
}
else
AssertFailed();
}
else
rc = VERR_INTERNAL_ERROR_5;
gmmR0MutexRelease(pGMM);
return rc;
}
#endif /* VBOX_STRICT && HC_ARCH_BITS == 64 */