1 | /* $Id: bs3-cmn-PagingMapRamAbove4GForLM.c 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - Bs3PagingInitMapAbove4GForLM
|
---|
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 |
|
---|
44 | #if ARCH_BITS == 16
|
---|
45 | # error "This does not work in 16-bit mode, only 32-bit and 64-bit"
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | #undef Bs3PagingMapRamAbove4GForLM
|
---|
49 | BS3_CMN_DEF(int, Bs3PagingMapRamAbove4GForLM,(uint64_t *puFailurePoint))
|
---|
50 | {
|
---|
51 | X86PML4 * const pPml4 = (X86PML4 *)((uintptr_t)g_PhysPagingRootLM);
|
---|
52 | unsigned iPml4 = 0;
|
---|
53 | unsigned iPdpt = 4;
|
---|
54 | uint64_t uAddr = _4G;
|
---|
55 | X86PDPT * pPdpt;
|
---|
56 |
|
---|
57 | if (puFailurePoint)
|
---|
58 | *puFailurePoint = 0;
|
---|
59 |
|
---|
60 | /* Must call Bs3PagingInitRootForLM first! */
|
---|
61 | if (g_PhysPagingRootLM == UINT32_MAX)
|
---|
62 | return VERR_WRONG_ORDER;
|
---|
63 |
|
---|
64 | /* Done already? */
|
---|
65 | if (pPml4->a[0].u != pPml4->a[4].u)
|
---|
66 | return VINF_ALREADY_INITIALIZED;
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * Map RAM pages up to g_uBs3EndOfRamAbove4G.
|
---|
70 | */
|
---|
71 | pPdpt = (X86PDPT *)(pPml4->a[0].u & X86_PML4E_PG_MASK);
|
---|
72 | while (uAddr < g_uBs3EndOfRamAbove4G)
|
---|
73 | {
|
---|
74 | X86PDPAE *pPd;
|
---|
75 | unsigned i;
|
---|
76 |
|
---|
77 | /* Do we need a new PDPT? */
|
---|
78 | if (iPdpt >= RT_ELEMENTS(pPdpt->a))
|
---|
79 | {
|
---|
80 | if (iPml4 >= RT_ELEMENTS(pPml4->a) / 2)
|
---|
81 | {
|
---|
82 | if (puFailurePoint)
|
---|
83 | *puFailurePoint = uAddr;
|
---|
84 | return VERR_OUT_OF_RANGE;
|
---|
85 | }
|
---|
86 | pPdpt = (X86PDPT *)Bs3MemAllocZ(BS3MEMKIND_FLAT32, X86_PAGE_SIZE);
|
---|
87 | if (!pPdpt || ((uintptr_t)pPdpt & X86_PAGE_OFFSET_MASK))
|
---|
88 | {
|
---|
89 | if (puFailurePoint)
|
---|
90 | *puFailurePoint = uAddr;
|
---|
91 | return !pPdpt ? VERR_NO_MEMORY : VERR_UNSUPPORTED_ALIGNMENT;
|
---|
92 | }
|
---|
93 | pPml4->a[++iPml4].u = X86_PML4E_P | X86_PML4E_RW | X86_PML4E_US | X86_PML4E_A
|
---|
94 | | (uintptr_t)pPdpt;
|
---|
95 | iPdpt = 0;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /* Allocate a new page directory. */
|
---|
99 | pPd = (X86PDPAE *)Bs3MemAlloc(BS3MEMKIND_FLAT32, X86_PAGE_SIZE);
|
---|
100 | if (!pPd || ((uintptr_t)pPd & X86_PAGE_OFFSET_MASK))
|
---|
101 | {
|
---|
102 | if (puFailurePoint)
|
---|
103 | *puFailurePoint = uAddr;
|
---|
104 | return !pPd ? VERR_NO_MEMORY : VERR_UNSUPPORTED_ALIGNMENT;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /* Initialize it. */
|
---|
108 | for (i = 0; i < RT_ELEMENTS(pPd->a); i++)
|
---|
109 | {
|
---|
110 | pPd->a[i].u = uAddr | X86_PDE4M_P | X86_PDE4M_RW | X86_PDE4M_US | X86_PDE4M_PS | X86_PDE4M_A | X86_PDE4M_D;
|
---|
111 | uAddr += _2M;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* Insert it into the page directory pointer table. */
|
---|
115 | pPdpt->a[iPdpt++].u = (uintptr_t)pPd | X86_PDPE_P | X86_PDPE_RW | X86_PDPE_US | X86_PDPE_A;
|
---|
116 | }
|
---|
117 |
|
---|
118 | return VINF_SUCCESS;
|
---|
119 | }
|
---|
120 |
|
---|