]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/Dependency.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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
1436aea4
MK
49 BOOLEAN *NewStack;\r
50 UINTN Size;\r
e42e9404 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
1436aea4 169 IN EFI_SMM_DRIVER_ENTRY *DriverEntry\r
e42e9404 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
1436aea4 179 Operator = FALSE;\r
e42e9404 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
e42e9404 208 Iterator = DriverEntry->Depex;\r
209\r
210 while (TRUE) {\r
211 //\r
212 // Check to see if we are attempting to fetch dependency expression instructions\r
213 // past the end of the dependency expression.\r
214 //\r
215 if (((UINTN)Iterator - (UINTN)DriverEntry->Depex) >= DriverEntry->DepexSize) {\r
6a55eea3 216 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Attempt to fetch past end of depex)\n"));\r
e42e9404 217 return FALSE;\r
218 }\r
219\r
220 //\r
221 // Look at the opcode of the dependency expression instruction.\r
222 //\r
223 switch (*Iterator) {\r
1436aea4
MK
224 case EFI_DEP_BEFORE:\r
225 case EFI_DEP_AFTER:\r
226 //\r
227 // For a well-formed Dependency Expression, the code should never get here.\r
228 // The BEFORE and AFTER are processed prior to this routine's invocation.\r
229 // If the code flow arrives at this point, there was a BEFORE or AFTER\r
230 // that were not the first opcodes.\r
231 //\r
232 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected BEFORE or AFTER opcode)\n"));\r
233 ASSERT (FALSE);\r
234\r
235 case EFI_DEP_PUSH:\r
e42e9404 236 //\r
1436aea4
MK
237 // Push operator is followed by a GUID. Test to see if the GUID protocol\r
238 // is installed and push the boolean result on the stack.\r
e42e9404 239 //\r
1436aea4
MK
240 CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));\r
241\r
242 Status = SmmLocateProtocol (&DriverGuid, NULL, &Interface);\r
243 if (EFI_ERROR (Status)) {\r
244 //\r
245 // For SMM Driver, it may depend on uefi protocols\r
246 //\r
247 Status = gBS->LocateProtocol (&DriverGuid, NULL, &Interface);\r
248 }\r
249\r
250 if (EFI_ERROR (Status)) {\r
251 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = FALSE\n", &DriverGuid));\r
252 Status = PushBool (FALSE);\r
253 } else {\r
254 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = TRUE\n", &DriverGuid));\r
255 *Iterator = EFI_DEP_REPLACE_TRUE;\r
256 Status = PushBool (TRUE);\r
257 }\r
258\r
259 if (EFI_ERROR (Status)) {\r
260 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
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
268 DEBUG ((DEBUG_DISPATCH, " AND\n"));\r
269 Status = PopBool (&Operator);\r
270 if (EFI_ERROR (Status)) {\r
271 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
272 return FALSE;\r
273 }\r
274\r
275 Status = PopBool (&Operator2);\r
276 if (EFI_ERROR (Status)) {\r
277 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
278 return FALSE;\r
279 }\r
280\r
281 Status = PushBool ((BOOLEAN)(Operator && Operator2));\r
282 if (EFI_ERROR (Status)) {\r
283 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
284 return FALSE;\r
285 }\r
286\r
287 break;\r
288\r
289 case EFI_DEP_OR:\r
290 DEBUG ((DEBUG_DISPATCH, " OR\n"));\r
291 Status = PopBool (&Operator);\r
292 if (EFI_ERROR (Status)) {\r
293 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
294 return FALSE;\r
295 }\r
296\r
297 Status = PopBool (&Operator2);\r
298 if (EFI_ERROR (Status)) {\r
299 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
300 return FALSE;\r
301 }\r
302\r
303 Status = PushBool ((BOOLEAN)(Operator || Operator2));\r
304 if (EFI_ERROR (Status)) {\r
305 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
306 return FALSE;\r
307 }\r
308\r
309 break;\r
310\r
311 case EFI_DEP_NOT:\r
312 DEBUG ((DEBUG_DISPATCH, " NOT\n"));\r
313 Status = PopBool (&Operator);\r
314 if (EFI_ERROR (Status)) {\r
315 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
316 return FALSE;\r
317 }\r
318\r
319 Status = PushBool ((BOOLEAN)(!Operator));\r
320 if (EFI_ERROR (Status)) {\r
321 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
322 return FALSE;\r
323 }\r
324\r
325 break;\r
326\r
327 case EFI_DEP_TRUE:\r
328 DEBUG ((DEBUG_DISPATCH, " TRUE\n"));\r
329 Status = PushBool (TRUE);\r
330 if (EFI_ERROR (Status)) {\r
331 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
332 return FALSE;\r
333 }\r
334\r
335 break;\r
e42e9404 336\r
1436aea4
MK
337 case EFI_DEP_FALSE:\r
338 DEBUG ((DEBUG_DISPATCH, " FALSE\n"));\r
e42e9404 339 Status = PushBool (FALSE);\r
1436aea4
MK
340 if (EFI_ERROR (Status)) {\r
341 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
342 return FALSE;\r
343 }\r
344\r
345 break;\r
346\r
347 case EFI_DEP_END:\r
348 DEBUG ((DEBUG_DISPATCH, " END\n"));\r
349 Status = PopBool (&Operator);\r
350 if (EFI_ERROR (Status)) {\r
351 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
352 return FALSE;\r
353 }\r
354\r
355 DEBUG ((DEBUG_DISPATCH, " RESULT = %a\n", Operator ? "TRUE" : "FALSE"));\r
356 return Operator;\r
357\r
358 case EFI_DEP_REPLACE_TRUE:\r
359 CopyMem (&DriverGuid, Iterator + 1, sizeof (EFI_GUID));\r
6a55eea3 360 DEBUG ((DEBUG_DISPATCH, " PUSH GUID(%g) = TRUE\n", &DriverGuid));\r
e42e9404 361 Status = PushBool (TRUE);\r
1436aea4
MK
362 if (EFI_ERROR (Status)) {\r
363 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unexpected error)\n"));\r
364 return FALSE;\r
365 }\r
366\r
367 Iterator += sizeof (EFI_GUID);\r
368 break;\r
369\r
370 default:\r
371 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE (Unknown opcode)\n"));\r
372 goto Done;\r
e42e9404 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
4be497df 380 // need to be careful using Iterator in the loop as its intermediate value\r
e42e9404 381 // may be strange.\r
382 //\r
383 Iterator++;\r
384 }\r
385\r
386Done:\r
387 return FALSE;\r
388}\r