1 /******************************************************************************
3 * Module Name: psloop - Main AML parse loop
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
45 * Parse the AML and build an operation tree as most interpreters, (such as
46 * Perl) do. Parsing is done by hand rather than with a YACC generated parser
47 * to tightly constrain stack and dynamic memory usage. Parsing is kept
48 * flexible and the code fairly compact by parsing based on a list of AML
49 * opcode templates in AmlOpInfo[].
52 #include <contrib/dev/acpica/include/acpi.h>
53 #include <contrib/dev/acpica/include/accommon.h>
54 #include <contrib/dev/acpica/include/acparser.h>
55 #include <contrib/dev/acpica/include/acdispat.h>
56 #include <contrib/dev/acpica/include/amlcode.h>
58 #define _COMPONENT ACPI_PARSER
59 ACPI_MODULE_NAME ("psloop")
62 /* Local prototypes */
66 ACPI_WALK_STATE *WalkState,
68 ACPI_PARSE_OBJECT *Op);
71 AcpiPsLinkModuleCode (
72 ACPI_PARSE_OBJECT *ParentOp,
75 ACPI_OWNER_ID OwnerId);
78 /*******************************************************************************
80 * FUNCTION: AcpiPsGetArguments
82 * PARAMETERS: WalkState - Current state
83 * AmlOpStart - Op start in AML
88 * DESCRIPTION: Get arguments for passed Op.
90 ******************************************************************************/
94 ACPI_WALK_STATE *WalkState,
96 ACPI_PARSE_OBJECT *Op)
98 ACPI_STATUS Status = AE_OK;
99 ACPI_PARSE_OBJECT *Arg = NULL;
100 const ACPI_OPCODE_INFO *OpInfo;
103 ACPI_FUNCTION_TRACE_PTR (PsGetArguments, WalkState);
106 switch (Op->Common.AmlOpcode)
108 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
109 case AML_WORD_OP: /* AML_WORDDATA_ARG */
110 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
111 case AML_QWORD_OP: /* AML_QWORDATA_ARG */
112 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
114 /* Fill in constant or string argument directly */
116 AcpiPsGetNextSimpleArg (&(WalkState->ParserState),
117 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), Op);
120 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
122 Status = AcpiPsGetNextNamepath (WalkState, &(WalkState->ParserState), Op, 1);
123 if (ACPI_FAILURE (Status))
125 return_ACPI_STATUS (Status);
128 WalkState->ArgTypes = 0;
133 * Op is not a constant or string, append each argument to the Op
135 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) && !WalkState->ArgCount)
137 WalkState->AmlOffset = (UINT32) ACPI_PTR_DIFF (WalkState->ParserState.Aml,
138 WalkState->ParserState.AmlStart);
140 Status = AcpiPsGetNextArg (WalkState, &(WalkState->ParserState),
141 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
142 if (ACPI_FAILURE (Status))
144 return_ACPI_STATUS (Status);
149 Arg->Common.AmlOffset = WalkState->AmlOffset;
150 AcpiPsAppendArg (Op, Arg);
153 INCREMENT_ARG_LIST (WalkState->ArgTypes);
158 * Handle executable code at "module-level". This refers to
159 * executable opcodes that appear outside of any control method.
161 if ((WalkState->PassNumber <= ACPI_IMODE_LOAD_PASS2) &&
162 ((WalkState->ParseFlags & ACPI_PARSE_DISASSEMBLE) == 0))
165 * We want to skip If/Else/While constructs during Pass1 because we
166 * want to actually conditionally execute the code during Pass2.
168 * Except for disassembly, where we always want to walk the
169 * If/Else/While packages
171 switch (Op->Common.AmlOpcode)
177 * Currently supported module-level opcodes are:
178 * IF/ELSE/WHILE. These appear to be the most common,
179 * and easiest to support since they open an AML
182 if (WalkState->PassNumber == ACPI_IMODE_LOAD_PASS1)
184 AcpiPsLinkModuleCode (Op->Common.Parent, AmlOpStart,
185 (UINT32) (WalkState->ParserState.PkgEnd - AmlOpStart),
189 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
190 "Pass1: Skipping an If/Else/While body\n"));
192 /* Skip body of if/else/while in pass 1 */
194 WalkState->ParserState.Aml = WalkState->ParserState.PkgEnd;
195 WalkState->ArgCount = 0;
200 * Check for an unsupported executable opcode at module
201 * level. We must be in PASS1, the parent must be a SCOPE,
202 * The opcode class must be EXECUTE, and the opcode must
203 * not be an argument to another opcode.
205 if ((WalkState->PassNumber == ACPI_IMODE_LOAD_PASS1) &&
206 (Op->Common.Parent->Common.AmlOpcode == AML_SCOPE_OP))
208 OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
209 if ((OpInfo->Class == AML_CLASS_EXECUTE) &&
212 ACPI_WARNING ((AE_INFO,
213 "Unsupported module-level executable opcode "
214 "0x%.2X at table offset 0x%.4X",
215 Op->Common.AmlOpcode,
216 (UINT32) (ACPI_PTR_DIFF (AmlOpStart,
217 WalkState->ParserState.AmlStart) +
218 sizeof (ACPI_TABLE_HEADER))));
225 /* Special processing for certain opcodes */
227 switch (Op->Common.AmlOpcode)
231 * Skip parsing of control method because we don't have enough
232 * info in the first pass to parse it correctly.
234 * Save the length and address of the body
236 Op->Named.Data = WalkState->ParserState.Aml;
237 Op->Named.Length = (UINT32)
238 (WalkState->ParserState.PkgEnd - WalkState->ParserState.Aml);
240 /* Skip body of method */
242 WalkState->ParserState.Aml = WalkState->ParserState.PkgEnd;
243 WalkState->ArgCount = 0;
248 case AML_VAR_PACKAGE_OP:
250 if ((Op->Common.Parent) &&
251 (Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
252 (WalkState->PassNumber <= ACPI_IMODE_LOAD_PASS2))
255 * Skip parsing of Buffers and Packages because we don't have
256 * enough info in the first pass to parse them correctly.
258 Op->Named.Data = AmlOpStart;
259 Op->Named.Length = (UINT32)
260 (WalkState->ParserState.PkgEnd - AmlOpStart);
264 WalkState->ParserState.Aml = WalkState->ParserState.PkgEnd;
265 WalkState->ArgCount = 0;
271 if (WalkState->ControlState)
273 WalkState->ControlState->Control.PackageEnd =
274 WalkState->ParserState.PkgEnd;
280 /* No action for all other opcodes */
288 return_ACPI_STATUS (AE_OK);
292 /*******************************************************************************
294 * FUNCTION: AcpiPsLinkModuleCode
296 * PARAMETERS: ParentOp - Parent parser op
297 * AmlStart - Pointer to the AML
298 * AmlLength - Length of executable AML
299 * OwnerId - OwnerId of module level code
303 * DESCRIPTION: Wrap the module-level code with a method object and link the
304 * object to the global list. Note, the mutex field of the method
305 * object is used to link multiple module-level code objects.
307 ******************************************************************************/
310 AcpiPsLinkModuleCode (
311 ACPI_PARSE_OBJECT *ParentOp,
314 ACPI_OWNER_ID OwnerId)
316 ACPI_OPERAND_OBJECT *Prev;
317 ACPI_OPERAND_OBJECT *Next;
318 ACPI_OPERAND_OBJECT *MethodObj;
319 ACPI_NAMESPACE_NODE *ParentNode;
322 /* Get the tail of the list */
324 Prev = Next = AcpiGbl_ModuleCodeList;
328 Next = Next->Method.Mutex;
332 * Insert the module level code into the list. Merge it if it is
333 * adjacent to the previous element.
336 ((Prev->Method.AmlStart + Prev->Method.AmlLength) != AmlStart))
338 /* Create, initialize, and link a new temporary method object */
340 MethodObj = AcpiUtCreateInternalObject (ACPI_TYPE_METHOD);
346 if (ParentOp->Common.Node)
348 ParentNode = ParentOp->Common.Node;
352 ParentNode = AcpiGbl_RootNode;
355 MethodObj->Method.AmlStart = AmlStart;
356 MethodObj->Method.AmlLength = AmlLength;
357 MethodObj->Method.OwnerId = OwnerId;
358 MethodObj->Method.InfoFlags |= ACPI_METHOD_MODULE_LEVEL;
361 * Save the parent node in NextObject. This is cheating, but we
362 * don't want to expand the method object.
364 MethodObj->Method.NextObject =
365 ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, ParentNode);
369 AcpiGbl_ModuleCodeList = MethodObj;
373 Prev->Method.Mutex = MethodObj;
378 Prev->Method.AmlLength += AmlLength;
382 /*******************************************************************************
384 * FUNCTION: AcpiPsParseLoop
386 * PARAMETERS: WalkState - Current state
390 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
393 ******************************************************************************/
397 ACPI_WALK_STATE *WalkState)
399 ACPI_STATUS Status = AE_OK;
400 ACPI_PARSE_OBJECT *Op = NULL; /* current op */
401 ACPI_PARSE_STATE *ParserState;
402 UINT8 *AmlOpStart = NULL;
405 ACPI_FUNCTION_TRACE_PTR (PsParseLoop, WalkState);
408 if (WalkState->DescendingCallback == NULL)
410 return_ACPI_STATUS (AE_BAD_PARAMETER);
413 ParserState = &WalkState->ParserState;
414 WalkState->ArgTypes = 0;
416 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
418 if (WalkState->WalkType & ACPI_WALK_METHOD_RESTART)
420 /* We are restarting a preempted control method */
422 if (AcpiPsHasCompletedScope (ParserState))
425 * We must check if a predicate to an IF or WHILE statement
428 if ((ParserState->Scope->ParseScope.Op) &&
429 ((ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_IF_OP) ||
430 (ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_WHILE_OP)) &&
431 (WalkState->ControlState) &&
432 (WalkState->ControlState->Common.State ==
433 ACPI_CONTROL_PREDICATE_EXECUTING))
436 * A predicate was just completed, get the value of the
437 * predicate and branch based on that value
439 WalkState->Op = NULL;
440 Status = AcpiDsGetPredicateValue (WalkState, ACPI_TO_POINTER (TRUE));
441 if (ACPI_FAILURE (Status) &&
442 ((Status & AE_CODE_MASK) != AE_CODE_CONTROL))
444 if (Status == AE_AML_NO_RETURN_VALUE)
446 ACPI_EXCEPTION ((AE_INFO, Status,
447 "Invoked method did not return a value"));
450 ACPI_EXCEPTION ((AE_INFO, Status, "GetPredicate Failed"));
451 return_ACPI_STATUS (Status);
454 Status = AcpiPsNextParseState (WalkState, Op, Status);
457 AcpiPsPopScope (ParserState, &Op,
458 &WalkState->ArgTypes, &WalkState->ArgCount);
459 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", Op));
461 else if (WalkState->PrevOp)
463 /* We were in the middle of an op */
465 Op = WalkState->PrevOp;
466 WalkState->ArgTypes = WalkState->PrevArgTypes;
471 /* Iterative parsing loop, while there is more AML to process: */
473 while ((ParserState->Aml < ParserState->AmlEnd) || (Op))
475 AmlOpStart = ParserState->Aml;
478 Status = AcpiPsCreateOp (WalkState, AmlOpStart, &Op);
479 if (ACPI_FAILURE (Status))
481 if (Status == AE_CTRL_PARSE_CONTINUE)
486 if (Status == AE_CTRL_PARSE_PENDING)
491 if (Status == AE_CTRL_TERMINATE)
493 return_ACPI_STATUS (Status);
496 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
497 if (ACPI_FAILURE (Status))
499 return_ACPI_STATUS (Status);
505 Op->Common.AmlOffset = WalkState->AmlOffset;
507 if (WalkState->OpInfo)
509 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
510 "Opcode %4.4X [%s] Op %p Aml %p AmlOffset %5.5X\n",
511 (UINT32) Op->Common.AmlOpcode, WalkState->OpInfo->Name,
512 Op, ParserState->Aml, Op->Common.AmlOffset));
518 * Start ArgCount at zero because we don't know if there are
521 WalkState->ArgCount = 0;
523 /* Are there any arguments that must be processed? */
525 if (WalkState->ArgTypes)
529 Status = AcpiPsGetArguments (WalkState, AmlOpStart, Op);
530 if (ACPI_FAILURE (Status))
532 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
533 if (ACPI_FAILURE (Status))
535 return_ACPI_STATUS (Status);
542 /* Check for arguments that need to be processed */
544 if (WalkState->ArgCount)
547 * There are arguments (complex ones), push Op and
548 * prepare for argument
550 Status = AcpiPsPushScope (ParserState, Op,
551 WalkState->ArgTypes, WalkState->ArgCount);
552 if (ACPI_FAILURE (Status))
554 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
555 if (ACPI_FAILURE (Status))
557 return_ACPI_STATUS (Status);
568 * All arguments have been processed -- Op is complete,
571 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
572 if (WalkState->OpInfo->Flags & AML_NAMED)
574 if (Op->Common.AmlOpcode == AML_REGION_OP ||
575 Op->Common.AmlOpcode == AML_DATA_REGION_OP)
578 * Skip parsing of control method or opregion body,
579 * because we don't have enough info in the first pass
580 * to parse them correctly.
582 * Completed parsing an OpRegion declaration, we now
585 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
589 if (WalkState->OpInfo->Flags & AML_CREATE)
592 * Backup to beginning of CreateXXXfield declaration (1 for
595 * BodyLength is unknown until we parse the body
597 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
600 if (Op->Common.AmlOpcode == AML_BANK_FIELD_OP)
603 * Backup to beginning of BankField declaration
605 * BodyLength is unknown until we parse the body
607 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
610 /* This op complete, notify the dispatcher */
612 if (WalkState->AscendingCallback != NULL)
615 WalkState->Opcode = Op->Common.AmlOpcode;
617 Status = WalkState->AscendingCallback (WalkState);
618 Status = AcpiPsNextParseState (WalkState, Op, Status);
619 if (Status == AE_CTRL_PENDING)
625 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
626 if (ACPI_FAILURE (Status))
628 return_ACPI_STATUS (Status);
631 } /* while ParserState->Aml */
633 Status = AcpiPsCompleteFinalOp (WalkState, Op, Status);
634 return_ACPI_STATUS (Status);