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