xref: /freebsd/sys/contrib/dev/acpica/common/adwalk.c (revision aa24f48b361effe51163877d84f1b70d32b77e04)
1 /******************************************************************************
2  *
3  * Module Name: adwalk - Application-level disassembler parse tree walk routines
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include <contrib/dev/acpica/include/acpi.h>
153 #include <contrib/dev/acpica/include/accommon.h>
154 #include <contrib/dev/acpica/include/acparser.h>
155 #include <contrib/dev/acpica/include/amlcode.h>
156 #include <contrib/dev/acpica/include/acdisasm.h>
157 #include <contrib/dev/acpica/include/acdispat.h>
158 #include <contrib/dev/acpica/include/acnamesp.h>
159 #include <contrib/dev/acpica/include/acapps.h>
160 
161 
162 #define _COMPONENT          ACPI_TOOLS
163         ACPI_MODULE_NAME    ("adwalk")
164 
165 /*
166  * aslmap - opcode mappings and reserved method names
167  */
168 ACPI_OBJECT_TYPE
169 AslMapNamedOpcodeToDataType (
170     UINT16                  Opcode);
171 
172 /* Local prototypes */
173 
174 static ACPI_STATUS
175 AcpiDmFindOrphanDescending (
176     ACPI_PARSE_OBJECT       *Op,
177     UINT32                  Level,
178     void                    *Context);
179 
180 static ACPI_STATUS
181 AcpiDmDumpDescending (
182     ACPI_PARSE_OBJECT       *Op,
183     UINT32                  Level,
184     void                    *Context);
185 
186 static ACPI_STATUS
187 AcpiDmXrefDescendingOp (
188     ACPI_PARSE_OBJECT       *Op,
189     UINT32                  Level,
190     void                    *Context);
191 
192 static ACPI_STATUS
193 AcpiDmCommonAscendingOp (
194     ACPI_PARSE_OBJECT       *Op,
195     UINT32                  Level,
196     void                    *Context);
197 
198 static ACPI_STATUS
199 AcpiDmLoadDescendingOp (
200     ACPI_PARSE_OBJECT       *Op,
201     UINT32                  Level,
202     void                    *Context);
203 
204 static UINT32
205 AcpiDmInspectPossibleArgs (
206     UINT32                  CurrentOpArgCount,
207     UINT32                  TargetCount,
208     ACPI_PARSE_OBJECT       *Op);
209 
210 static ACPI_STATUS
211 AcpiDmCommonDescendingOp (
212     ACPI_PARSE_OBJECT       *Op,
213     UINT32                  Level,
214     void                    *Context);
215 
216 static ACPI_STATUS
217 AcpiDmProcessResourceDescriptors (
218     ACPI_PARSE_OBJECT       *Op,
219     UINT32                  Level,
220     void                    *Context);
221 
222 /*******************************************************************************
223  *
224  * FUNCTION:    AcpiDmDumpTree
225  *
226  * PARAMETERS:  Origin              - Starting object
227  *
228  * RETURN:      None
229  *
230  * DESCRIPTION: Parse tree walk to format and output the nodes
231  *
232  ******************************************************************************/
233 
234 void
235 AcpiDmDumpTree (
236     ACPI_PARSE_OBJECT       *Origin)
237 {
238     ACPI_OP_WALK_INFO       Info;
239 
240 
241     if (!Origin)
242     {
243         return;
244     }
245 
246     AcpiOsPrintf ("/*\nAML Parse Tree\n\n");
247     Info.Flags = 0;
248     Info.Count = 0;
249     Info.Level = 0;
250     Info.WalkState = NULL;
251 
252     AcpiDmWalkParseTree (Origin, AcpiDmDumpDescending, NULL, &Info);
253     AcpiOsPrintf ("*/\n\n");
254 }
255 
256 
257 /*******************************************************************************
258  *
259  * FUNCTION:    AcpiDmFindOrphanMethods
260  *
261  * PARAMETERS:  Origin              - Starting object
262  *
263  * RETURN:      None
264  *
265  * DESCRIPTION: Parse tree walk to find "orphaned" method invocations -- methods
266  *              that are not resolved in the namespace
267  *
268  ******************************************************************************/
269 
270 void
271 AcpiDmFindOrphanMethods (
272     ACPI_PARSE_OBJECT       *Origin)
273 {
274     ACPI_OP_WALK_INFO       Info;
275 
276 
277     if (!Origin)
278     {
279         return;
280     }
281 
282     Info.Flags = 0;
283     Info.Level = 0;
284     Info.WalkState = NULL;
285 
286     AcpiDmWalkParseTree (Origin, AcpiDmFindOrphanDescending, NULL, &Info);
287 }
288 
289 
290 /*******************************************************************************
291  *
292  * FUNCTION:    AcpiDmFinishNamespaceLoad
293  *
294  * PARAMETERS:  ParseTreeRoot       - Root of the parse tree
295  *              NamespaceRoot       - Root of the internal namespace
296  *              OwnerId             - OwnerId of the table to be disassembled
297  *
298  * RETURN:      None
299  *
300  * DESCRIPTION: Load all namespace items that are created within control
301  *              methods. Used before namespace cross reference
302  *
303  ******************************************************************************/
304 
305 void
306 AcpiDmFinishNamespaceLoad (
307     ACPI_PARSE_OBJECT       *ParseTreeRoot,
308     ACPI_NAMESPACE_NODE     *NamespaceRoot,
309     ACPI_OWNER_ID           OwnerId)
310 {
311     ACPI_STATUS             Status;
312     ACPI_OP_WALK_INFO       Info;
313     ACPI_WALK_STATE         *WalkState;
314 
315 
316     if (!ParseTreeRoot)
317     {
318         return;
319     }
320 
321     /* Create and initialize a new walk state */
322 
323     WalkState = AcpiDsCreateWalkState (OwnerId, ParseTreeRoot, NULL, NULL);
324     if (!WalkState)
325     {
326         return;
327     }
328 
329     Status = AcpiDsScopeStackPush (NamespaceRoot, NamespaceRoot->Type,
330         WalkState);
331     if (ACPI_FAILURE (Status))
332     {
333         return;
334     }
335 
336     Info.Flags = 0;
337     Info.Level = 0;
338     Info.WalkState = WalkState;
339 
340     AcpiDmWalkParseTree (ParseTreeRoot, AcpiDmLoadDescendingOp,
341         AcpiDmCommonAscendingOp, &Info);
342     ACPI_FREE (WalkState);
343 }
344 
345 
346 /*******************************************************************************
347  *
348  * FUNCTION:    AcpiDmCrossReferenceNamespace
349  *
350  * PARAMETERS:  ParseTreeRoot       - Root of the parse tree
351  *              NamespaceRoot       - Root of the internal namespace
352  *              OwnerId             - OwnerId of the table to be disassembled
353  *
354  * RETURN:      None
355  *
356  * DESCRIPTION: Cross reference the namespace to create externals
357  *
358  ******************************************************************************/
359 
360 void
361 AcpiDmCrossReferenceNamespace (
362     ACPI_PARSE_OBJECT       *ParseTreeRoot,
363     ACPI_NAMESPACE_NODE     *NamespaceRoot,
364     ACPI_OWNER_ID           OwnerId)
365 {
366     ACPI_STATUS             Status;
367     ACPI_OP_WALK_INFO       Info;
368     ACPI_WALK_STATE         *WalkState;
369 
370 
371     if (!ParseTreeRoot)
372     {
373         return;
374     }
375 
376     /* Create and initialize a new walk state */
377 
378     WalkState = AcpiDsCreateWalkState (OwnerId, ParseTreeRoot, NULL, NULL);
379     if (!WalkState)
380     {
381         return;
382     }
383 
384     Status = AcpiDsScopeStackPush (NamespaceRoot, NamespaceRoot->Type,
385         WalkState);
386     if (ACPI_FAILURE (Status))
387     {
388         return;
389     }
390 
391     Info.Flags = 0;
392     Info.Level = 0;
393     Info.WalkState = WalkState;
394 
395     AcpiDmWalkParseTree (ParseTreeRoot, AcpiDmXrefDescendingOp,
396         AcpiDmCommonAscendingOp, &Info);
397     ACPI_FREE (WalkState);
398 }
399 
400 
401 /*******************************************************************************
402  *
403  * FUNCTION:    AcpiDmConvertParseObjects
404  *
405  * PARAMETERS:  ParseTreeRoot       - Root of the parse tree
406  *              NamespaceRoot       - Root of the internal namespace
407  *
408  * RETURN:      None
409  *
410  * DESCRIPTION: Begin parse tree walk to perform conversions needed for
411  *              disassembly. These include resource descriptors and switch/case
412  *              operations.
413  *
414  ******************************************************************************/
415 
416 void
417 AcpiDmConvertParseObjects (
418     ACPI_PARSE_OBJECT       *ParseTreeRoot,
419     ACPI_NAMESPACE_NODE     *NamespaceRoot)
420 {
421     ACPI_STATUS             Status;
422     ACPI_OP_WALK_INFO       Info;
423     ACPI_WALK_STATE         *WalkState;
424 
425 
426     if (!ParseTreeRoot)
427     {
428         return;
429     }
430 
431     /* Create and initialize a new walk state */
432 
433     WalkState = AcpiDsCreateWalkState (0, ParseTreeRoot, NULL, NULL);
434     if (!WalkState)
435     {
436         return;
437     }
438 
439     Status = AcpiDsScopeStackPush (NamespaceRoot, NamespaceRoot->Type,
440         WalkState);
441     if (ACPI_FAILURE (Status))
442     {
443         ACPI_FREE (WalkState);
444         return;
445     }
446 
447     Info.Flags = 0;
448     Info.Level = 0;
449     Info.WalkState = WalkState;
450 
451     AcpiDmWalkParseTree (ParseTreeRoot, AcpiDmCommonDescendingOp,
452         AcpiDmCommonAscendingOp, &Info);
453     ACPI_FREE (WalkState);
454 
455     if (AcpiGbl_TempListHead) {
456         AcpiDmClearTempList();
457     }
458 
459     return;
460 }
461 
462 
463 /*******************************************************************************
464  *
465  * FUNCTION:    AcpiDmDumpDescending
466  *
467  * PARAMETERS:  ASL_WALK_CALLBACK
468  *
469  * RETURN:      Status
470  *
471  * DESCRIPTION: Format and print contents of one parse Op.
472  *
473  ******************************************************************************/
474 
475 static ACPI_STATUS
476 AcpiDmDumpDescending (
477     ACPI_PARSE_OBJECT       *Op,
478     UINT32                  Level,
479     void                    *Context)
480 {
481     ACPI_OP_WALK_INFO       *Info = Context;
482     char                    *Path;
483 
484 
485     if (!Op)
486     {
487         return (AE_OK);
488     }
489 
490     /* Most of the information (count, level, name) here */
491 
492     Info->Count++;
493     AcpiOsPrintf ("% 5d [%2.2d] ", Info->Count, Level);
494     AcpiDmIndent (Level);
495     AcpiOsPrintf ("%-28s", AcpiPsGetOpcodeName (Op->Common.AmlOpcode));
496 
497     /* Extra info is helpful */
498 
499     switch (Op->Common.AmlOpcode)
500     {
501     case AML_BYTE_OP:
502 
503         AcpiOsPrintf ("%2.2X", (UINT32) Op->Common.Value.Integer);
504         break;
505 
506     case AML_WORD_OP:
507 
508         AcpiOsPrintf ("%4.4X", (UINT32) Op->Common.Value.Integer);
509         break;
510 
511     case AML_DWORD_OP:
512 
513         AcpiOsPrintf ("%8.8X", (UINT32) Op->Common.Value.Integer);
514         break;
515 
516     case AML_QWORD_OP:
517 
518         AcpiOsPrintf ("%8.8X%8.8X", ACPI_FORMAT_UINT64 (Op->Common.Value.Integer));
519         break;
520 
521     case AML_INT_NAMEPATH_OP:
522 
523         if (Op->Common.Value.String)
524         {
525             AcpiNsExternalizeName (ACPI_UINT32_MAX, Op->Common.Value.String,
526                 NULL, &Path);
527             AcpiOsPrintf ("%s %p", Path, Op->Common.Node);
528             ACPI_FREE (Path);
529         }
530         else
531         {
532             AcpiOsPrintf ("[NULL]");
533         }
534         break;
535 
536     case AML_NAME_OP:
537     case AML_METHOD_OP:
538     case AML_DEVICE_OP:
539     case AML_INT_NAMEDFIELD_OP:
540 
541         AcpiOsPrintf ("%4.4s", ACPI_CAST_PTR (char, &Op->Named.Name));
542         break;
543 
544     default:
545 
546         break;
547     }
548 
549     AcpiOsPrintf ("\n");
550     return (AE_OK);
551 }
552 
553 
554 /*******************************************************************************
555  *
556  * FUNCTION:    AcpiDmFindOrphanDescending
557  *
558  * PARAMETERS:  ASL_WALK_CALLBACK
559  *
560  * RETURN:      Status
561  *
562  * DESCRIPTION: Check namepath Ops for orphaned method invocations
563  *
564  * Note: Parts of this are experimental, under possible further development.
565  *
566  ******************************************************************************/
567 
568 static ACPI_STATUS
569 AcpiDmFindOrphanDescending (
570     ACPI_PARSE_OBJECT       *Op,
571     UINT32                  Level,
572     void                    *Context)
573 {
574     const ACPI_OPCODE_INFO  *OpInfo;
575     ACPI_PARSE_OBJECT       *ChildOp;
576     ACPI_PARSE_OBJECT       *NextOp;
577     ACPI_PARSE_OBJECT       *ParentOp;
578     UINT32                  ArgCount;
579 
580 
581     if (!Op)
582     {
583         return (AE_OK);
584     }
585 
586     OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
587 
588     switch (Op->Common.AmlOpcode)
589     {
590 #ifdef ACPI_UNDER_DEVELOPMENT
591     case AML_ADD_OP:
592 
593         ChildOp = Op->Common.Value.Arg;
594         if ((ChildOp->Common.AmlOpcode == AML_INT_NAMEPATH_OP) &&
595             !ChildOp->Common.Node)
596         {
597             AcpiNsExternalizeName (ACPI_UINT32_MAX, ChildOp->Common.Value.String,
598                 NULL, &Path);
599             AcpiOsPrintf ("/* %-16s A-NAMEPATH: %s  */\n",
600                 Op->Common.AmlOpName, Path);
601             ACPI_FREE (Path);
602 
603             NextOp = Op->Common.Next;
604             if (!NextOp)
605             {
606                 /* This NamePath has no args, assume it is an integer */
607 
608                 AcpiDmAddOpToExternalList (ChildOp,
609                     ChildOp->Common.Value.String, ACPI_TYPE_INTEGER, 0, 0);
610                 return (AE_OK);
611             }
612 
613             ArgCount = AcpiDmInspectPossibleArgs (3, 1, NextOp);
614             AcpiOsPrintf ("/* A-CHILDREN: %u Actual %u */\n",
615                 ArgCount, AcpiDmCountChildren (Op));
616 
617             if (ArgCount < 1)
618             {
619                 /* One Arg means this is just a Store(Name,Target) */
620 
621                 AcpiDmAddOpToExternalList (ChildOp,
622                     ChildOp->Common.Value.String, ACPI_TYPE_INTEGER, 0, 0);
623                 return (AE_OK);
624             }
625 
626             AcpiDmAddOpToExternalList (ChildOp,
627                 ChildOp->Common.Value.String, ACPI_TYPE_METHOD, ArgCount, 0);
628         }
629         break;
630 
631 #endif
632 
633     case AML_STORE_OP:
634 
635         ChildOp = Op->Common.Value.Arg;
636         if ((ChildOp->Common.AmlOpcode == AML_INT_NAMEPATH_OP) &&
637             !ChildOp->Common.Node)
638         {
639             NextOp = Op->Common.Next;
640             if (!NextOp)
641             {
642                 /* This NamePath has no args, assume it is an integer */
643 
644                 AcpiDmAddOpToExternalList (ChildOp,
645                     ChildOp->Common.Value.String, ACPI_TYPE_INTEGER, 0, 0);
646                 return (AE_OK);
647             }
648 
649             ArgCount = AcpiDmInspectPossibleArgs (2, 1, NextOp);
650             if (ArgCount <= 1)
651             {
652                 /* One Arg means this is just a Store(Name,Target) */
653 
654                 AcpiDmAddOpToExternalList (ChildOp,
655                     ChildOp->Common.Value.String, ACPI_TYPE_INTEGER, ArgCount, 0);
656                 return (AE_OK);
657             }
658 
659             AcpiDmAddOpToExternalList (ChildOp,
660                 ChildOp->Common.Value.String, ACPI_TYPE_METHOD, ArgCount, 0);
661         }
662         break;
663 
664     case AML_INT_NAMEPATH_OP:
665 
666         /* Must examine parent to see if this namepath is an argument */
667 
668         ParentOp = Op->Common.Parent;
669         OpInfo = AcpiPsGetOpcodeInfo (ParentOp->Common.AmlOpcode);
670 
671         if ((OpInfo->Class != AML_CLASS_EXECUTE) &&
672             (OpInfo->Class != AML_CLASS_CREATE) &&
673             (OpInfo->ObjectType != ACPI_TYPE_LOCAL_ALIAS) &&
674             (ParentOp->Common.AmlOpcode != AML_INT_METHODCALL_OP) &&
675             !Op->Common.Node)
676         {
677             ArgCount = AcpiDmInspectPossibleArgs (0, 0, Op);
678 
679             /*
680              * Check if namepath is a predicate for if/while or lone parameter to
681              * a return.
682              */
683             if (ArgCount == 0)
684             {
685                 if (((ParentOp->Common.AmlOpcode == AML_IF_OP) ||
686                      (ParentOp->Common.AmlOpcode == AML_WHILE_OP) ||
687                      (ParentOp->Common.AmlOpcode == AML_RETURN_OP)) &&
688 
689                      /* And namepath is the first argument */
690                      (ParentOp->Common.Value.Arg == Op))
691                 {
692                     AcpiDmAddOpToExternalList (Op,
693                         Op->Common.Value.String, ACPI_TYPE_INTEGER, 0, 0);
694                     break;
695                 }
696             }
697 
698             /*
699              * This is a standalone namestring (not a parameter to another
700              * operator) - it *must* be a method invocation, nothing else is
701              * grammatically possible.
702              */
703             AcpiDmAddOpToExternalList (Op,
704                 Op->Common.Value.String, ACPI_TYPE_METHOD, ArgCount, 0);
705         }
706         break;
707 
708     default:
709 
710         break;
711     }
712 
713     return (AE_OK);
714 }
715 
716 
717 /*******************************************************************************
718  *
719  * FUNCTION:    AcpiDmLoadDescendingOp
720  *
721  * PARAMETERS:  ASL_WALK_CALLBACK
722  *
723  * RETURN:      Status
724  *
725  * DESCRIPTION: Descending handler for namespace control method object load
726  *
727  ******************************************************************************/
728 
729 static ACPI_STATUS
730 AcpiDmLoadDescendingOp (
731     ACPI_PARSE_OBJECT       *Op,
732     UINT32                  Level,
733     void                    *Context)
734 {
735     ACPI_OP_WALK_INFO       *Info = Context;
736     const ACPI_OPCODE_INFO  *OpInfo;
737     ACPI_WALK_STATE         *WalkState;
738     ACPI_OBJECT_TYPE        ObjectType;
739     ACPI_STATUS             Status;
740     char                    *Path = NULL;
741     ACPI_PARSE_OBJECT       *NextOp;
742     ACPI_NAMESPACE_NODE     *Node;
743     char                    FieldPath[5];
744     BOOLEAN                 PreDefined = FALSE;
745     UINT8                   PreDefineIndex = 0;
746 
747 
748     WalkState = Info->WalkState;
749     OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
750     ObjectType = AslMapNamedOpcodeToDataType (Op->Asl.AmlOpcode);
751 
752     /* Only interested in operators that create new names */
753 
754     if (!(OpInfo->Flags & AML_NAMED) &&
755         !(OpInfo->Flags & AML_CREATE))
756     {
757         goto Exit;
758     }
759 
760     /* Get the NamePath from the appropriate place */
761 
762     if (OpInfo->Flags & AML_NAMED)
763     {
764         /* For all named operators, get the new name */
765 
766         Path = Op->Named.Path;
767 
768         if (!Path && Op->Common.AmlOpcode == AML_INT_NAMEDFIELD_OP)
769         {
770             *ACPI_CAST_PTR (UINT32, &FieldPath[0]) = Op->Named.Name;
771             FieldPath[4] = 0;
772             Path = FieldPath;
773         }
774     }
775     else if (OpInfo->Flags & AML_CREATE)
776     {
777         /* New name is the last child */
778 
779         NextOp = Op->Common.Value.Arg;
780 
781         while (NextOp->Common.Next)
782         {
783             NextOp = NextOp->Common.Next;
784         }
785 
786         Path = NextOp->Common.Value.String;
787     }
788 
789     if (!Path)
790     {
791         goto Exit;
792     }
793 
794     /* Insert the name into the namespace */
795 
796     Status = AcpiNsLookup (WalkState->ScopeInfo, Path, ObjectType,
797         ACPI_IMODE_LOAD_PASS2, ACPI_NS_DONT_OPEN_SCOPE,
798         WalkState, &Node);
799 
800     Op->Common.Node = Node;
801 
802     if (ACPI_SUCCESS (Status))
803     {
804         /* Check if it's a predefined node */
805 
806         while (AcpiGbl_PreDefinedNames[PreDefineIndex].Name)
807         {
808             if (ACPI_COMPARE_NAME (Node->Name.Ascii,
809                 AcpiGbl_PreDefinedNames[PreDefineIndex].Name))
810             {
811                 PreDefined = TRUE;
812                 break;
813             }
814 
815             PreDefineIndex++;
816         }
817 
818         /*
819          * Set node owner id if it satisfies all the following conditions:
820          * 1) Not a predefined node, _SB_ etc
821          * 2) Not the root node
822          * 3) Not a node created by Scope
823          */
824 
825         if (!PreDefined && Node != AcpiGbl_RootNode &&
826             Op->Common.AmlOpcode != AML_SCOPE_OP)
827         {
828             Node->OwnerId = WalkState->OwnerId;
829         }
830     }
831 
832 
833 Exit:
834 
835     if (AcpiNsOpensScope (ObjectType))
836     {
837         if (Op->Common.Node)
838         {
839             Status = AcpiDsScopeStackPush (Op->Common.Node, ObjectType,
840                 WalkState);
841             if (ACPI_FAILURE (Status))
842             {
843                 return (Status);
844             }
845         }
846     }
847 
848     return (AE_OK);
849 }
850 
851 
852 /*******************************************************************************
853  *
854  * FUNCTION:    AcpiDmXrefDescendingOp
855  *
856  * PARAMETERS:  ASL_WALK_CALLBACK
857  *
858  * RETURN:      Status
859  *
860  * DESCRIPTION: Descending handler for namespace cross reference
861  *
862  ******************************************************************************/
863 
864 static ACPI_STATUS
865 AcpiDmXrefDescendingOp (
866     ACPI_PARSE_OBJECT       *Op,
867     UINT32                  Level,
868     void                    *Context)
869 {
870     ACPI_OP_WALK_INFO       *Info = Context;
871     const ACPI_OPCODE_INFO  *OpInfo;
872     ACPI_WALK_STATE         *WalkState;
873     ACPI_OBJECT_TYPE        ObjectType;
874     ACPI_OBJECT_TYPE        ObjectType2;
875     ACPI_STATUS             Status;
876     char                    *Path = NULL;
877     ACPI_PARSE_OBJECT       *NextOp;
878     ACPI_NAMESPACE_NODE     *Node;
879     ACPI_OPERAND_OBJECT     *Object;
880     UINT32                  ParamCount = 0;
881     char                    *Pathname;
882     UINT16                  Flags = 0;
883 
884 
885     WalkState = Info->WalkState;
886     OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
887     ObjectType = AslMapNamedOpcodeToDataType (Op->Asl.AmlOpcode);
888 
889     if ((!(OpInfo->Flags & AML_NAMED)) &&
890         (!(OpInfo->Flags & AML_CREATE)) &&
891         (Op->Common.AmlOpcode != AML_INT_NAMEPATH_OP) &&
892         (Op->Common.AmlOpcode != AML_NOTIFY_OP))
893     {
894         goto Exit;
895     }
896 
897     /* Get the NamePath from the appropriate place */
898 
899     if (OpInfo->Flags & AML_NAMED)
900     {
901         /*
902          * Only these two operators (Alias, Scope) refer to an existing
903          * name, it is the first argument
904          */
905         if (Op->Common.AmlOpcode == AML_ALIAS_OP)
906         {
907             ObjectType = ACPI_TYPE_ANY;
908 
909             NextOp = Op->Common.Value.Arg;
910             NextOp = NextOp->Common.Value.Arg;
911             if (NextOp->Common.AmlOpcode == AML_INT_NAMEPATH_OP)
912             {
913                 Path = NextOp->Common.Value.String;
914             }
915         }
916         else if (Op->Common.AmlOpcode == AML_SCOPE_OP ||
917                  Op->Common.AmlOpcode == AML_EXTERNAL_OP)
918         {
919             Path = Op->Named.Path;
920         }
921     }
922     else if (OpInfo->Flags & AML_CREATE)
923     {
924         /* Referenced Buffer Name is the first child */
925 
926         ObjectType = ACPI_TYPE_BUFFER; /* Change from TYPE_BUFFER_FIELD */
927 
928         NextOp = Op->Common.Value.Arg;
929         if (NextOp->Common.AmlOpcode == AML_INT_NAMEPATH_OP)
930         {
931             Path = NextOp->Common.Value.String;
932         }
933     }
934     else if (Op->Common.AmlOpcode == AML_NOTIFY_OP)
935     {
936         Path = Op->Common.Value.Arg->Asl.Value.String;
937     }
938     else
939     {
940         Path = Op->Common.Value.String;
941     }
942 
943     if (!Path)
944     {
945         goto Exit;
946     }
947 
948     /*
949      * Lookup the name in the namespace. Name must exist at this point, or it
950      * is an invalid reference.
951      *
952      * The namespace is also used as a lookup table for references to resource
953      * descriptors and the fields within them.
954      */
955     Node = NULL;
956     Status = AcpiNsLookup (WalkState->ScopeInfo, Path, ACPI_TYPE_ANY,
957         ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
958         WalkState, &Node);
959 
960     if (ACPI_SUCCESS (Status) && (Node->Flags & ANOBJ_IS_EXTERNAL))
961     {
962         /* Node was created by an External() statement */
963 
964         Status = AE_NOT_FOUND;
965     }
966 
967     if (ACPI_FAILURE (Status))
968     {
969         if (Status == AE_NOT_FOUND)
970         {
971             /*
972              * Add this symbol as an external declaration, except if the
973              * parent is a CondRefOf operator. For this operator, we do not
974              * need an external, nor do we want one, since this can cause
975              * disassembly problems if the symbol is actually a control
976              * method.
977              */
978             if (!(Op->Asl.Parent &&
979                 (Op->Asl.Parent->Asl.AmlOpcode == AML_CONDITIONAL_REF_OF_OP)))
980             {
981                 if (Node)
982                 {
983                     AcpiDmAddNodeToExternalList (Node,
984                         (UINT8) ObjectType, 7, Flags);
985                 }
986                 else
987                 {
988                     AcpiDmAddOpToExternalList (Op, Path,
989                         (UINT8) ObjectType, 7, Flags);
990                 }
991             }
992         }
993     }
994 
995     /*
996      * Found the node, but check if it came from an external table.
997      * Add it to external list. Note: Node->OwnerId == 0 indicates
998      * one of the built-in ACPI Names (_OS_ etc.) which can safely
999      * be ignored.
1000      */
1001     else if (Node->OwnerId &&
1002             (WalkState->OwnerId != Node->OwnerId))
1003     {
1004         ObjectType2 = ObjectType;
1005 
1006         Object = AcpiNsGetAttachedObject (Node);
1007         if (Object)
1008         {
1009             ObjectType2 = Object->Common.Type;
1010             if (ObjectType2 == ACPI_TYPE_METHOD)
1011             {
1012                 ParamCount = Object->Method.ParamCount;
1013             }
1014         }
1015 
1016         Pathname = AcpiNsGetExternalPathname (Node);
1017         if (!Pathname)
1018         {
1019             return (AE_NO_MEMORY);
1020         }
1021 
1022         AcpiDmAddNodeToExternalList (Node, (UINT8) ObjectType2,
1023             ParamCount, ACPI_EXT_RESOLVED_REFERENCE);
1024 
1025         ACPI_FREE (Pathname);
1026         Op->Common.Node = Node;
1027     }
1028     else
1029     {
1030         Op->Common.Node = Node;
1031     }
1032 
1033 
1034 Exit:
1035     /* Open new scope if necessary */
1036 
1037     if (AcpiNsOpensScope (ObjectType))
1038     {
1039         if (Op->Common.Node)
1040         {
1041             Status = AcpiDsScopeStackPush (Op->Common.Node, ObjectType,
1042                 WalkState);
1043             if (ACPI_FAILURE (Status))
1044             {
1045                 return (Status);
1046             }
1047         }
1048     }
1049 
1050     return (AE_OK);
1051 }
1052 
1053 /*******************************************************************************
1054  *
1055  * FUNCTION:    AcpiDmCommonDescendingOp
1056  *
1057  * PARAMETERS:  ASL_WALK_CALLBACK
1058  *
1059  * RETURN:      ACPI_STATUS
1060  *
1061  * DESCRIPTION: Perform parse tree preprocessing before main disassembly walk.
1062  *
1063  ******************************************************************************/
1064 
1065 static ACPI_STATUS
1066 AcpiDmCommonDescendingOp (
1067     ACPI_PARSE_OBJECT       *Op,
1068     UINT32                  Level,
1069     void                    *Context)
1070 {
1071     ACPI_STATUS             Status;
1072 
1073     /* Resource descriptor conversion */
1074 
1075     Status = AcpiDmProcessResourceDescriptors (Op, Level, Context);
1076 
1077     if (ACPI_FAILURE (Status))
1078     {
1079         return (Status);
1080     }
1081 
1082     /* Switch/Case conversion */
1083 
1084     Status = AcpiDmProcessSwitch (Op);
1085 
1086     return (AE_OK);
1087 }
1088 
1089 
1090 /*******************************************************************************
1091  *
1092  * FUNCTION:    AcpiDmProcessResourceDescriptors
1093  *
1094  * PARAMETERS:  ASL_WALK_CALLBACK
1095  *
1096  * RETURN:      ACPI_STATUS
1097  *
1098  * DESCRIPTION: Convert fixed-offset references to resource descriptors to
1099  *              symbolic references. Should only be called after namespace has
1100  *              been cross referenced.
1101  *
1102  ******************************************************************************/
1103 
1104 static ACPI_STATUS
1105 AcpiDmProcessResourceDescriptors (
1106     ACPI_PARSE_OBJECT       *Op,
1107     UINT32                  Level,
1108     void                    *Context)
1109 {
1110     ACPI_OP_WALK_INFO       *Info = Context;
1111     const ACPI_OPCODE_INFO  *OpInfo;
1112     ACPI_WALK_STATE         *WalkState;
1113     ACPI_OBJECT_TYPE        ObjectType;
1114     ACPI_STATUS             Status;
1115 
1116     WalkState = Info->WalkState;
1117     OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
1118 
1119     /* Open new scope if necessary */
1120 
1121     ObjectType = OpInfo->ObjectType;
1122     if (AcpiNsOpensScope (ObjectType))
1123     {
1124         if (Op->Common.Node)
1125         {
1126 
1127             Status = AcpiDsScopeStackPush (Op->Common.Node, ObjectType,
1128                 WalkState);
1129             if (ACPI_FAILURE (Status))
1130             {
1131                 return (Status);
1132             }
1133         }
1134     }
1135 
1136     /*
1137      * Check if this operator contains a reference to a resource descriptor.
1138      * If so, convert the reference into a symbolic reference.
1139      */
1140     AcpiDmCheckResourceReference (Op, WalkState);
1141 
1142     return (AE_OK);
1143 }
1144 
1145 /*******************************************************************************
1146  *
1147  * FUNCTION:    AcpiDmCommonAscendingOp
1148  *
1149  * PARAMETERS:  ASL_WALK_CALLBACK
1150  *
1151  * RETURN:      None
1152  *
1153  * DESCRIPTION: Ascending handler for combined parse/namespace walks. Closes
1154  *              scope if necessary.
1155  *
1156  ******************************************************************************/
1157 
1158 static ACPI_STATUS
1159 AcpiDmCommonAscendingOp (
1160     ACPI_PARSE_OBJECT       *Op,
1161     UINT32                  Level,
1162     void                    *Context)
1163 {
1164     ACPI_OP_WALK_INFO       *Info = Context;
1165     ACPI_OBJECT_TYPE        ObjectType;
1166 
1167 
1168     /* Close scope if necessary */
1169 
1170     ObjectType = AslMapNamedOpcodeToDataType (Op->Asl.AmlOpcode);
1171 
1172     if (AcpiNsOpensScope (ObjectType))
1173     {
1174         (void) AcpiDsScopeStackPop (Info->WalkState);
1175     }
1176 
1177     return (AE_OK);
1178 }
1179 
1180 /*******************************************************************************
1181  *
1182  * FUNCTION:    AcpiDmInspectPossibleArgs
1183  *
1184  * PARAMETERS:  CurrentOpArgCount   - Which arg of the current op was the
1185  *                                    possible method invocation found
1186  *              TargetCount         - Number of targets (0,1,2) for this op
1187  *              Op                  - Parse op
1188  *
1189  * RETURN:      Status
1190  *
1191  * DESCRIPTION: Examine following args and next ops for possible arguments
1192  *              for an unrecognized method invocation.
1193  *
1194  ******************************************************************************/
1195 
1196 static UINT32
1197 AcpiDmInspectPossibleArgs (
1198     UINT32                  CurrentOpArgCount,
1199     UINT32                  TargetCount,
1200     ACPI_PARSE_OBJECT       *Op)
1201 {
1202     const ACPI_OPCODE_INFO  *OpInfo;
1203     UINT32                  i;
1204     UINT32                  ArgumentCount = 0;
1205     ACPI_PARSE_OBJECT       *NextOp;
1206     ACPI_PARSE_OBJECT       *ExecuteOp;
1207 
1208 
1209     if (!Op)
1210     {
1211         return (0);
1212     }
1213 
1214     /* Lookahead for the maximum number of possible arguments */
1215 
1216     NextOp = Op->Common.Next;
1217 
1218     for (i = 0; (i < ACPI_METHOD_NUM_ARGS) && NextOp; i++)
1219     {
1220         OpInfo = AcpiPsGetOpcodeInfo (NextOp->Common.AmlOpcode);
1221 
1222         /* Any one of these operators is "very probably" not a method arg */
1223 
1224         if ((NextOp->Common.AmlOpcode == AML_STORE_OP) ||
1225             (NextOp->Common.AmlOpcode == AML_NOTIFY_OP) ||
1226             (OpInfo->Class == AML_CLASS_CONTROL) ||
1227             (OpInfo->Class == AML_CLASS_CREATE) ||
1228             (OpInfo->Class == AML_CLASS_NAMED_OBJECT))
1229         {
1230             break;
1231         }
1232 
1233         if (OpInfo->Class == AML_CLASS_EXECUTE)
1234         {
1235             /* Probable that this is method arg if there is no target */
1236 
1237             ExecuteOp = NextOp->Common.Value.Arg;
1238             while (ExecuteOp)
1239             {
1240                 if ((ExecuteOp->Common.AmlOpcode == AML_INT_NAMEPATH_OP) &&
1241                     (ExecuteOp->Common.Value.Arg == NULL))
1242                 {
1243                     /* No target, could be a method arg */
1244 
1245                     break;
1246                 }
1247 
1248                 if (NextOp->Common.AmlOpcode == AML_REF_OF_OP)
1249                 {
1250                     break;
1251                 }
1252 
1253                 ExecuteOp = ExecuteOp->Common.Next;
1254             }
1255 
1256             if (!ExecuteOp)
1257             {
1258                 /* Has a target, not method arg */
1259 
1260                 return (ArgumentCount);
1261             }
1262         }
1263 
1264         ArgumentCount++;
1265         NextOp = NextOp->Common.Next;
1266     }
1267 
1268     return (ArgumentCount);
1269 }
1270