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