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