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