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