2 SMM 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 - 2018, Intel Corporation. All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include "PiSmmCore.h"
16 /// EFI_DEP_REPLACE_TRUE - Used to dynamically patch the dependency expression
17 /// to save time. A EFI_DEP_PUSH is evaluated one an
18 /// replaced with EFI_DEP_REPLACE_TRUE. If PI spec's Vol 2
19 /// Driver Execution Environment Core Interface use 0xff
20 /// as new DEPEX opcode. EFI_DEP_REPLACE_TRUE should be
21 /// defined to a new value that is not conflicting with PI spec.
23 #define EFI_DEP_REPLACE_TRUE 0xff
26 /// Define the initial size of the dependency expression evaluation stack
28 #define DEPEX_STACK_SIZE_INCREMENT 0x1000
31 // Global stack used to evaluate dependency expressions
33 BOOLEAN
*mDepexEvaluationStack
= NULL
;
34 BOOLEAN
*mDepexEvaluationStackEnd
= NULL
;
35 BOOLEAN
*mDepexEvaluationStackPointer
= NULL
;
38 Grow size of the Depex stack
40 @retval EFI_SUCCESS Stack successfully growed.
41 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
52 Size
= DEPEX_STACK_SIZE_INCREMENT
;
53 if (mDepexEvaluationStack
!= NULL
) {
54 Size
= Size
+ (mDepexEvaluationStackEnd
- mDepexEvaluationStack
);
57 NewStack
= AllocatePool (Size
* sizeof (BOOLEAN
));
58 if (NewStack
== NULL
) {
59 return EFI_OUT_OF_RESOURCES
;
62 if (mDepexEvaluationStack
!= NULL
) {
64 // Copy to Old Stack to the New Stack
68 mDepexEvaluationStack
,
69 (mDepexEvaluationStackEnd
- mDepexEvaluationStack
) * sizeof (BOOLEAN
)
75 FreePool (mDepexEvaluationStack
);
79 // Make the Stack pointer point to the old data in the new stack
81 mDepexEvaluationStackPointer
= NewStack
+ (mDepexEvaluationStackPointer
- mDepexEvaluationStack
);
82 mDepexEvaluationStack
= NewStack
;
83 mDepexEvaluationStackEnd
= NewStack
+ Size
;
89 Push an element onto the Boolean Stack.
91 @param Value BOOLEAN to push.
93 @retval EFI_SUCCESS The value was pushed onto the stack.
94 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
105 // Check for a stack overflow condition
107 if (mDepexEvaluationStackPointer
== mDepexEvaluationStackEnd
) {
111 Status
= GrowDepexStack ();
112 if (EFI_ERROR (Status
)) {
118 // Push the item onto the stack
120 *mDepexEvaluationStackPointer
= Value
;
121 mDepexEvaluationStackPointer
++;
127 Pop an element from the Boolean stack.
129 @param Value BOOLEAN to pop.
131 @retval EFI_SUCCESS The value was popped onto the stack.
132 @retval EFI_ACCESS_DENIED The pop operation underflowed the stack.
141 // Check for a stack underflow condition
143 if (mDepexEvaluationStackPointer
== mDepexEvaluationStack
) {
144 return EFI_ACCESS_DENIED
;
148 // Pop the item off the stack
150 mDepexEvaluationStackPointer
--;
151 *Value
= *mDepexEvaluationStackPointer
;
156 This is the POSTFIX version of the dependency evaluator. This code does
157 not need to handle Before or After, as it is not valid to call this
158 routine in this case. POSTFIX means all the math is done on top of the stack.
160 @param DriverEntry DriverEntry element to update.
162 @retval TRUE If driver is ready to run.
163 @retval FALSE If driver is not ready to run or some fatal error
169 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
182 if (DriverEntry
->After
|| DriverEntry
->Before
) {
184 // If Before or After Depex skip as SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
190 DEBUG ((DEBUG_DISPATCH
, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
192 if (DriverEntry
->Depex
== NULL
) {
194 // A NULL Depex means that the SMM driver is not built correctly.
195 // All SMM drivers must have a valid depex expression.
197 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Depex is empty)\n"));
203 // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
204 // incorrectly formed DEPEX expressions
206 mDepexEvaluationStackPointer
= mDepexEvaluationStack
;
208 Iterator
= DriverEntry
->Depex
;
212 // Check to see if we are attempting to fetch dependency expression instructions
213 // past the end of the dependency expression.
215 if (((UINTN
)Iterator
- (UINTN
)DriverEntry
->Depex
) >= DriverEntry
->DepexSize
) {
216 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Attempt to fetch past end of depex)\n"));
221 // Look at the opcode of the dependency expression instruction.
227 // For a well-formed Dependency Expression, the code should never get here.
228 // The BEFORE and AFTER are processed prior to this routine's invocation.
229 // If the code flow arrives at this point, there was a BEFORE or AFTER
230 // that were not the first opcodes.
232 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));
237 // Push operator is followed by a GUID. Test to see if the GUID protocol
238 // is installed and push the boolean result on the stack.
240 CopyMem (&DriverGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
242 Status
= SmmLocateProtocol (&DriverGuid
, NULL
, &Interface
);
243 if (EFI_ERROR (Status
)) {
245 // For SMM Driver, it may depend on uefi protocols
247 Status
= gBS
->LocateProtocol (&DriverGuid
, NULL
, &Interface
);
250 if (EFI_ERROR (Status
)) {
251 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = FALSE\n", &DriverGuid
));
252 Status
= PushBool (FALSE
);
254 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = TRUE\n", &DriverGuid
));
255 *Iterator
= EFI_DEP_REPLACE_TRUE
;
256 Status
= PushBool (TRUE
);
259 if (EFI_ERROR (Status
)) {
260 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
264 Iterator
+= sizeof (EFI_GUID
);
268 DEBUG ((DEBUG_DISPATCH
, " AND\n"));
269 Status
= PopBool (&Operator
);
270 if (EFI_ERROR (Status
)) {
271 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
275 Status
= PopBool (&Operator2
);
276 if (EFI_ERROR (Status
)) {
277 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
281 Status
= PushBool ((BOOLEAN
)(Operator
&& Operator2
));
282 if (EFI_ERROR (Status
)) {
283 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"));
312 DEBUG ((DEBUG_DISPATCH
, " NOT\n"));
313 Status
= PopBool (&Operator
);
314 if (EFI_ERROR (Status
)) {
315 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
319 Status
= PushBool ((BOOLEAN
)(!Operator
));
320 if (EFI_ERROR (Status
)) {
321 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
328 DEBUG ((DEBUG_DISPATCH
, " TRUE\n"));
329 Status
= PushBool (TRUE
);
330 if (EFI_ERROR (Status
)) {
331 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
338 DEBUG ((DEBUG_DISPATCH
, " FALSE\n"));
339 Status
= PushBool (FALSE
);
340 if (EFI_ERROR (Status
)) {
341 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
348 DEBUG ((DEBUG_DISPATCH
, " END\n"));
349 Status
= PopBool (&Operator
);
350 if (EFI_ERROR (Status
)) {
351 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
355 DEBUG ((DEBUG_DISPATCH
, " RESULT = %a\n", Operator
? "TRUE" : "FALSE"));
358 case EFI_DEP_REPLACE_TRUE
:
359 CopyMem (&DriverGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
360 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = TRUE\n", &DriverGuid
));
361 Status
= PushBool (TRUE
);
362 if (EFI_ERROR (Status
)) {
363 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
367 Iterator
+= sizeof (EFI_GUID
);
371 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unknown opcode)\n"));
376 // Skip over the Dependency Op Code we just processed in the switch.
377 // The math is done out of order, but it should not matter. That is
378 // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
379 // This is not an issue, since we just need the correct end result. You
380 // need to be careful using Iterator in the loop as its intermediate value