1 | /* $Id: PGMR0Pool.cpp 82567 2019-12-12 10:37:49Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PGM Shadow Page Pool, ring-0 specific bits.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 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 | */
|
---|
44 | VMMR0_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 |
|
---|
49 | /* With 32-bit guests and no EPT, the CR3 limits the root pages to low
|
---|
50 | (below 4 GB) memory. */
|
---|
51 | /** @todo change the pool to handle ROOT page allocations specially when
|
---|
52 | * required. */
|
---|
53 | bool const fCanUseHighMemory = HMIsNestedPagingActive(pGVM);
|
---|
54 |
|
---|
55 | STAM_REL_PROFILE_START(&pPool->StatGrow, a);
|
---|
56 | int rc = RTCritSectEnter(&pGVM->pgmr0.s.PoolGrowCritSect);
|
---|
57 | AssertRCReturn(rc, rc);
|
---|
58 |
|
---|
59 | /*
|
---|
60 | * Figure out how many pages should allocate.
|
---|
61 | */
|
---|
62 | uint32_t const cMaxPages = RT_MIN(pPool->cMaxPages, PGMPOOL_IDX_LAST);
|
---|
63 | uint32_t const cCurPages = RT_MIN(pPool->cCurPages, cMaxPages);
|
---|
64 | if (cCurPages < cMaxPages)
|
---|
65 | {
|
---|
66 | uint32_t cNewPages = cMaxPages - cCurPages;
|
---|
67 | if (cNewPages > PGMPOOL_CFG_MAX_GROW)
|
---|
68 | cNewPages = PGMPOOL_CFG_MAX_GROW;
|
---|
69 | LogFlow(("PGMR3PoolGrow: Growing the pool by %u (%#x) pages to %u (%#x) pages. fCanUseHighMemory=%RTbool\n",
|
---|
70 | cNewPages, cNewPages, cCurPages + cNewPages, cCurPages + cNewPages, fCanUseHighMemory));
|
---|
71 |
|
---|
72 | /* Check that the handles in the arrays entry are both NIL. */
|
---|
73 | uintptr_t const idxMemHandle = cCurPages / (PGMPOOL_CFG_MAX_GROW);
|
---|
74 | AssertCompile( (PGMPOOL_IDX_LAST + (PGMPOOL_CFG_MAX_GROW - 1)) / PGMPOOL_CFG_MAX_GROW
|
---|
75 | <= RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMemObjs));
|
---|
76 | AssertCompile(RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMemObjs) == RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMapObjs));
|
---|
77 | AssertLogRelMsgReturnStmt( pGVM->pgmr0.s.ahPoolMemObjs[idxMemHandle] == NIL_RTR0MEMOBJ
|
---|
78 | && pGVM->pgmr0.s.ahPoolMapObjs[idxMemHandle] == NIL_RTR0MEMOBJ,
|
---|
79 | ("idxMemHandle=%#x\n", idxMemHandle), RTCritSectLeave(&pGVM->pgmr0.s.PoolGrowCritSect),
|
---|
80 | VERR_PGM_POOL_IPE);
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Allocate the new pages and map them into ring-3.
|
---|
84 | */
|
---|
85 | RTR0MEMOBJ hMemObj = NIL_RTR0MEMOBJ;
|
---|
86 | if (fCanUseHighMemory)
|
---|
87 | rc = RTR0MemObjAllocPage(&hMemObj, cNewPages * PAGE_SIZE, false /*fExecutable*/);
|
---|
88 | else
|
---|
89 | rc = RTR0MemObjAllocLow(&hMemObj, cNewPages * PAGE_SIZE, false /*fExecutable*/);
|
---|
90 | if (RT_SUCCESS(rc))
|
---|
91 | {
|
---|
92 | RTR0MEMOBJ hMapObj = NIL_RTR0MEMOBJ;
|
---|
93 | rc = RTR0MemObjMapUser(&hMapObj, hMemObj, (RTR3PTR)-1, 0, RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
|
---|
94 | if (RT_SUCCESS(rc))
|
---|
95 | {
|
---|
96 | pGVM->pgmr0.s.ahPoolMemObjs[idxMemHandle] = hMemObj;
|
---|
97 | pGVM->pgmr0.s.ahPoolMapObjs[idxMemHandle] = hMapObj;
|
---|
98 |
|
---|
99 | uint8_t *pbRing0 = (uint8_t *)RTR0MemObjAddress(hMemObj);
|
---|
100 | RTR3PTR pbRing3 = RTR0MemObjAddressR3(hMapObj);
|
---|
101 | AssertPtr(pbRing0);
|
---|
102 | Assert(((uintptr_t)pbRing0 & PAGE_OFFSET_MASK) == 0);
|
---|
103 | Assert(pbRing3 != NIL_RTR3PTR);
|
---|
104 | Assert((pbRing3 & PAGE_OFFSET_MASK) == 0);
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Initialize the new pages.
|
---|
108 | */
|
---|
109 | for (unsigned iNewPage = 0; iNewPage < cNewPages; iNewPage++)
|
---|
110 | {
|
---|
111 | PPGMPOOLPAGE pPage = &pPool->aPages[cCurPages + iNewPage];
|
---|
112 | pPage->pvPageR0 = &pbRing0[iNewPage * PAGE_SIZE];
|
---|
113 | pPage->pvPageR3 = pbRing3 + iNewPage * PAGE_SIZE;
|
---|
114 | pPage->Core.Key = RTR0MemObjGetPagePhysAddr(hMemObj, iNewPage);
|
---|
115 | AssertFatal(pPage->Core.Key < _4G || fCanUseHighMemory);
|
---|
116 | pPage->GCPhys = NIL_RTGCPHYS;
|
---|
117 | pPage->enmKind = PGMPOOLKIND_FREE;
|
---|
118 | pPage->idx = pPage - &pPool->aPages[0];
|
---|
119 | LogFlow(("PGMR3PoolGrow: insert page #%#x - %RHp\n", pPage->idx, pPage->Core.Key));
|
---|
120 | pPage->iNext = pPool->iFreeHead;
|
---|
121 | pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
|
---|
122 | pPage->iModifiedNext = NIL_PGMPOOL_IDX;
|
---|
123 | pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
|
---|
124 | pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
|
---|
125 | pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
|
---|
126 | pPage->iAgeNext = NIL_PGMPOOL_IDX;
|
---|
127 | pPage->iAgePrev = NIL_PGMPOOL_IDX;
|
---|
128 | /* commit it */
|
---|
129 | bool fRc = RTAvloHCPhysInsert(&pPool->HCPhysTree, &pPage->Core); Assert(fRc); NOREF(fRc);
|
---|
130 | pPool->iFreeHead = cCurPages + iNewPage;
|
---|
131 | pPool->cCurPages = cCurPages + iNewPage + 1;
|
---|
132 | }
|
---|
133 |
|
---|
134 | STAM_REL_PROFILE_STOP(&pPool->StatGrow, a);
|
---|
135 | RTCritSectLeave(&pGVM->pgmr0.s.PoolGrowCritSect);
|
---|
136 | return VINF_SUCCESS;
|
---|
137 | }
|
---|
138 |
|
---|
139 | RTR0MemObjFree(hMemObj, true /*fFreeMappings*/);
|
---|
140 | }
|
---|
141 | }
|
---|
142 | RTCritSectLeave(&pGVM->pgmr0.s.PoolGrowCritSect);
|
---|
143 | return VINF_SUCCESS;
|
---|
144 | }
|
---|
145 |
|
---|