Changeset 60676 in vbox for trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-PagingProtect.c
- Timestamp:
- Apr 24, 2016 11:04:57 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-cmn-PagingProtect.c
r60527 r60676 37 37 * Defined Constants And Macros * 38 38 *********************************************************************************************************************************/ 39 #if 1 40 # define BS3PAGING_DPRINTF1(a) Bs3TestPrintf a 41 #else 42 # define BS3PAGING_DPRINTF1(a) do { } while (0) 43 #endif 39 44 #if 0 40 # define BS3PAGING_DPRINTF (a) Bs3TestPrintf a41 #else 42 # define BS3PAGING_DPRINTF (a) do { } while (0)45 # define BS3PAGING_DPRINTF2(a) Bs3TestPrintf a 46 #else 47 # define BS3PAGING_DPRINTF2(a) do { } while (0) 43 48 #endif 44 49 … … 68 73 uint32_t const uMaxAddr = UINT32_MAX; 69 74 #endif 70 BS3PAGING_DPRINTF (("bs3PagingGetLegacyPte: cr3=%RX32 uFlat=%RX32 uMaxAddr=%RX32\n", (uint32_t)cr3, uFlat, uMaxAddr));75 BS3PAGING_DPRINTF2(("bs3PagingGetLegacyPte: cr3=%RX32 uFlat=%RX32 uMaxAddr=%RX32\n", (uint32_t)cr3, uFlat, uMaxAddr)); 71 76 72 77 *prc = VERR_OUT_OF_RANGE; … … 82 87 83 88 BS3_ASSERT(pPD->a[iPde].b.u1Present); 84 BS3PAGING_DPRINTF (("bs3PagingGetLegacyPte: pPD=%p iPde=%#x: %#RX32\n", pPD, iPde, pPD->a[iPde]));89 BS3PAGING_DPRINTF2(("bs3PagingGetLegacyPte: pPD=%p iPde=%#x: %#RX32\n", pPD, iPde, pPD->a[iPde])); 85 90 if (pPD->a[iPde].b.u1Present) 86 91 { … … 88 93 { 89 94 if (pPD->a[iPde].u <= uMaxAddr) 95 { 90 96 pPTE = &((X86PT BS3_FAR *)Bs3XptrFlatToCurrent(pPD->a[iPde].u & ~(uint32_t)PAGE_OFFSET_MASK))->a[iPte]; 97 *prc = VINF_SUCCESS; 98 } 99 else 100 BS3PAGING_DPRINTF1(("bs3PagingGetLegacyPte: out of range! iPde=%#x: %#x\n", iPde, pPD->a[iPde].u)); 91 101 } 92 102 else … … 99 109 uPte |= X86_PTE_PAT; 100 110 101 pPT = (X86PT BS3_FAR *)bs3PagingBuildPaeTable(RT_MAKE_U64(uPte, uPte +PAGE_SIZE),111 pPT = (X86PT BS3_FAR *)bs3PagingBuildPaeTable(RT_MAKE_U64(uPte, uPte | PAGE_SIZE), 102 112 RT_MAKE_U64(PAGE_SIZE*2, PAGE_SIZE*2), 103 113 uMaxAddr > _1M ? BS3MEMKIND_TILED : BS3MEMKIND_REAL, prc); 104 114 105 BS3PAGING_DPRINTF (("bs3PagingGetLegacyPte: Built pPT=%p uPte=%RX32\n", pPT, uPte));115 BS3PAGING_DPRINTF2(("bs3PagingGetLegacyPte: Built pPT=%p uPte=%RX32\n", pPT, uPte)); 106 116 if (pPT) 107 117 { 108 118 pPD->a[iPde].u = Bs3SelPtrToFlat(pPT) 109 119 | (pPD->a[iPde].u & ~(uint32_t)(X86_PTE_PG_MASK | X86_PDE4M_PS | X86_PDE4M_G | X86_PDE4M_D)); 110 BS3PAGING_DPRINTF (("bs3PagingGetLegacyPte: iPde=%#x: %#RX32\n", iPde, pPD->a[iPde].u));120 BS3PAGING_DPRINTF2(("bs3PagingGetLegacyPte: iPde=%#x: %#RX32\n", iPde, pPD->a[iPde].u)); 111 121 if (fUseInvlPg) 112 122 ASMInvalidatePage(uFlat); 113 123 pPTE = &pPT->a[iPte]; 124 *prc = VINF_SUCCESS; 114 125 } 115 126 } … … 117 128 } 118 129 } 130 else 131 BS3PAGING_DPRINTF1(("bs3PagingGetLegacyPte: out of range! cr3=%#x\n", cr3)); 119 132 return pPTE; 120 133 } … … 149 162 if ((pPdpt->a[iPdpte].u & X86_PDPE_PG_MASK) <= uMaxAddr) 150 163 pPD = (X86PDPAE BS3_FAR *)Bs3XptrFlatToCurrent(pPdpt->a[iPdpte].u & ~(uint64_t)PAGE_OFFSET_MASK); 164 else 165 BS3PAGING_DPRINTF1(("bs3PagingGetPte: out of range! iPdpte=%#x: %RX64 max=%RX32\n", 166 iPdpte, pPdpt->a[iPdpte].u, (uint32_t)uMaxAddr)); 151 167 } 152 168 else … … 174 190 if ((pPdpt->a[iPdpte].u & X86_PDPE_PG_MASK) <= uMaxAddr) 175 191 pPD = (X86PDPAE BS3_FAR *)Bs3XptrFlatToCurrent(pPdpt->a[iPdpte].u & X86_PDPE_PG_MASK); 192 else 193 BS3PAGING_DPRINTF1(("bs3PagingGetPte: out of range! iPdpte=%#x: %RX64 max=%RX32\n", 194 iPdpte, pPdpt->a[iPdpte].u, (uint32_t)uMaxAddr)); 176 195 } 177 196 else 197 { 178 198 pPD = NULL; 199 BS3PAGING_DPRINTF1(("bs3PagingGetPte: out of range! uFlat=%#RX64 max=%RX32\n", uFlat, (uint32_t)uMaxAddr)); 200 } 179 201 if (pPD) 180 202 { … … 184 206 { 185 207 if ((pPD->a[iPde].u & X86_PDE_PAE_PG_MASK) <= uMaxAddr) 208 { 186 209 pPTE = &((X86PTPAE BS3_FAR *)Bs3XptrFlatToCurrent(pPD->a[iPde].u & ~(uint64_t)PAGE_OFFSET_MASK))->a[iPte]; 210 *prc = VINF_SUCCESS; 211 } 212 else 213 BS3PAGING_DPRINTF1(("bs3PagingGetPte: out of range! iPde=%#x: %RX64 max=%RX32\n", 214 iPde, pPD->a[iPde].u, (uint32_t)uMaxAddr)); 187 215 } 188 216 else … … 205 233 ASMInvalidatePage(uFlat); 206 234 pPTE = &pPT->a[iPte]; 235 *prc = VINF_SUCCESS; 207 236 } 208 237 } 209 238 } 210 239 } 211 240 else 241 BS3PAGING_DPRINTF1(("bs3PagingGetPte: out of range! cr3=%#RX32 uMaxAddr=%#RX32\n", (uint32_t)cr3, (uint32_t)uMaxAddr)); 212 242 return pPTE; 213 243 } … … 217 247 BS3_CMN_DEF(int, Bs3PagingProtect,(uint64_t uFlat, uint64_t cb, uint64_t fSet, uint64_t fClear)) 218 248 { 219 RTCCUINTXREG const cr3 = ASMGetCR3(); 220 RTCCUINTXREG const cr4 = g_uBs3CpuDetected & BS3CPU_F_CPUID ? ASMGetCR4() : 0; 221 bool const fLegacyPTs = !(cr4 & X86_CR4_PAE); 222 bool const fUseInvlPg = (g_uBs3CpuDetected & BS3CPU_TYPE_MASK) >= BS3CPU_80486 223 && ( cb < UINT64_C(16)*PAGE_SIZE 224 || (cr4 & X86_CR4_PGE)); 225 unsigned cEntries; 226 int rc; 227 249 #if ARCH_BITS == 16 250 if (!BS3_MODE_IS_V86(g_bBs3CurrentMode)) 251 #endif 252 { 253 RTCCUINTXREG const cr3 = ASMGetCR3(); 254 RTCCUINTXREG const cr4 = g_uBs3CpuDetected & BS3CPU_F_CPUID ? ASMGetCR4() : 0; 255 bool const fLegacyPTs = !(cr4 & X86_CR4_PAE); 256 bool const fUseInvlPg = (g_uBs3CpuDetected & BS3CPU_TYPE_MASK) >= BS3CPU_80486 257 && ( cb < UINT64_C(16)*PAGE_SIZE 258 || (cr4 & X86_CR4_PGE)); 259 unsigned cEntries; 260 int rc; 261 262 /* 263 * Adjust the range parameters. 264 */ 265 cb += uFlat & PAGE_OFFSET_MASK; 266 cb = RT_ALIGN_64(cb, PAGE_SIZE); 267 uFlat &= ~(uint64_t)PAGE_OFFSET_MASK; 268 269 fSet &= ~X86_PTE_PAE_PG_MASK; 270 fClear &= ~X86_PTE_PAE_PG_MASK; 271 272 BS3PAGING_DPRINTF1(("Bs3PagingProtect: uFlat=%RX64 cb=%RX64 fSet=%RX64 fClear=%RX64 %s %s\n", uFlat, cb, fSet, fClear, 273 fLegacyPTs ? "legacy" : "pae/amd64", fUseInvlPg ? "invlpg" : "reload-cr3")); 274 if (fLegacyPTs) 275 { 276 /* 277 * Legacy page tables. 278 */ 279 while ((uint32_t)cb > 0) 280 { 281 PX86PTE pPte = BS3_CMN_FAR_NM(bs3PagingGetLegacyPte)(cr3, (uint32_t)uFlat, fUseInvlPg, &rc); 282 if (!pPte) 283 return rc; 284 285 cEntries = X86_PG_ENTRIES - ((uFlat >> X86_PT_SHIFT) & X86_PT_MASK); 286 while (cEntries-- > 0 && cb > 0) 287 { 288 pPte->u &= ~(uint32_t)fClear; 289 pPte->u |= (uint32_t)fSet; 290 if (fUseInvlPg) 291 ASMInvalidatePage(uFlat); 292 293 pPte++; 294 uFlat += PAGE_SIZE; 295 cb -= PAGE_SIZE; 296 } 297 } 298 } 299 else 300 { 301 /* 302 * Long mode or PAE page tables (at this level they are the same). 303 */ 304 while (cb > 0) 305 { 306 PX86PTEPAE pPte = BS3_CMN_FAR_NM(bs3PagingGetPte)(cr3, uFlat, fUseInvlPg, &rc); 307 if (!pPte) 308 return rc; 309 310 cEntries = X86_PG_ENTRIES - ((uFlat >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK); 311 while (cEntries-- > 0 && cb > 0) 312 { 313 pPte->u &= ~fClear; 314 pPte->u |= fSet; 315 if (fUseInvlPg) 316 ASMInvalidatePage(uFlat); 317 318 pPte++; 319 uFlat += PAGE_SIZE; 320 cb -= PAGE_SIZE; 321 } 322 } 323 } 324 325 /* 326 * Flush the TLB if we didn't use INVLPG above. 327 */ 328 BS3PAGING_DPRINTF2(("Bs3PagingProtect: reloading cr3=%RX32\n", (uint32_t)cr3)); 329 //if (!fUseInvlPg) 330 ASMSetCR3(cr3); 331 BS3PAGING_DPRINTF2(("Bs3PagingProtect: reloaded cr3=%RX32\n", (uint32_t)cr3)); 332 } 333 #if ARCH_BITS == 16 228 334 /* 229 * Adjust the range parameters.335 * We can do this stuff in v8086 mode. 230 336 */ 231 cb += uFlat & PAGE_OFFSET_MASK;232 cb = RT_ALIGN_64(cb, PAGE_SIZE);233 uFlat &= ~(uint64_t)PAGE_OFFSET_MASK;234 235 fSet &= ~X86_PTE_PAE_PG_MASK;236 fClear &= ~X86_PTE_PAE_PG_MASK;237 238 BS3PAGING_DPRINTF(("Bs3PagingProtect: uFlat=%RX64 cb=%RX64 fSet=%RX64 fClear=%RX64 %s %s\n", uFlat, cb, fSet, fClear,239 fLegacyPTs ? "legacy" : "pae/amd64", fUseInvlPg ? "invlpg" : "reload-cr3"));240 if (fLegacyPTs)241 {242 /*243 * Legacy page tables.244 */245 while ((uint32_t)cb > 0)246 {247 PX86PTE pPte = BS3_CMN_NM(bs3PagingGetLegacyPte)(cr3, (uint32_t)uFlat, fUseInvlPg, &rc);248 if (!pPte)249 return rc;250 251 cEntries = X86_PG_ENTRIES - ((uFlat >> X86_PT_SHIFT) & X86_PT_MASK);252 while (cEntries-- > 0 && cb > 0)253 {254 pPte->u &= ~(uint32_t)fClear;255 pPte->u |= (uint32_t)fSet;256 if (fUseInvlPg)257 ASMInvalidatePage(uFlat);258 259 pPte++;260 uFlat += PAGE_SIZE;261 cb -= PAGE_SIZE;262 }263 }264 }265 337 else 266 { 267 /* 268 * Long mode or PAE page tables (at this level they are the same). 269 */ 270 while (cb > 0) 271 { 272 PX86PTEPAE pPte = BS3_CMN_NM(bs3PagingGetPte)(cr3, uFlat, fUseInvlPg, &rc); 273 if (!pPte) 274 return rc; 275 276 cEntries = X86_PG_ENTRIES - ((uFlat >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK); 277 while (cEntries-- > 0 && cb > 0) 278 { 279 pPte->u &= ~fClear; 280 pPte->u |= fSet; 281 if (fUseInvlPg) 282 ASMInvalidatePage(uFlat); 283 284 pPte++; 285 uFlat += PAGE_SIZE; 286 cb -= PAGE_SIZE; 287 } 288 } 289 } 290 291 /* 292 * Flush the TLB if we didn't use INVLPG above. 293 */ 294 BS3PAGING_DPRINTF(("Bs3PagingProtect: reloading cr3=%RX32\n", (uint32_t)cr3)); 295 //if (!fUseInvlPg) 296 ASMSetCR3(cr3); 297 BS3PAGING_DPRINTF(("Bs3PagingProtect: reloaded cr3=%RX32\n", (uint32_t)cr3)); 298 338 return Bs3SwitchFromV86To16BitAndCallC((FPFNBS3FAR)Bs3PagingProtect_f16, sizeof(uint64_t)*4, uFlat, cb, fSet, fClear); 339 #endif 299 340 return VINF_SUCCESS; 300 341 }
Note:
See TracChangeset
for help on using the changeset viewer.