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