1/******************************************************************************
2 *
3 * Module Name: dswload - Dispatcher first pass namespace load callbacks
4 *
5 *****************************************************************************/
6
7/******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2016, 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#include "acpi.h"
117#include "accommon.h"
118#include "acparser.h"
119#include "amlcode.h"
120#include "acdispat.h"
121#include "acinterp.h"
122#include "acnamesp.h"
123
124#ifdef ACPI_ASL_COMPILER
125#include "acdisasm.h"
126#endif
127
128#define _COMPONENT          ACPI_DISPATCHER
129        ACPI_MODULE_NAME    ("dswload")
130
131
132/*******************************************************************************
133 *
134 * FUNCTION:    AcpiDsInitCallbacks
135 *
136 * PARAMETERS:  WalkState       - Current state of the parse tree walk
137 *              PassNumber      - 1, 2, or 3
138 *
139 * RETURN:      Status
140 *
141 * DESCRIPTION: Init walk state callbacks
142 *
143 ******************************************************************************/
144
145ACPI_STATUS
146AcpiDsInitCallbacks (
147    ACPI_WALK_STATE         *WalkState,
148    UINT32                  PassNumber)
149{
150
151    switch (PassNumber)
152    {
153    case 0:
154
155        /* Parse only - caller will setup callbacks */
156
157        WalkState->ParseFlags         = ACPI_PARSE_LOAD_PASS1 |
158                                        ACPI_PARSE_DELETE_TREE |
159                                        ACPI_PARSE_DISASSEMBLE;
160        WalkState->DescendingCallback = NULL;
161        WalkState->AscendingCallback  = NULL;
162        break;
163
164    case 1:
165
166        /* Load pass 1 */
167
168        WalkState->ParseFlags         = ACPI_PARSE_LOAD_PASS1 |
169                                        ACPI_PARSE_DELETE_TREE;
170        WalkState->DescendingCallback = AcpiDsLoad1BeginOp;
171        WalkState->AscendingCallback  = AcpiDsLoad1EndOp;
172        break;
173
174    case 2:
175
176        /* Load pass 2 */
177
178        WalkState->ParseFlags         = ACPI_PARSE_LOAD_PASS1 |
179                                        ACPI_PARSE_DELETE_TREE;
180        WalkState->DescendingCallback = AcpiDsLoad2BeginOp;
181        WalkState->AscendingCallback  = AcpiDsLoad2EndOp;
182        break;
183
184    case 3:
185
186        /* Execution pass */
187
188#ifndef ACPI_NO_METHOD_EXECUTION
189        WalkState->ParseFlags        |= ACPI_PARSE_EXECUTE  |
190                                        ACPI_PARSE_DELETE_TREE;
191        WalkState->DescendingCallback = AcpiDsExecBeginOp;
192        WalkState->AscendingCallback  = AcpiDsExecEndOp;
193#endif
194        break;
195
196    default:
197
198        return (AE_BAD_PARAMETER);
199    }
200
201    return (AE_OK);
202}
203
204
205/*******************************************************************************
206 *
207 * FUNCTION:    AcpiDsLoad1BeginOp
208 *
209 * PARAMETERS:  WalkState       - Current state of the parse tree walk
210 *              OutOp           - Where to return op if a new one is created
211 *
212 * RETURN:      Status
213 *
214 * DESCRIPTION: Descending callback used during the loading of ACPI tables.
215 *
216 ******************************************************************************/
217
218ACPI_STATUS
219AcpiDsLoad1BeginOp (
220    ACPI_WALK_STATE         *WalkState,
221    ACPI_PARSE_OBJECT       **OutOp)
222{
223    ACPI_PARSE_OBJECT       *Op;
224    ACPI_NAMESPACE_NODE     *Node;
225    ACPI_STATUS             Status;
226    ACPI_OBJECT_TYPE        ObjectType;
227    char                    *Path;
228    UINT32                  Flags;
229
230
231    ACPI_FUNCTION_TRACE (DsLoad1BeginOp);
232
233
234    Op = WalkState->Op;
235    ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Op=%p State=%p\n", Op, WalkState));
236
237    /* We are only interested in opcodes that have an associated name */
238
239    if (Op)
240    {
241        if (!(WalkState->OpInfo->Flags & AML_NAMED))
242        {
243            *OutOp = Op;
244            return_ACPI_STATUS (AE_OK);
245        }
246
247        /* Check if this object has already been installed in the namespace */
248
249        if (Op->Common.Node)
250        {
251            *OutOp = Op;
252            return_ACPI_STATUS (AE_OK);
253        }
254    }
255
256    Path = AcpiPsGetNextNamestring (&WalkState->ParserState);
257
258    /* Map the raw opcode into an internal object type */
259
260    ObjectType = WalkState->OpInfo->ObjectType;
261
262    ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
263        "State=%p Op=%p [%s]\n", WalkState, Op,
264        AcpiUtGetTypeName (ObjectType)));
265
266    switch (WalkState->Opcode)
267    {
268    case AML_SCOPE_OP:
269        /*
270         * The target name of the Scope() operator must exist at this point so
271         * that we can actually open the scope to enter new names underneath it.
272         * Allow search-to-root for single namesegs.
273         */
274        Status = AcpiNsLookup (WalkState->ScopeInfo, Path, ObjectType,
275            ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT, WalkState, &(Node));
276#ifdef ACPI_ASL_COMPILER
277        if (Status == AE_NOT_FOUND)
278        {
279            /*
280             * Table disassembly:
281             * Target of Scope() not found. Generate an External for it, and
282             * insert the name into the namespace.
283             */
284            AcpiDmAddOpToExternalList (Op, Path, ACPI_TYPE_DEVICE, 0, 0);
285            Status = AcpiNsLookup (WalkState->ScopeInfo, Path, ObjectType,
286               ACPI_IMODE_LOAD_PASS1, ACPI_NS_SEARCH_PARENT,
287               WalkState, &Node);
288        }
289#endif
290        if (ACPI_FAILURE (Status))
291        {
292            ACPI_ERROR_NAMESPACE (Path, Status);
293            return_ACPI_STATUS (Status);
294        }
295
296        /*
297         * Check to make sure that the target is
298         * one of the opcodes that actually opens a scope
299         */
300        switch (Node->Type)
301        {
302        case ACPI_TYPE_ANY:
303        case ACPI_TYPE_LOCAL_SCOPE:         /* Scope  */
304        case ACPI_TYPE_DEVICE:
305        case ACPI_TYPE_POWER:
306        case ACPI_TYPE_PROCESSOR:
307        case ACPI_TYPE_THERMAL:
308
309            /* These are acceptable types */
310            break;
311
312        case ACPI_TYPE_INTEGER:
313        case ACPI_TYPE_STRING:
314        case ACPI_TYPE_BUFFER:
315            /*
316             * These types we will allow, but we will change the type.
317             * This enables some existing code of the form:
318             *
319             *  Name (DEB, 0)
320             *  Scope (DEB) { ... }
321             *
322             * Note: silently change the type here. On the second pass,
323             * we will report a warning
324             */
325            ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
326                "Type override - [%4.4s] had invalid type (%s) "
327                "for Scope operator, changed to type ANY\n",
328                AcpiUtGetNodeName (Node), AcpiUtGetTypeName (Node->Type)));
329
330            Node->Type = ACPI_TYPE_ANY;
331            WalkState->ScopeInfo->Common.Value = ACPI_TYPE_ANY;
332            break;
333
334        case ACPI_TYPE_METHOD:
335            /*
336             * Allow scope change to root during execution of module-level
337             * code. Root is typed METHOD during this time.
338             */
339            if ((Node == AcpiGbl_RootNode) &&
340                (WalkState->ParseFlags & ACPI_PARSE_MODULE_LEVEL))
341            {
342                break;
343            }
344
345            /*lint -fallthrough */
346
347        default:
348
349            /* All other types are an error */
350
351            ACPI_ERROR ((AE_INFO,
352                "Invalid type (%s) for target of "
353                "Scope operator [%4.4s] (Cannot override)",
354                AcpiUtGetTypeName (Node->Type), AcpiUtGetNodeName (Node)));
355
356            return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
357        }
358        break;
359
360    default:
361        /*
362         * For all other named opcodes, we will enter the name into
363         * the namespace.
364         *
365         * Setup the search flags.
366         * Since we are entering a name into the namespace, we do not want to
367         * enable the search-to-root upsearch.
368         *
369         * There are only two conditions where it is acceptable that the name
370         * already exists:
371         *    1) the Scope() operator can reopen a scoping object that was
372         *       previously defined (Scope, Method, Device, etc.)
373         *    2) Whenever we are parsing a deferred opcode (OpRegion, Buffer,
374         *       BufferField, or Package), the name of the object is already
375         *       in the namespace.
376         */
377        if (WalkState->DeferredNode)
378        {
379            /* This name is already in the namespace, get the node */
380
381            Node = WalkState->DeferredNode;
382            Status = AE_OK;
383            break;
384        }
385
386        /*
387         * If we are executing a method, do not create any namespace objects
388         * during the load phase, only during execution.
389         */
390        if (WalkState->MethodNode)
391        {
392            Node = NULL;
393            Status = AE_OK;
394            break;
395        }
396
397        Flags = ACPI_NS_NO_UPSEARCH;
398        if ((WalkState->Opcode != AML_SCOPE_OP) &&
399            (!(WalkState->ParseFlags & ACPI_PARSE_DEFERRED_OP)))
400        {
401            if (WalkState->NamespaceOverride)
402            {
403                Flags |= ACPI_NS_OVERRIDE_IF_FOUND;
404                ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "[%s] Override allowed\n",
405                    AcpiUtGetTypeName (ObjectType)));
406            }
407            else
408            {
409                Flags |= ACPI_NS_ERROR_IF_FOUND;
410                ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "[%s] Cannot already exist\n",
411                    AcpiUtGetTypeName (ObjectType)));
412            }
413        }
414        else
415        {
416            ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
417                "[%s] Both Find or Create allowed\n",
418                AcpiUtGetTypeName (ObjectType)));
419        }
420
421        /*
422         * Enter the named type into the internal namespace. We enter the name
423         * as we go downward in the parse tree. Any necessary subobjects that
424         * involve arguments to the opcode must be created as we go back up the
425         * parse tree later.
426         */
427        Status = AcpiNsLookup (WalkState->ScopeInfo, Path, ObjectType,
428            ACPI_IMODE_LOAD_PASS1, Flags, WalkState, &Node);
429        if (ACPI_FAILURE (Status))
430        {
431            if (Status == AE_ALREADY_EXISTS)
432            {
433                /* The name already exists in this scope */
434
435                if (Node->Flags & ANOBJ_IS_EXTERNAL)
436                {
437                    /*
438                     * Allow one create on an object or segment that was
439                     * previously declared External
440                     */
441                    Node->Flags &= ~ANOBJ_IS_EXTERNAL;
442                    Node->Type = (UINT8) ObjectType;
443
444                    /* Just retyped a node, probably will need to open a scope */
445
446                    if (AcpiNsOpensScope (ObjectType))
447                    {
448                        Status = AcpiDsScopeStackPush (
449                            Node, ObjectType, WalkState);
450                        if (ACPI_FAILURE (Status))
451                        {
452                            return_ACPI_STATUS (Status);
453                        }
454                    }
455
456                    Status = AE_OK;
457                }
458            }
459
460            if (ACPI_FAILURE (Status))
461            {
462                ACPI_ERROR_NAMESPACE (Path, Status);
463                return_ACPI_STATUS (Status);
464            }
465        }
466        break;
467    }
468
469    /* Common exit */
470
471    if (!Op)
472    {
473        /* Create a new op */
474
475        Op = AcpiPsAllocOp (WalkState->Opcode, WalkState->Aml);
476        if (!Op)
477        {
478            return_ACPI_STATUS (AE_NO_MEMORY);
479        }
480    }
481
482    /* Initialize the op */
483
484#if (defined (ACPI_NO_METHOD_EXECUTION) || defined (ACPI_CONSTANT_EVAL_ONLY))
485    Op->Named.Path = ACPI_CAST_PTR (UINT8, Path);
486#endif
487
488    if (Node)
489    {
490        /*
491         * Put the Node in the "op" object that the parser uses, so we
492         * can get it again quickly when this scope is closed
493         */
494        Op->Common.Node = Node;
495        Op->Named.Name = Node->Name.Integer;
496    }
497
498    AcpiPsAppendArg (AcpiPsGetParentScope (&WalkState->ParserState), Op);
499    *OutOp = Op;
500    return_ACPI_STATUS (Status);
501}
502
503
504/*******************************************************************************
505 *
506 * FUNCTION:    AcpiDsLoad1EndOp
507 *
508 * PARAMETERS:  WalkState       - Current state of the parse tree walk
509 *
510 * RETURN:      Status
511 *
512 * DESCRIPTION: Ascending callback used during the loading of the namespace,
513 *              both control methods and everything else.
514 *
515 ******************************************************************************/
516
517ACPI_STATUS
518AcpiDsLoad1EndOp (
519    ACPI_WALK_STATE         *WalkState)
520{
521    ACPI_PARSE_OBJECT       *Op;
522    ACPI_OBJECT_TYPE        ObjectType;
523    ACPI_STATUS             Status = AE_OK;
524
525
526    ACPI_FUNCTION_TRACE (DsLoad1EndOp);
527
528
529    Op = WalkState->Op;
530    ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Op=%p State=%p\n", Op, WalkState));
531
532    /* We are only interested in opcodes that have an associated name */
533
534    if (!(WalkState->OpInfo->Flags & (AML_NAMED | AML_FIELD)))
535    {
536        return_ACPI_STATUS (AE_OK);
537    }
538
539    /* Get the object type to determine if we should pop the scope */
540
541    ObjectType = WalkState->OpInfo->ObjectType;
542
543#ifndef ACPI_NO_METHOD_EXECUTION
544    if (WalkState->OpInfo->Flags & AML_FIELD)
545    {
546        /*
547         * If we are executing a method, do not create any namespace objects
548         * during the load phase, only during execution.
549         */
550        if (!WalkState->MethodNode)
551        {
552            if (WalkState->Opcode == AML_FIELD_OP          ||
553                WalkState->Opcode == AML_BANK_FIELD_OP     ||
554                WalkState->Opcode == AML_INDEX_FIELD_OP)
555            {
556                Status = AcpiDsInitFieldObjects (Op, WalkState);
557            }
558        }
559        return_ACPI_STATUS (Status);
560    }
561
562    /*
563     * If we are executing a method, do not create any namespace objects
564     * during the load phase, only during execution.
565     */
566    if (!WalkState->MethodNode)
567    {
568        if (Op->Common.AmlOpcode == AML_REGION_OP)
569        {
570            Status = AcpiExCreateRegion (Op->Named.Data, Op->Named.Length,
571                (ACPI_ADR_SPACE_TYPE)
572                    ((Op->Common.Value.Arg)->Common.Value.Integer),
573                WalkState);
574            if (ACPI_FAILURE (Status))
575            {
576                return_ACPI_STATUS (Status);
577            }
578        }
579        else if (Op->Common.AmlOpcode == AML_DATA_REGION_OP)
580        {
581            Status = AcpiExCreateRegion (Op->Named.Data, Op->Named.Length,
582                ACPI_ADR_SPACE_DATA_TABLE, WalkState);
583            if (ACPI_FAILURE (Status))
584            {
585                return_ACPI_STATUS (Status);
586            }
587        }
588    }
589#endif
590
591    if (Op->Common.AmlOpcode == AML_NAME_OP)
592    {
593        /* For Name opcode, get the object type from the argument */
594
595        if (Op->Common.Value.Arg)
596        {
597            ObjectType = (AcpiPsGetOpcodeInfo (
598                (Op->Common.Value.Arg)->Common.AmlOpcode))->ObjectType;
599
600            /* Set node type if we have a namespace node */
601
602            if (Op->Common.Node)
603            {
604                Op->Common.Node->Type = (UINT8) ObjectType;
605            }
606        }
607    }
608
609    /*
610     * If we are executing a method, do not create any namespace objects
611     * during the load phase, only during execution.
612     */
613    if (!WalkState->MethodNode)
614    {
615        if (Op->Common.AmlOpcode == AML_METHOD_OP)
616        {
617            /*
618             * MethodOp PkgLength NameString MethodFlags TermList
619             *
620             * Note: We must create the method node/object pair as soon as we
621             * see the method declaration. This allows later pass1 parsing
622             * of invocations of the method (need to know the number of
623             * arguments.)
624             */
625            ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
626                "LOADING-Method: State=%p Op=%p NamedObj=%p\n",
627                WalkState, Op, Op->Named.Node));
628
629            if (!AcpiNsGetAttachedObject (Op->Named.Node))
630            {
631                WalkState->Operands[0] = ACPI_CAST_PTR (void, Op->Named.Node);
632                WalkState->NumOperands = 1;
633
634                Status = AcpiDsCreateOperands (
635                    WalkState, Op->Common.Value.Arg);
636                if (ACPI_SUCCESS (Status))
637                {
638                    Status = AcpiExCreateMethod (Op->Named.Data,
639                        Op->Named.Length, WalkState);
640                }
641
642                WalkState->Operands[0] = NULL;
643                WalkState->NumOperands = 0;
644
645                if (ACPI_FAILURE (Status))
646                {
647                    return_ACPI_STATUS (Status);
648                }
649            }
650        }
651    }
652
653    /* Pop the scope stack (only if loading a table) */
654
655    if (!WalkState->MethodNode &&
656        AcpiNsOpensScope (ObjectType))
657    {
658        ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "(%s): Popping scope for Op %p\n",
659            AcpiUtGetTypeName (ObjectType), Op));
660
661        Status = AcpiDsScopeStackPop (WalkState);
662    }
663
664    return_ACPI_STATUS (Status);
665}
666