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