xref: /freebsd/sys/contrib/dev/acpica/compiler/aslload.c (revision 52c2bb75163559a6e2866ad374a7de67a4ea1273)
1 /******************************************************************************
2  *
3  * Module Name: dswload - Dispatcher namespace load callbacks
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2019, 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/compiler/aslcompiler.h>
153 #include <contrib/dev/acpica/include/amlcode.h>
154 #include <contrib/dev/acpica/include/acdispat.h>
155 #include <contrib/dev/acpica/include/acnamesp.h>
156 #include <contrib/dev/acpica/include/acparser.h>
157 #include "aslcompiler.y.h"
158 
159 
160 #define _COMPONENT          ACPI_COMPILER
161         ACPI_MODULE_NAME    ("aslload")
162 
163 /* Local prototypes */
164 
165 static ACPI_STATUS
166 LdLoadFieldElements (
167     ACPI_PARSE_OBJECT       *Op,
168     ACPI_WALK_STATE         *WalkState);
169 
170 static ACPI_STATUS
171 LdLoadResourceElements (
172     ACPI_PARSE_OBJECT       *Op,
173     ACPI_WALK_STATE         *WalkState);
174 
175 static ACPI_STATUS
176 LdNamespace1Begin (
177     ACPI_PARSE_OBJECT       *Op,
178     UINT32                  Level,
179     void                    *Context);
180 
181 static ACPI_STATUS
182 LdNamespace2Begin (
183     ACPI_PARSE_OBJECT       *Op,
184     UINT32                  Level,
185     void                    *Context);
186 
187 static ACPI_STATUS
188 LdCommonNamespaceEnd (
189     ACPI_PARSE_OBJECT       *Op,
190     UINT32                  Level,
191     void                    *Context);
192 
193 
194 /*******************************************************************************
195  *
196  * FUNCTION:    LdLoadNamespace
197  *
198  * PARAMETERS:  RootOp      - Root of the parse tree
199  *
200  * RETURN:      Status
201  *
202  * DESCRIPTION: Perform a walk of the parse tree that in turn loads all of the
203  *              named ASL/AML objects into the namespace. The namespace is
204  *              constructed in order to resolve named references and references
205  *              to named fields within resource templates/descriptors.
206  *
207  ******************************************************************************/
208 
209 ACPI_STATUS
210 LdLoadNamespace (
211     ACPI_PARSE_OBJECT       *RootOp)
212 {
213     ACPI_WALK_STATE         *WalkState;
214 
215 
216     /* Create a new walk state */
217 
218     WalkState = AcpiDsCreateWalkState (0, NULL, NULL, NULL);
219     if (!WalkState)
220     {
221         return (AE_NO_MEMORY);
222     }
223 
224     /* Walk the entire parse tree, first pass */
225 
226     TrWalkParseTree (RootOp, ASL_WALK_VISIT_TWICE, LdNamespace1Begin,
227         LdCommonNamespaceEnd, WalkState);
228 
229     /* Second pass to handle forward references */
230 
231     TrWalkParseTree (RootOp, ASL_WALK_VISIT_TWICE, LdNamespace2Begin,
232         LdCommonNamespaceEnd, WalkState);
233 
234     /* Dump the namespace if debug is enabled */
235 
236     if (AcpiDbgLevel & ACPI_LV_TABLES)
237     {
238         AcpiNsDumpTables (ACPI_NS_ALL, ACPI_UINT32_MAX);
239     }
240 
241     ACPI_FREE (WalkState);
242     return (AE_OK);
243 }
244 
245 
246 /*******************************************************************************
247  *
248  * FUNCTION:    LdLoadFieldElements
249  *
250  * PARAMETERS:  Op              - Parent node (Field)
251  *              WalkState       - Current walk state
252  *
253  * RETURN:      Status
254  *
255  * DESCRIPTION: Enter the named elements of the field (children of the parent)
256  *              into the namespace.
257  *
258  ******************************************************************************/
259 
260 static ACPI_STATUS
261 LdLoadFieldElements (
262     ACPI_PARSE_OBJECT       *Op,
263     ACPI_WALK_STATE         *WalkState)
264 {
265     ACPI_PARSE_OBJECT       *Child = NULL;
266     ACPI_PARSE_OBJECT       *SourceRegion;
267     ACPI_NAMESPACE_NODE     *Node;
268     ACPI_STATUS             Status;
269 
270 
271 
272     SourceRegion = UtGetArg (Op, 0);
273     if (SourceRegion)
274     {
275         Status = AcpiNsLookup (WalkState->ScopeInfo,
276             SourceRegion->Asl.Value.String,
277             ACPI_TYPE_REGION, ACPI_IMODE_EXECUTE,
278             ACPI_NS_DONT_OPEN_SCOPE, NULL, &Node);
279         if (Status == AE_NOT_FOUND)
280         {
281             /*
282              * If the named object is not found, it means that it is either a
283              * forward reference or the named object does not exist.
284              */
285             SourceRegion->Asl.CompileFlags |= OP_NOT_FOUND_DURING_LOAD;
286         }
287     }
288 
289     /* Get the first named field element */
290 
291     switch (Op->Asl.AmlOpcode)
292     {
293     case AML_BANK_FIELD_OP:
294 
295         Child = UtGetArg (Op, 6);
296         break;
297 
298     case AML_INDEX_FIELD_OP:
299 
300         Child = UtGetArg (Op, 5);
301         break;
302 
303     case AML_FIELD_OP:
304 
305         Child = UtGetArg (Op, 4);
306         break;
307 
308     default:
309 
310         /* No other opcodes should arrive here */
311 
312         return (AE_BAD_PARAMETER);
313     }
314 
315     /* Enter all elements into the namespace */
316 
317     while (Child)
318     {
319         switch (Child->Asl.AmlOpcode)
320         {
321         case AML_INT_RESERVEDFIELD_OP:
322         case AML_INT_ACCESSFIELD_OP:
323         case AML_INT_CONNECTION_OP:
324             break;
325 
326         default:
327 
328             Status = AcpiNsLookup (WalkState->ScopeInfo,
329                 Child->Asl.Value.String,
330                 ACPI_TYPE_LOCAL_REGION_FIELD,
331                 ACPI_IMODE_LOAD_PASS1,
332                 ACPI_NS_NO_UPSEARCH | ACPI_NS_DONT_OPEN_SCOPE |
333                     ACPI_NS_ERROR_IF_FOUND, NULL, &Node);
334             if (ACPI_FAILURE (Status))
335             {
336                 if (Status != AE_ALREADY_EXISTS)
337                 {
338                     AslError (ASL_ERROR, ASL_MSG_CORE_EXCEPTION, Child,
339                         Child->Asl.Value.String);
340                     return (Status);
341                 }
342                 else if (Status == AE_ALREADY_EXISTS &&
343                     (Node->Flags & ANOBJ_IS_EXTERNAL))
344                 {
345                     Node->Type = (UINT8) ACPI_TYPE_LOCAL_REGION_FIELD;
346                     Node->Flags &= ~ANOBJ_IS_EXTERNAL;
347                 }
348                 else
349                 {
350                     /*
351                      * The name already exists in this scope
352                      * But continue processing the elements
353                      */
354                     AslDualParseOpError (ASL_WARNING, ASL_MSG_NAME_EXISTS, Child,
355                         Child->Asl.Value.String, ASL_MSG_FOUND_HERE, Node->Op,
356                         Node->Op->Asl.ExternalName);
357                 }
358             }
359             else
360             {
361                 Child->Asl.Node = Node;
362                 Node->Op = Child;
363             }
364             break;
365         }
366 
367         Child = Child->Asl.Next;
368     }
369 
370     return (AE_OK);
371 }
372 
373 
374 /*******************************************************************************
375  *
376  * FUNCTION:    LdLoadResourceElements
377  *
378  * PARAMETERS:  Op              - Parent node (Resource Descriptor)
379  *              WalkState       - Current walk state
380  *
381  * RETURN:      Status
382  *
383  * DESCRIPTION: Enter the named elements of the resource descriptor (children
384  *              of the parent) into the namespace.
385  *
386  * NOTE: In the real AML namespace, these named elements never exist. But
387  *       we simply use the namespace here as a symbol table so we can look
388  *       them up as they are referenced.
389  *
390  ******************************************************************************/
391 
392 static ACPI_STATUS
393 LdLoadResourceElements (
394     ACPI_PARSE_OBJECT       *Op,
395     ACPI_WALK_STATE         *WalkState)
396 {
397     ACPI_PARSE_OBJECT       *InitializerOp = NULL;
398     ACPI_NAMESPACE_NODE     *Node;
399     ACPI_STATUS             Status;
400 
401 
402     /*
403      * Enter the resource name into the namespace. Name must not already exist.
404      * This opens a scope, so later field names are guaranteed to be new/unique.
405      */
406     Status = AcpiNsLookup (WalkState->ScopeInfo, Op->Asl.Namepath,
407         ACPI_TYPE_LOCAL_RESOURCE, ACPI_IMODE_LOAD_PASS1,
408         ACPI_NS_NO_UPSEARCH | ACPI_NS_ERROR_IF_FOUND,
409         WalkState, &Node);
410     if (ACPI_FAILURE (Status))
411     {
412         if (Status == AE_ALREADY_EXISTS)
413         {
414             /* Actual node causing the error was saved in ParentMethod */
415 
416             AslDualParseOpError (ASL_ERROR, ASL_MSG_NAME_EXISTS,
417                 (ACPI_PARSE_OBJECT *) Op->Asl.ParentMethod,
418                 Op->Asl.Namepath, ASL_MSG_FOUND_HERE, Node->Op,
419                 Node->Op->Asl.ExternalName);
420             return (AE_OK);
421         }
422         return (Status);
423     }
424 
425     Node->Value = (UINT32) Op->Asl.Value.Integer;
426     Node->Op = Op;
427     Op->Asl.Node = Node;
428 
429     /*
430      * Now enter the predefined fields, for easy lookup when referenced
431      * by the source ASL
432      */
433     InitializerOp = ASL_GET_CHILD_NODE (Op);
434     while (InitializerOp)
435     {
436         if (InitializerOp->Asl.ExternalName)
437         {
438             Status = AcpiNsLookup (WalkState->ScopeInfo,
439                 InitializerOp->Asl.ExternalName,
440                 ACPI_TYPE_LOCAL_RESOURCE_FIELD, ACPI_IMODE_LOAD_PASS1,
441                 ACPI_NS_NO_UPSEARCH | ACPI_NS_DONT_OPEN_SCOPE, NULL, &Node);
442             if (ACPI_FAILURE (Status))
443             {
444                 return (Status);
445             }
446 
447             /*
448              * Store the field offset and length in the namespace node
449              * so it can be used when the field is referenced
450              */
451             Node->Value = InitializerOp->Asl.Value.Tag.BitOffset;
452             Node->Length = InitializerOp->Asl.Value.Tag.BitLength;
453             InitializerOp->Asl.Node = Node;
454             Node->Op = InitializerOp;
455         }
456 
457         InitializerOp = ASL_GET_PEER_NODE (InitializerOp);
458     }
459 
460     return (AE_OK);
461 }
462 
463 
464 /*******************************************************************************
465  *
466  * FUNCTION:    LdNamespace1Begin
467  *
468  * PARAMETERS:  ASL_WALK_CALLBACK
469  *
470  * RETURN:      Status
471  *
472  * DESCRIPTION: Descending callback used during the parse tree walk. If this
473  *              is a named AML opcode, enter into the namespace
474  *
475  ******************************************************************************/
476 
477 static ACPI_STATUS
478 LdNamespace1Begin (
479     ACPI_PARSE_OBJECT       *Op,
480     UINT32                  Level,
481     void                    *Context)
482 {
483     ACPI_WALK_STATE         *WalkState = (ACPI_WALK_STATE *) Context;
484     ACPI_NAMESPACE_NODE     *Node;
485     ACPI_PARSE_OBJECT       *MethodOp;
486     ACPI_STATUS             Status;
487     ACPI_OBJECT_TYPE        ObjectType;
488     ACPI_OBJECT_TYPE        ActualObjectType = ACPI_TYPE_ANY;
489     char                    *Path;
490     UINT32                  Flags = ACPI_NS_NO_UPSEARCH;
491     ACPI_PARSE_OBJECT       *Arg;
492     UINT32                  i;
493     BOOLEAN                 ForceNewScope = FALSE;
494     const ACPI_OPCODE_INFO  *OpInfo;
495     ACPI_PARSE_OBJECT       *ParentOp;
496 
497 
498     ACPI_FUNCTION_NAME (LdNamespace1Begin);
499 
500 
501     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Op %p [%s]\n",
502         Op, Op->Asl.ParseOpName));
503 
504     /*
505      * We are only interested in opcodes that have an associated name
506      * (or multiple names)
507      */
508     switch (Op->Asl.AmlOpcode)
509     {
510     case AML_BANK_FIELD_OP:
511     case AML_INDEX_FIELD_OP:
512     case AML_FIELD_OP:
513 
514         Status = LdLoadFieldElements (Op, WalkState);
515         return (Status);
516 
517     case AML_INT_CONNECTION_OP:
518 
519 
520         if (Op->Asl.Child->Asl.AmlOpcode != AML_INT_NAMEPATH_OP)
521         {
522             break;
523         }
524         Arg = Op->Asl.Child;
525 
526         Status = AcpiNsLookup (WalkState->ScopeInfo, Arg->Asl.ExternalName,
527             ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT,
528             WalkState, &Node);
529         if (ACPI_FAILURE (Status))
530         {
531             break;
532         }
533 
534         if (Node->Type == ACPI_TYPE_BUFFER)
535         {
536             Arg->Asl.Node = Node;
537 
538             Arg = Node->Op->Asl.Child;  /* Get namepath */
539             Arg = Arg->Asl.Next;        /* Get actual buffer */
540             Arg = Arg->Asl.Child;       /* Buffer length */
541             Arg = Arg->Asl.Next;        /* RAW_DATA buffer */
542         }
543         break;
544 
545     default:
546 
547         /* All other opcodes go below */
548 
549         break;
550     }
551 
552     /* Check if this object has already been installed in the namespace */
553 
554     if (Op->Asl.Node)
555     {
556         return (AE_OK);
557     }
558 
559     /* Check for a possible illegal forward reference */
560 
561     if ((Op->Asl.ParseOpcode == PARSEOP_NAMESEG) ||
562         (Op->Asl.ParseOpcode == PARSEOP_NAMESTRING))
563     {
564         /*
565          * Op->Asl.Namepath will be NULL for these opcodes.
566          * These opcodes are guaranteed to have a parent.
567          * Examine the parent opcode.
568          */
569         Status = AE_OK;
570         ParentOp = Op->Asl.Parent;
571         OpInfo = AcpiPsGetOpcodeInfo (ParentOp->Asl.AmlOpcode);
572 
573         /*
574          * Exclude all operators that actually declare a new name:
575          *      Name (ABCD, 1) -> Ignore (AML_CLASS_NAMED_OBJECT)
576          * We only want references to named objects:
577          *      Store (2, WXYZ) -> Attempt to resolve the name
578          */
579         if (OpInfo->Class == AML_CLASS_NAMED_OBJECT)
580         {
581             return (AE_OK);
582         }
583 
584         /*
585          * Check if the referenced object exists at this point during
586          * the load:
587          * 1) If it exists, then this cannot be a forward reference.
588          * 2) If it does not exist, it could be a forward reference or
589          * it truly does not exist (and no external declaration).
590          */
591         Status = AcpiNsLookup (WalkState->ScopeInfo,
592             Op->Asl.Value.Name, ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
593             ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
594             WalkState, &Node);
595         if (Status == AE_NOT_FOUND)
596         {
597             /*
598              * This is either a forward reference or the object truly
599              * does not exist. The two cases can only be differentiated
600              * during the cross-reference stage later. Mark the Op/Name
601              * as not-found for now to indicate the need for further
602              * processing.
603              *
604              * Special case: Allow forward references from elements of
605              * Package objects. This provides compatibility with other
606              * ACPI implementations. To correctly implement this, the
607              * ACPICA table load defers package resolution until the entire
608              * namespace has been loaded.
609              */
610             if ((ParentOp->Asl.ParseOpcode != PARSEOP_PACKAGE) &&
611                 (ParentOp->Asl.ParseOpcode != PARSEOP_VAR_PACKAGE))
612             {
613                 Op->Asl.CompileFlags |= OP_NOT_FOUND_DURING_LOAD;
614             }
615 
616             return (AE_OK);
617         }
618 
619         return (Status);
620     }
621 
622     Path = Op->Asl.Namepath;
623     if (!Path)
624     {
625         return (AE_OK);
626     }
627 
628     /* Map the raw opcode into an internal object type */
629 
630     switch (Op->Asl.ParseOpcode)
631     {
632     case PARSEOP_NAME:
633 
634         Arg = Op->Asl.Child;  /* Get the NameSeg/NameString node */
635         Arg = Arg->Asl.Next;  /* First peer is the object to be associated with the name */
636 
637         /*
638          * If this name refers to a ResourceTemplate, we will need to open
639          * a new scope so that the resource subfield names can be entered into
640          * the namespace underneath this name
641          */
642         if (Op->Asl.CompileFlags & OP_IS_RESOURCE_DESC)
643         {
644             ForceNewScope = TRUE;
645         }
646 
647         /* Get the data type associated with the named object, not the name itself */
648 
649         /* Log2 loop to convert from Btype (binary) to Etype (encoded) */
650 
651         ObjectType = 1;
652         for (i = 1; i < Arg->Asl.AcpiBtype; i *= 2)
653         {
654             ObjectType++;
655         }
656         break;
657 
658     case PARSEOP_EXTERNAL:
659         /*
660          * "External" simply enters a name and type into the namespace.
661          * We must be careful to not open a new scope, however, no matter
662          * what type the external name refers to (e.g., a method)
663          *
664          * first child is name, next child is ObjectType
665          */
666         ActualObjectType = (UINT8) Op->Asl.Child->Asl.Next->Asl.Value.Integer;
667         ObjectType = ACPI_TYPE_ANY;
668 
669         /*
670          * We will mark every new node along the path as "External". This
671          * allows some or all of the nodes to be created later in the ASL
672          * code. Handles cases like this:
673          *
674          *   External (\_SB_.PCI0.ABCD, IntObj)
675          *   Scope (_SB_)
676          *   {
677          *       Device (PCI0)
678          *       {
679          *       }
680          *   }
681          *   Method (X)
682          *   {
683          *       Store (\_SB_.PCI0.ABCD, Local0)
684          *   }
685          */
686         Flags |= ACPI_NS_EXTERNAL;
687         break;
688 
689     case PARSEOP_DEFAULT_ARG:
690 
691         if (Op->Asl.CompileFlags == OP_IS_RESOURCE_DESC)
692         {
693             Status = LdLoadResourceElements (Op, WalkState);
694             return_ACPI_STATUS (Status);
695         }
696 
697         ObjectType = AslMapNamedOpcodeToDataType (Op->Asl.AmlOpcode);
698         break;
699 
700     case PARSEOP_SCOPE:
701         /*
702          * The name referenced by Scope(Name) must already exist at this point.
703          * In other words, forward references for Scope() are not supported.
704          * The only real reason for this is that the MS interpreter cannot
705          * handle this case. Perhaps someday this case can go away.
706          */
707         Status = AcpiNsLookup (WalkState->ScopeInfo, Path, ACPI_TYPE_ANY,
708             ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT, WalkState, &Node);
709         if (ACPI_FAILURE (Status))
710         {
711             if (Status == AE_NOT_FOUND)
712             {
713                 /* The name was not found, go ahead and create it */
714 
715                 Status = AcpiNsLookup (WalkState->ScopeInfo, Path,
716                     ACPI_TYPE_LOCAL_SCOPE, ACPI_IMODE_LOAD_PASS1,
717                     Flags, WalkState, &Node);
718                 if (ACPI_FAILURE (Status))
719                 {
720                     return_ACPI_STATUS (Status);
721                 }
722 
723                 /* However, this is an error -- operand to Scope must exist */
724 
725                 if (strlen (Op->Asl.ExternalName) == ACPI_NAMESEG_SIZE)
726                 {
727                     AslError (ASL_ERROR, ASL_MSG_NOT_FOUND, Op,
728                         Op->Asl.ExternalName);
729                 }
730                 else
731                 {
732                     AslError (ASL_ERROR, ASL_MSG_NAMEPATH_NOT_EXIST, Op,
733                         Op->Asl.ExternalName);
734                 }
735 
736                 goto FinishNode;
737             }
738 
739             AslCoreSubsystemError (Op, Status,
740                 "Failure from namespace lookup", FALSE);
741 
742             return_ACPI_STATUS (Status);
743         }
744         else /* Status AE_OK */
745         {
746             /*
747              * Do not allow references to external scopes from the DSDT.
748              * This is because the DSDT is always loaded first, and the
749              * external reference cannot be resolved -- causing a runtime
750              * error because Scope() must be resolved immediately.
751              * 10/2015.
752              */
753             if ((Node->Flags & ANOBJ_IS_EXTERNAL) &&
754                 (ACPI_COMPARE_NAMESEG (AslGbl_TableSignature, "DSDT")))
755             {
756                 /* However, allowed if the reference is within a method */
757 
758                 MethodOp = Op->Asl.Parent;
759                 while (MethodOp &&
760                       (MethodOp->Asl.ParseOpcode != PARSEOP_METHOD))
761                 {
762                     MethodOp = MethodOp->Asl.Parent;
763                 }
764 
765                 if (!MethodOp)
766                 {
767                     /* Not in a control method, error */
768 
769                     AslError (ASL_ERROR, ASL_MSG_CROSS_TABLE_SCOPE, Op, NULL);
770                 }
771             }
772         }
773 
774         /* We found a node with this name, now check the type */
775 
776         switch (Node->Type)
777         {
778         case ACPI_TYPE_LOCAL_SCOPE:
779         case ACPI_TYPE_DEVICE:
780         case ACPI_TYPE_POWER:
781         case ACPI_TYPE_PROCESSOR:
782         case ACPI_TYPE_THERMAL:
783 
784             /* These are acceptable types - they all open a new scope */
785             break;
786 
787         case ACPI_TYPE_INTEGER:
788         case ACPI_TYPE_STRING:
789         case ACPI_TYPE_BUFFER:
790             /*
791              * These types we will allow, but we will change the type.
792              * This enables some existing code of the form:
793              *
794              *  Name (DEB, 0)
795              *  Scope (DEB) { ... }
796              *
797              * Which is used to workaround the fact that the MS interpreter
798              * does not allow Scope() forward references.
799              */
800             sprintf (AslGbl_MsgBuffer, "%s [%s], changing type to [Scope]",
801                 Op->Asl.ExternalName, AcpiUtGetTypeName (Node->Type));
802             AslError (ASL_REMARK, ASL_MSG_SCOPE_TYPE, Op, AslGbl_MsgBuffer);
803 
804             /* Switch the type to scope, open the new scope */
805 
806             Node->Type = ACPI_TYPE_LOCAL_SCOPE;
807             Status = AcpiDsScopeStackPush (Node, ACPI_TYPE_LOCAL_SCOPE,
808                 WalkState);
809             if (ACPI_FAILURE (Status))
810             {
811                 return_ACPI_STATUS (Status);
812             }
813             break;
814 
815         default:
816 
817             /* All other types are an error */
818 
819             sprintf (AslGbl_MsgBuffer, "%s [%s]", Op->Asl.ExternalName,
820                 AcpiUtGetTypeName (Node->Type));
821             AslError (ASL_ERROR, ASL_MSG_SCOPE_TYPE, Op, AslGbl_MsgBuffer);
822 
823             /*
824              * However, switch the type to be an actual scope so
825              * that compilation can continue without generating a whole
826              * cascade of additional errors. Open the new scope.
827              */
828             Node->Type = ACPI_TYPE_LOCAL_SCOPE;
829             Status = AcpiDsScopeStackPush (Node, ACPI_TYPE_LOCAL_SCOPE,
830                 WalkState);
831             if (ACPI_FAILURE (Status))
832             {
833                 return_ACPI_STATUS (Status);
834             }
835             break;
836         }
837 
838         Status = AE_OK;
839         goto FinishNode;
840 
841     default:
842 
843         ObjectType = AslMapNamedOpcodeToDataType (Op->Asl.AmlOpcode);
844         break;
845     }
846 
847     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Loading name: %s, (%s)\n",
848         Op->Asl.ExternalName, AcpiUtGetTypeName (ObjectType)));
849 
850     /* The name must not already exist */
851 
852     Flags |= ACPI_NS_ERROR_IF_FOUND;
853 
854     /*
855      * For opcodes that enter new names into the namespace,
856      * all prefix NameSegs must exist.
857      */
858     WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Asl.AmlOpcode);
859     if (((WalkState->OpInfo->Flags & AML_NAMED) ||
860         (WalkState->OpInfo->Flags & AML_CREATE)) &&
861         (Op->Asl.AmlOpcode != AML_EXTERNAL_OP))
862     {
863         Flags |= ACPI_NS_PREFIX_MUST_EXIST;
864     }
865 
866     /*
867      * Enter the named type into the internal namespace. We enter the name
868      * as we go downward in the parse tree. Any necessary subobjects that
869      * involve arguments to the opcode must be created as we go back up the
870      * parse tree later.
871      */
872     Status = AcpiNsLookup (WalkState->ScopeInfo, Path, ObjectType,
873         ACPI_IMODE_LOAD_PASS1, Flags, WalkState, &Node);
874     if (ACPI_FAILURE (Status))
875     {
876         if (Status == AE_ALREADY_EXISTS)
877         {
878             /* The name already exists in this scope */
879 
880             if (Node->Type == ACPI_TYPE_LOCAL_SCOPE)
881             {
882                 /* Allow multiple references to the same scope */
883 
884                 Node->Type = (UINT8) ObjectType;
885                 Status = AE_OK;
886             }
887             else if ((Node->Flags & ANOBJ_IS_EXTERNAL) &&
888                      (Op->Asl.ParseOpcode != PARSEOP_EXTERNAL))
889             {
890                 /*
891                  * Allow one create on an object or segment that was
892                  * previously declared External
893                  */
894                 Node->Flags &= ~ANOBJ_IS_EXTERNAL;
895                 Node->Type = (UINT8) ObjectType;
896 
897                 /* Just retyped a node, probably will need to open a scope */
898 
899                 if (AcpiNsOpensScope (ObjectType))
900                 {
901                     Status = AcpiDsScopeStackPush (Node, ObjectType, WalkState);
902                     if (ACPI_FAILURE (Status))
903                     {
904                         return_ACPI_STATUS (Status);
905                     }
906                 }
907 
908                 Status = AE_OK;
909             }
910             else if (!(Node->Flags & ANOBJ_IS_EXTERNAL) &&
911                      (Op->Asl.ParseOpcode == PARSEOP_EXTERNAL))
912             {
913                 /*
914                  * Allow externals in same scope as the definition of the
915                  * actual object. Similar to C. Allows multiple definition
916                  * blocks that refer to each other in the same file.
917                  */
918                 Status = AE_OK;
919             }
920             else if ((Node->Flags & ANOBJ_IS_EXTERNAL) &&
921                      (Op->Asl.ParseOpcode == PARSEOP_EXTERNAL) &&
922                      (ObjectType == ACPI_TYPE_ANY))
923             {
924                 /* Allow update of externals of unknown type. */
925 
926                 if (AcpiNsOpensScope (ActualObjectType))
927                 {
928                     Node->Type = (UINT8) ActualObjectType;
929                     Status = AE_OK;
930                 }
931                 else
932                 {
933                     sprintf (AslGbl_MsgBuffer, "%s [%s]", Op->Asl.ExternalName,
934                         AcpiUtGetTypeName (Node->Type));
935                     AslError (ASL_ERROR, ASL_MSG_SCOPE_TYPE, Op, AslGbl_MsgBuffer);
936                     return_ACPI_STATUS (AE_OK);
937                 }
938             }
939             else
940             {
941                 /* Valid error, object already exists */
942 
943                 AslDualParseOpError (ASL_ERROR, ASL_MSG_NAME_EXISTS, Op,
944                     Op->Asl.ExternalName, ASL_MSG_FOUND_HERE, Node->Op,
945                     Node->Op->Asl.ExternalName);
946                 return_ACPI_STATUS (AE_OK);
947             }
948         }
949         else if (AE_NOT_FOUND)
950         {
951             /*
952              * One or more prefix NameSegs of the NamePath do not exist
953              * (all of them must exist). Attempt to continue compilation
954              * by setting the current scope to the root.
955              */
956             Node = AcpiGbl_RootNode;
957             Status = AE_OK;
958         }
959         else
960         {
961             /* Flag all other errors as coming from the ACPICA core */
962 
963             AslCoreSubsystemError (Op, Status,
964                 "Failure from namespace lookup", FALSE);
965             return_ACPI_STATUS (Status);
966         }
967     }
968 
969     if (ForceNewScope)
970     {
971         Status = AcpiDsScopeStackPush (Node, ObjectType, WalkState);
972         if (ACPI_FAILURE (Status))
973         {
974             return_ACPI_STATUS (Status);
975         }
976     }
977 
978 FinishNode:
979     /*
980      * Point the parse node to the new namespace node, and point
981      * the Node back to the original Parse node
982      */
983     Op->Asl.Node = Node;
984     Node->Op = Op;
985 
986     /* Set the actual data type if appropriate (EXTERNAL term only) */
987 
988     if (ActualObjectType != ACPI_TYPE_ANY)
989     {
990         Node->Type = (UINT8) ActualObjectType;
991         Node->Value = ASL_EXTERNAL_METHOD;
992     }
993 
994     if (Op->Asl.ParseOpcode == PARSEOP_METHOD)
995     {
996         /*
997          * Get the method argument count from "Extra" and save
998          * it in the namespace node
999          */
1000         Node->Value = (UINT32) Op->Asl.Extra;
1001     }
1002 
1003     return_ACPI_STATUS (Status);
1004 }
1005 
1006 
1007 /*******************************************************************************
1008  *
1009  * FUNCTION:    LdNamespace2Begin
1010  *
1011  * PARAMETERS:  ASL_WALK_CALLBACK
1012  *
1013  * RETURN:      Status
1014  *
1015  * DESCRIPTION: Descending callback used during the pass 2 parse tree walk.
1016  *              Second pass resolves some forward references.
1017  *
1018  * Notes:
1019  * Currently only needs to handle the Alias operator.
1020  * Could be used to allow forward references from the Scope() operator, but
1021  * the MS interpreter does not allow this, so this compiler does not either.
1022  *
1023  ******************************************************************************/
1024 
1025 static ACPI_STATUS
1026 LdNamespace2Begin (
1027     ACPI_PARSE_OBJECT       *Op,
1028     UINT32                  Level,
1029     void                    *Context)
1030 {
1031     ACPI_WALK_STATE         *WalkState = (ACPI_WALK_STATE *) Context;
1032     ACPI_STATUS             Status;
1033     ACPI_NAMESPACE_NODE     *Node;
1034     ACPI_OBJECT_TYPE        ObjectType;
1035     BOOLEAN                 ForceNewScope = FALSE;
1036     ACPI_PARSE_OBJECT       *Arg;
1037     char                    *Path;
1038     ACPI_NAMESPACE_NODE     *TargetNode;
1039 
1040 
1041     ACPI_FUNCTION_NAME (LdNamespace2Begin);
1042     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Op %p [%s]\n",
1043         Op, Op->Asl.ParseOpName));
1044 
1045 
1046     /* Ignore Ops with no namespace node */
1047 
1048     Node = Op->Asl.Node;
1049     if (!Node)
1050     {
1051         return (AE_OK);
1052     }
1053 
1054     /* Get the type to determine if we should push the scope */
1055 
1056     if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG) &&
1057         (Op->Asl.CompileFlags == OP_IS_RESOURCE_DESC))
1058     {
1059         ObjectType = ACPI_TYPE_LOCAL_RESOURCE;
1060     }
1061     else
1062     {
1063         ObjectType = AslMapNamedOpcodeToDataType (Op->Asl.AmlOpcode);
1064     }
1065 
1066     /* Push scope for Resource Templates */
1067 
1068     if (Op->Asl.ParseOpcode == PARSEOP_NAME)
1069     {
1070         if (Op->Asl.CompileFlags & OP_IS_RESOURCE_DESC)
1071         {
1072             ForceNewScope = TRUE;
1073         }
1074     }
1075 
1076     /* Push the scope stack */
1077 
1078     if (ForceNewScope || AcpiNsOpensScope (ObjectType))
1079     {
1080         Status = AcpiDsScopeStackPush (Node, ObjectType, WalkState);
1081         if (ACPI_FAILURE (Status))
1082         {
1083             return_ACPI_STATUS (Status);
1084         }
1085     }
1086 
1087     if (Op->Asl.ParseOpcode == PARSEOP_ALIAS)
1088     {
1089         /*
1090          * Complete the alias node by getting and saving the target node.
1091          * First child is the alias target
1092          */
1093         Arg = Op->Asl.Child;
1094 
1095         /* Get the target pathname */
1096 
1097         Path = Arg->Asl.Namepath;
1098         if (!Path)
1099         {
1100             Status = UtInternalizeName (Arg->Asl.ExternalName, &Path);
1101             if (ACPI_FAILURE (Status))
1102             {
1103                 return (Status);
1104             }
1105         }
1106 
1107         /* Get the NS node associated with the target. It must exist. */
1108 
1109         Status = AcpiNsLookup (WalkState->ScopeInfo, Path, ACPI_TYPE_ANY,
1110             ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
1111             WalkState, &TargetNode);
1112         if (ACPI_FAILURE (Status))
1113         {
1114             if (Status == AE_NOT_FOUND)
1115             {
1116                 /* Standalone NameSeg vs. NamePath */
1117 
1118                 if (strlen (Arg->Asl.ExternalName) == ACPI_NAMESEG_SIZE)
1119                 {
1120                     AslError (ASL_ERROR, ASL_MSG_NOT_FOUND, Op,
1121                         Arg->Asl.ExternalName);
1122                 }
1123                 else
1124                 {
1125                     AslError (ASL_ERROR, ASL_MSG_NAMEPATH_NOT_EXIST, Op,
1126                         Arg->Asl.ExternalName);
1127                 }
1128 
1129 #if 0
1130 /*
1131  * NOTE: Removed 10/2018 to enhance compiler error reporting. No
1132  * regressions seen.
1133  */
1134                 /*
1135                  * The name was not found, go ahead and create it.
1136                  * This prevents more errors later.
1137                  */
1138                 Status = AcpiNsLookup (WalkState->ScopeInfo, Path,
1139                     ACPI_TYPE_ANY, ACPI_IMODE_LOAD_PASS1,
1140                     ACPI_NS_NO_UPSEARCH, WalkState, &Node);
1141 #endif
1142                 return (Status);
1143 /* Removed: return (AE_OK)*/
1144             }
1145 
1146             AslCoreSubsystemError (Op, Status,
1147                 "Failure from namespace lookup", FALSE);
1148             return (AE_OK);
1149         }
1150 
1151         /* Save the target node within the alias node */
1152 
1153         Node->Object = ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, TargetNode);
1154     }
1155 
1156     return (AE_OK);
1157 }
1158 
1159 
1160 /*******************************************************************************
1161  *
1162  * FUNCTION:    LdCommonNamespaceEnd
1163  *
1164  * PARAMETERS:  ASL_WALK_CALLBACK
1165  *
1166  * RETURN:      Status
1167  *
1168  * DESCRIPTION: Ascending callback used during the loading of the namespace,
1169  *              We only need to worry about managing the scope stack here.
1170  *
1171  ******************************************************************************/
1172 
1173 static ACPI_STATUS
1174 LdCommonNamespaceEnd (
1175     ACPI_PARSE_OBJECT       *Op,
1176     UINT32                  Level,
1177     void                    *Context)
1178 {
1179     ACPI_WALK_STATE         *WalkState = (ACPI_WALK_STATE *) Context;
1180     ACPI_OBJECT_TYPE        ObjectType;
1181     BOOLEAN                 ForceNewScope = FALSE;
1182 
1183 
1184     ACPI_FUNCTION_NAME (LdCommonNamespaceEnd);
1185 
1186 
1187     /* We are only interested in opcodes that have an associated name */
1188 
1189     if (!Op->Asl.Namepath)
1190     {
1191         return (AE_OK);
1192     }
1193 
1194     /* Get the type to determine if we should pop the scope */
1195 
1196     if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG) &&
1197         (Op->Asl.CompileFlags == OP_IS_RESOURCE_DESC))
1198     {
1199         /* TBD: Merge into AcpiDsMapNamedOpcodeToDataType */
1200 
1201         ObjectType = ACPI_TYPE_LOCAL_RESOURCE;
1202     }
1203     else
1204     {
1205         ObjectType = AslMapNamedOpcodeToDataType (Op->Asl.AmlOpcode);
1206     }
1207 
1208     /* Pop scope that was pushed for Resource Templates */
1209 
1210     if (Op->Asl.ParseOpcode == PARSEOP_NAME)
1211     {
1212         if (Op->Asl.CompileFlags & OP_IS_RESOURCE_DESC)
1213         {
1214             ForceNewScope = TRUE;
1215         }
1216     }
1217 
1218     /* Pop the scope stack */
1219 
1220     if (ForceNewScope || AcpiNsOpensScope (ObjectType))
1221     {
1222         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
1223             "(%s): Popping scope for Op [%s] %p\n",
1224             AcpiUtGetTypeName (ObjectType), Op->Asl.ParseOpName, Op));
1225 
1226         (void) AcpiDsScopeStackPop (WalkState);
1227     }
1228 
1229     return (AE_OK);
1230 }
1231