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