OcMemoryLib: Added support for R/O page tables in SetupVirtualMap

references acidanthera/bugtracker#719
references acidanthera/bugtracker#1486
This commit is contained in:
vit9696 2021-02-18 08:15:35 +03:00
parent b4449f48dd
commit 3aa6bc3aaa
3 changed files with 53 additions and 4 deletions

View File

@ -8,6 +8,7 @@ OpenCore Changelog
- Added `SSN` (and `HW_SSN`) variable support
- Added onscreen early logging in DEBUG builds for legacy firmware
- Added workaround for firmware not specifying DeviceHandle at bootstrap
- Added support for R/O page tables in `SetupVirtualMap` quirk
#### v0.6.6
- Added keyboard and pointer entry scroll support in OpenCanopy

View File

@ -142,6 +142,8 @@ typedef union {
#pragma pack(pop)
#define CR0_WP BIT16
#define CR3_ADDR_MASK 0x000FFFFFFFFFF000ull
#define CR3_FLAG_PWT 0x0000000000000008ull
#define CR3_FLAG_PCD 0x0000000000000010ull

View File

@ -26,19 +26,49 @@ OcGetCurrentPageTable (
)
{
PAGE_MAP_AND_DIRECTORY_POINTER *PageTable;
UINTN CR3;
UINTN Cr3;
CR3 = AsmReadCr3 ();
Cr3 = AsmReadCr3 ();
PageTable = (PAGE_MAP_AND_DIRECTORY_POINTER *)(UINTN) (CR3 & CR3_ADDR_MASK);
PageTable = (PAGE_MAP_AND_DIRECTORY_POINTER *)(UINTN) (Cr3 & CR3_ADDR_MASK);
if (Flags != NULL) {
*Flags = CR3 & (CR3_FLAG_PWT | CR3_FLAG_PCD);
*Flags = Cr3 & (CR3_FLAG_PWT | CR3_FLAG_PCD);
}
return PageTable;
}
STATIC
BOOLEAN
DisablePageTableWriteProtection (
VOID
)
{
UINTN Cr0;
Cr0 = AsmReadCr0 ();
if ((Cr0 & CR0_WP) != 0) {
AsmWriteCr0 (Cr0 & ~CR0_WP);
return TRUE;
}
return FALSE;
}
STATIC
VOID
EnablePageTableWriteProtection (
VOID
)
{
UINTN Cr0;
Cr0 = AsmReadCr0 ();
AsmWriteCr0 (Cr0 | CR0_WP);
}
EFI_STATUS
OcGetPhysicalAddress (
IN PAGE_MAP_AND_DIRECTORY_POINTER *PageTable OPTIONAL,
@ -208,11 +238,14 @@ VmMapVirtualPage (
PAGE_TABLE_2M_ENTRY *PTE2M;
PAGE_TABLE_1G_ENTRY *PTE1G;
UINTN Index;
BOOLEAN WriteProtected;
if (PageTable == NULL) {
PageTable = OcGetCurrentPageTable (NULL);
}
WriteProtected = DisablePageTableWriteProtection ();
VA.Uint64 = (UINT64) VirtualAddr;
//
@ -242,6 +275,9 @@ VmMapVirtualPage (
PDPE = (PAGE_MAP_AND_DIRECTORY_POINTER *) VmAllocatePages (Context, 1);
if (PDPE == NULL) {
if (WriteProtected) {
EnablePageTableWriteProtection ();
}
return EFI_NO_MAPPING;
}
@ -282,6 +318,9 @@ VmMapVirtualPage (
PDE = (PAGE_MAP_AND_DIRECTORY_POINTER *) VmAllocatePages(Context, 1);
if (PDE == NULL) {
if (WriteProtected) {
EnablePageTableWriteProtection ();
}
return EFI_NO_MAPPING;
}
@ -325,6 +364,9 @@ VmMapVirtualPage (
PTE4K = (PAGE_TABLE_4K_ENTRY *) VmAllocatePages (Context, 1);
if (PTE4K == NULL) {
if (WriteProtected) {
EnablePageTableWriteProtection ();
}
return EFI_NO_MAPPING;
}
@ -370,6 +412,10 @@ VmMapVirtualPage (
PTE4K->Bits.ReadWrite = 1;
PTE4K->Bits.Present = 1;
if (WriteProtected) {
EnablePageTableWriteProtection ();
}
return EFI_SUCCESS;
}