1 /*******************************************************************************
3 * Module Name: dmwalk - AML disassembly tree walk
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2014, 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))
293 /*lint -fallthrough */
296 case AML_VAR_PACKAGE_OP:
298 return (BLOCK_PAREN | BLOCK_BRACE);
302 return (BLOCK_PAREN);
306 OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
307 if (OpInfo->Flags & AML_HAS_ARGS)
309 return (BLOCK_PAREN);
317 /*******************************************************************************
319 * FUNCTION: AcpiDmListType
321 * PARAMETERS: Op - Object to be examined
323 * RETURN: ListType - has commas or not.
325 * DESCRIPTION: Type of block for this op (parens or braces)
327 ******************************************************************************/
331 ACPI_PARSE_OBJECT *Op)
333 const ACPI_OPCODE_INFO *OpInfo;
341 switch (Op->Common.AmlOpcode)
348 case AML_POWER_RES_OP:
349 case AML_PROCESSOR_OP:
350 case AML_THERMAL_ZONE_OP:
354 case AML_INDEX_FIELD_OP:
355 case AML_BANK_FIELD_OP:
361 case AML_VAR_PACKAGE_OP:
363 return (BLOCK_COMMA_LIST);
367 OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
368 if (OpInfo->Flags & AML_HAS_ARGS)
370 return (BLOCK_COMMA_LIST);
378 /*******************************************************************************
380 * FUNCTION: AcpiDmDescendingOp
382 * PARAMETERS: ASL_WALK_CALLBACK
386 * DESCRIPTION: First visitation of a parse object during tree descent.
387 * Decode opcode name and begin parameter list(s), if any.
389 ******************************************************************************/
393 ACPI_PARSE_OBJECT *Op,
397 ACPI_OP_WALK_INFO *Info = Context;
398 const ACPI_OPCODE_INFO *OpInfo;
400 ACPI_PARSE_OBJECT *NextOp;
403 if (Op->Common.DisasmFlags & ACPI_PARSEOP_IGNORE)
405 /* Ignore this op -- it was handled elsewhere */
407 return (AE_CTRL_DEPTH);
410 /* Level 0 is at the Definition Block level */
414 /* In verbose mode, print the AML offset, opcode and depth count */
418 VERBOSE_PRINT ((DB_FULL_OP_INFO,
419 (Info->WalkState->MethodNode ?
420 Info->WalkState->MethodNode->Name.Ascii : " "),
421 Op->Common.AmlOffset, (UINT32) Op->Common.AmlOpcode));
424 if (Op->Common.AmlOpcode == AML_SCOPE_OP)
426 /* This is the beginning of the Definition Block */
428 AcpiOsPrintf ("{\n");
430 /* Emit all External() declarations here */
432 AcpiDmEmitExternals ();
436 else if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
437 (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST)) &&
438 (Op->Common.AmlOpcode != AML_INT_BYTELIST_OP))
441 * This is a first-level element of a term list,
444 switch (Op->Common.AmlOpcode)
448 * Optionally just ignore this opcode. Some tables use
449 * NoOp opcodes for "padding" out packages that the BIOS
450 * changes dynamically. This can leave hundreds or
451 * thousands of NoOp opcodes that if disassembled,
452 * cannot be compiled because they are syntactically
455 if (AcpiGbl_IgnoreNoopOperator)
457 Op->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
465 AcpiDmIndent (Level);
469 Info->LastLevel = Level;
474 * This is an inexpensive mechanism to try and keep lines from getting
475 * too long. When the limit is hit, start a new line at the previous
476 * indent plus one. A better but more expensive mechanism would be to
477 * keep track of the current column.
480 if (Info->Count /* +Info->LastLevel */ > 10)
484 AcpiDmIndent (Info->LastLevel + 1);
487 /* Print the opcode name */
489 AcpiDmDisassembleOneOp (NULL, Info, Op);
491 if ((Op->Common.DisasmOpcode == ACPI_DASM_LNOT_PREFIX) ||
492 (Op->Common.AmlOpcode == AML_INT_CONNECTION_OP))
497 if ((Op->Common.AmlOpcode == AML_NAME_OP) ||
498 (Op->Common.AmlOpcode == AML_RETURN_OP))
503 /* Start the opcode argument list if necessary */
505 OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
507 if ((OpInfo->Flags & AML_HAS_ARGS) ||
508 (Op->Common.AmlOpcode == AML_EVENT_OP))
510 /* This opcode has an argument list */
512 if (AcpiDmBlockType (Op) & BLOCK_PAREN)
517 /* If this is a named opcode, print the associated name value */
519 if (OpInfo->Flags & AML_NAMED)
521 switch (Op->Common.AmlOpcode)
525 NextOp = AcpiPsGetDepthNext (NULL, Op);
526 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
527 AcpiDmNamestring (NextOp->Common.Value.Name);
530 /*lint -fallthrough */
534 Name = AcpiPsGetName (Op);
537 AcpiDmNamestring ((char *) Op->Named.Path);
541 AcpiDmDumpName (Name);
544 if (Op->Common.AmlOpcode != AML_INT_NAMEDFIELD_OP)
546 if (AcpiGbl_DbOpt_verbose)
548 (void) AcpiPsDisplayObjectPathname (NULL, Op);
554 switch (Op->Common.AmlOpcode)
558 AcpiDmMethodFlags (Op);
561 /* Emit description comment for Method() with a predefined ACPI name */
563 AcpiDmPredefinedDescription (Op);
569 /* Check for _HID and related EISAID() */
571 AcpiDmCheckForHardwareId (Op);
578 AcpiDmRegionFlags (Op);
582 case AML_POWER_RES_OP:
584 /* Mark the next two Ops as part of the parameter list */
587 NextOp = AcpiPsGetDepthNext (NULL, Op);
588 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
590 NextOp = NextOp->Common.Next;
591 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
595 case AML_PROCESSOR_OP:
597 /* Mark the next three Ops as part of the parameter list */
600 NextOp = AcpiPsGetDepthNext (NULL, Op);
601 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
603 NextOp = NextOp->Common.Next;
604 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
606 NextOp = NextOp->Common.Next;
607 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
612 case AML_DATA_REGION_OP:
626 case AML_THERMAL_ZONE_OP:
634 AcpiOsPrintf ("*** Unhandled named opcode %X\n",
635 Op->Common.AmlOpcode);
640 else switch (Op->Common.AmlOpcode)
643 case AML_BANK_FIELD_OP:
644 case AML_INDEX_FIELD_OP:
648 /* Name of the parent OperationRegion */
650 NextOp = AcpiPsGetDepthNext (NULL, Op);
651 AcpiDmNamestring (NextOp->Common.Value.Name);
653 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
655 switch (Op->Common.AmlOpcode)
657 case AML_BANK_FIELD_OP:
659 /* Namestring - Bank Name */
661 NextOp = AcpiPsGetDepthNext (NULL, NextOp);
662 AcpiDmNamestring (NextOp->Common.Value.Name);
663 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
667 * Bank Value. This is a TermArg in the middle of the parameter
668 * list, must handle it here.
670 * Disassemble the TermArg parse tree. ACPI_PARSEOP_PARAMLIST
671 * eliminates newline in the output.
673 NextOp = NextOp->Common.Next;
675 Info->Flags = ACPI_PARSEOP_PARAMLIST;
676 AcpiDmWalkParseTree (NextOp, AcpiDmDescendingOp,
677 AcpiDmAscendingOp, Info);
681 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
685 case AML_INDEX_FIELD_OP:
687 /* Namestring - Data Name */
689 NextOp = AcpiPsGetDepthNext (NULL, NextOp);
690 AcpiDmNamestring (NextOp->Common.Value.Name);
692 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
700 AcpiDmFieldFlags (NextOp);
705 /* The next op is the size parameter */
707 NextOp = AcpiPsGetDepthNext (NULL, Op);
710 /* Single-step support */
715 if (Op->Common.DisasmOpcode == ACPI_DASM_RESOURCE)
718 * We have a resource list. Don't need to output
719 * the buffer size Op. Open up a new block
721 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
722 NextOp = NextOp->Common.Next;
725 /* Emit description comment for Name() with a predefined ACPI name */
727 AcpiDmPredefinedDescription (Op->Asl.Parent);
730 AcpiDmIndent (Info->Level);
731 AcpiOsPrintf ("{\n");
735 /* Normal Buffer, mark size as in the parameter list */
737 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
740 case AML_VAR_PACKAGE_OP:
744 /* The next op is the size or predicate parameter */
746 NextOp = AcpiPsGetDepthNext (NULL, Op);
749 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
755 /* The next op is the size parameter */
757 NextOp = AcpiPsGetDepthNext (NULL, Op);
760 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMLIST;
774 if (AcpiDmBlockType (Op) & BLOCK_BRACE)
777 AcpiDmIndent (Level);
778 AcpiOsPrintf ("{\n");
786 /*******************************************************************************
788 * FUNCTION: AcpiDmAscendingOp
790 * PARAMETERS: ASL_WALK_CALLBACK
794 * DESCRIPTION: Second visitation of a parse object, during ascent of parse
795 * tree. Close out any parameter lists and complete the opcode.
797 ******************************************************************************/
801 ACPI_PARSE_OBJECT *Op,
805 ACPI_OP_WALK_INFO *Info = Context;
806 ACPI_PARSE_OBJECT *ParentOp;
809 if (Op->Common.DisasmFlags & ACPI_PARSEOP_IGNORE)
811 /* Ignore this op -- it was handled elsewhere */
816 if ((Level == 0) && (Op->Common.AmlOpcode == AML_SCOPE_OP))
818 /* Indicates the end of the current descriptor block (table) */
820 AcpiOsPrintf ("}\n\n");
824 switch (AcpiDmBlockType (Op))
828 /* Completed an op that has arguments, add closing paren */
832 if (Op->Common.AmlOpcode == AML_NAME_OP)
834 /* Emit description comment for Name() with a predefined ACPI name */
836 AcpiDmPredefinedDescription (Op);
840 /* For Create* operators, attempt to emit resource tag description */
842 AcpiDmFieldPredefinedDescription (Op);
845 /* Decode Notify() values */
847 if (Op->Common.AmlOpcode == AML_NOTIFY_OP)
849 AcpiDmNotifyDescription (Op);
852 AcpiDmDisplayTargetPathname (Op);
854 /* Could be a nested operator, check if comma required */
856 if (!AcpiDmCommaIfListMember (Op))
858 if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
859 (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST)) &&
860 (Op->Common.AmlOpcode != AML_INT_BYTELIST_OP))
863 * This is a first-level element of a term list
866 if (!(Info->Flags & ACPI_PARSEOP_PARAMLIST))
875 case (BLOCK_BRACE | BLOCK_PAREN):
877 /* Completed an op that has a term list, add closing brace */
879 if (Op->Common.DisasmFlags & ACPI_PARSEOP_EMPTY_TERMLIST)
885 AcpiDmIndent (Level);
889 AcpiDmCommaIfListMember (Op);
891 if (AcpiDmBlockType (Op->Common.Parent) != BLOCK_PAREN)
894 if (!(Op->Common.DisasmFlags & ACPI_PARSEOP_EMPTY_TERMLIST))
896 if ((Op->Common.AmlOpcode == AML_IF_OP) &&
898 (Op->Common.Next->Common.AmlOpcode == AML_ELSE_OP))
903 if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
916 /* Could be a nested operator, check if comma required */
918 if (!AcpiDmCommaIfListMember (Op))
920 if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
921 (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST)) &&
922 (Op->Common.AmlOpcode != AML_INT_BYTELIST_OP))
925 * This is a first-level element of a term list
931 else if (Op->Common.Parent)
933 switch (Op->Common.Parent->Common.AmlOpcode)
936 case AML_VAR_PACKAGE_OP:
938 if (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST))
952 if (Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST)
954 if ((Op->Common.Next) &&
955 (Op->Common.Next->Common.DisasmFlags & ACPI_PARSEOP_PARAMLIST))
961 * The parent Op is guaranteed to be valid because of the flag
962 * ACPI_PARSEOP_PARAMLIST -- which means that this op is part of
963 * a parameter list and thus has a valid parent.
965 ParentOp = Op->Common.Parent;
968 * Just completed a parameter node for something like "Buffer (param)".
969 * Close the paren and open up the term list block with a brace
976 * Emit a description comment for a Name() operator that is a
977 * predefined ACPI name. Must check the grandparent.
979 ParentOp = ParentOp->Common.Parent;
981 (ParentOp->Asl.AmlOpcode == AML_NAME_OP))
983 AcpiDmPredefinedDescription (ParentOp);
987 AcpiDmIndent (Level - 1);
988 AcpiOsPrintf ("{\n");
992 ParentOp->Common.DisasmFlags |= ACPI_PARSEOP_EMPTY_TERMLIST;
993 AcpiOsPrintf (") {");
997 if ((Op->Common.AmlOpcode == AML_NAME_OP) ||
998 (Op->Common.AmlOpcode == AML_RETURN_OP))
1006 #endif /* ACPI_DISASSEMBLER */