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