Home | History | Annotate | Download | only in RuntimeDxe
      1 /** @file
      2   Report Status Code Router Driver which produces Report Stataus Code Handler Protocol
      3   and Status Code Runtime Protocol.
      4 
      5   Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
      6   This program and the accompanying materials
      7   are licensed and made available under the terms and conditions of the BSD License
      8   which accompanies this distribution.  The full text of the license may be found at
      9   http://opensource.org/licenses/bsd-license.php
     10 
     11   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     12   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     13 
     14 **/
     15 
     16 #include "ReportStatusCodeRouterRuntimeDxe.h"
     17 
     18 EFI_HANDLE   mHandle                    = NULL;
     19 LIST_ENTRY   mCallbackListHead          = INITIALIZE_LIST_HEAD_VARIABLE (mCallbackListHead);
     20 EFI_EVENT    mVirtualAddressChangeEvent = NULL;
     21 
     22 //
     23 // Report operation nest status.
     24 // If it is set, then the report operation has nested.
     25 //
     26 UINT32       mStatusCodeNestStatus = 0;
     27 
     28 EFI_STATUS_CODE_PROTOCOL  mStatusCodeProtocol  = {
     29   ReportDispatcher
     30 };
     31 
     32 EFI_RSC_HANDLER_PROTOCOL  mRscHandlerProtocol = {
     33   Register,
     34   Unregister
     35   };
     36 
     37 /**
     38   Event callback function to invoke status code handler in list.
     39 
     40   @param  Event         Event whose notification function is being invoked.
     41   @param  Context       Pointer to the notification function's context, which is
     42                         always zero in current implementation.
     43 
     44 **/
     45 VOID
     46 EFIAPI
     47 RscHandlerNotification (
     48   IN EFI_EVENT        Event,
     49   IN VOID             *Context
     50   )
     51 {
     52   RSC_HANDLER_CALLBACK_ENTRY  *CallbackEntry;
     53   EFI_PHYSICAL_ADDRESS        Address;
     54   RSC_DATA_ENTRY              *RscData;
     55 
     56   CallbackEntry = (RSC_HANDLER_CALLBACK_ENTRY *) Context;
     57 
     58   //
     59   // Traverse the status code data buffer to parse all
     60   // data to report.
     61   //
     62   Address = CallbackEntry->StatusCodeDataBuffer;
     63   while (Address < CallbackEntry->EndPointer) {
     64     RscData = (RSC_DATA_ENTRY *) (UINTN) Address;
     65     CallbackEntry->RscHandlerCallback (
     66                      RscData->Type,
     67                      RscData->Value,
     68                      RscData->Instance,
     69                      &RscData->CallerId,
     70                      &RscData->Data
     71                      );
     72 
     73     Address += (sizeof (RSC_DATA_ENTRY) + RscData->Data.Size);
     74     Address  = ALIGN_VARIABLE (Address);
     75   }
     76 
     77   CallbackEntry->EndPointer = CallbackEntry->StatusCodeDataBuffer;
     78 }
     79 
     80 /**
     81   Register the callback function for ReportStatusCode() notification.
     82 
     83   When this function is called the function pointer is added to an internal list and any future calls to
     84   ReportStatusCode() will be forwarded to the Callback function. During the bootservices,
     85   this is the callback for which this service can be invoked. The report status code router
     86   will create an event such that the callback function is only invoked at the TPL for which it was
     87   registered. The entity that registers for the callback should also register for an event upon
     88   generation of exit boot services and invoke the unregister service.
     89   If the handler does not have a TPL dependency, it should register for a callback at TPL high. The
     90   router infrastructure will support making callbacks at runtime, but the caller for runtime invocation
     91   must meet the following criteria:
     92   1. must be a runtime driver type so that its memory is not reclaimed
     93   2. not unregister at exit boot services so that the router will still have its callback address
     94   3. the caller must be self-contained (eg. Not call out into any boot-service interfaces) and be
     95   runtime safe, in general.
     96 
     97   @param[in] Callback   A pointer to a function of type EFI_RSC_HANDLER_CALLBACK that is called when
     98                         a call to ReportStatusCode() occurs.
     99   @param[in] Tpl        TPL at which callback can be safely invoked.
    100 
    101   @retval  EFI_SUCCESS              Function was successfully registered.
    102   @retval  EFI_INVALID_PARAMETER    The callback function was NULL.
    103   @retval  EFI_OUT_OF_RESOURCES     The internal buffer ran out of space. No more functions can be
    104                                     registered.
    105   @retval  EFI_ALREADY_STARTED      The function was already registered. It can't be registered again.
    106 
    107 **/
    108 EFI_STATUS
    109 EFIAPI
    110 Register (
    111   IN EFI_RSC_HANDLER_CALLBACK   Callback,
    112   IN EFI_TPL                    Tpl
    113   )
    114 {
    115   EFI_STATUS                    Status;
    116   LIST_ENTRY                    *Link;
    117   RSC_HANDLER_CALLBACK_ENTRY    *CallbackEntry;
    118 
    119   if (Callback == NULL) {
    120     return EFI_INVALID_PARAMETER;
    121   }
    122 
    123   for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
    124     CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
    125     if (CallbackEntry->RscHandlerCallback == Callback) {
    126       //
    127       // If the function was already registered. It can't be registered again.
    128       //
    129       return EFI_ALREADY_STARTED;
    130     }
    131   }
    132 
    133   CallbackEntry = AllocateRuntimeZeroPool (sizeof (RSC_HANDLER_CALLBACK_ENTRY));
    134   ASSERT (CallbackEntry != NULL);
    135 
    136   CallbackEntry->Signature          = RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE;
    137   CallbackEntry->RscHandlerCallback = Callback;
    138   CallbackEntry->Tpl                = Tpl;
    139 
    140   //
    141   // If TPL of registered callback funtion is not TPL_HIGH_LEVEL, then event should be created
    142   // for it, and related buffer for status code data should be prepared.
    143   // Here the data buffer must be prepared in advance, because Report Status Code Protocol might
    144   // be invoked under TPL_HIGH_LEVEL and no memory allocation is allowed then.
    145   // If TPL is TPL_HIGH_LEVEL, then all status code will be reported immediately, without data
    146   // buffer and event trigger.
    147   //
    148   if (Tpl != TPL_HIGH_LEVEL) {
    149     CallbackEntry->StatusCodeDataBuffer = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocatePool (EFI_PAGE_SIZE);
    150     CallbackEntry->BufferSize           = EFI_PAGE_SIZE;
    151     CallbackEntry->EndPointer           = CallbackEntry->StatusCodeDataBuffer;
    152     Status = gBS->CreateEvent (
    153                     EVT_NOTIFY_SIGNAL,
    154                     Tpl,
    155                     RscHandlerNotification,
    156                     CallbackEntry,
    157                     &CallbackEntry->Event
    158                     );
    159     ASSERT_EFI_ERROR (Status);
    160   }
    161 
    162   InsertTailList (&mCallbackListHead, &CallbackEntry->Node);
    163 
    164   return EFI_SUCCESS;
    165 }
    166 
    167 /**
    168   Remove a previously registered callback function from the notification list.
    169 
    170   A callback function must be unregistered before it is deallocated. It is important that any registered
    171   callbacks that are not runtime complaint be unregistered when ExitBootServices() is called.
    172 
    173   @param[in]  Callback  A pointer to a function of type EFI_RSC_HANDLER_CALLBACK that is to be
    174                         unregistered.
    175 
    176   @retval EFI_SUCCESS           The function was successfully unregistered.
    177   @retval EFI_INVALID_PARAMETER The callback function was NULL.
    178   @retval EFI_NOT_FOUND         The callback function was not found to be unregistered.
    179 
    180 **/
    181 EFI_STATUS
    182 EFIAPI
    183 Unregister (
    184   IN EFI_RSC_HANDLER_CALLBACK Callback
    185   )
    186 {
    187   LIST_ENTRY                    *Link;
    188   RSC_HANDLER_CALLBACK_ENTRY    *CallbackEntry;
    189 
    190   if (Callback == NULL) {
    191     return EFI_INVALID_PARAMETER;
    192   }
    193 
    194   for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
    195     CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
    196     if (CallbackEntry->RscHandlerCallback == Callback) {
    197       //
    198       // If the function is found in list, delete it and return.
    199       //
    200       if (CallbackEntry->Tpl != TPL_HIGH_LEVEL) {
    201         FreePool ((VOID *) (UINTN) CallbackEntry->StatusCodeDataBuffer);
    202         gBS->CloseEvent (CallbackEntry->Event);
    203       }
    204       RemoveEntryList (&CallbackEntry->Node);
    205       FreePool (CallbackEntry);
    206       return EFI_SUCCESS;
    207     }
    208   }
    209 
    210   return EFI_NOT_FOUND;
    211 }
    212 
    213 /**
    214   Provides an interface that a software module can call to report a status code.
    215 
    216   @param  Type             Indicates the type of status code being reported.
    217   @param  Value            Describes the current status of a hardware or software entity.
    218                            This included information about the class and subclass that is used to
    219                            classify the entity as well as an operation.
    220   @param  Instance         The enumeration of a hardware or software entity within
    221                            the system. Valid instance numbers start with 1.
    222   @param  CallerId         This optional parameter may be used to identify the caller.
    223                            This parameter allows the status code driver to apply different rules to
    224                            different callers.
    225   @param  Data             This optional parameter may be used to pass additional data.
    226 
    227   @retval EFI_SUCCESS           The function completed successfully
    228   @retval EFI_DEVICE_ERROR      The function should not be completed due to a device error.
    229 
    230 **/
    231 EFI_STATUS
    232 EFIAPI
    233 ReportDispatcher (
    234   IN EFI_STATUS_CODE_TYPE     Type,
    235   IN EFI_STATUS_CODE_VALUE    Value,
    236   IN UINT32                   Instance,
    237   IN EFI_GUID                 *CallerId  OPTIONAL,
    238   IN EFI_STATUS_CODE_DATA     *Data      OPTIONAL
    239   )
    240 {
    241   LIST_ENTRY                    *Link;
    242   RSC_HANDLER_CALLBACK_ENTRY    *CallbackEntry;
    243   RSC_DATA_ENTRY                *RscData;
    244   EFI_STATUS                    Status;
    245   VOID                          *NewBuffer;
    246 
    247   //
    248   // Use atom operation to avoid the reentant of report.
    249   // If current status is not zero, then the function is reentrancy.
    250   //
    251   if (InterlockedCompareExchange32 (&mStatusCodeNestStatus, 0, 1) == 1) {
    252     return EFI_DEVICE_ERROR;
    253   }
    254 
    255   for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link);) {
    256     CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
    257     //
    258     // The handler may remove itself, so get the next handler in advance.
    259     //
    260     Link = GetNextNode (&mCallbackListHead, Link);
    261     if ((CallbackEntry->Tpl == TPL_HIGH_LEVEL) || EfiAtRuntime ()) {
    262       CallbackEntry->RscHandlerCallback (
    263                        Type,
    264                        Value,
    265                        Instance,
    266                        CallerId,
    267                        Data
    268                        );
    269       continue;
    270     }
    271 
    272     //
    273     // If callback is registered with TPL lower than TPL_HIGH_LEVEL, event must be signaled at boot time to possibly wait for
    274     // allowed TPL to report status code. Related data should also be stored in data buffer.
    275     //
    276     CallbackEntry->EndPointer  = ALIGN_VARIABLE (CallbackEntry->EndPointer);
    277     RscData = (RSC_DATA_ENTRY *) (UINTN) CallbackEntry->EndPointer;
    278     CallbackEntry->EndPointer += sizeof (RSC_DATA_ENTRY);
    279     if (Data != NULL) {
    280       CallbackEntry->EndPointer += Data->Size;
    281     }
    282 
    283     //
    284     // If data buffer is about to be used up (7/8 here), try to reallocate a buffer with double size, if not at TPL_HIGH_LEVEL.
    285     //
    286     if (CallbackEntry->EndPointer > (CallbackEntry->StatusCodeDataBuffer + (CallbackEntry->BufferSize / 8) * 7)) {
    287       if (EfiGetCurrentTpl () < TPL_HIGH_LEVEL) {
    288         NewBuffer = ReallocatePool (
    289                       CallbackEntry->BufferSize,
    290                       CallbackEntry->BufferSize * 2,
    291                       (VOID *) (UINTN) CallbackEntry->StatusCodeDataBuffer
    292                       );
    293         if (NewBuffer != NULL) {
    294           CallbackEntry->EndPointer = (EFI_PHYSICAL_ADDRESS) (UINTN) NewBuffer + (CallbackEntry->EndPointer - CallbackEntry->StatusCodeDataBuffer);
    295           CallbackEntry->StatusCodeDataBuffer = (EFI_PHYSICAL_ADDRESS) (UINTN) NewBuffer;
    296           CallbackEntry->BufferSize *= 2;
    297         }
    298       }
    299     }
    300 
    301     //
    302     // If data buffer is used up, do not report for this time.
    303     //
    304     if (CallbackEntry->EndPointer > (CallbackEntry->StatusCodeDataBuffer + CallbackEntry->BufferSize)) {
    305       continue;
    306     }
    307 
    308     RscData->Type      = Type;
    309     RscData->Value     = Value;
    310     RscData->Instance  = Instance;
    311     if (CallerId != NULL) {
    312       CopyGuid (&RscData->CallerId, CallerId);
    313     }
    314     if (Data != NULL) {
    315       CopyMem (&RscData->Data, Data, Data->HeaderSize + Data->Size);
    316     }
    317 
    318     Status = gBS->SignalEvent (CallbackEntry->Event);
    319     ASSERT_EFI_ERROR (Status);
    320   }
    321 
    322   //
    323   // Restore the nest status of report
    324   //
    325   InterlockedCompareExchange32 (&mStatusCodeNestStatus, 1, 0);
    326 
    327   return EFI_SUCCESS;
    328 }
    329 
    330 /**
    331   Virtual address change notification call back. It converts global pointer
    332   to virtual address.
    333 
    334   @param  Event         Event whose notification function is being invoked.
    335   @param  Context       Pointer to the notification function's context, which is
    336                         always zero in current implementation.
    337 
    338 **/
    339 VOID
    340 EFIAPI
    341 VirtualAddressChangeCallBack (
    342   IN EFI_EVENT        Event,
    343   IN VOID             *Context
    344   )
    345 {
    346   EFI_STATUS					Status;
    347   LIST_ENTRY                    *Link;
    348   RSC_HANDLER_CALLBACK_ENTRY    *CallbackEntry;
    349 
    350   for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
    351     CallbackEntry = CR (Link, RSC_HANDLER_CALLBACK_ENTRY, Node, RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
    352     Status = EfiConvertFunctionPointer (0, (VOID **) &CallbackEntry->RscHandlerCallback);
    353     ASSERT_EFI_ERROR (Status);
    354   }
    355 
    356   Status = EfiConvertList (
    357              0,
    358              &mCallbackListHead
    359              );
    360   ASSERT_EFI_ERROR (Status);
    361 }
    362 
    363 /**
    364   Entry point of Generic Status Code Driver.
    365 
    366   This function is the entry point of this Generic Status Code Driver.
    367   It installs eport Stataus Code Handler Protocol  and Status Code Runtime Protocol,
    368   and registers event for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
    369 
    370   @param  ImageHandle       The firmware allocated handle for the EFI image.
    371   @param  SystemTable       A pointer to the EFI System Table.
    372 
    373   @retval EFI_SUCCESS       The entry point is executed successfully.
    374 
    375 **/
    376 EFI_STATUS
    377 EFIAPI
    378 GenericStatusCodeRuntimeDxeEntry (
    379   IN EFI_HANDLE         ImageHandle,
    380   IN EFI_SYSTEM_TABLE   *SystemTable
    381   )
    382 {
    383   EFI_STATUS     Status;
    384 
    385   Status = gBS->InstallMultipleProtocolInterfaces (
    386                   &mHandle,
    387                   &gEfiRscHandlerProtocolGuid,
    388                   &mRscHandlerProtocol,
    389                   &gEfiStatusCodeRuntimeProtocolGuid,
    390                   &mStatusCodeProtocol,
    391                   NULL
    392                   );
    393   ASSERT_EFI_ERROR (Status);
    394 
    395   Status = gBS->CreateEventEx (
    396                   EVT_NOTIFY_SIGNAL,
    397                   TPL_NOTIFY,
    398                   VirtualAddressChangeCallBack,
    399                   NULL,
    400                   &gEfiEventVirtualAddressChangeGuid,
    401                   &mVirtualAddressChangeEvent
    402                   );
    403   ASSERT_EFI_ERROR (Status);
    404 
    405   return EFI_SUCCESS;
    406 }
    407