2006-04-22 00:54:32 +02:00
|
|
|
/*++
|
|
|
|
|
|
|
|
Copyright (c) 2006, Intel Corporation
|
|
|
|
All rights reserved. This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
|
|
|
|
Module Name:
|
|
|
|
VirtualMemory.c
|
|
|
|
|
|
|
|
Abstract:
|
|
|
|
|
|
|
|
x64 Virtual Memory Management Services in the form of an IA-32 driver.
|
|
|
|
Used to establish a 1:1 Virtual to Physical Mapping that is required to
|
|
|
|
enter Long Mode (x64 64-bit mode).
|
|
|
|
|
|
|
|
While we make a 1:1 mapping (identity mapping) for all physical pages
|
|
|
|
we still need to use the MTRR's to ensure that the cachability attirbutes
|
|
|
|
for all memory regions is correct.
|
|
|
|
|
|
|
|
The basic idea is to use 2MB page table entries where ever possible. If
|
|
|
|
more granularity of cachability is required then 4K page tables are used.
|
|
|
|
|
|
|
|
References:
|
|
|
|
1) IA-32 Intel(R) Atchitecture Software Developer's Manual Volume 1:Basic Architecture, Intel
|
|
|
|
2) IA-32 Intel(R) Atchitecture Software Developer's Manual Volume 2:Instruction Set Reference, Intel
|
|
|
|
3) IA-32 Intel(R) Atchitecture Software Developer's Manual Volume 3:System Programmer's Guide, Intel
|
|
|
|
|
|
|
|
--*/
|
|
|
|
|
|
|
|
#include "VirtualMemory.h"
|
|
|
|
|
2006-11-29 02:49:26 +01:00
|
|
|
UINTN
|
2006-04-22 00:54:32 +02:00
|
|
|
CreateIdentityMappingPageTables (
|
2006-09-26 09:22:33 +02:00
|
|
|
VOID
|
2006-04-22 00:54:32 +02:00
|
|
|
)
|
|
|
|
/*++
|
|
|
|
|
|
|
|
Routine Description:
|
|
|
|
|
|
|
|
Allocates and fills in the Page Directory and Page Table Entries to
|
2006-09-26 09:22:33 +02:00
|
|
|
establish a 1:1 Virtual to Physical mapping.
|
2006-04-22 00:54:32 +02:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
|
|
NumberOfProcessorPhysicalAddressBits - Number of processor address bits to use.
|
|
|
|
Limits the number of page table entries
|
|
|
|
to the physical address space.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
EFI_SUCCESS The 1:1 Virtual to Physical identity mapping was created
|
|
|
|
|
|
|
|
--*/
|
|
|
|
{
|
2006-09-26 09:22:33 +02:00
|
|
|
UINT32 RegEax;
|
|
|
|
UINT8 PhysicalAddressBits;
|
2006-04-22 00:54:32 +02:00
|
|
|
EFI_PHYSICAL_ADDRESS PageAddress;
|
2006-09-26 09:22:33 +02:00
|
|
|
UINTN IndexOfPml4Entries;
|
|
|
|
UINTN IndexOfPdpEntries;
|
|
|
|
UINTN IndexOfPageDirectoryEntries;
|
|
|
|
UINTN NumberOfPml4EntriesNeeded;
|
|
|
|
UINTN NumberOfPdpEntriesNeeded;
|
|
|
|
PAGE_MAP_AND_DIRECTORY_POINTER *PageMapLevel4Entry;
|
|
|
|
PAGE_MAP_AND_DIRECTORY_POINTER *PageMap;
|
|
|
|
PAGE_MAP_AND_DIRECTORY_POINTER *PageDirectoryPointerEntry;
|
|
|
|
PAGE_TABLE_ENTRY *PageDirectoryEntry;
|
2006-12-22 09:11:47 +01:00
|
|
|
UINTN TotalPagesNum;
|
|
|
|
UINTN BigPageAddress;
|
2006-04-22 00:54:32 +02:00
|
|
|
|
|
|
|
//
|
2006-09-26 09:22:33 +02:00
|
|
|
// Get physical address bits supported.
|
2006-04-22 00:54:32 +02:00
|
|
|
//
|
2006-09-26 09:22:33 +02:00
|
|
|
AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
|
|
|
|
if (RegEax >= 0x80000008) {
|
|
|
|
AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
|
|
|
|
PhysicalAddressBits = (UINT8) RegEax;
|
|
|
|
} else {
|
|
|
|
PhysicalAddressBits = 36;
|
|
|
|
}
|
2006-04-22 00:54:32 +02:00
|
|
|
|
|
|
|
//
|
2006-09-26 09:22:33 +02:00
|
|
|
// Calculate the table entries needed.
|
2006-04-22 00:54:32 +02:00
|
|
|
//
|
2006-09-26 09:22:33 +02:00
|
|
|
if (PhysicalAddressBits <= 39 ) {
|
|
|
|
NumberOfPml4EntriesNeeded = 1;
|
|
|
|
NumberOfPdpEntriesNeeded = 1 << (PhysicalAddressBits - 30);
|
2006-04-22 00:54:32 +02:00
|
|
|
} else {
|
2006-09-26 09:22:33 +02:00
|
|
|
NumberOfPml4EntriesNeeded = 1 << (PhysicalAddressBits - 39);
|
|
|
|
NumberOfPdpEntriesNeeded = 512;
|
2006-04-22 00:54:32 +02:00
|
|
|
}
|
|
|
|
|
2006-12-22 09:11:47 +01:00
|
|
|
//
|
|
|
|
// Pre-allocate big pages to avoid later allocations.
|
|
|
|
//
|
|
|
|
TotalPagesNum = (NumberOfPdpEntriesNeeded + 1) * NumberOfPml4EntriesNeeded + 1;
|
|
|
|
BigPageAddress = (UINTN) AllocatePages (TotalPagesNum);
|
|
|
|
ASSERT (BigPageAddress != 0);
|
|
|
|
|
|
|
|
//
|
|
|
|
// By architecture only one PageMapLevel4 exists - so lets allocate storage for it.
|
|
|
|
//
|
|
|
|
PageMap = (VOID *) BigPageAddress;
|
|
|
|
BigPageAddress += EFI_PAGE_SIZE;
|
|
|
|
|
2006-09-26 09:22:33 +02:00
|
|
|
PageMapLevel4Entry = PageMap;
|
|
|
|
PageAddress = 0;
|
|
|
|
for (IndexOfPml4Entries = 0; IndexOfPml4Entries < NumberOfPml4EntriesNeeded; IndexOfPml4Entries++, PageMapLevel4Entry++) {
|
2006-04-22 00:54:32 +02:00
|
|
|
//
|
|
|
|
// Each PML4 entry points to a page of Page Directory Pointer entires.
|
2006-09-26 09:22:33 +02:00
|
|
|
// So lets allocate space for them and fill them in in the IndexOfPdpEntries loop.
|
|
|
|
//
|
2006-12-22 09:11:47 +01:00
|
|
|
PageDirectoryPointerEntry = (VOID *) BigPageAddress;
|
|
|
|
BigPageAddress += EFI_PAGE_SIZE;
|
2006-04-22 00:54:32 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Make a PML4 Entry
|
|
|
|
//
|
|
|
|
PageMapLevel4Entry->Uint64 = (UINT64)(UINTN)PageDirectoryPointerEntry;
|
|
|
|
PageMapLevel4Entry->Bits.ReadWrite = 1;
|
|
|
|
PageMapLevel4Entry->Bits.Present = 1;
|
|
|
|
|
2006-09-26 09:22:33 +02:00
|
|
|
for (IndexOfPdpEntries = 0; IndexOfPdpEntries < NumberOfPdpEntriesNeeded; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {
|
2006-04-22 00:54:32 +02:00
|
|
|
//
|
|
|
|
// Each Directory Pointer entries points to a page of Page Directory entires.
|
2006-09-26 09:22:33 +02:00
|
|
|
// So allocate space for them and fill them in in the IndexOfPageDirectoryEntries loop.
|
2006-04-22 00:54:32 +02:00
|
|
|
//
|
2006-12-22 09:11:47 +01:00
|
|
|
PageDirectoryEntry = (VOID *) BigPageAddress;
|
|
|
|
BigPageAddress += EFI_PAGE_SIZE;
|
2006-04-22 00:54:32 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Fill in a Page Directory Pointer Entries
|
|
|
|
//
|
2006-09-26 09:22:33 +02:00
|
|
|
PageDirectoryPointerEntry->Uint64 = (UINT64)(UINTN)PageDirectoryEntry;
|
2006-04-22 00:54:32 +02:00
|
|
|
PageDirectoryPointerEntry->Bits.ReadWrite = 1;
|
|
|
|
PageDirectoryPointerEntry->Bits.Present = 1;
|
|
|
|
|
2006-09-26 09:22:33 +02:00
|
|
|
for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress += 0x200000) {
|
2006-04-22 00:54:32 +02:00
|
|
|
//
|
|
|
|
// Fill in the Page Directory entries
|
|
|
|
//
|
2006-09-26 09:22:33 +02:00
|
|
|
PageDirectoryEntry->Uint64 = (UINT64)PageAddress;
|
|
|
|
PageDirectoryEntry->Bits.ReadWrite = 1;
|
|
|
|
PageDirectoryEntry->Bits.Present = 1;
|
|
|
|
PageDirectoryEntry->Bits.MustBe1 = 1;
|
2006-04-22 00:54:32 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// For the PML4 entries we are not using fill in a null entry.
|
2006-09-26 09:22:33 +02:00
|
|
|
// For now we just copy the first entry.
|
|
|
|
//
|
|
|
|
for (; IndexOfPml4Entries < 512; IndexOfPml4Entries++, PageMapLevel4Entry++) {
|
|
|
|
CopyMem (
|
|
|
|
PageMapLevel4Entry,
|
|
|
|
PageMap,
|
|
|
|
sizeof (PAGE_MAP_AND_DIRECTORY_POINTER)
|
|
|
|
);
|
2006-04-22 00:54:32 +02:00
|
|
|
}
|
|
|
|
|
2006-11-29 02:49:26 +01:00
|
|
|
return (UINTN)PageMap; // FIXME
|
2006-04-22 00:54:32 +02:00
|
|
|
}
|
|
|
|
|