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 - 2010, Intel Corporation. All rights reserved.<BR>
9 This program and the accompanying materials are licensed and made available
10 under the terms and conditions of the BSD License which accompanies this
11 distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include "PiSmmCore.h"
22 /// EFI_DEP_REPLACE_TRUE - Used to dynamically patch the dependecy expression
23 /// to save time. A EFI_DEP_PUSH is evauated one an
24 /// replaced with EFI_DEP_REPLACE_TRUE. If PI spec's Vol 2
25 /// Driver Execution Environment Core Interface use 0xff
26 /// as new DEPEX opcode. EFI_DEP_REPLACE_TRUE should be
27 /// defined to a new value that is not conflicting with PI spec.
29 #define EFI_DEP_REPLACE_TRUE 0xff
32 /// Define the initial size of the dependency expression evaluation stack
34 #define DEPEX_STACK_SIZE_INCREMENT 0x1000
37 // Global stack used to evaluate dependency expressions
39 BOOLEAN
*mDepexEvaluationStack
= NULL
;
40 BOOLEAN
*mDepexEvaluationStackEnd
= NULL
;
41 BOOLEAN
*mDepexEvaluationStackPointer
= NULL
;
44 Grow size of the Depex stack
46 @retval EFI_SUCCESS Stack successfully growed.
47 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
58 Size
= DEPEX_STACK_SIZE_INCREMENT
;
59 if (mDepexEvaluationStack
!= NULL
) {
60 Size
= Size
+ (mDepexEvaluationStackEnd
- mDepexEvaluationStack
);
63 NewStack
= AllocatePool (Size
* sizeof (BOOLEAN
));
64 if (NewStack
== NULL
) {
65 return EFI_OUT_OF_RESOURCES
;
68 if (mDepexEvaluationStack
!= NULL
) {
70 // Copy to Old Stack to the New Stack
74 mDepexEvaluationStack
,
75 (mDepexEvaluationStackEnd
- mDepexEvaluationStack
) * sizeof (BOOLEAN
)
81 FreePool (mDepexEvaluationStack
);
85 // Make the Stack pointer point to the old data in the new stack
87 mDepexEvaluationStackPointer
= NewStack
+ (mDepexEvaluationStackPointer
- mDepexEvaluationStack
);
88 mDepexEvaluationStack
= NewStack
;
89 mDepexEvaluationStackEnd
= NewStack
+ Size
;
95 Push an element onto the Boolean Stack.
97 @param Value BOOLEAN to push.
99 @retval EFI_SUCCESS The value was pushed onto the stack.
100 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
111 // Check for a stack overflow condition
113 if (mDepexEvaluationStackPointer
== mDepexEvaluationStackEnd
) {
117 Status
= GrowDepexStack ();
118 if (EFI_ERROR (Status
)) {
124 // Push the item onto the stack
126 *mDepexEvaluationStackPointer
= Value
;
127 mDepexEvaluationStackPointer
++;
133 Pop an element from the Boolean stack.
135 @param Value BOOLEAN to pop.
137 @retval EFI_SUCCESS The value was popped onto the stack.
138 @retval EFI_ACCESS_DENIED The pop operation underflowed the stack.
147 // Check for a stack underflow condition
149 if (mDepexEvaluationStackPointer
== mDepexEvaluationStack
) {
150 return EFI_ACCESS_DENIED
;
154 // Pop the item off the stack
156 mDepexEvaluationStackPointer
--;
157 *Value
= *mDepexEvaluationStackPointer
;
162 This is the POSTFIX version of the dependency evaluator. This code does
163 not need to handle Before or After, as it is not valid to call this
164 routine in this case. POSTFIX means all the math is done on top of the stack.
166 @param DriverEntry DriverEntry element to update.
168 @retval TRUE If driver is ready to run.
169 @retval FALSE If driver is not ready to run or some fatal error
175 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
188 if (DriverEntry
->After
|| DriverEntry
->Before
) {
190 // If Before or After Depex skip as SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
196 DEBUG ((DEBUG_DISPATCH
, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry
->FileName
));
198 if (DriverEntry
->Depex
== NULL
) {
200 // A NULL Depex means that the SMM driver is not built correctly.
201 // All SMM drivers must have a valid depex expressiion.
203 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Depex is empty)\n"));
209 // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
210 // incorrectly formed DEPEX expressions
212 mDepexEvaluationStackPointer
= mDepexEvaluationStack
;
215 Iterator
= DriverEntry
->Depex
;
219 // Check to see if we are attempting to fetch dependency expression instructions
220 // past the end of the dependency expression.
222 if (((UINTN
)Iterator
- (UINTN
)DriverEntry
->Depex
) >= DriverEntry
->DepexSize
) {
223 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Attempt to fetch past end of depex)\n"));
228 // Look at the opcode of the dependency expression instruction.
234 // For a well-formed Dependency Expression, the code should never get here.
235 // The BEFORE and AFTER are processed prior to this routine's invocation.
236 // If the code flow arrives at this point, there was a BEFORE or AFTER
237 // that were not the first opcodes.
239 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));
244 // Push operator is followed by a GUID. Test to see if the GUID protocol
245 // is installed and push the boolean result on the stack.
247 CopyMem (&DriverGuid
, Iterator
+ 1, sizeof (EFI_GUID
));
249 Status
= SmmLocateProtocol (&DriverGuid
, NULL
, &Interface
);
250 if (EFI_ERROR (Status
)) {
252 // For SMM Driver, it may depend on uefi protocols
254 Status
= gBS
->LocateProtocol (&DriverGuid
, NULL
, &Interface
);
257 if (EFI_ERROR (Status
)) {
258 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = FALSE\n", &DriverGuid
));
259 Status
= PushBool (FALSE
);
261 DEBUG ((DEBUG_DISPATCH
, " PUSH GUID(%g) = TRUE\n", &DriverGuid
));
262 *Iterator
= EFI_DEP_REPLACE_TRUE
;
263 Status
= PushBool (TRUE
);
265 if (EFI_ERROR (Status
)) {
266 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
270 Iterator
+= sizeof (EFI_GUID
);
274 DEBUG ((DEBUG_DISPATCH
, " AND\n"));
275 Status
= PopBool (&Operator
);
276 if (EFI_ERROR (Status
)) {
277 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
281 Status
= PopBool (&Operator2
);
282 if (EFI_ERROR (Status
)) {
283 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
287 Status
= PushBool ((BOOLEAN
)(Operator
&& Operator2
));
288 if (EFI_ERROR (Status
)) {
289 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
295 DEBUG ((DEBUG_DISPATCH
, " OR\n"));
296 Status
= PopBool (&Operator
);
297 if (EFI_ERROR (Status
)) {
298 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
302 Status
= PopBool (&Operator2
);
303 if (EFI_ERROR (Status
)) {
304 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
308 Status
= PushBool ((BOOLEAN
)(Operator
|| Operator2
));
309 if (EFI_ERROR (Status
)) {
310 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
316 DEBUG ((DEBUG_DISPATCH
, " NOT\n"));
317 Status
= PopBool (&Operator
);
318 if (EFI_ERROR (Status
)) {
319 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
323 Status
= PushBool ((BOOLEAN
)(!Operator
));
324 if (EFI_ERROR (Status
)) {
325 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
331 DEBUG ((DEBUG_DISPATCH
, " TRUE\n"));
332 Status
= PushBool (TRUE
);
333 if (EFI_ERROR (Status
)) {
334 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
340 DEBUG ((DEBUG_DISPATCH
, " FALSE\n"));
341 Status
= PushBool (FALSE
);
342 if (EFI_ERROR (Status
)) {
343 DEBUG ((DEBUG_DISPATCH
, " RESULT = FALSE (Unexpected error)\n"));
349 DEBUG ((DEBUG_DISPATCH
, " END\n"));
350 Status
= PopBool (&Operator
);
351 if (EFI_ERROR (Status
)) {
352 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 it's intermediate value