]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - ArmPkg/Drivers/CpuDxe/Exception.c
Sync up ArmPkg with patch from mailing list. Changed name of BdsLib.h to BdsUnixLib...
[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 //\r
153 // Reserve space for the exception handlers\r
154 //\r
155 Base = (EFI_PHYSICAL_ADDRESS)PcdGet32 (PcdCpuVectorBaseAddress);\r
156 VectorBase = (UINT32 *)(UINTN)Base;\r
157 Status = gBS->AllocatePages (AllocateAddress, EfiBootServicesCode, EFI_SIZE_TO_PAGES (Length), &Base);\r
158 // If the request was for memory that's not in the memory map (which is often the case for 0x00000000\r
159 // on embedded systems, for example, we don't want to hang up. So we'll check here for a status of \r
160 // EFI_NOT_FOUND, and continue in that case.\r
161 if (EFI_ERROR(Status) && (Status != EFI_NOT_FOUND)) {\r
162 ASSERT_EFI_ERROR (Status);\r
163 }\r
164 \r
165 // Save existing vector table, in case debugger is already hooked in\r
166 CopyMem ((VOID *)gDebuggerExceptionHandlers, (VOID *)VectorBase, sizeof (gDebuggerExceptionHandlers));\r
167 \r
168 // Copy our assembly code into the page that contains the exception vectors. \r
169 CopyMem ((VOID *)VectorBase, (VOID *)ExceptionHandlersStart, Length);\r
170 \r
171 //\r
172 // Patch in the common Assembly exception handler\r
173 //\r
174 Offset = (UINTN)CommonExceptionEntry - (UINTN)ExceptionHandlersStart;\r
175 *(UINTN *) ((UINT8 *)(UINTN)PcdGet32 (PcdCpuVectorBaseAddress) + Offset) = (UINTN)AsmCommonExceptionEntry;\r
176 \r
177 //\r
178 // Initialize the C entry points for interrupts\r
179 //\r
180 for (Index = 0; Index <= MAX_ARM_EXCEPTION; Index++) {\r
181 if ((gDebuggerExceptionHandlers[Index] == 0) || (gDebuggerExceptionHandlers[Index] == (VOID *)(UINTN)0xEAFFFFFE)) {\r
182 // Exception handler contains branch to vector location (jmp $) so no handler\r
183 // NOTE: This code assumes vectors are ARM and not Thumb code\r
184 Status = RegisterInterruptHandler (Index, NULL);\r
185 ASSERT_EFI_ERROR (Status);\r
186 } else {\r
187 // If the debugger has alread hooked put its vector back\r
188 VectorBase[Index] = (UINT32)(UINTN)gDebuggerExceptionHandlers[Index];\r
189 }\r
190 }\r
191 \r
192 // Flush Caches since we updated executable stuff\r
193 InvalidateInstructionCacheRange ((VOID *)PcdGet32(PcdCpuVectorBaseAddress), Length);\r
194\r
195 //Note: On ARM processor with the Security Extension, the Vector Table can be located anywhere in the memory.\r
196 // The Vector Base Address Register defines the location\r
197 ArmWriteVBar(PcdGet32(PcdCpuVectorBaseAddress));\r
198 } else {\r
199 // We do not copy the Exception Table at PcdGet32(PcdCpuVectorBaseAddress). We just set Vector Base Address to point into CpuDxe code.\r
200 ArmWriteVBar((UINT32)ExceptionHandlersStart);\r
201 }\r
202\r
203 if (FiqEnabled) {\r
204 ArmEnableFiq ();\r
205 }\r
206\r
207 if (IrqEnabled) {\r
208 // \r
209 // Restore interrupt state\r
210 //\r
211 Status = Cpu->EnableInterrupt (Cpu);\r
212 }\r
213\r
214 return Status;\r
215}\r