1 | /* $Id: bs3-cmn-PagingInitRootForPP.c 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - Bs3PagingInitRootForPP
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2024 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 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "bs3kit-template-header.h"
|
---|
42 | #include "bs3-cmn-paging.h"
|
---|
43 | #include "bs3-cmn-memory.h" /* bad bird */
|
---|
44 | #include <iprt/param.h>
|
---|
45 |
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Creates page tables for a section of the page directory.
|
---|
49 | *
|
---|
50 | * @returns VINF_SUCCESS or VERR_NO_MEMORY.
|
---|
51 | * @param pPgDir The page directory.
|
---|
52 | * @param iFirst The first PD entry.
|
---|
53 | * @param cEntries How many PD entries to create pages tables for.
|
---|
54 | */
|
---|
55 | static int Bs3PagingInitPageTablesForPgDir(X86PD BS3_FAR *pPgDir, unsigned iFirst, unsigned cEntries)
|
---|
56 | {
|
---|
57 | uint32_t uCurPhys = (uint32_t)iFirst << X86_PD_SHIFT;
|
---|
58 |
|
---|
59 | while (cEntries--)
|
---|
60 | {
|
---|
61 | X86PT BS3_FAR *pPt = (X86PT BS3_FAR *)Bs3MemAlloc(BS3MEMKIND_TILED, _4K);
|
---|
62 | if (pPt)
|
---|
63 | {
|
---|
64 | unsigned j = 0;
|
---|
65 | for (j = 0; j < RT_ELEMENTS(pPt->a); j++, uCurPhys += PAGE_SIZE)
|
---|
66 | {
|
---|
67 | pPt->a[j].u = uCurPhys;
|
---|
68 | pPt->a[j].u |= X86_PTE_P | X86_PTE_RW | X86_PTE_US | X86_PTE_A | X86_PTE_D;
|
---|
69 | }
|
---|
70 | pPgDir->a[iFirst].u = Bs3SelPtrToFlat(pPt);
|
---|
71 | pPgDir->a[iFirst].u |= X86_PDE_P | X86_PDE_RW | X86_PDE_US | X86_PDE_A;
|
---|
72 | iFirst++;
|
---|
73 | }
|
---|
74 | else
|
---|
75 | return VERR_NO_MEMORY;
|
---|
76 | }
|
---|
77 | return VINF_SUCCESS;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | #undef Bs3PagingInitRootForPP
|
---|
82 | BS3_CMN_DEF(int, Bs3PagingInitRootForPP,(void))
|
---|
83 | {
|
---|
84 | X86PD BS3_FAR *pPgDir;
|
---|
85 |
|
---|
86 | BS3_ASSERT(g_PhysPagingRootPP == UINT32_MAX);
|
---|
87 |
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * By default we do a identity mapping of the entire address space
|
---|
91 | * using 4 GB pages. So, we only really need one page directory,
|
---|
92 | * that's all.
|
---|
93 | *
|
---|
94 | * ASSUMES page size extension available, i.e. pentium+.
|
---|
95 | */
|
---|
96 | pPgDir = (X86PD BS3_FAR *)Bs3MemAllocZ(BS3MEMKIND_TILED, _4K);
|
---|
97 | if (pPgDir)
|
---|
98 | {
|
---|
99 | BS3_XPTR_AUTO(X86PD, XptrPgDir);
|
---|
100 | unsigned i;
|
---|
101 | int rc = VINF_SUCCESS;
|
---|
102 |
|
---|
103 | if (g_uBs3CpuDetected & BS3CPU_F_PSE)
|
---|
104 | {
|
---|
105 | for (i = 0; i < RT_ELEMENTS(pPgDir->a); i++)
|
---|
106 | {
|
---|
107 | pPgDir->a[i].u = (uint32_t)i << X86_PD_SHIFT;
|
---|
108 | pPgDir->a[i].u |= X86_PDE4M_P | X86_PDE4M_RW | X86_PDE4M_US | X86_PDE4M_PS | X86_PDE4M_A | X86_PDE4M_D;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /* Free up 4 consequtive entries for raw-mode hypervisor code. */
|
---|
112 | if (1) /** @todo detect raw-mode and only do this then. */
|
---|
113 | for (i = 0; i < 4; i++)
|
---|
114 | pPgDir->a[i + (UINT32_C(0xc0000000) >> X86_PD_SHIFT)].b.u1Present = 0;
|
---|
115 | }
|
---|
116 | else
|
---|
117 | {
|
---|
118 | /*
|
---|
119 | * This requires 4MB of page tables if we map everything.
|
---|
120 | * So, we check how much memory we have available and make sure we
|
---|
121 | * don't use all of it for page tables.
|
---|
122 | */
|
---|
123 | unsigned cMax = RT_ELEMENTS(pPgDir->a);
|
---|
124 | uint32_t cFreePages = g_Bs3Mem4KUpperTiled.Core.cFreeChunks + g_Bs3Mem4KLow.Core.cFreeChunks;
|
---|
125 | if (cFreePages >= cMax + 128)
|
---|
126 | Bs3PagingInitPageTablesForPgDir(pPgDir, 0, cMax);
|
---|
127 | else
|
---|
128 | {
|
---|
129 | unsigned cTop;
|
---|
130 | if (cMax >= 256 /*1MB*/)
|
---|
131 | {
|
---|
132 | cMax = cFreePages - 128;
|
---|
133 | cTop = 32;
|
---|
134 | }
|
---|
135 | else if (cMax >= 128)
|
---|
136 | {
|
---|
137 | cMax = cFreePages - 48;
|
---|
138 | cTop = 16;
|
---|
139 | }
|
---|
140 | else
|
---|
141 | {
|
---|
142 | cMax = cFreePages - 16;
|
---|
143 | cTop = RT_MIN(16, cMax / 4);
|
---|
144 | }
|
---|
145 | Bs3TestPrintf("Bs3PagingInitRootForPP: Warning! insufficient memory for mapping all 4GB!\n"
|
---|
146 | " Will only map 0x00000000-%#010RX32 and %#010RX32-0xffffffff.\n",
|
---|
147 | (uint32_t)(cMax - cTop) << PAGE_SHIFT, UINT32_MAX - ((uint32_t)cTop << PAGE_SHIFT) + 1);
|
---|
148 | rc = Bs3PagingInitPageTablesForPgDir(pPgDir, 0, cMax - cTop);
|
---|
149 | if (RT_SUCCESS(rc))
|
---|
150 | rc = Bs3PagingInitPageTablesForPgDir(pPgDir, RT_ELEMENTS(pPgDir->a) - cTop, cTop);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | BS3_XPTR_SET(X86PD, XptrPgDir, pPgDir);
|
---|
155 | g_PhysPagingRootPP = BS3_XPTR_GET_FLAT(X86PD, XptrPgDir);
|
---|
156 | return rc;
|
---|
157 | }
|
---|
158 |
|
---|
159 | Bs3Printf("Bs3PagingInitRootForPP: No memory!\n");
|
---|
160 | BS3_ASSERT(false);
|
---|
161 | return VERR_NO_MEMORY;
|
---|
162 | }
|
---|
163 |
|
---|