CpuPageTable.h 5.5 KB

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