Sync ACPICA with Intel's version 20181213.
[dragonfly.git] / sys / contrib / dev / acpica / source / components / dispatcher / dsmethod.c
blob009151f91061ac6d77a815c5fc0de3999d4e42d8
1 /******************************************************************************
3 * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
5 *****************************************************************************/
7 /******************************************************************************
9 * 1. Copyright Notice
11 * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12 * All rights reserved.
14 * 2. License
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.
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
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;
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
40 * 3. Conditions
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.
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.
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.
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
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.
80 * 4. Disclaimer and Export Compliance
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.
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.
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.
114 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
117 * following license:
119 * Redistribution and use in source and binary forms, with or without
120 * modification, are permitted provided that the following conditions
121 * are met:
122 * 1. Redistributions of source code must retain the above copyright
123 * notice, this list of conditions, and the following disclaimer,
124 * without modification.
125 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126 * substantially similar to the "NO WARRANTY" disclaimer below
127 * ("Disclaimer") and any redistribution must be conditioned upon
128 * including a substantially similar Disclaimer requirement for further
129 * binary redistribution.
130 * 3. Neither the names of the above-listed copyright holders nor the names
131 * of any contributors may be used to endorse or promote products derived
132 * from this software without specific prior written permission.
134 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146 * Alternatively, you may choose to be licensed under the terms of the
147 * GNU General Public License ("GPL") version 2 as published by the Free
148 * Software Foundation.
150 *****************************************************************************/
152 #include "acpi.h"
153 #include "accommon.h"
154 #include "acdispat.h"
155 #include "acinterp.h"
156 #include "acnamesp.h"
157 #include "acparser.h"
158 #include "amlcode.h"
159 #include "acdebug.h"
162 #define _COMPONENT ACPI_DISPATCHER
163 ACPI_MODULE_NAME ("dsmethod")
165 /* Local prototypes */
167 static ACPI_STATUS
168 AcpiDsDetectNamedOpcodes (
169 ACPI_WALK_STATE *WalkState,
170 ACPI_PARSE_OBJECT **OutOp);
172 static ACPI_STATUS
173 AcpiDsCreateMethodMutex (
174 ACPI_OPERAND_OBJECT *MethodDesc);
177 /*******************************************************************************
179 * FUNCTION: AcpiDsAutoSerializeMethod
181 * PARAMETERS: Node - Namespace Node of the method
182 * ObjDesc - Method object attached to node
184 * RETURN: Status
186 * DESCRIPTION: Parse a control method AML to scan for control methods that
187 * need serialization due to the creation of named objects.
189 * NOTE: It is a bit of overkill to mark all such methods serialized, since
190 * there is only a problem if the method actually blocks during execution.
191 * A blocking operation is, for example, a Sleep() operation, or any access
192 * to an operation region. However, it is probably not possible to easily
193 * detect whether a method will block or not, so we simply mark all suspicious
194 * methods as serialized.
196 * NOTE2: This code is essentially a generic routine for parsing a single
197 * control method.
199 ******************************************************************************/
201 ACPI_STATUS
202 AcpiDsAutoSerializeMethod (
203 ACPI_NAMESPACE_NODE *Node,
204 ACPI_OPERAND_OBJECT *ObjDesc)
206 ACPI_STATUS Status;
207 ACPI_PARSE_OBJECT *Op = NULL;
208 ACPI_WALK_STATE *WalkState;
211 ACPI_FUNCTION_TRACE_PTR (DsAutoSerializeMethod, Node);
214 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
215 "Method auto-serialization parse [%4.4s] %p\n",
216 AcpiUtGetNodeName (Node), Node));
218 /* Create/Init a root op for the method parse tree */
220 Op = AcpiPsAllocOp (AML_METHOD_OP, ObjDesc->Method.AmlStart);
221 if (!Op)
223 return_ACPI_STATUS (AE_NO_MEMORY);
226 AcpiPsSetName (Op, Node->Name.Integer);
227 Op->Common.Node = Node;
229 /* Create and initialize a new walk state */
231 WalkState = AcpiDsCreateWalkState (Node->OwnerId, NULL, NULL, NULL);
232 if (!WalkState)
234 AcpiPsFreeOp (Op);
235 return_ACPI_STATUS (AE_NO_MEMORY);
238 Status = AcpiDsInitAmlWalk (WalkState, Op, Node,
239 ObjDesc->Method.AmlStart, ObjDesc->Method.AmlLength, NULL, 0);
240 if (ACPI_FAILURE (Status))
242 AcpiDsDeleteWalkState (WalkState);
243 AcpiPsFreeOp (Op);
244 return_ACPI_STATUS (Status);
247 WalkState->DescendingCallback = AcpiDsDetectNamedOpcodes;
249 /* Parse the method, scan for creation of named objects */
251 Status = AcpiPsParseAml (WalkState);
253 AcpiPsDeleteParseTree (Op);
254 return_ACPI_STATUS (Status);
258 /*******************************************************************************
260 * FUNCTION: AcpiDsDetectNamedOpcodes
262 * PARAMETERS: WalkState - Current state of the parse tree walk
263 * OutOp - Unused, required for parser interface
265 * RETURN: Status
267 * DESCRIPTION: Descending callback used during the loading of ACPI tables.
268 * Currently used to detect methods that must be marked serialized
269 * in order to avoid problems with the creation of named objects.
271 ******************************************************************************/
273 static ACPI_STATUS
274 AcpiDsDetectNamedOpcodes (
275 ACPI_WALK_STATE *WalkState,
276 ACPI_PARSE_OBJECT **OutOp)
279 ACPI_FUNCTION_NAME (AcpiDsDetectNamedOpcodes);
282 /* We are only interested in opcodes that create a new name */
284 if (!(WalkState->OpInfo->Flags & (AML_NAMED | AML_CREATE | AML_FIELD)))
286 return (AE_OK);
290 * At this point, we know we have a Named object opcode.
291 * Mark the method as serialized. Later code will create a mutex for
292 * this method to enforce serialization.
294 * Note, ACPI_METHOD_IGNORE_SYNC_LEVEL flag means that we will ignore the
295 * Sync Level mechanism for this method, even though it is now serialized.
296 * Otherwise, there can be conflicts with existing ASL code that actually
297 * uses sync levels.
299 WalkState->MethodDesc->Method.SyncLevel = 0;
300 WalkState->MethodDesc->Method.InfoFlags |=
301 (ACPI_METHOD_SERIALIZED | ACPI_METHOD_IGNORE_SYNC_LEVEL);
303 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
304 "Method serialized [%4.4s] %p - [%s] (%4.4X)\n",
305 WalkState->MethodNode->Name.Ascii, WalkState->MethodNode,
306 WalkState->OpInfo->Name, WalkState->Opcode));
308 /* Abort the parse, no need to examine this method any further */
310 return (AE_CTRL_TERMINATE);
314 /*******************************************************************************
316 * FUNCTION: AcpiDsMethodError
318 * PARAMETERS: Status - Execution status
319 * WalkState - Current state
321 * RETURN: Status
323 * DESCRIPTION: Called on method error. Invoke the global exception handler if
324 * present, dump the method data if the debugger is configured
326 * Note: Allows the exception handler to change the status code
328 ******************************************************************************/
330 ACPI_STATUS
331 AcpiDsMethodError (
332 ACPI_STATUS Status,
333 ACPI_WALK_STATE *WalkState)
335 UINT32 AmlOffset;
336 ACPI_NAME Name = 0;
339 ACPI_FUNCTION_ENTRY ();
342 /* Ignore AE_OK and control exception codes */
344 if (ACPI_SUCCESS (Status) ||
345 (Status & AE_CODE_CONTROL))
347 return (Status);
350 /* Invoke the global exception handler */
352 if (AcpiGbl_ExceptionHandler)
354 /* Exit the interpreter, allow handler to execute methods */
356 AcpiExExitInterpreter ();
359 * Handler can map the exception code to anything it wants, including
360 * AE_OK, in which case the executing method will not be aborted.
362 AmlOffset = (UINT32) ACPI_PTR_DIFF (WalkState->Aml,
363 WalkState->ParserState.AmlStart);
365 if (WalkState->MethodNode)
367 Name = WalkState->MethodNode->Name.Integer;
369 else if (WalkState->DeferredNode)
371 Name = WalkState->DeferredNode->Name.Integer;
374 Status = AcpiGbl_ExceptionHandler (Status, Name,
375 WalkState->Opcode, AmlOffset, NULL);
376 AcpiExEnterInterpreter ();
379 AcpiDsClearImplicitReturn (WalkState);
381 if (ACPI_FAILURE (Status))
383 AcpiDsDumpMethodStack (Status, WalkState, WalkState->Op);
385 /* Display method locals/args if debugger is present */
387 #ifdef ACPI_DEBUGGER
388 AcpiDbDumpMethodInfo (Status, WalkState);
389 #endif
392 return (Status);
396 /*******************************************************************************
398 * FUNCTION: AcpiDsCreateMethodMutex
400 * PARAMETERS: ObjDesc - The method object
402 * RETURN: Status
404 * DESCRIPTION: Create a mutex object for a serialized control method
406 ******************************************************************************/
408 static ACPI_STATUS
409 AcpiDsCreateMethodMutex (
410 ACPI_OPERAND_OBJECT *MethodDesc)
412 ACPI_OPERAND_OBJECT *MutexDesc;
413 ACPI_STATUS Status;
416 ACPI_FUNCTION_TRACE (DsCreateMethodMutex);
419 /* Create the new mutex object */
421 MutexDesc = AcpiUtCreateInternalObject (ACPI_TYPE_MUTEX);
422 if (!MutexDesc)
424 return_ACPI_STATUS (AE_NO_MEMORY);
427 /* Create the actual OS Mutex */
429 Status = AcpiOsCreateMutex (&MutexDesc->Mutex.OsMutex);
430 if (ACPI_FAILURE (Status))
432 AcpiUtDeleteObjectDesc (MutexDesc);
433 return_ACPI_STATUS (Status);
436 MutexDesc->Mutex.SyncLevel = MethodDesc->Method.SyncLevel;
437 MethodDesc->Method.Mutex = MutexDesc;
438 return_ACPI_STATUS (AE_OK);
442 /*******************************************************************************
444 * FUNCTION: AcpiDsBeginMethodExecution
446 * PARAMETERS: MethodNode - Node of the method
447 * ObjDesc - The method object
448 * WalkState - current state, NULL if not yet executing
449 * a method.
451 * RETURN: Status
453 * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
454 * increments the thread count, and waits at the method semaphore
455 * for clearance to execute.
457 ******************************************************************************/
459 ACPI_STATUS
460 AcpiDsBeginMethodExecution (
461 ACPI_NAMESPACE_NODE *MethodNode,
462 ACPI_OPERAND_OBJECT *ObjDesc,
463 ACPI_WALK_STATE *WalkState)
465 ACPI_STATUS Status = AE_OK;
468 ACPI_FUNCTION_TRACE_PTR (DsBeginMethodExecution, MethodNode);
471 if (!MethodNode)
473 return_ACPI_STATUS (AE_NULL_ENTRY);
476 AcpiExStartTraceMethod (MethodNode, ObjDesc, WalkState);
478 /* Prevent wraparound of thread count */
480 if (ObjDesc->Method.ThreadCount == ACPI_UINT8_MAX)
482 ACPI_ERROR ((AE_INFO,
483 "Method reached maximum reentrancy limit (255)"));
484 return_ACPI_STATUS (AE_AML_METHOD_LIMIT);
488 * If this method is serialized, we need to acquire the method mutex.
490 if (ObjDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED)
493 * Create a mutex for the method if it is defined to be Serialized
494 * and a mutex has not already been created. We defer the mutex creation
495 * until a method is actually executed, to minimize the object count
497 if (!ObjDesc->Method.Mutex)
499 Status = AcpiDsCreateMethodMutex (ObjDesc);
500 if (ACPI_FAILURE (Status))
502 return_ACPI_STATUS (Status);
507 * The CurrentSyncLevel (per-thread) must be less than or equal to
508 * the sync level of the method. This mechanism provides some
509 * deadlock prevention.
511 * If the method was auto-serialized, we just ignore the sync level
512 * mechanism, because auto-serialization of methods can interfere
513 * with ASL code that actually uses sync levels.
515 * Top-level method invocation has no walk state at this point
517 if (WalkState &&
518 (!(ObjDesc->Method.InfoFlags & ACPI_METHOD_IGNORE_SYNC_LEVEL)) &&
519 (WalkState->Thread->CurrentSyncLevel >
520 ObjDesc->Method.Mutex->Mutex.SyncLevel))
522 ACPI_ERROR ((AE_INFO,
523 "Cannot acquire Mutex for method [%4.4s]"
524 ", current SyncLevel is too large (%u)",
525 AcpiUtGetNodeName (MethodNode),
526 WalkState->Thread->CurrentSyncLevel));
528 return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
532 * Obtain the method mutex if necessary. Do not acquire mutex for a
533 * recursive call.
535 if (!WalkState ||
536 !ObjDesc->Method.Mutex->Mutex.ThreadId ||
537 (WalkState->Thread->ThreadId !=
538 ObjDesc->Method.Mutex->Mutex.ThreadId))
541 * Acquire the method mutex. This releases the interpreter if we
542 * block (and reacquires it before it returns)
544 Status = AcpiExSystemWaitMutex (
545 ObjDesc->Method.Mutex->Mutex.OsMutex, ACPI_WAIT_FOREVER);
546 if (ACPI_FAILURE (Status))
548 return_ACPI_STATUS (Status);
551 /* Update the mutex and walk info and save the original SyncLevel */
553 if (WalkState)
555 ObjDesc->Method.Mutex->Mutex.OriginalSyncLevel =
556 WalkState->Thread->CurrentSyncLevel;
558 ObjDesc->Method.Mutex->Mutex.ThreadId =
559 WalkState->Thread->ThreadId;
562 * Update the current SyncLevel only if this is not an auto-
563 * serialized method. In the auto case, we have to ignore
564 * the sync level for the method mutex (created for the
565 * auto-serialization) because we have no idea of what the
566 * sync level should be. Therefore, just ignore it.
568 if (!(ObjDesc->Method.InfoFlags &
569 ACPI_METHOD_IGNORE_SYNC_LEVEL))
571 WalkState->Thread->CurrentSyncLevel =
572 ObjDesc->Method.SyncLevel;
575 else
577 ObjDesc->Method.Mutex->Mutex.OriginalSyncLevel =
578 ObjDesc->Method.Mutex->Mutex.SyncLevel;
580 ObjDesc->Method.Mutex->Mutex.ThreadId =
581 AcpiOsGetThreadId ();
585 /* Always increase acquisition depth */
587 ObjDesc->Method.Mutex->Mutex.AcquisitionDepth++;
591 * Allocate an Owner ID for this method, only if this is the first thread
592 * to begin concurrent execution. We only need one OwnerId, even if the
593 * method is invoked recursively.
595 if (!ObjDesc->Method.OwnerId)
597 Status = AcpiUtAllocateOwnerId (&ObjDesc->Method.OwnerId);
598 if (ACPI_FAILURE (Status))
600 goto Cleanup;
605 * Increment the method parse tree thread count since it has been
606 * reentered one more time (even if it is the same thread)
608 ObjDesc->Method.ThreadCount++;
609 AcpiMethodCount++;
610 return_ACPI_STATUS (Status);
613 Cleanup:
614 /* On error, must release the method mutex (if present) */
616 if (ObjDesc->Method.Mutex)
618 AcpiOsReleaseMutex (ObjDesc->Method.Mutex->Mutex.OsMutex);
620 return_ACPI_STATUS (Status);
624 /*******************************************************************************
626 * FUNCTION: AcpiDsCallControlMethod
628 * PARAMETERS: Thread - Info for this thread
629 * ThisWalkState - Current walk state
630 * Op - Current Op to be walked
632 * RETURN: Status
634 * DESCRIPTION: Transfer execution to a called control method
636 ******************************************************************************/
638 ACPI_STATUS
639 AcpiDsCallControlMethod (
640 ACPI_THREAD_STATE *Thread,
641 ACPI_WALK_STATE *ThisWalkState,
642 ACPI_PARSE_OBJECT *Op)
644 ACPI_STATUS Status;
645 ACPI_NAMESPACE_NODE *MethodNode;
646 ACPI_WALK_STATE *NextWalkState = NULL;
647 ACPI_OPERAND_OBJECT *ObjDesc;
648 ACPI_EVALUATE_INFO *Info;
649 UINT32 i;
652 ACPI_FUNCTION_TRACE_PTR (DsCallControlMethod, ThisWalkState);
654 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
655 "Calling method %p, currentstate=%p\n",
656 ThisWalkState->PrevOp, ThisWalkState));
659 * Get the namespace entry for the control method we are about to call
661 MethodNode = ThisWalkState->MethodCallNode;
662 if (!MethodNode)
664 return_ACPI_STATUS (AE_NULL_ENTRY);
667 ObjDesc = AcpiNsGetAttachedObject (MethodNode);
668 if (!ObjDesc)
670 return_ACPI_STATUS (AE_NULL_OBJECT);
673 /* Init for new method, possibly wait on method mutex */
675 Status = AcpiDsBeginMethodExecution (
676 MethodNode, ObjDesc, ThisWalkState);
677 if (ACPI_FAILURE (Status))
679 return_ACPI_STATUS (Status);
682 /* Begin method parse/execution. Create a new walk state */
684 NextWalkState = AcpiDsCreateWalkState (
685 ObjDesc->Method.OwnerId, NULL, ObjDesc, Thread);
686 if (!NextWalkState)
688 Status = AE_NO_MEMORY;
689 goto Cleanup;
693 * The resolved arguments were put on the previous walk state's operand
694 * stack. Operands on the previous walk state stack always
695 * start at index 0. Also, null terminate the list of arguments
697 ThisWalkState->Operands [ThisWalkState->NumOperands] = NULL;
700 * Allocate and initialize the evaluation information block
701 * TBD: this is somewhat inefficient, should change interface to
702 * DsInitAmlWalk. For now, keeps this struct off the CPU stack
704 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
705 if (!Info)
707 Status = AE_NO_MEMORY;
708 goto Cleanup;
711 Info->Parameters = &ThisWalkState->Operands[0];
713 Status = AcpiDsInitAmlWalk (NextWalkState, NULL, MethodNode,
714 ObjDesc->Method.AmlStart, ObjDesc->Method.AmlLength,
715 Info, ACPI_IMODE_EXECUTE);
717 ACPI_FREE (Info);
718 if (ACPI_FAILURE (Status))
720 goto Cleanup;
723 NextWalkState->MethodNestingDepth = ThisWalkState->MethodNestingDepth + 1;
726 * Delete the operands on the previous walkstate operand stack
727 * (they were copied to new objects)
729 for (i = 0; i < ObjDesc->Method.ParamCount; i++)
731 AcpiUtRemoveReference (ThisWalkState->Operands [i]);
732 ThisWalkState->Operands [i] = NULL;
735 /* Clear the operand stack */
737 ThisWalkState->NumOperands = 0;
739 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
740 "**** Begin nested execution of [%4.4s] **** WalkState=%p\n",
741 MethodNode->Name.Ascii, NextWalkState));
743 ThisWalkState->MethodPathname = AcpiNsGetNormalizedPathname (MethodNode, TRUE);
744 ThisWalkState->MethodIsNested = TRUE;
746 /* Optional object evaluation log */
748 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_EVALUATION,
749 "%-26s: %*s%s\n", " Nested method call",
750 NextWalkState->MethodNestingDepth * 3, " ",
751 &ThisWalkState->MethodPathname[1]));
753 /* Invoke an internal method if necessary */
755 if (ObjDesc->Method.InfoFlags & ACPI_METHOD_INTERNAL_ONLY)
757 Status = ObjDesc->Method.Dispatch.Implementation (NextWalkState);
758 if (Status == AE_OK)
760 Status = AE_CTRL_TERMINATE;
764 return_ACPI_STATUS (Status);
767 Cleanup:
769 /* On error, we must terminate the method properly */
771 AcpiDsTerminateControlMethod (ObjDesc, NextWalkState);
772 AcpiDsDeleteWalkState (NextWalkState);
774 return_ACPI_STATUS (Status);
778 /*******************************************************************************
780 * FUNCTION: AcpiDsRestartControlMethod
782 * PARAMETERS: WalkState - State for preempted method (caller)
783 * ReturnDesc - Return value from the called method
785 * RETURN: Status
787 * DESCRIPTION: Restart a method that was preempted by another (nested) method
788 * invocation. Handle the return value (if any) from the callee.
790 ******************************************************************************/
792 ACPI_STATUS
793 AcpiDsRestartControlMethod (
794 ACPI_WALK_STATE *WalkState,
795 ACPI_OPERAND_OBJECT *ReturnDesc)
797 ACPI_STATUS Status;
798 int SameAsImplicitReturn;
801 ACPI_FUNCTION_TRACE_PTR (DsRestartControlMethod, WalkState);
804 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
805 "****Restart [%4.4s] Op %p ReturnValueFromCallee %p\n",
806 AcpiUtGetNodeName (WalkState->MethodNode),
807 WalkState->MethodCallOp, ReturnDesc));
809 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
810 " ReturnFromThisMethodUsed?=%X ResStack %p Walk %p\n",
811 WalkState->ReturnUsed,
812 WalkState->Results, WalkState));
814 /* Did the called method return a value? */
816 if (ReturnDesc)
818 /* Is the implicit return object the same as the return desc? */
820 SameAsImplicitReturn = (WalkState->ImplicitReturnObj == ReturnDesc);
822 /* Are we actually going to use the return value? */
824 if (WalkState->ReturnUsed)
826 /* Save the return value from the previous method */
828 Status = AcpiDsResultPush (ReturnDesc, WalkState);
829 if (ACPI_FAILURE (Status))
831 AcpiUtRemoveReference (ReturnDesc);
832 return_ACPI_STATUS (Status);
836 * Save as THIS method's return value in case it is returned
837 * immediately to yet another method
839 WalkState->ReturnDesc = ReturnDesc;
843 * The following code is the optional support for the so-called
844 * "implicit return". Some AML code assumes that the last value of the
845 * method is "implicitly" returned to the caller, in the absence of an
846 * explicit return value.
848 * Just save the last result of the method as the return value.
850 * NOTE: this is optional because the ASL language does not actually
851 * support this behavior.
853 else if (!AcpiDsDoImplicitReturn (ReturnDesc, WalkState, FALSE) ||
854 SameAsImplicitReturn)
857 * Delete the return value if it will not be used by the
858 * calling method or remove one reference if the explicit return
859 * is the same as the implicit return value.
861 AcpiUtRemoveReference (ReturnDesc);
865 return_ACPI_STATUS (AE_OK);
869 /*******************************************************************************
871 * FUNCTION: AcpiDsTerminateControlMethod
873 * PARAMETERS: MethodDesc - Method object
874 * WalkState - State associated with the method
876 * RETURN: None
878 * DESCRIPTION: Terminate a control method. Delete everything that the method
879 * created, delete all locals and arguments, and delete the parse
880 * tree if requested.
882 * MUTEX: Interpreter is locked
884 ******************************************************************************/
886 void
887 AcpiDsTerminateControlMethod (
888 ACPI_OPERAND_OBJECT *MethodDesc,
889 ACPI_WALK_STATE *WalkState)
892 ACPI_FUNCTION_TRACE_PTR (DsTerminateControlMethod, WalkState);
895 /* MethodDesc is required, WalkState is optional */
897 if (!MethodDesc)
899 return_VOID;
902 if (WalkState)
904 /* Delete all arguments and locals */
906 AcpiDsMethodDataDeleteAll (WalkState);
909 * Delete any namespace objects created anywhere within the
910 * namespace by the execution of this method. Unless:
911 * 1) This method is a module-level executable code method, in which
912 * case we want make the objects permanent.
913 * 2) There are other threads executing the method, in which case we
914 * will wait until the last thread has completed.
916 if (!(MethodDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL) &&
917 (MethodDesc->Method.ThreadCount == 1))
919 /* Delete any direct children of (created by) this method */
921 (void) AcpiExExitInterpreter ();
922 AcpiNsDeleteNamespaceSubtree (WalkState->MethodNode);
923 (void) AcpiExEnterInterpreter ();
926 * Delete any objects that were created by this method
927 * elsewhere in the namespace (if any were created).
928 * Use of the ACPI_METHOD_MODIFIED_NAMESPACE optimizes the
929 * deletion such that we don't have to perform an entire
930 * namespace walk for every control method execution.
932 if (MethodDesc->Method.InfoFlags & ACPI_METHOD_MODIFIED_NAMESPACE)
934 (void) AcpiExExitInterpreter ();
935 AcpiNsDeleteNamespaceByOwner (MethodDesc->Method.OwnerId);
936 (void) AcpiExEnterInterpreter ();
937 MethodDesc->Method.InfoFlags &=
938 ~ACPI_METHOD_MODIFIED_NAMESPACE;
943 * If method is serialized, release the mutex and restore the
944 * current sync level for this thread
946 if (MethodDesc->Method.Mutex)
948 /* Acquisition Depth handles recursive calls */
950 MethodDesc->Method.Mutex->Mutex.AcquisitionDepth--;
951 if (!MethodDesc->Method.Mutex->Mutex.AcquisitionDepth)
953 WalkState->Thread->CurrentSyncLevel =
954 MethodDesc->Method.Mutex->Mutex.OriginalSyncLevel;
956 AcpiOsReleaseMutex (
957 MethodDesc->Method.Mutex->Mutex.OsMutex);
958 MethodDesc->Method.Mutex->Mutex.ThreadId = 0;
963 /* Decrement the thread count on the method */
965 if (MethodDesc->Method.ThreadCount)
967 MethodDesc->Method.ThreadCount--;
969 else
971 ACPI_ERROR ((AE_INFO,
972 "Invalid zero thread count in method"));
975 /* Are there any other threads currently executing this method? */
977 if (MethodDesc->Method.ThreadCount)
980 * Additional threads. Do not release the OwnerId in this case,
981 * we immediately reuse it for the next thread executing this method
983 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
984 "*** Completed execution of one thread, %u threads remaining\n",
985 MethodDesc->Method.ThreadCount));
987 else
989 /* This is the only executing thread for this method */
992 * Support to dynamically change a method from NotSerialized to
993 * Serialized if it appears that the method is incorrectly written and
994 * does not support multiple thread execution. The best example of this
995 * is if such a method creates namespace objects and blocks. A second
996 * thread will fail with an AE_ALREADY_EXISTS exception.
998 * This code is here because we must wait until the last thread exits
999 * before marking the method as serialized.
1001 if (MethodDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED_PENDING)
1003 if (WalkState)
1005 ACPI_INFO ((
1006 "Marking method %4.4s as Serialized "
1007 "because of AE_ALREADY_EXISTS error",
1008 WalkState->MethodNode->Name.Ascii));
1012 * Method tried to create an object twice and was marked as
1013 * "pending serialized". The probable cause is that the method
1014 * cannot handle reentrancy.
1016 * The method was created as NotSerialized, but it tried to create
1017 * a named object and then blocked, causing the second thread
1018 * entrance to begin and then fail. Workaround this problem by
1019 * marking the method permanently as Serialized when the last
1020 * thread exits here.
1022 MethodDesc->Method.InfoFlags &=
1023 ~ACPI_METHOD_SERIALIZED_PENDING;
1025 MethodDesc->Method.InfoFlags |=
1026 (ACPI_METHOD_SERIALIZED | ACPI_METHOD_IGNORE_SYNC_LEVEL);
1027 MethodDesc->Method.SyncLevel = 0;
1030 /* No more threads, we can free the OwnerId */
1032 if (!(MethodDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL))
1034 AcpiUtReleaseOwnerId (&MethodDesc->Method.OwnerId);
1038 AcpiExStopTraceMethod ((ACPI_NAMESPACE_NODE *) MethodDesc->Method.Node,
1039 MethodDesc, WalkState);
1041 return_VOID;