1 | /** @file
|
---|
2 | Page table management header file.
|
---|
3 |
|
---|
4 | Copyright (c) 2017 - 2019, 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 | #include <Protocol/MemoryAttribute.h>
|
---|
14 |
|
---|
15 | #define PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PSE BIT0
|
---|
16 | #define PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE BIT1
|
---|
17 | #define PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAGE_1G_SUPPORT BIT2
|
---|
18 | #define PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_5_LEVEL BIT3
|
---|
19 | #define PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_WP_ENABLE BIT30
|
---|
20 | #define PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_XD_ACTIVATED BIT31
|
---|
21 | // Other bits are reserved for future use
|
---|
22 | typedef struct {
|
---|
23 | UINT32 PageTableBase;
|
---|
24 | UINT32 Reserved;
|
---|
25 | UINT32 Attributes;
|
---|
26 | } PAGE_TABLE_LIB_PAGING_CONTEXT_IA32;
|
---|
27 |
|
---|
28 | typedef struct {
|
---|
29 | UINT64 PageTableBase;
|
---|
30 | UINT32 Attributes;
|
---|
31 | } PAGE_TABLE_LIB_PAGING_CONTEXT_X64;
|
---|
32 |
|
---|
33 | typedef union {
|
---|
34 | PAGE_TABLE_LIB_PAGING_CONTEXT_IA32 Ia32;
|
---|
35 | PAGE_TABLE_LIB_PAGING_CONTEXT_X64 X64;
|
---|
36 | } PAGE_TABLE_LIB_PAGING_CONTEXT_DATA;
|
---|
37 |
|
---|
38 | typedef struct {
|
---|
39 | //
|
---|
40 | // PE32+ Machine type for EFI images
|
---|
41 | //
|
---|
42 | // #define IMAGE_FILE_MACHINE_I386 0x014c
|
---|
43 | // #define IMAGE_FILE_MACHINE_X64 0x8664
|
---|
44 | //
|
---|
45 | UINT16 MachineType;
|
---|
46 | PAGE_TABLE_LIB_PAGING_CONTEXT_DATA ContextData;
|
---|
47 | } PAGE_TABLE_LIB_PAGING_CONTEXT;
|
---|
48 |
|
---|
49 | #define PAGE_TABLE_POOL_ALIGNMENT BASE_2MB
|
---|
50 | #define PAGE_TABLE_POOL_UNIT_SIZE SIZE_2MB
|
---|
51 | #define PAGE_TABLE_POOL_UNIT_PAGES EFI_SIZE_TO_PAGES (PAGE_TABLE_POOL_UNIT_SIZE)
|
---|
52 | #define PAGE_TABLE_POOL_ALIGN_MASK \
|
---|
53 | (~(EFI_PHYSICAL_ADDRESS)(PAGE_TABLE_POOL_ALIGNMENT - 1))
|
---|
54 |
|
---|
55 | typedef struct {
|
---|
56 | VOID *NextPool;
|
---|
57 | UINTN Offset;
|
---|
58 | UINTN FreePages;
|
---|
59 | } PAGE_TABLE_POOL;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | Allocates one or more 4KB pages for page table.
|
---|
63 |
|
---|
64 | @param Pages The number of 4 KB pages to allocate.
|
---|
65 |
|
---|
66 | @return A pointer to the allocated buffer or NULL if allocation fails.
|
---|
67 |
|
---|
68 | **/
|
---|
69 | typedef
|
---|
70 | VOID *
|
---|
71 | (EFIAPI *PAGE_TABLE_LIB_ALLOCATE_PAGES)(
|
---|
72 | IN UINTN Pages
|
---|
73 | );
|
---|
74 |
|
---|
75 | /**
|
---|
76 | This function assigns the page attributes for the memory region specified by BaseAddress and
|
---|
77 | Length from their current attributes to the attributes specified by Attributes.
|
---|
78 |
|
---|
79 | Caller should make sure BaseAddress and Length is at page boundary.
|
---|
80 |
|
---|
81 | Caller need guarantee the TPL <= TPL_NOTIFY, if there is split page request.
|
---|
82 |
|
---|
83 | @param PagingContext The paging context. NULL means get page table from current CPU context.
|
---|
84 | @param BaseAddress The physical address that is the start address of a memory region.
|
---|
85 | @param Length The size in bytes of the memory region.
|
---|
86 | @param Attributes The bit mask of attributes to set for the memory region.
|
---|
87 | @param AllocatePagesFunc If page split is needed, this function is used to allocate more pages.
|
---|
88 | NULL mean page split is unsupported.
|
---|
89 |
|
---|
90 | @retval RETURN_SUCCESS The attributes were cleared for the memory region.
|
---|
91 | @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by
|
---|
92 | BaseAddress and Length cannot be modified.
|
---|
93 | @retval RETURN_INVALID_PARAMETER Length is zero.
|
---|
94 | Attributes specified an illegal combination of attributes that
|
---|
95 | cannot be set together.
|
---|
96 | @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
|
---|
97 | the memory resource range.
|
---|
98 | @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the memory
|
---|
99 | resource range specified by BaseAddress and Length.
|
---|
100 | The bit mask of attributes is not support for the memory resource
|
---|
101 | range specified by BaseAddress and Length.
|
---|
102 | **/
|
---|
103 | RETURN_STATUS
|
---|
104 | EFIAPI
|
---|
105 | AssignMemoryPageAttributes (
|
---|
106 | IN PAGE_TABLE_LIB_PAGING_CONTEXT *PagingContext OPTIONAL,
|
---|
107 | IN PHYSICAL_ADDRESS BaseAddress,
|
---|
108 | IN UINT64 Length,
|
---|
109 | IN UINT64 Attributes,
|
---|
110 | IN PAGE_TABLE_LIB_ALLOCATE_PAGES AllocatePagesFunc OPTIONAL
|
---|
111 | );
|
---|
112 |
|
---|
113 | /**
|
---|
114 | Initialize the Page Table lib.
|
---|
115 | **/
|
---|
116 | VOID
|
---|
117 | InitializePageTableLib (
|
---|
118 | VOID
|
---|
119 | );
|
---|
120 |
|
---|
121 | /**
|
---|
122 | This API provides a way to allocate memory for page table.
|
---|
123 |
|
---|
124 | This API can be called more once to allocate memory for page tables.
|
---|
125 |
|
---|
126 | Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
|
---|
127 | allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
|
---|
128 | is returned. If there is not enough memory remaining to satisfy the request, then NULL is
|
---|
129 | returned.
|
---|
130 |
|
---|
131 | @param Pages The number of 4 KB pages to allocate.
|
---|
132 |
|
---|
133 | @return A pointer to the allocated buffer or NULL if allocation fails.
|
---|
134 |
|
---|
135 | **/
|
---|
136 | VOID *
|
---|
137 | EFIAPI
|
---|
138 | AllocatePageTableMemory (
|
---|
139 | IN UINTN Pages
|
---|
140 | );
|
---|
141 |
|
---|
142 | /**
|
---|
143 | Get paging details.
|
---|
144 |
|
---|
145 | @param PagingContextData The paging context.
|
---|
146 | @param PageTableBase Return PageTableBase field.
|
---|
147 | @param Attributes Return Attributes field.
|
---|
148 |
|
---|
149 | **/
|
---|
150 | VOID
|
---|
151 | GetPagingDetails (
|
---|
152 | IN PAGE_TABLE_LIB_PAGING_CONTEXT_DATA *PagingContextData,
|
---|
153 | OUT UINTN **PageTableBase OPTIONAL,
|
---|
154 | OUT UINT32 **Attributes OPTIONAL
|
---|
155 | );
|
---|
156 |
|
---|
157 | /**
|
---|
158 | Install Efi Memory Attribute Protocol.
|
---|
159 |
|
---|
160 | @param Handle A pointer to the EFI_HANDLE on which the interface is to be installed
|
---|
161 |
|
---|
162 | **/
|
---|
163 | VOID
|
---|
164 | InstallEfiMemoryAttributeProtocol (
|
---|
165 | IN EFI_HANDLE Handle
|
---|
166 | );
|
---|
167 |
|
---|
168 | #endif
|
---|