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