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