]>
Commit | Line | Data |
---|---|---|
e41aad15 JF |
1 | /** @file\r |
2 | CPU exception handler library implemenation for DXE modules.\r | |
3 | \r | |
2c5873fe | 4 | Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>\r |
e41aad15 JF |
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 <PiDxe.h>\r | |
16 | #include "CpuExceptionCommon.h"\r | |
17 | #include <Library/DebugLib.h>\r | |
18 | #include <Library/MemoryAllocationLib.h>\r | |
19 | \r | |
20 | CONST UINTN mDoFarReturnFlag = 0;\r | |
21 | \r | |
d91225cf JF |
22 | //\r |
23 | // Image align size for DXE/SMM\r | |
24 | //\r | |
25 | CONST UINTN mImageAlignSize = SIZE_4KB;\r | |
26 | \r | |
27 | RESERVED_VECTORS_DATA mReservedVectorsData[CPU_EXCEPTION_NUM];\r | |
28 | EFI_CPU_INTERRUPT_HANDLER mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];\r | |
29 | UINTN mEnabledInterruptNum = 0;\r | |
30 | \r | |
2c5873fe | 31 | EXCEPTION_HANDLER_DATA mExceptionHandlerData;\r |
e41aad15 | 32 | \r |
44ecbc28 JF |
33 | /**\r |
34 | Common exception handler.\r | |
35 | \r | |
36 | @param ExceptionType Exception type.\r | |
37 | @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r | |
38 | **/\r | |
39 | VOID\r | |
40 | EFIAPI\r | |
41 | CommonExceptionHandler (\r | |
42 | IN EFI_EXCEPTION_TYPE ExceptionType, \r | |
43 | IN EFI_SYSTEM_CONTEXT SystemContext\r | |
44 | )\r | |
45 | {\r | |
46 | CommonExceptionHandlerWorker (ExceptionType, SystemContext, &mExceptionHandlerData);\r | |
47 | }\r | |
48 | \r | |
e41aad15 JF |
49 | /**\r |
50 | Initializes all CPU exceptions entries and provides the default exception handlers.\r | |
51 | \r | |
52 | Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r | |
53 | persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r | |
54 | If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL. \r | |
55 | If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r | |
56 | \r | |
57 | @param[in] VectorInfo Pointer to reserved vector list.\r | |
58 | \r | |
59 | @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized \r | |
60 | with default exception handlers.\r | |
61 | @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r | |
62 | @retval EFI_UNSUPPORTED This function is not supported.\r | |
63 | \r | |
64 | **/\r | |
65 | EFI_STATUS\r | |
66 | EFIAPI\r | |
67 | InitializeCpuExceptionHandlers (\r | |
68 | IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r | |
69 | )\r | |
70 | {\r | |
ab95e54d JF |
71 | mExceptionHandlerData.ReservedVectors = mReservedVectorsData;\r |
72 | mExceptionHandlerData.ExternalInterruptHandler = mExternalInterruptHandlerTable;\r | |
73 | InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);\r | |
74 | return InitializeCpuExceptionHandlersWorker (VectorInfo, &mExceptionHandlerData);\r | |
e41aad15 JF |
75 | }\r |
76 | \r | |
77 | /**\r | |
78 | Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.\r | |
79 | \r | |
80 | Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r | |
81 | persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r | |
82 | If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL. \r | |
83 | If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r | |
84 | \r | |
85 | @param[in] VectorInfo Pointer to reserved vector list.\r | |
86 | \r | |
87 | @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized \r | |
88 | with default interrupt/exception handlers.\r | |
89 | @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r | |
90 | @retval EFI_UNSUPPORTED This function is not supported.\r | |
91 | \r | |
92 | **/\r | |
93 | EFI_STATUS\r | |
94 | EFIAPI\r | |
95 | InitializeCpuInterruptHandlers (\r | |
96 | IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r | |
97 | )\r | |
98 | {\r | |
99 | EFI_STATUS Status;\r | |
100 | IA32_IDT_GATE_DESCRIPTOR *IdtTable;\r | |
101 | IA32_DESCRIPTOR IdtDescriptor;\r | |
102 | UINTN IdtEntryCount;\r | |
103 | EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;\r | |
104 | UINTN Index;\r | |
105 | UINTN InterruptEntry;\r | |
106 | UINT8 *InterruptEntryCode;\r | |
9db15f81 JF |
107 | RESERVED_VECTORS_DATA *ReservedVectors;\r |
108 | EFI_CPU_INTERRUPT_HANDLER *ExternalInterruptHandler;\r | |
e41aad15 | 109 | \r |
9db15f81 JF |
110 | ReservedVectors = AllocatePool (sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM);\r |
111 | ASSERT (ReservedVectors != NULL);\r | |
112 | SetMem ((VOID *) ReservedVectors, sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM, 0xff);\r | |
e41aad15 | 113 | if (VectorInfo != NULL) {\r |
9db15f81 | 114 | Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors, CPU_INTERRUPT_NUM);\r |
e41aad15 | 115 | if (EFI_ERROR (Status)) {\r |
9db15f81 | 116 | FreePool (ReservedVectors);\r |
e41aad15 JF |
117 | return EFI_INVALID_PARAMETER;\r |
118 | }\r | |
119 | }\r | |
44ecbc28 | 120 | \r |
9db15f81 JF |
121 | ExternalInterruptHandler = AllocateZeroPool (sizeof (EFI_CPU_INTERRUPT_HANDLER) * CPU_INTERRUPT_NUM);\r |
122 | ASSERT (ExternalInterruptHandler != NULL);\r | |
e41aad15 JF |
123 | \r |
124 | //\r | |
125 | // Read IDT descriptor and calculate IDT size\r | |
126 | //\r | |
127 | AsmReadIdtr (&IdtDescriptor);\r | |
128 | IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR);\r | |
129 | if (IdtEntryCount > CPU_INTERRUPT_NUM) {\r | |
130 | IdtEntryCount = CPU_INTERRUPT_NUM;\r | |
131 | }\r | |
132 | //\r | |
133 | // Create Interrupt Descriptor Table and Copy the old IDT table in\r | |
134 | //\r | |
135 | IdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM);\r | |
136 | ASSERT (IdtTable != NULL);\r | |
137 | CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof (IA32_IDT_GATE_DESCRIPTOR) * IdtEntryCount);\r | |
138 | \r | |
139 | AsmGetTemplateAddressMap (&TemplateMap);\r | |
140 | ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);\r | |
141 | InterruptEntryCode = AllocatePool (TemplateMap.ExceptionStubHeaderSize * CPU_INTERRUPT_NUM);\r | |
142 | ASSERT (InterruptEntryCode != NULL);\r | |
143 | \r | |
144 | InterruptEntry = (UINTN) InterruptEntryCode;\r | |
145 | for (Index = 0; Index < CPU_INTERRUPT_NUM; Index ++) {\r | |
146 | CopyMem (\r | |
147 | (VOID *) InterruptEntry,\r | |
148 | (VOID *) TemplateMap.ExceptionStart,\r | |
149 | TemplateMap.ExceptionStubHeaderSize\r | |
150 | );\r | |
07da1ac8 | 151 | AsmVectorNumFixup ((VOID *) InterruptEntry, (UINT8) Index, (VOID *) TemplateMap.ExceptionStart);\r |
e41aad15 JF |
152 | InterruptEntry += TemplateMap.ExceptionStubHeaderSize;\r |
153 | }\r | |
154 | \r | |
155 | TemplateMap.ExceptionStart = (UINTN) InterruptEntryCode;\r | |
9db15f81 JF |
156 | mExceptionHandlerData.IdtEntryCount = CPU_INTERRUPT_NUM;\r |
157 | mExceptionHandlerData.ReservedVectors = ReservedVectors;\r | |
158 | mExceptionHandlerData.ExternalInterruptHandler = ExternalInterruptHandler;\r | |
159 | InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);\r | |
160 | \r | |
161 | UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);\r | |
e41aad15 JF |
162 | \r |
163 | //\r | |
164 | // Load Interrupt Descriptor Table\r | |
165 | //\r | |
166 | IdtDescriptor.Base = (UINTN) IdtTable;\r | |
167 | IdtDescriptor.Limit = (UINT16) (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM - 1);\r | |
168 | AsmWriteIdtr ((IA32_DESCRIPTOR *) &IdtDescriptor);\r | |
169 | \r | |
170 | return EFI_SUCCESS;\r | |
171 | }\r | |
172 | \r | |
173 | /**\r | |
174 | Registers a function to be called from the processor interrupt handler.\r | |
175 | \r | |
176 | This function registers and enables the handler specified by InterruptHandler for a processor \r | |
177 | interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the \r | |
178 | handler for the processor interrupt or exception type specified by InterruptType is uninstalled. \r | |
179 | The installed handler is called once for each processor interrupt or exception.\r | |
180 | NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or\r | |
181 | InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.\r | |
182 | \r | |
183 | @param[in] InterruptType Defines which interrupt or exception to hook.\r | |
184 | @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r | |
185 | when a processor interrupt occurs. If this parameter is NULL, then the handler\r | |
186 | will be uninstalled.\r | |
187 | \r | |
188 | @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r | |
189 | @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r | |
190 | previously installed.\r | |
191 | @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r | |
192 | previously installed.\r | |
193 | @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,\r | |
194 | or this function is not supported.\r | |
3f25e6ea | 195 | **/\r |
e41aad15 JF |
196 | EFI_STATUS\r |
197 | EFIAPI\r | |
198 | RegisterCpuInterruptHandler (\r | |
199 | IN EFI_EXCEPTION_TYPE InterruptType,\r | |
200 | IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r | |
201 | )\r | |
202 | {\r | |
670f13af | 203 | return RegisterCpuInterruptHandlerWorker (InterruptType, InterruptHandler, &mExceptionHandlerData);\r |
e41aad15 | 204 | }\r |