2 MM Driver Dispatcher Dependency Evaluator
4 This routine evaluates a dependency expression (DEPENDENCY_EXPRESSION) to determine
5 if a driver can be scheduled for execution. The criteria for
6 schedulability is that the dependency expression is satisfied.
8 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
9 Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
10 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include "StandaloneMmCore.h"
17 /// EFI_DEP_REPLACE_TRUE - Used to dynamically patch the dependency expression
18 /// to save time. A EFI_DEP_PUSH is evaluated one an
19 /// replaced with EFI_DEP_REPLACE_TRUE. If PI spec's Vol 2
20 /// Driver Execution Environment Core Interface use 0xff
21 /// as new DEPEX opcode. EFI_DEP_REPLACE_TRUE should be
22 /// defined to a new value that is not conflicting with PI spec.
24 #define EFI_DEP_REPLACE_TRUE 0xff
27 /// Define the initial size of the dependency expression evaluation stack
29 #define DEPEX_STACK_SIZE_INCREMENT 0x1000
32 // Global stack used to evaluate dependency expressions
34 BOOLEAN
*mDepexEvaluationStack
= NULL
;
35 BOOLEAN
*mDepexEvaluationStackEnd
= NULL
;
36 BOOLEAN
*mDepexEvaluationStackPointer
= NULL
;
39 Grow size of the Depex stack
41 @retval EFI_SUCCESS Stack successfully growed.
42 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
53 Size
= DEPEX_STACK_SIZE_INCREMENT
;
54 if (mDepexEvaluationStack
!= NULL
) {
55 Size
= Size
+ (mDepexEvaluationStackEnd
- mDepexEvaluationStack
);
58 NewStack
= AllocatePool (Size
* sizeof (BOOLEAN
));
59 if (NewStack
== NULL
) {
60 return EFI_OUT_OF_RESOURCES
;
63 if (mDepexEvaluationStack
!= NULL
) {
65 // Copy to Old Stack to the New Stack
69 mDepexEvaluationStack
,
70 (mDepexEvaluationStackEnd
- mDepexEvaluationStack
) * sizeof (BOOLEAN
)
76 FreePool (mDepexEvaluationStack
);
80 // Make the Stack pointer point to the old data in the new stack
82 mDepexEvaluationStackPointer
= NewStack
+ (mDepexEvaluationStackPointer
- mDepexEvaluationStack
);
83 mDepexEvaluationStack
= NewStack
;
84 mDepexEvaluationStackEnd
= NewStack
+ Size
;
90 Push an element onto the Boolean Stack.
92 @param Value BOOLEAN to push.
94 @retval EFI_SUCCESS The value was pushed onto the stack.
95 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
106 // Check for a stack overflow condition
108 if (mDepexEvaluationStackPointer
== mDepexEvaluationStackEnd
) {
112 Status
= GrowDepexStack ();
113 if (EFI_ERROR (Status
)) {
119 // Push the item onto the stack
121 *mDepexEvaluationStackPointer
= Value
;
122 mDepexEvaluationStackPointer
++;
128 Pop an element from the Boolean stack.
130 @param Value BOOLEAN to pop.
132 @retval EFI_SUCCESS The value was popped onto the stack.
133 @retval EFI_ACCESS_DENIED The pop operation underflowed the stack.
142 // Check for a stack underflow condition
144 if (mDepexEvaluationStackPointer
== mDepexEvaluationStack
) {
145 return EFI_ACCESS_DENIED
;
149 // Pop the item off the stack
151 mDepexEvaluationStackPointer
--;
152 *Value
= *mDepexEvaluationStackPointer
;
157 This is the POSTFIX version of the dependency evaluator. This code does
158 not need to handle Before or After, as it is not valid to call this
159 routine in this case. POSTFIX means all the math is done on top of the stack.
161 @param DriverEntry DriverEntry element to update.
163 @retval TRUE If driver is ready to run.
164 @retval FALSE If driver is not ready to run or some fatal error
170 IN EFI_MM_DRIVER_ENTRY
*DriverEntry
183 if (DriverEntry
->After
|| DriverEntry
->Before
) {
185 // If Before or After Depex skip as MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
191 DEBUG ((DEBUG_DISPATCH
, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
193 if (DriverEntry
->Depex
== NULL
) {
195 // A NULL Depex means that the MM driver is not built correctly.
196 // All MM drivers must have a valid depex expressiion.
198 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Depex is empty)\n"));
204 // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
205 // incorrectly formed DEPEX expressions
207 mDepexEvaluationStackPointer
= mDepexEvaluationStack
;
210 Iterator
= DriverEntry
->Depex
;
214 // Check to see if we are attempting to fetch dependency expression instructions
215 // past the end of the dependency expression.
217 if (((UINTN
)Iterator
- (UINTN
)DriverEntry
->Depex
) >= DriverEntry
->DepexSize
) {
218 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Attempt to fetch past end of depex)\n"));
223 // Look at the opcode of the dependency expression instruction.
229 // For a well-formed Dependency Expression, the code should never get here.
230 // The BEFORE and AFTER are processed prior to this routine's invocation.
231 // If the code flow arrives at this point, there was a BEFORE or AFTER
232 // that were not the first opcodes.
234 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));
239 // Push operator is followed by a GUID. Test to see if the GUID protocol
240 // is installed and push the boolean result on the stack.
242 CopyMem (&DriverGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
244 Status
= MmLocateProtocol (&DriverGuid
, NULL
, &Interface
);
245 if (EFI_ERROR (Status
) && (mEfiSystemTable
!= NULL
)) {
247 // For MM Driver, it may depend on uefi protocols
249 Status
= mEfiSystemTable
->BootServices
->LocateProtocol (&DriverGuid
, NULL
, &Interface
);
252 if (EFI_ERROR (Status
)) {
253 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = FALSE\n", &DriverGuid
));
254 Status
= PushBool (FALSE
);
256 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = TRUE\n", &DriverGuid
));
257 *Iterator
= EFI_DEP_REPLACE_TRUE
;
258 Status
= PushBool (TRUE
);
260 if (EFI_ERROR (Status
)) {
261 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
265 Iterator
+= sizeof (EFI_GUID
);
269 DEBUG ((DEBUG_DISPATCH
, " AND\n"));
270 Status
= PopBool (&Operator
);
271 if (EFI_ERROR (Status
)) {
272 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
276 Status
= PopBool (&Operator2
);
277 if (EFI_ERROR (Status
)) {
278 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
282 Status
= PushBool ((BOOLEAN
)(Operator
&& Operator2
));
283 if (EFI_ERROR (Status
)) {
284 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
290 DEBUG ((DEBUG_DISPATCH
, " OR\n"));
291 Status
= PopBool (&Operator
);
292 if (EFI_ERROR (Status
)) {
293 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
297 Status
= PopBool (&Operator2
);
298 if (EFI_ERROR (Status
)) {
299 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
303 Status
= PushBool ((BOOLEAN
)(Operator
|| Operator2
));
304 if (EFI_ERROR (Status
)) {
305 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
311 DEBUG ((DEBUG_DISPATCH
, " NOT\n"));
312 Status
= PopBool (&Operator
);
313 if (EFI_ERROR (Status
)) {
314 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
318 Status
= PushBool ((BOOLEAN
)(!Operator
));
319 if (EFI_ERROR (Status
)) {
320 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
326 DEBUG ((DEBUG_DISPATCH
, " TRUE\n"));
327 Status
= PushBool (TRUE
);
328 if (EFI_ERROR (Status
)) {
329 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
335 DEBUG ((DEBUG_DISPATCH
, " FALSE\n"));
336 Status
= PushBool (FALSE
);
337 if (EFI_ERROR (Status
)) {
338 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
344 DEBUG ((DEBUG_DISPATCH
, " END\n"));
345 Status
= PopBool (&Operator
);
346 if (EFI_ERROR (Status
)) {
347 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
350 DEBUG ((DEBUG_DISPATCH
, " RESULT = %a\n", Operator
? "TRUE" : "FALSE"));
353 case EFI_DEP_REPLACE_TRUE
:
354 CopyMem (&DriverGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
355 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = TRUE\n", &DriverGuid
));
356 Status
= PushBool (TRUE
);
357 if (EFI_ERROR (Status
)) {
358 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
362 Iterator
+= sizeof (EFI_GUID
);
366 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unknown opcode)\n"));
371 // Skip over the Dependency Op Code we just processed in the switch.
372 // The math is done out of order, but it should not matter. That is
373 // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
374 // This is not an issue, since we just need the correct end result. You
375 // need to be careful using Iterator in the loop as it's intermediate value