OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
VirtualMemory.c File Reference
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BaseOverflowLib.h>
#include <Library/MtrrLib.h>
#include <Library/OcDebugLogLib.h>
#include <Library/OcMemoryLib.h>
#include <Register/Intel/Cpuid.h>

Go to the source code of this file.

Functions

CHAR8 * OcGetMtrrTypeString (UINT8 MtrrType)
 
CHAR8 * OcGetPatTypeString (UINT8 PatType)
 
BOOLEAN OcIsPatSupported (VOID)
 
PAGE_MAP_AND_DIRECTORY_POINTEROcGetCurrentPageTable (OUT UINTN *Flags OPTIONAL)
 
STATIC BOOLEAN DisablePageTableWriteProtection (VOID)
 
STATIC VOID EnablePageTableWriteProtection (VOID)
 
EFI_STATUS OcGetPhysicalAddress (IN PAGE_MAP_AND_DIRECTORY_POINTER *PageTable OPTIONAL, IN EFI_VIRTUAL_ADDRESS VirtualAddr, OUT EFI_PHYSICAL_ADDRESS *PhysicalAddr)
 
EFI_STATUS OcSetPatIndexForAddressRange (IN PAGE_MAP_AND_DIRECTORY_POINTER *PageTable OPTIONAL, IN EFI_VIRTUAL_ADDRESS VirtualAddr, IN UINT64 Length, IN PAT_INDEX *PatIndex)
 
EFI_STATUS OcGetSetPageTableInfoForAddress (IN PAGE_MAP_AND_DIRECTORY_POINTER *PageTable OPTIONAL, IN EFI_VIRTUAL_ADDRESS VirtualAddr, OUT EFI_PHYSICAL_ADDRESS *PhysicalAddr OPTIONAL, OUT UINT8 *Level OPTIONAL, OUT UINT64 *Bits OPTIONAL, IN OUT PAT_INDEX *PatIndex OPTIONAL, IN BOOLEAN SetPat)
 
EFI_STATUS OcModifyMtrrRange (IN EFI_PHYSICAL_ADDRESS Address, IN UINT8 MtrrType, OUT UINT64 *Length OPTIONAL)
 
EFI_STATUS VmAllocateMemoryPool (OUT OC_VMEM_CONTEXT *Context, IN UINTN NumPages, IN EFI_GET_MEMORY_MAP GetMemoryMap OPTIONAL)
 
VOID * VmAllocatePages (IN OUT OC_VMEM_CONTEXT *Context, IN UINTN NumPages)
 
EFI_STATUS VmMapVirtualPage (IN OUT OC_VMEM_CONTEXT *Context, IN OUT PAGE_MAP_AND_DIRECTORY_POINTER *PageTable OPTIONAL, IN EFI_VIRTUAL_ADDRESS VirtualAddr, IN EFI_PHYSICAL_ADDRESS PhysicalAddr)
 
EFI_STATUS VmMapVirtualPages (IN OUT OC_VMEM_CONTEXT *Context, IN OUT PAGE_MAP_AND_DIRECTORY_POINTER *PageTable OPTIONAL, IN EFI_VIRTUAL_ADDRESS VirtualAddr, IN UINT64 NumPages, IN EFI_PHYSICAL_ADDRESS PhysicalAddr)
 
VOID VmFlushCaches (VOID)
 

Variables

STATIC CHAR8 * MtrrTypeStrings []
 
STATIC CHAR8 * PatTypeStrings []
 

Detailed Description

Copyright (C) 2011, dmazar. All rights reserved. Copyright (C) 2019, vit9696. All rights reserved.

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.

Definition in file VirtualMemory.c.

Function Documentation

◆ DisablePageTableWriteProtection()

STATIC BOOLEAN DisablePageTableWriteProtection ( VOID )

Definition at line 117 of file VirtualMemory.c.

◆ EnablePageTableWriteProtection()

STATIC VOID EnablePageTableWriteProtection ( VOID )

Definition at line 135 of file VirtualMemory.c.

◆ OcGetCurrentPageTable()

PAGE_MAP_AND_DIRECTORY_POINTER * OcGetCurrentPageTable ( OUT UINTN *Flags OPTIONAL)

Return pointer to PML4 table in PageTable and PWT and PCD flags in Flags.

Parameters
[out]FlagsCurrent page table PWT and PCT flags.
Return values
Currentpage table address.

Definition at line 97 of file VirtualMemory.c.

◆ OcGetMtrrTypeString()

CHAR8 * OcGetMtrrTypeString ( UINT8 MtrrType)

Return short string representation of MTRR cache type, as used in Intel tables.

Parameters
[in]MtrrTypeMTRR type.
Return values
Stringrepresentation.

Definition at line 58 of file VirtualMemory.c.

◆ OcGetPatTypeString()

CHAR8 * OcGetPatTypeString ( UINT8 PatType)

Return short string representation of PAT cache type, as used in Intel tables.

Parameters
[in]PatTypePAT type.
Return values
Stringrepresentation.

Definition at line 70 of file VirtualMemory.c.

◆ OcGetPhysicalAddress()

EFI_STATUS OcGetPhysicalAddress ( IN PAGE_MAP_AND_DIRECTORY_POINTER *PageTable OPTIONAL,
IN EFI_VIRTUAL_ADDRESS VirtualAddr,
OUT EFI_PHYSICAL_ADDRESS * PhysicalAddr )

Return physical address for given virtual address.

Parameters
[in]PageTablePage table to use for solving.
[in]VirtualAddrVirtual address to look up.
[out]PhysicalAddrPhysical address to return.
Return values
EFI_SUCCESSon successful lookup.

Definition at line 146 of file VirtualMemory.c.

◆ OcGetSetPageTableInfoForAddress()

EFI_STATUS OcGetSetPageTableInfoForAddress ( IN PAGE_MAP_AND_DIRECTORY_POINTER *PageTable OPTIONAL,
IN EFI_VIRTUAL_ADDRESS VirtualAddr,
OUT EFI_PHYSICAL_ADDRESS *PhysicalAddr OPTIONAL,
OUT UINT8 *Level OPTIONAL,
OUT UINT64 *Bits OPTIONAL,
IN OUT PAT_INDEX *PatIndex OPTIONAL,
IN BOOLEAN SetPat )

Get page table info for a given virtual address. Includes option to get or set Page Attribute Table (PAT) index for caching type.

Parameters
[in]PageTablePage table to use for solving, and modify iff SetPat==TRUE.
[in]VirtualAddrVirtual address to look up.
[out]PhysicalAddrPhysical address to return.
[out]LevelPage table mapping level, 1, 2 or 4 for 1Gb, 2Mb or 4kb.
[out]BitsPage table mapping flag bits.
[in,out]PatIndexPAT index to get or set.
[in]SetPatIf TRUE then set PatIndex if present, else get.
Return values
EFI_SUCCESSon success.

Definition at line 221 of file VirtualMemory.c.

◆ OcIsPatSupported()

BOOLEAN OcIsPatSupported ( VOID )

Checks if PAT is supported.

Return values
TRUEPAT is supported.
FALSEPAT is not supported.

Definition at line 82 of file VirtualMemory.c.

◆ OcModifyMtrrRange()

EFI_STATUS OcModifyMtrrRange ( IN EFI_PHYSICAL_ADDRESS Address,
IN UINT8 MtrrType,
OUT UINT64 *Length OPTIONAL )

Attempt to set entire MTRR range which starts with address to new MTRR cache type. Use with caution - attempting to change cache-type on the fly of any region used for code or data is likely to hang. Works successfully to uncache GOP (i.e. WC->UC), producing visibly slower results, on newer firmware where GOP range is set to WC in the first place.

Parameters
[in]AddressStart of range.
[in]MtrrTypeMTRR cache type to assign.
[out]LengthLength of range converted or attempted to be converted, when non-zero.
Return values
EFI_SUCCESSif cache type is set and system is still running.
EFI_UNSUPPORTEDif MTRR is unsupported.
error- other error from methods called.

Definition at line 410 of file VirtualMemory.c.

◆ OcSetPatIndexForAddressRange()

EFI_STATUS OcSetPatIndexForAddressRange ( IN PAGE_MAP_AND_DIRECTORY_POINTER *PageTable OPTIONAL,
IN EFI_VIRTUAL_ADDRESS VirtualAddr,
IN UINT64 Length,
IN PAT_INDEX * PatIndex )

Set Page Attribute Table (PAT) index for an address range. Assumes UEFI identity mapping between physical and virtual addresses.

Parameters
[in]PageTablePage table to use and modify.
[in]VirtualAddrAddress range start.
[in]LengthAddress range length.
[in]PatIndexPAT index to set.
Return values
EFI_SUCCESSon success.
EFI_INVALID_PARAMETERon invalid memory range.
error- other error from called methods.

Definition at line 164 of file VirtualMemory.c.

◆ VmAllocateMemoryPool()

EFI_STATUS VmAllocateMemoryPool ( OUT OC_VMEM_CONTEXT * Context,
IN UINTN NumPages,
IN EFI_GET_MEMORY_MAP GetMemoryMap OPTIONAL )

Allocate EfiBootServicesData virtual memory pool from boot services in the end of BASE_4GB of RAM. Should be called while boot services are still usable.

Parameters
[out]ContextVirtual memory pool context.
[in]NumPagesNumber of pages to be allocated in the pool.
[in]GetMemoryMapCustom GetMemoryMap implementation to use, optional.
Return values
EFI_SUCCESSon successful allocation.

Definition at line 441 of file VirtualMemory.c.

◆ VmAllocatePages()

VOID * VmAllocatePages ( IN OUT OC_VMEM_CONTEXT * Context,
IN UINTN NumPages )

Allocate pages for e.g. vm page maps.

Parameters
[in,out]ContextVirtual memory pool context.
[in]NumPagesNumber of pages to allocate.
Return values
allocatedpages or NULL.

Definition at line 469 of file VirtualMemory.c.

◆ VmFlushCaches()

VOID VmFlushCaches ( VOID )

Flushes TLB caches.

Definition at line 726 of file VirtualMemory.c.

◆ VmMapVirtualPage()

EFI_STATUS VmMapVirtualPage ( IN OUT OC_VMEM_CONTEXT * Context,
IN OUT PAGE_MAP_AND_DIRECTORY_POINTER *PageTable OPTIONAL,
IN EFI_VIRTUAL_ADDRESS VirtualAddr,
IN EFI_PHYSICAL_ADDRESS PhysicalAddr )

Map (remap) given page at physical address to given virtual address in the specified page table.

Parameters
[in,out]ContextVirtual memory pool context.
[in]PageTablePage table to update.
[in]VirtualAddrVirtual memory address to map at.
[in]PhysicalAddrPhysical memory address to map from.
Return values
EFI_SUCCESSon success.

Definition at line 488 of file VirtualMemory.c.

◆ VmMapVirtualPages()

EFI_STATUS VmMapVirtualPages ( IN OUT OC_VMEM_CONTEXT * Context,
IN OUT PAGE_MAP_AND_DIRECTORY_POINTER *PageTable OPTIONAL,
IN EFI_VIRTUAL_ADDRESS VirtualAddr,
IN UINT64 NumPages,
IN EFI_PHYSICAL_ADDRESS PhysicalAddr )

Map (remap) a range of 4K pages at physical address to given virtual address in the specified page table.

Parameters
[in,out]ContextVirtual memory pool context.
[in]PageTablePage table to update.
[in]VirtualAddrVirtual memory address to map at.
[in]NumPagesNumber of 4K pages to map.
[in]PhysicalAddrPhysical memory address to map from.
Return values
EFI_SUCCESSon success.

Definition at line 693 of file VirtualMemory.c.

Variable Documentation

◆ MtrrTypeStrings

STATIC CHAR8* MtrrTypeStrings[]
Initial value:
= {
"UC",
"WC",
"Reserved",
"Reserved",
"WT",
"WP",
"WB"
}

Definition at line 31 of file VirtualMemory.c.

◆ PatTypeStrings

STATIC CHAR8* PatTypeStrings[]
Initial value:
= {
"UC",
"WC",
"Reserved",
"Reserved",
"WT",
"WP",
"WB",
"UC-"
}

Definition at line 46 of file VirtualMemory.c.