VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/PGMR0Pool.cpp@ 82968

Last change on this file since 82968 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1/* $Id: PGMR0Pool.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * PGM Shadow Page Pool, ring-0 specific bits.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_PGM_POOL
23#include <VBox/vmm/pgm.h>
24#include <VBox/vmm/hm.h>
25#include "PGMInternal.h"
26#include <VBox/vmm/vmcc.h>
27#include "PGMInline.h"
28
29#include <VBox/log.h>
30#include <VBox/err.h>
31#include <iprt/mem.h>
32#include <iprt/memobj.h>
33
34
35
36/**
37 * Grows the shadow page pool.
38 *
39 * I.e. adds more pages to it, assuming that hasn't reached cMaxPages yet.
40 *
41 * @returns VBox status code.
42 * @param pGVM The ring-0 VM structure.
43 */
44VMMR0_INT_DECL(int) PGMR0PoolGrow(PGVM pGVM)
45{
46 PPGMPOOL pPool = pGVM->pgm.s.pPoolR0;
47 AssertReturn(pPool->cCurPages < pPool->cMaxPages, VERR_PGM_POOL_MAXED_OUT_ALREADY);
48 AssertReturn(pPool->pVMR3 == pGVM->pVMR3, VERR_PGM_POOL_IPE);
49 AssertReturn(pPool->pVMR0 == pGVM, VERR_PGM_POOL_IPE);
50
51 /* With 32-bit guests and no EPT, the CR3 limits the root pages to low
52 (below 4 GB) memory. */
53 /** @todo change the pool to handle ROOT page allocations specially when
54 * required. */
55 bool const fCanUseHighMemory = HMIsNestedPagingActive(pGVM);
56
57 STAM_REL_PROFILE_START(&pPool->StatGrow, a);
58 int rc = RTCritSectEnter(&pGVM->pgmr0.s.PoolGrowCritSect);
59 AssertRCReturn(rc, rc);
60
61 /*
62 * Figure out how many pages should allocate.
63 */
64 uint32_t const cMaxPages = RT_MIN(pPool->cMaxPages, PGMPOOL_IDX_LAST);
65 uint32_t const cCurPages = RT_MIN(pPool->cCurPages, cMaxPages);
66 if (cCurPages < cMaxPages)
67 {
68 uint32_t cNewPages = cMaxPages - cCurPages;
69 if (cNewPages > PGMPOOL_CFG_MAX_GROW)
70 cNewPages = PGMPOOL_CFG_MAX_GROW;
71 LogFlow(("PGMR3PoolGrow: Growing the pool by %u (%#x) pages to %u (%#x) pages. fCanUseHighMemory=%RTbool\n",
72 cNewPages, cNewPages, cCurPages + cNewPages, cCurPages + cNewPages, fCanUseHighMemory));
73
74 /* Check that the handles in the arrays entry are both NIL. */
75 uintptr_t const idxMemHandle = cCurPages / (PGMPOOL_CFG_MAX_GROW);
76 AssertCompile( (PGMPOOL_IDX_LAST + (PGMPOOL_CFG_MAX_GROW - 1)) / PGMPOOL_CFG_MAX_GROW
77 <= RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMemObjs));
78 AssertCompile(RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMemObjs) == RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMapObjs));
79 AssertLogRelMsgReturnStmt( pGVM->pgmr0.s.ahPoolMemObjs[idxMemHandle] == NIL_RTR0MEMOBJ
80 && pGVM->pgmr0.s.ahPoolMapObjs[idxMemHandle] == NIL_RTR0MEMOBJ,
81 ("idxMemHandle=%#x\n", idxMemHandle), RTCritSectLeave(&pGVM->pgmr0.s.PoolGrowCritSect),
82 VERR_PGM_POOL_IPE);
83
84 /*
85 * Allocate the new pages and map them into ring-3.
86 */
87 RTR0MEMOBJ hMemObj = NIL_RTR0MEMOBJ;
88 if (fCanUseHighMemory)
89 rc = RTR0MemObjAllocPage(&hMemObj, cNewPages * PAGE_SIZE, false /*fExecutable*/);
90 else
91 rc = RTR0MemObjAllocLow(&hMemObj, cNewPages * PAGE_SIZE, false /*fExecutable*/);
92 if (RT_SUCCESS(rc))
93 {
94 RTR0MEMOBJ hMapObj = NIL_RTR0MEMOBJ;
95 rc = RTR0MemObjMapUser(&hMapObj, hMemObj, (RTR3PTR)-1, 0, RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
96 if (RT_SUCCESS(rc))
97 {
98 pGVM->pgmr0.s.ahPoolMemObjs[idxMemHandle] = hMemObj;
99 pGVM->pgmr0.s.ahPoolMapObjs[idxMemHandle] = hMapObj;
100
101 uint8_t *pbRing0 = (uint8_t *)RTR0MemObjAddress(hMemObj);
102 RTR3PTR pbRing3 = RTR0MemObjAddressR3(hMapObj);
103 AssertPtr(pbRing0);
104 Assert(((uintptr_t)pbRing0 & PAGE_OFFSET_MASK) == 0);
105 Assert(pbRing3 != NIL_RTR3PTR);
106 Assert((pbRing3 & PAGE_OFFSET_MASK) == 0);
107
108 /*
109 * Initialize the new pages.
110 */
111 for (unsigned iNewPage = 0; iNewPage < cNewPages; iNewPage++)
112 {
113 PPGMPOOLPAGE pPage = &pPool->aPages[cCurPages + iNewPage];
114 pPage->pvPageR0 = &pbRing0[iNewPage * PAGE_SIZE];
115 pPage->pvPageR3 = pbRing3 + iNewPage * PAGE_SIZE;
116 pPage->Core.Key = RTR0MemObjGetPagePhysAddr(hMemObj, iNewPage);
117 AssertFatal(pPage->Core.Key < _4G || fCanUseHighMemory);
118 pPage->GCPhys = NIL_RTGCPHYS;
119 pPage->enmKind = PGMPOOLKIND_FREE;
120 pPage->idx = pPage - &pPool->aPages[0];
121 LogFlow(("PGMR3PoolGrow: insert page #%#x - %RHp\n", pPage->idx, pPage->Core.Key));
122 pPage->iNext = pPool->iFreeHead;
123 pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
124 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
125 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
126 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
127 pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
128 pPage->iAgeNext = NIL_PGMPOOL_IDX;
129 pPage->iAgePrev = NIL_PGMPOOL_IDX;
130 /* commit it */
131 bool fRc = RTAvloHCPhysInsert(&pPool->HCPhysTree, &pPage->Core); Assert(fRc); NOREF(fRc);
132 pPool->iFreeHead = cCurPages + iNewPage;
133 pPool->cCurPages = cCurPages + iNewPage + 1;
134 }
135
136 STAM_REL_PROFILE_STOP(&pPool->StatGrow, a);
137 RTCritSectLeave(&pGVM->pgmr0.s.PoolGrowCritSect);
138 return VINF_SUCCESS;
139 }
140
141 RTR0MemObjFree(hMemObj, true /*fFreeMappings*/);
142 }
143 if (cCurPages > 64)
144 LogRelMax(5, ("PGMR0PoolGrow: rc=%Rrc cNewPages=%#x cCurPages=%#x cMaxPages=%#x fCanUseHighMemory=%d\n",
145 rc, cNewPages, cCurPages, cMaxPages, fCanUseHighMemory));
146 else
147 LogRel(("PGMR0PoolGrow: rc=%Rrc cNewPages=%#x cCurPages=%#x cMaxPages=%#x fCanUseHighMemory=%d\n",
148 rc, cNewPages, cCurPages, cMaxPages, fCanUseHighMemory));
149 }
150 RTCritSectLeave(&pGVM->pgmr0.s.PoolGrowCritSect);
151 return rc;
152}
153
Note: See TracBrowser for help on using the repository browser.

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