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