1 | /* $Id: bs3-cmn-PagingInitRootForLM.c 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - Bs3PagingInitRootForLM
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-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 | * 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 |
|
---|
34 |
|
---|
35 | #undef Bs3PagingInitRootForLM
|
---|
36 | BS3_CMN_DEF(int, Bs3PagingInitRootForLM,(void))
|
---|
37 | {
|
---|
38 | X86PML4 BS3_FAR *pPml4;
|
---|
39 |
|
---|
40 | BS3_ASSERT(g_PhysPagingRootLM == UINT32_MAX);
|
---|
41 |
|
---|
42 | /*
|
---|
43 | * The default is an identity mapping of the first 4GB repeated for the
|
---|
44 | * whole 48-bit virtual address space. So, we need one level more than PAE.
|
---|
45 | */
|
---|
46 | pPml4 = (X86PML4 BS3_FAR *)Bs3MemAlloc(BS3MEMKIND_TILED, _4K);
|
---|
47 | if (pPml4)
|
---|
48 | {
|
---|
49 | X86PDPT BS3_FAR *pPdPtr = (X86PDPT BS3_FAR *)Bs3MemAlloc(BS3MEMKIND_TILED, _4K);
|
---|
50 | BS3_ASSERT((uintptr_t)pPdPtr != (uintptr_t)pPml4);
|
---|
51 | if (pPdPtr)
|
---|
52 | {
|
---|
53 | X86PDPAE BS3_FAR *paPgDirs = (X86PDPAE BS3_FAR *)Bs3MemAlloc(BS3MEMKIND_TILED, _4K * 4U);
|
---|
54 | BS3_ASSERT((uintptr_t)paPgDirs != (uintptr_t)pPml4);
|
---|
55 | if (paPgDirs)
|
---|
56 | {
|
---|
57 | unsigned i;
|
---|
58 | BS3_XPTR_AUTO(X86PML4, XPtrPml4);
|
---|
59 | BS3_XPTR_AUTO(X86PDPT, XPtrPdPtr);
|
---|
60 | BS3_XPTR_AUTO(X86PDPAE, XPtrPgDirs);
|
---|
61 |
|
---|
62 | /* Set up the 2048 2MB pages first. */
|
---|
63 | for (i = 0; i < RT_ELEMENTS(paPgDirs->a) * 4U; i++)
|
---|
64 | paPgDirs->a[i].u = ((uint32_t)i << X86_PD_PAE_SHIFT)
|
---|
65 | | X86_PDE4M_P | X86_PDE4M_RW | X86_PDE4M_US | X86_PDE4M_PS | X86_PDE4M_A | X86_PDE4M_D;
|
---|
66 |
|
---|
67 | /* Set up the page directory pointer table next (4GB replicated, remember). */
|
---|
68 | BS3_XPTR_SET(X86PDPAE, XPtrPgDirs, paPgDirs);
|
---|
69 | pPdPtr->a[0].u = BS3_XPTR_GET_FLAT(X86PDPAE, XPtrPgDirs)
|
---|
70 | | X86_PDPE_P | X86_PDPE_RW | X86_PDPE_US | X86_PDPE_A;
|
---|
71 | pPdPtr->a[1].u = pPdPtr->a[0].u + _4K;
|
---|
72 | pPdPtr->a[2].u = pPdPtr->a[1].u + _4K;
|
---|
73 | pPdPtr->a[3].u = pPdPtr->a[2].u + _4K;
|
---|
74 |
|
---|
75 | for (i = 4; i < RT_ELEMENTS(pPdPtr->a); i += 4)
|
---|
76 | {
|
---|
77 | pPdPtr->a[i + 0].u = pPdPtr->a[0].u;
|
---|
78 | pPdPtr->a[i + 1].u = pPdPtr->a[1].u;
|
---|
79 | pPdPtr->a[i + 2].u = pPdPtr->a[2].u;
|
---|
80 | pPdPtr->a[i + 3].u = pPdPtr->a[3].u;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /* Set up the page map level 4 (all entries are the same). */
|
---|
84 | BS3_XPTR_SET(X86PDPT, XPtrPdPtr, pPdPtr);
|
---|
85 | pPml4->a[0].u = BS3_XPTR_GET_FLAT(X86PDPT, XPtrPdPtr)
|
---|
86 | | X86_PML4E_P | X86_PML4E_RW | X86_PML4E_US | X86_PML4E_A;
|
---|
87 | for (i = 1; i < RT_ELEMENTS(pPml4->a); i++)
|
---|
88 | pPml4->a[i].u = pPml4->a[0].u;
|
---|
89 |
|
---|
90 | /* Set the global root pointer and we're done. */
|
---|
91 | BS3_XPTR_SET(X86PML4, XPtrPml4, pPml4);
|
---|
92 | g_PhysPagingRootLM = BS3_XPTR_GET_FLAT(X86PML4, XPtrPml4);
|
---|
93 | return VINF_SUCCESS;
|
---|
94 | }
|
---|
95 |
|
---|
96 | BS3_ASSERT(false);
|
---|
97 | Bs3MemFree(pPdPtr, _4K);
|
---|
98 | }
|
---|
99 | BS3_ASSERT(false);
|
---|
100 | Bs3MemFree(pPml4, _4K);
|
---|
101 | }
|
---|
102 | BS3_ASSERT(false);
|
---|
103 | return VERR_NO_MEMORY;
|
---|
104 | }
|
---|
105 |
|
---|