1 /******************************************************************************
3 * Module Name: psloop - Main AML parse loop
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2015, 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 *****************************************************************************/
117 * Parse the AML and build an operation tree as most interpreters, (such as
118 * Perl) do. Parsing is done by hand rather than with a YACC generated parser
119 * to tightly constrain stack and dynamic memory usage. Parsing is kept
120 * flexible and the code fairly compact by parsing based on a list of AML
121 * opcode templates in AmlOpInfo[].
125 #include "accommon.h"
126 #include "acparser.h"
127 #include "acdispat.h"
130 #define _COMPONENT ACPI_PARSER
131 ACPI_MODULE_NAME ("psloop")
134 /* Local prototypes */
138 ACPI_WALK_STATE *WalkState,
140 ACPI_PARSE_OBJECT *Op);
143 AcpiPsLinkModuleCode (
144 ACPI_PARSE_OBJECT *ParentOp,
147 ACPI_OWNER_ID OwnerId);
150 /*******************************************************************************
152 * FUNCTION: AcpiPsGetArguments
154 * PARAMETERS: WalkState - Current state
155 * AmlOpStart - Op start in AML
160 * DESCRIPTION: Get arguments for passed Op.
162 ******************************************************************************/
166 ACPI_WALK_STATE *WalkState,
168 ACPI_PARSE_OBJECT *Op)
170 ACPI_STATUS Status = AE_OK;
171 ACPI_PARSE_OBJECT *Arg = NULL;
172 const ACPI_OPCODE_INFO *OpInfo;
175 ACPI_FUNCTION_TRACE_PTR (PsGetArguments, WalkState);
178 switch (Op->Common.AmlOpcode)
180 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
181 case AML_WORD_OP: /* AML_WORDDATA_ARG */
182 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
183 case AML_QWORD_OP: /* AML_QWORDATA_ARG */
184 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
186 /* Fill in constant or string argument directly */
188 AcpiPsGetNextSimpleArg (&(WalkState->ParserState),
189 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), Op);
192 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
194 Status = AcpiPsGetNextNamepath (WalkState, &(WalkState->ParserState), Op, 1);
195 if (ACPI_FAILURE (Status))
197 return_ACPI_STATUS (Status);
200 WalkState->ArgTypes = 0;
205 * Op is not a constant or string, append each argument to the Op
207 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) && !WalkState->ArgCount)
209 WalkState->AmlOffset = (UINT32) ACPI_PTR_DIFF (WalkState->ParserState.Aml,
210 WalkState->ParserState.AmlStart);
212 Status = AcpiPsGetNextArg (WalkState, &(WalkState->ParserState),
213 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
214 if (ACPI_FAILURE (Status))
216 return_ACPI_STATUS (Status);
221 Arg->Common.AmlOffset = WalkState->AmlOffset;
222 AcpiPsAppendArg (Op, Arg);
225 INCREMENT_ARG_LIST (WalkState->ArgTypes);
230 * Handle executable code at "module-level". This refers to
231 * executable opcodes that appear outside of any control method.
233 if ((WalkState->PassNumber <= ACPI_IMODE_LOAD_PASS2) &&
234 ((WalkState->ParseFlags & ACPI_PARSE_DISASSEMBLE) == 0))
237 * We want to skip If/Else/While constructs during Pass1 because we
238 * want to actually conditionally execute the code during Pass2.
240 * Except for disassembly, where we always want to walk the
241 * If/Else/While packages
243 switch (Op->Common.AmlOpcode)
249 * Currently supported module-level opcodes are:
250 * IF/ELSE/WHILE. These appear to be the most common,
251 * and easiest to support since they open an AML
254 if (WalkState->PassNumber == ACPI_IMODE_LOAD_PASS1)
256 AcpiPsLinkModuleCode (Op->Common.Parent, AmlOpStart,
257 (UINT32) (WalkState->ParserState.PkgEnd - AmlOpStart),
261 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
262 "Pass1: Skipping an If/Else/While body\n"));
264 /* Skip body of if/else/while in pass 1 */
266 WalkState->ParserState.Aml = WalkState->ParserState.PkgEnd;
267 WalkState->ArgCount = 0;
272 * Check for an unsupported executable opcode at module
273 * level. We must be in PASS1, the parent must be a SCOPE,
274 * The opcode class must be EXECUTE, and the opcode must
275 * not be an argument to another opcode.
277 if ((WalkState->PassNumber == ACPI_IMODE_LOAD_PASS1) &&
278 (Op->Common.Parent->Common.AmlOpcode == AML_SCOPE_OP))
280 OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
281 if ((OpInfo->Class == AML_CLASS_EXECUTE) &&
284 ACPI_WARNING ((AE_INFO,
285 "Unsupported module-level executable opcode "
286 "0x%.2X at table offset 0x%.4X",
287 Op->Common.AmlOpcode,
288 (UINT32) (ACPI_PTR_DIFF (AmlOpStart,
289 WalkState->ParserState.AmlStart) +
290 sizeof (ACPI_TABLE_HEADER))));
297 /* Special processing for certain opcodes */
299 switch (Op->Common.AmlOpcode)
303 * Skip parsing of control method because we don't have enough
304 * info in the first pass to parse it correctly.
306 * Save the length and address of the body
308 Op->Named.Data = WalkState->ParserState.Aml;
309 Op->Named.Length = (UINT32)
310 (WalkState->ParserState.PkgEnd - WalkState->ParserState.Aml);
312 /* Skip body of method */
314 WalkState->ParserState.Aml = WalkState->ParserState.PkgEnd;
315 WalkState->ArgCount = 0;
320 case AML_VAR_PACKAGE_OP:
322 if ((Op->Common.Parent) &&
323 (Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
324 (WalkState->PassNumber <= ACPI_IMODE_LOAD_PASS2))
327 * Skip parsing of Buffers and Packages because we don't have
328 * enough info in the first pass to parse them correctly.
330 Op->Named.Data = AmlOpStart;
331 Op->Named.Length = (UINT32)
332 (WalkState->ParserState.PkgEnd - AmlOpStart);
336 WalkState->ParserState.Aml = WalkState->ParserState.PkgEnd;
337 WalkState->ArgCount = 0;
343 if (WalkState->ControlState)
345 WalkState->ControlState->Control.PackageEnd =
346 WalkState->ParserState.PkgEnd;
352 /* No action for all other opcodes */
360 return_ACPI_STATUS (AE_OK);
364 /*******************************************************************************
366 * FUNCTION: AcpiPsLinkModuleCode
368 * PARAMETERS: ParentOp - Parent parser op
369 * AmlStart - Pointer to the AML
370 * AmlLength - Length of executable AML
371 * OwnerId - OwnerId of module level code
375 * DESCRIPTION: Wrap the module-level code with a method object and link the
376 * object to the global list. Note, the mutex field of the method
377 * object is used to link multiple module-level code objects.
379 ******************************************************************************/
382 AcpiPsLinkModuleCode (
383 ACPI_PARSE_OBJECT *ParentOp,
386 ACPI_OWNER_ID OwnerId)
388 ACPI_OPERAND_OBJECT *Prev;
389 ACPI_OPERAND_OBJECT *Next;
390 ACPI_OPERAND_OBJECT *MethodObj;
391 ACPI_NAMESPACE_NODE *ParentNode;
394 /* Get the tail of the list */
396 Prev = Next = AcpiGbl_ModuleCodeList;
400 Next = Next->Method.Mutex;
404 * Insert the module level code into the list. Merge it if it is
405 * adjacent to the previous element.
408 ((Prev->Method.AmlStart + Prev->Method.AmlLength) != AmlStart))
410 /* Create, initialize, and link a new temporary method object */
412 MethodObj = AcpiUtCreateInternalObject (ACPI_TYPE_METHOD);
418 if (ParentOp->Common.Node)
420 ParentNode = ParentOp->Common.Node;
424 ParentNode = AcpiGbl_RootNode;
427 MethodObj->Method.AmlStart = AmlStart;
428 MethodObj->Method.AmlLength = AmlLength;
429 MethodObj->Method.OwnerId = OwnerId;
430 MethodObj->Method.InfoFlags |= ACPI_METHOD_MODULE_LEVEL;
433 * Save the parent node in NextObject. This is cheating, but we
434 * don't want to expand the method object.
436 MethodObj->Method.NextObject =
437 ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, ParentNode);
441 AcpiGbl_ModuleCodeList = MethodObj;
445 Prev->Method.Mutex = MethodObj;
450 Prev->Method.AmlLength += AmlLength;
454 /*******************************************************************************
456 * FUNCTION: AcpiPsParseLoop
458 * PARAMETERS: WalkState - Current state
462 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
465 ******************************************************************************/
469 ACPI_WALK_STATE *WalkState)
471 ACPI_STATUS Status = AE_OK;
472 ACPI_PARSE_OBJECT *Op = NULL; /* current op */
473 ACPI_PARSE_STATE *ParserState;
474 UINT8 *AmlOpStart = NULL;
477 ACPI_FUNCTION_TRACE_PTR (PsParseLoop, WalkState);
480 if (WalkState->DescendingCallback == NULL)
482 return_ACPI_STATUS (AE_BAD_PARAMETER);
485 ParserState = &WalkState->ParserState;
486 WalkState->ArgTypes = 0;
488 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
490 if (WalkState->WalkType & ACPI_WALK_METHOD_RESTART)
492 /* We are restarting a preempted control method */
494 if (AcpiPsHasCompletedScope (ParserState))
497 * We must check if a predicate to an IF or WHILE statement
500 if ((ParserState->Scope->ParseScope.Op) &&
501 ((ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_IF_OP) ||
502 (ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_WHILE_OP)) &&
503 (WalkState->ControlState) &&
504 (WalkState->ControlState->Common.State ==
505 ACPI_CONTROL_PREDICATE_EXECUTING))
508 * A predicate was just completed, get the value of the
509 * predicate and branch based on that value
511 WalkState->Op = NULL;
512 Status = AcpiDsGetPredicateValue (WalkState, ACPI_TO_POINTER (TRUE));
513 if (ACPI_FAILURE (Status) &&
514 ((Status & AE_CODE_MASK) != AE_CODE_CONTROL))
516 if (Status == AE_AML_NO_RETURN_VALUE)
518 ACPI_EXCEPTION ((AE_INFO, Status,
519 "Invoked method did not return a value"));
522 ACPI_EXCEPTION ((AE_INFO, Status, "GetPredicate Failed"));
523 return_ACPI_STATUS (Status);
526 Status = AcpiPsNextParseState (WalkState, Op, Status);
529 AcpiPsPopScope (ParserState, &Op,
530 &WalkState->ArgTypes, &WalkState->ArgCount);
531 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", Op));
533 else if (WalkState->PrevOp)
535 /* We were in the middle of an op */
537 Op = WalkState->PrevOp;
538 WalkState->ArgTypes = WalkState->PrevArgTypes;
543 /* Iterative parsing loop, while there is more AML to process: */
545 while ((ParserState->Aml < ParserState->AmlEnd) || (Op))
547 AmlOpStart = ParserState->Aml;
550 Status = AcpiPsCreateOp (WalkState, AmlOpStart, &Op);
551 if (ACPI_FAILURE (Status))
553 if (Status == AE_CTRL_PARSE_CONTINUE)
558 if (Status == AE_CTRL_PARSE_PENDING)
563 if (Status == AE_CTRL_TERMINATE)
565 return_ACPI_STATUS (Status);
568 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
569 if (ACPI_FAILURE (Status))
571 return_ACPI_STATUS (Status);
577 Op->Common.AmlOffset = WalkState->AmlOffset;
579 if (WalkState->OpInfo)
581 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
582 "Opcode %4.4X [%s] Op %p Aml %p AmlOffset %5.5X\n",
583 (UINT32) Op->Common.AmlOpcode, WalkState->OpInfo->Name,
584 Op, ParserState->Aml, Op->Common.AmlOffset));
590 * Start ArgCount at zero because we don't know if there are
593 WalkState->ArgCount = 0;
595 /* Are there any arguments that must be processed? */
597 if (WalkState->ArgTypes)
601 Status = AcpiPsGetArguments (WalkState, AmlOpStart, Op);
602 if (ACPI_FAILURE (Status))
604 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
605 if (ACPI_FAILURE (Status))
607 return_ACPI_STATUS (Status);
614 /* Check for arguments that need to be processed */
616 if (WalkState->ArgCount)
619 * There are arguments (complex ones), push Op and
620 * prepare for argument
622 Status = AcpiPsPushScope (ParserState, Op,
623 WalkState->ArgTypes, WalkState->ArgCount);
624 if (ACPI_FAILURE (Status))
626 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
627 if (ACPI_FAILURE (Status))
629 return_ACPI_STATUS (Status);
640 * All arguments have been processed -- Op is complete,
643 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
644 if (WalkState->OpInfo->Flags & AML_NAMED)
646 if (Op->Common.AmlOpcode == AML_REGION_OP ||
647 Op->Common.AmlOpcode == AML_DATA_REGION_OP)
650 * Skip parsing of control method or opregion body,
651 * because we don't have enough info in the first pass
652 * to parse them correctly.
654 * Completed parsing an OpRegion declaration, we now
657 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
661 if (WalkState->OpInfo->Flags & AML_CREATE)
664 * Backup to beginning of CreateXXXfield declaration (1 for
667 * BodyLength is unknown until we parse the body
669 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
672 if (Op->Common.AmlOpcode == AML_BANK_FIELD_OP)
675 * Backup to beginning of BankField declaration
677 * BodyLength is unknown until we parse the body
679 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
682 /* This op complete, notify the dispatcher */
684 if (WalkState->AscendingCallback != NULL)
687 WalkState->Opcode = Op->Common.AmlOpcode;
689 Status = WalkState->AscendingCallback (WalkState);
690 Status = AcpiPsNextParseState (WalkState, Op, Status);
691 if (Status == AE_CTRL_PENDING)
697 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
698 if (ACPI_FAILURE (Status))
700 return_ACPI_STATUS (Status);
703 } /* while ParserState->Aml */
705 Status = AcpiPsCompleteFinalOp (WalkState, Op, Status);
706 return_ACPI_STATUS (Status);