]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/CpuDxe/CpuPageTable.h
UefiCpuPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuPageTable.h
1 /** @file
2 Page table management header file.
3
4 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #ifndef _PAGE_TABLE_LIB_H_
10 #define _PAGE_TABLE_LIB_H_
11
12 #include <IndustryStandard/PeImage.h>
13
14 #define PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PSE BIT0
15 #define PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE BIT1
16 #define PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAGE_1G_SUPPORT BIT2
17 #define PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_WP_ENABLE BIT30
18 #define PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_XD_ACTIVATED BIT31
19 // Other bits are reserved for future use
20 typedef struct {
21 UINT32 PageTableBase;
22 UINT32 Reserved;
23 UINT32 Attributes;
24 } PAGE_TABLE_LIB_PAGING_CONTEXT_IA32;
25
26 typedef struct {
27 UINT64 PageTableBase;
28 UINT32 Attributes;
29 } PAGE_TABLE_LIB_PAGING_CONTEXT_X64;
30
31 typedef union {
32 PAGE_TABLE_LIB_PAGING_CONTEXT_IA32 Ia32;
33 PAGE_TABLE_LIB_PAGING_CONTEXT_X64 X64;
34 } PAGE_TABLE_LIB_PAGING_CONTEXT_DATA;
35
36 typedef struct {
37 //
38 // PE32+ Machine type for EFI images
39 //
40 // #define IMAGE_FILE_MACHINE_I386 0x014c
41 // #define IMAGE_FILE_MACHINE_X64 0x8664
42 //
43 UINT16 MachineType;
44 PAGE_TABLE_LIB_PAGING_CONTEXT_DATA ContextData;
45 } PAGE_TABLE_LIB_PAGING_CONTEXT;
46
47 #define PAGE_TABLE_POOL_ALIGNMENT BASE_2MB
48 #define PAGE_TABLE_POOL_UNIT_SIZE SIZE_2MB
49 #define PAGE_TABLE_POOL_UNIT_PAGES EFI_SIZE_TO_PAGES (PAGE_TABLE_POOL_UNIT_SIZE)
50 #define PAGE_TABLE_POOL_ALIGN_MASK \
51 (~(EFI_PHYSICAL_ADDRESS)(PAGE_TABLE_POOL_ALIGNMENT - 1))
52
53 typedef struct {
54 VOID *NextPool;
55 UINTN Offset;
56 UINTN FreePages;
57 } PAGE_TABLE_POOL;
58
59
60 /**
61 Allocates one or more 4KB pages for page table.
62
63 @param Pages The number of 4 KB pages to allocate.
64
65 @return A pointer to the allocated buffer or NULL if allocation fails.
66
67 **/
68 typedef
69 VOID *
70 (EFIAPI *PAGE_TABLE_LIB_ALLOCATE_PAGES) (
71 IN UINTN Pages
72 );
73
74 /**
75 This function assigns the page attributes for the memory region specified by BaseAddress and
76 Length from their current attributes to the attributes specified by Attributes.
77
78 Caller should make sure BaseAddress and Length is at page boundary.
79
80 Caller need guarentee the TPL <= TPL_NOTIFY, if there is split page request.
81
82 @param PagingContext The paging context. NULL means get page table from current CPU context.
83 @param BaseAddress The physical address that is the start address of a memory region.
84 @param Length The size in bytes of the memory region.
85 @param Attributes The bit mask of attributes to set for the memory region.
86 @param AllocatePagesFunc If page split is needed, this function is used to allocate more pages.
87 NULL mean page split is unsupported.
88
89 @retval RETURN_SUCCESS The attributes were cleared for the memory region.
90 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by
91 BaseAddress and Length cannot be modified.
92 @retval RETURN_INVALID_PARAMETER Length is zero.
93 Attributes specified an illegal combination of attributes that
94 cannot be set together.
95 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
96 the memory resource range.
97 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the memory
98 resource range specified by BaseAddress and Length.
99 The bit mask of attributes is not support for the memory resource
100 range specified by BaseAddress and Length.
101 **/
102 RETURN_STATUS
103 EFIAPI
104 AssignMemoryPageAttributes (
105 IN PAGE_TABLE_LIB_PAGING_CONTEXT *PagingContext OPTIONAL,
106 IN PHYSICAL_ADDRESS BaseAddress,
107 IN UINT64 Length,
108 IN UINT64 Attributes,
109 IN PAGE_TABLE_LIB_ALLOCATE_PAGES AllocatePagesFunc OPTIONAL
110 );
111
112 /**
113 Initialize the Page Table lib.
114 **/
115 VOID
116 InitializePageTableLib (
117 VOID
118 );
119
120 /**
121 This API provides a way to allocate memory for page table.
122
123 This API can be called more once to allocate memory for page tables.
124
125 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
126 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
127 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
128 returned.
129
130 @param Pages The number of 4 KB pages to allocate.
131
132 @return A pointer to the allocated buffer or NULL if allocation fails.
133
134 **/
135 VOID *
136 EFIAPI
137 AllocatePageTableMemory (
138 IN UINTN Pages
139 );
140
141 #endif