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/acdisasm.h>
49 #include <contrib/dev/acpica/include/acdebug.h>
52 #ifdef ACPI_DISASSEMBLER
54 #define _COMPONENT ACPI_CA_DEBUGGER
55 ACPI_MODULE_NAME ("dmwalk")
58 #define DB_FULL_OP_INFO "[%4.4s] @%5.5X #%4.4X: "
60 /* Stub for non-compiler code */
62 #ifndef ACPI_ASL_COMPILER
71 /* Local prototypes */
75 ACPI_PARSE_OBJECT *Op,
81 ACPI_PARSE_OBJECT *Op,
87 ACPI_PARSE_OBJECT *Op);
90 /*******************************************************************************
92 * FUNCTION: AcpiDmDisassemble
94 * PARAMETERS: WalkState - Current state
95 * Origin - Starting object
96 * NumOpcodes - Max number of opcodes to be displayed
100 * DESCRIPTION: Disassemble parser object and its children. This is the
101 * main entry point of the disassembler.
103 ******************************************************************************/
107 ACPI_WALK_STATE *WalkState,
108 ACPI_PARSE_OBJECT *Origin,
111 ACPI_PARSE_OBJECT *Op = Origin;
112 ACPI_OP_WALK_INFO Info;
123 Info.WalkState = WalkState;
124 AcpiDmWalkParseTree (Op, AcpiDmDescendingOp, AcpiDmAscendingOp, &Info);
129 /*******************************************************************************
131 * FUNCTION: AcpiDmWalkParseTree
133 * PARAMETERS: Op - Root Op object
134 * DescendingCallback - Called during tree descent
135 * AscendingCallback - Called during tree ascent
136 * Context - To be passed to the callbacks
138 * RETURN: Status from callback(s)
140 * DESCRIPTION: Walk the entire parse tree.
142 ******************************************************************************/
145 AcpiDmWalkParseTree (
146 ACPI_PARSE_OBJECT *Op,
147 ASL_WALK_CALLBACK DescendingCallback,
148 ASL_WALK_CALLBACK AscendingCallback,
151 BOOLEAN NodePreviouslyVisited;
152 ACPI_PARSE_OBJECT *StartOp = Op;
154 ACPI_PARSE_OBJECT *Next;
155 ACPI_OP_WALK_INFO *Info = Context;
159 NodePreviouslyVisited = FALSE;
163 if (NodePreviouslyVisited)
165 if (AscendingCallback)
167 Status = AscendingCallback (Op, Info->Level, Context);
168 if (ACPI_FAILURE (Status))
176 /* Let the callback process the node */
178 Status = DescendingCallback (Op, Info->Level, Context);
179 if (ACPI_SUCCESS (Status))
181 /* Visit children first, once */
183 Next = AcpiPsGetArg (Op, 0);
191 else if (Status != AE_CTRL_DEPTH)
193 /* Exit immediately on any error */
199 /* Terminate walk at start op */
206 /* No more children, re-visit this node */
208 if (!NodePreviouslyVisited)
210 NodePreviouslyVisited = TRUE;
214 /* No more children, visit peers */
218 Op = Op->Common.Next;
219 NodePreviouslyVisited = FALSE;
223 /* No peers, re-visit parent */
225 if (Info->Level != 0 )
230 Op = Op->Common.Parent;
231 NodePreviouslyVisited = TRUE;
235 /* If we get here, the walk completed with no errors */
241 /*******************************************************************************
243 * FUNCTION: AcpiDmBlockType
245 * PARAMETERS: Op - Object to be examined
247 * RETURN: BlockType - not a block, parens, braces, or even both.
249 * DESCRIPTION: Type of block for this op (parens or braces)
251 ******************************************************************************/
255 ACPI_PARSE_OBJECT *Op)
257 const ACPI_OPCODE_INFO *OpInfo;
265 switch (Op->Common.AmlOpcode)
269 return (BLOCK_BRACE);
274 case AML_PROCESSOR_OP:
275 case AML_POWER_RES_OP:
276 case AML_THERMAL_ZONE_OP:
280 case AML_INDEX_FIELD_OP:
281 case AML_BANK_FIELD_OP:
283 return (BLOCK_PAREN | BLOCK_BRACE);
287 if ((Op->Common.DisasmOpcode == ACPI_DASM_UNICODE) ||
288 (Op->Common.DisasmOpcode == ACPI_DASM_UUID) ||
289 (Op->Common.DisasmOpcode == ACPI_DASM_PLD_METHOD))
294 /*lint -fallthrough */
297 case AML_VAR_PACKAGE_OP:
299 return (BLOCK_PAREN | BLOCK_BRACE);
303 return (BLOCK_PAREN);
305 case AML_INT_METHODCALL_OP:
307 if (Op->Common.Parent &&
308 ((Op->Common.Parent->Common.AmlOpcode == AML_PACKAGE_OP) ||
309 (Op->Common.Parent->Common.AmlOpcode == AML_VAR_PACKAGE_OP)))
311 /* This is a reference to a method, not an invocation */
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;
415 if (Op->Common.DisasmFlags & ACPI_PARSEOP_IGNORE)
417 /* Ignore this op -- it was handled elsewhere */
419 return (AE_CTRL_DEPTH);
422 /* Level 0 is at the Definition Block level */
426 /* In verbose mode, print the AML offset, opcode and depth count */
430 VERBOSE_PRINT ((DB_FULL_OP_INFO,
431 (Info->WalkState->MethodNode ?
432 Info->WalkState->MethodNode->Name.Ascii : " "),
433 Op->Common.AmlOffset, (UINT32) Op->Common.AmlOpcode));
436 if (Op->Common.AmlOpcode == AML_SCOPE_OP)
438 /* This is the beginning of the Definition Block */
440 AcpiOsPrintf ("{\n");
442 /* Emit all External() declarations here */
444 AcpiDmEmitExternals ();
448 else if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
449 (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST)) &&
450 (Op->Common.AmlOpcode != AML_INT_BYTELIST_OP))
453 * This is a first-level element of a term list,
456 switch (Op->Common.AmlOpcode)
460 * Optionally just ignore this opcode. Some tables use
461 * NoOp opcodes for "padding" out packages that the BIOS
462 * changes dynamically. This can leave hundreds or
463 * thousands of NoOp opcodes that if disassembled,
464 * cannot be compiled because they are syntactically
467 if (AcpiGbl_IgnoreNoopOperator)
469 Op->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
477 AcpiDmIndent (Level);
481 Info->LastLevel = Level;
486 * This is an inexpensive mechanism to try and keep lines from getting
487 * too long. When the limit is hit, start a new line at the previous
488 * indent plus one. A better but more expensive mechanism would be to
489 * keep track of the current column.
492 if (Info->Count /* +Info->LastLevel */ > 12)
496 AcpiDmIndent (Info->LastLevel + 1);
499 /* If ASL+ is enabled, check for a C-style operator */
501 if (AcpiDmCheckForSymbolicOpcode (Op, Info))
506 /* Print the opcode name */
508 AcpiDmDisassembleOneOp (NULL, Info, Op);
510 if ((Op->Common.DisasmOpcode == ACPI_DASM_LNOT_PREFIX) ||
511 (Op->Common.AmlOpcode == AML_INT_CONNECTION_OP))
516 if ((Op->Common.AmlOpcode == AML_NAME_OP) ||
517 (Op->Common.AmlOpcode == AML_RETURN_OP))
522 /* Start the opcode argument list if necessary */
524 OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
526 if ((OpInfo->Flags & AML_HAS_ARGS) ||
527 (Op->Common.AmlOpcode == AML_EVENT_OP))
529 /* This opcode has an argument list */
531 if (AcpiDmBlockType (Op) & BLOCK_PAREN)
536 /* If this is a named opcode, print the associated name value */
538 if (OpInfo->Flags & AML_NAMED)
540 switch (Op->Common.AmlOpcode)
544 NextOp = AcpiPsGetDepthNext (NULL, Op);
545 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
546 AcpiDmNamestring (NextOp->Common.Value.Name);
549 /*lint -fallthrough */
553 Name = AcpiPsGetName (Op);
556 AcpiDmNamestring ((char *) Op->Named.Path);
560 AcpiDmDumpName (Name);
563 if (Op->Common.AmlOpcode != AML_INT_NAMEDFIELD_OP)
565 if (AcpiGbl_DbOpt_Verbose)
567 (void) AcpiPsDisplayObjectPathname (NULL, Op);
573 switch (Op->Common.AmlOpcode)
577 AcpiDmMethodFlags (Op);
580 /* Emit description comment for Method() with a predefined ACPI name */
582 AcpiDmPredefinedDescription (Op);
587 /* Check for _HID and related EISAID() */
589 AcpiDmCheckForHardwareId (Op);
595 AcpiDmRegionFlags (Op);
598 case AML_POWER_RES_OP:
600 /* Mark the next two Ops as part of the parameter list */
603 NextOp = AcpiPsGetDepthNext (NULL, Op);
604 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
606 NextOp = NextOp->Common.Next;
607 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
610 case AML_PROCESSOR_OP:
612 /* Mark the next three Ops as part of the parameter list */
615 NextOp = AcpiPsGetDepthNext (NULL, Op);
616 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
618 NextOp = NextOp->Common.Next;
619 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
621 NextOp = NextOp->Common.Next;
622 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
626 case AML_DATA_REGION_OP:
638 case AML_THERMAL_ZONE_OP:
645 AcpiOsPrintf ("*** Unhandled named opcode %X\n",
646 Op->Common.AmlOpcode);
651 else switch (Op->Common.AmlOpcode)
654 case AML_BANK_FIELD_OP:
655 case AML_INDEX_FIELD_OP:
659 /* Name of the parent OperationRegion */
661 NextOp = AcpiPsGetDepthNext (NULL, Op);
662 AcpiDmNamestring (NextOp->Common.Value.Name);
664 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
666 switch (Op->Common.AmlOpcode)
668 case AML_BANK_FIELD_OP:
670 /* Namestring - Bank Name */
672 NextOp = AcpiPsGetDepthNext (NULL, NextOp);
673 AcpiDmNamestring (NextOp->Common.Value.Name);
674 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
678 * Bank Value. This is a TermArg in the middle of the parameter
679 * list, must handle it here.
681 * Disassemble the TermArg parse tree. ACPI_PARSEOP_PARAMLIST
682 * eliminates newline in the output.
684 NextOp = NextOp->Common.Next;
686 Info->Flags = ACPI_PARSEOP_PARAMLIST;
687 AcpiDmWalkParseTree (NextOp, AcpiDmDescendingOp,
688 AcpiDmAscendingOp, Info);
692 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
696 case AML_INDEX_FIELD_OP:
698 /* Namestring - Data Name */
700 NextOp = AcpiPsGetDepthNext (NULL, NextOp);
701 AcpiDmNamestring (NextOp->Common.Value.Name);
703 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
711 AcpiDmFieldFlags (NextOp);
716 /* The next op is the size parameter */
718 NextOp = AcpiPsGetDepthNext (NULL, Op);
721 /* Single-step support */
726 if (Op->Common.DisasmOpcode == ACPI_DASM_RESOURCE)
729 * We have a resource list. Don't need to output
730 * the buffer size Op. Open up a new block
732 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
733 NextOp = NextOp->Common.Next;
736 /* Emit description comment for Name() with a predefined ACPI name */
738 AcpiDmPredefinedDescription (Op->Asl.Parent);
741 AcpiDmIndent (Info->Level);
742 AcpiOsPrintf ("{\n");
746 /* Normal Buffer, mark size as in the parameter list */
748 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
751 case AML_VAR_PACKAGE_OP:
755 /* The next op is the size or predicate parameter */
757 NextOp = AcpiPsGetDepthNext (NULL, Op);
760 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
766 /* The next op is the size parameter */
768 NextOp = AcpiPsGetDepthNext (NULL, Op);
771 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
785 if (AcpiDmBlockType (Op) & BLOCK_BRACE)
788 AcpiDmIndent (Level);
789 AcpiOsPrintf ("{\n");
797 /*******************************************************************************
799 * FUNCTION: AcpiDmAscendingOp
801 * PARAMETERS: ASL_WALK_CALLBACK
805 * DESCRIPTION: Second visitation of a parse object, during ascent of parse
806 * tree. Close out any parameter lists and complete the opcode.
808 ******************************************************************************/
812 ACPI_PARSE_OBJECT *Op,
816 ACPI_OP_WALK_INFO *Info = Context;
817 ACPI_PARSE_OBJECT *ParentOp;
820 if (Op->Common.DisasmFlags & ACPI_PARSEOP_IGNORE)
822 /* Ignore this op -- it was handled elsewhere */
827 if ((Level == 0) && (Op->Common.AmlOpcode == AML_SCOPE_OP))
829 /* Indicates the end of the current descriptor block (table) */
831 AcpiOsPrintf ("}\n\n");
835 switch (AcpiDmBlockType (Op))
839 /* Completed an op that has arguments, add closing paren if needed */
841 AcpiDmCloseOperator (Op);
843 if (Op->Common.AmlOpcode == AML_NAME_OP)
845 /* Emit description comment for Name() with a predefined ACPI name */
847 AcpiDmPredefinedDescription (Op);
851 /* For Create* operators, attempt to emit resource tag description */
853 AcpiDmFieldPredefinedDescription (Op);
856 /* Decode Notify() values */
858 if (Op->Common.AmlOpcode == AML_NOTIFY_OP)
860 AcpiDmNotifyDescription (Op);
863 AcpiDmDisplayTargetPathname (Op);
865 /* Could be a nested operator, check if comma required */
867 if (!AcpiDmCommaIfListMember (Op))
869 if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
870 (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST)) &&
871 (Op->Common.AmlOpcode != AML_INT_BYTELIST_OP))
874 * This is a first-level element of a term list
877 if (!(Info->Flags & ACPI_PARSEOP_PARAMLIST))
886 case (BLOCK_BRACE | BLOCK_PAREN):
888 /* Completed an op that has a term list, add closing brace */
890 if (Op->Common.DisasmFlags & ACPI_PARSEOP_EMPTY_TERMLIST)
896 AcpiDmIndent (Level);
900 AcpiDmCommaIfListMember (Op);
902 if (AcpiDmBlockType (Op->Common.Parent) != BLOCK_PAREN)
905 if (!(Op->Common.DisasmFlags & ACPI_PARSEOP_EMPTY_TERMLIST))
907 if ((Op->Common.AmlOpcode == AML_IF_OP) &&
909 (Op->Common.Next->Common.AmlOpcode == AML_ELSE_OP))
914 if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
927 /* Could be a nested operator, check if comma required */
929 if (!AcpiDmCommaIfListMember (Op))
931 if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
932 (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST)) &&
933 (Op->Common.AmlOpcode != AML_INT_BYTELIST_OP))
936 * This is a first-level element of a term list
942 else if (Op->Common.Parent)
944 switch (Op->Common.Parent->Common.AmlOpcode)
947 case AML_VAR_PACKAGE_OP:
949 if (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST))
963 if (Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST)
965 if ((Op->Common.Next) &&
966 (Op->Common.Next->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST))
972 * The parent Op is guaranteed to be valid because of the flag
973 * ACPI_PARSEOP_PARAMLIST -- which means that this op is part of
974 * a parameter list and thus has a valid parent.
976 ParentOp = Op->Common.Parent;
979 * Just completed a parameter node for something like "Buffer (param)".
980 * Close the paren and open up the term list block with a brace
987 * Emit a description comment for a Name() operator that is a
988 * predefined ACPI name. Must check the grandparent.
990 ParentOp = ParentOp->Common.Parent;
992 (ParentOp->Asl.AmlOpcode == AML_NAME_OP))
994 AcpiDmPredefinedDescription (ParentOp);
998 AcpiDmIndent (Level - 1);
999 AcpiOsPrintf ("{\n");
1003 ParentOp->Common.DisasmFlags |= ACPI_PARSEOP_EMPTY_TERMLIST;
1004 AcpiOsPrintf (") {");
1008 if ((Op->Common.AmlOpcode == AML_NAME_OP) ||
1009 (Op->Common.AmlOpcode == AML_RETURN_OP))
1015 * For ASL+, check for and emit a C-style symbol. If valid, the
1016 * symbol string has been deferred until after the first operand
1018 if (AcpiGbl_CstyleDisassembly)
1020 if (Op->Asl.OperatorSymbol)
1022 AcpiOsPrintf ("%s", Op->Asl.OperatorSymbol);
1023 Op->Asl.OperatorSymbol = NULL;
1030 #endif /* ACPI_DISASSEMBLER */