1 /******************************************************************************
3 * Module Name: psparse - Parser top level AML parse routines
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2011, Intel Corp.
12 * All rights reserved.
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
72 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************/
118 * Parse the AML and build an operation tree as most interpreters,
119 * like Perl, do. Parsing is done by hand rather than with a YACC
120 * generated parser to tightly constrain stack and dynamic memory
121 * usage. At the same time, parsing is kept flexible and the code
122 * fairly compact by parsing based on a list of AML opcode
123 * templates in AmlOpInfo[]
127 #include "accommon.h"
128 #include "acparser.h"
129 #include "acdispat.h"
131 #include "acinterp.h"
133 #define _COMPONENT ACPI_PARSER
134 ACPI_MODULE_NAME ("psparse")
137 /*******************************************************************************
139 * FUNCTION: AcpiPsGetOpcodeSize
141 * PARAMETERS: Opcode - An AML opcode
143 * RETURN: Size of the opcode, in bytes (1 or 2)
145 * DESCRIPTION: Get the size of the current opcode.
147 ******************************************************************************/
150 AcpiPsGetOpcodeSize (
154 /* Extended (2-byte) opcode if > 255 */
161 /* Otherwise, just a single byte opcode */
167 /*******************************************************************************
169 * FUNCTION: AcpiPsPeekOpcode
171 * PARAMETERS: ParserState - A parser state object
173 * RETURN: Next AML opcode
175 * DESCRIPTION: Get next AML opcode (without incrementing AML pointer)
177 ******************************************************************************/
181 ACPI_PARSE_STATE
*ParserState
)
187 Aml
= ParserState
->Aml
;
188 Opcode
= (UINT16
) ACPI_GET8 (Aml
);
190 if (Opcode
== AML_EXTENDED_OP_PREFIX
)
192 /* Extended opcode, get the second opcode byte */
195 Opcode
= (UINT16
) ((Opcode
<< 8) | ACPI_GET8 (Aml
));
202 /*******************************************************************************
204 * FUNCTION: AcpiPsCompleteThisOp
206 * PARAMETERS: WalkState - Current State
207 * Op - Op to complete
211 * DESCRIPTION: Perform any cleanup at the completion of an Op.
213 ******************************************************************************/
216 AcpiPsCompleteThisOp (
217 ACPI_WALK_STATE
*WalkState
,
218 ACPI_PARSE_OBJECT
*Op
)
220 ACPI_PARSE_OBJECT
*Prev
;
221 ACPI_PARSE_OBJECT
*Next
;
222 const ACPI_OPCODE_INFO
*ParentInfo
;
223 ACPI_PARSE_OBJECT
*ReplacementOp
= NULL
;
224 ACPI_STATUS Status
= AE_OK
;
227 ACPI_FUNCTION_TRACE_PTR (PsCompleteThisOp
, Op
);
230 /* Check for null Op, can happen if AML code is corrupt */
234 return_ACPI_STATUS (AE_OK
); /* OK for now */
237 /* Delete this op and the subtree below it if asked to */
239 if (((WalkState
->ParseFlags
& ACPI_PARSE_TREE_MASK
) != ACPI_PARSE_DELETE_TREE
) ||
240 (WalkState
->OpInfo
->Class
== AML_CLASS_ARGUMENT
))
242 return_ACPI_STATUS (AE_OK
);
245 /* Make sure that we only delete this subtree */
247 if (Op
->Common
.Parent
)
249 Prev
= Op
->Common
.Parent
->Common
.Value
.Arg
;
252 /* Nothing more to do */
258 * Check if we need to replace the operator and its subtree
259 * with a return value op (placeholder op)
261 ParentInfo
= AcpiPsGetOpcodeInfo (Op
->Common
.Parent
->Common
.AmlOpcode
);
263 switch (ParentInfo
->Class
)
265 case AML_CLASS_CONTROL
:
268 case AML_CLASS_CREATE
:
271 * These opcodes contain TermArg operands. The current
272 * op must be replaced by a placeholder return op
274 ReplacementOp
= AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP
);
277 Status
= AE_NO_MEMORY
;
281 case AML_CLASS_NAMED_OBJECT
:
284 * These opcodes contain TermArg operands. The current
285 * op must be replaced by a placeholder return op
287 if ((Op
->Common
.Parent
->Common
.AmlOpcode
== AML_REGION_OP
) ||
288 (Op
->Common
.Parent
->Common
.AmlOpcode
== AML_DATA_REGION_OP
) ||
289 (Op
->Common
.Parent
->Common
.AmlOpcode
== AML_BUFFER_OP
) ||
290 (Op
->Common
.Parent
->Common
.AmlOpcode
== AML_PACKAGE_OP
) ||
291 (Op
->Common
.Parent
->Common
.AmlOpcode
== AML_BANK_FIELD_OP
) ||
292 (Op
->Common
.Parent
->Common
.AmlOpcode
== AML_VAR_PACKAGE_OP
))
294 ReplacementOp
= AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP
);
297 Status
= AE_NO_MEMORY
;
300 else if ((Op
->Common
.Parent
->Common
.AmlOpcode
== AML_NAME_OP
) &&
301 (WalkState
->PassNumber
<= ACPI_IMODE_LOAD_PASS2
))
303 if ((Op
->Common
.AmlOpcode
== AML_BUFFER_OP
) ||
304 (Op
->Common
.AmlOpcode
== AML_PACKAGE_OP
) ||
305 (Op
->Common
.AmlOpcode
== AML_VAR_PACKAGE_OP
))
307 ReplacementOp
= AcpiPsAllocOp (Op
->Common
.AmlOpcode
);
310 Status
= AE_NO_MEMORY
;
314 ReplacementOp
->Named
.Data
= Op
->Named
.Data
;
315 ReplacementOp
->Named
.Length
= Op
->Named
.Length
;
323 ReplacementOp
= AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP
);
326 Status
= AE_NO_MEMORY
;
330 /* We must unlink this op from the parent tree */
334 /* This op is the first in the list */
338 ReplacementOp
->Common
.Parent
= Op
->Common
.Parent
;
339 ReplacementOp
->Common
.Value
.Arg
= NULL
;
340 ReplacementOp
->Common
.Node
= Op
->Common
.Node
;
341 Op
->Common
.Parent
->Common
.Value
.Arg
= ReplacementOp
;
342 ReplacementOp
->Common
.Next
= Op
->Common
.Next
;
346 Op
->Common
.Parent
->Common
.Value
.Arg
= Op
->Common
.Next
;
350 /* Search the parent list */
354 /* Traverse all siblings in the parent's argument list */
356 Next
= Prev
->Common
.Next
;
361 ReplacementOp
->Common
.Parent
= Op
->Common
.Parent
;
362 ReplacementOp
->Common
.Value
.Arg
= NULL
;
363 ReplacementOp
->Common
.Node
= Op
->Common
.Node
;
364 Prev
->Common
.Next
= ReplacementOp
;
365 ReplacementOp
->Common
.Next
= Op
->Common
.Next
;
370 Prev
->Common
.Next
= Op
->Common
.Next
;
381 /* Now we can actually delete the subtree rooted at Op */
383 AcpiPsDeleteParseTree (Op
);
384 return_ACPI_STATUS (Status
);
388 /*******************************************************************************
390 * FUNCTION: AcpiPsNextParseState
392 * PARAMETERS: WalkState - Current state
393 * Op - Current parse op
394 * CallbackStatus - Status from previous operation
398 * DESCRIPTION: Update the parser state based upon the return exception from
399 * the parser callback.
401 ******************************************************************************/
404 AcpiPsNextParseState (
405 ACPI_WALK_STATE
*WalkState
,
406 ACPI_PARSE_OBJECT
*Op
,
407 ACPI_STATUS CallbackStatus
)
409 ACPI_PARSE_STATE
*ParserState
= &WalkState
->ParserState
;
410 ACPI_STATUS Status
= AE_CTRL_PENDING
;
413 ACPI_FUNCTION_TRACE_PTR (PsNextParseState
, Op
);
416 switch (CallbackStatus
)
418 case AE_CTRL_TERMINATE
:
420 * A control method was terminated via a RETURN statement.
421 * The walk of this method is complete.
423 ParserState
->Aml
= ParserState
->AmlEnd
;
424 Status
= AE_CTRL_TERMINATE
;
430 ParserState
->Aml
= WalkState
->AmlLastWhile
;
431 WalkState
->ControlState
->Common
.Value
= FALSE
;
432 Status
= AE_CTRL_BREAK
;
436 case AE_CTRL_CONTINUE
:
438 ParserState
->Aml
= WalkState
->AmlLastWhile
;
439 Status
= AE_CTRL_CONTINUE
;
443 case AE_CTRL_PENDING
:
445 ParserState
->Aml
= WalkState
->AmlLastWhile
;
451 ParserState
->Aml
= ParserState
->Scope
->ParseScope
.PkgEnd
;
458 * Predicate of an IF was true, and we are at the matching ELSE.
459 * Just close out this package
461 ParserState
->Aml
= AcpiPsGetNextPackageEnd (ParserState
);
462 Status
= AE_CTRL_PENDING
;
468 * Either an IF/WHILE Predicate was false or we encountered a BREAK
469 * opcode. In both cases, we do not execute the rest of the
470 * package; We simply close out the parent (finishing the walk of
471 * this branch of the tree) and continue execution at the parent
474 ParserState
->Aml
= ParserState
->Scope
->ParseScope
.PkgEnd
;
476 /* In the case of a BREAK, just force a predicate (if any) to FALSE */
478 WalkState
->ControlState
->Common
.Value
= FALSE
;
479 Status
= AE_CTRL_END
;
483 case AE_CTRL_TRANSFER
:
485 /* A method call (invocation) -- transfer control */
487 Status
= AE_CTRL_TRANSFER
;
488 WalkState
->PrevOp
= Op
;
489 WalkState
->MethodCallOp
= Op
;
490 WalkState
->MethodCallNode
= (Op
->Common
.Value
.Arg
)->Common
.Node
;
492 /* Will return value (if any) be used by the caller? */
494 WalkState
->ReturnUsed
= AcpiDsIsResultUsed (Op
, WalkState
);
500 Status
= CallbackStatus
;
501 if ((CallbackStatus
& AE_CODE_MASK
) == AE_CODE_CONTROL
)
508 return_ACPI_STATUS (Status
);
512 /*******************************************************************************
514 * FUNCTION: AcpiPsParseAml
516 * PARAMETERS: WalkState - Current state
521 * DESCRIPTION: Parse raw AML and return a tree of ops
523 ******************************************************************************/
527 ACPI_WALK_STATE
*WalkState
)
530 ACPI_THREAD_STATE
*Thread
;
531 ACPI_THREAD_STATE
*PrevWalkList
= AcpiGbl_CurrentWalkList
;
532 ACPI_WALK_STATE
*PreviousWalkState
;
535 ACPI_FUNCTION_TRACE (PsParseAml
);
537 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE
,
538 "Entered with WalkState=%p Aml=%p size=%X\n",
539 WalkState
, WalkState
->ParserState
.Aml
,
540 WalkState
->ParserState
.AmlSize
));
542 if (!WalkState
->ParserState
.Aml
)
544 return_ACPI_STATUS (AE_NULL_OBJECT
);
547 /* Create and initialize a new thread state */
549 Thread
= AcpiUtCreateThreadState ();
552 if (WalkState
->MethodDesc
)
554 /* Executing a control method - additional cleanup */
556 AcpiDsTerminateControlMethod (WalkState
->MethodDesc
, WalkState
);
559 AcpiDsDeleteWalkState (WalkState
);
560 return_ACPI_STATUS (AE_NO_MEMORY
);
563 WalkState
->Thread
= Thread
;
566 * If executing a method, the starting SyncLevel is this method's
569 if (WalkState
->MethodDesc
)
571 WalkState
->Thread
->CurrentSyncLevel
= WalkState
->MethodDesc
->Method
.SyncLevel
;
574 AcpiDsPushWalkState (WalkState
, Thread
);
577 * This global allows the AML debugger to get a handle to the currently
578 * executing control method.
580 AcpiGbl_CurrentWalkList
= Thread
;
583 * Execute the walk loop as long as there is a valid Walk State. This
584 * handles nested control method invocations without recursion.
586 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE
, "State=%p\n", WalkState
));
591 if (ACPI_SUCCESS (Status
))
594 * The ParseLoop executes AML until the method terminates
595 * or calls another method.
597 Status
= AcpiPsParseLoop (WalkState
);
600 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE
,
601 "Completed one call to walk loop, %s State=%p\n",
602 AcpiFormatException (Status
), WalkState
));
604 if (Status
== AE_CTRL_TRANSFER
)
607 * A method call was detected.
608 * Transfer control to the called control method
610 Status
= AcpiDsCallControlMethod (Thread
, WalkState
, NULL
);
611 if (ACPI_FAILURE (Status
))
613 Status
= AcpiDsMethodError (Status
, WalkState
);
617 * If the transfer to the new method method call worked, a new walk
618 * state was created -- get it
620 WalkState
= AcpiDsGetCurrentWalkState (Thread
);
623 else if (Status
== AE_CTRL_TERMINATE
)
627 else if ((Status
!= AE_OK
) && (WalkState
->MethodDesc
))
629 /* Either the method parse or actual execution failed */
631 ACPI_ERROR_METHOD ("Method parse/execution failed",
632 WalkState
->MethodNode
, NULL
, Status
);
634 /* Check for possible multi-thread reentrancy problem */
636 if ((Status
== AE_ALREADY_EXISTS
) &&
637 (!(WalkState
->MethodDesc
->Method
.InfoFlags
& ACPI_METHOD_SERIALIZED
)))
640 * Method is not serialized and tried to create an object
641 * twice. The probable cause is that the method cannot
642 * handle reentrancy. Mark as "pending serialized" now, and
643 * then mark "serialized" when the last thread exits.
645 WalkState
->MethodDesc
->Method
.InfoFlags
|=
646 ACPI_METHOD_SERIALIZED_PENDING
;
650 /* We are done with this walk, move on to the parent if any */
652 WalkState
= AcpiDsPopWalkState (Thread
);
654 /* Reset the current scope to the beginning of scope stack */
656 AcpiDsScopeStackClear (WalkState
);
659 * If we just returned from the execution of a control method or if we
660 * encountered an error during the method parse phase, there's lots of
663 if (((WalkState
->ParseFlags
& ACPI_PARSE_MODE_MASK
) == ACPI_PARSE_EXECUTE
) ||
664 (ACPI_FAILURE (Status
)))
666 AcpiDsTerminateControlMethod (WalkState
->MethodDesc
, WalkState
);
669 /* Delete this walk state and all linked control states */
671 AcpiPsCleanupScope (&WalkState
->ParserState
);
672 PreviousWalkState
= WalkState
;
674 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE
,
675 "ReturnValue=%p, ImplicitValue=%p State=%p\n",
676 WalkState
->ReturnDesc
, WalkState
->ImplicitReturnObj
, WalkState
));
678 /* Check if we have restarted a preempted walk */
680 WalkState
= AcpiDsGetCurrentWalkState (Thread
);
683 if (ACPI_SUCCESS (Status
))
686 * There is another walk state, restart it.
687 * If the method return value is not used by the parent,
688 * The object is deleted
690 if (!PreviousWalkState
->ReturnDesc
)
693 * In slack mode execution, if there is no return value
694 * we should implicitly return zero (0) as a default value.
696 if (AcpiGbl_EnableInterpreterSlack
&&
697 !PreviousWalkState
->ImplicitReturnObj
)
699 PreviousWalkState
->ImplicitReturnObj
=
700 AcpiUtCreateIntegerObject ((UINT64
) 0);
701 if (!PreviousWalkState
->ImplicitReturnObj
)
703 return_ACPI_STATUS (AE_NO_MEMORY
);
707 /* Restart the calling control method */
709 Status
= AcpiDsRestartControlMethod (WalkState
,
710 PreviousWalkState
->ImplicitReturnObj
);
715 * We have a valid return value, delete any implicit
718 AcpiDsClearImplicitReturn (PreviousWalkState
);
720 Status
= AcpiDsRestartControlMethod (WalkState
,
721 PreviousWalkState
->ReturnDesc
);
723 if (ACPI_SUCCESS (Status
))
725 WalkState
->WalkType
|= ACPI_WALK_METHOD_RESTART
;
730 /* On error, delete any return object or implicit return */
732 AcpiUtRemoveReference (PreviousWalkState
->ReturnDesc
);
733 AcpiDsClearImplicitReturn (PreviousWalkState
);
738 * Just completed a 1st-level method, save the final internal return
741 else if (PreviousWalkState
->CallerReturnDesc
)
743 if (PreviousWalkState
->ImplicitReturnObj
)
745 *(PreviousWalkState
->CallerReturnDesc
) =
746 PreviousWalkState
->ImplicitReturnObj
;
750 /* NULL if no return value */
752 *(PreviousWalkState
->CallerReturnDesc
) =
753 PreviousWalkState
->ReturnDesc
;
758 if (PreviousWalkState
->ReturnDesc
)
760 /* Caller doesn't want it, must delete it */
762 AcpiUtRemoveReference (PreviousWalkState
->ReturnDesc
);
764 if (PreviousWalkState
->ImplicitReturnObj
)
766 /* Caller doesn't want it, must delete it */
768 AcpiUtRemoveReference (PreviousWalkState
->ImplicitReturnObj
);
772 AcpiDsDeleteWalkState (PreviousWalkState
);
777 AcpiExReleaseAllMutexes (Thread
);
778 AcpiUtDeleteGenericState (ACPI_CAST_PTR (ACPI_GENERIC_STATE
, Thread
));
779 AcpiGbl_CurrentWalkList
= PrevWalkList
;
780 return_ACPI_STATUS (Status
);