1 /*******************************************************************************
3 * Module Name: dmwalk - AML disassembly tree walk
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.
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acparser.h>
47 #include <contrib/dev/acpica/include/amlcode.h>
48 #include <contrib/dev/acpica/include/acdebug.h>
51 #define _COMPONENT ACPI_CA_DEBUGGER
52 ACPI_MODULE_NAME ("dmwalk")
55 #define DB_FULL_OP_INFO "[%4.4s] @%5.5X #%4.4X: "
57 /* Stub for non-compiler code */
59 #ifndef ACPI_ASL_COMPILER
68 /* Local prototypes */
72 ACPI_PARSE_OBJECT *Op,
78 ACPI_PARSE_OBJECT *Op,
84 ACPI_PARSE_OBJECT *Op);
87 /*******************************************************************************
89 * FUNCTION: AcpiDmDisassemble
91 * PARAMETERS: WalkState - Current state
92 * Origin - Starting object
93 * NumOpcodes - Max number of opcodes to be displayed
97 * DESCRIPTION: Disassemble parser object and its children. This is the
98 * main entry point of the disassembler.
100 ******************************************************************************/
104 ACPI_WALK_STATE *WalkState,
105 ACPI_PARSE_OBJECT *Origin,
108 ACPI_PARSE_OBJECT *Op = Origin;
109 ACPI_OP_WALK_INFO Info;
117 memset (&Info, 0, sizeof (ACPI_OP_WALK_INFO));
118 Info.WalkState = WalkState;
119 Info.StartAml = Op->Common.Aml - sizeof (ACPI_TABLE_HEADER);
120 Info.AmlOffset = Op->Common.Aml - Info.StartAml;
122 AcpiDmWalkParseTree (Op, AcpiDmDescendingOp, AcpiDmAscendingOp, &Info);
127 /*******************************************************************************
129 * FUNCTION: AcpiDmWalkParseTree
131 * PARAMETERS: Op - Root Op object
132 * DescendingCallback - Called during tree descent
133 * AscendingCallback - Called during tree ascent
134 * Context - To be passed to the callbacks
136 * RETURN: Status from callback(s)
138 * DESCRIPTION: Walk the entire parse tree.
140 ******************************************************************************/
143 AcpiDmWalkParseTree (
144 ACPI_PARSE_OBJECT *Op,
145 ASL_WALK_CALLBACK DescendingCallback,
146 ASL_WALK_CALLBACK AscendingCallback,
149 BOOLEAN NodePreviouslyVisited;
150 ACPI_PARSE_OBJECT *StartOp = Op;
152 ACPI_PARSE_OBJECT *Next;
153 ACPI_OP_WALK_INFO *Info = Context;
157 NodePreviouslyVisited = FALSE;
161 if (NodePreviouslyVisited)
163 if (AscendingCallback)
165 Status = AscendingCallback (Op, Info->Level, Context);
166 if (ACPI_FAILURE (Status))
174 /* Let the callback process the node */
176 Status = DescendingCallback (Op, Info->Level, Context);
177 if (ACPI_SUCCESS (Status))
179 /* Visit children first, once */
181 Next = AcpiPsGetArg (Op, 0);
189 else if (Status != AE_CTRL_DEPTH)
191 /* Exit immediately on any error */
197 /* Terminate walk at start op */
204 /* No more children, re-visit this node */
206 if (!NodePreviouslyVisited)
208 NodePreviouslyVisited = TRUE;
212 /* No more children, visit peers */
216 Op = Op->Common.Next;
217 NodePreviouslyVisited = FALSE;
221 /* No peers, re-visit parent */
223 if (Info->Level != 0 )
228 Op = Op->Common.Parent;
229 NodePreviouslyVisited = TRUE;
233 /* If we get here, the walk completed with no errors */
239 /*******************************************************************************
241 * FUNCTION: AcpiDmBlockType
243 * PARAMETERS: Op - Object to be examined
245 * RETURN: BlockType - not a block, parens, braces, or even both.
247 * DESCRIPTION: Type of block for this op (parens or braces)
249 ******************************************************************************/
253 ACPI_PARSE_OBJECT *Op)
255 const ACPI_OPCODE_INFO *OpInfo;
263 switch (Op->Common.AmlOpcode)
267 return (BLOCK_BRACE);
272 case AML_PROCESSOR_OP:
273 case AML_POWER_RES_OP:
274 case AML_THERMAL_ZONE_OP:
278 case AML_INDEX_FIELD_OP:
279 case AML_BANK_FIELD_OP:
281 return (BLOCK_PAREN | BLOCK_BRACE);
285 if ((Op->Common.DisasmOpcode == ACPI_DASM_UNICODE) ||
286 (Op->Common.DisasmOpcode == ACPI_DASM_UUID) ||
287 (Op->Common.DisasmOpcode == ACPI_DASM_PLD_METHOD))
292 /*lint -fallthrough */
295 case AML_VAR_PACKAGE_OP:
297 return (BLOCK_PAREN | BLOCK_BRACE);
301 return (BLOCK_PAREN);
303 case AML_INT_METHODCALL_OP:
305 if (Op->Common.Parent &&
306 ((Op->Common.Parent->Common.AmlOpcode == AML_PACKAGE_OP) ||
307 (Op->Common.Parent->Common.AmlOpcode == AML_VAR_PACKAGE_OP)))
309 /* This is a reference to a method, not an invocation */
314 /*lint -fallthrough */
318 OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
319 if (OpInfo->Flags & AML_HAS_ARGS)
321 return (BLOCK_PAREN);
329 /*******************************************************************************
331 * FUNCTION: AcpiDmListType
333 * PARAMETERS: Op - Object to be examined
335 * RETURN: ListType - has commas or not.
337 * DESCRIPTION: Type of block for this op (parens or braces)
339 ******************************************************************************/
343 ACPI_PARSE_OBJECT *Op)
345 const ACPI_OPCODE_INFO *OpInfo;
353 switch (Op->Common.AmlOpcode)
360 case AML_POWER_RES_OP:
361 case AML_PROCESSOR_OP:
362 case AML_THERMAL_ZONE_OP:
366 case AML_INDEX_FIELD_OP:
367 case AML_BANK_FIELD_OP:
373 case AML_VAR_PACKAGE_OP:
375 return (BLOCK_COMMA_LIST);
379 OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
380 if (OpInfo->Flags & AML_HAS_ARGS)
382 return (BLOCK_COMMA_LIST);
390 /*******************************************************************************
392 * FUNCTION: AcpiDmDescendingOp
394 * PARAMETERS: ASL_WALK_CALLBACK
398 * DESCRIPTION: First visitation of a parse object during tree descent.
399 * Decode opcode name and begin parameter list(s), if any.
401 ******************************************************************************/
405 ACPI_PARSE_OBJECT *Op,
409 ACPI_OP_WALK_INFO *Info = Context;
410 const ACPI_OPCODE_INFO *OpInfo;
412 ACPI_PARSE_OBJECT *NextOp;
416 OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
418 /* Listing support to dump the AML code after the ASL statement */
420 if (AcpiGbl_DmOpt_Listing)
422 /* We only care about these classes of objects */
424 if ((OpInfo->Class == AML_CLASS_NAMED_OBJECT) ||
425 (OpInfo->Class == AML_CLASS_CONTROL) ||
426 (OpInfo->Class == AML_CLASS_CREATE) ||
427 ((OpInfo->Class == AML_CLASS_EXECUTE) && (!Op->Common.Next)))
429 if (AcpiGbl_DmOpt_Listing && Info->PreviousAml)
431 /* Dump the AML byte code for the previous Op */
433 if (Op->Common.Aml > Info->PreviousAml)
437 (Info->StartAml + Info->AmlOffset),
438 (Op->Common.Aml - Info->PreviousAml),
444 Info->AmlOffset = (Op->Common.Aml - Info->StartAml);
447 Info->PreviousAml = Op->Common.Aml;
451 if (Op->Common.DisasmFlags & ACPI_PARSEOP_IGNORE)
453 /* Ignore this op -- it was handled elsewhere */
455 return (AE_CTRL_DEPTH);
458 /* Level 0 is at the Definition Block level */
462 /* In verbose mode, print the AML offset, opcode and depth count */
466 AmlOffset = (UINT32) ACPI_PTR_DIFF (Op->Common.Aml,
467 Info->WalkState->ParserState.AmlStart);
468 if (AcpiGbl_DmOpt_Verbose)
470 AcpiOsPrintf (DB_FULL_OP_INFO,
471 (Info->WalkState->MethodNode ?
472 Info->WalkState->MethodNode->Name.Ascii : " "),
473 AmlOffset, (UINT32) Op->Common.AmlOpcode);
477 if (Op->Common.AmlOpcode == AML_SCOPE_OP)
479 /* This is the beginning of the Definition Block */
481 AcpiOsPrintf ("{\n");
483 /* Emit all External() declarations here */
485 AcpiDmEmitExternals ();
489 else if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
490 (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST)) &&
491 (Op->Common.AmlOpcode != AML_INT_BYTELIST_OP))
494 * This is a first-level element of a term list,
497 switch (Op->Common.AmlOpcode)
501 * Optionally just ignore this opcode. Some tables use
502 * NoOp opcodes for "padding" out packages that the BIOS
503 * changes dynamically. This can leave hundreds or
504 * thousands of NoOp opcodes that if disassembled,
505 * cannot be compiled because they are syntactically
508 if (AcpiGbl_IgnoreNoopOperator)
510 Op->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
518 AcpiDmIndent (Level);
522 Info->LastLevel = Level;
527 * This is an inexpensive mechanism to try and keep lines from getting
528 * too long. When the limit is hit, start a new line at the previous
529 * indent plus one. A better but more expensive mechanism would be to
530 * keep track of the current column.
533 if (Info->Count /* +Info->LastLevel */ > 12)
537 AcpiDmIndent (Info->LastLevel + 1);
540 /* If ASL+ is enabled, check for a C-style operator */
542 if (AcpiDmCheckForSymbolicOpcode (Op, Info))
547 /* Print the opcode name */
549 AcpiDmDisassembleOneOp (NULL, Info, Op);
551 if ((Op->Common.DisasmOpcode == ACPI_DASM_LNOT_PREFIX) ||
552 (Op->Common.AmlOpcode == AML_INT_CONNECTION_OP))
557 if ((Op->Common.AmlOpcode == AML_NAME_OP) ||
558 (Op->Common.AmlOpcode == AML_RETURN_OP))
563 /* Start the opcode argument list if necessary */
565 if ((OpInfo->Flags & AML_HAS_ARGS) ||
566 (Op->Common.AmlOpcode == AML_EVENT_OP))
568 /* This opcode has an argument list */
570 if (AcpiDmBlockType (Op) & BLOCK_PAREN)
575 /* If this is a named opcode, print the associated name value */
577 if (OpInfo->Flags & AML_NAMED)
579 switch (Op->Common.AmlOpcode)
583 NextOp = AcpiPsGetDepthNext (NULL, Op);
584 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
585 AcpiDmNamestring (NextOp->Common.Value.Name);
588 /*lint -fallthrough */
592 Name = AcpiPsGetName (Op);
595 AcpiDmNamestring ((char *) Op->Named.Path);
599 AcpiDmDumpName (Name);
602 if (Op->Common.AmlOpcode != AML_INT_NAMEDFIELD_OP)
604 if (AcpiGbl_DmOpt_Verbose)
606 (void) AcpiPsDisplayObjectPathname (NULL, Op);
612 switch (Op->Common.AmlOpcode)
616 AcpiDmMethodFlags (Op);
619 /* Emit description comment for Method() with a predefined ACPI name */
621 AcpiDmPredefinedDescription (Op);
626 /* Check for _HID and related EISAID() */
628 AcpiDmCheckForHardwareId (Op);
634 AcpiDmRegionFlags (Op);
637 case AML_POWER_RES_OP:
639 /* Mark the next two Ops as part of the parameter list */
642 NextOp = AcpiPsGetDepthNext (NULL, Op);
643 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
645 NextOp = NextOp->Common.Next;
646 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
649 case AML_PROCESSOR_OP:
651 /* Mark the next three Ops as part of the parameter list */
654 NextOp = AcpiPsGetDepthNext (NULL, Op);
655 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
657 NextOp = NextOp->Common.Next;
658 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
660 NextOp = NextOp->Common.Next;
661 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
665 case AML_DATA_REGION_OP:
677 case AML_THERMAL_ZONE_OP:
684 AcpiOsPrintf ("*** Unhandled named opcode %X\n",
685 Op->Common.AmlOpcode);
690 else switch (Op->Common.AmlOpcode)
693 case AML_BANK_FIELD_OP:
694 case AML_INDEX_FIELD_OP:
698 /* Name of the parent OperationRegion */
700 NextOp = AcpiPsGetDepthNext (NULL, Op);
701 AcpiDmNamestring (NextOp->Common.Value.Name);
703 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
705 switch (Op->Common.AmlOpcode)
707 case AML_BANK_FIELD_OP:
709 /* Namestring - Bank Name */
711 NextOp = AcpiPsGetDepthNext (NULL, NextOp);
712 AcpiDmNamestring (NextOp->Common.Value.Name);
713 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
717 * Bank Value. This is a TermArg in the middle of the parameter
718 * list, must handle it here.
720 * Disassemble the TermArg parse tree. ACPI_PARSEOP_PARAMLIST
721 * eliminates newline in the output.
723 NextOp = NextOp->Common.Next;
725 Info->Flags = ACPI_PARSEOP_PARAMLIST;
726 AcpiDmWalkParseTree (NextOp, AcpiDmDescendingOp,
727 AcpiDmAscendingOp, Info);
731 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
735 case AML_INDEX_FIELD_OP:
737 /* Namestring - Data Name */
739 NextOp = AcpiPsGetDepthNext (NULL, NextOp);
740 AcpiDmNamestring (NextOp->Common.Value.Name);
742 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
750 AcpiDmFieldFlags (NextOp);
755 /* The next op is the size parameter */
757 NextOp = AcpiPsGetDepthNext (NULL, Op);
760 /* Single-step support */
765 if (Op->Common.DisasmOpcode == ACPI_DASM_RESOURCE)
768 * We have a resource list. Don't need to output
769 * the buffer size Op. Open up a new block
771 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
772 NextOp = NextOp->Common.Next;
775 /* Emit description comment for Name() with a predefined ACPI name */
777 AcpiDmPredefinedDescription (Op->Asl.Parent);
780 AcpiDmIndent (Info->Level);
781 AcpiOsPrintf ("{\n");
785 /* Normal Buffer, mark size as in the parameter list */
787 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
790 case AML_VAR_PACKAGE_OP:
794 /* The next op is the size or predicate parameter */
796 NextOp = AcpiPsGetDepthNext (NULL, Op);
799 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
805 /* The next op is the size parameter */
807 NextOp = AcpiPsGetDepthNext (NULL, Op);
810 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
824 if (AcpiDmBlockType (Op) & BLOCK_BRACE)
827 AcpiDmIndent (Level);
828 AcpiOsPrintf ("{\n");
836 /*******************************************************************************
838 * FUNCTION: AcpiDmAscendingOp
840 * PARAMETERS: ASL_WALK_CALLBACK
844 * DESCRIPTION: Second visitation of a parse object, during ascent of parse
845 * tree. Close out any parameter lists and complete the opcode.
847 ******************************************************************************/
851 ACPI_PARSE_OBJECT *Op,
855 ACPI_OP_WALK_INFO *Info = Context;
856 ACPI_PARSE_OBJECT *ParentOp;
859 if (Op->Common.DisasmFlags & ACPI_PARSEOP_IGNORE)
861 /* Ignore this op -- it was handled elsewhere */
866 if ((Level == 0) && (Op->Common.AmlOpcode == AML_SCOPE_OP))
868 /* Indicates the end of the current descriptor block (table) */
870 AcpiOsPrintf ("}\n\n");
874 switch (AcpiDmBlockType (Op))
878 /* Completed an op that has arguments, add closing paren if needed */
880 AcpiDmCloseOperator (Op);
882 if (Op->Common.AmlOpcode == AML_NAME_OP)
884 /* Emit description comment for Name() with a predefined ACPI name */
886 AcpiDmPredefinedDescription (Op);
890 /* For Create* operators, attempt to emit resource tag description */
892 AcpiDmFieldPredefinedDescription (Op);
895 /* Decode Notify() values */
897 if (Op->Common.AmlOpcode == AML_NOTIFY_OP)
899 AcpiDmNotifyDescription (Op);
902 AcpiDmDisplayTargetPathname (Op);
904 /* Could be a nested operator, check if comma required */
906 if (!AcpiDmCommaIfListMember (Op))
908 if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
909 (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST)) &&
910 (Op->Common.AmlOpcode != AML_INT_BYTELIST_OP))
913 * This is a first-level element of a term list
916 if (!(Info->Flags & ACPI_PARSEOP_PARAMLIST))
925 case (BLOCK_BRACE | BLOCK_PAREN):
927 /* Completed an op that has a term list, add closing brace */
929 if (Op->Common.DisasmFlags & ACPI_PARSEOP_EMPTY_TERMLIST)
935 AcpiDmIndent (Level);
939 AcpiDmCommaIfListMember (Op);
941 if (AcpiDmBlockType (Op->Common.Parent) != BLOCK_PAREN)
944 if (!(Op->Common.DisasmFlags & ACPI_PARSEOP_EMPTY_TERMLIST))
946 if ((Op->Common.AmlOpcode == AML_IF_OP) &&
948 (Op->Common.Next->Common.AmlOpcode == AML_ELSE_OP))
953 if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
966 /* Could be a nested operator, check if comma required */
968 if (!AcpiDmCommaIfListMember (Op))
970 if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
971 (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST)) &&
972 (Op->Common.AmlOpcode != AML_INT_BYTELIST_OP))
975 * This is a first-level element of a term list
981 else if (Op->Common.Parent)
983 switch (Op->Common.Parent->Common.AmlOpcode)
986 case AML_VAR_PACKAGE_OP:
988 if (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST))
1002 if (Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST)
1004 if ((Op->Common.Next) &&
1005 (Op->Common.Next->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST))
1011 * The parent Op is guaranteed to be valid because of the flag
1012 * ACPI_PARSEOP_PARAMLIST -- which means that this op is part of
1013 * a parameter list and thus has a valid parent.
1015 ParentOp = Op->Common.Parent;
1018 * Just completed a parameter node for something like "Buffer (param)".
1019 * Close the paren and open up the term list block with a brace
1021 if (Op->Common.Next)
1026 * Emit a description comment for a Name() operator that is a
1027 * predefined ACPI name. Must check the grandparent.
1029 ParentOp = ParentOp->Common.Parent;
1031 (ParentOp->Asl.AmlOpcode == AML_NAME_OP))
1033 AcpiDmPredefinedDescription (ParentOp);
1036 AcpiOsPrintf ("\n");
1037 AcpiDmIndent (Level - 1);
1038 AcpiOsPrintf ("{\n");
1042 ParentOp->Common.DisasmFlags |= ACPI_PARSEOP_EMPTY_TERMLIST;
1043 AcpiOsPrintf (") {");
1047 if ((Op->Common.AmlOpcode == AML_NAME_OP) ||
1048 (Op->Common.AmlOpcode == AML_RETURN_OP))
1054 * For ASL+, check for and emit a C-style symbol. If valid, the
1055 * symbol string has been deferred until after the first operand
1057 if (AcpiGbl_CstyleDisassembly)
1059 if (Op->Asl.OperatorSymbol)
1061 AcpiOsPrintf ("%s", Op->Asl.OperatorSymbol);
1062 Op->Asl.OperatorSymbol = NULL;