1 /** @file
2   Common header file for CPU Exception Handler Library.
3 
4   Copyright (c) 2012 - 2015, Intel Corporation. All rights reserved.<BR>
5   This program and the accompanying materials
6   are licensed and made available under the terms and conditions of the BSD License
7   which accompanies this distribution.  The full text of the license may be found at
8   http://opensource.org/licenses/bsd-license.php
9 
10   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13 **/
14 
15 #ifndef _CPU_EXCEPTION_COMMON_H_
16 #define _CPU_EXCEPTION_COMMON_H_
17 
18 #include <Ppi/VectorHandoffInfo.h>
19 #include <Protocol/Cpu.h>
20 #include <Library/BaseLib.h>
21 #include <Library/SerialPortLib.h>
22 #include <Library/PrintLib.h>
23 #include <Library/LocalApicLib.h>
24 #include <Library/PeCoffGetEntryPointLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/SynchronizationLib.h>
27 
28 #define  CPU_EXCEPTION_NUM          32
29 #define  CPU_INTERRUPT_NUM         256
30 #define  HOOKAFTER_STUB_SIZE        16
31 
32 #include "ArchInterruptDefs.h"
33 
34 //
35 // Record exception handler information
36 //
37 typedef struct {
38   UINTN ExceptionStart;
39   UINTN ExceptionStubHeaderSize;
40   UINTN HookAfterStubHeaderStart;
41 } EXCEPTION_HANDLER_TEMPLATE_MAP;
42 
43 extern CONST UINT32                mErrorCodeFlag;
44 extern CONST UINTN                 mImageAlignSize;
45 extern CONST UINTN                 mDoFarReturnFlag;
46 extern RESERVED_VECTORS_DATA       *mReservedVectors;
47 
48 /**
49   Return address map of exception handler template so that C code can generate
50   exception tables.
51 
52   @param AddressMap  Pointer to a buffer where the address map is returned.
53 **/
54 VOID
55 EFIAPI
56 AsmGetTemplateAddressMap (
57   OUT EXCEPTION_HANDLER_TEMPLATE_MAP *AddressMap
58   );
59 
60 /**
61   Return address map of exception handler template so that C code can generate
62   exception tables.
63 
64   @param IdtEntry          Pointer to IDT entry to be updated.
65   @param InterruptHandler  IDT handler value.
66 
67 **/
68 VOID
69 ArchUpdateIdtEntry (
70   IN IA32_IDT_GATE_DESCRIPTOR        *IdtEntry,
71   IN UINTN                           InterruptHandler
72   );
73 
74 /**
75   Read IDT handler value from IDT entry.
76 
77   @param IdtEntry          Pointer to IDT entry to be read.
78 
79 **/
80 UINTN
81 ArchGetIdtHandler (
82   IN IA32_IDT_GATE_DESCRIPTOR        *IdtEntry
83   );
84 
85 /**
86   Prints a message to the serial port.
87 
88   @param  Format      Format string for the message to print.
89   @param  ...         Variable argument list whose contents are accessed
90                       based on the format string specified by Format.
91 
92 **/
93 VOID
94 EFIAPI
95 InternalPrintMessage (
96   IN  CONST CHAR8  *Format,
97   ...
98   );
99 
100 /**
101   Find and display image base address and return image base and its entry point.
102 
103   @param CurrentEip      Current instruction pointer.
104   @param EntryPoint      Return module entry point if module header is found.
105 
106   @return !0     Image base address.
107   @return 0      Image header cannot be found.
108 **/
109 UINTN
110 FindModuleImageBase (
111   IN  UINTN              CurrentEip,
112   OUT UINTN              *EntryPoint
113   );
114 
115 /**
116   Display CPU information.
117 
118   @param ExceptionType  Exception type.
119   @param SystemContext  Pointer to EFI_SYSTEM_CONTEXT.
120 **/
121 VOID
122 DumpCpuContent (
123   IN EFI_EXCEPTION_TYPE   ExceptionType,
124   IN EFI_SYSTEM_CONTEXT   SystemContext
125   );
126 
127 /**
128   Internal worker function to initialize exception handler.
129 
130   @param[in]  VectorInfo    Pointer to reserved vector list.
131 
132   @retval EFI_SUCCESS           CPU Exception Entries have been successfully initialized
133                                 with default exception handlers.
134   @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
135   @retval EFI_UNSUPPORTED       This function is not supported.
136 
137 **/
138 EFI_STATUS
139 InitializeCpuExceptionHandlersWorker (
140   IN EFI_VECTOR_HANDOFF_INFO       *VectorInfo OPTIONAL
141   );
142 
143 /**
144   Registers a function to be called from the processor interrupt handler.
145 
146   @param[in]  InterruptType     Defines which interrupt or exception to hook.
147   @param[in]  InterruptHandler  A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
148                                 when a processor interrupt occurs. If this parameter is NULL, then the handler
149                                 will be uninstalled.
150 
151   @retval EFI_SUCCESS           The handler for the processor interrupt was successfully installed or uninstalled.
152   @retval EFI_ALREADY_STARTED   InterruptHandler is not NULL, and a handler for InterruptType was
153                                 previously installed.
154   @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
155                                 previously installed.
156   @retval EFI_UNSUPPORTED       The interrupt specified by InterruptType is not supported,
157                                 or this function is not supported.
158 **/
159 EFI_STATUS
160 RegisterCpuInterruptHandlerWorker (
161   IN EFI_EXCEPTION_TYPE            InterruptType,
162   IN EFI_CPU_INTERRUPT_HANDLER     InterruptHandler
163   );
164 
165 /**
166   Internal worker function to update IDT entries accordling to vector attributes.
167 
168   @param[in] IdtTable       Pointer to IDT table.
169   @param[in] TemplateMap    Pointer to a buffer where the address map is returned.
170   @param[in] IdtEntryCount  IDT entries number to be updated.
171 
172 **/
173 VOID
174 UpdateIdtTable (
175   IN IA32_IDT_GATE_DESCRIPTOR        *IdtTable,
176   IN EXCEPTION_HANDLER_TEMPLATE_MAP  *TemplateMap,
177   IN UINTN                           IdtEntryCount
178   );
179 
180 /**
181   Save CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
182 
183   @param[in] ExceptionType  Exception type.
184   @param[in] SystemContext  Pointer to EFI_SYSTEM_CONTEXT.
185 
186 **/
187 VOID
188 ArchSaveExceptionContext (
189   IN UINTN                ExceptionType,
190   IN EFI_SYSTEM_CONTEXT   SystemContext
191   );
192 
193 /**
194   Restore CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
195 
196   @param[in] ExceptionType  Exception type.
197   @param[in] SystemContext  Pointer to EFI_SYSTEM_CONTEXT.
198 
199 **/
200 VOID
201 ArchRestoreExceptionContext (
202   IN UINTN                ExceptionType,
203   IN EFI_SYSTEM_CONTEXT   SystemContext
204   );
205 
206 /**
207   Fix up the vector number and function address in the vector code.
208 
209   @param[in] NewVectorAddr   New vector handler address.
210   @param[in] VectorNum       Index of vector.
211   @param[in] OldVectorAddr   Old vector handler address.
212 
213 **/
214 VOID
215 EFIAPI
216 AsmVectorNumFixup (
217   IN VOID    *NewVectorAddr,
218   IN UINT8   VectorNum,
219   IN VOID    *OldVectorAddr
220   );
221 
222 /**
223   Read and save reserved vector information
224 
225   @param[in]  VectorInfo        Pointer to reserved vector list.
226   @param[out] ReservedVector    Pointer to reserved vector data buffer.
227   @param[in]  VectorCount       Vector number to be updated.
228 
229   @return EFI_SUCCESS           Read and save vector info successfully.
230   @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
231 
232 **/
233 EFI_STATUS
234 ReadAndVerifyVectorInfo (
235   IN  EFI_VECTOR_HANDOFF_INFO       *VectorInfo,
236   OUT RESERVED_VECTORS_DATA         *ReservedVector,
237   IN  UINTN                         VectorCount
238   );
239 
240 /**
241   Get ASCII format string exception name by exception type.
242 
243   @param ExceptionType  Exception type.
244 
245   @return  ASCII format string exception name.
246 **/
247 CONST CHAR8 *
248 GetExceptionNameStr (
249   IN EFI_EXCEPTION_TYPE          ExceptionType
250   );
251 
252 #endif
253 
254