]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/Dependency.c
Update the copyright notice format
[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
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 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter ()\r
192 // processes them.\r
193 //\r
194 return FALSE;\r
195 }\r
196\r
197 if (DriverEntry->Depex == NULL) {\r
198 //\r
199 // A NULL Depex means that the SMM driver is not built correctly. \r
200 // All SMM drivers must have a valid depex expressiion.\r
201 //\r
202 ASSERT (FALSE);\r
203 return FALSE;\r
204 }\r
205\r
206 //\r
207 // Clean out memory leaks in Depex Boolean stack. Leaks are only caused by\r
208 // incorrectly formed DEPEX expressions\r
209 //\r
210 mDepexEvaluationStackPointer = mDepexEvaluationStack;\r
211\r
212\r
213 Iterator = DriverEntry->Depex;\r
214\r
215 while (TRUE) {\r
216 //\r
217 // Check to see if we are attempting to fetch dependency expression instructions\r
218 // past the end of the dependency expression.\r
219 //\r
220 if (((UINTN)Iterator - (UINTN)DriverEntry->Depex) >= DriverEntry->DepexSize) {\r
221 return FALSE;\r
222 }\r
223\r
224 //\r
225 // Look at the opcode of the dependency expression instruction.\r
226 //\r
227 switch (*Iterator) {\r
228 case EFI_DEP_BEFORE:\r
229 case EFI_DEP_AFTER:\r
230 //\r
231 // For a well-formed Dependency Expression, the code should never get here.\r
232 // The BEFORE and AFTER are processed prior to this routine's invocation.\r
233 // If the code flow arrives at this point, there was a BEFORE or AFTER\r
234 // that were not the first opcodes.\r
235 //\r
236 ASSERT (FALSE);\r
237 case EFI_DEP_SOR:\r
238 //\r
239 // These opcodes can only appear once as the first opcode. If it is found\r
240 // at any other location, then the dependency expression evaluates to FALSE\r
241 //\r
242 if (Iterator != DriverEntry->Depex) {\r
243 return FALSE;\r
244 }\r
245 //\r
246 // Otherwise, it is the first opcode and should be treated as a NOP.\r
247 //\r
248 break;\r
249\r
250 case EFI_DEP_PUSH:\r
251 //\r
252 // Push operator is followed by a GUID. Test to see if the GUID protocol\r
253 // is installed and push the boolean result on the stack.\r
254 //\r
255 CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));\r
256\r
257 Status = SmmLocateProtocol (&DriverGuid, NULL, &Interface);\r
258 if (EFI_ERROR (Status)) {\r
259 //\r
260 // For SMM Driver, it may depend on uefi protocols\r
261 //\r
262 Status = gBS->LocateProtocol (&DriverGuid, NULL, &Interface);\r
263 }\r
264\r
265 if (EFI_ERROR (Status)) {\r
266 Status = PushBool (FALSE);\r
267 } else {\r
268 *Iterator = EFI_DEP_REPLACE_TRUE;\r
269 Status = PushBool (TRUE);\r
270 }\r
271 if (EFI_ERROR (Status)) {\r
272 return FALSE;\r
273 }\r
274\r
275 Iterator += sizeof (EFI_GUID);\r
276 break;\r
277\r
278 case EFI_DEP_AND:\r
279 Status = PopBool (&Operator);\r
280 if (EFI_ERROR (Status)) {\r
281 return FALSE;\r
282 }\r
283\r
284 Status = PopBool (&Operator2);\r
285 if (EFI_ERROR (Status)) {\r
286 return FALSE;\r
287 }\r
288\r
289 Status = PushBool ((BOOLEAN)(Operator && Operator2));\r
290 if (EFI_ERROR (Status)) {\r
291 return FALSE;\r
292 }\r
293 break;\r
294\r
295 case EFI_DEP_OR:\r
296 Status = PopBool (&Operator);\r
297 if (EFI_ERROR (Status)) {\r
298 return FALSE;\r
299 }\r
300\r
301 Status = PopBool (&Operator2);\r
302 if (EFI_ERROR (Status)) {\r
303 return FALSE;\r
304 }\r
305\r
306 Status = PushBool ((BOOLEAN)(Operator || Operator2));\r
307 if (EFI_ERROR (Status)) {\r
308 return FALSE;\r
309 }\r
310 break;\r
311\r
312 case EFI_DEP_NOT:\r
313 Status = PopBool (&Operator);\r
314 if (EFI_ERROR (Status)) {\r
315 return FALSE;\r
316 }\r
317\r
318 Status = PushBool ((BOOLEAN)(!Operator));\r
319 if (EFI_ERROR (Status)) {\r
320 return FALSE;\r
321 }\r
322 break;\r
323\r
324 case EFI_DEP_TRUE:\r
325 Status = PushBool (TRUE);\r
326 if (EFI_ERROR (Status)) {\r
327 return FALSE;\r
328 }\r
329 break;\r
330\r
331 case EFI_DEP_FALSE:\r
332 Status = PushBool (FALSE);\r
333 if (EFI_ERROR (Status)) {\r
334 return FALSE;\r
335 }\r
336 break;\r
337\r
338 case EFI_DEP_END:\r
339 Status = PopBool (&Operator);\r
340 if (EFI_ERROR (Status)) {\r
341 return FALSE;\r
342 }\r
343 return Operator;\r
344\r
345 case EFI_DEP_REPLACE_TRUE:\r
346 Status = PushBool (TRUE);\r
347 if (EFI_ERROR (Status)) {\r
348 return FALSE;\r
349 }\r
350\r
351 Iterator += sizeof (EFI_GUID);\r
352 break;\r
353\r
354 default:\r
355 goto Done;\r
356 }\r
357\r
358 //\r
359 // Skip over the Dependency Op Code we just processed in the switch.\r
360 // The math is done out of order, but it should not matter. That is\r
361 // we may add in the sizeof (EFI_GUID) before we account for the OP Code.\r
362 // This is not an issue, since we just need the correct end result. You\r
363 // need to be careful using Iterator in the loop as it's intermediate value\r
364 // may be strange.\r
365 //\r
366 Iterator++;\r
367 }\r
368\r
369Done:\r
370 return FALSE;\r
371}\r