Home | History | Annotate | Download | only in CpuExceptionHandlerLib
      1 /** @file
      2   Common header file for CPU Exception Handler Library.
      3 
      4   Copyright (c) 2012 - 2016, 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 #define CPU_EXCEPTION_HANDLER_LIB_HOB_GUID \
     35   { \
     36     0xb21d9148, 0x9211, 0x4d8f, { 0xad, 0xd3, 0x66, 0xb1, 0x89, 0xc9, 0x2c, 0x83 } \
     37   }
     38 
     39 //
     40 // Record exception handler information
     41 //
     42 typedef struct {
     43   UINTN ExceptionStart;
     44   UINTN ExceptionStubHeaderSize;
     45   UINTN HookAfterStubHeaderStart;
     46 } EXCEPTION_HANDLER_TEMPLATE_MAP;
     47 
     48 typedef struct {
     49   UINTN                       IdtEntryCount;
     50   SPIN_LOCK                   DisplayMessageSpinLock;
     51   RESERVED_VECTORS_DATA       *ReservedVectors;
     52   EFI_CPU_INTERRUPT_HANDLER   *ExternalInterruptHandler;
     53 } EXCEPTION_HANDLER_DATA;
     54 
     55 extern CONST UINT32                mErrorCodeFlag;
     56 extern CONST UINTN                 mImageAlignSize;
     57 extern CONST UINTN                 mDoFarReturnFlag;
     58 
     59 /**
     60   Return address map of exception handler template so that C code can generate
     61   exception tables.
     62 
     63   @param AddressMap  Pointer to a buffer where the address map is returned.
     64 **/
     65 VOID
     66 EFIAPI
     67 AsmGetTemplateAddressMap (
     68   OUT EXCEPTION_HANDLER_TEMPLATE_MAP *AddressMap
     69   );
     70 
     71 /**
     72   Return address map of exception handler template so that C code can generate
     73   exception tables.
     74 
     75   @param IdtEntry          Pointer to IDT entry to be updated.
     76   @param InterruptHandler  IDT handler value.
     77 
     78 **/
     79 VOID
     80 ArchUpdateIdtEntry (
     81   IN IA32_IDT_GATE_DESCRIPTOR        *IdtEntry,
     82   IN UINTN                           InterruptHandler
     83   );
     84 
     85 /**
     86   Read IDT handler value from IDT entry.
     87 
     88   @param IdtEntry          Pointer to IDT entry to be read.
     89 
     90 **/
     91 UINTN
     92 ArchGetIdtHandler (
     93   IN IA32_IDT_GATE_DESCRIPTOR        *IdtEntry
     94   );
     95 
     96 /**
     97   Prints a message to the serial port.
     98 
     99   @param  Format      Format string for the message to print.
    100   @param  ...         Variable argument list whose contents are accessed
    101                       based on the format string specified by Format.
    102 
    103 **/
    104 VOID
    105 EFIAPI
    106 InternalPrintMessage (
    107   IN  CONST CHAR8  *Format,
    108   ...
    109   );
    110 
    111 /**
    112   Find and display image base address and return image base and its entry point.
    113 
    114   @param CurrentEip      Current instruction pointer.
    115   @param EntryPoint      Return module entry point if module header is found.
    116 
    117   @return !0     Image base address.
    118   @return 0      Image header cannot be found.
    119 **/
    120 UINTN
    121 FindModuleImageBase (
    122   IN  UINTN              CurrentEip,
    123   OUT UINTN              *EntryPoint
    124   );
    125 
    126 /**
    127   Display CPU information.
    128 
    129   @param ExceptionType  Exception type.
    130   @param SystemContext  Pointer to EFI_SYSTEM_CONTEXT.
    131 **/
    132 VOID
    133 DumpCpuContent (
    134   IN EFI_EXCEPTION_TYPE   ExceptionType,
    135   IN EFI_SYSTEM_CONTEXT   SystemContext
    136   );
    137 
    138 /**
    139   Internal worker function to initialize exception handler.
    140 
    141   @param[in]      VectorInfo            Pointer to reserved vector list.
    142   @param[in, out] ExceptionHandlerData  Pointer to exception handler data.
    143 
    144   @retval EFI_SUCCESS           CPU Exception Entries have been successfully initialized
    145                                 with default exception handlers.
    146   @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
    147   @retval EFI_UNSUPPORTED       This function is not supported.
    148 
    149 **/
    150 EFI_STATUS
    151 InitializeCpuExceptionHandlersWorker (
    152   IN EFI_VECTOR_HANDOFF_INFO       *VectorInfo OPTIONAL,
    153   IN OUT EXCEPTION_HANDLER_DATA    *ExceptionHandlerData
    154   );
    155 
    156 /**
    157   Registers a function to be called from the processor interrupt handler.
    158 
    159   @param[in]  InterruptType        Defines which interrupt or exception to hook.
    160   @param[in]  InterruptHandler     A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
    161                                    when a processor interrupt occurs. If this parameter is NULL, then the handler
    162                                    will be uninstalled
    163   @param[in] ExceptionHandlerData  Pointer to exception handler data.
    164 
    165   @retval EFI_SUCCESS           The handler for the processor interrupt was successfully installed or uninstalled.
    166   @retval EFI_ALREADY_STARTED   InterruptHandler is not NULL, and a handler for InterruptType was
    167                                 previously installed.
    168   @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
    169                                 previously installed.
    170   @retval EFI_UNSUPPORTED       The interrupt specified by InterruptType is not supported,
    171                                 or this function is not supported.
    172 **/
    173 EFI_STATUS
    174 RegisterCpuInterruptHandlerWorker (
    175   IN EFI_EXCEPTION_TYPE            InterruptType,
    176   IN EFI_CPU_INTERRUPT_HANDLER     InterruptHandler,
    177   IN EXCEPTION_HANDLER_DATA        *ExceptionHandlerData
    178   );
    179 
    180 /**
    181   Internal worker function to update IDT entries accordling to vector attributes.
    182 
    183   @param[in] IdtTable              Pointer to IDT table.
    184   @param[in] TemplateMap           Pointer to a buffer where the address map is
    185                                    returned.
    186   @param[in] ExceptionHandlerData  Pointer to exception handler data.
    187 
    188 **/
    189 VOID
    190 UpdateIdtTable (
    191   IN IA32_IDT_GATE_DESCRIPTOR        *IdtTable,
    192   IN EXCEPTION_HANDLER_TEMPLATE_MAP  *TemplateMap,
    193   IN EXCEPTION_HANDLER_DATA          *ExceptionHandlerData
    194   );
    195 
    196 /**
    197   Save CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
    198 
    199   @param[in] ExceptionType        Exception type.
    200   @param[in] SystemContext        Pointer to EFI_SYSTEM_CONTEXT.
    201   @param[in] ExceptionHandlerData Pointer to exception handler data.
    202 **/
    203 VOID
    204 ArchSaveExceptionContext (
    205   IN UINTN                        ExceptionType,
    206   IN EFI_SYSTEM_CONTEXT           SystemContext,
    207   IN EXCEPTION_HANDLER_DATA       *ExceptionHandlerData
    208   );
    209 
    210 /**
    211   Restore CPU exception context when handling EFI_VECTOR_HANDOFF_HOOK_AFTER case.
    212 
    213   @param[in] ExceptionType        Exception type.
    214   @param[in] SystemContext        Pointer to EFI_SYSTEM_CONTEXT.
    215   @param[in] ExceptionHandlerData Pointer to exception handler data.
    216 **/
    217 VOID
    218 ArchRestoreExceptionContext (
    219   IN UINTN                        ExceptionType,
    220   IN EFI_SYSTEM_CONTEXT           SystemContext,
    221   IN EXCEPTION_HANDLER_DATA       *ExceptionHandlerData
    222   );
    223 
    224 /**
    225   Fix up the vector number and function address in the vector code.
    226 
    227   @param[in] NewVectorAddr   New vector handler address.
    228   @param[in] VectorNum       Index of vector.
    229   @param[in] OldVectorAddr   Old vector handler address.
    230 
    231 **/
    232 VOID
    233 EFIAPI
    234 AsmVectorNumFixup (
    235   IN VOID    *NewVectorAddr,
    236   IN UINT8   VectorNum,
    237   IN VOID    *OldVectorAddr
    238   );
    239 
    240 /**
    241   Read and save reserved vector information
    242 
    243   @param[in]  VectorInfo        Pointer to reserved vector list.
    244   @param[out] ReservedVector    Pointer to reserved vector data buffer.
    245   @param[in]  VectorCount       Vector number to be updated.
    246 
    247   @return EFI_SUCCESS           Read and save vector info successfully.
    248   @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
    249 
    250 **/
    251 EFI_STATUS
    252 ReadAndVerifyVectorInfo (
    253   IN  EFI_VECTOR_HANDOFF_INFO       *VectorInfo,
    254   OUT RESERVED_VECTORS_DATA         *ReservedVector,
    255   IN  UINTN                         VectorCount
    256   );
    257 
    258 /**
    259   Get ASCII format string exception name by exception type.
    260 
    261   @param ExceptionType  Exception type.
    262 
    263   @return  ASCII format string exception name.
    264 **/
    265 CONST CHAR8 *
    266 GetExceptionNameStr (
    267   IN EFI_EXCEPTION_TYPE          ExceptionType
    268   );
    269 
    270 /**
    271   Internal worker function for common exception handler.
    272 
    273   @param ExceptionType         Exception type.
    274   @param SystemContext         Pointer to EFI_SYSTEM_CONTEXT.
    275   @param ExceptionHandlerData  Pointer to exception handler data.
    276 **/
    277 VOID
    278 CommonExceptionHandlerWorker (
    279   IN EFI_EXCEPTION_TYPE          ExceptionType,
    280   IN EFI_SYSTEM_CONTEXT          SystemContext,
    281   IN EXCEPTION_HANDLER_DATA      *ExceptionHandlerData
    282   );
    283 
    284 #endif
    285 
    286