TianoCore EDK2 master
Loading...
Searching...
No Matches
MemoryAttribute.c
Go to the documentation of this file.
1
9#include "CpuDxe.h"
10
22BOOLEAN
24 IN EFI_PHYSICAL_ADDRESS BaseAddress,
25 IN UINT64 Length
26 )
27{
29 EFI_PHYSICAL_ADDRESS GcdEndAddress;
30 EFI_STATUS Status;
31
32 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);
33 if (EFI_ERROR (Status) ||
35 {
36 return FALSE;
37 }
38
39 GcdEndAddress = GcdDescriptor.BaseAddress + GcdDescriptor.Length;
40
41 //
42 // Return TRUE if the GCD descriptor covers the range entirely
43 //
44 return GcdEndAddress >= (BaseAddress + Length);
45}
46
72 IN EFI_PHYSICAL_ADDRESS BaseAddress,
73 IN UINT64 Length,
74 OUT UINT64 *Attributes
75 )
76{
77 UINTN RegionAddress;
78 UINTN RegionLength;
79 UINTN RegionAttributes;
80 UINTN Union;
81 UINTN Intersection;
82 EFI_STATUS Status;
83
84 if ((Length == 0) || (Attributes == NULL)) {
85 DEBUG ((
86 DEBUG_ERROR,
87 "%a: BaseAddress 0x%llx Length 0x%llx is zero or Attributes is NULL\n",
88 __func__,
89 BaseAddress,
90 Length
91 ));
92 return EFI_INVALID_PARAMETER;
93 }
94
95 if (!RegionIsSystemMemory (BaseAddress, Length)) {
96 return EFI_UNSUPPORTED;
97 }
98
99 DEBUG ((
100 DEBUG_VERBOSE,
101 "%a: BaseAddress == 0x%lx, Length == 0x%lx\n",
102 __func__,
103 BaseAddress,
104 Length
105 ));
106
107 Union = 0;
108 Intersection = MAX_UINTN;
109
110 for (RegionAddress = (UINTN)BaseAddress;
111 RegionAddress < (UINTN)(BaseAddress + Length);
112 RegionAddress += RegionLength)
113 {
114 Status = GetMemoryRegion (
115 &RegionAddress,
116 &RegionLength,
117 &RegionAttributes
118 );
119
120 DEBUG ((
121 DEBUG_VERBOSE,
122 "%a: RegionAddress == 0x%lx, RegionLength == 0x%lx, RegionAttributes == 0x%lx\n",
123 __func__,
124 (UINT64)RegionAddress,
125 (UINT64)RegionLength,
126 (UINT64)RegionAttributes
127 ));
128
129 if (EFI_ERROR (Status)) {
130 return EFI_NO_MAPPING;
131 }
132
133 Union |= RegionAttributes;
134 Intersection &= RegionAttributes;
135 }
136
137 DEBUG ((
138 DEBUG_VERBOSE,
139 "%a: Union == %lx, Intersection == %lx\n",
140 __func__,
141 (UINT64)Union,
142 (UINT64)Intersection
143 ));
144
145 if (Union != Intersection) {
146 return EFI_NO_MAPPING;
147 }
148
149 *Attributes = RegionAttributeToGcdAttribute (Union);
150 *Attributes &= EFI_MEMORY_RP | EFI_MEMORY_RO | EFI_MEMORY_XP;
151 return EFI_SUCCESS;
152}
153
184STATIC
188 IN EFI_PHYSICAL_ADDRESS BaseAddress,
189 IN UINT64 Length,
190 IN UINT64 Attributes
191 )
192{
193 DEBUG ((
194 DEBUG_INFO,
195 "%a: BaseAddress == 0x%lx, Length == 0x%lx, Attributes == 0x%lx\n",
196 __func__,
197 (UINTN)BaseAddress,
198 (UINTN)Length,
199 (UINTN)Attributes
200 ));
201
202 if ((Length == 0) ||
203 ((Attributes & ~(EFI_MEMORY_RO | EFI_MEMORY_RP | EFI_MEMORY_XP)) != 0))
204 {
205 DEBUG ((
206 DEBUG_ERROR,
207 "%a: BaseAddress 0x%llx Length is zero or Attributes (0x%llx) is invalid\n",
208 __func__,
209 BaseAddress,
210 Attributes
211 ));
212 return EFI_INVALID_PARAMETER;
213 }
214
215 if (!RegionIsSystemMemory (BaseAddress, Length)) {
216 return EFI_UNSUPPORTED;
217 }
218
219 return ArmSetMemoryAttributes (BaseAddress, Length, Attributes, Attributes);
220}
221
252STATIC
256 IN EFI_PHYSICAL_ADDRESS BaseAddress,
257 IN UINT64 Length,
258 IN UINT64 Attributes
259 )
260{
261 DEBUG ((
262 DEBUG_INFO,
263 "%a: BaseAddress == 0x%lx, Length == 0x%lx, Attributes == 0x%lx\n",
264 __func__,
265 (UINTN)BaseAddress,
266 (UINTN)Length,
267 (UINTN)Attributes
268 ));
269
270 if ((Length == 0) ||
271 ((Attributes & ~(EFI_MEMORY_RO | EFI_MEMORY_RP | EFI_MEMORY_XP)) != 0))
272 {
273 DEBUG ((
274 DEBUG_ERROR,
275 "%a: BaseAddress 0x%llx Length is zero or Attributes (0x%llx) is invalid\n",
276 __func__,
277 BaseAddress,
278 Attributes
279 ));
280 return EFI_INVALID_PARAMETER;
281 }
282
283 if (!RegionIsSystemMemory (BaseAddress, Length)) {
284 return EFI_UNSUPPORTED;
285 }
286
287 return ArmSetMemoryAttributes (BaseAddress, Length, 0, Attributes);
288}
289
290EFI_MEMORY_ATTRIBUTE_PROTOCOL mMemoryAttribute = {
294};
UINT64 UINTN
EFI_STATUS ArmSetMemoryAttributes(IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes, IN UINT64 AttributeMask)
EFI_DXE_SERVICES * gDS
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG(Expression)
Definition: DebugLib.h:434
STATIC EFI_STATUS ClearMemoryAttributes(IN EFI_MEMORY_ATTRIBUTE_PROTOCOL *This, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes)
STATIC EFI_STATUS SetMemoryAttributes(IN EFI_MEMORY_ATTRIBUTE_PROTOCOL *This, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN UINT64 Attributes)
STATIC EFI_STATUS GetMemoryAttributes(IN EFI_MEMORY_ATTRIBUTE_PROTOCOL *This, IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, OUT UINT64 *Attributes)
STATIC BOOLEAN RegionIsSystemMemory(IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length)
@ EfiGcdMemoryTypeSystemMemory
Definition: PiDxeCis.h:38
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_GCD_MEMORY_TYPE GcdMemoryType
Definition: PiDxeCis.h:152
EFI_PHYSICAL_ADDRESS BaseAddress
Definition: PiDxeCis.h:130