blob: efe77eb09f76f829d5bb69e1ea1387ea2dfc48a9 [file] [log] [blame]
Vishal Bhoj82c80712015-12-15 21:13:33 +05301/** @file
2 Common header file for CPU Exception Handler Library.
3
4 Copyright (c) 2012 - 2013, 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//
37typedef struct {
38 UINTN ExceptionStart;
39 UINTN ExceptionStubHeaderSize;
40 UINTN HookAfterStubHeaderStart;
41} EXCEPTION_HANDLER_TEMPLATE_MAP;
42
43extern CONST UINT32 mErrorCodeFlag;
44extern CONST UINTN mImageAlignSize;
45extern CONST UINTN mDoFarReturnFlag;
46extern 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**/
54VOID
55EFIAPI
56AsmGetTemplateAddressMap (
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**/
68VOID
69ArchUpdateIdtEntry (
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**/
80UINTN
81ArchGetIdtHandler (
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**/
93VOID
94EFIAPI
95InternalPrintMessage (
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**/
109UINTN
110FindModuleImageBase (
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**/
121VOID
122DumpCpuContent (
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**/
138EFI_STATUS
139InitializeCpuExceptionHandlersWorker (
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**/
159EFI_STATUS
160RegisterCpuInterruptHandlerWorker (
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**/
173VOID
174UpdateIdtTable (
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**/
187VOID
188ArchSaveExceptionContext (
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**/
200VOID
201ArchRestoreExceptionContext (
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**/
214VOID
215EFIAPI
216AsmVectorNumFixup (
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**/
233EFI_STATUS
234ReadAndVerifyVectorInfo (
235 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo,
236 OUT RESERVED_VECTORS_DATA *ReservedVector,
237 IN UINTN VectorCount
238 );
239
240#endif
241