Home | History | Annotate | Line # | Download | only in dispatcher
      1 /******************************************************************************
      2  *
      3  * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
      4  *
      5  *****************************************************************************/
      6 
      7 /*
      8  * Copyright (C) 2000 - 2026, Intel Corp.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions, and the following disclaimer,
     16  *    without modification.
     17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18  *    substantially similar to the "NO WARRANTY" disclaimer below
     19  *    ("Disclaimer") and any redistribution must be conditioned upon
     20  *    including a substantially similar Disclaimer requirement for further
     21  *    binary redistribution.
     22  * 3. Neither the names of the above-listed copyright holders nor the names
     23  *    of any contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * Alternatively, this software may be distributed under the terms of the
     27  * GNU General Public License ("GPL") version 2 as published by the Free
     28  * Software Foundation.
     29  *
     30  * NO WARRANTY
     31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  * POSSIBILITY OF SUCH DAMAGES.
     42  */
     43 
     44 #include "acpi.h"
     45 #include "accommon.h"
     46 #include "acdispat.h"
     47 #include "acinterp.h"
     48 #include "acnamesp.h"
     49 #include "acparser.h"
     50 #include "amlcode.h"
     51 #include "acdebug.h"
     52 
     53 
     54 #define _COMPONENT          ACPI_DISPATCHER
     55         ACPI_MODULE_NAME    ("dsmethod")
     56 
     57 /* Local prototypes */
     58 
     59 static ACPI_STATUS
     60 AcpiDsDetectNamedOpcodes (
     61     ACPI_WALK_STATE         *WalkState,
     62     ACPI_PARSE_OBJECT       **OutOp);
     63 
     64 static ACPI_STATUS
     65 AcpiDsCreateMethodMutex (
     66     ACPI_OPERAND_OBJECT     *MethodDesc);
     67 
     68 
     69 /*******************************************************************************
     70  *
     71  * FUNCTION:    AcpiDsAutoSerializeMethod
     72  *
     73  * PARAMETERS:  Node                        - Namespace Node of the method
     74  *              ObjDesc                     - Method object attached to node
     75  *
     76  * RETURN:      Status
     77  *
     78  * DESCRIPTION: Parse a control method AML to scan for control methods that
     79  *              need serialization due to the creation of named objects.
     80  *
     81  * NOTE: It is a bit of overkill to mark all such methods serialized, since
     82  * there is only a problem if the method actually blocks during execution.
     83  * A blocking operation is, for example, a Sleep() operation, or any access
     84  * to an operation region. However, it is probably not possible to easily
     85  * detect whether a method will block or not, so we simply mark all suspicious
     86  * methods as serialized.
     87  *
     88  * NOTE2: This code is essentially a generic routine for parsing a single
     89  * control method.
     90  *
     91  ******************************************************************************/
     92 
     93 ACPI_STATUS
     94 AcpiDsAutoSerializeMethod (
     95     ACPI_NAMESPACE_NODE     *Node,
     96     ACPI_OPERAND_OBJECT     *ObjDesc)
     97 {
     98     ACPI_STATUS             Status;
     99     ACPI_PARSE_OBJECT       *Op = NULL;
    100     ACPI_WALK_STATE         *WalkState;
    101 
    102 
    103     ACPI_FUNCTION_TRACE_PTR (DsAutoSerializeMethod, Node);
    104 
    105 
    106     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
    107         "Method auto-serialization parse [%4.4s] %p\n",
    108         AcpiUtGetNodeName (Node), Node));
    109 
    110     /* Create/Init a root op for the method parse tree */
    111 
    112     Op = AcpiPsAllocOp (AML_METHOD_OP, ObjDesc->Method.AmlStart);
    113     if (!Op)
    114     {
    115         return_ACPI_STATUS (AE_NO_MEMORY);
    116     }
    117 
    118     AcpiPsSetName (Op, Node->Name.Integer);
    119     Op->Common.Node = Node;
    120 
    121     /* Create and initialize a new walk state */
    122 
    123     WalkState = AcpiDsCreateWalkState (Node->OwnerId, NULL, NULL, NULL);
    124     if (!WalkState)
    125     {
    126         AcpiPsFreeOp (Op);
    127         return_ACPI_STATUS (AE_NO_MEMORY);
    128     }
    129 
    130     Status = AcpiDsInitAmlWalk (WalkState, Op, Node,
    131         ObjDesc->Method.AmlStart, ObjDesc->Method.AmlLength, NULL, 0);
    132     if (ACPI_FAILURE (Status))
    133     {
    134         AcpiDsDeleteWalkState (WalkState);
    135         AcpiPsFreeOp (Op);
    136         return_ACPI_STATUS (Status);
    137     }
    138 
    139     WalkState->DescendingCallback = AcpiDsDetectNamedOpcodes;
    140 
    141     /* Parse the method, scan for creation of named objects */
    142 
    143     Status = AcpiPsParseAml (WalkState);
    144 
    145     AcpiPsDeleteParseTree (Op);
    146     return_ACPI_STATUS (Status);
    147 }
    148 
    149 
    150 /*******************************************************************************
    151  *
    152  * FUNCTION:    AcpiDsDetectNamedOpcodes
    153  *
    154  * PARAMETERS:  WalkState       - Current state of the parse tree walk
    155  *              OutOp           - Unused, required for parser interface
    156  *
    157  * RETURN:      Status
    158  *
    159  * DESCRIPTION: Descending callback used during the loading of ACPI tables.
    160  *              Currently used to detect methods that must be marked serialized
    161  *              in order to avoid problems with the creation of named objects.
    162  *
    163  ******************************************************************************/
    164 
    165 static ACPI_STATUS
    166 AcpiDsDetectNamedOpcodes (
    167     ACPI_WALK_STATE         *WalkState,
    168     ACPI_PARSE_OBJECT       **OutOp)
    169 {
    170 
    171     ACPI_FUNCTION_NAME (AcpiDsDetectNamedOpcodes);
    172 
    173 
    174     /* We are only interested in opcodes that create a new name */
    175 
    176     if (!(WalkState->OpInfo->Flags & (AML_NAMED | AML_CREATE | AML_FIELD)))
    177     {
    178         return (AE_OK);
    179     }
    180 
    181     /*
    182      * At this point, we know we have a Named object opcode.
    183      * Mark the method as serialized. Later code will create a mutex for
    184      * this method to enforce serialization.
    185      *
    186      * Note, ACPI_METHOD_IGNORE_SYNC_LEVEL flag means that we will ignore the
    187      * Sync Level mechanism for this method, even though it is now serialized.
    188      * Otherwise, there can be conflicts with existing ASL code that actually
    189      * uses sync levels.
    190      */
    191     WalkState->MethodDesc->Method.SyncLevel = 0;
    192     WalkState->MethodDesc->Method.InfoFlags |=
    193         (ACPI_METHOD_SERIALIZED | ACPI_METHOD_IGNORE_SYNC_LEVEL);
    194 
    195     ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
    196         "Method serialized [%4.4s] %p - [%s] (%4.4X)\n",
    197         WalkState->MethodNode->Name.Ascii, WalkState->MethodNode,
    198         WalkState->OpInfo->Name, WalkState->Opcode));
    199 
    200     /* Abort the parse, no need to examine this method any further */
    201 
    202     return (AE_CTRL_TERMINATE);
    203 }
    204 
    205 
    206 /*******************************************************************************
    207  *
    208  * FUNCTION:    AcpiDsMethodError
    209  *
    210  * PARAMETERS:  Status          - Execution status
    211  *              WalkState       - Current state
    212  *
    213  * RETURN:      Status
    214  *
    215  * DESCRIPTION: Called on method error. Invoke the global exception handler if
    216  *              present, dump the method data if the debugger is configured
    217  *
    218  *              Note: Allows the exception handler to change the status code
    219  *
    220  ******************************************************************************/
    221 
    222 ACPI_STATUS
    223 AcpiDsMethodError (
    224     ACPI_STATUS             Status,
    225     ACPI_WALK_STATE         *WalkState)
    226 {
    227     UINT32                  AmlOffset;
    228     ACPI_NAME               Name = 0;
    229 
    230 
    231     ACPI_FUNCTION_ENTRY ();
    232 
    233 
    234     /* Ignore AE_OK and control exception codes */
    235 
    236     if (ACPI_SUCCESS (Status) ||
    237         (Status & AE_CODE_CONTROL))
    238     {
    239         return (Status);
    240     }
    241 
    242     /* Invoke the global exception handler */
    243 
    244     if (AcpiGbl_ExceptionHandler)
    245     {
    246         /* Exit the interpreter, allow handler to execute methods */
    247 
    248         AcpiExExitInterpreter ();
    249 
    250         /*
    251          * Handler can map the exception code to anything it wants, including
    252          * AE_OK, in which case the executing method will not be aborted.
    253          */
    254         AmlOffset = (UINT32) ACPI_PTR_DIFF (WalkState->Aml,
    255             WalkState->ParserState.AmlStart);
    256 
    257         if (WalkState->MethodNode)
    258         {
    259             Name = WalkState->MethodNode->Name.Integer;
    260         }
    261         else if (WalkState->DeferredNode)
    262         {
    263             Name = WalkState->DeferredNode->Name.Integer;
    264         }
    265 
    266         Status = AcpiGbl_ExceptionHandler (Status, Name,
    267             WalkState->Opcode, AmlOffset, NULL);
    268         AcpiExEnterInterpreter ();
    269     }
    270 
    271     AcpiDsClearImplicitReturn (WalkState);
    272 
    273     if (ACPI_FAILURE (Status))
    274     {
    275         AcpiDsDumpMethodStack (Status, WalkState, WalkState->Op);
    276 
    277         /* Display method locals/args if debugger is present */
    278 
    279 #ifdef ACPI_DEBUGGER
    280         AcpiDbDumpMethodInfo (Status, WalkState);
    281 #endif
    282     }
    283 
    284     return (Status);
    285 }
    286 
    287 
    288 /*******************************************************************************
    289  *
    290  * FUNCTION:    AcpiDsCreateMethodMutex
    291  *
    292  * PARAMETERS:  ObjDesc             - The method object
    293  *
    294  * RETURN:      Status
    295  *
    296  * DESCRIPTION: Create a mutex object for a serialized control method
    297  *
    298  ******************************************************************************/
    299 
    300 static ACPI_STATUS
    301 AcpiDsCreateMethodMutex (
    302     ACPI_OPERAND_OBJECT     *MethodDesc)
    303 {
    304     ACPI_OPERAND_OBJECT     *MutexDesc;
    305     ACPI_STATUS             Status;
    306 
    307 
    308     ACPI_FUNCTION_TRACE (DsCreateMethodMutex);
    309 
    310 
    311     /* Create the new mutex object */
    312 
    313     MutexDesc = AcpiUtCreateInternalObject (ACPI_TYPE_MUTEX);
    314     if (!MutexDesc)
    315     {
    316         return_ACPI_STATUS (AE_NO_MEMORY);
    317     }
    318 
    319     /* Create the actual OS Mutex */
    320 
    321     Status = AcpiOsCreateMutex (&MutexDesc->Mutex.OsMutex);
    322     if (ACPI_FAILURE (Status))
    323     {
    324         AcpiUtDeleteObjectDesc (MutexDesc);
    325         return_ACPI_STATUS (Status);
    326     }
    327 
    328     MutexDesc->Mutex.SyncLevel = MethodDesc->Method.SyncLevel;
    329     MethodDesc->Method.Mutex = MutexDesc;
    330     return_ACPI_STATUS (AE_OK);
    331 }
    332 
    333 
    334 /*******************************************************************************
    335  *
    336  * FUNCTION:    AcpiDsBeginMethodExecution
    337  *
    338  * PARAMETERS:  MethodNode          - Node of the method
    339  *              ObjDesc             - The method object
    340  *              WalkState           - current state, NULL if not yet executing
    341  *                                    a method.
    342  *
    343  * RETURN:      Status
    344  *
    345  * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
    346  *              increments the thread count, and waits at the method semaphore
    347  *              for clearance to execute.
    348  *
    349  ******************************************************************************/
    350 
    351 ACPI_STATUS
    352 AcpiDsBeginMethodExecution (
    353     ACPI_NAMESPACE_NODE     *MethodNode,
    354     ACPI_OPERAND_OBJECT     *ObjDesc,
    355     ACPI_WALK_STATE         *WalkState)
    356 {
    357     ACPI_STATUS             Status = AE_OK;
    358 
    359 
    360     ACPI_FUNCTION_TRACE_PTR (DsBeginMethodExecution, MethodNode);
    361 
    362 
    363     if (!MethodNode)
    364     {
    365         return_ACPI_STATUS (AE_NULL_ENTRY);
    366     }
    367 
    368     AcpiExStartTraceMethod (MethodNode, ObjDesc, WalkState);
    369 
    370     /* Prevent wraparound of thread count */
    371 
    372     if (ObjDesc->Method.ThreadCount == ACPI_UINT8_MAX)
    373     {
    374         ACPI_ERROR ((AE_INFO,
    375             "Method reached maximum reentrancy limit (255)"));
    376         return_ACPI_STATUS (AE_AML_METHOD_LIMIT);
    377     }
    378 
    379     /*
    380      * If this method is serialized, we need to acquire the method mutex.
    381      */
    382     if (ObjDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED)
    383     {
    384         /*
    385          * Create a mutex for the method if it is defined to be Serialized
    386          * and a mutex has not already been created. We defer the mutex creation
    387          * until a method is actually executed, to minimize the object count
    388          */
    389         if (!ObjDesc->Method.Mutex)
    390         {
    391             Status = AcpiDsCreateMethodMutex (ObjDesc);
    392             if (ACPI_FAILURE (Status))
    393             {
    394                 return_ACPI_STATUS (Status);
    395             }
    396         }
    397 
    398         /*
    399          * The CurrentSyncLevel (per-thread) must be less than or equal to
    400          * the sync level of the method. This mechanism provides some
    401          * deadlock prevention.
    402          *
    403          * If the method was auto-serialized, we just ignore the sync level
    404          * mechanism, because auto-serialization of methods can interfere
    405          * with ASL code that actually uses sync levels.
    406          *
    407          * Top-level method invocation has no walk state at this point
    408          */
    409         if (WalkState &&
    410             (!(ObjDesc->Method.InfoFlags & ACPI_METHOD_IGNORE_SYNC_LEVEL)) &&
    411             (WalkState->Thread->CurrentSyncLevel >
    412                 ObjDesc->Method.Mutex->Mutex.SyncLevel))
    413         {
    414             ACPI_ERROR ((AE_INFO,
    415                 "Cannot acquire Mutex for method [%4.4s]"
    416                 ", current SyncLevel is too large (%u)",
    417                 AcpiUtGetNodeName (MethodNode),
    418                 WalkState->Thread->CurrentSyncLevel));
    419 
    420             return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
    421         }
    422 
    423         /*
    424          * Obtain the method mutex if necessary. Do not acquire mutex for a
    425          * recursive call.
    426          */
    427         if (!WalkState ||
    428             !ObjDesc->Method.Mutex->Mutex.ThreadId ||
    429             (WalkState->Thread->ThreadId !=
    430                 ObjDesc->Method.Mutex->Mutex.ThreadId))
    431         {
    432             /*
    433              * Acquire the method mutex. This releases the interpreter if we
    434              * block (and reacquires it before it returns)
    435              */
    436             Status = AcpiExSystemWaitMutex (
    437                 ObjDesc->Method.Mutex->Mutex.OsMutex, ACPI_WAIT_FOREVER);
    438             if (ACPI_FAILURE (Status))
    439             {
    440                 return_ACPI_STATUS (Status);
    441             }
    442 
    443             /* Update the mutex and walk info and save the original SyncLevel */
    444 
    445             if (WalkState)
    446             {
    447                 ObjDesc->Method.Mutex->Mutex.OriginalSyncLevel =
    448                     WalkState->Thread->CurrentSyncLevel;
    449 
    450                 ObjDesc->Method.Mutex->Mutex.ThreadId =
    451                     WalkState->Thread->ThreadId;
    452 
    453                 /*
    454                  * Update the current SyncLevel only if this is not an auto-
    455                  * serialized method. In the auto case, we have to ignore
    456                  * the sync level for the method mutex (created for the
    457                  * auto-serialization) because we have no idea of what the
    458                  * sync level should be. Therefore, just ignore it.
    459                  */
    460                 if (!(ObjDesc->Method.InfoFlags &
    461                     ACPI_METHOD_IGNORE_SYNC_LEVEL))
    462                 {
    463                     WalkState->Thread->CurrentSyncLevel =
    464                         ObjDesc->Method.SyncLevel;
    465                 }
    466             }
    467             else
    468             {
    469                 ObjDesc->Method.Mutex->Mutex.OriginalSyncLevel =
    470                     ObjDesc->Method.Mutex->Mutex.SyncLevel;
    471 
    472                 ObjDesc->Method.Mutex->Mutex.ThreadId =
    473                     AcpiOsGetThreadId ();
    474             }
    475         }
    476 
    477         /* Always increase acquisition depth */
    478 
    479         ObjDesc->Method.Mutex->Mutex.AcquisitionDepth++;
    480     }
    481 
    482     /*
    483      * Allocate an Owner ID for this method, only if this is the first thread
    484      * to begin concurrent execution. We only need one OwnerId, even if the
    485      * method is invoked recursively.
    486      */
    487     if (!ObjDesc->Method.OwnerId)
    488     {
    489         Status = AcpiUtAllocateOwnerId (&ObjDesc->Method.OwnerId);
    490         if (ACPI_FAILURE (Status))
    491         {
    492             goto Cleanup;
    493         }
    494     }
    495 
    496     /*
    497      * Increment the method parse tree thread count since it has been
    498      * reentered one more time (even if it is the same thread)
    499      */
    500     ObjDesc->Method.ThreadCount++;
    501     AcpiMethodCount++;
    502     return_ACPI_STATUS (Status);
    503 
    504 
    505 Cleanup:
    506     /* On error, must release the method mutex (if present) */
    507 
    508     if (ObjDesc->Method.Mutex)
    509     {
    510         AcpiOsReleaseMutex (ObjDesc->Method.Mutex->Mutex.OsMutex);
    511     }
    512     return_ACPI_STATUS (Status);
    513 }
    514 
    515 
    516 /*******************************************************************************
    517  *
    518  * FUNCTION:    AcpiDsCallControlMethod
    519  *
    520  * PARAMETERS:  Thread              - Info for this thread
    521  *              ThisWalkState       - Current walk state
    522  *              Op                  - Current Op to be walked
    523  *
    524  * RETURN:      Status
    525  *
    526  * DESCRIPTION: Transfer execution to a called control method
    527  *
    528  ******************************************************************************/
    529 
    530 ACPI_STATUS
    531 AcpiDsCallControlMethod (
    532     ACPI_THREAD_STATE       *Thread,
    533     ACPI_WALK_STATE         *ThisWalkState,
    534     ACPI_PARSE_OBJECT       *Op)
    535 {
    536     ACPI_STATUS             Status;
    537     ACPI_NAMESPACE_NODE     *MethodNode;
    538     ACPI_WALK_STATE         *NextWalkState = NULL;
    539     ACPI_OPERAND_OBJECT     *ObjDesc;
    540     ACPI_EVALUATE_INFO      *Info;
    541 
    542     ACPI_FUNCTION_TRACE_PTR (DsCallControlMethod, ThisWalkState);
    543 
    544     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
    545         "Calling method %p, currentstate=%p\n",
    546         ThisWalkState->PrevOp, ThisWalkState));
    547 
    548     /*
    549      * Get the namespace entry for the control method we are about to call
    550      */
    551     MethodNode = ThisWalkState->MethodCallNode;
    552     if (!MethodNode)
    553     {
    554         return_ACPI_STATUS (AE_NULL_ENTRY);
    555     }
    556 
    557     ObjDesc = AcpiNsGetAttachedObject (MethodNode);
    558     if (!ObjDesc)
    559     {
    560         return_ACPI_STATUS (AE_NULL_OBJECT);
    561     }
    562 
    563     if (ThisWalkState->NumOperands < ObjDesc->Method.ParamCount)
    564     {
    565         ACPI_ERROR ((AE_INFO, "Missing argument(s) for method [%4.4s]",
    566             AcpiUtGetNodeName (MethodNode)));
    567 
    568         return_ACPI_STATUS (AE_AML_TOO_FEW_ARGUMENTS);
    569     }
    570 
    571     else if (ThisWalkState->NumOperands > ObjDesc->Method.ParamCount)
    572     {
    573         ACPI_ERROR ((AE_INFO, "Too many arguments for method [%4.4s]",
    574             AcpiUtGetNodeName (MethodNode)));
    575 
    576         return_ACPI_STATUS (AE_AML_TOO_MANY_ARGUMENTS);
    577     }
    578 
    579 
    580     /* Init for new method, possibly wait on method mutex */
    581 
    582     Status = AcpiDsBeginMethodExecution (
    583         MethodNode, ObjDesc, ThisWalkState);
    584     if (ACPI_FAILURE (Status))
    585     {
    586         return_ACPI_STATUS (Status);
    587     }
    588 
    589     /* Begin method parse/execution. Create a new walk state */
    590 
    591     NextWalkState = AcpiDsCreateWalkState (
    592         ObjDesc->Method.OwnerId, NULL, ObjDesc, Thread);
    593     if (!NextWalkState)
    594     {
    595         Status = AE_NO_MEMORY;
    596         goto Cleanup;
    597     }
    598 
    599     /*
    600      * The resolved arguments were put on the previous walk state's operand
    601      * stack. Operands on the previous walk state stack always
    602      * start at index 0. Also, null terminate the list of arguments
    603      */
    604     ThisWalkState->Operands [ThisWalkState->NumOperands] = NULL;
    605 
    606     /*
    607      * Allocate and initialize the evaluation information block
    608      * TBD: this is somewhat inefficient, should change interface to
    609      * DsInitAmlWalk. For now, keeps this struct off the CPU stack
    610      */
    611     Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
    612     if (!Info)
    613     {
    614         Status = AE_NO_MEMORY;
    615         goto PopWalkState;
    616     }
    617 
    618     Info->Parameters = &ThisWalkState->Operands[0];
    619 
    620     Status = AcpiDsInitAmlWalk (NextWalkState, NULL, MethodNode,
    621         ObjDesc->Method.AmlStart, ObjDesc->Method.AmlLength,
    622         Info, ACPI_IMODE_EXECUTE);
    623 
    624     ACPI_FREE (Info);
    625     if (ACPI_FAILURE (Status))
    626     {
    627         goto PopWalkState;
    628     }
    629 
    630     NextWalkState->MethodNestingDepth = ThisWalkState->MethodNestingDepth + 1;
    631 
    632     /*
    633      * Delete the operands on the previous walkstate operand stack
    634      * (they were copied to new objects)
    635      */
    636     AcpiDsClearOperands (ThisWalkState);
    637 
    638     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
    639         "**** Begin nested execution of [%4.4s] **** WalkState=%p\n",
    640         MethodNode->Name.Ascii, NextWalkState));
    641 
    642     ThisWalkState->MethodPathname = AcpiNsGetNormalizedPathname (MethodNode, TRUE);
    643     ThisWalkState->MethodIsNested = TRUE;
    644 
    645     /* Optional object evaluation log */
    646 
    647     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_EVALUATION,
    648         "%-26s:  %*s%s\n", "   Nested method call",
    649         NextWalkState->MethodNestingDepth * 3, " ",
    650         &ThisWalkState->MethodPathname[1]));
    651 
    652     /* Invoke an internal method if necessary */
    653 
    654     if (ObjDesc->Method.InfoFlags & ACPI_METHOD_INTERNAL_ONLY)
    655     {
    656         Status = ObjDesc->Method.Dispatch.Implementation (NextWalkState);
    657         if (Status == AE_OK)
    658         {
    659             Status = AE_CTRL_TERMINATE;
    660         }
    661     }
    662 
    663     return_ACPI_STATUS (Status);
    664 
    665 
    666 PopWalkState:
    667 
    668     /* On error, pop the walk state to be deleted from thread */
    669 
    670     AcpiDsPopWalkState(Thread);
    671 
    672 Cleanup:
    673 
    674     /* On error, we must terminate the method properly */
    675 
    676     AcpiDsTerminateControlMethod (ObjDesc, NextWalkState);
    677     AcpiDsDeleteWalkState (NextWalkState);
    678 
    679     return_ACPI_STATUS (Status);
    680 }
    681 
    682 
    683 /*******************************************************************************
    684  *
    685  * FUNCTION:    AcpiDsRestartControlMethod
    686  *
    687  * PARAMETERS:  WalkState           - State for preempted method (caller)
    688  *              ReturnDesc          - Return value from the called method
    689  *
    690  * RETURN:      Status
    691  *
    692  * DESCRIPTION: Restart a method that was preempted by another (nested) method
    693  *              invocation. Handle the return value (if any) from the callee.
    694  *
    695  ******************************************************************************/
    696 
    697 ACPI_STATUS
    698 AcpiDsRestartControlMethod (
    699     ACPI_WALK_STATE         *WalkState,
    700     ACPI_OPERAND_OBJECT     *ReturnDesc)
    701 {
    702     ACPI_STATUS             Status;
    703     int                     SameAsImplicitReturn;
    704 
    705 
    706     ACPI_FUNCTION_TRACE_PTR (DsRestartControlMethod, WalkState);
    707 
    708 
    709     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
    710         "****Restart [%4.4s] Op %p ReturnValueFromCallee %p\n",
    711         AcpiUtGetNodeName (WalkState->MethodNode),
    712         WalkState->MethodCallOp, ReturnDesc));
    713 
    714     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
    715         "    ReturnFromThisMethodUsed?=%X ResStack %p Walk %p\n",
    716         WalkState->ReturnUsed,
    717         WalkState->Results, WalkState));
    718 
    719     /* Did the called method return a value? */
    720 
    721     if (ReturnDesc)
    722     {
    723         /* Is the implicit return object the same as the return desc? */
    724 
    725         SameAsImplicitReturn = (WalkState->ImplicitReturnObj == ReturnDesc);
    726 
    727         /* Are we actually going to use the return value? */
    728 
    729         if (WalkState->ReturnUsed)
    730         {
    731             /* Save the return value from the previous method */
    732 
    733             Status = AcpiDsResultPush (ReturnDesc, WalkState);
    734             if (ACPI_FAILURE (Status))
    735             {
    736                 AcpiUtRemoveReference (ReturnDesc);
    737                 return_ACPI_STATUS (Status);
    738             }
    739 
    740             /*
    741              * Save as THIS method's return value in case it is returned
    742              * immediately to yet another method
    743              */
    744             WalkState->ReturnDesc = ReturnDesc;
    745         }
    746 
    747         /*
    748          * The following code is the optional support for the so-called
    749          * "implicit return". Some AML code assumes that the last value of the
    750          * method is "implicitly" returned to the caller, in the absence of an
    751          * explicit return value.
    752          *
    753          * Just save the last result of the method as the return value.
    754          *
    755          * NOTE: this is optional because the ASL language does not actually
    756          * support this behavior.
    757          */
    758         else if (!AcpiDsDoImplicitReturn (ReturnDesc, WalkState, FALSE) ||
    759                  SameAsImplicitReturn)
    760         {
    761             /*
    762              * Delete the return value if it will not be used by the
    763              * calling method or remove one reference if the explicit return
    764              * is the same as the implicit return value.
    765              */
    766             AcpiUtRemoveReference (ReturnDesc);
    767         }
    768     }
    769 
    770     return_ACPI_STATUS (AE_OK);
    771 }
    772 
    773 
    774 /*******************************************************************************
    775  *
    776  * FUNCTION:    AcpiDsTerminateControlMethod
    777  *
    778  * PARAMETERS:  MethodDesc          - Method object
    779  *              WalkState           - State associated with the method
    780  *
    781  * RETURN:      None
    782  *
    783  * DESCRIPTION: Terminate a control method. Delete everything that the method
    784  *              created, delete all locals and arguments, and delete the parse
    785  *              tree if requested.
    786  *
    787  * MUTEX:       Interpreter is locked
    788  *
    789  ******************************************************************************/
    790 
    791 void
    792 AcpiDsTerminateControlMethod (
    793     ACPI_OPERAND_OBJECT     *MethodDesc,
    794     ACPI_WALK_STATE         *WalkState)
    795 {
    796     UINT32                  i;
    797     ACPI_NAMESPACE_NODE     *RefNode;
    798 
    799     ACPI_FUNCTION_TRACE_PTR (DsTerminateControlMethod, WalkState);
    800 
    801 
    802     /* MethodDesc is required, WalkState is optional */
    803 
    804     if (!MethodDesc)
    805     {
    806         return_VOID;
    807     }
    808 
    809     if (WalkState)
    810     {
    811         /*
    812          * Check if the return value is a RefOf reference to a method local
    813          * or argument. If so, clear the reference to avoid use-after-free
    814          * when the walk state is deleted.
    815          */
    816         if (WalkState->ReturnDesc &&
    817             (WalkState->ReturnDesc->Common.Type == ACPI_TYPE_LOCAL_REFERENCE) &&
    818             (WalkState->ReturnDesc->Reference.Class == ACPI_REFCLASS_REFOF))
    819         {
    820             RefNode = WalkState->ReturnDesc->Reference.Object;
    821             if (RefNode)
    822             {
    823                 /* Check against method locals */
    824                 for (i = 0; i < ACPI_METHOD_NUM_LOCALS; i++)
    825                 {
    826                     if (RefNode == &WalkState->LocalVariables[i])
    827                     {
    828                         AcpiUtRemoveReference (WalkState->ReturnDesc);
    829                         WalkState->ReturnDesc = NULL;
    830                         break;
    831                     }
    832                 }
    833 
    834                 /* Check against method arguments if not already cleared */
    835                 if (WalkState->ReturnDesc)
    836                 {
    837                     for (i = 0; i < ACPI_METHOD_NUM_ARGS; i++)
    838                     {
    839                         if (RefNode == &WalkState->Arguments[i])
    840                         {
    841                             AcpiUtRemoveReference (WalkState->ReturnDesc);
    842                             WalkState->ReturnDesc = NULL;
    843                             break;
    844                         }
    845                     }
    846                 }
    847             }
    848         }
    849 
    850         /* Delete all arguments and locals */
    851 
    852         AcpiDsMethodDataDeleteAll (WalkState);
    853 
    854         /*
    855          * Delete any namespace objects created anywhere within the
    856          * namespace by the execution of this method. Unless:
    857          * 1) This method is a module-level executable code method, in which
    858          *    case we want make the objects permanent.
    859          * 2) There are other threads executing the method, in which case we
    860          *    will wait until the last thread has completed.
    861          */
    862         if (!(MethodDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL) &&
    863              (MethodDesc->Method.ThreadCount == 1))
    864         {
    865             /* Delete any direct children of (created by) this method */
    866 
    867             (void) AcpiExExitInterpreter ();
    868             AcpiNsDeleteNamespaceSubtree (WalkState->MethodNode);
    869             (void) AcpiExEnterInterpreter ();
    870 
    871             /*
    872              * Delete any objects that were created by this method
    873              * elsewhere in the namespace (if any were created).
    874              * Use of the ACPI_METHOD_MODIFIED_NAMESPACE optimizes the
    875              * deletion such that we don't have to perform an entire
    876              * namespace walk for every control method execution.
    877              */
    878             if (MethodDesc->Method.InfoFlags & ACPI_METHOD_MODIFIED_NAMESPACE)
    879             {
    880                 (void) AcpiExExitInterpreter ();
    881                 AcpiNsDeleteNamespaceByOwner (MethodDesc->Method.OwnerId);
    882                 (void) AcpiExEnterInterpreter ();
    883                 MethodDesc->Method.InfoFlags &=
    884                     ~ACPI_METHOD_MODIFIED_NAMESPACE;
    885             }
    886         }
    887 
    888         /*
    889          * If method is serialized, release the mutex and restore the
    890          * current sync level for this thread
    891          */
    892         if (MethodDesc->Method.Mutex)
    893         {
    894             /* Acquisition Depth handles recursive calls */
    895 
    896             MethodDesc->Method.Mutex->Mutex.AcquisitionDepth--;
    897             if (!MethodDesc->Method.Mutex->Mutex.AcquisitionDepth)
    898             {
    899                 WalkState->Thread->CurrentSyncLevel =
    900                     MethodDesc->Method.Mutex->Mutex.OriginalSyncLevel;
    901 
    902                 AcpiOsReleaseMutex (
    903                     MethodDesc->Method.Mutex->Mutex.OsMutex);
    904                 MethodDesc->Method.Mutex->Mutex.ThreadId = 0;
    905             }
    906         }
    907     }
    908 
    909     /* Decrement the thread count on the method */
    910 
    911     if (MethodDesc->Method.ThreadCount)
    912     {
    913         MethodDesc->Method.ThreadCount--;
    914     }
    915     else
    916     {
    917         ACPI_ERROR ((AE_INFO,
    918             "Invalid zero thread count in method"));
    919     }
    920 
    921     /* Are there any other threads currently executing this method? */
    922 
    923     if (MethodDesc->Method.ThreadCount)
    924     {
    925         /*
    926          * Additional threads. Do not release the OwnerId in this case,
    927          * we immediately reuse it for the next thread executing this method
    928          */
    929         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
    930             "*** Completed execution of one thread, %u threads remaining\n",
    931             MethodDesc->Method.ThreadCount));
    932     }
    933     else
    934     {
    935         /* This is the only executing thread for this method */
    936 
    937         /*
    938          * Support to dynamically change a method from NotSerialized to
    939          * Serialized if it appears that the method is incorrectly written and
    940          * does not support multiple thread execution. The best example of this
    941          * is if such a method creates namespace objects and blocks. A second
    942          * thread will fail with an AE_ALREADY_EXISTS exception.
    943          *
    944          * This code is here because we must wait until the last thread exits
    945          * before marking the method as serialized.
    946          */
    947         if (MethodDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED_PENDING)
    948         {
    949             if (WalkState)
    950             {
    951                 ACPI_INFO ((
    952                     "Marking method %4.4s as Serialized "
    953                     "because of AE_ALREADY_EXISTS error",
    954                     WalkState->MethodNode->Name.Ascii));
    955             }
    956 
    957             /*
    958              * Method tried to create an object twice and was marked as
    959              * "pending serialized". The probable cause is that the method
    960              * cannot handle reentrancy.
    961              *
    962              * The method was created as NotSerialized, but it tried to create
    963              * a named object and then blocked, causing the second thread
    964              * entrance to begin and then fail. Workaround this problem by
    965              * marking the method permanently as Serialized when the last
    966              * thread exits here.
    967              */
    968             MethodDesc->Method.InfoFlags &=
    969                 ~ACPI_METHOD_SERIALIZED_PENDING;
    970 
    971             MethodDesc->Method.InfoFlags |=
    972                 (ACPI_METHOD_SERIALIZED | ACPI_METHOD_IGNORE_SYNC_LEVEL);
    973             MethodDesc->Method.SyncLevel = 0;
    974         }
    975 
    976         /* No more threads, we can free the OwnerId */
    977 
    978         if (!(MethodDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL))
    979         {
    980             AcpiUtReleaseOwnerId (&MethodDesc->Method.OwnerId);
    981         }
    982     }
    983 
    984     AcpiExStopTraceMethod ((ACPI_NAMESPACE_NODE *) MethodDesc->Method.Node,
    985         MethodDesc, WalkState);
    986 
    987     return_VOID;
    988 }
    989