VirtualBox

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

Last change on this file since 87792 was 86473, checked in by vboxsync, 4 years ago

VMM/PGM: Working on eliminating page table bitfield use. bugref:9841 bugref:9746

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
1/* $Id: PGMR0Pool.cpp 86473 2020-10-07 17:30:25Z 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#define VBOX_WITHOUT_PAGING_BIT_FIELDS /* 64-bit bitfields are just asking for trouble. See @bugref{9841} and others. */
24#include <VBox/vmm/pgm.h>
25#include <VBox/vmm/hm.h>
26#include "PGMInternal.h"
27#include <VBox/vmm/vmcc.h>
28#include "PGMInline.h"
29
30#include <VBox/log.h>
31#include <VBox/err.h>
32#include <iprt/mem.h>
33#include <iprt/memobj.h>
34
35
36
37/**
38 * Grows the shadow page pool.
39 *
40 * I.e. adds more pages to it, assuming that hasn't reached cMaxPages yet.
41 *
42 * @returns VBox status code.
43 * @param pGVM The ring-0 VM structure.
44 */
45VMMR0_INT_DECL(int) PGMR0PoolGrow(PGVM pGVM)
46{
47 PPGMPOOL pPool = pGVM->pgm.s.pPoolR0;
48 AssertReturn(pPool->cCurPages < pPool->cMaxPages, VERR_PGM_POOL_MAXED_OUT_ALREADY);
49 AssertReturn(pPool->pVMR3 == pGVM->pVMR3, VERR_PGM_POOL_IPE);
50 AssertReturn(pPool->pVMR0 == pGVM, VERR_PGM_POOL_IPE);
51
52 /* With 32-bit guests and no EPT, the CR3 limits the root pages to low
53 (below 4 GB) memory. */
54 /** @todo change the pool to handle ROOT page allocations specially when
55 * required. */
56 bool const fCanUseHighMemory = HMIsNestedPagingActive(pGVM);
57
58 STAM_REL_PROFILE_START(&pPool->StatGrow, a);
59 int rc = RTCritSectEnter(&pGVM->pgmr0.s.PoolGrowCritSect);
60 AssertRCReturn(rc, rc);
61
62 /*
63 * Figure out how many pages should allocate.
64 */
65 uint32_t const cMaxPages = RT_MIN(pPool->cMaxPages, PGMPOOL_IDX_LAST);
66 uint32_t const cCurPages = RT_MIN(pPool->cCurPages, cMaxPages);
67 if (cCurPages < cMaxPages)
68 {
69 uint32_t cNewPages = cMaxPages - cCurPages;
70 if (cNewPages > PGMPOOL_CFG_MAX_GROW)
71 cNewPages = PGMPOOL_CFG_MAX_GROW;
72 LogFlow(("PGMR3PoolGrow: Growing the pool by %u (%#x) pages to %u (%#x) pages. fCanUseHighMemory=%RTbool\n",
73 cNewPages, cNewPages, cCurPages + cNewPages, cCurPages + cNewPages, fCanUseHighMemory));
74
75 /* Check that the handles in the arrays entry are both NIL. */
76 uintptr_t const idxMemHandle = cCurPages / (PGMPOOL_CFG_MAX_GROW);
77 AssertCompile( (PGMPOOL_IDX_LAST + (PGMPOOL_CFG_MAX_GROW - 1)) / PGMPOOL_CFG_MAX_GROW
78 <= RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMemObjs));
79 AssertCompile(RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMemObjs) == RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMapObjs));
80 AssertLogRelMsgReturnStmt( pGVM->pgmr0.s.ahPoolMemObjs[idxMemHandle] == NIL_RTR0MEMOBJ
81 && pGVM->pgmr0.s.ahPoolMapObjs[idxMemHandle] == NIL_RTR0MEMOBJ,
82 ("idxMemHandle=%#x\n", idxMemHandle), RTCritSectLeave(&pGVM->pgmr0.s.PoolGrowCritSect),
83 VERR_PGM_POOL_IPE);
84
85 /*
86 * Allocate the new pages and map them into ring-3.
87 */
88 RTR0MEMOBJ hMemObj = NIL_RTR0MEMOBJ;
89 if (fCanUseHighMemory)
90 rc = RTR0MemObjAllocPage(&hMemObj, cNewPages * PAGE_SIZE, false /*fExecutable*/);
91 else
92 rc = RTR0MemObjAllocLow(&hMemObj, cNewPages * PAGE_SIZE, false /*fExecutable*/);
93 if (RT_SUCCESS(rc))
94 {
95 RTR0MEMOBJ hMapObj = NIL_RTR0MEMOBJ;
96 rc = RTR0MemObjMapUser(&hMapObj, hMemObj, (RTR3PTR)-1, 0, RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
97 if (RT_SUCCESS(rc))
98 {
99 pGVM->pgmr0.s.ahPoolMemObjs[idxMemHandle] = hMemObj;
100 pGVM->pgmr0.s.ahPoolMapObjs[idxMemHandle] = hMapObj;
101
102 uint8_t *pbRing0 = (uint8_t *)RTR0MemObjAddress(hMemObj);
103 RTR3PTR pbRing3 = RTR0MemObjAddressR3(hMapObj);
104 AssertPtr(pbRing0);
105 Assert(((uintptr_t)pbRing0 & PAGE_OFFSET_MASK) == 0);
106 Assert(pbRing3 != NIL_RTR3PTR);
107 Assert((pbRing3 & PAGE_OFFSET_MASK) == 0);
108
109 /*
110 * Initialize the new pages.
111 */
112 for (unsigned iNewPage = 0; iNewPage < cNewPages; iNewPage++)
113 {
114 PPGMPOOLPAGE pPage = &pPool->aPages[cCurPages + iNewPage];
115 pPage->pvPageR0 = &pbRing0[iNewPage * PAGE_SIZE];
116 pPage->pvPageR3 = pbRing3 + iNewPage * PAGE_SIZE;
117 pPage->Core.Key = RTR0MemObjGetPagePhysAddr(hMemObj, iNewPage);
118 AssertFatal(pPage->Core.Key < _4G || fCanUseHighMemory);
119 pPage->GCPhys = NIL_RTGCPHYS;
120 pPage->enmKind = PGMPOOLKIND_FREE;
121 pPage->idx = pPage - &pPool->aPages[0];
122 LogFlow(("PGMR3PoolGrow: insert page #%#x - %RHp\n", pPage->idx, pPage->Core.Key));
123 pPage->iNext = pPool->iFreeHead;
124 pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
125 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
126 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
127 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
128 pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
129 pPage->iAgeNext = NIL_PGMPOOL_IDX;
130 pPage->iAgePrev = NIL_PGMPOOL_IDX;
131 /* commit it */
132 bool fRc = RTAvloHCPhysInsert(&pPool->HCPhysTree, &pPage->Core); Assert(fRc); NOREF(fRc);
133 pPool->iFreeHead = cCurPages + iNewPage;
134 pPool->cCurPages = cCurPages + iNewPage + 1;
135 }
136
137 STAM_REL_PROFILE_STOP(&pPool->StatGrow, a);
138 RTCritSectLeave(&pGVM->pgmr0.s.PoolGrowCritSect);
139 return VINF_SUCCESS;
140 }
141
142 RTR0MemObjFree(hMemObj, true /*fFreeMappings*/);
143 }
144 if (cCurPages > 64)
145 LogRelMax(5, ("PGMR0PoolGrow: rc=%Rrc cNewPages=%#x cCurPages=%#x cMaxPages=%#x fCanUseHighMemory=%d\n",
146 rc, cNewPages, cCurPages, cMaxPages, fCanUseHighMemory));
147 else
148 LogRel(("PGMR0PoolGrow: rc=%Rrc cNewPages=%#x cCurPages=%#x cMaxPages=%#x fCanUseHighMemory=%d\n",
149 rc, cNewPages, cCurPages, cMaxPages, fCanUseHighMemory));
150 }
151 RTCritSectLeave(&pGVM->pgmr0.s.PoolGrowCritSect);
152 return rc;
153}
154
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