]> git.proxmox.com Git - mirror_edk2.git/blame - StandaloneMmPkg/Core/Dependency.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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
da29388b 6 scheduling is that the dependency expression is satisfied.\r
6b46d772
SV
7\r
8 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
da29388b 9 Copyright (c) 2016 - 2021, 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
91415a36
MK
50 BOOLEAN *NewStack;\r
51 UINTN Size;\r
6b46d772
SV
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
91415a36 170 IN EFI_MM_DRIVER_ENTRY *DriverEntry\r
6b46d772
SV
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
91415a36 180 Operator = FALSE;\r
6b46d772
SV
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
da29388b 196 // All MM drivers must have a valid depex expression.\r
6b46d772
SV
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
6b46d772
SV
209 Iterator = DriverEntry->Depex;\r
210\r
211 while (TRUE) {\r
212 //\r
213 // Check to see if we are attempting to fetch dependency expression instructions\r
214 // past the end of the dependency expression.\r
215 //\r
216 if (((UINTN)Iterator - (UINTN)DriverEntry->Depex) >= DriverEntry->DepexSize) {\r
217 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Attempt to fetch past end of depex)\n"));\r
218 return FALSE;\r
219 }\r
220\r
221 //\r
222 // Look at the opcode of the dependency expression instruction.\r
223 //\r
224 switch (*Iterator) {\r
91415a36
MK
225 case EFI_DEP_BEFORE:\r
226 case EFI_DEP_AFTER:\r
227 //\r
228 // For a well-formed Dependency Expression, the code should never get here.\r
229 // The BEFORE and AFTER are processed prior to this routine's invocation.\r
230 // If the code flow arrives at this point, there was a BEFORE or AFTER\r
231 // that were not the first opcodes.\r
232 //\r
233 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));\r
234 ASSERT (FALSE);\r
235\r
236 case EFI_DEP_PUSH:\r
6b46d772 237 //\r
91415a36
MK
238 // Push operator is followed by a GUID. Test to see if the GUID protocol\r
239 // is installed and push the boolean result on the stack.\r
6b46d772 240 //\r
91415a36
MK
241 CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));\r
242\r
243 Status = MmLocateProtocol (&DriverGuid, NULL, &Interface);\r
244 if (EFI_ERROR (Status) && (mEfiSystemTable != NULL)) {\r
245 //\r
246 // For MM Driver, it may depend on uefi protocols\r
247 //\r
248 Status = mEfiSystemTable->BootServices->LocateProtocol (&DriverGuid, NULL, &Interface);\r
249 }\r
250\r
251 if (EFI_ERROR (Status)) {\r
252 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = FALSE\n", &DriverGuid));\r
253 Status = PushBool (FALSE);\r
254 } else {\r
255 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = TRUE\n", &DriverGuid));\r
256 *Iterator = EFI_DEP_REPLACE_TRUE;\r
257 Status = PushBool (TRUE);\r
258 }\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\r
288 break;\r
289\r
290 case EFI_DEP_OR:\r
291 DEBUG ((DEBUG_DISPATCH, " OR\n"));\r
292 Status = PopBool (&Operator);\r
293 if (EFI_ERROR (Status)) {\r
294 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
295 return FALSE;\r
296 }\r
297\r
298 Status = PopBool (&Operator2);\r
299 if (EFI_ERROR (Status)) {\r
300 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
301 return FALSE;\r
302 }\r
303\r
304 Status = PushBool ((BOOLEAN)(Operator || Operator2));\r
305 if (EFI_ERROR (Status)) {\r
306 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
307 return FALSE;\r
308 }\r
309\r
310 break;\r
311\r
312 case EFI_DEP_NOT:\r
313 DEBUG ((DEBUG_DISPATCH, " NOT\n"));\r
314 Status = PopBool (&Operator);\r
315 if (EFI_ERROR (Status)) {\r
316 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
317 return FALSE;\r
318 }\r
319\r
320 Status = PushBool ((BOOLEAN)(!Operator));\r
321 if (EFI_ERROR (Status)) {\r
322 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
323 return FALSE;\r
324 }\r
325\r
326 break;\r
327\r
328 case EFI_DEP_TRUE:\r
329 DEBUG ((DEBUG_DISPATCH, " TRUE\n"));\r
330 Status = PushBool (TRUE);\r
331 if (EFI_ERROR (Status)) {\r
332 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
333 return FALSE;\r
334 }\r
335\r
336 break;\r
6b46d772 337\r
91415a36
MK
338 case EFI_DEP_FALSE:\r
339 DEBUG ((DEBUG_DISPATCH, " FALSE\n"));\r
6b46d772 340 Status = PushBool (FALSE);\r
91415a36
MK
341 if (EFI_ERROR (Status)) {\r
342 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
343 return FALSE;\r
344 }\r
345\r
346 break;\r
347\r
348 case EFI_DEP_END:\r
349 DEBUG ((DEBUG_DISPATCH, " END\n"));\r
350 Status = PopBool (&Operator);\r
351 if (EFI_ERROR (Status)) {\r
352 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
353 return FALSE;\r
354 }\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
6b46d772 361 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = TRUE\n", &DriverGuid));\r
6b46d772 362 Status = PushBool (TRUE);\r
91415a36
MK
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
6b46d772
SV
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
2a0755a9 381 // need to be careful using Iterator in the loop as its intermediate value\r
6b46d772
SV
382 // may be strange.\r
383 //\r
384 Iterator++;\r
385 }\r
386\r
387Done:\r
388 return FALSE;\r
389}\r