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