VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-PagingInitRootForPP.c@ 60462

Last change on this file since 60462 was 60321, checked in by vboxsync, 9 years ago

bs3kit: 386 fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1/* $Id: bs3-cmn-PagingInitRootForPP.c 60321 2016-04-05 00:33:14Z vboxsync $ */
2/** @file
3 * BS3Kit - Bs3PagingInitRootForPP
4 */
5
6/*
7 * Copyright (C) 2007-2015 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* Header Files *
29*********************************************************************************************************************************/
30#include "bs3kit-template-header.h"
31#include "bs3-cmn-paging.h"
32#include "bs3-cmn-memory.h" /* bad bird */
33#include <iprt/param.h>
34
35
36/**
37 * Creates page tables for a section of the page directory.
38 *
39 * @returns VINF_SUCCESS or VERR_NO_MEMORY.
40 * @param pPgDir The page directory.
41 * @param iFirst The first PD entry.
42 * @param cEntries How many PD entries to create pages tables for.
43 */
44static int Bs3PagingInitPageTablesForPgDir(X86PD BS3_FAR *pPgDir, unsigned iFirst, unsigned cEntries)
45{
46 uint32_t uCurPhys = (uint32_t)iFirst << X86_PD_SHIFT;
47
48 while (cEntries--)
49 {
50 X86PT BS3_FAR *pPt = (X86PT BS3_FAR *)Bs3MemAlloc(BS3MEMKIND_TILED, _4K);
51 if (pPt)
52 {
53 unsigned j = 0;
54 for (j = 0; j < RT_ELEMENTS(pPt->a); j++, uCurPhys += PAGE_SIZE)
55 {
56 pPt->a[j].u = uCurPhys;
57 pPt->a[j].u |= X86_PTE_P | X86_PTE_RW | X86_PTE_US | X86_PTE_A | X86_PTE_D;
58 }
59 pPgDir->a[iFirst].u = Bs3SelPtrToFlat(pPt);
60 pPgDir->a[iFirst].u |= X86_PDE_P | X86_PDE_RW | X86_PDE_US | X86_PDE_A;
61 iFirst++;
62 }
63 else
64 return VERR_NO_MEMORY;
65 }
66 return VINF_SUCCESS;
67}
68
69
70BS3_DECL(int) Bs3PagingInitRootForPP(void)
71{
72 X86PD BS3_FAR *pPgDir;
73
74 BS3_ASSERT(g_PhysPagingRootPP == UINT32_MAX);
75
76
77 /*
78 * By default we do a identity mapping of the entire address space
79 * using 4 GB pages. So, we only really need one page directory,
80 * that's all.
81 *
82 * ASSUMES page size extension available, i.e. pentium+.
83 */
84 pPgDir = (X86PD BS3_FAR *)Bs3MemAllocZ(BS3MEMKIND_TILED, _4K);
85 if (pPgDir)
86 {
87 BS3_XPTR_AUTO(X86PD, XptrPgDir);
88 unsigned i;
89 int rc = VINF_SUCCESS;
90
91 if (g_uBs3CpuDetected & BS3CPU_F_PSE)
92 {
93 for (i = 0; i < RT_ELEMENTS(pPgDir->a); i++)
94 {
95 pPgDir->a[i].u = (uint32_t)i << X86_PD_SHIFT;
96 pPgDir->a[i].u |= X86_PDE4M_P | X86_PDE4M_RW | X86_PDE4M_US | X86_PDE4M_PS | X86_PDE4M_A | X86_PDE4M_D;
97 }
98 }
99 else
100 {
101 /*
102 * This requires 4MB of page tables if we map everything.
103 * So, we check how much memory we have available and make sure we
104 * don't use all of it for page tables.
105 */
106 unsigned cMax = RT_ELEMENTS(pPgDir->a);
107 uint32_t cFreePages = g_Bs3Mem4KUpperTiled.Core.cFreeChunks + g_Bs3Mem4KLow.Core.cFreeChunks;
108 if (cFreePages >= cMax + 128)
109 Bs3PagingInitPageTablesForPgDir(pPgDir, 0, cMax);
110 else
111 {
112 unsigned cTop;
113 if (cMax >= 256 /*1MB*/)
114 {
115 cMax = cFreePages - 128;
116 cTop = 32;
117 }
118 else if (cMax >= 128)
119 {
120 cMax = cFreePages - 48;
121 cTop = 16;
122 }
123 else
124 {
125 cMax = cFreePages - 16;
126 cTop = RT_MIN(16, cMax / 4);
127 }
128 Bs3TestPrintf("Bs3PagingInitRootForPP: Warning! insufficient memory for mapping all 4GB!\n"
129 " Will only map 0x00000000-%#010RX32 and %#010RX32-0xffffffff.\n",
130 (uint32_t)(cMax - cTop) << PAGE_SHIFT, UINT32_MAX - ((uint32_t)cTop << PAGE_SHIFT) + 1);
131 rc = Bs3PagingInitPageTablesForPgDir(pPgDir, 0, cMax - cTop);
132 if (RT_SUCCESS(rc))
133 rc = Bs3PagingInitPageTablesForPgDir(pPgDir, RT_ELEMENTS(pPgDir->a) - cTop, cTop);
134 }
135 }
136
137 BS3_XPTR_SET(X86PD, XptrPgDir, pPgDir);
138 g_PhysPagingRootPP = BS3_XPTR_GET_FLAT(X86PD, XptrPgDir);
139 return rc;
140 }
141
142 Bs3Printf("Bs3PagingInitRootForPP: No memory!\n");
143 BS3_ASSERT(false);
144 return VERR_NO_MEMORY;
145}
146
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette