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/acinterp.h>
55 #include <contrib/dev/acpica/include/acparser.h>
56 #include <contrib/dev/acpica/include/acdispat.h>
57 #include <contrib/dev/acpica/include/amlcode.h>
59 #define _COMPONENT ACPI_PARSER
60 ACPI_MODULE_NAME ("psloop")
63 /* Local prototypes */
67 ACPI_WALK_STATE *WalkState,
69 ACPI_PARSE_OBJECT *Op);
72 AcpiPsLinkModuleCode (
73 ACPI_PARSE_OBJECT *ParentOp,
76 ACPI_OWNER_ID OwnerId);
79 /*******************************************************************************
81 * FUNCTION: AcpiPsGetArguments
83 * PARAMETERS: WalkState - Current state
84 * AmlOpStart - Op start in AML
89 * DESCRIPTION: Get arguments for passed Op.
91 ******************************************************************************/
95 ACPI_WALK_STATE *WalkState,
97 ACPI_PARSE_OBJECT *Op)
99 ACPI_STATUS Status = AE_OK;
100 ACPI_PARSE_OBJECT *Arg = NULL;
101 const ACPI_OPCODE_INFO *OpInfo;
104 ACPI_FUNCTION_TRACE_PTR (PsGetArguments, WalkState);
107 switch (Op->Common.AmlOpcode)
109 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
110 case AML_WORD_OP: /* AML_WORDDATA_ARG */
111 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
112 case AML_QWORD_OP: /* AML_QWORDATA_ARG */
113 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
115 /* Fill in constant or string argument directly */
117 AcpiPsGetNextSimpleArg (&(WalkState->ParserState),
118 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), Op);
121 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
123 Status = AcpiPsGetNextNamepath (WalkState, &(WalkState->ParserState), Op, 1);
124 if (ACPI_FAILURE (Status))
126 return_ACPI_STATUS (Status);
129 WalkState->ArgTypes = 0;
134 * Op is not a constant or string, append each argument to the Op
136 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) && !WalkState->ArgCount)
138 WalkState->Aml = WalkState->ParserState.Aml;
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 AcpiPsAppendArg (Op, Arg);
152 INCREMENT_ARG_LIST (WalkState->ArgTypes);
157 * Handle executable code at "module-level". This refers to
158 * executable opcodes that appear outside of any control method.
160 if ((WalkState->PassNumber <= ACPI_IMODE_LOAD_PASS2) &&
161 ((WalkState->ParseFlags & ACPI_PARSE_DISASSEMBLE) == 0))
164 * We want to skip If/Else/While constructs during Pass1 because we
165 * want to actually conditionally execute the code during Pass2.
167 * Except for disassembly, where we always want to walk the
168 * If/Else/While packages
170 switch (Op->Common.AmlOpcode)
176 * Currently supported module-level opcodes are:
177 * IF/ELSE/WHILE. These appear to be the most common,
178 * and easiest to support since they open an AML
181 if (WalkState->PassNumber == ACPI_IMODE_LOAD_PASS1)
183 AcpiPsLinkModuleCode (Op->Common.Parent, AmlOpStart,
184 (UINT32) (WalkState->ParserState.PkgEnd - AmlOpStart),
188 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
189 "Pass1: Skipping an If/Else/While body\n"));
191 /* Skip body of if/else/while in pass 1 */
193 WalkState->ParserState.Aml = WalkState->ParserState.PkgEnd;
194 WalkState->ArgCount = 0;
199 * Check for an unsupported executable opcode at module
200 * level. We must be in PASS1, the parent must be a SCOPE,
201 * The opcode class must be EXECUTE, and the opcode must
202 * not be an argument to another opcode.
204 if ((WalkState->PassNumber == ACPI_IMODE_LOAD_PASS1) &&
205 (Op->Common.Parent->Common.AmlOpcode == AML_SCOPE_OP))
207 OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
208 if ((OpInfo->Class == AML_CLASS_EXECUTE) &&
211 ACPI_WARNING ((AE_INFO,
212 "Unsupported module-level executable opcode "
213 "0x%.2X at table offset 0x%.4X",
214 Op->Common.AmlOpcode,
215 (UINT32) (ACPI_PTR_DIFF (AmlOpStart,
216 WalkState->ParserState.AmlStart) +
217 sizeof (ACPI_TABLE_HEADER))));
224 /* Special processing for certain opcodes */
226 switch (Op->Common.AmlOpcode)
230 * Skip parsing of control method because we don't have enough
231 * info in the first pass to parse it correctly.
233 * Save the length and address of the body
235 Op->Named.Data = WalkState->ParserState.Aml;
236 Op->Named.Length = (UINT32)
237 (WalkState->ParserState.PkgEnd - WalkState->ParserState.Aml);
239 /* Skip body of method */
241 WalkState->ParserState.Aml = WalkState->ParserState.PkgEnd;
242 WalkState->ArgCount = 0;
247 case AML_VAR_PACKAGE_OP:
249 if ((Op->Common.Parent) &&
250 (Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
251 (WalkState->PassNumber <= ACPI_IMODE_LOAD_PASS2))
254 * Skip parsing of Buffers and Packages because we don't have
255 * enough info in the first pass to parse them correctly.
257 Op->Named.Data = AmlOpStart;
258 Op->Named.Length = (UINT32)
259 (WalkState->ParserState.PkgEnd - AmlOpStart);
263 WalkState->ParserState.Aml = WalkState->ParserState.PkgEnd;
264 WalkState->ArgCount = 0;
270 if (WalkState->ControlState)
272 WalkState->ControlState->Control.PackageEnd =
273 WalkState->ParserState.PkgEnd;
279 /* No action for all other opcodes */
287 return_ACPI_STATUS (AE_OK);
291 /*******************************************************************************
293 * FUNCTION: AcpiPsLinkModuleCode
295 * PARAMETERS: ParentOp - Parent parser op
296 * AmlStart - Pointer to the AML
297 * AmlLength - Length of executable AML
298 * OwnerId - OwnerId of module level code
302 * DESCRIPTION: Wrap the module-level code with a method object and link the
303 * object to the global list. Note, the mutex field of the method
304 * object is used to link multiple module-level code objects.
306 ******************************************************************************/
309 AcpiPsLinkModuleCode (
310 ACPI_PARSE_OBJECT *ParentOp,
313 ACPI_OWNER_ID OwnerId)
315 ACPI_OPERAND_OBJECT *Prev;
316 ACPI_OPERAND_OBJECT *Next;
317 ACPI_OPERAND_OBJECT *MethodObj;
318 ACPI_NAMESPACE_NODE *ParentNode;
321 /* Get the tail of the list */
323 Prev = Next = AcpiGbl_ModuleCodeList;
327 Next = Next->Method.Mutex;
331 * Insert the module level code into the list. Merge it if it is
332 * adjacent to the previous element.
335 ((Prev->Method.AmlStart + Prev->Method.AmlLength) != AmlStart))
337 /* Create, initialize, and link a new temporary method object */
339 MethodObj = AcpiUtCreateInternalObject (ACPI_TYPE_METHOD);
345 if (ParentOp->Common.Node)
347 ParentNode = ParentOp->Common.Node;
351 ParentNode = AcpiGbl_RootNode;
354 MethodObj->Method.AmlStart = AmlStart;
355 MethodObj->Method.AmlLength = AmlLength;
356 MethodObj->Method.OwnerId = OwnerId;
357 MethodObj->Method.InfoFlags |= ACPI_METHOD_MODULE_LEVEL;
360 * Save the parent node in NextObject. This is cheating, but we
361 * don't want to expand the method object.
363 MethodObj->Method.NextObject =
364 ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, ParentNode);
368 AcpiGbl_ModuleCodeList = MethodObj;
372 Prev->Method.Mutex = MethodObj;
377 Prev->Method.AmlLength += AmlLength;
381 /*******************************************************************************
383 * FUNCTION: AcpiPsParseLoop
385 * PARAMETERS: WalkState - Current state
389 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
392 ******************************************************************************/
396 ACPI_WALK_STATE *WalkState)
398 ACPI_STATUS Status = AE_OK;
399 ACPI_PARSE_OBJECT *Op = NULL; /* current op */
400 ACPI_PARSE_STATE *ParserState;
401 UINT8 *AmlOpStart = NULL;
404 ACPI_FUNCTION_TRACE_PTR (PsParseLoop, WalkState);
407 if (WalkState->DescendingCallback == NULL)
409 return_ACPI_STATUS (AE_BAD_PARAMETER);
412 ParserState = &WalkState->ParserState;
413 WalkState->ArgTypes = 0;
415 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
417 if (WalkState->WalkType & ACPI_WALK_METHOD_RESTART)
419 /* We are restarting a preempted control method */
421 if (AcpiPsHasCompletedScope (ParserState))
424 * We must check if a predicate to an IF or WHILE statement
427 if ((ParserState->Scope->ParseScope.Op) &&
428 ((ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_IF_OP) ||
429 (ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_WHILE_OP)) &&
430 (WalkState->ControlState) &&
431 (WalkState->ControlState->Common.State ==
432 ACPI_CONTROL_PREDICATE_EXECUTING))
435 * A predicate was just completed, get the value of the
436 * predicate and branch based on that value
438 WalkState->Op = NULL;
439 Status = AcpiDsGetPredicateValue (WalkState, ACPI_TO_POINTER (TRUE));
440 if (ACPI_FAILURE (Status) &&
441 ((Status & AE_CODE_MASK) != AE_CODE_CONTROL))
443 if (Status == AE_AML_NO_RETURN_VALUE)
445 ACPI_EXCEPTION ((AE_INFO, Status,
446 "Invoked method did not return a value"));
449 ACPI_EXCEPTION ((AE_INFO, Status, "GetPredicate Failed"));
450 return_ACPI_STATUS (Status);
453 Status = AcpiPsNextParseState (WalkState, Op, Status);
456 AcpiPsPopScope (ParserState, &Op,
457 &WalkState->ArgTypes, &WalkState->ArgCount);
458 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", Op));
460 else if (WalkState->PrevOp)
462 /* We were in the middle of an op */
464 Op = WalkState->PrevOp;
465 WalkState->ArgTypes = WalkState->PrevArgTypes;
470 /* Iterative parsing loop, while there is more AML to process: */
472 while ((ParserState->Aml < ParserState->AmlEnd) || (Op))
474 AmlOpStart = ParserState->Aml;
477 Status = AcpiPsCreateOp (WalkState, AmlOpStart, &Op);
478 if (ACPI_FAILURE (Status))
480 if (Status == AE_CTRL_PARSE_CONTINUE)
485 if (Status == AE_CTRL_PARSE_PENDING)
490 if (Status == AE_CTRL_TERMINATE)
492 return_ACPI_STATUS (Status);
495 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
496 if (ACPI_FAILURE (Status))
498 return_ACPI_STATUS (Status);
504 AcpiExStartTraceOpcode (Op, WalkState);
509 * Start ArgCount at zero because we don't know if there are
512 WalkState->ArgCount = 0;
514 /* Are there any arguments that must be processed? */
516 if (WalkState->ArgTypes)
520 Status = AcpiPsGetArguments (WalkState, AmlOpStart, Op);
521 if (ACPI_FAILURE (Status))
523 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
524 if (ACPI_FAILURE (Status))
526 return_ACPI_STATUS (Status);
533 /* Check for arguments that need to be processed */
535 if (WalkState->ArgCount)
538 * There are arguments (complex ones), push Op and
539 * prepare for argument
541 Status = AcpiPsPushScope (ParserState, Op,
542 WalkState->ArgTypes, WalkState->ArgCount);
543 if (ACPI_FAILURE (Status))
545 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
546 if (ACPI_FAILURE (Status))
548 return_ACPI_STATUS (Status);
559 * All arguments have been processed -- Op is complete,
562 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
563 if (WalkState->OpInfo->Flags & AML_NAMED)
565 if (Op->Common.AmlOpcode == AML_REGION_OP ||
566 Op->Common.AmlOpcode == AML_DATA_REGION_OP)
569 * Skip parsing of control method or opregion body,
570 * because we don't have enough info in the first pass
571 * to parse them correctly.
573 * Completed parsing an OpRegion declaration, we now
576 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
580 if (WalkState->OpInfo->Flags & AML_CREATE)
583 * Backup to beginning of CreateXXXfield declaration (1 for
586 * BodyLength is unknown until we parse the body
588 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
591 if (Op->Common.AmlOpcode == AML_BANK_FIELD_OP)
594 * Backup to beginning of BankField declaration
596 * BodyLength is unknown until we parse the body
598 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
601 /* This op complete, notify the dispatcher */
603 if (WalkState->AscendingCallback != NULL)
606 WalkState->Opcode = Op->Common.AmlOpcode;
608 Status = WalkState->AscendingCallback (WalkState);
609 Status = AcpiPsNextParseState (WalkState, Op, Status);
610 if (Status == AE_CTRL_PENDING)
616 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
617 if (ACPI_FAILURE (Status))
619 return_ACPI_STATUS (Status);
622 } /* while ParserState->Aml */
624 Status = AcpiPsCompleteFinalOp (WalkState, Op, Status);
625 return_ACPI_STATUS (Status);