]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/SmmPeriodicSmiLib: Get Periodic SMI Context More Robustly
authorRuiyu Ni <ruiyu.ni@intel.com>
Wed, 9 May 2018 09:36:06 +0000 (17:36 +0800)
committerRuiyu Ni <ruiyu.ni@intel.com>
Mon, 21 May 2018 04:47:10 +0000 (12:47 +0800)
The PeriodicSmiDispatchFunction() in SmmPeriodicSmiLib may assert
with "Bad CR signature".

Currently, the SetActivePeriodicSmiLibraryHandler() function
(invoked at the beginning of the PeriodicSmiDispatchFunction()
function) attempts to locate the PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT
structure pointer for the current periodic SMI from a given
EFI_SMM_PERIODIC_TIMER_REGISTER_CONTEXT (RegiserContext) structure
pointer (using the CR macro).

The RegisterContext structure pointer passed to the
PeriodicSmiDispatchFunction() is assumed to point to the same
RegisterContext structure address given to the
SmmPeriodicTimerDispatch2 protocol Register() API in
PeriodicSmiEnable().

However, certain SmmPeriodicTimerDispatch2 implementation may copy
the RegisterContext to a local buffer and pass that address as the
context to PeriodicSmiDispatchFunction() in which case usage of the
CR macro to find the parent structure base fails.

The patch uses the LookupPeriodicSmiLibraryHandler() function to
find the PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure pointer.
This works even in this scenario since the DispatchHandle returned
from the SmmPeriodicTimerDispatch2 Register() function uniquely
identifies that registration.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
MdePkg/Library/SmmPeriodicSmiLib/SmmPeriodicSmiLib.c

index 2016af60d87b729c755f0da52ca2bbf8290f03ba..ca6967c9beeb13d53f48f1c0e9778a15df121ce0 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   SMM Periodic SMI Library.\r
 \r
-  Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -144,19 +144,6 @@ typedef struct {
   UINT64                                   ElapsedTime;\r
 } PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT;\r
 \r
-/**\r
- Macro that returns a pointer to a PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT \r
- structure based on a pointer to a RegisterContext field.\r
-\r
-**/\r
-#define PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_REGISTER_CONTEXT(a) \\r
-  CR (                                                                \\r
-    a,                                                                \\r
-    PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT,                             \\r
-    RegisterContext,                                                  \\r
-    PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_SIGNATURE                    \\r
-    )\r
-\r
 /**\r
  Macro that returns a pointer to a PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT \r
  structure based on a pointer to a Link field.\r
@@ -280,26 +267,31 @@ LookupPeriodicSmiLibraryHandler (
 \r
 /**\r
   Internal worker function that sets that active periodic SMI handler based on \r
-  the Context used when the periodic SMI handler was registered with the \r
-  SMM Periodic Timer Dispatch 2 Protocol.  If Context is NULL, then the \r
+  the DispatchHandle that was returned when the periodic SMI handler was enabled\r
+  with PeriodicSmiEnable(). If DispatchHandle is NULL, then the \r
   state is updated to show that there is not active periodic SMI handler.\r
   A pointer to the active PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT structure \r
   is returned.\r
-  \r
-  @retval  NULL   Context is NULL.\r
+\r
+  @param [in] DispatchHandle DispatchHandle that was returned when the periodic\r
+                             SMI handler was enabled with PeriodicSmiEnable().\r
+                             This is an optional parameter that may be NULL.\r
+                             If this parameter is NULL, then the state is updated\r
+                             to show that there is not active periodic SMI handler.\r
+  @retval  NULL   DispatchHandle is NULL.\r
   @retval  other  Pointer to the PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT\r
-                  associated with Context.\r
+                  associated with DispatchHandle.\r
   \r
 **/\r
 PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT *\r
 SetActivePeriodicSmiLibraryHandler (\r
-  IN CONST VOID  *Context  OPTIONAL\r
+  IN EFI_HANDLE                         DispatchHandle    OPTIONAL\r
   )\r
 {\r
-  if (Context == NULL) {\r
+  if (DispatchHandle == NULL) {\r
     gActivePeriodicSmiLibraryHandler = NULL;\r
   } else {\r
-    gActivePeriodicSmiLibraryHandler = PERIODIC_SMI_LIBRARY_HANDLER_CONTEXT_FROM_REGISTER_CONTEXT (Context);\r
+    gActivePeriodicSmiLibraryHandler = LookupPeriodicSmiLibraryHandler (DispatchHandle);\r
   }\r
   return gActivePeriodicSmiLibraryHandler;\r
 }\r
@@ -798,7 +790,7 @@ PeriodicSmiDispatchFunction (
   //\r
   // Set the active periodic SMI handler\r
   //  \r
-  PeriodicSmiLibraryHandler = SetActivePeriodicSmiLibraryHandler (Context);\r
+  PeriodicSmiLibraryHandler = SetActivePeriodicSmiLibraryHandler (DispatchHandle);\r
   if (PeriodicSmiLibraryHandler == NULL) {\r
     return EFI_NOT_FOUND;\r
   }\r