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