]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/Dependency.c
Update PEI/DXE/SMM dispatchers to include DEBUG ((DEBUG_DISPATCH, )) macros to log...
[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
22/// EFI_DEP_REPLACE_TRUE - Used to dynamically patch the dependecy expression\r
23/// to save time. A EFI_DEP_PUSH is evauated one an\r
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
164 routine in this case. The SOR is just ignored and is a nop in the grammer.\r
165 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
175SmmIsSchedulable (\r
176 IN EFI_SMM_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
6a55eea3 186 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
187 \r
e42e9404 188 Operator = FALSE;\r
189 Operator2 = FALSE;\r
190\r
191 if (DriverEntry->After || DriverEntry->Before) {\r
192 //\r
193 // If Before or After Depex skip as SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()\r
194 // processes them.\r
195 //\r
196 return FALSE;\r
197 }\r
198\r
199 if (DriverEntry->Depex == NULL) {\r
200 //\r
201 // A NULL Depex means that the SMM driver is not built correctly. \r
202 // All SMM drivers must have a valid depex expressiion.\r
203 //\r
6a55eea3 204 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Depex is empty)\n"));\r
e42e9404 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
6a55eea3 224 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Attempt to fetch past end of depex)\n"));\r
e42e9404 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
6a55eea3 240 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));\r
e42e9404 241 ASSERT (FALSE);\r
242 case EFI_DEP_SOR:\r
243 //\r
244 // These opcodes can only appear once as the first opcode. If it is found\r
245 // at any other location, then the dependency expression evaluates to FALSE\r
246 //\r
247 if (Iterator != DriverEntry->Depex) {\r
6a55eea3 248 DEBUG ((DEBUG_DISPATCH, " SOR\n"));\r
249 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected SOR opcode)\n"));\r
e42e9404 250 return FALSE;\r
251 }\r
6a55eea3 252 DEBUG ((DEBUG_DISPATCH, " SOR = Requested\n"));\r
e42e9404 253 //\r
254 // Otherwise, it is the first opcode and should be treated as a NOP.\r
255 //\r
256 break;\r
257\r
258 case EFI_DEP_PUSH:\r
259 //\r
260 // Push operator is followed by a GUID. Test to see if the GUID protocol\r
261 // is installed and push the boolean result on the stack.\r
262 //\r
263 CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));\r
264\r
265 Status = SmmLocateProtocol (&DriverGuid, NULL, &Interface);\r
266 if (EFI_ERROR (Status)) {\r
267 //\r
268 // For SMM Driver, it may depend on uefi protocols\r
269 //\r
270 Status = gBS->LocateProtocol (&DriverGuid, NULL, &Interface);\r
271 }\r
272\r
273 if (EFI_ERROR (Status)) {\r
6a55eea3 274 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = FALSE\n", &DriverGuid));\r
e42e9404 275 Status = PushBool (FALSE);\r
276 } else {\r
6a55eea3 277 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = TRUE\n", &DriverGuid));\r
e42e9404 278 *Iterator = EFI_DEP_REPLACE_TRUE;\r
279 Status = PushBool (TRUE);\r
280 }\r
281 if (EFI_ERROR (Status)) {\r
6a55eea3 282 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
e42e9404 283 return FALSE;\r
284 }\r
285\r
286 Iterator += sizeof (EFI_GUID);\r
287 break;\r
288\r
289 case EFI_DEP_AND:\r
6a55eea3 290 DEBUG ((DEBUG_DISPATCH, " AND\n"));\r
e42e9404 291 Status = PopBool (&Operator);\r
292 if (EFI_ERROR (Status)) {\r
6a55eea3 293 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
e42e9404 294 return FALSE;\r
295 }\r
296\r
297 Status = PopBool (&Operator2);\r
298 if (EFI_ERROR (Status)) {\r
6a55eea3 299 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
e42e9404 300 return FALSE;\r
301 }\r
302\r
303 Status = PushBool ((BOOLEAN)(Operator && Operator2));\r
304 if (EFI_ERROR (Status)) {\r
6a55eea3 305 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
e42e9404 306 return FALSE;\r
307 }\r
308 break;\r
309\r
310 case EFI_DEP_OR:\r
6a55eea3 311 DEBUG ((DEBUG_DISPATCH, " OR\n"));\r
e42e9404 312 Status = PopBool (&Operator);\r
313 if (EFI_ERROR (Status)) {\r
6a55eea3 314 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
e42e9404 315 return FALSE;\r
316 }\r
317\r
318 Status = PopBool (&Operator2);\r
319 if (EFI_ERROR (Status)) {\r
6a55eea3 320 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
e42e9404 321 return FALSE;\r
322 }\r
323\r
324 Status = PushBool ((BOOLEAN)(Operator || Operator2));\r
325 if (EFI_ERROR (Status)) {\r
6a55eea3 326 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
e42e9404 327 return FALSE;\r
328 }\r
329 break;\r
330\r
331 case EFI_DEP_NOT:\r
6a55eea3 332 DEBUG ((DEBUG_DISPATCH, " NOT\n"));\r
e42e9404 333 Status = PopBool (&Operator);\r
334 if (EFI_ERROR (Status)) {\r
6a55eea3 335 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
e42e9404 336 return FALSE;\r
337 }\r
338\r
339 Status = PushBool ((BOOLEAN)(!Operator));\r
340 if (EFI_ERROR (Status)) {\r
6a55eea3 341 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
e42e9404 342 return FALSE;\r
343 }\r
344 break;\r
345\r
346 case EFI_DEP_TRUE:\r
6a55eea3 347 DEBUG ((DEBUG_DISPATCH, " TRUE\n"));\r
e42e9404 348 Status = PushBool (TRUE);\r
349 if (EFI_ERROR (Status)) {\r
6a55eea3 350 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
e42e9404 351 return FALSE;\r
352 }\r
353 break;\r
354\r
355 case EFI_DEP_FALSE:\r
6a55eea3 356 DEBUG ((DEBUG_DISPATCH, " FALSE\n"));\r
e42e9404 357 Status = PushBool (FALSE);\r
358 if (EFI_ERROR (Status)) {\r
6a55eea3 359 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
e42e9404 360 return FALSE;\r
361 }\r
362 break;\r
363\r
364 case EFI_DEP_END:\r
6a55eea3 365 DEBUG ((DEBUG_DISPATCH, " END\n"));\r
e42e9404 366 Status = PopBool (&Operator);\r
367 if (EFI_ERROR (Status)) {\r
6a55eea3 368 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
e42e9404 369 return FALSE;\r
370 }\r
6a55eea3 371 DEBUG ((DEBUG_DISPATCH, " RESULT = %a\n", Operator ? "TRUE" : "FALSE"));\r
e42e9404 372 return Operator;\r
373\r
374 case EFI_DEP_REPLACE_TRUE:\r
6a55eea3 375 CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));\r
376 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = TRUE\n", &DriverGuid));\r
e42e9404 377 Status = PushBool (TRUE);\r
378 if (EFI_ERROR (Status)) {\r
6a55eea3 379 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
e42e9404 380 return FALSE;\r
381 }\r
382\r
383 Iterator += sizeof (EFI_GUID);\r
384 break;\r
385\r
386 default:\r
6a55eea3 387 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unknown opcode)\n"));\r
e42e9404 388 goto Done;\r
389 }\r
390\r
391 //\r
392 // Skip over the Dependency Op Code we just processed in the switch.\r
393 // The math is done out of order, but it should not matter. That is\r
394 // we may add in the sizeof (EFI_GUID) before we account for the OP Code.\r
395 // This is not an issue, since we just need the correct end result. You\r
396 // need to be careful using Iterator in the loop as it's intermediate value\r
397 // may be strange.\r
398 //\r
399 Iterator++;\r
400 }\r
401\r
402Done:\r
403 return FALSE;\r
404}\r