]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - ArmPkg/Drivers/CpuDxe/Exception.c
ArmPkg: Introduce ArmSetLowVectors/ArmSetHighVectors functions
[mirror_edk2.git] / ArmPkg / Drivers / CpuDxe / Exception.c
... / ...
CommitLineData
1/** @file\r
2\r
3 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\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//FIXME: Will not compile on non-ARMv7 builds\r
18#include <Chipset/ArmV7.h>\r
19\r
20VOID\r
21ExceptionHandlersStart (\r
22 VOID\r
23 );\r
24\r
25VOID\r
26ExceptionHandlersEnd (\r
27 VOID\r
28 );\r
29\r
30VOID\r
31CommonExceptionEntry (\r
32 VOID\r
33 );\r
34\r
35VOID\r
36AsmCommonExceptionEntry (\r
37 VOID\r
38 );\r
39\r
40\r
41EFI_EXCEPTION_CALLBACK gExceptionHandlers[MAX_ARM_EXCEPTION + 1];\r
42EFI_EXCEPTION_CALLBACK gDebuggerExceptionHandlers[MAX_ARM_EXCEPTION + 1];\r
43\r
44\r
45\r
46/**\r
47 This function registers and enables the handler specified by InterruptHandler for a processor \r
48 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the \r
49 handler for the processor interrupt or exception type specified by InterruptType is uninstalled. \r
50 The installed handler is called once for each processor interrupt or exception.\r
51\r
52 @param InterruptType A pointer to the processor's current interrupt state. Set to TRUE if interrupts\r
53 are enabled and FALSE if interrupts are disabled.\r
54 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r
55 when a processor interrupt occurs. If this parameter is NULL, then the handler\r
56 will be uninstalled.\r
57\r
58 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r
59 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r
60 previously installed.\r
61 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r
62 previously installed.\r
63 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.\r
64\r
65**/\r
66EFI_STATUS\r
67RegisterInterruptHandler (\r
68 IN EFI_EXCEPTION_TYPE InterruptType,\r
69 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
70 )\r
71{\r
72 if (InterruptType > MAX_ARM_EXCEPTION) {\r
73 return EFI_UNSUPPORTED;\r
74 }\r
75\r
76 if ((InterruptHandler != NULL) && (gExceptionHandlers[InterruptType] != NULL)) {\r
77 return EFI_ALREADY_STARTED;\r
78 }\r
79\r
80 gExceptionHandlers[InterruptType] = InterruptHandler;\r
81\r
82 return EFI_SUCCESS;\r
83}\r
84\r
85\r
86\r
87\r
88VOID\r
89EFIAPI\r
90CommonCExceptionHandler (\r
91 IN EFI_EXCEPTION_TYPE ExceptionType,\r
92 IN OUT EFI_SYSTEM_CONTEXT SystemContext\r
93 )\r
94{\r
95\r
96 if (ExceptionType <= MAX_ARM_EXCEPTION) {\r
97 if (gExceptionHandlers[ExceptionType]) {\r
98 gExceptionHandlers[ExceptionType] (ExceptionType, SystemContext);\r
99 return;\r
100 }\r
101 } else {\r
102 DEBUG ((EFI_D_ERROR, "Unknown exception type %d from %08x\n", ExceptionType, SystemContext.SystemContextArm->PC));\r
103 ASSERT (FALSE);\r
104 }\r
105 \r
106 if (ExceptionType == EXCEPT_ARM_SOFTWARE_INTERRUPT) {\r
107 //\r
108 // ARM JTAG debuggers some times use this vector, so it is not an error to get one\r
109 //\r
110 return;\r
111 }\r
112\r
113 DefaultExceptionHandler (ExceptionType, SystemContext);\r
114}\r
115\r
116\r
117\r
118EFI_STATUS\r
119InitializeExceptions (\r
120 IN EFI_CPU_ARCH_PROTOCOL *Cpu\r
121 )\r
122{\r
123 EFI_STATUS Status;\r
124 UINTN Offset;\r
125 UINTN Length;\r
126 UINTN Index;\r
127 BOOLEAN IrqEnabled;\r
128 BOOLEAN FiqEnabled;\r
129 EFI_PHYSICAL_ADDRESS Base;\r
130 UINT32 *VectorBase;\r
131\r
132 Status = EFI_SUCCESS;\r
133 //\r
134 // Disable interrupts\r
135 //\r
136 Cpu->GetInterruptState (Cpu, &IrqEnabled);\r
137 Cpu->DisableInterrupt (Cpu);\r
138\r
139 //\r
140 // EFI does not use the FIQ, but a debugger might so we must disable \r
141 // as we take over the exception vectors. \r
142 //\r
143 FiqEnabled = ArmGetFiqState ();\r
144 ArmDisableFiq ();\r
145\r
146 if (FeaturePcdGet(PcdRelocateVectorTable) == TRUE) {\r
147 //\r
148 // Copy an implementation of the ARM exception vectors to PcdCpuVectorBaseAddress.\r
149 //\r
150 Length = (UINTN)ExceptionHandlersEnd - (UINTN)ExceptionHandlersStart;\r
151\r
152 // Check if the exception vector is in the low address\r
153 if (PcdGet32 (PcdCpuVectorBaseAddress) == 0x0) {\r
154 // Set SCTLR.V to 0 to enable VBAR to be used\r
155 ArmSetLowVectors ();\r
156 } else {\r
157 ArmSetHighVectors ();\r
158 }\r
159\r
160 //\r
161 // Reserve space for the exception handlers\r
162 //\r
163 Base = (EFI_PHYSICAL_ADDRESS)PcdGet32 (PcdCpuVectorBaseAddress);\r
164 VectorBase = (UINT32 *)(UINTN)Base;\r
165 Status = gBS->AllocatePages (AllocateAddress, EfiBootServicesCode, EFI_SIZE_TO_PAGES (Length), &Base);\r
166 // If the request was for memory that's not in the memory map (which is often the case for 0x00000000\r
167 // on embedded systems, for example, we don't want to hang up. So we'll check here for a status of\r
168 // EFI_NOT_FOUND, and continue in that case.\r
169 if (EFI_ERROR(Status) && (Status != EFI_NOT_FOUND)) {\r
170 ASSERT_EFI_ERROR (Status);\r
171 }\r
172\r
173 if (FeaturePcdGet(PcdDebuggerExceptionSupport) == TRUE) {\r
174 // Save existing vector table, in case debugger is already hooked in\r
175 CopyMem ((VOID *)gDebuggerExceptionHandlers, (VOID *)VectorBase, sizeof (gDebuggerExceptionHandlers));\r
176 }\r
177\r
178 // Copy our assembly code into the page that contains the exception vectors.\r
179 CopyMem ((VOID *)VectorBase, (VOID *)ExceptionHandlersStart, Length);\r
180\r
181 //\r
182 // Patch in the common Assembly exception handler\r
183 //\r
184 Offset = (UINTN)CommonExceptionEntry - (UINTN)ExceptionHandlersStart;\r
185 *(UINTN *) ((UINT8 *)(UINTN)PcdGet32 (PcdCpuVectorBaseAddress) + Offset) = (UINTN)AsmCommonExceptionEntry;\r
186\r
187 //\r
188 // Initialize the C entry points for interrupts\r
189 //\r
190 for (Index = 0; Index <= MAX_ARM_EXCEPTION; Index++) {\r
191 if (!FeaturePcdGet(PcdDebuggerExceptionSupport) ||\r
192 (gDebuggerExceptionHandlers[Index] == 0) || (gDebuggerExceptionHandlers[Index] == (VOID *)(UINTN)0xEAFFFFFE)) {\r
193 // Exception handler contains branch to vector location (jmp $) so no handler\r
194 // NOTE: This code assumes vectors are ARM and not Thumb code\r
195 Status = RegisterInterruptHandler (Index, NULL);\r
196 ASSERT_EFI_ERROR (Status);\r
197 } else {\r
198 // If the debugger has already hooked put its vector back\r
199 VectorBase[Index] = (UINT32)(UINTN)gDebuggerExceptionHandlers[Index];\r
200 }\r
201 }\r
202\r
203 // Flush Caches since we updated executable stuff\r
204 InvalidateInstructionCacheRange ((VOID *)PcdGet32(PcdCpuVectorBaseAddress), Length);\r
205\r
206 //Note: On ARM processor with the Security Extension, the Vector Table can be located anywhere in the memory.\r
207 // The Vector Base Address Register defines the location\r
208 ArmWriteVBar(PcdGet32(PcdCpuVectorBaseAddress));\r
209 } else {\r
210 // We do not copy the Exception Table at PcdGet32(PcdCpuVectorBaseAddress). We just set Vector Base Address to point into CpuDxe code.\r
211 ArmWriteVBar((UINT32)ExceptionHandlersStart);\r
212 }\r
213\r
214 if (FiqEnabled) {\r
215 ArmEnableFiq ();\r
216 }\r
217\r
218 if (IrqEnabled) {\r
219 // \r
220 // Restore interrupt state\r
221 //\r
222 Status = Cpu->EnableInterrupt (Cpu);\r
223 }\r
224\r
225 return Status;\r
226}\r