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