VirtualBox

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

Last change on this file since 99725 was 98103, checked in by vboxsync, 2 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: 8.6 KB
Line 
1/* $Id: PGMR0Pool.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * PGM Shadow Page Pool, ring-0 specific bits.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_PGM_POOL
33#define VBOX_WITHOUT_PAGING_BIT_FIELDS /* 64-bit bitfields are just asking for trouble. See @bugref{9841} and others. */
34#include <VBox/vmm/pgm.h>
35#include <VBox/vmm/hm.h>
36#include "PGMInternal.h"
37#include <VBox/vmm/vmcc.h>
38#include "PGMInline.h"
39
40#include <VBox/log.h>
41#include <VBox/err.h>
42#include <iprt/mem.h>
43#include <iprt/memobj.h>
44
45
46/**
47 * Called by PGMR0InitVM to complete the page pool setup for ring-0.
48 *
49 * @returns VBox status code.
50 * @param pGVM Pointer to the global VM structure.
51 */
52int pgmR0PoolInitVM(PGVM pGVM)
53{
54 PPGMPOOL pPool = pGVM->pgm.s.pPoolR0;
55 AssertPtrReturn(pPool, VERR_PGM_POOL_IPE);
56
57 int rc = PGMR0HandlerPhysicalTypeSetUpContext(pGVM, PGMPHYSHANDLERKIND_WRITE, PGMPHYSHANDLER_F_KEEP_PGM_LOCK,
58 pgmPoolAccessHandler, pgmRZPoolAccessPfHandler,
59 "Guest Paging Access Handler", pPool->hAccessHandlerType);
60 AssertLogRelRCReturn(rc, rc);
61
62 return VINF_SUCCESS;
63}
64
65
66/**
67 * Worker for PGMR0PoolGrow.
68 */
69static int pgmR0PoolGrowInner(PGVM pGVM, PPGMPOOL pPool)
70{
71 int rc;
72
73 /* With 32-bit guests and no EPT, the CR3 limits the root pages to low
74 (below 4 GB) memory. */
75 /** @todo change the pool to handle ROOT page allocations specially when
76 * required. */
77 bool const fCanUseHighMemory = HMIsNestedPagingActive(pGVM);
78
79 /*
80 * Figure out how many pages should allocate.
81 */
82 uint32_t const cMaxPages = RT_MIN(pPool->cMaxPages, PGMPOOL_IDX_LAST);
83 uint32_t const cCurPages = RT_MIN(pPool->cCurPages, cMaxPages);
84 if (cCurPages < cMaxPages)
85 {
86 uint32_t cNewPages = cMaxPages - cCurPages;
87 if (cNewPages > PGMPOOL_CFG_MAX_GROW)
88 cNewPages = PGMPOOL_CFG_MAX_GROW;
89 LogFlow(("PGMR0PoolGrow: Growing the pool by %u (%#x) pages to %u (%#x) pages. fCanUseHighMemory=%RTbool\n",
90 cNewPages, cNewPages, cCurPages + cNewPages, cCurPages + cNewPages, fCanUseHighMemory));
91
92 /* Check that the handles in the arrays entry are both NIL. */
93 uintptr_t const idxMemHandle = cCurPages / (PGMPOOL_CFG_MAX_GROW);
94 AssertCompile( (PGMPOOL_IDX_LAST + (PGMPOOL_CFG_MAX_GROW - 1)) / PGMPOOL_CFG_MAX_GROW
95 <= RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMemObjs));
96 AssertCompile(RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMemObjs) == RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMapObjs));
97 AssertLogRelMsgReturn( pGVM->pgmr0.s.ahPoolMemObjs[idxMemHandle] == NIL_RTR0MEMOBJ
98 && pGVM->pgmr0.s.ahPoolMapObjs[idxMemHandle] == NIL_RTR0MEMOBJ, ("idxMemHandle=%#x\n", idxMemHandle),
99 VERR_PGM_POOL_IPE);
100
101 /*
102 * Allocate the new pages and map them into ring-3.
103 */
104 RTR0MEMOBJ hMemObj = NIL_RTR0MEMOBJ;
105 if (fCanUseHighMemory)
106 rc = RTR0MemObjAllocPage(&hMemObj, cNewPages * HOST_PAGE_SIZE, false /*fExecutable*/);
107 else
108 rc = RTR0MemObjAllocLow(&hMemObj, cNewPages * HOST_PAGE_SIZE, false /*fExecutable*/);
109 if (RT_SUCCESS(rc))
110 {
111 RTR0MEMOBJ hMapObj = NIL_RTR0MEMOBJ;
112 rc = RTR0MemObjMapUser(&hMapObj, hMemObj, (RTR3PTR)-1, 0, RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
113 if (RT_SUCCESS(rc))
114 {
115 pGVM->pgmr0.s.ahPoolMemObjs[idxMemHandle] = hMemObj;
116 pGVM->pgmr0.s.ahPoolMapObjs[idxMemHandle] = hMapObj;
117
118 uint8_t *pbRing0 = (uint8_t *)RTR0MemObjAddress(hMemObj);
119 RTR3PTR pbRing3 = RTR0MemObjAddressR3(hMapObj);
120 AssertPtr(pbRing0);
121 Assert(((uintptr_t)pbRing0 & HOST_PAGE_OFFSET_MASK) == 0);
122 Assert(pbRing3 != NIL_RTR3PTR);
123 Assert((pbRing3 & HOST_PAGE_OFFSET_MASK) == 0);
124
125 /*
126 * Initialize the new pages.
127 */
128 for (unsigned iNewPage = 0; iNewPage < cNewPages; iNewPage++)
129 {
130 PPGMPOOLPAGE pPage = &pPool->aPages[cCurPages + iNewPage];
131 pPage->pvPageR0 = &pbRing0[iNewPage * HOST_PAGE_SIZE];
132 pPage->pvPageR3 = pbRing3 + iNewPage * HOST_PAGE_SIZE;
133 pPage->Core.Key = RTR0MemObjGetPagePhysAddr(hMemObj, iNewPage);
134 AssertFatal(pPage->Core.Key < _4G || fCanUseHighMemory);
135 pPage->GCPhys = NIL_RTGCPHYS;
136 pPage->enmKind = PGMPOOLKIND_FREE;
137 pPage->idx = pPage - &pPool->aPages[0];
138 LogFlow(("PGMR0PoolGrow: insert page #%#x - %RHp\n", pPage->idx, pPage->Core.Key));
139 pPage->iNext = pPool->iFreeHead;
140 pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
141 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
142 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
143 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
144 pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
145 pPage->iAgeNext = NIL_PGMPOOL_IDX;
146 pPage->iAgePrev = NIL_PGMPOOL_IDX;
147 /* commit it */
148 bool fRc = RTAvloHCPhysInsert(&pPool->HCPhysTree, &pPage->Core); Assert(fRc); NOREF(fRc);
149 pPool->iFreeHead = cCurPages + iNewPage;
150 pPool->cCurPages = cCurPages + iNewPage + 1;
151 }
152
153 return VINF_SUCCESS;
154 }
155
156 RTR0MemObjFree(hMemObj, true /*fFreeMappings*/);
157 }
158 if (cCurPages > 64)
159 LogRelMax(5, ("PGMR0PoolGrow: rc=%Rrc cNewPages=%#x cCurPages=%#x cMaxPages=%#x fCanUseHighMemory=%d\n",
160 rc, cNewPages, cCurPages, cMaxPages, fCanUseHighMemory));
161 else
162 LogRel(("PGMR0PoolGrow: rc=%Rrc cNewPages=%#x cCurPages=%#x cMaxPages=%#x fCanUseHighMemory=%d\n",
163 rc, cNewPages, cCurPages, cMaxPages, fCanUseHighMemory));
164 }
165 else
166 rc = VINF_SUCCESS;
167 return rc;
168}
169
170
171/**
172 * Grows the shadow page pool.
173 *
174 * I.e. adds more pages to it, assuming that hasn't reached cMaxPages yet.
175 *
176 * @returns VBox status code.
177 * @param pGVM The ring-0 VM structure.
178 * @param idCpu The ID of the calling EMT.
179 * @thread EMT(idCpu)
180 */
181VMMR0_INT_DECL(int) PGMR0PoolGrow(PGVM pGVM, VMCPUID idCpu)
182{
183 /*
184 * Validate input.
185 */
186 PPGMPOOL pPool = pGVM->pgm.s.pPoolR0;
187 AssertReturn(pPool->cCurPages < pPool->cMaxPages, VERR_PGM_POOL_MAXED_OUT_ALREADY);
188 AssertReturn(pPool->pVMR3 == pGVM->pVMR3, VERR_PGM_POOL_IPE);
189 AssertReturn(pPool->pVMR0 == pGVM, VERR_PGM_POOL_IPE);
190
191 AssertReturn(idCpu < pGVM->cCpus, VERR_VM_THREAD_NOT_EMT);
192 PGVMCPU const pGVCpu = &pGVM->aCpus[idCpu];
193
194 /*
195 * Enter the grow critical section and call worker.
196 */
197 STAM_REL_PROFILE_START(&pPool->StatGrow, a);
198
199 VMMR0EMTBLOCKCTX Ctx;
200 int rc = VMMR0EmtPrepareToBlock(pGVCpu, VINF_SUCCESS, __FUNCTION__, &pGVM->pgmr0.s.PoolGrowCritSect, &Ctx);
201 AssertRCReturn(rc, rc);
202
203 rc = RTCritSectEnter(&pGVM->pgmr0.s.PoolGrowCritSect);
204 AssertRCReturn(rc, rc);
205
206 rc = pgmR0PoolGrowInner(pGVM, pPool);
207
208 STAM_REL_PROFILE_STOP(&pPool->StatGrow, a);
209 RTCritSectLeave(&pGVM->pgmr0.s.PoolGrowCritSect);
210
211 VMMR0EmtResumeAfterBlocking(pGVCpu, &Ctx);
212 return rc;
213}
214
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