1 | /* $Id: bs3-cmn-PagingAlias.c 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - Bs3PagingAlias, Bs3PagingUnalias
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2022 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 "iprt/asm-amd64-x86.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | #undef Bs3PagingAlias
|
---|
47 | BS3_CMN_DEF(int, Bs3PagingAlias,(uint64_t uDst, uint64_t uPhysToAlias, uint32_t cbHowMuch, uint64_t fPte))
|
---|
48 | {
|
---|
49 | #if ARCH_BITS == 16
|
---|
50 | if (!BS3_MODE_IS_V86(g_bBs3CurrentMode))
|
---|
51 | #endif
|
---|
52 | {
|
---|
53 | RTCCUINTXREG cr3 = ASMGetCR3();
|
---|
54 | uint32_t cPages;
|
---|
55 | int rc;
|
---|
56 |
|
---|
57 | /*
|
---|
58 | * Validate and adjust the input a little.
|
---|
59 | */
|
---|
60 | if (uDst & X86_PAGE_OFFSET_MASK)
|
---|
61 | {
|
---|
62 | cbHowMuch += X86_PAGE_SIZE - (uDst & X86_PAGE_OFFSET_MASK);
|
---|
63 | uDst &= ~(uint64_t)X86_PAGE_OFFSET_MASK;
|
---|
64 | }
|
---|
65 | uPhysToAlias &= X86_PTE_PAE_PG_MASK;
|
---|
66 | fPte &= ~(X86_PTE_PAE_MBZ_MASK_NX | X86_PTE_PAE_PG_MASK);
|
---|
67 | cbHowMuch = RT_ALIGN_32(cbHowMuch, X86_PAGE_SIZE);
|
---|
68 | cPages = cbHowMuch >> X86_PAGE_SHIFT;
|
---|
69 | //Bs3TestPrintf("Bs3PagingAlias: adjusted: uDst=%RX64 uPhysToAlias=%RX64 cbHowMuch=%RX32 fPte=%Rx64 cPages=%RX32\n", uDst, uPhysToAlias, cbHowMuch, fPte, cPages);
|
---|
70 | if (BS3_MODE_IS_LEGACY_PAGING(g_bBs3CurrentMode))
|
---|
71 | {
|
---|
72 | X86PTE BS3_FAR *pPteLegacy;
|
---|
73 | uint32_t uDst32 = (uint32_t)uDst;
|
---|
74 | uint32_t uPhysToAlias32 = (uint32_t)uPhysToAlias;
|
---|
75 | if (uDst32 != uDst)
|
---|
76 | {
|
---|
77 | Bs3TestPrintf("warning: Bs3PagingAlias - uDst=%RX64 is out of range for legacy paging!\n", uDst);
|
---|
78 | return VERR_INVALID_PARAMETER;
|
---|
79 | }
|
---|
80 | if (uPhysToAlias32 != uPhysToAlias)
|
---|
81 | {
|
---|
82 | Bs3TestPrintf("warning: Bs3PagingAlias - uPhysToAlias=%RX64 is out of range for legacy paging!\n", uPhysToAlias);
|
---|
83 | return VERR_INVALID_PARAMETER;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Trigger page table splitting first.
|
---|
88 | */
|
---|
89 | while (cPages > 0)
|
---|
90 | {
|
---|
91 | pPteLegacy = bs3PagingGetLegacyPte(cr3, uDst32, false, &rc);
|
---|
92 | if (pPteLegacy)
|
---|
93 | {
|
---|
94 | uint32_t cLeftInPt = X86_PG_ENTRIES - ((uDst32 >> X86_PT_SHIFT) & X86_PT_MASK);
|
---|
95 | if (cPages <= cLeftInPt)
|
---|
96 | break;
|
---|
97 | uDst32 += cLeftInPt << X86_PAGE_SHIFT;
|
---|
98 | cPages -= cLeftInPt;
|
---|
99 | }
|
---|
100 | else
|
---|
101 | {
|
---|
102 | Bs3TestPrintf("warning: Bs3PagingAlias - bs3PagingGetLegacyPte failed: rc=%d\n", rc);
|
---|
103 | return rc;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * Make the changes.
|
---|
109 | */
|
---|
110 | cPages = cbHowMuch >> X86_PAGE_SHIFT;
|
---|
111 | uDst32 = (uint32_t)uDst;
|
---|
112 | while (cPages > 0)
|
---|
113 | {
|
---|
114 | uint32_t cLeftInPt = X86_PG_ENTRIES - ((uDst32 >> X86_PT_SHIFT) & X86_PT_MASK);
|
---|
115 | pPteLegacy = bs3PagingGetLegacyPte(cr3, uDst32, false, &rc);
|
---|
116 | while (cLeftInPt > 0 && cPages > 0)
|
---|
117 | {
|
---|
118 | pPteLegacy->u = uPhysToAlias32 | (uint32_t)fPte;
|
---|
119 | pPteLegacy++;
|
---|
120 | uDst32 += X86_PAGE_SIZE;
|
---|
121 | uPhysToAlias32 += X86_PAGE_SIZE;
|
---|
122 | cPages--;
|
---|
123 | cLeftInPt--;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | else
|
---|
128 | {
|
---|
129 | X86PTEPAE BS3_FAR *pPtePae;
|
---|
130 | uint64_t const uDstSaved = uDst;
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * Trigger page table splitting first.
|
---|
134 | */
|
---|
135 | while (cPages > 0)
|
---|
136 | {
|
---|
137 | pPtePae = bs3PagingGetPaePte(cr3, g_bBs3CurrentMode, uDst, false, &rc);
|
---|
138 | if (pPtePae)
|
---|
139 | {
|
---|
140 | uint32_t cLeftInPt = X86_PG_PAE_ENTRIES - ((uDst >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK);
|
---|
141 | if (cPages <= cLeftInPt)
|
---|
142 | break;
|
---|
143 | cPages -= cLeftInPt;
|
---|
144 | uDst += cLeftInPt << X86_PAGE_SHIFT;
|
---|
145 | }
|
---|
146 | else
|
---|
147 | {
|
---|
148 | Bs3TestPrintf("warning: Bs3PagingAlias - bs3PagingGetLegacyPte failed: rc=%d\n", rc);
|
---|
149 | return rc;
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | /*
|
---|
154 | * Make the changes.
|
---|
155 | */
|
---|
156 | cPages = cbHowMuch >> X86_PAGE_SHIFT;
|
---|
157 | uDst = uDstSaved;
|
---|
158 | while (cPages > 0)
|
---|
159 | {
|
---|
160 | uint32_t cLeftInPt = X86_PG_PAE_ENTRIES - ((uDst >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK);
|
---|
161 | pPtePae = bs3PagingGetPaePte(cr3, g_bBs3CurrentMode, uDst, false, &rc);
|
---|
162 | while (cLeftInPt > 0 && cPages > 0)
|
---|
163 | {
|
---|
164 | pPtePae->u = uPhysToAlias | fPte;
|
---|
165 | pPtePae++;
|
---|
166 | uDst += X86_PAGE_SIZE;
|
---|
167 | uPhysToAlias += X86_PAGE_SIZE;
|
---|
168 | cPages--;
|
---|
169 | cLeftInPt--;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | ASMReloadCR3();
|
---|
175 | }
|
---|
176 | #if ARCH_BITS == 16
|
---|
177 | /*
|
---|
178 | * We can't do this stuff in v8086 mode, so switch to 16-bit prot mode and do it there.
|
---|
179 | */
|
---|
180 | else
|
---|
181 | return Bs3SwitchFromV86To16BitAndCallC((FPFNBS3FAR)Bs3PagingAlias_f16, sizeof(uint64_t)*3 + sizeof(uint32_t),
|
---|
182 | uDst, uPhysToAlias, cbHowMuch, fPte);
|
---|
183 | #endif
|
---|
184 | return VINF_SUCCESS;
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | #undef Bs3PagingUnalias
|
---|
189 | BS3_CMN_DEF(int, Bs3PagingUnalias,(uint64_t uDst, uint32_t cbHowMuch))
|
---|
190 | {
|
---|
191 | return BS3_CMN_NM(Bs3PagingAlias)(uDst, uDst, cbHowMuch, X86_PTE_P | X86_PTE_RW | X86_PTE_US | X86_PTE_A | X86_PTE_D);
|
---|
192 | }
|
---|
193 |
|
---|