StandaloneMmPkg/Core: Implementation of Standalone MM Core Module.
[mirror_edk2.git] / StandaloneMmPkg / Core / Dependency.c
1 /** @file
2 MM Driver Dispatcher Dependency Evaluator
3
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.
7
8 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
9 Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
10 This program and the accompanying materials are licensed and made available
11 under the terms and conditions of the BSD License which accompanies this
12 distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
14
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17
18 **/
19
20 #include "StandaloneMmCore.h"
21
22 ///
23 /// EFI_DEP_REPLACE_TRUE - Used to dynamically patch the dependency expression
24 /// to save time. A EFI_DEP_PUSH is evaluated one an
25 /// replaced with EFI_DEP_REPLACE_TRUE. If PI spec's Vol 2
26 /// Driver Execution Environment Core Interface use 0xff
27 /// as new DEPEX opcode. EFI_DEP_REPLACE_TRUE should be
28 /// defined to a new value that is not conflicting with PI spec.
29 ///
30 #define EFI_DEP_REPLACE_TRUE 0xff
31
32 ///
33 /// Define the initial size of the dependency expression evaluation stack
34 ///
35 #define DEPEX_STACK_SIZE_INCREMENT 0x1000
36
37 //
38 // Global stack used to evaluate dependency expressions
39 //
40 BOOLEAN *mDepexEvaluationStack = NULL;
41 BOOLEAN *mDepexEvaluationStackEnd = NULL;
42 BOOLEAN *mDepexEvaluationStackPointer = NULL;
43
44 /**
45 Grow size of the Depex stack
46
47 @retval EFI_SUCCESS Stack successfully growed.
48 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
49
50 **/
51 EFI_STATUS
52 GrowDepexStack (
53 VOID
54 )
55 {
56 BOOLEAN *NewStack;
57 UINTN Size;
58
59 Size = DEPEX_STACK_SIZE_INCREMENT;
60 if (mDepexEvaluationStack != NULL) {
61 Size = Size + (mDepexEvaluationStackEnd - mDepexEvaluationStack);
62 }
63
64 NewStack = AllocatePool (Size * sizeof (BOOLEAN));
65 if (NewStack == NULL) {
66 return EFI_OUT_OF_RESOURCES;
67 }
68
69 if (mDepexEvaluationStack != NULL) {
70 //
71 // Copy to Old Stack to the New Stack
72 //
73 CopyMem (
74 NewStack,
75 mDepexEvaluationStack,
76 (mDepexEvaluationStackEnd - mDepexEvaluationStack) * sizeof (BOOLEAN)
77 );
78
79 //
80 // Free The Old Stack
81 //
82 FreePool (mDepexEvaluationStack);
83 }
84
85 //
86 // Make the Stack pointer point to the old data in the new stack
87 //
88 mDepexEvaluationStackPointer = NewStack + (mDepexEvaluationStackPointer - mDepexEvaluationStack);
89 mDepexEvaluationStack = NewStack;
90 mDepexEvaluationStackEnd = NewStack + Size;
91
92 return EFI_SUCCESS;
93 }
94
95 /**
96 Push an element onto the Boolean Stack.
97
98 @param Value BOOLEAN to push.
99
100 @retval EFI_SUCCESS The value was pushed onto the stack.
101 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
102
103 **/
104 EFI_STATUS
105 PushBool (
106 IN BOOLEAN Value
107 )
108 {
109 EFI_STATUS Status;
110
111 //
112 // Check for a stack overflow condition
113 //
114 if (mDepexEvaluationStackPointer == mDepexEvaluationStackEnd) {
115 //
116 // Grow the stack
117 //
118 Status = GrowDepexStack ();
119 if (EFI_ERROR (Status)) {
120 return Status;
121 }
122 }
123
124 //
125 // Push the item onto the stack
126 //
127 *mDepexEvaluationStackPointer = Value;
128 mDepexEvaluationStackPointer++;
129
130 return EFI_SUCCESS;
131 }
132
133 /**
134 Pop an element from the Boolean stack.
135
136 @param Value BOOLEAN to pop.
137
138 @retval EFI_SUCCESS The value was popped onto the stack.
139 @retval EFI_ACCESS_DENIED The pop operation underflowed the stack.
140
141 **/
142 EFI_STATUS
143 PopBool (
144 OUT BOOLEAN *Value
145 )
146 {
147 //
148 // Check for a stack underflow condition
149 //
150 if (mDepexEvaluationStackPointer == mDepexEvaluationStack) {
151 return EFI_ACCESS_DENIED;
152 }
153
154 //
155 // Pop the item off the stack
156 //
157 mDepexEvaluationStackPointer--;
158 *Value = *mDepexEvaluationStackPointer;
159 return EFI_SUCCESS;
160 }
161
162 /**
163 This is the POSTFIX version of the dependency evaluator. This code does
164 not need to handle Before or After, as it is not valid to call this
165 routine in this case. POSTFIX means all the math is done on top of the stack.
166
167 @param DriverEntry DriverEntry element to update.
168
169 @retval TRUE If driver is ready to run.
170 @retval FALSE If driver is not ready to run or some fatal error
171 was found.
172
173 **/
174 BOOLEAN
175 MmIsSchedulable (
176 IN EFI_MM_DRIVER_ENTRY *DriverEntry
177 )
178 {
179 EFI_STATUS Status;
180 UINT8 *Iterator;
181 BOOLEAN Operator;
182 BOOLEAN Operator2;
183 EFI_GUID DriverGuid;
184 VOID *Interface;
185
186 Operator = FALSE;
187 Operator2 = FALSE;
188
189 if (DriverEntry->After || DriverEntry->Before) {
190 //
191 // If Before or After Depex skip as MmInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()
192 // processes them.
193 //
194 return FALSE;
195 }
196
197 DEBUG ((DEBUG_DISPATCH, "Evaluate MM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
198
199 if (DriverEntry->Depex == NULL) {
200 //
201 // A NULL Depex means that the MM driver is not built correctly.
202 // All MM drivers must have a valid depex expressiion.
203 //
204 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Depex is empty)\n"));
205 ASSERT (FALSE);
206 return FALSE;
207 }
208
209 //
210 // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by
211 // incorrectly formed DEPEX expressions
212 //
213 mDepexEvaluationStackPointer = mDepexEvaluationStack;
214
215
216 Iterator = DriverEntry->Depex;
217
218 while (TRUE) {
219 //
220 // Check to see if we are attempting to fetch dependency expression instructions
221 // past the end of the dependency expression.
222 //
223 if (((UINTN)Iterator - (UINTN)DriverEntry->Depex) >= DriverEntry->DepexSize) {
224 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Attempt to fetch past end of depex)\n"));
225 return FALSE;
226 }
227
228 //
229 // Look at the opcode of the dependency expression instruction.
230 //
231 switch (*Iterator) {
232 case EFI_DEP_BEFORE:
233 case EFI_DEP_AFTER:
234 //
235 // For a well-formed Dependency Expression, the code should never get here.
236 // The BEFORE and AFTER are processed prior to this routine's invocation.
237 // If the code flow arrives at this point, there was a BEFORE or AFTER
238 // that were not the first opcodes.
239 //
240 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));
241 ASSERT (FALSE);
242
243 case EFI_DEP_PUSH:
244 //
245 // Push operator is followed by a GUID. Test to see if the GUID protocol
246 // is installed and push the boolean result on the stack.
247 //
248 CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));
249
250 Status = MmLocateProtocol (&DriverGuid, NULL, &Interface);
251 if (EFI_ERROR (Status) && (mEfiSystemTable != NULL)) {
252 //
253 // For MM Driver, it may depend on uefi protocols
254 //
255 Status = mEfiSystemTable->BootServices->LocateProtocol (&DriverGuid, NULL, &Interface);
256 }
257
258 if (EFI_ERROR (Status)) {
259 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = FALSE\n", &DriverGuid));
260 Status = PushBool (FALSE);
261 } else {
262 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = TRUE\n", &DriverGuid));
263 *Iterator = EFI_DEP_REPLACE_TRUE;
264 Status = PushBool (TRUE);
265 }
266 if (EFI_ERROR (Status)) {
267 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
268 return FALSE;
269 }
270
271 Iterator += sizeof (EFI_GUID);
272 break;
273
274 case EFI_DEP_AND:
275 DEBUG ((DEBUG_DISPATCH, " AND\n"));
276 Status = PopBool (&Operator);
277 if (EFI_ERROR (Status)) {
278 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
279 return FALSE;
280 }
281
282 Status = PopBool (&Operator2);
283 if (EFI_ERROR (Status)) {
284 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
285 return FALSE;
286 }
287
288 Status = PushBool ((BOOLEAN)(Operator && Operator2));
289 if (EFI_ERROR (Status)) {
290 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
291 return FALSE;
292 }
293 break;
294
295 case EFI_DEP_OR:
296 DEBUG ((DEBUG_DISPATCH, " OR\n"));
297 Status = PopBool (&Operator);
298 if (EFI_ERROR (Status)) {
299 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
300 return FALSE;
301 }
302
303 Status = PopBool (&Operator2);
304 if (EFI_ERROR (Status)) {
305 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
306 return FALSE;
307 }
308
309 Status = PushBool ((BOOLEAN)(Operator || Operator2));
310 if (EFI_ERROR (Status)) {
311 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
312 return FALSE;
313 }
314 break;
315
316 case EFI_DEP_NOT:
317 DEBUG ((DEBUG_DISPATCH, " NOT\n"));
318 Status = PopBool (&Operator);
319 if (EFI_ERROR (Status)) {
320 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
321 return FALSE;
322 }
323
324 Status = PushBool ((BOOLEAN)(!Operator));
325 if (EFI_ERROR (Status)) {
326 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
327 return FALSE;
328 }
329 break;
330
331 case EFI_DEP_TRUE:
332 DEBUG ((DEBUG_DISPATCH, " TRUE\n"));
333 Status = PushBool (TRUE);
334 if (EFI_ERROR (Status)) {
335 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
336 return FALSE;
337 }
338 break;
339
340 case EFI_DEP_FALSE:
341 DEBUG ((DEBUG_DISPATCH, " FALSE\n"));
342 Status = PushBool (FALSE);
343 if (EFI_ERROR (Status)) {
344 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
345 return FALSE;
346 }
347 break;
348
349 case EFI_DEP_END:
350 DEBUG ((DEBUG_DISPATCH, " END\n"));
351 Status = PopBool (&Operator);
352 if (EFI_ERROR (Status)) {
353 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
354 return FALSE;
355 }
356 DEBUG ((DEBUG_DISPATCH, " RESULT = %a\n", Operator ? "TRUE" : "FALSE"));
357 return Operator;
358
359 case EFI_DEP_REPLACE_TRUE:
360 CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));
361 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = TRUE\n", &DriverGuid));
362 Status = PushBool (TRUE);
363 if (EFI_ERROR (Status)) {
364 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));
365 return FALSE;
366 }
367
368 Iterator += sizeof (EFI_GUID);
369 break;
370
371 default:
372 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unknown opcode)\n"));
373 goto Done;
374 }
375
376 //
377 // Skip over the Dependency Op Code we just processed in the switch.
378 // The math is done out of order, but it should not matter. That is
379 // we may add in the sizeof (EFI_GUID) before we account for the OP Code.
380 // This is not an issue, since we just need the correct end result. You
381 // need to be careful using Iterator in the loop as it's intermediate value
382 // may be strange.
383 //
384 Iterator++;
385 }
386
387 Done:
388 return FALSE;
389 }