1 | /* $Id: bs3-cmn-PagingQueryAddressInfo.c 102096 2023-11-15 11:11:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - Bs3PagingQueryAddressInfo
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2023 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.h>
|
---|
42 | #include <iprt/asm-amd64-x86.h>
|
---|
43 | #include <VBox/err.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | #undef Bs3PagingQueryAddressInfo
|
---|
47 | BS3_CMN_DEF(int, Bs3PagingQueryAddressInfo,(uint64_t uFlat, PBS3PAGINGINFO4ADDR pPgInfo))
|
---|
48 | {
|
---|
49 | #ifdef _MSC_VER
|
---|
50 | BS3PAGINGINFO4ADDR volatile BS3_FAR *pPgInfoNoSseHack = (BS3PAGINGINFO4ADDR volatile BS3_FAR *)pPgInfo;
|
---|
51 | # define pPgInfo pPgInfoNoSseHack
|
---|
52 | #endif
|
---|
53 | RTCCUINTXREG const cr3 = ASMGetCR3();
|
---|
54 | RTCCUINTXREG const cr4 = g_uBs3CpuDetected & BS3CPU_F_CPUID ? ASMGetCR4() : 0;
|
---|
55 | bool const fLegacyPTs = !(cr4 & X86_CR4_PAE);
|
---|
56 | int rc = VERR_OUT_OF_RANGE;
|
---|
57 |
|
---|
58 |
|
---|
59 | pPgInfo->fFlags = 0;
|
---|
60 | pPgInfo->u.apbEntries[0] = NULL;
|
---|
61 | pPgInfo->u.apbEntries[1] = NULL;
|
---|
62 | pPgInfo->u.apbEntries[2] = NULL;
|
---|
63 | pPgInfo->u.apbEntries[3] = NULL;
|
---|
64 |
|
---|
65 | if (!fLegacyPTs)
|
---|
66 | {
|
---|
67 | #if TMPL_BITS == 16
|
---|
68 | uint32_t const uMaxAddr = BS3_MODE_IS_RM_OR_V86(g_bBs3CurrentMode) ? _1M - 1 : BS3_SEL_TILED_AREA_SIZE - 1;
|
---|
69 | #else
|
---|
70 | uintptr_t const uMaxAddr = ~(uintptr_t)0;
|
---|
71 | #endif
|
---|
72 | uint64_t const fEfer = g_uBs3CpuDetected & BS3CPU_F_LONG_MODE ? ASMRdMsr(MSR_K6_EFER) : 0;
|
---|
73 |
|
---|
74 | pPgInfo->cEntries = fEfer & MSR_K6_EFER_LMA ? 4 : 3;
|
---|
75 | pPgInfo->cbEntry = sizeof(X86PTEPAE);
|
---|
76 | if ((cr3 & X86_CR3_AMD64_PAGE_MASK) <= uMaxAddr)
|
---|
77 | {
|
---|
78 | if ( (fEfer & MSR_K6_EFER_LMA)
|
---|
79 | && X86_IS_CANONICAL(uFlat))
|
---|
80 | {
|
---|
81 | /* 48-bit long mode paging. */
|
---|
82 | pPgInfo->u.Pae.pPml4e = (X86PML4E BS3_FAR *)Bs3XptrFlatToCurrent(cr3 & X86_CR3_AMD64_PAGE_MASK);
|
---|
83 | pPgInfo->u.Pae.pPml4e += (uFlat >> X86_PML4_SHIFT) & X86_PML4_MASK;
|
---|
84 | if (!pPgInfo->u.Pae.pPml4e->n.u1Present)
|
---|
85 | rc = VERR_PAGE_NOT_PRESENT;
|
---|
86 | else if ((pPgInfo->u.Pae.pPml4e->u & X86_PML4E_PG_MASK) <= uMaxAddr)
|
---|
87 | {
|
---|
88 | pPgInfo->u.Pae.pPdpe = (X86PDPE BS3_FAR *)Bs3XptrFlatToCurrent(pPgInfo->u.Pae.pPml4e->u & X86_PML4E_PG_MASK);
|
---|
89 | pPgInfo->u.Pae.pPdpe += (uFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
|
---|
90 | if (!pPgInfo->u.Pae.pPdpe->n.u1Present)
|
---|
91 | rc = VERR_PAGE_NOT_PRESENT;
|
---|
92 | else if (pPgInfo->u.Pae.pPdpe->b.u1Size)
|
---|
93 | rc = VINF_SUCCESS;
|
---|
94 | else
|
---|
95 | rc = VINF_TRY_AGAIN;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | else if ( !(fEfer & MSR_K6_EFER_LMA)
|
---|
99 | && uFlat <= _4G)
|
---|
100 | {
|
---|
101 | /* 32-bit PAE paging. */
|
---|
102 | pPgInfo->u.Pae.pPdpe = (X86PDPE BS3_FAR *)Bs3XptrFlatToCurrent(cr3 & X86_CR3_PAE_PAGE_MASK);
|
---|
103 | pPgInfo->u.Pae.pPdpe += ((uint32_t)uFlat >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
|
---|
104 | if (!pPgInfo->u.Pae.pPdpe->n.u1Present)
|
---|
105 | rc = VERR_PAGE_NOT_PRESENT;
|
---|
106 | else
|
---|
107 | rc = VINF_TRY_AGAIN;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /* Common code for the PD and PT levels. */
|
---|
111 | if ( rc == VINF_TRY_AGAIN
|
---|
112 | && (pPgInfo->u.Pae.pPdpe->u & X86_PDPE_PG_MASK) <= uMaxAddr)
|
---|
113 | {
|
---|
114 | rc = VERR_OUT_OF_RANGE;
|
---|
115 | pPgInfo->u.Pae.pPde = (X86PDEPAE BS3_FAR *)Bs3XptrFlatToCurrent(pPgInfo->u.Pae.pPdpe->u & X86_PDPE_PG_MASK);
|
---|
116 | pPgInfo->u.Pae.pPde += (uFlat >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
|
---|
117 | if (!pPgInfo->u.Pae.pPde->n.u1Present)
|
---|
118 | rc = VERR_PAGE_NOT_PRESENT;
|
---|
119 | else if (pPgInfo->u.Pae.pPde->b.u1Size)
|
---|
120 | rc = VINF_SUCCESS;
|
---|
121 | else if ((pPgInfo->u.Pae.pPde->u & X86_PDE_PAE_PG_MASK) <= uMaxAddr)
|
---|
122 | {
|
---|
123 | pPgInfo->u.Pae.pPte = (X86PTEPAE BS3_FAR *)Bs3XptrFlatToCurrent(pPgInfo->u.Pae.pPde->u & X86_PDE_PAE_PG_MASK);
|
---|
124 | rc = VINF_SUCCESS;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | else if (rc == VINF_TRY_AGAIN)
|
---|
128 | rc = VERR_OUT_OF_RANGE;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | else
|
---|
132 | {
|
---|
133 | #if TMPL_BITS == 16
|
---|
134 | uint32_t const uMaxAddr = BS3_MODE_IS_RM_OR_V86(g_bBs3CurrentMode) ? _1M - 1 : BS3_SEL_TILED_AREA_SIZE - 1;
|
---|
135 | #else
|
---|
136 | uint32_t const uMaxAddr = UINT32_MAX;
|
---|
137 | #endif
|
---|
138 |
|
---|
139 | pPgInfo->cEntries = 2;
|
---|
140 | pPgInfo->cbEntry = sizeof(X86PTE);
|
---|
141 | if ( uFlat < _4G
|
---|
142 | && cr3 <= uMaxAddr)
|
---|
143 | {
|
---|
144 | pPgInfo->u.Legacy.pPde = (X86PDE BS3_FAR *)Bs3XptrFlatToCurrent(cr3 & X86_CR3_PAGE_MASK);
|
---|
145 | pPgInfo->u.Legacy.pPde += ((uint32_t)uFlat >> X86_PD_SHIFT) & X86_PD_MASK;
|
---|
146 | if (!pPgInfo->u.Legacy.pPde->b.u1Present)
|
---|
147 | rc = VERR_PAGE_NOT_PRESENT;
|
---|
148 | else if (pPgInfo->u.Legacy.pPde->b.u1Size)
|
---|
149 | rc = VINF_SUCCESS;
|
---|
150 | else if (pPgInfo->u.Legacy.pPde->u <= uMaxAddr)
|
---|
151 | {
|
---|
152 | pPgInfo->u.Legacy.pPte = (X86PTE BS3_FAR *)Bs3XptrFlatToCurrent(pPgInfo->u.Legacy.pPde->u & X86_PDE_PG_MASK);
|
---|
153 | pPgInfo->u.Legacy.pPte += ((uint32_t)uFlat >> X86_PT_SHIFT) & X86_PT_MASK;
|
---|
154 | if (pPgInfo->u.Legacy.pPte->n.u1Present)
|
---|
155 | rc = VINF_SUCCESS;
|
---|
156 | else
|
---|
157 | rc = VERR_PAGE_NOT_PRESENT;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 | return rc;
|
---|
162 | }
|
---|
163 |
|
---|