--- /dev/null
+/** @file\r
+ SMM Driver Dispatcher Dependency Evaluator\r
+\r
+ This routine evaluates a dependency expression (DEPENDENCY_EXPRESSION) to determine\r
+ if a driver can be scheduled for execution. The criteria for\r
+ schedulability is that the dependency expression is satisfied.\r
+\r
+ Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
+ This program and the accompanying materials are licensed and made available \r
+ under the terms and conditions of the BSD License which accompanies this \r
+ distribution. The full text of the license may be found at \r
+ http://opensource.org/licenses/bsd-license.php \r
+\r
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+\r
+**/\r
+\r
+#include "PiSmmCore.h"\r
+\r
+///\r
+/// EFI_DEP_REPLACE_TRUE - Used to dynamically patch the dependecy expression\r
+/// to save time. A EFI_DEP_PUSH is evauated one an\r
+/// replaced with EFI_DEP_REPLACE_TRUE. If PI spec's Vol 2\r
+/// Driver Execution Environment Core Interface use 0xff\r
+/// as new DEPEX opcode. EFI_DEP_REPLACE_TRUE should be\r
+/// defined to a new value that is not conflicting with PI spec.\r
+///\r
+#define EFI_DEP_REPLACE_TRUE 0xff\r
+\r
+///\r
+/// Define the initial size of the dependency expression evaluation stack\r
+///\r
+#define DEPEX_STACK_SIZE_INCREMENT 0x1000\r
+\r
+//\r
+// Global stack used to evaluate dependency expressions\r
+//\r
+BOOLEAN *mDepexEvaluationStack = NULL;\r
+BOOLEAN *mDepexEvaluationStackEnd = NULL;\r
+BOOLEAN *mDepexEvaluationStackPointer = NULL;\r
+\r
+/**\r
+ Grow size of the Depex stack\r
+\r
+ @retval EFI_SUCCESS Stack successfully growed.\r
+ @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.\r
+\r
+**/\r
+EFI_STATUS\r
+GrowDepexStack (\r
+ VOID\r
+ )\r
+{\r
+ BOOLEAN *NewStack;\r
+ UINTN Size;\r
+\r
+ Size = DEPEX_STACK_SIZE_INCREMENT;\r
+ if (mDepexEvaluationStack != NULL) {\r
+ Size = Size + (mDepexEvaluationStackEnd - mDepexEvaluationStack);\r
+ }\r
+\r
+ NewStack = AllocatePool (Size * sizeof (BOOLEAN));\r
+ if (NewStack == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ if (mDepexEvaluationStack != NULL) {\r
+ //\r
+ // Copy to Old Stack to the New Stack\r
+ //\r
+ CopyMem (\r
+ NewStack,\r
+ mDepexEvaluationStack,\r
+ (mDepexEvaluationStackEnd - mDepexEvaluationStack) * sizeof (BOOLEAN)\r
+ );\r
+\r
+ //\r
+ // Free The Old Stack\r
+ //\r
+ FreePool (mDepexEvaluationStack);\r
+ }\r
+\r
+ //\r
+ // Make the Stack pointer point to the old data in the new stack\r
+ //\r
+ mDepexEvaluationStackPointer = NewStack + (mDepexEvaluationStackPointer - mDepexEvaluationStack);\r
+ mDepexEvaluationStack = NewStack;\r
+ mDepexEvaluationStackEnd = NewStack + Size;\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Push an element onto the Boolean Stack.\r
+\r
+ @param Value BOOLEAN to push.\r
+\r
+ @retval EFI_SUCCESS The value was pushed onto the stack.\r
+ @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.\r
+\r
+**/\r
+EFI_STATUS\r
+PushBool (\r
+ IN BOOLEAN Value\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Check for a stack overflow condition\r
+ //\r
+ if (mDepexEvaluationStackPointer == mDepexEvaluationStackEnd) {\r
+ //\r
+ // Grow the stack\r
+ //\r
+ Status = GrowDepexStack ();\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Push the item onto the stack\r
+ //\r
+ *mDepexEvaluationStackPointer = Value;\r
+ mDepexEvaluationStackPointer++;\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Pop an element from the Boolean stack.\r
+\r
+ @param Value BOOLEAN to pop.\r
+\r
+ @retval EFI_SUCCESS The value was popped onto the stack.\r
+ @retval EFI_ACCESS_DENIED The pop operation underflowed the stack.\r
+\r
+**/\r
+EFI_STATUS\r
+PopBool (\r
+ OUT BOOLEAN *Value\r
+ )\r
+{\r
+ //\r
+ // Check for a stack underflow condition\r
+ //\r
+ if (mDepexEvaluationStackPointer == mDepexEvaluationStack) {\r
+ return EFI_ACCESS_DENIED;\r
+ }\r
+\r
+ //\r
+ // Pop the item off the stack\r
+ //\r
+ mDepexEvaluationStackPointer--;\r
+ *Value = *mDepexEvaluationStackPointer;\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ This is the POSTFIX version of the dependency evaluator. This code does\r
+ not need to handle Before or After, as it is not valid to call this\r
+ routine in this case. The SOR is just ignored and is a nop in the grammer.\r
+ POSTFIX means all the math is done on top of the stack.\r
+\r
+ @param DriverEntry DriverEntry element to update.\r
+\r
+ @retval TRUE If driver is ready to run.\r
+ @retval FALSE If driver is not ready to run or some fatal error\r
+ was found.\r
+\r
+**/\r
+BOOLEAN\r
+SmmIsSchedulable (\r
+ IN EFI_SMM_DRIVER_ENTRY *DriverEntry\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINT8 *Iterator;\r
+ BOOLEAN Operator;\r
+ BOOLEAN Operator2;\r
+ EFI_GUID DriverGuid;\r
+ VOID *Interface;\r
+\r
+ Operator = FALSE;\r
+ Operator2 = FALSE;\r
+\r
+ if (DriverEntry->After || DriverEntry->Before) {\r
+ //\r
+ // If Before or After Depex skip as SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()\r
+ // processes them.\r
+ //\r
+ return FALSE;\r
+ }\r
+\r
+ if (DriverEntry->Depex == NULL) {\r
+ //\r
+ // A NULL Depex means that the SMM driver is not built correctly. \r
+ // All SMM drivers must have a valid depex expressiion.\r
+ //\r
+ ASSERT (FALSE);\r
+ return FALSE;\r
+ }\r
+\r
+ //\r
+ // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by\r
+ // incorrectly formed DEPEX expressions\r
+ //\r
+ mDepexEvaluationStackPointer = mDepexEvaluationStack;\r
+\r
+\r
+ Iterator = DriverEntry->Depex;\r
+\r
+ while (TRUE) {\r
+ //\r
+ // Check to see if we are attempting to fetch dependency expression instructions\r
+ // past the end of the dependency expression.\r
+ //\r
+ if (((UINTN)Iterator - (UINTN)DriverEntry->Depex) >= DriverEntry->DepexSize) {\r
+ return FALSE;\r
+ }\r
+\r
+ //\r
+ // Look at the opcode of the dependency expression instruction.\r
+ //\r
+ switch (*Iterator) {\r
+ case EFI_DEP_BEFORE:\r
+ case EFI_DEP_AFTER:\r
+ //\r
+ // For a well-formed Dependency Expression, the code should never get here.\r
+ // The BEFORE and AFTER are processed prior to this routine's invocation.\r
+ // If the code flow arrives at this point, there was a BEFORE or AFTER\r
+ // that were not the first opcodes.\r
+ //\r
+ ASSERT (FALSE);\r
+ case EFI_DEP_SOR:\r
+ //\r
+ // These opcodes can only appear once as the first opcode. If it is found\r
+ // at any other location, then the dependency expression evaluates to FALSE\r
+ //\r
+ if (Iterator != DriverEntry->Depex) {\r
+ return FALSE;\r
+ }\r
+ //\r
+ // Otherwise, it is the first opcode and should be treated as a NOP.\r
+ //\r
+ break;\r
+\r
+ case EFI_DEP_PUSH:\r
+ //\r
+ // Push operator is followed by a GUID. Test to see if the GUID protocol\r
+ // is installed and push the boolean result on the stack.\r
+ //\r
+ CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));\r
+\r
+ Status = SmmLocateProtocol (&DriverGuid, NULL, &Interface);\r
+ if (EFI_ERROR (Status)) {\r
+ //\r
+ // For SMM Driver, it may depend on uefi protocols\r
+ //\r
+ Status = gBS->LocateProtocol (&DriverGuid, NULL, &Interface);\r
+ }\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ Status = PushBool (FALSE);\r
+ } else {\r
+ *Iterator = EFI_DEP_REPLACE_TRUE;\r
+ Status = PushBool (TRUE);\r
+ }\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ Iterator += sizeof (EFI_GUID);\r
+ break;\r
+\r
+ case EFI_DEP_AND:\r
+ Status = PopBool (&Operator);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ Status = PopBool (&Operator2);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ Status = PushBool ((BOOLEAN)(Operator && Operator2));\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ break;\r
+\r
+ case EFI_DEP_OR:\r
+ Status = PopBool (&Operator);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ Status = PopBool (&Operator2);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ Status = PushBool ((BOOLEAN)(Operator || Operator2));\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ break;\r
+\r
+ case EFI_DEP_NOT:\r
+ Status = PopBool (&Operator);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ Status = PushBool ((BOOLEAN)(!Operator));\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ break;\r
+\r
+ case EFI_DEP_TRUE:\r
+ Status = PushBool (TRUE);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ break;\r
+\r
+ case EFI_DEP_FALSE:\r
+ Status = PushBool (FALSE);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ break;\r
+\r
+ case EFI_DEP_END:\r
+ Status = PopBool (&Operator);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ return Operator;\r
+\r
+ case EFI_DEP_REPLACE_TRUE:\r
+ Status = PushBool (TRUE);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ Iterator += sizeof (EFI_GUID);\r
+ break;\r
+\r
+ default:\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Skip over the Dependency Op Code we just processed in the switch.\r
+ // The math is done out of order, but it should not matter. That is\r
+ // we may add in the sizeof (EFI_GUID) before we account for the OP Code.\r
+ // This is not an issue, since we just need the correct end result. You\r
+ // need to be careful using Iterator in the loop as it's intermediate value\r
+ // may be strange.\r
+ //\r
+ Iterator++;\r
+ }\r
+\r
+Done:\r
+ return FALSE;\r
+}\r