]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Drivers/CpuDxe/CpuMmuCommon.c
ArmPkg/CpuDxe: AArch64: Fix wrong comparison of exception type
[mirror_edk2.git] / ArmPkg / Drivers / CpuDxe / CpuMmuCommon.c
CommitLineData
591fb378
OM
1/** @file\r
2*\r
3* Copyright (c) 2013, ARM Limited. All rights reserved.\r
4*\r
5* This program and the accompanying materials\r
6* are licensed and made available under the terms and conditions of the BSD License\r
7* which accompanies this distribution. The full text of the license may be found at\r
8* http://opensource.org/licenses/bsd-license.php\r
9*\r
10* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12*\r
13**/\r
14\r
15#include "CpuDxe.h"\r
16\r
17/**\r
18 Searches memory descriptors covered by given memory range.\r
19\r
20 This function searches into the Gcd Memory Space for descriptors\r
21 (from StartIndex to EndIndex) that contains the memory range\r
22 specified by BaseAddress and Length.\r
23\r
24 @param MemorySpaceMap Gcd Memory Space Map as array.\r
25 @param NumberOfDescriptors Number of descriptors in map.\r
26 @param BaseAddress BaseAddress for the requested range.\r
27 @param Length Length for the requested range.\r
28 @param StartIndex Start index into the Gcd Memory Space Map.\r
29 @param EndIndex End index into the Gcd Memory Space Map.\r
30\r
31 @retval EFI_SUCCESS Search successfully.\r
32 @retval EFI_NOT_FOUND The requested descriptors does not exist.\r
33\r
34**/\r
35EFI_STATUS\r
36SearchGcdMemorySpaces (\r
37 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap,\r
38 IN UINTN NumberOfDescriptors,\r
39 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
40 IN UINT64 Length,\r
41 OUT UINTN *StartIndex,\r
42 OUT UINTN *EndIndex\r
43 )\r
44{\r
45 UINTN Index;\r
46\r
47 *StartIndex = 0;\r
48 *EndIndex = 0;\r
49 for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
50 if ((BaseAddress >= MemorySpaceMap[Index].BaseAddress) &&\r
51 (BaseAddress < (MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length))) {\r
52 *StartIndex = Index;\r
53 }\r
54 if (((BaseAddress + Length - 1) >= MemorySpaceMap[Index].BaseAddress) &&\r
55 ((BaseAddress + Length - 1) < (MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length))) {\r
56 *EndIndex = Index;\r
57 return EFI_SUCCESS;\r
58 }\r
59 }\r
60 return EFI_NOT_FOUND;\r
61}\r
62\r
63\r
64/**\r
65 Sets the attributes for a specified range in Gcd Memory Space Map.\r
66\r
67 This function sets the attributes for a specified range in\r
68 Gcd Memory Space Map.\r
69\r
70 @param MemorySpaceMap Gcd Memory Space Map as array\r
71 @param NumberOfDescriptors Number of descriptors in map\r
72 @param BaseAddress BaseAddress for the range\r
73 @param Length Length for the range\r
74 @param Attributes Attributes to set\r
75\r
76 @retval EFI_SUCCESS Memory attributes set successfully\r
77 @retval EFI_NOT_FOUND The specified range does not exist in Gcd Memory Space\r
78\r
79**/\r
80EFI_STATUS\r
81SetGcdMemorySpaceAttributes (\r
82 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap,\r
83 IN UINTN NumberOfDescriptors,\r
84 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
85 IN UINT64 Length,\r
86 IN UINT64 Attributes\r
87 )\r
88{\r
89 EFI_STATUS Status;\r
90 UINTN Index;\r
91 UINTN StartIndex;\r
92 UINTN EndIndex;\r
93 EFI_PHYSICAL_ADDRESS RegionStart;\r
94 UINT64 RegionLength;\r
95\r
96 DEBUG ((DEBUG_GCD, "SetGcdMemorySpaceAttributes[0x%lX; 0x%lX] = 0x%lX\n",\r
97 BaseAddress, BaseAddress + Length, Attributes));\r
98\r
99 //\r
100 // Get all memory descriptors covered by the memory range\r
101 //\r
102 Status = SearchGcdMemorySpaces (\r
103 MemorySpaceMap,\r
104 NumberOfDescriptors,\r
105 BaseAddress,\r
106 Length,\r
107 &StartIndex,\r
108 &EndIndex\r
109 );\r
110 if (EFI_ERROR (Status)) {\r
111 return Status;\r
112 }\r
113\r
114 //\r
115 // Go through all related descriptors and set attributes accordingly\r
116 //\r
117 for (Index = StartIndex; Index <= EndIndex; Index++) {\r
118 if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeNonExistent) {\r
119 continue;\r
120 }\r
121 //\r
122 // Calculate the start and end address of the overlapping range\r
123 //\r
124 if (BaseAddress >= MemorySpaceMap[Index].BaseAddress) {\r
125 RegionStart = BaseAddress;\r
126 } else {\r
127 RegionStart = MemorySpaceMap[Index].BaseAddress;\r
128 }\r
129 if ((BaseAddress + Length - 1) < (MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length)) {\r
130 RegionLength = BaseAddress + Length - RegionStart;\r
131 } else {\r
132 RegionLength = MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length - RegionStart;\r
133 }\r
134 //\r
135 // Set memory attributes according to MTRR attribute and the original attribute of descriptor\r
136 //\r
137 gDS->SetMemorySpaceAttributes (\r
138 RegionStart,\r
139 RegionLength,\r
140 (MemorySpaceMap[Index].Attributes & ~EFI_MEMORY_CACHETYPE_MASK) | (MemorySpaceMap[Index].Capabilities & Attributes)\r
141 );\r
142 }\r
143\r
144 return EFI_SUCCESS;\r
145}\r
146\r
147/**\r
148 This function modifies the attributes for the memory region specified by BaseAddress and\r
149 Length from their current attributes to the attributes specified by Attributes.\r
150\r
151 @param This The EFI_CPU_ARCH_PROTOCOL instance.\r
152 @param BaseAddress The physical address that is the start address of a memory region.\r
153 @param Length The size in bytes of the memory region.\r
154 @param Attributes The bit mask of attributes to set for the memory region.\r
155\r
156 @retval EFI_SUCCESS The attributes were set for the memory region.\r
157 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
158 BaseAddress and Length cannot be modified.\r
159 @retval EFI_INVALID_PARAMETER Length is zero.\r
160 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
161 the memory resource range.\r
162 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
163 resource range specified by BaseAddress and Length.\r
164 The bit mask of attributes is not support for the memory resource\r
165 range specified by BaseAddress and Length.\r
166\r
167**/\r
168EFI_STATUS\r
169EFIAPI\r
170CpuSetMemoryAttributes (\r
171 IN EFI_CPU_ARCH_PROTOCOL *This,\r
172 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
173 IN UINT64 Length,\r
174 IN UINT64 Attributes\r
175 )\r
176{\r
177 DEBUG ((EFI_D_PAGE, "CpuSetMemoryAttributes(%lx, %lx, %lx)\n", BaseAddress, Length, Attributes));\r
178\r
179 if ((BaseAddress & (SIZE_4KB - 1)) != 0) {\r
180 // Minimum granularity is SIZE_4KB (4KB on ARM)\r
181 DEBUG ((EFI_D_PAGE, "CpuSetMemoryAttributes(%lx, %lx, %lx): Minimum ganularity is SIZE_4KB\n", BaseAddress, Length, Attributes));\r
182 return EFI_UNSUPPORTED;\r
183 }\r
184\r
185 return SetMemoryAttributes (BaseAddress, Length, Attributes, 0);\r
186}\r
187\r
188EFI_STATUS\r
189EFIAPI\r
190CpuConvertPagesToUncachedVirtualAddress (\r
191 IN VIRTUAL_UNCACHED_PAGES_PROTOCOL *This,\r
192 IN EFI_PHYSICAL_ADDRESS Address,\r
193 IN UINTN Length,\r
194 IN EFI_PHYSICAL_ADDRESS VirtualMask,\r
195 OUT UINT64 *Attributes OPTIONAL\r
196 )\r
197{\r
198 EFI_STATUS Status;\r
199 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
200\r
201 if (Attributes != NULL) {\r
202 Status = gDS->GetMemorySpaceDescriptor (Address, &GcdDescriptor);\r
203 if (!EFI_ERROR (Status)) {\r
204 *Attributes = GcdDescriptor.Attributes;\r
205 }\r
206 }\r
207\r
208 //\r
209 // Make this address range page fault if accessed. If it is a DMA buffer than this would\r
210 // be the PCI address. Code should always use the CPU address, and we will or in VirtualMask\r
211 // to that address.\r
212 //\r
213 Status = SetMemoryAttributes (Address, Length, EFI_MEMORY_WP, 0);\r
214 if (!EFI_ERROR (Status)) {\r
215 Status = SetMemoryAttributes (Address | VirtualMask, Length, EFI_MEMORY_UC, VirtualMask);\r
216 }\r
217\r
218 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "CpuConvertPagesToUncachedVirtualAddress()\n Unmapped 0x%08lx Mapped 0x%08lx 0x%x bytes\n", Address, Address | VirtualMask, Length));\r
219\r
220 return Status;\r
221}\r
222\r
223\r
224EFI_STATUS\r
225EFIAPI\r
226CpuReconvertPages (\r
227 IN VIRTUAL_UNCACHED_PAGES_PROTOCOL *This,\r
228 IN EFI_PHYSICAL_ADDRESS Address,\r
229 IN UINTN Length,\r
230 IN EFI_PHYSICAL_ADDRESS VirtualMask,\r
231 IN UINT64 Attributes\r
232 )\r
233{\r
234 EFI_STATUS Status;\r
235\r
236 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "CpuReconvertPages(%lx, %x, %lx, %lx)\n", Address, Length, VirtualMask, Attributes));\r
237\r
238 //\r
239 // Unmap the aliased Address\r
240 //\r
241 Status = SetMemoryAttributes (Address | VirtualMask, Length, EFI_MEMORY_WP, 0);\r
242 if (!EFI_ERROR (Status)) {\r
243 //\r
244 // Restore atttributes\r
245 //\r
246 Status = SetMemoryAttributes (Address, Length, Attributes, 0);\r
247 }\r
248\r
249 return Status;\r
250}\r
251\r
252\r
253VIRTUAL_UNCACHED_PAGES_PROTOCOL gVirtualUncachedPages = {\r
254 CpuConvertPagesToUncachedVirtualAddress,\r
255 CpuReconvertPages\r
256};\r