3 Virtual Memory Management Services to set or clear the memory encryption bit
5 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
6 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 Code is derived from MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
20 #ifndef __VIRTUAL_MEMORY__
21 #define __VIRTUAL_MEMORY__
24 #include <Library/BaseLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/DebugLib.h>
27 #include <Library/MemoryAllocationLib.h>
29 #include <Library/CacheMaintenanceLib.h>
30 #define SYS_CODE64_SEL 0x38
35 // Page-Map Level-4 Offset (PML4) and
36 // Page-Directory-Pointer Offset (PDPE) entries 4K & 2MB
41 UINT64 Present
:1; // 0 = Not present in memory, 1 = Present in memory
42 UINT64 ReadWrite
:1; // 0 = Read-Only, 1= Read/Write
43 UINT64 UserSupervisor
:1; // 0 = Supervisor, 1=User
44 UINT64 WriteThrough
:1; // 0 = Write-Back caching, 1=Write-Through caching
45 UINT64 CacheDisabled
:1; // 0 = Cached, 1=Non-Cached
46 UINT64 Accessed
:1; // 0 = Not accessed, 1 = Accessed (set by CPU)
47 UINT64 Reserved
:1; // Reserved
48 UINT64 MustBeZero
:2; // Must Be Zero
49 UINT64 Available
:3; // Available for use by system software
50 UINT64 PageTableBaseAddress
:40; // Page Table Base Address
51 UINT64 AvabilableHigh
:11; // Available for use by system software
52 UINT64 Nx
:1; // No Execute bit
55 } PAGE_MAP_AND_DIRECTORY_POINTER
;
58 // Page Table Entry 4KB
62 UINT64 Present
:1; // 0 = Not present in memory, 1 = Present in memory
63 UINT64 ReadWrite
:1; // 0 = Read-Only, 1= Read/Write
64 UINT64 UserSupervisor
:1; // 0 = Supervisor, 1=User
65 UINT64 WriteThrough
:1; // 0 = Write-Back caching, 1=Write-Through caching
66 UINT64 CacheDisabled
:1; // 0 = Cached, 1=Non-Cached
67 UINT64 Accessed
:1; // 0 = Not accessed, 1 = Accessed (set by CPU)
68 UINT64 Dirty
:1; // 0 = Not Dirty, 1 = written by processor on access to page
70 UINT64 Global
:1; // 0 = Not global page, 1 = global page TLB not cleared on CR3 write
71 UINT64 Available
:3; // Available for use by system software
72 UINT64 PageTableBaseAddress
:40; // Page Table Base Address
73 UINT64 AvabilableHigh
:11; // Available for use by system software
74 UINT64 Nx
:1; // 0 = Execute Code, 1 = No Code Execution
77 } PAGE_TABLE_4K_ENTRY
;
80 // Page Table Entry 2MB
84 UINT64 Present
:1; // 0 = Not present in memory, 1 = Present in memory
85 UINT64 ReadWrite
:1; // 0 = Read-Only, 1= Read/Write
86 UINT64 UserSupervisor
:1; // 0 = Supervisor, 1=User
87 UINT64 WriteThrough
:1; // 0 = Write-Back caching, 1=Write-Through caching
88 UINT64 CacheDisabled
:1; // 0 = Cached, 1=Non-Cached
89 UINT64 Accessed
:1; // 0 = Not accessed, 1 = Accessed (set by CPU)
90 UINT64 Dirty
:1; // 0 = Not Dirty, 1 = written by processor on access to page
91 UINT64 MustBe1
:1; // Must be 1
92 UINT64 Global
:1; // 0 = Not global page, 1 = global page TLB not cleared on CR3 write
93 UINT64 Available
:3; // Available for use by system software
95 UINT64 MustBeZero
:8; // Must be zero;
96 UINT64 PageTableBaseAddress
:31; // Page Table Base Address
97 UINT64 AvabilableHigh
:11; // Available for use by system software
98 UINT64 Nx
:1; // 0 = Execute Code, 1 = No Code Execution
104 // Page Table Entry 1GB
108 UINT64 Present
:1; // 0 = Not present in memory, 1 = Present in memory
109 UINT64 ReadWrite
:1; // 0 = Read-Only, 1= Read/Write
110 UINT64 UserSupervisor
:1; // 0 = Supervisor, 1=User
111 UINT64 WriteThrough
:1; // 0 = Write-Back caching, 1=Write-Through caching
112 UINT64 CacheDisabled
:1; // 0 = Cached, 1=Non-Cached
113 UINT64 Accessed
:1; // 0 = Not accessed, 1 = Accessed (set by CPU)
114 UINT64 Dirty
:1; // 0 = Not Dirty, 1 = written by processor on access to page
115 UINT64 MustBe1
:1; // Must be 1
116 UINT64 Global
:1; // 0 = Not global page, 1 = global page TLB not cleared on CR3 write
117 UINT64 Available
:3; // Available for use by system software
119 UINT64 MustBeZero
:17; // Must be zero;
120 UINT64 PageTableBaseAddress
:22; // Page Table Base Address
121 UINT64 AvabilableHigh
:11; // Available for use by system software
122 UINT64 Nx
:1; // 0 = Execute Code, 1 = No Code Execution
125 } PAGE_TABLE_1G_ENTRY
;
129 #define IA32_PG_P BIT0
130 #define IA32_PG_RW BIT1
132 #define PAGETABLE_ENTRY_MASK ((1UL << 9) - 1)
133 #define PML4_OFFSET(x) ( (x >> 39) & PAGETABLE_ENTRY_MASK)
134 #define PDP_OFFSET(x) ( (x >> 30) & PAGETABLE_ENTRY_MASK)
135 #define PDE_OFFSET(x) ( (x >> 21) & PAGETABLE_ENTRY_MASK)
136 #define PTE_OFFSET(x) ( (x >> 12) & PAGETABLE_ENTRY_MASK)
137 #define PAGING_1G_ADDRESS_MASK_64 0x000FFFFFC0000000ull
140 This function clears memory encryption bit for the memory region specified by PhysicalAddress
141 and length from the current page table context.
143 @param[in] PhysicalAddress The physical address that is the start address of a memory region.
144 @param[in] Length The length of memory region
145 @param[in] Flush Flush the caches before applying the encryption mask
147 @retval RETURN_SUCCESS The attributes were cleared for the memory region.
148 @retval RETURN_INVALID_PARAMETER Number of pages is zero.
149 @retval RETURN_UNSUPPORTED Setting the memory encyrption attribute is not supported
153 InternalMemEncryptSevSetMemoryDecrypted (
154 IN PHYSICAL_ADDRESS Cr3BaseAddress
,
155 IN PHYSICAL_ADDRESS PhysicalAddress
,
157 IN BOOLEAN CacheFlush
161 This function sets memory encryption bit for the memory region specified by
162 PhysicalAddress and length from the current page table context.
164 @param[in] PhysicalAddress The physical address that is the start address
166 @param[in] Length The length of memory region
167 @param[in] Flush Flush the caches before applying the
170 @retval RETURN_SUCCESS The attributes were cleared for the memory region.
171 @retval RETURN_INVALID_PARAMETER Number of pages is zero.
172 @retval RETURN_UNSUPPORTED Setting the memory encyrption attribute is
177 InternalMemEncryptSevSetMemoryEncrypted (
178 IN PHYSICAL_ADDRESS Cr3BaseAddress
,
179 IN PHYSICAL_ADDRESS PhysicalAddress
,
181 IN BOOLEAN CacheFlush