psparse.c revision 1.1.1.6 1 /******************************************************************************
2 *
3 * Module Name: psparse - Parser top level AML parse routines
4 *
5 *****************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2015, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
44 /*
45 * Parse the AML and build an operation tree as most interpreters,
46 * like Perl, do. Parsing is done by hand rather than with a YACC
47 * generated parser to tightly constrain stack and dynamic memory
48 * usage. At the same time, parsing is kept flexible and the code
49 * fairly compact by parsing based on a list of AML opcode
50 * templates in AmlOpInfo[]
51 */
52
53 #include "acpi.h"
54 #include "accommon.h"
55 #include "acparser.h"
56 #include "acdispat.h"
57 #include "amlcode.h"
58 #include "acinterp.h"
59
60 #define _COMPONENT ACPI_PARSER
61 ACPI_MODULE_NAME ("psparse")
62
63
64 /*******************************************************************************
65 *
66 * FUNCTION: AcpiPsGetOpcodeSize
67 *
68 * PARAMETERS: Opcode - An AML opcode
69 *
70 * RETURN: Size of the opcode, in bytes (1 or 2)
71 *
72 * DESCRIPTION: Get the size of the current opcode.
73 *
74 ******************************************************************************/
75
76 UINT32
77 AcpiPsGetOpcodeSize (
78 UINT32 Opcode)
79 {
80
81 /* Extended (2-byte) opcode if > 255 */
82
83 if (Opcode > 0x00FF)
84 {
85 return (2);
86 }
87
88 /* Otherwise, just a single byte opcode */
89
90 return (1);
91 }
92
93
94 /*******************************************************************************
95 *
96 * FUNCTION: AcpiPsPeekOpcode
97 *
98 * PARAMETERS: ParserState - A parser state object
99 *
100 * RETURN: Next AML opcode
101 *
102 * DESCRIPTION: Get next AML opcode (without incrementing AML pointer)
103 *
104 ******************************************************************************/
105
106 UINT16
107 AcpiPsPeekOpcode (
108 ACPI_PARSE_STATE *ParserState)
109 {
110 UINT8 *Aml;
111 UINT16 Opcode;
112
113
114 Aml = ParserState->Aml;
115 Opcode = (UINT16) ACPI_GET8 (Aml);
116
117 if (Opcode == AML_EXTENDED_OP_PREFIX)
118 {
119 /* Extended opcode, get the second opcode byte */
120
121 Aml++;
122 Opcode = (UINT16) ((Opcode << 8) | ACPI_GET8 (Aml));
123 }
124
125 return (Opcode);
126 }
127
128
129 /*******************************************************************************
130 *
131 * FUNCTION: AcpiPsCompleteThisOp
132 *
133 * PARAMETERS: WalkState - Current State
134 * Op - Op to complete
135 *
136 * RETURN: Status
137 *
138 * DESCRIPTION: Perform any cleanup at the completion of an Op.
139 *
140 ******************************************************************************/
141
142 ACPI_STATUS
143 AcpiPsCompleteThisOp (
144 ACPI_WALK_STATE *WalkState,
145 ACPI_PARSE_OBJECT *Op)
146 {
147 ACPI_PARSE_OBJECT *Prev;
148 ACPI_PARSE_OBJECT *Next;
149 const ACPI_OPCODE_INFO *ParentInfo;
150 ACPI_PARSE_OBJECT *ReplacementOp = NULL;
151 ACPI_STATUS Status = AE_OK;
152
153
154 ACPI_FUNCTION_TRACE_PTR (PsCompleteThisOp, Op);
155
156
157 /* Check for null Op, can happen if AML code is corrupt */
158
159 if (!Op)
160 {
161 return_ACPI_STATUS (AE_OK); /* OK for now */
162 }
163
164 AcpiExStopTraceOpcode (Op, WalkState);
165
166 /* Delete this op and the subtree below it if asked to */
167
168 if (((WalkState->ParseFlags & ACPI_PARSE_TREE_MASK) != ACPI_PARSE_DELETE_TREE) ||
169 (WalkState->OpInfo->Class == AML_CLASS_ARGUMENT))
170 {
171 return_ACPI_STATUS (AE_OK);
172 }
173
174 /* Make sure that we only delete this subtree */
175
176 if (Op->Common.Parent)
177 {
178 Prev = Op->Common.Parent->Common.Value.Arg;
179 if (!Prev)
180 {
181 /* Nothing more to do */
182
183 goto Cleanup;
184 }
185
186 /*
187 * Check if we need to replace the operator and its subtree
188 * with a return value op (placeholder op)
189 */
190 ParentInfo = AcpiPsGetOpcodeInfo (Op->Common.Parent->Common.AmlOpcode);
191
192 switch (ParentInfo->Class)
193 {
194 case AML_CLASS_CONTROL:
195
196 break;
197
198 case AML_CLASS_CREATE:
199 /*
200 * These opcodes contain TermArg operands. The current
201 * op must be replaced by a placeholder return op
202 */
203 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP,
204 Op->Common.Aml);
205 if (!ReplacementOp)
206 {
207 Status = AE_NO_MEMORY;
208 }
209 break;
210
211 case AML_CLASS_NAMED_OBJECT:
212 /*
213 * These opcodes contain TermArg operands. The current
214 * op must be replaced by a placeholder return op
215 */
216 if ((Op->Common.Parent->Common.AmlOpcode == AML_REGION_OP) ||
217 (Op->Common.Parent->Common.AmlOpcode == AML_DATA_REGION_OP) ||
218 (Op->Common.Parent->Common.AmlOpcode == AML_BUFFER_OP) ||
219 (Op->Common.Parent->Common.AmlOpcode == AML_PACKAGE_OP) ||
220 (Op->Common.Parent->Common.AmlOpcode == AML_BANK_FIELD_OP) ||
221 (Op->Common.Parent->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
222 {
223 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP,
224 Op->Common.Aml);
225 if (!ReplacementOp)
226 {
227 Status = AE_NO_MEMORY;
228 }
229 }
230 else if ((Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
231 (WalkState->PassNumber <= ACPI_IMODE_LOAD_PASS2))
232 {
233 if ((Op->Common.AmlOpcode == AML_BUFFER_OP) ||
234 (Op->Common.AmlOpcode == AML_PACKAGE_OP) ||
235 (Op->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
236 {
237 ReplacementOp = AcpiPsAllocOp (Op->Common.AmlOpcode,
238 Op->Common.Aml);
239 if (!ReplacementOp)
240 {
241 Status = AE_NO_MEMORY;
242 }
243 else
244 {
245 ReplacementOp->Named.Data = Op->Named.Data;
246 ReplacementOp->Named.Length = Op->Named.Length;
247 }
248 }
249 }
250 break;
251
252 default:
253
254 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP,
255 Op->Common.Aml);
256 if (!ReplacementOp)
257 {
258 Status = AE_NO_MEMORY;
259 }
260 }
261
262 /* We must unlink this op from the parent tree */
263
264 if (Prev == Op)
265 {
266 /* This op is the first in the list */
267
268 if (ReplacementOp)
269 {
270 ReplacementOp->Common.Parent = Op->Common.Parent;
271 ReplacementOp->Common.Value.Arg = NULL;
272 ReplacementOp->Common.Node = Op->Common.Node;
273 Op->Common.Parent->Common.Value.Arg = ReplacementOp;
274 ReplacementOp->Common.Next = Op->Common.Next;
275 }
276 else
277 {
278 Op->Common.Parent->Common.Value.Arg = Op->Common.Next;
279 }
280 }
281
282 /* Search the parent list */
283
284 else while (Prev)
285 {
286 /* Traverse all siblings in the parent's argument list */
287
288 Next = Prev->Common.Next;
289 if (Next == Op)
290 {
291 if (ReplacementOp)
292 {
293 ReplacementOp->Common.Parent = Op->Common.Parent;
294 ReplacementOp->Common.Value.Arg = NULL;
295 ReplacementOp->Common.Node = Op->Common.Node;
296 Prev->Common.Next = ReplacementOp;
297 ReplacementOp->Common.Next = Op->Common.Next;
298 Next = NULL;
299 }
300 else
301 {
302 Prev->Common.Next = Op->Common.Next;
303 Next = NULL;
304 }
305 }
306 Prev = Next;
307 }
308 }
309
310
311 Cleanup:
312
313 /* Now we can actually delete the subtree rooted at Op */
314
315 AcpiPsDeleteParseTree (Op);
316 return_ACPI_STATUS (Status);
317 }
318
319
320 /*******************************************************************************
321 *
322 * FUNCTION: AcpiPsNextParseState
323 *
324 * PARAMETERS: WalkState - Current state
325 * Op - Current parse op
326 * CallbackStatus - Status from previous operation
327 *
328 * RETURN: Status
329 *
330 * DESCRIPTION: Update the parser state based upon the return exception from
331 * the parser callback.
332 *
333 ******************************************************************************/
334
335 ACPI_STATUS
336 AcpiPsNextParseState (
337 ACPI_WALK_STATE *WalkState,
338 ACPI_PARSE_OBJECT *Op,
339 ACPI_STATUS CallbackStatus)
340 {
341 ACPI_PARSE_STATE *ParserState = &WalkState->ParserState;
342 ACPI_STATUS Status = AE_CTRL_PENDING;
343
344
345 ACPI_FUNCTION_TRACE_PTR (PsNextParseState, Op);
346
347
348 switch (CallbackStatus)
349 {
350 case AE_CTRL_TERMINATE:
351 /*
352 * A control method was terminated via a RETURN statement.
353 * The walk of this method is complete.
354 */
355 ParserState->Aml = ParserState->AmlEnd;
356 Status = AE_CTRL_TERMINATE;
357 break;
358
359 case AE_CTRL_BREAK:
360
361 ParserState->Aml = WalkState->AmlLastWhile;
362 WalkState->ControlState->Common.Value = FALSE;
363 Status = AE_CTRL_BREAK;
364 break;
365
366 case AE_CTRL_CONTINUE:
367
368 ParserState->Aml = WalkState->AmlLastWhile;
369 Status = AE_CTRL_CONTINUE;
370 break;
371
372 case AE_CTRL_PENDING:
373
374 ParserState->Aml = WalkState->AmlLastWhile;
375 break;
376
377 #if 0
378 case AE_CTRL_SKIP:
379
380 ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
381 Status = AE_OK;
382 break;
383 #endif
384
385 case AE_CTRL_TRUE:
386 /*
387 * Predicate of an IF was true, and we are at the matching ELSE.
388 * Just close out this package
389 */
390 ParserState->Aml = AcpiPsGetNextPackageEnd (ParserState);
391 Status = AE_CTRL_PENDING;
392 break;
393
394 case AE_CTRL_FALSE:
395 /*
396 * Either an IF/WHILE Predicate was false or we encountered a BREAK
397 * opcode. In both cases, we do not execute the rest of the
398 * package; We simply close out the parent (finishing the walk of
399 * this branch of the tree) and continue execution at the parent
400 * level.
401 */
402 ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
403
404 /* In the case of a BREAK, just force a predicate (if any) to FALSE */
405
406 WalkState->ControlState->Common.Value = FALSE;
407 Status = AE_CTRL_END;
408 break;
409
410 case AE_CTRL_TRANSFER:
411
412 /* A method call (invocation) -- transfer control */
413
414 Status = AE_CTRL_TRANSFER;
415 WalkState->PrevOp = Op;
416 WalkState->MethodCallOp = Op;
417 WalkState->MethodCallNode = (Op->Common.Value.Arg)->Common.Node;
418
419 /* Will return value (if any) be used by the caller? */
420
421 WalkState->ReturnUsed = AcpiDsIsResultUsed (Op, WalkState);
422 break;
423
424 default:
425
426 Status = CallbackStatus;
427 if ((CallbackStatus & AE_CODE_MASK) == AE_CODE_CONTROL)
428 {
429 Status = AE_OK;
430 }
431 break;
432 }
433
434 return_ACPI_STATUS (Status);
435 }
436
437
438 /*******************************************************************************
439 *
440 * FUNCTION: AcpiPsParseAml
441 *
442 * PARAMETERS: WalkState - Current state
443 *
444 *
445 * RETURN: Status
446 *
447 * DESCRIPTION: Parse raw AML and return a tree of ops
448 *
449 ******************************************************************************/
450
451 ACPI_STATUS
452 AcpiPsParseAml (
453 ACPI_WALK_STATE *WalkState)
454 {
455 ACPI_STATUS Status;
456 ACPI_THREAD_STATE *Thread;
457 ACPI_THREAD_STATE *PrevWalkList = AcpiGbl_CurrentWalkList;
458 ACPI_WALK_STATE *PreviousWalkState;
459
460
461 ACPI_FUNCTION_TRACE (PsParseAml);
462
463 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
464 "Entered with WalkState=%p Aml=%p size=%X\n",
465 WalkState, WalkState->ParserState.Aml,
466 WalkState->ParserState.AmlSize));
467
468 if (!WalkState->ParserState.Aml)
469 {
470 return_ACPI_STATUS (AE_NULL_OBJECT);
471 }
472
473 /* Create and initialize a new thread state */
474
475 Thread = AcpiUtCreateThreadState ();
476 if (!Thread)
477 {
478 if (WalkState->MethodDesc)
479 {
480 /* Executing a control method - additional cleanup */
481
482 AcpiDsTerminateControlMethod (WalkState->MethodDesc, WalkState);
483 }
484
485 AcpiDsDeleteWalkState (WalkState);
486 return_ACPI_STATUS (AE_NO_MEMORY);
487 }
488
489 WalkState->Thread = Thread;
490
491 /*
492 * If executing a method, the starting SyncLevel is this method's
493 * SyncLevel
494 */
495 if (WalkState->MethodDesc)
496 {
497 WalkState->Thread->CurrentSyncLevel = WalkState->MethodDesc->Method.SyncLevel;
498 }
499
500 AcpiDsPushWalkState (WalkState, Thread);
501
502 /*
503 * This global allows the AML debugger to get a handle to the currently
504 * executing control method.
505 */
506 AcpiGbl_CurrentWalkList = Thread;
507
508 /*
509 * Execute the walk loop as long as there is a valid Walk State. This
510 * handles nested control method invocations without recursion.
511 */
512 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "State=%p\n", WalkState));
513
514 Status = AE_OK;
515 while (WalkState)
516 {
517 if (ACPI_SUCCESS (Status))
518 {
519 /*
520 * The ParseLoop executes AML until the method terminates
521 * or calls another method.
522 */
523 Status = AcpiPsParseLoop (WalkState);
524 }
525
526 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
527 "Completed one call to walk loop, %s State=%p\n",
528 AcpiFormatException (Status), WalkState));
529
530 if (Status == AE_CTRL_TRANSFER)
531 {
532 /*
533 * A method call was detected.
534 * Transfer control to the called control method
535 */
536 Status = AcpiDsCallControlMethod (Thread, WalkState, NULL);
537 if (ACPI_FAILURE (Status))
538 {
539 Status = AcpiDsMethodError (Status, WalkState);
540 }
541
542 /*
543 * If the transfer to the new method method call worked, a new walk
544 * state was created -- get it
545 */
546 WalkState = AcpiDsGetCurrentWalkState (Thread);
547 continue;
548 }
549 else if (Status == AE_CTRL_TERMINATE)
550 {
551 Status = AE_OK;
552 }
553 else if ((Status != AE_OK) && (WalkState->MethodDesc))
554 {
555 /* Either the method parse or actual execution failed */
556
557 ACPI_ERROR_METHOD ("Method parse/execution failed",
558 WalkState->MethodNode, NULL, Status);
559
560 /* Check for possible multi-thread reentrancy problem */
561
562 if ((Status == AE_ALREADY_EXISTS) &&
563 (!(WalkState->MethodDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED)))
564 {
565 /*
566 * Method is not serialized and tried to create an object
567 * twice. The probable cause is that the method cannot
568 * handle reentrancy. Mark as "pending serialized" now, and
569 * then mark "serialized" when the last thread exits.
570 */
571 WalkState->MethodDesc->Method.InfoFlags |=
572 ACPI_METHOD_SERIALIZED_PENDING;
573 }
574 }
575
576 /* We are done with this walk, move on to the parent if any */
577
578 WalkState = AcpiDsPopWalkState (Thread);
579
580 /* Reset the current scope to the beginning of scope stack */
581
582 AcpiDsScopeStackClear (WalkState);
583
584 /*
585 * If we just returned from the execution of a control method or if we
586 * encountered an error during the method parse phase, there's lots of
587 * cleanup to do
588 */
589 if (((WalkState->ParseFlags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE) ||
590 (ACPI_FAILURE (Status)))
591 {
592 AcpiDsTerminateControlMethod (WalkState->MethodDesc, WalkState);
593 }
594
595 /* Delete this walk state and all linked control states */
596
597 AcpiPsCleanupScope (&WalkState->ParserState);
598 PreviousWalkState = WalkState;
599
600 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
601 "ReturnValue=%p, ImplicitValue=%p State=%p\n",
602 WalkState->ReturnDesc, WalkState->ImplicitReturnObj, WalkState));
603
604 /* Check if we have restarted a preempted walk */
605
606 WalkState = AcpiDsGetCurrentWalkState (Thread);
607 if (WalkState)
608 {
609 if (ACPI_SUCCESS (Status))
610 {
611 /*
612 * There is another walk state, restart it.
613 * If the method return value is not used by the parent,
614 * The object is deleted
615 */
616 if (!PreviousWalkState->ReturnDesc)
617 {
618 /*
619 * In slack mode execution, if there is no return value
620 * we should implicitly return zero (0) as a default value.
621 */
622 if (AcpiGbl_EnableInterpreterSlack &&
623 !PreviousWalkState->ImplicitReturnObj)
624 {
625 PreviousWalkState->ImplicitReturnObj =
626 AcpiUtCreateIntegerObject ((UINT64) 0);
627 if (!PreviousWalkState->ImplicitReturnObj)
628 {
629 return_ACPI_STATUS (AE_NO_MEMORY);
630 }
631 }
632
633 /* Restart the calling control method */
634
635 Status = AcpiDsRestartControlMethod (WalkState,
636 PreviousWalkState->ImplicitReturnObj);
637 }
638 else
639 {
640 /*
641 * We have a valid return value, delete any implicit
642 * return value.
643 */
644 AcpiDsClearImplicitReturn (PreviousWalkState);
645
646 Status = AcpiDsRestartControlMethod (WalkState,
647 PreviousWalkState->ReturnDesc);
648 }
649 if (ACPI_SUCCESS (Status))
650 {
651 WalkState->WalkType |= ACPI_WALK_METHOD_RESTART;
652 }
653 }
654 else
655 {
656 /* On error, delete any return object or implicit return */
657
658 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
659 AcpiDsClearImplicitReturn (PreviousWalkState);
660 }
661 }
662
663 /*
664 * Just completed a 1st-level method, save the final internal return
665 * value (if any)
666 */
667 else if (PreviousWalkState->CallerReturnDesc)
668 {
669 if (PreviousWalkState->ImplicitReturnObj)
670 {
671 *(PreviousWalkState->CallerReturnDesc) =
672 PreviousWalkState->ImplicitReturnObj;
673 }
674 else
675 {
676 /* NULL if no return value */
677
678 *(PreviousWalkState->CallerReturnDesc) =
679 PreviousWalkState->ReturnDesc;
680 }
681 }
682 else
683 {
684 if (PreviousWalkState->ReturnDesc)
685 {
686 /* Caller doesn't want it, must delete it */
687
688 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
689 }
690 if (PreviousWalkState->ImplicitReturnObj)
691 {
692 /* Caller doesn't want it, must delete it */
693
694 AcpiUtRemoveReference (PreviousWalkState->ImplicitReturnObj);
695 }
696 }
697
698 AcpiDsDeleteWalkState (PreviousWalkState);
699 }
700
701 /* Normal exit */
702
703 AcpiExReleaseAllMutexes (Thread);
704 AcpiUtDeleteGenericState (ACPI_CAST_PTR (ACPI_GENERIC_STATE, Thread));
705 AcpiGbl_CurrentWalkList = PrevWalkList;
706 return_ACPI_STATUS (Status);
707 }
708